From 07888881db6d6f5b5be954811e1ec9a4b617947b Mon Sep 17 00:00:00 2001 From: Tom Early Date: Mon, 11 Mar 2019 11:06:03 -0700 Subject: [PATCH] Breakout CTimer from Modem and use in DVAP and ITAP --- QnetDVAP.cpp | 43 +++++++++++++++++-------------------------- QnetITAP.cpp | 10 +++++----- QnetModem.h | 17 +---------------- Timer.h | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 Timer.h diff --git a/QnetDVAP.cpp b/QnetDVAP.cpp index 5e0fa3c..b22c6e1 100644 --- a/QnetDVAP.cpp +++ b/QnetDVAP.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include "DVAPDongle.h" @@ -50,6 +49,7 @@ #include "Random.h" #include "UnixDgramSocket.h" #include "QnetConfigure.h" +#include "Timer.h" #define DVAP_VERSION "QnetDVAP-6.1.1" @@ -82,6 +82,7 @@ static int REMOTE_TIMEOUT; /* 1 second */ static int DELAY_BETWEEN; static int DELAY_BEFORE; static bool RPTR_ACK; +static double TIMING_TIMEOUT_LOCAL_RPTR; static int inactiveMax = 25; /* helper data */ @@ -93,7 +94,6 @@ static bool busy20000 = false; std::atomic keep_running(true); static unsigned int space = 0; -static unsigned int aseed = 0; /* helper routines */ static bool ReadConfig(const char *cfgFile); @@ -232,9 +232,10 @@ static bool ReadConfig(const char *cfgFile) cfg.GetValue(dvap_path+"_packet_wait", type, WAIT_FOR_PACKETS, 6, 100); cfg.GetValue(dvap_path+"_acknowledge", type, RPTR_ACK); - dvap_path.assign("timing_"); - cfg.GetValue(dvap_path+"timeout_remote_g2", estr, REMOTE_TIMEOUT, 1, 10); - dvap_path.append("play_"); + dvap_path.assign("timing_timeout_"); + cfg.GetValue(dvap_path+"remote_g2", estr, REMOTE_TIMEOUT, 1, 10); + cfg.GetValue(dvap_path+"local_rptr", estr, TIMING_TIMEOUT_LOCAL_RPTR, 1.0, 10.0); + dvap_path.assign("timing_play_"); cfg.GetValue(dvap_path+"delay", estr, DELAY_BETWEEN, 9, 25); cfg.GetValue(dvap_path+"wait", estr, DELAY_BEFORE, 1, 10); @@ -517,7 +518,6 @@ static void RptrAckThread(SDVAP_ACK_ARG *parg) memcpy(RADIO_ID, "BER%", 4); struct sigaction act; - time_t tnow = 0; unsigned char silence[12] = { 0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x70,0x4f,0x93 }; act.sa_handler = sig_catch; @@ -537,16 +537,14 @@ static void RptrAckThread(SDVAP_ACK_ARG *parg) sleep(DELAY_BEFORE); - time(&tnow); - - uint16_t stream_id_to_dvap = Random.NewStreamID(); + uint16_t sid = Random.NewStreamID(); // HEADER while ((space < 1) && keep_running) usleep(5); SDVAP_REGISTER dr; dr.header = 0xa02fu; - dr.frame.streamid = stream_id_to_dvap; + dr.frame.streamid = sid; dr.frame.framepos = 0x80; dr.frame.seq = 0; dr.frame.hdr.flag[0] = 0x01; @@ -562,7 +560,7 @@ static void RptrAckThread(SDVAP_ACK_ARG *parg) // SYNC dr.header = 0xc012u; - dr.frame.streamid = stream_id_to_dvap; + dr.frame.streamid = sid; for (int i=0; i<10; i++) { while ((space < 1) && keep_running) usleep(5); @@ -636,8 +634,7 @@ static void ReadDVAPThread() REPLY_TYPE reply; SDSVT dsvt; SDVAP_REGISTER dr; - time_t tnow = 0; - time_t last_RF_time = 0; + CTimer last_RF_time; struct sigaction act; bool dvap_busy = false; // bool ptt = false; @@ -671,12 +668,10 @@ static void ReadDVAPThread() } while (keep_running) { - time(&tnow); // local RF user went away ? if (dvap_busy) { - time(&tnow); - if ((tnow - last_RF_time) > 1) + if (last_RF_time.time() > TIMING_TIMEOUT_LOCAL_RPTR) dvap_busy = false; } @@ -821,7 +816,7 @@ static void ReadDVAPThread() // local RF user keying up, start timer dvap_busy = true; - time(&last_RF_time); + last_RF_time.start(); // save mycall for the ack later memcpy(mycall, dr.frame.hdr.mycall, 8); @@ -850,7 +845,7 @@ static void ReadDVAPThread() sequence = 0; // local RF user still talking, update timer - time(&last_RF_time); + last_RF_time.start(); if (the_end) { // local RF user stopped talking dvap_busy = false; @@ -889,8 +884,6 @@ int main(int argc, const char **argv) { struct sigaction act; int rc = -1; - time_t tnow = 0; - time_t ackpoint = 0; short cnt = 0; setvbuf(stdout, NULL, _IOLBF, 0); @@ -960,8 +953,7 @@ int main(int argc, const char **argv) strcpy(RPTR_and_MOD, RPTR.c_str()); RPTR_and_MOD[7] = RPTR_MOD; - time(&tnow); - aseed = tnow + getpid(); + CTimer ackpoint; act.sa_handler = sig_catch; sigemptyset(&act.sa_mask); @@ -1001,8 +993,7 @@ int main(int argc, const char **argv) printf("Started ReadDVAPThread()\n"); while (keep_running) { - time(&tnow); - if ((tnow - ackpoint) > 2) { + if (ackpoint.time() > 2.5) { rc = dongle.KeepAlive(); if (rc < 0) { cnt ++; @@ -1012,13 +1003,13 @@ int main(int argc, const char **argv) } } else cnt = 0; - ackpoint = tnow; + ackpoint.start(); } readFrom20000(); } readthread.get(); Gate2Modem.Close(); - printf("dvap_rptr exiting\n"); + printf("QnetDVAP exiting\n"); return 0; } diff --git a/QnetITAP.cpp b/QnetITAP.cpp index 76264bb..4bea0fe 100644 --- a/QnetITAP.cpp +++ b/QnetITAP.cpp @@ -43,8 +43,9 @@ #include "QnetITAP.h" #include "QnetTypeDefs.h" #include "QnetConfigure.h" +#include "Timer.h" -#define ITAP_VERSION "QnetITAP-2.1.0" +#define ITAP_VERSION "QnetITAP-2.1.1" std::atomic CQnetITAP::keep_running(true); @@ -215,7 +216,7 @@ void CQnetITAP::Run(const char *cfgfile) unsigned poll_counter = 0; bool is_alive = false; acknowledged = true; - std::chrono::steady_clock::time_point lastdata = std::chrono::steady_clock::now(); + CTimer lastdata; while (keep_running) { @@ -238,8 +239,7 @@ void CQnetITAP::Run(const char *cfgfile) } // check for a dead or disconnected radio - std::chrono::steady_clock::duration sincelastdata = std::chrono::steady_clock::now() - lastdata; - double deadtime = sincelastdata.count() * std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den; + double deadtime = lastdata.time(); if (10.0 < deadtime) { printf("no activity from radio for 10 sec. Exiting...\n"); break; @@ -261,7 +261,7 @@ void CQnetITAP::Run(const char *cfgfile) unsigned char buf[100]; if (keep_running && FD_ISSET(serfd, &readfds)) { - lastdata = std::chrono::steady_clock::now(); + lastdata.start(); switch (GetITAPData(buf)) { case RT_ERROR: keep_running = false; diff --git a/QnetModem.h b/QnetModem.h index 5b55559..d754238 100644 --- a/QnetModem.h +++ b/QnetModem.h @@ -27,6 +27,7 @@ #include "Random.h" // for streamid generation #include "UnixDgramSocket.h" #include "QnetTypeDefs.h" +#include "Timer.h" #define CALL_SIZE 8 #define IP_SIZE 15 @@ -178,22 +179,6 @@ private: SMODEM frame; }; -class CTimer -{ -public: - CTimer() { start(); } - ~CTimer() {} - void start() { - starttime = std::chrono::steady_clock::now(); - } - double time() { - std::chrono::steady_clock::duration elapsed = std::chrono::steady_clock::now() - starttime; - return double(elapsed.count()) * std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den; - } -private: - std::chrono::steady_clock::time_point starttime; -}; - class CQnetModem { public: diff --git a/Timer.h b/Timer.h new file mode 100644 index 0000000..218c76a --- /dev/null +++ b/Timer.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2019 by Thomas A. Early N7TAE + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +#include +#include + +class CTimer +{ +public: + CTimer() { start(); } + ~CTimer() {} + void start() { + starttime = std::chrono::steady_clock::now(); + } + double time() { + std::chrono::steady_clock::duration elapsed = std::chrono::steady_clock::now() - starttime; + return double(elapsed.count()) * std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den; + } +private: + std::chrono::steady_clock::time_point starttime; +};