try to keep alive longer

dev
Tom Early 5 years ago
parent 7e3947438e
commit 94e079f9c4

@ -223,7 +223,8 @@ void CQnetITAP::Run(const char *cfgfile)
keep_running = true; keep_running = true;
unsigned int poll_counter = 0; unsigned int poll_counter = 0;
bool is_alive = false; bool initialized = false;
bool alive = true;
acknowledged = true; acknowledged = true;
CTimer ackTimer; CTimer ackTimer;
CTimer lastdataTimer; CTimer lastdataTimer;
@ -256,41 +257,40 @@ void CQnetITAP::Run(const char *cfgfile)
// check for a dead or disconnected radio // check for a dead or disconnected radio
if (10.0 < lastdataTimer.time()) if (10.0 < lastdataTimer.time())
{ {
printf("no activity from radio for 10 sec. Exiting...\n"); printf("no activity from radio for 10 sec. Restarting...\n");
break; alive = false;
} }
if (pingTimer.time() >= pingtime) if (alive && keep_running && (pingTimer.time() >= pingtime))
{ {
if (poll_counter < 18 ) if (poll_counter < 18 )
{ {
const unsigned char poll[2] = { 0xffu, 0xffu }; const unsigned char poll[2] = { 0xffu, 0xffu };
SendTo(poll); SendToIcom(poll);
if (poll_counter == 17) if (poll_counter++ == 17)
pingtime = 1.0; pingtime = 1.0;
} }
else else
{ {
const unsigned char ping[2] = { 0x02u, 0x02u }; const unsigned char ping[2] = { 0x02u, 0x02u };
SendTo(ping); SendToIcom(ping);
} }
poll_counter++;
pingTimer.start(); pingTimer.start();
} }
unsigned char buf[100]; unsigned char buf[100];
if (keep_running && FD_ISSET(serfd, &readfds)) // there is something to read! if (keep_running && FD_ISSET(serfd, &readfds)) // there is something to read from the Icom!
{ {
switch (GetITAPData(buf)) switch (GetITAPData(buf))
{ {
case RT_ERROR: case RT_ERROR:
keep_running = false; alive = false;
break; break;
case RT_HEADER: case RT_HEADER:
{ {
unsigned char ack_header[3] = { 0x03U, 0x11U, 0x0U }; unsigned char ack_header[3] = { 0x03U, 0x11U, 0x0U };
SendTo(ack_header); SendToIcom(ack_header);
} }
if (ProcessITAP(buf)) if (ProcessITAP(buf))
keep_running = false; keep_running = false;
@ -300,14 +300,14 @@ void CQnetITAP::Run(const char *cfgfile)
{ {
unsigned char ack_voice[4] = { 0x04U, 0x13U, 0x0U, 0x0U }; unsigned char ack_voice[4] = { 0x04U, 0x13U, 0x0U, 0x0U };
ack_voice[2] = buf[2]; ack_voice[2] = buf[2];
SendTo(ack_voice); SendToIcom(ack_voice);
} }
if (ProcessITAP(buf)) if (ProcessITAP(buf))
keep_running = false; keep_running = false;
lastdataTimer.start(); lastdataTimer.start();
break; break;
case RT_PONG: case RT_PONG:
if (! is_alive) if (! initialized)
{ {
if (LOG_DEBUG) if (LOG_DEBUG)
{ {
@ -317,7 +317,7 @@ void CQnetITAP::Run(const char *cfgfile)
} }
else else
printf("Icom Radio is connected.\n"); printf("Icom Radio is connected.\n");
is_alive = true; initialized = true;
} }
lastdataTimer.start(); lastdataTimer.start();
break; break;
@ -365,7 +365,7 @@ void CQnetITAP::Run(const char *cfgfile)
break; break;
} }
if (0 == memcmp(buf, "DSVT", 4)) if (alive && (0 == memcmp(buf, "DSVT", 4)))
{ {
//printf("read %d bytes from QnetGateway\n", (int)len); //printf("read %d bytes from QnetGateway\n", (int)len);
if (ProcessGateway(len, buf)) if (ProcessGateway(len, buf))
@ -379,13 +379,13 @@ void CQnetITAP::Run(const char *cfgfile)
{ {
if (acknowledged) if (acknowledged)
{ {
if (is_alive) if (initialized)
{ {
if (! queue.empty()) if (! queue.empty())
{ {
CFrame frame = queue.front(); CFrame frame = queue.front();
queue.pop(); queue.pop();
SendTo(frame.data()); SendToIcom(frame.data());
ackTimer.start(); ackTimer.start();
acknowledged = false; acknowledged = false;
} }
@ -396,11 +396,17 @@ void CQnetITAP::Run(const char *cfgfile)
if (ackTimer.time() >= ackwait) if (ackTimer.time() >= ackwait)
{ {
fprintf(stderr, "Icom failure suspected, restarting...\n"); fprintf(stderr, "Icom failure suspected, restarting...\n");
alive = false;
}
}
}
if (! alive)
{
close(serfd); close(serfd);
poll_counter = 0; poll_counter = 0;
pingtime = 0.001; pingtime = 0.001;
is_alive = false; initialized = false;
acknowledged = true; alive = acknowledged = true;
lastdataTimer.start(); lastdataTimer.start();
pingTimer.start(); pingTimer.start();
serfd = OpenITAP(); serfd = OpenITAP();
@ -415,14 +421,12 @@ void CQnetITAP::Run(const char *cfgfile)
} }
} }
} }
}
}
close(serfd); close(serfd);
ToGate.Close(); ToGate.Close();
} }
int CQnetITAP::SendTo(const unsigned char *buf) void CQnetITAP::SendToIcom(const unsigned char *buf)
{ {
ssize_t n; ssize_t n;
unsigned int ptr = 0; unsigned int ptr = 0;
@ -436,7 +440,7 @@ int CQnetITAP::SendTo(const unsigned char *buf)
if (EAGAIN != errno) if (EAGAIN != errno)
{ {
printf("Error %d writing to %s: %s\n", errno, ITAP_DEVICE.c_str(), strerror(errno)); printf("Error %d writing to %s: %s\n", errno, ITAP_DEVICE.c_str(), strerror(errno));
return -1; return;
} }
} }
ptr += n; ptr += n;
@ -452,12 +456,9 @@ int CQnetITAP::SendTo(const unsigned char *buf)
if (EAGAIN != errno) if (EAGAIN != errno)
{ {
printf("Error %d writing to %s: %s\n", errno, ITAP_DEVICE.c_str(), strerror(errno)); printf("Error %d writing to %s: %s\n", errno, ITAP_DEVICE.c_str(), strerror(errno));
return -1;
} }
} }
} }
return length;
} }
bool CQnetITAP::ProcessGateway(const int len, const unsigned char *raw) bool CQnetITAP::ProcessGateway(const int len, const unsigned char *raw)

@ -122,7 +122,7 @@ private:
bool ProcessGateway(const int len, const unsigned char *raw); bool ProcessGateway(const int len, const unsigned char *raw);
bool ProcessITAP(const unsigned char *raw); bool ProcessITAP(const unsigned char *raw);
int OpenITAP(); int OpenITAP();
int SendTo(const unsigned char *buf); void SendToIcom(const unsigned char *buf);
REPLY_TYPE GetITAPData(unsigned char *buf); REPLY_TYPE GetITAPData(unsigned char *buf);
void calcPFCS(const unsigned char *packet, unsigned char *pfcs); void calcPFCS(const unsigned char *packet, unsigned char *pfcs);
void DumpSerialPacket(const char *title, const unsigned char *); void DumpSerialPacket(const char *title, const unsigned char *);

Loading…
Cancel
Save

Powered by TurnKey Linux.