blob: aeed88a17601e3622ad09ab3df4c769accbdf16e [file] [log] [blame]
Piotr Krysik1a32fc42018-05-11 11:38:07 +02001/* -*- c++ -*- */
2/* @file
3 * @author Piotr Krysik <ptrkrysik@gmail.com>
4 * @section LICENSE
5 *
6 * Gr-gsm is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * Gr-gsm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gr-gsm; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
28#include <grgsm/gsmtap.h>
29
Piotr Krysikdc4cb982019-07-16 15:39:11 +020030#include "freq_hopper_tag_impl.h"
Piotr Krysik1a32fc42018-05-11 11:38:07 +020031#include "../misc_utils/freq_hopping_utils.h"
Piotr Krysik6463dc72018-06-18 14:43:50 +020032extern "C" {
33#include <osmocom/gsm/gsm_utils.h>
34}
Piotr Krysik1a32fc42018-05-11 11:38:07 +020035
36namespace gr {
37 namespace gsm {
38 using namespace pmt;
39
Piotr Krysikdc4cb982019-07-16 15:39:11 +020040 freq_hopper_tag::sptr
41 freq_hopper_tag::make(
Piotr Krysik1a32fc42018-05-11 11:38:07 +020042 pmt_t hopping_cmd)
43 {
44 return gnuradio::get_initial_sptr
Piotr Krysikdc4cb982019-07-16 15:39:11 +020045 (new freq_hopper_tag_impl(hopping_cmd));
Piotr Krysik1a32fc42018-05-11 11:38:07 +020046 }
47
48 /*
49 * The private constructor
50 */
Piotr Krysikdc4cb982019-07-16 15:39:11 +020051 freq_hopper_tag_impl::freq_hopper_tag_impl(
Piotr Krysik1a32fc42018-05-11 11:38:07 +020052 pmt_t hopping_cmd
Piotr Krysikdc4cb982019-07-16 15:39:11 +020053 ) : gr::block("freq_hopper_tag",
Piotr Krysik1a32fc42018-05-11 11:38:07 +020054 gr::io_signature::make(0, 0, 0),
55 gr::io_signature::make(0, 0, 0)),
56 d_hopping_cmd(PMT_NIL),
Piotr Krysik6463dc72018-06-18 14:43:50 +020057 d_hopping_enable(false),
58 d_base_freq(890e6)
Piotr Krysik1a32fc42018-05-11 11:38:07 +020059 {
60 // Register I/O ports
61 message_port_register_in(mp("hopping_cmd"));
62 message_port_register_in(mp("bursts_in"));
63 message_port_register_out(mp("bursts_out"));
64
65 // Bind message handlers
66 set_msg_handler(mp("hopping_cmd"),
Piotr Krysikdc4cb982019-07-16 15:39:11 +020067 boost::bind(&freq_hopper_tag_impl::add_hopping_cmd,
Piotr Krysik1a32fc42018-05-11 11:38:07 +020068 this, _1));
69
70 set_msg_handler(mp("bursts_in"),
Piotr Krysikdc4cb982019-07-16 15:39:11 +020071 boost::bind(&freq_hopper_tag_impl::set_freq_metadata,
Piotr Krysik1a32fc42018-05-11 11:38:07 +020072 this, _1));
73
74 add_hopping_cmd(hopping_cmd);
75 }
76
77 /*
78 * Our virtual destructor.
79 */
Piotr Krysikdc4cb982019-07-16 15:39:11 +020080 freq_hopper_tag_impl::~freq_hopper_tag_impl()
Piotr Krysik1a32fc42018-05-11 11:38:07 +020081 {
82 }
83
Piotr Krysikdc4cb982019-07-16 15:39:11 +020084 void freq_hopper_tag_impl::add_hopping_cmd(pmt_t cmd) //TODO: fn and discard not supported at the moment
Piotr Krysik1a32fc42018-05-11 11:38:07 +020085 {
86 if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("start")) {
87 if(dict_ref(cmd,intern("fn"), PMT_NIL) != PMT_NIL){
88 //TODO add the command to the map<int,pmt_t>
89 } else {
90 pmt_t hopping_params = dict_ref(cmd, intern("hopping_params"), PMT_NIL);
91 d_hopping_enable = set_hopping_params(hopping_params);
92 }
93 } else if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("stop")) {
94 if(dict_ref(cmd,intern("fn"),PMT_NIL) != PMT_NIL){
95 //TODO add the command to the map<int,pmt_t>
Piotr Krysik6463dc72018-06-18 14:43:50 +020096
Piotr Krysik1a32fc42018-05-11 11:38:07 +020097 } else {
98 d_hopping_enable = false;
99 }
100 }
101 }
102
Piotr Krysikdc4cb982019-07-16 15:39:11 +0200103 void freq_hopper_tag_impl::set_freq_metadata(pmt_t burst)
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200104 {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200105
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200106 if(d_hopping_enable) {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200107 pmt_t pdu_header = car(burst);
108 pmt_t tx_time = dict_ref(pdu_header, intern("tx_time"),PMT_NIL);
109 if(tx_time != PMT_NIL){
110 pmt_t tx_command_time = cons(tuple_ref(tx_time,0),tuple_ref(tx_time,1));
111 pmt_t header_plus_burst = cdr(burst);
112 uint32_t frame_nr = 0;
113
114 pmt_t fn = dict_ref(pdu_header,intern("fn"),PMT_NIL);
115 if(fn == PMT_NIL){
116 gsmtap_hdr *header = (gsmtap_hdr *)blob_data(header_plus_burst);
117 uint32_t frame_nr = be32toh(header->frame_number);
118 } else {
119 frame_nr = to_uint64(fn);
120 }
121
122 int mai = calculate_ma_sfh(d_maio, d_hsn, d_ma.size(), frame_nr);
123 uint16_t arfcn = d_ma[mai];
124
125 // if(fn == PMT_NIL){
126 // header->arfcn = htobe16(arfcn);
127 // header->arfcn = header->arfcn | 0x8000; // set uplink flag
128 // }
129
130 //compute the frequences to be set in the burst header
131 double freq_uplink = static_cast<double>(gsm_arfcn2freq10(arfcn, 1)) * 1.0e5;
132 double freq_downlink = static_cast<double>(gsm_arfcn2freq10(arfcn, 0)) * 1.0e5;
133
134 pmt_t tx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
135 tx_command = dict_add(tx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
136 tx_command = dict_add(tx_command,intern("time"),tx_command_time);
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200137
Piotr Krysik6463dc72018-06-18 14:43:50 +0200138 pmt_t rx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
139 rx_command = dict_add(rx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
140 rx_command = dict_add(rx_command,intern("time"),tx_command_time);
141 rx_command = dict_add(rx_command,intern("direction"),intern("RX"));
142
143 pdu_header = dict_add(pdu_header, intern("tx_command"),tx_command);
144// pdu_header = dict_add(pdu_header, intern("tx_command"),rx_command);
145// std::cout << "arfcn " << arfcn << " mai " << mai << " d_ma.size() " << d_ma.size() << " d_hsn " << d_hsn << std::endl;
146 std::cout << "arfcn_uplink " << arfcn << std::endl;
147// std::cout << "freq_downlink " << freq_downlink << std::endl;
148// std::cout << "pdu_header " << pdu_header << std::endl;
149// std::cout << "size_header_plus_burst " << length(header_plus_burst) << std::endl;
150 message_port_pub(mp("bursts_out"), cons(pdu_header,header_plus_burst));
151 }
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200152 } else {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200153 message_port_pub(mp("bursts_out"), burst);
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200154 }
155 }
156
Piotr Krysikdc4cb982019-07-16 15:39:11 +0200157 bool freq_hopper_tag_impl::set_hopping_params(pmt_t hopping_params){
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200158 bool retval = false;
159 if(hopping_params != PMT_NIL){
160 //set hopping parameters immediately
161 pmt_t hsn = dict_ref(hopping_params, intern("hsn"), PMT_NIL);
162 pmt_t maio = dict_ref(hopping_params, intern("maio"), PMT_NIL);;
163 pmt_t ma = dict_ref(hopping_params, intern("ma"), PMT_NIL);
164
165 if(is_vector(ma) && is_integer(hsn) && is_integer(maio)){ //TODO: checking the values
166 d_hsn = to_uint64(hsn);
167 d_maio = to_uint64(maio);
168 d_ma.resize(length(ma));
169 for(int i=0; i<length(ma); i++){
170 d_ma[i] = to_uint64(vector_ref(ma,i));
171 }
172 retval = true;
173 }
174 }
175 return retval;
176 }
177 } /* namespace gsm */
178} /* namespace gr */