blob: 48ef51a65e3177e16ac7c770a8c078333853b8e5 [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 Yanitskiy89fc14b2017-06-16 21:00:29 +070036namespace gr {
37 namespace grgsm {
38
39 trx::sptr
40 trx::make(
41 const std::string &remote_addr,
42 const std::string &base_port)
43 {
44 int base_port_int = boost::lexical_cast<int> (base_port);
45
46 return gnuradio::get_initial_sptr
47 (new trx_impl(remote_addr, base_port_int));
48 }
49
50 /*
51 * The private constructor
52 */
53 trx_impl::trx_impl(const std::string &remote_addr, int base_port)
54 : gr::block("trx",
55 gr::io_signature::make(0, 0, 0),
56 gr::io_signature::make(0, 0, 0))
57 {
58 message_port_register_in(pmt::mp("bursts"));
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070059 message_port_register_out(pmt::mp("bursts"));
60
61 // Bind a port handler
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070062 set_msg_handler(pmt::mp("bursts"),
63 boost::bind(&trx_impl::handle_dl_burst, this, _1));
64
65 // Prepare port numbers
66 std::string clck_src_port = boost::lexical_cast<std::string> (base_port + 0);
67 std::string clck_dst_port = boost::lexical_cast<std::string> (base_port + 100);
68 std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2);
69 std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102);
70
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070071 // Init DATA & CLCK interfaces
72 d_data_sock = new udp_socket(remote_addr,
73 data_src_port, data_dst_port, DATA_IF_MTU);
74 d_clck_sock = new udp_socket(remote_addr,
75 clck_src_port, clck_dst_port, DATA_IF_MTU);
76
77 // Bind DATA interface handler
78 d_data_sock->udp_rx_handler = boost::bind(
79 &trx_impl::handle_ul_burst, this, _1, _2);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070080 }
81
82 /*
83 * Our virtual destructor.
84 */
85 trx_impl::~trx_impl()
86 {
87 // Release all UDP sockets and free memory
88 delete d_data_sock;
89 delete d_clck_sock;
90 }
91
92 /*
93 * Create an UDP payload with clock indication
94 */
95 void
96 trx_impl::clck_ind_send(uint32_t frame_nr)
97 {
98 char buf[20];
99 size_t n;
100
101 n = snprintf(buf, 20, "IND CLOCK %u", frame_nr);
102 d_clck_sock->udp_send((uint8_t *) buf, n + 1);
103 }
104
105 /*
106 * Create an UDP payload with burst bits
107 * and some channel data.
108 */
109 void
110 trx_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf)
111 {
112 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
113
114 // Extract GSMTAP header from message
115 gsmtap_hdr *header = (gsmtap_hdr *)
116 pmt::blob_data(header_plus_burst);
117
118 // Pack timeslot index
119 buf[0] = header->timeslot;
120
121 // Extract frame number
122 uint32_t frame_nr = be32toh(header->frame_number);
123
124 // HACK: send clock indications every 51-th frame
125 if (frame_nr % 51 == 0)
126 clck_ind_send(frame_nr);
127
128 // Pack frame number
129 buf[1] = (frame_nr >> 24) & 0xff;
130 buf[2] = (frame_nr >> 16) & 0xff;
131 buf[3] = (frame_nr >> 8) & 0xff;
132 buf[4] = (frame_nr >> 0) & 0xff;
133
134 // Pack RSSI (-dBm)
135 buf[5] = -(uint8_t) header->signal_dbm;
136
137 // Pack correlator timing offset (TOA)
138 // FIXME: where to find this value?
139 buf[6] = 0;
140 buf[7] = 0;
141
142 // Extract bits {0..1} from message
143 // Despite GR-GSM uses int8_t, they are not real sbits {-127..127}
144 uint8_t *burst = (uint8_t *)
145 (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr);
146
147 // Convert to transceiver interface specific bits {255..0}
148 for (int i = 0; i < 148; i++)
149 buf[8 + i] = burst[i] ? 255 : 0;
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700150
151 // Fill two unused bytes
152 buf[156] = 0x00;
153 buf[157] = 0x00;
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700154 }
155
156 void
157 trx_impl::handle_dl_burst(pmt::pmt_t msg)
158 {
159 // 8 bytes of header + 148 bytes of burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700160 // + two unused, but required bytes
161 // otherwise bursts would be rejected
162 uint8_t buf[158];
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700163
164 // Compose a new UDP payload with burst
165 burst_pack(msg, buf);
166
167 // Send a burst
Vadim Yanitskiyc36c4982017-07-16 23:16:54 +0700168 d_data_sock->udp_send(buf, 158);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700169 }
170
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700171 void
172 trx_impl::handle_ul_burst(uint8_t *payload, size_t len)
173 {
174 // Check length according to the protocol
175 if (len != 154)
176 return;
177
178 /* Make sure TS index is correct */
179 if (payload[0] >= 8)
180 return;
181
182 /* Unpack and check frame number */
183 uint32_t fn = (payload[1] << 24)
184 | (payload[2] << 16)
185 | (payload[3] << 8)
186 | payload[4];
187
188 if (fn >= 2715648)
189 return;
190
191 // Prepare a buffer for GSMTAP header and burst
192 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
193
194 // Set up pointer to GSMTAP header structure
195 struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf;
196 memset(header, 0x00, sizeof(struct gsmtap_hdr));
197
198 // Fill in basic info
199 header->version = GSMTAP_VERSION;
200 header->hdr_len = sizeof(gsmtap_hdr) / 4;
201 header->type = GSMTAP_TYPE_UM_BURST;
202
203 // Set timeslot index and frame number
204 header->timeslot = payload[0];
205 header->frame_number = htobe32(fn);
206
207 // HACK: use GSMTAP_BURST_NORMAL for now
208 // FIXME: We will need to distinguish between RACN and NORMAL
209 header->sub_type = GSMTAP_BURST_NORMAL;
210
211 // Copy burst bits (0 & 1) for source message
212 memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE);
213
214 // Create a pmt blob
215 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
216 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob);
217
218 /* Send a message to the output */
219 message_port_pub(pmt::mp("bursts"), msg);
220 }
221
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700222 } /* namespace grgsm */
223} /* namespace gr */