try to keep alive longer

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

@ -124,28 +124,28 @@ void CQnetITAP::DumpSerialPacket(const char *title, const unsigned char *buf)
memcpy(&itap, buf, buf[0]); memcpy(&itap, buf, buf[0]);
switch (itap.type) switch (itap.type)
{ {
case 0x03U: //pong case 0x03U: //pong
printf("Pong\n"); printf("Pong\n");
break; break;
case 0x10U: // header case 0x10U: // header
case 0x20U: case 0x20U:
printf("Header ur=%8.8s r1=%8.8s r2=%8.8s my=%8.8s/%4.4s", itap.header.ur, itap.header.r1, itap.header.r2, itap.header.my, itap.header.nm); printf("Header ur=%8.8s r1=%8.8s r2=%8.8s my=%8.8s/%4.4s", itap.header.ur, itap.header.r1, itap.header.r2, itap.header.my, itap.header.nm);
break; break;
case 0x12U: // data case 0x12U: // data
case 0x22U: case 0x22U:
printf("Data count=%u seq=%u f=%02u%02u%02u a=%02u%02u%02u%02u%02u%02u%02u%02u%02u t=%02u%02u%02u\n", itap.voice.counter, itap.voice.sequence, itap.header.flag[0], itap.header.flag[1], itap.header.flag[2], itap.voice.ambe[0], itap.voice.ambe[1], itap.voice.ambe[2], itap.voice.ambe[3], itap.voice.ambe[4], itap.voice.ambe[5], itap.voice.ambe[6], itap.voice.ambe[7], itap.voice.ambe[8], itap.voice.text[0], itap.voice.text[1], itap.voice.text[2]); printf("Data count=%u seq=%u f=%02u%02u%02u a=%02u%02u%02u%02u%02u%02u%02u%02u%02u t=%02u%02u%02u\n", itap.voice.counter, itap.voice.sequence, itap.header.flag[0], itap.header.flag[1], itap.header.flag[2], itap.voice.ambe[0], itap.voice.ambe[1], itap.voice.ambe[2], itap.voice.ambe[3], itap.voice.ambe[4], itap.voice.ambe[5], itap.voice.ambe[6], itap.voice.ambe[7], itap.voice.ambe[8], itap.voice.text[0], itap.voice.text[1], itap.voice.text[2]);
break; break;
case 0x11U: // header acknowledgement case 0x11U: // header acknowledgement
case 0x21U: case 0x21U:
printf("Header acknowledgement code=%02u", itap.header.flag[0]); printf("Header acknowledgement code=%02u", itap.header.flag[0]);
break; break;
case 0x13U: // data acknowledgment case 0x13U: // data acknowledgment
case 0x23U: case 0x23U:
printf("Data acknowledgement seq=%02u code=%02u", itap.header.flag[0], itap.header.flag[1]); printf("Data acknowledgement seq=%02u code=%02u", itap.header.flag[0], itap.header.flag[1]);
break; break;
default: default:
printf("UNKNOWN packet buf[0] = 0x%02u\n", buf[0]); printf("UNKNOWN packet buf[0] = 0x%02u\n", buf[0]);
break; break;
} }
} }
@ -194,18 +194,18 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf)
switch (buf[1U]) switch (buf[1U])
{ {
case 0x03U: case 0x03U:
return RT_PONG; return RT_PONG;
case 0x10U: case 0x10U:
return RT_HEADER; return RT_HEADER;
case 0x12U: case 0x12U:
return RT_DATA; return RT_DATA;
case 0x21U: case 0x21U:
return RT_HEADER_ACK; return RT_HEADER_ACK;
case 0x23U: case 0x23U:
return RT_DATA_ACK; return RT_DATA_ACK;
default: default:
return RT_UNKNOWN; return RT_UNKNOWN;
} }
} }
@ -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,101 +257,100 @@ 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;
lastdataTimer.start(); lastdataTimer.start();
break; break;
case RT_DATA: case RT_DATA:
{
unsigned char ack_voice[4] = { 0x04U, 0x13U, 0x0U, 0x0U };
ack_voice[2] = buf[2];
SendTo(ack_voice);
}
if (ProcessITAP(buf))
keep_running = false;
lastdataTimer.start();
break;
case RT_PONG:
if (! is_alive)
{
if (LOG_DEBUG)
{ {
auto count = queue.size(); unsigned char ack_voice[4] = { 0x04U, 0x13U, 0x0U, 0x0U };
if (count) ack_voice[2] = buf[2];
printf("%u packets in queue. Icom radio is connected.", (unsigned int)count); SendToIcom(ack_voice);
}
if (ProcessITAP(buf))
keep_running = false;
lastdataTimer.start();
break;
case RT_PONG:
if (! initialized)
{
if (LOG_DEBUG)
{
auto count = queue.size();
if (count)
printf("%u packets in queue. Icom radio is connected.", (unsigned int)count);
}
else
printf("Icom Radio is connected.\n");
initialized = true;
}
lastdataTimer.start();
break;
case RT_HEADER_ACK:
if (acknowledged)
{
fprintf(stderr, "ERROR: Header already acknowledged!\n");
} }
else else
printf("Icom Radio is connected.\n"); {
is_alive = true; if (0x0U == buf[2])
} acknowledged = true;
lastdataTimer.start(); }
break; lastdataTimer.start();
case RT_HEADER_ACK: break;
if (acknowledged) case RT_DATA_ACK:
{ if (acknowledged)
fprintf(stderr, "ERROR: Header already acknowledged!\n"); {
} fprintf(stderr, "ERROR: voice frame %d already acknowledged!\n", (int)buf[2]);
else }
{ else
if (0x0U == buf[2]) {
acknowledged = true; if (0x0U == buf[3])
} acknowledged = true;
lastdataTimer.start(); }
break; lastdataTimer.start();
case RT_DATA_ACK: break;
if (acknowledged) case RT_TIMEOUT: // nothing or 0xff
{ break;
fprintf(stderr, "ERROR: voice frame %d already acknowledged!\n", (int)buf[2]); default:
} if (buf[0] != 255)
else DumpSerialPacket("GetITAPData returned", buf);
{ break;
if (0x0U == buf[3])
acknowledged = true;
}
lastdataTimer.start();
break;
case RT_TIMEOUT: // nothing or 0xff
break;
default:
if (buf[0] != 255)
DumpSerialPacket("GetITAPData returned", buf);
break;
} }
FD_CLR(serfd, &readfds); FD_CLR(serfd, &readfds);
} }
@ -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,33 +396,37 @@ 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");
close(serfd); alive = false;
poll_counter = 0;
pingtime = 0.001;
is_alive = false;
acknowledged = true;
lastdataTimer.start();
pingTimer.start();
serfd = OpenITAP();
if (serfd < 0)
{
keep_running = false;
}
else
{
while (! queue.empty())
queue.pop();
}
} }
} }
} }
if (! alive)
{
close(serfd);
poll_counter = 0;
pingtime = 0.001;
initialized = false;
alive = acknowledged = true;
lastdataTimer.start();
pingTimer.start();
serfd = OpenITAP();
if (serfd < 0)
{
keep_running = false;
}
else
{
while (! queue.empty())
queue.pop();
}
}
} }
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.