blob: 678b577e820a18be1f997f95ea3bfcd81ef9e8f0 [file] [log] [blame]
Piotr Krysikdc4cb982019-07-16 15:39:11 +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
30#include "freq_hopper_msg_impl.h"
31#include "../misc_utils/freq_hopping_utils.h"
32extern "C" {
33#include <osmocom/gsm/gsm_utils.h>
34}
35
36namespace gr {
37 namespace gsm {
38 using namespace pmt;
39
40 freq_hopper_msg::sptr
41 freq_hopper_msg::make(
42 double samp_rate,
43 double start_fc_rx,
44 double start_fc_tx,
45 bool rx_hopping,
46 bool tx_hopping,
47 double freq_change_period)
48 {
49 return gnuradio::get_initial_sptr
50 (new freq_hopper_msg_impl(samp_rate, start_fc_rx, start_fc_tx,
51 rx_hopping, tx_hopping, freq_change_period));
52 }
53
54 /*
55 * The private constructor
56 */
57 freq_hopper_msg_impl::freq_hopper_msg_impl(
58 double samp_rate,
59 double start_fc_rx,
60 double start_fc_tx,
61 bool rx_hopping,
62 bool tx_hopping,
63 double freq_change_period) :
64 sync_block("freq_hopper_msg",
65 gr::io_signature::make(1, 1, sizeof(gr_complex)),
66 gr::io_signature::make(0, 0, 0)),
67 d_samp_rate(samp_rate),
68 d_fc_rx(start_fc_rx),
69 d_fc_tx(start_fc_tx),
70 d_rx_hopping(rx_hopping),
71 d_tx_hopping(tx_hopping),
72 d_freq_change_period(freq_change_period),
73 d_rx_time_received(false),
74 d_last_rx_time(0.0),
75 d_current_time(0.0),
76 d_current_start_offset(0.0),
77 d_i(0),
78 d_start_time(1.0),
79 d_next_change_time(time_spec_t(2.0)),
80 d_change_advance(0.15),
81 d_rx_time_key(string_to_symbol("rx_time"))
82// d_hopping_cmd(PMT_NIL),
83// d_hopping_enable(false),
84// d_base_freq(890e6)
85 {
86 // Register I/O ports
87// message_port_register_in(mp("hopping_cmd"));
88 message_port_register_out(mp("control"));
89// d_f.push_back(-0.3e6);
90// d_f.push_back(-0.2e6);
91// d_f.push_back(-0.1e6);
92// d_f.push_back(0);
93// d_f.push_back(0.1e6);
94// d_f.push_back(0.2e6);
95// d_f.push_back(0.3e6);
96
97 d_ma.push_back(51);
98 d_ma.push_back(2);
99 d_ma.push_back(37);
100 d_ma.push_back(45);
101 d_hsn = 0;
102 d_maio = 0;
103 d_current_fn = 0;
104// // Bind message handlers
105// set_msg_handler(mp("hopping_cmd"),
106// boost::bind(&freq_hopper_msg_impl::add_hopping_cmd,
107// this, _1));
108
109// add_hopping_cmd(hopping_cmd);
110 }
111
112 /*
113 * Our virtual destructor.
114 */
115 freq_hopper_msg_impl::~freq_hopper_msg_impl()
116 {
117 }
118
119 int freq_hopper_msg_impl::work(
120 int noutput_items,
121 gr_vector_const_void_star &input_items,
122 gr_vector_void_star &output_items)
123 {
124 std::vector<tag_t> rx_time_tags;
125 get_tags_in_window(rx_time_tags, 0, 0, noutput_items, d_rx_time_key);
126 if(rx_time_tags.size() > 0){
127 tag_t rx_time_tag = rx_time_tags[0];
128 uint64_t rx_time_full_part = to_uint64(tuple_ref(rx_time_tag.value,0));
129 double rx_time_frac_part = to_double(tuple_ref(rx_time_tag.value,1));
130 d_last_rx_time = time_spec_t(rx_time_full_part, rx_time_frac_part);
131 d_current_start_offset = rx_time_tag.offset;
132 d_rx_time_received = true;
133 }
134 if(d_rx_time_received){
135 uint64_t buffer_end_offset = nitems_read(0) + noutput_items;
136 d_current_time = time_spec_t::from_ticks(buffer_end_offset - d_current_start_offset, d_samp_rate) + d_last_rx_time;
137 }
138
139// Freq. control messages generation
140 if ((d_current_time.get_real_secs() > d_start_time) &&
141 ((d_next_change_time-d_current_time).get_real_secs() < d_change_advance)){
142 pmt_t freq_change_time_pmt = cons(
143 from_uint64(d_next_change_time.get_full_secs()),
144 from_double(d_next_change_time.get_frac_secs())
145 );
146 d_next_change_time = d_next_change_time + d_freq_change_period;
147 double dsp_freq = get_dsp_freq(d_current_fn);
148 if(d_tx_hopping){
149 pmt_t tx_tune_msg = dict_add(make_dict(), mp("lo_freq"), from_double(d_fc_tx));
150// tx_tune_msg = dict_add(tx_tune_msg, mp("dsp_freq"), from_double(d_f[d_i % d_f.size()]));
151 tx_tune_msg = dict_add(tx_tune_msg, mp("dsp_freq"), from_double(dsp_freq));
152 tx_tune_msg = dict_add(tx_tune_msg, mp("time"), freq_change_time_pmt);
153 tx_tune_msg = dict_add(tx_tune_msg, mp("direction"), mp("TX"));
154 message_port_pub(mp("control"), tx_tune_msg);
155// std::cout << "tx: " << tx_tune_msg << std::endl;
156 }
157 if(d_rx_hopping){
158 pmt_t rx_tune_msg = dict_add(make_dict(), mp("lo_freq"), from_double(d_fc_rx));
159// rx_tune_msg = dict_add(rx_tune_msg, mp("dsp_freq"), from_double(-d_f[d_i % d_f.size()]));
160 rx_tune_msg = dict_add(rx_tune_msg, mp("dsp_freq"), from_double(-dsp_freq));
161 rx_tune_msg = dict_add(rx_tune_msg, mp("time"), freq_change_time_pmt);
162 rx_tune_msg = dict_add(rx_tune_msg, mp("direction"), mp("RX"));
163 message_port_pub(mp("control"), rx_tune_msg);
164// std::cout << "rx: " << rx_tune_msg << std::endl;
165 }
166 d_i++;
167 d_current_fn++;
168 }
169 return noutput_items;
170 }
171
172 double freq_hopper_msg_impl::get_dsp_freq(uint64_t fn){ //TODO: jak otrzymac fn
173 int mai = calculate_ma_sfh(d_maio, d_hsn, d_ma.size(), fn);
174 uint16_t arfcn = d_ma[mai];
175 double dsp_freq = static_cast<double>(gsm_arfcn2freq10(arfcn, 0)) * 1.0e5 - d_fc_rx;
176 }
177/*
178 void freq_hopper_msg_impl::add_hopping_cmd(pmt_t cmd) //TODO: fn and discard not supported at the moment
179 {
180 if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("start")) {
181 if(dict_ref(cmd,intern("fn"), PMT_NIL) != PMT_NIL){
182 //TODO add the command to the map<int,pmt_t>
183 } else {
184 pmt_t hopping_params = dict_ref(cmd, intern("hopping_params"), PMT_NIL);
185 d_hopping_enable = set_hopping_params(hopping_params);
186 }
187 } else if(dict_ref(cmd,intern("cmd"), PMT_NIL) == intern("stop")) {
188 if(dict_ref(cmd,intern("fn"),PMT_NIL) != PMT_NIL){
189 //TODO add the command to the map<int,pmt_t>
190
191 } else {
192 d_hopping_enable = false;
193 }
194 }
195 }
196
197 void freq_hopper_msg_impl::set_freq_metadata(pmt_t burst)
198 {
199
200 if(d_hopping_enable) {
201 pmt_t pdu_header = car(burst);
202 pmt_t tx_time = dict_ref(pdu_header, intern("tx_time"),PMT_NIL);
203 if(tx_time != PMT_NIL){
204 pmt_t tx_command_time = cons(tuple_ref(tx_time,0),tuple_ref(tx_time,1));
205 pmt_t header_plus_burst = cdr(burst);
206 uint32_t frame_nr = 0;
207
208 pmt_t fn = dict_ref(pdu_header,intern("fn"),PMT_NIL);
209 if(fn == PMT_NIL){
210 gsmtap_hdr *header = (gsmtap_hdr *)blob_data(header_plus_burst);
211 uint32_t frame_nr = be32toh(header->frame_number);
212 } else {
213 frame_nr = to_uint64(fn);
214 }
215
216 int mai = calculate_ma_sfh(d_maio, d_hsn, d_ma.size(), frame_nr);
217 uint16_t arfcn = d_ma[mai];
218
219 // if(fn == PMT_NIL){
220 // header->arfcn = htobe16(arfcn);
221 // header->arfcn = header->arfcn | 0x8000; // set uplink flag
222 // }
223
224 //compute the frequences to be set in the burst header
225 double freq_uplink = static_cast<double>(gsm_arfcn2freq10(arfcn, 1)) * 1.0e5;
226 double freq_downlink = static_cast<double>(gsm_arfcn2freq10(arfcn, 0)) * 1.0e5;
227
228 pmt_t tx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
229 tx_command = dict_add(tx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
230 tx_command = dict_add(tx_command,intern("time"),tx_command_time);
231
232 pmt_t rx_command = dict_add(make_dict(),intern("lo_freq"),from_double(d_base_freq));
233 rx_command = dict_add(rx_command,intern("dsp_freq"),from_double(freq_uplink-d_base_freq));
234 rx_command = dict_add(rx_command,intern("time"),tx_command_time);
235 rx_command = dict_add(rx_command,intern("direction"),intern("RX"));
236
237 pdu_header = dict_add(pdu_header, intern("tx_command"),tx_command);
238// pdu_header = dict_add(pdu_header, intern("tx_command"),rx_command);
239// std::cout << "arfcn " << arfcn << " mai " << mai << " d_ma.size() " << d_ma.size() << " d_hsn " << d_hsn << std::endl;
240 std::cout << "arfcn_uplink " << arfcn << std::endl;
241// std::cout << "freq_downlink " << freq_downlink << std::endl;
242// std::cout << "pdu_header " << pdu_header << std::endl;
243// std::cout << "size_header_plus_burst " << length(header_plus_burst) << std::endl;
244 message_port_pub(mp("bursts_out"), cons(pdu_header,header_plus_burst));
245 }
246 } else {
247 message_port_pub(mp("bursts_out"), burst);
248 }
249 }
250
251 bool freq_hopper_msg_impl::set_hopping_params(pmt_t hopping_params){
252 bool retval = false;
253 if(hopping_params != PMT_NIL){
254 //set hopping parameters immediately
255 pmt_t hsn = dict_ref(hopping_params, intern("hsn"), PMT_NIL);
256 pmt_t maio = dict_ref(hopping_params, intern("maio"), PMT_NIL);;
257 pmt_t ma = dict_ref(hopping_params, intern("ma"), PMT_NIL);
258
259 if(is_vector(ma) && is_integer(hsn) && is_integer(maio)){ //TODO: checking the values
260 d_hsn = to_uint64(hsn);
261 d_maio = to_uint64(maio);
262 d_ma.resize(length(ma));
263 for(int i=0; i<length(ma); i++){
264 d_ma[i] = to_uint64(vector_ref(ma,i));
265 }
266 retval = true;
267 }
268 }
269 return retval;
270 }*/
271
272 } /* namespace gsm */
273} /* namespace gr */