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 | |
| 30 | #include "udp_socket.h" |
| 31 | #include "trx_impl.h" |
| 32 | |
| 33 | namespace gr { |
| 34 | namespace grgsm { |
| 35 | |
| 36 | trx::sptr |
| 37 | trx::make( |
| 38 | const std::string &remote_addr, |
| 39 | const std::string &base_port) |
| 40 | { |
| 41 | int base_port_int = boost::lexical_cast<int> (base_port); |
| 42 | |
| 43 | return gnuradio::get_initial_sptr |
| 44 | (new trx_impl(remote_addr, base_port_int)); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * The private constructor |
| 49 | */ |
| 50 | trx_impl::trx_impl(const std::string &remote_addr, int base_port) |
| 51 | : gr::block("trx", |
| 52 | gr::io_signature::make(0, 0, 0), |
| 53 | gr::io_signature::make(0, 0, 0)) |
| 54 | { |
| 55 | message_port_register_in(pmt::mp("bursts")); |
| 56 | set_msg_handler(pmt::mp("bursts"), |
| 57 | boost::bind(&trx_impl::handle_dl_burst, this, _1)); |
| 58 | |
| 59 | // Prepare port numbers |
| 60 | std::string clck_src_port = boost::lexical_cast<std::string> (base_port + 0); |
| 61 | std::string clck_dst_port = boost::lexical_cast<std::string> (base_port + 100); |
| 62 | std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2); |
| 63 | std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102); |
| 64 | |
| 65 | // Init DATA interface |
| 66 | d_data_sock = new udp_socket(remote_addr, data_src_port, data_dst_port); |
| 67 | d_clck_sock = new udp_socket(remote_addr, clck_src_port, clck_dst_port); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Our virtual destructor. |
| 72 | */ |
| 73 | trx_impl::~trx_impl() |
| 74 | { |
| 75 | // Release all UDP sockets and free memory |
| 76 | delete d_data_sock; |
| 77 | delete d_clck_sock; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Create an UDP payload with clock indication |
| 82 | */ |
| 83 | void |
| 84 | trx_impl::clck_ind_send(uint32_t frame_nr) |
| 85 | { |
| 86 | char buf[20]; |
| 87 | size_t n; |
| 88 | |
| 89 | n = snprintf(buf, 20, "IND CLOCK %u", frame_nr); |
| 90 | d_clck_sock->udp_send((uint8_t *) buf, n + 1); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Create an UDP payload with burst bits |
| 95 | * and some channel data. |
| 96 | */ |
| 97 | void |
| 98 | trx_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf) |
| 99 | { |
| 100 | pmt::pmt_t header_plus_burst = pmt::cdr(msg); |
| 101 | |
| 102 | // Extract GSMTAP header from message |
| 103 | gsmtap_hdr *header = (gsmtap_hdr *) |
| 104 | pmt::blob_data(header_plus_burst); |
| 105 | |
| 106 | // Pack timeslot index |
| 107 | buf[0] = header->timeslot; |
| 108 | |
| 109 | // Extract frame number |
| 110 | uint32_t frame_nr = be32toh(header->frame_number); |
| 111 | |
| 112 | // HACK: send clock indications every 51-th frame |
| 113 | if (frame_nr % 51 == 0) |
| 114 | clck_ind_send(frame_nr); |
| 115 | |
| 116 | // Pack frame number |
| 117 | buf[1] = (frame_nr >> 24) & 0xff; |
| 118 | buf[2] = (frame_nr >> 16) & 0xff; |
| 119 | buf[3] = (frame_nr >> 8) & 0xff; |
| 120 | buf[4] = (frame_nr >> 0) & 0xff; |
| 121 | |
| 122 | // Pack RSSI (-dBm) |
| 123 | buf[5] = -(uint8_t) header->signal_dbm; |
| 124 | |
| 125 | // Pack correlator timing offset (TOA) |
| 126 | // FIXME: where to find this value? |
| 127 | buf[6] = 0; |
| 128 | buf[7] = 0; |
| 129 | |
| 130 | // Extract bits {0..1} from message |
| 131 | // Despite GR-GSM uses int8_t, they are not real sbits {-127..127} |
| 132 | uint8_t *burst = (uint8_t *) |
| 133 | (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr); |
| 134 | |
| 135 | // Convert to transceiver interface specific bits {255..0} |
| 136 | for (int i = 0; i < 148; i++) |
| 137 | buf[8 + i] = burst[i] ? 255 : 0; |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame^] | 138 | |
| 139 | // Fill two unused bytes |
| 140 | buf[156] = 0x00; |
| 141 | buf[157] = 0x00; |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | trx_impl::handle_dl_burst(pmt::pmt_t msg) |
| 146 | { |
| 147 | // 8 bytes of header + 148 bytes of burst |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame^] | 148 | // + two unused, but required bytes |
| 149 | // otherwise bursts would be rejected |
| 150 | uint8_t buf[158]; |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 151 | |
| 152 | // Compose a new UDP payload with burst |
| 153 | burst_pack(msg, buf); |
| 154 | |
| 155 | // Send a burst |
Vadim Yanitskiy | c36c498 | 2017-07-16 23:16:54 +0700 | [diff] [blame^] | 156 | d_data_sock->udp_send(buf, 158); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } /* namespace grgsm */ |
| 160 | } /* namespace gr */ |