blob: d6a2fb5f836809a48eb8fbebb736e0c78a97fb74 [file] [log] [blame]
Piotr Krysik1a32fc42018-05-11 11:38:07 +02001
2/* -*- c++ -*- */
3/* @file
4 * @author Piotr Krysik <ptrkrysik@gmail.com>
5 * @section LICENSE
6 *
7 * Gr-gsm is free software; you can redistribute it and/or modify
8 * 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.
11 *
12 * Gr-gsm is distributed in the hope that it will be useful,
13 * 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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with gr-gsm; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <gnuradio/io_signature.h>
29#include <grgsm/gsmtap.h>
30
31#include "freq_hopping_impl.h"
32#include "../misc_utils/freq_hopping_utils.h"
Piotr Krysik6463dc72018-06-18 14:43:50 +020033extern "C" {
34#include <osmocom/gsm/gsm_utils.h>
35}
Piotr Krysik1a32fc42018-05-11 11:38:07 +020036
37namespace gr {
38 namespace gsm {
39 using namespace pmt;
40
41 freq_hopping::sptr
42 freq_hopping::make(
43 pmt_t hopping_cmd)
44 {
45 return gnuradio::get_initial_sptr
46 (new freq_hopping_impl(hopping_cmd));
47 }
48
49 /*
50 * The private constructor
51 */
52 freq_hopping_impl::freq_hopping_impl(
53 pmt_t hopping_cmd
54 ) : gr::block("freq_hopping",
55 gr::io_signature::make(0, 0, 0),
56 gr::io_signature::make(0, 0, 0)),
57 d_hopping_cmd(PMT_NIL),
Piotr Krysik6463dc72018-06-18 14:43:50 +020058 d_hopping_enable(false),
59 d_base_freq(890e6)
Piotr Krysik1a32fc42018-05-11 11:38:07 +020060 {
61 // Register I/O ports
62 message_port_register_in(mp("hopping_cmd"));
63 message_port_register_in(mp("bursts_in"));
64 message_port_register_out(mp("bursts_out"));
65
66 // Bind message handlers
67 set_msg_handler(mp("hopping_cmd"),
68 boost::bind(&freq_hopping_impl::add_hopping_cmd,
69 this, _1));
70
71 set_msg_handler(mp("bursts_in"),
72 boost::bind(&freq_hopping_impl::set_freq_metadata,
73 this, _1));
74
75 add_hopping_cmd(hopping_cmd);
76 }
77
78 /*
79 * Our virtual destructor.
80 */
81 freq_hopping_impl::~freq_hopping_impl()
82 {
83 }
84
85 void freq_hopping_impl::add_hopping_cmd(pmt_t cmd) //TODO: fn and discard not supported at the moment
86 {
87 if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("start")) {
88 if(dict_ref(cmd,intern("fn"), PMT_NIL) != PMT_NIL){
89 //TODO add the command to the map<int,pmt_t>
90 } else {
91 pmt_t hopping_params = dict_ref(cmd, intern("hopping_params"), PMT_NIL);
92 d_hopping_enable = set_hopping_params(hopping_params);
93 }
94 } else if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("stop")) {
95 if(dict_ref(cmd,intern("fn"),PMT_NIL) != PMT_NIL){
96 //TODO add the command to the map<int,pmt_t>
Piotr Krysik6463dc72018-06-18 14:43:50 +020097
Piotr Krysik1a32fc42018-05-11 11:38:07 +020098 } else {
99 d_hopping_enable = false;
100 }
101 }
102 }
103
104 void freq_hopping_impl::set_freq_metadata(pmt_t burst)
105 {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200106
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200107 if(d_hopping_enable) {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200108 pmt_t pdu_header = car(burst);
109 pmt_t tx_time = dict_ref(pdu_header, intern("tx_time"),PMT_NIL);
110 if(tx_time != PMT_NIL){
111 pmt_t tx_command_time = cons(tuple_ref(tx_time,0),tuple_ref(tx_time,1));
112 pmt_t header_plus_burst = cdr(burst);
113 uint32_t frame_nr = 0;
114
115 pmt_t fn = dict_ref(pdu_header,intern("fn"),PMT_NIL);
116 if(fn == PMT_NIL){
117 gsmtap_hdr *header = (gsmtap_hdr *)blob_data(header_plus_burst);
118 uint32_t frame_nr = be32toh(header->frame_number);
119 } else {
120 frame_nr = to_uint64(fn);
121 }
122
123 int mai = calculate_ma_sfh(d_maio, d_hsn, d_ma.size(), frame_nr);
124 uint16_t arfcn = d_ma[mai];
125
126 // if(fn == PMT_NIL){
127 // header->arfcn = htobe16(arfcn);
128 // header->arfcn = header->arfcn | 0x8000; // set uplink flag
129 // }
130
131 //compute the frequences to be set in the burst header
132 double freq_uplink = static_cast<double>(gsm_arfcn2freq10(arfcn, 1)) * 1.0e5;
133 double freq_downlink = static_cast<double>(gsm_arfcn2freq10(arfcn, 0)) * 1.0e5;
134
135 pmt_t tx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
136 tx_command = dict_add(tx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
137 tx_command = dict_add(tx_command,intern("time"),tx_command_time);
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200138
Piotr Krysik6463dc72018-06-18 14:43:50 +0200139 pmt_t rx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
140 rx_command = dict_add(rx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
141 rx_command = dict_add(rx_command,intern("time"),tx_command_time);
142 rx_command = dict_add(rx_command,intern("direction"),intern("RX"));
143
144 pdu_header = dict_add(pdu_header, intern("tx_command"),tx_command);
145// pdu_header = dict_add(pdu_header, intern("tx_command"),rx_command);
146// std::cout << "arfcn " << arfcn << " mai " << mai << " d_ma.size() " << d_ma.size() << " d_hsn " << d_hsn << std::endl;
147 std::cout << "arfcn_uplink " << arfcn << std::endl;
148// std::cout << "freq_downlink " << freq_downlink << std::endl;
149// std::cout << "pdu_header " << pdu_header << std::endl;
150// std::cout << "size_header_plus_burst " << length(header_plus_burst) << std::endl;
151 message_port_pub(mp("bursts_out"), cons(pdu_header,header_plus_burst));
152 }
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200153 } else {
Piotr Krysik6463dc72018-06-18 14:43:50 +0200154 message_port_pub(mp("bursts_out"), burst);
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200155 }
156 }
157
Piotr Krysik6463dc72018-06-18 14:43:50 +0200158 bool freq_hopping_impl::set_hopping_params(pmt_t hopping_params){
Piotr Krysik1a32fc42018-05-11 11:38:07 +0200159 bool retval = false;
160 if(hopping_params != PMT_NIL){
161 //set hopping parameters immediately
162 pmt_t hsn = dict_ref(hopping_params, intern("hsn"), PMT_NIL);
163 pmt_t maio = dict_ref(hopping_params, intern("maio"), PMT_NIL);;
164 pmt_t ma = dict_ref(hopping_params, intern("ma"), PMT_NIL);
165
166 if(is_vector(ma) && is_integer(hsn) && is_integer(maio)){ //TODO: checking the values
167 d_hsn = to_uint64(hsn);
168 d_maio = to_uint64(maio);
169 d_ma.resize(length(ma));
170 for(int i=0; i<length(ma); i++){
171 d_ma[i] = to_uint64(vector_ref(ma,i));
172 }
173 retval = true;
174 }
175 }
176 return retval;
177 }
178 } /* namespace gsm */
179} /* namespace gr */