blob: 200888a2f442172402b8fdce82daa17fdcca40a0 [file] [log] [blame]
piotrab663c82014-08-06 14:14:15 +02001/* -*- c++ -*- */
ptrkrysik529895b2014-12-02 18:07:38 +01002/*
3 * @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004 * @author (C) 2014 by Piotr Krysik <ptrkrysik@gmail.com>
ptrkrysik529895b2014-12-02 18:07:38 +01005 * @section LICENSE
6 *
7 * Gr-gsm is free software; you can redistribute it and/or modify
piotrab663c82014-08-06 14:14:15 +02008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
ptrkrysik529895b2014-12-02 18:07:38 +010011 *
12 * Gr-gsm is distributed in the hope that it will be useful,
piotrab663c82014-08-06 14:14:15 +020013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
ptrkrysik529895b2014-12-02 18:07:38 +010016 *
piotrab663c82014-08-06 14:14:15 +020017 * You should have received a copy of the GNU General Public License
ptrkrysik529895b2014-12-02 18:07:38 +010018 * along with gr-gsm; see the file COPYING. If not, write to
piotrab663c82014-08-06 14:14:15 +020019 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
ptrkrysik402c1fa2014-12-03 22:09:29 +010028#include <stdio.h>
piotrab663c82014-08-06 14:14:15 +020029#include "message_printer_impl.h"
ptrkrysik3be74a72014-12-13 10:11:00 +010030#include "grgsm/gsmtap.h"
Piotr Krysik29bcd392015-09-20 13:15:51 +020031#include <grgsm/endian.h>
32
33
Roman Khassrafdfe70092015-09-13 10:59:56 +020034extern "C" {
35 #include <osmocom/gsm/a5.h>
36}
piotrab663c82014-08-06 14:14:15 +020037
38namespace gr {
39 namespace gsm {
40
41 void message_printer_impl::message_print(pmt::pmt_t msg)
42 {
ptrkrysik617ba032014-11-21 10:11:05 +010043 pmt::pmt_t message_plus_header_blob = pmt::cdr(msg);
44 uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob);
45 size_t message_plus_header_len=pmt::blob_length(message_plus_header_blob);
ptrkrysik617ba032014-11-21 10:11:05 +010046 gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header;
Roman Khassrafdfe70092015-09-13 10:59:56 +020047 uint32_t frame_nr = be32toh(header->frame_number);
ptrkrysik617ba032014-11-21 10:11:05 +010048
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030049 std::ostringstream out;
50 out << d_prepend_string;
Roman Khassrafdfe70092015-09-13 10:59:56 +020051 if (d_prepend_fnr)
52 {
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030053 out << frame_nr;
Roman Khassrafdfe70092015-09-13 10:59:56 +020054 }
55
56 if (d_prepend_fnr && d_prepend_frame_count)
57 {
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030058 out << " ";
Roman Khassrafdfe70092015-09-13 10:59:56 +020059 }
60
61 if (d_prepend_frame_count)
62 {
63 // calculate fn count using libosmogsm
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030064 out << osmo_a5_fn_count(frame_nr);
Roman Khassrafdfe70092015-09-13 10:59:56 +020065 }
66
67 if (d_prepend_fnr || d_prepend_frame_count)
68 {
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030069 out << ": ";
Roman Khassrafdfe70092015-09-13 10:59:56 +020070 }
Roman Khassraf2bf49e42015-07-26 13:47:26 +020071
72 int start_index = sizeof(gsmtap_hdr);
73
74 if (d_print_gsmtap_header)
75 {
76 start_index = 0;
77 }
78
79 for(int ii=start_index; ii<message_plus_header_len; ii++)
piotrab663c82014-08-06 14:14:15 +020080 {
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030081 out<<" "<<(std::hex)<<std::setw(2)<<std::setfill('0')<<(uint32_t)message_plus_header[ii];
piotrab663c82014-08-06 14:14:15 +020082 }
Vasil Velichkov75ae9cc2018-08-31 00:59:48 +030083
84 out << std::endl;
85 std::cout << out.str() << std::flush;
piotrab663c82014-08-06 14:14:15 +020086 }
87
88 message_printer::sptr
Roman Khassrafdfe70092015-09-13 10:59:56 +020089 message_printer::make(pmt::pmt_t prepend_string, bool prepend_fnr,
90 bool prepend_frame_count, bool print_gsmtap_header)
piotrab663c82014-08-06 14:14:15 +020091 {
92 return gnuradio::get_initial_sptr
Roman Khassrafdfe70092015-09-13 10:59:56 +020093 (new message_printer_impl(prepend_string, prepend_fnr,
94 prepend_frame_count, print_gsmtap_header));
piotrab663c82014-08-06 14:14:15 +020095 }
96
97 /*
98 * The private constructor
99 */
Roman Khassrafdfe70092015-09-13 10:59:56 +0200100 message_printer_impl::message_printer_impl(pmt::pmt_t prepend_string, bool prepend_fnr,
101 bool prepend_frame_count, bool print_gsmtap_header)
piotrab663c82014-08-06 14:14:15 +0200102 : gr::block("message_printer",
103 gr::io_signature::make(0, 0, 0),
104 gr::io_signature::make(0, 0, 0))
105 {
Jacob Gilberte89aee12014-12-13 10:32:21 -0800106 d_prepend_string = prepend_string;
Roman Khassrafdfe70092015-09-13 10:59:56 +0200107 d_prepend_fnr = prepend_fnr;
108 d_prepend_frame_count = prepend_frame_count;
Roman Khassraf2bf49e42015-07-26 13:47:26 +0200109 d_print_gsmtap_header = print_gsmtap_header;
piotrab663c82014-08-06 14:14:15 +0200110 message_port_register_in(pmt::mp("msgs"));
Vadim Yanitskiya1169202021-05-03 19:00:43 +0200111 set_msg_handler(pmt::mp("msgs"), boost::bind(&message_printer_impl::message_print, this, boost::placeholders::_1));
piotrab663c82014-08-06 14:14:15 +0200112 }
113
114 /*
115 * Our virtual destructor.
116 */
117 message_printer_impl::~message_printer_impl()
118 {
119 }
120 } /* namespace gsm */
121} /* namespace gr */
122