From 1ffddf823ffd1fbfb9c86848fd803970db3ee6c2 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Wed, 24 Apr 2024 16:42:25 -0400 Subject: [PATCH 1/8] Fix and Consolidate Date and Time Functions (WIP); #255 --- include/dsd.h | 10 +++- src/dmr_bs.c | 55 +++++++--------------- src/dmr_ms.c | 69 +++++++-------------------- src/dmr_pdu.c | 38 ++++----------- src/dsd_file.c | 48 +++++-------------- src/dsd_frame_sync.c | 37 ++------------- src/dsd_gps.c | 62 ++++++++++++++++++++----- src/dsd_ncurses.c | 108 +++++++++++++++---------------------------- src/dsd_time.c | 71 ++++++++++++++++++++++++++++ src/edacs-fme.c | 53 +++++---------------- src/p25p2_frame.c | 22 ++------- 11 files changed, 240 insertions(+), 333 deletions(-) create mode 100644 src/dsd_time.c diff --git a/include/dsd.h b/include/dsd.h index be506ae..c3f5aad 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -1246,8 +1246,14 @@ void dmr_pdu (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DMR void dmr_reset_blocks (dsd_opts * opts, dsd_state * state); void dmr_lrrp (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DMR_PDU[]); void dmr_locn (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DMR_PDU[]); -char * getTimeL(void); -char * getDateL(void); + +//Time and Date Functions +char * getTime(); +char * getTimeC(); +char * getTimeN(time_t t); +char * getDate(); +char * getDateH(); +char * getDateN(time_t t); //dmr alg stuff void dmr_alg_reset (dsd_opts * opts, dsd_state * state); diff --git a/src/dmr_bs.c b/src/dmr_bs.c index 3245ef9..174ba0c 100644 --- a/src/dmr_bs.c +++ b/src/dmr_bs.c @@ -13,8 +13,9 @@ //processing voice and/or data on both BS slots (channels) simultaneously void dmrBS (dsd_opts * opts, dsd_state * state) { - int i, dibit; + char * timestr; + int i, dibit; char ambe_fr[4][24]; char ambe_fr2[4][24]; char ambe_fr3[4][24]; @@ -55,22 +56,6 @@ void dmrBS (dsd_opts * opts, dsd_state * state) uint8_t dummy_bits[196]; memset (dummy_bits, 0, sizeof(dummy_bits)); - //add time to mirror printFrameSync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } - //Init slot lights sprintf (state->slot1light, " slot1 "); sprintf (state->slot2light, " slot2 "); @@ -114,6 +99,8 @@ void dmrBS (dsd_opts * opts, dsd_state * state) cleanupAndExit (opts, state); } + timestr = getTimeC(); + memset (ambe_fr, 0, sizeof(ambe_fr)); memset (ambe_fr2, 0, sizeof(ambe_fr2)); memset (ambe_fr3, 0, sizeof(ambe_fr3)); @@ -279,7 +266,7 @@ void dmrBS (dsd_opts * opts, dsd_state * state) if ( strcmp (sync, DMR_BS_DATA_SYNC) == 0 ) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); if (internalslot == 0) { if (opts->inverted_dmr == 0) @@ -315,7 +302,7 @@ void dmrBS (dsd_opts * opts, dsd_state * state) //check to see if we are expecting a VC at this point vc > 7 if (strcmp (sync, DMR_BS_DATA_SYNC) != 0 && internalslot == 0 && vc1 > 6) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); //simplifying things // char polarity[3]; @@ -336,7 +323,7 @@ void dmrBS (dsd_opts * opts, dsd_state * state) //check to see if we are expecting a VC at this point vc > 7 if (strcmp (sync, DMR_BS_DATA_SYNC) != 0 && internalslot == 1 && vc2 > 6) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); //simplifying things // char polarity[3]; @@ -374,7 +361,7 @@ void dmrBS (dsd_opts * opts, dsd_state * state) skipcount = 0; //reset skip count if processing voice frames - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); //simplifying things char polarity[3]; @@ -585,7 +572,7 @@ void dmrBS (dsd_opts * opts, dsd_state * state) if (tact_okay != 1 || emb_ok != 1) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); fprintf (stderr,"Sync: DMR "); fprintf (stderr, "%s", KRED); fprintf (stderr, "| VOICE CACH/EMB ERR"); @@ -608,6 +595,8 @@ void dmrBS (dsd_opts * opts, dsd_state * state) } + free (timestr); + } //Process buffered half frame and 2nd half and then jump to full BS decoding @@ -643,22 +632,9 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) 11, 12, 2, 13, 14, 15, 3, 16, 4, 17, 18, 19, 5, 20, 21, 22, 6, 23 - }; - //add time to mirror printFrameSync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); + }; - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } + char * timestr = getTimeC(); //payload buffer //CACH + First Half Payload + Sync = 12 + 54 + 24 @@ -807,7 +783,7 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) fclose (pFile); } - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); char polarity[3]; char light[18]; @@ -909,7 +885,7 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) //if we have a tact err, then produce sync pattern/err message if (tact_okay != 1 || sync_okay != 1) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); fprintf (stderr,"Sync: DMR "); fprintf (stderr, "%s", KRED); fprintf (stderr, "| VOICE CACH/SYNC ERR"); @@ -931,5 +907,6 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) dmr_reset_blocks (opts, state); } + free (timestr); } diff --git a/src/dmr_ms.c b/src/dmr_ms.c index df59c2f..f527563 100644 --- a/src/dmr_ms.c +++ b/src/dmr_ms.c @@ -13,6 +13,8 @@ void dmrMS (dsd_opts * opts, dsd_state * state) { + char * timestr = getTimeC(); + int i, j, dibit; char ambe_fr[4][24]; char ambe_fr2[4][24]; @@ -55,22 +57,6 @@ void dmrMS (dsd_opts * opts, dsd_state * state) uint8_t dummy_bits[196]; memset (dummy_bits, 0, sizeof (dummy_bits)); - //add time to mirror printFrameSync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } - vc = 2; //Hardset variables for MS/Mono @@ -187,7 +173,7 @@ void dmrMS (dsd_opts * opts, dsd_state * state) if ( strcmp (sync, DMR_RC_DATA_SYNC) == 0) { state->dmr_ms_rc = 1; - fprintf (stderr, "%s ", getTime()); + fprintf (stderr, "%s ", timestr); if (opts->inverted_dmr == 0) { fprintf (stderr,"Sync: +DMR MS/DM MODE/MONO "); @@ -387,13 +373,17 @@ void dmrMS (dsd_opts * opts, dsd_state * state) state->dmr_ms_mode = 0; state->dmr_ms_rc = 0; state->directmode = 0; //flag off - + + free(timestr); } //collect buffered 1st half and get 2nd half voice payload and then jump to full MS Voice decoding. void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) { + + char * timestr = getTimeC(); + int i, dibit; int *dibit_p; @@ -417,22 +407,6 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) char cachdata[25]; UNUSED(cachdata); - //add time to mirror sync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } - state->dmrburstL = 16; state->currentslot = 0; //force to slot 0 @@ -568,7 +542,7 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) fclose (pFile); } - fprintf (stderr, "%s ", getTime()); + fprintf (stderr, "%s ", timestr); if (opts->inverted_dmr == 0) { fprintf (stderr,"Sync: +DMR MS/DM MODE/MONO "); @@ -644,6 +618,7 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) //errors due to skipping other slot // cach_err = dmr_cach (opts, state, cachdata); + free (timestr); skipDibit (opts, state, 144); //skip to next TDMA slot dmrMS (opts, state); //bootstrap into full TDMA frame @@ -653,27 +628,13 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) //simplied to a simple data collector, and then passed on to dmr_data_sync for the usual processing void dmrMSData (dsd_opts * opts, dsd_state * state) { + + char * timestr = getTimeC(); + int i; int dibit; int *dibit_p; - //add time to mirror sync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } - - //CACH + First Half Payload + Sync = 12 + 54 + 24 dibit_p = state->dmr_payload_p - 90; for (i = 0; i < 90; i++) //90 @@ -691,7 +652,7 @@ void dmrMSData (dsd_opts * opts, dsd_state * state) state->dmr_stereo_payload[i+90] = dibit; } - fprintf (stderr, "%s ", getTime()); + fprintf (stderr, "%s ", timestr); if (opts->inverted_dmr == 0) { fprintf (stderr,"Sync: +DMR MS/DM MODE/MONO "); @@ -728,4 +689,6 @@ void dmrMSData (dsd_opts * opts, dsd_state * state) state->dmr_stereo_payload[i+66] = 1; ////set to one so first frame will fail intentionally instead of zero fill } + free (timestr); + } diff --git a/src/dmr_pdu.c b/src/dmr_pdu.c index c37c8bc..a836657 100644 --- a/src/dmr_pdu.c +++ b/src/dmr_pdu.c @@ -8,33 +8,6 @@ #include "dsd.h" -char * getTimeL(void) //get pretty hh:mm:ss timestamp -{ - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; -} - -char * getDateL(void) { - char datename[120]; - char * curr2; - struct tm * to; - time_t t; - t = time(NULL); - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y/%m/%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - void dmr_pdu (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DMR_PDU[]) { @@ -274,16 +247,21 @@ void dmr_lrrp (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DM FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + char * timestr = getTime(); + char * datestr = getDate(); + //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); //write current date/time if not present in LRRP data - if (!year) fprintf (pFile, "%s\t", getDateL() ); //current date, only add this IF no included timestamp in LRRP data? - if (!year) fprintf (pFile, "%s\t", getTimeL() ); //current timestamp, only add this IF no included timestamp in LRRP data? + if (!year) fprintf (pFile, "%s\t", datestr ); //current date, only add this IF no included timestamp in LRRP data? + if (!year) fprintf (pFile, "%s\t", timestr ); //current timestamp, only add this IF no included timestamp in LRRP data? if (year) fprintf (pFile, "%04d/%02d/%02d\t%02d:%02d:%02d\t", year, month, day, hour, minute, second); //add timestamp from decoded audio if available //write data header source if not available in lrrp data if (!source) fprintf (pFile, "%08lld\t", state->dmr_lrrp_source[state->currentslot]); //source address from data header if (source) fprintf (pFile, "%08d\t", source); //add source form decoded audio if available, else its from the header - + + free (timestr); + free (datestr); } if (pot_report) diff --git a/src/dsd_file.c b/src/dsd_file.c index 67607e5..21bda4c 100644 --- a/src/dsd_file.c +++ b/src/dsd_file.c @@ -17,38 +17,6 @@ #include "dsd.h" -//fix from YorgosTheodorakis fork -- https://github.com/YorgosTheodorakis/dsd-fme/commit/7884ee555521a887d388152b3b1f11f20433a94b -char * getTimeF(void) //get pretty hhmmss timestamp -{ - char * curr = (char *) malloc(9); - time_t t = time(NULL); - struct tm * ptm = localtime(& t); - sprintf( - curr, - "%02d%02d%02d", - ptm->tm_hour, - ptm->tm_min, - ptm->tm_sec - ); - return curr; -} - -char * getDateF(void) { - #ifdef AERO_BUILD - char datename[80]; - #else - char datename[99]; - #endif - char * curr2; - struct tm * to; - time_t t; - t = time(NULL); - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y%m%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - void saveImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d) { int i, j, k; @@ -397,7 +365,10 @@ void openMbeOutFile (dsd_opts * opts, dsd_state * state) int i, j; char ext[5]; char * timestr; //add timestr here, so we can assign it and also free it to prevent memory leak - timestr = getTimeF(); + char * datestr; + + timestr = getTime(); + datestr = getDate(); //phase 1 and provoice if ( (state->synctype == 0) || (state->synctype == 1) || (state->synctype == 14) || (state->synctype == 15) ) @@ -423,7 +394,7 @@ void openMbeOutFile (dsd_opts * opts, dsd_state * state) state->tgcount = 0; - sprintf (opts->mbe_out_file, "%s %s S1%s", getDateF(), timestr, ext); + sprintf (opts->mbe_out_file, "%s %s S1%s", datestr, timestr, ext); sprintf (opts->mbe_out_path, "%s%s", opts->mbe_out_dir, opts->mbe_out_file); @@ -439,6 +410,7 @@ void openMbeOutFile (dsd_opts * opts, dsd_state * state) fflush (opts->mbe_out_f); free (timestr); //free allocated memory to prevent memory leak + free (datestr); } void openMbeOutFileR (dsd_opts * opts, dsd_state * state) @@ -447,7 +419,10 @@ void openMbeOutFileR (dsd_opts * opts, dsd_state * state) int i, j; char ext[5]; char * timestr; //add timestr here, so we can assign it and also free it to prevent memory leak - timestr = getTimeF(); + char * datestr; + + timestr = getTime(); + datestr = getDate(); //phase 1 and provoice if ( (state->synctype == 0) || (state->synctype == 1) || (state->synctype == 14) || (state->synctype == 15) ) @@ -473,7 +448,7 @@ void openMbeOutFileR (dsd_opts * opts, dsd_state * state) state->tgcount = 0; - sprintf (opts->mbe_out_fileR, "%s %s S2%s", getDateF(), timestr, ext); + sprintf (opts->mbe_out_fileR, "%s %s S2%s", datestr, timestr, ext); sprintf (opts->mbe_out_path, "%s%s", opts->mbe_out_dir, opts->mbe_out_fileR); @@ -489,6 +464,7 @@ void openMbeOutFileR (dsd_opts * opts, dsd_state * state) fflush (opts->mbe_out_fR); free (timestr); //free allocated memory to prevent memory leak + free (datestr); } void openWavOutFile (dsd_opts * opts, dsd_state * state) diff --git a/src/dsd_frame_sync.c b/src/dsd_frame_sync.c index e10f3cc..d9245e8 100644 --- a/src/dsd_frame_sync.c +++ b/src/dsd_frame_sync.c @@ -18,45 +18,14 @@ #include "dsd.h" #include -char * getTime(void) //get pretty hh:mm:ss timestamp -{ - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; -} - -char * getDate(void) { - #ifdef AERO_BUILD - char datename[80]; - #else - char datename[99]; - #endif - char * curr2; - struct tm * to; - time_t t; - t = time(NULL); - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y-%m-%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - void printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, char *modulation) { UNUSED3(state, offset, modulation); - + char * timestr = getTimeC(); if (opts->verbose > 0) { - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); fprintf (stderr,"Sync: %s ", frametype); } if (opts->verbose > 2) @@ -72,6 +41,8 @@ printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, //fprintf (stderr,"g: %f ", state->aout_gain); } + free (timestr); + } int diff --git a/src/dsd_gps.c b/src/dsd_gps.c index 22e6dc7..ea3afd1 100644 --- a/src/dsd_gps.c +++ b/src/dsd_gps.c @@ -119,10 +119,14 @@ void lip_protocol_decoder (dsd_opts * opts, dsd_state * state, uint8_t * input) FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + + char * datestr = getDate(); + char * timestr = getTime(); + //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); fprintf (pFile, "%08d\t", add_hash); fprintf (pFile, "%.5lf\t", latitude); fprintf (pFile, "%.5lf\t", longitude); @@ -131,6 +135,9 @@ void lip_protocol_decoder (dsd_opts * opts, dsd_state * state, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); + free (datestr); + free (timestr); + } } @@ -219,14 +226,18 @@ void nmea_iec_61162_1 (dsd_opts * opts, dsd_state * state, uint8_t * input, uint FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + + char * datestr = getDate(); + char * timestr = getTime(); + int s = (int)fkph; //rounded interger format for the log report int a = 0; if (type == 2) a = nmea_cog; //long format only //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); //could switch to UTC time if desired, but would require local user offset + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); //could switch to UTC time if desired, but would require local user offset fprintf (pFile, "%08d\t", src); fprintf (pFile, "%.6lf\t", latitude); fprintf (pFile, "%.6lf\t", longitude); @@ -235,6 +246,9 @@ void nmea_iec_61162_1 (dsd_opts * opts, dsd_state * state, uint8_t * input, uint fprintf (pFile, "\n"); fclose (pFile); + free (datestr); + free (timestr); + } } @@ -337,14 +351,18 @@ void nmea_harris (dsd_opts * opts, dsd_state * state, uint8_t * input, uint32_t FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + + char * datestr = getDate(); + char * timestr = getTime(); + //rounded interger formats for the log report int s = (int)fkph; int a = nmea_cog; //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); //could switch to UTC time on PDU if desired + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); //could switch to UTC time on PDU if desired fprintf (pFile, "%08d\t", src); fprintf (pFile, "%.6lf\t", latitude); fprintf (pFile, "%.6lf\t", longitude); @@ -353,6 +371,9 @@ void nmea_harris (dsd_opts * opts, dsd_state * state, uint8_t * input, uint32_t fprintf (pFile, "\n"); fclose (pFile); + free (datestr); + free (timestr); + } //NOTE: There seems to be a few more octets left undecoded in this PDU @@ -464,10 +485,14 @@ void harris_gps(dsd_opts * opts, dsd_state * state, int slot, uint8_t * input) FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + + char * datestr = getDate(); + char * timestr = getTime(); + //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); fprintf (pFile, "%08d\t", src); fprintf (pFile, "%.6lf\t", lat_dec); fprintf (pFile, "%.6lf\t", lon_dec); @@ -476,6 +501,9 @@ void harris_gps(dsd_opts * opts, dsd_state * state, int slot, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); + free (datestr); + free (timestr); + } //NOTE: Thanks to DSheirer (SDRTrunk) for helping me work out a few of the things in here @@ -561,14 +589,18 @@ void dmr_embedded_gps (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[]) FILE * pFile; //file pointer if (opts->lrrp_file_output == 1) { + + char * datestr = getDate(); + char * timestr = getTime(); + int src = 0; if (slot == 0) src = state->lasttg; if (slot == 1) src = state->lasttgR; //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); fprintf (pFile, "%08d\t", src); fprintf (pFile, "%.5lf\t", latitude); fprintf (pFile, "%.5lf\t", longitude); @@ -667,10 +699,13 @@ void apx_embedded_gps (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[]) if (slot == 0) src = state->lastsrc; if (slot == 1) src = state->lastsrcR; + char * datestr = getDate(); + char * timestr = getTime(); + //open file by name that is supplied in the ncurses terminal, or cli pFile = fopen (opts->lrrp_out_file, "a"); - fprintf (pFile, "%s\t", getDateL() ); - fprintf (pFile, "%s\t", getTimeL() ); + fprintf (pFile, "%s\t", datestr ); + fprintf (pFile, "%s\t", timestr ); fprintf (pFile, "%08d\t", src); fprintf (pFile, "%.5lf\t", latitude); fprintf (pFile, "%.5lf\t", longitude); @@ -679,6 +714,9 @@ void apx_embedded_gps (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[]) fprintf (pFile, "\n"); fclose (pFile); + free (datestr); + free (timestr); + } } diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 7874488..2cea676 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -31,6 +31,9 @@ struct stat st_wav = {0}; static char alias_ch[10][50]; int reset = 0; char * timestr; +char * datestr; +char * timestrC; +char * datestrH; int tg; int tgR; int tgn; @@ -256,65 +259,8 @@ void beeper (dsd_opts * opts, dsd_state * state, int lr) } -char * getDateN(void) { - #ifdef AERO_BUILD - char datename[80]; - #else - char datename[99]; - #endif - char * curr2; - struct tm * to; - time_t t; - t = time(NULL); - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y%m%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - time_t nowN; -//fix from YorgosTheodorakis fork -- https://github.com/YorgosTheodorakis/dsd-fme/commit/7884ee555521a887d388152b3b1f11f20433a94b -char * getTimeN(void) //get pretty hhmmss timestamp -{ - char * curr = (char *) malloc(9); - time_t t = time(NULL); - struct tm * ptm = localtime(& t); - sprintf( - curr, - "%02d%02d%02d", - ptm->tm_hour, - ptm->tm_min, - ptm->tm_sec - ); - return curr; -} - -//this version is broken now in Cygwin with the 80 value, reverted back -char * getDateC(time_t t) { - char datename[99]; - char * curr2; - struct tm * to; - - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y-%m-%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - -char * getTimeC(time_t t) //get pretty hh:mm:ss timestamp -{ - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; -} - int isCustomAfsString(dsd_state * state) { return state->edacs_a_bits != 4 || state->edacs_f_bits != 4 || state->edacs_s_bits != 3; } @@ -710,7 +656,7 @@ void ncursesMenu (dsd_opts * opts, dsd_state * state) print_menuc(test_win, highlightc); if (choicec == 2) { - sprintf (opts->wav_out_file, "%s %s DSD-FME-DECODED.wav", getDateN(), timestr); + sprintf (opts->wav_out_file, "%s %s DSD-FME-DECODED.wav", datestr, timestr); openWavOutFile (opts, state); } if (choicec == 3) @@ -2004,7 +1950,14 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) uint8_t idas = 0; int level = 0; int c = 0; - timestr = getTimeN(); + + //for filenames (no colons, etc) + timestr = getTime(); + datestr = getDate(); + + //for display + timestrC = getTimeC(); + datestrH = getDateH(); if (opts->audio_in_type != 1) //can't run getch/menu when using STDIN - { @@ -2187,7 +2140,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) { //close old first, assign name based on time and radio, open wav file closeWavOutFileL (opts, state); - sprintf (opts->wav_out_file, "./%s/%s %s NXDN - RAN %d - TGT %d - SRC %d.wav", opts->wav_out_dir, getDateN(), timestr, rn, tgn, src); + sprintf (opts->wav_out_file, "./%s/%s %s NXDN - RAN %d - TGT %d - SRC %d.wav", opts->wav_out_dir, datestr, timestr, rn, tgn, src); openWavOutFileL (opts, state); //testing for now, will want to move to per call later } @@ -2232,7 +2185,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) { //close old first, assign name based on time and radio, open wav file closeWavOutFileL (opts, state); - sprintf (opts->wav_out_file, "./%s/%s %s MS - CC %d - TG %d - RD %d.wav", opts->wav_out_dir, getDateN(), timestr, dcc, tg, rd); + sprintf (opts->wav_out_file, "./%s/%s %s MS - CC %d - TG %d - RD %d.wav", opts->wav_out_dir, datestr, timestr, dcc, tg, rd); openWavOutFileL (opts, state); //testing for now, will want to move to per call later } @@ -2275,7 +2228,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //close old first, assign name based on time and radio, open wav file closeWavOutFileL (opts, state); - sprintf (opts->wav_out_file, "./%s/%s %s CC %d - TG %d - RD %d.wav", opts->wav_out_dir, getDateN(), timestr, dcc, tg, rd); + sprintf (opts->wav_out_file, "./%s/%s %s CC %d - TG %d - RD %d.wav", opts->wav_out_dir, datestr, timestr, dcc, tg, rd); openWavOutFileL (opts, state); //testing for now, will want to move to per call later } @@ -2317,7 +2270,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) { //close old first, assign name based on time and radio, open wav file closeWavOutFileR (opts, state); - sprintf (opts->wav_out_fileR, "./%s/%s %s CC %d - TG %d - RD %d.wav", opts->wav_out_dir, getDateN(), timestr, dcc, tgR, rdR); + sprintf (opts->wav_out_fileR, "./%s/%s %s CC %d - TG %d - RD %d.wav", opts->wav_out_dir, datestr, timestr, dcc, tgR, rdR); openWavOutFileR (opts, state); //testing for now, will want to move to per call later } @@ -2358,7 +2311,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) { //close old first, assign name based on time and radio, open wav file closeWavOutFileL (opts, state); - sprintf (opts->wav_out_file, "./%s/%s %s P1 - NAC %X - TGT %d - SRC %d.wav", opts->wav_out_dir, getDateN(), timestr, nc, tg, rd); + sprintf (opts->wav_out_file, "./%s/%s %s P1 - NAC %X - TGT %d - SRC %d.wav", opts->wav_out_dir, datestr, timestr, nc, tg, rd); openWavOutFileL (opts, state); //testing for now, will want to move to per call later } @@ -3850,11 +3803,15 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //only print if a valid time was assigned to the matrix, and not EDACS/PV, and source is not zero if ( ((time(NULL) - call_matrix[9-j][5]) < 999999) && call_matrix[9-j][0] != 14 && call_matrix[9-j][0] != 15 && call_matrix[9-j][0] != 37 && call_matrix[9-j][0] != 38 && call_matrix[9-j][2] != 0) // { - // printw ("| %s ", SyncTypes[call_matrix[9-j][0]]); //sync type + char * timeCH = getTimeN(call_matrix[9-j][5]); + char * dateCH = getDateN(call_matrix[9-j][5]); printw ("| "); - printw ("%s ", getDateC(call_matrix[9-j][5]) ); - printw ("%s ", getTimeC(call_matrix[9-j][5]) ); + printw ("%s ", dateCH); + printw ("%s ", timeCH); + + free (dateCH); + free (timeCH); if (lls == 28 || lls == 29) { @@ -3925,8 +3882,16 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) if (call_matrix[j][2] != 0) { printw ("| "); - printw ("%s ", getDateC(call_matrix[j][5]) ); - printw ("%s ", getTimeC(call_matrix[j][5]) ); + char * timeCHE = getTimeN(call_matrix[j][5]); + char * dateCHE = getDateN(call_matrix[j][5]); + + printw ("| "); + printw ("%s ", dateCHE); + printw ("%s ", timeCHE); + + free (dateCHE); + free (timeCHE); + printw ("LCN [%2lld] ", call_matrix[j][1]); if (state->ea_mode == 1) { @@ -4309,7 +4274,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) if (c == 82) //'R', save symbol capture bin with date/time string as name { - sprintf (opts->symbol_out_file, "%s %s.bin", getDateN(), timestr); + sprintf (opts->symbol_out_file, "%s %s.bin", datestr, timestr); openSymbolOutFile (opts, state); } @@ -4881,6 +4846,9 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //TODO: Redo this in edacs and file, making a seperate char * timestr instead of calling GetTime //directly from the sprintf command free (timestr); + free (datestr); + free (timestrC); + free (datestrH); } //end ncursesPrinter diff --git a/src/dsd_time.c b/src/dsd_time.c new file mode 100644 index 0000000..e04dabb --- /dev/null +++ b/src/dsd_time.c @@ -0,0 +1,71 @@ +/*------------------------------------------------------------------------------- + * dsd_time.c + * Time and Date Functions + * + * LWVMOBILE + * 2024-04 DSD-FME Florida Man Edition + *-----------------------------------------------------------------------------*/ + +//TODO: Make sure everything still works as intended, no random crashes on free (timestr) +//make sure no other loose or random time functions embedded in other files or functions, etc + +#include "dsd.h" + +//get hhmmss timestamp no colon (file operations) +char * getTime() +{ + char * curr = (char *) malloc(9); + time_t t = time(NULL); + struct tm * ptm = localtime(& t); + sprintf(curr,"%02d%02d%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + return curr; +} + +//get hh:mm:ss timestamp with colon (Ncurses Display) +char * getTimeC() +{ + char * curr = (char *) malloc(9); + time_t t = time(NULL); + struct tm * ptm = localtime(& t); + sprintf(curr, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + return curr; +} + +//get hh:mm:ss timestamp with colon (Ncurses Display) +char * getTimeN(time_t t) +{ + char * curr = (char *) malloc(9); + struct tm * ptm = localtime(& t); + sprintf(curr, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + return curr; +} + +//get YYYYMMDD without hyphen (file operations) +char * getDate() +{ + char * curr = (char *) malloc(25); + time_t t = time(NULL); + struct tm * ptm = localtime(& t); + sprintf(curr,"%04d%02d%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); + return curr; +} + +//get YYYY-MM-DD with hyphen (Ncurses Display) +char * getDateH() +{ + char * curr = (char *) malloc(27); + time_t t = time(NULL); + struct tm * ptm = localtime(& t); + sprintf(curr, "%04d-%02d-%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); + return curr; +} + +//get YYYY-MM-DD with hyphen (Ncurses Call History) +char * getDateN(time_t t) +{ + char * curr = (char *) malloc(27); + struct tm * ptm = localtime(& t); + sprintf(curr, "%04d-%02d-%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); + return curr; +} + diff --git a/src/edacs-fme.c b/src/edacs-fme.c index 46c937d..2914683 100644 --- a/src/edacs-fme.c +++ b/src/edacs-fme.c @@ -21,38 +21,6 @@ *-----------------------------------------------------------------------------*/ #include "dsd.h" -char * getDateE(void) { - #ifdef AERO_BUILD - char datename[80]; - #else - char datename[99]; - #endif - char * curr2; - struct tm * to; - time_t t; - t = time(NULL); - to = localtime( & t); - strftime(datename, sizeof(datename), "%Y%m%d", to); - curr2 = strtok(datename, " "); - return curr2; -} - -//fix from YorgosTheodorakis fork -- https://github.com/YorgosTheodorakis/dsd-fme/commit/7884ee555521a887d388152b3b1f11f20433a94b -char * getTimeE(void) //get pretty hhmmss timestamp -{ - char * curr = (char *) malloc(9); - time_t t = time(NULL); - struct tm * ptm = localtime(& t); - sprintf( - curr, - "%02d%02d%02d", - ptm->tm_hour, - ptm->tm_min, - ptm->tm_sec - ); - return curr; -} - char * getLcnStatusString(int lcn) { if (lcn == 26 || lcn == 27) @@ -473,7 +441,9 @@ void edacs(dsd_opts * opts, dsd_state * state) state->edacs_s_mask = (1 << state->edacs_s_bits) - 1; char * timestr; //add timestr here, so we can assign it and also free it to prevent memory leak - timestr = getTimeE(); + char * datestr; + timestr = getTime(); + datestr = getDate(); state->edacs_vc_lcn = -1; //init on negative for ncurses and tuning @@ -1000,7 +970,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here, should probably check as well to make sure we have a valid trunking method active (rigctl, rtl) if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %d SRC %d.wav", getDateE(), timestr, state->edacs_site_id, group, source); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %d SRC %d.wav", datestr, timestr, state->edacs_site_id, group, source); if (is_digital == 1) openWavOutFile (opts, state); else @@ -1115,7 +1085,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here, should probably check as well to make sure we have a valid trunking method active (rigctl, rtl) if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %d SRC %d I-Call.wav", getDateE(), timestr, state->edacs_site_id, target, source); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %d SRC %d I-Call.wav", datestr, timestr, state->edacs_site_id, target, source); if (is_digital == 1) openWavOutFile (opts, state); else @@ -1221,7 +1191,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here, should probably check as well to make sure we have a valid trunking method active (rigctl, rtl) if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld SRC %d All-Call.wav", getDateE(), timestr, state->edacs_site_id, source); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld SRC %d All-Call.wav", datestr, timestr, state->edacs_site_id, source); if (is_digital == 1) openWavOutFile (opts, state); else @@ -1400,7 +1370,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %04d SRC %05d.wav", getDateE(), timestr, state->edacs_site_id, group, lid); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %04d SRC %05d.wav", datestr, timestr, state->edacs_site_id, group, lid); if (is_digital == 0) openWavOutFile48k (opts, state); //analog at 48k else openWavOutFile (opts, state); //digital } @@ -1652,8 +1622,8 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - if (is_individual == 0) sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %04d SRC %05d.wav", getDateE(), timestr, state->edacs_site_id, target, state->lastsrc); - else sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %05d SRC %05d I-Call.wav", getDateE(), timestr, state->edacs_site_id, target, state->lastsrc); + if (is_individual == 0) sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TG %04d SRC %05d.wav", datestr, timestr, state->edacs_site_id, target, state->lastsrc); + else sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %05d SRC %05d I-Call.wav", datestr, timestr, state->edacs_site_id, target, state->lastsrc); if (is_digital == 0) openWavOutFile48k (opts, state); //analog at 48k else openWavOutFile (opts, state); //digital } @@ -1763,7 +1733,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %05d SRC %05d I-Call.wav", getDateE(), timestr, state->edacs_site_id, target, state->lastsrc); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld TGT %05d SRC %05d I-Call.wav", datestr, timestr, state->edacs_site_id, target, state->lastsrc); if (is_digital == 0) openWavOutFile48k (opts, state); //analog at 48k else openWavOutFile (opts, state); //digital } @@ -2056,7 +2026,7 @@ void edacs(dsd_opts * opts, dsd_state * state) //openwav file and do per call right here if (opts->dmr_stereo_wav == 1 && (opts->use_rigctl == 1 || opts->audio_in_type == 3)) { - sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld SRC %05d All-Call.wav", getDateE(), timestr, state->edacs_site_id, state->lastsrc); + sprintf (opts->wav_out_file, "./WAV/%s %s EDACS Site %lld SRC %05d All-Call.wav", datestr, timestr, state->edacs_site_id, state->lastsrc); if (is_digital == 0) openWavOutFile48k (opts, state); //analog at 48k else openWavOutFile (opts, state); //digital } @@ -2166,6 +2136,7 @@ void edacs(dsd_opts * opts, dsd_state * state) free (timestr); //free allocated memory to prevent memory leak + free (datestr); fprintf (stderr, "\n"); } diff --git a/src/p25p2_frame.c b/src/p25p2_frame.c index a01db06..cc408ac 100644 --- a/src/p25p2_frame.c +++ b/src/p25p2_frame.c @@ -846,22 +846,6 @@ void process_P2_DUID (dsd_opts * opts, dsd_state * state) vc_counter = 0; int err_counter = 0; - //add time to mirror printFrameSync - char * getTime(void) //get pretty hh:mm:ss timestamp - { - time_t t = time(NULL); - - char * curr; - char * stamp = asctime(localtime( & t)); - - curr = strtok(stamp, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - curr = strtok(NULL, " "); - - return curr; - } - for (ts_counter = 0; ts_counter < 4; ts_counter++) //12 { duid_decoded = -2; @@ -884,9 +868,13 @@ void process_P2_DUID (dsd_opts * opts, dsd_state * state) } duid_decoded = duid_lookup[p2_duid_complete]; + char * timestr = getTimeC(); + fprintf (stderr, "\n"); - fprintf (stderr,"%s ", getTime()); + fprintf (stderr,"%s ", timestr); fprintf (stderr, " P25p2 "); + + free(timestr); if (state->currentslot == 0 && duid_decoded != 3 && duid_decoded != 12 && duid_decoded != 13 && duid_decoded != 4) { From ceb695af01041768dff60bc47d62c3aa6a7f48b3 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 09:24:01 -0400 Subject: [PATCH 2/8] NCURSES: Remove Extra Fence Post on EDACS Call History; --- src/dsd_ncurses.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 2cea676..fe6dd54 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -3885,7 +3885,6 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) char * timeCHE = getTimeN(call_matrix[j][5]); char * dateCHE = getDateN(call_matrix[j][5]); - printw ("| "); printw ("%s ", dateCHE); printw ("%s ", timeCHE); From ab9a9ddab35270a846cac0508633f5736352d177 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 11:06:13 -0400 Subject: [PATCH 3/8] TIME: NULL Check Before free(timestr); --- src/dmr_bs.c | 20 ++++++++++++++++++-- src/dmr_ms.c | 6 +++--- src/dmr_pdu.c | 4 ++-- src/dsd_file.c | 8 ++++---- src/dsd_frame_sync.c | 23 +++++++++-------------- src/dsd_gps.c | 20 ++++++++++---------- src/dsd_ncurses.c | 40 ++++++++++++++++++++-------------------- src/edacs-fme.c | 4 ++-- src/p25p2_frame.c | 2 +- 9 files changed, 69 insertions(+), 58 deletions(-) diff --git a/src/dmr_bs.c b/src/dmr_bs.c index 174ba0c..8d2775b 100644 --- a/src/dmr_bs.c +++ b/src/dmr_bs.c @@ -549,6 +549,15 @@ void dmrBS (dsd_opts * opts, dsd_state * state) ncursesPrinter(opts, state); } + //both here and at bottom SHOULD be okay with the NULL check, if not, disable this one + if (timestr != NULL) + { + //debug + // fprintf (stderr, " FREE: 1; "); + free (timestr); + } + // else fprintf (stderr, " NO FREE: 1; "); + } // while loop END: @@ -595,7 +604,14 @@ void dmrBS (dsd_opts * opts, dsd_state * state) } - free (timestr); + //second free + if (timestr != NULL) + { + //debug + // fprintf (stderr, " FREE: 2; "); + free (timestr); + } +// else fprintf (stderr, " NO FREE: 2; "); } @@ -907,6 +923,6 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) dmr_reset_blocks (opts, state); } - free (timestr); + if (timestr != NULL) free (timestr); } diff --git a/src/dmr_ms.c b/src/dmr_ms.c index f527563..dacba16 100644 --- a/src/dmr_ms.c +++ b/src/dmr_ms.c @@ -374,7 +374,7 @@ void dmrMS (dsd_opts * opts, dsd_state * state) state->dmr_ms_rc = 0; state->directmode = 0; //flag off - free(timestr); + if (timestr != NULL) free (timestr); } @@ -618,7 +618,7 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) //errors due to skipping other slot // cach_err = dmr_cach (opts, state, cachdata); - free (timestr); + if (timestr != NULL) free (timestr); skipDibit (opts, state, 144); //skip to next TDMA slot dmrMS (opts, state); //bootstrap into full TDMA frame @@ -689,6 +689,6 @@ void dmrMSData (dsd_opts * opts, dsd_state * state) state->dmr_stereo_payload[i+66] = 1; ////set to one so first frame will fail intentionally instead of zero fill } - free (timestr); + if (timestr != NULL) free (timestr); } diff --git a/src/dmr_pdu.c b/src/dmr_pdu.c index a836657..38bc16e 100644 --- a/src/dmr_pdu.c +++ b/src/dmr_pdu.c @@ -260,8 +260,8 @@ void dmr_lrrp (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DM if (!source) fprintf (pFile, "%08lld\t", state->dmr_lrrp_source[state->currentslot]); //source address from data header if (source) fprintf (pFile, "%08d\t", source); //add source form decoded audio if available, else its from the header - free (timestr); - free (datestr); + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); } if (pot_report) diff --git a/src/dsd_file.c b/src/dsd_file.c index 21bda4c..32c09fa 100644 --- a/src/dsd_file.c +++ b/src/dsd_file.c @@ -409,8 +409,8 @@ void openMbeOutFile (dsd_opts * opts, dsd_state * state) fprintf (opts->mbe_out_f, "%s", ext); fflush (opts->mbe_out_f); - free (timestr); //free allocated memory to prevent memory leak - free (datestr); + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); } void openMbeOutFileR (dsd_opts * opts, dsd_state * state) @@ -463,8 +463,8 @@ void openMbeOutFileR (dsd_opts * opts, dsd_state * state) fprintf (opts->mbe_out_fR, "%s", ext); fflush (opts->mbe_out_fR); - free (timestr); //free allocated memory to prevent memory leak - free (datestr); + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); } void openWavOutFile (dsd_opts * opts, dsd_state * state) diff --git a/src/dsd_frame_sync.c b/src/dsd_frame_sync.c index d9245e8..924469c 100644 --- a/src/dsd_frame_sync.c +++ b/src/dsd_frame_sync.c @@ -22,26 +22,21 @@ void printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, char *modulation) { UNUSED3(state, offset, modulation); + char * timestr = getTimeC(); if (opts->verbose > 0) - { - fprintf (stderr,"%s ", timestr); - fprintf (stderr,"Sync: %s ", frametype); - } + { + fprintf (stderr,"%s ", timestr); + fprintf (stderr,"Sync: %s ", frametype); + } if (opts->verbose > 2) - { - //fprintf (stderr,"o: %4i ", offset); - } + //fprintf (stderr,"o: %4i ", offset); if (opts->verbose > 1) - { - //fprintf (stderr,"mod: %s ", modulation); - } + //fprintf (stderr,"mod: %s ", modulation); if (opts->verbose > 2) - { - //fprintf (stderr,"g: %f ", state->aout_gain); - } + //fprintf (stderr,"g: %f ", state->aout_gain); - free (timestr); + if (timestr != NULL) free (timestr); } diff --git a/src/dsd_gps.c b/src/dsd_gps.c index ea3afd1..7a6b4d9 100644 --- a/src/dsd_gps.c +++ b/src/dsd_gps.c @@ -135,8 +135,8 @@ void lip_protocol_decoder (dsd_opts * opts, dsd_state * state, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); - free (datestr); - free (timestr); + if (datestr != NULL) free (datestr); + if (timestr != NULL) free (timestr); } @@ -246,8 +246,8 @@ void nmea_iec_61162_1 (dsd_opts * opts, dsd_state * state, uint8_t * input, uint fprintf (pFile, "\n"); fclose (pFile); - free (datestr); - free (timestr); + if (datestr != NULL) free (datestr); + if (timestr != NULL) free (timestr); } @@ -371,8 +371,8 @@ void nmea_harris (dsd_opts * opts, dsd_state * state, uint8_t * input, uint32_t fprintf (pFile, "\n"); fclose (pFile); - free (datestr); - free (timestr); + if (datestr != NULL) free (datestr); + if (timestr != NULL) free (timestr); } @@ -501,8 +501,8 @@ void harris_gps(dsd_opts * opts, dsd_state * state, int slot, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); - free (datestr); - free (timestr); + if (datestr != NULL) free (datestr); + if (timestr != NULL) free (timestr); } @@ -714,8 +714,8 @@ void apx_embedded_gps (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[]) fprintf (pFile, "\n"); fclose (pFile); - free (datestr); - free (timestr); + if (datestr != NULL) free (datestr); + if (timestr != NULL) free (timestr); } diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index fe6dd54..193eb1b 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -3803,15 +3803,15 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //only print if a valid time was assigned to the matrix, and not EDACS/PV, and source is not zero if ( ((time(NULL) - call_matrix[9-j][5]) < 999999) && call_matrix[9-j][0] != 14 && call_matrix[9-j][0] != 15 && call_matrix[9-j][0] != 37 && call_matrix[9-j][0] != 38 && call_matrix[9-j][2] != 0) // { - char * timeCH = getTimeN(call_matrix[9-j][5]); - char * dateCH = getDateN(call_matrix[9-j][5]); + char * timestrCH = getTimeN(call_matrix[9-j][5]); + char * datestrCH = getDateN(call_matrix[9-j][5]); printw ("| "); - printw ("%s ", dateCH); - printw ("%s ", timeCH); + printw ("%s ", datestrCH); + printw ("%s ", timestrCH); - free (dateCH); - free (timeCH); + if (datestrCH != NULL) free (datestrCH); + if (timestrCH != NULL) free (timestrCH); if (lls == 28 || lls == 29) { @@ -3882,14 +3882,14 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) if (call_matrix[j][2] != 0) { printw ("| "); - char * timeCHE = getTimeN(call_matrix[j][5]); - char * dateCHE = getDateN(call_matrix[j][5]); + char * timestrCHE = getTimeN(call_matrix[j][5]); + char * datestrCHE = getDateN(call_matrix[j][5]); - printw ("%s ", dateCHE); - printw ("%s ", timeCHE); + printw ("%s ", datestrCHE); + printw ("%s ", timestrCHE); - free (dateCHE); - free (timeCHE); + if (datestrCHE != NULL) free (datestrCHE); + if (timestrCHE != NULL) free (timestrCHE); printw ("LCN [%2lld] ", call_matrix[j][1]); if (state->ea_mode == 1) @@ -4840,14 +4840,14 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //so probably just write a function to handle c input, and when c = certain values //needing an entry box, then stop all of those - //allocated memory pointer needs to be free'd each time - //if issues arise, just delete this line. - //TODO: Redo this in edacs and file, making a seperate char * timestr instead of calling GetTime - //directly from the sprintf command - free (timestr); - free (datestr); - free (timestrC); - free (datestrH); + + + + //allocated memory pointer needs to be free'd + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); + if (timestrC != NULL) free (timestrC); + if (datestrH != NULL) free (datestrH); } //end ncursesPrinter diff --git a/src/edacs-fme.c b/src/edacs-fme.c index 2914683..58a5261 100644 --- a/src/edacs-fme.c +++ b/src/edacs-fme.c @@ -2135,8 +2135,8 @@ void edacs(dsd_opts * opts, dsd_state * state) } - free (timestr); //free allocated memory to prevent memory leak - free (datestr); + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); fprintf (stderr, "\n"); } diff --git a/src/p25p2_frame.c b/src/p25p2_frame.c index cc408ac..fcda2cb 100644 --- a/src/p25p2_frame.c +++ b/src/p25p2_frame.c @@ -874,7 +874,7 @@ void process_P2_DUID (dsd_opts * opts, dsd_state * state) fprintf (stderr,"%s ", timestr); fprintf (stderr, " P25p2 "); - free(timestr); + if (timestr != NULL) free (timestr); if (state->currentslot == 0 && duid_decoded != 3 && duid_decoded != 12 && duid_decoded != 13 && duid_decoded != 4) { From 9da8544408192a51a2f07e303809dcebfef3099f Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 11:27:26 -0400 Subject: [PATCH 4/8] TIME: malloc and HHmmss Nits; Remove Unneeded time/date strings from Ncurses; --- src/dsd_ncurses.c | 10 +++------- src/dsd_time.c | 12 ++++++------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 193eb1b..8002065 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -32,8 +32,6 @@ static char alias_ch[10][50]; int reset = 0; char * timestr; char * datestr; -char * timestrC; -char * datestrH; int tg; int tgR; int tgn; @@ -1955,9 +1953,9 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) timestr = getTime(); datestr = getDate(); - //for display - timestrC = getTimeC(); - datestrH = getDateH(); + //NOTE: Any times associates with call history are stored + //in the array and need to be set by passing those values into + //getTimeN and getDateN if (opts->audio_in_type != 1) //can't run getch/menu when using STDIN - { @@ -4846,8 +4844,6 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //allocated memory pointer needs to be free'd if (timestr != NULL) free (timestr); if (datestr != NULL) free (datestr); - if (timestrC != NULL) free (timestrC); - if (datestrH != NULL) free (datestrH); } //end ncursesPrinter diff --git a/src/dsd_time.c b/src/dsd_time.c index e04dabb..d67e305 100644 --- a/src/dsd_time.c +++ b/src/dsd_time.c @@ -6,22 +6,22 @@ * 2024-04 DSD-FME Florida Man Edition *-----------------------------------------------------------------------------*/ -//TODO: Make sure everything still works as intended, no random crashes on free (timestr) +//TODO: Make sure everything still works as intended, every free(timestr) has a NULL check first, //make sure no other loose or random time functions embedded in other files or functions, etc #include "dsd.h" -//get hhmmss timestamp no colon (file operations) +//get HHmmss timestamp no colon (file operations) char * getTime() { - char * curr = (char *) malloc(9); + char * curr = (char *) malloc(7); time_t t = time(NULL); struct tm * ptm = localtime(& t); sprintf(curr,"%02d%02d%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); return curr; } -//get hh:mm:ss timestamp with colon (Ncurses Display) +//get HH:mm:ss timestamp with colon (Sync/Console Display) char * getTimeC() { char * curr = (char *) malloc(9); @@ -31,7 +31,7 @@ char * getTimeC() return curr; } -//get hh:mm:ss timestamp with colon (Ncurses Display) +//get HH:mm:ss timestamp with colon (Ncurses Call History) char * getTimeN(time_t t) { char * curr = (char *) malloc(9); @@ -50,7 +50,7 @@ char * getDate() return curr; } -//get YYYY-MM-DD with hyphen (Ncurses Display) +//get YYYY-MM-DD with hyphen (Sync/Console Display) char * getDateH() { char * curr = (char *) malloc(27); From c5b36af672da9f2c1239d868fab08557acc6e6a3 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 16:14:59 -0400 Subject: [PATCH 5/8] Add .bin loop 'debug mode' on -J; --- include/dsd.h | 1 + src/dsd_main.c | 13 ++++++++++--- src/dsd_ncurses.c | 1 + src/dsd_symbol.c | 8 +++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/dsd.h b/include/dsd.h index c3f5aad..aeb2d4a 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -588,6 +588,7 @@ typedef struct unsigned int debug_audio_errorsR; unsigned int debug_header_errors; unsigned int debug_header_critical_errors; + int debug_mode; //debug misc things // Last dibit read int last_dibit; diff --git a/src/dsd_main.c b/src/dsd_main.c index c387a9403..95c9afe 100644 --- a/src/dsd_main.c +++ b/src/dsd_main.c @@ -948,6 +948,7 @@ initState (dsd_state * state) state->debug_audio_errorsR = 0; state->debug_header_errors = 0; state->debug_header_critical_errors = 0; + state->debug_mode = 0; state->nxdn_last_ran = -1; state->nxdn_last_rid = 0; @@ -1694,7 +1695,7 @@ main (int argc, char **argv) exitflag = 0; - while ((c = getopt (argc, argv, "yhaepPqs:t:v:z:i:o:d:c:g:n:w:B:C:R:f:m:u:x:A:S:M:G:D:L:V:U:YK:b:H:X:NQ:WrlZTF01:2:345:6:7:89Ek:I:")) != -1) + while ((c = getopt (argc, argv, "yhaepPqs:t:v:z:i:o:d:c:g:n:w:B:C:R:f:m:u:x:A:S:M:G:D:L:V:U:YK:b:H:X:NQ:WrlZTF01:2:345:6:7:89Ek:I:J")) != -1) { opterr = 0; switch (c) @@ -1708,8 +1709,8 @@ main (int argc, char **argv) opts.call_alert = 1; break; - //Free'd up switches include: I, J, j, n, O, v, y - //all numerals have reclaimed, except for 4 and 0,1 (rc4 enforcement and single key) + //Free'd up switches include: j, O, + // //make sure to put a colon : after each if they need an argument //or remove colon if no argument required @@ -1717,6 +1718,12 @@ main (int argc, char **argv) //NOTE: The 'K' option for single BP key has been swapped to 'b' //'K' is now used for hexidecimal key.csv imports + //this is a debug option hidden from users, but use it to replay .bin files on loop + case 'J': + state.debug_mode = 1; + fprintf (stderr, "Debug Mode Enabled; \n"); + break; + //Specify M17 encoder User Data (CAN, DST, SRC values) //NOTE: Disabled QPSK settings by borrowing these switches (nobody probalby used them anyways) case 'M': diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 8002065..b9653ce 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -2382,6 +2382,7 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) printw("%s", FM_bannerN[i]); if (i == 1) printw (" ESC to Menu"); if (i == 2) printw (" 'q' to Quit "); + if (i == 3 && state->debug_mode == 1) printw (" DEBUG MODE!"); if (i == 4 && opts->frame_m17 == 0) printw (" MBElib %s", versionstr); #ifdef USE_CODEC2 if (i == 4 && opts->frame_m17 == 1) printw (" CODEC2"); diff --git a/src/dsd_symbol.c b/src/dsd_symbol.c index a23831d..0a335ce 100644 --- a/src/dsd_symbol.c +++ b/src/dsd_symbol.c @@ -658,8 +658,14 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) // opts->audio_in_type = 0; //switch to pulse after playback, ncurses terminal can initiate replay if wanted fclose(opts->symbolfile); fprintf (stderr, "\nEnd of %s\n", opts->audio_in_dev); + //in debug mode, re-run .bin files over and over (look for memory leaks, etc) + if (state->debug_mode == 1) + { + opts->symbolfile = fopen(opts->audio_in_dev, "r"); + opts->audio_in_type = 4; //symbol capture bin files + } //open pulse input if we are pulse output AND using ncurses terminal - if (opts->audio_out_type == 0 && opts->use_ncurses_terminal == 1) + else if (opts->audio_out_type == 0 && opts->use_ncurses_terminal == 1) { opts->audio_in_type = 0; //set input type openPulseInput(opts); //open pulse input From 7bcd29abecaf87691694d28741ceff4c2e334a43 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 16:15:44 -0400 Subject: [PATCH 6/8] NCURSES: Move timestr/datestr around; --- src/dsd_ncurses.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index b9653ce..7ba5d43 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -30,8 +30,8 @@ uint32_t temp_freq = -1; struct stat st_wav = {0}; static char alias_ch[10][50]; int reset = 0; -char * timestr; -char * datestr; + + int tg; int tgR; int tgn; @@ -654,7 +654,11 @@ void ncursesMenu (dsd_opts * opts, dsd_state * state) print_menuc(test_win, highlightc); if (choicec == 2) { + char * timestr = getTime(); + char * datestr = getDate(); sprintf (opts->wav_out_file, "%s %s DSD-FME-DECODED.wav", datestr, timestr); + if (timestr != NULL) free (timestr); + if (datestr != NULL) free (datestr); openWavOutFile (opts, state); } if (choicec == 3) @@ -1950,8 +1954,8 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) int c = 0; //for filenames (no colons, etc) - timestr = getTime(); - datestr = getDate(); + char * timestr = getTime(); + char * datestr = getDate(); //NOTE: Any times associates with call history are stored //in the array and need to be set by passing those values into From db036f4dfcca258d6df22367ec75450ec6bbfa80 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 18:22:28 -0400 Subject: [PATCH 7/8] TIME: Set Allocated Pointers to NULL after free()'d; --- src/dmr_bs.c | 26 +++++++++---------- src/dmr_ms.c | 18 ++++++++++--- src/dmr_pdu.c | 12 +++++++-- src/dsd_file.c | 24 +++++++++++++++--- src/dsd_frame_sync.c | 6 ++++- src/dsd_gps.c | 60 ++++++++++++++++++++++++++++++++++++-------- src/dsd_ncurses.c | 56 ++++++++++++++++++++++++++++++++--------- src/dsd_time.c | 12 ++++----- src/edacs-fme.c | 17 ++++++++++--- src/p25p2_frame.c | 7 +++++- 10 files changed, 182 insertions(+), 56 deletions(-) diff --git a/src/dmr_bs.c b/src/dmr_bs.c index 8d2775b..bdab262 100644 --- a/src/dmr_bs.c +++ b/src/dmr_bs.c @@ -13,7 +13,7 @@ //processing voice and/or data on both BS slots (channels) simultaneously void dmrBS (dsd_opts * opts, dsd_state * state) { - char * timestr; + char * timestr = NULL; int i, dibit; char ambe_fr[4][24]; @@ -549,14 +549,12 @@ void dmrBS (dsd_opts * opts, dsd_state * state) ncursesPrinter(opts, state); } - //both here and at bottom SHOULD be okay with the NULL check, if not, disable this one + // if (timestr != NULL) { - //debug - // fprintf (stderr, " FREE: 1; "); free (timestr); + timestr = NULL; } - // else fprintf (stderr, " NO FREE: 1; "); } // while loop @@ -604,14 +602,12 @@ void dmrBS (dsd_opts * opts, dsd_state * state) } - //second free + // if (timestr != NULL) - { - //debug - // fprintf (stderr, " FREE: 2; "); - free (timestr); - } -// else fprintf (stderr, " NO FREE: 2; "); + { + free (timestr); + timestr = NULL; + } } @@ -923,6 +919,10 @@ void dmrBSBootstrap (dsd_opts * opts, dsd_state * state) dmr_reset_blocks (opts, state); } - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } } diff --git a/src/dmr_ms.c b/src/dmr_ms.c index dacba16..a5e70f4 100644 --- a/src/dmr_ms.c +++ b/src/dmr_ms.c @@ -374,7 +374,11 @@ void dmrMS (dsd_opts * opts, dsd_state * state) state->dmr_ms_rc = 0; state->directmode = 0; //flag off - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } } @@ -618,7 +622,11 @@ void dmrMSBootstrap (dsd_opts * opts, dsd_state * state) //errors due to skipping other slot // cach_err = dmr_cach (opts, state, cachdata); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } skipDibit (opts, state, 144); //skip to next TDMA slot dmrMS (opts, state); //bootstrap into full TDMA frame @@ -689,6 +697,10 @@ void dmrMSData (dsd_opts * opts, dsd_state * state) state->dmr_stereo_payload[i+66] = 1; ////set to one so first frame will fail intentionally instead of zero fill } - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } } diff --git a/src/dmr_pdu.c b/src/dmr_pdu.c index 38bc16e..f814027 100644 --- a/src/dmr_pdu.c +++ b/src/dmr_pdu.c @@ -260,8 +260,16 @@ void dmr_lrrp (dsd_opts * opts, dsd_state * state, uint8_t block_len, uint8_t DM if (!source) fprintf (pFile, "%08lld\t", state->dmr_lrrp_source[state->currentslot]); //source address from data header if (source) fprintf (pFile, "%08d\t", source); //add source form decoded audio if available, else its from the header - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } if (pot_report) diff --git a/src/dsd_file.c b/src/dsd_file.c index 32c09fa..6f4ecaa 100644 --- a/src/dsd_file.c +++ b/src/dsd_file.c @@ -409,8 +409,16 @@ void openMbeOutFile (dsd_opts * opts, dsd_state * state) fprintf (opts->mbe_out_f, "%s", ext); fflush (opts->mbe_out_f); - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } void openMbeOutFileR (dsd_opts * opts, dsd_state * state) @@ -463,8 +471,16 @@ void openMbeOutFileR (dsd_opts * opts, dsd_state * state) fprintf (opts->mbe_out_fR, "%s", ext); fflush (opts->mbe_out_fR); - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } void openWavOutFile (dsd_opts * opts, dsd_state * state) diff --git a/src/dsd_frame_sync.c b/src/dsd_frame_sync.c index 924469c..315d21a 100644 --- a/src/dsd_frame_sync.c +++ b/src/dsd_frame_sync.c @@ -36,7 +36,11 @@ printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, if (opts->verbose > 2) //fprintf (stderr,"g: %f ", state->aout_gain); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } } diff --git a/src/dsd_gps.c b/src/dsd_gps.c index 7a6b4d9..45d90d5 100644 --- a/src/dsd_gps.c +++ b/src/dsd_gps.c @@ -135,8 +135,16 @@ void lip_protocol_decoder (dsd_opts * opts, dsd_state * state, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); - if (datestr != NULL) free (datestr); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } @@ -246,8 +254,16 @@ void nmea_iec_61162_1 (dsd_opts * opts, dsd_state * state, uint8_t * input, uint fprintf (pFile, "\n"); fclose (pFile); - if (datestr != NULL) free (datestr); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } @@ -371,8 +387,16 @@ void nmea_harris (dsd_opts * opts, dsd_state * state, uint8_t * input, uint32_t fprintf (pFile, "\n"); fclose (pFile); - if (datestr != NULL) free (datestr); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } @@ -501,8 +525,16 @@ void harris_gps(dsd_opts * opts, dsd_state * state, int slot, uint8_t * input) fprintf (pFile, "\n"); fclose (pFile); - if (datestr != NULL) free (datestr); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } @@ -714,8 +746,16 @@ void apx_embedded_gps (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[]) fprintf (pFile, "\n"); fclose (pFile); - if (datestr != NULL) free (datestr); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 7ba5d43..1686993 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -654,11 +654,19 @@ void ncursesMenu (dsd_opts * opts, dsd_state * state) print_menuc(test_win, highlightc); if (choicec == 2) { - char * timestr = getTime(); - char * datestr = getDate(); + char * timestr = getTime(); + char * datestr = getDate(); sprintf (opts->wav_out_file, "%s %s DSD-FME-DECODED.wav", datestr, timestr); - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } openWavOutFile (opts, state); } if (choicec == 3) @@ -1954,8 +1962,8 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) int c = 0; //for filenames (no colons, etc) - char * timestr = getTime(); - char * datestr = getDate(); + char * timestr = getTime(); + char * datestr = getDate(); //NOTE: Any times associates with call history are stored //in the array and need to be set by passing those values into @@ -3813,8 +3821,16 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) printw ("%s ", datestrCH); printw ("%s ", timestrCH); - if (datestrCH != NULL) free (datestrCH); - if (timestrCH != NULL) free (timestrCH); + if (datestrCH != NULL) + { + free (datestrCH); + datestrCH = NULL; + } + if (timestrCH != NULL) + { + free (timestrCH); + timestrCH = NULL; + } if (lls == 28 || lls == 29) { @@ -3891,8 +3907,16 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) printw ("%s ", datestrCHE); printw ("%s ", timestrCHE); - if (datestrCHE != NULL) free (datestrCHE); - if (timestrCHE != NULL) free (timestrCHE); + if (datestrCHE != NULL) + { + free (datestrCHE); + datestrCHE = NULL; + } + if (timestrCHE != NULL) + { + free (timestrCHE); + timestrCHE = NULL; + } printw ("LCN [%2lld] ", call_matrix[j][1]); if (state->ea_mode == 1) @@ -4847,8 +4871,16 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) //allocated memory pointer needs to be free'd - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } } //end ncursesPrinter diff --git a/src/dsd_time.c b/src/dsd_time.c index d67e305..87ac97d 100644 --- a/src/dsd_time.c +++ b/src/dsd_time.c @@ -14,7 +14,7 @@ //get HHmmss timestamp no colon (file operations) char * getTime() { - char * curr = (char *) malloc(7); + char * curr = calloc(7, sizeof(char)); time_t t = time(NULL); struct tm * ptm = localtime(& t); sprintf(curr,"%02d%02d%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); @@ -24,7 +24,7 @@ char * getTime() //get HH:mm:ss timestamp with colon (Sync/Console Display) char * getTimeC() { - char * curr = (char *) malloc(9); + char * curr = calloc(9, sizeof(char)); time_t t = time(NULL); struct tm * ptm = localtime(& t); sprintf(curr, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); @@ -34,7 +34,7 @@ char * getTimeC() //get HH:mm:ss timestamp with colon (Ncurses Call History) char * getTimeN(time_t t) { - char * curr = (char *) malloc(9); + char * curr = calloc(9, sizeof(char)); struct tm * ptm = localtime(& t); sprintf(curr, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); return curr; @@ -43,7 +43,7 @@ char * getTimeN(time_t t) //get YYYYMMDD without hyphen (file operations) char * getDate() { - char * curr = (char *) malloc(25); + char * curr = calloc(25, sizeof(char)); time_t t = time(NULL); struct tm * ptm = localtime(& t); sprintf(curr,"%04d%02d%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); @@ -53,7 +53,7 @@ char * getDate() //get YYYY-MM-DD with hyphen (Sync/Console Display) char * getDateH() { - char * curr = (char *) malloc(27); + char * curr = calloc(27, sizeof(char)); time_t t = time(NULL); struct tm * ptm = localtime(& t); sprintf(curr, "%04d-%02d-%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); @@ -63,7 +63,7 @@ char * getDateH() //get YYYY-MM-DD with hyphen (Ncurses Call History) char * getDateN(time_t t) { - char * curr = (char *) malloc(27); + char * curr = calloc(27, sizeof(char)); struct tm * ptm = localtime(& t); sprintf(curr, "%04d-%02d-%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); return curr; diff --git a/src/edacs-fme.c b/src/edacs-fme.c index 58a5261..351bc51 100644 --- a/src/edacs-fme.c +++ b/src/edacs-fme.c @@ -440,8 +440,8 @@ void edacs(dsd_opts * opts, dsd_state * state) state->edacs_f_mask = (1 << state->edacs_f_bits) - 1; state->edacs_s_mask = (1 << state->edacs_s_bits) - 1; - char * timestr; //add timestr here, so we can assign it and also free it to prevent memory leak - char * datestr; + char * timestr = NULL; + char * datestr = NULL; timestr = getTime(); datestr = getDate(); @@ -2135,8 +2135,17 @@ void edacs(dsd_opts * opts, dsd_state * state) } - if (timestr != NULL) free (timestr); - if (datestr != NULL) free (datestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (datestr != NULL) + { + free (datestr); + datestr = NULL; + } + fprintf (stderr, "\n"); } diff --git a/src/p25p2_frame.c b/src/p25p2_frame.c index fcda2cb..6c329af 100644 --- a/src/p25p2_frame.c +++ b/src/p25p2_frame.c @@ -874,7 +874,12 @@ void process_P2_DUID (dsd_opts * opts, dsd_state * state) fprintf (stderr,"%s ", timestr); fprintf (stderr, " P25p2 "); - if (timestr != NULL) free (timestr); + if (timestr != NULL) + { + free (timestr); + timestr = NULL; + } + if (state->currentslot == 0 && duid_decoded != 3 && duid_decoded != 12 && duid_decoded != 13 && duid_decoded != 4) { From 601a1ad2d15e763f9ed90a58c0e3129c34eccc96 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Thu, 25 Apr 2024 21:23:20 -0400 Subject: [PATCH 8/8] TIME: Fix printFrameSync Oopsie; NULL PTR on Debug symbolfile; --- src/dsd_frame_sync.c | 10 +++++++--- src/dsd_symbol.c | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/dsd_frame_sync.c b/src/dsd_frame_sync.c index 315d21a..062bb20 100644 --- a/src/dsd_frame_sync.c +++ b/src/dsd_frame_sync.c @@ -29,11 +29,15 @@ printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, fprintf (stderr,"%s ", timestr); fprintf (stderr,"Sync: %s ", frametype); } - if (opts->verbose > 2) + + //oops, that made a nested if-if-if-if statement, + //causing a memory leak + + // if (opts->verbose > 2) //fprintf (stderr,"o: %4i ", offset); - if (opts->verbose > 1) + // if (opts->verbose > 1) //fprintf (stderr,"mod: %s ", modulation); - if (opts->verbose > 2) + // if (opts->verbose > 2) //fprintf (stderr,"g: %f ", state->aout_gain); if (timestr != NULL) diff --git a/src/dsd_symbol.c b/src/dsd_symbol.c index 0a335ce..9cb06db 100644 --- a/src/dsd_symbol.c +++ b/src/dsd_symbol.c @@ -661,6 +661,7 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) //in debug mode, re-run .bin files over and over (look for memory leaks, etc) if (state->debug_mode == 1) { + opts->symbolfile = NULL; opts->symbolfile = fopen(opts->audio_in_dev, "r"); opts->audio_in_type = 4; //symbol capture bin files }