diff --git a/grc/satnogs_multi_format_msg_sink.xml b/grc/satnogs_multi_format_msg_sink.xml index de65e57..d802052 100644 --- a/grc/satnogs_multi_format_msg_sink.xml +++ b/grc/satnogs_multi_format_msg_sink.xml @@ -3,7 +3,7 @@ Multi Format Message Sink satnogs_multi_format_msg_sink import satnogs - satnogs.multi_format_msg_sink($format) + satnogs.multi_format_msg_sink($format, $timestamp, $outstream, $filename) Output format @@ -22,6 +22,41 @@ 2 + + + Output Timestamp + timestamp + enum + + + + + Output Result + outstream + enum + + + + + File + filename + + file_save + #if $outstream.t == "file" then 'none' else 'all'# in diff --git a/include/satnogs/multi_format_msg_sink.h b/include/satnogs/multi_format_msg_sink.h index e281a87..9061f33 100644 --- a/include/satnogs/multi_format_msg_sink.h +++ b/include/satnogs/multi_format_msg_sink.h @@ -2,7 +2,7 @@ /* * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module * - * Copyright (C) 2016, Libre Space Foundation + * Copyright (C) 2016,2017, Libre Space Foundation * * 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 @@ -44,12 +44,24 @@ namespace gr /*! * \brief Block accepting clear text messages from various decoders. - * Its purpose is to forward these messages at other services, programs, - * stdout, etc, + * Its purpose is to either print these messages to stdout or save them + * in text format in a file. * + * Depending on format parameter, the contents of each message are + * converted to hexademical, binary or ASCII format. + * + * @param format the format that will used to display the messages. + * 0: Clear Text 1: Hexademical 2: Binary + * @param timestamp if set, a ISO 8601 timestamp is inserted in front of + * each message + * @param out_stdout if set, the messages are displayed in the stdout. + * Otherwise messages are saved in a text file + * @param filepath specifies the file path of the text file */ static sptr - make (size_t format); + make (size_t format, bool timestamp = true, + bool out_stdout = true, + const std::string& filepath = ""); }; } // namespace satnogs diff --git a/lib/multi_format_msg_sink_impl.cc b/lib/multi_format_msg_sink_impl.cc index b7be9a5..64c97dd 100644 --- a/lib/multi_format_msg_sink_impl.cc +++ b/lib/multi_format_msg_sink_impl.cc @@ -2,7 +2,7 @@ /* * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module * - * Copyright (C) 2016, Libre Space Foundation + * Copyright (C) 2016,2017 Libre Space Foundation * * 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 @@ -24,58 +24,136 @@ #include #include "multi_format_msg_sink_impl.h" +#include +#include +#include -namespace gr { - namespace satnogs { +namespace gr +{ + namespace satnogs + { multi_format_msg_sink::sptr - multi_format_msg_sink::make(size_t format) + multi_format_msg_sink::make (size_t format, + bool timestamp, + bool out_stdout, + const std::string& filepath) { - return gnuradio::get_initial_sptr - (new multi_format_msg_sink_impl(format)); + return gnuradio::get_initial_sptr ( + new multi_format_msg_sink_impl (format, timestamp, + out_stdout, filepath)); } void - multi_format_msg_sink_impl::msg_handler (pmt::pmt_t msg) + multi_format_msg_sink_impl::msg_handler_file (pmt::pmt_t msg) { uint8_t *su; - std::string s((const char *)pmt::blob_data(msg), pmt::blob_length(msg)); - switch(d_format){ + std::string s ((const char *) pmt::blob_data (msg), + pmt::blob_length (msg)); + + if(d_timestamp) { + std::time_t t = std::time(nullptr); + std::tm tm = *std::localtime(&t); + d_fos << "[" << std::put_time(&tm, "%F %T %z") << "] "; + } + + switch (d_format) + { case 0: - std::cout << "Received text sequence:" << s << " " << std::endl; + d_fos << s << std::endl; break; case 1: - su = (uint8_t *)pmt::blob_data(msg); - for (size_t i = 0; i < pmt::blob_length (msg); i++) { - printf ("0x%02x ", su[i]); - } - std::cout << std::endl; + su = (uint8_t *) pmt::blob_data (msg); + for (size_t i = 0; i < pmt::blob_length (msg); i++) { + d_fos << std::hex << std::showbase << std::setw (4) + << (uint32_t) su[i] << " "; + } + d_fos << std::endl; break; case 2: - su = (uint8_t *)pmt::blob_data(msg); - for (size_t i = 0; i < pmt::blob_length (msg); i++) { - std::cout << "0b" << std::bitset<8> (su[i]) << " "; - } - std::cout << std::endl; + su = (uint8_t *) pmt::blob_data (msg); + for (size_t i = 0; i < pmt::blob_length (msg); i++) { + d_fos << "0b" << std::bitset<8> (su[i]) << " "; + } + d_fos << std::endl; break; default: - printf("Invalid format"); + throw std::invalid_argument("Invalid format"); + } + } + + void + multi_format_msg_sink_impl::msg_handler_stdout (pmt::pmt_t msg) + { + uint8_t *su; + std::string s ((const char *) pmt::blob_data (msg), + pmt::blob_length (msg)); + + if(d_timestamp) { + std::time_t t = std::time(nullptr); + std::tm tm = *std::localtime(&t); + std::cout << "[" << std::put_time(&tm, "%F %T %z") << "] "; } + + switch (d_format) + { + case 0: + std::cout << s << std::endl; + break; + case 1: + su = (uint8_t *) pmt::blob_data (msg); + for (size_t i = 0; i < pmt::blob_length (msg); i++) { + std::cout << std::hex << std::showbase << std::setw (4) + << (uint32_t) su[i] << " "; + } + std::cout << std::endl; + break; + case 2: + su = (uint8_t *) pmt::blob_data (msg); + for (size_t i = 0; i < pmt::blob_length (msg); i++) { + std::cout << "0b" << std::bitset<8> (su[i]) << " "; + } + std::cout << std::endl; + break; + default: + throw std::invalid_argument("Invalid format"); + } } /* * The private constructor */ - multi_format_msg_sink_impl::multi_format_msg_sink_impl(size_t format) - : gr::block("multi_format_msg_sink", - gr::io_signature::make(0, 0, 0), - gr::io_signature::make(0, 0, 0)), - d_format(format) + multi_format_msg_sink_impl::multi_format_msg_sink_impl ( + size_t format, bool timestamp, bool out_stdout, + const std::string& filepath) : + gr::block ("multi_format_msg_sink", + gr::io_signature::make (0, 0, 0), + gr::io_signature::make (0, 0, 0)), + d_format (format), + d_timestamp (timestamp), + d_stdout (out_stdout) { - message_port_register_in(pmt::mp("in")); - set_msg_handler ( - pmt::mp ("in"), - boost::bind (&multi_format_msg_sink_impl::msg_handler, this, _1)); + message_port_register_in (pmt::mp ("in")); + if(out_stdout) { + set_msg_handler ( + pmt::mp ("in"), + boost::bind (&multi_format_msg_sink_impl::msg_handler_stdout, + this, _1)); + } + else{ + d_fos.open(filepath); + set_msg_handler ( + pmt::mp ("in"), + boost::bind (&multi_format_msg_sink_impl::msg_handler_file, + this, _1)); + } + } + + multi_format_msg_sink_impl::~multi_format_msg_sink_impl () + { + if(!d_stdout) { + d_fos.close(); + } } } /* namespace satnogs */ diff --git a/lib/multi_format_msg_sink_impl.h b/lib/multi_format_msg_sink_impl.h index e7a091c..2061acb 100644 --- a/lib/multi_format_msg_sink_impl.h +++ b/lib/multi_format_msg_sink_impl.h @@ -2,7 +2,7 @@ /* * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module * - * Copyright (C) 2016, Libre Space Foundation + * Copyright (C) 2016,2017, Libre Space Foundation * * 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 @@ -22,6 +22,7 @@ #define INCLUDED_SATNOGS_MULTI_FORMAT_MSG_SINK_IMPL_H #include +#include namespace gr { @@ -32,12 +33,20 @@ namespace gr { private: void - msg_handler(pmt::pmt_t msg); + msg_handler_stdout (pmt::pmt_t msg); + void + msg_handler_file (pmt::pmt_t msg); - size_t d_format; + const size_t d_format; + const bool d_timestamp; + const bool d_stdout; + std::ofstream d_fos; public: - multi_format_msg_sink_impl (size_t format); + multi_format_msg_sink_impl (size_t format, bool timestamp, + bool out_stdout, const std::string& filepath); + + ~multi_format_msg_sink_impl (); };