blob: 8c348ac52c9ffefd1583487cc73f0fc278587c19 [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"
31#include "trx_impl.h"
32
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
49 trx::sptr
50 trx::make(
51 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
57 (new trx_impl(remote_addr, base_port_int));
58 }
59
60 /*
61 * The private constructor
62 */
63 trx_impl::trx_impl(const std::string &remote_addr, int base_port)
64 : gr::block("trx",
65 gr::io_signature::make(0, 0, 0),
66 gr::io_signature::make(0, 0, 0))
67 {
68 message_port_register_in(pmt::mp("bursts"));
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070069 message_port_register_out(pmt::mp("bursts"));
70
71 // Bind a port handler
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070072 set_msg_handler(pmt::mp("bursts"),
73 boost::bind(&trx_impl::handle_dl_burst, this, _1));
74
75 // Prepare port numbers
76 std::string clck_src_port = boost::lexical_cast<std::string> (base_port + 0);
77 std::string clck_dst_port = boost::lexical_cast<std::string> (base_port + 100);
78 std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2);
79 std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102);
80
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070081 // Init DATA & CLCK interfaces
82 d_data_sock = new udp_socket(remote_addr,
83 data_src_port, data_dst_port, DATA_IF_MTU);
84 d_clck_sock = new udp_socket(remote_addr,
85 clck_src_port, clck_dst_port, DATA_IF_MTU);
86
87 // Bind DATA interface handler
88 d_data_sock->udp_rx_handler = boost::bind(
89 &trx_impl::handle_ul_burst, this, _1, _2);
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +070090
91 // Init timeslot filter
92 d_ts_filter_tn = -1;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070093 }
94
95 /*
96 * Our virtual destructor.
97 */
98 trx_impl::~trx_impl()
99 {
100 // Release all UDP sockets and free memory
101 delete d_data_sock;
102 delete d_clck_sock;
103 }
104
105 /*
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700106 * Timeslot filter API (getter and setter)
107 */
108 void
109 trx_impl::ts_filter_set_tn(int tn)
110 {
111 d_ts_filter_tn = (tn >= 0 && tn <= 7) ? tn : -1;
112 }
113
114 int
115 trx_impl::ts_filter_get_tn(void)
116 {
117 return d_ts_filter_tn;
118 }
119
120 /*
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700121 * Check if burst is a RACH burst
122 */
123 bool trx_impl::detect_rach(uint8_t *burst)
124 {
125 // Compare synchronization sequence
126 for (int i = 0; i < 41; i++)
127 if (burst[i + 8] != rach_synch_seq[i])
128 return false;
129
130 // Make sure TB and GP are filled by 0x00
131 for (int i = 0; i < 63; i++)
132 if (burst[i + 85] != 0x00)
133 return false;
134
135 return true;
136 }
137
138 /*
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700139 * Create an UDP payload with clock indication
140 */
141 void
142 trx_impl::clck_ind_send(uint32_t frame_nr)
143 {
144 char buf[20];
145 size_t n;
146
147 n = snprintf(buf, 20, "IND CLOCK %u", frame_nr);
148 d_clck_sock->udp_send((uint8_t *) buf, n + 1);
149 }
150
151 /*
152 * Create an UDP payload with burst bits
153 * and some channel data.
154 */
155 void
156 trx_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf)
157 {
158 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
159
160 // Extract GSMTAP header from message
161 gsmtap_hdr *header = (gsmtap_hdr *)
162 pmt::blob_data(header_plus_burst);
163
164 // Pack timeslot index
165 buf[0] = header->timeslot;
166
167 // Extract frame number
168 uint32_t frame_nr = be32toh(header->frame_number);
169
170 // HACK: send clock indications every 51-th frame
171 if (frame_nr % 51 == 0)
172 clck_ind_send(frame_nr);
173
174 // Pack frame number
175 buf[1] = (frame_nr >> 24) & 0xff;
176 buf[2] = (frame_nr >> 16) & 0xff;
177 buf[3] = (frame_nr >> 8) & 0xff;
178 buf[4] = (frame_nr >> 0) & 0xff;
179
180 // Pack RSSI (-dBm)
181 buf[5] = -(uint8_t) header->signal_dbm;
182
183 // Pack correlator timing offset (TOA)
184 // FIXME: where to find this value?
185 buf[6] = 0;
186 buf[7] = 0;
187
188 // Extract bits {0..1} from message
189 // Despite GR-GSM uses int8_t, they are not real sbits {-127..127}
190 uint8_t *burst = (uint8_t *)
191 (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr);
192
193 // Convert to transceiver interface specific bits {255..0}
194 for (int i = 0; i < 148; i++)
195 buf[8 + i] = burst[i] ? 255 : 0;
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700196
197 // Fill two unused bytes
198 buf[156] = 0x00;
199 buf[157] = 0x00;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700200 }
201
202 void
203 trx_impl::handle_dl_burst(pmt::pmt_t msg)
204 {
205 // 8 bytes of header + 148 bytes of burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700206 // + two unused, but required bytes
207 // otherwise bursts would be rejected
208 uint8_t buf[158];
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700209
210 // Compose a new UDP payload with burst
211 burst_pack(msg, buf);
212
Vadim Yanitskiy5b7c60a2017-07-19 17:30:44 +0700213 // Timeslot filter
214 if (d_ts_filter_tn != -1 && buf[0] != d_ts_filter_tn)
215 return;
216
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700217 // Send a burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700218 d_data_sock->udp_send(buf, 158);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700219 }
220
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700221 void
222 trx_impl::handle_ul_burst(uint8_t *payload, size_t len)
223 {
224 // Check length according to the protocol
225 if (len != 154)
226 return;
227
228 /* Make sure TS index is correct */
229 if (payload[0] >= 8)
230 return;
231
232 /* Unpack and check frame number */
233 uint32_t fn = (payload[1] << 24)
234 | (payload[2] << 16)
235 | (payload[3] << 8)
236 | payload[4];
237
238 if (fn >= 2715648)
239 return;
240
241 // Prepare a buffer for GSMTAP header and burst
242 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
243
244 // Set up pointer to GSMTAP header structure
245 struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf;
246 memset(header, 0x00, sizeof(struct gsmtap_hdr));
247
248 // Fill in basic info
249 header->version = GSMTAP_VERSION;
250 header->hdr_len = sizeof(gsmtap_hdr) / 4;
251 header->type = GSMTAP_TYPE_UM_BURST;
252
253 // Set timeslot index and frame number
254 header->timeslot = payload[0];
255 header->frame_number = htobe32(fn);
256
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700257 // Check if one is a RACH burst
258 header->sub_type = detect_rach(payload + 6) ?
259 GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL;
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700260
261 // Copy burst bits (0 & 1) for source message
262 memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE);
263
264 // Create a pmt blob
265 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
266 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob);
267
268 /* Send a message to the output */
269 message_port_pub(pmt::mp("bursts"), msg);
270 }
271
Piotr Krysik264fbf62017-11-05 12:25:51 +0100272 } /* namespace gsm */
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700273} /* namespace gr */