Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 1 | /* -*- 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 | |
David Holm | 1785216 | 2018-09-16 13:13:52 +0200 | [diff] [blame] | 30 | #include "grgsm/endian.h" |
Piotr Krysik | 9930553 | 2018-05-05 12:38:11 +0200 | [diff] [blame] | 31 | #include "grgsm/misc_utils/udp_socket.h" |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 32 | #include "trx_burst_if_impl.h" |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 33 | |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 34 | #define BURST_SIZE 148 |
| 35 | #define DATA_IF_MTU 160 |
| 36 | |
Vadim Yanitskiy | 1d6b628 | 2017-07-19 14:23:21 +0700 | [diff] [blame] | 37 | /** |
| 38 | * 41-bit RACH synchronization sequence |
| 39 | * GSM 05.02 Chapter 5.2.7 Access burst (AB) |
| 40 | */ |
| 41 | static uint8_t rach_synch_seq[] = { |
| 42 | 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, |
| 43 | 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, |
| 44 | 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, |
| 45 | }; |
| 46 | |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 47 | namespace gr { |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 48 | namespace gsm { |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 49 | |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 50 | trx_burst_if::sptr |
| 51 | trx_burst_if::make( |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 52 | const std::string &bind_addr, |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 53 | const std::string &remote_addr, |
| 54 | const std::string &base_port) |
| 55 | { |
| 56 | int base_port_int = boost::lexical_cast<int> (base_port); |
| 57 | |
| 58 | return gnuradio::get_initial_sptr |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 59 | (new trx_burst_if_impl(bind_addr, remote_addr, |
| 60 | base_port_int)); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * The private constructor |
| 65 | */ |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 66 | trx_burst_if_impl::trx_burst_if_impl( |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 67 | const std::string &bind_addr, |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 68 | const std::string &remote_addr, |
| 69 | int base_port |
| 70 | ) : gr::block("trx_burst_if", |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 71 | gr::io_signature::make(0, 0, 0), |
| 72 | gr::io_signature::make(0, 0, 0)) |
| 73 | { |
| 74 | message_port_register_in(pmt::mp("bursts")); |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 75 | message_port_register_out(pmt::mp("bursts")); |
| 76 | |
| 77 | // Bind a port handler |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 78 | set_msg_handler(pmt::mp("bursts"), |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 79 | boost::bind(&trx_burst_if_impl::handle_dl_burst, this, _1)); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 80 | |
| 81 | // Prepare port numbers |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 82 | std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2); |
| 83 | std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102); |
| 84 | |
Vadim Yanitskiy | 6bac574 | 2017-10-17 08:52:27 +0700 | [diff] [blame] | 85 | // Init DATA interface |
Vadim Yanitskiy | 5394c60 | 2018-08-10 00:01:26 +0700 | [diff] [blame] | 86 | d_data_sock = new udp_socket(bind_addr, data_src_port, |
| 87 | remote_addr, data_dst_port, DATA_IF_MTU); |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 88 | |
| 89 | // Bind DATA interface handler |
| 90 | d_data_sock->udp_rx_handler = boost::bind( |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 91 | &trx_burst_if_impl::handle_ul_burst, this, _1, _2); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Our virtual destructor. |
| 96 | */ |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 97 | trx_burst_if_impl::~trx_burst_if_impl() |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 98 | { |
| 99 | // Release all UDP sockets and free memory |
| 100 | delete d_data_sock; |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
Vadim Yanitskiy | 1d6b628 | 2017-07-19 14:23:21 +0700 | [diff] [blame] | 104 | * Check if burst is a RACH burst |
| 105 | */ |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 106 | bool trx_burst_if_impl::detect_rach(uint8_t *burst) |
Vadim Yanitskiy | 1d6b628 | 2017-07-19 14:23:21 +0700 | [diff] [blame] | 107 | { |
| 108 | // Compare synchronization sequence |
| 109 | for (int i = 0; i < 41; i++) |
| 110 | if (burst[i + 8] != rach_synch_seq[i]) |
| 111 | return false; |
| 112 | |
| 113 | // Make sure TB and GP are filled by 0x00 |
| 114 | for (int i = 0; i < 63; i++) |
| 115 | if (burst[i + 85] != 0x00) |
| 116 | return false; |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /* |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 122 | * Create an UDP payload with burst bits |
| 123 | * and some channel data. |
| 124 | */ |
| 125 | void |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 126 | trx_burst_if_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf) |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 127 | { |
| 128 | pmt::pmt_t header_plus_burst = pmt::cdr(msg); |
| 129 | |
| 130 | // Extract GSMTAP header from message |
| 131 | gsmtap_hdr *header = (gsmtap_hdr *) |
| 132 | pmt::blob_data(header_plus_burst); |
| 133 | |
| 134 | // Pack timeslot index |
| 135 | buf[0] = header->timeslot; |
| 136 | |
| 137 | // Extract frame number |
| 138 | uint32_t frame_nr = be32toh(header->frame_number); |
| 139 | |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 140 | // Pack frame number |
| 141 | buf[1] = (frame_nr >> 24) & 0xff; |
| 142 | buf[2] = (frame_nr >> 16) & 0xff; |
| 143 | buf[3] = (frame_nr >> 8) & 0xff; |
| 144 | buf[4] = (frame_nr >> 0) & 0xff; |
| 145 | |
| 146 | // Pack RSSI (-dBm) |
| 147 | buf[5] = -(uint8_t) header->signal_dbm; |
| 148 | |
| 149 | // Pack correlator timing offset (TOA) |
| 150 | // FIXME: where to find this value? |
| 151 | buf[6] = 0; |
| 152 | buf[7] = 0; |
| 153 | |
| 154 | // Extract bits {0..1} from message |
| 155 | // Despite GR-GSM uses int8_t, they are not real sbits {-127..127} |
| 156 | uint8_t *burst = (uint8_t *) |
| 157 | (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr); |
| 158 | |
| 159 | // Convert to transceiver interface specific bits {255..0} |
| 160 | for (int i = 0; i < 148; i++) |
| 161 | buf[8 + i] = burst[i] ? 255 : 0; |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame] | 162 | |
| 163 | // Fill two unused bytes |
| 164 | buf[156] = 0x00; |
| 165 | buf[157] = 0x00; |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 169 | trx_burst_if_impl::handle_dl_burst(pmt::pmt_t msg) |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 170 | { |
| 171 | // 8 bytes of header + 148 bytes of burst |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame] | 172 | // + two unused, but required bytes |
| 173 | // otherwise bursts would be rejected |
| 174 | uint8_t buf[158]; |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 175 | |
| 176 | // Compose a new UDP payload with burst |
| 177 | burst_pack(msg, buf); |
| 178 | |
| 179 | // Send a burst |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame] | 180 | d_data_sock->udp_send(buf, 158); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 181 | } |
| 182 | |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 183 | void |
Vadim Yanitskiy | e601c36 | 2017-10-17 08:24:25 +0700 | [diff] [blame] | 184 | trx_burst_if_impl::handle_ul_burst(uint8_t *payload, size_t len) |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 185 | { |
| 186 | // Check length according to the protocol |
| 187 | if (len != 154) |
| 188 | return; |
| 189 | |
| 190 | /* Make sure TS index is correct */ |
| 191 | if (payload[0] >= 8) |
| 192 | return; |
| 193 | |
| 194 | /* Unpack and check frame number */ |
| 195 | uint32_t fn = (payload[1] << 24) |
| 196 | | (payload[2] << 16) |
| 197 | | (payload[3] << 8) |
| 198 | | payload[4]; |
| 199 | |
| 200 | if (fn >= 2715648) |
| 201 | return; |
| 202 | |
| 203 | // Prepare a buffer for GSMTAP header and burst |
| 204 | uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE]; |
| 205 | |
| 206 | // Set up pointer to GSMTAP header structure |
| 207 | struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf; |
| 208 | memset(header, 0x00, sizeof(struct gsmtap_hdr)); |
| 209 | |
| 210 | // Fill in basic info |
| 211 | header->version = GSMTAP_VERSION; |
| 212 | header->hdr_len = sizeof(gsmtap_hdr) / 4; |
| 213 | header->type = GSMTAP_TYPE_UM_BURST; |
| 214 | |
| 215 | // Set timeslot index and frame number |
| 216 | header->timeslot = payload[0]; |
| 217 | header->frame_number = htobe32(fn); |
| 218 | |
Vadim Yanitskiy | 1d6b628 | 2017-07-19 14:23:21 +0700 | [diff] [blame] | 219 | // Check if one is a RACH burst |
| 220 | header->sub_type = detect_rach(payload + 6) ? |
| 221 | GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL; |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 222 | |
| 223 | // Copy burst bits (0 & 1) for source message |
| 224 | memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE); |
| 225 | |
| 226 | // Create a pmt blob |
| 227 | pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE); |
| 228 | pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob); |
| 229 | |
| 230 | /* Send a message to the output */ |
| 231 | message_port_pub(pmt::mp("bursts"), msg); |
| 232 | } |
| 233 | |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 234 | } /* namespace gsm */ |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 235 | } /* namespace gr */ |