From 32001f0ede4750dd0a8f550e597f31fb09fab1d2 Mon Sep 17 00:00:00 2001 From: James Coxon Date: Sat, 20 Mar 2010 18:35:13 +0000 Subject: [PATCH] split extra.h into dlpipe.h which contains the curl bits and extra for the little helpful functions such as uppercase. Have also added section to trx.cxx to start sending status data though is missing some variables - needs testing --- src/main.cxx | 5 +-- src/misc/dlpipe.h | 102 +++++++++++++++++++++++++++++++++++++++++++++ src/misc/extra.h | 103 ++++------------------------------------------ src/trx/trx.cxx | 79 +++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+), 98 deletions(-) create mode 100644 src/misc/dlpipe.h diff --git a/src/main.cxx b/src/main.cxx index 7a297a07..6a6c2561 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -113,10 +113,7 @@ using namespace std; //jcoxon include files -#include -#include -#include -#include "misc/extra.h" +#include "misc/dlpipe.h" // string appname; diff --git a/src/misc/dlpipe.h b/src/misc/dlpipe.h new file mode 100644 index 00000000..1cd2d197 --- /dev/null +++ b/src/misc/dlpipe.h @@ -0,0 +1,102 @@ +#ifndef _EXTRA_H_ +#define _EXTRA_H_ +#include +#include +#include +#include + +//-------------------------------------// +/* RJH Pipe vars */ +int rjh_pfds[2]; +pid_t rjh_cpid; + + // This is the writer call back function used by curl + static int writer(char *data, size_t size, size_t nmemb, std::string *buffer) + { + // What we will return + int result = 0; + + // Is there anything in the buffer? + if (buffer != NULL) + { + // Append the data to the buffer + buffer->append(data, size * nmemb); + + // How much did we write? + result = size * nmemb; + } + + return result; + } + +void serverCommunicator() +{ + if (pipe(rjh_pfds) == -1) { perror("pipe"); exit(EXIT_FAILURE); } + + rjh_cpid = fork(); + + if (rjh_cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } + if (rjh_cpid == 0) { + + char c; + char buffer [2000]; + int charpos = 0; + + CURL *easyhandle_status; + CURLcode result; + + // ofstream fout; + + // fout.clear(); + // fout.open ("log.txt"); + // if (fout.fail()) { + // cout << "Failed to open log.txt\n"; + // } + + /* Close the write side of the pipe */ + close(rjh_pfds[1]); + + buffer[0] ='\0'; + + while (read(rjh_pfds[0], &c, 1) > 0) + { + buffer[charpos] = c; + if (charpos <1999) charpos ++; + + if (c == '\n') + { + + if (charpos > 0) + buffer[charpos-1] = '\0'; + else + buffer[0] = '\0'; + + charpos=0; + + printf(" CHILD: received \"%s\"\n", buffer); + + // fout << "Buffer string : '" << buffer << "'" << endl; + + if (strlen(buffer) > 0) + { + easyhandle_status = curl_easy_init(); + if(easyhandle_status) { + curl_easy_setopt(easyhandle_status, CURLOPT_POSTFIELDS, buffer); + curl_easy_setopt(easyhandle_status, CURLOPT_URL, "http://www.robertharrison.org/listen/listen.php"); + result = curl_easy_perform(easyhandle_status); /* post away! */ + cout << "result: " << result << "\n"; + curl_easy_cleanup(easyhandle_status); + } + } + buffer[0] = '\0'; + } + } + close(rjh_pfds[0]); + _exit (0); + } + + /* Close the read side of the pipe */ + close(rjh_pfds[0]); +} +//----------------------------------- +#endif \ No newline at end of file diff --git a/src/misc/extra.h b/src/misc/extra.h index 7e98e839..211fc9fd 100644 --- a/src/misc/extra.h +++ b/src/misc/extra.h @@ -1,100 +1,15 @@ #ifndef _EXTRA_H_ #define _EXTRA_H_ +#include -//void UpperCase(string& str); - -//-------------------------------------// -/* RJH Pipe vars */ -int rjh_pfds[2]; -pid_t rjh_cpid; - - // This is the writer call back function used by curl - static int writer(char *data, size_t size, size_t nmemb, std::string *buffer) - { - // What we will return - int result = 0; - - // Is there anything in the buffer? - if (buffer != NULL) - { - // Append the data to the buffer - buffer->append(data, size * nmemb); - - // How much did we write? - result = size * nmemb; - } - - return result; - } - -void serverCommunicator() +//jcoxon +void UpperCase(string& str) { - if (pipe(rjh_pfds) == -1) { perror("pipe"); exit(EXIT_FAILURE); } - - rjh_cpid = fork(); - - if (rjh_cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } - if (rjh_cpid == 0) { - - char c; - char buffer [2000]; - int charpos = 0; - - CURL *easyhandle_status; - CURLcode result; - - // ofstream fout; - - // fout.clear(); - // fout.open ("log.txt"); - // if (fout.fail()) { - // cout << "Failed to open log.txt\n"; - // } - - /* Close the write side of the pipe */ - close(rjh_pfds[1]); - - buffer[0] ='\0'; - - while (read(rjh_pfds[0], &c, 1) > 0) - { - buffer[charpos] = c; - if (charpos <1999) charpos ++; - - if (c == '\n') - { - - if (charpos > 0) - buffer[charpos-1] = '\0'; - else - buffer[0] = '\0'; - - charpos=0; - - printf(" CHILD: received \"%s\"\n", buffer); - - // fout << "Buffer string : '" << buffer << "'" << endl; - - if (strlen(buffer) > 0) - { - easyhandle_status = curl_easy_init(); - if(easyhandle_status) { - curl_easy_setopt(easyhandle_status, CURLOPT_POSTFIELDS, buffer); - curl_easy_setopt(easyhandle_status, CURLOPT_URL, "http://www.robertharrison.org/listen/listen.php"); - result = curl_easy_perform(easyhandle_status); /* post away! */ - cout << "result: " << result << "\n"; - curl_easy_cleanup(easyhandle_status); - } - } - buffer[0] = '\0'; - } - } - close(rjh_pfds[0]); - _exit (0); - } - - /* Close the read side of the pipe */ - close(rjh_pfds[0]); + for(int i = 0; i < str.length(); i++) + { + str[i] = toupper(str[i]); + } + return; } -//----------------------------------- +// #endif \ No newline at end of file diff --git a/src/trx/trx.cxx b/src/trx/trx.cxx index cdc44d3f..ae1afe51 100644 --- a/src/trx/trx.cxx +++ b/src/trx/trx.cxx @@ -51,6 +51,21 @@ LOG_FILE_SOURCE(debug::LOG_MODEM); using namespace std; +//New stuff added by jcoxon +#include +#include +#include "misc/extra.h"; + +time_t rawtime; +struct tm * timeinfo; + +time_t seconds; +int status_count = 901; //Why 1001? well as it'll trigger the status update to be sent when fldigi starts +int old_seconds = 0; + +char date_time [80]; +// + void trx_reset_loop(); void trx_start_modem_loop(); void trx_receive_loop(); @@ -89,6 +104,10 @@ void trx_trx_receive_loop() { size_t numread; int current_samplerate; + + //jcoxon + extern int rjh_pfds[2]; + // assert(powerof2(SCBLOCKSIZE)); if (unlikely(!active_modem)) { @@ -126,6 +145,66 @@ void trx_trx_receive_loop() active_modem->rx_init(); while (1) { + //New stuff added by jcoxon + if (status_count >= 1000) { + seconds = time (NULL); +#if !defined(__CYGWIN__) + cout << seconds << "\n"; +#endif + if (int(seconds) > old_seconds + 900) { + //Send status update +#if !defined(__CYGWIN__) + cout << "Send status update\n"; +#endif + + string identity_callsign = (progdefaults.myCall.empty() ? "UNKNOWN" : progdefaults.myCall.c_str()); + UpperCase (identity_callsign); + //string string_lat = (progdefaults.myLat.empty() ? "UNKNOWN" : progdefaults.myLat.c_str()); + string string_lat = "52.0"; + UpperCase (string_lat); + //string string_lon = (progdefaults.myLon.empty() ? "UNKNOWN" : progdefaults.myLon.c_str()); + string string_lon = "0.0"; + UpperCase (string_lon); + //string string_radio = (progdefaults.myRadio.empty() ? "UNKNOWN" : progdefaults.myRadio.c_str()); + string string_radio = "radio"; + UpperCase (string_radio); + string string_antenna = (progdefaults.myAntenna.empty() ? "UNKNOWN" : progdefaults.myAntenna.c_str()); + UpperCase (string_antenna); + //string string_payload = (progdefaults.flight_sel.empty() ? "UNKNOWN" : progdefaults.flight_sel.c_str()); + string string_payload = "Test"; + UpperCase (string_payload); + + time ( &rawtime ); + timeinfo = gmtime ( &rawtime ); + strftime(date_time,80,"%Y-%m-%d %H:%M:%S",timeinfo); +#if !defined(__CYGWIN__) + cout << date_time << "\n"; +#endif +//-------------------------------------------------------- + string dlfldigi_version = "r100"; //Please update with revision number +//------------------------------------------------------- + //ZZ,Callsign,Date Time,Lat,Lon,Radio,Antenna + string rx_data ="ZZ," + identity_callsign + "," + date_time + "," + string_lat + "," + string_lon + "," + string_radio + "," + string_antenna + "," + dlfldigi_version + "," + string_payload; + string postData = "string=" + rx_data + "&identity=" + identity_callsign + "\n"; + + //We really don't want people sending status updates from UNKNOWN - somehow need to remind people to change their callsign + if (identity_callsign != "UNKNOWN") { + const char* data = postData.c_str(); + write (rjh_pfds[1],data,strlen(data)); + } + else { +#if !defined(__CYGWIN__) + cout << "Need to enter a callsign, please go to 'Configure' then 'Operator' and add a callsign/nickname.\n"; +#endif + } + old_seconds = seconds; + } + status_count = 0; + } + else { + status_count++; + } + //-------------------------- try { numread = 0; while (numread < SCBLOCKSIZE && trx_state == STATE_RX)