multi_format_msg_sink_impl.cc 5.01 KB
Newer Older
1 2 3 4
/* -*- c++ -*- */
/*
 * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
 *
5
 *  Copyright (C) 2016,2017 Libre Space Foundation <http://librespacefoundation.org/>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
26
#include "multi_format_msg_sink_impl.h"
27 28 29
#include <ctime>
#include <iostream>
#include <iomanip>
30

31 32 33 34
namespace gr
{
  namespace satnogs
  {
35

36
    multi_format_msg_sink::sptr
37 38 39 40
    multi_format_msg_sink::make (size_t format,
                                 bool timestamp,
                                 bool out_stdout,
                                 const std::string& filepath)
41
    {
42 43 44
      return gnuradio::get_initial_sptr (
          new multi_format_msg_sink_impl (format, timestamp,
                                          out_stdout, filepath));
45 46 47
    }

    void
48
    multi_format_msg_sink_impl::msg_handler_file (pmt::pmt_t msg)
49
    {
Manolis Surligas's avatar
Manolis Surligas committed
50
      uint8_t *su;
51
      char buf[256];
52 53 54 55 56 57
      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);
58 59
        strftime(buf, sizeof(buf), "%F %T %z", &tm);
        d_fos << "[" << buf << "]";
60 61 62 63
      }

      switch (d_format)
        {
64
        case 0:
65
          d_fos << s << std::endl;
66 67
          break;
        case 1:
68 69
          su = (uint8_t *) pmt::blob_data (msg);
          for (size_t i = 0; i < pmt::blob_length (msg); i++) {
70 71
            d_fos << "0x" << std::hex << std::setw (2) << std::setfill ('0')
                << (uint32_t) su[i] << " ";
72 73
          }
          d_fos << std::endl;
74 75
          break;
        case 2:
76 77 78 79 80
          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;
81 82
          break;
        default:
83 84 85 86 87 88 89 90
          throw std::invalid_argument("Invalid format");
        }
    }

    void
    multi_format_msg_sink_impl::msg_handler_stdout (pmt::pmt_t msg)
    {
      uint8_t *su;
91
      char buf[256];
92 93 94 95 96 97
      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);
98 99
        strftime(buf, sizeof(buf), "%F %T %z", &tm);
        std::cout << "[" << buf << "]";
100
      }
101 102 103

      switch (d_format)
        {
104
        case 0: // binary
105 106 107 108
          for (size_t i = 0; i < pmt::blob_length (msg); i++) {
            std::cout << s[i];
          }
          std::cout << std::endl;
109
          break;
110
        case 1: // hex annotated
111 112
          su = (uint8_t *) pmt::blob_data (msg);
          for (size_t i = 0; i < pmt::blob_length (msg); i++) {
113
            std::cout << "0x" << std::hex << std::setw (2) << std::setfill ('0')
114 115 116 117
                << (uint32_t) su[i] << " ";
          }
          std::cout << std::endl;
          break;
118
        case 2: // binary annotated
119 120 121 122 123 124 125 126 127
          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");
        }
128 129 130 131 132
    }

    /*
     * The private constructor
     */
133 134 135 136 137 138 139 140 141
    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)
142
    {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      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();
      }
164 165 166 167 168
    }

  } /* namespace satnogs */
} /* namespace gr */