2021-08-30 10:51:31 +00:00
/*!
* \ file tlm_crc_stats . cc
* \ brief Class that computes the telemetry CRC statistics
* \ author Marc Majoral , 2021. mmajoral ( at ) cttc . es
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
* GNSS - SDR is a Global Navigation Satellite System software - defined receiver .
* This file is part of GNSS - SDR .
*
* Copyright ( C ) 2010 - 2020 ( see AUTHORS file for a list of contributors )
* SPDX - License - Identifier : GPL - 3.0 - or - later
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
# include "tlm_crc_stats.h"
# include "gnss_sdr_create_directory.h"
# include "gnss_sdr_filesystem.h"
# include <glog/logging.h>
# include <iomanip> // for std::setw()
# include <iostream> // for cerr, cout
2021-10-10 16:56:14 +00:00
# include <utility> // for std::move
2021-08-30 10:51:31 +00:00
2021-08-30 14:12:13 +00:00
2022-12-11 09:21:32 +00:00
void Tlm_CRC_Stats : : initialize ( std : : string dump_crc_stats_filename )
2021-08-30 10:51:31 +00:00
{
2022-12-11 09:21:32 +00:00
d_dump_crc_stats_filename = std : : move ( dump_crc_stats_filename ) ;
2021-08-30 10:51:31 +00:00
2022-12-11 09:21:32 +00:00
d_enable_crc_stats = true ;
d_num_crc_ok = 0 ;
d_num_crc_not_ok = 0 ;
2021-08-30 10:51:31 +00:00
}
2021-08-30 14:12:13 +00:00
2022-12-11 09:21:32 +00:00
bool Tlm_CRC_Stats : : set_channel ( int32_t channel )
2021-08-30 10:51:31 +00:00
{
std : : string dump_path ;
2022-12-11 09:21:32 +00:00
d_channel = channel ;
2021-08-30 10:51:31 +00:00
// Get path
if ( d_dump_crc_stats_filename . find_last_of ( ' / ' ) ! = std : : string : : npos )
{
std : : string dump_filename_ = d_dump_crc_stats_filename . substr ( d_dump_crc_stats_filename . find_last_of ( ' / ' ) + 1 ) ;
dump_path = d_dump_crc_stats_filename . substr ( 0 , d_dump_crc_stats_filename . find_last_of ( ' / ' ) ) ;
2023-11-29 20:34:59 +00:00
d_dump_crc_stats_filename = std : : move ( dump_filename_ ) ;
2021-08-30 10:51:31 +00:00
}
else
{
dump_path = std : : string ( " . " ) ;
}
// remove extension if any
if ( d_dump_crc_stats_filename . substr ( 1 ) . find_last_of ( ' . ' ) ! = std : : string : : npos )
{
d_dump_crc_stats_filename = d_dump_crc_stats_filename . substr ( 0 , d_dump_crc_stats_filename . find_last_of ( ' . ' ) ) ;
}
d_dump_crc_stats_filename . append ( " _ch " ) ;
2022-12-11 09:21:32 +00:00
d_dump_crc_stats_filename . append ( std : : to_string ( d_channel ) ) ;
2021-08-30 10:51:31 +00:00
d_dump_crc_stats_filename . append ( " .txt " ) ;
d_dump_crc_stats_filename = dump_path + fs : : path : : preferred_separator + d_dump_crc_stats_filename ;
// create directory
if ( ! gnss_sdr_create_directory ( dump_path ) )
{
std : : cerr < < " GNSS-SDR cannot create telemetry CRC stats dump file for the Telemetry block. The telemetry CRC statistics has been disabled. Wrong permissions? \n " ;
2022-12-11 09:21:32 +00:00
d_enable_crc_stats = false ;
2021-08-30 10:51:31 +00:00
}
2022-12-11 09:21:32 +00:00
d_dump_file . exceptions ( std : : ofstream : : failbit | std : : ofstream : : badbit ) ;
2021-08-30 10:51:31 +00:00
try
{
d_dump_file . open ( d_dump_crc_stats_filename . c_str ( ) , std : : ios : : out ) ;
}
2022-12-11 09:21:32 +00:00
catch ( const std : : ofstream : : failure & e )
2021-08-30 10:51:31 +00:00
{
LOG ( WARNING ) < < " Exception opening telemetry CRC stats dump file " < < e . what ( ) ;
2022-12-11 09:21:32 +00:00
d_enable_crc_stats = false ;
2021-08-30 10:51:31 +00:00
}
return true ;
}
2021-08-30 14:12:13 +00:00
2021-08-30 10:51:31 +00:00
void Tlm_CRC_Stats : : update_CRC_stats ( bool CRC )
{
if ( CRC )
{
2022-12-11 09:21:32 +00:00
d_num_crc_ok + + ;
2021-08-30 10:51:31 +00:00
}
else
{
2022-12-11 09:21:32 +00:00
d_num_crc_not_ok + + ;
2021-08-30 10:51:31 +00:00
}
}
2021-08-30 14:12:13 +00:00
2021-08-30 10:51:31 +00:00
Tlm_CRC_Stats : : ~ Tlm_CRC_Stats ( )
{
2022-12-11 09:21:32 +00:00
const uint32_t num_crc_tests = d_num_crc_ok + d_num_crc_not_ok ;
2021-08-30 10:51:31 +00:00
float success_rate = 0.0 ;
if ( num_crc_tests > 0 )
{
2022-12-11 09:21:32 +00:00
success_rate = static_cast < float > ( d_num_crc_ok ) / static_cast < float > ( num_crc_tests ) ;
2021-08-30 10:51:31 +00:00
}
2022-12-11 09:21:32 +00:00
try
2021-08-30 10:51:31 +00:00
{
2022-12-11 09:21:32 +00:00
const std : : string txt_num_crc_tests ( " Num CRC Tests " ) ;
const auto align_num_crc_tests = txt_num_crc_tests . length ( ) ;
const std : : string txt_success_tests ( " | Successful Tests " ) ;
const auto align_success_tests = txt_success_tests . length ( ) ;
const std : : string txt_success_rate ( " | Success rate " ) ;
const auto align_success_rate = txt_success_rate . length ( ) ;
const std : : string txt_delimiter ( " | " ) ;
const auto align_delimiter = txt_delimiter . length ( ) ;
if ( d_enable_crc_stats & & d_dump_file . is_open ( ) )
2021-08-30 10:51:31 +00:00
{
2022-12-11 09:21:32 +00:00
// write results to the telemetry CRC statistics output file
try
{
d_dump_file < < txt_num_crc_tests < < txt_success_tests < < txt_success_rate < < std : : endl ;
d_dump_file < < std : : setw ( align_num_crc_tests ) < < num_crc_tests < < txt_delimiter < < std : : setw ( align_success_tests - align_delimiter ) < < d_num_crc_ok < < txt_delimiter < < std : : setw ( align_success_rate - align_delimiter ) < < std : : setprecision ( 4 ) < < success_rate < < std : : endl ;
}
catch ( const std : : exception & ex )
{
DLOG ( INFO ) < < " Telemetry CRC stats cannot write on the output file " < < d_dump_crc_stats_filename . c_str ( ) ;
}
2021-08-30 10:51:31 +00:00
2022-12-11 09:21:32 +00:00
const auto pos = d_dump_file . tellp ( ) ;
try
{
d_dump_file . close ( ) ;
}
catch ( const std : : exception & ex )
2021-08-30 10:51:31 +00:00
{
2022-12-11 09:21:32 +00:00
LOG ( WARNING ) < < " Exception in destructor closing the telemetry CRC stats dump file " < < ex . what ( ) ;
}
if ( pos = = 0 )
{
errorlib : : error_code ec ;
if ( ! fs : : remove ( fs : : path ( d_dump_crc_stats_filename ) , ec ) )
{
std : : cerr < < " Problem removing telemetry CRC stats temporary file " < < d_dump_crc_stats_filename < < ' \n ' ;
}
2021-08-30 10:51:31 +00:00
}
}
}
2022-12-11 09:21:32 +00:00
catch ( const std : : exception & e )
{
std : : cerr < < " Problem in Tlm_CRC_Stats destructor: " < < e . what ( ) < < ' \n ' ;
}
2021-08-30 10:51:31 +00:00
}