blob: f72eecd60c695dc007c06abc7311b3580aa054f6 [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
Piotr Krysik99305532018-05-05 12:38:11 +020030#include "grgsm/misc_utils/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 Yanitskiy5394c602018-08-10 00:01:26 +070051 const std::string &bind_addr,
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070052 const std::string &remote_addr,
53 const std::string &base_port)
54 {
55 int base_port_int = boost::lexical_cast<int> (base_port);
56
57 return gnuradio::get_initial_sptr
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070058 (new trx_burst_if_impl(bind_addr, remote_addr,
59 base_port_int));
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070060 }
61
62 /*
63 * The private constructor
64 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +070065 trx_burst_if_impl::trx_burst_if_impl(
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070066 const std::string &bind_addr,
Vadim Yanitskiye601c362017-10-17 08:24:25 +070067 const std::string &remote_addr,
68 int base_port
69 ) : gr::block("trx_burst_if",
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070070 gr::io_signature::make(0, 0, 0),
71 gr::io_signature::make(0, 0, 0))
72 {
73 message_port_register_in(pmt::mp("bursts"));
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070074 message_port_register_out(pmt::mp("bursts"));
75
76 // Bind a port handler
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070077 set_msg_handler(pmt::mp("bursts"),
Vadim Yanitskiye601c362017-10-17 08:24:25 +070078 boost::bind(&trx_burst_if_impl::handle_dl_burst, this, _1));
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070079
80 // Prepare port numbers
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070081 std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2);
82 std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102);
83
Vadim Yanitskiy6bac5742017-10-17 08:52:27 +070084 // Init DATA interface
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070085 d_data_sock = new udp_socket(bind_addr, data_src_port,
86 remote_addr, data_dst_port, DATA_IF_MTU);
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070087
88 // Bind DATA interface handler
89 d_data_sock->udp_rx_handler = boost::bind(
Vadim Yanitskiye601c362017-10-17 08:24:25 +070090 &trx_burst_if_impl::handle_ul_burst, this, _1, _2);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070091 }
92
93 /*
94 * Our virtual destructor.
95 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +070096 trx_burst_if_impl::~trx_burst_if_impl()
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070097 {
98 // Release all UDP sockets and free memory
99 delete d_data_sock;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700100 }
101
102 /*
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700103 * Check if burst is a RACH burst
104 */
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700105 bool trx_burst_if_impl::detect_rach(uint8_t *burst)
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700106 {
107 // Compare synchronization sequence
108 for (int i = 0; i < 41; i++)
109 if (burst[i + 8] != rach_synch_seq[i])
110 return false;
111
112 // Make sure TB and GP are filled by 0x00
113 for (int i = 0; i < 63; i++)
114 if (burst[i + 85] != 0x00)
115 return false;
116
117 return true;
118 }
119
120 /*
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700121 * Create an UDP payload with burst bits
122 * and some channel data.
123 */
124 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700125 trx_burst_if_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700126 {
127 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
128
129 // Extract GSMTAP header from message
130 gsmtap_hdr *header = (gsmtap_hdr *)
131 pmt::blob_data(header_plus_burst);
132
133 // Pack timeslot index
134 buf[0] = header->timeslot;
135
136 // Extract frame number
137 uint32_t frame_nr = be32toh(header->frame_number);
138
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700139 // Pack frame number
140 buf[1] = (frame_nr >> 24) & 0xff;
141 buf[2] = (frame_nr >> 16) & 0xff;
142 buf[3] = (frame_nr >> 8) & 0xff;
143 buf[4] = (frame_nr >> 0) & 0xff;
144
145 // Pack RSSI (-dBm)
146 buf[5] = -(uint8_t) header->signal_dbm;
147
148 // Pack correlator timing offset (TOA)
149 // FIXME: where to find this value?
150 buf[6] = 0;
151 buf[7] = 0;
152
153 // Extract bits {0..1} from message
154 // Despite GR-GSM uses int8_t, they are not real sbits {-127..127}
155 uint8_t *burst = (uint8_t *)
156 (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr);
157
158 // Convert to transceiver interface specific bits {255..0}
159 for (int i = 0; i < 148; i++)
160 buf[8 + i] = burst[i] ? 255 : 0;
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700161
162 // Fill two unused bytes
163 buf[156] = 0x00;
164 buf[157] = 0x00;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700165 }
166
167 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700168 trx_burst_if_impl::handle_dl_burst(pmt::pmt_t msg)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700169 {
170 // 8 bytes of header + 148 bytes of burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700171 // + two unused, but required bytes
172 // otherwise bursts would be rejected
173 uint8_t buf[158];
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700174
175 // Compose a new UDP payload with burst
176 burst_pack(msg, buf);
177
178 // Send a burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700179 d_data_sock->udp_send(buf, 158);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700180 }
181
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700182 void
Vadim Yanitskiye601c362017-10-17 08:24:25 +0700183 trx_burst_if_impl::handle_ul_burst(uint8_t *payload, size_t len)
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700184 {
185 // Check length according to the protocol
186 if (len != 154)
187 return;
188
189 /* Make sure TS index is correct */
190 if (payload[0] >= 8)
191 return;
192
193 /* Unpack and check frame number */
194 uint32_t fn = (payload[1] << 24)
195 | (payload[2] << 16)
196 | (payload[3] << 8)
197 | payload[4];
198
199 if (fn >= 2715648)
200 return;
201
202 // Prepare a buffer for GSMTAP header and burst
203 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
204
205 // Set up pointer to GSMTAP header structure
206 struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf;
207 memset(header, 0x00, sizeof(struct gsmtap_hdr));
208
209 // Fill in basic info
210 header->version = GSMTAP_VERSION;
211 header->hdr_len = sizeof(gsmtap_hdr) / 4;
212 header->type = GSMTAP_TYPE_UM_BURST;
213
214 // Set timeslot index and frame number
215 header->timeslot = payload[0];
216 header->frame_number = htobe32(fn);
217
Vadim Yanitskiy1d6b6282017-07-19 14:23:21 +0700218 // Check if one is a RACH burst
219 header->sub_type = detect_rach(payload + 6) ?
220 GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL;
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700221
222 // Copy burst bits (0 & 1) for source message
223 memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE);
224
225 // Create a pmt blob
226 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
227 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob);
228
229 /* Send a message to the output */
230 message_port_pub(pmt::mp("bursts"), msg);
231 }
232
Piotr Krysik264fbf62017-11-05 12:25:51 +0100233 } /* namespace gsm */
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700234} /* namespace gr */