From 110a70a27af2df74e93e695c785e120e037abcf6 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Fri, 24 Jul 2020 06:33:54 -0700 Subject: [PATCH] astyle -A1 -T -O --- CacheManager.cpp | 24 +- CacheManager.h | 3 +- DPlusAuthenticator.cpp | 32 +- DPlusAuthenticator.h | 3 +- DStarDecode.cpp | 48 +- DStarDecode.h | 3 +- DVAPDongle.cpp | 401 ++++---- DVAPDongle.h | 89 +- HostQueue.h | 3 +- KRBase.cpp | 19 +- Location.cpp | 21 +- QnetConfigure.cpp | 103 ++- QnetConfigure.h | 13 +- QnetDB.cpp | 143 +-- QnetDB.h | 12 +- QnetDVAP.cpp | 447 +++++---- QnetDVAP.h | 3 +- QnetDVRPTR.cpp | 517 +++++++---- QnetGateway.cpp | 1775 ++++++++++++++++++++++-------------- QnetGateway.h | 20 +- QnetITAP.cpp | 442 +++++---- QnetITAP.h | 43 +- QnetLink.cpp | 1614 ++++++++++++++++++++------------ QnetLink.h | 56 +- QnetModem.cpp | 446 +++++---- QnetModem.h | 74 +- QnetRelay.cpp | 176 ++-- QnetRemote.cpp | 183 ++-- QnetTypeDefs.h | 71 +- QnetVoice.cpp | 25 +- SEcho.h | 5 +- SockAddress.h | 91 +- TCPReaderWriterClient.cpp | 78 +- TCPReaderWriterClient.h | 3 +- Timer.h | 6 +- UDPSocket.cpp | 15 +- UnixDgramSocket.cpp | 21 +- UnixPacketSock.cpp | 60 +- UnixPacketSock.h | 9 +- Utilities.h | 48 +- aprs.cpp | 203 +++-- aprs.h | 18 +- ircddb/IRCClient.cpp | 228 ++--- ircddb/IRCDDB.cpp | 101 +- ircddb/IRCDDB.h | 6 +- ircddb/IRCDDBApp.cpp | 250 +++-- ircddb/IRCMessage.cpp | 31 +- ircddb/IRCMessageQueue.cpp | 11 +- ircddb/IRCProtocol.cpp | 180 ++-- ircddb/IRCReceiver.cpp | 85 +- ircddb/IRCReceiver.h | 2 +- ircddb/IRCutils.cpp | 22 +- 52 files changed, 5190 insertions(+), 3092 deletions(-) diff --git a/CacheManager.cpp b/CacheManager.cpp index 35717d9..267f11f 100644 --- a/CacheManager.cpp +++ b/CacheManager.cpp @@ -90,8 +90,10 @@ std::string CCacheManager::findServerUser() { std::string suser; mux.lock(); - for (auto it=NameNick.begin(); it!=NameNick.end(); it++) { - if (0 == it->first.compare(0, 2, "s-")) { + for (auto it=NameNick.begin(); it!=NameNick.end(); it++) + { + if (0 == it->first.compare(0, 2, "s-")) + { suser.assign(it->first); break; } @@ -109,14 +111,16 @@ void CCacheManager::updateUser(const std::string &user, const std::string &rptr, if (! time.empty()) UserTime[user] = time; - if (rptr.empty()) { + if (rptr.empty()) + { mux.unlock(); return; } UserRptr[user] = rptr; - if (gate.empty() || addr.empty()) { + if (gate.empty() || addr.empty()) + { mux.unlock(); return; } @@ -135,7 +139,8 @@ void CCacheManager::updateRptr(const std::string &rptr, const std::string &gate, mux.lock(); RptrGate[rptr] = gate; - if (addr.empty()) { + if (addr.empty()) + { mux.unlock(); return; } @@ -149,7 +154,8 @@ void CCacheManager::updateGate(const std::string &G, const std::string &addr) return; std::string gate(G); auto p = gate.find('_'); - while (gate.npos != p) { + while (gate.npos != p) + { gate[p] = ' '; p = gate.find('_'); } @@ -207,10 +213,12 @@ std::string CCacheManager::findRptrGate(const std::string &rptr) if (rptr.empty()) return gate; auto it = RptrGate.find(rptr); - if (it == RptrGate.end()) { + if (it == RptrGate.end()) + { gate.assign(rptr); gate[7] = 'G'; - } else + } + else gate.assign(it->second); return gate; } diff --git a/CacheManager.h b/CacheManager.h index 048b2a9..cf9b931 100644 --- a/CacheManager.h +++ b/CacheManager.h @@ -22,7 +22,8 @@ #include #include -class CCacheManager { +class CCacheManager +{ public: CCacheManager() {} ~CCacheManager() {} diff --git a/DPlusAuthenticator.cpp b/DPlusAuthenticator.cpp index 9ea9393..e9f45ab 100644 --- a/DPlusAuthenticator.cpp +++ b/DPlusAuthenticator.cpp @@ -32,8 +32,8 @@ #include "Utilities.h" CDPlusAuthenticator::CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address) : -m_loginCallsign(loginCallsign), -m_address(address) + m_loginCallsign(loginCallsign), + m_address(address) { assert(loginCallsign.size()); @@ -48,7 +48,8 @@ int CDPlusAuthenticator::Process(CQnetDB &db, const bool reflectors, const bool // return true if everything went okay { int result = client.Open(m_address, AF_UNSPEC, "20001"); - if (result) { + if (result) + { fprintf(stderr, "DPlus Authorization failed: %s\n", gai_strerror(result)); return 0; } @@ -70,7 +71,8 @@ int CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const ::memcpy(buffer+28, "W7IB2", 5); ::memcpy(buffer+40, "DHS0257", 7); - if (client.Write(buffer, 56U)) { + if (client.Write(buffer, 56U)) + { fprintf(stderr, "ERROR: could not write opening phrase\n"); client.Close(); return 0; @@ -80,21 +82,25 @@ int CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const unsigned int rval = 0; CHostQueue hqueue; - while (ret == 2) { + while (ret == 2) + { unsigned int len = (buffer[1U] & 0x0FU) * 256U + buffer[0U]; // Ensure that we get exactly len - 2U bytes from the TCP stream ret = client.ReadExact(buffer + 2U, len - 2U); - if (0 > ret) { + if (0 > ret) + { fprintf(stderr, "Problem reading line, it returned %d\n", errno); return rval; } - if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) { + if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) + { fprintf(stderr, "Invalid packet received from 20001\n"); return rval; } - for (unsigned int i = 8U; (i + 25U) < len; i += 26U) { + for (unsigned int i = 8U; (i + 25U) < len; i += 26U) + { std::string address((char *)(buffer + i)); std::string name((char *)(buffer + i + 16U)); @@ -106,11 +112,15 @@ int CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const bool active = (buffer[i + 25U] & 0x80U) == 0x80U; // An empty name or IP address or an inactive gateway/reflector is not added - if (address.size()>0U && name.size()>0U && active) { - if (reflectors && 0==name.compare(0, 3, "REF")) { + if (address.size()>0U && name.size()>0U && active) + { + if (reflectors && 0==name.compare(0, 3, "REF")) + { rval++; hqueue.Push(CHost(name.c_str(), address.c_str(), 20001)); - } else if (repeaters && name.compare(0, 3, "REF")) { + } + else if (repeaters && name.compare(0, 3, "REF")) + { rval++; hqueue.Push(CHost(name.c_str(), address.c_str(), 20001)); } diff --git a/DPlusAuthenticator.h b/DPlusAuthenticator.h index b34b8c6..7f972af 100644 --- a/DPlusAuthenticator.h +++ b/DPlusAuthenticator.h @@ -25,7 +25,8 @@ #include "TCPReaderWriterClient.h" #include "QnetDB.h" -class CDPlusAuthenticator { +class CDPlusAuthenticator +{ public: CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address); ~CDPlusAuthenticator(); diff --git a/DStarDecode.cpp b/DStarDecode.cpp index bbcc48e..2a2ddae 100644 --- a/DStarDecode.cpp +++ b/DStarDecode.cpp @@ -26,7 +26,8 @@ #define MASK12 0xfffff800 /* auxiliary vector for testing */ #define GENPOL 0x00000c75 /* generator polinomial, g(x) */ -static const int bit_pos1[] = { +static const int bit_pos1[] = +{ 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, @@ -42,7 +43,8 @@ static const int bit_pos1[] = { 0, 0, 1, 1, 2, 2 }; -static const int bit_pos2[] = { +static const int bit_pos2[] = +{ 23, 11, 23, 11, 23, 11, 22, 10, 22, 10, 22, 10, 21, 9, 21, 9, 21, 9, @@ -66,7 +68,8 @@ CDStarDecode::CDStarDecode(void) decoding_table[0] = 0; decoding_table[1] = 1; temp = 1; - for (i=2; i<= 23; i++) { + for (i=2; i<= 23; i++) + { temp = temp << 1; decoding_table[get_syndrome(temp)] = temp; } @@ -75,7 +78,8 @@ CDStarDecode::CDStarDecode(void) a[2] = 2; temp = arr2int(a,2); decoding_table[get_syndrome(temp)] = temp; - for (i=1; i<253; i++) { + for (i=1; i<253; i++) + { nextcomb(23,2,a); temp = arr2int(a,2); decoding_table[get_syndrome(temp)] = temp; @@ -86,13 +90,15 @@ CDStarDecode::CDStarDecode(void) a[3] = 3; temp = arr2int(a,3); decoding_table[get_syndrome(temp)] = temp; - for (i=1; i<1771; i++) { + for (i=1; i<1771; i++) + { nextcomb(23,3,a); temp = arr2int(a,3); decoding_table[get_syndrome(temp)] = temp; } - for (i=0; i < 4096; i++) { + for (i=0; i < 4096; i++) + { int mask = 0x800000; int j; int pr; @@ -100,10 +106,12 @@ CDStarDecode::CDStarDecode(void) prng[i] = 0; pr = i << 4; - for (j=0; j < 24; j++) { + for (j=0; j < 24; j++) + { pr = ((173 * pr) + 13849) & 0xFFFF; - if ((pr & 0x8000) != 0) { + if ((pr & 0x8000) != 0) + { prng[i] |= mask; } @@ -121,7 +129,8 @@ long CDStarDecode::arr2int(int *a, int r) int i; long mul, result = 0, temp; - for (i=1; i<=r; i++) { + for (i=1; i<=r; i++) + { mul = 1; temp = a[i]-1; while (temp--) @@ -164,7 +173,8 @@ long CDStarDecode::get_syndrome(long pattern) long aux = X22; if (pattern >= X11) - while (pattern & MASK12) { + while (pattern & MASK12) + { while (!(aux & pattern)) aux = aux >> 1; pattern ^= (aux/X11) * GENPOL; @@ -181,22 +191,26 @@ int CDStarDecode::golay2412(int data, int *decoded) int parity_corr = 0; int i; - for (i = 0; i < 23; i++) { + for (i = 0; i < 23; i++) + { int mask = 1 << i; int bit_rcvd = block & mask; int bit_corr = corrected_block & mask; - if (bit_corr != 0) { + if (bit_corr != 0) + { parity_corr ++; } - if (bit_rcvd != bit_corr) { + if (bit_rcvd != bit_corr) + { errs ++; } } - if ((parity_corr & 0x01) != (data & 0x01)) { + if ((parity_corr & 0x01) != (data & 0x01)) + { errs ++; } @@ -211,11 +225,13 @@ int CDStarDecode::Decode(const unsigned char *d, int data[3]) int i; int errs; - for (i=0; i < 3; i++) { + for (i=0; i < 3; i++) + { bits[i] = 0; } - for (i=0; i < 72; i++) { + for (i=0; i < 72; i++) + { bits[ bit_pos1[i] ] |= (d[ i >> 3 ] & (0x80 >> (i & 0x07))) ? (1 << bit_pos2[i]) : 0; } diff --git a/DStarDecode.h b/DStarDecode.h index 11aa462..13a2e57 100644 --- a/DStarDecode.h +++ b/DStarDecode.h @@ -20,7 +20,8 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -class CDStarDecode { +class CDStarDecode +{ public: CDStarDecode(); ~CDStarDecode() {} diff --git a/DVAPDongle.cpp b/DVAPDongle.cpp index 74c1898..27163af 100644 --- a/DVAPDongle.cpp +++ b/DVAPDongle.cpp @@ -41,8 +41,10 @@ bool CDVAPDongle::Initialize(const char *serialno, const int frequency, const in bool ok = false; char device[128]; - do { - for (int i = 0; i < 32; i++) { + do + { + for (int i = 0; i < 32; i++) + { sprintf(device, "/dev/ttyUSB%d", i); if (access(device, R_OK | W_OK) != 0) @@ -52,7 +54,8 @@ bool CDVAPDongle::Initialize(const char *serialno, const int frequency, const in if (!ok) continue; - if (flock(serfd, LOCK_EX | LOCK_NB) != 0) { + if (flock(serfd, LOCK_EX | LOCK_NB) != 0) + { close(serfd); serfd = -1; ok = false; @@ -62,7 +65,8 @@ bool CDVAPDongle::Initialize(const char *serialno, const int frequency, const in printf("Device %s now locked for exclusive use\n", device); ok = get_ser(device, serialno); - if (!ok) { + if (!ok) + { close(serfd); serfd = -1; continue; @@ -109,10 +113,13 @@ bool CDVAPDongle::Initialize(const char *serialno, const int frequency, const in if (!ok) break; - } while (false); + } + while (false); - if (!ok) { - if (serfd != -1) { + if (!ok) + { + if (serfd != -1) + { Stop(); close(serfd); serfd = -1; @@ -132,29 +139,31 @@ REPLY_TYPE CDVAPDongle::GetReply(SDVAP_REGISTER &dr) if (rc != 2) return RT_ERR; - switch (dr.header) { - case 0x5u: - case 0x6u: - case 0x7u: - case 0x8u: - case 0xcu: - case 0xdu: - case 0x10u: - case 0x2005u: - case 0x2007u: - case 0x602fu: - case 0xa02fu: - case 0xc012u: - break; // these are all expected headers - default: - printf("unknown header=0x%d\n", (unsigned)dr.header); - if (syncit()) - return RT_ERR; - return RT_TIMEOUT; + switch (dr.header) + { + case 0x5u: + case 0x6u: + case 0x7u: + case 0x8u: + case 0xcu: + case 0xdu: + case 0x10u: + case 0x2005u: + case 0x2007u: + case 0x602fu: + case 0xa02fu: + case 0xc012u: + break; // these are all expected headers + default: + printf("unknown header=0x%d\n", (unsigned)dr.header); + if (syncit()) + return RT_ERR; + return RT_TIMEOUT; } // read the rest of the register uint16_t len = dr.header & 0x1fff; - while (off < len) { + while (off < len) + { uint8_t *ptr = (uint8_t *)&dr; rc = read_from_dvp(ptr + off, len - off); if (rc < 0) @@ -163,68 +172,71 @@ REPLY_TYPE CDVAPDongle::GetReply(SDVAP_REGISTER &dr) off += rc; } // okay, now we'll parse the register and return its type - switch (dr.header) { - case 0x5u: - switch (dr.param.control) { - case 0x18u: - if (dr.param.byte) - return RT_START; - else - return RT_STOP; - case 0x28u: - if (0x1u==dr.param.ustr[0]) - return RT_MODU; - break; - case 0x80u: - return RT_SQL; - case 0x2au: - if (0x0u==dr.param.ustr[0]) - return RT_MODE; - break; - } - break; - case 0x6u: - switch (dr.param.control) { - case 0x138u: - return RT_PWR; - case 0x400u: - return RT_OFF; - } - break; - case 0x7u: - if (0x4u==dr.param.control && 0x1u==dr.param.ustr[0]) - return RT_FW; - break; - case 0x8u: - if (0x220u==dr.param.control) - return RT_FREQ; + switch (dr.header) + { + case 0x5u: + switch (dr.param.control) + { + case 0x18u: + if (dr.param.byte) + return RT_START; + else + return RT_STOP; + case 0x28u: + if (0x1u==dr.param.ustr[0]) + return RT_MODU; break; - case 0xcu: - if (0x230u==dr.param.control) - return RT_FREQ_LIMIT; + case 0x80u: + return RT_SQL; + case 0x2au: + if (0x0u==dr.param.ustr[0]) + return RT_MODE; break; - case 0xdu: - if (0x2u==dr.param.control) - return RT_SER; - break; - case 0x10u: - if (0x1u==dr.param.control) - return RT_NAME; - break; - case 0x2005u: - if (0x118u==dr.param.control) - return RT_PTT; - break; - case 0x2007u: - if (0x90u==dr.param.control) - return RT_STS; - break; - case 0x602fu: - return RT_HDR_ACK; - case 0xa02fu: - return RT_HDR; - case 0xc012u: - return RT_DAT; + } + break; + case 0x6u: + switch (dr.param.control) + { + case 0x138u: + return RT_PWR; + case 0x400u: + return RT_OFF; + } + break; + case 0x7u: + if (0x4u==dr.param.control && 0x1u==dr.param.ustr[0]) + return RT_FW; + break; + case 0x8u: + if (0x220u==dr.param.control) + return RT_FREQ; + break; + case 0xcu: + if (0x230u==dr.param.control) + return RT_FREQ_LIMIT; + break; + case 0xdu: + if (0x2u==dr.param.control) + return RT_SER; + break; + case 0x10u: + if (0x1u==dr.param.control) + return RT_NAME; + break; + case 0x2005u: + if (0x118u==dr.param.control) + return RT_PTT; + break; + case 0x2007u: + if (0x90u==dr.param.control) + return RT_STS; + break; + case 0x602fu: + return RT_HDR_ACK; + case 0xa02fu: + return RT_HDR; + case 0xc012u: + return RT_DAT; } printf("Unrecognized data from dvap: header=%#x control=%#x\n", (unsigned)dr.header, (unsigned)dr.param.control); if (syncit()) @@ -244,22 +256,28 @@ bool CDVAPDongle::syncit() dvapreg.header = 0x2007u; dvapreg.param.control = 0x90u; - while (memcmp(data, &dvapreg, 4) != 0) { + while (memcmp(data, &dvapreg, 4) != 0) + { FD_ZERO(&fds); FD_SET(serfd, &fds); tv.tv_sec = 0; tv.tv_usec = 1000; int n = select(serfd + 1, &fds, NULL, NULL, &tv); - if (n <= 0) { + if (n <= 0) + { cnt ++; - if (cnt > 100) { + if (cnt > 100) + { printf("syncit() uncessful...stopping\n"); return true; } - } else { + } + else + { unsigned char c; n = read_from_dvp(&c, 1); - if (n > 0) { + if (n > 0) + { data[0] = data[1]; data[1] = data[2]; data[2] = data[3]; @@ -284,23 +302,28 @@ bool CDVAPDongle::get_ser(const char *dvp, const char *dvap_serial_number) dvapreg.param.control = 0x2u; int rc = write_to_dvp(&dvapreg, 4); - if (rc != 4) { + if (rc != 4) + { printf("Failed to send request to get dvap serial#\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to receive dvap serial#\n"); return false; } - } while (reply != RT_SER); + } + while (reply != RT_SER); - if (0 == strcmp(dvapreg.param.sstr, dvap_serial_number)) { + if (0 == strcmp(dvapreg.param.sstr, dvap_serial_number)) + { printf("Using %s: %s, because serial number matches your configuration file\n", dvp, dvap_serial_number); return true; } @@ -316,23 +339,28 @@ bool CDVAPDongle::get_name() dvapreg.param.control = 0x1u; int rc = write_to_dvp(&dvapreg, 4); - if (rc != 4) { + if (rc != 4) + { printf("Failed to send request to get dvap name\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to receive dvap name\n"); return false; } - } while (reply != RT_NAME); + } + while (reply != RT_NAME); - if (0x10u!=dvapreg.header || 0x1u!=dvapreg.param.control || strncmp(dvapreg.param.sstr, "DVAP Dongle", 11)) { + if (0x10u!=dvapreg.header || 0x1u!=dvapreg.param.control || strncmp(dvapreg.param.sstr, "DVAP Dongle", 11)) + { printf("Failed to receive dvap name, got %s\n", dvapreg.param.sstr); return false; } @@ -350,21 +378,25 @@ bool CDVAPDongle::get_fw() dvapreg.param.ustr[0] = 0x1u; int rc = write_to_dvp(&dvapreg, 5); - if (rc != 5) { + if (rc != 5) + { printf("Failed to send request to get dvap fw\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to receive dvap fw\n"); return false; } - } while (reply != RT_FW); + } + while (reply != RT_FW); unsigned int ver = dvapreg.param.ustr[1] + 256 * dvapreg.param.ustr[2]; printf("dvap fw ver: %u.%u\n", ver / 100, ver % 100); @@ -381,22 +413,26 @@ bool CDVAPDongle::set_modu() dvapreg.param.ustr[0] = 0x1u; int rc = write_to_dvp(&dvapreg, 5); - if (rc != 5) { + if (rc != 5) + { printf("Failed to send request to set dvap modulation\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap modulation\n"); return false; } - } while (reply != RT_MODU); + } + while (reply != RT_MODU); return true; } @@ -410,22 +446,26 @@ bool CDVAPDongle::set_mode() dvapreg.param.ustr[0] = 0x0u; int rc = write_to_dvp(&dvapreg, 5); - if (rc != 5) { + if (rc != 5) + { printf("Failed to send request to set dvap mode\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap mode\n"); return false; } - } while (reply != RT_MODE); + } + while (reply != RT_MODE); return true; } @@ -437,32 +477,39 @@ bool CDVAPDongle::set_sql(int squelch) dvapreg.header = 0x5u; dvapreg.param.control = 0x80u; - if (squelch < -128) { + if (squelch < -128) + { printf("Squelch setting of %d too small, resetting...\n", squelch); squelch = -128; - } else if (squelch > -45) { + } + else if (squelch > -45) + { printf("Squelch setting of %d too large, resetting...\n", squelch); squelch = -45; } dvapreg.param.byte = (int8_t)squelch; int rc = write_to_dvp(&dvapreg, 5); - if (rc != 5) { + if (rc != 5) + { printf("Failed to send request to set dvap sql\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap sql\n"); return false; } - } while (reply != RT_SQL); + } + while (reply != RT_SQL); printf("DVAP squelch is %d dB\n", (int)dvapreg.param.byte); return true; } @@ -474,32 +521,39 @@ bool CDVAPDongle::set_pwr(int power) dvapreg.header = 0x6u; dvapreg.param.control = 0x138u; - if (power < -12) { + if (power < -12) + { printf("Power setting of %d is too low, resetting...\n", power); power = -12; - } else if (power > 10) { + } + else if (power > 10) + { printf("Power setting of %d is too high, resetting...\n", power); power = 10; } dvapreg.param.word = (int16_t)power; int rc = write_to_dvp(&dvapreg, 6); - if (rc != 6) { + if (rc != 6) + { printf("Failed to send request to set dvap pwr\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap pwr\n"); return false; } - } while (reply != RT_PWR); + } + while (reply != RT_PWR); printf("DVAP power is %d dB\n", (int)dvapreg.param.word); return true; } @@ -511,32 +565,39 @@ bool CDVAPDongle::set_off(int offset) dvapreg.header = 0x6u; dvapreg.param.control = 0x400u; - if (offset < -2000) { + if (offset < -2000) + { printf("Offset of %d is too low, resetting...\n", offset); offset = -2000; - } else if (offset > 2000) { + } + else if (offset > 2000) + { printf("Offset of %d is too high, resetting...\n", offset); offset = 2000; } dvapreg.param.word = (int16_t)offset; int rc = write_to_dvp(&dvapreg, 6); - if (rc != 6) { + if (rc != 6) + { printf("Failed to send request to set dvap offset\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap offset\n"); return false; } - } while (reply != RT_OFF); + } + while (reply != RT_OFF); printf("DVAP offset is %d Hz\n", (int)dvapreg.param.word); return true; } @@ -551,29 +612,36 @@ bool CDVAPDongle::set_freq(int frequency) dvapreg.param.control = 0x230u; int rc = write_to_dvp(&dvapreg, 4); - if (rc != 4) { + if (rc != 4) + { printf("Failed to send request for frequency limits\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests for dvap frequency limits\n"); return false; } - } while (reply != RT_FREQ_LIMIT); + } + while (reply != RT_FREQ_LIMIT); printf("DVAP Frequency limits are from %d to %d Hz\n", dvapreg.param.twod[0], dvapreg.param.twod[1]); // okay, now we know the frequency limits, get on with the show... - if (frequency < dvapreg.param.twod[0]) { + if (frequency < dvapreg.param.twod[0]) + { printf("Frequency of %d is too small, resetting...\n", frequency); frequency = dvapreg.param.twod[0]; - } else if (frequency > dvapreg.param.twod[1]) { + } + else if (frequency > dvapreg.param.twod[1]) + { printf("Frequency of %d is too large, resetting...\n", frequency); frequency = dvapreg.param.twod[1]; } @@ -584,22 +652,26 @@ bool CDVAPDongle::set_freq(int frequency) dvapreg.param.dword = frequency; rc = write_to_dvp(&dvapreg, 8); - if (rc != 8) { + if (rc != 8) + { printf("Failed to send request to set dvap frequency\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to set dvap frequency\n"); return false; } - } while (reply != RT_FREQ); + } + while (reply != RT_FREQ); printf("DVAP frequency is %d Hz\n", dvapreg.param.dword); return true; } @@ -614,22 +686,26 @@ bool CDVAPDongle::start_dvap() dvapreg.param.byte = 0x1; int rc = write_to_dvp(&dvapreg, 5); - if (rc != 5) { + if (rc != 5) + { printf("Failed to send request to start the dvap dongle\n"); return false; } - do { + do + { usleep(5000); reply = GetReply(dvapreg); cnt ++; - if (cnt >= MAX_REPL_CNT) { + if (cnt >= MAX_REPL_CNT) + { printf("Reached max number of requests to start the dvap dongle\n"); return false; } - } while (reply != RT_START); + } + while (reply != RT_START); return true; } @@ -649,9 +725,11 @@ int CDVAPDongle::write_to_dvp(const void *buffer, const unsigned int len) return 0; uint8_t *buf = (uint8_t *)buffer; - while (ptr < len) { + while (ptr < len) + { ssize_t n = write(serfd, buf + ptr, len - ptr); - if (n < 0) { + if (n < 0) + { printf("Error %d writing to dvap, message=%s\n", errno, strerror(errno)); return -1; } @@ -675,25 +753,30 @@ int CDVAPDongle::read_from_dvp(void *buffer, unsigned int len) if (len == 0) return 0; - while (off < len) { + while (off < len) + { FD_ZERO(&fds); FD_SET(serfd, &fds); - if (off == 0) { + if (off == 0) + { tv.tv_sec = 0; tv.tv_usec = 0; n = select(serfd + 1, &fds, NULL, NULL, &tv); if (n == 0) return 0; // nothing to read from the dvap - } else + } + else n = select(serfd + 1, &fds, NULL, NULL, NULL); - if (n < 0) { + if (n < 0) + { printf("select error=%d on dvap\n", errno); return -1; } - if (n > 0) { + if (n > 0) + { temp_len = read(serfd, buf + off, len - off); if (temp_len > 0) off += temp_len; @@ -708,19 +791,22 @@ bool CDVAPDongle::OpenSerial(char *device) static termios t; serfd = open(device, O_RDWR | O_NOCTTY | O_NDELAY, 0); - if (serfd < 0) { + if (serfd < 0) + { printf("Failed to open device [%s], error=%d, message=%s\n", device, errno, strerror(errno)); return false; } - if (isatty(serfd) == 0) { + if (isatty(serfd) == 0) + { printf("Device %s is not a tty device\n", device); close(serfd); serfd = -1; return false; } - if (tcgetattr(serfd, &t) < 0) { + if (tcgetattr(serfd, &t) < 0) + { printf("tcgetattr failed for %s, error=%d, message-%s\n", device, errno, strerror(errno)); close(serfd); serfd = -1; @@ -738,7 +824,8 @@ bool CDVAPDongle::OpenSerial(char *device) cfsetospeed(&t, B230400); cfsetispeed(&t, B230400); - if (tcsetattr(serfd, TCSANOW, &t) < 0) { + if (tcsetattr(serfd, TCSANOW, &t) < 0) + { printf("tcsetattr failed for %s, error=%dm message=%s\n", device, errno, strerror(errno)); close(serfd); serfd = -1; diff --git a/DVAPDongle.h b/DVAPDongle.h index 777d591..5431f60 100644 --- a/DVAPDongle.h +++ b/DVAPDongle.h @@ -17,9 +17,10 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - #include +#include -enum REPLY_TYPE { +enum REPLY_TYPE +{ RT_TIMEOUT, RT_ERR, RT_UNKNOWN, @@ -44,13 +45,17 @@ enum REPLY_TYPE { }; #pragma pack(push,1) -using SDVAP_REGISTER = struct dvp_register_tag { +using SDVAP_REGISTER = struct dvp_register_tag +{ uint16_t header; - union { + union + { uint8_t nul; - struct { + struct + { uint16_t control; - union { + union + { int8_t byte; int16_t word; int32_t dword; @@ -59,12 +64,15 @@ using SDVAP_REGISTER = struct dvp_register_tag { uint8_t ustr[12]; }; } param; - struct { + struct + { uint16_t streamid; uint8_t framepos; uint8_t seq; - union { - struct { + union + { + struct + { unsigned char flag[3]; unsigned char rpt2[8]; unsigned char rpt1[8]; @@ -73,7 +81,8 @@ using SDVAP_REGISTER = struct dvp_register_tag { unsigned char sfx[4]; unsigned char pfcs[2]; } hdr; - struct { + struct + { unsigned char voice; unsigned char sdata; } vad; @@ -85,36 +94,36 @@ using SDVAP_REGISTER = struct dvp_register_tag { class CDVAPDongle { - public: - CDVAPDongle(); - ~CDVAPDongle(); - bool Initialize(const char *serialno, const int frequency, const int offset, const int power, const int squelch); - REPLY_TYPE GetReply(SDVAP_REGISTER &dr); - void Stop(); - int KeepAlive(); - void SendRegister(SDVAP_REGISTER &dr); +public: + CDVAPDongle(); + ~CDVAPDongle(); + bool Initialize(const char *serialno, const int frequency, const int offset, const int power, const int squelch); + REPLY_TYPE GetReply(SDVAP_REGISTER &dr); + void Stop(); + int KeepAlive(); + void SendRegister(SDVAP_REGISTER &dr); - private: - // data - int serfd; - const unsigned int MAX_REPL_CNT; - uint32_t frequency; - int32_t offset; - SDVAP_REGISTER dvapreg; +private: + // data + int serfd; + const unsigned int MAX_REPL_CNT; + uint32_t frequency; + int32_t offset; + SDVAP_REGISTER dvapreg; - // functions - bool OpenSerial(char *device); - int read_from_dvp(void* buf, unsigned int len); - int write_to_dvp(const void* buf, const unsigned int len); - bool syncit(); - bool get_ser(const char *dvp, const char *dvap_serial_number); - bool get_name(); - bool get_fw(); - bool set_modu(); - bool set_mode(); - bool set_sql(int squelch); - bool set_pwr(int power); - bool set_off(int offset); - bool set_freq(int frequency); - bool start_dvap(); + // functions + bool OpenSerial(char *device); + int read_from_dvp(void* buf, unsigned int len); + int write_to_dvp(const void* buf, const unsigned int len); + bool syncit(); + bool get_ser(const char *dvp, const char *dvap_serial_number); + bool get_name(); + bool get_fw(); + bool set_modu(); + bool set_mode(); + bool set_sql(int squelch); + bool set_pwr(int power); + bool set_off(int offset); + bool set_freq(int frequency); + bool start_dvap(); }; diff --git a/HostQueue.h b/HostQueue.h index ef6e75b..b3f472a 100644 --- a/HostQueue.h +++ b/HostQueue.h @@ -21,7 +21,8 @@ #include #include -class CHost { +class CHost +{ public: CHost() {} diff --git a/KRBase.cpp b/KRBase.cpp index ab9068c..76786fd 100644 --- a/KRBase.cpp +++ b/KRBase.cpp @@ -42,15 +42,16 @@ void CKRBase::SetState(bool state) void CKRBase::SigHandler(int sig) { - switch (sig) { - case SIGINT: - case SIGHUP: - case SIGTERM: - keep_running = false; - break; - default: - std::cerr << "caught an unexpected signal=" << sig << std::endl; - break; + switch (sig) + { + case SIGINT: + case SIGHUP: + case SIGTERM: + keep_running = false; + break; + default: + std::cerr << "caught an unexpected signal=" << sig << std::endl; + break; } } diff --git a/Location.cpp b/Location.cpp index 4d377c0..7865b01 100644 --- a/Location.cpp +++ b/Location.cpp @@ -38,13 +38,15 @@ bool CLocation::Parse(const char *instr) if (s.size() < 20) return false; - if (! std::regex_search(s.c_str(), cm, gps, std::regex_constants::match_default)) { + if (! std::regex_search(s.c_str(), cm, gps, std::regex_constants::match_default)) + { //std::cerr << "Unsuccessful gps parse of '" << s << "'" << std::endl; return false; } auto size = cm.size(); - if (size != 7) { + if (size != 7) + { std::cerr << "Bad CRC Match for " << s << ":"; for (unsigned i=0; i &amap) { FILE *fp = fopen(configfile, "r"); - if (fp) { + if (fp) + { char line[2048]; - while (fgets(line, 2048, fp)) { + while (fgets(line, 2048, fp)) + { char *key = strtok(line, "="); key = Trim(key); - if (strlen(key) && '#' != *key) { + if (strlen(key) && '#' != *key) + { char *val = strtok(NULL, "\r\n"); char *val2 = Trim(val); - if ('\'' == val2[0]) { + if ('\'' == val2[0]) + { if ('\'' == val2[1]) val[0] = '\0'; else val = strtok(val2, "'"); - } else + } + else val = strtok(val2, "# \t"); amap[key] = val; } @@ -95,7 +100,8 @@ bool CQnetConfigure::GetDefaultBool(const std::string &path, const std::string & dvalue = false; else if ('1'==value.at(0) || 't'==value.at(0) || 'T'==value.at(0)) dvalue = true; - else { + else + { fprintf(stderr, "%s=%s doesn't seem to be a boolean!\n", path.c_str(), value.c_str()); return true; } @@ -123,28 +129,38 @@ bool CQnetConfigure::GetDefaultInt(const std::string &path, const std::string &m bool CQnetConfigure::GetDefaultString(const std::string &path, const std::string &mod, std::string &dvalue) { std::string search, search_again; - if (mod.empty()) { + if (mod.empty()) + { search = path + "_d"; // there is no mod, so this is a simple search - } else { + } + else + { search_again = mod; // we're looking from a module value. We may have to look for non-generic module parameters - if (0==path.compare(0, 7, "module_") && ('a'==path.at(7) || 'b'==path.at(7) || 'c'==path.at(7)) && '_'==path.at(8)) { + if (0==path.compare(0, 7, "module_") && ('a'==path.at(7) || 'b'==path.at(7) || 'c'==path.at(7)) && '_'==path.at(8)) + { // path begins with module_{a|b|c}_ - if (0==mod.compare("dvrptr") || 0==mod.compare("dvap") || 0==mod.compare("mmdvmhost") || 0==mod.compare("mmdvmmodem") || 0==mod.compare("itap") || 0==mod.compare("thumbdv")) { + if (0==mod.compare("dvrptr") || 0==mod.compare("dvap") || 0==mod.compare("mmdvmhost") || 0==mod.compare("mmdvmmodem") || 0==mod.compare("itap") || 0==mod.compare("thumbdv")) + { // and the module is recognized search = path; search.replace(7, 1, 1, 'x'); search_again += path.substr(8); // now the search_again path might look like dvap_frequency, for example. - } else { + } + else + { fprintf(stderr, "Unrecognized module type = '%s'\n", mod.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "%s looks like an ilformed request from module '%s'\n", path.c_str(), mod.c_str()); return true; } } auto it = defaults.find(search); - if (defaults.end() == it) { + if (defaults.end() == it) + { it = defaults.find(search_again); if (defaults.end() == it) return true; @@ -156,20 +172,25 @@ bool CQnetConfigure::GetDefaultString(const std::string &path, const std::string bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, bool &value) { auto it = cfg.find(path); - if (cfg.end() == it) { + if (cfg.end() == it) + { bool dvalue; - if (GetDefaultBool(path, mod, dvalue)) { + if (GetDefaultBool(path, mod, dvalue)) + { fprintf(stderr, "%s not found in either the cfg file or the defaults file!\n", path.c_str()); return true; } value = dvalue; // found a value in the defaults - } else { // found a value in the cfg file + } + else // found a value in the cfg file + { char c = it->second.at(0); if ('0'==c || 'f'==c || 'F'==c) value = false; else if ('1'==c || 't'==c || 'T'==c) value = true; - else { + else + { fprintf(stderr, "%s=%s doesn't seem to define a boolean\n", path.c_str(), it->second.c_str()); return true; } @@ -181,20 +202,26 @@ bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, b bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, double &value, const double min, const double max) { auto it = cfg.find(path); - if (cfg.end() == it) { + if (cfg.end() == it) + { double dvalue; - if (GetDefaultDouble(path, mod, dvalue)) { + if (GetDefaultDouble(path, mod, dvalue)) + { fprintf(stderr, "%s not found in either the cfg file or the defaults file!\n", path.c_str()); return true; } - if (dvalue < min || dvalue > max) { + if (dvalue < min || dvalue > max) + { fprintf(stderr, "Default value %s=%g is out of acceptable range\n", path.c_str(), value); return true; } value = dvalue; - } else { + } + else + { value = std::stod(it->second); - if (value < min || value > max) { + if (value < min || value > max) + { fprintf(stderr, "%s=%g is out of acceptable range\n", path.c_str(), value); return true; } @@ -206,20 +233,26 @@ bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, d bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, int &value, const int min, const int max) { auto it = cfg.find(path); - if (cfg.end() == it) { + if (cfg.end() == it) + { int dvalue; - if (GetDefaultInt(path, mod, dvalue)) { + if (GetDefaultInt(path, mod, dvalue)) + { fprintf(stderr, "%s not found in either the cfg file or the defaults file\n", path.c_str()); return true; } - if (dvalue < min || dvalue > max) { + if (dvalue < min || dvalue > max) + { fprintf(stderr, "Default value %s=%d is out of acceptable range\n", path.c_str(), value); return true; } value = dvalue; - } else { + } + else + { value = std::stoi(it->second); - if (value < min || value > max) { + if (value < min || value > max) + { fprintf(stderr, "%s=%s is out of acceptable range\n", path.c_str(), it->second.c_str()); return true; } @@ -231,22 +264,28 @@ bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, i bool CQnetConfigure::GetValue(const std::string &path, const std::string &mod, std::string &value, int min, int max) { auto it = cfg.find(path); - if (cfg.end() == it) { + if (cfg.end() == it) + { std::string dvalue; - if (GetDefaultString(path, mod, dvalue)) { + if (GetDefaultString(path, mod, dvalue)) + { fprintf(stderr, "%s not found in either the cfg file or the defaults file\n", path.c_str()); return true; } int l = dvalue.length(); - if (min-1>=l || l>max) { + if (min-1>=l || l>max) + { printf("Default value %s='%s' is wrong size\n", path.c_str(), value.c_str()); return true; } value.assign(dvalue); - } else { + } + else + { value.assign(it->second); int l = value.length(); - if (lmax) { + if (lmax) + { printf("%s='%s' is wrong size\n", path.c_str(), value.c_str()); return true; } diff --git a/QnetConfigure.h b/QnetConfigure.h index efb404f..03b4ba3 100644 --- a/QnetConfigure.h +++ b/QnetConfigure.h @@ -21,13 +21,14 @@ #include #include -class CQnetConfigure { +class CQnetConfigure +{ public: CQnetConfigure(); virtual ~CQnetConfigure(); bool Initialize(const char *configfile); - bool GetValue(const std::string &path, const std::string &mod, bool &value); - bool GetValue(const std::string &path, const std::string &mod, double &value, const double min, const double max); + bool GetValue(const std::string &path, const std::string &mod, bool &value); + bool GetValue(const std::string &path, const std::string &mod, double &value, const double min, const double max); bool GetValue(const std::string &path, const std::string &mod, int &value, const int min, const int max); bool GetValue(const std::string &path, const std::string &mod, std::string &value, const int min, const int max); bool KeyExists(const std::string &key); @@ -39,7 +40,7 @@ private: char *Trim(char *s); bool ReadConfigFile(const char *file, std::map &amap); bool GetDefaultBool (const std::string &key, const std::string &mod, bool &dval); - bool GetDefaultDouble(const std::string &key, const std::string &mod, double &dval); - bool GetDefaultInt (const std::string &key, const std::string &mod, int &dval); - bool GetDefaultString(const std::string &key, const std::string &mod, std::string &dval); + bool GetDefaultDouble(const std::string &key, const std::string &mod, double &dval); + bool GetDefaultInt (const std::string &key, const std::string &mod, int &dval); + bool GetDefaultString(const std::string &key, const std::string &mod, std::string &dval); }; diff --git a/QnetDB.cpp b/QnetDB.cpp index 72a009d..c1525a6 100644 --- a/QnetDB.cpp +++ b/QnetDB.cpp @@ -23,7 +23,8 @@ bool CQnetDB::Open(const char *name) { - if (sqlite3_open_v2(name, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL)) { + if (sqlite3_open_v2(name, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL)) + { fprintf(stderr, "CQnetDB::Open: can't open %s\n", name); return true; } @@ -36,44 +37,47 @@ bool CQnetDB::Init() char *eMsg; std::string sql("CREATE TABLE IF NOT EXISTS LHEARD(" - "callsign TEXT PRIMARY KEY, " - "sfx TEXT DEFAULT ' ', " - "message TEXT DEFAULT ' ', " - "maidenhead TEXT DEFAULT ' ', " - "latitude REAL DEFAULT 0.0, " - "longitude REAL DEFAULT 0.0, " - "module TEXT, " - "reflector TEXT, " - "lasttime INT NOT NULL" + "callsign TEXT PRIMARY KEY, " + "sfx TEXT DEFAULT ' ', " + "message TEXT DEFAULT ' ', " + "maidenhead TEXT DEFAULT ' ', " + "latitude REAL DEFAULT 0.0, " + "longitude REAL DEFAULT 0.0, " + "module TEXT, " + "reflector TEXT, " + "lasttime INT NOT NULL" ") WITHOUT ROWID;"); - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; } sql.assign("CREATE TABLE IF NOT EXISTS LINKSTATUS(" - "ip_address TEXT PRIMARY KEY, " - "from_mod TEXT NOT NULL, " - "to_callsign TEXT NOT NULL, " - "to_mod TEXT NOT NULL, " - "linked_time INT NOT NULL" - ") WITHOUT ROWID;"); - - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + "ip_address TEXT PRIMARY KEY, " + "from_mod TEXT NOT NULL, " + "to_callsign TEXT NOT NULL, " + "to_mod TEXT NOT NULL, " + "linked_time INT NOT NULL" + ") WITHOUT ROWID;"); + + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; } sql.assign("CREATE TABLE IF NOT EXISTS GATEWAYS(" - "name TEXT PRIMARY KEY, " - "address TEXT NOT NULL, " - "port INT NOT NULL" - ") WITHOUT ROWID;"); + "name TEXT PRIMARY KEY, " + "address TEXT NOT NULL, " + "port INT NOT NULL" + ") WITHOUT ROWID;"); - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -81,10 +85,11 @@ bool CQnetDB::Init() return false; } -static int countcallback(void *count, int /*argc*/, char **argv, char **/*azColName*/) { - auto c = (int *)count; - *c = atoi(argv[0]); - return 0; +static int countcallback(void *count, int /*argc*/, char **argv, char **/*azColName*/) +{ + auto c = (int *)count; + *c = atoi(argv[0]); + return 0; } bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, const char *reflector) @@ -98,13 +103,15 @@ bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, int count = 0; char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateLH [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; } - if (count) { + if (count) + { sql.assign("UPDATE LHEARD SET (sfx,module,reflector,lasttime) = ('"); sql.append(sfx); sql.append("','"); @@ -115,7 +122,9 @@ bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, sql.append("strftime('%s','now')) WHERE callsign='"); sql.append(callsign); sql.append("';"); - } else { + } + else + { sql.assign("INSERT INTO LHEARD (callsign,sfx,module,reflector,lasttime) VALUES ('"); sql.append(callsign); sql.append("','"); @@ -128,7 +137,8 @@ bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, sql.append("strftime('%s','now'));"); } - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateLH [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -153,7 +163,8 @@ bool CQnetDB::UpdatePosition(const char *callsign, const char *maidenhead, doubl sql.append("';"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdatePosition [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -174,7 +185,8 @@ bool CQnetDB::UpdateMessage(const char *callsign, const char *message) sql.append("';"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateMessage [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -200,7 +212,8 @@ bool CQnetDB::UpdateLS(const char *address, const char from_mod, const char *to_ sql.append(");"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateLS [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -224,7 +237,8 @@ bool CQnetDB::UpdateGW(const char *name, const char *address, unsigned short por sql.append(");"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateGW [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -239,18 +253,21 @@ bool CQnetDB::UpdateGW(CHostQueue &hqueue) return false; char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateGW BEGIN TRANSATION error: %s\n", eMsg); sqlite3_free(eMsg); return true; } - while (! hqueue.Empty()) { + while (! hqueue.Empty()) + { auto h = hqueue.Pop(); UpdateGW(h.name.c_str(), h.addr.c_str(), h.port); } - if (SQLITE_OK != sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::UpdateGW COMMIT TRANSACTION error: %s\n", eMsg); sqlite3_free(eMsg); return true; @@ -267,7 +284,8 @@ bool CQnetDB::DeleteLS(const char *address) sql.append("';"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::DeleteLS [%s] error: %s\n", sql.c_str(), eMsg); sqlite3_free(eMsg); return true; @@ -286,15 +304,18 @@ bool CQnetDB::FindLS(const char mod, std::list &linklist) sqlite3_stmt *stmt; int rval = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0); - if (SQLITE_OK != rval) { + if (SQLITE_OK != rval) + { fprintf(stderr, "CQnetDB::FindLS [%s] error\n", sql.c_str()); return true; } - while (SQLITE_ROW == sqlite3_step(stmt)) { + while (SQLITE_ROW == sqlite3_step(stmt)) + { std::string cs((const char *)sqlite3_column_text(stmt, 1)); std::string mod((const char *)sqlite3_column_text(stmt, 2)); - if (mod.at(0) != 'p') { + if (mod.at(0) != 'p') + { cs.resize(7, ' '); cs.append(mod); } @@ -318,17 +339,21 @@ bool CQnetDB::FindGW(const char *name, std::string &address, unsigned short &por sqlite3_stmt *stmt; int rval = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0); - if (SQLITE_OK != rval) { + if (SQLITE_OK != rval) + { fprintf(stderr, "CQnetDB::FindGW error: %d\n", rval); return true; } - if (SQLITE_ROW == sqlite3_step(stmt)) { + if (SQLITE_ROW == sqlite3_step(stmt)) + { address.assign((const char *)sqlite3_column_text(stmt, 0)); port = (unsigned short)(sqlite3_column_int(stmt, 1)); sqlite3_finalize(stmt); return false; - } else { + } + else + { sqlite3_finalize(stmt); return true; } @@ -347,15 +372,19 @@ bool CQnetDB::FindGW(const char *name) sqlite3_stmt *stmt; int rval = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0); - if (SQLITE_OK != rval) { + if (SQLITE_OK != rval) + { fprintf(stderr, "CQnetDB::FindGW error: %d\n", rval); return true; } - if (SQLITE_ROW == sqlite3_step(stmt)) { + if (SQLITE_ROW == sqlite3_step(stmt)) + { sqlite3_finalize(stmt); return true; - } else { + } + else + { sqlite3_finalize(stmt); return false; } @@ -368,7 +397,8 @@ void CQnetDB::ClearLH() char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LHEARD;", NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LHEARD;", NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::ClearLH error: %s\n", eMsg); sqlite3_free(eMsg); } @@ -381,7 +411,8 @@ void CQnetDB::ClearLS() char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LINKSTATUS;", NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LINKSTATUS;", NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::ClearLS error: %s\n", eMsg); sqlite3_free(eMsg); } @@ -394,7 +425,8 @@ void CQnetDB::ClearGW() char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM GATEWAYS;", NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM GATEWAYS;", NULL, 0, &eMsg)) + { fprintf(stderr, "CQnetDB::ClearGW error: %s\n", eMsg); sqlite3_free(eMsg); } @@ -405,17 +437,18 @@ int CQnetDB::Count(const char *table) if (NULL == db) return 0; - std::string sql("SELECT COUNT(*) FROM "); + std::string sql("SELECT COUNT(*) FROM "); sql.append(table); sql.append(";"); int count = 0; char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg)) + { fprintf(stderr, "CQnetDB::Count error: %s\n", eMsg); sqlite3_free(eMsg); } - return count; + return count; } diff --git a/QnetDB.h b/QnetDB.h index 5e5e080..52c86e9 100644 --- a/QnetDB.h +++ b/QnetDB.h @@ -24,22 +24,23 @@ #include "HostQueue.h" -class CLink { +class CLink +{ public: - CLink(const std::string &call, const unsigned char *addr, time_t ltime) : callsign(call) , address((const char *)addr) , linked_time(ltime) {} + CLink(const std::string &call, const unsigned char *addr, time_t ltime) : callsign(call), address((const char *)addr), linked_time(ltime) {} CLink(const CLink &from) { callsign.assign(from.callsign); address.assign(from.address), - linked_time=from.linked_time; + linked_time=from.linked_time; } CLink &operator=(const CLink &from) { callsign.assign(from.callsign); address.assign(from.address), - linked_time=from.linked_time; + linked_time=from.linked_time; return *this; } @@ -49,7 +50,8 @@ public: time_t linked_time; }; -class CQnetDB { +class CQnetDB +{ public: CQnetDB() : db(NULL) {} ~CQnetDB() { if (db) sqlite3_close(db); } diff --git a/QnetDVAP.cpp b/QnetDVAP.cpp index cb444e9..8ae4e51 100644 --- a/QnetDVAP.cpp +++ b/QnetDVAP.cpp @@ -62,7 +62,8 @@ void CQnetDVAP::calcPFCS(unsigned char *packet, unsigned char *pfcs) { unsigned short crc_dstar_ffff = 0xffff; unsigned short tmp, short_c; - unsigned short crc_tabccitt[256] = { + unsigned short crc_tabccitt[256] = + { 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, @@ -81,7 +82,8 @@ void CQnetDVAP::calcPFCS(unsigned char *packet, unsigned char *pfcs) 0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330,0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78 }; - for (int i = 0; i < 39 ; i++) { + for (int i = 0; i < 39 ; i++) + { short_c = 0x00ff & (unsigned short)packet[i]; tmp = (crc_dstar_ffff & 0x00ff) ^ short_c; crc_dstar_ffff = (crc_dstar_ffff >> 8) ^ crc_tabccitt[tmp]; @@ -107,12 +109,15 @@ bool CQnetDVAP::ReadConfig(const char *cfgFile) const std::string estr; // an empty string std::string type; std::string dvap_path("module_"); - if (0 > assigned_module) { + if (0 > assigned_module) + { // we need to find the lone dvap module - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { std::string test(dvap_path); test.append(1, 'a'+i); - if (cfg.KeyExists(test)) { + if (cfg.KeyExists(test)) + { cfg.GetValue(test, estr, type, 1, 16); if (type.compare("dvap")) continue; // this ain't it! @@ -121,27 +126,35 @@ bool CQnetDVAP::ReadConfig(const char *cfgFile) break; } } - if (0 > assigned_module) { + if (0 > assigned_module) + { fprintf(stderr, "Error: no 'dvap' module found\n!"); return true; } - } else { + } + else + { // make sure dvap module is defined dvap_path.append(1, 'a' + assigned_module); - if (cfg.KeyExists(dvap_path)) { + if (cfg.KeyExists(dvap_path)) + { cfg.GetValue(dvap_path, estr, type, 1, 16); - if (type.compare("dvap")) { + if (type.compare("dvap")) + { fprintf(stderr, "%s = %s is not 'dvap' type!\n", dvap_path.c_str(), type.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "Module '%c' is not defined.\n", 'a'+assigned_module); return true; } } RPTR_MOD = 'A' + assigned_module; cfg.GetValue("gateway_tomodem"+std::string(1, 'a'+assigned_module), estr, togate, 1, FILENAME_MAX); - if (cfg.KeyExists(dvap_path+"_callsign")) { + if (cfg.KeyExists(dvap_path+"_callsign")) + { if (cfg.GetValue(dvap_path+"_callsign", type, RPTR, 3, 6)) return true; } @@ -150,11 +163,13 @@ bool CQnetDVAP::ReadConfig(const char *cfgFile) if (RPTR.empty()) RPTR.assign(OWNER); - for (unsigned long i=0; i= inactiveMax) { + if (!written_to_q) // we could also end up here if we are busy and we received a non-standard packet size + { + if (busy20000) + { + if (++inactive >= inactiveMax) + { if (LOG_QSO) printf("G2 Timeout...\n"); @@ -404,15 +450,21 @@ void CQnetDVAP::ReadFromGateway() busy20000 = false; break; - } else { // inactive too long - if (space == 127) { + } + else // inactive too long + { + if (space == 127) + { if (LOG_DEBUG) fprintf(stderr, "sending silent frame where: len=%d, inactive=%d\n", len, inactive); - if (seq_no == 0) { + if (seq_no == 0) + { silence[9] = 0x55; silence[10] = 0x2d; silence[11] = 0x16; - } else { + } + else + { silence[9] = 0x70; silence[10] = 0x4f; silence[11] = 0x93; @@ -435,7 +487,8 @@ void CQnetDVAP::ReadFromGateway() seq_no = 0; } } - } else // busy20000 is false + } + else // busy20000 is false break; } } @@ -481,65 +534,67 @@ void CQnetDVAP::RptrAckThread(SDVAP_ACK_ARG *parg) // SYNC dr.header = 0xc012u; dr.frame.streamid = sid; - for (int i=0; i<10; i++) { + for (int i=0; i<10; i++) + { while ((space < 1) && keep_running) usleep(5); dr.frame.framepos = dr.frame.seq = i; - switch (i) { - case 0: - silence[9] = 0x55; - silence[10] = 0x2d; - silence[11] = 0x16; - break; - case 1: - silence[9] = '@' ^ 0x70; - silence[10] = RADIO_ID[0] ^ 0x4f; - silence[11] = RADIO_ID[1] ^ 0x93; - break; - case 2: - silence[9] = RADIO_ID[2] ^ 0x70; - silence[10] = RADIO_ID[3] ^ 0x4f; - silence[11] = RADIO_ID[4] ^ 0x93; - break; - case 3: - silence[9] = 'A' ^ 0x70; - silence[10] = RADIO_ID[5] ^ 0x4f; - silence[11] = RADIO_ID[6] ^ 0x93; - break; - case 4: - silence[9] = RADIO_ID[7] ^ 0x70; - silence[10] = RADIO_ID[8] ^ 0x4f; - silence[11] = RADIO_ID[9] ^ 0x93; - break; - case 5: - silence[9] = 'B' ^ 0x70; - silence[10] = RADIO_ID[10] ^ 0x4f; - silence[11] = RADIO_ID[11] ^ 0x93; - break; - case 6: - silence[9] = RADIO_ID[12] ^ 0x70; - silence[10] = RADIO_ID[13] ^ 0x4f; - silence[11] = RADIO_ID[14] ^ 0x93; - break; - case 7: - silence[9] = 'C' ^ 0x70; - silence[10] = RADIO_ID[15] ^ 0x4f; - silence[11] = RADIO_ID[16] ^ 0x93; - break; - case 8: - silence[9] = RADIO_ID[17] ^ 0x70; - silence[10] = RADIO_ID[18] ^ 0x4f; - silence[11] = RADIO_ID[19] ^ 0x93; - break; - case 9: - silence[0] = 0x55; - silence[1] = 0xc8; - silence[2] = 0x7a; - silence[9] = 0x55; - silence[10] = 0x55; - silence[11] = 0x55; - dr.frame.framepos |= 0x40; - break; + switch (i) + { + case 0: + silence[9] = 0x55; + silence[10] = 0x2d; + silence[11] = 0x16; + break; + case 1: + silence[9] = '@' ^ 0x70; + silence[10] = RADIO_ID[0] ^ 0x4f; + silence[11] = RADIO_ID[1] ^ 0x93; + break; + case 2: + silence[9] = RADIO_ID[2] ^ 0x70; + silence[10] = RADIO_ID[3] ^ 0x4f; + silence[11] = RADIO_ID[4] ^ 0x93; + break; + case 3: + silence[9] = 'A' ^ 0x70; + silence[10] = RADIO_ID[5] ^ 0x4f; + silence[11] = RADIO_ID[6] ^ 0x93; + break; + case 4: + silence[9] = RADIO_ID[7] ^ 0x70; + silence[10] = RADIO_ID[8] ^ 0x4f; + silence[11] = RADIO_ID[9] ^ 0x93; + break; + case 5: + silence[9] = 'B' ^ 0x70; + silence[10] = RADIO_ID[10] ^ 0x4f; + silence[11] = RADIO_ID[11] ^ 0x93; + break; + case 6: + silence[9] = RADIO_ID[12] ^ 0x70; + silence[10] = RADIO_ID[13] ^ 0x4f; + silence[11] = RADIO_ID[14] ^ 0x93; + break; + case 7: + silence[9] = 'C' ^ 0x70; + silence[10] = RADIO_ID[15] ^ 0x4f; + silence[11] = RADIO_ID[16] ^ 0x93; + break; + case 8: + silence[9] = RADIO_ID[17] ^ 0x70; + silence[10] = RADIO_ID[18] ^ 0x4f; + silence[11] = RADIO_ID[19] ^ 0x93; + break; + case 9: + silence[0] = 0x55; + silence[1] = 0xc8; + silence[2] = 0x7a; + silence[9] = 0x55; + silence[10] = 0x55; + silence[11] = 0x55; + dr.frame.framepos |= 0x40; + break; } memcpy(&dr.frame.vad.voice, silence, 12); dongle.SendRegister(dr); @@ -568,32 +623,43 @@ void CQnetDVAP::ReadDVAPThread() int num_dv_frames = 0; int num_bit_errors = 0; - while (keep_running) { + while (keep_running) + { // local RF user went away ? - if (dvap_busy) { + if (dvap_busy) + { if (last_RF_time.time() > TIMING_TIMEOUT_LOCAL_RPTR) dvap_busy = false; } // read from the dvap and process reply = dongle.GetReply(dr); - if (reply == RT_ERR) { + if (reply == RT_ERR) + { printf("Detected ERROR event from DVAP dongle, stopping...n"); break; - } else if (reply == RT_STOP) { + } + else if (reply == RT_STOP) + { printf("Detected STOP event from DVAP dongle, stopping...\n"); break; - } else if (reply == RT_START) { + } + else if (reply == RT_START) + { printf("Detected START event from DVAP dongle\n"); - // else if (reply == RT_PTT) { - // ptt = (dvp_buf[4] == 0x01); - // printf("Detected PTT=%s\n", ptt?"on":"off"); - } else if (reply == RT_STS) { + // else if (reply == RT_PTT) { + // ptt = (dvp_buf[4] == 0x01); + // printf("Detected PTT=%s\n", ptt?"on":"off"); + } + else if (reply == RT_STS) + { space = (unsigned int)dr.param.sstr[2]; if (status_cntr < 3000) status_cntr += 20; - } else if (reply == RT_HDR) { + } + else if (reply == RT_HDR) + { num_dv_frames = 0; num_bit_errors = 0; @@ -603,12 +669,13 @@ void CQnetDVAP::ReadDVAPThread() ok = true; /* Accept valid flags only */ - if (ok) { + if (ok) + { if ((dr.frame.hdr.flag[0] != 0x00) && (dr.frame.hdr.flag[0] != 0x08) && // net - (dr.frame.hdr.flag[0] != 0x20) && (dr.frame.hdr.flag[0] != 0x28) && // flags + (dr.frame.hdr.flag[0] != 0x20) && (dr.frame.hdr.flag[0] != 0x28) && // flags - (dr.frame.hdr.flag[0] != 0x40) && (dr.frame.hdr.flag[0] != 0x48) && // rptr - (dr.frame.hdr.flag[0] != 0x60) && (dr.frame.hdr.flag[0] != 0x68)) // flags + (dr.frame.hdr.flag[0] != 0x40) && (dr.frame.hdr.flag[0] != 0x48) && // rptr + (dr.frame.hdr.flag[0] != 0x60) && (dr.frame.hdr.flag[0] != 0x68)) // flags ok = false; } @@ -621,9 +688,9 @@ void CQnetDVAP::ReadDVAPThread() /* RPT2 must also be valid */ if ((dsvt.hdr.rpt2[7] == 'A') || - (dsvt.hdr.rpt2[7] == 'B') || - (dsvt.hdr.rpt2[7] == 'C') || - (dsvt.hdr.rpt2[7] == 'G')) + (dsvt.hdr.rpt2[7] == 'B') || + (dsvt.hdr.rpt2[7] == 'C') || + (dsvt.hdr.rpt2[7] == 'G')) memcpy(dsvt.hdr.rpt2, RPTR.c_str(), 7); else memset(dsvt.hdr.rpt2, ' ', 8); @@ -642,21 +709,28 @@ void CQnetDVAP::ReadDVAPThread() that means that mycall, rpt1, rpt2 must be equal to RPTR otherwise we drop the rf data */ - if (RPTR.compare(OWNER)) { - if (memcmp(dsvt.hdr.mycall, RPTR.c_str(), CALL_SIZE) != 0) { + if (RPTR.compare(OWNER)) + { + if (memcmp(dsvt.hdr.mycall, RPTR.c_str(), CALL_SIZE) != 0) + { printf("mycall=[%.8s], not equal to %s\n", dsvt.hdr.mycall, RPTR.c_str()); ok = false; } - } else if (memcmp(dsvt.hdr.mycall, " ", 8) == 0) { + } + else if (memcmp(dsvt.hdr.mycall, " ", 8) == 0) + { printf("Invalid value for mycall=[%.8s]\n", dsvt.hdr.mycall); ok = false; } - if (ok) { - for (i = 0; i < 8; i++) { + if (ok) + { + for (i = 0; i < 8; i++) + { if (!isupper(dsvt.hdr.mycall[i]) && - !isdigit(dsvt.hdr.mycall[i]) && - (dsvt.hdr.mycall[i] != ' ')) { + !isdigit(dsvt.hdr.mycall[i]) && + (dsvt.hdr.mycall[i] != ' ')) + { memset(dsvt.hdr.mycall, ' ', 8); ok = false; printf("Invalid value for MYCALL\n"); @@ -664,20 +738,24 @@ void CQnetDVAP::ReadDVAPThread() } } - for (i = 0; i < 4; i++) { + for (i = 0; i < 4; i++) + { if (!isupper(dsvt.hdr.sfx[i]) && - !isdigit(dsvt.hdr.sfx[i]) && - (dsvt.hdr.sfx[i] != ' ')) { + !isdigit(dsvt.hdr.sfx[i]) && + (dsvt.hdr.sfx[i] != ' ')) + { memset(dsvt.hdr.sfx, ' ', 4); break; } } - for (i = 0; i < 8; i++) { + for (i = 0; i < 8; i++) + { if (!isupper(dsvt.hdr.urcall[i]) && - !isdigit(dsvt.hdr.urcall[i]) && - (dsvt.hdr.urcall[i] != ' ') && - (dsvt.hdr.urcall[i] != '/')) { + !isdigit(dsvt.hdr.urcall[i]) && + (dsvt.hdr.urcall[i] != ' ') && + (dsvt.hdr.urcall[i] != '/')) + { memcpy(dsvt.hdr.urcall, "CQCQCQ ", 8); break; } @@ -724,9 +802,12 @@ void CQnetDVAP::ReadDVAPThread() memcpy(mycall, dr.frame.hdr.mycall, 8); } - } else if (reply == RT_DAT) { + } + else if (reply == RT_DAT) + { /* have we already received a header ? */ - if (dvap_busy) { + if (dvap_busy) + { the_end = ((dr.frame.framepos & 0x40) == 0x40); dsvt.config = 0x20U; @@ -738,7 +819,8 @@ void CQnetDVAP::ReadDVAPThread() int ber_data[3]; int ber_errs = decode.Decode(dsvt.vasd.voice, ber_data); - if (ber_data[0] != 0xf85) { + if (ber_data[0] != 0xf85) + { num_bit_errors += ber_errs; num_dv_frames++; } @@ -748,7 +830,8 @@ void CQnetDVAP::ReadDVAPThread() // local RF user still talking, update timer last_RF_time.start(); - if (the_end) { + if (the_end) + { // local RF user stopped talking dvap_busy = false; SDVAP_ACK_ARG dvap_ack_arg; @@ -756,11 +839,15 @@ void CQnetDVAP::ReadDVAPThread() if (LOG_QSO) printf("End of dvap audio, ber=%.02f\n", dvap_ack_arg.ber); - if (MODULE_ACKNOWLEDGE && !busy20000) { + if (MODULE_ACKNOWLEDGE && !busy20000) + { memcpy(dvap_ack_arg.mycall, mycall, 8); - try { + try + { std::async(std::launch::async, &CQnetDVAP::RptrAckThread, this, &dvap_ack_arg); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start RptrAckThread(). Exception: %s\n", e.what()); } } @@ -787,16 +874,19 @@ bool CQnetDVAP::Init(const char *file, const int amod) { assigned_module = amod; - if (ReadConfig(file)) { + if (ReadConfig(file)) + { printf("Failed to process config file %s\n", file); return true; } - if (RPTR.length() != 8) { + if (RPTR.length() != 8) + { printf("Bad RPTR value, length must be exactly 8 bytes\n"); return true; } - if ((RPTR_MOD != 'A') && (RPTR_MOD != 'B') && (RPTR_MOD != 'C')) { + if ((RPTR_MOD != 'A') && (RPTR_MOD != 'B') && (RPTR_MOD != 'C')) + { printf("Bad RPTR_MOD value, must be one of A or B or C\n"); return true; } @@ -818,7 +908,8 @@ bool CQnetDVAP::Init(const char *file, const int amod) if (!dongle.Initialize(MODULE_SERIAL_NUMBER.c_str(), MODULE_FREQUENCY, MODULE_OFFSET, MODULE_POWER, MODULE_SQUELCH)) return true; - if (ToGate.Open(togate.c_str(), this)) { + if (ToGate.Open(togate.c_str(), this)) + { dongle.Stop(); close(serfd); return true; @@ -831,25 +922,33 @@ void CQnetDVAP::Run() { CTimer ackpoint; std::future readthread; - try { + try + { readthread = std::async(std::launch::async, &CQnetDVAP::ReadDVAPThread, this); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Unable to start ReadDVAPThread(). Exception: %s\n", e.what()); keep_running = false; } printf("Started ReadDVAPThread()\n"); int cnt = 0; - while (keep_running) { - if (ackpoint.time() > 2.5) { + while (keep_running) + { + if (ackpoint.time() > 2.5) + { int rc = dongle.KeepAlive(); - if (rc < 0) { + if (rc < 0) + { cnt ++; - if (cnt > 5) { + if (cnt > 5) + { printf("Could not send KEEPALIVE signal to dvap 5 times...exiting\n"); keep_running = false; } - } else + } + else cnt = 0; ackpoint.start(); } @@ -867,12 +966,14 @@ int main(int argc, char *argv[]) setvbuf(stdout, NULL, _IOLBF, 0); printf("dvap_rptr VERSION %s\n", DVAP_VERSION); - if (argc != 2) { + if (argc != 2) + { fprintf(stderr, "Usage: %s dvap_rptr.cfg\n", argv[0]); return EXIT_FAILURE; } - if ('-' == argv[1][0]) { + if ('-' == argv[1][0]) + { printf("\nQnetDVAP Version #%s Copyright (C) 2018-2020 by Thomas A. Early N7TAE\n", DVAP_VERSION); printf("QnetDVAP comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); @@ -880,29 +981,31 @@ int main(int argc, char *argv[]) } const char *qn = strstr(argv[0], "qndvap"); - if (NULL == qn) { + if (NULL == qn) + { fprintf(stderr, "Error finding 'qndvap' in %s!\n", argv[0]); return EXIT_FAILURE; } qn += 6; int mod; - switch (*qn) { - case NULL: - mod = -1; - break; - case 'a': - mod = 0; - break; - case 'b': - mod = 1; - break; - case 'c': - mod = 2; - break; - default: - fprintf(stderr, "ERROR: '%s' is not a valid module\nassigned module must be a, b or c\n", argv[1]); - return 1; + switch (*qn) + { + case NULL: + mod = -1; + break; + case 'a': + mod = 0; + break; + case 'b': + mod = 1; + break; + case 'c': + mod = 2; + break; + default: + fprintf(stderr, "ERROR: '%s' is not a valid module\nassigned module must be a, b or c\n", argv[1]); + return 1; } CQnetDVAP dvap; diff --git a/QnetDVAP.h b/QnetDVAP.h index a5fa0e7..445ff32 100644 --- a/QnetDVAP.h +++ b/QnetDVAP.h @@ -20,7 +20,8 @@ #include "KRBase.h" -using SDVAP_ACK_ARG = struct davp_ack_arg_tag { +using SDVAP_ACK_ARG = struct davp_ack_arg_tag +{ char mycall[8]; float ber; }; diff --git a/QnetDVRPTR.cpp b/QnetDVRPTR.cpp index 60a455f..d9205dc 100644 --- a/QnetDVRPTR.cpp +++ b/QnetDVRPTR.cpp @@ -45,7 +45,8 @@ #define BAUD B115200 #define IP_SIZE 15 -static const uint32_t ENCODING_TABLE_24128[] = { +static const uint32_t ENCODING_TABLE_24128[] = +{ 0x000000U, 0x0018EBU, 0x00293EU, 0x0031D5U, 0x004A97U, 0x00527CU, 0x0063A9U, 0x007B42U, 0x008DC6U, 0x00952DU, 0x00A4F8U, 0x00BC13U, 0x00C751U, 0x00DFBAU, 0x00EE6FU, 0x00F684U, 0x010367U, 0x011B8CU, 0x012A59U, 0x0132B2U, 0x0149F0U, 0x01511BU, 0x0160CEU, 0x017825U, 0x018EA1U, 0x01964AU, 0x01A79FU, 0x01BF74U, 0x01C436U, 0x01DCDDU, @@ -471,8 +472,10 @@ static const uint32_t ENCODING_TABLE_24128[] = { uint32_t CQnetDVRPTR::get_syndrome_23127(uint32_t pattern) { uint32_t aux = GORLAY_X22; - if (pattern >= GORLAY_X11) { - while (pattern & GORLAY_MASK12) { + if (pattern >= GORLAY_X11) + { + while (pattern & GORLAY_MASK12) + { while ((aux & pattern)==0) aux >>= 1; pattern ^= (aux / GORLAY_X11) * GORLAY_GENPOL; } @@ -482,7 +485,8 @@ uint32_t CQnetDVRPTR::get_syndrome_23127(uint32_t pattern) unsigned int CQnetDVRPTR::gorlay_decode23127(unsigned int code) { - const uint32_t DECODING_TABLE_23127[] = { + const uint32_t DECODING_TABLE_23127[] = + { 0x000000U, 0x000001U, 0x000002U, 0x000003U, 0x000004U, 0x000005U, 0x000006U, 0x000007U, 0x000008U, 0x000009U, 0x00000AU, 0x00000BU, 0x00000CU, 0x00000DU, 0x00000EU, 0x024020U, 0x000010U, 0x000011U, 0x000012U, 0x000013U, 0x000014U, 0x000015U, 0x000016U, 0x412000U, 0x000018U, 0x000019U, 0x00001AU, 0x180800U, 0x00001CU, 0x200300U, @@ -706,7 +710,8 @@ unsigned int CQnetDVRPTR::gorlay_decode24128(unsigned int code) return gorlay_decode23127(code >> 1); } -const uint32_t PRNG_TABLE[] = { +const uint32_t PRNG_TABLE[] = +{ 0x42CC47U, 0x19D6FEU, 0x304729U, 0x6B2CD0U, 0x60BF47U, 0x39650EU, 0x7354F1U, 0xEACF60U, 0x819C9FU, 0xDE25CEU, 0xD7B745U, 0x8CC8B8U, 0x8D592BU, 0xF71257U, 0xBCA084U, 0xA5B329U, 0xEE6AFAU, 0xF7D9A7U, 0xBCC21CU, 0x4712D9U, 0x4F2922U, 0x14FA37U, 0x5D43ECU, 0x564115U, 0x299A92U, 0x20A9EBU, 0x7B707DU, 0x3BE3A4U, 0x20D95BU, 0x6B085AU, @@ -1124,7 +1129,8 @@ void CQnetDVRPTR::ambefec_deinterleave(tambevoicefec result, const tambevoicefec uint32_t bitpos, bytcnt; memset(result, 0, sizeof(tambevoicefec)); // init result bitpos = 0; - for (bytcnt = 0; bytcnt < sizeof(tambevoicefec); bytcnt++) { + for (bytcnt = 0; bytcnt < sizeof(tambevoicefec); bytcnt++) + { char voice_dsr = voice[bytcnt]; if (voice_dsr & 0x80) result[bitpos>>3] |= (0x80 >> (bitpos&7)); interleaveambe12(bitpos); @@ -1149,7 +1155,8 @@ void CQnetDVRPTR::ambefec_interleave(tambevoicefec result, const tambevoicefec r { uint32_t bitpos, bytcnt; bitpos = 0; - for (bytcnt = 0; bytcnt < sizeof(tambevoicefec); bytcnt++) { + for (bytcnt = 0; bytcnt < sizeof(tambevoicefec); bytcnt++) + { char voice_dsr = (raw_voice[bitpos>>3] & (0x80 >> (bitpos&7)))?0x80:0x00; interleaveambe12(bitpos); if (raw_voice[bitpos>>3] & (0x80 >> (bitpos&7))) voice_dsr |= 0x40; @@ -1191,14 +1198,16 @@ void CQnetDVRPTR::ambefec_regenerate(tambevoicefec voice) const unsigned char silence[12] = { 0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x70,0x4f,0x93 }; -void CQnetDVRPTR::CleanCall(std::string &callsign) { +void CQnetDVRPTR::CleanCall(std::string &callsign) +{ for (auto it=callsign.begin(); it!=callsign.end(); it++) if (islower(*it)) *it = toupper(*it); callsign.resize(CALL_SIZE, ' '); } -const unsigned short crc_tabccitt[256] = { +const unsigned short crc_tabccitt[256] = +{ 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, @@ -1223,7 +1232,8 @@ void CQnetDVRPTR::calcPFCS(unsigned char *packet) //Netzwerk CRC unsigned short tmp, short_c; int i; - for (i = 17; i < 56 ; i++) { + for (i = 17; i < 56 ; i++) + { short_c = 0x00ff & (unsigned short)packet[i]; tmp = (crc_dstar_ffff & 0x00ff) ^ short_c; crc_dstar_ffff = (crc_dstar_ffff >> 8) ^ crc_tabccitt[tmp]; @@ -1249,12 +1259,15 @@ bool CQnetDVRPTR::ReadConfig(const char *cfgFile) const std::string estr; // an empty string std::string type; std::string path("module_"); - if (0 > assigned_module) { + if (0 > assigned_module) + { // we need to find the lone dvrptr module - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { std::string test(path); test.append(1, 'a'+i); - if (cfg.KeyExists(test)) { + if (cfg.KeyExists(test)) + { cfg.GetValue(test, estr, type, 1, 16); if (type.compare("dvrptr")) continue; // this ain't it! @@ -1263,20 +1276,27 @@ bool CQnetDVRPTR::ReadConfig(const char *cfgFile) break; } } - if (0 > assigned_module) { + if (0 > assigned_module) + { fprintf(stderr, "Error: no 'dvrptr' module found\n!"); return true; } - } else { + } + else + { // make sure dvrptr module is defined path.append(1, 'a' + assigned_module); - if (cfg.KeyExists(path)) { + if (cfg.KeyExists(path)) + { cfg.GetValue(path, estr, type, 1, 16); - if (type.compare("dvrptr")) { + if (type.compare("dvrptr")) + { fprintf(stderr, "%s = %s is not 'dvrptr' type!\n", path.c_str(), type.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "Module '%c' is not defined.\n", 'a'+assigned_module); return true; } @@ -1291,12 +1311,15 @@ bool CQnetDVRPTR::ReadConfig(const char *cfgFile) CleanCall(call); strncpy(DVCALL, call.c_str(), CALL_SIZE+1); - if (cfg.KeyExists(path+"_callsign")) { + if (cfg.KeyExists(path+"_callsign")) + { if (cfg.GetValue(path+"_callsign", type, call, 3, 6)) return true; CleanCall(call); strncpy(RPTR, call.c_str(), CALL_SIZE+1); - } else { + } + else + { strncpy(RPTR, DVCALL, CALL_SIZE+1); } @@ -1345,16 +1368,20 @@ char *CQnetDVRPTR::cleanstr(char *Text) static char cbuf[250]; memset(cbuf, 0U, 250U); - for (unsigned int x=0; x < strlen(Text); x++) { - if ((Text[x] > 0x1F) && (Text[x] < 0x7F)) { - if (Text[x] == '\'') { + for (unsigned int x=0; x < strlen(Text); x++) + { + if ((Text[x] > 0x1F) && (Text[x] < 0x7F)) + { + if (Text[x] == '\'') + { strcat(cbuf, "\\"); } strncat(cbuf, &Text[x], 1); } } - for (unsigned int x=0; x < strlen(cbuf); x++) { + for (unsigned int x=0; x < strlen(cbuf); x++) + { if (!isdigit(cbuf[x]) && !isupper(cbuf[x]) && (cbuf[x] != '/')) cbuf[x] = ' '; } @@ -1368,9 +1395,10 @@ int CQnetDVRPTR::open_port(char *dvrptr_device) struct termios terminal; fd_ser = open(dvrptr_device, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY); - if (fd_ser < 0) { + if (fd_ser < 0) + { printf("Can not open the serial port %s, error=%d(%s)\n", - dvrptr_device, errno, strerror(errno)); + dvrptr_device, errno, strerror(errno)); return -1; } printf("Success in opening device %s\n", dvrptr_device); @@ -1399,10 +1427,12 @@ int CQnetDVRPTR::read_port(int *fd_ser,unsigned char* buffera) tva.tv_usec = 50; select(*fd_ser + 1,&rfds,NULL,NULL,&tva); - if (FD_ISSET(*fd_ser, &rfds)) { + if (FD_ISSET(*fd_ser, &rfds)) + { a = read(*fd_ser,buffera,200); return a; - } else + } + else return 0; } @@ -1440,7 +1470,8 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) memcpy(Send_Modem_Header + 35, DVCALL_and_MOD, 8); memcpy(Send_Modem_Header + 43, "RPTR", 4); - if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) { + if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) + { memcpy(Send_Modem_Header + 11, RPTR, 7); memcpy(Send_Modem_Header + 19, RPTR, 7); @@ -1456,9 +1487,11 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) write(fd_ser, start_Header, sizeof (start_Header)); write(fd_ser, Send_Modem_Header, sizeof (Send_Modem_Header)); - for (i = 0; i < 16; i++) { + for (i = 0; i < 16; i++) + { usleep(DELAY_BETWEEN); - if (i == 0) { + if (i == 0) + { writevoice[0] =0xD0; writevoice[1] =0x13; writevoice[2] =0x00; @@ -1482,7 +1515,8 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) write(fd_ser, writevoice, sizeof (writevoice)); } - if ((i < 9) && ( i > 0 )) { + if ((i < 9) && ( i > 0 )) + { writevoice[0] =0xD0; writevoice[1] =0x13; writevoice[2] =0x00; @@ -1493,35 +1527,50 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) writevoice[7] =0x00; memcpy(writevoice + 8, silence, 9); - if (i == 1) { + if (i == 1) + { writevoice[17] = '@' ^ 0x70; writevoice[18] = RADIO_ID[0] ^ 0x4f; writevoice[19] = RADIO_ID[1] ^ 0x93; - } else if (i == 2) { + } + else if (i == 2) + { writevoice[17] = RADIO_ID[2] ^ 0x70; writevoice[18] = RADIO_ID[3] ^ 0x4f; writevoice[19] = RADIO_ID[4] ^ 0x93; - } else if (i == 3) { + } + else if (i == 3) + { writevoice[17] = 'A' ^ 0x70; writevoice[18] = RADIO_ID[5] ^ 0x4f; writevoice[19] = RADIO_ID[6] ^ 0x93; - } else if (i == 4) { + } + else if (i == 4) + { writevoice[17] = RADIO_ID[7] ^ 0x70; writevoice[18] = RADIO_ID[8] ^ 0x4f; writevoice[19] = RADIO_ID[9] ^ 0x93; - } else if (i == 5) { + } + else if (i == 5) + { writevoice[17] = 'B' ^ 0x70; writevoice[18] = RADIO_ID[10] ^ 0x4f; writevoice[19] = RADIO_ID[11] ^ 0x93; - } else if (i == 6) { + } + else if (i == 6) + { writevoice[17] = RADIO_ID[12] ^ 0x70; writevoice[18] = RADIO_ID[13] ^ 0x4f; writevoice[19] = RADIO_ID[14] ^ 0x93; - } else if (i == 7) { + } + else if (i == 7) + { writevoice[17] = 'C' ^ 0x70; writevoice[18] = RADIO_ID[15] ^ 0x4f; writevoice[19] = RADIO_ID[16] ^ 0x93; - } else if (i == 8) { + } + else if (i == 8) + { writevoice[17] = RADIO_ID[17] ^ 0x70; writevoice[18] = RADIO_ID[18] ^ 0x4f; writevoice[19] = RADIO_ID[19] ^ 0x93; @@ -1538,7 +1587,8 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) write(fd_ser, writevoice, sizeof (writevoice)); } - if (i == 9) { + if (i == 9) + { writevoice[0] =0xD0; writevoice[1] =0x13; writevoice[2] =0x00; @@ -1563,7 +1613,8 @@ void CQnetDVRPTR::send_ack(char *a_call, float ber) write(fd_ser, writevoice, sizeof (writevoice)); } - if (i == 15) { + if (i == 15) + { write(fd_ser, ptt_off, sizeof(ptt_off)); seq_no1 ++; if (seq_no1 == 250) @@ -1586,7 +1637,8 @@ void CQnetDVRPTR::readFrom20000() bool written_to_q = false; int fd = ToGate.GetFD(); - while (keep_running) { + while (keep_running) + { written_to_q = false; tv.tv_sec = 0; @@ -1594,23 +1646,28 @@ void CQnetDVRPTR::readFrom20000() FD_ZERO (&readfd); FD_SET (fd, &readfd); select(fd + 1, &readfd, NULL, NULL, &tv); - if (FD_ISSET(fd, &readfd)) { + if (FD_ISSET(fd, &readfd)) + { len = ToGate.Read(recv_buf.title, 56); - if (len == 56) { - if (busy20000) { + if (len == 56) + { + if (busy20000) + { FD_CLR (fd, &readfd); continue; } /* check the module and gateway */ - if (recv_buf.hdr.rpt1[7] != DVRPTR_MOD) { + if (recv_buf.hdr.rpt1[7] != DVRPTR_MOD) + { fprintf(stderr, "rpt1=%.8s != %c, ignoring\n", recv_buf.hdr.rpt1, DVRPTR_MOD); FD_CLR (fd, &readfd); break; } memcpy(recv_buf.hdr.rpt2, DVCALL_and_G, 8); - if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) { + if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) + { memcpy(recv_buf.hdr.rpt1, RPTR, 7); memcpy(recv_buf.hdr.rpt2, RPTR, 7); @@ -1623,16 +1680,18 @@ void CQnetDVRPTR::readFrom20000() } if ((recv_buf.hdr.flag[0] != 0x00) && - (recv_buf.hdr.flag[0] != 0x01) && - (recv_buf.hdr.flag[0] != 0x08) && - (recv_buf.hdr.flag[0] != 0x20) && - (recv_buf.hdr.flag[0] != 0x28) && - (recv_buf.hdr.flag[0] != 0x40)) { + (recv_buf.hdr.flag[0] != 0x01) && + (recv_buf.hdr.flag[0] != 0x08) && + (recv_buf.hdr.flag[0] != 0x20) && + (recv_buf.hdr.flag[0] != 0x28) && + (recv_buf.hdr.flag[0] != 0x40)) + { FD_CLR (fd, &readfd); break; } - if ((memcmp(recv_buf.title, "DSVT", 4) != 0) || (recv_buf.id != 0x20)) { + if ((memcmp(recv_buf.title, "DSVT", 4) != 0) || (recv_buf.id != 0x20)) + { FD_CLR (fd, &readfd); break; } @@ -1653,7 +1712,8 @@ void CQnetDVRPTR::readFrom20000() Send_Modem_Header[6] =0x00; Send_Modem_Header[7] =0x00; - if (recv_buf.hdr.flag[0] != 0x1U) { + if (recv_buf.hdr.flag[0] != 0x1U) + { if (recv_buf.hdr.flag[0] == 0x0U) recv_buf.hdr.flag[0] = 0x40U; else if (recv_buf.hdr.flag[0] == 0x8U) @@ -1692,9 +1752,12 @@ void CQnetDVRPTR::readFrom20000() write(fd_ser, Send_Modem_Header, sizeof (Send_Modem_Header)); inactive = 0; seq_no = 0; - } else if (len == 27) { + } + else if (len == 27) + { seq_no = recv_buf.ctrl & 0x1f; - if ((seq_no < 3) && (old_seq_no > 17)) { + if ((seq_no < 3) && (old_seq_no > 17)) + { block ++; if (block >= 12) block = 0; @@ -1702,12 +1765,17 @@ void CQnetDVRPTR::readFrom20000() old_seq_no = seq_no; seq_no = block * 21 + seq_no; - if (busy20000) { - if (recv_buf.streamid == streamid) { - if ((recv_buf.ctrl <= ctrl_in) && (ctrl_in != 0x80)) { + if (busy20000) + { + if (recv_buf.streamid == streamid) + { + if ((recv_buf.ctrl <= ctrl_in) && (ctrl_in != 0x80)) + { /* do not update written_to_q, ctrl_in */ ; // printf("dup\n"); - } else { + } + else + { ctrl_in = recv_buf.ctrl; if (ctrl_in == 0x14) ctrl_in = 0x80; @@ -1733,7 +1801,8 @@ void CQnetDVRPTR::readFrom20000() write(fd_ser, writevoice, sizeof (writevoice)); inactive = 1; - if ((recv_buf.ctrl & 0x40) != 0) { + if ((recv_buf.ctrl & 0x40) != 0) + { printf("End G2: streamid=%04x\n",ntohs(recv_buf.streamid)); ptt_off[4] = Send_Modem_Header[4]; @@ -1748,12 +1817,17 @@ void CQnetDVRPTR::readFrom20000() } } } - } else { + } + else + { FD_CLR (fd, &readfd); break; } - } else { - if (!busy20000) { + } + else + { + if (!busy20000) + { FD_CLR (fd, &readfd); break; } @@ -1765,9 +1839,12 @@ void CQnetDVRPTR::readFrom20000() If we received a dup or select() timed out or streamids dont match, then written_to_q is false */ - if (!written_to_q) { /* nothing was written to the adapter */ - if (busy20000) { - if (++inactive == inactiveMax) { + if (!written_to_q) /* nothing was written to the adapter */ + { + if (busy20000) + { + if (++inactive == inactiveMax) + { printf("G2 Timeout...\n"); ptt_off[4] = Send_Modem_Header[4]; write(fd_ser, ptt_off, 8); @@ -1778,7 +1855,8 @@ void CQnetDVRPTR::readFrom20000() inactive = 0; break; } - } else + } + else break; } } @@ -1792,7 +1870,8 @@ bool CQnetDVRPTR::check_serial() bool match = false; unsigned char puffer[200]; - for (int i = 0; i < 32; i++) { + for (int i = 0; i < 32; i++) + { sprintf(dvrptr_device, "/dev/ttyACM%d", i); if (access(dvrptr_device, R_OK | W_OK) != 0) continue; @@ -1804,14 +1883,16 @@ bool CQnetDVRPTR::check_serial() if (RX_Inverse == true) Modem_Init2[6]=0x01; - if (TX_Inverse == true) { + if (TX_Inverse == true) + { if (Modem_Init2[6]==0x01) Modem_Init2[6]=0x03; else Modem_Init2[6]=0x02; } - if (flock(fd_ser, LOCK_EX | LOCK_NB) != 0) { + if (flock(fd_ser, LOCK_EX | LOCK_NB) != 0) + { close(fd_ser); fd_ser = -1; printf("Device %s is already locked/used by other dvrptr repeater\n", dvrptr_device); @@ -1820,20 +1901,25 @@ bool CQnetDVRPTR::check_serial() printf("Device %s now locked for exclusive use\n", dvrptr_device); int loop_count = 0; - while (true) { - if (InitCount == 4 ) { + while (true) + { + if (InitCount == 4 ) + { write(fd_ser, Modem_SERIAL, sizeof (Modem_SERIAL)); InitCount = 0; } - if (InitCount == 3 ) { + if (InitCount == 3 ) + { write(fd_ser, Modem_Init0, sizeof (Modem_Init0)); InitCount = 4; } - if (InitCount == 2 ) { + if (InitCount == 2 ) + { write(fd_ser, Modem_Init1, sizeof (Modem_Init1)); InitCount = 3; } - if (InitCount == 1 ) { + if (InitCount == 1 ) + { write(fd_ser, Modem_Init2, sizeof (Modem_Init2)); InitCount = 2; } @@ -1843,26 +1929,28 @@ bool CQnetDVRPTR::check_serial() (void)read_port(&fd_ser,puffer); if ((puffer[0] == 0xD0) && - (puffer[1] == 0x17) && - (puffer[2] == 0x00) && - (puffer[3] == 0x91)) + (puffer[1] == 0x17) && + (puffer[2] == 0x00) && + (puffer[3] == 0x91)) puffer[1] = 0x00; if ((puffer[0] == 0xD0) && - ((puffer[1] == 0x07) || (puffer[1] == 0x08)) && - (puffer[2] == 0x00) && - (puffer[3] == 0x90)) + ((puffer[1] == 0x07) || (puffer[1] == 0x08)) && + (puffer[2] == 0x00) && + (puffer[3] == 0x90)) puffer[1] = 0x00; if ((puffer[0] == 0xD0) && - (puffer[1] == 0x05) && - (puffer[2] == 0x00) && - (puffer[3] == 0x92)) { + (puffer[1] == 0x05) && + (puffer[2] == 0x00) && + (puffer[3] == 0x92)) + { puffer[1] = 0x00; char temp_dvrptr_serial[16]; sprintf(temp_dvrptr_serial, "%02X.%02X.%02X.%02X", puffer[4], puffer[5], puffer[6], puffer[7]); printf("Device %s has serial=[%s]\n", dvrptr_device, temp_dvrptr_serial); - if (strcmp(temp_dvrptr_serial, DVRPTR_SERIAL.c_str()) == 0) { + if (strcmp(temp_dvrptr_serial, DVRPTR_SERIAL.c_str()) == 0) + { printf("Device %s serial number matches DVRPTR_SERIAL in dvrptr.cfg\n", dvrptr_device); match = true; } @@ -1870,13 +1958,15 @@ bool CQnetDVRPTR::check_serial() } loop_count ++; - if (loop_count > 50) { + if (loop_count > 50) + { printf("Waited 5 seconds to receive serial number from device %s, ...aborting\n", dvrptr_device); break; } } - if (match) { + if (match) + { printf("Found a match after %d loops\n", loop_count); break; } @@ -1895,12 +1985,14 @@ int main(int argc, const char **argv) setvbuf(stdout, NULL, _IOLBF, 0); printf("dvrptr VERSION %s\n", DVRPTR_VERSION); - if (argc != 2) { + if (argc != 2) + { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } - if ('-' == argv[1][0]) { + if ('-' == argv[1][0]) + { printf("\nQnetDVRPTR Version #%s Copyright (C) 2018-2019 by Thomas A. Early N7TAE\n", DVRPTR_VERSION); printf("QnetDVRPTR comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); @@ -1908,27 +2000,29 @@ int main(int argc, const char **argv) } const char *qn = strstr(argv[0], "qndvrptr"); - if (NULL == qn) { + if (NULL == qn) + { fprintf(stderr, "Error finding 'qndvrptr' in %s!\n", argv[0]); return 1; } qn += 8; int mod; - switch (*qn) { - case NULL: - mod = -1; - break; - case 'a': - mod = 0; - break; - case 'b': - mod = 1; - break; - case 'c': - mod = 2; - break; - default: + switch (*qn) + { + case NULL: + mod = -1; + break; + case 'a': + mod = 0; + break; + case 'b': + mod = 1; + break; + case 'c': + mod = 2; + break; + default: fprintf(stderr, "ERROR: '%s' is not a valid module\nassigned module must be a, b or c\n", argv[1]); return 1; } @@ -1944,21 +2038,25 @@ int main(int argc, const char **argv) bool CQnetDVRPTR::Init(const char *file, int mod) { assigned_module = mod; - if (ReadConfig(file)) { + if (ReadConfig(file)) + { fprintf(stderr, "Failed to process config file %s\n", file); return true; } - if (!check_serial()) { + if (!check_serial()) + { fprintf(stderr, "Cant find any FREE ACMx device that matches\n"); return true; } - if (strlen(DVCALL) != 8) { + if (strlen(DVCALL) != 8) + { fprintf(stderr, "Bad DVCALL value, length must be exactly 8 bytes\n"); return true; } - if ((DVRPTR_MOD != 'A') && (DVRPTR_MOD != 'B') && (DVRPTR_MOD != 'C')) { + if ((DVRPTR_MOD != 'A') && (DVRPTR_MOD != 'B') && (DVRPTR_MOD != 'C')) + { fprintf(stderr, "Bad DVCALL_MOD value, must be one of A or B or C\n"); return true; } @@ -1979,11 +2077,13 @@ bool CQnetDVRPTR::Init(const char *file, int mod) if (ToGate.Open(togate.c_str(), this)) return true; - if (RX_Inverse == true) { + if (RX_Inverse == true) + { Modem_Init2[6]=0x01; } - if (TX_Inverse == true) { + if (TX_Inverse == true) + { if (Modem_Init2[6]==0x01) Modem_Init2[6]=0x03; else @@ -2000,16 +2100,19 @@ void CQnetDVRPTR::Run() time_t time_rqst = tNow; time_t last_RF_time = tNow; - while (keep_running) { + while (keep_running) + { time(&tNow); - if ((tNow - time_rqst) > 2) { + if ((tNow - time_rqst) > 2) + { if (rqst_count < (RQST_COUNT - 2)) write(fd_ser, Modem_SERIAL, sizeof(Modem_SERIAL)); time_rqst = tNow; rqst_count --; - if (rqst_count < 0) { + if (rqst_count < 0) + { printf("Modem is not responding... shuttting down\n"); close(fd_ser); break; @@ -2017,19 +2120,23 @@ void CQnetDVRPTR::Run() } // init Modem - if (InitCount == 4 ) { + if (InitCount == 4 ) + { write(fd_ser, Modem_STATUS, sizeof (Modem_STATUS)); InitCount = 0; } - if (InitCount == 3 ) { + if (InitCount == 3 ) + { write(fd_ser, Modem_Init0, sizeof (Modem_Init0)); InitCount = 4; } - if (InitCount == 2 ) { + if (InitCount == 2 ) + { write(fd_ser, Modem_Init1, sizeof (Modem_Init1)); InitCount = 3; } - if (InitCount == 1 ) { + if (InitCount == 1 ) + { write(fd_ser, Modem_Init2, sizeof (Modem_Init2)); InitCount = 2; } @@ -2040,8 +2147,10 @@ void CQnetDVRPTR::Run() int seq_no = 0; bool ok = false; - if (bytes2 > 0) { - switch (bytes2) { + if (bytes2 > 0) + { + switch (bytes2) + { case 52: num_dv_frames = 0; @@ -2059,7 +2168,7 @@ void CQnetDVRPTR::Run() strcpy (myCall, cleanstr (Temp_Text)); strcpy (Temp_Text,""); - strncat (Temp_Text ,(char *) puffer + 43,4); + strncat (Temp_Text,(char *) puffer + 43,4); strcpy (myCall2, cleanstr (Temp_Text)); strcpy (Temp_Text,""); @@ -2074,36 +2183,41 @@ void CQnetDVRPTR::Run() /* just in case the data arrived from another repeater over RF */ /* Also, just in case our local RF user's radio is messed up */ if ((puffer[8] != 0x00) && - (puffer[8] != 0x08) && - (puffer[8] != 0x20) && - (puffer[8] != 0x28) && - (puffer[8] != 0x40) && - (puffer[8] != 0x48) && - (puffer[8] != 0x60) && - (puffer[8] != 0x68)) { + (puffer[8] != 0x08) && + (puffer[8] != 0x20) && + (puffer[8] != 0x28) && + (puffer[8] != 0x40) && + (puffer[8] != 0x48) && + (puffer[8] != 0x60) && + (puffer[8] != 0x68)) + { // printf("flags look BAD\n"); ok = false; } - if ((puffer[9] != 0x00) || (puffer[10] != 0x00)) { + if ((puffer[9] != 0x00) || (puffer[10] != 0x00)) + { // printf("flags look BAD\n"); ok = false; } - if (ok) { - if ((puffer[5] & 0x80) == 0x80) { + if (ok) + { + if ((puffer[5] & 0x80) == 0x80) + { printf("From RF: flags=%02x:%02x:%02x, myCall=%s/%s, yrCall=%s, rpt1=%s, rpt2=%s\n", - puffer[8], puffer[9], puffer[10], myCall, myCall2, myUR, myRPT1, myRPT2); + puffer[8], puffer[9], puffer[10], myCall, myCall2, myUR, myRPT1, myRPT2); printf("CRC checksum is BAD, will NOT process this QSO\n"); ok = false; } } - if (ok) { + if (ok) + { time(&last_RF_time); printf("From RF: flags=%02x:%02x:%02x, myCall=%s/%s, yrCall=%s, rpt1=%s, rpt2=%s\n", - puffer[8], puffer[9], puffer[10], myCall, myCall2, myUR, myRPT1, myRPT2); + puffer[8], puffer[9], puffer[10], myCall, myCall2, myUR, myRPT1, myRPT2); /* If rpt1 is equal to rpt2, but the first 7 bytes is not our system, @@ -2111,36 +2225,42 @@ void CQnetDVRPTR::Run() in this case, set rpt1 and rpt2 to our values, because the remote standalone repeater does not have a gateway. */ - if (memcmp(myRPT1, myRPT2, 7) == 0) { + if (memcmp(myRPT1, myRPT2, 7) == 0) + { if ((memcmp(myRPT1, DVCALL, 7) != 0) && - (memcmp(myRPT1, RPTR, 7) != 0)) { + (memcmp(myRPT1, RPTR, 7) != 0)) + { memcpy(myRPT1, DVCALL_and_MOD, 8); memcpy(myRPT2, DVCALL_and_G, 8); } } } - if (ok) { + if (ok) + { /* RPT1 must always be the repeater + module */ memcpy(myRPT1, DVCALL_and_MOD, 8); /* RPT2 must also be valid */ if ((myRPT2[7] == 'A') || - (myRPT2[7] == 'B') || - (myRPT2[7] == 'C') || - (myRPT2[7] == 'G')) + (myRPT2[7] == 'B') || + (myRPT2[7] == 'C') || + (myRPT2[7] == 'G')) memcpy(myRPT2, DVCALL, 7); - else { + else + { printf("myRPT2=%.8s not A, B, C or G, blanking it\n", myRPT2); memset(myRPT2, ' ', 8); } - if ((memcmp(myUR, "CQCQCQ", 6) != 0) && (myRPT2[0] != ' ')) { + if ((memcmp(myUR, "CQCQCQ", 6) != 0) && (myRPT2[0] != ' ')) + { memcpy(myRPT2, DVCALL_and_G, 8); } /* 8th in rpt1, rpt2 must be diff */ - if (myRPT2[7] == myRPT1[7]) { + if (myRPT2[7] == myRPT1[7]) + { memset(myRPT2, ' ', 8); printf("%.8s==%.8s, blanking myRPT2\n", myRPT2, myRPT1); } @@ -2152,22 +2272,29 @@ void CQnetDVRPTR::Run() that means that mycall, rpt1, rpt2 must be equal to RPTR otherwise we drop the rf data */ - if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) { - if (memcmp(myCall, RPTR, CALL_SIZE) != 0) { + if (memcmp(RPTR, DVCALL, CALL_SIZE) != 0) + { + if (memcmp(myCall, RPTR, CALL_SIZE) != 0) + { printf("mycall=[%.8s], not equal to %s\n", myCall, RPTR); ok = false; } - } else if (memcmp(myCall, " ", 8) == 0) { + } + else if (memcmp(myCall, " ", 8) == 0) + { printf("Invalid value for mycall=[%.8s]\n", myCall); ok = false; } } - if (ok) { - for (int i = 0; i < 8; i++) { + if (ok) + { + for (int i = 0; i < 8; i++) + { if (!isupper(myCall[i]) && - !isdigit(myCall[i]) && - (myCall[i] != ' ')) { + !isdigit(myCall[i]) && + (myCall[i] != ' ')) + { memset(myCall, ' ', 8); ok = false; printf("Invalid value for MYCALL\n"); @@ -2175,20 +2302,24 @@ void CQnetDVRPTR::Run() } } - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) + { if (!isupper(myCall2[i]) && - !isdigit(myCall2[i]) && - (myCall2[i] != ' ')) { + !isdigit(myCall2[i]) && + (myCall2[i] != ' ')) + { memset(myCall2, ' ', 4); break; } } - for (int i = 0; i < 8; i++) { + for (int i = 0; i < 8; i++) + { if (!isupper(myUR[i]) && - !isdigit(myUR[i]) && - (myUR[i] != ' ') && - (myUR[i] != '/')) { + !isdigit(myUR[i]) && + (myUR[i] != ' ') && + (myUR[i] != '/')) + { memcpy(myUR, "CQCQCQ ", 8); break; } @@ -2199,11 +2330,15 @@ void CQnetDVRPTR::Run() memcpy(myUR, "CQCQCQ ", 8); } - if (ok) { - if ((memcmp(myUR, ENABLE_RF, 8) == 0) && (ENABLE_RF[0] != ' ')) { + if (ok) + { + if ((memcmp(myUR, ENABLE_RF, 8) == 0) && (ENABLE_RF[0] != ' ')) + { IS_ENABLED = true; memcpy(myUR, "CQCQCQ ", 8); - } else if ((memcmp(myUR, DISABLE_RF, 8) == 0) && (DISABLE_RF[0] != ' ')) { + } + else if ((memcmp(myUR, DISABLE_RF, 8) == 0) && (DISABLE_RF[0] != ' ')) + { IS_ENABLED = false; memcpy(myUR, "CQCQCQ ", 8); } @@ -2217,7 +2352,7 @@ void CQnetDVRPTR::Run() if (myRPT2[7] != ' ') memcpy(myRPT2, DVCALL, 7); - memcpy(Send_Network_Header.title , "DSVT", 4); + memcpy(Send_Network_Header.title, "DSVT", 4); Send_Network_Header.config = 0x10U; memset(Send_Network_Header.flaga, 0U, 3U); Send_Network_Header.id = 0x20U; @@ -2249,8 +2384,10 @@ void CQnetDVRPTR::Run() memcpy(Send_Network_Header.hdr.sfx, myCall2, 4); calcPFCS(Send_Network_Header.title); - if (ok) { - if (IS_ENABLED) { + if (ok) + { + if (IS_ENABLED) + { ToGate.Write(Send_Network_Header.title, 56); } } @@ -2259,7 +2396,8 @@ void CQnetDVRPTR::Run() bytes2 = 0; - if (DUPLEX && !busy20000) { + if (DUPLEX && !busy20000) + { Send_Modem_Header[0] =0xd0; Send_Modem_Header[1] =0x2f; Send_Modem_Header[2] =0x00; @@ -2279,8 +2417,10 @@ void CQnetDVRPTR::Run() ptt_off[4] = seq_no1; start_Header[4]=seq_no1; - if (ok) { - if (IS_ENABLED) { + if (ok) + { + if (IS_ENABLED) + { write(fd_ser, start_Header, sizeof (start_Header)); write(fd_ser, Send_Modem_Header, sizeof (Send_Modem_Header)); } @@ -2299,7 +2439,7 @@ void CQnetDVRPTR::Run() if (last_RF_time > 0) time(&last_RF_time); - memcpy(Send_Network_Audio.title , "DSVT", 4); + memcpy(Send_Network_Audio.title, "DSVT", 4); Send_Network_Audio.config = 0x20U; memset(Send_Network_Audio.flaga, 0U, 3U); Send_Network_Audio.id = 0x20U; @@ -2308,9 +2448,10 @@ void CQnetDVRPTR::Run() Send_Network_Audio.flagb[2] = SND_TERM_ID; Send_Network_Audio.streamid = streamid_raw; Send_Network_Audio.ctrl = seq_no; - memcpy(Send_Network_Audio.vasd.voice , puffer + 8, 12); + memcpy(Send_Network_Audio.vasd.voice, puffer + 8, 12); - if (IS_ENABLED) { + if (IS_ENABLED) + { ToGate.Write(Send_Network_Audio.title, 27); ber_errs = decode.Decode(Send_Network_Audio.vasd.voice, ber_data); num_bit_errors += ber_errs; @@ -2321,7 +2462,8 @@ void CQnetDVRPTR::Run() if (seq_no == 21) seq_no = 0; - if (DUPLEX && !busy20000) { + if (DUPLEX && !busy20000) + { writevoice1[0] =0xD0; writevoice1[1] =0x13; writevoice1[2] =0x00; @@ -2343,8 +2485,9 @@ void CQnetDVRPTR::Run() } if ((puffer[17] == 0x55) && - (puffer[18] == 0x55) && - (puffer[19] == 0x55)) { + (puffer[18] == 0x55) && + (puffer[19] == 0x55)) + { ptt_off[4] = writevoice1[4]; write(fd_ser, ptt_off, 8); } @@ -2365,7 +2508,8 @@ void CQnetDVRPTR::Run() } } - if ((puffer[0] == 0xD0) && (puffer[1] == 0x17) && (puffer[2] == 0x00) && (puffer[3] == 0x91)) { + if ((puffer[0] == 0xD0) && (puffer[1] == 0x17) && (puffer[2] == 0x00) && (puffer[3] == 0x91)) + { rqst_count = RQST_COUNT; printf("DVRPTR Hardware ver: %.20s\n",puffer+6 ); @@ -2385,7 +2529,8 @@ void CQnetDVRPTR::Run() puffer[1] = 0x00; } - if ((puffer[0] == 0xD0) && ((puffer[1] == 0x07) || (puffer[1] == 0x08)) && (puffer[2] == 0x00) && (puffer[3] == 0x90)) { + if ((puffer[0] == 0xD0) && ((puffer[1] == 0x07) || (puffer[1] == 0x08)) && (puffer[2] == 0x00) && (puffer[3] == 0x90)) + { rqst_count = RQST_COUNT; printf("\n------- STATUS READ FROM MODEM ---------\n"); @@ -2401,7 +2546,8 @@ void CQnetDVRPTR::Run() } /* serial */ - if ((puffer[0] == 0xD0) && (puffer[1] == 0x05) && (puffer[2] == 0x00) && (puffer[3] == 0x92)) { + if ((puffer[0] == 0xD0) && (puffer[1] == 0x05) && (puffer[2] == 0x00) && (puffer[3] == 0x92)) + { rqst_count = RQST_COUNT; puffer[1] = 0x00; } @@ -2412,19 +2558,22 @@ void CQnetDVRPTR::Run() time(&tNow); /* If an RF user TXed and disappeared after that */ - if ((last_RF_time > 0) && ((tNow - last_RF_time) > 1)) { + if ((last_RF_time > 0) && ((tNow - last_RF_time) > 1)) + { printf("End RF(Timeout), ber=%.02f\n", - (num_dv_frames == 0) ? 0.0 : 100.0 * ((float)num_bit_errors / (float)(num_dv_frames * 24.0)) ); + (num_dv_frames == 0) ? 0.0 : 100.0 * ((float)num_bit_errors / (float)(num_dv_frames * 24.0)) ); ptt_off[4] = Send_Modem_Header[4]; write(fd_ser, ptt_off, 8); } - if ( ((puffer[0] == 0xD0) && (puffer[1] == 0x03) && (puffer[2] == 0x00) && (puffer[3] == 0x1A)) || ((last_RF_time > 0) && ((tNow - last_RF_time) > 1)) ) { + if ( ((puffer[0] == 0xD0) && (puffer[1] == 0x03) && (puffer[2] == 0x00) && (puffer[3] == 0x1A)) || ((last_RF_time > 0) && ((tNow - last_RF_time) > 1)) ) + { puffer[0] = 0x00; - if (last_RF_time > 0) { + if (last_RF_time > 0) + { const unsigned char silence[12] = { 0x9EU,0x8DU,0x32U,0x88U,0x26U,0x1AU,0x3FU,0x61U,0xE8U,0x70U,0x4FU,0x93U }; const unsigned char silsync[12] = { 0x9EU,0x8DU,0x32U,0x88U,0x26U,0x1AU,0x3FU,0x61U,0xE8U,0x55U,0x2DU,0x16U }; - memcpy(Send_Network_Audio.title , "DSVT", 4); + memcpy(Send_Network_Audio.title, "DSVT", 4); Send_Network_Audio.config = 0x20U; memset(Send_Network_Audio.flaga, 0U, 3U); Send_Network_Audio.id = 0x20U; @@ -2434,8 +2583,10 @@ void CQnetDVRPTR::Run() Send_Network_Audio.ctrl = seq_no | 0x40U; memcpy(Send_Network_Audio.vasd.voice, seq_no ? silsync : silence, 12U); - if (ok) { - if (IS_ENABLED) { + if (ok) + { + if (IS_ENABLED) + { ToGate.Write(Send_Network_Audio.title, 27); } } diff --git a/QnetGateway.cpp b/QnetGateway.cpp index 3bcc537..fdd6310 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -58,24 +58,30 @@ const std::string GW_VERSION("QnetGateway-626"); int CQnetGateway::FindIndex(const int i) const { - if (i<0 || i > 2) - return -1; - int index = Index[i]; - if (index < 0) { - if (AF_INET == link_family[i]) { - index = ii[1] ? 1 : 0; - } else if (AF_INET6 == link_family[i]) { - index = 0; - } - } - return index; + if (i<0 || i > 2) + return -1; + int index = Index[i]; + if (index < 0) + { + if (AF_INET == link_family[i]) + { + index = ii[1] ? 1 : 0; + } + else if (AF_INET6 == link_family[i]) + { + index = 0; + } + } + return index; } bool CQnetGateway::Printable(unsigned char *s) { bool rval = false; - for (unsigned i=0; s[i]; i++) { - if (0 == isprint(s[i])) { + for (unsigned i=0; s[i]; i++) + { + if (0 == isprint(s[i])) + { rval = true; s[i] = '?'; } @@ -93,13 +99,16 @@ void CQnetGateway::UnpackCallsigns(const std::string &str, std::set std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning. std::string::size_type pos = str.find_first_of(delimiters, lastPos); // Find first non-delimiter. - while (std::string::npos != pos || std::string::npos != lastPos) { + while (std::string::npos != pos || std::string::npos != lastPos) + { std::string element = str.substr(lastPos, pos-lastPos); - if (element.length()>=3 && element.length()<=8) { + if (element.length()>=3 && element.length()<=8) + { ToUpper(element); element.resize(CALL_SIZE, ' '); set.insert(element); // Found a token, add it to the list. - } else + } + else fprintf(stderr, "found bad callsign in list: %s\n", str.c_str()); lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters. pos = str.find_first_of(delimiters, lastPos); // Find next non-delimiter. @@ -109,7 +118,8 @@ void CQnetGateway::UnpackCallsigns(const std::string &str, std::set void CQnetGateway::PrintCallsigns(const std::string &key, const std::set &set) { printf("%s = [", key.c_str()); - for (auto it=set.begin(); it!=set.end(); it++) { + for (auto it=set.begin(); it!=set.end(); it++) + { if (it != set.begin()) printf(","); printf("%s", (*it).c_str()); @@ -136,7 +146,8 @@ void CQnetGateway::set_dest_rptr(const char mod, std::string &call) /* compute checksum */ void CQnetGateway::calcPFCS(unsigned char *packet, int len) { - const unsigned short crc_tabccitt[256] = { + const unsigned short crc_tabccitt[256] = + { 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, @@ -158,20 +169,22 @@ void CQnetGateway::calcPFCS(unsigned char *packet, int len) short int low, high; unsigned short tmp; - switch (len) { - case 56: - low = 15; - high = 54; - break; - case 58: - low = 17; - high = 56; - break; - default: - return; + switch (len) + { + case 56: + low = 15; + high = 54; + break; + case 58: + low = 17; + high = 56; + break; + default: + return; } - for (unsigned short int i = low; i < high ; i++) { + for (unsigned short int i = low; i < high ; i++) + { unsigned short short_c = 0x00ff & (unsigned short)packet[i]; tmp = (crc_dstar_ffff & 0x00ff) ^ short_c; crc_dstar_ffff = (crc_dstar_ffff >> 8) ^ crc_tabccitt[tmp]; @@ -179,10 +192,13 @@ void CQnetGateway::calcPFCS(unsigned char *packet, int len) crc_dstar_ffff = ~crc_dstar_ffff; tmp = crc_dstar_ffff; - if (len == 56) { + if (len == 56) + { packet[54] = (unsigned char)(crc_dstar_ffff & 0xff); packet[55] = (unsigned char)((tmp >> 8) & 0xff); - } else { + } + else + { packet[56] = (unsigned char)(crc_dstar_ffff & 0xff); packet[57] = (unsigned char)((tmp >> 8) & 0xff); } @@ -209,25 +225,31 @@ bool CQnetGateway::ReadConfig(char *cfgFile) OWNER.resize(CALL_SIZE, ' '); path.assign("ircddb"); - for (int i=0; i<2; i++) { + for (int i=0; i<2; i++) + { std::string p(path + std::to_string(i) + "_"); cfg.GetValue(p+"host", estr, ircddb[i].ip, 0, MAXHOSTNAMELEN); cfg.GetValue(p+"port", estr, ircddb[i].port, 1000, 65535); cfg.GetValue(p+"password", estr, IRCDDB_PASSWORD[i], 0, 512); } - if ((ircddb[0].ip.size()+ircddb[1].ip.size() > 0) && (0 == ircddb[0].ip.compare(ircddb[1].ip))) { + if ((ircddb[0].ip.size()+ircddb[1].ip.size() > 0) && (0 == ircddb[0].ip.compare(ircddb[1].ip))) + { fprintf(stderr, "IRC networks must be different\n"); return true; } // module - for (int m=0; m<3; m++) { + for (int m=0; m<3; m++) + { path.assign("module_"); path.append(1, 'a' + m); std::string type; - if (cfg.GetValue(path, estr, type, 1, 16)) { + if (cfg.GetValue(path, estr, type, 1, 16)) + { Rptr.mod[m].defined = false; - } else { + } + else + { printf("Found Module: %s = '%s'\n", path.c_str(), type.c_str()); if (0 == type.compare("dvap")) { Rptr.mod[m].package_version.assign(GW_VERSION+".DVAP"); } else if (0 == type.compare("dvrptr")) { Rptr.mod[m].package_version.assign(GW_VERSION+".DVRPTR"); } @@ -235,24 +257,30 @@ bool CQnetGateway::ReadConfig(char *cfgFile) else if (0 == type.compare("mmdvmmodem")) { Rptr.mod[m].package_version.assign(GW_VERSION+".Modem"); } else if (0 == type.compare("itap")) { Rptr.mod[m].package_version.assign(GW_VERSION+".ITAP"); } else if (0 == type.compare("thumbdv")) { Rptr.mod[m].package_version.assign(GW_VERSION+".ThumbDV"); } - else { + else + { printf("module type '%s' is invalid\n", type.c_str()); return true; } Rptr.mod[m].defined = true; path.append(1, '_'); - if (cfg.KeyExists(path+"tx_frequency")) { + if (cfg.KeyExists(path+"tx_frequency")) + { cfg.GetValue(path+"tx_frequency", type, Rptr.mod[m].frequency, 0.0, 6.0E9); double rx_freq; cfg.GetValue(path+"rx_frequency", type, rx_freq, 0.0, 6.0E9); if (0.0 == rx_freq) rx_freq = Rptr.mod[m].frequency; Rptr.mod[m].offset = rx_freq - Rptr.mod[m].frequency; - } else if (cfg.KeyExists(path+"frequency")) { + } + else if (cfg.KeyExists(path+"frequency")) + { cfg.GetValue(path+"frequency", type, Rptr.mod[m].frequency, 0.0, 1.0E9); Rptr.mod[m].offset = 0.0; - } else { + } + else + { Rptr.mod[m].frequency = Rptr.mod[m].offset = 0.0; } cfg.GetValue(path+"range", type, Rptr.mod[m].range, 0.0, 1609344.0); @@ -260,7 +288,8 @@ bool CQnetGateway::ReadConfig(char *cfgFile) cfg.GetValue(path+"is_hf", type, IS_HF[m]); } } - if (! (Rptr.mod[0].defined || Rptr.mod[1].defined || Rptr.mod[2].defined)) { + if (! (Rptr.mod[0].defined || Rptr.mod[1].defined || Rptr.mod[2].defined)) + { printf("No modules defined!\n"); return true; } @@ -275,8 +304,10 @@ bool CQnetGateway::ReadConfig(char *cfgFile) cfg.GetValue(path+"send_qrgs_maps", estr, GATEWAY_SEND_QRGS_MAP); cfg.GetValue(path+"tolink", estr, tolink, 1, FILENAME_MAX); cfg.GetValue(path+"fromremote", estr, fromremote, 1, FILENAME_MAX); - for (int m=0; m<3; m++) { - if (Rptr.mod[m].defined) { + for (int m=0; m<3; m++) + { + if (Rptr.mod[m].defined) + { cfg.GetValue(path+"tomodem"+std::string(1, 'a'+m), estr, tomodem[m], 1, FILENAME_MAX); cfg.GetValue(path+"latitude", estr, Rptr.mod[m].latitude, -90.0, 90.0); cfg.GetValue(path+"longitude", estr, Rptr.mod[m].longitude, -180.0, 180.0); @@ -286,7 +317,8 @@ bool CQnetGateway::ReadConfig(char *cfgFile) } } path.append("find_route"); - if (cfg.KeyExists(path)) { + if (cfg.KeyExists(path)) + { std::string csv; cfg.GetValue(path, estr, csv, 0, 10240); UnpackCallsigns(csv, findRoute); @@ -338,7 +370,8 @@ int CQnetGateway::open_port(const SPORTIP *pip, int family) CSockAddress sin(family, pip->port, pip->ip.c_str()); int sock = socket(family, SOCK_DGRAM, 0); - if (0 > sock) { + if (0 > sock) + { printf("Failed to create socket on %s:%d, errno=%d, %s\n", pip->ip.c_str(), pip->port, errno, strerror(errno)); return -1; } @@ -350,7 +383,8 @@ int CQnetGateway::open_port(const SPORTIP *pip, int family) // return -1; //} - if (bind(sock, sin.GetCPointer(), sizeof(struct sockaddr_storage)) != 0) { + if (bind(sock, sin.GetCPointer(), sizeof(struct sockaddr_storage)) != 0) + { printf("Failed to bind %s:%d, errno=%d, %s\n", pip->ip.c_str(), pip->port, errno, strerror(errno)); close(sock); return -1; @@ -371,9 +405,11 @@ void CQnetGateway::GetIRCDataThread(const int i) not_announced[i] = Rptr.mod[i].defined; // announce to all modules that are defined! bool is_quadnet = (std::string::npos != ircddb[i].ip.find(".openquad.net")); bool doFind = true; - while (IsRunning()) { + while (IsRunning()) + { int rc = ii[i]->getConnectionState(); - if (rc > 5 && rc < 8 && is_quadnet) { + if (rc > 5 && rc < 8 && is_quadnet) + { char ch = '\0'; if (not_announced[0]) ch = 'A'; @@ -381,23 +417,29 @@ void CQnetGateway::GetIRCDataThread(const int i) ch = 'B'; else if (not_announced[2]) ch = 'C'; - if (ch) { + if (ch) + { // we need to announce, but can we? struct stat sbuf; - if (stat(FILE_QNVOICE_FILE.c_str(), &sbuf)) { + if (stat(FILE_QNVOICE_FILE.c_str(), &sbuf)) + { // yes, there is no FILE_QNVOICE_FILE, so create it FILE *fp = fopen(FILE_QNVOICE_FILE.c_str(), "w"); - if (fp) { + if (fp) + { fprintf(fp, "%c_connected2network.dat_WELCOME_TO_QUADNET", ch); fclose(fp); not_announced[ch - 'A'] = false; - } else + } + else fprintf(stderr, "could not open %s\n", FILE_QNVOICE_FILE.c_str()); } } - if (doFind) { + if (doFind) + { printf("Finding Routes for...\n"); - for (auto it=findRoute.begin(); it!=findRoute.end(); it++) { + for (auto it=findRoute.begin(); it!=findRoute.end(); it++) + { std::this_thread::sleep_for(std::chrono::milliseconds(800)); printf("\t'%s'\n", it->c_str()); ii[i]->findUser(*it); @@ -406,19 +448,28 @@ void CQnetGateway::GetIRCDataThread(const int i) } } threshold++; - if (threshold >= 100) { - if ((rc == 0) || (rc == 10)) { - if (last_status != 0) { + if (threshold >= 100) + { + if ((rc == 0) || (rc == 10)) + { + if (last_status != 0) + { printf("irc status=%d, probable disconnect...\n", rc); last_status = 0; } - } else if (rc == 7) { - if (last_status != 2) { + } + else if (rc == 7) + { + if (last_status != 2) + { printf("irc status=%d, probable connect...\n", rc); last_status = 2; } - } else { - if (last_status != 1) { + } + else + { + if (last_status != 1) + { printf("irc status=%d, probable connect...\n", rc); last_status = 1; } @@ -426,27 +477,31 @@ void CQnetGateway::GetIRCDataThread(const int i) threshold = 0; } - while (((type = ii[i]->getMessageType()) != IDRT_NONE) && IsRunning()) { - switch (type) { - case IDRT_PING: { - std::string rptr, gate, addr; - ii[i]->receivePing(rptr); - if (! rptr.empty()) { - ReplaceChar(rptr, '_', ' '); - ii[i]->cache.findRptrData(rptr, gate, addr); - if (addr.empty()) - break; - CSockAddress to; - if (addr.npos == rptr.find(':')) - to.Initialize(AF_INET, (unsigned short)g2_external.port, addr.c_str()); - else - to.Initialize(AF_INET6, (unsigned short)g2_ipv6_external.port, addr.c_str()); - sendto(g2_sock[i], "PONG", 4, 0, to.GetCPointer(), to.GetSize()); - if (LOG_QSO) - printf("Sent 'PONG' to %s\n", addr.c_str()); - } + while (((type = ii[i]->getMessageType()) != IDRT_NONE) && IsRunning()) + { + switch (type) + { + case IDRT_PING: + { + std::string rptr, gate, addr; + ii[i]->receivePing(rptr); + if (! rptr.empty()) + { + ReplaceChar(rptr, '_', ' '); + ii[i]->cache.findRptrData(rptr, gate, addr); + if (addr.empty()) + break; + CSockAddress to; + if (addr.npos == rptr.find(':')) + to.Initialize(AF_INET, (unsigned short)g2_external.port, addr.c_str()); + else + to.Initialize(AF_INET6, (unsigned short)g2_ipv6_external.port, addr.c_str()); + sendto(g2_sock[i], "PONG", 4, 0, to.GetCPointer(), to.GetSize()); + if (LOG_QSO) + printf("Sent 'PONG' to %s\n", addr.c_str()); } - break; + } + break; default: break; } // switch (type) @@ -460,30 +515,34 @@ void CQnetGateway::GetIRCDataThread(const int i) /* return codes: 0=OK(found it), 1=TRY AGAIN, 2=FAILED(bad data) */ int CQnetGateway::get_yrcall_rptr_from_cache(const int i, const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU) { - switch (RoU) { - case 'U': - ii[i]->cache.findUserData(call, rptr, gate, addr); - if (rptr.empty()) { - printf("Could not find last heard repeater for user '%s'\n", call.c_str()); - return 1; - } - break; - case 'R': - rptr.assign(call); - ii[i]->cache.findRptrData(call, gate, addr); - break; - default: - fprintf(stderr, "ERROR: Invalid Rou of '%c'\n", RoU); - return 2; + switch (RoU) + { + case 'U': + ii[i]->cache.findUserData(call, rptr, gate, addr); + if (rptr.empty()) + { + printf("Could not find last heard repeater for user '%s'\n", call.c_str()); + return 1; + } + break; + case 'R': + rptr.assign(call); + ii[i]->cache.findRptrData(call, gate, addr); + break; + default: + fprintf(stderr, "ERROR: Invalid Rou of '%c'\n", RoU); + return 2; } std::string temp; - if (rptr.at(7) == 'G') { + if (rptr.at(7) == 'G') + { fprintf(stderr, "ERROR: Invalid module %c\n", rptr.at(7)); return 2; } - if (addr.empty()) { + if (addr.empty()) + { printf("Couldn't find IP address for %s\n", ('R' == RoU) ? "repeater" : "user"); return 1; } @@ -494,8 +553,10 @@ int CQnetGateway::get_yrcall_rptr(const std::string &call, std::string &rptr, st // returns 0 if unsuccessful, otherwise returns ii index plus one { int rval[2] = { 1, 1 }; - for (int i=0; i<2; i++) { - if (ii[i]) { + for (int i=0; i<2; i++) + { + if (ii[i]) + { rval[i] = get_yrcall_rptr_from_cache(i, call, rptr, gate, addr, RoU); if (0 == rval[i]) return i + 1; @@ -503,18 +564,24 @@ int CQnetGateway::get_yrcall_rptr(const std::string &call, std::string &rptr, st } /* at this point, the data is not in cache */ - for (int i=0; i<2; i++) { - if (ii[i] && (1 == rval[i])) { - if (ii[i]->getConnectionState() > 5) { + for (int i=0; i<2; i++) + { + if (ii[i] && (1 == rval[i])) + { + if (ii[i]->getConnectionState() > 5) + { // we can try a find - if (RoU == 'U') { + if (RoU == 'U') + { printf("User [%s] not in local cache, try again\n", call.c_str()); /*** YRCALL=KJ4NHFBL ***/ if (((call.at(6) == 'A') || (call.at(6) == 'B') || (call.at(6) == 'C')) && (call.at(7) == 'L')) printf("If this was a gateway link request, that is ok\n"); if (!ii[i]->findUser(call)) printf("findUser(%s): Network error\n", call.c_str()); - } else if (RoU == 'R') { + } + else if (RoU == 'R') + { printf("Repeater [%s] not found\n", call.c_str()); } } @@ -531,12 +598,15 @@ bool CQnetGateway::Flag_is_ok(unsigned char flag) void CQnetGateway::ProcessTimeouts() { - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { time_t t_now; /* echotest recording timed out? */ - if (recd[i].last_time != 0) { + if (recd[i].last_time != 0) + { time(&t_now); - if ((t_now - recd[i].last_time) > TIMING_TIMEOUT_ECHO) { + if ((t_now - recd[i].last_time) > TIMING_TIMEOUT_ECHO) + { printf("Inactivity on echotest recording module %c, removing stream id=%04x\n", 'A'+i, ntohs(recd[i].streamid)); recd[i].streamid = 0; @@ -546,9 +616,12 @@ void CQnetGateway::ProcessTimeouts() // printf("Closed echotest audio file:[%s]\n", recd[i].file); /* START: echotest thread setup */ - try { + try + { std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, std::ref(recd[i])); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start echotest thread. Exception: %s\n", e.what()); // when the echotest thread runs, it deletes the file, // Because the echotest thread did NOT start, we delete the file here @@ -559,9 +632,11 @@ void CQnetGateway::ProcessTimeouts() } /* voicemail recording timed out? */ - if (vm[i].last_time != 0) { + if (vm[i].last_time != 0) + { time(&t_now); - if ((t_now - vm[i].last_time) > TIMING_TIMEOUT_VOICEMAIL) { + if ((t_now - vm[i].last_time) > TIMING_TIMEOUT_VOICEMAIL) + { printf("Inactivity on voicemail recording module %c, removing stream id=%04x\n", 'A'+i, ntohs(vm[i].streamid)); vm[i].streamid = 0; @@ -573,12 +648,14 @@ void CQnetGateway::ProcessTimeouts() } // any stream going to local repeater timed out? - if (toRptr[i].last_time != 0) { + if (toRptr[i].last_time != 0) + { time(&t_now); // The stream can be from a cross-band, or from a remote system, // so we could use either FROM_LOCAL_RPTR_TIMEOUT or FROM_REMOTE_G2_TIMEOUT // but FROM_REMOTE_G2_TIMEOUT makes more sense, probably is a bigger number - if ((t_now - toRptr[i].last_time) > TIMING_TIMEOUT_REMOTE_G2) { + if ((t_now - toRptr[i].last_time) > TIMING_TIMEOUT_REMOTE_G2) + { printf("Inactivity to local rptr module %c, removing stream id %04x\n", 'A'+i, ntohs(toRptr[i].saved_hdr.streamid)); // Send end_of_audio to local repeater. @@ -593,9 +670,11 @@ void CQnetGateway::ProcessTimeouts() } /* any stream coming from local repeater timed out ? */ - if (band_txt[i].last_time != 0) { + if (band_txt[i].last_time != 0) + { time(&t_now); - if ((t_now - band_txt[i].last_time) > TIMING_TIMEOUT_LOCAL_RPTR) { + if ((t_now - band_txt[i].last_time) > TIMING_TIMEOUT_LOCAL_RPTR) + { /* This local stream never went to a remote system, so trace the timeout */ if (to_remote_g2[i].toDstar.AddressIsZero()) printf("Inactivity from local rptr module %c, removing stream id %04x\n", 'A'+i, ntohs(band_txt[i].streamID)); @@ -605,9 +684,11 @@ void CQnetGateway::ProcessTimeouts() } /* any stream from local repeater to a remote gateway timed out ? */ - if (! to_remote_g2[i].toDstar.AddressIsZero()) { + if (! to_remote_g2[i].toDstar.AddressIsZero()) + { time(&t_now); - if ((t_now - to_remote_g2[i].last_time) > TIMING_TIMEOUT_LOCAL_RPTR) { + if ((t_now - to_remote_g2[i].last_time) > TIMING_TIMEOUT_LOCAL_RPTR) + { printf("Inactivity from local rptr mod %c, removing stream id %04x\n", 'A'+i, ntohs(to_remote_g2[i].streamid)); to_remote_g2[i].toDstar.Clear(); @@ -622,44 +703,59 @@ bool CQnetGateway::ProcessG2Msg(const unsigned char *data, const int mod, std::s { static unsigned int part[3] = { 0 }; static char txt[3][21]; - if ((data[0] != 0x55u) || (data[1] != 0x2du) || (data[2] != 0x16u)) { - const unsigned char c[3] = { + if ((data[0] != 0x55u) || (data[1] != 0x2du) || (data[2] != 0x16u)) + { + const unsigned char c[3] = + { static_cast(data[0] ^ 0x70u), static_cast(data[1] ^ 0x4fu), static_cast(data[2] ^ 0x93u) }; // unscramble - if (part[mod]) { + if (part[mod]) + { // we are in a message - if (part[mod] % 2) { + if (part[mod] % 2) + { // this is the second part of the 2-frame pair memcpy(txt[mod]+(5u*(part[mod]/2u)+2u), c, 3); - if (++part[mod] > 7) { + if (++part[mod] > 7) + { // we've got everything! part[mod] = 0; // now we can start over if (0 == strncmp(txt[mod], "VIA SMARTGP ", 12)) smrtgrp.assign(txt[mod]+12); - if (smrtgrp.size() < 8) { + if (smrtgrp.size() < 8) + { // something bad happened smrtgrp.empty(); return false; } return true; } - } else { // we'll get here when part[mod] = 2, 4 or 6 + } + else // we'll get here when part[mod] = 2, 4 or 6 + { unsigned int sequence = part[mod]++ / 2; // this is the sequency we are expecting, 1, 2 or 3 - if ((sequence | 0x40u) == c[0]) { + if ((sequence | 0x40u) == c[0]) + { memcpy(txt[mod]+(5u*sequence), c+1, 2); // got it, copy the 2 remainin chars - } else { + } + else + { part[mod] = 0; // unexpected } } - } else if (0x40u == c[0]) { + } + else if (0x40u == c[0]) + { // start a new message memcpy(txt[mod], c+1, 2); memset(txt[mod]+2, 0, 19); part[mod] = 1; } - } else { + } + else + { part[mod] = 0; // messages will never be spread across a superframe } return false; @@ -668,155 +764,192 @@ bool CQnetGateway::ProcessG2Msg(const unsigned char *data, const int mod, std::s void CQnetGateway::ProcessIncomingSD(const SDSVT &dsvt, const int source_sock) { int i; - for (i=0; i<3; i++) { + for (i=0; i<3; i++) + { if (Rptr.mod[i].defined && (toRptr[i].saved_hdr.streamid == dsvt.streamid)) - break; + break; } // if i==3, then the streamid of this voice packet didn't match any module SSD &sd = sdin[i]; - if (VoicePacketIsSync(dsvt.vasd.text)) { + if (VoicePacketIsSync(dsvt.vasd.text)) + { sd.first = true; return; } - const unsigned char c[3] = { + const unsigned char c[3] = + { static_cast(dsvt.vasd.text[0] ^ 0x70u), static_cast(dsvt.vasd.text[1] ^ 0x4fu), static_cast(dsvt.vasd.text[2] ^ 0x93u) }; // unscramble - if (sd.first) { + if (sd.first) + { // this is the first of a two voice-packet pair // get the "size" and type from the first byte sd.size = 0x0FU & c[0]; - if (sd.size > 5) { + if (sd.size > 5) + { sd.size = 5; } int size = sd.size; if (size > 2) size = 2; sd.type = 0xF0U & c[0]; - switch (sd.type) { - case 0x30U: // GPS data - if (sd.size + sd.ig < 255) { - memcpy(sd.gps+sd.ig, c+1, size); - if (c[1]=='\r' || c[2]=='\r') { - sd.gps[sd.ig + ((c[1] == '\r') ? 0 : 1)] = '\0'; - if (i < 3) { - Printable(sd.gps); - if (showLastHeard && gps.Parse((const char *)&sd.gps)) { - char call[CALL_SIZE+1]; - memcpy(call, toRptr[i].saved_hdr.hdr.mycall, CALL_SIZE); - call[CALL_SIZE] = '\0'; - qnDB.UpdatePosition(call, gps.MaidenHead(), gps.Latitude(), gps.Longitude()); - } + switch (sd.type) + { + case 0x30U: // GPS data + if (sd.size + sd.ig < 255) + { + memcpy(sd.gps+sd.ig, c+1, size); + if (c[1]=='\r' || c[2]=='\r') + { + sd.gps[sd.ig + ((c[1] == '\r') ? 0 : 1)] = '\0'; + if (i < 3) + { + Printable(sd.gps); + if (showLastHeard && gps.Parse((const char *)&sd.gps)) + { + char call[CALL_SIZE+1]; + memcpy(call, toRptr[i].saved_hdr.hdr.mycall, CALL_SIZE); + call[CALL_SIZE] = '\0'; + qnDB.UpdatePosition(call, gps.MaidenHead(), gps.Latitude(), gps.Longitude()); } - sd.ig = sd.size = 0; - } else { - sd.ig += size; - sd.size -= size; } - } else { - printf("GPS string is too large at %d bytes\n", sd.ig + sd.size); sd.ig = sd.size = 0; } - sd.first = false; - break; - case 0x40U: // 20 character user message - if (sd.size * 5 == sd.im) { - memcpy(sd.message+sd.im, c+1, 2); - sd.im += 2; - sd.size = 3; - } else { - //printf("A message voiceframe, #%d, is out of order because message size is %d\n", sd.size, sd.im); - sd.im = sd.size = 0; + else + { + sd.ig += size; + sd.size -= size; } - sd.first = false; - break; - case 0x50U: // header - if (3 == i) { // only when the streamid can't be matched - if (sd.size + sd.ih < 42) { // make sure there's room - memcpy(sd.header+sd.ih, c+1, size); - sd.ih += size; - if (sd.ih == 41) { // we have liftoff, calculate the checksum - memcpy(sdheader.hdr.flag, sd.header, 39); - calcPFCS(sdheader.title, 56); - if (0 == memcmp(sd.header+39, sdheader.hdr.pfcs, 2)) { // checksum looks okay - int mod = sdheader.hdr.rpt2[CALL_SIZE-1] - 'A'; // the sd header lists the gateway first, so we check here to see if there's a match - if (mod >= 0 && mod < 3 && Rptr.mod[mod].defined) { - printf("Got a slow data header: %36.36s\n", sd.header+3); - unsigned char call[CALL_SIZE]; // swap rpt1 and rpt2 - memcpy(call, sdheader.hdr.rpt1, CALL_SIZE); - memcpy(sdheader.hdr.rpt1, sdheader.hdr.rpt2, CALL_SIZE); - memcpy(sdheader.hdr.rpt2, call, CALL_SIZE); - calcPFCS(sdheader.title, 56); - ProcessG2Header(sdheader, source_sock); // start the voice stream - sd.ih = sd.size = 0; - } else { - fprintf(stderr, "Got a valid slow data header but module %d doesn't exist\n", mod); - } + } + else + { + printf("GPS string is too large at %d bytes\n", sd.ig + sd.size); + sd.ig = sd.size = 0; + } + sd.first = false; + break; + case 0x40U: // 20 character user message + if (sd.size * 5 == sd.im) + { + memcpy(sd.message+sd.im, c+1, 2); + sd.im += 2; + sd.size = 3; + } + else + { + //printf("A message voiceframe, #%d, is out of order because message size is %d\n", sd.size, sd.im); + sd.im = sd.size = 0; + } + sd.first = false; + break; + case 0x50U: // header + if (3 == i) // only when the streamid can't be matched + { + if (sd.size + sd.ih < 42) // make sure there's room + { + memcpy(sd.header+sd.ih, c+1, size); + sd.ih += size; + if (sd.ih == 41) // we have liftoff, calculate the checksum + { + memcpy(sdheader.hdr.flag, sd.header, 39); + calcPFCS(sdheader.title, 56); + if (0 == memcmp(sd.header+39, sdheader.hdr.pfcs, 2)) // checksum looks okay + { + int mod = sdheader.hdr.rpt2[CALL_SIZE-1] - 'A'; // the sd header lists the gateway first, so we check here to see if there's a match + if (mod >= 0 && mod < 3 && Rptr.mod[mod].defined) + { + printf("Got a slow data header: %36.36s\n", sd.header+3); + unsigned char call[CALL_SIZE]; // swap rpt1 and rpt2 + memcpy(call, sdheader.hdr.rpt1, CALL_SIZE); + memcpy(sdheader.hdr.rpt1, sdheader.hdr.rpt2, CALL_SIZE); + memcpy(sdheader.hdr.rpt2, call, CALL_SIZE); + calcPFCS(sdheader.title, 56); + ProcessG2Header(sdheader, source_sock); // start the voice stream + sd.ih = sd.size = 0; + } + else + { + fprintf(stderr, "Got a valid slow data header but module %d doesn't exist\n", mod); } } - } else { - //printf("Header overflow, message has %d bytes, trying to add %d more\n", sd.ih, sd.size); - sd.ih = sd.size = 0; } } - sd.first = false; - break; + else + { + //printf("Header overflow, message has %d bytes, trying to add %d more\n", sd.ih, sd.size); + sd.ih = sd.size = 0; + } + } + sd.first = false; + break; } - } else { + } + else + { // this is the second of a two voice-frame pair sd.first = true; if (0 == sd.size) return; - switch (sd.type) { - case 0x30U: // GPS - memcpy(sd.gps+sd.ig, c, sd.size); - if (c[0]=='\r' || c[1]=='\r' || c[2]=='\r') { - if (c[0]=='\r') - sd.gps[sd.ig] = '\0'; - else if (c[1]=='\r') - sd.gps[sd.ig+1] = '\0'; - else - sd.gps[sd.ig+2] = '\0'; - if (i < 3) { - Printable(sd.gps); - if (showLastHeard && gps.Parse((const char *)&sd.gps)) { - char call[CALL_SIZE+1]; - memcpy(call, toRptr[i].saved_hdr.hdr.mycall, CALL_SIZE); - call[CALL_SIZE] = '\0'; - qnDB.UpdatePosition(call, gps.MaidenHead(), gps.Latitude(), gps.Longitude()); - } - } - sd.ig = 0; - } else { - sd.ig += sd.size; - sd.gps[sd.ig] = 0; - } - break; - case 0x40U: // message - memcpy(sd.message+sd.im, c, 3); - sd.im += 3; - if (sd.im >= 20) { - sd.message[20] = '\0'; - Printable(sd.message); - if (showLastHeard && (i < 3) && memcmp(toRptr[i].saved_hdr.hdr.sfx, "RPTR", 4) && memcmp(sd.message, "VIA SMARTGP", 11)) { + switch (sd.type) + { + case 0x30U: // GPS + memcpy(sd.gps+sd.ig, c, sd.size); + if (c[0]=='\r' || c[1]=='\r' || c[2]=='\r') + { + if (c[0]=='\r') + sd.gps[sd.ig] = '\0'; + else if (c[1]=='\r') + sd.gps[sd.ig+1] = '\0'; + else + sd.gps[sd.ig+2] = '\0'; + if (i < 3) + { + Printable(sd.gps); + if (showLastHeard && gps.Parse((const char *)&sd.gps)) + { char call[CALL_SIZE+1]; memcpy(call, toRptr[i].saved_hdr.hdr.mycall, CALL_SIZE); call[CALL_SIZE] = '\0'; - qnDB.UpdateMessage(call, (const char *)&(sd.message)); + qnDB.UpdatePosition(call, gps.MaidenHead(), gps.Latitude(), gps.Longitude()); } - sd.im = 0; } - break; - case 0x50U: // header - if ((3 == i) && sd.size) { - memcpy(sd.header+sd.ih, c, 3); - sd.ih += 3; + sd.ig = 0; + } + else + { + sd.ig += sd.size; + sd.gps[sd.ig] = 0; + } + break; + case 0x40U: // message + memcpy(sd.message+sd.im, c, 3); + sd.im += 3; + if (sd.im >= 20) + { + sd.message[20] = '\0'; + Printable(sd.message); + if (showLastHeard && (i < 3) && memcmp(toRptr[i].saved_hdr.hdr.sfx, "RPTR", 4) && memcmp(sd.message, "VIA SMARTGP", 11)) + { + char call[CALL_SIZE+1]; + memcpy(call, toRptr[i].saved_hdr.hdr.mycall, CALL_SIZE); + call[CALL_SIZE] = '\0'; + qnDB.UpdateMessage(call, (const char *)&(sd.message)); } - break; + sd.im = 0; + } + break; + case 0x50U: // header + if ((3 == i) && sd.size) + { + memcpy(sd.header+sd.ih, c, 3); + sd.ih += 3; + } + break; } } } @@ -827,73 +960,94 @@ void CQnetGateway::ProcessOutGoingSD(const SDSVT &dsvt, const int i) return; SSD &sd = sdout[i]; - if (VoicePacketIsSync(dsvt.vasd.text)) { + if (VoicePacketIsSync(dsvt.vasd.text)) + { sd.first = true; return; } - const unsigned char c[3] = { + const unsigned char c[3] = + { static_cast(dsvt.vasd.text[0] ^ 0x70u), static_cast(dsvt.vasd.text[1] ^ 0x4fu), static_cast(dsvt.vasd.text[2] ^ 0x93u) }; // unscramble - if (sd.first) { + if (sd.first) + { // this is the first of a two voice-packet pair // get the "size" and type from the first byte sd.size = 0x0FU & c[0]; - if (sd.size > 5) { + if (sd.size > 5) + { sd.size = 5; } int size = sd.size; if (size > 2) size = 2; sd.type = 0xF0U & c[0]; - switch (sd.type) { - case 0x30U: // GPS data - if (sd.size + sd.ig < 255) { - memcpy(sd.gps+sd.ig, c+1, size); - if (c[1]=='\r' || c[2]=='\r') { - sd.gps[sd.ig + ((c[1] == '\r') ? 0 : 1)] = '\0'; - if (i < 3) { - Printable(sd.gps); - if (showLastHeard && gps.Parse((const char *)&sd.gps)) { - qnDB.UpdatePosition(band_txt[i].mycall.c_str(), gps.MaidenHead(), gps.Latitude(), gps.Longitude()); - if (APRS_ENABLE && (! band_txt[i].is_gps_sent) && (time(NULL) - band_txt[i].gps_last_time > 30)) { - std::string call(band_txt[i].mycall); - const char *s = gps.APRS(call, Rptr.mod[i].call.c_str()); - if (! aprs->aprs_sock.Write((unsigned char *)s, strlen(s))) { - time(&band_txt[i].gps_last_time); - band_txt[i].is_gps_sent = true; - } + switch (sd.type) + { + case 0x30U: // GPS data + if (sd.size + sd.ig < 255) + { + memcpy(sd.gps+sd.ig, c+1, size); + if (c[1]=='\r' || c[2]=='\r') + { + sd.gps[sd.ig + ((c[1] == '\r') ? 0 : 1)] = '\0'; + if (i < 3) + { + Printable(sd.gps); + if (showLastHeard && gps.Parse((const char *)&sd.gps)) + { + qnDB.UpdatePosition(band_txt[i].mycall.c_str(), gps.MaidenHead(), gps.Latitude(), gps.Longitude()); + if (APRS_ENABLE && (! band_txt[i].is_gps_sent) && (time(NULL) - band_txt[i].gps_last_time > 30)) + { + std::string call(band_txt[i].mycall); + const char *s = gps.APRS(call, Rptr.mod[i].call.c_str()); + if (! aprs->aprs_sock.Write((unsigned char *)s, strlen(s))) + { + time(&band_txt[i].gps_last_time); + band_txt[i].is_gps_sent = true; } } } - sd.ig = sd.size = 0; - } else { - sd.ig += size; - sd.size -= size; } - } else { - printf("GPS string is too large at %d bytes\n", sd.ig + sd.size); sd.ig = sd.size = 0; } - sd.first = false; - break; - case 0x40U: // 20 character user message - if (sd.size * 5 == sd.im) { - memcpy(sd.message+sd.im, c+1, 2); - sd.im += 2; - sd.size = 3; - } else { - //printf("A message voiceframe, #%d, is out of order because message size is %d\n", sd.size, sd.im); - sd.im = sd.size = 0; + else + { + sd.ig += size; + sd.size -= size; } - sd.first = false; - break; + } + else + { + printf("GPS string is too large at %d bytes\n", sd.ig + sd.size); + sd.ig = sd.size = 0; + } + sd.first = false; + break; + case 0x40U: // 20 character user message + if (sd.size * 5 == sd.im) + { + memcpy(sd.message+sd.im, c+1, 2); + sd.im += 2; + sd.size = 3; + } + else + { + //printf("A message voiceframe, #%d, is out of order because message size is %d\n", sd.size, sd.im); + sd.im = sd.size = 0; + } + sd.first = false; + break; } - } else { - if (! band_txt[i].sent_key_on_msg && vPacketCount[i] > 100) { + } + else + { + if (! band_txt[i].sent_key_on_msg && vPacketCount[i] > 100) + { // 100 voice packets received and still no 20-char message! band_txt[i].txt.clear(); if (0 == band_txt[i].urcall.compare(0, 6, "CQCQCQ")) @@ -908,59 +1062,70 @@ void CQnetGateway::ProcessOutGoingSD(const SDSVT &dsvt, const int i) sd.first = true; if (0 == sd.size) return; - switch (sd.type) { - case 0x30U: // GPS - memcpy(sd.gps+sd.ig, c, sd.size); - if (c[0]=='\r' || c[1]=='\r' || c[2]=='\r') { - if (c[0]=='\r') - sd.gps[sd.ig] = '\0'; - else if (c[1]=='\r') - sd.gps[sd.ig+1] = '\0'; - else - sd.gps[sd.ig+2] = '\0'; - if (i < 3) { - Printable(sd.gps); - if (showLastHeard && gps.Parse((const char *)&sd.gps)) { - qnDB.UpdatePosition(band_txt[i].mycall.c_str(), gps.MaidenHead(), gps.Latitude(), gps.Longitude()); - if (APRS_ENABLE && (! band_txt[i].is_gps_sent) && (time(NULL) - band_txt[i].gps_last_time > 30)) { - std::string call(band_txt[i].mycall); - const char *s = gps.APRS(call, Rptr.mod[i].call.c_str()); - if (! aprs->aprs_sock.Write((unsigned char *)s, strlen(s))) { - time(&band_txt[i].gps_last_time); - band_txt[i].is_gps_sent = true; - } + switch (sd.type) + { + case 0x30U: // GPS + memcpy(sd.gps+sd.ig, c, sd.size); + if (c[0]=='\r' || c[1]=='\r' || c[2]=='\r') + { + if (c[0]=='\r') + sd.gps[sd.ig] = '\0'; + else if (c[1]=='\r') + sd.gps[sd.ig+1] = '\0'; + else + sd.gps[sd.ig+2] = '\0'; + if (i < 3) + { + Printable(sd.gps); + if (showLastHeard && gps.Parse((const char *)&sd.gps)) + { + qnDB.UpdatePosition(band_txt[i].mycall.c_str(), gps.MaidenHead(), gps.Latitude(), gps.Longitude()); + if (APRS_ENABLE && (! band_txt[i].is_gps_sent) && (time(NULL) - band_txt[i].gps_last_time > 30)) + { + std::string call(band_txt[i].mycall); + const char *s = gps.APRS(call, Rptr.mod[i].call.c_str()); + if (! aprs->aprs_sock.Write((unsigned char *)s, strlen(s))) + { + time(&band_txt[i].gps_last_time); + band_txt[i].is_gps_sent = true; } } } - sd.ig = 0; - } else { - sd.ig += sd.size; } - break; - case 0x40U: // message - memcpy(sd.message+sd.im, c, 3); - sd.im += 3; - if (sd.im >= 20) { - sd.message[20] = '\0'; - Printable(sd.message); - if (! band_txt[i].sent_key_on_msg) { - if (0 == band_txt[i].urcall.compare(0, 6, "CQCQCQ")) - set_dest_rptr(i+'A', band_txt[i].dest_rptr); - - int x = FindIndex(i); - if (x >= 0) - ii[x]->sendHeardWithTXMsg(band_txt[i].mycall, band_txt[i].sfx, band_txt[i].urcall, band_txt[i].rpt1, band_txt[i].rpt2, band_txt[i].flags[0], band_txt[i].flags[1], band_txt[i].flags[2], IS_HF[i] ? "" : band_txt[i].dest_rptr, (const char *)sd.message); - band_txt[i].sent_key_on_msg = true; - } - if (showLastHeard && (i < 3) && memcmp(toRptr[i].saved_hdr.hdr.sfx, "RPTR", 4) && memcmp(sd.message, "VIA SMARTGP", 11)) { - char call[CALL_SIZE+1]; - memcpy(call, band_txt[i].mycall.c_str(), CALL_SIZE); - call[CALL_SIZE] = '\0'; - qnDB.UpdateMessage(call, (const char *)&(sd.message)); - } - sd.im = 0; + sd.ig = 0; + } + else + { + sd.ig += sd.size; + } + break; + case 0x40U: // message + memcpy(sd.message+sd.im, c, 3); + sd.im += 3; + if (sd.im >= 20) + { + sd.message[20] = '\0'; + Printable(sd.message); + if (! band_txt[i].sent_key_on_msg) + { + if (0 == band_txt[i].urcall.compare(0, 6, "CQCQCQ")) + set_dest_rptr(i+'A', band_txt[i].dest_rptr); + + int x = FindIndex(i); + if (x >= 0) + ii[x]->sendHeardWithTXMsg(band_txt[i].mycall, band_txt[i].sfx, band_txt[i].urcall, band_txt[i].rpt1, band_txt[i].rpt2, band_txt[i].flags[0], band_txt[i].flags[1], band_txt[i].flags[2], IS_HF[i] ? "" : band_txt[i].dest_rptr, (const char *)sd.message); + band_txt[i].sent_key_on_msg = true; } - break; + if (showLastHeard && (i < 3) && memcmp(toRptr[i].saved_hdr.hdr.sfx, "RPTR", 4) && memcmp(sd.message, "VIA SMARTGP", 11)) + { + char call[CALL_SIZE+1]; + memcpy(call, band_txt[i].mycall.c_str(), CALL_SIZE); + call[CALL_SIZE] = '\0'; + qnDB.UpdateMessage(call, (const char *)&(sd.message)); + } + sd.im = 0; + } + break; } } } @@ -970,13 +1135,16 @@ void CQnetGateway::ProcessG2Header(const SDSVT &g2buf, const int source_sock) // Find out the local repeater module IP/port to send the data to int i = g2buf.hdr.rpt1[7] - 'A'; /* valid repeater module? */ - if (i>=0 && i<3 && Rptr.mod[i].defined) { + if (i>=0 && i<3 && Rptr.mod[i].defined) + { // toRptr[i] is active if a remote system is talking to it or // toRptr[i] is receiving data from a cross-band - if (0==toRptr[i].last_time && 0==band_txt[i].last_time && (Flag_is_ok(g2buf.hdr.flag[0]) || 0x01U==g2buf.hdr.flag[0] || 0x40U==g2buf.hdr.flag[0])) { + if (0==toRptr[i].last_time && 0==band_txt[i].last_time && (Flag_is_ok(g2buf.hdr.flag[0]) || 0x01U==g2buf.hdr.flag[0] || 0x40U==g2buf.hdr.flag[0])) + { superframe[i].clear(); sdin[i].Init(); // with a header, we should reset the Sd structs - if (LOG_QSO) { + if (LOG_QSO) + { printf("id=%04x flags=%02x:%02x:%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s ", ntohs(g2buf.streamid), g2buf.hdr.flag[0], g2buf.hdr.flag[1], g2buf.hdr.flag[2], g2buf.hdr.urcall, g2buf.hdr.rpt1, g2buf.hdr.rpt2, g2buf.hdr.mycall, g2buf.hdr.sfx); if (source_sock >= 0) printf("IP=[%s]:%u\n", fromDstar.GetAddress(), fromDstar.GetPort()); @@ -984,7 +1152,8 @@ void CQnetGateway::ProcessG2Header(const SDSVT &g2buf, const int source_sock) printf("UnixSock=%s\n", tolink.c_str()); } lhcallsign[i].assign((const char *)g2buf.hdr.mycall, 8); - if (showLastHeard && memcmp(g2buf.hdr.sfx, "RPTR", 4) && std::regex_match(lhcallsign[i].c_str(), preg)) { + if (showLastHeard && memcmp(g2buf.hdr.sfx, "RPTR", 4) && std::regex_match(lhcallsign[i].c_str(), preg)) + { lhsfx[i].assign((const char *)g2buf.hdr.sfx, 4); std::string reflector((const char *)g2buf.hdr.urcall, 8); if (0 == reflector.compare("CQCQCQ ")) @@ -998,11 +1167,12 @@ void CQnetGateway::ProcessG2Header(const SDSVT &g2buf, const int source_sock) nextctrl[i] = 0U; /* save the header */ - if (source_sock < 0) { + if (source_sock < 0) + { //if (AF_INET == af_family) { - char address[16]; - snprintf(address, 16, "%d.0.0.0", i); - fromDstar.Initialize(AF_INET, 40000U, address); + char address[16]; + snprintf(address, 16, "%d.0.0.0", i); + fromDstar.Initialize(AF_INET, 40000U, address); //} else { // char address[8]; // snprintf(address, 8, "%d::", i); @@ -1022,79 +1192,107 @@ void CQnetGateway::ProcessG2Header(const SDSVT &g2buf, const int source_sock) void CQnetGateway::ProcessG2(const ssize_t g2buflen, SDSVT &g2buf, const int source_sock) // source_sock is the socket number of the incoming data, or -1 if it's a unix socket { - if ( (g2buflen==56 || g2buflen==27) && 0==memcmp(g2buf.title, "DSVT", 4) && (g2buf.config==0x10 || g2buf.config==0x20) && g2buf.id==0x20) { - if (g2buflen == 56) { + if ( (g2buflen==56 || g2buflen==27) && 0==memcmp(g2buf.title, "DSVT", 4) && (g2buf.config==0x10 || g2buf.config==0x20) && g2buf.id==0x20) + { + if (g2buflen == 56) + { ProcessG2Header(g2buf, source_sock); - } else { // g2buflen == 27 + } + else // g2buflen == 27 + { ProcessIncomingSD(g2buf, source_sock); /* find out which repeater module to send the data to */ int i; - for (i=0; i<3; i++) { - if (Rptr.mod[i].defined) { + for (i=0; i<3; i++) + { + if (Rptr.mod[i].defined) + { /* streamid match ? */ bool match = (toRptr[i].saved_hdr.streamid == g2buf.streamid); - if (match) { - if (LOG_DEBUG) { + if (match) + { + if (LOG_DEBUG) + { const unsigned int ctrl = g2buf.ctrl & 0x1FU; - if (VoicePacketIsSync(g2buf.vasd.text)) { - if (superframe[i].size() > 65U) { + if (VoicePacketIsSync(g2buf.vasd.text)) + { + if (superframe[i].size() > 65U) + { printf("Frame[%c]: %s\n", 'A'+i, superframe[i].c_str()); superframe[i].clear(); } const char *ch = "#abcdefghijklmnopqrstuvwxyz"; superframe[i].append(1, (ctrl<27U) ? ch[ctrl] : '%' ); - } else { + } + else + { const char *ch = "!ABCDEFGHIJKLMNOPQRSTUVWXYZ"; superframe[i].append(1, (ctrl<27U) ? ch[ctrl] : '*' ); } } int diff = int(0x1FU & g2buf.ctrl) - int(nextctrl[i]); - if (diff) { + if (diff) + { if (diff < 0) diff += 21; - if (diff < 6) { // fill up to 5 missing voice frames + if (diff < 6) // fill up to 5 missing voice frames + { if (LOG_DEBUG) fprintf(stderr, "Warning: inserting %d missing voice frame(s)\n", diff); SDSVT dsvt; memcpy(dsvt.title, g2buf.title, 14U); // everything but the ctrl and voice data const unsigned char quite[9] = { 0x9EU, 0x8DU, 0x32U, 0x88U, 0x26U, 0x1AU, 0x3FU, 0x61U, 0xE8U }; memcpy(dsvt.vasd.voice, quite, 9U); - while (diff-- > 0) { + while (diff-- > 0) + { dsvt.ctrl = nextctrl[i]++; nextctrl[i] %= 21U; - if (dsvt.ctrl) { + if (dsvt.ctrl) + { const unsigned char silence[3] = { 0x70U, 0x4FU, 0x93U }; memcpy(dsvt.vasd.voice, silence, 3U); - } else { + } + else + { const unsigned char sync[3] = { 0x55U, 0x2DU, 0x16U }; memcpy(dsvt.vasd.voice, sync, 3U); } ToModem[i].Write(dsvt.title, 27); } - } else { + } + else + { if (LOG_DEBUG) printf("missing %d packets from voice stream on module %c, resetting\n", diff, 'A'+i); nextctrl[i] = g2buf.ctrl; } } - if ((nextctrl[i] == (0x1FU & g2buf.ctrl)) || (0x40U & g2buf.ctrl)) { + if ((nextctrl[i] == (0x1FU & g2buf.ctrl)) || (0x40U & g2buf.ctrl)) + { // no matter what, we will send this on if it is the closing frame - if (0x40U & g2buf.ctrl) { + if (0x40U & g2buf.ctrl) + { g2buf.ctrl = (nextctrl[i] | 0x40U); - } else { + } + else + { g2buf.ctrl = nextctrl[i]; nextctrl[i] = (nextctrl[i] + 1U) % 21U; } ToModem[i].Write(g2buf.title, 27); - if (source_sock >= 0 && showLastHeard) { + if (source_sock >= 0 && showLastHeard) + { std::string smartgroup; - if(ProcessG2Msg(g2buf.vasd.text, i, smartgroup)) { + if(ProcessG2Msg(g2buf.vasd.text, i, smartgroup)) + { qnDB.UpdateLH(lhcallsign[i].c_str(), lhsfx[i].c_str(), 'A'+i, smartgroup.c_str()); } } - } else { + } + else + { if (LOG_DEBUG) fprintf(stderr, "Warning: Ignoring packet because its ctrl=0x%02xU and nextctrl=0x%02xU\n", g2buf.ctrl, nextctrl[i]); } @@ -1105,12 +1303,14 @@ void CQnetGateway::ProcessG2(const ssize_t g2buflen, SDSVT &g2buf, const int sou toRptr[i].sequence = g2buf.ctrl; /* End of stream ? */ - if (g2buf.ctrl & 0x40U) { + if (g2buf.ctrl & 0x40U) + { /* clear the saved header */ memset(toRptr[i].saved_hdr.title, 0U, 56U); toRptr[i].last_time = 0; - if (LOG_DEBUG && superframe[i].size()) { + if (LOG_DEBUG && superframe[i].size()) + { printf("Final[%c]: %s\n", 'A'+i, superframe[i].c_str()); superframe[i].clear(); } @@ -1124,21 +1324,26 @@ void CQnetGateway::ProcessG2(const ssize_t g2buflen, SDSVT &g2buf, const int sou } /* no match ? */ - if ((i == 3) && GATEWAY_HEADER_REGEN) { + if ((i == 3) && GATEWAY_HEADER_REGEN) + { /* check if this a continuation of audio that timed out */ if (g2buf.ctrl & 0x40) ; /* we do not care about end-of-QSO */ - else { + else + { /* for which repeater this stream has timed out ? */ - for (i=0; i<3; i++) { + for (i=0; i<3; i++) + { if (! Rptr.mod[i].defined) continue; /* match saved stream ? */ bool match = (toRptr[i].saved_hdr.streamid == g2buf.streamid); - if (match) { + if (match) + { /* repeater module is inactive ? */ - if (toRptr[i].last_time==0 && band_txt[i].last_time==0) { + if (toRptr[i].last_time==0 && band_txt[i].last_time==0) + { printf("Re-generating header for streamID=%04x\n", ntohs(g2buf.streamid)); /* re-generate/send the header */ @@ -1165,23 +1370,28 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) { char tempfile[FILENAME_MAX]; - if (0 == memcmp(dsvt.title, "DSVT", 4)) { - if ( (recvlen==56 || recvlen==27) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U) ) { - if (recvlen == 56) { + if (0 == memcmp(dsvt.title, "DSVT", 4)) + { + if ( (recvlen==56 || recvlen==27) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U) ) + { + if (recvlen == 56) + { if (LOG_QSO) printf("id=%04x start RPTR flag0=%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dsvt.streamid), dsvt.hdr.flag[0], dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); - if (0==memcmp(dsvt.hdr.rpt1, "DIRECT ", 8) && 0==memcmp(dsvt.hdr.rpt2, "DIRECT ", 8)) { // DIRECT mode??? + if (0==memcmp(dsvt.hdr.rpt1, "DIRECT ", 8) && 0==memcmp(dsvt.hdr.rpt2, "DIRECT ", 8)) // DIRECT mode??? + { memcpy(dsvt.hdr.rpt1, OWNER.c_str(), 7); - switch (dsvt.flagb[2]) { - case 0x01U: - dsvt.hdr.rpt1[7] = 'B'; - break; - case 0x02U: - dsvt.hdr.rpt1[7] = 'C'; - break; - default: - dsvt.hdr.rpt1[7] = 'A'; - break; + switch (dsvt.flagb[2]) + { + case 0x01U: + dsvt.hdr.rpt1[7] = 'B'; + break; + case 0x02U: + dsvt.hdr.rpt1[7] = 'C'; + break; + default: + dsvt.hdr.rpt1[7] = 'A'; + break; } memcpy(dsvt.hdr.rpt2, OWNER.c_str(), 7); dsvt.hdr.rpt2[7] = 'G'; @@ -1190,13 +1400,15 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) printf("Resetting: r1=%.8s r2=%.8s\n", dsvt.hdr.rpt1, dsvt.hdr.rpt2); } - if (0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && Flag_is_ok(dsvt.hdr.flag[0])) { + if (0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && Flag_is_ok(dsvt.hdr.flag[0])) + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { - vPacketCount[i] = 0; - Index[i] = -1; + if (i>=0 && i<3) + { + vPacketCount[i] = 0; + Index[i] = -1; if (LOG_DTMF) printf("resetting dtmf[%d] (got a header)\n", i); dtmf_last_frame[i] = 0; @@ -1237,7 +1449,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) /* select the band for aprs processing, and lock on the stream ID */ if (APRS_ENABLE) aprs->SelectBand(i, ntohs(dsvt.streamid)); - if (std::regex_match(band_txt[i].mycall, preg)) { + if (std::regex_match(band_txt[i].mycall, preg)) + { qnDB.UpdateLH(band_txt[i].mycall.c_str(), band_txt[i].sfx.c_str(), 'A'+i, "Module "); } } @@ -1255,12 +1468,13 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) printf("MYCALL [%s] failed IRC expression validation\n", call.c_str()); if ( mycall_valid && - memcmp(dsvt.hdr.urcall, "XLX", 3) && // not a reflector - memcmp(dsvt.hdr.urcall, "XRF", 3) && - memcmp(dsvt.hdr.urcall, "REF", 3) && - memcmp(dsvt.hdr.urcall, "DCS", 3) && - dsvt.hdr.urcall[0]!=' ' && // must have something - memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) ) { // urcall is NOT CQCQCQ + memcmp(dsvt.hdr.urcall, "XLX", 3) && // not a reflector + memcmp(dsvt.hdr.urcall, "XRF", 3) && + memcmp(dsvt.hdr.urcall, "REF", 3) && + memcmp(dsvt.hdr.urcall, "DCS", 3) && + dsvt.hdr.urcall[0]!=' ' && // must have something + memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) ) // urcall is NOT CQCQCQ + { std::string user, rptr, gate, addr; if ( dsvt.hdr.urcall[0]=='/' && // repeater routing! 0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && // rpt1 this repeater @@ -1269,12 +1483,15 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) dsvt.hdr.rpt2[7]=='G' && // local Gateway Flag_is_ok(dsvt.hdr.flag[0]) ) { - if (memcmp(dsvt.hdr.urcall+1, OWNER.c_str(), 6)) { // the value after the slash is NOT this repeater + if (memcmp(dsvt.hdr.urcall+1, OWNER.c_str(), 6)) // the value after the slash is NOT this repeater + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { /* one radio user on a repeater module at a time */ - if (to_remote_g2[i].toDstar.AddressIsZero()) { + if (to_remote_g2[i].toDstar.AddressIsZero()) + { /* YRCALL=/repeater + mod */ /* YRCALL=/KJ4NHFB */ @@ -1285,7 +1502,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) user[7] = 'A'; Index[i] = get_yrcall_rptr(user, rptr, gate, addr, 'R'); - if (Index[i]--) { /* it is a repeater */ + if (Index[i]--) /* it is a repeater */ + { // std::string from = OWNER.substr(0, 7); // from.append(1, i+'A'); // ii[Index[i]]->sendPing(user, from); @@ -1314,8 +1532,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) sendto(g2_sock[Index[i]], dsvt.title, 56, 0, to_remote_g2[i].toDstar.GetCPointer(), to_remote_g2[i].toDstar.GetSize()); printf("id=%04x zone route to [%s]:%u ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", - ntohs(dsvt.streamid), to_remote_g2[i].toDstar.GetAddress(), to_remote_g2[i].toDstar.GetPort(), - dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); + ntohs(dsvt.streamid), to_remote_g2[i].toDstar.GetAddress(), to_remote_g2[i].toDstar.GetPort(), + dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); time(&(to_remote_g2[i].last_time)); } @@ -1324,23 +1542,29 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) } } else if (memcmp(dsvt.hdr.urcall, OWNER.c_str(), 7) && // urcall is not this repeater - 0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && // rpt1 is this repeater - (dsvt.hdr.rpt1[7]>='A'&& dsvt.hdr.rpt1[7]<='C') && // mod is A,B,C - 0==memcmp(dsvt.hdr.rpt2, OWNER.c_str(), 7) && // rpt2 is this repeater - dsvt.hdr.rpt2[7]=='G' && // local Gateway - Flag_is_ok(dsvt.hdr.flag[0])) { + 0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && // rpt1 is this repeater + (dsvt.hdr.rpt1[7]>='A'&& dsvt.hdr.rpt1[7]<='C') && // mod is A,B,C + 0==memcmp(dsvt.hdr.rpt2, OWNER.c_str(), 7) && // rpt2 is this repeater + dsvt.hdr.rpt2[7]=='G' && // local Gateway + Flag_is_ok(dsvt.hdr.flag[0])) + { user.assign((char *)dsvt.hdr.urcall, 8); - int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { - Index[i] = get_yrcall_rptr(user, rptr, gate, addr, 'U'); - if (Index[i]--) { - /* destination is a remote system */ - if (0 != gate.compare(0, 7, OWNER, 0, 7)) { + int i = dsvt.hdr.rpt1[7] - 'A'; + if (i>=0 && i<3) + { + Index[i] = get_yrcall_rptr(user, rptr, gate, addr, 'U'); + if (Index[i]--) + { + /* destination is a remote system */ + if (0 != gate.compare(0, 7, OWNER, 0, 7)) + { /* one radio user on a repeater module at a time */ - if (to_remote_g2[i].toDstar.AddressIsZero()) { - if (std::regex_match(user.c_str(), preg)) { + if (to_remote_g2[i].toDstar.AddressIsZero()) + { + if (std::regex_match(user.c_str(), preg)) + { // don't do a ping to a routing group std::string from((const char *)dsvt.hdr.rpt1, 8); ii[Index[i]]->sendPing(gate, from); @@ -1371,110 +1595,129 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) time(&(to_remote_g2[i].last_time)); } } - else - { - int i = dsvt.hdr.rpt1[7] - 'A'; - - if (i>=0 && i<3) { - /* the user we are trying to contact is on our gateway */ - /* make sure they are on a different module */ - if (rptr.at(7) != dsvt.hdr.rpt1[7]) { - /* - The remote repeater has been set, lets fill in the dest_rptr - so that later we can send that to the LIVE web site - */ - band_txt[i].dest_rptr.assign((char *)dsvt.hdr.rpt2, 7); - band_txt[i].dest_rptr.append(1, rptr.at(7)); - - i = rptr.at(7) - 'A'; - - /* valid destination repeater module? */ - if (i>=0 && i<3) { - /* - toRptr[i] : receiving from a remote system or cross-band - band_txt[i] : local RF is talking. - */ - if ((toRptr[i].last_time == 0) && (band_txt[i].last_time == 0)) { - printf("CALLmode cross-banding from mod %c to %c\n", dsvt.hdr.rpt1[7], rptr.at(7)); - - dsvt.hdr.rpt2[7] = rptr.at(7); - dsvt.hdr.rpt1[7] = 'G'; - calcPFCS(dsvt.title, 56); - - ToModem[i].Write(dsvt.title, 56); - - /* time it, in case stream times out */ - time(&toRptr[i].last_time); - - toRptr[i].sequence = dsvt.ctrl; - } + else + { + int i = dsvt.hdr.rpt1[7] - 'A'; + + if (i>=0 && i<3) + { + /* the user we are trying to contact is on our gateway */ + /* make sure they are on a different module */ + if (rptr.at(7) != dsvt.hdr.rpt1[7]) + { + /* + The remote repeater has been set, lets fill in the dest_rptr + so that later we can send that to the LIVE web site + */ + band_txt[i].dest_rptr.assign((char *)dsvt.hdr.rpt2, 7); + band_txt[i].dest_rptr.append(1, rptr.at(7)); + + i = rptr.at(7) - 'A'; + + /* valid destination repeater module? */ + if (i>=0 && i<3) + { + /* + toRptr[i] : receiving from a remote system or cross-band + band_txt[i] : local RF is talking. + */ + if ((toRptr[i].last_time == 0) && (band_txt[i].last_time == 0)) + { + printf("CALLmode cross-banding from mod %c to %c\n", dsvt.hdr.rpt1[7], rptr.at(7)); + + dsvt.hdr.rpt2[7] = rptr.at(7); + dsvt.hdr.rpt1[7] = 'G'; + calcPFCS(dsvt.title, 56); + + ToModem[i].Write(dsvt.title, 56); + + /* time it, in case stream times out */ + time(&toRptr[i].last_time); + + toRptr[i].sequence = dsvt.ctrl; + } } } } else - { + { printf("icom rule: no routing from %.8s to %s%c\n", dsvt.hdr.rpt1, rptr.c_str(), rptr.at(7)); - } + } } } - else - { - if ('L' != dsvt.hdr.urcall[7]) // as long as this doesn't look like a linking command - playNotInCache = true; // we need to wait until user's transmission is over - } + else + { + if ('L' != dsvt.hdr.urcall[7]) // as long as this doesn't look like a linking command + playNotInCache = true; // we need to wait until user's transmission is over + } } } } - else if (0 == memcmp(dsvt.hdr.urcall, " C0", 8)) { + else if (0 == memcmp(dsvt.hdr.urcall, " C0", 8)) + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { /* voicemail file is closed */ - if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) { + if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) + { unlink(vm[i].file); printf("removed voicemail file: %s\n", vm[i].file); vm[i].file[0] = '\0'; - } else + } + else printf("No voicemail to clear or still recording\n"); } } - else if (0 == memcmp(dsvt.hdr.urcall, " R0", 8)) { + else if (0 == memcmp(dsvt.hdr.urcall, " R0", 8)) + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { /* voicemail file is closed */ - if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) { - band_txt[i].last_time = 0; - band_txt[i].streamID = 0U; // prevent vm timeout + if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) + { + band_txt[i].last_time = 0; + band_txt[i].streamID = 0U; // prevent vm timeout snprintf(vm[i].message, 21, "VOICEMAIL ON MOD %c ", 'A'+i); - try { + try + { std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, std::ref(vm[i])); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start voicemail playback. Exception: %s\n", e.what()); } - } else + } + else printf("No voicemail to recall or still recording\n"); } } - else if (0 == memcmp(dsvt.hdr.urcall, " S0", 8)) { + else if (0 == memcmp(dsvt.hdr.urcall, " S0", 8)) + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { if (vm[i].fd >= 0) printf("Already recording for voicemail on mod %d\n", i); - else { + else + { memset(tempfile, '\0', sizeof(tempfile)); snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", FILE_ECHOTEST.c_str(), dsvt.hdr.rpt1[7], "voicemail.dat2"); vm[i].fd = open(tempfile, O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (vm[i].fd < 0) printf("Failed to create file %s for voicemail\n", tempfile); - else { + else + { strcpy(vm[i].file, tempfile); printf("Recording mod %c for voicemail into file:[%s]\n", dsvt.hdr.rpt1[7], vm[i].file); time(&vm[i].last_time); - vm[i].streamid = dsvt.streamid; + vm[i].streamid = dsvt.streamid; memcpy(recbuf.title, dsvt.title, 56); memset(recbuf.hdr.rpt1, ' ', 8); memcpy(recbuf.hdr.rpt1, OWNER.c_str(), OWNER.size()); @@ -1486,25 +1729,29 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) calcPFCS(recbuf.title, 56); - write(vm[i].fd, recbuf.title, 56); + write(vm[i].fd, recbuf.title, 56); } } } } - else if (0 == memcmp(dsvt.hdr.urcall, " E", 8)) { + else if (0 == memcmp(dsvt.hdr.urcall, " E", 8)) + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { if (recd[i].fd >= 0) printf("Already recording for echotest on mod %d\n", i); - else { + else + { memset(tempfile, '\0', sizeof(tempfile)); snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", FILE_ECHOTEST.c_str(), dsvt.hdr.rpt1[7], "echotest.dat"); recd[i].fd = open(tempfile, O_CREAT | O_WRONLY | O_EXCL | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (recd[i].fd < 0) printf("Failed to create file %s for echotest\n", tempfile); - else { + else + { strcpy(recd[i].file, tempfile); printf("Recording mod %c for echotest into file:[%s]\n", dsvt.hdr.rpt1[7], recd[i].file); snprintf(recd[i].message, 21, "ECHO ON MODULE %c ", 'A' + i); @@ -1522,21 +1769,23 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) calcPFCS(recbuf.title, 56); - write (recd[i].fd, recbuf.title, 56); + write (recd[i].fd, recbuf.title, 56); } } } - /* check for cross-banding */ + /* check for cross-banding */ } else if ( 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && // yrcall is CQCQCQ - 0==memcmp(dsvt.hdr.rpt2, OWNER.c_str(), 7) && // rpt1 is this repeater - 0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && // rpt2 is this repeater - (dsvt.hdr.rpt1[7]>='A' && dsvt.hdr.rpt1[7]<='C') && // mod of rpt1 is A,B,C - (dsvt.hdr.rpt2[7]>='A' && dsvt.hdr.rpt2[7]<='C') && // !!! usually G on rpt2, but we see A,B,C with - dsvt.hdr.rpt2[7]!=dsvt.hdr.rpt1[7] ) { // cross-banding? make sure NOT the same + 0==memcmp(dsvt.hdr.rpt2, OWNER.c_str(), 7) && // rpt1 is this repeater + 0==memcmp(dsvt.hdr.rpt1, OWNER.c_str(), 7) && // rpt2 is this repeater + (dsvt.hdr.rpt1[7]>='A' && dsvt.hdr.rpt1[7]<='C') && // mod of rpt1 is A,B,C + (dsvt.hdr.rpt2[7]>='A' && dsvt.hdr.rpt2[7]<='C') && // !!! usually G on rpt2, but we see A,B,C with + dsvt.hdr.rpt2[7]!=dsvt.hdr.rpt1[7] ) // cross-banding? make sure NOT the same + { int i = dsvt.hdr.rpt1[7] - 'A'; - if (i>=0 && i<3) { + if (i>=0 && i<3) + { // The remote repeater has been set, lets fill in the dest_rptr // so that later we can send that to the LIVE web site band_txt[i].dest_rptr.append((char *)dsvt.hdr.rpt2, 8); @@ -1545,10 +1794,12 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) i = dsvt.hdr.rpt2[7] - 'A'; // valid destination repeater module? - if (i>=0 && i<3) { + if (i>=0 && i<3) + { // toRptr[i] : receiving from a remote system or cross-band // band_txt[i] : local RF is talking. - if ((toRptr[i].last_time == 0) && (band_txt[i].last_time == 0)) { + if ((toRptr[i].last_time == 0) && (band_txt[i].last_time == 0)) + { printf("ZONEmode cross-banding from mod %c to %c\n", dsvt.hdr.rpt1[7], dsvt.hdr.rpt2[7]); dsvt.hdr.rpt1[7] = 'G'; @@ -1565,13 +1816,18 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) } } else - { // recvlen is 27 - for (int i=0; i<3; i++) { - if (band_txt[i].streamID == dsvt.streamid) { + { + // recvlen is 27 + for (int i=0; i<3; i++) + { + if (band_txt[i].streamID == dsvt.streamid) + { time(&band_txt[i].last_time); - if (dsvt.ctrl & 0x40) { // end of voice data - if (dtmf_buf_count[i] > 0) { + if (dsvt.ctrl & 0x40) // end of voice data + { + if (dtmf_buf_count[i] > 0) + { std::string dtmf_file(FILE_DTMF); dtmf_file.push_back('/'); dtmf_file.push_back('A'+i); @@ -1579,10 +1835,12 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) if (LOG_DTMF) printf("Saving dtmfs=[%s] into file: [%s]\n", dtmf_buf[i], dtmf_file.c_str()); FILE *dtmf_fp = fopen(dtmf_file.c_str(), "w"); - if (dtmf_fp) { + if (dtmf_fp) + { fprintf(dtmf_fp, "%s\n%s", dtmf_buf[i], band_txt[i].mycall.c_str()); fclose(dtmf_fp); - } else + } + else printf("Failed to create dtmf file %s\n", dtmf_file.c_str()); @@ -1593,32 +1851,40 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) dtmf_counter[i] = 0; dtmf_last_frame[i] = 0; } - if (! band_txt[i].sent_key_on_msg) { + if (! band_txt[i].sent_key_on_msg) + { band_txt[i].txt[0] = '\0'; - if (0 == band_txt[i].urcall.compare(0, 6, "CQCQCQ")) { + if (0 == band_txt[i].urcall.compare(0, 6, "CQCQCQ")) + { set_dest_rptr(i+'A', band_txt[i].dest_rptr); } - int x = FindIndex(i); + int x = FindIndex(i); if (x >= 0) ii[x]->sendHeardWithTXMsg(band_txt[i].mycall, band_txt[i].sfx, band_txt[i].urcall, band_txt[i].rpt1, band_txt[i].rpt2, band_txt[i].flags[0], band_txt[i].flags[1], band_txt[i].flags[2], IS_HF[i] ? "" : band_txt[i].dest_rptr, band_txt[i].txt); band_txt[i].sent_key_on_msg = true; } // send the "key off" message, this will end up in the openquad.net Last Heard webpage. - int index = Index[i]; - if (index < 0) { - if (AF_INET == link_family[i]) { - index = ii[1] ? 1 : 0; - } else if (AF_INET6 == link_family[i]) { - index = 0; - } - } + int index = Index[i]; + if (index < 0) + { + if (AF_INET == link_family[i]) + { + index = ii[1] ? 1 : 0; + } + else if (AF_INET6 == link_family[i]) + { + index = 0; + } + } if (index >= 0) ii[index]->sendHeardWithTXStats(band_txt[i].mycall, band_txt[i].sfx, band_txt[i].urcall, band_txt[i].rpt1, band_txt[i].rpt2, band_txt[i].flags[0], band_txt[i].flags[1], band_txt[i].flags[2], band_txt[i].num_dv_frames, band_txt[i].num_dv_silent_frames, band_txt[i].num_bit_errors); - if (playNotInCache) { + if (playNotInCache) + { // Not in cache, please try again! FILE *fp = fopen(FILE_QNVOICE_FILE.c_str(), "w"); - if (fp) { + if (fp) + { fprintf(fp, "%c_notincache.dat_NOT_IN_CACHE\n", band_txt[i].rpt1.at(7)); fclose(fp); } @@ -1628,7 +1894,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) band_txt[i].Initialize(); } else - { // not the end of the voice stream + { + // not the end of the voice stream int ber_data[3]; int ber_errs = decode.Decode(dsvt.vasd.voice, ber_data); if (ber_data[0] == 0xf85) @@ -1636,17 +1903,21 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) band_txt[i].num_bit_errors += ber_errs; band_txt[i].num_dv_frames++; - if ((ber_data[0] & 0x0ffc) == 0xfc0) { + if ((ber_data[0] & 0x0ffc) == 0xfc0) + { dtmf_digit = (ber_data[0] & 0x03) | ((ber_data[2] & 0x60) >> 3); - if (dtmf_counter[i] > 0) { + if (dtmf_counter[i] > 0) + { if (dtmf_last_frame[i] != dtmf_digit) dtmf_counter[i] = 0; } dtmf_last_frame[i] = dtmf_digit; dtmf_counter[i]++; - if ((dtmf_counter[i] == 5) && (dtmf_digit >= 0) && (dtmf_digit <= 15)) { - if (dtmf_buf_count[i] < MAX_DTMF_BUF) { + if ((dtmf_counter[i] == 5) && (dtmf_digit >= 0) && (dtmf_digit <= 15)) + { + if (dtmf_buf_count[i] < MAX_DTMF_BUF) + { const char *dtmf_chars = "147*2580369#ABCD"; dtmf_buf[i][ dtmf_buf_count[i] ] = dtmf_chars[dtmf_digit]; dtmf_buf_count[i]++; @@ -1654,12 +1925,13 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) } const unsigned char silence[9] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8 }; memcpy(dsvt.vasd.voice, silence, 9); - } else + } + else dtmf_counter[i] = 0; } ProcessOutGoingSD(dsvt, i); } - vPacketCount[i]++; + vPacketCount[i]++; } /* send data to qnlink */ @@ -1669,27 +1941,32 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) if (APRS_ENABLE) aprs->ProcessText(ntohs(dsvt.streamid), dsvt.ctrl, dsvt.vasd.voice); - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { /* find out if data must go to the remote G2 */ - if (to_remote_g2[i].streamid==dsvt.streamid && Index[i]>=0) { + if (to_remote_g2[i].streamid==dsvt.streamid && Index[i]>=0) + { sendto(g2_sock[Index[i]], dsvt.title, 27, 0, to_remote_g2[i].toDstar.GetCPointer(), to_remote_g2[i].toDstar.GetSize()); time(&(to_remote_g2[i].last_time)); /* Is this the end-of-stream */ - if (dsvt.ctrl & 0x40) { + if (dsvt.ctrl & 0x40) + { to_remote_g2[i].toDstar.Clear(); to_remote_g2[i].streamid = 0; to_remote_g2[i].last_time = 0; } break; } - else if (recd[i].fd>=0 && recd[i].streamid==dsvt.streamid) { // Is the data to be recorded for echotest + else if (recd[i].fd>=0 && recd[i].streamid==dsvt.streamid) // Is the data to be recorded for echotest + { time(&recd[i].last_time); write(recd[i].fd, dsvt.vasd.voice, 9); - if ((dsvt.ctrl & 0x40) != 0) { + if ((dsvt.ctrl & 0x40) != 0) + { recd[i].streamid = 0; recd[i].last_time = 0; close(recd[i].fd); @@ -1697,9 +1974,12 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) // printf("Closed echotest audio file:[%s]\n", recd[i].file); /* we are in echotest mode, so play it back */ - try { + try + { std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, std::ref(recd[i])); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("failed to start PlayFileThread. Exception: %s\n", e.what()); // When the echotest thread runs, it deletes the file, // Because the echotest thread did NOT start, we delete the file here @@ -1708,12 +1988,14 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) } break; } - else if ((vm[i].fd >= 0) && (vm[i].streamid==dsvt.streamid)) { // Is the data to be recorded for voicemail + else if ((vm[i].fd >= 0) && (vm[i].streamid==dsvt.streamid)) // Is the data to be recorded for voicemail + { time(&vm[i].last_time); write(vm[i].fd, dsvt.vasd.voice, 9); - if ((dsvt.ctrl & 0x40) != 0) { + if ((dsvt.ctrl & 0x40) != 0) + { vm[i].streamid = 0; vm[i].last_time = 0; close(vm[i].fd); @@ -1722,7 +2004,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) } break; } - else if (toRptr[i].saved_hdr.streamid == dsvt.streamid) { // or maybe this is cross-banding data + else if (toRptr[i].saved_hdr.streamid == dsvt.streamid) // or maybe this is cross-banding data + { ToModem[i].Write(dsvt.title, 27); /* timeit */ @@ -1731,13 +2014,15 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) toRptr[i].sequence = dsvt.ctrl; /* End of stream ? */ - if (dsvt.ctrl & 0x40) { + if (dsvt.ctrl & 0x40) + { toRptr[i].last_time = 0; } break; } } - if (0x40U & dsvt.ctrl) { + if (0x40U & dsvt.ctrl) + { if (LOG_QSO) printf("id=%04x END RPTR\n", ntohs(dsvt.streamid)); } @@ -1750,7 +2035,8 @@ void CQnetGateway::ProcessModem(const ssize_t recvlen, SDSVT &dsvt) void CQnetGateway::Process() { // dtmf stuff initialize - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { dtmf_buf_count[i] = 0; dtmf_buf[i][0] = '\0'; dtmf_last_frame[i] = 0; @@ -1758,21 +2044,30 @@ void CQnetGateway::Process() } std::future aprs_future, irc_data_future[2]; - if (APRS_ENABLE) { // start the beacon thread - try { + if (APRS_ENABLE) // start the beacon thread + { + try + { aprs_future = std::async(std::launch::async, &CQnetGateway::APRSBeaconThread, this); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start the APRSBeaconThread. Exception: %s\n", e.what()); } if (aprs_future.valid()) printf("APRS beacon thread started\n"); } - for (int i=0; i<2; i++) { - if (ii[i]) { - try { // start the IRC read thread + for (int i=0; i<2; i++) + { + if (ii[i]) + { + try // start the IRC read thread + { irc_data_future[i] = std::async(std::launch::async, &CQnetGateway::GetIRCDataThread, this, i); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start GetIRCDataThread[%d]. Exception: %s\n", i, e.what()); SetState(false); } @@ -1783,7 +2078,8 @@ void CQnetGateway::Process() } } - while (IsRunning()) { + while (IsRunning()) + { ProcessTimeouts(); // wait 20 ms max @@ -1795,7 +2091,8 @@ void CQnetGateway::Process() if (g2_sock[1] >= 0) AddFDSet(max_nfds, g2_sock[1], &fdset); AddFDSet(max_nfds, ToLink.GetFD(), &fdset); - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { if (Rptr.mod[i].defined) AddFDSet(max_nfds, ToModem[i].GetFD(), &fdset); } @@ -1806,10 +2103,12 @@ void CQnetGateway::Process() (void)select(max_nfds + 1, &fdset, 0, 0, &tv); // process packets coming from remote G2 - for (int i=0; i<2; i++) { + for (int i=0; i<2; i++) + { if (g2_sock[i] < 0) continue; - if (IsRunning() && FD_ISSET(g2_sock[i], &fdset)) { + if (IsRunning() && FD_ISSET(g2_sock[i], &fdset)) + { SDSVT dsvt; socklen_t fromlen = sizeof(struct sockaddr_storage); ssize_t g2buflen = recvfrom(g2_sock[i], dsvt.title, 56, 0, fromDstar.GetPointer(), &fromlen); @@ -1822,7 +2121,8 @@ void CQnetGateway::Process() } // process packets from qnremote - if (IsRunning() && FD_ISSET(FromRemote.GetFD(), &fdset)) { + if (IsRunning() && FD_ISSET(FromRemote.GetFD(), &fdset)) + { SDSVT dsvt; const ssize_t len = FromRemote.Read(dsvt.title, 56); ProcessModem(len, dsvt); @@ -1830,29 +2130,36 @@ void CQnetGateway::Process() } // process packets from qnlink - if (IsRunning() && FD_ISSET(ToLink.GetFD(), &fdset)) { + if (IsRunning() && FD_ISSET(ToLink.GetFD(), &fdset)) + { SDSVT dsvt; ssize_t g2buflen = ToLink.Read(dsvt.title, 56); - if (16==g2buflen && 0==memcmp(dsvt.title, "LINK", 4)) { - SLINKFAMILY fam; - memcpy(fam.title, dsvt.title, 16); - if (LOG_DEBUG) { - printf("Families of linked nodes: A=AF_%s, B=AF_%s, C=AF_%s\n", - (AF_UNSPEC==fam.family[0]) ? "UNSPEC" : ((AF_INET==fam.family[0]) ? "INET" : "INET6"), - (AF_UNSPEC==fam.family[1]) ? "UNSPEC" : ((AF_INET==fam.family[1]) ? "INET" : "INET6"), - (AF_UNSPEC==fam.family[2]) ? "UNSPEC" : ((AF_INET==fam.family[2]) ? "INET" : "INET6") - ); - } - memcpy(link_family, fam.family, 12); - } else { - ProcessG2(g2buflen, dsvt, -1); - } + if (16==g2buflen && 0==memcmp(dsvt.title, "LINK", 4)) + { + SLINKFAMILY fam; + memcpy(fam.title, dsvt.title, 16); + if (LOG_DEBUG) + { + printf("Families of linked nodes: A=AF_%s, B=AF_%s, C=AF_%s\n", + (AF_UNSPEC==fam.family[0]) ? "UNSPEC" : ((AF_INET==fam.family[0]) ? "INET" : "INET6"), + (AF_UNSPEC==fam.family[1]) ? "UNSPEC" : ((AF_INET==fam.family[1]) ? "INET" : "INET6"), + (AF_UNSPEC==fam.family[2]) ? "UNSPEC" : ((AF_INET==fam.family[2]) ? "INET" : "INET6") + ); + } + memcpy(link_family, fam.family, 12); + } + else + { + ProcessG2(g2buflen, dsvt, -1); + } FD_CLR(ToLink.GetFD(), &fdset); } // process packets coming from local repeater module(s) - for (int i=0; i<3; i++) { - if (IsRunning() && FD_ISSET(ToModem[i].GetFD(), &fdset)) { + for (int i=0; i<3; i++) + { + if (IsRunning() && FD_ISSET(ToModem[i].GetFD(), &fdset)) + { SDSVT dsvt; const ssize_t len = ToModem[i].Read(dsvt.title, 56); if (Rptr.mod[i].defined) @@ -1863,11 +2170,13 @@ void CQnetGateway::Process() } // thread clean-up - if (APRS_ENABLE) { + if (APRS_ENABLE) + { if (aprs_future.valid()) aprs_future.get(); } - for (int i=0; i<2; i++) { + for (int i=0; i<2; i++) + { if (ii[i] && irc_data_future[i].valid()) irc_data_future[i].get(); } @@ -1880,7 +2189,8 @@ void CQnetGateway::compute_aprs_hash() strcpy(rptr_sign, OWNER.c_str()); char *p = strchr(rptr_sign, ' '); - if (!p) { + if (!p) + { printf("Failed to build repeater callsign for aprs hash\n"); return; } @@ -1888,7 +2198,8 @@ void CQnetGateway::compute_aprs_hash() p = rptr_sign; short int len = strlen(rptr_sign); - for (short int i=0; i < len; i+=2) { + for (short int i=0; i < len; i+=2) + { hash ^= (*p++) << 8; hash ^= (*p++); } @@ -1920,8 +2231,10 @@ void CQnetGateway::APRSBeaconThread() time_t last_beacon_time = 0; /* This thread is also saying to the APRS_HOST that we are ALIVE */ - while (IsRunning()) { - if (aprs->aprs_sock.GetFD() == -1) { + while (IsRunning()) + { + if (aprs->aprs_sock.GetFD() == -1) + { aprs->Open(OWNER); if (aprs->aprs_sock.GetFD() == -1) sleep(1); @@ -1930,9 +2243,12 @@ void CQnetGateway::APRSBeaconThread() } time(&tnow); - if ((tnow - last_beacon_time) > (Rptr.aprs_interval * 60)) { - for (short int i=0; i<3; i++) { - if (Rptr.mod[i].defined) { + if ((tnow - last_beacon_time) > (Rptr.aprs_interval * 60)) + { + for (short int i=0; i<3; i++) + { + if (Rptr.mod[i].defined) + { float tmp_lat = fabs(Rptr.mod[i].latitude); float tmp_lon = fabs(Rptr.mod[i].longitude); float lat = floor(tmp_lat); @@ -1963,45 +2279,57 @@ void CQnetGateway::APRSBeaconThread() /* send to aprs */ sprintf(snd_buf, "%s>APJI23,TCPIP*,qAC,%sS:!%s%cD%s%c&RNG%04u %s %s", - Rptr.mod[i].call.c_str(), Rptr.mod[i].call.c_str(), - lat_s, (Rptr.mod[i].latitude < 0.0) ? 'S' : 'N', - lon_s, (Rptr.mod[i].longitude < 0.0) ? 'W' : 'E', - (unsigned int)Rptr.mod[i].range, Rptr.mod[i].band.c_str(), GW_VERSION.c_str()); + Rptr.mod[i].call.c_str(), Rptr.mod[i].call.c_str(), + lat_s, (Rptr.mod[i].latitude < 0.0) ? 'S' : 'N', + lon_s, (Rptr.mod[i].longitude < 0.0) ? 'W' : 'E', + (unsigned int)Rptr.mod[i].range, Rptr.mod[i].band.c_str(), GW_VERSION.c_str()); if (LOG_DEBUG) printf("APRS Beacon =[%s]\n", snd_buf); strcat(snd_buf, "\r\n"); - while (IsRunning()) { - if (aprs->aprs_sock.GetFD() == -1) { + while (IsRunning()) + { + if (aprs->aprs_sock.GetFD() == -1) + { aprs->Open(OWNER); if (aprs->aprs_sock.GetFD() == -1) sleep(1); else THRESHOLD_COUNTDOWN = 15; - } else { + } + else + { int rc = aprs->aprs_sock.Write((unsigned char *)snd_buf, strlen(snd_buf)); - if (rc < 0) { + if (rc < 0) + { if ((errno == EPIPE) || - (errno == ECONNRESET) || - (errno == ETIMEDOUT) || - (errno == ECONNABORTED) || - (errno == ESHUTDOWN) || - (errno == EHOSTUNREACH) || - (errno == ENETRESET) || - (errno == ENETDOWN) || - (errno == ENETUNREACH) || - (errno == EHOSTDOWN) || - (errno == ENOTCONN)) { + (errno == ECONNRESET) || + (errno == ETIMEDOUT) || + (errno == ECONNABORTED) || + (errno == ESHUTDOWN) || + (errno == EHOSTUNREACH) || + (errno == ENETRESET) || + (errno == ENETDOWN) || + (errno == ENETUNREACH) || + (errno == EHOSTDOWN) || + (errno == ENOTCONN)) + { printf("send_aprs_beacon: APRS_HOST closed connection,error=%d\n",errno); aprs->aprs_sock.Close(); - } else if (errno == EWOULDBLOCK) { + } + else if (errno == EWOULDBLOCK) + { std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } else { + } + else + { /* Cant do nothing about it */ printf("send_aprs_beacon failed, error=%d\n", errno); break; } - } else { + } + else + { // printf("APRS beacon sent\n"); break; } @@ -2021,38 +2349,46 @@ void CQnetGateway::APRSBeaconThread() Are we still receiving from APRS host ? */ int rc = aprs->aprs_sock.Read((unsigned char *)rcv_buf, sizeof(rcv_buf)); - if (rc < 0) { + if (rc < 0) + { if ((errno == EPIPE) || - (errno == ECONNRESET) || - (errno == ETIMEDOUT) || - (errno == ECONNABORTED) || - (errno == ESHUTDOWN) || - (errno == EHOSTUNREACH) || - (errno == ENETRESET) || - (errno == ENETDOWN) || - (errno == ENETUNREACH) || - (errno == EHOSTDOWN) || - (errno == ENOTCONN)) { + (errno == ECONNRESET) || + (errno == ETIMEDOUT) || + (errno == ECONNABORTED) || + (errno == ESHUTDOWN) || + (errno == EHOSTUNREACH) || + (errno == ENETRESET) || + (errno == ENETDOWN) || + (errno == ENETUNREACH) || + (errno == EHOSTDOWN) || + (errno == ENOTCONN)) + { printf("send_aprs_beacon: recv error: APRS_HOST closed connection,error=%d\n",errno); aprs->aprs_sock.Close(); } - } else if (rc == 0) { + } + else if (rc == 0) + { printf("send_aprs_beacon: recv: APRS shutdown\n"); aprs->aprs_sock.Close(); - } else + } + else THRESHOLD_COUNTDOWN = 15; std::this_thread::sleep_for(std::chrono::milliseconds(100)); /* 20 seconds passed already ? */ time(&tnow); - if ((tnow - last_keepalive_time) > 20) { + if ((tnow - last_keepalive_time) > 20) + { /* we should be receving keepalive packets ONLY if the connection is alive */ - if (aprs->aprs_sock.GetFD() >= 0) { + if (aprs->aprs_sock.GetFD() >= 0) + { if (THRESHOLD_COUNTDOWN > 0) THRESHOLD_COUNTDOWN--; - if (THRESHOLD_COUNTDOWN == 0) { + if (THRESHOLD_COUNTDOWN == 0) + { printf("APRS host keepalive timeout\n"); aprs->aprs_sock.Close(); } @@ -2074,41 +2410,47 @@ void CQnetGateway::PlayFileThread(SECHO &edata) printf("File to playback:[%s]\n", edata.file); struct stat sbuf; - if (stat(edata.file, &sbuf)) { + if (stat(edata.file, &sbuf)) + { fprintf(stderr, "Can't stat %s\n", edata.file); return; } - if (sbuf.st_size < 65) { - fprintf(stderr, "Error %s file is too small!\n", edata.file); - return; - } + if (sbuf.st_size < 65) + { + fprintf(stderr, "Error %s file is too small!\n", edata.file); + return; + } if ((sbuf.st_size - 56) % 9) printf("Warning %s file size of %ld is unexpected!\n", edata.file, sbuf.st_size); int ambeblocks = ((int)sbuf.st_size - 56) / 9; FILE *fp = fopen(edata.file, "rb"); - if (!fp) { + if (!fp) + { fprintf(stderr, "Failed to open file %s\n", edata.file); return; } - if (1 != fread(dsvt.title, 56, 1, fp)) { - fprintf(stderr, "PlayFile Error: Can't read header from %s\n", edata.file); - fclose(fp); - return; - } + if (1 != fread(dsvt.title, 56, 1, fp)) + { + fprintf(stderr, "PlayFile Error: Can't read header from %s\n", edata.file); + fclose(fp); + return; + } int mod = dsvt.hdr.rpt1[7] - 'A'; - if (! Rptr.mod[mod].defined) { + if (! Rptr.mod[mod].defined) + { fprintf(stderr, "Module %c is not configured, erasing file %s\n", mod+'A', edata.file); unlink(edata.file); return; } - if (mod<0 || mod>2) { + if (mod<0 || mod>2) + { fprintf(stderr, "Unknown module suffix '%s'\n", dsvt.hdr.rpt1); return; } @@ -2123,58 +2465,64 @@ void CQnetGateway::PlayFileThread(SECHO &edata) dsvt.config = 0x20U; - for (int i=0; irptrQTH(rptrcall, Rptr.mod[i].latitude, Rptr.mod[i].longitude, Rptr.mod[i].desc1, Rptr.mod[i].desc2, Rptr.mod[i].url, Rptr.mod[i].package_version); if (Rptr.mod[i].frequency) @@ -2224,12 +2576,14 @@ bool CQnetGateway::Init(char *cfgfile) /* Used to validate MYCALL input */ preg = std::regex("^(([1-9][A-Z])|([A-PR-Z][0-9])|([A-PR-Z][A-Z][0-9]))[0-9A-Z]*[A-Z][ ]*[ A-RT-Z]$", std::regex::extended); - for (i=0; i<3; i++) { + for (i=0; i<3; i++) + { band_txt[i].Initialize(); } /* process configuration file */ - if ( ReadConfig(cfgfile) ) { + if ( ReadConfig(cfgfile) ) + { printf("Failed to process config file %s\n", cfgfile); return true; } @@ -2249,8 +2603,10 @@ bool CQnetGateway::Init(char *cfgfile) if (FromRemote.Open(fromremote.c_str())) return true; - for (i=0; i<3; i++) { - if (Rptr.mod[i].defined) { // open unix sockets between qngateway and each defined modem + for (i=0; i<3; i++) + { + if (Rptr.mod[i].defined) // open unix sockets between qngateway and each defined modem + { printf("Connecting to modem at %s\n", tomodem[i].c_str()); if (ToModem[i].Open(tomodem[i].c_str(), this)) return true; @@ -2299,52 +2655,62 @@ bool CQnetGateway::Init(char *cfgfile) Rptr.mod[2].band = "2m"; printf("Repeater callsigns: [%s] [%s] [%s]\n", Rptr.mod[0].call.c_str(), Rptr.mod[1].call.c_str(), Rptr.mod[2].call.c_str()); - if (APRS_ENABLE) { + if (APRS_ENABLE) + { aprs = new CAPRS(&Rptr); if (aprs) aprs->Init(); - else { + else + { printf("aprs class init failed!\nAPRS will be turned off"); APRS_ENABLE = false; } } compute_aprs_hash(); - for (int j=0; j<2; j++) { + for (int j=0; j<2; j++) + { if (ircddb[j].ip.empty()) continue; ii[j] = new CIRCDDB(ircddb[j].ip, ircddb[j].port, owner, IRCDDB_PASSWORD[j], GW_VERSION.c_str()); bool ok = ii[j]->open(); - if (!ok) { + if (!ok) + { printf("%s open failed\n", ircddb[j].ip.c_str()); return true; } } - for (int j=0; j<2; j++) { + for (int j=0; j<2; j++) + { if (ircddb[j].ip.empty()) continue; int rc = ii[j]->getConnectionState(); printf("Waiting for %s connection status of 2\n", ircddb[j].ip.c_str()); i = 0; - while (rc < 2) { + while (rc < 2) + { printf("%s status=%d\n", ircddb[j].ip.c_str(), rc); - if (rc < 2) { + if (rc < 2) + { i++; sleep(5); - } else + } + else break; if (!IsRunning()) break; - if (i > 5) { + if (i > 5) + { printf("We can not wait any longer for %s...\n", ircddb[j].ip.c_str()); break; } rc = ii[j]->getConnectionState(); } - switch (ii[j]->GetFamily()) { + switch (ii[j]->GetFamily()) + { case AF_INET: printf("IRC server %s is using IPV4\n", ircddb[j].ip.c_str()); af_family[j] = AF_INET; @@ -2358,26 +2724,33 @@ bool CQnetGateway::Init(char *cfgfile) return true; } } - /* udp port 40000 must open first */ - if (ii[0]) { + /* udp port 40000 must open first */ + if (ii[0]) + { SPORTIP *pip = (AF_INET == af_family[0]) ? &g2_external : & g2_ipv6_external; g2_sock[0] = open_port(pip, af_family[0]); - if (0 > g2_sock[0]) { + if (0 > g2_sock[0]) + { printf("Can't open %s:%d for %s\n", pip->ip.c_str(), pip->port, ircddb[i].ip.c_str()); return true; } - if (ii[1] && (af_family[0] != af_family[1])) { // we only need to open a second port if the family for the irc servers are different! + if (ii[1] && (af_family[0] != af_family[1])) // we only need to open a second port if the family for the irc servers are different! + { SPORTIP *pip = (AF_INET == af_family[1]) ? &g2_external : & g2_ipv6_external; g2_sock[1] = open_port(pip, af_family[1]); - if (0 > g2_sock[1]) { + if (0 > g2_sock[1]) + { printf("Can't open %s:%d for %s\n", pip->ip.c_str(), pip->port, ircddb[1].ip.c_str()); return true; } } - } else if (ii[1]) { + } + else if (ii[1]) + { SPORTIP *pip = (AF_INET == af_family[1]) ? &g2_external : & g2_ipv6_external; g2_sock[1] = open_port(pip, af_family[1]); - if (0 > g2_sock[1]) { + if (0 > g2_sock[1]) + { printf("Can't open %s:%d for %s\n", pip->ip.c_str(), pip->port, ircddb[1].ip.c_str()); return true; } @@ -2401,7 +2774,8 @@ bool CQnetGateway::Init(char *cfgfile) sdheader.ctrl = 0x80; /* to remote systems */ - for (i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) + { to_remote_g2[i].toDstar.Clear(); to_remote_g2[i].streamid = 0; to_remote_g2[i].last_time = 0; @@ -2412,7 +2786,8 @@ bool CQnetGateway::Init(char *cfgfile) if (GATEWAY_SEND_QRGS_MAP) qrgs_and_maps(); - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { sdin[i].Init(); sdout[i].Init(); } @@ -2430,34 +2805,42 @@ CQnetGateway::~CQnetGateway() { ToLink.Close(); FromRemote.Close(); - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { if (Rptr.mod[i].defined) ToModem[i].Close(); } - if (APRS_ENABLE) { - if (aprs->aprs_sock.GetFD() != -1) { + if (APRS_ENABLE) + { + if (aprs->aprs_sock.GetFD() != -1) + { aprs->aprs_sock.Close(); printf("Closed APRS\n"); } delete aprs; } - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { recd[i].last_time = 0; recd[i].streamid = 0; - if (recd[i].fd >= 0) { + if (recd[i].fd >= 0) + { close(recd[i].fd); unlink(recd[i].file); } } - for (int i=0; i<2; i++) { - if (g2_sock[i] >= 0) { + for (int i=0; i<2; i++) + { + if (g2_sock[i] >= 0) + { close(g2_sock[i]); printf("Closed G2_EXTERNAL_PORT %d\n", i); } - if (ii[i]) { + if (ii[i]) + { ii[i]->close(); delete ii[i]; } @@ -2469,12 +2852,14 @@ CQnetGateway::~CQnetGateway() int main(int argc, char **argv) { printf("VERSION %s\n", GW_VERSION.c_str()); - if (argc != 2) { + if (argc != 2) + { printf("usage: %s qn.cfg\n", argv[0]); return 1; } CQnetGateway QnetGateway; - if (QnetGateway.Init(argv[1])) { + if (QnetGateway.Init(argv[1])) + { return 1; } QnetGateway.Process(); diff --git a/QnetGateway.h b/QnetGateway.h index 73b9e26..e4b10e7 100644 --- a/QnetGateway.h +++ b/QnetGateway.h @@ -35,20 +35,23 @@ #define CALL_SIZE 8 #define MAX_DTMF_BUF 32 -using STOREMOTEG2 = struct to_remote_g2_tag { +using STOREMOTEG2 = struct to_remote_g2_tag +{ unsigned short streamid; CSockAddress toDstar; time_t last_time; }; -using STOREPEATER = struct torepeater_tag { +using STOREPEATER = struct torepeater_tag +{ // help with header re-generation SDSVT saved_hdr; // repeater format time_t last_time; unsigned char sequence; }; -using SBANDTXT = struct band_txt_tag { +using SBANDTXT = struct band_txt_tag +{ unsigned short streamID; unsigned char flags[3]; std::string mycall, sfx, urcall, rpt1, rpt2, txt, dest_rptr; @@ -76,7 +79,8 @@ using SBANDTXT = struct band_txt_tag { } }; -using SSD = struct sd_tag { +using SSD = struct sd_tag +{ unsigned char header[41]; unsigned char message[21]; unsigned char gps[256]; @@ -96,12 +100,12 @@ public: bool Init(char *cfgfile); private: - // link type - int link_family[3] = { AF_UNSPEC, AF_UNSPEC, AF_UNSPEC }; + // link type + int link_family[3] = { AF_UNSPEC, AF_UNSPEC, AF_UNSPEC }; // network type int af_family[2] = { AF_UNSPEC, AF_UNSPEC }; - int Index[3] = { -1, -1, -1 }; + int Index[3] = { -1, -1, -1 }; SPORTIP g2_external, g2_ipv6_external, ircddb[2]; @@ -197,7 +201,7 @@ private: bool Flag_is_ok(unsigned char flag); void UnpackCallsigns(const std::string &str, std::set &set, const std::string &delimiters = ","); void PrintCallsigns(const std::string &key, const std::set &set); - int FindIndex(const int i) const; + int FindIndex(const int i) const; // read configuration file bool ReadConfig(char *); diff --git a/QnetITAP.cpp b/QnetITAP.cpp index 5093270..1b560df 100644 --- a/QnetITAP.cpp +++ b/QnetITAP.cpp @@ -48,7 +48,7 @@ #define ITAP_VERSION "QnetITAP-526" CQnetITAP::CQnetITAP(int mod) -: assigned_module(mod) + : assigned_module(mod) { } @@ -70,19 +70,22 @@ bool CQnetITAP::Initialize(const char *cfgfile) int CQnetITAP::OpenITAP() { int fd = open(ITAP_DEVICE.c_str(), O_RDWR | O_NOCTTY | O_NDELAY, 0); - if (fd < 0) { + if (fd < 0) + { printf("Failed to open device [%s], error=%d, message=%s\n", ITAP_DEVICE.c_str(), errno, strerror(errno)); return -1; } - if (isatty(fd) == 0) { + if (isatty(fd) == 0) + { printf("Device %s is not a tty device\n", ITAP_DEVICE.c_str()); close(fd); return -1; } static termios t; - if (tcgetattr(fd, &t) < 0) { + if (tcgetattr(fd, &t) < 0) + { printf("tcgetattr failed for %s, error=%d, message-%s\n", ITAP_DEVICE.c_str(), errno, strerror(errno)); close(fd); return -1; @@ -99,7 +102,8 @@ int CQnetITAP::OpenITAP() cfsetospeed(&t, B38400); cfsetispeed(&t, B38400); - if (tcsetattr(fd, TCSANOW, &t) < 0) { + if (tcsetattr(fd, TCSANOW, &t) < 0) + { printf("tcsetattr failed for %s, error=%dm message=%s\n", ITAP_DEVICE.c_str(), errno, strerror(errno)); close(fd); return -1; @@ -111,35 +115,37 @@ int CQnetITAP::OpenITAP() void CQnetITAP::DumpSerialPacket(const char *title, const unsigned char *buf) { printf("%s: ", title); - if (buf[0] > 41) { + if (buf[0] > 41) + { printf("UNKNOWN: length=%u", (unsigned)buf[0]); return; } SITAP itap; memcpy(&itap, buf, buf[0]); - switch (itap.type) { - case 0x03U: //pong - printf("Pong\n"); - break; - case 0x10U: // header - 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); - break; - case 0x12U: // data - 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]); - break; - case 0x11U: // header acknowledgement - case 0x21U: - printf("Header acknowledgement code=%02u", itap.header.flag[0]); - break; - case 0x13U: // data acknowledgment - case 0x23U: - printf("Data acknowledgement seq=%02u code=%02u", itap.header.flag[0], itap.header.flag[1]); - break; - default: - printf("UNKNOWN packet buf[0] = 0x%02u\n", buf[0]); - break; + switch (itap.type) + { + case 0x03U: //pong + printf("Pong\n"); + break; + case 0x10U: // header + 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); + break; + case 0x12U: // data + 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]); + break; + case 0x11U: // header acknowledgement + case 0x21U: + printf("Header acknowledgement code=%02u", itap.header.flag[0]); + break; + case 0x13U: // data acknowledgment + case 0x23U: + printf("Data acknowledgement seq=%02u code=%02u", itap.header.flag[0], itap.header.flag[1]); + break; + default: + printf("UNKNOWN packet buf[0] = 0x%02u\n", buf[0]); + break; } } @@ -150,7 +156,8 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf) // Get the buffer size or nothing at all int ret = read(serfd, buf, 1U); - if (ret < 0) { + if (ret < 0) + { printf("Error when reading first byte from the Icom radio %d: %s", errno, strerror(errno)); return RT_ERROR; } @@ -163,17 +170,20 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf) unsigned int length = buf[0U]; - if (length >= 100U) { + if (length >= 100U) + { printf("Invalid data received from the Icom radio, length=%d\n", length); return RT_ERROR; } unsigned int offset = 1U; - while (offset < length) { + while (offset < length) + { ret = read(serfd, buf + offset, length - offset); - if (ret<0 && errno!=EAGAIN) { + if (ret<0 && errno!=EAGAIN) + { printf("Error when reading buffer from the Icom radio %d: %s\n", errno, strerror(errno)); return RT_ERROR; } @@ -182,19 +192,20 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf) offset += ret; } - switch (buf[1U]) { - case 0x03U: - return RT_PONG; - case 0x10U: - return RT_HEADER; - case 0x12U: - return RT_DATA; - case 0x21U: - return RT_HEADER_ACK; - case 0x23U: - return RT_DATA_ACK; - default: - return RT_UNKNOWN; + switch (buf[1U]) + { + case 0x03U: + return RT_PONG; + case 0x10U: + return RT_HEADER; + case 0x12U: + return RT_DATA; + case 0x21U: + return RT_HEADER_ACK; + case 0x23U: + return RT_DATA_ACK; + default: + return RT_UNKNOWN; } } @@ -220,7 +231,8 @@ void CQnetITAP::Run(const char *cfgfile) double pingtime = 0.001; const double ackwait = AP_MODE ? 0.4 : 0.06; - while (keep_running) { + while (keep_running) + { fd_set readfds; FD_ZERO(&readfds); @@ -235,24 +247,30 @@ void CQnetITAP::Run(const char *cfgfile) // don't care about writefds and exceptfds: // and we'll wait for 5 ms int ret = select(maxfs+1, &readfds, NULL, NULL, &tv); - if (ret < 0) { + if (ret < 0) + { printf("ERROR: Run: select returned err=%d, %s\n", errno, strerror(errno)); break; } // 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"); break; } - if (pingTimer.time() >= pingtime) { - if (poll_counter < 18 ) { + if (pingTimer.time() >= pingtime) + { + if (poll_counter < 18 ) + { const unsigned char poll[2] = { 0xffu, 0xffu }; SendTo(poll); if (poll_counter == 17) pingtime = 1.0; - } else { + } + else + { const unsigned char ping[2] = { 0x02u, 0x02u }; SendTo(ping); } @@ -262,79 +280,93 @@ void CQnetITAP::Run(const char *cfgfile) unsigned char buf[100]; - if (keep_running && FD_ISSET(serfd, &readfds)) { // there is something to read! - switch (GetITAPData(buf)) { - case RT_ERROR: - keep_running = false; - break; - case RT_HEADER: - { - unsigned char ack_header[3] = { 0x03U, 0x11U, 0x0U }; - SendTo(ack_header); - } - if (ProcessITAP(buf)) - keep_running = false; - lastdataTimer.start(); - break; - case RT_DATA: + if (keep_running && FD_ISSET(serfd, &readfds)) // there is something to read! + { + switch (GetITAPData(buf)) + { + case RT_ERROR: + keep_running = false; + break; + case RT_HEADER: + { + unsigned char ack_header[3] = { 0x03U, 0x11U, 0x0U }; + SendTo(ack_header); + } + if (ProcessITAP(buf)) + keep_running = false; + lastdataTimer.start(); + break; + 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) { - 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(); - if (count) - printf("%u packets in queue. Icom radio is connected.", (unsigned int)count); - } else - printf("Icom Radio is connected.\n"); - is_alive = true; - } - lastdataTimer.start(); - break; - case RT_HEADER_ACK: - if (acknowledged) { - fprintf(stderr, "ERROR: Header already acknowledged!\n"); - } else { - if (0x0U == buf[2]) - acknowledged = true; - } - lastdataTimer.start(); - break; - case RT_DATA_ACK: - if (acknowledged) { - fprintf(stderr, "ERROR: voice frame %d already acknowledged!\n", (int)buf[2]); - } else { - if (0x0U == buf[3]) - acknowledged = true; + auto count = queue.size(); + if (count) + printf("%u packets in queue. Icom radio is connected.", (unsigned int)count); } - lastdataTimer.start(); - break; - case RT_TIMEOUT: // nothing or 0xff - break; - default: - if (buf[0] != 255) - DumpSerialPacket("GetITAPData returned", buf); - break; + else + printf("Icom Radio is connected.\n"); + is_alive = true; + } + lastdataTimer.start(); + break; + case RT_HEADER_ACK: + if (acknowledged) + { + fprintf(stderr, "ERROR: Header already acknowledged!\n"); + } + else + { + if (0x0U == buf[2]) + acknowledged = true; + } + lastdataTimer.start(); + break; + case RT_DATA_ACK: + if (acknowledged) + { + fprintf(stderr, "ERROR: voice frame %d already acknowledged!\n", (int)buf[2]); + } + else + { + 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); } - if (keep_running && FD_ISSET(ug2m, &readfds)) { + if (keep_running && FD_ISSET(ug2m, &readfds)) + { ssize_t len = ToGate.Read(buf, 100); - if (len < 0) { + if (len < 0) + { printf("ERROR: Run: recvfrom(gsock) returned error %d, %s\n", errno, strerror(errno)); break; } - if (0 == memcmp(buf, "DSVT", 4)) { + if (0 == memcmp(buf, "DSVT", 4)) + { //printf("read %d bytes from QnetGateway\n", (int)len); if (ProcessGateway(len, buf)) break; @@ -343,10 +375,14 @@ void CQnetITAP::Run(const char *cfgfile) } // send queued frames - if (keep_running) { - if (acknowledged) { - if (is_alive) { - if (! queue.empty()) { + if (keep_running) + { + if (acknowledged) + { + if (is_alive) + { + if (! queue.empty()) + { CFrame frame = queue.front(); queue.pop(); SendTo(frame.data()); @@ -354,8 +390,11 @@ void CQnetITAP::Run(const char *cfgfile) acknowledged = false; } } - } else { // we are waiting on an acknowledgement - if (ackTimer.time() >= ackwait) { + } + else // we are waiting on an acknowledgement + { + if (ackTimer.time() >= ackwait) + { fprintf(stderr, "Icom failure suspected, restarting...\n"); close(serfd); poll_counter = 0; @@ -365,9 +404,12 @@ void CQnetITAP::Run(const char *cfgfile) lastdataTimer.start(); pingTimer.start(); serfd = OpenITAP(); - if (serfd < 0) { + if (serfd < 0) + { keep_running = false; - } else { + } + else + { while (! queue.empty()) queue.pop(); } @@ -386,10 +428,13 @@ int CQnetITAP::SendTo(const unsigned char *buf) unsigned int ptr = 0; unsigned int length = (0xffu == buf[0]) ? 2 : buf[0]; - while (ptr < length) { + while (ptr < length) + { n = write(serfd, buf + ptr, length - ptr); - if (n < 0) { - if (EAGAIN != errno) { + if (n < 0) + { + if (EAGAIN != errno) + { printf("Error %d writing to dvap, message=%s\n", errno, strerror(errno)); return -1; } @@ -398,11 +443,14 @@ int CQnetITAP::SendTo(const unsigned char *buf) } n = 0; // send an ending 0xffu - while (0 == n) { + while (0 == n) + { const unsigned char push = 0xffu; n = write(serfd, &push, 1); - if (n < 0) { - if (EAGAIN != errno) { + if (n < 0) + { + if (EAGAIN != errno) + { printf("Error %d writing to dvap, message=%s\n", errno, strerror(errno)); return -1; } @@ -415,20 +463,25 @@ int CQnetITAP::SendTo(const unsigned char *buf) bool CQnetITAP::ProcessGateway(const int len, const unsigned char *raw) { static unsigned char counter = 0; - if (27==len || 56==len) { //here is dstar data + if (27==len || 56==len) //here is dstar data + { SDSVT dsvt; memcpy(dsvt.title, raw, len); // transfer raw data to SDSVT struct SITAP itap; // destination - if (56 == len) { // write a Header packet + if (56 == len) // write a Header packet + { counter = 0; itap.length = 41U; itap.type = 0x20; memcpy(itap.header.flag, dsvt.hdr.flag, 3); - if (RPTR_MOD == dsvt.hdr.rpt2[7]) { + if (RPTR_MOD == dsvt.hdr.rpt2[7]) + { memcpy(itap.header.r1, dsvt.hdr.rpt2, 8); memcpy(itap.header.r2, dsvt.hdr.rpt1, 8); - } else { + } + else + { memcpy(itap.header.r1, dsvt.hdr.rpt1, 8); memcpy(itap.header.r2, dsvt.hdr.rpt2, 8); } @@ -437,7 +490,9 @@ bool CQnetITAP::ProcessGateway(const int len, const unsigned char *raw) memcpy(itap.header.nm, dsvt.hdr.sfx, 4); if (LOG_QSO) printf("Queued ITAP to %s ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ITAP_DEVICE.c_str(), itap.header.ur, itap.header.r1, itap.header.r2, itap.header.my, itap.header.nm); - } else { // write an AMBE packet + } + else // write an AMBE packet + { itap.length = 16U; itap.type = 0x22U; itap.voice.counter = counter++; @@ -450,7 +505,8 @@ bool CQnetITAP::ProcessGateway(const int len, const unsigned char *raw) } queue.push(CFrame(&itap.length)); - } else + } + else printf("DEBUG: ProcessGateway: unusual packet size read len=%d\n", len); return false; } @@ -477,19 +533,22 @@ bool CQnetITAP::ProcessITAP(const unsigned char *buf) dsvt.flagb[2] = ('B'==RPTR_MOD) ? 0x1 : (('C'==RPTR_MOD) ? 0x2 : 0x3); dsvt.streamid = htons(stream_id); - if (41 == len) { // header + if (41 == len) // header + { dsvt.ctrl = 0x80; memcpy(dsvt.hdr.flag, itap.header.flag, 3); ////////////////// Terminal or Access ///////////////////////// - if (0 == memcmp(itap.header.r1, "DIRECT", 6)) { + if (0 == memcmp(itap.header.r1, "DIRECT", 6)) + { // Terminal Mode! memcpy(dsvt.hdr.rpt1, RPTR.c_str(), 7); // build r1 dsvt.hdr.rpt1[7] = RPTR_MOD; // with module memcpy(dsvt.hdr.rpt2, RPTR.c_str(), 7); // build r2 dsvt.hdr.rpt2[7] = 'G'; // with gateway - if (' '==itap.header.ur[2] && ' '!=itap.header.ur[0]) { + if (' '==itap.header.ur[2] && ' '!=itap.header.ur[0]) + { // it's a command because it has as space in the 3rd position, we have to right-justify it! // Terminal Mode left justifies short commands. memset(dsvt.hdr.urcall, ' ', 8); // first file ur with spaces @@ -497,9 +556,12 @@ bool CQnetITAP::ProcessITAP(const unsigned char *buf) dsvt.hdr.urcall[7] = itap.header.ur[0]; // one char command, like "E" or "I" else memcpy(dsvt.hdr.urcall+6, itap.header.ur, 2); // two char command, like "HX" or "S0" - } else + } + else memcpy(dsvt.hdr.urcall, itap.header.ur, 8); // ur is at least 3 chars - } else { + } + else + { // Access Point Mode memcpy(dsvt.hdr.rpt1, itap.header.r1, 8); memcpy(dsvt.hdr.rpt2, itap.header.r2, 8); @@ -510,16 +572,20 @@ bool CQnetITAP::ProcessITAP(const unsigned char *buf) memcpy(dsvt.hdr.mycall, itap.header.my, 8); memcpy(dsvt.hdr.sfx, itap.header.nm, 4); calcPFCS(dsvt.hdr.flag, dsvt.hdr.pfcs); - if (ToGate.Write(dsvt.title, 56)) { + if (ToGate.Write(dsvt.title, 56)) + { printf("ERROR: ProcessITAP: Could not write gateway header packet\n"); return true; } if (LOG_QSO) printf("Sent DSVT to gateway, streamid=%04x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dsvt.streamid), dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); - } else if (16 == len) { // ambe + } + else if (16 == len) // ambe + { dsvt.ctrl = itap.voice.sequence; memcpy(dsvt.vasd.voice, itap.voice.ambe, 12); - if (ToGate.Write(dsvt.title, 27)) { + if (ToGate.Write(dsvt.title, 27)) + { printf("ERROR: ProcessMMDVM: Could not write gateway voice packet\n"); return true; } @@ -542,12 +608,15 @@ bool CQnetITAP::ReadConfig(const char *cfgFile) const std::string estr; // an empty string std::string type; std::string itap_path("module_"); - if (0 > assigned_module) { + if (0 > assigned_module) + { // we need to find the lone itap module - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { std::string test(itap_path); test.append(1, 'a'+i); - if (cfg.KeyExists(test)) { + if (cfg.KeyExists(test)) + { cfg.GetValue(test, estr, type, 1, 16); if (type.compare("itap")) continue; // this ain't it! @@ -556,20 +625,27 @@ bool CQnetITAP::ReadConfig(const char *cfgFile) break; } } - if (0 > assigned_module) { + if (0 > assigned_module) + { fprintf(stderr, "Error: no 'itap' module found\n!"); return true; } - } else { + } + else + { // make sure itap module is defined itap_path.append(1, 'a' + assigned_module); - if (cfg.KeyExists(itap_path)) { + if (cfg.KeyExists(itap_path)) + { cfg.GetValue(itap_path, estr, type, 1, 16); - if (type.compare("itap")) { + if (type.compare("itap")) + { fprintf(stderr, "%s = %s is not 'itap' type!\n", itap_path.c_str(), type.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "Module '%c' is not defined.\n", 'a'+assigned_module); return true; } @@ -580,22 +656,30 @@ bool CQnetITAP::ReadConfig(const char *cfgFile) cfg.GetValue(itap_path+"_ap_mode", type, AP_MODE); itap_path.append("_callsign"); - if (cfg.KeyExists(itap_path)) { + if (cfg.KeyExists(itap_path)) + { if (cfg.GetValue(itap_path, type, RPTR, 3, 6)) return true; - } else { + } + else + { itap_path.assign("ircddb_login"); - if (cfg.KeyExists(itap_path)) { + if (cfg.KeyExists(itap_path)) + { if (cfg.GetValue(itap_path, estr, RPTR, 3, 6)) return true; } } int l = RPTR.length(); - if (l<3 || l>6) { + if (l<3 || l>6) + { printf("Call '%s' is invalid length!\n", RPTR.c_str()); return true; - } else { - for (int i=0; i> 8) ^ crc_tabccitt[tmp]; @@ -648,12 +734,14 @@ void CQnetITAP::calcPFCS(const unsigned char *packet, unsigned char *pfcs) int main(int argc, const char **argv) { setbuf(stdout, NULL); - if (2 != argc) { + if (2 != argc) + { fprintf(stderr, "usage: %s path_to_config_file\n", argv[0]); return 1; } - if ('-' == argv[1][0]) { + if ('-' == argv[1][0]) + { printf("\nQnetITAP Version #%s Copyright (C) 2018-2019 by Thomas A. Early N7TAE\n", ITAP_VERSION); printf("QnetITAP comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); @@ -661,29 +749,31 @@ int main(int argc, const char **argv) } const char *qn = strstr(argv[0], "qnitap"); - if (NULL == qn) { + if (NULL == qn) + { fprintf(stderr, "Error finding 'qnitap' in %s!\n", argv[0]); return 1; } qn += 6; int assigned_module; - switch (*qn) { - case NULL: - assigned_module = -1; - break; - case 'a': - assigned_module = 0; - break; - case 'b': - assigned_module = 1; - break; - case 'c': - assigned_module = 2; - break; - default: - fprintf(stderr, "assigned module must be a, b or c\n"); - return 1; + switch (*qn) + { + case NULL: + assigned_module = -1; + break; + case 'a': + assigned_module = 0; + break; + case 'b': + assigned_module = 1; + break; + case 'c': + assigned_module = 2; + break; + default: + fprintf(stderr, "assigned module must be a, b or c\n"); + return 1; } CQnetITAP qnitap(assigned_module); diff --git a/QnetITAP.h b/QnetITAP.h index 9b96882..de0fa3f 100644 --- a/QnetITAP.h +++ b/QnetITAP.h @@ -31,7 +31,8 @@ #define CALL_SIZE 8 #define IP_SIZE 15 -enum REPLY_TYPE { +enum REPLY_TYPE +{ RT_TIMEOUT, RT_ERROR, RT_UNKNOWN, @@ -44,22 +45,25 @@ enum REPLY_TYPE { // Icom Terminal and Access Point Mode data structure #pragma pack(push, 1) -using SITAP = struct itap_tag { +using SITAP = struct itap_tag +{ unsigned char length; - // 41 for header - // 16 for voice + // 41 for header + // 16 for voice unsigned char type; - // 0x03U pong - // 0x10U header from icom - // 0x11U acknowledgment - // 0x12U data from icom (it's EOT if voice.sequence bit 0x40 is set) - // 0x13U acknowledgment - // 0x20U header to icom - // 0x21U header acknowledgment - // 0x22U data to icom - // 0x23U data acknowledgment - union { - struct { + // 0x03U pong + // 0x10U header from icom + // 0x11U acknowledgment + // 0x12U data from icom (it's EOT if voice.sequence bit 0x40 is set) + // 0x13U acknowledgment + // 0x20U header to icom + // 0x21U header acknowledgment + // 0x22U data to icom + // 0x23U data acknowledgment + union + { + struct + { unsigned char flag[3]; unsigned char r2[8]; unsigned char r1[8]; @@ -67,7 +71,8 @@ using SITAP = struct itap_tag { unsigned char my[8]; unsigned char nm[4]; } header; - struct { + struct + { unsigned char counter; // ordinal counter is reset with each header unsigned char sequence; // is modulo 21 unsigned char ambe[9]; @@ -80,11 +85,13 @@ using SITAP = struct itap_tag { class CFrame { public: - CFrame(const unsigned char *buf) { + CFrame(const unsigned char *buf) + { memcpy(&frame.length, buf, buf[0]); } - CFrame(const CFrame &from) { + CFrame(const CFrame &from) + { memcpy(&frame.length, from.data(), from.size()); } diff --git a/QnetLink.cpp b/QnetLink.cpp index a79a2fb..882ffa8 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -107,37 +107,46 @@ bool CQnetLink::resolve_rmt(const char *name, const unsigned short port, CSockAd hints.ai_socktype = SOCK_DGRAM; int rc = getaddrinfo(name, NULL, &hints, &res); - if (rc != 0) { + if (rc != 0) + { printf("getaddrinfo return error code %d for [%s]\n", rc, name); return false; } - for (rp=res; rp!=NULL; rp=rp->ai_next) { - if ((AF_INET==rp->ai_family || AF_INET6==rp->ai_family) && SOCK_DGRAM==rp->ai_socktype) { - if (AF_INET == rp->ai_family) { - char saddr[INET_ADDRSTRLEN]; - struct sockaddr_in *addr4 = (struct sockaddr_in *)rp->ai_addr; - if (inet_ntop(rp->ai_family, &(addr4->sin_addr), saddr, INET_ADDRSTRLEN)) { - addr.Initialize(rp->ai_family, port, saddr); - found = true; - break; - } - } else if (AF_INET6 == rp->ai_family) { - char saddr[INET6_ADDRSTRLEN]; - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)rp->ai_addr; - if (inet_ntop(rp->ai_family, &(addr6->sin6_addr), saddr, INET6_ADDRSTRLEN)) { - addr.Initialize(rp->ai_family, port, saddr); - found = true; - break; - } - } + for (rp=res; rp!=NULL; rp=rp->ai_next) + { + if ((AF_INET==rp->ai_family || AF_INET6==rp->ai_family) && SOCK_DGRAM==rp->ai_socktype) + { + if (AF_INET == rp->ai_family) + { + char saddr[INET_ADDRSTRLEN]; + struct sockaddr_in *addr4 = (struct sockaddr_in *)rp->ai_addr; + if (inet_ntop(rp->ai_family, &(addr4->sin_addr), saddr, INET_ADDRSTRLEN)) + { + addr.Initialize(rp->ai_family, port, saddr); + found = true; + break; + } + } + else if (AF_INET6 == rp->ai_family) + { + char saddr[INET6_ADDRSTRLEN]; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)rp->ai_addr; + if (inet_ntop(rp->ai_family, &(addr6->sin6_addr), saddr, INET6_ADDRSTRLEN)) + { + addr.Initialize(rp->ai_family, port, saddr); + found = true; + break; + } + } } } freeaddrinfo(res); - if (found && strcmp(name, addr.GetAddress())) { - printf("Node address %s on port %u resolved to %s\n", name, port, addr.GetAddress()); - } + if (found && strcmp(name, addr.GetAddress())) + { + printf("Node address %s on port %u resolved to %s\n", name, port, addr.GetAddress()); + } return found; } @@ -146,14 +155,16 @@ bool CQnetLink::resolve_rmt(const char *name, const unsigned short port, CSockAd void CQnetLink::send_heartbeat() { // heartbeats for connected donglers - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; REFWrite(REF_ACK, 3, inbound->addr); if (inbound->countdown >= 0) inbound->countdown--; - if (inbound->countdown < 0) { + if (inbound->countdown < 0) + { printf("call=%s timeout, removing %s, users=%d\n", inbound->call, pos->first.c_str(), (int)inbound_list.size() - 1); qnDB.DeleteLS(pos->first.c_str()); delete pos->second; @@ -173,21 +184,24 @@ void CQnetLink::send_heartbeat() XRFWrite(owner.c_str(), CALL_SIZE+1, to_remote_g2[2].addr); /* send heartbeat to linked DCS reflectors */ - if (to_remote_g2[0].addr.GetPort() == rmt_dcs_port) { + if (to_remote_g2[0].addr.GetPort() == rmt_dcs_port) + { strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[7] = to_remote_g2[0].from_mod; memcpy(cmd_2_dcs + 9, to_remote_g2[0].cs, 8); cmd_2_dcs[16] = to_remote_g2[0].to_mod; DCSWrite(cmd_2_dcs, 17, to_remote_g2[0].addr); } - if (to_remote_g2[1].addr.GetPort() == rmt_dcs_port) { + if (to_remote_g2[1].addr.GetPort() == rmt_dcs_port) + { strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[7] = to_remote_g2[1].from_mod; memcpy(cmd_2_dcs + 9, to_remote_g2[1].cs, 8); cmd_2_dcs[16] = to_remote_g2[1].to_mod; DCSWrite(cmd_2_dcs, 17, to_remote_g2[1].addr); } - if (to_remote_g2[2].addr.GetPort() == rmt_dcs_port) { + if (to_remote_g2[2].addr.GetPort() == rmt_dcs_port) + { strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[7] = to_remote_g2[2].from_mod; memcpy(cmd_2_dcs + 9, to_remote_g2[2].cs, 8); @@ -205,23 +219,29 @@ void CQnetLink::send_heartbeat() if (to_remote_g2[2].is_connected && to_remote_g2[2].addr.GetPort()==rmt_ref_port && strcmp(to_remote_g2[2].cs, to_remote_g2[0].cs) && strcmp(to_remote_g2[2].cs, to_remote_g2[1].cs)) REFWrite(REF_ACK, 3, to_remote_g2[2].addr); - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { /* check for timeouts from remote */ - if (to_remote_g2[i].cs[0] != '\0') { + if (to_remote_g2[i].cs[0] != '\0') + { if (to_remote_g2[i].countdown >= 0) to_remote_g2[i].countdown--; - if (to_remote_g2[i].countdown < 0) { + if (to_remote_g2[i].countdown < 0) + { /* maybe remote system has changed IP */ printf("Unlinked from [%s] mod %c, TIMEOUT...\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); sprintf(notify_msg[i], "%c_unlinked.dat_LINK_TIMEOUT", to_remote_g2[i].from_mod); qnDB.DeleteLS(to_remote_g2[i].addr.GetAddress()); - if (to_remote_g2[i].auto_link) { + if (to_remote_g2[i].auto_link) + { char cs[CALL_SIZE+1]; memcpy(cs, to_remote_g2[i].cs, CALL_SIZE+1); // call is passed by pointer so we have to copy it g2link(to_remote_g2[i].from_mod, cs, to_remote_g2[i].to_mod); - } else { + } + else + { to_remote_g2[i].cs[0] = '\0'; to_remote_g2[i].from_mod = to_remote_g2[i].to_mod = ' '; to_remote_g2[i].addr.Clear(); @@ -233,13 +253,16 @@ void CQnetLink::send_heartbeat() } /*** check for RF inactivity ***/ - if (to_remote_g2[i].is_connected) { - if (((tnow - tracing[i].last_time) > rf_inactivity_timer[i]) && (rf_inactivity_timer[i] > 0)) { + if (to_remote_g2[i].is_connected) + { + if (((tnow - tracing[i].last_time) > rf_inactivity_timer[i]) && (rf_inactivity_timer[i] > 0)) + { tracing[i].last_time = 0; printf("Unlinked from [%s] mod %c, local RF inactivity...\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); - if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) { + if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + { queryCommand[0] = 5; queryCommand[1] = 0; queryCommand[2] = 24; @@ -248,9 +271,12 @@ void CQnetLink::send_heartbeat() REFWrite(queryCommand, 5, to_remote_g2[i].addr); /* zero out any other entries here that match that system */ - for (int j=0; j<3; j++) { - if (j != i) { - if (to_remote_g2[j].addr == to_remote_g2[i].addr) { + for (int j=0; j<3; j++) + { + if (j != i) + { + if (to_remote_g2[j].addr == to_remote_g2[i].addr) + { to_remote_g2[j].cs[0] = '\0'; to_remote_g2[j].addr.Clear(); to_remote_g2[j].from_mod = ' '; @@ -261,7 +287,9 @@ void CQnetLink::send_heartbeat() } } } - } else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { char unlink_request[CALL_SIZE + 3]; strcpy(unlink_request, owner.c_str()); unlink_request[8] = to_remote_g2[i].from_mod; @@ -270,7 +298,9 @@ void CQnetLink::send_heartbeat() for (int j=0; j<5; j++) XRFWrite(unlink_request, CALL_SIZE+3, to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[8] = to_remote_g2[i].from_mod; cmd_2_dcs[9] = ' '; @@ -278,7 +308,7 @@ void CQnetLink::send_heartbeat() memcpy(cmd_2_dcs + 11, to_remote_g2[i].cs, 8); for (int j=0; j<2; j++) - DCSWrite(cmd_2_dcs, 19 , to_remote_g2[i].addr); + DCSWrite(cmd_2_dcs, 19, to_remote_g2[i].addr); } qnDB.DeleteLS(to_remote_g2[i].addr.GetAddress()); sprintf(notify_msg[i], "%c_unlinked.dat_UNLINKED_TIMEOUT", to_remote_g2[i].from_mod); @@ -308,20 +338,28 @@ void CQnetLink::rptr_ack(int i) else if (i == 2) mod_and_RADIO_ID[i][0] = 'C'; - if (to_remote_g2[i].is_connected) { + if (to_remote_g2[i].is_connected) + { memcpy(mod_and_RADIO_ID[i] + 1, "LINKED TO ", 10); memcpy(mod_and_RADIO_ID[i] + 11, to_remote_g2[i].cs, CALL_SIZE); mod_and_RADIO_ID[i][11 + CALL_SIZE] = to_remote_g2[i].to_mod; - } else if (to_remote_g2[i].cs[0] != '\0') { + } + else if (to_remote_g2[i].cs[0] != '\0') + { memcpy(mod_and_RADIO_ID[i] + 1, "TRYING ", 10); memcpy(mod_and_RADIO_ID[i] + 11, to_remote_g2[i].cs, CALL_SIZE); mod_and_RADIO_ID[i][11 + CALL_SIZE] = to_remote_g2[i].to_mod; - } else { + } + else + { memcpy(mod_and_RADIO_ID[i] + 1, "NOT LINKED", 10); } - try { + try + { std::async(std::launch::async, &CQnetLink::RptrAckThread, this, mod_and_RADIO_ID[i]); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf("Failed to start RptrAckThread(). Exception: %s\n", e.what()); } return; @@ -376,60 +414,62 @@ void CQnetLink::RptrAckThread(char *arg) /* start sending silence + announcement text */ - for (int i=0; i<10; i++) { + for (int i=0; i<10; i++) + { dsvt.ctrl = (unsigned char)i; - switch (i) { - case 0: - dsvt.vasd.text[0] = 0x55; - dsvt.vasd.text[1] = 0x2d; - dsvt.vasd.text[2] = 0x16; - break; - case 1: - dsvt.vasd.text[0] = '@' ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[0] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[1] ^ 0x93; - break; - case 2: - dsvt.vasd.text[0] = RADIO_ID[2] ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[3] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[4] ^ 0x93; - break; - case 3: - dsvt.vasd.text[0] = 'A' ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[5] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[6] ^ 0x93; - break; - case 4: - dsvt.vasd.text[0] = RADIO_ID[7] ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[8] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[9] ^ 0x93; - break; - case 5: - dsvt.vasd.text[0] = 'B' ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[10] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[11] ^ 0x93; - break; - case 6: - dsvt.vasd.text[0] = RADIO_ID[12] ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[13] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[14] ^ 0x93; - break; - case 7: - dsvt.vasd.text[0] = 'C' ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[15] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[16] ^ 0x93; - break; - case 8: - dsvt.vasd.text[0] = RADIO_ID[17] ^ 0x70; - dsvt.vasd.text[1] = RADIO_ID[18] ^ 0x4f; - dsvt.vasd.text[2] = RADIO_ID[19] ^ 0x93; - break; - case 9: - dsvt.ctrl |= 0x40; - dsvt.vasd.text[0] = 0x16; - dsvt.vasd.text[1] = 0x29; - dsvt.vasd.text[2] = 0xf5; - break; + switch (i) + { + case 0: + dsvt.vasd.text[0] = 0x55; + dsvt.vasd.text[1] = 0x2d; + dsvt.vasd.text[2] = 0x16; + break; + case 1: + dsvt.vasd.text[0] = '@' ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[0] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[1] ^ 0x93; + break; + case 2: + dsvt.vasd.text[0] = RADIO_ID[2] ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[3] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[4] ^ 0x93; + break; + case 3: + dsvt.vasd.text[0] = 'A' ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[5] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[6] ^ 0x93; + break; + case 4: + dsvt.vasd.text[0] = RADIO_ID[7] ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[8] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[9] ^ 0x93; + break; + case 5: + dsvt.vasd.text[0] = 'B' ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[10] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[11] ^ 0x93; + break; + case 6: + dsvt.vasd.text[0] = RADIO_ID[12] ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[13] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[14] ^ 0x93; + break; + case 7: + dsvt.vasd.text[0] = 'C' ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[15] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[16] ^ 0x93; + break; + case 8: + dsvt.vasd.text[0] = RADIO_ID[17] ^ 0x70; + dsvt.vasd.text[1] = RADIO_ID[18] ^ 0x4f; + dsvt.vasd.text[2] = RADIO_ID[19] ^ 0x93; + break; + case 9: + dsvt.ctrl |= 0x40; + dsvt.vasd.text[0] = 0x16; + dsvt.vasd.text[1] = 0x29; + dsvt.vasd.text[2] = 0xf5; + break; } ToGate.Write(dsvt.title, 27); if (i < 9) @@ -444,7 +484,8 @@ void CQnetLink::LoadGateways(const std::string &filename) const std::string website("auth.dstargateway.org"); int dplus = 0; // DPlus Authenticate - if (dplus_authorize && !dplus_priority) { + if (dplus_authorize && !dplus_priority) + { CDPlusAuthenticator auth(login_call, website); dplus = auth.Process(qnDB, dplus_reflectors, dplus_repeaters); if (0 == dplus) @@ -455,12 +496,15 @@ void CQnetLink::LoadGateways(const std::string &filename) int count = 0; std::ifstream hostfile(filename); - if (hostfile.is_open()) { + if (hostfile.is_open()) + { CHostQueue hqueue; std::string line; - while (std::getline(hostfile, line)) { + while (std::getline(hostfile, line)) + { trim(line); - if (! line.empty() && ('#' != line.at(0))) { + if (! line.empty() && ('#' != line.at(0))) + { std::istringstream iss(line); std::string host, address; unsigned short port; @@ -474,21 +518,28 @@ void CQnetLink::LoadGateways(const std::string &filename) qnDB.UpdateGW(hqueue); } - if (dplus_authorize) { + if (dplus_authorize) + { if (! dplus_priority) printf("#Gateways: %s=%d %s=%d Total=%d\n", website.c_str(), dplus, filename.c_str(), count, qnDB.Count("GATEWAYS")); - } else { + } + else + { printf("#Gateways: %s=%d\n", filename.c_str(), count); } // DPlus Authenticate - if (dplus_authorize && dplus_priority) { + if (dplus_authorize && dplus_priority) + { CDPlusAuthenticator auth(login_call, website); dplus = auth.Process(qnDB, dplus_reflectors, dplus_repeaters); - if (0 == dplus) { + if (0 == dplus) + { printf("#Gateways: %s=%d\n", filename.c_str(), count); fprintf(stdout, "DPlus Authorization failed.\n"); - } else { + } + else + { fprintf(stderr, "DPlus Authorization completed!\n"); printf("#Gateways %s=%d %s=%d Total=%d\n", filename.c_str(), count, website.c_str(), dplus, qnDB.Count("GATEWAYS")); } @@ -498,7 +549,8 @@ void CQnetLink::LoadGateways(const std::string &filename) /* compute checksum */ void CQnetLink::calcPFCS(unsigned char *packet, int len) { - unsigned short crc_tabccitt[256] = { + unsigned short crc_tabccitt[256] = + { 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, @@ -521,16 +573,21 @@ void CQnetLink::calcPFCS(unsigned char *packet, int len) unsigned short tmp; short int low, high; - if (len == 56) { + if (len == 56) + { low = 15; high = 54; - } else if (len == 58) { + } + else if (len == 58) + { low = 17; high = 56; - } else + } + else return; - for (short int i=low; i> 8) ^ crc_tabccitt[tmp]; @@ -538,10 +595,13 @@ void CQnetLink::calcPFCS(unsigned char *packet, int len) crc_dstar_ffff = ~crc_dstar_ffff; tmp = crc_dstar_ffff; - if (len == 56) { + if (len == 56) + { packet[54] = (unsigned char)(crc_dstar_ffff & 0xff); packet[55] = (unsigned char)((tmp >> 8) & 0xff); - } else { + } + else + { packet[56] = (unsigned char)(crc_dstar_ffff & 0xff); packet[57] = (unsigned char)((tmp >> 8) & 0xff); } @@ -560,13 +620,16 @@ void CQnetLink::UnpackCallsigns(const std::string &str, std::set &s std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning. std::string::size_type pos = str.find_first_of(delimiters, lastPos); // Find first non-delimiter. - while (std::string::npos != pos || std::string::npos != lastPos) { + while (std::string::npos != pos || std::string::npos != lastPos) + { std::string element = str.substr(lastPos, pos-lastPos); - if (element.length()>=3 && element.length()<=6) { + if (element.length()>=3 && element.length()<=6) + { ToUpper(element); element.resize(CALL_SIZE, ' '); set.insert(element); // Found a token, add it to the list. - } else + } + else fprintf(stderr, "found bad callsign in list: %s\n", str.c_str()); lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters. pos = str.find_first_of(delimiters, lastPos); // Find next non-delimiter. @@ -576,7 +639,8 @@ void CQnetLink::UnpackCallsigns(const std::string &str, std::set &s void CQnetLink::PrintCallsigns(const std::string &key, const std::set &set) { printf("%s = [", key.c_str()); - for (auto it=set.begin(); it!=set.end(); it++) { + for (auto it=set.begin(); it!=set.end(); it++) + { if (it != set.begin()) printf(","); printf("%s", (*it).c_str()); @@ -601,9 +665,12 @@ bool CQnetLink::ReadConfig(const char *cfgFile) owner.resize(CALL_SIZE, ' '); std::string host; cfg.GetValue("ircddb0_host", estr, host, 0, MAXHOSTNAMELEN); - if (0 == host.compare(0, 4, "rrv6")) { + if (0 == host.compare(0, 4, "rrv6")) + { uses_ipv6 = true; - } else { + } + else + { cfg.GetValue("ircddb1_host", estr, host, 0, MAXHOSTNAMELEN); uses_ipv6 = (0 == host.compare(0, 4, "rrv6")) ? true : false; } @@ -611,10 +678,12 @@ bool CQnetLink::ReadConfig(const char *cfgFile) printf("IPv6 linking is enabled\n"); int modules = 0; - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { key.assign("module_"); key.append(1, 'a'+i); - if (cfg.KeyExists(key)) { + if (cfg.KeyExists(key)) + { std::string modem_type; cfg.GetValue(key, estr, modem_type, 1, 16); modules++; @@ -624,14 +693,16 @@ bool CQnetLink::ReadConfig(const char *cfgFile) cfg.GetValue(key+"_auto_link", modem_type, to_remote_g2[i].auto_link); } } - if (0 == modules) { + if (0 == modules) + { fprintf(stderr, "no rf modules defined!\n"); return true; } std::string csv; key.assign("link_admin"); - if (cfg.KeyExists(key)) { + if (cfg.KeyExists(key)) + { cfg.GetValue(key, estr, csv, 0, 10240); UnpackCallsigns(csv, admin); PrintCallsigns(key, admin); @@ -639,14 +710,18 @@ bool CQnetLink::ReadConfig(const char *cfgFile) csv.clear(); key.assign("link_no_link_unlink"); - if (cfg.KeyExists(key)) { + if (cfg.KeyExists(key)) + { cfg.GetValue(key, estr, csv, 0, 10240); UnpackCallsigns(csv, link_blacklist); PrintCallsigns(key, link_blacklist); - } else { + } + else + { csv.clear(); key.assign("link_link_unlink"); - if (cfg.KeyExists(key)) { + if (cfg.KeyExists(key)) + { cfg.GetValue(key, estr, csv, 0, 10240); UnpackCallsigns(csv, link_unlink_user); PrintCallsigns(key, link_unlink_user); @@ -654,13 +729,13 @@ bool CQnetLink::ReadConfig(const char *cfgFile) } key.assign("link_"); - int port; + int port; cfg.GetValue(key+"ref_port", estr, port, 10000, 65535); - rmt_ref_port = (unsigned short)port; + rmt_ref_port = (unsigned short)port; cfg.GetValue(key+"xrf_port", estr, port, 10000, 65535); - rmt_xrf_port = (unsigned short)port; + rmt_xrf_port = (unsigned short)port; cfg.GetValue(key+"dcs_port", estr, port, 10000, 65535); - rmt_dcs_port = (unsigned short)port; + rmt_dcs_port = (unsigned short)port; cfg.GetValue(key+"acknowledge", estr, bool_rptr_ack); cfg.GetValue(key+"announce", estr, announce); int maxdongle; @@ -687,9 +762,12 @@ bool CQnetLink::ReadConfig(const char *cfgFile) cfg.GetValue(key+"use_reflectors", estr, dplus_reflectors); cfg.GetValue(key+"use_repeaters", estr, dplus_repeaters); cfg.GetValue(key+"ref_login", estr, login_call, 0, 6); - if (login_call.length() < 4) { + if (login_call.length() < 4) + { login_call.assign(owner); - } else { + } + else + { ToUpper(login_call); login_call.resize(CALL_SIZE, ' '); } @@ -701,28 +779,33 @@ bool CQnetLink::ReadConfig(const char *cfgFile) /* create our server */ bool CQnetLink::srv_open() { - if (uses_ipv6) { - if (XRFSock6.Open(CSockAddress(AF_INET6, rmt_xrf_port, "any")) || DCSSock6.Open(CSockAddress(AF_INET6, rmt_dcs_port, "any")) || REFSock6.Open(CSockAddress(AF_INET6, rmt_ref_port, "any"))) { + if (uses_ipv6) + { + if (XRFSock6.Open(CSockAddress(AF_INET6, rmt_xrf_port, "any")) || DCSSock6.Open(CSockAddress(AF_INET6, rmt_dcs_port, "any")) || REFSock6.Open(CSockAddress(AF_INET6, rmt_ref_port, "any"))) + { srv_close(); return true; } } - if (XRFSock4.Open(CSockAddress(AF_INET, rmt_xrf_port, "any")) || DCSSock4.Open(CSockAddress(AF_INET, rmt_dcs_port, "any")) || REFSock4.Open(CSockAddress(AF_INET, rmt_ref_port, "any"))) { + if (XRFSock4.Open(CSockAddress(AF_INET, rmt_xrf_port, "any")) || DCSSock4.Open(CSockAddress(AF_INET, rmt_dcs_port, "any")) || REFSock4.Open(CSockAddress(AF_INET, rmt_ref_port, "any"))) + { srv_close(); return true; - } + } /* create our gateway unix sockets */ printf("Connecting to qngateway at %s\n", togate.c_str()); - if (ToGate.Open(togate.c_str(), this)) { + if (ToGate.Open(togate.c_str(), this)) + { srv_close(); return true; } /* initialize all remote links */ - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) + { to_remote_g2[i].cs[0] = '\0'; - to_remote_g2[i].addr.Clear(); + to_remote_g2[i].addr.Clear(); to_remote_g2[i].from_mod = ' '; to_remote_g2[i].to_mod = ' '; to_remote_g2[i].countdown = 0; @@ -763,23 +846,25 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) i = 1; else if (from_mod == 'C') i = 2; - else { + else + { printf("from_mod %c invalid\n", from_mod); return; } - to_remote_g2[i].addr.Clear(); - to_remote_g2[i].countdown = 0; - to_remote_g2[i].from_mod = '\0'; - to_remote_g2[i].in_streamid = 0; - to_remote_g2[i].is_connected = false; - to_remote_g2[i].out_streamid = 0; - to_remote_g2[i].cs[0] = '\0'; - to_remote_g2[i].to_mod = '\0'; + to_remote_g2[i].addr.Clear(); + to_remote_g2[i].countdown = 0; + to_remote_g2[i].from_mod = '\0'; + to_remote_g2[i].in_streamid = 0; + to_remote_g2[i].is_connected = false; + to_remote_g2[i].out_streamid = 0; + to_remote_g2[i].cs[0] = '\0'; + to_remote_g2[i].to_mod = '\0'; std::string address; unsigned short port; - if (qnDB.FindGW(call, address, port)) { + if (qnDB.FindGW(call, address, port)) + { sprintf(notify_msg[i], "%c_gatewaynotfound.dat_GATEWAY_NOT_FOUND", from_mod); printf("%s not found in gwy list\n", call); return; @@ -788,10 +873,13 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) strcpy(to_remote_g2[i].cs, call); to_remote_g2[i].to_mod = to_mod; - if ((memcmp(call, "REF", 3) == 0) || (memcmp(call, "DCS", 3) == 0)) { + if ((memcmp(call, "REF", 3) == 0) || (memcmp(call, "DCS", 3) == 0)) + { int counter; - for (counter = 0; counter < 3; counter++) { - if (counter != i) { + for (counter = 0; counter < 3; counter++) + { + if (counter != i) + { if ('\0'!=to_remote_g2[counter].cs[0] && !strcmp(to_remote_g2[counter].cs,to_remote_g2[i].cs) && to_remote_g2[counter].to_mod==to_remote_g2[i].to_mod) break; } @@ -799,24 +887,27 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) to_remote_g2[i].cs[0] = '\0'; to_remote_g2[i].to_mod = ' '; - if (counter < 3) { + if (counter < 3) + { printf("Another mod(%c) is already linked to %s %c\n", to_remote_g2[counter].from_mod, to_remote_g2[counter].cs, to_remote_g2[counter].to_mod); return; } } - if (address.size()) { + if (address.size()) + { ok = resolve_rmt(address.c_str(), port, to_remote_g2[i].addr); - if (!ok) { + if (!ok) + { printf("Call %s is host %s but could not resolve to IP\n", call, address.c_str()); - to_remote_g2[i].addr.Clear(); - to_remote_g2[i].countdown = 0; - to_remote_g2[i].from_mod = '\0'; - to_remote_g2[i].in_streamid = 0; - to_remote_g2[i].is_connected = false; - to_remote_g2[i].out_streamid = 0; - to_remote_g2[i].cs[0] = '\0'; - to_remote_g2[i].to_mod = '\0'; + to_remote_g2[i].addr.Clear(); + to_remote_g2[i].countdown = 0; + to_remote_g2[i].from_mod = '\0'; + to_remote_g2[i].in_streamid = 0; + to_remote_g2[i].is_connected = false; + to_remote_g2[i].out_streamid = 0; + to_remote_g2[i].cs[0] = '\0'; + to_remote_g2[i].to_mod = '\0'; return; } @@ -828,7 +919,8 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) to_remote_g2[i].in_streamid= 0x0; /* is it XRF? */ - if (port == rmt_xrf_port) { + if (port == rmt_xrf_port) + { strcpy(link_request, owner.c_str()); link_request[8] = from_mod; link_request[9] = to_mod; @@ -838,7 +930,9 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) for (int j=0; j<5; j++) XRFWrite(link_request, CALL_SIZE + 3, to_remote_g2[i].addr); - } else if (port == rmt_dcs_port) { + } + else if (port == rmt_dcs_port) + { strcpy(link_request, owner.c_str()); link_request[8] = from_mod; link_request[9] = to_mod; @@ -848,15 +942,20 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) printf("sending link request from mod %c to link with: [%s] mod %c [%s]:%u\n", to_remote_g2[i].from_mod, to_remote_g2[i].cs, to_remote_g2[i].to_mod, address.c_str(), port); DCSWrite(link_request, 519, to_remote_g2[i].addr); - } else if (port == rmt_ref_port) { + } + else if (port == rmt_ref_port) + { int counter; - for (counter = 0; counter < 3; counter++) { - if (counter != i) { + for (counter = 0; counter < 3; counter++) + { + if (counter != i) + { if ( (to_remote_g2[counter].cs[0] != '\0') && (strcmp(to_remote_g2[counter].cs, to_remote_g2[i].cs) == 0) ) break; } } - if (counter > 2) { + if (counter > 2) + { printf("sending link command from mod %c to: [%s] mod %c [%s]:%u\n", to_remote_g2[i].from_mod, to_remote_g2[i].cs, to_remote_g2[i].to_mod, address.c_str(), port); queryCommand[0] = 5; @@ -866,8 +965,11 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) queryCommand[4] = 1; REFWrite(queryCommand, 5, to_remote_g2[i].addr); - } else { - if (to_remote_g2[counter].is_connected) { + } + else + { + if (to_remote_g2[counter].is_connected) + { to_remote_g2[i].is_connected = true; printf("Local module %c is also connected to %s %c\n", from_mod, call, to_mod); @@ -879,7 +981,8 @@ void CQnetLink::g2link(const char from_mod, const char *call, const char to_mod) if (space_p) *space_p = '\0'; sprintf(notify_msg[i], "%c_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); - } else + } + else printf("status from %s %c pending\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); } } @@ -897,12 +1000,16 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) /* A packet of length (CALL_SIZE + 1) is a keepalive from a repeater/reflector */ /* If it is from a dongle, it is either a keepalive or a request to connect */ bool found = false; - if (length == (CALL_SIZE + 1)) { + if (length == (CALL_SIZE + 1)) + { /* Find out if it is a keepalive from a repeater */ - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) + { found = true; - if (!to_remote_g2[i].is_connected) { + if (!to_remote_g2[i].is_connected) + { tracing[i].last_time = time(NULL); to_remote_g2[i].is_connected = true; @@ -919,14 +1026,20 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) to_remote_g2[i].countdown = TIMEOUT; } } - } else if (length == (CALL_SIZE + 6)) { + } + else if (length == (CALL_SIZE + 6)) + { /* A packet of length (CALL_SIZE + 6) is either an ACK or a NAK from repeater-reflector */ /* Because we sent a request before asking to link */ - for (int i=0; i<3; i++) { - if ((fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port)) { - if (0==memcmp(buf + 10, "ACK", 3) && to_remote_g2[i].from_mod==buf[8]) { - if (!to_remote_g2[i].is_connected) { + for (int i=0; i<3; i++) + { + if ((fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port)) + { + if (0==memcmp(buf + 10, "ACK", 3) && to_remote_g2[i].from_mod==buf[8]) + { + if (!to_remote_g2[i].is_connected) + { tracing[i].last_time = time(NULL); to_remote_g2[i].is_connected = true; @@ -940,7 +1053,9 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) *space_p = '\0'; sprintf(notify_msg[i], "%c_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); } - } else if (0==memcmp(buf + 10, "NAK", 3) && to_remote_g2[i].from_mod==buf[8]) { + } + else if (0==memcmp(buf + 10, "NAK", 3) && to_remote_g2[i].from_mod==buf[8]) + { printf("Link module %c to [%s] %c is rejected\n", to_remote_g2[i].from_mod, to_remote_g2[i].cs, to_remote_g2[i].to_mod); sprintf(notify_msg[i], "%c_failed_link.dat_FAILED_TO_LINK", to_remote_g2[i].from_mod); @@ -954,16 +1069,22 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) } } } - } else if (length == CALL_SIZE + 3) { + } + else if (length == CALL_SIZE + 3) + { // A packet of length (CALL_SIZE + 3) is a request // from a remote repeater to link-unlink with our repeater /* Check our linked repeaters/reflectors */ - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) { - if (to_remote_g2[i].to_mod == buf[8]) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) + { + if (to_remote_g2[i].to_mod == buf[8]) + { /* unlink request from remote repeater that we know */ - if (buf[9] == ' ') { + if (buf[9] == ' ') + { printf("Received: %.*s\n", length - 1, buf); printf("Module %c to [%s] %c is unlinked\n", to_remote_g2[i].from_mod, to_remote_g2[i].cs, to_remote_g2[i].to_mod); qnDB.DeleteLS(to_remote_g2[i].addr.GetAddress()); @@ -975,7 +1096,9 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) to_remote_g2[i].countdown = 0; to_remote_g2[i].is_connected = false; to_remote_g2[i].in_streamid = 0x0; - } else if ((i==0 && buf[9]=='A') || (i==1 && buf[9]=='B') || (i==2 && buf[9]=='C')) {/* link request from a remote repeater that we know */ + } + else if ((i==0 && buf[9]=='A') || (i==1 && buf[9]=='B') || (i==2 && buf[9]=='C')) /* link request from a remote repeater that we know */ + { /* I HAVE TO ADD CODE here to PREVENT the REMOTE NODE from LINKING one of their remote modules to @@ -1002,7 +1125,8 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) memcpy(buf + 10, "ACK", 4); XRFWrite(buf, CALL_SIZE+6, to_remote_g2[i].addr); - if (to_remote_g2[i].from_mod != buf[9]) { + if (to_remote_g2[i].from_mod != buf[9]) + { to_remote_g2[i].from_mod = buf[9]; char linked_remote_system[CALL_SIZE + 1]; @@ -1028,22 +1152,29 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) i = 2; /* Is this repeater listed in gwys.txt? */ - if (qnDB.FindGW(call)) { + if (qnDB.FindGW(call)) + { int rc = regexec(&preg, call, 0, NULL, 0); - if (rc != 0) { + if (rc != 0) + { printf("Invalid repeater %s, %s requesting to link\n", call, ip.c_str()); i = -1; } - } else { + } + else + { /* We did NOT find this repeater in gwys.txt, reject the incoming link request */ printf("Incoming link from %s,%s but not found in gwys.txt\n", call, ip.c_str()); i = -1; } - if (i >= 0) { + if (i >= 0) + { /* Is the local repeater module linked to anything ? */ - if (to_remote_g2[i].to_mod == ' ') { - if (buf[8]>='A' && buf[8]<='E') { + if (to_remote_g2[i].to_mod == ' ') + { + if (buf[8]>='A' && buf[8]<='E') + { /* I HAVE TO ADD CODE here to PREVENT the REMOTE NODE from LINKING one of their remote modules to @@ -1077,32 +1208,42 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) memcpy(buf + 10, "ACK", 4); XRFWrite(buf, CALL_SIZE+6, to_remote_g2[i].addr); } - } else { - if (fromDst4 != to_remote_g2[i].addr) { + } + else + { + if (fromDst4 != to_remote_g2[i].addr) + { /* Our repeater module is linked to another repeater-reflector */ memcpy(buf + 10, "NAK", 4); - if (fromDst4.GetPort() != rmt_xrf_port) { + if (fromDst4.GetPort() != rmt_xrf_port) + { fromDst4.Initialize(fromDst4.GetFamily(), rmt_xrf_port, fromDst4.GetAddress()); } XRFWrite(buf, CALL_SIZE+6, fromDst4); } } } - } else if ((length==56 || length==27) && 0==memcmp(buf, "DSVT", 4) && (buf[4]==0x10 || buf[4]==0x20) && buf[8]==0x20) { + } + else if ((length==56 || length==27) && 0==memcmp(buf, "DSVT", 4) && (buf[4]==0x10 || buf[4]==0x20) && buf[8]==0x20) + { /* reset countdown and protect against hackers */ found = false; - for (int i=0; i<3; i++) { - if ((fromDst4 == to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort() == rmt_xrf_port)) { + for (int i=0; i<3; i++) + { + if ((fromDst4 == to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort() == rmt_xrf_port)) + { to_remote_g2[i].countdown = TIMEOUT; found = true; } } - SDSVT dsvt; memcpy(dsvt.title, buf, length); // copy to struct + SDSVT dsvt; + memcpy(dsvt.title, buf, length); // copy to struct /* process header */ - if ((length == 56) && found) { + if ((length == 56) && found) + { char source_stn[9]; memset(source_stn, ' ', 9); source_stn[8] = '\0'; @@ -1113,10 +1254,13 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) /* A reflector will send to us its own RPT1 */ /* A repeater will send to us our RPT1 */ - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_xrf_port) + { /* it is a reflector, reflector's rpt1 */ - if (0==memcmp(dsvt.hdr.rpt1, to_remote_g2[i].cs, 7) && dsvt.hdr.rpt1[7]==to_remote_g2[i].to_mod) { + if (0==memcmp(dsvt.hdr.rpt1, to_remote_g2[i].cs, 7) && dsvt.hdr.rpt1[7]==to_remote_g2[i].to_mod) + { memcpy(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE); dsvt.hdr.rpt1[7] = to_remote_g2[i].from_mod; memcpy(dsvt.hdr.urcall, "CQCQCQ ", 8); @@ -1124,9 +1268,11 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) memcpy(source_stn, to_remote_g2[i].cs, 8); source_stn[7] = to_remote_g2[i].to_mod; break; - } else + } + else /* it is a repeater, our rpt1 */ - if (memcmp(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt1[7]==to_remote_g2[i].from_mod) { + if (memcmp(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt1[7]==to_remote_g2[i].from_mod) + { memcpy(source_stn, to_remote_g2[i].cs, 8); source_stn[7] = to_remote_g2[i].to_mod; break; @@ -1152,9 +1298,11 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) i = 2; /* are we sure that RPT1 is our system? */ - if (0==memcmp(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && i>=0) { + if (0==memcmp(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && i>=0) + { /* Last Heard */ - if (old_sid[i].sid != dsvt.streamid) { + if (old_sid[i].sid != dsvt.streamid) + { if (qso_details) printf("START from remote g2: streamID=%04x, flags=%02x:%02x:%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes fromIP=%s, source=%.8s\n", ntohs(dsvt.streamid), dsvt.hdr.flag[0], dsvt.hdr.flag[1], dsvt.hdr.flag[2], dsvt.hdr.mycall, dsvt.hdr.sfx, dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, length, fromDst4.GetAddress(), source_stn); @@ -1164,14 +1312,17 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) tmp1[8] = '\0'; // delete the user if exists - for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) { - if (0 == strcmp((char *)dt_lh_pos->second.c_str(), tmp1)) { + for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) + { + if (0 == strcmp((char *)dt_lh_pos->second.c_str(), tmp1)) + { dt_lh_list.erase(dt_lh_pos); break; } } /* Limit?, delete oldest user */ - if (dt_lh_list.size() == LH_MAX_SIZE) { + if (dt_lh_list.size() == LH_MAX_SIZE) + { auto dt_lh_pos = dt_lh_list.begin(); dt_lh_list.erase(dt_lh_pos); } @@ -1189,11 +1340,15 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) /* send data to donglers */ /* no changes here */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; - if (fromDst4 == inbound->addr) { + if (fromDst4 == inbound->addr) + { inbound->mod = dsvt.hdr.rpt1[7]; - } else { + } + else + { SREFDSVT rdsvt; rdsvt.head[0] = 58U; rdsvt.head[1] = 0x80U; @@ -1209,12 +1364,14 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) /* If Yes, then broadcast */ int k = i + 1; - if (k < 3) { + if (k < 3) + { brd_from_xrf_idx = 0; auto streamid_raw = ntohs(dsvt.streamid); /* We can only enter this loop up to 2 times max */ - for (int j=k; j<3; j++) { + for (int j=k; j<3; j++) + { /* it is a remote gateway, not a dongle user */ if (fromDst4==to_remote_g2[j].addr && /* it is xrf */ @@ -1222,7 +1379,8 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) 0==memcmp(to_remote_g2[j].cs, "XRF", 3) && /* it is the same xrf and xrf module */ 0==memcmp(to_remote_g2[j].cs, to_remote_g2[i].cs, 8) && - to_remote_g2[j].to_mod==to_remote_g2[i].to_mod) { + to_remote_g2[j].to_mod==to_remote_g2[i].to_mod) + { /* send the packet to another module of our local repeater: this is multi-link */ /* generate new packet */ @@ -1251,16 +1409,19 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) } } - if ((to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].is_connected) { - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + if ((to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].is_connected) + { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { if ( /*** (memcmp(readBuffer2 + 42, owner, 8) != 0) && ***/ /* block repeater announcements */ (memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) == 0) && /* CQ calls only */ (dsvt.hdr.flag[0] == 0x00 || /* normal */ - dsvt.hdr.flag[0] == 0x08 || /* EMR */ - dsvt.hdr.flag[0] == 0x20 || /* BK */ - dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ + dsvt.hdr.flag[0] == 0x08 || /* EMR */ + dsvt.hdr.flag[0] == 0x20 || /* BK */ + dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ - dsvt.hdr.rpt2[7] == 'G') { + dsvt.hdr.rpt2[7] == 'G') + { to_remote_g2[i].in_streamid = dsvt.streamid; /* inform XRF about the source */ @@ -1274,15 +1435,18 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) XRFWrite(dsvt.title, 56, to_remote_g2[i].addr); } - } else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + { if ( /*** (memcmp(readBuffer2 + 42, owner, 8) != 0) && ***/ /* block repeater announcements */ - 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && /* CQ calls only */ - (dsvt.hdr.flag[0] == 0x00 || /* normal */ - dsvt.hdr.flag[0] == 0x08 || /* EMR */ - dsvt.hdr.flag[0] == 0x20 || /* BK */ - dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ - 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ - dsvt.hdr.rpt2[7] == 'G') { + 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && /* CQ calls only */ + (dsvt.hdr.flag[0] == 0x00 || /* normal */ + dsvt.hdr.flag[0] == 0x08 || /* EMR */ + dsvt.hdr.flag[0] == 0x20 || /* BK */ + dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ + 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ + dsvt.hdr.rpt2[7] == 'G') + { to_remote_g2[i].in_streamid = dsvt.streamid; SREFDSVT rdsvt; @@ -1303,15 +1467,18 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) REFWrite(rdsvt.head, 58, to_remote_g2[i].addr); } - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { if ( /*** (memcmp(readBuffer2 + 42, owner, 8) != 0) && ***/ /* block repeater announcements */ - 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && /* CQ calls only */ - (dsvt.hdr.flag[0] == 0x00 || /* normal */ - dsvt.hdr.flag[0] == 0x08 || /* EMR */ - dsvt.hdr.flag[0] == 0x20 || /* BK */ - dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ - 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ - dsvt.hdr.rpt2[7] == 'G') { + 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && /* CQ calls only */ + (dsvt.hdr.flag[0] == 0x00 || /* normal */ + dsvt.hdr.flag[0] == 0x08 || /* EMR */ + dsvt.hdr.flag[0] == 0x20 || /* BK */ + dsvt.hdr.flag[0] == 0x28) && /* EMR + BK */ + 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ + dsvt.hdr.rpt2[7] == 'G') + { to_remote_g2[i].in_streamid = dsvt.streamid; memcpy(xrf_2_dcs[i].mycall, dsvt.hdr.mycall, CALL_SIZE); @@ -1321,10 +1488,15 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) } } } - } else if (found) { // length is 27 - if ((dsvt.ctrl & 0x40) != 0) { - for (int i=0; i<3; i++) { - if (old_sid[i].sid == dsvt.streamid) { + } + else if (found) // length is 27 + { + if ((dsvt.ctrl & 0x40) != 0) + { + for (int i=0; i<3; i++) + { + if (old_sid[i].sid == dsvt.streamid) + { if (qso_details) printf("END from remote g2: streamID=%04x, %d bytes from IP=%s\n", ntohs(dsvt.streamid), length, fromDst4.GetAddress()); old_sid[i].sid = 0x0; @@ -1339,9 +1511,11 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) /* send data to donglers */ /* no changes here */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; - if (fromDst4 != inbound->addr) { + if (fromDst4 != inbound->addr) + { SREFDSVT rdsvt; rdsvt.head[0] = (dsvt.ctrl & 0x40U) ? 32U : 29U; rdsvt.head[1] = 0x80U; @@ -1355,33 +1529,42 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) } /* do we have to broadcast ? */ - if (brd_from_xrf.xrf_streamid == dsvt.streamid) { + if (brd_from_xrf.xrf_streamid == dsvt.streamid) + { memcpy(from_xrf_torptr_brd.title, dsvt.title, 27); - if (brd_from_xrf.rptr_streamid[0] != 0x0) { + if (brd_from_xrf.rptr_streamid[0] != 0x0) + { from_xrf_torptr_brd.streamid = brd_from_xrf.rptr_streamid[0]; ToGate.Write(from_xrf_torptr_brd.title, 27); } - if (brd_from_xrf.rptr_streamid[1] != 0x0) { + if (brd_from_xrf.rptr_streamid[1] != 0x0) + { from_xrf_torptr_brd.streamid = brd_from_xrf.rptr_streamid[1]; ToGate.Write(from_xrf_torptr_brd.title, 27); } - if (dsvt.ctrl & 0x40) { + if (dsvt.ctrl & 0x40) + { brd_from_xrf.xrf_streamid = brd_from_xrf.rptr_streamid[0] = brd_from_xrf.rptr_streamid[1] = 0x0; brd_from_xrf_idx = 0; } } - for (int i=0; i<3; i++) { - if (to_remote_g2[i].is_connected && (to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].in_streamid==dsvt.streamid) { - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + for (int i=0; i<3; i++) + { + if (to_remote_g2[i].is_connected && (to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].in_streamid==dsvt.streamid) + { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { /* inform XRF about the source */ dsvt.flagb[2] = to_remote_g2[i].from_mod; XRFWrite(dsvt.title, 27, to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + { SREFDSVT rdsvt; rdsvt.head[0] = (dsvt.ctrl & 0x40) ? 32U : 29U; rdsvt.head[1] = 0x80U; @@ -1390,7 +1573,9 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) if (32U == rdsvt.head[0]) memcpy(rdsvt.dsvt.vend.end, endbytes, 6); REFWrite(rdsvt.head, rdsvt.head[0], to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { unsigned char dcs_buf[1000]; memset(dcs_buf, 0x00, 600); dcs_buf[0] = dcs_buf[1] = dcs_buf[2] = '0'; @@ -1419,7 +1604,8 @@ void CQnetLink::ProcessXRF(unsigned char *buf, const int length) DCSWrite(dcs_buf, 100, to_remote_g2[i].addr); } - if (dsvt.ctrl & 0x40) { + if (dsvt.ctrl & 0x40) + { to_remote_g2[i].in_streamid = 0x0; } break; @@ -1434,16 +1620,20 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) const std::string ip(fromDst4.GetAddress()); /* header, audio */ - if (dcs_buf[0]=='0' && dcs_buf[1]=='0' && dcs_buf[2]=='0' && dcs_buf[3]=='1') { - if (length == 100) { + if (dcs_buf[0]=='0' && dcs_buf[1]=='0' && dcs_buf[2]=='0' && dcs_buf[3]=='1') + { + if (length == 100) + { char source_stn[9]; memset(source_stn, ' ', 9); source_stn[8] = '\0'; /* find out our local module */ int i; - for (i=0; i<3; i++) { - if (to_remote_g2[i].is_connected && fromDst4==to_remote_g2[i].addr && 0==memcmp(dcs_buf + 7, to_remote_g2[i].cs, 7) && to_remote_g2[i].to_mod==dcs_buf[14]) { + for (i=0; i<3; i++) + { + if (to_remote_g2[i].is_connected && fromDst4==to_remote_g2[i].addr && 0==memcmp(dcs_buf + 7, to_remote_g2[i].cs, 7) && to_remote_g2[i].to_mod==dcs_buf[14]) + { memcpy(source_stn, to_remote_g2[i].cs, 8); source_stn[7] = to_remote_g2[i].to_mod; break; @@ -1451,9 +1641,11 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) } /* Is it our local module */ - if (i < 3) { + if (i < 3) + { /* Last Heard */ - if (memcmp(&old_sid[i].sid, dcs_buf + 43, 2)) { + if (memcmp(&old_sid[i].sid, dcs_buf + 43, 2)) + { if (qso_details) printf("START from dcs: streamID=%02x%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes fromIP=%s, source=%.8s\n", dcs_buf[44],dcs_buf[43], &dcs_buf[31], &dcs_buf[39], &dcs_buf[23], &dcs_buf[7], &dcs_buf[15], length, fromDst4.GetAddress(), source_stn); @@ -1463,14 +1655,17 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) tmp1[8] = '\0'; // delete the user if exists - for (auto dt_lh_pos=dt_lh_list.begin(); dt_lh_pos!=dt_lh_list.end(); dt_lh_pos++) { - if (strcmp(dt_lh_pos->second.c_str(), tmp1) == 0) { + for (auto dt_lh_pos=dt_lh_list.begin(); dt_lh_pos!=dt_lh_list.end(); dt_lh_pos++) + { + if (strcmp(dt_lh_pos->second.c_str(), tmp1) == 0) + { dt_lh_list.erase(dt_lh_pos); break; } } /* Limit?, delete oldest user */ - if (dt_lh_list.size() == LH_MAX_SIZE) { + if (dt_lh_list.size() == LH_MAX_SIZE) + { auto dt_lh_pos = dt_lh_list.begin(); dt_lh_list.erase(dt_lh_pos); } @@ -1486,7 +1681,8 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) to_remote_g2[i].countdown = TIMEOUT; /* new stream ? */ - if (memcmp(&to_remote_g2[i].in_streamid, dcs_buf+43, 2)) { + if (memcmp(&to_remote_g2[i].in_streamid, dcs_buf+43, 2)) + { memcpy(&to_remote_g2[i].in_streamid, dcs_buf+43, 2); dcs_seq[i] = 0xff; @@ -1523,14 +1719,16 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) ToGate.Write(rdsvt.dsvt.title, 56); /* send the data to the donglers */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; for (int j=0; j<5; j++) REFWrite(rdsvt.head, 58, inbound->addr); } } - if (0==memcmp(&to_remote_g2[i].in_streamid, dcs_buf+43, 2) && dcs_seq[i]!=dcs_buf[45]) { + if (0==memcmp(&to_remote_g2[i].in_streamid, dcs_buf+43, 2) && dcs_seq[i]!=dcs_buf[45]) + { dcs_seq[i] = dcs_buf[45]; SREFDSVT rdsvt; rdsvt.head[0] = (dcs_buf[45] & 0x40U) ? 32U : 29U; @@ -1557,12 +1755,14 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) ToGate.Write(rdsvt.dsvt.title, 27); /* send the data to the donglers */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; REFWrite(rdsvt.head, rdsvt.head[0], inbound->addr); } - if ((dcs_buf[45] & 0x40) != 0) { + if ((dcs_buf[45] & 0x40) != 0) + { old_sid[i].sid = 0x0; if (qso_details) @@ -1574,12 +1774,14 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) } } } - } else if (dcs_buf[0]=='E' && dcs_buf[1]=='E' && dcs_buf[2]=='E' && dcs_buf[3]=='E') + } + else if (dcs_buf[0]=='E' && dcs_buf[1]=='E' && dcs_buf[2]=='E' && dcs_buf[3]=='E') ; else if (length == 35) ; /* is this a keepalive 22 bytes */ - else if (length == 22) { + else if (length == 22) + { int i = -1; if (dcs_buf[17] == 'A') i = 0; @@ -1590,10 +1792,13 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) /* It is one of our valid repeaters */ // DG1HT from owner 8 to 7 - if (i>=0 && 0==memcmp(dcs_buf + 9, owner.c_str(), CALL_SIZE-1)) { + if (i>=0 && 0==memcmp(dcs_buf + 9, owner.c_str(), CALL_SIZE-1)) + { /* is that the remote system that we asked to connect to? */ - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_dcs_port && 0==memcmp(to_remote_g2[i].cs, dcs_buf, 7) && to_remote_g2[i].to_mod==dcs_buf[7]) { - if (!to_remote_g2[i].is_connected) { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_dcs_port && 0==memcmp(to_remote_g2[i].cs, dcs_buf, 7) && to_remote_g2[i].to_mod==dcs_buf[7]) + { + if (!to_remote_g2[i].is_connected) + { tracing[i].last_time = time(NULL); to_remote_g2[i].is_connected = true; @@ -1610,7 +1815,9 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) to_remote_g2[i].countdown = TIMEOUT; } } - } else if (length == 14) { /* is this a reply to our link/unlink request: 14 bytes */ + } + else if (length == 14) /* is this a reply to our link/unlink request: 14 bytes */ + { int i = -1; if (dcs_buf[8] == 'A') i = 0; @@ -1620,12 +1827,16 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) i = 2; /* It is one of our valid repeaters */ - if ((i >= 0) && (memcmp(dcs_buf, owner.c_str(), CALL_SIZE) == 0)) { + if ((i >= 0) && (memcmp(dcs_buf, owner.c_str(), CALL_SIZE) == 0)) + { /* It is from a remote that we contacted */ - if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_dcs_port) && (to_remote_g2[i].from_mod == dcs_buf[8])) { - if ((to_remote_g2[i].to_mod == dcs_buf[9]) && (memcmp(dcs_buf + 10, "ACK", 3) == 0)) { + if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_dcs_port) && (to_remote_g2[i].from_mod == dcs_buf[8])) + { + if ((to_remote_g2[i].to_mod == dcs_buf[9]) && (memcmp(dcs_buf + 10, "ACK", 3) == 0)) + { to_remote_g2[i].countdown = TIMEOUT; - if (!to_remote_g2[i].is_connected) { + if (!to_remote_g2[i].is_connected) + { tracing[i].last_time = time(NULL); to_remote_g2[i].is_connected = true; @@ -1639,7 +1850,9 @@ void CQnetLink::ProcessDCS(unsigned char *dcs_buf, const int length) *space_p = '\0'; sprintf(notify_msg[i], "%c_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); } - } else if (memcmp(dcs_buf + 10, "NAK", 3) == 0) { + } + else if (memcmp(dcs_buf + 10, "NAK", 3) == 0) + { printf("Link module %c to [%s] %c is unlinked\n", to_remote_g2[i].from_mod, to_remote_g2[i].cs, to_remote_g2[i].to_mod); sprintf(notify_msg[i], "%c_failed_link.dat_UNLINKED", to_remote_g2[i].from_mod); @@ -1663,13 +1876,15 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) bool found = false; /* LH */ - if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==7 && buf[3]==0) { + if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==7 && buf[3]==0) + { unsigned short j_idx = 0; unsigned short k_idx = 0; unsigned char tmp[2]; auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { //SINBOUND *inbound = (SINBOUND *)pos->second; // printf("Remote station %s %s requested LH list\n", inbound_ptr->call, ip); @@ -1683,13 +1898,15 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) time(&tnow); memcpy(buf + 6, (char *)&tnow, sizeof(time_t)); - for (auto r_dt_lh_pos = dt_lh_list.rbegin(); r_dt_lh_pos != dt_lh_list.rend(); r_dt_lh_pos++) { + for (auto r_dt_lh_pos = dt_lh_list.rbegin(); r_dt_lh_pos != dt_lh_list.rend(); r_dt_lh_pos++) + { /* each entry has 24 bytes */ /* start at position 10 to bypass the header */ strcpy((char *)buf + 10 + (24 * j_idx), r_dt_lh_pos->second.c_str()); auto p = strchr((char *)r_dt_lh_pos->first.c_str(), '='); - if (p) { + if (p) + { memcpy((char *)buf + 18 + (24 * j_idx), p + 2, 8); /* if local or local w/gps */ @@ -1700,7 +1917,9 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) tnow = atol(r_dt_lh_pos->first.c_str()); *p = '='; memcpy(buf + 26 + (24 * j_idx), &tnow, sizeof(time_t)); - } else { + } + else + { memcpy(buf + 18 + (24 * j_idx), "ERROR ", 8); time(&tnow); memcpy(buf + 26 + (24 * j_idx), &tnow, sizeof(time_t)); @@ -1714,7 +1933,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) j_idx++; /* process 39 entries at a time */ - if (j_idx == 39) { + if (j_idx == 39) + { /* 39 * 24 = 936 + 10 header = 946 */ buf[0] = 0xb2; buf[1] = 0xc3; @@ -1729,7 +1949,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - if (j_idx != 0) { + if (j_idx != 0) + { k_idx = 10 + (j_idx * 24); memcpy(tmp, (char *)&k_idx, 2); buf[0] = tmp[0]; @@ -1742,8 +1963,10 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, k_idx, fromDst4); } } - /* linked repeaters request */ - } else if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==5 && buf[3]==0) { + /* linked repeaters request */ + } + else if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==5 && buf[3]==0) + { if (log_debug) printf("Got a linked repeater request!\n"); unsigned short i_idx = 0; @@ -1753,7 +1976,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) unsigned short total = 0; auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { //SINBOUND *inbound = (SINBOUND *)pos->second; // printf("Remote station %s %s requested linked repeaters list\n", inbound_ptr->call, ip); @@ -1769,9 +1993,11 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) buf[6] = tmp[0]; buf[7] = tmp[1]; - for (int i=0, i_idx=0; i<3; i++, i_idx++) { + for (int i=0, i_idx=0; i<3; i++, i_idx++) + { /* each entry has 20 bytes */ - if (to_remote_g2[i].to_mod != ' ') { + if (to_remote_g2[i].to_mod != ' ') + { if (i == 0) buf[8 + (20 * j_idx)] = 'A'; else if (i == 1) @@ -1794,7 +2020,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) j_idx++; - if (j_idx == 39) { + if (j_idx == 39) + { /* 20 bytes for each user, so 39 * 20 = 780 bytes + 8 bytes header = 788 */ buf[0] = 0x14; buf[1] = 0xc3; @@ -1810,7 +2037,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - if (j_idx != 0) { + if (j_idx != 0) + { k_idx = 8 + (j_idx * 20); memcpy(tmp, (char *)&k_idx, 2); buf[0] = tmp[0]; @@ -1828,8 +2056,10 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, 8+(j_idx*20), fromDst4); } } - /* connected user list request */ - } else if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==6 && buf[3]==0) { + /* connected user list request */ + } + else if (length==4 && buf[0]==4 && buf[1]==192 && buf[2]==6 && buf[3]==0) + { if (log_debug) printf("Got a linked dongle request!!\n"); unsigned short i_idx = 0; @@ -1839,7 +2069,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) unsigned short total = 0; auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { // printf("Remote station %s %s requested connected user list\n", inbound_ptr->call, ip); /* header is 8 bytes */ /* reply type */ @@ -1852,7 +2083,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) buf[6] = tmp[0]; buf[7] = tmp[1]; - for (pos = inbound_list.begin(), i_idx = 0; pos != inbound_list.end(); pos++, i_idx++) { + for (pos = inbound_list.begin(), i_idx = 0; pos != inbound_list.end(); pos++, i_idx++) + { /* each entry has 20 bytes */ buf[8 + (20 * j_idx)] = ' '; SINBOUND *inbound = (SINBOUND *)pos->second; @@ -1875,7 +2107,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) j_idx++; - if (j_idx == 39) { + if (j_idx == 39) + { /* 20 bytes for each user, so 39 * 20 = 788 bytes + 8 bytes header = 788 */ buf[0] = 0x14; buf[1] = 0xc3; @@ -1891,7 +2124,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - if (j_idx != 0) { + if (j_idx != 0) + { k_idx = 8 + (j_idx * 20); memcpy(tmp, (char *)&k_idx, 2); buf[0] = tmp[0]; @@ -1909,15 +2143,18 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, 8+(j_idx*20), fromDst4); } } - /* date request */ - } else if (length== 4 && buf[0]==4 && buf[1]==192 && buf[2]==8 && buf[3]==0) { + /* date request */ + } + else if (length== 4 && buf[0]==4 && buf[1]==192 && buf[2]==8 && buf[3]==0) + { if (log_debug) printf("Got a dongle time request!!\n"); time_t ltime; struct tm tm; auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { //SINBOUND *inbound = (SINBOUND *)pos->second; // printf("Remote station %s %s requested date\n", inbound_ptr->call, ip); @@ -1930,17 +2167,20 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) buf[6] = 0x37; buf[7] = 0x4d; snprintf((char *)buf + 8, 99, "20%02d/%02d/%02d %02d:%02d:%02d %5.5s", - tm.tm_year % 100, tm.tm_mon+1,tm.tm_mday, tm.tm_hour,tm.tm_min,tm.tm_sec, - (tzname[0] == NULL)?" ":tzname[0]); + tm.tm_year % 100, tm.tm_mon+1,tm.tm_mday, tm.tm_hour,tm.tm_min,tm.tm_sec, + (tzname[0] == NULL)?" ":tzname[0]); REFWrite(buf, 34, fromDst4); } - /* version request */ - } else if (length== 4 && buf[0]==4 && buf[1]==192 && buf[2]==3 && buf[3]==0) { + /* version request */ + } + else if (length== 4 && buf[0]==4 && buf[1]==192 && buf[2]==3 && buf[3]==0) + { if (log_debug) printf("Got a version request!!\n"); auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { //SINBOUND *inbound = (SINBOUND *)pos->second; // printf("Remote station %s %s requested version\n", inbound_ptr->call, ip); @@ -1951,14 +2191,17 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, 9, fromDst4); } } - else if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==0) { + else if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==0) + { if (log_debug) printf("Got a disconnect request!!\n"); /* reply with the same DISCONNECT */ REFWrite(buf, 5, fromDst4); - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { printf("Call %s disconnected\n", to_remote_g2[i].cs); to_remote_g2[i].cs[0] = '\0'; @@ -1971,7 +2214,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { qnDB.DeleteLS(pos->first.c_str()); SINBOUND *inbound = pos->second; if (memcmp(inbound->call, "1NFO", 4) != 0) @@ -1981,10 +2225,13 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { found = true; - if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==1) { + if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==1) + { printf("Connected to call %s\n", to_remote_g2[i].cs); queryCommand[0] = 28; queryCommand[1] = 192; @@ -1992,7 +2239,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) queryCommand[3] = 0; memcpy(queryCommand + 4, login_call.c_str(), CALL_SIZE); - for (int j=11; j>3; j--) { + for (int j=11; j>3; j--) + { if (queryCommand[j] == ' ') queryCommand[j] = '\0'; else @@ -2010,12 +2258,17 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - for (int i=0; i<3; i++) { - if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_ref_port)) { + for (int i=0; i<3; i++) + { + if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_ref_port)) + { found = true; - if (length==8 && buf[0]==8 && buf[1]==192 && buf[2]==4 && buf[3]==0) { - if (buf[4]== 79 && buf[5]==75 && buf[6]==82) { - if (!to_remote_g2[i].is_connected) { + if (length==8 && buf[0]==8 && buf[1]==192 && buf[2]==4 && buf[3]==0) + { + if (buf[4]== 79 && buf[5]==75 && buf[6]==82) + { + if (!to_remote_g2[i].is_connected) + { to_remote_g2[i].is_connected = true; to_remote_g2[i].countdown = TIMEOUT; printf("Login OK to call %s mod %c\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); @@ -2030,7 +2283,9 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) *space_p = '\0'; sprintf(notify_msg[i], "%c_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); } - } else if (buf[4]==70 && buf[5]==65 && buf[6]==73 && buf[7]==76) { + } + else if (buf[4]==70 && buf[5]==65 && buf[6]==73 && buf[7]==76) + { printf("Login failed to call %s mod %c\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); sprintf(notify_msg[i], "%c_failed_link.dat_FAILED_TO_LINK", to_remote_g2[i].from_mod); @@ -2041,7 +2296,9 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) to_remote_g2[i].countdown = 0; to_remote_g2[i].is_connected = false; to_remote_g2[i].in_streamid = 0x0; - } else if (buf[4]==66 && buf[5]==85 && buf[6]==83 && buf[7]==89) { + } + else if (buf[4]==66 && buf[5]==85 && buf[6]==83 && buf[7]==89) + { printf("Busy or unknown status from call %s mod %c\n", to_remote_g2[i].cs, to_remote_g2[i].to_mod); sprintf(notify_msg[i], "%c_failed_link.dat_FAILED_TO_LINK", to_remote_g2[i].from_mod); @@ -2057,17 +2314,22 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - for (int i=0; i<3; i++) { - if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_ref_port)) { + for (int i=0; i<3; i++) + { + if ((fromDst4==to_remote_g2[i].addr) && (to_remote_g2[i].addr.GetPort()==rmt_ref_port)) + { found = true; - if (length==24 && buf[0]==24 && buf[1]==192 && buf[2]==3 && buf[3]==0) { + if (length==24 && buf[0]==24 && buf[1]==192 && buf[2]==3 && buf[3]==0) + { to_remote_g2[i].countdown = TIMEOUT; } } } - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { found = true; if (length == 3) to_remote_g2[i].countdown = TIMEOUT; @@ -2076,7 +2338,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) /* find out if it is a connected dongle */ auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { SINBOUND *inbound = (SINBOUND *)pos->second; found = true; inbound->countdown = TIMEOUT; @@ -2085,23 +2348,28 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) ***/ } - if (!found) { + if (!found) + { /* The incoming packet is not in the list of outbound repeater connections. and it is not a connected dongle. In this case, this must be an INCOMING dongle request */ - if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==1) { + if (length==5 && buf[0]==5 && buf[1]==0 && buf[2]==24 && buf[3]==0 && buf[4]==1) + { if ((inbound_list.size() + 1) > max_dongles) printf("Inbound DONGLE-p connection from %s but over the max_dongles limit of %d\n", ip.c_str(), (int)inbound_list.size()); else REFWrite(buf, 5, fromDst4); - } else if (length==28 && buf[0]==28 && buf[1]==192 && buf[2]==4 && buf[3]==0) { + } + else if (length==28 && buf[0]==28 && buf[1]==192 && buf[2]==4 && buf[3]==0) + { /* verify callsign */ char call[CALL_SIZE + 1]; memcpy(call, buf + 4, CALL_SIZE); call[CALL_SIZE] = '\0'; - for (int i=7; i>0; i--) { + for (int i=7; i>0; i--) + { if (call[i] == '\0') call[i] = ' '; else @@ -2115,7 +2383,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) printf("Inbound DONGLE-p connection from %s but over the max_dongles limit of %d\n", ip.c_str(), (int)inbound_list.size()); //else if (admin.size() && (admin.find(call) == admin.end())) // printf("Incoming call [%s] from %s not an ADMIN\n", call, ip.c_str()); - else if (regexec(&preg, call, 0, NULL, 0) != 0) { + else if (regexec(&preg, call, 0, NULL, 0) != 0) + { printf("Invalid dongle callsign: CALL=%s,ip=%s\n", call, ip.c_str()); buf[0] = 8; @@ -2125,10 +2394,13 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) buf[7] = 76; REFWrite(buf, 8, fromDst4); - } else { + } + else + { /* add the dongle to the inbound list */ SINBOUND *inbound = new SINBOUND; - if (inbound) { + if (inbound) + { inbound->countdown = TIMEOUT; inbound->addr = fromDst4; strcpy(inbound->call, call); @@ -2143,7 +2415,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) inbound->client = 'D'; /* dongle */ auto insert_pair = inbound_list.insert(std::pair(ip, inbound)); - if (insert_pair.second) { + if (insert_pair.second) + { if (memcmp(inbound->call, "1NFO", 4) != 0) printf("new CALL=%s, DONGLE-p, ip=%s, users=%d\n", inbound->call, ip.c_str(), (int)inbound_list.size()); @@ -2153,7 +2426,9 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, 8, fromDst4); qnDB.UpdateLS(ip.c_str(), 'p', inbound->call, 'p', time(NULL)); - } else { + } + else + { printf("failed to add CALL=%s,ip=%s\n", inbound->call, ip.c_str()); delete inbound; @@ -2162,7 +2437,9 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(buf, 8, fromDst4); } - } else { + } + else + { printf("new SINBOUND failed for call=%s,ip=%s\n", call, ip.c_str()); buf[0] = 8; @@ -2174,18 +2451,23 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - if ((length==58 || length==29 || length==32) && 0==memcmp(buf + 2, "DSVT", 4) && (buf[6]==0x10 || buf[6]==0x20) && buf[10]==0x20) { + if ((length==58 || length==29 || length==32) && 0==memcmp(buf + 2, "DSVT", 4) && (buf[6]==0x10 || buf[6]==0x20) && buf[10]==0x20) + { /* Is it one of the donglers or repeaters-reflectors */ found = false; - for (int i=0; i<3; i++) { - if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + for (int i=0; i<3; i++) + { + if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { to_remote_g2[i].countdown = TIMEOUT; found = true; } } - if (!found) { + if (!found) + { auto pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { SINBOUND *inbound = (SINBOUND *)pos->second; inbound->countdown = TIMEOUT; found = true; @@ -2195,7 +2477,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) SREFDSVT rdsvt; memcpy(rdsvt.head, buf, length); // copy to struct - if (length==58 && found) { + if (length==58 && found) + { char source_stn[9]; memset(source_stn, ' ', 9); source_stn[8] = '\0'; @@ -2210,12 +2493,14 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) /* It is from a repeater-reflector, correct rpt1, rpt2 and re-compute pfcs */ int i; - for (i=0; i<3; i++) { + for (i=0; i<3; i++) + { if (fromDst4==to_remote_g2[i].addr && to_remote_g2[i].addr.GetPort()==rmt_ref_port && ( (0==memcmp(rdsvt.dsvt.hdr.rpt1, to_remote_g2[i].cs, 7) && rdsvt.dsvt.hdr.rpt1[7]==to_remote_g2[i].to_mod) || (0==memcmp(rdsvt.dsvt.hdr.rpt2, to_remote_g2[i].cs, 7) && rdsvt.dsvt.hdr.rpt2[7]==to_remote_g2[i].to_mod) - )) { + )) + { memcpy(rdsvt.dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE); rdsvt.dsvt.hdr.rpt1[7] = to_remote_g2[i].from_mod; memcpy(rdsvt.dsvt.hdr.urcall, "CQCQCQ ", CALL_SIZE); @@ -2227,9 +2512,11 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } - if (i == 3) { + if (i == 3) + { pos = inbound_list.find(ip); - if (pos != inbound_list.end()) { + if (pos != inbound_list.end()) + { SINBOUND *inbound = (SINBOUND *)pos->second; memcpy(source_stn, inbound->call, 8); } @@ -2252,14 +2539,16 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) i = 2; /* are we sure that RPT1 is our system? */ - if (0==memcmp(rdsvt.dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && i>=0) { + if (0==memcmp(rdsvt.dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE-1) && i>=0) + { /* Last Heard */ - if (old_sid[i].sid != rdsvt.dsvt.streamid) { + if (old_sid[i].sid != rdsvt.dsvt.streamid) + { if (qso_details) printf("START from remote g2: streamID=%04x, flags=%02x:%02x:%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes fromIP=%s, source=%.8s\n", - ntohs(rdsvt.dsvt.streamid), rdsvt.dsvt.hdr.flag[0], rdsvt.dsvt.hdr.flag[0], rdsvt.dsvt.hdr.flag[0], - rdsvt.dsvt.hdr.mycall, rdsvt.dsvt.hdr.sfx, rdsvt.dsvt.hdr.urcall, rdsvt.dsvt.hdr.rpt1, rdsvt.dsvt.hdr.rpt2, - length, fromDst4.GetAddress(), source_stn); + ntohs(rdsvt.dsvt.streamid), rdsvt.dsvt.hdr.flag[0], rdsvt.dsvt.hdr.flag[0], rdsvt.dsvt.hdr.flag[0], + rdsvt.dsvt.hdr.mycall, rdsvt.dsvt.hdr.sfx, rdsvt.dsvt.hdr.urcall, rdsvt.dsvt.hdr.rpt1, rdsvt.dsvt.hdr.rpt2, + length, fromDst4.GetAddress(), source_stn); // put user into tmp1 char tmp1[CALL_SIZE + 1]; @@ -2267,14 +2556,17 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) tmp1[8] = '\0'; // delete the user if exists - for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) { - if (strcmp((char *)dt_lh_pos->second.c_str(), tmp1) == 0) { + for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) + { + if (strcmp((char *)dt_lh_pos->second.c_str(), tmp1) == 0) + { dt_lh_list.erase(dt_lh_pos); break; } } /* Limit?, delete oldest user */ - if (dt_lh_list.size() == LH_MAX_SIZE) { + if (dt_lh_list.size() == LH_MAX_SIZE) + { auto dt_lh_pos = dt_lh_list.begin(); dt_lh_list.erase(dt_lh_pos); } @@ -2291,7 +2583,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) ToGate.Write(rdsvt.dsvt.title, 56); /* send the data to the donglers */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; if (fromDst4 == inbound->addr) inbound->mod = rdsvt.dsvt.hdr.rpt1[7]; @@ -2299,31 +2592,38 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) REFWrite(rdsvt.head, 58, inbound->addr); } - if ((to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].is_connected) { + if ((to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].is_connected) + { if ( /*** (memcmp(readBuffer2 + 44, owner, 8) != 0) && ***/ /* block repeater announcements */ 0==memcmp(rdsvt.dsvt.hdr.urcall, "CQCQCQ", 6) && /* CQ calls only */ (rdsvt.dsvt.hdr.flag[0]==0x00 || /* normal */ - rdsvt.dsvt.hdr.flag[0]==0x08 || /* EMR */ - rdsvt.dsvt.hdr.flag[0]==0x20 || /* BK */ - rdsvt.dsvt.hdr.flag[7]==0x28) && /* EMR + BK */ + rdsvt.dsvt.hdr.flag[0]==0x08 || /* EMR */ + rdsvt.dsvt.hdr.flag[0]==0x20 || /* BK */ + rdsvt.dsvt.hdr.flag[7]==0x28) && /* EMR + BK */ 0==memcmp(rdsvt.dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && /* rpt2 must be us */ - rdsvt.dsvt.hdr.rpt2[7] == 'G') { + rdsvt.dsvt.hdr.rpt2[7] == 'G') + { to_remote_g2[i].in_streamid = rdsvt.dsvt.streamid; - if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { memcpy(rdsvt.dsvt.hdr.rpt1, to_remote_g2[i].cs, CALL_SIZE); rdsvt.dsvt.hdr.rpt1[7] = to_remote_g2[i].to_mod; memcpy(rdsvt.dsvt.hdr.rpt2, to_remote_g2[i].cs, CALL_SIZE); rdsvt.dsvt.hdr.rpt2[7] = 'G'; calcPFCS(rdsvt.dsvt.title, 56); - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { /* inform XRF about the source */ rdsvt.dsvt.flagb[2] = to_remote_g2[i].from_mod; XRFWrite(rdsvt.dsvt.title, 56, to_remote_g2[i].addr); - } else + } + else REFWrite(rdsvt.head, 58, to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { memcpy(ref_2_dcs[i].mycall, rdsvt.dsvt.hdr.mycall, 8); memcpy(ref_2_dcs[i].sfx, rdsvt.dsvt.hdr.sfx, 4); ref_2_dcs[i].dcs_rptr_seq = 0; @@ -2331,10 +2631,15 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) } } } - } else if (found) { - if (rdsvt.dsvt.ctrl & 0x40U) { - for (int i=0; i<3; i++) { - if (old_sid[i].sid == rdsvt.dsvt.streamid) { + } + else if (found) + { + if (rdsvt.dsvt.ctrl & 0x40U) + { + for (int i=0; i<3; i++) + { + if (old_sid[i].sid == rdsvt.dsvt.streamid) + { if (qso_details) printf("END from remote g2: streamID=%04x, %d bytes from IP=%s\n", ntohs(rdsvt.dsvt.streamid), length, fromDst4.GetAddress()); @@ -2349,22 +2654,29 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) ToGate.Write(rdsvt.dsvt.title, 27); /* send the data to the donglers */ - for (pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; - if (fromDst4 != inbound->addr) { + if (fromDst4 != inbound->addr) + { REFWrite(rdsvt.head, rdsvt.head[0], inbound->addr); } } - for (int i=0; i<3; i++) { - if (to_remote_g2[i].is_connected && (to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].in_streamid==rdsvt.dsvt.streamid) { - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + for (int i=0; i<3; i++) + { + if (to_remote_g2[i].is_connected && (to_remote_g2[i].addr != fromDst4) && to_remote_g2[i].in_streamid==rdsvt.dsvt.streamid) + { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { /* inform XRF about the source */ rdsvt.dsvt.flagb[2] = to_remote_g2[i].from_mod; XRFWrite(rdsvt.dsvt.title, 27, to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + } + else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) REFWrite(rdsvt.head, rdsvt.head[0], to_remote_g2[i].addr); - else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { unsigned char dcs_buf[600]; memset(dcs_buf, 0x00, 600); dcs_buf[0] = dcs_buf[1] = dcs_buf[2] = '0'; @@ -2394,7 +2706,8 @@ void CQnetLink::ProcessREF(unsigned char *buf, const int length) DCSWrite(dcs_buf, 100, to_remote_g2[i].addr); } - if (rdsvt.dsvt.ctrl & 0x40) { + if (rdsvt.dsvt.ctrl & 0x40) + { to_remote_g2[i].in_streamid = 0x0; } break; @@ -2415,9 +2728,12 @@ void CQnetLink::Process() // initialize all request links bool first = true; - for (int i=0; i<3; i++) { - if (8 == link_at_startup[i].length()) { - if (first) { + for (int i=0; i<3; i++) + { + if (8 == link_at_startup[i].length()) + { + if (first) + { printf("sleep for 15 sec before link at startup\n"); sleep(15); first = false; @@ -2428,10 +2744,12 @@ void CQnetLink::Process() } } - while (keep_running) { + while (keep_running) + { static bool loadG[3] = { false, false, false }; time(&tnow); - if (keep_running && (tnow - heartbeat) > 0) { + if (keep_running && (tnow - heartbeat) > 0) + { send_heartbeat(); time(&heartbeat); } @@ -2439,8 +2757,10 @@ void CQnetLink::Process() // play a qnvoice file if it is specified // this could be coming from qnvoice or qngateway (connected2network or notincache) std::ifstream voicefile(qnvoice_file.c_str(), std::ifstream::in); - if (voicefile) { - if (keep_running) { + if (voicefile) + { + if (keep_running) + { char line[FILENAME_MAX]; voicefile.getline(line, FILENAME_MAX); // trim whitespace @@ -2465,7 +2785,8 @@ void CQnetLink::Process() AddFDSet(max_nfds, XRFSock4.GetSocket(), &fdset); AddFDSet(max_nfds, DCSSock4.GetSocket(), &fdset); AddFDSet(max_nfds, REFSock4.GetSocket(), &fdset); - if (uses_ipv6) { + if (uses_ipv6) + { AddFDSet(max_nfds, XRFSock6.GetSocket(), &fdset); AddFDSet(max_nfds, DCSSock6.GetSocket(), &fdset); AddFDSet(max_nfds, REFSock6.GetSocket(), &fdset); @@ -2474,49 +2795,57 @@ void CQnetLink::Process() tv.tv_sec = 0; tv.tv_usec = 20000; auto sval = select(max_nfds + 1, &fdset, 0, 0, &tv); - if (0 > sval) { + if (0 > sval) + { fprintf(stderr, "select error: %s\n", strerror(errno)); keep_running = false; } unsigned char buffer[1000]; - if (keep_running && FD_ISSET(XRFSock4.GetSocket(), &fdset)) { + if (keep_running && FD_ISSET(XRFSock4.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = XRFSock4.Read(buffer, 1000, fromDst4); ProcessXRF(buffer, length); FD_CLR(XRFSock4.GetSocket(), &fdset); } - if (keep_running && FD_ISSET(REFSock4.GetSocket(), &fdset)) { + if (keep_running && FD_ISSET(REFSock4.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = REFSock4.Read(buffer, 1000, fromDst4); ProcessREF(buffer, length); FD_CLR (REFSock4.GetSocket(), &fdset); } - if (keep_running && FD_ISSET(DCSSock4.GetSocket(), &fdset)) { + if (keep_running && FD_ISSET(DCSSock4.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = DCSSock4.Read(buffer, 1000, fromDst4); ProcessDCS(buffer, length); FD_CLR(DCSSock4.GetSocket(), &fdset); } - if (uses_ipv6) { - if (keep_running && FD_ISSET(XRFSock6.GetSocket(), &fdset)) { + if (uses_ipv6) + { + if (keep_running && FD_ISSET(XRFSock6.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = XRFSock6.Read(buffer, 1000, fromDst4); ProcessXRF(buffer, length); FD_CLR(XRFSock6.GetSocket(), &fdset); } - if (keep_running && FD_ISSET(REFSock6.GetSocket(), &fdset)) { + if (keep_running && FD_ISSET(REFSock6.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = REFSock6.Read(buffer, 1000, fromDst4); ProcessREF(buffer, length); FD_CLR (REFSock6.GetSocket(), &fdset); } - if (keep_running && FD_ISSET(DCSSock6.GetSocket(), &fdset)) { + if (keep_running && FD_ISSET(DCSSock6.GetSocket(), &fdset)) + { socklen_t fromlen = sizeof(struct sockaddr_storage); int length = DCSSock6.Read(buffer, 1000, fromDst4); ProcessDCS(buffer, length); @@ -2524,14 +2853,17 @@ void CQnetLink::Process() } } - if (keep_running && FD_ISSET(ToGate.GetFD(), &fdset)) { + if (keep_running && FD_ISSET(ToGate.GetFD(), &fdset)) + { unsigned char your[3] = { 'C', 'C', 'C' }; SDSVT dsvt; int length = ToGate.Read(dsvt.title, 56); - if ((length==56 || length==27) && 0==memcmp(dsvt.title,"DSVT", 4U) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U)) { + if ((length==56 || length==27) && 0==memcmp(dsvt.title,"DSVT", 4U) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U)) + { - if (length == 56) { + if (length == 56) + { if (qso_details) printf("START from local g2: streamID=%04x, flags=%02x:%02x:%02x, my=%.8s/%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes on %s\n", ntohs(dsvt.streamid), dsvt.hdr.flag[0], dsvt.hdr.flag[1], dsvt.hdr.flag[2], dsvt.hdr.mycall, dsvt.hdr.sfx, dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, length, togate.c_str()); @@ -2548,7 +2880,8 @@ void CQnetLink::Process() else if (dsvt.hdr.rpt1[7] == 'C') i = 2; - if (i >= 0) { + if (i >= 0) + { // save the first char of urcall your[i] = dsvt.hdr.urcall[0]; // used by rptr_ack memcpy(dtmf_mycall[i], dsvt.hdr.mycall, 8); @@ -2564,14 +2897,17 @@ void CQnetLink::Process() tmp1[8] = '\0'; // delete the user if exists - for (auto dt_lh_pos=dt_lh_list.begin(); dt_lh_pos!=dt_lh_list.end(); dt_lh_pos++) { - if (strcmp(dt_lh_pos->second.c_str(), tmp1) == 0) { + for (auto dt_lh_pos=dt_lh_list.begin(); dt_lh_pos!=dt_lh_list.end(); dt_lh_pos++) + { + if (strcmp(dt_lh_pos->second.c_str(), tmp1) == 0) + { dt_lh_list.erase(dt_lh_pos); break; } } /* Limit?, delete oldest user */ - if (dt_lh_list.size() == LH_MAX_SIZE) { + if (dt_lh_list.size() == LH_MAX_SIZE) + { auto dt_lh_pos = dt_lh_list.begin(); dt_lh_list.erase(dt_lh_pos); } @@ -2585,27 +2921,33 @@ void CQnetLink::Process() tracing[i].last_time = time(NULL); } - if (memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && i>=0) { - if (memcmp(dsvt.hdr.urcall, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.urcall[0] != ' ' && dsvt.hdr.urcall[7] == 'L' && 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt2[7] == 'G' && (dsvt.hdr.flag[0]==0x00 || dsvt.hdr.flag[0]==0x08 || dsvt.hdr.flag[0]==0x20 || dsvt.hdr.flag[0]==0x28)) { + if (memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && i>=0) + { + if (memcmp(dsvt.hdr.urcall, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.urcall[0] != ' ' && dsvt.hdr.urcall[7] == 'L' && 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt2[7] == 'G' && (dsvt.hdr.flag[0]==0x00 || dsvt.hdr.flag[0]==0x08 || dsvt.hdr.flag[0]==0x20 || dsvt.hdr.flag[0]==0x28)) + { if ( - // if there is a black list, is he in the blacklist? - (link_blacklist.size() && link_blacklist.end()!=link_blacklist.find(call)) || - // or if there is an allow list, is he not in it? - (link_unlink_user.size() && link_unlink_user.find(call)==link_unlink_user.end()) - ) { + // if there is a black list, is he in the blacklist? + (link_blacklist.size() && link_blacklist.end()!=link_blacklist.find(call)) || + // or if there is an allow list, is he not in it? + (link_unlink_user.size() && link_unlink_user.find(call)==link_unlink_user.end()) + ) + { printf("link request denied, unauthorized user [%s]\n", call); - } else { + } + else + { char temp_repeater[CALL_SIZE + 1]; memset(temp_repeater, ' ', CALL_SIZE); memcpy(temp_repeater, dsvt.hdr.urcall, CALL_SIZE - 2); temp_repeater[CALL_SIZE] = '\0'; if ((to_remote_g2[i].cs[0] == '\0') || /* not linked */ - ((to_remote_g2[i].cs[0] != '\0') && /* waiting for a link reply that may never arrive */ - !to_remote_g2[i].is_connected)) + ((to_remote_g2[i].cs[0] != '\0') && /* waiting for a link reply that may never arrive */ + !to_remote_g2[i].is_connected)) g2link(dsvt.hdr.rpt1[7], temp_repeater, dsvt.hdr.urcall[6]); - else if (to_remote_g2[i].is_connected) { + else if (to_remote_g2[i].is_connected) + { char linked_remote_system[CALL_SIZE + 1]; strcpy(linked_remote_system, to_remote_g2[i].cs); auto space_p = strchr(linked_remote_system, ' '); @@ -2614,29 +2956,40 @@ void CQnetLink::Process() sprintf(notify_msg[i], "%c_already_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); } } - } else if (0==memcmp(dsvt.hdr.urcall, " U", CALL_SIZE)) { + } + else if (0==memcmp(dsvt.hdr.urcall, " U", CALL_SIZE)) + { if ( - // if there is a black list, is he in the blacklist? - (link_blacklist.size() && link_blacklist.end()!=link_blacklist.find(call)) || - // or if there is an allow list, is he not in it? - (link_unlink_user.size() && link_unlink_user.find(call)==link_unlink_user.end()) - ) { + // if there is a black list, is he in the blacklist? + (link_blacklist.size() && link_blacklist.end()!=link_blacklist.find(call)) || + // or if there is an allow list, is he not in it? + (link_unlink_user.size() && link_unlink_user.find(call)==link_unlink_user.end()) + ) + { printf("unlink request denied, unauthorized user [%s]\n", call); - } else { - if (to_remote_g2[i].cs[0] != '\0') { - if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) { + } + else + { + if (to_remote_g2[i].cs[0] != '\0') + { + if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + { /* Check to see if any other local bands are linked to that same IP */ int j; - for (j=0; j<3; j++) { - if (j != i) { - if (to_remote_g2[j].addr==to_remote_g2[i].addr && to_remote_g2[j].addr.GetPort()==rmt_ref_port) { + for (j=0; j<3; j++) + { + if (j != i) + { + if (to_remote_g2[j].addr==to_remote_g2[i].addr && to_remote_g2[j].addr.GetPort()==rmt_ref_port) + { printf("Info: Local %c is also linked to %s (different module) %c\n", to_remote_g2[j].from_mod, to_remote_g2[j].cs, to_remote_g2[j].to_mod); break; } } } - if (j == 3) { + if (j == 3) + { /* nothing else is linked there, send DISCONNECT */ queryCommand[0] = 5; queryCommand[1] = 0; @@ -2645,7 +2998,9 @@ void CQnetLink::Process() queryCommand[4] = 0; REFWrite(queryCommand, 5, to_remote_g2[i].addr); } - } else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { char unlink_request[CALL_SIZE + 3]; strcpy(unlink_request, owner.c_str()); unlink_request[8] = to_remote_g2[i].from_mod; @@ -2654,7 +3009,9 @@ void CQnetLink::Process() for (int j=0; j<5; j++) XRFWrite(unlink_request, CALL_SIZE+3, to_remote_g2[i].addr); - } else { + } + else + { char cmd_2_dcs[23]; strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[8] = to_remote_g2[i].from_mod; @@ -2676,28 +3033,39 @@ void CQnetLink::Process() to_remote_g2[i].countdown = 0; to_remote_g2[i].is_connected = false; to_remote_g2[i].in_streamid = 0x0; - } else { + } + else + { sprintf(notify_msg[i], "%c_already_unlinked.dat_UNLINKED", dsvt.hdr.rpt1[7]); } } } - else if (0 == memcmp(dsvt.hdr.urcall, " I", CALL_SIZE)) { - if (to_remote_g2[i].is_connected) { + else if (0 == memcmp(dsvt.hdr.urcall, " I", CALL_SIZE)) + { + if (to_remote_g2[i].is_connected) + { char linked_remote_system[CALL_SIZE + 1]; strcpy(linked_remote_system, to_remote_g2[i].cs); auto space_p = strchr(linked_remote_system, ' '); if (space_p) *space_p = '\0'; sprintf(notify_msg[i], "%c_linked.dat_LINKED_%s_%c", to_remote_g2[i].from_mod, linked_remote_system, to_remote_g2[i].to_mod); - } else { + } + else + { sprintf(notify_msg[i], "%c_id.dat_%s_NOT_LINKED", dsvt.hdr.rpt1[7], owner.c_str()); } } - else if (0==memcmp(dsvt.hdr.urcall, " ", 6) && dsvt.hdr.urcall[7]=='X') { // execute a script - if (dsvt.hdr.urcall[6] != ' ') { // there has to be a char here - if (admin.size()>0 && admin.end()==admin.find(call)) { // only admins (if defined) can execute scripts + else if (0==memcmp(dsvt.hdr.urcall, " ", 6) && dsvt.hdr.urcall[7]=='X') // execute a script + { + if (dsvt.hdr.urcall[6] != ' ') // there has to be a char here + { + if (admin.size()>0 && admin.end()==admin.find(call)) // only admins (if defined) can execute scripts + { printf("%s not found in admin list, ignoring script %c request\n", call, dsvt.hdr.urcall[6]); - } else { + } + else + { char system_cmd[128]; memset(system_cmd, '\0', sizeof(system_cmd)); snprintf(system_cmd, 127, "%s/exec_%c.sh %s %c &", BIN_DIR, dsvt.hdr.urcall[6], call, dsvt.hdr.rpt1[7]); @@ -2706,24 +3074,35 @@ void CQnetLink::Process() } } } - else if (0==memcmp(dsvt.hdr.urcall, " ", 6) && dsvt.hdr.urcall[6]=='D') { // only ADMIN can block dongle users - if (admin.size()>0 && admin.end()==admin.find(call)) { + else if (0==memcmp(dsvt.hdr.urcall, " ", 6) && dsvt.hdr.urcall[6]=='D') // only ADMIN can block dongle users + { + if (admin.size()>0 && admin.end()==admin.find(call)) + { printf("%s not found in admin list, ignoring dongle gate request\n", call); - } else { - if (dsvt.hdr.urcall[7] == '1') { + } + else + { + if (dsvt.hdr.urcall[7] == '1') + { max_dongles = saved_max_dongles; printf("Dongle connections are now allowed by %s\n", call); - } else if (dsvt.hdr.urcall[7] == '0') { + } + else if (dsvt.hdr.urcall[7] == '0') + { inbound_list.clear(); max_dongles = 0; printf("Dongle connections are now disallowed by %s\n", call); } } } - else if (0==memcmp(dsvt.hdr.urcall, " F", CALL_SIZE)) { // only ADMIN can reload gwys.txt - if (admin.size()>0 && admin.end()==admin.find(call)) { + else if (0==memcmp(dsvt.hdr.urcall, " F", CALL_SIZE)) // only ADMIN can reload gwys.txt + { + if (admin.size()>0 && admin.end()==admin.find(call)) + { printf("%s not found in admin list, ignoring gwys read request\n", call); - } else { + } + else + { loadG[i] = true; } } @@ -2731,7 +3110,8 @@ void CQnetLink::Process() /* send data to the donglers */ SREFDSVT rdsvt; - if (inbound_list.size() > 0) { + if (inbound_list.size() > 0) + { memset(rdsvt.head, 0U, 58U); rdsvt.head[0] = 58U; rdsvt.head[1] = 0x80U; @@ -2744,22 +3124,27 @@ void CQnetLink::Process() memcpy(rdsvt.dsvt.hdr.urcall, "CQCQCQ ", 8); calcPFCS(rdsvt.dsvt.title, 56); - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; for (int j=0; j<5; j++) REFWrite(rdsvt.head, 58, inbound->addr); } } - if (i >= 0) { + if (i >= 0) + { /* do we have to broadcast ? */ /* make sure the source is linked to xrf */ - if (to_remote_g2[i].is_connected && 0==memcmp(to_remote_g2[i].cs, "XRF", 3) && 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt2[7]=='G' && 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6)) { + if (to_remote_g2[i].is_connected && 0==memcmp(to_remote_g2[i].cs, "XRF", 3) && 0==memcmp(dsvt.hdr.rpt2, owner.c_str(), CALL_SIZE-1) && dsvt.hdr.rpt2[7]=='G' && 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6)) + { brd_from_rptr_idx = 0; auto streamid_raw = ntohs(dsvt.streamid); - for (int j=0; j<3; j++) { - if (j!=i && to_remote_g2[j].is_connected && 0==memcmp(to_remote_g2[j].cs, to_remote_g2[i].cs, 8) && to_remote_g2[j].to_mod==to_remote_g2[i].to_mod && to_remote_g2[j].to_mod!='E') { + for (int j=0; j<3; j++) + { + if (j!=i && to_remote_g2[j].is_connected && 0==memcmp(to_remote_g2[j].cs, to_remote_g2[i].cs, 8) && to_remote_g2[j].to_mod==to_remote_g2[i].to_mod && to_remote_g2[j].to_mod!='E') + { memcpy(fromrptr_torptr_brd.title, dsvt.title, 56); if (++streamid_raw == 0) @@ -2784,11 +3169,14 @@ void CQnetLink::Process() } } - if (to_remote_g2[i].is_connected) { - if (0==memcmp(dsvt.hdr.rpt2, owner.c_str(), 7) && 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && dsvt.hdr.rpt2[7] == 'G') { + if (to_remote_g2[i].is_connected) + { + if (0==memcmp(dsvt.hdr.rpt2, owner.c_str(), 7) && 0==memcmp(dsvt.hdr.urcall, "CQCQCQ", 6) && dsvt.hdr.rpt2[7] == 'G') + { to_remote_g2[i].out_streamid = dsvt.streamid; - if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) { + if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { SREFDSVT rdsvt; rdsvt.head[0] = 58U; rdsvt.head[1] = 0x80U; @@ -2803,17 +3191,22 @@ void CQnetLink::Process() memcpy(rdsvt.dsvt.hdr.urcall, "CQCQCQ ", CALL_SIZE); calcPFCS(rdsvt.dsvt.title, 56); - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { /* inform XRF about the source */ rdsvt.dsvt.flagb[2] = to_remote_g2[i].from_mod; calcPFCS(rdsvt.dsvt.title, 56); for (int j=0; j<5; j++) XRFWrite(rdsvt.dsvt.title, 56, to_remote_g2[i].addr); - } else { + } + else + { for (int j=0; j<5; j++) REFWrite(rdsvt.head, 58, to_remote_g2[i].addr); } - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { memcpy(rptr_2_dcs[i].mycall, dsvt.hdr.mycall, CALL_SIZE); memcpy(rptr_2_dcs[i].sfx, dsvt.hdr.sfx, 4); rptr_2_dcs[i].dcs_rptr_seq = 0; @@ -2822,7 +3215,8 @@ void CQnetLink::Process() } } } - else { // length is 27 + else // length is 27 + { SREFDSVT rdsvt; rdsvt.head[0] = (dsvt.ctrl & 0x40U) ? 32U : 29U; rdsvt.head[1] = 0x80U; @@ -2830,43 +3224,56 @@ void CQnetLink::Process() memcpy(rdsvt.dsvt.title, dsvt.title, 27); if (dsvt.ctrl & 0x40U) memcpy(rdsvt.dsvt.vend.end, endbytes, 6); - if (inbound_list.size() > 0) { + if (inbound_list.size() > 0) + { - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { REFWrite(rdsvt.head, rdsvt.head[0], pos->second->addr); } } - for (int i=0; i<3; i++) { - if (to_remote_g2[i].is_connected && to_remote_g2[i].out_streamid==dsvt.streamid) { + for (int i=0; i<3; i++) + { + if (to_remote_g2[i].is_connected && to_remote_g2[i].out_streamid==dsvt.streamid) + { /* check for broadcast */ - if (brd_from_rptr.from_rptr_streamid == dsvt.streamid) { + if (brd_from_rptr.from_rptr_streamid == dsvt.streamid) + { memcpy(fromrptr_torptr_brd.title, dsvt.title, 27); - if (brd_from_rptr.to_rptr_streamid[0]) { + if (brd_from_rptr.to_rptr_streamid[0]) + { fromrptr_torptr_brd.streamid = brd_from_rptr.to_rptr_streamid[0]; ToGate.Write(fromrptr_torptr_brd.title, 27); } - if (brd_from_rptr.to_rptr_streamid[1]) { + if (brd_from_rptr.to_rptr_streamid[1]) + { fromrptr_torptr_brd.streamid = brd_from_rptr.to_rptr_streamid[1]; ToGate.Write(fromrptr_torptr_brd.title, 27); } - if (dsvt.ctrl & 0x40U) { + if (dsvt.ctrl & 0x40U) + { brd_from_rptr.from_rptr_streamid = brd_from_rptr.to_rptr_streamid[0] = brd_from_rptr.to_rptr_streamid[1] = 0x0; brd_from_rptr_idx = 0; } } - if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) { - if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + if (to_remote_g2[i].addr.GetPort()==rmt_xrf_port || to_remote_g2[i].addr.GetPort()==rmt_ref_port) + { + if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { /* inform XRF about the source */ rdsvt.dsvt.flagb[2] = to_remote_g2[i].from_mod; XRFWrite(dsvt.title, 27, to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) + } + else if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) REFWrite(rdsvt.head, rdsvt.head[0], to_remote_g2[i].addr); - } else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) { + } + else if (to_remote_g2[i].addr.GetPort() == rmt_dcs_port) + { unsigned char dcs_buf[600]; memset(dcs_buf, 0x0, 600); dcs_buf[0] = dcs_buf[1] = dcs_buf[2] = '0'; @@ -2895,23 +3302,28 @@ void CQnetLink::Process() DCSWrite(dcs_buf, 100, to_remote_g2[i].addr); } - if (dsvt.ctrl & 0x40U) { + if (dsvt.ctrl & 0x40U) + { to_remote_g2[i].out_streamid = 0x0; } break; } } - for (int i=0; i<3; i++) { - if (tracing[i].streamid == dsvt.streamid) { + for (int i=0; i<3; i++) + { + if (tracing[i].streamid == dsvt.streamid) + { /* update the last time RF user talked */ tracing[i].last_time = time(NULL); - if (dsvt.ctrl & 0x40U) { + if (dsvt.ctrl & 0x40U) + { if (qso_details) printf("END from local g2: streamID=%04x, %d bytes\n", ntohs(dsvt.streamid), length); - if ('\0' == notify_msg[i][0]) { + if ('\0' == notify_msg[i][0]) + { if (bool_rptr_ack && ' ' != your[i]) rptr_ack(i); } @@ -2921,18 +3333,24 @@ void CQnetLink::Process() GPS_seen[i] = false; tracing[i].streamid = 0x0; - } else { - if (!GPS_seen[i]) { + } + else + { + if (!GPS_seen[i]) + { memcpy(tmp_txt, dsvt.vasd.text, 3); - if (tmp_txt[0]!=0x55 || tmp_txt[1]!=0x2d || tmp_txt[2]!=0x16) { - if (new_group[i]) { + if (tmp_txt[0]!=0x55 || tmp_txt[1]!=0x2d || tmp_txt[2]!=0x16) + { + if (new_group[i]) + { tmp_txt[0] = tmp_txt[0] ^ 0x70; header_type = tmp_txt[0] & 0xf0; - // header squelch + // header squelch if (header_type== 0x50 || header_type==0xc0) new_group[i] = false; - else if (header_type == 0x30) { /* GPS or GPS id or APRS */ + else if (header_type == 0x30) /* GPS or GPS id or APRS */ + { GPS_seen[i] = true; new_group[i] = false; @@ -2943,11 +3361,14 @@ void CQnetLink::Process() // delete the user if exists and it is a local RF entry p_tmp2 = NULL; char tmp2[36]; - for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) { - if (strcmp((char *)dt_lh_pos->second.c_str(), tmp1) == 0) { + for (auto dt_lh_pos = dt_lh_list.begin(); dt_lh_pos != dt_lh_list.end(); dt_lh_pos++) + { + if (strcmp((char *)dt_lh_pos->second.c_str(), tmp1) == 0) + { strcpy(tmp2, (char *)dt_lh_pos->first.c_str()); p_tmp2 = strstr(tmp2, "=l"); - if (p_tmp2) { + if (p_tmp2) + { dt_lh_list.erase(dt_lh_pos); break; } @@ -2955,15 +3376,18 @@ void CQnetLink::Process() } /* we have tmp1 and tmp2, we have the user and it is already been removed */ /* add the user with gps indicator g */ - if (p_tmp2) { + if (p_tmp2) + { *(p_tmp2 + 1) = 'g'; dt_lh_list[tmp2] = tmp1; } - } else if (header_type == 0x40) /* ABC text */ + } + else if (header_type == 0x40) /* ABC text */ new_group[i] = false; else new_group[i] = false; - } else + } + else new_group[i] = true; } } @@ -2975,12 +3399,15 @@ void CQnetLink::Process() } FD_CLR (ToGate.GetFD(), &fdset); } - for (int i=0; i<3 && keep_running; i++) { - if (notify_msg[i][0] && 0x0U == tracing[i].streamid) { + for (int i=0; i<3 && keep_running; i++) + { + if (notify_msg[i][0] && 0x0U == tracing[i].streamid) + { PlayAudioNotifyThread(notify_msg[i]); notify_msg[i][0] = '\0'; } - if (loadG[i] && 0x0U == tracing[i].streamid) { + if (loadG[i] && 0x0U == tracing[i].streamid) + { LoadGateways(gwys); loadG[i] = false; if (bool_rptr_ack) @@ -2995,7 +3422,8 @@ void CQnetLink::PlayAudioNotifyThread(char *msg) if (! announce) return; - if (msg[0]<'A' || msg[0]>'C') { + if (msg[0]<'A' || msg[0]>'C') + { fprintf(stderr, "Improper module in msg '%s'\n", msg); return; } @@ -3004,19 +3432,24 @@ void CQnetLink::PlayAudioNotifyThread(char *msg) edata.is_linked = (NULL == strstr(msg, "_linked.dat_LINKED_")) ? false : true; char *p = strstr(msg, ".dat"); - if (NULL == p) { + if (NULL == p) + { fprintf(stderr, "Improper AMBE data file in msg '%s'\n", msg); return; } - if ('_' == p[4]) { + if ('_' == p[4]) + { std::string message(p+5); message.resize(20, ' '); strcpy(edata.message, message.c_str()); - for (int i=0; i<20; i++) { + for (int i=0; i<20; i++) + { if ('_' == edata.message[i]) edata.message[i] = ' '; } - } else { + } + else + { strcpy(edata.message, "QnetGateway Message "); } p[4] = '\0'; @@ -3038,9 +3471,12 @@ void CQnetLink::PlayAudioNotifyThread(char *msg) memcpy(edata.header.hdr.sfx, "RPTR", 4); calcPFCS(edata.header.title, 56); - try { + try + { std::async(std::launch::async, &CQnetLink::AudioNotifyThread, this, std::ref(edata)); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { printf ("Failed to start AudioNotifyThread(). Exception: %s\n", e.what()); } return; @@ -3050,7 +3486,8 @@ void CQnetLink::AudioNotifyThread(SECHO &edata) { char mod = edata.header.hdr.rpt1[7]; - if ((mod != 'A') && (mod != 'B') && (mod != 'C')) { + if ((mod != 'A') && (mod != 'B') && (mod != 'C')) + { fprintf(stderr, "Invalid module %c in %s\n", mod, edata.file); return; } @@ -3060,7 +3497,8 @@ void CQnetLink::AudioNotifyThread(SECHO &edata) printf("sending File:[%s], mod:[%c], RADIO_ID=[%s]\n", edata.file, mod, edata.message); struct stat sbuf; - if (stat(edata.file, &sbuf)) { + if (stat(edata.file, &sbuf)) + { fprintf(stderr, "can't stat %s\n", edata.file); return; } @@ -3071,7 +3509,8 @@ void CQnetLink::AudioNotifyThread(SECHO &edata) FILE *fp = fopen(edata.file, "rb"); - if (!fp) { + if (!fp) + { fprintf(stderr, "Failed to open file %s for reading\n", edata.file); return; } @@ -3083,57 +3522,63 @@ void CQnetLink::AudioNotifyThread(SECHO &edata) int count; const unsigned char sdsync[3] = { 0x55U, 0x2DU, 0x16U }; const unsigned char sdsilence[3] = { 0x16U, 0x29U, 0xF5U }; - for (count=0; count> name >> offset >> size; - if (name.size() && offset.size() && size.size()) { + if (name.size() && offset.size() && size.size()) + { unsigned long of = std::stoul(offset); unsigned long sz = std::stoul(size); speak.push_back(1000U * of + sz); @@ -3265,9 +3723,12 @@ bool CQnetLink::Init(const char *cfgfile) } indexfile.close(); } - if (62 == speak.size()) { + if (62 == speak.size()) + { printf("read %d indicies from %s\n", (unsigned int)speak.size(), index.c_str()); - } else { + } + else + { fprintf(stderr, "read unexpected (%d) number of indices from %s\n", (unsigned int)speak.size(), index.c_str()); speak.clear(); } @@ -3285,18 +3746,23 @@ void CQnetLink::Shutdown() queryCommand[2] = 24; queryCommand[3] = 0; queryCommand[4] = 0; - for (int i=0; i<3; i++) { - if (to_remote_g2[i].cs[0] != '\0') { + for (int i=0; i<3; i++) + { + if (to_remote_g2[i].cs[0] != '\0') + { if (to_remote_g2[i].addr.GetPort() == rmt_ref_port) REFWrite(queryCommand, 5, to_remote_g2[i].addr); - else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) { + else if (to_remote_g2[i].addr.GetPort() == rmt_xrf_port) + { strcpy(unlink_request, owner.c_str()); unlink_request[8] = to_remote_g2[i].from_mod; unlink_request[9] = ' '; unlink_request[10] = '\0'; for (int j=0; j<5; j++) XRFWrite(unlink_request, CALL_SIZE+3, to_remote_g2[i].addr); - } else { + } + else + { strcpy(cmd_2_dcs, owner.c_str()); cmd_2_dcs[8] = to_remote_g2[i].from_mod; cmd_2_dcs[9] = ' '; @@ -3316,7 +3782,8 @@ void CQnetLink::Shutdown() } /* tell inbound dongles we are down */ - for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) { + for (auto pos = inbound_list.begin(); pos != inbound_list.end(); pos++) + { SINBOUND *inbound = (SINBOUND *)pos->second; qnDB.DeleteLS(pos->first.c_str()); REFWrite(queryCommand, 5, inbound->addr); @@ -3330,7 +3797,8 @@ void CQnetLink::Shutdown() int main(int argc, char **argv) { - if (argc != 2) { + if (argc != 2) + { printf("Usage: %s configuration_file\n", argv[0]); return 1; } diff --git a/QnetLink.h b/QnetLink.h index aeec6a7..cc346fd 100644 --- a/QnetLink.h +++ b/QnetLink.h @@ -43,24 +43,27 @@ #define TIMEOUT 50 #define LH_MAX_SIZE 39 -using SREFDSVT = struct refdsvt_tag { +using SREFDSVT = struct refdsvt_tag +{ unsigned char head[2]; SDSVT dsvt; }; -using STOREMOTE = struct to_remote_g2_tag { - char cs[CALL_SIZE + 1]; - CSockAddress addr; - char from_mod, to_mod; - short countdown; +using STOREMOTE = struct to_remote_g2_tag +{ + char cs[CALL_SIZE + 1]; + CSockAddress addr; + char from_mod, to_mod; + short countdown; bool auto_link, is_connected; - unsigned short in_streamid; // incoming from remote systems - unsigned short out_streamid; // outgoing to remote systems + unsigned short in_streamid; // incoming from remote systems + unsigned short out_streamid; // outgoing to remote systems }; // This is the data payload in the map: inbound_list // This is for inbound dongles -using SINBOUND = struct inbound_tag { +using SINBOUND = struct inbound_tag +{ char call[CALL_SIZE + 1]; // the callsign of the remote CSockAddress addr; // IP and port of remote short countdown; // if countdown expires, the connection is terminated @@ -68,13 +71,15 @@ using SINBOUND = struct inbound_tag { char client; // dvap, dvdongle }; -using STRACING = struct tracing_tag { +using STRACING = struct tracing_tag +{ unsigned short streamid; time_t last_time; // last time RF user talked }; -class CQnetLink : CKRBase { +class CQnetLink : CKRBase +{ public: // functions CQnetLink(); @@ -111,7 +116,7 @@ private: bool only_admin_login, only_link_unlink, qso_details, log_debug, bool_rptr_ack, announce; bool dplus_authorize, dplus_reflectors, dplus_repeaters, dplus_priority, uses_ipv6; unsigned short rmt_xrf_port, rmt_ref_port, rmt_dcs_port, my_g2_link_port, to_g2_external_port; - int delay_between, delay_before; + int delay_between, delay_before; std::string link_at_startup[3]; unsigned int max_dongles, saved_max_dongles; int rf_inactivity_timer[3]; @@ -129,7 +134,8 @@ private: STOREMOTE to_remote_g2[3]; // broadcast for data arriving from xrf to local rptr - struct brd_from_xrf_tag { + struct brd_from_xrf_tag + { unsigned short xrf_streamid; // streamid from xrf unsigned short rptr_streamid[2]; // generated streamid to rptr(s) } brd_from_xrf; @@ -137,7 +143,8 @@ private: short brd_from_xrf_idx; // broadcast for data arriving from local rptr to xrf - struct brd_from_rptr_tag { + struct brd_from_rptr_tag + { unsigned short from_rptr_streamid; unsigned short to_rptr_streamid[2]; } brd_from_rptr; @@ -172,7 +179,8 @@ private: // END: TEXT crap // this is used for the "dashboard and qso_details" to avoid processing multiple headers - struct old_sid_tag { + struct old_sid_tag + { unsigned short sid; } old_sid[3]; @@ -184,29 +192,35 @@ private: const unsigned char endbytes[6] = { 0x55U, 0x55U, 0x55U, 0x55U, 0xC8U, 0x7AU }; time_t tnow; unsigned char dcs_seq[3] = { 0x00, 0x00, 0x00 }; - struct { + struct + { char mycall[9]; char sfx[5]; unsigned int dcs_rptr_seq; - } rptr_2_dcs[3] = { + } rptr_2_dcs[3] = + { {" ", " ", 0}, {" ", " ", 0}, {" ", " ", 0} }; - struct { + struct + { char mycall[9]; char sfx[5]; unsigned int dcs_rptr_seq; - } ref_2_dcs[3] = { + } ref_2_dcs[3] = + { {" ", " ", 0}, {" ", " ", 0}, {" ", " ", 0} }; - struct { + struct + { char mycall[9]; char sfx[5]; unsigned int dcs_rptr_seq; - } xrf_2_dcs[3] = { + } xrf_2_dcs[3] = + { {" ", " ", 0}, {" ", " ", 0}, {" ", " ", 0} diff --git a/QnetModem.cpp b/QnetModem.cpp index 78fe74f..6d590f1 100644 --- a/QnetModem.cpp +++ b/QnetModem.cpp @@ -66,8 +66,8 @@ const unsigned char TYPE_ACK = 0x70U; const unsigned char TYPE_NACK = 0x7FU; CQnetModem::CQnetModem(int mod) -: assigned_module(mod) -, dstarSpace(0U) + : assigned_module(mod) + , dstarSpace(0U) { } @@ -84,7 +84,8 @@ bool CQnetModem::GetBufferSize() { std::this_thread::sleep_for(std::chrono::seconds(2)); - for (int i=0; i<6; i++) { + for (int i=0; i<6; i++) + { SMODEM frame; frame.start = FRAME_START; @@ -94,10 +95,12 @@ bool CQnetModem::GetBufferSize() if (3 != SendToModem(&frame.start)) return true; - for (int count = 0; count < MAX_RESPONSES; count++) { + for (int count = 0; count < MAX_RESPONSES; count++) + { std::this_thread::sleep_for(std::chrono::milliseconds(10)); EModemResponse resp = GetModemData(&frame.start, sizeof(SVERSION)); - if (resp == EModemResponse::status) { + if (resp == EModemResponse::status) + { dstarSpace = frame.status.dsrsize; printf("D-Star buffer will hold %u voice frames\n", dstarSpace); return false; @@ -114,7 +117,8 @@ bool CQnetModem::GetVersion() { std::this_thread::sleep_for(std::chrono::seconds(2)); - for (int i=0; i<6; i++) { + for (int i=0; i<6; i++) + { SVERSION frame; frame.start = FRAME_START; @@ -124,10 +128,12 @@ bool CQnetModem::GetVersion() if (3 != SendToModem(&frame.start)) return true; - for (int count = 0; count < MAX_RESPONSES; count++) { + for (int count = 0; count < MAX_RESPONSES; count++) + { std::this_thread::sleep_for(std::chrono::milliseconds(10)); EModemResponse resp = GetModemData(&frame.start, sizeof(SVERSION)); - if (resp == EModemResponse::version && frame.length > 14U) { + if (resp == EModemResponse::version && frame.length > 14U) + { frame.version[frame.length-4U] = '\0'; // just to make sure! if (0 == memcmp(frame.version, "MMDVM ", 6U)) hardwareType = EHardwareType::mmdvm; @@ -145,7 +151,8 @@ bool CQnetModem::GetVersion() hardwareType = EHardwareType::nano_dv; else if (0 == memcmp(frame.version, "MMDVM_HS-", 9U)) hardwareType = EHardwareType::mmdvm_hs; - else { + else + { hardwareType = EHardwareType::unknown; } @@ -170,7 +177,8 @@ bool CQnetModem::SetFrequency() if (hardwareType == EHardwareType::dvmega) frame.length = 12U; - else { + else + { frame.frequency.level = 255U; frame.frequency.ps = __builtin_bswap32(htonl(pocsagFrequency)); @@ -188,22 +196,25 @@ bool CQnetModem::SetFrequency() int count = 0; bool got_ack = false; - while (! got_ack) { + while (! got_ack) + { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - switch (GetModemData(&frame.start, sizeof(SMODEM))) { - case EModemResponse::ack: - got_ack = true; - break; - case EModemResponse::nack: - fprintf(stderr, "SET_FREQ failed, returned NACK reason %u\n", frame.nack.reason); + switch (GetModemData(&frame.start, sizeof(SMODEM))) + { + case EModemResponse::ack: + got_ack = true; + break; + case EModemResponse::nack: + fprintf(stderr, "SET_FREQ failed, returned NACK reason %u\n", frame.nack.reason); + return true; + default: + if (++count >= MAX_RESPONSES) + { + fprintf(stderr, "The MMDVM is not responding to the SET_FREQ command!\n"); return true; - default: - if (++count >= MAX_RESPONSES) { - fprintf(stderr, "The MMDVM is not responding to the SET_FREQ command!\n"); - return true; - } - break; + } + break; } } printf("Modem frequencies set: rx=%u(%u) tx=%u(%u) Hz\n", (uint32_t)(1.0E6 * RX_FREQUENCY), rx_frequency, (uint32_t)(1.0E6 * TX_FREQUENCY), tx_frequency); @@ -271,22 +282,25 @@ bool CQnetModem::SetConfiguration() int count = 0; bool got_ack = false; - while (! got_ack) { + while (! got_ack) + { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - switch (GetModemData(&frame.start, sizeof(SMODEM))) { - case EModemResponse::ack: - got_ack = true; - break; - case EModemResponse::nack: - fprintf(stderr, "SET_CONFIG failed, returned NACK reason %u\n", frame.nack.reason); + switch (GetModemData(&frame.start, sizeof(SMODEM))) + { + case EModemResponse::ack: + got_ack = true; + break; + case EModemResponse::nack: + fprintf(stderr, "SET_CONFIG failed, returned NACK reason %u\n", frame.nack.reason); + return true; + default: + if (++count >= MAX_RESPONSES) + { + fprintf(stderr, "The MMDVM is not responding to the SET_CONFIG command!\n"); return true; - default: - if (++count >= MAX_RESPONSES) { - fprintf(stderr, "The MMDVM is not responding to the SET_CONFIG command!\n"); - return true; - } - break; + } + break; } } printf("Modem configuration set for D-Star only\n"); @@ -296,19 +310,22 @@ bool CQnetModem::SetConfiguration() int CQnetModem::OpenModem() { int fd = open(MODEM_DEVICE.c_str(), O_RDWR | O_NOCTTY | O_SYNC, 0); - if (fd < 0) { + if (fd < 0) + { printf("Failed to open device [%s], error=%d, message=%s\n", MODEM_DEVICE.c_str(), errno, strerror(errno)); return -1; } - if (isatty(fd) == 0) { + if (isatty(fd) == 0) + { printf("Device %s is not a tty device\n", MODEM_DEVICE.c_str()); close(fd); return -1; } static termios t; - if (tcgetattr(fd, &t) < 0) { + if (tcgetattr(fd, &t) < 0) + { printf("tcgetattr failed for %s, error=%d, message-%s\n", MODEM_DEVICE.c_str(), errno, strerror(errno)); close(fd); return -1; @@ -325,7 +342,8 @@ int CQnetModem::OpenModem() cfsetospeed(&t, B115200); cfsetispeed(&t, B115200); - if (tcsetattr(fd, TCSANOW, &t) < 0) { + if (tcsetattr(fd, TCSANOW, &t) < 0) + { printf("tcsetattr failed for %s, error=%dm message=%s\n", MODEM_DEVICE.c_str(), errno, strerror(errno)); close(fd); return -1; @@ -336,28 +354,36 @@ int CQnetModem::OpenModem() EModemResponse CQnetModem::GetModemData(unsigned char *buf, unsigned int size) { - if (size < 4U) { + if (size < 4U) + { fprintf(stderr, "Buffer size, %u is too small\n", size); return EModemResponse::error; } // Get the start byte int ret = read(serfd, buf, 1U); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "Error when reading frame start byte: %s\n", strerror(errno)); return EModemResponse::error; - } else if (ret == 0) { + } + else if (ret == 0) + { printf("READ START RETURNED A ZERO!\n"); return EModemResponse::timeout; - } else if (buf[0] != FRAME_START) + } + else if (buf[0] != FRAME_START) return EModemResponse::timeout; //get the length byte ret = read(serfd, buf+1, 1U); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "Error when reading frame length: %s\n", strerror(errno)); return EModemResponse::error; - } else if (ret == 0) { + } + else if (ret == 0) + { printf("READ LENGTH RETURNED A ZERO!\n"); return(EModemResponse::timeout); } @@ -366,62 +392,76 @@ EModemResponse CQnetModem::GetModemData(unsigned char *buf, unsigned int size) // get the type byte ret = read(serfd, buf+2, 1U); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "Error when reading frame type: %s\n", strerror(errno)); return EModemResponse::error; - } else if (ret == 0) { + } + else if (ret == 0) + { printf("READ TYPE RETURNED A ZERO!\n"); return(EModemResponse::timeout); } // get the data unsigned int length = buf[1]; unsigned int offset = 3; - while (offset < length) { + while (offset < length) + { ret = read(serfd, buf + offset, length - offset); - if (ret < 0) { + if (ret < 0) + { printf("Error when reading data: %s\n", strerror(errno)); return EModemResponse::error; } - if (ret == 0) { + if (ret == 0) + { printf("READ DATA RETURNED A ZERO!\n"); return(EModemResponse::timeout); - } else + } + else offset += ret; } - while (junk_count) { + while (junk_count) + { unsigned char junk[8]; ret = read(serfd, junk, (junk_count > 8U) ? 8U : junk_count); - if (ret < 0) { + if (ret < 0) + { printf("Error when reading junk: %s\n", strerror(errno)); return EModemResponse::error; - } else if (ret == 0) { + } + else if (ret == 0) + { printf("READ junk RETURNED A ZERO!\n"); return(EModemResponse::timeout); - } else { + } + else + { junk_count -= (unsigned int)ret; } } - switch (buf[2]) { - case TYPE_ACK: - return EModemResponse::ack; - case TYPE_NACK: - return EModemResponse::nack; - case TYPE_HEADER: - return EModemResponse::header; - case TYPE_DATA: - return EModemResponse::data; - case TYPE_LOST: - return EModemResponse::lost; - case TYPE_EOT: - return EModemResponse::eot; - case TYPE_VERSION: - return EModemResponse::version; - case TYPE_STATUS: - return EModemResponse::status; - default: - return EModemResponse::error; + switch (buf[2]) + { + case TYPE_ACK: + return EModemResponse::ack; + case TYPE_NACK: + return EModemResponse::nack; + case TYPE_HEADER: + return EModemResponse::header; + case TYPE_DATA: + return EModemResponse::data; + case TYPE_LOST: + return EModemResponse::lost; + case TYPE_EOT: + return EModemResponse::eot; + case TYPE_VERSION: + return EModemResponse::version; + case TYPE_STATUS: + return EModemResponse::status; + default: + return EModemResponse::error; }; } @@ -438,7 +478,8 @@ void CQnetModem::Run(const char *cfgfile) CTimer statusTimer; CTimer deadTimer; - while (keep_running) { + while (keep_running) + { SMODEM frame; frame.start = FRAME_START; @@ -454,55 +495,64 @@ void CQnetModem::Run(const char *cfgfile) // don't care about writefds and exceptfds: int ret = select(maxfs+1, &readfds, NULL, NULL, &tv); - if (ret < 0) { + if (ret < 0) + { printf("ERROR: Run: select returned err=%d, %s\n", errno, strerror(errno)); break; } // check for a dead or disconnected radio - if (10.0 < deadTimer.time()) { + if (10.0 < deadTimer.time()) + { printf("no activity from radio for 10 sec. Exiting...\n"); keep_running = false; } - if (keep_running && FD_ISSET(serfd, &readfds)) { + if (keep_running && FD_ISSET(serfd, &readfds)) + { deadTimer.start(); - switch (GetModemData(&frame.start, sizeof(SMODEM))) { - case EModemResponse::data: - case EModemResponse::header: - case EModemResponse::eot: - case EModemResponse::lost: - if (ProcessModem(frame)) - keep_running = false; - break; - case EModemResponse::status: - if (frame.status.flags & 0x02U) - fprintf(stderr, "Modem ADC levels have overflowed\n"); - if (frame.status.flags & 0x04U) - fprintf(stderr, "Modem RX buffer has overflowed\n"); - if (frame.status.flags & 0x08U) - fprintf(stderr, "Modem TX buffer has overflowed\n"); - if (frame.status.flags & 0x20U) - fprintf(stderr, "Modem DAC levels have overflowed\n"); - dstarSpace = frame.status.dsrsize; - break; - default: - break; + switch (GetModemData(&frame.start, sizeof(SMODEM))) + { + case EModemResponse::data: + case EModemResponse::header: + case EModemResponse::eot: + case EModemResponse::lost: + if (ProcessModem(frame)) + keep_running = false; + break; + case EModemResponse::status: + if (frame.status.flags & 0x02U) + fprintf(stderr, "Modem ADC levels have overflowed\n"); + if (frame.status.flags & 0x04U) + fprintf(stderr, "Modem RX buffer has overflowed\n"); + if (frame.status.flags & 0x08U) + fprintf(stderr, "Modem TX buffer has overflowed\n"); + if (frame.status.flags & 0x20U) + fprintf(stderr, "Modem DAC levels have overflowed\n"); + dstarSpace = frame.status.dsrsize; + break; + default: + break; } FD_CLR(serfd, &readfds); } - if (keep_running && FD_ISSET(ug2m, &readfds)) { + if (keep_running && FD_ISSET(ug2m, &readfds)) + { SDSVT dsvt; ssize_t len = ToGate.Read(dsvt.title, sizeof(SDSVT)); - if (len <= 0) { + if (len <= 0) + { break; } - if (0 == memcmp(dsvt.title, "DSVT", 4) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U) && (len==56 || len==27)) { + if (0 == memcmp(dsvt.title, "DSVT", 4) && dsvt.id==0x20U && (dsvt.config==0x10U || dsvt.config==0x20U) && (len==56 || len==27)) + { ProcessGateway(dsvt); - } else { + } + else + { fprintf(stderr, "Unexpected data, returned %d bytes from the gateway: %02x", int(len), *dsvt.title); for (ssize_t i=1; i packet_wait) { // // g2 has timed out // frame.length = 3U; @@ -520,17 +571,20 @@ void CQnetModem::Run(const char *cfgfile) // queue.push(CFrame(&frame.start)); // g2_is_active = false; //} - if (! queue.empty()) { + if (! queue.empty()) + { // send queued D-Star frames to modem CFrame cframe = queue.front(); const unsigned char type = cframe.type(); - if ((type==TYPE_HEADER && dstarSpace>3U) || ((type==TYPE_DATA || type==TYPE_EOT || type==TYPE_LOST) && dstarSpace>0U)) { + if ((type==TYPE_HEADER && dstarSpace>3U) || ((type==TYPE_DATA || type==TYPE_EOT || type==TYPE_LOST) && dstarSpace>0U)) + { SendToModem(cframe.data()); queue.pop(); dstarSpace -= (type==TYPE_HEADER) ? 4U : 1U; } } - if (statusTimer.time() > 0.25) { + if (statusTimer.time() > 0.25) + { // request a status update every 250 milliseconds frame.length = 3U; frame.type = TYPE_STATUS; @@ -550,10 +604,13 @@ int CQnetModem::SendToModem(const unsigned char *buf) size_t sent = 0; ssize_t length = buf[1]; - while ((ssize_t)sent < length) { + while ((ssize_t)sent < length) + { n = write(serfd, buf + sent, length - sent); - if (n < 0) { - if (EAGAIN != errno) { + if (n < 0) + { + if (EAGAIN != errno) + { printf("Error %d writing to dvap, message=%s\n", errno, strerror(errno)); return -1; } @@ -569,7 +626,8 @@ void CQnetModem::ProcessGateway(const SDSVT &dsvt) static std::string superframe; SMODEM frame; // destination frame.start = FRAME_START; - if (0x10U == dsvt.config) { // write a Header packet + if (0x10U == dsvt.config) // write a Header packet + { superframe.clear(); frame.length = 44U; frame.type = TYPE_HEADER; @@ -584,29 +642,39 @@ void CQnetModem::ProcessGateway(const SDSVT &dsvt) PacketWait.start(); if (LOG_QSO) printf("Queued to %s flags=%02x:%02x:%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", MODEM_DEVICE.c_str(), frame.header.flag[0], frame.header.flag[1], frame.header.flag[2], frame.header.ur, frame.header.r2, frame.header.r1, frame.header.my, frame.header.nm); - } else { // write a voice data packet + } + else // write a voice data packet + { //const unsigned char sdsync[3] = { 0x55U, 0x2DU, 0x16U }; - if (dsvt.ctrl & 0x40U) { + if (dsvt.ctrl & 0x40U) + { if (LOG_DEBUG && superframe.size()) printf("Final order: %s\n", superframe.c_str()); frame.length = 3U; frame.type = TYPE_EOT; if (LOG_QSO) printf("Queued modem end of transmission\n"); - } else { + } + else + { frame.length = 15U; frame.type = TYPE_DATA; memcpy(frame.voice.ambe, dsvt.vasd.voice, 12); - if (LOG_DEBUG) { + if (LOG_DEBUG) + { const unsigned int ctrl = dsvt.ctrl & 0x3FU; - if (VoicePacketIsSync(dsvt.vasd.text)) { - if (superframe.size() > 65) { + if (VoicePacketIsSync(dsvt.vasd.text)) + { + if (superframe.size() > 65) + { printf("Frame order: %s\n", superframe.c_str()); superframe.clear(); } const char *ch = "#abcdefghijklmnopqrstuvwxyz"; superframe.append(1, (ctrl<27U) ? ch[ctrl] : '%'); - } else { + } + else + { const char *ch = "!ABCDEFGHIJKLMNOPQRSTUVWXYZ"; superframe.append(1, (ctrl<27U) ? ch[ctrl] : '*'); } @@ -638,7 +706,8 @@ bool CQnetModem::ProcessModem(const SMODEM &frame) dsvt.flagb[2] = ('B'==RPTR_MOD) ? 0x1U : (('C'==RPTR_MOD) ? 0x2U : 0x3U); dsvt.streamid = htons(stream_id); - if (frame.type == TYPE_HEADER) { // header + if (frame.type == TYPE_HEADER) // header + { nextctrl = 21U; in_stream = first_voice_packet = true; dsvt.config = 0x10U; @@ -653,20 +722,26 @@ bool CQnetModem::ProcessModem(const SMODEM &frame) memcpy(dsvt.hdr.mycall, frame.header.my, 8); memcpy(dsvt.hdr.sfx, frame.header.nm, 4); memcpy(dsvt.hdr.pfcs, frame.header.pfcs, 2); - if (ToGate.Write(dsvt.title, 56)) { + if (ToGate.Write(dsvt.title, 56)) + { printf("ERROR: ProcessModem: Could not write gateway header packet\n"); return true; } if (LOG_QSO) printf("Sent DSVT to gateway, streamid=%04x flags=%02x:%02x:%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dsvt.streamid), dsvt.hdr.flag[0], dsvt.hdr.flag[1], dsvt.hdr.flag[2], dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); - } else if (in_stream && (frame.type==TYPE_DATA || frame.type==TYPE_EOT || frame.type==TYPE_LOST)) { // ambe + } + else if (in_stream && (frame.type==TYPE_DATA || frame.type==TYPE_EOT || frame.type==TYPE_LOST)) // ambe + { const unsigned char sync[12] = { 0x9EU,0x8DU,0x32U,0x88U,0x26U,0x1AU,0x3FU,0x61U,0xE8U,0x55U,0x2DU,0x16U }; const unsigned char silence[12] = { 0x9EU,0x8DU,0x32U,0x88U,0x26U,0x1AU,0x3FU,0x61U,0xE8U,0x70U,0x4FU,0x93U }; dsvt.config = 0x20U; - if (frame.type == TYPE_DATA) { + if (frame.type == TYPE_DATA) + { - if (first_voice_packet) { // make sure the first voice packet is a sync frame - if (! VoicePacketIsSync(frame.voice.text)) { // create a quite sync voice packet + if (first_voice_packet) // make sure the first voice packet is a sync frame + { + if (! VoicePacketIsSync(frame.voice.text)) // create a quite sync voice packet + { if (LOG_DEBUG) printf("Warning: Inserting missing frame sync after header\n"); dsvt.ctrl = 0U; @@ -677,18 +752,21 @@ bool CQnetModem::ProcessModem(const SMODEM &frame) first_voice_packet = false; } - if (VoicePacketIsSync(frame.voice.text)) { + if (VoicePacketIsSync(frame.voice.text)) + { if (nextctrl < 21U) fprintf(stderr, "Warning: The last superframe had %u frames, inserting missing frame(s)\n", nextctrl); memcpy(dsvt.vasd.voice, silence, 12U); - while (nextctrl < 21U) { + while (nextctrl < 21U) + { dsvt.ctrl = nextctrl++; ToGate.Write(dsvt.title, 27); } nextctrl = 0x0U; } - if (nextctrl > 20U) { + if (nextctrl > 20U) + { fprintf(stderr, "Warning: nextctrl=%u, inserting missing sync frame\n", nextctrl); dsvt.ctrl = 0U; memcpy(dsvt.vasd.voice, sync, 12U); @@ -697,16 +775,22 @@ bool CQnetModem::ProcessModem(const SMODEM &frame) } memcpy(dsvt.vasd.voice, frame.voice.ambe, 12); - } else { + } + else + { if (frame.type == TYPE_LOST) printf("Got a TYPE_LOST packet.\n"); - if (0U == nextctrl) { + if (0U == nextctrl) + { memcpy(dsvt.vasd.voice, sync, 12); - } else { + } + else + { memcpy(dsvt.vasd.voice, silence, 12); } nextctrl |= 0x40U; - if (LOG_QSO) { + if (LOG_QSO) + { if (frame.type == TYPE_EOT) printf("Sent DSVT end of streamid=%04x\n", ntohs(dsvt.streamid)); else @@ -715,13 +799,17 @@ bool CQnetModem::ProcessModem(const SMODEM &frame) in_stream = false; } dsvt.ctrl = nextctrl++; - if (ToGate.Write(dsvt.title, 27)) { + if (ToGate.Write(dsvt.title, 27)) + { printf("ERROR: ProcessModem: Could not write gateway voice packet\n"); return true; } - } else { - if (in_stream) { + } + else + { + if (in_stream) + { fprintf(stderr, "Warning! Unexpected frame: %02x", frame.start); for (unsigned int i=1U; i assigned_module) { + if (0 > assigned_module) + { // we need to find the lone mmdvmmodem module - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { std::string test(modem_path); test.append(1, 'a'+i); - if (cfg.KeyExists(test)) { + if (cfg.KeyExists(test)) + { cfg.GetValue(test, estr, type, 1, 16); if (type.compare("mmdvmmodem")) continue; // this ain't it! @@ -757,20 +848,27 @@ bool CQnetModem::ReadConfig(const char *cfgFile) break; } } - if (0 > assigned_module) { + if (0 > assigned_module) + { fprintf(stderr, "Error: no 'mmdvmmodem' module found\n!"); return true; } - } else { + } + else + { // make sure mmdvmmodem module is defined modem_path.append(1, 'a' + assigned_module); - if (cfg.KeyExists(modem_path)) { + if (cfg.KeyExists(modem_path)) + { cfg.GetValue(modem_path, estr, type, 1, 16); - if (type.compare("mmdvmmodem")) { + if (type.compare("mmdvmmodem")) + { fprintf(stderr, "%s = %s is not 'mmdvmmodem' type!\n", modem_path.c_str(), type.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "Module '%c' is not defined.\n", 'a'+assigned_module); return true; } @@ -798,22 +896,30 @@ bool CQnetModem::ReadConfig(const char *cfgFile) packet_wait = 1.0E-3 * double(PACKET_WAIT); modem_path.append("_callsign"); - if (cfg.KeyExists(modem_path)) { + if (cfg.KeyExists(modem_path)) + { if (cfg.GetValue(modem_path, type, RPTR, 3, 6)) return true; - } else { + } + else + { modem_path.assign("ircddb_login"); - if (cfg.KeyExists(modem_path)) { + if (cfg.KeyExists(modem_path)) + { if (cfg.GetValue(modem_path, estr, RPTR, 3, 6)) return true; } } int l = RPTR.length(); - if (l<3 || l>6) { + if (l<3 || l>6) + { printf("Call '%s' is invalid length!\n", RPTR.c_str()); return true; - } else { - for (int i=0; i=0x20u && buf[i]<0x7fu) ? buf[i] : '.'; @@ -210,7 +228,8 @@ int CQnetRelay::SendTo(const int fd, const unsigned char *buf, const int size, c bool CQnetRelay::ProcessGateway(const int len, const unsigned char *raw) { - if (27==len || 56==len) { //here is dstar data + if (27==len || 56==len) //here is dstar data + { SDSVT dsvt; ::memcpy(dsvt.title, raw, len); // transfer raw data to SDSVT struct @@ -219,7 +238,8 @@ bool CQnetRelay::ProcessGateway(const int len, const unsigned char *raw) ::memcpy(dsrp.title, "DSRP", 4); dsrp.voice.id = dsvt.streamid; // voice or header is the same position dsrp.voice.seq = dsvt.ctrl; // ditto - if (27 == len) { // write an AMBE packet + if (27 == len) // write an AMBE packet + { dsrp.tag = 0x21U; if (log_qso && (dsrp.voice.seq & 0x40)) printf("Sent DSRP end of streamid=%04x\n", ntohs(dsrp.voice.id)); @@ -228,13 +248,17 @@ bool CQnetRelay::ProcessGateway(const int len, const unsigned char *raw) dsrp.voice.err = 0; // NOT SURE WHERE TO GET THIS FROM THE INPUT buf memcpy(dsrp.voice.ambe, dsvt.vasd.voice, 12); int ret = SendTo(msock, dsrp.title, 21, MMDVM_IP, MMDVM_IN_PORT); - if (ret != 21) { + if (ret != 21) + { printf("ERROR: ProcessGateway: Could not write AMBE mmdvmhost packet\n"); return true; } - } else { // write a Header packet + } + else // write a Header packet + { dsrp.tag = 0x20U; - if (dsrp.header.seq) { + if (dsrp.header.seq) + { // printf("DEBUG: ProcessGateway: unexpected pkt.header.seq %d, resetting to 0\n", pkt.header.seq); dsrp.header.seq = 0; } @@ -247,7 +271,8 @@ bool CQnetRelay::ProcessGateway(const int len, const unsigned char *raw) memcpy(dsrp.header.nm, dsvt.hdr.sfx, 4); memcpy(dsrp.header.pfcs, dsvt.hdr.pfcs, 2); int ret = SendTo(msock, dsrp.title, 49, MMDVM_IP, MMDVM_IN_PORT); - if (ret != 49) { + if (ret != 49) + { printf("ERROR: ProcessGateway: Could not write Header mmdvmhost packet\n"); return true; } @@ -255,7 +280,8 @@ bool CQnetRelay::ProcessGateway(const int len, const unsigned char *raw) printf("Sent DSRP to %u, streamid=%04x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", MMDVM_IN_PORT, ntohs(dsrp.header.id), dsrp.header.ur, dsrp.header.r2, dsrp.header.r1, dsrp.header.my, dsrp.header.nm); } - } else + } + else printf("DEBUG: ProcessGateway: unusual packet size read len=%d\n", len); return false; } @@ -267,13 +293,17 @@ bool CQnetRelay::ProcessMMDVM(const int len, const unsigned char *raw) if (len < 65) ::memcpy(dsrp.title, raw, len); // transfer raw data to SDSRP struct - if (49==len || 21==len) { + if (49==len || 21==len) + { // grab the stream id if this is a header - if (49 == len) { + if (49 == len) + { if (dsrp.header.id == id) return false; id = dsrp.header.id; - } else { + } + else + { if (dsrp.voice.id != id) return false; } @@ -289,7 +319,8 @@ bool CQnetRelay::ProcessMMDVM(const int len, const unsigned char *raw) dsvt.flagb[2] = ('B'==RPTR_MOD) ? 0x1U : (('C'==RPTR_MOD) ? 0x2U : 0x3U); dsvt.streamid = id; - if (49 == len) { // header + if (49 == len) // header + { dsvt.ctrl = 0x80; //memcpy(dsvt.hdr.flag, dsrp.header.flag, 41); memcpy(dsvt.hdr.flag, dsrp.header.flag, 3); @@ -299,17 +330,21 @@ bool CQnetRelay::ProcessMMDVM(const int len, const unsigned char *raw) memcpy(dsvt.hdr.mycall, dsrp.header.my, 8); memcpy(dsvt.hdr.sfx, dsrp.header.nm, 4); memcpy(dsvt.hdr.pfcs, dsrp.header.pfcs, 2); - if (ToGate.Write(dsvt.title, 56)) { + if (ToGate.Write(dsvt.title, 56)) + { printf("ERROR: ProcessMMDVM: Could not write gateway header packet\n"); return true; } if (log_qso) printf("Sent DSVT streamid=%04x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dsvt.streamid), dsvt.hdr.urcall, dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.mycall, dsvt.hdr.sfx); - } else if (21 == len) { // ambe + } + else if (21 == len) // ambe + { dsvt.ctrl = dsrp.header.seq; memcpy(dsvt.vasd.voice, dsrp.voice.ambe, 12); - if (ToGate.Write(dsvt.title, 27)) { + if (ToGate.Write(dsvt.title, 27)) + { printf("ERROR: ProcessMMDVM: Could not write gateway voice packet\n"); return true; } @@ -317,9 +352,12 @@ bool CQnetRelay::ProcessMMDVM(const int len, const unsigned char *raw) if (log_qso && dsvt.ctrl&0x40) printf("Sent DSVT end of streamid=%04x\n", ntohs(dsvt.streamid)); } - } else if (len < 65 && dsrp.tag == 0xAU) { + } + else if (len < 65 && dsrp.tag == 0xAU) + { // printf("MMDVM Poll: '%s'\n", (char *)mpkt.poll_msg); - } else + } + else printf("DEBUG: ProcessMMDVM: unusual packet len=%d\n", len); return false; } @@ -336,12 +374,15 @@ bool CQnetRelay::ReadConfig(const char *cfgFile) std::string mmdvm_path("module_"); std::string type; - if (0 > assigned_module) { + if (0 > assigned_module) + { // we need to find the lone mmdvmhost module - for (int i=0; i<3; i++) { + for (int i=0; i<3; i++) + { std::string test(mmdvm_path); test.append(1, 'a'+i); - if (cfg.KeyExists(test)) { + if (cfg.KeyExists(test)) + { cfg.GetValue(test, estr, type, 1, 16); if (type.compare("mmdvmhost")) continue; // this ain't it! @@ -350,20 +391,27 @@ bool CQnetRelay::ReadConfig(const char *cfgFile) break; } } - if (0 > assigned_module) { + if (0 > assigned_module) + { fprintf(stderr, "Error: no 'mmdvmhost' module found\n!"); return true; } - } else { + } + else + { // make sure mmdvmhost module is defined mmdvm_path.append(1, 'a' + assigned_module); - if (cfg.KeyExists(mmdvm_path)) { + if (cfg.KeyExists(mmdvm_path)) + { cfg.GetValue(mmdvm_path, estr, type, 1, 16); - if (type.compare("mmdvmhost")) { + if (type.compare("mmdvmhost")) + { fprintf(stderr, "%s = %s is not 'mmdvmhost' type!\n", mmdvm_path.c_str(), type.c_str()); return true; } - } else { + } + else + { fprintf(stderr, "Module '%c' is not defined.\n", 'a'+assigned_module); return true; } @@ -386,12 +434,14 @@ bool CQnetRelay::ReadConfig(const char *cfgFile) int main(int argc, const char **argv) { setbuf(stdout, NULL); - if (2 != argc) { + if (2 != argc) + { fprintf(stderr, "usage: %s path_to_config_file\n", argv[0]); return 1; } - if ('-' == argv[1][0]) { + if ('-' == argv[1][0]) + { printf("\nQnetRelay Version #%s Copyright (C) 2018-2019 by Thomas A. Early N7TAE\n", RELAY_VERSION); printf("QnetRelay comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); @@ -399,28 +449,30 @@ int main(int argc, const char **argv) } const char *qn = strstr(argv[0], "qnrelay"); - if (NULL == qn) { + if (NULL == qn) + { fprintf(stderr, "Error finding 'qnrelay' in %s!\n", argv[0]); return 1; } qn += 7; int module; - switch (*qn) { - case NULL: - module = -1; - break; - case 'a': - module = 0; - break; - case 'b': - module = 1; - break; - case 'c': - module = 2; - break; - default: - fprintf(stderr, "assigned module must be a, b or c\n"); - return 1; + switch (*qn) + { + case NULL: + module = -1; + break; + case 'a': + module = 0; + break; + case 'b': + module = 1; + break; + case 'c': + module = 2; + break; + default: + fprintf(stderr, "assigned module must be a, b or c\n"); + return 1; } CQnetRelay qnmmdvm(module); diff --git a/QnetRemote.cpp b/QnetRemote.cpp index 905309e..db22794 100644 --- a/QnetRemote.cpp +++ b/QnetRemote.cpp @@ -54,7 +54,8 @@ static int PLAY_WAIT, PLAY_DELAY; static unsigned char silence[9] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8 }; -static unsigned short crc_tabccitt[256] = { +static unsigned short crc_tabccitt[256] = +{ 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, @@ -82,15 +83,18 @@ static bool ReadCfgFile() std::string path = "module_"; path.append(1, 'a'+module); - if (! cfg.KeyExists(path)) { + if (! cfg.KeyExists(path)) + { fprintf(stderr, "%s not defined!\n", path.c_str()); return true; } cfg.GetValue(path, estr, type, 1, 16); cfg.GetValue(path+"_callsign", type, REPEATER, 0, 6); - if (REPEATER.length() < 4) { - if (cfg.GetValue("ircddb_login", estr, REPEATER, 3, 6)) { + if (REPEATER.length() < 4) + { + if (cfg.GetValue("ircddb_login", estr, REPEATER, 3, 6)) + { fprintf(stderr, "no Callsign for the repeater was found!\n"); return true; } @@ -108,7 +112,8 @@ static void calcPFCS(unsigned char rawbytes[56]) unsigned short tmp, short_c; short int i; - for (i = 15; i < 54 ; i++) { + for (i = 15; i < 54 ; i++) + { short_c = 0x00ff & (unsigned short)rawbytes[i]; tmp = (crc_dstar_ffff & 0x00ff) ^ short_c; crc_dstar_ffff = (crc_dstar_ffff >> 8) ^ crc_tabccitt[tmp]; @@ -130,7 +135,8 @@ static void ToUpper(std::string &str) int main(int argc, char *argv[]) { - if (argc != 4) { + if (argc != 4) + { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, "Example: %s c n7tae xrf757al\n", argv[0]); fprintf(stderr, "Where...\n"); @@ -140,25 +146,26 @@ int main(int argc, char *argv[]) return 0; } - switch (argv[1][0]) { - case '0': - case 'a': - case 'A': - module = 0; - break; - case '1': - case 'b': - case 'B': - module = 1; - break; - case '2': - case 'c': - case 'C': - module = 2; - break; - default: - fprintf(stderr, "module must be 0, a, A, 1, b, B, 2, c or C, not %s\n", argv[1]); - return 1; + switch (argv[1][0]) + { + case '0': + case 'a': + case 'A': + module = 0; + break; + case '1': + case 'b': + case 'B': + module = 1; + break; + case '2': + case 'c': + case 'C': + module = 2; + break; + default: + fprintf(stderr, "module must be 0, a, A, 1, b, B, 2, c or C, not %s\n", argv[1]); + return 1; } std::string cfgfile(CFG_DIR); @@ -169,13 +176,15 @@ int main(int argc, char *argv[]) if (ReadCfgFile()) return 1; - if (REPEATER.size() > 6) { + if (REPEATER.size() > 6) + { printf("repeaterCallsign can not be more than 6 characters, %s is invalid\n", REPEATER.c_str()); return 1; } ToUpper(REPEATER); - if (strlen(argv[2]) > 8) { + if (strlen(argv[2]) > 8) + { printf("MYCALL can not be more than 8 characters, %s is invalid\n", argv[2]); return 1; } @@ -183,7 +192,8 @@ int main(int argc, char *argv[]) ToUpper(mycall); - if (strlen(argv[3]) > 8) { + if (strlen(argv[3]) > 8) + { printf("YOURCALL can not be more than 8 characters, %s is invalid\n", argv[3]); return 1; } @@ -191,7 +201,8 @@ int main(int argc, char *argv[]) ToUpper(yourcall); // replace underscores with spaces auto pos = yourcall.find_first_of('_'); - while (yourcall.npos != pos) { + while (yourcall.npos != pos) + { yourcall[pos] = ' '; pos = yourcall.find_first_of('_'); } @@ -244,7 +255,8 @@ int main(int argc, char *argv[]) calcPFCS(pkt.title); // send the header - if (56 != ToGateway.Write(pkt.title, 56)) { + if (56 != ToGateway.Write(pkt.title, 56)) + { printf("%s: ERROR: Couldn't send header!\n", argv[0]); return 1; } @@ -253,65 +265,68 @@ int main(int argc, char *argv[]) pkt.config = 0x20U; memcpy(pkt.vasd.voice, silence, 9); - for (int i=0; i<10; i++) { + for (int i=0; i<10; i++) + { /* start sending silence + text */ pkt.ctrl = i; - switch (i) { - case 0: // sync voice frame - pkt.vasd.text[0] = 0x55; - pkt.vasd.text[1] = 0x2d; - pkt.vasd.text[2] = 0x16; - break; - case 1: - pkt.vasd.text[0] = '@' ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[0] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[1] ^ 0x93; - break; - case 2: - pkt.vasd.text[0] = RADIO_ID[2] ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[3] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[4] ^ 0x93; - break; - case 3: - pkt.vasd.text[0] = 'A' ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[5] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[6] ^ 0x93; - break; - case 4: - pkt.vasd.text[0] = RADIO_ID[7] ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[8] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[9] ^ 0x93; - break; - case 5: - pkt.vasd.text[0] = 'B' ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[10] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[11] ^ 0x93; - break; - case 6: - pkt.vasd.text[0] = RADIO_ID[12] ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[13] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[14] ^ 0x93; - break; - case 7: - pkt.vasd.text[0] = 'C' ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[15] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[16] ^ 0x93; - break; - case 8: - pkt.vasd.text[0] = RADIO_ID[17] ^ 0x70; - pkt.vasd.text[1] = RADIO_ID[18] ^ 0x4f; - pkt.vasd.text[2] = RADIO_ID[19] ^ 0x93; - break; - case 9: // terminal voice packet - pkt.ctrl |= 0x40; - pkt.vasd.text[0] = 0x70; - pkt.vasd.text[1] = 0x4f; - pkt.vasd.text[2] = 0x93; - break; + switch (i) + { + case 0: // sync voice frame + pkt.vasd.text[0] = 0x55; + pkt.vasd.text[1] = 0x2d; + pkt.vasd.text[2] = 0x16; + break; + case 1: + pkt.vasd.text[0] = '@' ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[0] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[1] ^ 0x93; + break; + case 2: + pkt.vasd.text[0] = RADIO_ID[2] ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[3] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[4] ^ 0x93; + break; + case 3: + pkt.vasd.text[0] = 'A' ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[5] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[6] ^ 0x93; + break; + case 4: + pkt.vasd.text[0] = RADIO_ID[7] ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[8] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[9] ^ 0x93; + break; + case 5: + pkt.vasd.text[0] = 'B' ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[10] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[11] ^ 0x93; + break; + case 6: + pkt.vasd.text[0] = RADIO_ID[12] ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[13] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[14] ^ 0x93; + break; + case 7: + pkt.vasd.text[0] = 'C' ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[15] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[16] ^ 0x93; + break; + case 8: + pkt.vasd.text[0] = RADIO_ID[17] ^ 0x70; + pkt.vasd.text[1] = RADIO_ID[18] ^ 0x4f; + pkt.vasd.text[2] = RADIO_ID[19] ^ 0x93; + break; + case 9: // terminal voice packet + pkt.ctrl |= 0x40; + pkt.vasd.text[0] = 0x70; + pkt.vasd.text[1] = 0x4f; + pkt.vasd.text[2] = 0x93; + break; } - if (27 != ToGateway.Write(pkt.title, 27)) { + if (27 != ToGateway.Write(pkt.title, 27)) + { printf("%s: ERROR: could not send voice packet %d\n", argv[0], i); return 1; } diff --git a/QnetTypeDefs.h b/QnetTypeDefs.h index 7bde96e..633a871 100644 --- a/QnetTypeDefs.h +++ b/QnetTypeDefs.h @@ -19,25 +19,31 @@ // for communicating with the g2 gateway on the internal port #pragma pack(push, 1) // used internally by Icom stacks -using SDSTR = struct dstr_tag { +using SDSTR = struct dstr_tag +{ unsigned char pkt_id[4]; // 0 "DSTR" unsigned short counter; // 4 unsigned char flag[3]; // 6 { 0x73, 0x12, 0x00 } unsigned char remaining; // 9 the number of bytes left in the packet - union { - struct { + union + { + struct + { unsigned char mycall[8]; // 10 unsigned char rpt[8]; // 18 } spkt; // total 26 - struct { + struct + { unsigned char icm_id; // 10 unsigned char dst_rptr_id; // 11 unsigned char snd_rptr_id; // 12 unsigned char snd_term_id; // 13 unsigned short streamid; // 14 unsigned char ctrl; // 16 sequence number hdr=0, voice%21, end|=0x40 - union { - struct { + union + { + struct + { unsigned char flag[3]; // 17 unsigned char r2[8]; // 20 unsigned char r1[8]; // 28 @@ -46,12 +52,15 @@ using SDSTR = struct dstr_tag { unsigned char nm[4]; // 52 unsigned char pfcs[2]; // 56 } hdr; // total 58 - union { - struct { + union + { + struct + { unsigned char voice[9]; // 17 unsigned char text[3]; // 26 } vasd; // total 29 - struct { + struct + { unsigned char UNKNOWN[3]; // 17 not sure what this is, but g2_ doesn't seem to need it unsigned char voice[9]; // 20 unsigned char text[3]; // 29 @@ -65,7 +74,8 @@ using SDSTR = struct dstr_tag { // for the g2 external port and between QnetGateway programs #pragma pack(push, 1) -using SDSVT = struct dsvt_tag { +using SDSVT = struct dsvt_tag +{ unsigned char title[4]; // 0 "DSVT" unsigned char config; // 4 0x10 is hdr 0x20 is vasd unsigned char flaga[3]; // 5 zeros @@ -73,8 +83,10 @@ using SDSVT = struct dsvt_tag { unsigned char flagb[3]; // 9 0x0 0x1 (A:0x3 B:0x1 C:0x2) unsigned short streamid;// 12 unsigned char ctrl; // 14 hdr: 0x80 vsad: framecounter (mod 21) - union { - struct { // index + union + { + struct // index + { unsigned char flag[3]; // 15 unsigned char rpt1[8]; // 18 unsigned char rpt2[8]; // 26 @@ -83,11 +95,13 @@ using SDSVT = struct dsvt_tag { unsigned char sfx[4]; // 50 unsigned char pfcs[2]; // 54 } hdr; // total 56 - struct { + struct + { unsigned char voice[9]; // 15 unsigned char text[3]; // 24 } vasd; // voice and slow data total 27 - struct { + struct + { unsigned char voice[9]; // 15 unsigned char end[6]; // 24 } vend; // voice and end seq total 32 (for DPlus) @@ -97,19 +111,22 @@ using SDSVT = struct dsvt_tag { // for mmdvm #pragma pack(push, 1) -using SDSRP = struct dsrp_tag { // offset size +using SDSRP = struct dsrp_tag // offset size +{ unsigned char title[4]; // "DSRP" 0 unsigned char tag; // Poll : 0xA 4 - // Header : busy ? 0x22 : 0x20 - // Voice : busy ? 0x23 : 0x21 - union { + // Header : busy ? 0x22 : 0x20 + // Voice : busy ? 0x23 : 0x21 + union + { unsigned char poll_msg[59]; // space for text 5 variable, max is 64, including trailing null - struct { + struct + { unsigned short id; // random id number 5 unsigned char seq; // 0x0 7 unsigned char flag[3]; // 0x80 Dstar Data 8 - // 0x40 Dstar Repeater - // 0x01 Dstar Relay Unavailable + // 0x40 Dstar Repeater + // 0x01 Dstar Relay Unavailable unsigned char r2[8]; // Repeater 2 11 unsigned char r1[8]; // Repeater 1 19 unsigned char ur[8]; // Your Call 27 @@ -117,10 +134,11 @@ using SDSRP = struct dsrp_tag { // offset size unsigned char nm[4]; // Name 43 unsigned char pfcs[2]; // checksum 47 49 } header; - struct { + struct + { unsigned short id; // random id number 5 unsigned char seq; // sequence from 0 to 0x14 7 - // if end then sequence |= 0x40 + // if end then sequence |= 0x40 unsigned char err; // # of errors? 8 unsigned char ambe[12]; // voice + slow data 9 21 } voice; @@ -129,8 +147,9 @@ using SDSRP = struct dsrp_tag { // offset size #pragma pack(pop) #pragma pack(push, 1) -using SLINKFAMILY = struct link_family_tag { - char title[4]; - int family[3]; +using SLINKFAMILY = struct link_family_tag +{ + char title[4]; + int family[3]; }; #pragma pack(pop) diff --git a/QnetVoice.cpp b/QnetVoice.cpp index e78c3a8..7a4e01b 100644 --- a/QnetVoice.cpp +++ b/QnetVoice.cpp @@ -37,13 +37,16 @@ bool read_config(const char *cfgFile) if (cfg.Initialize(cfgFile)) return true; - for (int m=0; m<3; m++) { + for (int m=0; m<3; m++) + { std::string path("module_"); path.append(std::to_string(m)); std::string type; - if (cfg.KeyExists(path)) { + if (cfg.KeyExists(path)) + { cfg.GetValue(path, "", type, 1, 16); - if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm") && strcasecmp(type.c_str(), "itap")) { + if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm") && strcasecmp(type.c_str(), "itap")) + { printf("module type '%s' is invalid\n", type.c_str()); return true; } @@ -69,7 +72,8 @@ int main(int argc, char *argv[]) { char RADIO_ID[21]; - if (argc != 4) { + if (argc != 4) + { printf("Usage: %s \n", argv[0]); printf("Where...\n"); printf(" is one of your modules: A, B or C\n"); @@ -87,7 +91,8 @@ int main(int argc, char *argv[]) if (islower(module)) module = toupper(module); - if ((module != 'A') && (module != 'B') && (module != 'C')) { + if ((module != 'A') && (module != 'B') && (module != 'C')) + { printf("module must be one of A B C\n"); return 1; } @@ -96,7 +101,8 @@ int main(int argc, char *argv[]) snprintf(pathname, FILENAME_MAX, "%s/%s", announce_dir.c_str(), argv[2]); FILE *fp = fopen(pathname, "rb"); - if (!fp) { + if (!fp) + { printf("Failed to find file %s for reading\n", pathname); return 1; } @@ -112,10 +118,13 @@ int main(int argc, char *argv[]) RADIO_ID[i] = '_'; fp = fopen(qnvoice_file.c_str(), "w"); - if (fp) { + if (fp) + { fprintf(fp, "%c_%s_%s\n", module, argv[2], RADIO_ID); fclose(fp); - } else { + } + else + { printf("Failed to open %s for writing", qnvoice_file.c_str()); return 1; } diff --git a/SEcho.h b/SEcho.h index 03ac3b3..3ccd915 100644 --- a/SEcho.h +++ b/SEcho.h @@ -17,12 +17,13 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -using SECHO = struct echo_tag { +using SECHO = struct echo_tag +{ bool is_linked; time_t last_time; unsigned short streamid; int fd; char message[24]; - SDSVT header; // only used in qnlink (qngateway writes the header to the file) + SDSVT header; // only used in qnlink (qngateway writes the header to the file) char file[FILENAME_MAX + 1]; }; diff --git a/SockAddress.h b/SockAddress.h index 8f4ceeb..1d9bad4 100644 --- a/SockAddress.h +++ b/SockAddress.h @@ -44,33 +44,41 @@ public: { Clear(); addr.ss_family = family; - if (AF_INET == family) { + if (AF_INET == family) + { auto addr4 = (struct sockaddr_in *)&addr; addr4->sin_port = htons(port); - if (address) { + if (address) + { if (0 == strncasecmp(address, "loc", 3)) inet_pton(AF_INET, "127.0.0.1", &(addr4->sin_addr)); else if (0 == strncasecmp(address, "any", 3)) inet_pton(AF_INET, "0.0.0.0", &(addr4->sin_addr)); - else if (address) { + else if (address) + { if (1 > inet_pton(AF_INET, address, &(addr4->sin_addr))) std::cerr << "Address Initialization Error: '" << address << "' is not a valdid IPV4 address!" << std::endl; } } - } else if (AF_INET6 == family) { + } + else if (AF_INET6 == family) + { auto addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_port = htons(port); - if (address) { + if (address) + { if (0 == strncasecmp(address, "loc", 3)) inet_pton(AF_INET6, "::1", &(addr6->sin6_addr)); else if (0 == strncasecmp(address, "any", 3)) inet_pton(AF_INET6, "::", &(addr6->sin6_addr)); - else if (address) { + else if (address) + { if (1 > inet_pton(AF_INET6, address, &(addr6->sin6_addr))) std::cerr << "Address Initialization Error: '" << address << "' is not a valid IPV6 address!" << std::endl; } } - } else + } + else std::cerr << "Error: Wrong address family type:" << family << " for [" << (address ? address : "NULL") << "]:" << port << std::endl; } @@ -89,11 +97,14 @@ public: { if (addr.ss_family != rhs.addr.ss_family) return false; - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto l = (struct sockaddr_in *)&addr; auto r = (struct sockaddr_in *)&rhs.addr; return (l->sin_addr.s_addr == r->sin_addr.s_addr); - } else if (AF_INET6 == addr.ss_family) { + } + else if (AF_INET6 == addr.ss_family) + { auto l = (struct sockaddr_in6 *)&addr; auto r = (struct sockaddr_in6 *)&rhs.addr; return (0 == memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))); @@ -105,11 +116,14 @@ public: { if (addr.ss_family != rhs.addr.ss_family) return true; - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto l = (struct sockaddr_in *)&addr; auto r = (struct sockaddr_in *)&rhs.addr; return (l->sin_addr.s_addr != r->sin_addr.s_addr); - } else if (AF_INET6 == addr.ss_family) { + } + else if (AF_INET6 == addr.ss_family) + { auto l = (struct sockaddr_in6 *)&addr; auto r = (struct sockaddr_in6 *)&rhs.addr; return (0 != memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))); @@ -119,12 +133,16 @@ public: bool AddressIsZero() const { - if (AF_INET == addr.ss_family) { - auto addr4 = (struct sockaddr_in *)&addr; + if (AF_INET == addr.ss_family) + { + auto addr4 = (struct sockaddr_in *)&addr; return (addr4->sin_addr.s_addr == 0U); - } else { + } + else + { auto addr6 = (struct sockaddr_in6 *)&addr; - for (unsigned int i=0; i<16; i++) { + for (unsigned int i=0; i<16; i++) + { if (addr6->sin6_addr.s6_addr[i]) return false; } @@ -134,11 +152,14 @@ public: void ClearAddress() { - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto addr4 = (struct sockaddr_in *)&addr; addr4->sin_addr.s_addr = 0U; strcpy(straddr, "0.0.0.0"); - } else { + } + else + { auto addr6 = (struct sockaddr_in6 *)&addr; memset(&(addr6->sin6_addr.s6_addr), 0, 16); strcpy(straddr, "::"); @@ -149,41 +170,53 @@ public: { if (straddr[0]) return straddr; - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto addr4 = (struct sockaddr_in *)&addr; inet_ntop(AF_INET, &(addr4->sin_addr), straddr, INET6_ADDRSTRLEN); - } else if (AF_INET6 == addr.ss_family) { + } + else if (AF_INET6 == addr.ss_family) + { auto addr6 = (struct sockaddr_in6 *)&addr; inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, INET6_ADDRSTRLEN); - } else { + } + else + { std::cerr << "Unknown socket family: " << addr.ss_family << std::endl; } return straddr; } - int GetFamily() const - { - return addr.ss_family; - } + int GetFamily() const + { + return addr.ss_family; + } unsigned short GetPort() const { - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto addr4 = (struct sockaddr_in *)&addr; return ntohs(addr4->sin_port); - } else if (AF_INET6 == addr.ss_family) { + } + else if (AF_INET6 == addr.ss_family) + { auto addr6 = (struct sockaddr_in6 *)&addr; return ntohs(addr6->sin6_port); - } else + } + else return 0; } void SetPort(const uint16_t newport) { - if (AF_INET == addr.ss_family) { + if (AF_INET == addr.ss_family) + { auto addr4 = (struct sockaddr_in *)&addr; addr4->sin_port = htons(newport); - } else if (AF_INET6 == addr.ss_family) { + } + else if (AF_INET6 == addr.ss_family) + { auto addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_port = htons(newport); } diff --git a/TCPReaderWriterClient.cpp b/TCPReaderWriterClient.cpp index 7a6eaca..32fae7b 100644 --- a/TCPReaderWriterClient.cpp +++ b/TCPReaderWriterClient.cpp @@ -25,10 +25,10 @@ CTCPReaderWriterClient::CTCPReaderWriterClient(const std::string &address, int family, const std::string &port) : -m_address(address), -m_family(family), -m_port(port), -m_fd(-1) + m_address(address), + m_family(family), + m_port(port), + m_fd(-1) { } @@ -51,17 +51,20 @@ bool CTCPReaderWriterClient::Open(const std::string &address, int family, const bool CTCPReaderWriterClient::Open() { - if (m_fd != -1) { + if (m_fd != -1) + { fprintf(stderr, "ERROR: port for '%s' is already open!\n", m_address.c_str()); return true; } - if (0 == m_address.size() || 0 == m_port.size() || 0 == std::stoul(m_port)) { + if (0 == m_address.size() || 0 == m_port.size() || 0 == std::stoul(m_port)) + { fprintf(stderr, "ERROR: '[%s]:%s' is malformed!\n", m_address.c_str(), m_port.c_str()); return true; } - if (AF_INET!=m_family && AF_INET6!=m_family && AF_UNSPEC!=m_family) { + if (AF_INET!=m_family && AF_INET6!=m_family && AF_UNSPEC!=m_family) + { fprintf(stderr, "ERROR: family must be AF_INET, AF_INET6 or AF_UNSPEC\n"); return true; } @@ -76,37 +79,47 @@ bool CTCPReaderWriterClient::Open() struct addrinfo *res; int s = EAI_AGAIN; int count = 0; - while (EAI_AGAIN==s and count++<20) { + while (EAI_AGAIN==s and count++<20) + { // connecting to a server, so we can wait until it's ready s = getaddrinfo(m_address.c_str(), m_port.c_str(), &hints, &res); - if (s && s != EAI_AGAIN) { + if (s && s != EAI_AGAIN) + { fprintf(stderr, "ERROR: getaddrinfo of %s: %s\n", m_address.c_str(), gai_strerror(s)); return true; } std::this_thread::sleep_for(std::chrono::seconds(3)); } - if (EAI_AGAIN == s) { - fprintf(stderr, "ERROR getaddrinfo of %s failed 20 times\n", m_address.c_str()); - return true; - } + if (EAI_AGAIN == s) + { + fprintf(stderr, "ERROR getaddrinfo of %s failed 20 times\n", m_address.c_str()); + return true; + } struct addrinfo *rp; - for (rp = res; rp != NULL; rp = rp->ai_next) { + for (rp = res; rp != NULL; rp = rp->ai_next) + { m_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (m_fd == -1) continue; - if (connect(m_fd, rp->ai_addr, rp->ai_addrlen)) { + if (connect(m_fd, rp->ai_addr, rp->ai_addrlen)) + { Close(); continue; - } else { + } + else + { char buf[INET6_ADDRSTRLEN]; void *addr; - if (AF_INET == rp->ai_family) { + if (AF_INET == rp->ai_family) + { struct sockaddr_in *addr4 = (struct sockaddr_in *)rp->ai_addr; addr = &(addr4->sin_addr); - } else { + } + else + { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)rp->ai_addr; addr = &(addr6->sin6_addr); } @@ -117,7 +130,8 @@ bool CTCPReaderWriterClient::Open() } freeaddrinfo(res); - if (rp == NULL) { + if (rp == NULL) + { fprintf(stderr, "Could not connect to any system returned by %s\n", m_address.c_str()); m_fd = -1; return true; @@ -130,13 +144,15 @@ int CTCPReaderWriterClient::ReadExact(unsigned char *buf, const unsigned int len { unsigned int offset = 0U; - do { + do + { int n = Read(buf + offset, length - offset); if (n < 0) return n; offset += n; - } while ((length - offset) > 0U); + } + while ((length - offset) > 0U); return length; } @@ -148,7 +164,8 @@ int CTCPReaderWriterClient::Read(unsigned char* buffer, const unsigned int lengt assert(m_fd != -1); ssize_t len = recv(m_fd, buffer, length, 0); - if (len <= 0) { + if (len <= 0) + { if (len < 0) fprintf(stderr, "Error returned from recv, err=%d\n", errno); return -1; @@ -167,11 +184,13 @@ int CTCPReaderWriterClient::ReadLine(std::string& line) do { resultCode = Read(&c, 1); - if(resultCode == 1) { + if(resultCode == 1) + { line += c; len++; } - } while(c != '\n' && resultCode == 1); + } + while(c != '\n' && resultCode == 1); return resultCode <= 0 ? resultCode : len; } @@ -183,7 +202,8 @@ bool CTCPReaderWriterClient::Write(const unsigned char *buffer, const unsigned i assert(m_fd != -1); ssize_t ret = send(m_fd, (char *)buffer, length, 0); - if (ret != ssize_t(length)) { + if (ret != ssize_t(length)) + { if (ret < 0) fprintf(stderr, "Error returned from send, err=%s\n", strerror(errno)); else @@ -202,9 +222,10 @@ bool CTCPReaderWriterClient::WriteLine(const std::string& line) size_t len = lineCopy.size(); bool result = true; - for(size_t i = 0; i < len && result; i++){ + for(size_t i = 0; i < len && result; i++) + { unsigned char c = lineCopy.at(i); - result = Write(&c , 1); + result = Write(&c, 1); } return result; @@ -212,7 +233,8 @@ bool CTCPReaderWriterClient::WriteLine(const std::string& line) void CTCPReaderWriterClient::Close() { - if (m_fd != -1) { + if (m_fd != -1) + { close(m_fd); m_fd = -1; } diff --git a/TCPReaderWriterClient.h b/TCPReaderWriterClient.h index 0aed516..691153f 100644 --- a/TCPReaderWriterClient.h +++ b/TCPReaderWriterClient.h @@ -33,7 +33,8 @@ #include #include -class CTCPReaderWriterClient { +class CTCPReaderWriterClient +{ public: CTCPReaderWriterClient(const std::string &address, int family, const std::string &port); CTCPReaderWriterClient(); diff --git a/Timer.h b/Timer.h index f3a6e3a..3219da4 100644 --- a/Timer.h +++ b/Timer.h @@ -26,10 +26,12 @@ class CTimer public: CTimer() { start(); } ~CTimer() {} - void start() { + void start() + { starttime = std::chrono::steady_clock::now(); } - double time() { + double time() + { std::chrono::duration elapsed(std::chrono::steady_clock::now() - starttime); return elapsed.count(); } diff --git a/UDPSocket.cpp b/UDPSocket.cpp index 4ced971..515fffe 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -49,12 +49,14 @@ bool CUDPSocket::Open(const CSockAddress &addr) { // create socket m_fd = socket(addr.GetFamily(), SOCK_DGRAM, 0); - if (0 > m_fd) { + if (0 > m_fd) + { std::cerr << "Cannot create socket on " << addr << ", " << strerror(errno) << std::endl; return true; } - if (0 > fcntl(m_fd, F_SETFL, O_NONBLOCK)) { + if (0 > fcntl(m_fd, F_SETFL, O_NONBLOCK)) + { std::cerr << "cannot set socket " << addr << " to non-blocking: " << strerror(errno) << std::endl; close(m_fd); m_fd = -1; @@ -62,7 +64,8 @@ bool CUDPSocket::Open(const CSockAddress &addr) } const int reuse = 1; - if (0 > setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))) { + if (0 > setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))) + { std::cerr << "Cannot set the UDP socket option on " << m_addr << ", err: " << strerror(errno) << std::endl; close(m_fd); m_fd = -1; @@ -72,7 +75,8 @@ bool CUDPSocket::Open(const CSockAddress &addr) // initialize sockaddr struct m_addr = addr; - if (0 != bind(m_fd, m_addr.GetCPointer(), m_addr.GetSize())) { + if (0 != bind(m_fd, m_addr.GetCPointer(), m_addr.GetSize())) + { std::cerr << "bind failed on " << m_addr << ", " << strerror(errno) << std::endl; close(m_fd); m_fd = -1; @@ -84,7 +88,8 @@ bool CUDPSocket::Open(const CSockAddress &addr) void CUDPSocket::Close(void) { - if ( m_fd >= 0 ) { + if ( m_fd >= 0 ) + { close(m_fd); m_fd = -1; } diff --git a/UnixDgramSocket.cpp b/UnixDgramSocket.cpp index e494619..f07d2fb 100644 --- a/UnixDgramSocket.cpp +++ b/UnixDgramSocket.cpp @@ -39,7 +39,8 @@ CUnixDgramReader::~CUnixDgramReader() bool CUnixDgramReader::Open(const char *path) // returns true on failure { fd = socket(AF_UNIX, SOCK_DGRAM, 0); - if (fd < 0) { + if (fd < 0) + { fprintf(stderr, "CUnixDgramReader::Open: socket() failed: %s\n", strerror(errno)); return true; } @@ -51,7 +52,8 @@ bool CUnixDgramReader::Open(const char *path) // returns true on failure strncpy(addr.sun_path+1, path, sizeof(addr.sun_path)-2); int rval = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); - if (rval < 0) { + if (rval < 0) + { fprintf(stderr, "CUnixDgramReader::Open: bind() failed: %s\n", strerror(errno)); close(fd); fd = -1; @@ -98,13 +100,15 @@ ssize_t CUnixDgramWriter::Write(const void *buf, size_t size) { // open the socket int fd = socket(AF_UNIX, SOCK_DGRAM, 0); - if (fd < 0) { + if (fd < 0) + { fprintf(stderr, "Failed to open socket %s : %s\n", addr.sun_path+1, strerror(errno)); return -1; } // connect to the receiver int rval = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); - if (rval < 0) { + if (rval < 0) + { fprintf(stderr, "Failed to connect to socket %s : %s\n", addr.sun_path+1, strerror(errno)); close(fd); return -1; @@ -112,7 +116,8 @@ ssize_t CUnixDgramWriter::Write(const void *buf, size_t size) ssize_t written = 0; int count = 0; - while (written <= 0) { + while (written <= 0) + { written = write(fd, buf, size); if (written == (ssize_t)size) break; @@ -120,11 +125,13 @@ ssize_t CUnixDgramWriter::Write(const void *buf, size_t size) fprintf(stderr, "ERROR: faied to write to %s : %s\n", addr.sun_path+1, strerror(errno)); else if (written == 0) fprintf(stderr, "Warning: zero bytes written to %s\n", addr.sun_path+1); - else if (written != (ssize_t)size) { + else if (written != (ssize_t)size) + { fprintf(stderr, "ERROR: only %d of %d bytes written to %s\n", (int)written, (int)size, addr.sun_path+1); break; } - if (++count >= 100) { + if (++count >= 100) + { fprintf(stderr, "ERROR: Write failed after %d attempts\n", count-1); break; } diff --git a/UnixPacketSock.cpp b/UnixPacketSock.cpp index 578724b..3096a93 100644 --- a/UnixPacketSock.cpp +++ b/UnixPacketSock.cpp @@ -33,10 +33,14 @@ ssize_t CUnixPacket::Read(void *buffer, const ssize_t size) if (0 > m_fd) return -1; ssize_t len = read(m_fd, buffer, size); - if (len < 1) { - if (-1 == len) { + if (len < 1) + { + if (-1 == len) + { std::cerr << "Read error on '" << m_name << "': " << strerror(errno) << std::endl; - } else if (0 == len) { + } + else if (0 == len) + { std::cerr << "Read error on '" << m_name << "': EOF" << std::endl; } if (Restart()) @@ -52,10 +56,14 @@ bool CUnixPacket::Write(const void *buffer, const ssize_t size) if (0 > m_fd) return true; ssize_t written = write(m_fd, buffer, size); - if (written != size) { - if (-1 == written) { + if (written != size) + { + if (-1 == written) + { std::cerr << "Write error on '" << m_name << "': " << strerror(errno) << std::endl; - } else { + } + else + { std::cout << "Write error on '" << m_name << "': Only wrote " << written << " of " << size << " bytes" << std::endl; } return Restart(); @@ -86,7 +94,8 @@ CUnixPacketServer::~CUnixPacketServer() bool CUnixPacketServer::Open(const char *name, CKRBase *host) { m_server = socket(AF_UNIX, SOCK_SEQPACKET, 0); - if (m_server < 0) { + if (m_server < 0) + { std::cerr << "Cannot open '" << name << "' socket: " << strerror(errno) << std::endl; return true; } @@ -95,20 +104,23 @@ bool CUnixPacketServer::Open(const char *name, CKRBase *host) memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; memcpy(addr.sun_path+1, name, strlen(name)); - if (-1 == bind(m_server, (struct sockaddr *)&addr, sizeof(addr))) { + if (-1 == bind(m_server, (struct sockaddr *)&addr, sizeof(addr))) + { std::cerr << "Cannot bind '" << name << "' socket: " << strerror(errno) << std::endl; Close(); return true; } - if (-1 == listen(m_server, 1)) { + if (-1 == listen(m_server, 1)) + { std::cerr << "Cannot listen on '" << name << "' socket: " << strerror(errno) << std::endl; Close(); return true; } m_fd = accept(m_server, nullptr, 0); - if (m_fd < 0) { + if (m_fd < 0) + { std::cerr << "Cannot accept on '" << name << "' socket: " << strerror(errno) << std::endl; Close(); return true; @@ -121,11 +133,13 @@ bool CUnixPacketServer::Open(const char *name, CKRBase *host) void CUnixPacketServer::Close() { - if (m_server >= 0) { + if (m_server >= 0) + { close(m_server); m_server = -1; } - if (m_fd >= 0) { + if (m_fd >= 0) + { close(m_fd); m_fd = -1; } @@ -139,7 +153,8 @@ CUnixPacketClient::~CUnixPacketClient() bool CUnixPacketClient::Open(const char *name, CKRBase *host) { m_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); - if (m_fd < 0) { + if (m_fd < 0) + { std::cerr << "Cannot open unix client socket " << name << std::endl; return true; } @@ -150,20 +165,26 @@ bool CUnixPacketClient::Open(const char *name, CKRBase *host) memcpy(addr.sun_path+1, name, strlen(name)); int rval = -1; int tries = 0; - while (rval < 0) { + while (rval < 0) + { rval = connect(m_fd, (struct sockaddr *)&addr, sizeof(addr)); - if (rval < 0) { - if (ECONNREFUSED == errno) { + if (rval < 0) + { + if (ECONNREFUSED == errno) + { if (0 == tries++ % 20) std::cout << "Waiting for " << name << " server to start..." << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(250)); - } else { + } + else + { std::cerr << "Cannot connect '" << name << "' socket: " << strerror(errno) << std::endl; Close(); return true; } } - if (! m_host->IsRunning()) { + if (! m_host->IsRunning()) + { Close(); return true; } @@ -176,7 +197,8 @@ bool CUnixPacketClient::Open(const char *name, CKRBase *host) void CUnixPacketClient::Close() { - if (m_fd >= 0) { + if (m_fd >= 0) + { close(m_fd); m_fd = -1; } diff --git a/UnixPacketSock.h b/UnixPacketSock.h index 6612a71..6d7dacd 100644 --- a/UnixPacketSock.h +++ b/UnixPacketSock.h @@ -22,7 +22,8 @@ #include "KRBase.h" -class CUnixPacket { +class CUnixPacket +{ public: CUnixPacket(); virtual bool Open(const char *name, CKRBase *host) = 0; @@ -37,7 +38,8 @@ protected: char m_name[108]; }; -class CUnixPacketServer : public CUnixPacket { +class CUnixPacketServer : public CUnixPacket +{ public: CUnixPacketServer(); ~CUnixPacketServer(); @@ -47,7 +49,8 @@ protected: int m_server; }; -class CUnixPacketClient : public CUnixPacket { +class CUnixPacketClient : public CUnixPacket +{ public: ~CUnixPacketClient(); bool Open(const char *name, CKRBase *host); diff --git a/Utilities.h b/Utilities.h index 0420615..f61b109 100644 --- a/Utilities.h +++ b/Utilities.h @@ -3,39 +3,47 @@ #include // trim from start (in place) -static inline void ltrim(std::string &s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { - return !std::isspace(ch); - })); +static inline void ltrim(std::string &s) +{ + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) + { + return !std::isspace(ch); + })); } // trim from end (in place) -static inline void rtrim(std::string &s) { - s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { - return !std::isspace(ch); - }).base(), s.end()); +static inline void rtrim(std::string &s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) + { + return !std::isspace(ch); + }).base(), s.end()); } // trim from both ends (in place) -static inline void trim(std::string &s) { - ltrim(s); - rtrim(s); +static inline void trim(std::string &s) +{ + ltrim(s); + rtrim(s); } // trim from start (copying) -static inline std::string ltrim_copy(std::string s) { - ltrim(s); - return s; +static inline std::string ltrim_copy(std::string s) +{ + ltrim(s); + return s; } // trim from end (copying) -static inline std::string rtrim_copy(std::string s) { - rtrim(s); - return s; +static inline std::string rtrim_copy(std::string s) +{ + rtrim(s); + return s; } // trim from both ends (copying) -static inline std::string trim_copy(std::string s) { - trim(s); - return s; +static inline std::string trim_copy(std::string s) +{ + trim(s); + return s; } diff --git a/aprs.cpp b/aprs.cpp index 987046f..43262eb 100644 --- a/aprs.cpp +++ b/aprs.cpp @@ -16,23 +16,24 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - #include - #include - #include - #include - #include - #include - #include +#include +#include +#include +#include +#include +#include +#include - #include - #include +#include +#include - #include "aprs.h" +#include "aprs.h" // This is called when header comes in from repeater void CAPRS::SelectBand(short int rptr_idx, unsigned short streamID) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("ERROR in aprs_select_band, invalid mod %d\n", rptr_idx); return; } @@ -58,14 +59,17 @@ void CAPRS::ProcessText(unsigned short streamID, unsigned char seq, unsigned cha short int rptr_idx = -1; - for (short int i = 0; i < 3; i++) { - if (streamID == aprs_streamID[i].streamID) { + for (short int i = 0; i < 3; i++) + { + if (streamID == aprs_streamID[i].streamID) + { rptr_idx = i; break; } } - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("ERROR in aprs_process_text: rptr_idx %d is invalid\n", rptr_idx); return; } @@ -73,7 +77,8 @@ void CAPRS::ProcessText(unsigned short streamID, unsigned char seq, unsigned cha if ((seq & 0x40) == 0x40) return; - if ((seq & 0x1f) == 0x00) { + if ((seq & 0x1f) == 0x00) + { SyncIt(rptr_idx); return; } @@ -93,7 +98,8 @@ void CAPRS::ProcessText(unsigned short streamID, unsigned char seq, unsigned cha return; char *p = strchr((char*)aprs_data, ':'); - if (!p) { + if (!p) + { Reset(rptr_idx); return; } @@ -110,22 +116,25 @@ void CAPRS::ProcessText(unsigned short streamID, unsigned char seq, unsigned cha sprintf(aprs_buf, "%s,qAR,%s:%s\r\n", hdr, m_rptr->mod[rptr_idx].call.c_str(), aud); // printf("GPS-A=%s", aprs_buf); - int rc = aprs_sock.Write((unsigned char *)aprs_buf, strlen(aprs_buf)); - if (rc == -1) { + int rc = aprs_sock.Write((unsigned char *)aprs_buf, strlen(aprs_buf)); + if (rc == -1) + { if ((errno == EPIPE) || - (errno == ECONNRESET) || - (errno == ETIMEDOUT) || - (errno == ECONNABORTED) || - (errno == ESHUTDOWN) || - (errno == EHOSTUNREACH) || - (errno == ENETRESET) || - (errno == ENETDOWN) || - (errno == ENETUNREACH) || - (errno == EHOSTDOWN) || - (errno == ENOTCONN)) { + (errno == ECONNRESET) || + (errno == ETIMEDOUT) || + (errno == ECONNABORTED) || + (errno == ESHUTDOWN) || + (errno == EHOSTUNREACH) || + (errno == ENETRESET) || + (errno == ENETDOWN) || + (errno == ENETUNREACH) || + (errno == EHOSTDOWN) || + (errno == ENOTCONN)) + { printf("CAPRS::ProcessText(): APRS_HOST closed connection, error=%d\n",errno); aprs_sock.Close(); - } else /* if it is WOULDBLOCK, we will not go into a loop here */ + } + else /* if it is WOULDBLOCK, we will not go into a loop here */ printf("CAPRS::ProcessText(): send error=%d\n", errno); } @@ -137,7 +146,8 @@ void CAPRS::ProcessText(unsigned short streamID, unsigned char seq, unsigned cha void CAPRS::Init() { /* Initialize the statistics on the APRS packets */ - for (short int rptr_idx = 0; rptr_idx < 3; rptr_idx++) { + for (short int rptr_idx = 0; rptr_idx < 3; rptr_idx++) + { aprs_pack[rptr_idx].al = al_none; aprs_pack[rptr_idx].data[0] = '\0'; aprs_pack[rptr_idx].len = 0; @@ -146,7 +156,8 @@ void CAPRS::Init() aprs_pack[rptr_idx].is_sent = false; } - for (short int i = 0; i < 3; i++) { + for (short int i = 0; i < 3; i++) + { aprs_streamID[i].streamID = 0; aprs_streamID[i].last_time = 0; } @@ -157,7 +168,8 @@ void CAPRS::Init() bool CAPRS::WriteData(short int rptr_idx, unsigned char *data) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::WriteData: rptr_idx %d is invalid\n", rptr_idx); return false; } @@ -165,7 +177,8 @@ bool CAPRS::WriteData(short int rptr_idx, unsigned char *data) if (aprs_pack[rptr_idx].is_sent) return false; - switch (aprs_pack[rptr_idx].sl) { + switch (aprs_pack[rptr_idx].sl) + { case sl_first: aprs_pack[rptr_idx].buf[0] = data[0] ^ 0x70; aprs_pack[rptr_idx].buf[1] = data[1] ^ 0x4f; @@ -190,7 +203,8 @@ bool CAPRS::WriteData(short int rptr_idx, unsigned char *data) void CAPRS::SyncIt(short int rptr_idx) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::SyncIt(): rptr_idx %d is invalid\n", rptr_idx); return; } @@ -201,7 +215,8 @@ void CAPRS::SyncIt(short int rptr_idx) void CAPRS::Reset(short int rptr_idx) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::Reset(): rptr_idx %d is invalid\n", rptr_idx); return; } @@ -216,7 +231,8 @@ void CAPRS::Reset(short int rptr_idx) unsigned int CAPRS::GetData(short int rptr_idx, unsigned char *data, unsigned int len) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::GetData: rptr_idx %d is invalid\n", rptr_idx); return 0; } @@ -239,117 +255,157 @@ void CAPRS::Open(const std::string OWNER) { char snd_buf[512]; char rcv_buf[512]; - while (aprs_sock.Open(m_rptr->aprs.ip, AF_UNSPEC, std::to_string(m_rptr->aprs.port))) { - fprintf(stderr, "Failed to open %s, retry in 10 seconds...\n", m_rptr->aprs.ip.c_str()); - std::this_thread::sleep_for(std::chrono::seconds(10)); - } + while (aprs_sock.Open(m_rptr->aprs.ip, AF_UNSPEC, std::to_string(m_rptr->aprs.port))) + { + fprintf(stderr, "Failed to open %s, retry in 10 seconds...\n", m_rptr->aprs.ip.c_str()); + std::this_thread::sleep_for(std::chrono::seconds(10)); + } /* login to aprs */ //sprintf(snd_buf, "user %s pass %d vers QnetGateway 9 UDP 5 ", OWNER.c_str(), m_rptr->aprs_hash); sprintf(snd_buf, "user %s pass %d vers QnetGateway-9 ", OWNER.c_str(), m_rptr->aprs_hash); /* add the user's filter */ - if (m_rptr->aprs_filter.length()) { + if (m_rptr->aprs_filter.length()) + { strcat(snd_buf, "filter "); strcat(snd_buf, m_rptr->aprs_filter.c_str()); } //printf("APRS Login command:[%s]\n", snd_buf); strcat(snd_buf, "\r\n"); - while (true) { - int rc = aprs_sock.Write((unsigned char *)snd_buf, strlen(snd_buf)); - if (rc < 0) { - if (errno == EWOULDBLOCK) { - aprs_sock.Read((unsigned char *)rcv_buf, sizeof(rcv_buf)); + while (true) + { + int rc = aprs_sock.Write((unsigned char *)snd_buf, strlen(snd_buf)); + if (rc < 0) + { + if (errno == EWOULDBLOCK) + { + aprs_sock.Read((unsigned char *)rcv_buf, sizeof(rcv_buf)); std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } else { + } + else + { printf("APRS Login command failed, error=%d\n", errno); break; } - } else { + } + else + { // printf("APRS Login command sent\n"); break; } } - aprs_sock.Read((unsigned char *)rcv_buf, sizeof(rcv_buf)); + aprs_sock.Read((unsigned char *)rcv_buf, sizeof(rcv_buf)); //printf("APRS Login returned: %s", rcv_buf); return; } bool CAPRS::AddData(short int rptr_idx, unsigned char *data) { - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::AddData(): rptr_idx %d is invalid\n", rptr_idx); return false; } - for (unsigned int i = 0; i < 5; i++) { + for (unsigned int i = 0; i < 5; i++) + { unsigned char c = data[i]; - if ((aprs_pack[rptr_idx].al == al_none) && (c == '$')) { + if ((aprs_pack[rptr_idx].al == al_none) && (c == '$')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_$1; - } else if ((aprs_pack[rptr_idx].al == al_$1) && (c == '$')) { + } + else if ((aprs_pack[rptr_idx].al == al_$1) && (c == '$')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_$2; - } else if ((aprs_pack[rptr_idx].al == al_$2) && (c == 'C')) { + } + else if ((aprs_pack[rptr_idx].al == al_$2) && (c == 'C')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_c1; - } else if ((aprs_pack[rptr_idx].al == al_c1) && (c == 'R')) { + } + else if ((aprs_pack[rptr_idx].al == al_c1) && (c == 'R')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_r1; - } else if ((aprs_pack[rptr_idx].al == al_r1) && (c == 'C')) { + } + else if ((aprs_pack[rptr_idx].al == al_r1) && (c == 'C')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_c2; - } else if (aprs_pack[rptr_idx].al == al_c2) { + } + else if (aprs_pack[rptr_idx].al == al_c2) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_csum1; - } else if (aprs_pack[rptr_idx].al == al_csum1) { + } + else if (aprs_pack[rptr_idx].al == al_csum1) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_csum2; - } else if (aprs_pack[rptr_idx].al == al_csum2) { + } + else if (aprs_pack[rptr_idx].al == al_csum2) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_csum3; - } else if (aprs_pack[rptr_idx].al == al_csum3) { + } + else if (aprs_pack[rptr_idx].al == al_csum3) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_csum4; - } else if ((aprs_pack[rptr_idx].al == al_csum4) && (c == ',')) { + } + else if ((aprs_pack[rptr_idx].al == al_csum4) && (c == ',')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; aprs_pack[rptr_idx].al = al_data; - } else if ((aprs_pack[rptr_idx].al == al_data) && (c != '\r')) { + } + else if ((aprs_pack[rptr_idx].al == al_data) && (c != '\r')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; - if (aprs_pack[rptr_idx].len >= 300) { + if (aprs_pack[rptr_idx].len >= 300) + { printf("ERROR in aprs_add_data: Expected END of APRS data\n"); aprs_pack[rptr_idx].len = 0; aprs_pack[rptr_idx].al = al_none; } - } else if ((aprs_pack[rptr_idx].al == al_data) && (c == '\r')) { + } + else if ((aprs_pack[rptr_idx].al == al_data) && (c == '\r')) + { aprs_pack[rptr_idx].data[aprs_pack[rptr_idx].len] = c; aprs_pack[rptr_idx].len++; bool ok = CheckData(rptr_idx); - if (ok) { + if (ok) + { aprs_pack[rptr_idx].al = al_end; return true; - } else { + } + else + { printf("BAD checksum in APRS data\n"); aprs_pack[rptr_idx].al = al_none; aprs_pack[rptr_idx].len = 0; } - } else { + } + else + { aprs_pack[rptr_idx].al = al_none; aprs_pack[rptr_idx].len = 0; } @@ -362,7 +418,8 @@ bool CAPRS::CheckData(short int rptr_idx) unsigned int my_sum; char buf[5]; - if ((rptr_idx < 0) || (rptr_idx > 2)) { + if ((rptr_idx < 0) || (rptr_idx > 2)) + { printf("CAPRS::CheckData(): rptr_idx %d is invalid\n", rptr_idx); return false; } @@ -383,10 +440,12 @@ unsigned int CAPRS::CalcCRC(unsigned char* buf, unsigned int len) if (len <= 0) return 0; - for (unsigned int j = 0; j < len; j++) { + for (unsigned int j = 0; j < len; j++) + { unsigned int c = buf[j]; - for (unsigned int i = 0; i < 8; i++) { + for (unsigned int i = 0; i < 8; i++) + { bool xor_val = (((my_crc ^ c) & 0x01) == 0x01); my_crc >>= 1; @@ -406,10 +465,10 @@ CAPRS::CAPRS(SRPTR *prptr) CAPRS::~CAPRS() { - aprs_sock.Close(); + aprs_sock.Close(); } void CAPRS::CloseSock() { - aprs_sock.Close(); + aprs_sock.Close(); } diff --git a/aprs.h b/aprs.h index 13874bb..d9dd701 100644 --- a/aprs.h +++ b/aprs.h @@ -30,12 +30,14 @@ enum aprs_level { al_none, al_$1, al_$2, al_c1, al_r1, al_c2, al_csum1, al_csum2 enum slow_level { sl_first, sl_second }; -using SPORTIP = struct portip_tag { +using SPORTIP = struct portip_tag +{ std::string ip; int port; }; -using SMOD = struct aprs_module { +using SMOD = struct aprs_module +{ std::string call; /* KJ4NHF-B */ bool defined; std::string band; /* 23cm ... */ @@ -43,7 +45,8 @@ using SMOD = struct aprs_module { std::string desc1, desc2, url, package_version; }; -using SRPTR = struct aprs_info { +using SRPTR = struct aprs_info +{ SPORTIP aprs; std::string aprs_filter; int aprs_hash; @@ -53,7 +56,8 @@ using SRPTR = struct aprs_info { SMOD mod[3]; }; -class CAPRS { +class CAPRS +{ public: // functions CAPRS(SRPTR *prptr); @@ -68,7 +72,8 @@ public: private: // data - struct { + struct + { aprs_level al; unsigned char data[300]; unsigned int len; @@ -77,7 +82,8 @@ private: bool is_sent; } aprs_pack[3]; // lock down a stream per band - struct { + struct + { unsigned short streamID; time_t last_time; } aprs_streamID[3]; diff --git a/ircddb/IRCClient.cpp b/ircddb/IRCClient.cpp index 0edab22..2126ab6 100644 --- a/ircddb/IRCClient.cpp +++ b/ircddb/IRCClient.cpp @@ -41,7 +41,7 @@ bool IRCClient::startWork() void IRCClient::stopWork() { terminateThread = true; - client_thread.get(); + client_thread.get(); } #define MAXIPV4ADDR 10 @@ -53,113 +53,133 @@ void IRCClient::Entry() int timer = 0; socklen_t optlen; - while (true) { + while (true) + { - if (timer > 0) { + if (timer > 0) + { timer--; } - switch (state) { - case 0: - if (terminateThread) { - printf("IRCClient::Entry: thread terminated at state=%d\n", state); - return; - } - - if (timer == 0) { - timer = 30; - - if (! ircSock.Open(host_name, AF_UNSPEC, std::to_string(port))) { - state = 4; - timer = 0; - } - } - break; - - - case 4: - optlen = sizeof(int); - getsockopt(ircSock.GetFD(), SOL_SOCKET, SO_DOMAIN, &family, &optlen); - recvQ = new IRCMessageQueue(); - sendQ = new IRCMessageQueue(); - - receiver.Init(&ircSock, recvQ); - receiver.startWork(); - - proto.setNetworkReady(true); - state = 5; - timer = 0; - break; - - - case 5: - if (terminateThread) { - state = 6; - } else { - - if (recvQ->isEOF()) { - timer = 0; - state = 6; - } else if (proto.processQueues(recvQ, sendQ) == false) { - timer = 0; - state = 6; - } - - while ((state == 5) && sendQ->messageAvailable()) { - IRCMessage * m = sendQ->getMessage(); - - std::string out; - - m->composeMessage(out); - - char buf[200]; - safeStringCopy(buf, out.c_str(), sizeof buf); - int len = strlen(buf); - - if (buf[len - 1] == 10) { // is there a NL char at the end? - if (ircSock.Write((unsigned char *)buf, len)) { - printf("IRCClient::Entry: short write\n"); - - timer = 0; - state = 6; - } - } else { - printf("IRCClient::Entry: no NL at end, len=%d\n", len); - - timer = 0; - state = 6; - } - - delete m; - } - } - break; - - case 6: { - if (app != NULL) { - app->setSendQ(NULL); - app->userListReset(); - } - - proto.setNetworkReady(false); - receiver.stopWork(); - - sleep(2); - - delete recvQ; - delete sendQ; - - ircSock.Close(); - - if (terminateThread) { // request to end the thread - printf("IRCClient::Entry: thread terminated at state=%d\n", state); - return; - } - - timer = 30; - state = 0; // reconnect to IRC server - } - break; + switch (state) + { + case 0: + if (terminateThread) + { + printf("IRCClient::Entry: thread terminated at state=%d\n", state); + return; + } + + if (timer == 0) + { + timer = 30; + + if (! ircSock.Open(host_name, AF_UNSPEC, std::to_string(port))) + { + state = 4; + timer = 0; + } + } + break; + + + case 4: + optlen = sizeof(int); + getsockopt(ircSock.GetFD(), SOL_SOCKET, SO_DOMAIN, &family, &optlen); + recvQ = new IRCMessageQueue(); + sendQ = new IRCMessageQueue(); + + receiver.Init(&ircSock, recvQ); + receiver.startWork(); + + proto.setNetworkReady(true); + state = 5; + timer = 0; + break; + + + case 5: + if (terminateThread) + { + state = 6; + } + else + { + + if (recvQ->isEOF()) + { + timer = 0; + state = 6; + } + else if (proto.processQueues(recvQ, sendQ) == false) + { + timer = 0; + state = 6; + } + + while ((state == 5) && sendQ->messageAvailable()) + { + IRCMessage * m = sendQ->getMessage(); + + std::string out; + + m->composeMessage(out); + + char buf[200]; + safeStringCopy(buf, out.c_str(), sizeof buf); + int len = strlen(buf); + + if (buf[len - 1] == 10) // is there a NL char at the end? + { + if (ircSock.Write((unsigned char *)buf, len)) + { + printf("IRCClient::Entry: short write\n"); + + timer = 0; + state = 6; + } + } + else + { + printf("IRCClient::Entry: no NL at end, len=%d\n", len); + + timer = 0; + state = 6; + } + + delete m; + } + } + break; + + case 6: + { + if (app != NULL) + { + app->setSendQ(NULL); + app->userListReset(); + } + + proto.setNetworkReady(false); + receiver.stopWork(); + + sleep(2); + + delete recvQ; + delete sendQ; + + ircSock.Close(); + + if (terminateThread) // request to end the thread + { + printf("IRCClient::Entry: thread terminated at state=%d\n", state); + return; + } + + timer = 30; + state = 0; // reconnect to IRC server + } + break; } // switch usleep(500000); diff --git a/ircddb/IRCDDB.cpp b/ircddb/IRCDDB.cpp index 59e5cb1..ed24939 100644 --- a/ircddb/IRCDDB.cpp +++ b/ircddb/IRCDDB.cpp @@ -59,27 +59,32 @@ void CIRCDDB::kickWatchdog(const std::string &wdInfo) // Send heard data, a false return implies a network error bool CIRCDDB::sendHeard(const std::string &myCall, const std::string &myCallExt, const std::string &yourCall, const std::string &rpt1, const std::string &rpt2, unsigned char flag1, unsigned char flag2, unsigned char flag3) { - if (myCall.size() != 8) { + if (myCall.size() != 8) + { printf("CIRCDDB::sendHeard:myCall: len != 8\n"); return false; } - if (myCallExt.size() != 4) { + if (myCallExt.size() != 4) + { printf("CIRCDDB::sendHeard:myCallExt: len != 4\n"); return false; } - if (yourCall.size() != 8) { + if (yourCall.size() != 8) + { printf("CIRCDDB::sendHeard:yourCall: len != 8\n"); return false; } - if (rpt1.size() != 8) { + if (rpt1.size() != 8) + { printf("CIRCDDB::sendHeard:rpt1: len != 8\n"); return false; } - if (rpt2.size() != 8) { + if (rpt2.size() != 8) + { printf("CIRCDDB::sendHeard:rpt2: len != 8\n"); return false; } @@ -90,27 +95,32 @@ bool CIRCDDB::sendHeard(const std::string &myCall, const std::string &myCallExt, // Send heard data, a false return implies a network error bool CIRCDDB::sendHeardWithTXMsg(const std::string &myCall, const std::string &myCallExt, const std::string &yourCall, const std::string &rpt1, const std::string &rpt2, unsigned char flag1, unsigned char flag2, unsigned char flag3, const std::string &network_destination, const std::string &tx_message) { - if (myCall.size() != 8) { + if (myCall.size() != 8) + { printf("CIRCDDB::sendHeard:myCall: len != 8\n"); return false; } - if (myCallExt.size() != 4) { + if (myCallExt.size() != 4) + { printf("CIRCDDB::sendHeard:myCallExt: len != 4\n"); return false; } - if (yourCall.size() != 8) { + if (yourCall.size() != 8) + { printf("CIRCDDB::sendHeard:yourCall: len != 8\n"); return false; } - if (rpt1.size() != 8) { + if (rpt1.size() != 8) + { printf("CIRCDDB::sendHeard:rpt1: len != 8\n"); return false; } - if (rpt2.size() != 8) { + if (rpt2.size() != 8) + { printf("CIRCDDB::sendHeard:rpt2: len != 8\n"); return false; } @@ -120,20 +130,26 @@ bool CIRCDDB::sendHeardWithTXMsg(const std::string &myCall, const std::string &m if (dest.size() == 0) dest = " "; - if (dest.size() != 8) { + if (dest.size() != 8) + { printf("CIRCDDB::sendHeard:network_destination: len != 8\n"); return false; } std::string msg; - if (tx_message.length() == 20) { - for (unsigned int i=0; i < tx_message.size(); i++) { + if (tx_message.length() == 20) + { + for (unsigned int i=0; i < tx_message.size(); i++) + { char ch = tx_message.at(i); - if (ch>32 && ch<127) { + if (ch>32 && ch<127) + { msg.push_back(ch); - } else { + } + else + { msg.push_back('_'); } } @@ -143,44 +159,52 @@ bool CIRCDDB::sendHeardWithTXMsg(const std::string &myCall, const std::string &m } bool CIRCDDB::sendHeardWithTXStats(const std::string &myCall, const std::string &myCallExt, const std::string &yourCall, const std::string &rpt1, const std::string &rpt2, unsigned char flag1, - unsigned char flag2, unsigned char flag3, int num_dv_frames, int num_dv_silent_frames, int num_bit_errors) + unsigned char flag2, unsigned char flag3, int num_dv_frames, int num_dv_silent_frames, int num_bit_errors) { - if (num_dv_frames<= 0 || num_dv_frames>65535) { + if (num_dv_frames<= 0 || num_dv_frames>65535) + { printf("CIRCDDB::sendHeard:num_dv_frames not in range 1-65535\n"); return false; } - if (num_dv_silent_frames > num_dv_frames) { + if (num_dv_silent_frames > num_dv_frames) + { printf("CIRCDDB::sendHeard:num_dv_silent_frames > num_dv_frames\n"); return false; } - if (num_bit_errors > 4*num_dv_frames) { // max 4 bit errors per frame + if (num_bit_errors > 4*num_dv_frames) // max 4 bit errors per frame + { printf("CIRCDDB::sendHeard:num_bit_errors > (4*num_dv_frames)\n"); return false; } - if (myCall.size() != 8) { + if (myCall.size() != 8) + { printf("CIRCDDB::sendHeard:myCall: len != 8\n"); return false; } - if (myCallExt.size() != 4) { + if (myCallExt.size() != 4) + { printf("CIRCDDB::sendHeard:myCallExt: len != 4\n"); return false; } - if (yourCall.size() != 8) { + if (yourCall.size() != 8) + { printf("CIRCDDB::sendHeard:yourCall: len != 8\n"); return false; } - if (rpt1.size() != 8) { + if (rpt1.size() != 8) + { printf("CIRCDDB::sendHeard:rpt1: len != 8\n"); return false; } - if (rpt2.size() != 8) { + if (rpt2.size() != 8) + { printf("CIRCDDB::sendHeard:rpt2: len != 8\n"); return false; } @@ -189,17 +213,23 @@ bool CIRCDDB::sendHeardWithTXStats(const std::string &myCall, const std::string snprintf(buf, 16, "%04x", num_dv_frames); std::string stats = buf; - if (num_dv_silent_frames >= 0) { + if (num_dv_silent_frames >= 0) + { snprintf(buf, 16, "%02x", num_dv_silent_frames * 100 / num_dv_frames); stats.append(buf); - if (num_bit_errors >= 0) { + if (num_bit_errors >= 0) + { snprintf(buf,16, "%02x", num_bit_errors * 125 / (num_dv_frames * 3)); stats.append(buf); - } else { + } + else + { stats.append("__"); } - } else { + } + else + { stats.append("____"); } @@ -211,7 +241,8 @@ bool CIRCDDB::sendHeardWithTXStats(const std::string &myCall, const std::string // Send query for a user, a false return implies a network error bool CIRCDDB::findUser(const std::string &userCallsign) { - if (userCallsign.size() != 8) { + if (userCallsign.size() != 8) + { printf("CIRCDDB::findUser: len != 8\n"); return false; } @@ -232,24 +263,28 @@ bool CIRCDDB::receivePing(std::string &repeaterCallsign) { IRCDDB_RESPONSE_TYPE rt = app->getReplyMessageType(); - if (rt != IDRT_PING) { + if (rt != IDRT_PING) + { printf("CIRCDDB::receivePing: unexpected response type\n"); return false; } IRCMessage *m = app->getReplyMessage(); - if (NULL == m) { + if (NULL == m) + { printf("CIRCDDB::receivePing: no message\n"); return false; } - if (m->getCommand().compare("IDRT_PING")) { + if (m->getCommand().compare("IDRT_PING")) + { printf("CIRCDDB::receivePing: wrong messsage type\n"); return false; } - if (1 != m->getParamCount()) { + if (1 != m->getParamCount()) + { printf("CIRCDDB::receivePing: unexpected number of message parameters\n"); return false; } diff --git a/ircddb/IRCDDB.h b/ircddb/IRCDDB.h index 57e3a93..0565eb9 100644 --- a/ircddb/IRCDDB.h +++ b/ircddb/IRCDDB.h @@ -4,12 +4,14 @@ #include "../CacheManager.h" -enum IRCDDB_RESPONSE_TYPE { +enum IRCDDB_RESPONSE_TYPE +{ IDRT_NONE, IDRT_PING }; -enum DSTAR_PROTOCOL { +enum DSTAR_PROTOCOL +{ DP_UNKNOWN, DP_DEXTRA, DP_DPLUS diff --git a/ircddb/IRCDDBApp.cpp b/ircddb/IRCDDBApp.cpp index 9e035eb..dfef825 100644 --- a/ircddb/IRCDDBApp.cpp +++ b/ircddb/IRCDDBApp.cpp @@ -30,7 +30,8 @@ IRCDDBApp::IRCDDBApp(const std::string &u_chan, CCacheManager *cache) : numberOf IRCDDBApp::~IRCDDBApp() { - if (sendQ != NULL) { + if (sendQ != NULL) + { delete sendQ; } } @@ -98,7 +99,8 @@ void IRCDDBApp::rptrQTH(const std::string &rptrcall, double latitude, double lon void IRCDDBApp::rptrQRG(const std::string &rptrcall, double txFrequency, double duplexShift, double range, double agl) { - if (std::regex_match(rptrcall, modulePattern)) { + if (std::regex_match(rptrcall, modulePattern)) + { std::string c = rptrcall; ReplaceChar(c, ' ', '_'); @@ -119,7 +121,8 @@ void IRCDDBApp::rptrQRG(const std::string &rptrcall, double txFrequency, double void IRCDDBApp::kickWatchdog(const std::string &s) { - if (s.length() > 0) { + if (s.length() > 0) + { std::regex nonValid("[^[:graph:]]+"); std::smatch sm; @@ -141,13 +144,15 @@ int IRCDDBApp::getConnectionState() IRCDDB_RESPONSE_TYPE IRCDDBApp::getReplyMessageType() { IRCMessage * m = replyQ.peekFirst(); - if (m == NULL) { + if (m == NULL) + { return IDRT_NONE; } std::string msgType = m->getCommand(); - if (msgType == std::string("IDRT_PING")) { + if (msgType == std::string("IDRT_PING")) + { return IDRT_PING; } @@ -181,7 +186,8 @@ void IRCDDBApp::stopWork() void IRCDDBApp::userJoin(const std::string &nick, const std::string &name, const std::string &addr) { - if (0 == nick.compare(0, 2, "u-")) { + if (0 == nick.compare(0, 2, "u-")) + { return; } std::string gate(name); @@ -194,7 +200,8 @@ void IRCDDBApp::userJoin(const std::string &nick, const std::string &name, const void IRCDDBApp::userLeave(const std::string &nick) { - if (0 == nick.compare(0, 2, "s-")) { + if (0 == nick.compare(0, 2, "s-")) + { currentServer.clear(); state = 2; timer = 200; @@ -203,7 +210,8 @@ void IRCDDBApp::userLeave(const std::string &nick) } std::string name(nick); name.pop_back(); - if ('-' == name.back()) { + if ('-' == name.back()) + { name.pop_back(); cache->eraseName(name); ToUpper(name); @@ -252,7 +260,8 @@ void IRCDDBApp::sendPing(const std::string &to, const std::string &from) auto nick = cache->findNameNick(name); - if (! nick.empty()) { + if (! nick.empty()) + { std::string rptr(from); ReplaceChar(rptr, ' ', '_'); IRCMessage *m = new IRCMessage(nick, "IDRT_PING"); @@ -292,7 +301,8 @@ bool IRCDDBApp::sendHeard(const std::string &myCall, const std::string &myCallEx std::string srv = currentServer; IRCMessageQueue *q = getSendQ(); - if ((srv.length() > 0) && (state >= 6) && (q != NULL)) { + if ((srv.length() > 0) && (state >= 6) && (q != NULL)) + { std::string cmd("UPDATE "); cmd.append(getCurrentTime()); @@ -303,7 +313,8 @@ bool IRCDDBApp::sendHeard(const std::string &myCall, const std::string &myCallEx cmd.append(" "); cmd.append(r1); cmd.append(" "); - if (!statsMsg) { + if (!statsMsg) + { cmd.append("0 "); } cmd.append(r2); @@ -318,14 +329,18 @@ bool IRCDDBApp::sendHeard(const std::string &myCall, const std::string &myCallEx cmd.append(" "); cmd.append(myext); - if (statsMsg) { + if (statsMsg) + { cmd.append(" # "); cmd.append(tx_stats); - } else { + } + else + { cmd.append(" 00 "); cmd.append(dest); - if (tx_msg.length() == 20) { + if (tx_msg.length() == 20) + { cmd.append(" "); cmd.append(tx_msg); } @@ -336,7 +351,8 @@ bool IRCDDBApp::sendHeard(const std::string &myCall, const std::string &myCallEx q->putMessage(m); return true; - } else + } + else return false; } @@ -345,7 +361,8 @@ bool IRCDDBApp::findUser(const std::string &usrCall) std::string srv = currentServer; IRCMessageQueue *q = getSendQ(); - if ((srv.length() > 0) && (state >= 6) && (q != NULL)) { + if ((srv.length() > 0) && (state >= 6) && (q != NULL)) + { std::string usr = usrCall; ReplaceChar(usr, ' ', '_'); @@ -360,7 +377,8 @@ bool IRCDDBApp::findUser(const std::string &usrCall) void IRCDDBApp::msgChannel(IRCMessage *m) { - if (0==m->getPrefixNick().compare(0, 2, "s-") && (m->numParams >= 2)) { // server msg + if (0==m->getPrefixNick().compare(0, 2, "s-") && (m->numParams >= 2)) // server msg + { doUpdate(m->params[1]); } } @@ -378,10 +396,12 @@ void IRCDDBApp::doNotFound(std::string &msg, std::string &retval) tkz.erase(tkz.begin()); - if (std::regex_match(tk, tablePattern)) { + if (std::regex_match(tk, tablePattern)) + { long tableID = std::stol(tk); - if ((tableID < 0) || (tableID >= numberOfTables)) { + if ((tableID < 0) || (tableID >= numberOfTables)) + { printf("invalid table ID %ld", tableID); return; } @@ -393,7 +413,8 @@ void IRCDDBApp::doNotFound(std::string &msg, std::string &retval) tk.erase(tk.begin()); } - if (tableID == 0) { + if (tableID == 0) + { if (! std::regex_match(tk, dbPattern)) return; // no valid key @@ -413,9 +434,11 @@ void IRCDDBApp::doUpdate(std::string &msg) std::string tk = tkz.front(); tkz.erase(tkz.begin()); - if (std::regex_match(tk, tablePattern)) { + if (std::regex_match(tk, tablePattern)) + { tableID = stol(tk); - if ((tableID < 0) || (tableID >= numberOfTables)) { + if ((tableID < 0) || (tableID >= numberOfTables)) + { printf("invalid table ID %d", tableID); return; } @@ -427,7 +450,8 @@ void IRCDDBApp::doUpdate(std::string &msg) tkz.erase(tkz.begin()); } - if (std::regex_match(tk, datePattern)) { + if (std::regex_match(tk, datePattern)) + { if (0 == tkz.size()) return; // nothing after date string @@ -440,7 +464,8 @@ void IRCDDBApp::doUpdate(std::string &msg) std::string tstr(std::string(tk + " " + timeToken)); // used to update user time auto rtime = parseTime(tstr); // used to update maxTime for sendlist - if ((tableID == 0) || (tableID == 1)) { + if ((tableID == 0) || (tableID == 1)) + { if (0 == tkz.size()) return; // nothing after time string @@ -461,9 +486,11 @@ void IRCDDBApp::doUpdate(std::string &msg) //printf("TABLE %d %s %s\n", tableID, key.c_str(), value.c_str()); - if (tableID == 1) { + if (tableID == 1) + { - if (initReady && key.compare(0,6, value, 0, 6)) { + if (initReady && key.compare(0,6, value, 0, 6)) + { std::string rptr(key); std::string gate(value); @@ -474,7 +501,9 @@ void IRCDDBApp::doUpdate(std::string &msg) if (rtime > maxTime) maxTime = rtime; } - } else if ((tableID == 0) && initReady) { + } + else if ((tableID == 0) && initReady) + { std::string user(key); std::string rptr(value); @@ -490,15 +519,23 @@ void IRCDDBApp::doUpdate(std::string &msg) std::string IRCDDBApp::getTableIDString(int tableID, bool spaceBeforeNumber) { - if (tableID == 0) { + if (tableID == 0) + { return std::string(""); - } else if ((tableID > 0) && (tableID < numberOfTables)) { - if (spaceBeforeNumber) { + } + else if ((tableID > 0) && (tableID < numberOfTables)) + { + if (spaceBeforeNumber) + { return std::string(" ") + std::to_string(tableID); - } else { + } + else + { return std::to_string(tableID) + std::string(" "); } - } else { + } + else + { return std::string(" TABLE_ID_OUT_OF_RANGE "); } } @@ -506,7 +543,8 @@ std::string IRCDDBApp::getTableIDString(int tableID, bool spaceBeforeNumber) void IRCDDBApp::msgQuery(IRCMessage *m) { - if (0 == strcmp(m->getPrefixNick().substr(0,2).c_str(), "s-") && (m->numParams >= 2)) { // server msg + if (0 == strcmp(m->getPrefixNick().substr(0,2).c_str(), "s-") && (m->numParams >= 2)) // server msg + { std::string msg = m->params[1]; std::vector tkz = stringTokenizer(msg); @@ -516,27 +554,38 @@ void IRCDDBApp::msgQuery(IRCMessage *m) std::string cmd = tkz.front(); tkz.erase(tkz.begin()); - if (cmd == std::string("UPDATE")) { + if (cmd == std::string("UPDATE")) + { std::string restOfLine; - while (tkz.size()) { + while (tkz.size()) + { restOfLine += tkz.front(); tkz.erase(tkz.begin()); if (tkz.size()) restOfLine += " "; } doUpdate(restOfLine); - } else if (cmd == std::string("LIST_END")) { - if (state == 5) { // if in sendlist processing state + } + else if (cmd == std::string("LIST_END")) + { + if (state == 5) // if in sendlist processing state + { state = 3; // get next table } - } else if (cmd == std::string("LIST_MORE")) { - if (state == 5) { // if in sendlist processing state + } + else if (cmd == std::string("LIST_MORE")) + { + if (state == 5) // if in sendlist processing state + { state = 4; // send next SENDLIST } - } else if (cmd == std::string("NOT_FOUND")) { + } + else if (cmd == std::string("NOT_FOUND")) + { std::string callsign; std::string restOfLine; - while (tkz.size()) { + while (tkz.size()) + { restOfLine += tkz.front(); tkz.erase(tkz.begin()); if (tkz.size()) @@ -544,7 +593,8 @@ void IRCDDBApp::msgQuery(IRCMessage *m) } doNotFound(restOfLine, callsign); - if (callsign.length() > 0) { + if (callsign.length() > 0) + { ReplaceChar(callsign, '_', ' '); IRCMessage *m2 = new IRCMessage("IDRT_USER"); @@ -571,7 +621,8 @@ IRCMessageQueue *IRCDDBApp::getSendQ() std::string IRCDDBApp::getLastEntryTime(int tableID) { - if (tableID == 1) { + if (tableID == 1) + { struct tm *ptm = gmtime(&maxTime); char tstr[80]; strftime(tstr, 80, "%Y-%m-%d %H:%M:%S", ptm); @@ -586,16 +637,20 @@ void IRCDDBApp::Entry() { int sendlistTableID = 0; - while (!terminateThread) { + while (!terminateThread) + { - if (timer > 0) { + if (timer > 0) + { timer--; } - switch(state) { + switch(state) + { case 0: // wait for network to start - if (getSendQ() != NULL) { + if (getSendQ() != NULL) + { state = 1; } break; @@ -608,21 +663,28 @@ void IRCDDBApp::Entry() case 2: // choose server printf("IRCDDBApp: state=2 choose new 's-'-user\n"); - if (getSendQ() == NULL) { + if (getSendQ() == NULL) + { state = 10; - } else { - if (findServerUser()) { + } + else + { + if (findServerUser()) + { sendlistTableID = numberOfTables; state = 3; // next: send "SENDLIST" - } else if (timer == 0) { + } + else if (timer == 0) + { state = 10; IRCMessage *m = new IRCMessage("QUIT"); m->addParam("no op user with 's-' found."); IRCMessageQueue * q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } } @@ -630,13 +692,19 @@ void IRCDDBApp::Entry() break; case 3: - if (getSendQ() == NULL) { + if (getSendQ() == NULL) + { state = 10; // disconnect DB - } else { + } + else + { sendlistTableID --; - if (sendlistTableID < 0) { + if (sendlistTableID < 0) + { state = 6; // end of sendlist - } else { + } + else + { printf("IRCDDBApp: state=3 tableID=%d\n", sendlistTableID); state = 4; // send "SENDLIST" timer = 900; // 15 minutes max for update @@ -645,34 +713,43 @@ void IRCDDBApp::Entry() break; case 4: - if (getSendQ() == NULL) { + if (getSendQ() == NULL) + { state = 10; // disconnect DB - } else { - if (1 == sendlistTableID) { + } + else + { + if (1 == sendlistTableID) + { IRCMessage *m = new IRCMessage(currentServer, std::string("SENDLIST") + getTableIDString(sendlistTableID, true) - + std::string(" ") + getLastEntryTime(sendlistTableID)); + + std::string(" ") + getLastEntryTime(sendlistTableID)); IRCMessageQueue *q = getSendQ(); if (q != NULL) q->putMessage(m); state = 5; // wait for answers - } else + } + else state = 3; // don't send SENDLIST for this table (tableID 0), go to next table } break; case 5: // sendlist processing - if (getSendQ() == NULL) { + if (getSendQ() == NULL) + { state = 10; // disconnect DB - } else if (timer == 0) { + } + else if (timer == 0) + { state = 10; // disconnect DB IRCMessage *m = new IRCMessage("QUIT"); m->addParam("timeout SENDLIST"); IRCMessageQueue *q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } @@ -680,9 +757,12 @@ void IRCDDBApp::Entry() break; case 6: - if (getSendQ() == NULL) { + if (getSendQ() == NULL) + { state = 10; // disconnect DB - } else { + } + else + { printf("IRCDDBApp: state=6 initialization completed\n"); infoTimer = 2; @@ -697,48 +777,58 @@ void IRCDDBApp::Entry() if (getSendQ() == NULL) state = 10; // disconnect DB - if (infoTimer > 0) { + if (infoTimer > 0) + { infoTimer--; - if (infoTimer == 0) { + if (infoTimer == 0) + { moduleMapMutex.lock(); - for (auto itl = locationMap.begin(); itl != locationMap.end(); itl++) { + for (auto itl = locationMap.begin(); itl != locationMap.end(); itl++) + { std::string value = itl->second; IRCMessage *m = new IRCMessage(currentServer, std::string("IRCDDB RPTRQTH: ") + value); IRCMessageQueue * q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } } - for (auto itu = urlMap.begin(); itu != urlMap.end(); itu++) { + for (auto itu = urlMap.begin(); itu != urlMap.end(); itu++) + { std::string value = itu->second; IRCMessage * m = new IRCMessage(currentServer, std::string("IRCDDB RPTRURL: ") + value); IRCMessageQueue * q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } } - for(auto itm = moduleMap.begin(); itm != moduleMap.end(); itm++) { + for(auto itm = moduleMap.begin(); itm != moduleMap.end(); itm++) + { std::string value = itm->second; IRCMessage * m = new IRCMessage(currentServer, std::string("IRCDDB RPTRQRG: ") + value); IRCMessageQueue *q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } } - for(auto its = swMap.begin(); its != swMap.end(); its++) { + for(auto its = swMap.begin(); its != swMap.end(); its++) + { std::string value = its->second; IRCMessage * m = new IRCMessage(currentServer, std::string("IRCDDB RPTRSW: ") + value); IRCMessageQueue *q = getSendQ(); - if (q != NULL) { + if (q != NULL) + { q->putMessage(m); } } @@ -747,13 +837,15 @@ void IRCDDBApp::Entry() } } - if (wdTimer > 0) { + if (wdTimer > 0) + { wdTimer--; - if (wdTimer <= 0) { + if (wdTimer <= 0) + { wdTimer = 900; // 15 minutes IRCMessage *m = new IRCMessage(currentServer, std::string("IRCDDB WATCHDOG: ") + - getCurrentTime() + std::string(" ") + wdInfo + std::string(" 1")); + getCurrentTime() + std::string(" ") + wdInfo + std::string(" 1")); IRCMessageQueue *q = getSendQ(); if (q != NULL) diff --git a/ircddb/IRCMessage.cpp b/ircddb/IRCMessage.cpp index 0229435..0127c51 100644 --- a/ircddb/IRCMessage.cpp +++ b/ircddb/IRCMessage.cpp @@ -56,16 +56,19 @@ void IRCMessage::parsePrefix() { unsigned int i; - for (i=0; i < 3; i++) { + for (i=0; i < 3; i++) + { prefixComponents.push_back(""); } int state = 0; - for (i=0; i < prefix.length(); i++) { + for (i=0; i < prefix.length(); i++) + { char c = prefix.at(i); - switch (c) { + switch (c) + { case '!': state = 1; // next is name break; @@ -85,7 +88,8 @@ void IRCMessage::parsePrefix() std::string &IRCMessage::getPrefixNick() { - if (!prefixParsed) { + if (!prefixParsed) + { parsePrefix(); } @@ -94,7 +98,8 @@ std::string &IRCMessage::getPrefixNick() std::string &IRCMessage::getPrefixName() { - if (!prefixParsed) { + if (!prefixParsed) + { parsePrefix(); } @@ -103,7 +108,8 @@ std::string &IRCMessage::getPrefixName() std::string &IRCMessage::getPrefixHost() { - if (!prefixParsed) { + if (!prefixParsed) + { parsePrefix(); } @@ -114,16 +120,21 @@ void IRCMessage::composeMessage(std::string &output) { std::string o; - if (prefix.length() > 0) { + if (prefix.length() > 0) + { o = std::string(":") + prefix + ' '; } o += command; - for (int i=0; i < numParams; i++) { - if (i == (numParams - 1)) { + for (int i=0; i < numParams; i++) + { + if (i == (numParams - 1)) + { o += (std::string(" :") + params[i]); - } else { + } + else + { o += (std::string(" ") + params[i]); } } diff --git a/ircddb/IRCMessageQueue.cpp b/ircddb/IRCMessageQueue.cpp index 7d80397..04c9fa9 100644 --- a/ircddb/IRCMessageQueue.cpp +++ b/ircddb/IRCMessageQueue.cpp @@ -31,7 +31,8 @@ IRCMessageQueue::IRCMessageQueue() IRCMessageQueue::~IRCMessageQueue() { accessMutex.lock(); - while (! m_queue.empty()) { + while (! m_queue.empty()) + { delete m_queue.front(); m_queue.pop(); } @@ -50,10 +51,10 @@ void IRCMessageQueue::signalEOF() bool IRCMessageQueue::messageAvailable() { - accessMutex.lock(); - bool retv = ! m_queue.empty(); - accessMutex.unlock(); - return retv; + accessMutex.lock(); + bool retv = ! m_queue.empty(); + accessMutex.unlock(); + return retv; } IRCMessage *IRCMessageQueue::peekFirst() diff --git a/ircddb/IRCProtocol.cpp b/ircddb/IRCProtocol.cpp index 911371a..3b6f2e5 100644 --- a/ircddb/IRCProtocol.cpp +++ b/ircddb/IRCProtocol.cpp @@ -20,7 +20,8 @@ void IRCProtocol::Init(IRCDDBApp *app, const std::string &callsign, const std::s this->versionInfo = "CIRCDDB:"; this->versionInfo.append(CIRCDDB_VERSION); - if (versionInfo.length() > 0) { + if (versionInfo.length() > 0) + { this->versionInfo.append(" "); this->versionInfo.append(versionInfo); } @@ -28,7 +29,8 @@ void IRCProtocol::Init(IRCDDBApp *app, const std::string &callsign, const std::s int hyphenPos = callsign.find('-'); - if (hyphenPos < 0) { + if (hyphenPos < 0) + { std::string n; n = callsign + "-1"; @@ -39,7 +41,9 @@ void IRCProtocol::Init(IRCDDBApp *app, const std::string &callsign, const std::s nicks.push_back(n); n = callsign + "-4"; nicks.push_back(n); - } else { + } + else + { nicks.push_back(callsign); } @@ -66,13 +70,16 @@ void IRCProtocol::chooseNewNick() void IRCProtocol::setNetworkReady(bool b) { - if (b == true) { + if (b == true) + { if (state != 0) printf("IRCProtocol::setNetworkReady: unexpected state"); state = 1; chooseNewNick(); - } else { + } + else + { state = 0; } } @@ -80,16 +87,19 @@ void IRCProtocol::setNetworkReady(bool b) bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) { - if (timer > 0) { + if (timer > 0) + { timer--; } - while (recvQ->messageAvailable()) { + while (recvQ->messageAvailable()) + { IRCMessage *m = recvQ->getMessage(); #if defined(DEBUG_IRC) std::string d = std::string("R [") + m->prefix + std::string("] [") + m->command + std::string("]"); - for (int i=0; i < m->numParams; i++) { + for (int i=0; i < m->numParams; i++) + { d.append(std::string(" [") + m->params[i] + std::string("]") ); } // d.Replace(std::string("%"), std::string("%%"), true); @@ -97,96 +107,146 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) printf("%s\n", d.c_str()); #endif - if (0 == m->command.compare("004")) { - if (state == 4) { - if (m->params.size() > 1) { + if (0 == m->command.compare("004")) + { + if (state == 4) + { + if (m->params.size() > 1) + { std::regex serverNamePattern("^grp[1-9]s[1-9].ircDDB$"); - if (std::regex_match(m->params[1], serverNamePattern)) { + if (std::regex_match(m->params[1], serverNamePattern)) + { app->setBestServer(std::string("s-") + m->params[1].substr(0,6)); } } state = 5; // next: JOIN app->setCurrentNick(currentNick); } - } else if (0 == m->command.compare("PING")) { + } + else if (0 == m->command.compare("PING")) + { IRCMessage *m2 = new IRCMessage(); m2->command = "PONG"; - if (m->params.size() > 0) { + if (m->params.size() > 0) + { m2->numParams = 1; m2->params.push_back( m->params[0] ); } sendQ -> putMessage(m2); - } else if (0 == m->command.compare("JOIN")) { - if ((m->numParams >= 1) && 0==m->params[0].compare(channel)) { - if (0==m->getPrefixNick().compare(currentNick) && (state == 6)) { - if (debugChannel.length() > 0) { + } + else if (0 == m->command.compare("JOIN")) + { + if ((m->numParams >= 1) && 0==m->params[0].compare(channel)) + { + if (0==m->getPrefixNick().compare(currentNick) && (state == 6)) + { + if (debugChannel.length() > 0) + { state = 7; // next: join debug_channel - } else { + } + else + { state = 10; // next: WHO * } - } else if (app != NULL) { + } + else if (app != NULL) + { app->userJoin( m->getPrefixNick(), m->getPrefixName(), m->getPrefixHost()); } } - if ((m->numParams >= 1) && 0==m->params[0].compare(debugChannel)) { - if (0==m->getPrefixNick().compare(currentNick) && (state == 8)) { + if ((m->numParams >= 1) && 0==m->params[0].compare(debugChannel)) + { + if (0==m->getPrefixNick().compare(currentNick) && (state == 8)) + { state = 10; // next: WHO * } } - } else if (0 == m->command.compare("PONG")) { - if (state == 12) { + } + else if (0 == m->command.compare("PONG")) + { + if (state == 12) + { timer = pingTimer; state = 11; } - } else if (0 == m->command.compare("PART")) { - if ((m->numParams >= 1) && 0==m->params[0].compare(channel)) { - if (app != NULL) { + } + else if (0 == m->command.compare("PART")) + { + if ((m->numParams >= 1) && 0==m->params[0].compare(channel)) + { + if (app != NULL) + { app->userLeave( m->getPrefixNick() ); } } - } else if (0 == m->command.compare("KICK")) { - if ((m->numParams >= 2) && 0==m->params[0].compare(channel)) { - if (0 == m->params[1].compare(currentNick)) { + } + else if (0 == m->command.compare("KICK")) + { + if ((m->numParams >= 2) && 0==m->params[0].compare(channel)) + { + if (0 == m->params[1].compare(currentNick)) + { // i was kicked!! delete m; return false; - } else if (app != NULL) { + } + else if (app != NULL) + { app->userLeave( m->params[1] ); } } - } else if (0 == m->command.compare("QUIT")) { - if (app != NULL) { + } + else if (0 == m->command.compare("QUIT")) + { + if (app != NULL) + { app->userLeave( m->getPrefixNick() ); } - } else if (0 == m->command.compare("PRIVMSG")) { - if (app) { + } + else if (0 == m->command.compare("PRIVMSG")) + { + if (app) + { // std::string out; // m->composeMessage(out); // out.pop_back(); out.pop_back(); - if (2 == m->numParams) { - if (0 == m->params[0].compare(channel)) { + if (2 == m->numParams) + { + if (0 == m->params[0].compare(channel)) + { app->msgChannel(m); - } else if (0 == m->params[0].compare(currentNick)) { - if (0 == m->params[1].find("IDRT_PING")) { + } + else if (0 == m->params[0].compare(currentNick)) + { + if (0 == m->params[1].find("IDRT_PING")) + { std::string from = m->params[1].substr(10); IRCMessage *rm = new IRCMessage("IDRT_PING"); rm->addParam(from); app->putReplyMessage(rm); - } else + } + else app->msgQuery(m); } } } - } else if (0 == m->command.compare("352")) { // WHO list - if ((m->numParams >= 7) && 0==m->params[0].compare(currentNick) && 0==m->params[1].compare(channel)) { - if (app != NULL) { + } + else if (0 == m->command.compare("352")) // WHO list + { + if ((m->numParams >= 7) && 0==m->params[0].compare(currentNick) && 0==m->params[1].compare(channel)) + { + if (app != NULL) + { app->userJoin(m->params[5], m->params[2], m->params[3]); } } - } else if (0 == m->command.compare("433")) { // nick collision - if (state == 2) { + } + else if (0 == m->command.compare("433")) // nick collision + { + if (state == 2) + { state = 3; // nick collision, choose new nick timer = 10; // wait 5 seconds.. } @@ -197,7 +257,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) IRCMessage * m; - switch (state) { + switch (state) + { case 1: m = new IRCMessage(); m->command = "PASS"; @@ -216,7 +277,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 2: - if (timer == 0) { + if (timer == 0) + { m = new IRCMessage(); m->command = "USER"; m->numParams = 4; @@ -232,7 +294,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 3: - if (timer == 0) { + if (timer == 0) + { chooseNewNick(); m = new IRCMessage(); m->command = "NICK"; @@ -246,7 +309,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 4: - if (timer == 0) { + if (timer == 0) + { // no login message received -> disconnect return false; } @@ -264,14 +328,16 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 6: - if (timer == 0) { + if (timer == 0) + { // no join message received -> disconnect return false; } break; case 7: - if (debugChannel.length() == 0) { + if (debugChannel.length() == 0) + { return false; // this state cannot be processed if there is no debug_channel } @@ -286,7 +352,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 8: - if (timer == 0) { + if (timer == 0) + { // no join message received -> disconnect return false; } @@ -303,13 +370,15 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) timer = pingTimer; state = 11; // wait for timer and then send ping - if (app != NULL) { + if (app != NULL) + { app->setSendQ(sendQ); // this switches the application on } break; case 11: - if (timer == 0) { + if (timer == 0) + { m = new IRCMessage(); m->command = "PING"; m->numParams = 1; @@ -322,7 +391,8 @@ bool IRCProtocol::processQueues(IRCMessageQueue *recvQ, IRCMessageQueue *sendQ) break; case 12: - if (timer == 0) { + if (timer == 0) + { // no pong message received -> disconnect return false; } diff --git a/ircddb/IRCReceiver.cpp b/ircddb/IRCReceiver.cpp index e677c84..3284daa 100644 --- a/ircddb/IRCReceiver.cpp +++ b/ircddb/IRCReceiver.cpp @@ -47,25 +47,34 @@ int IRCReceiver::doRead(CTCPReaderWriterClient *ircSock, char *buf, int buf_size res = select(fd+1, &rdset, NULL, &errset, &tv); - if ( res < 0 ) { + if ( res < 0 ) + { printf("IRCReceiver::doread: select() error.\n"); return -1; - } else if ( res > 0 ) { - if (FD_ISSET(fd, &errset)) { + } + else if ( res > 0 ) + { + if (FD_ISSET(fd, &errset)) + { printf("IRCReceiver::doRead: FD_ISSET error\n"); return -1; } - if (FD_ISSET(fd, &rdset)) { + if (FD_ISSET(fd, &rdset)) + { res = ircSock->Read((unsigned char *)buf, buf_size); - if (res < 0) { + if (res < 0) + { printf("IRCReceiver::doRead: recv error\n"); return -1; - } else if (res == 0) { + } + else if (res == 0) + { printf("IRCReceiver::doRead: EOF read==0\n"); return -1; - } else + } + else return res; } @@ -81,67 +90,93 @@ void IRCReceiver::Entry() int i; int state = 0; - while (!terminateThread) { + while (!terminateThread) + { char buf[200]; int r = doRead(ircSock, buf, sizeof buf); - if (r < 0) { + if (r < 0) + { recvQ->signalEOF(); delete m; // delete unfinished IRCMessage break; } - for (i=0; i 0) { - if (b == '\n') { + if (b > 0) + { + if (b == '\n') + { recvQ->putMessage(m); m = new IRCMessage(); state = 0; - } else if (b == '\r') { + } + else if (b == '\r') + { // do nothing - } else switch (state) { + } + else switch (state) + { case 0: - if (b == ':') { + if (b == ':') + { state = 1; // prefix - } else if (b == ' ') { + } + else if (b == ' ') + { // do nothing - } else { + } + else + { m->command.push_back(b); state = 2; // command } break; case 1: - if (b == ' ') { + if (b == ' ') + { state = 2; // command is next - } else { + } + else + { m->prefix.push_back(b); } break; case 2: - if (b == ' ') { + if (b == ' ') + { state = 3; // params m->numParams = 1; m->params.push_back(""); - } else { + } + else + { m->command.push_back(b); } break; case 3: - if (b == ' ') { + if (b == ' ') + { m->numParams++; - if (m->numParams >= 15) { + if (m->numParams >= 15) + { state = 5; // ignore the rest } m->params.push_back(""); - } else if ((b == ':') && (m->params[m->numParams - 1].length() == 0)) { + } + else if ((b == ':') && (m->params[m->numParams - 1].length() == 0)) + { state = 4; // rest of line is this param - } else { + } + else + { m->params[m->numParams - 1].push_back(b); } break; diff --git a/ircddb/IRCReceiver.h b/ircddb/IRCReceiver.h index cf2ac69..1028ee3 100644 --- a/ircddb/IRCReceiver.h +++ b/ircddb/IRCReceiver.h @@ -22,5 +22,5 @@ private: bool terminateThread; int sock; IRCMessageQueue *recvQ; - std::future rec_thread; + std::future rec_thread; }; diff --git a/ircddb/IRCutils.cpp b/ircddb/IRCutils.cpp index c844c94..227583a 100644 --- a/ircddb/IRCutils.cpp +++ b/ircddb/IRCutils.cpp @@ -28,18 +28,19 @@ time_t parseTime(const std::string str) std::vector stringTokenizer(const std::string &s) { - std::stringstream ss(s); - std::istream_iterator it(ss); - std::istream_iterator end; - std::vector result(it, end); - return result; + std::stringstream ss(s); + std::istream_iterator it(ss); + std::istream_iterator end; + std::vector result(it, end); + return result; } void safeStringCopy (char *dest, const char *src, unsigned int buf_size) { unsigned int i = 0; - while (i<(buf_size - 1) && src[i] != 0) { + while (i<(buf_size - 1) && src[i] != 0) + { dest[i] = src[i]; i++; } @@ -64,7 +65,8 @@ char *getCurrentTime(void) void ToUpper(std::string &str) { - for (auto it=str.begin(); it!=str.end(); it++) { + for (auto it=str.begin(); it!=str.end(); it++) + { if (islower(*it)) *it = toupper(*it); } @@ -72,7 +74,8 @@ void ToUpper(std::string &str) void ToLower(std::string &str) { - for (auto it=str.begin(); it!=str.end(); it++) { + for (auto it=str.begin(); it!=str.end(); it++) + { if (isupper(*it)) *it = tolower(*it); } @@ -81,7 +84,8 @@ void ToLower(std::string &str) void ReplaceChar(std::string &str, char from, char to) { - for (auto it=str.begin(); it!=str.end(); it++) { + for (auto it=str.begin(); it!=str.end(); it++) + { if (from == *it) *it = to; }