blob: 432ad2f3cbb7d25ce3579a383ecf2bc6ea76a9b7 [file] [log] [blame]
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +07001/* -*- c++ -*- */
2/* @file
3 * @author Vadim Yanitskiy <axilirator@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 <boost/lexical_cast.hpp>
29
30#include "udp_socket.h"
Vadim Yanitskiye601c362017-10-17 08:24:25 +070031#include "trx_burst_if_impl.h"
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070032
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070033#define BURST_SIZE 148
34#define DATA_IF_MTU 160
35
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +070036/**
37 * 41-bit RACH synchronization sequence
38 * GSM 05.02 Chapter 5.2.7 Access burst (AB)
39 */
40static uint8_t rach_synch_seq[] = {
41 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
42 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0,
43 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
44};
45
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070046namespace gr {
Piotr Krysik264fbf62017-11-05 12:25:51 +010047 namespace gsm {
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070048
Vadim Yanitskiye601c362017-10-17 08:24:25 +070049 trx_burst_if::sptr
50 trx_burst_if::make(
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070051 const std::string &remote_addr,
52 const std::string &base_port)
53 {
54 int base_port_int = boost::lexical_cast<int> (base_port);
55
56 return gnuradio::get_initial_sptr
Vadim Yanitskiye601c362017-10-17 08:24:25 +070057 (new trx_burst_if_impl(remote_addr, base_port_int));
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070058 }
59
60 /*
61 * The private constructor
62 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +070063 trx_burst_if_impl::trx_burst_if_impl(
64 const std::string &remote_addr,
65 int base_port
66 ) : gr::block("trx_burst_if",
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070067 gr::io_signature::make(0, 0, 0),
68 gr::io_signature::make(0, 0, 0))
69 {
70 message_port_register_in(pmt::mp("bursts"));
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070071 message_port_register_out(pmt::mp("bursts"));
72
73 // Bind a port handler
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070074 set_msg_handler(pmt::mp("bursts"),
Vadim Yanitskiye601c362017-10-17 08:24:25 +070075 boost::bind(&trx_burst_if_impl::handle_dl_burst, this, _1));
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070076
77 // Prepare port numbers
78 std::string clck_src_port = boost::lexical_cast<std::string> (base_port + 0);
79 std::string clck_dst_port = boost::lexical_cast<std::string> (base_port + 100);
80 std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2);
81 std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102);
82
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070083 // Init DATA & CLCK interfaces
84 d_data_sock = new udp_socket(remote_addr,
85 data_src_port, data_dst_port, DATA_IF_MTU);
86 d_clck_sock = new udp_socket(remote_addr,
87 clck_src_port, clck_dst_port, DATA_IF_MTU);
88
89 // Bind DATA interface handler
90 d_data_sock->udp_rx_handler = boost::bind(
Vadim Yanitskiye601c362017-10-17 08:24:25 +070091 &trx_burst_if_impl::handle_ul_burst, this, _1, _2);
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +070092
93 // Init timeslot filter
94 d_ts_filter_tn = -1;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070095 }
96
97 /*
98 * Our virtual destructor.
99 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700100 trx_burst_if_impl::~trx_burst_if_impl()
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700101 {
102 // Release all UDP sockets and free memory
103 delete d_data_sock;
104 delete d_clck_sock;
105 }
106
107 /*
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700108 * Timeslot filter API (getter and setter)
109 */
110 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700111 trx_burst_if_impl::ts_filter_set_tn(int tn)
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700112 {
113 d_ts_filter_tn = (tn >= 0 && tn <= 7) ? tn : -1;
114 }
115
116 int
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700117 trx_burst_if_impl::ts_filter_get_tn(void)
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700118 {
119 return d_ts_filter_tn;
120 }
121
122 /*
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700123 * Check if burst is a RACH burst
124 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700125 bool trx_burst_if_impl::detect_rach(uint8_t *burst)
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700126 {
127 // Compare synchronization sequence
128 for (int i = 0; i < 41; i++)
129 if (burst[i + 8] != rach_synch_seq[i])
130 return false;
131
132 // Make sure TB and GP are filled by 0x00
133 for (int i = 0; i < 63; i++)
134 if (burst[i + 85] != 0x00)
135 return false;
136
137 return true;
138 }
139
140 /*
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700141 * Create an UDP payload with clock indication
142 */
143 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700144 trx_burst_if_impl::clck_ind_send(uint32_t frame_nr)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700145 {
146 char buf[20];
147 size_t n;
148
149 n = snprintf(buf, 20, "IND CLOCK %u", frame_nr);
150 d_clck_sock->udp_send((uint8_t *) buf, n + 1);
151 }
152
153 /*
154 * Create an UDP payload with burst bits
155 * and some channel data.
156 */
157 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700158 trx_burst_if_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700159 {
160 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
161
162 // Extract GSMTAP header from message
163 gsmtap_hdr *header = (gsmtap_hdr *)
164 pmt::blob_data(header_plus_burst);
165
166 // Pack timeslot index
167 buf[0] = header->timeslot;
168
169 // Extract frame number
170 uint32_t frame_nr = be32toh(header->frame_number);
171
172 // HACK: send clock indications every 51-th frame
173 if (frame_nr % 51 == 0)
174 clck_ind_send(frame_nr);
175
176 // Pack frame number
177 buf[1] = (frame_nr >> 24) & 0xff;
178 buf[2] = (frame_nr >> 16) & 0xff;
179 buf[3] = (frame_nr >> 8) & 0xff;
180 buf[4] = (frame_nr >> 0) & 0xff;
181
182 // Pack RSSI (-dBm)
183 buf[5] = -(uint8_t) header->signal_dbm;
184
185 // Pack correlator timing offset (TOA)
186 // FIXME: where to find this value?
187 buf[6] = 0;
188 buf[7] = 0;
189
190 // Extract bits {0..1} from message
191 // Despite GR-GSM uses int8_t, they are not real sbits {-127..127}
192 uint8_t *burst = (uint8_t *)
193 (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr);
194
195 // Convert to transceiver interface specific bits {255..0}
196 for (int i = 0; i < 148; i++)
197 buf[8 + i] = burst[i] ? 255 : 0;
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700198
199 // Fill two unused bytes
200 buf[156] = 0x00;
201 buf[157] = 0x00;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700202 }
203
204 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700205 trx_burst_if_impl::handle_dl_burst(pmt::pmt_t msg)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700206 {
207 // 8 bytes of header + 148 bytes of burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700208 // + two unused, but required bytes
209 // otherwise bursts would be rejected
210 uint8_t buf[158];
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700211
212 // Compose a new UDP payload with burst
213 burst_pack(msg, buf);
214
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700215 // Timeslot filter
216 if (d_ts_filter_tn != -1 && buf[0] != d_ts_filter_tn)
217 return;
218
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700219 // Send a burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700220 d_data_sock->udp_send(buf, 158);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700221 }
222
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700223 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700224 trx_burst_if_impl::handle_ul_burst(uint8_t *payload, size_t len)
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700225 {
226 // Check length according to the protocol
227 if (len != 154)
228 return;
229
230 /* Make sure TS index is correct */
231 if (payload[0] >= 8)
232 return;
233
234 /* Unpack and check frame number */
235 uint32_t fn = (payload[1] << 24)
236 | (payload[2] << 16)
237 | (payload[3] << 8)
238 | payload[4];
239
240 if (fn >= 2715648)
241 return;
242
243 // Prepare a buffer for GSMTAP header and burst
244 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
245
246 // Set up pointer to GSMTAP header structure
247 struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf;
248 memset(header, 0x00, sizeof(struct gsmtap_hdr));
249
250 // Fill in basic info
251 header->version = GSMTAP_VERSION;
252 header->hdr_len = sizeof(gsmtap_hdr) / 4;
253 header->type = GSMTAP_TYPE_UM_BURST;
254
255 // Set timeslot index and frame number
256 header->timeslot = payload[0];
257 header->frame_number = htobe32(fn);
258
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700259 // Check if one is a RACH burst
260 header->sub_type = detect_rach(payload + 6) ?
261 GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL;
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700262
263 // Copy burst bits (0 & 1) for source message
264 memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE);
265
266 // Create a pmt blob
267 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
268 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob);
269
270 /* Send a message to the output */
271 message_port_pub(pmt::mp("bursts"), msg);
272 }
273
Piotr Krysik264fbf62017-11-05 12:25:51 +0100274 } /* namespace gsm */
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700275} /* namespace gr */