diff --git a/DVAPDongle.h b/DVAPDongle.h index b4d0636..78f6fce 100644 --- a/DVAPDongle.h +++ b/DVAPDongle.h @@ -18,7 +18,7 @@ */ #include - + enum REPLY_TYPE { RT_TIMEOUT, RT_ERR, @@ -66,8 +66,8 @@ typedef struct dvp_register_tag { union { struct { unsigned char flag[3]; - unsigned char rpt1[8]; unsigned char rpt2[8]; + unsigned char rpt1[8]; unsigned char urcall[8]; unsigned char mycall[8]; unsigned char sfx[4]; diff --git a/Makefile b/Makefile index a01f0c6..6b046e8 100644 --- a/Makefile +++ b/Makefile @@ -41,11 +41,13 @@ ALL_PROGRAMS=qngateway qnlink qnremote qnvoice qnrelay qndvap qndvrptr MDV_PROGRAMS=qngateway qnlink qnremote qnvoice qnrelay DVP_PROGRAMS=qngateway qnlink qnremote qnvoice qndvap DVR_PROGRAMS=qngateway qnlink qnremote qnvoice qndvrptr +ICM_PROGRAMS=qngateway qnlink qnremote qnvoice all : $(ALL_PROGRAMS) mmdvm : $(MDV_PROGRAMS) dvap : $(DVP_PROGRAMS) dvrptr : $(DVR_PROGRAMS) +icom : $(ICM_PROGRAMS) qngateway : $(IRCOBJS) QnetGateway.o aprs.o g++ $(CPPFLAGS) -o qngateway QnetGateway.o aprs.o $(IRCOBJS) $(LDFLAGS) -pthread @@ -74,7 +76,7 @@ qnvoice : QnetVoice.o Random.o .PHONY: clean clean: - $(RM) $(OBJS) $(DEPS) $(PROGRAMS) + $(RM) $(OBJS) $(DEPS) $(ALL_PROGRAMS) -include $(DEPS) @@ -103,6 +105,25 @@ install : $(MDV_PROGRAMS) gwys.txt qn.cfg systemctl daemon-reload systemctl start qnrelay.service +installicom : $(ICM_PROGRAMS) gwys.txt qn.cfg + ######### QnetGateway ######### + /bin/cp -f qngateway $(BINDIR) + /bin/cp -f qnremote qnvoice $(BINDIR) + /bin/ln -s $(shell pwd)/qn.cfg $(CFGDIR) + /bin/cp -f system/qngateway.service $(SYSDIR) + systemctl enable qngateway.service + systemctl daemon-reload + systemctl start qngateway.service + ######### QnetLink ######### + /bin/cp -f qnlink $(BINDIR) + /bin/cp -f announce/*.dat $(CFGDIR) + /bin/ln -s $(shell pwd)/gwys.txt $(CFGDIR) + /bin/cp -f exec_?.sh $(CFGDIR) + /bin/cp -f system/qnlink.service $(SYSDIR) + systemctl enable qnlink.service + systemctl daemon-reload + systemctl start qnlink.service + installdvap : $(DVP_PROGRAMS) gwys.txt qn.cfg ######### QnetGateway ######### /bin/cp -f qngateway $(BINDIR) @@ -161,7 +182,7 @@ installdtmf : qndtmf systemctl start qndtmf.service installmmdvm : $(MMPATH)/MMDVMHost $(MMPATH)/MMDVM.qn - ( /bin/cp -f $(MMPATH)/MMDVMHost $(BINDIR) ) + /bin/cp -f $(MMPATH)/MMDVMHost $(BINDIR) /bin/ln -s $(shell pwd)/$(MMPATH)/MMDVM.qn $(CFGDIR) /bin/cp -f system/mmdvm.service $(SYSDIR) /bin/cp -f system/mmdvm.timer $(SYSDIR) @@ -208,6 +229,30 @@ uninstall : /bin/rm -f $(BINDIR)/qnrelay systemctl daemon-reload +uninstallicom : + ######### QnetGateway ######### + systemctl stop qngateway.service + systemctl disable qngateway.service + /bin/rm -f $(SYSDIR)/qngateway.service + /bin/rm -f $(BINDIR)/qngateway + /bin/rm -f $(BINDIR)/qnremote + /bin/rm -f $(BINDIR)/qnvoice + /bin/rm -f $(CFGDIR)/qn.cfg + ######### QnetLink ######### + systemctl stop qnlink.service + systemctl disable qnlink.service + /bin/rm -f $(SYSDIR)/qnlink.service + /bin/rm -f $(BINDIR)/qnlink + /bin/rm -f $(CFGDIR)/already_linked.dat + /bin/rm -f $(CFGDIR)/already_unlinked.dat + /bin/rm -f $(CFGDIR)/failed_linked.dat + /bin/rm -f $(CFGDIR)/id.dat + /bin/rm -f $(CFGDIR)/linked.dat + /bin/rm -f $(CFGDIR)/unlinked.dat + /bin/rm -f $(CFGDIR)/RPT_STATUS.txt + /bin/rm -f $(CFGDIR)/gwys.txt + /bin/rm -f $(CFGDIR)/exec_?.sh + uninstalldvap : ######### QnetGateway ######### systemctl stop qngateway.service diff --git a/QnetDVAP.cpp b/QnetDVAP.cpp index 540e97a..57c7266 100644 --- a/QnetDVAP.cpp +++ b/QnetDVAP.cpp @@ -402,12 +402,12 @@ static void readFrom20000() } /* check the module and gateway */ - if (net_buf.vpkt.hdr.r2[7] != RPTR_MOD) { + if (net_buf.vpkt.hdr.r1[7] != RPTR_MOD) { FD_CLR(insock, &readfd); break; } - memcpy(net_buf.vpkt.hdr.r1, OWNER, 7); - net_buf.vpkt.hdr.r1[7] = 'G'; + memcpy(net_buf.vpkt.hdr.r2, OWNER, 7); + net_buf.vpkt.hdr.r2[7] = 'G'; if (memcmp(RPTR, OWNER, RPTR_SIZE) != 0) { // restriction mode @@ -447,7 +447,7 @@ static void readFrom20000() net_buf.vpkt.streamid, net_buf.vpkt.hdr.flag[0], net_buf.vpkt.hdr.flag[1], net_buf.vpkt.hdr.flag[2], net_buf.vpkt.hdr.my, net_buf.vpkt.hdr.nm, net_buf.vpkt.hdr.ur, - net_buf.vpkt.hdr.r2, net_buf.vpkt.hdr.r1); + net_buf.vpkt.hdr.r1, net_buf.vpkt.hdr.r2); /* save the streamid that is winning */ streamid = net_buf.vpkt.streamid; @@ -945,7 +945,7 @@ static void ReadDVAPThread() printf("From DVAP: flags=%02x:%02x:%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s\n", dr.frame.hdr.flag[0], dr.frame.hdr.flag[1], dr.frame.hdr.flag[2], dr.frame.hdr.mycall, dr.frame.hdr.sfx, dr.frame.hdr.urcall, - dr.frame.hdr.rpt2, dr.frame.hdr.rpt1); + dr.frame.hdr.rpt1, dr.frame.hdr.rpt2); ok = true; @@ -971,12 +971,14 @@ static void ReadDVAPThread() } } - memcpy(&net_buf.vpkt.hdr, dr.frame.hdr.flag, 41); // copy the header + memcpy(&net_buf.vpkt.hdr, dr.frame.hdr.flag, 41); // copy the header, but... + memcpy(net_buf.vpkt.hdr.r1, dr.frame.hdr.rpt1, 8); // swap r1 <--> r2 + memcpy(net_buf.vpkt.hdr.r2, dr.frame.hdr.rpt2, 8); // Internet Labs DVAP Dongle Tech. Ref. V 1.01 has it backwards! /* RPT1 must always be the repeater + module */ memcpy(net_buf.vpkt.hdr.r1, RPTR_and_MOD, 8); /* copy RPT2 */ - memcpy(net_buf.vpkt.hdr.r2, dr.frame.hdr.rpt1, 8); + memcpy(net_buf.vpkt.hdr.r2, dr.frame.hdr.rpt2, 8); /* RPT2 must also be valid */ if ((net_buf.vpkt.hdr.r2[7] == 'A') || diff --git a/QnetGateway.cpp b/QnetGateway.cpp index b97a47e..517d6fc 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -64,6 +64,9 @@ extern void dstar_dv_init(); extern int dstar_dv_decode(const unsigned char *d, int data[3]); static std::atomic keep_running(true); +static std::atomic G2_COUNTER_OUT(0); +static unsigned short OLD_REPLY_SEQ = 0; +static unsigned short NEW_REPLY_SEQ = 0; /* signal catching function */ static void sigCatch(int signum) @@ -170,47 +173,47 @@ void CQnetGateway::calcPFCS(unsigned char *packet, int len) return; } -bool CQnetGateway::get_value(const Config &cfg, const char *path, int &value, int min, int max, int default_value) +bool CQnetGateway::get_value(const Config &cfg, const std::string path, int &value, int min, int max, int default_value) { if (cfg.lookupValue(path, value)) { if (value < min || value > max) value = default_value; } else value = default_value; - printf("%s = [%d]\n", path, value); + printf("%s = [%d]\n", path.c_str(), value); return true; } -bool CQnetGateway::get_value(const Config &cfg, const char *path, double &value, double min, double max, double default_value) +bool CQnetGateway::get_value(const Config &cfg, const std::string path, double &value, double min, double max, double default_value) { if (cfg.lookupValue(path, value)) { if (value < min || value > max) value = default_value; } else value = default_value; - printf("%s = [%lg]\n", path, value); + printf("%s = [%lg]\n", path.c_str(), value); return true; } -bool CQnetGateway::get_value(const Config &cfg, const char *path, bool &value, bool default_value) +bool CQnetGateway::get_value(const Config &cfg, const std::string path, bool &value, bool default_value) { if (! cfg.lookupValue(path, value)) value = default_value; - printf("%s = [%s]\n", path, value ? "true" : "false"); + printf("%s = [%s]\n", path.c_str(), value ? "true" : "false"); return true; } -bool CQnetGateway::get_value(const Config &cfg, const char *path, std::string &value, int min, int max, const char *default_value) +bool CQnetGateway::get_value(const Config &cfg, const std::string path, std::string &value, int min, int max, const char *default_value) { if (cfg.lookupValue(path, value)) { int l = value.length(); if (lmax) { - printf("%s is invalid\n", path); + printf("%s is invalid\n", path.c_str()); return false; } } else value = default_value; - printf("%s = [%s]\n", path, value.c_str()); + printf("%s = [%s]\n", path.c_str(), value.c_str()); return true; } @@ -230,8 +233,9 @@ bool CQnetGateway::read_config(char *cfgFile) printf("Parse error at %s:%d - %s\n", pex.getFile(), pex.getLine(), pex.getError()); return true; } - - if (! get_value(cfg, "ircddb.login", owner, 3, CALL_SIZE-2, "UNDEFINED")) + // ircddb + std::string path("ircddb."); + if (! get_value(cfg, path+"login", owner, 3, CALL_SIZE-2, "UNDEFINED")) return true; OWNER = owner; ToLower(owner); @@ -239,36 +243,59 @@ bool CQnetGateway::read_config(char *cfgFile) printf("OWNER=[%s]\n", OWNER.c_str()); OWNER.resize(CALL_SIZE, ' '); + if (! get_value(cfg, path+"host", ircddb.ip, 3, MAXHOSTNAMELEN, "rr.openquad.net")) + return true; + + get_value(cfg, path+"port", ircddb.port, 1000, 65535, 9007); + + if(! get_value(cfg, path+"password", irc_pass, 0, 512, "1111111111111111")) + return true; + + // modules + is_icom = is_not_icom = false; for (short int m=0; m<3; m++) { std::string path = "module."; path += m + 'a'; + path += '.'; std::string type; if (cfg.lookupValue(std::string(path+".type").c_str(), type)) { - if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm")) { - printf("module type '%s' is invalid\n", type.c_str()); - return true; - } + printf("%s = [%s]\n", std::string(path+"type").c_str(), type.c_str()); rptr.mod[m].defined = true; - if (0 == strcasecmp(type.c_str(), "dvap")) + if (0 == type.compare("icom")) { + rptr.mod[m].package_version = ICOM_VERSION; + is_icom = true; + } else if (0 == type.compare("dvap")) { rptr.mod[m].package_version = DVAP_VERSION; - else if (0 == strcasecmp(type.c_str(), "dvrptr")) + is_not_icom = true; + } else if (0 == type.compare("dvrptr")) { rptr.mod[m].package_version = DVRPTR_VERSION; - else + is_not_icom = true; + } else if (0 == type.compare("mmdvm")) { rptr.mod[m].package_version = MMDVM_VERSION; - if (! get_value(cfg, std::string(path+".ip").c_str(), rptr.mod[m].portip.ip, 7, IP_SIZE, "127.0.0.1")) + is_not_icom = true; + } else { + printf("module type '%s' is invalid\n", type.c_str()); return true; - get_value(cfg, std::string(path+".port").c_str(), rptr.mod[m].portip.port, 16000, 65535, 19998+m); - get_value(cfg, std::string(path+".frequency").c_str(), rptr.mod[m].frequency, 0.0, 1.0e12, 0.0); - get_value(cfg, std::string(path+".offset").c_str(), rptr.mod[m].offset,-1.0e12, 1.0e12, 0.0); - get_value(cfg, std::string(path+".range").c_str(), rptr.mod[m].range, 0.0, 1609344.0, 0.0); - get_value(cfg, std::string(path+".agl").c_str(), rptr.mod[m].agl, 0.0, 1000.0, 0.0); - get_value(cfg, std::string(path+".latitude").c_str(), rptr.mod[m].latitude, -90.0, 90.0, 0.0); - get_value(cfg, std::string(path+".longitude").c_str(), rptr.mod[m].longitude, -180.0, 180.0, 0.0); - if (! cfg.lookupValue(path+".desc1", rptr.mod[m].desc1)) + } + if (is_icom && is_not_icom) { + printf("cannot define both icom and non-icom modules\n"); + return true; + } + + if (! get_value(cfg, std::string(path+"ip").c_str(), rptr.mod[m].portip.ip, 7, IP_SIZE, is_icom ? "172.16.0.20" : "127.0.0.1")) + return true; + get_value(cfg, std::string(path+"port").c_str(), rptr.mod[m].portip.port, 16000, 65535, is_icom ? 20000 : 19998+m); + get_value(cfg, std::string(path+"frequency").c_str(), rptr.mod[m].frequency, 0.0, 1.0e12, 0.0); + get_value(cfg, std::string(path+"offset").c_str(), rptr.mod[m].offset,-1.0e12, 1.0e12, 0.0); + get_value(cfg, std::string(path+"range").c_str(), rptr.mod[m].range, 0.0, 1609344.0, 0.0); + get_value(cfg, std::string(path+"agl").c_str(), rptr.mod[m].agl, 0.0, 1000.0, 0.0); + get_value(cfg, std::string(path+"latitude").c_str(), rptr.mod[m].latitude, -90.0, 90.0, 0.0); + get_value(cfg, std::string(path+"longitude").c_str(), rptr.mod[m].longitude, -180.0, 180.0, 0.0); + if (! cfg.lookupValue(path+"desc1", rptr.mod[m].desc1)) rptr.mod[m].desc1 = ""; - if (! cfg.lookupValue(path+".desc2", rptr.mod[m].desc2)) + if (! cfg.lookupValue(path+"desc2", rptr.mod[m].desc2)) rptr.mod[m].desc2 = ""; - if (! get_value(cfg, std::string(path+".url").c_str(), rptr.mod[m].url, 0, 80, "github.com/n7tae/QnetGateway")) + if (! get_value(cfg, std::string(path+"url").c_str(), rptr.mod[m].url, 0, 80, "github.com/n7tae/QnetGateway")) return true; // truncate strings if (rptr.mod[m].desc1.length() > 20) @@ -282,79 +309,108 @@ bool CQnetGateway::read_config(char *cfgFile) } else rptr.mod[m].defined = false; } - if (false==rptr.mod[0].defined && false==rptr.mod[1].defined && false==rptr.mod[2].defined) { - printf("No repeaters defined!\n"); + if (! is_icom && ! is_not_icom) { + printf("No modules defined!\n"); return true; + } else if (is_icom) { // make sure all ICOM modules have the same IP and port number + std::string addr; + int port; + for (int i=0; i<3; i++) { + if (rptr.mod[i].defined) { + if (addr.size()) { + if (addr.compare(rptr.mod[i].portip.ip) || port!=rptr.mod[i].portip.port) { + printf("all defined ICOM modules must have the same IP and port number!\n"); + return true; + } + } else { + addr = rptr.mod[i].portip.ip; + port = rptr.mod[i].portip.port; + } + } + } + for (int i=0; i<3; i++) { + if (! rptr.mod[i].defined) { + rptr.mod[i].portip.ip = addr; + rptr.mod[i].portip.port = port; + } + } } - if (! get_value(cfg, "file.status", status_file, 1, FILENAME_MAX, "/usr/local/etc/RPTR_STATUS.txt")) + // gateway + path = "gateway."; + if (! get_value(cfg, path+"local_irc_ip", local_irc_ip, 7, IP_SIZE, "0.0.0.0")) return true; - if (! get_value(cfg, "gateway.local_irc_ip", local_irc_ip, 7, IP_SIZE, "0.0.0.0")) - return true; - - get_value(cfg, "gateway.send_qrgs_maps", bool_send_qrgs, true); - - if (! get_value(cfg, "aprs.host", rptr.aprs.ip, 7, MAXHOSTNAMELEN, "rotate.aprs.net")) + if (! get_value(cfg, path+"external.ip", g2_external.ip, 7, IP_SIZE, "0.0.0.0")) return true; - get_value(cfg, "aprs.port", rptr.aprs.port, 10000, 65535, 14580); + get_value(cfg, path+"external.port", g2_external.port, 1024, 65535, 40000); - get_value(cfg, "aprs.interval", rptr.aprs_interval, 40, 1000, 40); - - if (! get_value(cfg, "aprs.filter", rptr.aprs_filter, 0, 512, "")) + if (! get_value(cfg, path+"internal.ip", g2_internal.ip, 7, IP_SIZE, is_icom ? "172.16.0.1" : "0.0.0.0")) return true; - if (! get_value(cfg, "gateway.external.ip", g2_external.ip, 7, IP_SIZE, "0.0.0.0")) - return true; + get_value(cfg, path+"internal.port", g2_internal.port, 16000, 65535, is_icom ? 20000 : 19000); - get_value(cfg, "gateway.external.port", g2_external.port, 1024, 65535, 40000); + get_value(cfg, path+"regen_header", bool_regen_header, true); - if (! get_value(cfg, "gateway.internal.ip", g2_internal.ip, 7, IP_SIZE, "0.0.0.0")) - return true; + get_value(cfg, path+"aprs_send", bool_send_aprs, true); - get_value(cfg, "gateway.internal.port", g2_internal.port, 16000, 65535, 19000); + get_value(cfg, path+"send_qrgs_maps", bool_send_qrgs, true); - if (! get_value(cfg, "link.outgoing_ip", g2_link.ip, 7, IP_SIZE, "127.0.0.1")) + // APRS + path = "aprs."; + if (! get_value(cfg, path+"host", rptr.aprs.ip, 7, MAXHOSTNAMELEN, "rotate.aprs.net")) return true; - get_value(cfg, "link.port", g2_link.port, 16000, 65535, 18997); + get_value(cfg, path+"port", rptr.aprs.port, 10000, 65535, 14580); - get_value(cfg, "log.qso", bool_qso_details, false); + get_value(cfg, path+"interval", rptr.aprs_interval, 40, 1000, 40); - get_value(cfg, "log.irc", bool_irc_debug, false); + if (! get_value(cfg, path+"filter", rptr.aprs_filter, 0, 512, "")) + return true; - get_value(cfg, "log.dtmf", bool_dtmf_debug, false); + // log + path = "log."; + get_value(cfg, path+"qso", bool_qso_details, false); - get_value(cfg, "gateway.regen_header", bool_regen_header, true); + get_value(cfg, path+"irc", bool_irc_debug, false); - get_value(cfg, "gateway.aprs_send", bool_send_aprs, true); + get_value(cfg, path+"dtmf", bool_dtmf_debug, false); + if (! get_value(cfg, "link.outgoing_ip", g2_link.ip, 7, IP_SIZE, "127.0.0.1")) + return true; - if (! get_value(cfg, "file.echotest", echotest_dir, 2, FILENAME_MAX, "/tmp")) + // file + path = "file."; + if (! get_value(cfg, path+"echotest", echotest_dir, 2, FILENAME_MAX, "/tmp")) return true; - get_value(cfg, "timing.play.wait", play_wait, 1, 10, 2); + if (! get_value(cfg, path+"dtmf", dtmf_dir, 2,FILENAME_MAX, "/tmp")) + return true; - get_value(cfg, "timing.play.delay", play_delay, 9, 25, 19); + if (! get_value(cfg, path+"status", status_file, 1, FILENAME_MAX, "/usr/local/etc/RPTR_STATUS.txt")) + return true; - get_value(cfg, "timing.timeeout.echo", echotest_rec_timeout, 1, 10, 1); + // link + path = "link."; + get_value(cfg, path+"port", g2_link.port, 16000, 65535, 18997); - get_value(cfg, "timing.timeout.voicemail", voicemail_rec_timeout, 1, 10, 1); + if (! get_value(cfg, path+"ip", g2_link.ip, 7, 15, "127.0.0.1")) + return true; - get_value(cfg, "timing.timeout.remote_g2", from_remote_g2_timeout, 1, 10, 2); + // timing + path = "timing.play."; + get_value(cfg, path+"wait", play_wait, 1, 10, 2); - get_value(cfg, "timing.timeout.local_rptr", from_local_rptr_timeout, 1, 10, 1); + get_value(cfg, path+"delay", play_delay, 9, 25, 19); - if (! get_value(cfg, "ircddb.host", ircddb.ip, 3, MAXHOSTNAMELEN, "rr.openquad.net")) - return true; + path = "timing.timeout."; + get_value(cfg, path+"echo", echotest_rec_timeout, 1, 10, 1); - get_value(cfg, "ircddb.port", ircddb.port, 1000, 65535, 9007); + get_value(cfg, path+"voicemail", voicemail_rec_timeout, 1, 10, 1); - if(! get_value(cfg, "ircddb.password", irc_pass, 0, 512, "1111111111111111")) - return true; + get_value(cfg, path+"remote_g2", from_remote_g2_timeout, 1, 10, 2); - if (! get_value(cfg, "file.dtmf", dtmf_dir, 2,FILENAME_MAX, "/tmp")) - return true; + get_value(cfg, path+"local_rptr", from_local_rptr_timeout, 1, 10, 1); return false; } @@ -670,6 +726,35 @@ void CQnetGateway::process() ii->kickWatchdog(IRCDDB_VERSION); + if (is_icom) { + // send INIT to Icom Stack + unsigned char buf[500]; + memset(buf, 0, 10); + memcpy(buf, "INIT", 4); + buf[6] = 0x73U; + // we can use the module a band_addr for INIT + sendto(srv_sock, buf, 10, 0, (struct sockaddr *)&toRptr[0].band_addr, sizeof(struct sockaddr_in)); + printf("Waiting for ICOM controller...\n"); + + // get the acknowledgement from the ICOM Stack + while (keep_running) { + socklen_t fromlength = sizeof(struct sockaddr_in); + int recvlen = recvfrom(srv_sock, buf, 500, 0, (struct sockaddr *)&fromRptr, &fromlength); + if (10==recvlen && 0==memcmp(buf, "INIT", 4) && 0x72U==buf[6] && 0x0U==buf[7]) { + OLD_REPLY_SEQ = 256U * buf[4] + buf[5]; + NEW_REPLY_SEQ = OLD_REPLY_SEQ + 1; + G2_COUNTER_OUT = NEW_REPLY_SEQ; + unsigned int ui = G2_COUNTER_OUT; + printf("SYNC: old=%u, new=%u out=%u\n", OLD_REPLY_SEQ, NEW_REPLY_SEQ, ui); + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + printf("Detected ICOM controller!\n"); + } else + printf("Skipping ICOM initialization\n"); + + while (keep_running) { for (int i=0; i<3; i++) { /* echotest recording timed out? */ @@ -724,7 +809,7 @@ void CQnetGateway::process() // Send end_of_audio to local repeater. // Let the repeater re-initialize - end_of_audio.counter = toRptr[i].G2_COUNTER; + end_of_audio.counter = is_icom ? G2_COUNTER_OUT++ :toRptr[i].G2_COUNTER++; if (i == 0) end_of_audio.vpkt.snd_term_id = 0x03; else if (i == 1) @@ -737,7 +822,6 @@ void CQnetGateway::process() for (int j=0; j<2; j++) sendto(srv_sock, end_of_audio.pkt_id, 29, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - toRptr[i].G2_COUNTER++; toRptr[i].streamid = 0; toRptr[i].adr = 0; @@ -778,8 +862,7 @@ void CQnetGateway::process() if (to_remote_g2[i].toDst4.sin_addr.s_addr != 0) { time(&t_now); if ((t_now - to_remote_g2[i].last_time) > from_local_rptr_timeout) { - printf("Inactivity from local rptr mod %d, removing stream id %04x\n", - i, to_remote_g2[i].streamid); + printf("Inactivity from local rptr mod %d, removing stream id %04x\n", i, to_remote_g2[i].streamid); memset(&(to_remote_g2[i].toDst4),0,sizeof(struct sockaddr_in)); to_remote_g2[i].streamid = 0; @@ -814,10 +897,8 @@ void CQnetGateway::process() } - if ( ((g2buflen == 56) || (g2buflen == 27)) && - (0==memcmp(g2buf.title, "DSVT", 4)) && - ((g2buf.config == 0x10) || (g2buf.config == 0x20)) && /* header or voiceframe */ - (g2buf.id == 0x20)) { /* voice type */ + if ( (g2buflen==56 || g2buflen==27) && 0==memcmp(g2buf.title, "DSVT", 4) && + (g2buf.config==0x10 || g2buf.config==0x20) && g2buf.id==0x20) { if (g2buflen == 56) { // Find out the local repeater module IP/port to send the data to @@ -835,22 +916,36 @@ void CQnetGateway::process() (g2buf.hdr.flag[0] == 0x28) || (g2buf.hdr.flag[0] == 0x40))) { if (bool_qso_details) - printf("START from g2: streamID=%04x, flags=%02x:%02x:%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes fromIP=%s and port %u\n", - g2buf.streamid, - g2buf.hdr.flag[0], g2buf.hdr.flag[1], g2buf.hdr.flag[2], - g2buf.hdr.mycall, - g2buf.hdr.sfx, g2buf.hdr.urcall, - g2buf.hdr.rpt1, g2buf.hdr.rpt2, - g2buflen, inet_ntoa(fromDst4.sin_addr), ntohs(fromDst4.sin_port)); + printf("id=%04x G2 start, ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s IP=%s:%u\n", + ntohs(g2buf.streamid), g2buf.hdr.mycall, g2buf.hdr.rpt1, g2buf.hdr.rpt2, + g2buf.hdr.urcall, g2buf.hdr.sfx, inet_ntoa(fromDst4.sin_addr), ntohs(fromDst4.sin_port)); memcpy(rptrbuf.pkt_id, "DSTR", 4); - rptrbuf.counter = toRptr[i].G2_COUNTER; + rptrbuf.counter = htons(is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++); // bump the counter rptrbuf.flag[0] = 0x73; rptrbuf.flag[1] = 0x12; rptrbuf.flag[2] = 0x00; rptrbuf.remaining = 0x30; rptrbuf.vpkt.icm_id = 0x20; - memcpy(&rptrbuf.vpkt.dst_rptr_id, g2buf.flagb, 47); + //memcpy(&rptrbuf.vpkt.dst_rptr_id, g2buf.flagb, 47); + rptrbuf.vpkt.dst_rptr_id = g2buf.flagb[0]; + rptrbuf.vpkt.snd_rptr_id = g2buf.flagb[1]; + rptrbuf.vpkt.snd_term_id = g2buf.flagb[2]; + rptrbuf.vpkt.streamid = g2buf.streamid; + rptrbuf.vpkt.ctrl = g2buf.ctrl; + memcpy(rptrbuf.vpkt.hdr.flag, g2buf.hdr.flag, 3); + if (is_icom) { + memcpy(rptrbuf.vpkt.hdr.r1, g2buf.hdr.rpt2, 8); + memcpy(rptrbuf.vpkt.hdr.r2, g2buf.hdr.rpt1, 8); + } else { + memcpy(rptrbuf.vpkt.hdr.r1, g2buf.hdr.rpt1, 8); + memcpy(rptrbuf.vpkt.hdr.r2, g2buf.hdr.rpt2, 8); + } + memcpy(rptrbuf.vpkt.hdr.ur, g2buf.hdr.urcall, 8); + memcpy(rptrbuf.vpkt.hdr.my, g2buf.hdr.mycall, 8); + memcpy(rptrbuf.vpkt.hdr.nm, g2buf.hdr.sfx, 4); + memcpy(rptrbuf.vpkt.hdr.pfcs, g2buf.hdr.pfcs, 2); + sendto(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); /* save the header */ @@ -864,26 +959,20 @@ void CQnetGateway::process() /* time it, in case stream times out */ time(&toRptr[i].last_time); - /* bump the G2 counter */ - toRptr[i].G2_COUNTER++; - toRptr[i].sequence = rptrbuf.vpkt.ctrl; } } - } else { - if (g2buf.counter & 0x40) { - if (bool_qso_details) - printf("END from g2: streamID=%04x, %d bytes from IP=%s\n", g2buf.streamid, g2buflen,inet_ntoa(fromDst4.sin_addr)); - } + } else { // g2buflen == 27 + if (bool_qso_details && g2buf.ctrl & 0x40) + printf("id=%04x END G2\n", ntohs(g2buf.streamid)); /* find out which repeater module to send the data to */ int i; for (i=0; i<3; i++) { /* streamid match ? */ - if ((toRptr[i].streamid==g2buf.streamid) && - (toRptr[i].adr == fromDst4.sin_addr.s_addr)) { + if (toRptr[i].streamid==g2buf.streamid && toRptr[i].adr==fromDst4.sin_addr.s_addr) { memcpy(rptrbuf.pkt_id, "DSTR", 4); - rptrbuf.counter = toRptr[i].G2_COUNTER; + rptrbuf.counter = htons(is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++); rptrbuf.flag[0] = 0x73; rptrbuf.flag[1] = 0x12; rptrbuf.flag[2] = 0x00; @@ -896,13 +985,10 @@ void CQnetGateway::process() /* timeit */ time(&toRptr[i].last_time); - /* bump G2 counter */ - toRptr[i].G2_COUNTER++; - toRptr[i].sequence = rptrbuf.vpkt.ctrl; /* End of stream ? */ - if (g2buf.counter & 0x40) { + if (g2buf.ctrl & 0x40) { /* clear the saved header */ memset(toRptr[i].saved_hdr, 0, sizeof(toRptr[i].saved_hdr)); toRptr[i].saved_adr = 0; @@ -919,7 +1005,7 @@ void CQnetGateway::process() if ((i == 3) && bool_regen_header) { /* check if this a continuation of audio that timed out */ - if (g2buf.counter & 0x40) + if (g2buf.ctrl & 0x40) ; /* we do not care about end-of-QSO */ else { /* for which repeater this stream has timed out ? */ @@ -927,21 +1013,18 @@ void CQnetGateway::process() /* match saved stream ? */ if (0==memcmp(toRptr[i].saved_hdr + 14, &g2buf.streamid, 2) && toRptr[i].saved_adr==fromDst4.sin_addr.s_addr) { /* 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", g2buf.streamid); - toRptr[i].saved_hdr[5] = (unsigned char)(toRptr[i].G2_COUNTER & 0xff); - toRptr[i].saved_hdr[4] = (unsigned char)((toRptr[i].G2_COUNTER >> 8) & 0xff); + toRptr[i].saved_hdr[4] = (unsigned char)(((is_icom ? G2_COUNTER_OUT : toRptr[i].G2_COUNTER) >> 8) & 0xff); + toRptr[i].saved_hdr[5] = (unsigned char)((is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++) & 0xff); /* re-generate/send the header */ sendto(srv_sock, toRptr[i].saved_hdr, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - /* bump G2 counter */ - toRptr[i].G2_COUNTER++; - /* send this audio packet to repeater */ memcpy(rptrbuf.pkt_id, "DSTR", 4); - rptrbuf.counter = toRptr[i].G2_COUNTER; + rptrbuf.counter = htons(is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++); rptrbuf.flag[0] = 0x73; rptrbuf.flag[1] = 0x12; rptrbuf.flag[2] = 0x00; @@ -958,9 +1041,6 @@ void CQnetGateway::process() /* time it, in case stream times out */ time(&toRptr[i].last_time); - /* bump the G2 counter */ - toRptr[i].G2_COUNTER++; - toRptr[i].sequence = rptrbuf.vpkt.ctrl; } @@ -979,787 +1059,707 @@ void CQnetGateway::process() socklen_t fromlen = sizeof(struct sockaddr_in); int recvlen = recvfrom(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&fromRptr, &fromlen); - /* DV */ - if ( ((recvlen == 58) || (recvlen == 29) || (recvlen == 32)) && - (rptrbuf.flag[0] == 0x73) && (rptrbuf.flag[1] == 0x12) && - (0 == memcmp(rptrbuf.pkt_id,"DSTR", 4)) && - (rptrbuf.vpkt.icm_id == 0x20) && (rptrbuf.flag[2] == 0x00) && - ((rptrbuf.remaining == 0x30) || /* 48 bytes follow */ - (rptrbuf.remaining == 0x13) || /* 19 bytes follow */ - (rptrbuf.remaining == 0x16)) ) { /* 22 bytes follow */ + if (0 == memcmp(rptrbuf.pkt_id, "DSTR", 4)) { + ///////////////////////////////////////////////////////////////////// + // some ICOM handshaking... + if (is_icom && 10==recvlen && 0x72==rptrbuf.flag[0]) { // ACK from rptr + NEW_REPLY_SEQ = ntohs(rptrbuf.counter); + if (NEW_REPLY_SEQ == OLD_REPLY_SEQ) { + G2_COUNTER_OUT = NEW_REPLY_SEQ; + OLD_REPLY_SEQ = NEW_REPLY_SEQ - 1; + } else + OLD_REPLY_SEQ = NEW_REPLY_SEQ; + } else if (is_icom && 0x73U==rptrbuf.flag[0] && (0x21U==rptrbuf.flag[1] || 0x11U==rptrbuf.flag[1] || 0x0U==rptrbuf.flag[1])) { + rptrbuf.flag[0] = 0x72U; + memset(rptrbuf.flag+1, 0x0U, 3); + sendto(srv_sock, rptrbuf.pkt_id, 10, 0, (struct sockaddr *)&toRptr[0].band_addr, sizeof(struct sockaddr_in)); + // end of ICOM handshaking + ///////////////////////////////////////////////////////////////////// + } else if ( (recvlen==58 || recvlen==29 || recvlen==32) && + rptrbuf.flag[0]==0x73 && rptrbuf.flag[1]==0x12 && rptrbuf.flag[2]==0x0 && rptrbuf.vpkt.icm_id==0x20 && + (rptrbuf.remaining==0x30 || rptrbuf.remaining==0x13 || rptrbuf.remaining==0x16) ) { + if (is_icom) { // acknowledge packet to ICOM + SDSTR reply; + memcpy(reply.pkt_id, "DSTR", 4); + reply.counter = rptrbuf.counter; + reply.flag[0] = 0x72U; + memset(reply.flag+1, 0, 3); + sendto(srv_sock, reply.pkt_id, 10, 0, (struct sockaddr *)&toRptr[0].band_addr, sizeof(struct sockaddr_in)); + } - if (recvlen == 58) { + if (recvlen == 58) { - if (bool_qso_details) - printf("START from rptr: cntr=%04x, streamID=%04x, flags=%02x:%02x:%02x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes fromIP=%s\n", - rptrbuf.counter, - rptrbuf.vpkt.streamid, - rptrbuf.vpkt.hdr.flag[0], rptrbuf.vpkt.hdr.flag[1], rptrbuf.vpkt.hdr.flag[2], - rptrbuf.vpkt.hdr.my, rptrbuf.vpkt.hdr.nm, rptrbuf.vpkt.hdr.ur, - rptrbuf.vpkt.hdr.r1, rptrbuf.vpkt.hdr.r2, recvlen, inet_ntoa(fromRptr.sin_addr)); + if (bool_qso_details) + printf("id=%04x cntr=%04x start RPTR ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s ip=%s\n", + ntohs(rptrbuf.vpkt.streamid), ntohs(rptrbuf.counter), rptrbuf.vpkt.hdr.ur, rptrbuf.vpkt.hdr.r1, + rptrbuf.vpkt.hdr.r2, rptrbuf.vpkt.hdr.my, rptrbuf.vpkt.hdr.nm, inet_ntoa(fromRptr.sin_addr)); - if ((memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) == 0) && /* rpt1 is this repeater */ - /*** (memcmp(rptrbuf + 44, OWNER, 7) != 0) && ***/ /* MYCALL is NOT this repeater */ - ((rptrbuf.vpkt.hdr.flag[0] == 0x00) || /* normal */ - (rptrbuf.vpkt.hdr.flag[0] == 0x08) || /* EMR */ - (rptrbuf.vpkt.hdr.flag[0] == 0x20) || /* BREAK */ - (rptrbuf.vpkt.hdr.flag[0] == 0x28))) { /* EMR + BREAK */ + if (0==memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) && // rpt1 is this repeater + (rptrbuf.vpkt.hdr.flag[0]==0x00 || // normal + rptrbuf.vpkt.hdr.flag[0]==0x08 || // EMR + rptrbuf.vpkt.hdr.flag[0]==0x20 || // BREAK + rptrbuf.vpkt.hdr.flag[0]==0x28)) { // EMR + BREAK (0x1, announcements are not allowed) - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - if (i>=0 && i<3) { - if (bool_dtmf_debug) - printf("resetting dtmf[%d] (got a header)\n", i); - dtmf_last_frame[i] = 0; - dtmf_counter[i] = 0; - memset(dtmf_buf[i], 0, sizeof(dtmf_buf[i])); - dtmf_buf_count[i] = 0; + if (i>=0 && i<3) { + if (bool_dtmf_debug) + printf("resetting dtmf[%d] (got a header)\n", i); + dtmf_last_frame[i] = 0; + dtmf_counter[i] = 0; + memset(dtmf_buf[i], 0, sizeof(dtmf_buf[i])); + dtmf_buf_count[i] = 0; - /* Initialize the LAST HEARD data for the band */ + /* Initialize the LAST HEARD data for the band */ - band_txt[i].streamID = rptrbuf.vpkt.streamid; + band_txt[i].streamID = rptrbuf.vpkt.streamid; - memcpy(band_txt[i].flags, rptrbuf.vpkt.hdr.flag, 3); + memcpy(band_txt[i].flags, rptrbuf.vpkt.hdr.flag, 3); - memcpy(band_txt[i].lh_mycall, rptrbuf.vpkt.hdr.my, 8); - band_txt[i].lh_mycall[8] = '\0'; + memcpy(band_txt[i].lh_mycall, rptrbuf.vpkt.hdr.my, 8); + band_txt[i].lh_mycall[8] = '\0'; - memcpy(band_txt[i].lh_sfx, rptrbuf.vpkt.hdr.nm, 4); - band_txt[i].lh_sfx[4] = '\0'; + memcpy(band_txt[i].lh_sfx, rptrbuf.vpkt.hdr.nm, 4); + band_txt[i].lh_sfx[4] = '\0'; - memcpy(band_txt[i].lh_yrcall, rptrbuf.vpkt.hdr.ur, 8); - band_txt[i].lh_yrcall[8] = '\0'; + memcpy(band_txt[i].lh_yrcall, rptrbuf.vpkt.hdr.ur, 8); + band_txt[i].lh_yrcall[8] = '\0'; - memcpy(band_txt[i].lh_rpt1, rptrbuf.vpkt.hdr.r1, 8); - band_txt[i].lh_rpt1[8] = '\0'; + memcpy(band_txt[i].lh_rpt1, rptrbuf.vpkt.hdr.r1, 8); + band_txt[i].lh_rpt1[8] = '\0'; - memcpy(band_txt[i].lh_rpt2, rptrbuf.vpkt.hdr.r2, 8); - band_txt[i].lh_rpt2[8] = '\0'; + memcpy(band_txt[i].lh_rpt2, rptrbuf.vpkt.hdr.r2, 8); + band_txt[i].lh_rpt2[8] = '\0'; - time(&band_txt[i].last_time); + time(&band_txt[i].last_time); - band_txt[i].txt[0] = '\0'; - band_txt[i].txt_cnt = 0; - band_txt[i].txt_stats_sent = false; + band_txt[i].txt[0] = '\0'; + band_txt[i].txt_cnt = 0; + band_txt[i].txt_stats_sent = false; - band_txt[i].dest_rptr[0] = '\0'; + band_txt[i].dest_rptr[0] = '\0'; - /* try to process GPS mode: GPRMC and ID */ - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - band_txt[i].gprmc[0] = '\0'; - band_txt[i].gpid[0] = '\0'; - band_txt[i].is_gps_sent = false; - // band_txt[i].gps_last_time = 0; DO NOT reset it + /* try to process GPS mode: GPRMC and ID */ + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + band_txt[i].gprmc[0] = '\0'; + band_txt[i].gpid[0] = '\0'; + band_txt[i].is_gps_sent = false; + // band_txt[i].gps_last_time = 0; DO NOT reset it - new_group[i] = true; - to_print[i] = 0; - ABC_grp[i] = false; + new_group[i] = true; + to_print[i] = 0; + ABC_grp[i] = false; - band_txt[i].num_dv_frames = 0; - band_txt[i].num_dv_silent_frames = 0; - band_txt[i].num_bit_errors = 0; + band_txt[i].num_dv_frames = 0; + band_txt[i].num_dv_silent_frames = 0; + band_txt[i].num_bit_errors = 0; - /* select the band for aprs processing, and lock on the stream ID */ - if (bool_send_aprs) - aprs->SelectBand(i, rptrbuf.vpkt.streamid); + /* select the band for aprs processing, and lock on the stream ID */ + if (bool_send_aprs) + aprs->SelectBand(i, ntohs(rptrbuf.vpkt.streamid)); + } } - } - /* Is MYCALL valid ? */ - memset(temp_radio_user, ' ', 8); - memcpy(temp_radio_user, rptrbuf.vpkt.hdr.my, 8); - temp_radio_user[8] = '\0'; + /* Is MYCALL valid ? */ + memset(temp_radio_user, ' ', 8); + memcpy(temp_radio_user, rptrbuf.vpkt.hdr.my, 8); + temp_radio_user[8] = '\0'; - int mycall_valid = regexec(&preg, temp_radio_user, 0, NULL, 0); + int mycall_valid = regexec(&preg, temp_radio_user, 0, NULL, 0); - if (mycall_valid == REG_NOERROR) - ; // printf("MYCALL [%s] passed IRC expression validation\n", temp_radio_user); - else { - if (mycall_valid == REG_NOMATCH) - printf("MYCALL [%s] failed IRC expression validation\n", temp_radio_user); - else - printf("Failed to validate MYCALL [%s], regexec error=%d\n", temp_radio_user, mycall_valid); - } - - /* send data qnlink */ - if (mycall_valid == REG_NOERROR) - sendto(srv_sock, rptrbuf.pkt_id, recvlen, 0, (struct sockaddr *)&plug, sizeof(struct sockaddr_in)); + if (mycall_valid == REG_NOERROR) + ; // printf("MYCALL [%s] passed IRC expression validation\n", temp_radio_user); + else { + if (mycall_valid == REG_NOMATCH) + printf("MYCALL [%s] failed IRC expression validation\n", temp_radio_user); + else + printf("Failed to validate MYCALL [%s], regexec error=%d\n", temp_radio_user, mycall_valid); + } - if ((mycall_valid == REG_NOERROR) && - (memcmp(rptrbuf.vpkt.hdr.ur, "XRF", 3) != 0) && /* not a reflector */ - (memcmp(rptrbuf.vpkt.hdr.ur, "REF", 3) != 0) && /* not a reflector */ - (memcmp(rptrbuf.vpkt.hdr.ur, "DCS", 3) != 0) && /* not a reflector */ - (rptrbuf.vpkt.hdr.ur[0] != ' ') && /* must have something */ - (memcmp(rptrbuf.vpkt.hdr.ur, "CQCQCQ", 6) != 0)) { /* urcall is NOT CQCQCQ */ - if ((rptrbuf.vpkt.hdr.ur[0] == '/') && /* urcall starts with a slash */ - (memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) == 0) && /* rpt1 is this repeater */ - ((rptrbuf.vpkt.hdr.r1[7] == 'A') || - (rptrbuf.vpkt.hdr.r1[7] == 'B') || - (rptrbuf.vpkt.hdr.r1[7] == 'C')) && /* mod is A,B,C */ - (memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7) == 0) && /* rpt2 is this repeater */ - (rptrbuf.vpkt.hdr.r2[7] == 'G') && /* local Gateway */ - /*** (memcmp(rptrbuf + 44, OWNER, 7) != 0) && ***/ /* mycall is NOT this repeater */ - - ((rptrbuf.vpkt.hdr.flag[0] == 0x00) || /* normal */ - (rptrbuf.vpkt.hdr.flag[0] == 0x08) || /* EMR */ - (rptrbuf.vpkt.hdr.flag[0] == 0x20) || /* BK */ - (rptrbuf.vpkt.hdr.flag[0] == 0x28)) /* EMR + BK */ - ) { - if (memcmp(rptrbuf.vpkt.hdr.ur+1, OWNER.c_str(), 6) != 0) { /* the value after the slash in urcall, is NOT this repeater */ - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - - if (i>=0 && i<3) { - /* one radio user on a repeater module at a time */ - if (to_remote_g2[i].toDst4.sin_addr.s_addr == 0) { - /* YRCALL=/repeater + mod */ - /* YRCALL=/KJ4NHFB */ - - memset(temp_radio_user, ' ', 8); - memcpy(temp_radio_user, rptrbuf.vpkt.hdr.ur+1, 6); - temp_radio_user[6] = ' '; - temp_radio_user[7] = rptrbuf.vpkt.hdr.ur[7]; - if (temp_radio_user[7] == ' ') - temp_radio_user[7] = 'A'; - temp_radio_user[CALL_SIZE] = '\0'; - - bool result = get_yrcall_rptr(temp_radio_user, arearp_cs, zonerp_cs, &temp_mod, ip, 'R'); - if (result) { /* it is a repeater */ - uint32_t address; - /* set the destination */ - to_remote_g2[i].streamid = rptrbuf.vpkt.streamid; - memset(&to_remote_g2[i].toDst4, 0, sizeof(struct sockaddr_in)); - to_remote_g2[i].toDst4.sin_family = AF_INET; - to_remote_g2[i].toDst4.sin_addr.s_addr = address = inet_addr(ip); - // if the address is in the portmap, we'll use that saved port instead of the default port - auto theAddress = portmap.find(address); - to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end()) ? g2_external.port : theAddress->second); - - memcpy(g2buf.title, "DSVT", 4); - g2buf.config = 0x10; - g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0x00; - g2buf.id = rptrbuf.vpkt.icm_id; - g2buf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; - g2buf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; - g2buf.flagb[2] = rptrbuf.vpkt.snd_term_id; - memcpy(&g2buf.streamid, &rptrbuf.vpkt.streamid, 44); - /* set rpt1 */ - memset(g2buf.hdr.rpt1, ' ', 8); - memcpy(g2buf.hdr.rpt1, arearp_cs, strlen(arearp_cs)); - g2buf.hdr.rpt1[7] = temp_mod; - /* set rpt2 */ - memset(g2buf.hdr.rpt2, ' ', 8); - memcpy(g2buf.hdr.rpt2, zonerp_cs, strlen(zonerp_cs)); - g2buf.hdr.rpt2[7] = 'G'; - /* set yrcall, can NOT let it be slash and repeater + module */ - memcpy(g2buf.hdr.urcall, "CQCQCQ ", 8); - - /* set PFCS */ - calcPFCS(g2buf.title, 56); - - // The remote repeater has been set, lets fill in the dest_rptr - // so that later we can send that to the LIVE web site - memcpy(band_txt[i].dest_rptr, g2buf.hdr.rpt1, 8); - band_txt[i].dest_rptr[CALL_SIZE] = '\0'; - - // send to remote gateway - for (int j=0; j<5; j++) - sendto(g2_sock, g2buf.title, 56, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); - - printf("Routing to IP=%s port=%u, streamID=%04x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes\n", - inet_ntoa(to_remote_g2[i].toDst4.sin_addr), ntohs(to_remote_g2[i].toDst4.sin_port), - g2buf.streamid, g2buf.hdr.mycall, - g2buf.hdr.sfx, g2buf.hdr.urcall, - g2buf.hdr.rpt1, g2buf.hdr.rpt2, - 56); - - time(&(to_remote_g2[i].last_time)); - } - } - } - } - } else if ((memcmp(rptrbuf.vpkt.hdr.ur, OWNER.c_str(), 7) != 0) && /* urcall is not this repeater */ - (memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) == 0) && /* rpt1 is this repeater */ - ((rptrbuf.vpkt.hdr.r1[7] == 'A') || - (rptrbuf.vpkt.hdr.r1[7] == 'B') || - (rptrbuf.vpkt.hdr.r1[7] == 'C')) && /* mod is A,B,C */ - (memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7) == 0) && /* rpt2 is this repeater */ - (rptrbuf.vpkt.hdr.r2[7] == 'G') && /* local Gateway */ - /*** (memcmp(rptrbuf + 44, OWNER, 7) != 0) && ***/ /* mycall is NOT this repeater */ - - ((rptrbuf.vpkt.hdr.flag[0] == 0x00) || /* normal */ - (rptrbuf.vpkt.hdr.flag[0] == 0x08) || /* EMR */ - (rptrbuf.vpkt.hdr.flag[0] == 0x20) || /* BK */ - (rptrbuf.vpkt.hdr.flag[0] == 0x28)) /* EMR + BK */ - ) { - - memset(temp_radio_user, ' ', 8); - memcpy(temp_radio_user, rptrbuf.vpkt.hdr.ur, 8); - temp_radio_user[8] = '\0'; - bool result = get_yrcall_rptr(temp_radio_user, arearp_cs, zonerp_cs, &temp_mod, ip, 'U'); - if (result) { - /* destination is a remote system */ - if (memcmp(zonerp_cs, OWNER.c_str(), 7) != 0) { + /* send data qnlink */ + if (mycall_valid == REG_NOERROR) + sendto(srv_sock, rptrbuf.pkt_id, recvlen, 0, (struct sockaddr *)&plug, sizeof(struct sockaddr_in)); + + if (mycall_valid==REG_NOERROR && + memcmp(rptrbuf.vpkt.hdr.ur, "XRF", 3) && // not a reflector + memcmp(rptrbuf.vpkt.hdr.ur, "REF", 3) && + memcmp(rptrbuf.vpkt.hdr.ur, "DCS", 3) && + rptrbuf.vpkt.hdr.ur[0]!=' ' && // must have something + memcmp(rptrbuf.vpkt.hdr.ur, "CQCQCQ", 6)) { // urcall is NOT CQCQCQ + if (rptrbuf.vpkt.hdr.ur[0]=='/' && // repeater routing! + 0==memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) && // rpt1 this repeater + (rptrbuf.vpkt.hdr.r1[7]>='A' && rptrbuf.vpkt.hdr.r1[7]<='C') && // with a valid module + 0==memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7) && // rpt2 is this repeater + rptrbuf.vpkt.hdr.r2[7]=='G' && // local Gateway + (rptrbuf.vpkt.hdr.flag[0]== 0x00 || // normal + rptrbuf.vpkt.hdr.flag[0]== 0x08 || // EMR + rptrbuf.vpkt.hdr.flag[0]== 0x20 || // BK + rptrbuf.vpkt.hdr.flag[0]== 0x28)) {// EMR + BK + + if (memcmp(rptrbuf.vpkt.hdr.ur+1, OWNER.c_str(), 6)) { // the value after the slash is NOT this repeater int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; if (i>=0 && i<3) { /* one radio user on a repeater module at a time */ if (to_remote_g2[i].toDst4.sin_addr.s_addr == 0) { - uint32_t address; - /* set the destination */ - to_remote_g2[i].streamid = rptrbuf.vpkt.streamid; - memset(&to_remote_g2[i].toDst4, 0, sizeof(struct sockaddr_in)); - to_remote_g2[i].toDst4.sin_family = AF_INET; - to_remote_g2[i].toDst4.sin_addr.s_addr = address = inet_addr(ip); - // if the address is in the portmap, we'll use that port instead of the default - auto theAddress = portmap.find(address); - to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end())? g2_external.port : theAddress->second); - - memcpy(g2buf.title, "DSVT", 4); - g2buf.config = 0x10; - g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0x00; - g2buf.id = rptrbuf.vpkt.icm_id; - g2buf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; - g2buf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; - g2buf.flagb[2] = rptrbuf.vpkt.snd_term_id; - memcpy(&g2buf.streamid, &rptrbuf.vpkt.streamid, 44); - /* set rpt1 */ - memset(g2buf.hdr.rpt1, ' ', 8); - memcpy(g2buf.hdr.rpt1, arearp_cs, strlen(arearp_cs)); - g2buf.hdr.rpt1[7] = temp_mod; - /* set rpt2 */ - memset(g2buf.hdr.rpt2, ' ', 8); - memcpy(g2buf.hdr.rpt2, zonerp_cs, strlen(zonerp_cs)); - g2buf.hdr.rpt2[7] = 'G'; - /* set PFCS */ - calcPFCS(g2buf.title, 56); - - // The remote repeater has been set, lets fill in the dest_rptr - // so that later we can send that to the LIVE web site - memcpy(band_txt[i].dest_rptr, g2buf.hdr.rpt1, 8); - band_txt[i].dest_rptr[CALL_SIZE] = '\0'; - - /* send to remote gateway */ - for (int j=0; j<5; j++) - sendto(g2_sock, g2buf.title, 56, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); - - printf("Routing to IP=%s, port=%u, streamID=%04x, my=%.8s, sfx=%.4s, ur=%.8s, rpt1=%.8s, rpt2=%.8s, %d bytes\n", - inet_ntoa(to_remote_g2[i].toDst4.sin_addr), ntohs(to_remote_g2[i].toDst4.sin_port), - g2buf.streamid, g2buf.hdr.mycall, - g2buf.hdr.sfx, g2buf.hdr.urcall, - g2buf.hdr.rpt1, g2buf.hdr.rpt2, - 56); - - time(&(to_remote_g2[i].last_time)); + /* YRCALL=/repeater + mod */ + /* YRCALL=/KJ4NHFB */ + + memset(temp_radio_user, ' ', 8); + memcpy(temp_radio_user, rptrbuf.vpkt.hdr.ur+1, 6); + temp_radio_user[6] = ' '; + temp_radio_user[7] = rptrbuf.vpkt.hdr.ur[7]; + if (temp_radio_user[7] == ' ') + temp_radio_user[7] = 'A'; + temp_radio_user[CALL_SIZE] = '\0'; + + bool result = get_yrcall_rptr(temp_radio_user, arearp_cs, zonerp_cs, &temp_mod, ip, 'R'); + if (result) { /* it is a repeater */ + uint32_t address; + /* set the destination */ + to_remote_g2[i].streamid = rptrbuf.vpkt.streamid; + memset(&to_remote_g2[i].toDst4, 0, sizeof(struct sockaddr_in)); + to_remote_g2[i].toDst4.sin_family = AF_INET; + to_remote_g2[i].toDst4.sin_addr.s_addr = address = inet_addr(ip); + // if the address is in the portmap, we'll use that saved port instead of the default port + auto theAddress = portmap.find(address); + to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end()) ? g2_external.port : theAddress->second); + + memcpy(g2buf.title, "DSVT", 4); + g2buf.config = 0x10; + g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0x00; + g2buf.id = rptrbuf.vpkt.icm_id; + g2buf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; + g2buf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; + g2buf.flagb[2] = rptrbuf.vpkt.snd_term_id; + g2buf.streamid = rptrbuf.vpkt.streamid; + g2buf.ctrl = rptrbuf.vpkt.ctrl; + memcpy(g2buf.hdr.flag, rptrbuf.vpkt.hdr.flag, 3); + /* set rpt1 */ + memset(g2buf.hdr.rpt1, ' ', 8); + memcpy(g2buf.hdr.rpt1, arearp_cs, strlen(arearp_cs)); + g2buf.hdr.rpt1[7] = temp_mod; + /* set rpt2 */ + memset(g2buf.hdr.rpt2, ' ', 8); + memcpy(g2buf.hdr.rpt2, zonerp_cs, strlen(zonerp_cs)); + g2buf.hdr.rpt2[7] = 'G'; + /* set yrcall, can NOT let it be slash and repeater + module */ + memcpy(g2buf.hdr.urcall, "CQCQCQ ", 8); + memcpy(g2buf.hdr.mycall, rptrbuf.vpkt.hdr.my, 8); + memcpy(g2buf.hdr.sfx, rptrbuf.vpkt.hdr.nm, 4); + + /* set PFCS */ + calcPFCS(g2buf.title, 56); + + // The remote repeater has been set, lets fill in the dest_rptr + // so that later we can send that to the LIVE web site + memcpy(band_txt[i].dest_rptr, g2buf.hdr.rpt1, 8); + band_txt[i].dest_rptr[CALL_SIZE] = '\0'; + + // send to remote gateway + for (int j=0; j<5; j++) + sendto(g2_sock, g2buf.title, 56, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); + + printf("id=%04x Routing to IP=%s:%u ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", + ntohs(g2buf.streamid), inet_ntoa(to_remote_g2[i].toDst4.sin_addr), ntohs(to_remote_g2[i].toDst4.sin_port), + g2buf.hdr.urcall, g2buf.hdr.rpt1, g2buf.hdr.rpt2, g2buf.hdr.mycall, g2buf.hdr.sfx); + + time(&(to_remote_g2[i].last_time)); + } } } - } else { - int i = rptrbuf.vpkt.hdr.r1[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 (temp_mod != rptrbuf.vpkt.hdr.r1[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 - */ - memcpy(band_txt[i].dest_rptr, rptrbuf.vpkt.hdr.r2, 8); - band_txt[i].dest_rptr[7] = temp_mod; - band_txt[i].dest_rptr[8] = '\0'; - - i = temp_mod - 'A'; + } + } else if (memcmp(rptrbuf.vpkt.hdr.ur, OWNER.c_str(), 7) && // urcall is not this repeater + 0==memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) && // rpt1 is this repeater + (rptrbuf.vpkt.hdr.r1[7]>='A'&& rptrbuf.vpkt.hdr.r1[7]<='C') && // mod is A,B,C + 0==memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7) && // rpt2 is this repeater + rptrbuf.vpkt.hdr.r2[7]=='G' && // local Gateway + + (rptrbuf.vpkt.hdr.flag[0]==0x00 || // normal + rptrbuf.vpkt.hdr.flag[0]==0x08 || // EMR + rptrbuf.vpkt.hdr.flag[0]==0x20 || // BK + rptrbuf.vpkt.hdr.flag[0]==0x28)) { // EMR + BK + + + memset(temp_radio_user, ' ', 8); + memcpy(temp_radio_user, rptrbuf.vpkt.hdr.ur, 8); + temp_radio_user[8] = '\0'; + bool result = get_yrcall_rptr(temp_radio_user, arearp_cs, zonerp_cs, &temp_mod, ip, 'U'); + if (result) { + /* destination is a remote system */ + if (memcmp(zonerp_cs, OWNER.c_str(), 7) != 0) { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; + + if (i>=0 && i<3) { + /* one radio user on a repeater module at a time */ + if (to_remote_g2[i].toDst4.sin_addr.s_addr == 0) { + uint32_t address; + /* set the destination */ + to_remote_g2[i].streamid = rptrbuf.vpkt.streamid; + memset(&to_remote_g2[i].toDst4, 0, sizeof(struct sockaddr_in)); + to_remote_g2[i].toDst4.sin_family = AF_INET; + to_remote_g2[i].toDst4.sin_addr.s_addr = address = inet_addr(ip); + // if the address is in the portmap, we'll use that port instead of the default + auto theAddress = portmap.find(address); + to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end())? g2_external.port : theAddress->second); + + memcpy(g2buf.title, "DSVT", 4); + g2buf.config = 0x10; + g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0x00; + g2buf.id = rptrbuf.vpkt.icm_id; + g2buf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; + g2buf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; + g2buf.flagb[2] = rptrbuf.vpkt.snd_term_id; + g2buf.streamid = rptrbuf.vpkt.streamid; + g2buf.ctrl = rptrbuf.vpkt.ctrl; + memcpy(g2buf.hdr.flag, rptrbuf.vpkt.hdr.flag, 3); + /* set rpt1 */ + memset(g2buf.hdr.rpt1, ' ', 8); + memcpy(g2buf.hdr.rpt1, arearp_cs, strlen(arearp_cs)); + g2buf.hdr.rpt1[7] = temp_mod; + /* set rpt2 */ + memset(g2buf.hdr.rpt2, ' ', 8); + memcpy(g2buf.hdr.rpt2, zonerp_cs, strlen(zonerp_cs)); + g2buf.hdr.rpt2[7] = 'G'; + /* set PFCS */ + memcpy(g2buf.hdr.urcall, rptrbuf.vpkt.hdr.ur, 8); + memcpy(g2buf.hdr.mycall, rptrbuf.vpkt.hdr.my, 8); + memcpy(g2buf.hdr.sfx, rptrbuf.vpkt.hdr.nm, 4); + calcPFCS(g2buf.title, 56); + + + // The remote repeater has been set, lets fill in the dest_rptr + // so that later we can send that to the LIVE web site + memcpy(band_txt[i].dest_rptr, g2buf.hdr.rpt1, 8); + band_txt[i].dest_rptr[CALL_SIZE] = '\0'; + + /* send to remote gateway */ + for (int j=0; j<5; j++) + sendto(g2_sock, g2buf.title, 56, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); + + printf("Routing to IP=%s:%u id=%04x my=%.8s/%.4s ur=%.8s rpt1=%.8s rpt2=%.8s\n", + inet_ntoa(to_remote_g2[i].toDst4.sin_addr), ntohs(to_remote_g2[i].toDst4.sin_port), + g2buf.streamid, g2buf.hdr.mycall, g2buf.hdr.sfx, g2buf.hdr.urcall, g2buf.hdr.rpt1, g2buf.hdr.rpt2); + + time(&(to_remote_g2[i].last_time)); + } + } + } else { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - /* valid destination repeater module? */ - if (i>=0 && i<3) { + 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 (temp_mod != rptrbuf.vpkt.hdr.r1[7]) { /* - toRptr[i] : receiving from a remote system or cross-band - band_txt[i] : local RF is talking. + The remote repeater has been set, lets fill in the dest_rptr + so that later we can send that to the LIVE web site */ - if ((toRptr[i].last_time == 0) && (band_txt[i].last_time == 0)) { - printf("CALLmode cross-banding from mod %c to %c\n", rptrbuf.vpkt.hdr.r1[7], temp_mod); + memcpy(band_txt[i].dest_rptr, rptrbuf.vpkt.hdr.r2, 8); + band_txt[i].dest_rptr[7] = temp_mod; + band_txt[i].dest_rptr[8] = '\0'; - rptrbuf.vpkt.hdr.r2[7] = temp_mod; - rptrbuf.vpkt.hdr.r1[7] = 'G'; - calcPFCS(rptrbuf.pkt_id, 58); + i = temp_mod - 'A'; - sendto(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); + /* 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", rptrbuf.vpkt.hdr.r1[7], temp_mod); - /* This is the active streamid */ - toRptr[i].streamid = rptrbuf.vpkt.streamid; - toRptr[i].adr = fromRptr.sin_addr.s_addr; + rptrbuf.vpkt.hdr.r2[7] = temp_mod; + rptrbuf.vpkt.hdr.r1[7] = 'G'; + calcPFCS(rptrbuf.pkt_id, 58); - /* time it, in case stream times out */ - time(&toRptr[i].last_time); + sendto(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - /* bump the G2 counter */ - toRptr[i].G2_COUNTER++; + /* This is the active streamid */ + toRptr[i].streamid = rptrbuf.vpkt.streamid; + toRptr[i].adr = fromRptr.sin_addr.s_addr; - toRptr[i].sequence = rptrbuf.vpkt.ctrl; + /* time it, in case stream times out */ + time(&toRptr[i].last_time); + + /* bump the G2 counter */ + if (is_icom) + G2_COUNTER_OUT++; + else + toRptr[i].G2_COUNTER++; + + toRptr[i].sequence = rptrbuf.vpkt.ctrl; + } } - } - } else - printf("icom rule: no routing from %.8s to %s%c\n", rptrbuf.vpkt.hdr.r1, arearp_cs, temp_mod); + } else + printf("icom rule: no routing from %.8s to %s%c\n", rptrbuf.vpkt.hdr.r1, arearp_cs, temp_mod); + } } } } - } - } else if ((rptrbuf.vpkt.hdr.ur[7] == '0') && - (rptrbuf.vpkt.hdr.ur[6] == 'C') && - (rptrbuf.vpkt.hdr.ur[0] == ' ')) { - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - - if (i>=0 && i<3) { - /* voicemail file is closed */ - 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 - printf("No voicemail to clear or still recording\n"); - } - } else if ((rptrbuf.vpkt.hdr.ur[7] == '0') && - (rptrbuf.vpkt.hdr.ur[6] == 'R') && - (rptrbuf.vpkt.hdr.ur[0] == ' ')) { - int i = -1; - switch (rptrbuf.vpkt.hdr.r1[7]) { - case 'A': - i = 0; - break; - case 'B': - i = 1; - break; - case 'C': - i = 2; - break; - } - - if (i >= 0) { - /* voicemail file is closed */ - if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) { - try { - std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, vm[i].file); - } catch (const std::exception &e) { - printf("Filed to start voicemail playback. Exception: %s\n", e.what()); - } - } else - printf("No voicemail to recall or still recording\n"); - } - } else if ((rptrbuf.vpkt.hdr.ur[7] == '0') && - (rptrbuf.vpkt.hdr.ur[6] == 'S') && - (rptrbuf.vpkt.hdr.ur[0] == ' ')) { - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - - if (i>=0 && i<3) { - if (vm[i].fd >= 0) - printf("Already recording for voicemail on mod %d\n", i); - else { - memset(tempfile, '\0', sizeof(tempfile)); - snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", - echotest_dir.c_str(), - rptrbuf.vpkt.hdr.r1[7], - "voicemail.dat"); - - 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 { - strcpy(vm[i].file, tempfile); - printf("Recording mod %c for voicemail into file:[%s]\n", - rptrbuf.vpkt.hdr.r1[7], - vm[i].file); + } else if (0 == memcmp(rptrbuf.vpkt.hdr.ur, " C0", 8)) { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; + + if (i>=0 && i<3) { + /* voicemail file is closed */ + 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 + printf("No voicemail to clear or still recording\n"); + } + } else if (0 == memcmp(rptrbuf.vpkt.hdr.ur, " R0", 8)) { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - time(&vm[i].last_time); - vm[i].streamid = rptrbuf.vpkt.streamid; + if (i>=0 && i<3) { + /* voicemail file is closed */ + if ((vm[i].fd == -1) && (vm[i].file[0] != '\0')) { + try { + std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, vm[i].file); + } catch (const std::exception &e) { + printf("Failed to start voicemail playback. Exception: %s\n", e.what()); + } + } else + printf("No voicemail to recall or still recording\n"); + } + } else if (0 == memcmp(rptrbuf.vpkt.hdr.ur, " S0", 8)) { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - memcpy(recbuf.title, "DSVT", 4); - recbuf.config = 0x10; - recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; - recbuf.id = rptrbuf.vpkt.icm_id; - recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; - recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; - recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; - memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 44); - memset(recbuf.hdr.rpt1, ' ', 8); - memcpy(recbuf.hdr.rpt1, OWNER.c_str(), OWNER.length()); - recbuf.hdr.rpt1[7] = rptrbuf.vpkt.hdr.r1[7]; - memset(recbuf.hdr.rpt2, ' ', 8); - memcpy(recbuf.hdr.rpt2, OWNER.c_str(), OWNER.length()); - recbuf.hdr.rpt2[7] = 'G'; - memcpy(recbuf.hdr.urcall, "CQCQCQ ", 8); - - calcPFCS(recbuf.title, 56); - - rec_len = 56; - (void)write(vm[i].fd, "DVTOOL", 6); - (void)write(vm[i].fd, &num_recs, 4); - (void)write(vm[i].fd, &rec_len, 2); - (void)write(vm[i].fd, &recbuf, rec_len); + if (i>=0 && i<3) { + if (vm[i].fd >= 0) + printf("Already recording for voicemail on mod %d\n", i); + else { + memset(tempfile, '\0', sizeof(tempfile)); + snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", echotest_dir.c_str(), rptrbuf.vpkt.hdr. r1[7], "voicemail.dat"); + + 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 { + strcpy(vm[i].file, tempfile); + printf("Recording mod %c for voicemail into file:[%s]\n", rptrbuf.vpkt.hdr.r1[7], vm[i].file); + + time(&vm[i].last_time); + vm[i].streamid = rptrbuf.vpkt.streamid; + + memcpy(recbuf.title, "DSVT", 4); + recbuf.config = 0x10; + recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; + recbuf.id = rptrbuf.vpkt.icm_id; + recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; + recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; + recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; + memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 44); + memset(recbuf.hdr.rpt1, ' ', 8); + memcpy(recbuf.hdr.rpt1, OWNER.c_str(), OWNER.size()); + recbuf.hdr.rpt1[7] = rptrbuf.vpkt.hdr.r1[7]; + memset(recbuf.hdr.rpt2, ' ', 8); + memcpy(recbuf.hdr.rpt2, OWNER.c_str(), OWNER.size()); + recbuf.hdr.rpt2[7] = 'G'; + memcpy(recbuf.hdr.urcall, "CQCQCQ ", 8); + + calcPFCS(recbuf.title, 56); + + rec_len = 56; + (void)write(vm[i].fd, "DVTOOL", 6); + (void)write(vm[i].fd, &num_recs, 4); + (void)write(vm[i].fd, &rec_len, 2); + (void)write(vm[i].fd, &recbuf, rec_len); + } } } - } - } else if (('E' == rptrbuf.vpkt.hdr.ur[7]) && (' ' == rptrbuf.vpkt.hdr.ur[0])) { - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - - if (i>=0 && i<3) { - if (recd[i].fd >= 0) - printf("Already recording for echotest on mod %d\n", i); - else { - memset(tempfile, '\0', sizeof(tempfile)); - snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", echotest_dir.c_str(), - rptrbuf.vpkt.hdr.r1[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 { - strcpy(recd[i].file, tempfile); - printf("Recording mod %c for echotest into file:[%s]\n", - rptrbuf.vpkt.hdr.r1[7], recd[i].file); - - time(&recd[i].last_time); - recd[i].streamid = rptrbuf.vpkt.streamid; + } else if (0 == memcmp(rptrbuf.vpkt.hdr.ur, " E", 8)) { + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - memcpy(recbuf.title, "DSVT", 4); - recbuf.config = 0x10; - recbuf.id = rptrbuf.vpkt.icm_id; - recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; - recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; - recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; - recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; - memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 44); - memset(recbuf.hdr.rpt1, ' ', 8); - memcpy(recbuf.hdr.rpt1, OWNER.c_str(), OWNER.length()); - recbuf.hdr.rpt1[7] = rptrbuf.vpkt.hdr.r1[7]; - memset(recbuf.hdr.rpt2, ' ', 8); - memcpy(recbuf.hdr.rpt2, OWNER.c_str(), OWNER.length()); - recbuf.hdr.rpt2[7] = 'G'; - memcpy(recbuf.hdr.urcall, "CQCQCQ ", 8); - - calcPFCS(recbuf.title, 56); - - rec_len = 56; - (void)write(recd[i].fd, "DVTOOL", 6); - (void)write(recd[i].fd, &num_recs, 4); - (void)write(recd[i].fd, &rec_len, 2); - (void)write(recd[i].fd, &recbuf, rec_len); + if (i>=0 && i<3) { + if (recd[i].fd >= 0) + printf("Already recording for echotest on mod %d\n", i); + else { + memset(tempfile, '\0', sizeof(tempfile)); + snprintf(tempfile, FILENAME_MAX, "%s/%c_%s", echotest_dir.c_str(), rptrbuf.vpkt.hdr.r1[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 { + strcpy(recd[i].file, tempfile); + printf("Recording mod %c for echotest into file:[%s]\n", rptrbuf.vpkt.hdr.r1[7], recd[i].file); + + time(&recd[i].last_time); + recd[i].streamid = rptrbuf.vpkt.streamid; + + memcpy(recbuf.title, "DSVT", 4); + recbuf.config = 0x10; + recbuf.id = rptrbuf.vpkt.icm_id; + recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; + recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; + recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; + recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; + memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 44); + memset(recbuf.hdr.rpt1, ' ', 8); + memcpy(recbuf.hdr.rpt1, OWNER.c_str(), OWNER.length()); + recbuf.hdr.rpt1[7] = rptrbuf.vpkt.hdr.r1[7]; + memset(recbuf.hdr.rpt2, ' ', 8); + memcpy(recbuf.hdr.rpt2, OWNER.c_str(), OWNER.length()); + recbuf.hdr.rpt2[7] = 'G'; + memcpy(recbuf.hdr.urcall, "CQCQCQ ", 8); + + calcPFCS(recbuf.title, 56); + + rec_len = 56; + (void)write(recd[i].fd, "DVTOOL", 6); + (void)write(recd[i].fd, &num_recs, 4); + (void)write(recd[i].fd, &rec_len, 2); + (void)write(recd[i].fd, &recbuf, rec_len); + } } } - } - /* check for cross-banding */ - } else if (0 == (memcmp(rptrbuf.vpkt.hdr.ur, "CQCQCQ", 6)) && /* yrcall is CQCQCQ */ - (0 == memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7)) && /* rpt1 is this repeater */ - (0 == memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7)) && /* rpt2 is this repeater */ - ((rptrbuf.vpkt.hdr.r1[7] == 'A') || - (rptrbuf.vpkt.hdr.r1[7] == 'B') || - (rptrbuf.vpkt.hdr.r1[7] == 'C')) && /* mod of rpt1 is A,B,C */ - ((rptrbuf.vpkt.hdr.r2[7] == 'A') || - (rptrbuf.vpkt.hdr.r2[7] == 'B') || - (rptrbuf.vpkt.hdr.r2[7] == 'C')) && /* !!! usually a G of rpt2, but we see A,B,C */ - (rptrbuf.vpkt.hdr.r2[7] != rptrbuf.vpkt.hdr.r1[7])) { /* cross-banding? make sure NOT the same */ - int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; - - 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 - memcpy(band_txt[i].dest_rptr, rptrbuf.vpkt.hdr.r2, 8); - band_txt[i].dest_rptr[8] = '\0'; - } - - i = rptrbuf.vpkt.hdr.r2[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("ZONEmode cross-banding from mod %c to %c\n", rptrbuf.vpkt.hdr.r1[7], rptrbuf.vpkt.hdr.r2[7]); + /* check for cross-banding */ + } else if ( 0==memcmp(rptrbuf.vpkt.hdr.ur, "CQCQCQ", 6) && // yrcall is CQCQCQ + 0==memcmp(rptrbuf.vpkt.hdr.r2, OWNER.c_str(), 7) && // rpt1 is this repeater + 0==memcmp(rptrbuf.vpkt.hdr.r1, OWNER.c_str(), 7) && // rpt2 is this repeater + (rptrbuf.vpkt.hdr.r1[7]>='A' && rptrbuf.vpkt.hdr.r1[7]<='C') && // mod of rpt1 is A,B,C + (rptrbuf.vpkt.hdr.r2[7]>='A' && rptrbuf.vpkt.hdr.r2[7]<='C') && // !!! usually G on rpt2, but we see A,B,C with + rptrbuf.vpkt.hdr.r2[7]!=rptrbuf.vpkt.hdr.r1[7] ) { // cross-banding? make sure NOT the same + int i = rptrbuf.vpkt.hdr.r1[7] - 'A'; + + 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 + memcpy(band_txt[i].dest_rptr, rptrbuf.vpkt.hdr.r2, 8); + band_txt[i].dest_rptr[8] = '\0'; + } - rptrbuf.vpkt.hdr.r1[7] = 'G'; - calcPFCS(rptrbuf.pkt_id, 58); + i = rptrbuf.vpkt.hdr.r2[7] - 'A'; - sendto(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); + // 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("ZONEmode cross-banding from mod %c to %c\n", rptrbuf.vpkt.hdr.r1[7], rptrbuf.vpkt.hdr.r2[7]); - /* This is the active streamid */ - toRptr[i].streamid = rptrbuf.vpkt.streamid; - toRptr[i].adr = fromRptr.sin_addr.s_addr; + rptrbuf.vpkt.hdr.r1[7] = 'G'; + calcPFCS(rptrbuf.pkt_id, 58); - /* time it, in case stream times out */ - time(&toRptr[i].last_time); + sendto(srv_sock, rptrbuf.pkt_id, 58, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - /* bump the G2 counter */ - toRptr[i].G2_COUNTER ++; + /* This is the active streamid */ + toRptr[i].streamid = rptrbuf.vpkt.streamid; + toRptr[i].adr = fromRptr.sin_addr.s_addr; - toRptr[i].sequence = rptrbuf.vpkt.ctrl; - } - } - } - } else { // recvlen != 58 - for (int i=0; i<3; i++) { - if (band_txt[i].streamID == rptrbuf.vpkt.streamid) { - time(&band_txt[i].last_time); - - if ((rptrbuf.vpkt.ctrl & 0x40) != 0) { // end of voice data - if (dtmf_buf_count[i] > 0) { - dtmf_file = dtmf_dir; - dtmf_file.push_back('/'); - dtmf_file.push_back('A'+i); - dtmf_file += "_mod_DTMF_NOTIFY"; - if (bool_dtmf_debug) - 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) { - fprintf(dtmf_fp, "%s\n%s", dtmf_buf[i], band_txt[i].lh_mycall); - fclose(dtmf_fp); - } else - printf("Failed to create dtmf file %s\n", dtmf_file.c_str()); + /* time it, in case stream times out */ + time(&toRptr[i].last_time); + /* bump the G2 counter */ + if (is_icom) + G2_COUNTER_OUT++; + else + toRptr[i].G2_COUNTER ++; - if (bool_dtmf_debug) - printf("resetting dtmf[%d] (printed dtmf code %s from %s)\n", i, dtmf_buf[i], band_txt[i].lh_mycall); - memset(dtmf_buf[i], 0, sizeof(dtmf_buf[i])); - dtmf_buf_count[i] = 0; - dtmf_counter[i] = 0; - dtmf_last_frame[i] = 0; + toRptr[i].sequence = rptrbuf.vpkt.ctrl; } - - ii->sendHeardWithTXStats(band_txt[i].lh_mycall, - band_txt[i].lh_sfx, - //(strstr(band_txt[i].lh_yrcall,"REF") == NULL)?band_txt[i].lh_yrcall:"CQCQCQ ", - band_txt[i].lh_yrcall, - band_txt[i].lh_rpt1, - band_txt[i].lh_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); - - band_txt[i].streamID = 0; - band_txt[i].flags[0] = band_txt[i].flags[1] = band_txt[i].flags[2] = 0; - band_txt[i].lh_mycall[0] = '\0'; - band_txt[i].lh_sfx[0] = '\0'; - band_txt[i].lh_yrcall[0] = '\0'; - band_txt[i].lh_rpt1[0] = '\0'; - band_txt[i].lh_rpt2[0] = '\0'; - - band_txt[i].last_time = 0; - - band_txt[i].txt[0] = '\0'; - band_txt[i].txt_cnt = 0; - - band_txt[i].dest_rptr[0] = '\0'; - - band_txt[i].num_dv_frames = 0; - band_txt[i].num_dv_silent_frames = 0; - band_txt[i].num_bit_errors = 0; - - } else { // not the end of the voice stream - int ber_data[3]; - int ber_errs = dstar_dv_decode(rptrbuf.vpkt.vasd.voice, ber_data); - if (ber_data[0] == 0xf85) - band_txt[i].num_dv_silent_frames++; - band_txt[i].num_bit_errors += ber_errs; - band_txt[i].num_dv_frames++; - - if ((ber_data[0] & 0x0ffc) == 0xfc0) { - dtmf_digit = (ber_data[0] & 0x03) | ((ber_data[2] & 0x60) >> 3); - 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) { - const char *dtmf_chars = "147*2580369#ABCD"; - dtmf_buf[i][ dtmf_buf_count[i] ] = dtmf_chars[dtmf_digit]; - dtmf_buf_count[i]++; - } - } - const unsigned char silence[9] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8 }; - memcpy(rptrbuf.vpkt.vasd.voice, silence, 9); - } else - dtmf_counter[i] = 0; } - break; } - } - - if (recvlen == 29) - memcpy(tmp_txt, rptrbuf.vpkt.vasd.text, 3); - else - memcpy(tmp_txt, rptrbuf.vpkt.vasd1.text, 3); - - // printf("%x%x%x\n", tmp_txt[0], tmp_txt[1], tmp_txt[2]); - // printf("%c%c%c\n", tmp_txt[0] ^ 0x70, tmp_txt[1] ^ 0x4f, tmp_txt[2] ^ 0x93); - - /* extract 20-byte RADIO ID */ - if ((tmp_txt[0] != 0x55) || (tmp_txt[1] != 0x2d) || (tmp_txt[2] != 0x16)) { - // printf("%x%x%x\n", tmp_txt[0], tmp_txt[1], tmp_txt[2]); - // printf("%c%c%c\n", tmp_txt[0] ^ 0x70, tmp_txt[1] ^ 0x4f, tmp_txt[2] ^ 0x93); - + } else { // recvlen is 29 or 32 for (int i=0; i<3; i++) { if (band_txt[i].streamID == rptrbuf.vpkt.streamid) { - if (new_group[i]) { - tmp_txt[0] = tmp_txt[0] ^ 0x70; - header_type = tmp_txt[0] & 0xf0; + time(&band_txt[i].last_time); + + if (rptrbuf.vpkt.ctrl & 0x40) { // end of voice data + if (dtmf_buf_count[i] > 0) { + dtmf_file = dtmf_dir; + dtmf_file.push_back('/'); + dtmf_file.push_back('A'+i); + dtmf_file += "_mod_DTMF_NOTIFY"; + if (bool_dtmf_debug) + 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) { + fprintf(dtmf_fp, "%s\n%s", dtmf_buf[i], band_txt[i].lh_mycall); + fclose(dtmf_fp); + } else + printf("Failed to create dtmf file %s\n", dtmf_file.c_str()); - if ((header_type == 0x50) || /* header */ - (header_type == 0xc0)) { /* squelch */ - new_group[i] = false; - to_print[i] = 0; - ABC_grp[i] = false; - } else if (header_type == 0x30) { /* GPS or GPS id or APRS */ - new_group[i] = false; - to_print[i] = tmp_txt[0] & 0x0f; - ABC_grp[i] = false; - if (to_print[i] > 5) - to_print[i] = 5; - else if (to_print[i] < 1) - to_print[i] = 1; - if ((to_print[i] > 1) && (to_print[i] <= 5)) { - /* something went wrong? all bets are off */ - if (band_txt[i].temp_line_cnt > 200) { - printf("Reached the limit in the OLD gps mode\n"); - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } + if (bool_dtmf_debug) + printf("resetting dtmf[%d] (printed dtmf code %s from %s)\n", i, dtmf_buf[i], band_txt[i].lh_mycall); + memset(dtmf_buf[i], 0, sizeof(dtmf_buf[i])); + dtmf_buf_count[i] = 0; + dtmf_counter[i] = 0; + dtmf_last_frame[i] = 0; + } - /* fresh GPS string, re-initialize */ - if ((to_print[i] == 5) && ((tmp_txt[1] ^ 0x4f) == '$')) { - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; + ii->sendHeardWithTXStats(band_txt[i].lh_mycall, band_txt[i].lh_sfx, band_txt[i].lh_yrcall, band_txt[i].lh_rpt1, + band_txt[i].lh_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); + + band_txt[i].streamID = 0; + band_txt[i].flags[0] = band_txt[i].flags[1] = band_txt[i].flags[2] = 0; + band_txt[i].lh_mycall[0] = '\0'; + band_txt[i].lh_sfx[0] = '\0'; + band_txt[i].lh_yrcall[0] = '\0'; + band_txt[i].lh_rpt1[0] = '\0'; + band_txt[i].lh_rpt2[0] = '\0'; + + band_txt[i].last_time = 0; + + band_txt[i].txt[0] = '\0'; + band_txt[i].txt_cnt = 0; + + band_txt[i].dest_rptr[0] = '\0'; + + band_txt[i].num_dv_frames = 0; + band_txt[i].num_dv_silent_frames = 0; + band_txt[i].num_bit_errors = 0; + + } else { // not the end of the voice stream + int ber_data[3]; + int ber_errs = dstar_dv_decode(rptrbuf.vpkt.vasd.voice, ber_data); + if (ber_data[0] == 0xf85) + band_txt[i].num_dv_silent_frames++; + band_txt[i].num_bit_errors += ber_errs; + band_txt[i].num_dv_frames++; + + if ((ber_data[0] & 0x0ffc) == 0xfc0) { + dtmf_digit = (ber_data[0] & 0x03) | ((ber_data[2] & 0x60) >> 3); + 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) { + const char *dtmf_chars = "147*2580369#ABCD"; + dtmf_buf[i][ dtmf_buf_count[i] ] = dtmf_chars[dtmf_digit]; + dtmf_buf_count[i]++; } + } + const unsigned char silence[9] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8 }; + memcpy(rptrbuf.vpkt.vasd.voice, silence, 9); + } else + dtmf_counter[i] = 0; + } + break; + } + } - /* do not copy CR, NL */ - if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; - band_txt[i].temp_line_cnt++; - } - if (((tmp_txt[2] ^ 0x93) != '\r') && ((tmp_txt[2] ^ 0x93) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[2] ^ 0x93; - band_txt[i].temp_line_cnt++; - } + if (recvlen == 29) + memcpy(tmp_txt, rptrbuf.vpkt.vasd.text, 3); + else + memcpy(tmp_txt, rptrbuf.vpkt.vasd1.text, 3); + + // extract 20-byte RADIO ID + if ((tmp_txt[0] != 0x55) || (tmp_txt[1] != 0x2d) || (tmp_txt[2] != 0x16)) { + + for (int i=0; i<3; i++) { + if (band_txt[i].streamID == rptrbuf.vpkt.streamid) { + if (new_group[i]) { + tmp_txt[0] = tmp_txt[0] ^ 0x70; + header_type = tmp_txt[0] & 0xf0; + + if ((header_type == 0x50) || /* header */ (header_type == 0xc0)) { /* squelch */ + new_group[i] = false; + to_print[i] = 0; + ABC_grp[i] = false; + } else if (header_type == 0x30) { /* GPS or GPS id or APRS */ + new_group[i] = false; + to_print[i] = tmp_txt[0] & 0x0f; + ABC_grp[i] = false; + if (to_print[i] > 5) + to_print[i] = 5; + else if (to_print[i] < 1) + to_print[i] = 1; + + if ((to_print[i] > 1) && (to_print[i] <= 5)) { + /* something went wrong? all bets are off */ + if (band_txt[i].temp_line_cnt > 200) { + printf("Reached the limit in the OLD gps mode\n"); + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } - if (((tmp_txt[1] ^ 0x4f) == '\r') || ((tmp_txt[2] ^ 0x93) == '\r')) { - if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { - memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; - } else if (band_txt[i].temp_line[0] != '$') { - memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; - if (bool_send_aprs && !band_txt[i].is_gps_sent) - gps_send(i); + /* fresh GPS string, re-initialize */ + if ((to_print[i] == 5) && ((tmp_txt[1] ^ 0x4f) == '$')) { + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; } - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } else if (((tmp_txt[1] ^ 0x4f) == '\n') || ((tmp_txt[2] ^ 0x93) == '\n')) { - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - to_print[i] -= 2; - } else { - /* something went wrong? all bets are off */ - if (band_txt[i].temp_line_cnt > 200) { - printf("Reached the limit in the OLD gps mode\n"); - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - /* do not copy CR, NL */ - if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; - band_txt[i].temp_line_cnt++; - } + /* do not copy CR, NL */ + if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; + band_txt[i].temp_line_cnt++; + } + if (((tmp_txt[2] ^ 0x93) != '\r') && ((tmp_txt[2] ^ 0x93) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[2] ^ 0x93; + band_txt[i].temp_line_cnt++; + } - if ((tmp_txt[1] ^ 0x4f) == '\r') { - if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { - memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; - } else if (band_txt[i].temp_line[0] != '$') { - memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; - if (bool_send_aprs && !band_txt[i].is_gps_sent) - gps_send(i); + if (((tmp_txt[1] ^ 0x4f) == '\r') || ((tmp_txt[2] ^ 0x93) == '\r')) { + if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { + memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; + } else if (band_txt[i].temp_line[0] != '$') { + memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; + if (bool_send_aprs && !band_txt[i].is_gps_sent) + gps_send(i); + } + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } else if (((tmp_txt[1] ^ 0x4f) == '\n') || ((tmp_txt[2] ^ 0x93) == '\n')) { + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; } - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } else if ((tmp_txt[1] ^ 0x4f) == '\n') { - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - to_print[i] --; - } - } else if (header_type == 0x40) { /* ABC text */ - new_group[i] = false; - to_print[i] = 3; - ABC_grp[i] = true; - C_seen[i] = ((tmp_txt[0] & 0x0f) == 0x03)?true:false; - - band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[1] ^ 0x4f; - band_txt[i].txt_cnt++; - - band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[2] ^ 0x93; - band_txt[i].txt_cnt++; - - /* - We should NOT see any more text, - if we already processed text, - so blank out the codes. - */ - if (band_txt[i].txt_stats_sent) { - if (recvlen == 29) { - rptrbuf.vpkt.vasd.text[0] = 0x70; - rptrbuf.vpkt.vasd.text[1] = 0x4f; - rptrbuf.vpkt.vasd.text[2] = 0x93; + to_print[i] -= 2; } else { - rptrbuf.vpkt.vasd1.text[0] = 0x70; - rptrbuf.vpkt.vasd1.text[1] = 0x4f; - rptrbuf.vpkt.vasd1.text[2] = 0x93; - } - } + /* something went wrong? all bets are off */ + if (band_txt[i].temp_line_cnt > 200) { + printf("Reached the limit in the OLD gps mode\n"); + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } - if (band_txt[i].txt_cnt >= 20) { - band_txt[i].txt[band_txt[i].txt_cnt] = '\0'; - /*** - ii->sendHeardWithTXMsg(band_txt[i].lh_mycall, - band_txt[i].lh_sfx, - (strstr(band_txt[i].lh_yrcall,"REF") == NULL)?band_txt[i].lh_yrcall:"CQCQCQ ", - band_txt[i].lh_rpt1, - band_txt[i].lh_rpt2, - band_txt[i].flags[0], - band_txt[i].flags[1], - band_txt[i].flags[2], - band_txt[i].dest_rptr, - band_txt[i].txt); - ***/ - // printf("TEXT1=[%s]\n", band_txt[i].txt); - band_txt[i].txt_cnt = 0; - } - } else { - new_group[i] = false; - to_print[i] = 0; - ABC_grp[i] = false; - } - } else { - if (to_print[i] == 3) { - if (ABC_grp[i]) { - band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[0] ^ 0x70; - band_txt[i].txt_cnt ++; + /* do not copy CR, NL */ + if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; + band_txt[i].temp_line_cnt++; + } + + if ((tmp_txt[1] ^ 0x4f) == '\r') { + if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { + memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; + } else if (band_txt[i].temp_line[0] != '$') { + memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; + if (bool_send_aprs && !band_txt[i].is_gps_sent) + gps_send(i); + } + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } else if ((tmp_txt[1] ^ 0x4f) == '\n') { + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } + to_print[i] --; + } + } else if (header_type == 0x40) { /* ABC text */ + new_group[i] = false; + to_print[i] = 3; + ABC_grp[i] = true; + C_seen[i] = ((tmp_txt[0] & 0x0f) == 0x03)?true:false; band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[1] ^ 0x4f; - band_txt[i].txt_cnt ++; + band_txt[i].txt_cnt++; band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[2] ^ 0x93; - band_txt[i].txt_cnt ++; + band_txt[i].txt_cnt++; /* We should NOT see any more text, @@ -1778,35 +1778,108 @@ void CQnetGateway::process() } } - if ((band_txt[i].txt_cnt >= 20) || C_seen[i]) { + if (band_txt[i].txt_cnt >= 20) { band_txt[i].txt[band_txt[i].txt_cnt] = '\0'; - if (!band_txt[i].txt_stats_sent) { - /*** if YRCALL is CQCQCQ, set dest_rptr ***/ - if (memcmp(band_txt[i].lh_yrcall, "CQCQCQ", 6) == 0) { - set_dest_rptr(i, band_txt[i].dest_rptr); - // if (memcmp(band_txt[i].dest_rptr, "REF", 3) == 0) - // band_txt[i].dest_rptr[0] = '\0'; + band_txt[i].txt_cnt = 0; + } + } else { + new_group[i] = false; + to_print[i] = 0; + ABC_grp[i] = false; + } + } else { + if (to_print[i] == 3) { + if (ABC_grp[i]) { + band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[0] ^ 0x70; + band_txt[i].txt_cnt ++; + + band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[1] ^ 0x4f; + band_txt[i].txt_cnt ++; + + band_txt[i].txt[band_txt[i].txt_cnt] = tmp_txt[2] ^ 0x93; + band_txt[i].txt_cnt ++; + + // We should NOT see any more text, + // if we already processed text, + // so blank out the codes. + + if (band_txt[i].txt_stats_sent) { + if (recvlen == 29) { + rptrbuf.vpkt.vasd.text[0] = 0x70; + rptrbuf.vpkt.vasd.text[1] = 0x4f; + rptrbuf.vpkt.vasd.text[2] = 0x93; + } else { + rptrbuf.vpkt.vasd1.text[0] = 0x70; + rptrbuf.vpkt.vasd1.text[1] = 0x4f; + rptrbuf.vpkt.vasd1.text[2] = 0x93; } + } - ii->sendHeardWithTXMsg(band_txt[i].lh_mycall, - band_txt[i].lh_sfx, - //(strstr(band_txt[i].lh_yrcall,"REF") == NULL)?band_txt[i].lh_yrcall:"CQCQCQ ", - band_txt[i].lh_yrcall, - band_txt[i].lh_rpt1, - band_txt[i].lh_rpt2, - band_txt[i].flags[0], - band_txt[i].flags[1], - band_txt[i].flags[2], - band_txt[i].dest_rptr, - band_txt[i].txt); - // printf("TEXT2=[%s], destination repeater=[%s]\n", band_txt[i].txt, band_txt[i].dest_rptr); - band_txt[i].txt_stats_sent = true; + if ((band_txt[i].txt_cnt >= 20) || C_seen[i]) { + band_txt[i].txt[band_txt[i].txt_cnt] = '\0'; + if (!band_txt[i].txt_stats_sent) { + /*** if YRCALL is CQCQCQ, set dest_rptr ***/ + if (memcmp(band_txt[i].lh_yrcall, "CQCQCQ", 6) == 0) { + set_dest_rptr(i, band_txt[i].dest_rptr); + // if (memcmp(band_txt[i].dest_rptr, "REF", 3) == 0) + // band_txt[i].dest_rptr[0] = '\0'; + } + + ii->sendHeardWithTXMsg(band_txt[i].lh_mycall, band_txt[i].lh_sfx,band_txt[i].lh_yrcall, + band_txt[i].lh_rpt1, band_txt[i].lh_rpt2, band_txt[i].flags[0], band_txt[i].flags[1], + band_txt[i].flags[2], band_txt[i].dest_rptr, band_txt[i].txt); + band_txt[i].txt_stats_sent = true; + } + band_txt[i].txt_cnt = 0; + } + if (C_seen[i]) + C_seen[i] = false; + } else { + /* something went wrong? all bets are off */ + if (band_txt[i].temp_line_cnt > 200) { + printf("Reached the limit in the OLD gps mode\n"); + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } + + /* do not copy CR, NL */ + if (((tmp_txt[0] ^ 0x70) != '\r') && ((tmp_txt[0] ^ 0x70) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[0] ^ 0x70; + band_txt[i].temp_line_cnt++; + } + if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; + band_txt[i].temp_line_cnt++; + } + if (((tmp_txt[2] ^ 0x93) != '\r') && ((tmp_txt[2] ^ 0x93) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[2] ^ 0x93; + band_txt[i].temp_line_cnt++; + } + + if ( + ((tmp_txt[0] ^ 0x70) == '\r') || + ((tmp_txt[1] ^ 0x4f) == '\r') || + ((tmp_txt[2] ^ 0x93) == '\r') + ) { + if (0 == memcmp(band_txt[i].temp_line, "$GPRMC", 6)) { + memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; + } else if (band_txt[i].temp_line[0] != '$') { + memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; + if (bool_send_aprs && !band_txt[i].is_gps_sent) + gps_send(i); + } + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } else if (((tmp_txt[0] ^ 0x70) == '\n') || + ((tmp_txt[1] ^ 0x4f) == '\n') || + ((tmp_txt[2] ^ 0x93) == '\n')) { + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; } - band_txt[i].txt_cnt = 0; } - if (C_seen[i]) - C_seen[i] = false; - } else { + } else if (to_print[i] == 2) { /* something went wrong? all bets are off */ if (band_txt[i].temp_line_cnt > 200) { printf("Reached the limit in the OLD gps mode\n"); @@ -1823,16 +1896,8 @@ void CQnetGateway::process() band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; band_txt[i].temp_line_cnt++; } - if (((tmp_txt[2] ^ 0x93) != '\r') && ((tmp_txt[2] ^ 0x93) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[2] ^ 0x93; - band_txt[i].temp_line_cnt++; - } - if ( - ((tmp_txt[0] ^ 0x70) == '\r') || - ((tmp_txt[1] ^ 0x4f) == '\r') || - ((tmp_txt[2] ^ 0x93) == '\r') - ) { + if (((tmp_txt[0] ^ 0x70) == '\r') || ((tmp_txt[1] ^ 0x4f) == '\r')) { if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; @@ -1844,171 +1909,94 @@ void CQnetGateway::process() } band_txt[i].temp_line[0] = '\0'; band_txt[i].temp_line_cnt = 0; - } else if (((tmp_txt[0] ^ 0x70) == '\n') || - ((tmp_txt[1] ^ 0x4f) == '\n') || - ((tmp_txt[2] ^ 0x93) == '\n')) { + } else if (((tmp_txt[0] ^ 0x70) == '\n') || ((tmp_txt[1] ^ 0x4f) == '\n')) { band_txt[i].temp_line[0] = '\0'; band_txt[i].temp_line_cnt = 0; } - } - } else if (to_print[i] == 2) { - /* something went wrong? all bets are off */ - if (band_txt[i].temp_line_cnt > 200) { - printf("Reached the limit in the OLD gps mode\n"); - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - - /* do not copy CR, NL */ - if (((tmp_txt[0] ^ 0x70) != '\r') && ((tmp_txt[0] ^ 0x70) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[0] ^ 0x70; - band_txt[i].temp_line_cnt++; - } - if (((tmp_txt[1] ^ 0x4f) != '\r') && ((tmp_txt[1] ^ 0x4f) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[1] ^ 0x4f; - band_txt[i].temp_line_cnt++; - } - - if (((tmp_txt[0] ^ 0x70) == '\r') || ((tmp_txt[1] ^ 0x4f) == '\r')) { - if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { - memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; - } else if (band_txt[i].temp_line[0] != '$') { - memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; - if (bool_send_aprs && !band_txt[i].is_gps_sent) - gps_send(i); + } else if (to_print[i] == 1) { + /* something went wrong? all bets are off */ + if (band_txt[i].temp_line_cnt > 200) { + printf("Reached the limit in the OLD gps mode\n"); + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; } - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } else if (((tmp_txt[0] ^ 0x70) == '\n') || ((tmp_txt[1] ^ 0x4f) == '\n')) { - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - } else if (to_print[i] == 1) { - /* something went wrong? all bets are off */ - if (band_txt[i].temp_line_cnt > 200) { - printf("Reached the limit in the OLD gps mode\n"); - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } - /* do not copy CR, NL */ - if (((tmp_txt[0] ^ 0x70) != '\r') && ((tmp_txt[0] ^ 0x70) != '\n')) { - band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[0] ^ 0x70; - band_txt[i].temp_line_cnt++; - } + /* do not copy CR, NL */ + if (((tmp_txt[0] ^ 0x70) != '\r') && ((tmp_txt[0] ^ 0x70) != '\n')) { + band_txt[i].temp_line[band_txt[i].temp_line_cnt] = tmp_txt[0] ^ 0x70; + band_txt[i].temp_line_cnt++; + } - if ((tmp_txt[0] ^ 0x70) == '\r') { - if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { - memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; - } else if (band_txt[i].temp_line[0] != '$') { - memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); - band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; - if (bool_send_aprs && !band_txt[i].is_gps_sent) - gps_send(i); + if ((tmp_txt[0] ^ 0x70) == '\r') { + if (memcmp(band_txt[i].temp_line, "$GPRMC", 6) == 0) { + memcpy(band_txt[i].gprmc, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gprmc[band_txt[i].temp_line_cnt] = '\0'; + } else if (band_txt[i].temp_line[0] != '$') { + memcpy(band_txt[i].gpid, band_txt[i].temp_line, band_txt[i].temp_line_cnt); + band_txt[i].gpid[band_txt[i].temp_line_cnt] = '\0'; + if (bool_send_aprs && !band_txt[i].is_gps_sent) + gps_send(i); + } + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + } else if ((tmp_txt[0] ^ 0x70) == '\n') { + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; } - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; - } else if ((tmp_txt[0] ^ 0x70) == '\n') { - band_txt[i].temp_line[0] = '\0'; - band_txt[i].temp_line_cnt = 0; } + new_group[i] = true; + to_print[i] = 0; + ABC_grp[i] = false; } - new_group[i] = true; - to_print[i] = 0; - ABC_grp[i] = false; + break; } - break; } } - } - /* send data to qnlink */ - sendto(srv_sock, rptrbuf.pkt_id, recvlen, 0, (struct sockaddr *)&plug, sizeof(struct sockaddr_in)); - - /* aprs processing */ - if (bool_send_aprs) - // streamID seq audio+text size - aprs->ProcessText(rptrbuf.vpkt.streamid, rptrbuf.vpkt.ctrl, rptrbuf.vpkt.vasd.voice, (recvlen == 29)?12:15); - - for (int i=0; i<3; i++) { - /* find out if data must go to the remote G2 */ - if (to_remote_g2[i].streamid == rptrbuf.vpkt.streamid) { - memcpy(g2buf.title, "DSVT", 4); - g2buf.config = 0x20; - g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0; - memcpy(&g2buf.id, &rptrbuf.vpkt.icm_id, 7); - if (recvlen == 29) - memcpy(g2buf.vasd.voice, rptrbuf.vpkt.vasd.voice, 12); - else - memcpy(g2buf.vasd.voice, rptrbuf.vpkt.vasd1.voice, 12); - - uint32_t address = to_remote_g2[i].toDst4.sin_addr.s_addr; - // if the address is in the portmap, we'll use that port instead of the default - auto theAddress = portmap.find(address); - to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end())? g2_external.port : theAddress->second); - sendto(g2_sock, g2buf.title, 27, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); + /* send data to qnlink */ + sendto(srv_sock, rptrbuf.pkt_id, recvlen, 0, (struct sockaddr *)&plug, sizeof(struct sockaddr_in)); - time(&(to_remote_g2[i].last_time)); + /* aprs processing */ + if (bool_send_aprs) + // streamID seq audio+text size + aprs->ProcessText(rptrbuf.vpkt.streamid, rptrbuf.vpkt.ctrl, rptrbuf.vpkt.vasd.voice, (recvlen == 29)?12:15); - /* Is this the end-of-stream */ - if (rptrbuf.vpkt.ctrl & 0x40) { - memset(&to_remote_g2[i].toDst4,0,sizeof(struct sockaddr_in)); - to_remote_g2[i].streamid = 0; - to_remote_g2[i].last_time = 0; - } - break; - } else - /* Is the data to be recorded for echotest */ - if (recd[i].fd>=0 && recd[i].streamid==rptrbuf.vpkt.streamid) { - time(&recd[i].last_time); - - memcpy(recbuf.title, "DSVT", 4); - recbuf.config = 0x20; - recbuf.id = rptrbuf.vpkt.icm_id; - recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[20] = 0; - recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; - recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; - recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; - memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 3); + for (int i=0; i<3; i++) { + /* find out if data must go to the remote G2 */ + if (to_remote_g2[i].streamid == rptrbuf.vpkt.streamid) { + memcpy(g2buf.title, "DSVT", 4); + g2buf.config = 0x20; + g2buf.flaga[0] = g2buf.flaga[1] = g2buf.flaga[2] = 0; + memcpy(&g2buf.id, &rptrbuf.vpkt.icm_id, 7); if (recvlen == 29) - memcpy(recbuf.vasd.voice, rptrbuf.vpkt.vasd.voice, 12); + memcpy(g2buf.vasd.voice, rptrbuf.vpkt.vasd.voice, 12); else - memcpy(recbuf.vasd.voice, rptrbuf.vpkt.vasd1.voice, 12); + memcpy(g2buf.vasd.voice, rptrbuf.vpkt.vasd1.voice, 12); - rec_len = 27; - (void)write(recd[i].fd, &rec_len, 2); - (void)write(recd[i].fd, &recbuf, rec_len); + uint32_t address = to_remote_g2[i].toDst4.sin_addr.s_addr; + // if the address is in the portmap, we'll use that port instead of the default + auto theAddress = portmap.find(address); + to_remote_g2[i].toDst4.sin_port = htons((theAddress==portmap.end())? g2_external.port : theAddress->second); + sendto(g2_sock, g2buf.title, 27, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(struct sockaddr_in)); - if ((rptrbuf.vpkt.ctrl & 0x40) != 0) { - recd[i].streamid = 0; - recd[i].last_time = 0; - close(recd[i].fd); - recd[i].fd = -1; - // printf("Closed echotest audio file:[%s]\n", recd[i].file); + time(&(to_remote_g2[i].last_time)); - /* we are in echotest mode, so play it back */ - try { - std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, recd[i].file); - } 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 - unlink(recd[i].file); - } + /* Is this the end-of-stream */ + if (rptrbuf.vpkt.ctrl & 0x40) { + memset(&to_remote_g2[i].toDst4,0,sizeof(struct sockaddr_in)); + to_remote_g2[i].streamid = 0; + to_remote_g2[i].last_time = 0; } break; } else - /* Is the data to be recorded for voicemail */ - if ((vm[i].fd >= 0) && (vm[i].streamid==rptrbuf.vpkt.streamid)) { - time(&vm[i].last_time); + /* Is the data to be recorded for echotest */ + if (recd[i].fd>=0 && recd[i].streamid==rptrbuf.vpkt.streamid) { + time(&recd[i].last_time); memcpy(recbuf.title, "DSVT", 4); recbuf.config = 0x20; - recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; recbuf.id = rptrbuf.vpkt.icm_id; + recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[20] = 0; recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; @@ -2019,43 +2007,85 @@ void CQnetGateway::process() memcpy(recbuf.vasd.voice, rptrbuf.vpkt.vasd1.voice, 12); rec_len = 27; - (void)write(vm[i].fd, &rec_len, 2); - (void)write(vm[i].fd, &recbuf, rec_len); + (void)write(recd[i].fd, &rec_len, 2); + (void)write(recd[i].fd, &recbuf, rec_len); if ((rptrbuf.vpkt.ctrl & 0x40) != 0) { - vm[i].streamid = 0; - vm[i].last_time = 0; - close(vm[i].fd); - vm[i].fd = -1; - // printf("Closed voicemail audio file:[%s]\n", vm[i].file); + recd[i].streamid = 0; + recd[i].last_time = 0; + close(recd[i].fd); + recd[i].fd = -1; + // printf("Closed echotest audio file:[%s]\n", recd[i].file); + + /* we are in echotest mode, so play it back */ + try { + std::async(std::launch::async, &CQnetGateway::PlayFileThread, this, recd[i].file); + } 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 + unlink(recd[i].file); + } } break; } else - /* or maybe this is cross-banding data */ - if ((toRptr[i].streamid==rptrbuf.vpkt.streamid) && (toRptr[i].adr == fromRptr.sin_addr.s_addr)) { - sendto(srv_sock, rptrbuf.pkt_id, 29, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - - /* timeit */ - time(&toRptr[i].last_time); - - /* bump G2 counter */ - toRptr[i].G2_COUNTER ++; - - toRptr[i].sequence = rptrbuf.vpkt.ctrl; - - /* End of stream ? */ - if (rptrbuf.vpkt.ctrl & 0x40) { - toRptr[i].last_time = 0; - toRptr[i].streamid = 0; - toRptr[i].adr = 0; + /* Is the data to be recorded for voicemail */ + if ((vm[i].fd >= 0) && (vm[i].streamid==rptrbuf.vpkt.streamid)) { + time(&vm[i].last_time); + + memcpy(recbuf.title, "DSVT", 4); + recbuf.config = 0x20; + recbuf.flaga[0] = recbuf.flaga[1] = recbuf.flaga[2] = 0; + recbuf.id = rptrbuf.vpkt.icm_id; + recbuf.flagb[0] = rptrbuf.vpkt.dst_rptr_id; + recbuf.flagb[1] = rptrbuf.vpkt.snd_rptr_id; + recbuf.flagb[2] = rptrbuf.vpkt.snd_term_id; + memcpy(&recbuf.streamid, &rptrbuf.vpkt.streamid, 3); + if (recvlen == 29) + memcpy(recbuf.vasd.voice, rptrbuf.vpkt.vasd.voice, 12); + else + memcpy(recbuf.vasd.voice, rptrbuf.vpkt.vasd1.voice, 12); + + rec_len = 27; + (void)write(vm[i].fd, &rec_len, 2); + (void)write(vm[i].fd, &recbuf, rec_len); + + if ((rptrbuf.vpkt.ctrl & 0x40) != 0) { + vm[i].streamid = 0; + vm[i].last_time = 0; + close(vm[i].fd); + vm[i].fd = -1; + // printf("Closed voicemail audio file:[%s]\n", vm[i].file); } break; - } - } + } else + /* or maybe this is cross-banding data */ + if ((toRptr[i].streamid==rptrbuf.vpkt.streamid) && (toRptr[i].adr == fromRptr.sin_addr.s_addr)) { + sendto(srv_sock, rptrbuf.pkt_id, 29, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); + + /* timeit */ + time(&toRptr[i].last_time); + + /* bump G2 counter */ + if (is_icom) + G2_COUNTER_OUT++; + else + toRptr[i].G2_COUNTER ++; + + toRptr[i].sequence = rptrbuf.vpkt.ctrl; + + /* End of stream ? */ + if (rptrbuf.vpkt.ctrl & 0x40) { + toRptr[i].last_time = 0; + toRptr[i].streamid = 0; + toRptr[i].adr = 0; + } + break; + } + } - if (rptrbuf.vpkt.ctrl & 0x40) { - if (bool_qso_details) - printf("END from rptr: cntr=%04x, streamID=%04x, %d bytes\n", rptrbuf.counter, rptrbuf.vpkt.streamid, recvlen); + if (bool_qso_details && rptrbuf.vpkt.ctrl&0x40) + printf("id=%04x, cntr=%04x END RPTR\n", ntohs(rptrbuf.vpkt.streamid), ntohs(rptrbuf.counter)); } } } @@ -2288,12 +2318,10 @@ void CQnetGateway::APRSBeaconThread() void CQnetGateway::PlayFileThread(char *file) { - unsigned short rlen = 0; - unsigned char dstar_buf[56]; - unsigned char rptr_buf[58]; - short int i = 0; - struct sigaction act; + SDSTR dstr; + SDSVT dsvt; + struct sigaction act; act.sa_handler = sigCatch; sigemptyset(&act.sa_mask); act.sa_flags = SA_RESTART; @@ -2318,14 +2346,14 @@ void CQnetGateway::PlayFileThread(char *file) return; } - size_t nread = fread(dstar_buf, 10, 1, fp); + size_t nread = fread(dstr.pkt_id, 10, 1, fp); if (nread != 1) { printf("Cant read first 10 bytes in %s\n", file); fclose(fp); return; } - if (memcmp(dstar_buf, "DVTOOL", 6) != 0) { + if (memcmp(dstr.pkt_id, "DVTOOL", 6) != 0) { printf("DVTOOL keyword not found in %s\n", file); fclose(fp); return; @@ -2333,6 +2361,7 @@ void CQnetGateway::PlayFileThread(char *file) sleep(play_wait); while (keep_running) { + unsigned short rlen; nread = fread(&rlen, 2, 1, fp); if (nread != 1) break; @@ -2341,54 +2370,68 @@ void CQnetGateway::PlayFileThread(char *file) printf("Expected 56 bytes or 27 bytes, found %d\n", rlen); break; } - nread = fread(dstar_buf, rlen, 1, fp); + nread = fread(dsvt.title, rlen, 1, fp); if (nread == 1) { - if (memcmp(dstar_buf, "DSVT", 4) != 0) { + if (memcmp(dsvt.title, "DSVT", 4) != 0) { printf("DVST keyword not found in %s\n", file); break; } - if (dstar_buf[8] != 0x20) { + if (dsvt.id != 0x20) { printf("Not Voice type in %s\n", file); break; } - if ((dstar_buf[4] != 0x10) && (dstar_buf[4] != 0x20)) { + if ((dsvt.config != 0x10) && (dsvt.config != 0x20)) { printf("Not a valid record type in %s\n",file); break; } + int i; if (rlen == 56) { /* which module is this recording for? */ - if (dstar_buf[25] == 'A') - i = 0; - else if (dstar_buf[25] == 'B') - i = 1; - else if (dstar_buf[25] == 'C') - i = 2; - - memcpy(rptr_buf, "DSTR", 4); - rptr_buf[5] = (unsigned char)(toRptr[i].G2_COUNTER & 0xff); - rptr_buf[4] = (unsigned char)((toRptr[i].G2_COUNTER >> 8) & 0xff); - rptr_buf[6] = 0x73; - rptr_buf[7] = 0x12; - rptr_buf[8] = 0x00; - rptr_buf[9] = 0x30; - rptr_buf[10] = 0x20; - memcpy(rptr_buf + 11, dstar_buf + 9, 47); + i = dsvt.hdr.rpt1[7] - 'A'; + if (i<0 || i>2) { + printf("found a bad module destination for this file!\n"); + fclose(fp); + return; + } + + memcpy(dstr.pkt_id, "DSTR", 4); + dstr.counter = htons(is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++); + dstr.flag[0] = 0x73; + dstr.flag[1] = 0x12; + dstr.flag[2] = 0x00; + dstr.remaining = 0x30; + dstr.vpkt.icm_id = 0x20; + //memcpy(&dstr.vpkt.dst_rptr_id, dsvt.flagb, 47); + dstr.vpkt.dst_rptr_id = dsvt.flagb[0]; + dstr.vpkt.snd_rptr_id = dsvt.flagb[1]; + dstr.vpkt.snd_term_id = dsvt.flagb[2]; + dstr.vpkt.streamid = dsvt.streamid; + dstr.vpkt.ctrl = dsvt.ctrl; + memcpy(dstr.vpkt.hdr.flag, dsvt.hdr.flag, 3); + if (is_icom) { + memcpy(dstr.vpkt.hdr.r1, dsvt.hdr.rpt2, 8); + memcpy(dstr.vpkt.hdr.r2, dsvt.hdr.rpt1, 8); + } else { + memcpy(dstr.vpkt.hdr.r1, dsvt.hdr.rpt1, 8); + memcpy(dstr.vpkt.hdr.r2, dsvt.hdr.rpt2, 8); + } + memcpy(dstr.vpkt.hdr.ur, dsvt.hdr.urcall, 8); + memcpy(dstr.vpkt.hdr.my, dsvt.hdr.mycall, 8); + memcpy(dstr.vpkt.hdr.nm, dsvt.hdr.sfx, 4); + memcpy(dstr.vpkt.hdr.pfcs, dsvt.hdr.pfcs, 2); /* We did not change anything */ // calcPFCS(rptr_buf, 58); } else { - rptr_buf[5] = (unsigned char)(toRptr[i].G2_COUNTER & 0xff); - rptr_buf[4] = (unsigned char)((toRptr[i].G2_COUNTER >> 8) & 0xff); - rptr_buf[9] = 0x13; - memcpy(rptr_buf + 11, dstar_buf + 9, 18); + dstr.counter = htons(is_icom ? G2_COUNTER_OUT++ : toRptr[i].G2_COUNTER++); + dstr.remaining = 0x13; + memcpy(&dstr.vpkt.dst_rptr_id, dsvt.flagb, 18); } - sendto(srv_sock, rptr_buf, rlen + 2, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); - - toRptr[i].G2_COUNTER ++; + sendto(srv_sock, dstr.pkt_id, rlen+2, 0, (struct sockaddr *)&toRptr[i].band_addr, sizeof(struct sockaddr_in)); std::this_thread::sleep_for(std::chrono::milliseconds(play_delay)); } @@ -2446,13 +2489,8 @@ int CQnetGateway::init(char *cfgfile) return 1; } - rptr.mod[0].band = "23cm"; - rptr.mod[1].band = "70cm"; - rptr.mod[2].band = "2m"; - - for (i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) memset(&band_txt[0], 0, sizeof(SBANDTXT)); - } /* process configuration file */ if ( read_config(cfgfile) ) { @@ -2472,8 +2510,42 @@ int CQnetGateway::init(char *cfgfile) rptr.mod[0].call += "-A"; rptr.mod[1].call += "-B"; rptr.mod[2].call += "-C"; + rptr.mod[0].band = "23cm"; + rptr.mod[1].band = "70cm"; + 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()); + for (i = 0; i < 3; i++) { + rptr.mod[i].frequency = rptr.mod[i].offset = rptr.mod[i].latitude = rptr.mod[i].longitude = rptr.mod[i].agl = rptr.mod[i].range = 0.0; + band_txt[i].streamID = 0; + band_txt[i].flags[0] = band_txt[i].flags[1] = band_txt[i].flags[2] = 0; + band_txt[i].lh_mycall[0] = '\0'; + band_txt[i].lh_sfx[0] = '\0'; + band_txt[i].lh_yrcall[0] = '\0'; + band_txt[i].lh_rpt1[0] = '\0'; + band_txt[i].lh_rpt2[0] = '\0'; + + band_txt[i].last_time = 0; + + band_txt[i].txt[0] = '\0'; + band_txt[i].txt_cnt = 0; + band_txt[i].txt_stats_sent = false; + + band_txt[i].dest_rptr[0] = '\0'; + + band_txt[i].temp_line[0] = '\0'; + band_txt[i].temp_line_cnt = 0; + band_txt[i].gprmc[0] = '\0'; + band_txt[i].gpid[0] = '\0'; + band_txt[i].is_gps_sent = false; + band_txt[i].gps_last_time = 0; + + band_txt[i].num_dv_frames = 0; + band_txt[i].num_dv_silent_frames = 0; + band_txt[i].num_bit_errors = 0; + + } + if (bool_send_aprs) { aprs = new CAPRS(&rptr); if (aprs) @@ -2520,7 +2592,9 @@ int CQnetGateway::init(char *cfgfile) return 1; } - /* Open udp INTERNAL port (default: 19000) */ + // Open G2 INTERNAL: + // default non-icom 127.0.0.1:19000 + // default icom 172.16.0.1:20000 srv_sock = open_port(g2_internal); if (0 > srv_sock) { printf("Can't open %s:%d\n", g2_internal.ip.c_str(), g2_internal.port); diff --git a/QnetGateway.h b/QnetGateway.h index 17adabe..197c21a 100644 --- a/QnetGateway.h +++ b/QnetGateway.h @@ -85,7 +85,12 @@ class CQnetGateway { public: CQnetGateway(); ~CQnetGateway(); + void process(); + int init(char *cfgfile); + private: + bool is_icom, is_not_icom; + SPORTIP g2_internal, g2_external, g2_link, ircddb; std::string OWNER, owner, local_irc_ip, status_file, dtmf_dir, dtmf_file, echotest_dir, irc_pass; @@ -155,10 +160,10 @@ private: // read configuration file bool read_config(char *); - bool get_value(const Config &cfg, const char *path, int &value, int min, int max, int default_value); - bool get_value(const Config &cfg, const char *path, double &value, double min, double max, double default_value); - bool get_value(const Config &cfg, const char *path, bool &value, bool default_value); - bool get_value(const Config &cfg, const char *path, std::string &value, int min, int max, const char *default_value); + bool get_value(const Config &cfg, const std::string path, int &value, int min, int max, int default_value); + bool get_value(const Config &cfg, const std::string path, double &value, double min, double max, double default_value); + bool get_value(const Config &cfg, const std::string path, bool &value, bool default_value); + bool get_value(const Config &cfg, const std::string path, std::string &value, int min, int max, const char *default_value); /* aprs functions, borrowed from my retired IRLP node 4201 */ void gps_send(short int rptr_idx); @@ -169,8 +174,4 @@ private: void set_dest_rptr(int mod_ndx, char *dest_rptr); bool validate_csum(SBANDTXT &bt, bool is_gps); - -public: - void process(); - int init(char *cfgfile); }; diff --git a/QnetLink.cpp b/QnetLink.cpp index d72c2e3..402a414 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -41,6 +41,8 @@ #include #include +#include +#include #include #include #include @@ -197,7 +199,7 @@ void CQnetLink::RptrAckThread(char *arg) dsvt.flagb[1] = 0x1; dsvt.streamid = htons(streamid_raw); - dsvt.counter = 0x80; + dsvt.ctrl = 0x80; dsvt.hdr.flag[0] = 0x1; dsvt.hdr.flag[1] = dsvt.hdr.flag[2] = 0x0; @@ -223,7 +225,7 @@ void CQnetLink::RptrAckThread(char *arg) /* start sending silence + announcement text */ for (int i=0; i<10; i++) { - dsvt.counter = (unsigned char)i; + dsvt.ctrl = (unsigned char)i; switch (i) { case 0: dsvt.vasd.text[0] = 0x55; @@ -271,7 +273,7 @@ void CQnetLink::RptrAckThread(char *arg) dsvt.vasd.text[2] = RADIO_ID[19] ^ 0x93; break; case 9: - dsvt.counter |= 0x40; + dsvt.ctrl |= 0x40; dsvt.vasd.text[0] = 0x16; dsvt.vasd.text[1] = 0x29; dsvt.vasd.text[2] = 0xf5; @@ -682,6 +684,8 @@ bool CQnetLink::read_config(const char *cfgFile) if (! get_value(cfg, "file.status", status_file, 2, FILENAME_MAX, "/usr/local/etc/RPTR_STATUS.txt")) return true; + get_value(cfg, "file.qnvoicefile", qnvoice_file, 2, FILENAME_MAX, "/tmp/qnvoice.txt"); + get_value(cfg, "timing.play.delay", delay_between, 9, 25, 19); get_value(cfg, "link.acknowledge", bool_rptr_ack, true); @@ -1254,6 +1258,26 @@ void CQnetLink::Process() time(&hb); } + // play a qnvoice file if it is specified + std::ifstream voicefile(qnvoice_file.c_str(), std::ifstream::in); + if (voicefile) { + char line[FILENAME_MAX]; + voicefile.getline(line, FILENAME_MAX); + // trim whitespace + char *start = line; + while (isspace(*start)) + start++; + char *end = start + strlen(start) - 1; + while (isspace(*end)) + *end-- = (char)0; + // anthing reasonable left? + if (strlen(start) > 2) + audio_notify(start); + //clean-up + voicefile.close(); + remove(qnvoice_file.c_str()); + } + FD_ZERO(&fdset); FD_SET(xrf_g2_sock,&fdset); FD_SET(dcs_g2_sock,&fdset); @@ -1716,7 +1740,7 @@ void CQnetLink::Process() } } } else if (found) { // length is 27 - if ((dsvt.counter & 0x40) != 0) { + if ((dsvt.ctrl & 0x40) != 0) { for (int i=0; i<3; i++) { if (old_sid[i].sid == dsvt.streamid) { if (qso_details) @@ -1761,7 +1785,7 @@ void CQnetLink::Process() sendto(rptr_sock, from_xrf_torptr_brd.title, 27, 0, (struct sockaddr *)&toLocalg2, sizeof(struct sockaddr_in)); } - if (dsvt.counter & 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; } @@ -1796,7 +1820,7 @@ void CQnetLink::Process() memcpy(dcs_buf + 31, xrf_2_dcs[i].mycall, 8); memcpy(dcs_buf + 39, xrf_2_dcs[i].sfx, 4); memcpy(dcs_buf + 43, &dsvt.streamid, 2); - dcs_buf[45] = dsvt.counter; /* cycle sequence */ + dcs_buf[45] = dsvt.ctrl; /* cycle sequence */ memcpy(dcs_buf + 46, dsvt.vasd.voice, 12); dcs_buf[58] = (xrf_2_dcs[i].dcs_rptr_seq >> 0) & 0xff; @@ -1811,7 +1835,7 @@ void CQnetLink::Process() sendto(dcs_g2_sock, dcs_buf, 100, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(to_remote_g2[i].toDst4)); } - if (dsvt.counter & 0x40) { + if (dsvt.ctrl & 0x40) { to_remote_g2[i].in_streamid = 0x0; } break; @@ -2501,7 +2525,7 @@ void CQnetLink::Process() } } } else if (found) { - if (rdsvt.dsvt.counter & 0x40U) { + if (rdsvt.dsvt.ctrl & 0x40U) { for (int i=0; i<3; i++) { if (old_sid[i].sid == rdsvt.dsvt.streamid) { if (qso_details) @@ -2562,7 +2586,7 @@ void CQnetLink::Process() sendto(dcs_g2_sock, dcs_buf, 100, 0, (struct sockaddr *)&(to_remote_g2[i].toDst4), sizeof(to_remote_g2[i].toDst4)); } - if (rdsvt.dsvt.counter & 0x40) { + if (rdsvt.dsvt.ctrl & 0x40) { to_remote_g2[i].in_streamid = 0x0; } break; @@ -2655,7 +2679,7 @@ void CQnetLink::Process() else rdsvt.dsvt.flagb[2] = 0x02; memcpy(&rdsvt.dsvt.streamid, dcs_buf+43, 2); - rdsvt.dsvt.counter = 0x80; + rdsvt.dsvt.ctrl = 0x80; rdsvt.dsvt.hdr.flag[0] = rdsvt.dsvt.hdr.flag[1] = rdsvt.dsvt.hdr.flag[2] = 0x00; memcpy(rdsvt.dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE); rdsvt.dsvt.hdr.rpt1[7] = to_remote_g2[i].from_mod; @@ -2697,7 +2721,7 @@ void CQnetLink::Process() else rdsvt.dsvt.flagb[2] = 0x02; memcpy(&rdsvt.dsvt.streamid, dcs_buf+43, 2); - rdsvt.dsvt.counter = dcs_buf[45]; + rdsvt.dsvt.ctrl = dcs_buf[45]; memcpy(rdsvt.dsvt.vasd.voice, dcs_buf+46, 12); /* send the data to the local gateway/repeater */ @@ -3487,7 +3511,7 @@ void CQnetLink::AudioNotifyThread(char *arg) dsvt.streamid = htons(streamid_raw); if (rlen == 56) { - dsvt.hdr.flag[0] = 0x01; + dsvt.hdr.flag[0] = 0x0; memcpy(dsvt.hdr.rpt1, owner.c_str(), CALL_SIZE); dsvt.hdr.rpt1[7] = mod; diff --git a/QnetLink.h b/QnetLink.h index 51a057a..1f6ef3b 100644 --- a/QnetLink.h +++ b/QnetLink.h @@ -81,7 +81,7 @@ private: bool get_value(const Config &cfg, const char *path, std::string &value, int min, int max, const char *default_value); /* configuration data */ - std::string login_call, owner, to_g2_external_ip, my_g2_link_ip, gwys, status_file, announce_dir; + std::string login_call, owner, to_g2_external_ip, my_g2_link_ip, gwys, status_file, qnvoice_file, announce_dir; bool only_admin_login, only_link_unlink, qso_details, bool_rptr_ack, announce; int rmt_xrf_port, rmt_ref_port, rmt_dcs_port, my_g2_link_port, to_g2_external_port, delay_between, delay_before; char link_at_startup[CALL_SIZE+1]; diff --git a/QnetRelay.cpp b/QnetRelay.cpp index bf5656b..dd2ad48 100644 --- a/QnetRelay.cpp +++ b/QnetRelay.cpp @@ -230,42 +230,49 @@ 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 (29==len || 58==len) { //here is dstar data - SDSTR buf; - ::memcpy(buf.pkt_id, raw, len); // transfer raw data to SDSTR struct + SDSTR dstr; + ::memcpy(dstr.pkt_id, raw, len); // transfer raw data to SDSTR struct - SDSRP pkt; // destination + SDSRP dsrp; // destination // fill in some inital stuff - ::memcpy(pkt.title, "DSRP", 4); - pkt.voice.id = buf.vpkt.streamid; - pkt.voice.seq = buf.vpkt.ctrl; + ::memcpy(dsrp.title, "DSRP", 4); + dsrp.voice.id = dstr.vpkt.streamid; // voice or header is the same position + dsrp.voice.seq = dstr.vpkt.ctrl; // ditto if (29 == len) { // write an AMBE packet - pkt.tag = 0x21U; - if (pkt.voice.seq & 0x40) -// printf("INFO: ProcessGateway: sending voice end-of-stream\n"); - ; - else if (pkt.voice.seq > 20) - printf("DEBUG: ProcessGateway: unexpected voice sequence number %d\n", pkt.voice.seq); - pkt.voice.err = 0; // NOT SURE WHERE TO GET THIS FROM THE INPUT buf - memcpy(pkt.voice.ambe, buf.vpkt.vasd.voice, 12); - int ret = SendTo(msock, pkt.title, 21, MMDVM_IP, MMDVM_IN_PORT); + dsrp.tag = 0x21U; + if (log_qso && dsrp.voice.seq&0x40) + printf("Sent DSRP end of streamid=%04x\n", ntohs(dsrp.voice.id)); + else if (dsrp.voice.seq > 20) + printf("DEBUG: ProcessGateway: unexpected voice sequence number %d\n", dsrp.voice.seq); + dsrp.voice.err = 0; // NOT SURE WHERE TO GET THIS FROM THE INPUT buf + memcpy(dsrp.voice.ambe, dstr.vpkt.vasd.voice, 12); + int ret = SendTo(msock, dsrp.title, 21, MMDVM_IP, MMDVM_IN_PORT); if (ret != 21) { printf("ERROR: ProcessGateway: Could not write AMBE mmdvm packet\n"); return true; } } else { // write a Header packet - pkt.tag = 0x20U; - pkt.header.id = buf.vpkt.streamid; - if (pkt.header.seq) { + dsrp.tag = 0x20U; + if (dsrp.header.seq) { // printf("DEBUG: ProcessGateway: unexpected pkt.header.seq %d, resetting to 0\n", pkt.header.seq); - pkt.header.seq = 0; + dsrp.header.seq = 0; } - memcpy(pkt.header.flag, buf.vpkt.hdr.flag, 41); - int ret = SendTo(msock, pkt.title, 49, MMDVM_IP, MMDVM_IN_PORT); + //memcpy(dsrp.header.flag, dstr.vpkt.hdr.flag, 41); + memcpy(dsrp.header.flag, dstr.vpkt.hdr.flag, 3); + memcpy(dsrp.header.r1, dstr.vpkt.hdr.r1, 8); + memcpy(dsrp.header.r2, dstr.vpkt.hdr.r2, 8); + memcpy(dsrp.header.ur, dstr.vpkt.hdr.ur, 8); + memcpy(dsrp.header.my, dstr.vpkt.hdr.my, 8); + memcpy(dsrp.header.nm, dstr.vpkt.hdr.nm, 4); + memcpy(dsrp.header.pfcs, dstr.vpkt.hdr.pfcs, 2); + int ret = SendTo(msock, dsrp.title, 49, MMDVM_IP, MMDVM_IN_PORT); if (ret != 49) { printf("ERROR: ProcessGateway: Could not write Header mmdvm packet\n"); return true; } - printf("INFO: ProcessGateway: sent header to port %u pkt = '%s'\n", MMDVM_IN_PORT, std::string((char *)pkt.header.r2, 36).c_str()); + if (log_qso) + 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.r1, dsrp.header.r2, dsrp.header.my, dsrp.header.nm); } } else @@ -277,53 +284,65 @@ bool CQnetRelay::ProcessMMDVM(const int len, const unsigned char *raw) { static short old_id = 0U; static short stream_id = 0U; - SDSRP mpkt; + SDSRP dsrp; if (len < 65) - ::memcpy(mpkt.title, raw, len); // transfer raw data to SDSRP struct + ::memcpy(dsrp.title, raw, len); // transfer raw data to SDSRP struct if (49==len || 21==len) { // grab the stream id if this is a header if (49 == len) { - stream_id = mpkt.header.id; + stream_id = dsrp.header.id; if (old_id == stream_id) return false; old_id = stream_id; } - SDSTR gpkt; // destination + SDSTR dstr; // destination // sets most of the params - ::memcpy(gpkt.pkt_id, "DSTR", 4); - gpkt.counter = COUNTER++; - gpkt.flag[0] = 0x73; - gpkt.flag[1] = 0x12; - gpkt.flag[2] = 0x0; - gpkt.vpkt.icm_id = 0x20; - gpkt.vpkt.dst_rptr_id = 0x0; - gpkt.vpkt.snd_rptr_id = 0x1; - gpkt.vpkt.snd_term_id = ('B'==RPTR_MOD) ? 0x1 : (('C'==RPTR_MOD) ? 0x2 : 0x3); - gpkt.vpkt.streamid = stream_id; + ::memcpy(dstr.pkt_id, "DSTR", 4); + dstr.counter = htons(COUNTER++); + dstr.flag[0] = 0x73; + dstr.flag[1] = 0x12; + dstr.flag[2] = 0x0; + dstr.vpkt.icm_id = 0x20; + dstr.vpkt.dst_rptr_id = 0x0; + dstr.vpkt.snd_rptr_id = 0x1; + dstr.vpkt.snd_term_id = ('B'==RPTR_MOD) ? 0x1 : (('C'==RPTR_MOD) ? 0x2 : 0x3); + dstr.vpkt.streamid = stream_id; if (49 == len) { // header - gpkt.remaining = 0x30; - gpkt.vpkt.ctrl = 0x80; - ::memcpy(gpkt.vpkt.hdr.flag, mpkt.header.flag, 41); - int ret = SendTo(msock, gpkt.pkt_id, 58, G2_INTERNAL_IP, G2_IN_PORT); + dstr.remaining = 0x30; + dstr.vpkt.ctrl = 0x80; + //memcpy(dstr.vpkt.hdr.flag, dsrp.header.flag, 41); + memcpy(dstr.vpkt.hdr.flag, dsrp.header.flag, 3); + memcpy(dstr.vpkt.hdr.r1, dsrp.header.r1, 8); + memcpy(dstr.vpkt.hdr.r2, dsrp.header.r2, 8); + memcpy(dstr.vpkt.hdr.ur, dsrp.header.ur, 8); + memcpy(dstr.vpkt.hdr.my, dsrp.header.my, 8); + memcpy(dstr.vpkt.hdr.nm, dsrp.header.nm, 4); + memcpy(dstr.vpkt.hdr.pfcs, dsrp.header.pfcs, 2); + int ret = SendTo(msock, dstr.pkt_id, 58, G2_INTERNAL_IP, G2_IN_PORT); if (ret != 58) { printf("ERROR: ProcessMMDVM: Could not write gateway header packet\n"); return true; } - printf("INFO: ProcessMMDVM: sent header to port %u pkt = '%s'\n", G2_IN_PORT, std::string((char *)gpkt.vpkt.hdr.r2, 36).c_str()); + if (log_qso) + printf("Sent DSTR to %u, streamid=%04x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", G2_IN_PORT, ntohs(dstr.vpkt.streamid), + dstr.vpkt.hdr.ur, dstr.vpkt.hdr.r1, dstr.vpkt.hdr.r2, dstr.vpkt.hdr.my, dstr.vpkt.hdr.nm); } else if (21 == len) { // ambe - gpkt.remaining = 0x16; - gpkt.vpkt.ctrl = mpkt.header.seq; - ::memcpy(gpkt.vpkt.vasd.voice, mpkt.voice.ambe, 12); - int ret = SendTo(msock, gpkt.pkt_id, 29, G2_INTERNAL_IP, G2_IN_PORT); + dstr.remaining = 0x16; + dstr.vpkt.ctrl = dsrp.header.seq; + memcpy(dstr.vpkt.vasd.voice, dsrp.voice.ambe, 12); + int ret = SendTo(msock, dstr.pkt_id, 29, G2_INTERNAL_IP, G2_IN_PORT); + if (log_qso && dstr.vpkt.ctrl&0x40) + printf("Sent dstr end of streamid=%04x\n", ntohs(dstr.vpkt.streamid)); + if (ret != 29) { printf("ERROR: ProcessMMDVM: Could not write gateway voice packet\n"); return true; } } - } else if (len < 65 && mpkt.tag == 0xAU) { + } else if (len < 65 && dsrp.tag == 0xAU) { // printf("MMDVM Poll: '%s'\n", (char *)mpkt.poll_msg); } else printf("DEBUG: ProcessMMDVM: unusual packet len=%d\n", len); @@ -469,11 +488,7 @@ bool CQnetRelay::ReadConfig(const char *cfgFile) } else return true; - GetValue(cfg, "timing.play.delay", DELAY_BETWEEN, 9, 25, 19); - - GetValue(cfg, "timing.play.wait", DELAY_BEFORE, 1, 10, 2); - - GetValue(cfg, std::string(mmdvm_path+".packet_wait").c_str(), WAIT_FOR_PACKETS, 6, 100, 25); + GetValue(cfg, "log.qso", log_qso, false); return false; } @@ -495,8 +510,8 @@ int main(int argc, const char **argv) } if ('-' == argv[1][0]) { - printf("\nMMDVM Modem Version #%s Copyright (C) 2018 by Thomas A. Early N7TAE\n", MMDVM_VERSION); - printf("MMDVM Modem comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); + printf("\nQnetRelay Version #%s Copyright (C) 2018 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"); return 0; } diff --git a/QnetRelay.h b/QnetRelay.h index e890dac..ea3e614 100644 --- a/QnetRelay.h +++ b/QnetRelay.h @@ -62,7 +62,7 @@ private: char OWNER[CALL_SIZE + 1]; std::string MMDVM_IP, G2_INTERNAL_IP; unsigned short MMDVM_IN_PORT, MMDVM_OUT_PORT, G2_IN_PORT, G2_OUT_PORT; - int WAIT_FOR_PACKETS, DELAY_BEFORE, DELAY_BETWEEN; + bool log_qso; // parameters int msock, gsock; diff --git a/QnetRemote.cpp b/QnetRemote.cpp index 73fdb62..c6c1274 100644 --- a/QnetRemote.cpp +++ b/QnetRemote.cpp @@ -50,6 +50,7 @@ short streamid_raw = 0; bool isdefined[3] = { false, false, false }; std::string REPEATER, IP_ADDRESS; int PORT, PLAY_WAIT, PLAY_DELAY; +bool is_icom = false; unsigned char silence[9] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8 }; @@ -197,10 +198,11 @@ bool read_config(const char *cfgFile) path += m + 'a'; std::string type; if (cfg.lookupValue(std::string(path+".type").c_str(), type)) { - if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm")) { + if (type.compare("dvap") && type.compare("dvrptr") && type.compare("mmdvm") && type.compare("icom")) { printf("module type '%s' is invalid\n", type.c_str()); return true; } + is_icom = type.compare("icom") ? false : true; isdefined[m] = true; } } @@ -209,10 +211,10 @@ bool read_config(const char *cfgFile) return true; } - if (! get_value(cfg, "gateway.internal.ip", IP_ADDRESS, 7, 15, "127.0.0.1")) + if (! get_value(cfg, "gateway.internal.ip", IP_ADDRESS, 7, 15, is_icom ? "172.16.0.1" : "127.0.0.1")) return true; - get_value(cfg, "gateway.internal.port", PORT, 16000, 65535, 19000); + get_value(cfg, "gateway.internal.port", PORT, 16000, 65535, is_icom ? 20000 : 19000); get_value(cfg, "timing.play.wait", PLAY_WAIT, 1, 10, 2); @@ -234,11 +236,11 @@ int main(int argc, char *argv[]) if (argc != 4) { printf("Usage: %s \n", argv[0]); - printf("Example: %s c n7tae xrf757cl\n", argv[0]); + printf("Example: %s c n7tae xrf757al\n", argv[0]); printf("Where...\n"); printf(" c is the local repeater module\n"); printf(" n7tae is the value of mycall\n"); - printf(" xrf757cl is the value of yourcall, in this case this is a Link command\n\n"); + printf(" xrf757al is the value of yourcall, in this case this is a Link command\n\n"); return 0; } diff --git a/QnetTypeDefs.h b/QnetTypeDefs.h index 6ff7668..41aac94 100644 --- a/QnetTypeDefs.h +++ b/QnetTypeDefs.h @@ -72,7 +72,7 @@ typedef struct dsvt_tag { unsigned char id; // 8 0x20 unsigned char flagb[3]; // 9 0x0 0x1 0x1 unsigned short streamid;// 12 - unsigned char counter; // 14 hdr: 0x80 vsad: framecounter (mod 21) + unsigned char ctrl; // 14 hdr: 0x80 vsad: framecounter (mod 21) union { struct { // index unsigned char flag[3]; // 15 @@ -108,10 +108,10 @@ typedef struct dsrp_tag { // offset size // 0x01 Dstar Relay Unavailable unsigned char r2[8]; // Repeater 2 11 unsigned char r1[8]; // Repeater 1 19 - unsigned char yr[8]; // Your Call 27 + unsigned char ur[8]; // Your Call 27 unsigned char my[8]; // My Call 35 unsigned char nm[4]; // Name 43 - unsigned short pfcs; // checksum 47 49 + unsigned char pfcs[2]; // checksum 47 49 } header; struct { unsigned short id; // random id number 5 diff --git a/QnetVoice.cpp b/QnetVoice.cpp index b168634..46d4ce4 100644 --- a/QnetVoice.cpp +++ b/QnetVoice.cpp @@ -18,112 +18,19 @@ */ #include -#include #include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include #include #include -#include "QnetTypeDefs.h" -#include "Random.h" using namespace libconfig; -#define VERSION "v3.1" +bool isamod[3] = { false, false, false }; +std::string announce_dir; +std::string qnvoice_file; -int sockDst = -1; -struct sockaddr_in toDst; -FILE *fp = NULL; -time_t tNow = 0; -short streamid_raw = 0; -int moduleport[3] = { 0, 0, 0 }; -std::string REPEATER, IP_ADDRESS; -int PORT, PLAY_WAIT, PLAY_DELAY; - -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, - 0x3183,0x200a,0x1291,0x0318,0x77a7,0x662e,0x54b5,0x453c,0xbdcb,0xac42,0x9ed9,0x8f50,0xfbef,0xea66,0xd8fd,0xc974, - 0x4204,0x538d,0x6116,0x709f,0x0420,0x15a9,0x2732,0x36bb,0xce4c,0xdfc5,0xed5e,0xfcd7,0x8868,0x99e1,0xab7a,0xbaf3, - 0x5285,0x430c,0x7197,0x601e,0x14a1,0x0528,0x37b3,0x263a,0xdecd,0xcf44,0xfddf,0xec56,0x98e9,0x8960,0xbbfb,0xaa72, - 0x6306,0x728f,0x4014,0x519d,0x2522,0x34ab,0x0630,0x17b9,0xef4e,0xfec7,0xcc5c,0xddd5,0xa96a,0xb8e3,0x8a78,0x9bf1, - 0x7387,0x620e,0x5095,0x411c,0x35a3,0x242a,0x16b1,0x0738,0xffcf,0xee46,0xdcdd,0xcd54,0xb9eb,0xa862,0x9af9,0x8b70, - 0x8408,0x9581,0xa71a,0xb693,0xc22c,0xd3a5,0xe13e,0xf0b7,0x0840,0x19c9,0x2b52,0x3adb,0x4e64,0x5fed,0x6d76,0x7cff, - 0x9489,0x8500,0xb79b,0xa612,0xd2ad,0xc324,0xf1bf,0xe036,0x18c1,0x0948,0x3bd3,0x2a5a,0x5ee5,0x4f6c,0x7df7,0x6c7e, - 0xa50a,0xb483,0x8618,0x9791,0xe32e,0xf2a7,0xc03c,0xd1b5,0x2942,0x38cb,0x0a50,0x1bd9,0x6f66,0x7eef,0x4c74,0x5dfd, - 0xb58b,0xa402,0x9699,0x8710,0xf3af,0xe226,0xd0bd,0xc134,0x39c3,0x284a,0x1ad1,0x0b58,0x7fe7,0x6e6e,0x5cf5,0x4d7c, - 0xc60c,0xd785,0xe51e,0xf497,0x8028,0x91a1,0xa33a,0xb2b3,0x4a44,0x5bcd,0x6956,0x78df,0x0c60,0x1de9,0x2f72,0x3efb, - 0xd68d,0xc704,0xf59f,0xe416,0x90a9,0x8120,0xb3bb,0xa232,0x5ac5,0x4b4c,0x79d7,0x685e,0x1ce1,0x0d68,0x3ff3,0x2e7a, - 0xe70e,0xf687,0xc41c,0xd595,0xa12a,0xb0a3,0x8238,0x93b1,0x6b46,0x7acf,0x4854,0x59dd,0x2d62,0x3ceb,0x0e70,0x1ff9, - 0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330,0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78 -}; - - - -void calcPFCS(unsigned char rawbytes[58]) -{ - unsigned short crc_dstar_ffff = 0xffff; - unsigned short tmp, short_c; - short int i; - - for (i = 17; i < 56 ; 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]; - } - crc_dstar_ffff = ~crc_dstar_ffff; - tmp = crc_dstar_ffff; - - rawbytes[56] = (unsigned char)(crc_dstar_ffff & 0xff); - rawbytes[57] = (unsigned char)((tmp >> 8) & 0xff); - return; - -} - -bool dst_open(const char *ip, const int port) -{ - int reuse = 1; - - sockDst = socket(PF_INET,SOCK_DGRAM,0); - if (sockDst == -1) { - printf("Failed to create DSTAR socket\n"); - return true; - } - if (setsockopt(sockDst,SOL_SOCKET,SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1) { - close(sockDst); - sockDst = -1; - printf("setsockopt DSTAR REUSE failed\n"); - return true; - } - memset(&toDst,0,sizeof(struct sockaddr_in)); - toDst.sin_family = AF_INET; - toDst.sin_port = htons(port); - toDst.sin_addr.s_addr = inet_addr(ip); - - fcntl(sockDst,F_SETFL,O_NONBLOCK); - return false; -} - -void dst_close() -{ - if (sockDst != -1) { - close(sockDst); - sockDst = -1; - } - return; -} bool get_value(const Config &cfg, const char *path, int &value, int min, int max, int default_value) { @@ -186,34 +93,22 @@ bool read_config(const char *cfgFile) return true; } - if (! get_value(cfg, "ircddb.login", REPEATER, 3, 6, "UNDEFINED")) - return true; - REPEATER.resize(6, ' '); - printf("REPEATER=[%s]\n", REPEATER.c_str()); - for (short int m=0; m<3; m++) { std::string path = "module."; path += m + 'a'; std::string type; if (cfg.lookupValue(std::string(path+".type").c_str(), type)) { - if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm")) { + if (strcasecmp(type.c_str(), "dvap") && strcasecmp(type.c_str(), "dvrptr") && strcasecmp(type.c_str(), "mmdvm") && strcasecmp(type.c_str(), "icom")) { printf("module type '%s' is invalid\n", type.c_str()); return true; } - get_value(cfg, std::string(path+".port").c_str(), moduleport[m], 1000, 65535, 19998+m); + isamod[m] = true; } } - if (0==moduleport[0] && 0==moduleport[1] && 0==moduleport[2]) { - printf("No repeaters defined!\n"); - return true; - } - - if (! get_value(cfg, "gateway.internal.ip", IP_ADDRESS, 7, 15, "127.0.0.1")) - return true; - get_value(cfg, "timing.play.wait", PLAY_WAIT, 1, 10, 2); + get_value(cfg, "file.announce_dir", announce_dir, 2, FILENAME_MAX, "/usr/local/etc"); - get_value(cfg, "timing.play.delay", PLAY_DELAY, 9, 25, 19); + get_value(cfg, "file.qnvoice_file", qnvoice_file, 2, FILENAME_MAX, "/tmp/qnvoice.txt"); return false; } @@ -227,20 +122,15 @@ void ToUpper(std::string &str) int main(int argc, char *argv[]) { - unsigned short rlen = 0; - static unsigned short G2_COUNTER = 0; - size_t nread = 0; - SDSVT dsvt; - SDSTR dstr; char RADIO_ID[21]; - short int TEXT_idx = 0; if (argc != 4) { - printf("Usage: %s \n", argv[0]); + printf("Usage: %s \n", argv[0]); printf("Where...\n"); - printf(" module is one of your modules\n"); - printf(" mycall is your personal callsign\n"); - printf(" dvtoolFile is a dvtool file\n"); + printf(" is one of your modules: A, B or C\n"); + printf(" is an installed voice file in the configured\n"); + printf(" directory, for example \"unlinked.dat\"\n"); + printf(" is an up to 20-character text message\n"); return 0; } @@ -249,12 +139,6 @@ int main(int argc, char *argv[]) if (read_config(cfgfile.c_str())) return 1; - if (REPEATER.size() > 6) { - printf("repeaterCallsign can not be more than 6 characters, %s is invalid\n", REPEATER.c_str()); - return 1; - } - ToUpper(REPEATER); - char module = argv[1][0]; if (islower(module)) module = toupper(module); @@ -263,176 +147,33 @@ int main(int argc, char *argv[]) return 1; } - PORT = moduleport[module - 'A']; - if (0 == PORT) { - printf("module %c has no port defined!\n", module); - return 1; - } - - if (strlen(argv[2]) > 8) { - printf("MYCALL can not be more than 8 characters, %s is invalid\n", argv[2]); - return 1; - } - std::string mycall(argv[2]); - ToUpper(mycall); + char pathname[FILENAME_MAX]; + snprintf(pathname, FILENAME_MAX, "%s/%s", announce_dir.c_str(), argv[2]); - fp = fopen(argv[3], "rb"); + FILE *fp = fopen(pathname, "rb"); if (!fp) { - printf("Failed to open file %s for reading\n", argv[3]); - return 1; - } - - /* DVTOOL + 4 byte num_of_records */ - unsigned char buf[10]; - nread = fread(buf, 10, 1, fp); - if (nread != 1) { - printf("Cant read first 10 bytes\n"); - fclose(fp); - return 1; - } - if (0 != memcmp(buf, "DVTOOL", 6)) { - printf("DVTOOL signature not found in %s\n", argv[3]); - fclose(fp); + printf("Failed to find file %s for reading\n", pathname); return 1; } + fclose(fp); - memset(RADIO_ID, ' ', 20); + memset(RADIO_ID, '_', 20); RADIO_ID[20] = '\0'; - memcpy(RADIO_ID, "QnetVoice AMBE Data", 19); - - unsigned long int delay = PLAY_DELAY * 1000L; - sleep(PLAY_WAIT); + unsigned int len = strlen(argv[3]); + strncpy(RADIO_ID, argv[3], len > 20 ? 20 : len); + for (int i=0; i<20; i++) + if (isspace(RADIO_ID[i])) + RADIO_ID[i] = '_'; - time(&tNow); - CRandom Random; - - if (dst_open(IP_ADDRESS.c_str(), PORT)) + fp = fopen(qnvoice_file.c_str(), "w"); + if (fp) { + fprintf(fp, "%c_%s_%s\n", module, argv[2], RADIO_ID); + fclose(fp); + } else { + printf("Failed to open %s for writing", qnvoice_file.c_str()); return 1; - - // Read and reformat and write packets - while (true) { - /* 2 byte length */ - nread = fread(&rlen, 2, 1, fp); - if (nread != 1) { - printf("End-Of-File\n"); - break; - } - if (rlen == 56) - streamid_raw = Random.NewStreamID(); - else if (rlen == 27) - ; - else { - printf("Wrong packet size!\n"); - return 1; - } - - /* read the packet */ - nread = fread(dsvt.title, rlen, 1, fp); - printf("Read %d byte packet from %s\n", (int)nread*rlen, argv[3]); - if (rlen == 56) - printf("rpt1=%.8s rpt2=%.8s urcall=%.8s, mycall=%.8s, sfx=%.4s\n", - dsvt.hdr.rpt1, dsvt.hdr.rpt2, dsvt.hdr.urcall, dsvt.hdr.mycall, dsvt.hdr.sfx); - else - printf("streamid=%04X counter=%02X\n", dsvt.streamid, dsvt.counter); - if (nread == 1) { - if (memcmp(dsvt.title, "DSVT", 4) != 0) { - printf("DVST title not found\n"); - return 1; - } - - if (dsvt.id != 0x20) { - printf("Not Voice type\n"); - return 1; - } - - if (dsvt.config!=0x10 && dsvt.config!=0x20) { - printf("Not a valid record type\n"); - return 1; - } - - dstr.counter = htons(G2_COUNTER++); - if (rlen == 56) { - memcpy(dstr.pkt_id, "DSTR", 4); - dstr.flag[0] = 0x73; - dstr.flag[1] = 0x12; - dstr.flag[2] = 0x00; - dstr.remaining = 0x30; - dstr.vpkt.icm_id = 0x20; - dstr.vpkt.dst_rptr_id = dsvt.flagb[0]; - dstr.vpkt.snd_rptr_id = dsvt.flagb[1]; - dstr.vpkt.snd_term_id = dsvt.flagb[2]; - dstr.vpkt.streamid = htons(streamid_raw); - dstr.vpkt.ctrl = dsvt.counter; - for (int i=0; i<3; i++) - dstr.vpkt.hdr.flag[i] = dsvt.hdr.flag[i]; - memset(dstr.vpkt.hdr.r2, ' ', 36); - memcpy(dstr.vpkt.hdr.r2, REPEATER.c_str(), REPEATER.size()); - dstr.vpkt.hdr.r1[7] = 'G'; - memcpy(dstr.vpkt.hdr.r1, REPEATER.c_str(), REPEATER.size()); - dstr.vpkt.hdr.r2[7] = module; - memcpy(dstr.vpkt.hdr.ur, "CQCQCQ", 6); /* yrcall */ - memcpy(dstr.vpkt.hdr.my, mycall.c_str(), mycall.size()); - memcpy(dstr.vpkt.hdr.nm, "QNET", 4); - calcPFCS(dstr.pkt_id); - } else { - dstr.remaining = 0x13; - dstr.vpkt.ctrl = dsvt.counter; - memcpy(dstr.vpkt.vasd.voice, dsvt.vasd.voice, 12); - - if ((dstr.vpkt.vasd.text[0] != 0x55) || (dstr.vpkt.vasd.text[1] != 0x2d) || (dstr.vpkt.vasd.text[2] != 0x16)) { - if (TEXT_idx == 0) { - dstr.vpkt.vasd.text[0] = '@' ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 2) { - dstr.vpkt.vasd.text[0] = RADIO_ID[TEXT_idx++] ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 5) { - dstr.vpkt.vasd.text[0] = 'A' ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 7) { - dstr.vpkt.vasd.text[0] = RADIO_ID[TEXT_idx++] ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 10) { - dstr.vpkt.vasd.text[0] = 'B' ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 12) { - dstr.vpkt.vasd.text[0] = RADIO_ID[TEXT_idx++] ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 15) { - dstr.vpkt.vasd.text[0] = 'C' ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else if (TEXT_idx == 17) { - dstr.vpkt.vasd.text[0] = RADIO_ID[TEXT_idx++] ^ 0x70; - dstr.vpkt.vasd.text[1] = RADIO_ID[TEXT_idx++] ^ 0x4f; - dstr.vpkt.vasd.text[2] = RADIO_ID[TEXT_idx++] ^ 0x93; - } else { - dstr.vpkt.vasd.text[0] = 0x70; - dstr.vpkt.vasd.text[1] = 0x4f; - dstr.vpkt.vasd.text[2] = 0x93; - } - } - } - - int sent = sendto(sockDst, dstr.pkt_id, rlen + 2,0, (struct sockaddr *)&toDst, sizeof(toDst)); - if (sent == 58) - printf("Sent DSTR HDR r2=%.8s r1=%.8s ur=%.8s my=%.8s nm=%.4s\n", - dstr.vpkt.hdr.r2, dstr.vpkt.hdr.r1, dstr.vpkt.hdr.ur, dstr.vpkt.hdr.my, dstr.vpkt.hdr.nm); - else if (sent == 29) - printf("Sent DSTR DATA streamid=%04X, ctrl=%02X\n", dstr.vpkt.streamid, dstr.vpkt.ctrl); - else - printf("ERROR: sendto returned %d!\n", sent); - } - usleep(delay); } - dst_close(); - fclose(fp); + return 0; } diff --git a/qn.everything.cfg b/qn.everything.cfg index e9dfa08..4408baf 100644 --- a/qn.everything.cfg +++ b/qn.everything.cfg @@ -1,10 +1,10 @@ -# g2_ircddb Configuration +# Qnet Gateway Configuration ircddb = { login = "CHANGEME!!!!"; # login callsign for the ircDDB network # host = "rr.openquad.net" # other include group1-irc.ircddb.net # port = 9007 # not a good idea to change! -# password = "1111111111111" # not needed for Openquad +# password = "1111111111111" # not needed for rr.openquad.net } gateway = { @@ -285,6 +285,7 @@ file = { # status = "/usr/local/etc/rptr_status" # where repeater status info is passed between services # DTMF = "/tmp" # # echotest = "/tmp" # echo dat files will end up here +# qnvoicefile = /tmp/qnvoice.txt # where qnvoice will create the play command # gwys = "/usr/local/etc/gwys.txt" # where the list of gateways and reflectors (with ports) is. # announce_dir = "/usr/local/etc" # where are the *.dat files for the verbal link, unlink, etc. announcements } diff --git a/qn.icom.cfg b/qn.icom.cfg new file mode 100644 index 0000000..a80488c --- /dev/null +++ b/qn.icom.cfg @@ -0,0 +1,140 @@ +# Qnet Gateway Configuration + +ircddb = { + login = "CHANGEME!!!!"; # login callsign for the ircDDB network +# host = "rr.openquad.net" # other include group1-irc.ircddb.net +# port = 9007 # not a good idea to change! +# password = "1111111111111" # not needed for rr.openquad.net +} + +gateway = { +# regen_header = true # regenerate headers from incoming data +# send_qrgs_maps = true # send frequecy, offset, cooridinates and url to irc-server +# local_irc_ip = "0.0.0.0" # 0.0.0.0 means accept any incoming connections +# aprs_send = true # send info to aprs +# ip = "127.0.0.1" # where the gateway is running + + external = { +# ip = "0.0.0.0" +# port = 40000 + } + + internal = { +# ip = "172.16.0.1" +# port = 20000 + } +} + +module = { + a = { # an ICOM full stack might consist of up to three module + # Sorry, the 23 cm data module is not yet supported + # 23 cm module will use "a" + # 70 cm module will use "b" + # 2 M module will use "c" +# type = "icom" # you must define at least one module by uncommenting the type +# ip = "172.16.0.20" # all icom modules should have the same IP address +# port = 20000 # all icom modules should have the same UDP port +# frequency = 0 # in MHz, if you specify here, this frequency will show up on the QuadNet USER GATEWAYS webpage +# offset = 0 +# range = 0 # the range of this repeater, in meters. 1609.344 meters is 1.0 miles +# agl = 0 # the height above ground level for this repeater's antenna +# latitude = 0 # you can leave this unspecified for a mobile rig +# longitude = 0 # like the latitude +# desc1 = "" # maximum of 20 characters, most special symbols are not allowed +# desc2 = "" # just like desc1 +# url = "github.com/n7tae/g2_ircddb" # 80 characters max + } + + b = { +# type = "icom" +# ip = "172.16.0.20" # all icom modules should have the same IP address +# port = 20000 # all icom modules should have the same UDP port +# frequency = 0 +# offset = 0 +# range = 0.0 +# agl = 0.0 +# latitude = 0.0 +# longitude = 0.0 +# desc1 = "" +# desc2 = "" +# url = "github.com/n7tae/g2_ircddb" + } + + c = { +# type = "icom" +# ip = "172.16.0.20" # all icom modules should have the same IP address +# port = 20000 # all icom modules should have the same UDP port +# frequency = 0 +# dvap_offset = 0 +# range = 0.0 +# agl = 0.0 +# latitude = 0.0 +# longitude = 0.0 +# desc1 = "" +# desc2 = "" +# url = "github.com/n7tae/QnetGateway" + } +} + +log = { + # debuging and extra logging switches +# qso = false # QSO info goes into the log +# irc = false # IRC debug info +# dtmf = false # DTMF debug info +} + +aprs = { # APRS.NET connect info +# host = "rotate.aprs.net" +# port = 14580 +# interval = 40 +# filter = "" +} + +link = { +# link_at_start = "NONE" # Link to a reflector at startup. +# to link repeater module B to REF001 C, use "BREF001C" +# ref_login = "" # for loging into REF reflectors, if undefined, ircddb.username will be used +# admin = [ "CALL1", "CALL2", "CALL3" ] # only these users can execute scripts, block dongles and reload the gwys.txt +# # you probabaly want you own callsign in the admin list! +# link_unlink = [ "CALL4", "CALL5", "CALL6" ] # if defined, only these users can link and unlink a repeater +# no_link_unlink = [ "CALL7", "CALL8", "CALL9" ] # if defined, these users cannot link or unlink, it's a blacklist +# # if the blacklist is defined (even if it's empty), the link_unlink will not be read +# incoming_ip = "0.0.0.0" # incoming ip address, "0.0.0.0" means accepts all connections. +# ip = "127.0.0.1" # where g2_link is running +# port = 18997 # port for communications to g2_link +# ref_port = 20001 # port for REF linking, don't change +# xrf_port = 30001 # port for XRF linking, don't change +# dcs_port = 30051 # port for DCS linking, don't change +# announce = true # do link, unlink, etc. announcements +# acknowledge = true # send text acknowledgement on key-up +# max_dongles = 5 # maximum number of linked hotspots +} + +file = { +# status = "/usr/local/etc/rptr_status" # where repeater status info is passed between services +# DTMF = "/tmp" # +# echotest = "/tmp" # echo dat files will end up here +# qnvoicefile = /tmp/qnvoice.txt # where qnvoice will create the play command +# gwys = "/usr/local/etc/gwys.txt" # where the list of gateways and reflectors (with ports) is. +# announce_dir = "/usr/local/etc" # where are the *.dat files for the verbal link, unlink, etc. announcements +} + +timing = { + timeout = { +# echo = 1 # delay time in seconds for echo +# voicemail = 1 # delay time for voicemail +# remote_g2 = 2 # after this many seconds with no packets, we assume the tx is closed +# local_rptr = 1 # local timeout, in seconds + } + + play = { +# wait = 2 # seconds before playback occurs, between 1 and 10 +# delay = 19 # microseconds between frames playback, if echo sounds bad, adjust this up or down 1,2 microseconds + } + + inactivity = { +# a = 0 # unlink repeater if no activity for this many minutes +# b = 0 # zero mean there will be no timer +# c = 0 + } +} diff --git a/versions.h b/versions.h index f9c2499..2c7bcf5 100644 --- a/versions.h +++ b/versions.h @@ -1,6 +1,8 @@ // version strings must be 55 characters or less! -#define IRCDDB_VERSION "linux-qngateway-6.1.1" -#define LINK_VERSION "6.0.0" -#define DVAP_VERSION "linux-qndvap-5.1.1" -#define DVRPTR_VERSION "linux-qndvrptr-5.1.0" -#define MMDVM_VERSION "mmdvm-qnmodem-0.1.0" +#define IRCDDB_VERSION "QnetGateway-7.0.0" +#define LINK_VERSION "QnetLink-6.0.0" +#define DVAP_VERSION "QnetDVAP-5.1.1" +#define RELAY_VERSION "QnetRelay-0.2.0" +#define DVRPTR_VERSION "QnetDVRPTR-5.1.0" +#define MMDVM_VERSION "QnetGateway-MMDVM-0.1.0" +#define ICOM_VERSION IRCDDB_VERSION