diff options
author | Piotr Krysik <ptrkrysik@gmail.com> | 2018-04-16 22:21:29 +0200 |
---|---|---|
committer | Piotr Krysik <ptrkrysik@gmail.com> | 2018-04-16 22:21:29 +0200 |
commit | 8a8d41a58320d5f30713a6e6362c86d5db6de1de (patch) | |
tree | d95b1badab52b83126d57d410dd8e92e7a255c8e /lib/misc_utils | |
parent | 8b9b88d788cc5064c0f6d7aa60a14954aee59eab (diff) | |
parent | fe4db93e6d03e92a958664bfffcd0e2923f20e34 (diff) |
Merge branch 'ptrkrysik/trx' into development
Diffstat (limited to 'lib/misc_utils')
-rw-r--r-- | lib/misc_utils/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lib/misc_utils/controlled_fractional_resampler_cc_impl.cc | 2 | ||||
-rw-r--r-- | lib/misc_utils/controlled_fractional_resampler_cc_impl.h | 2 | ||||
-rw-r--r-- | lib/misc_utils/fn_time.cc | 94 | ||||
-rw-r--r-- | lib/misc_utils/msg_to_tag_impl.cc | 2 | ||||
-rw-r--r-- | lib/misc_utils/msg_to_tag_impl.h | 2 | ||||
-rw-r--r-- | lib/misc_utils/time_spec.cc | 122 | ||||
-rw-r--r-- | lib/misc_utils/trx_burst_if_impl.cc | 231 | ||||
-rw-r--r-- | lib/misc_utils/trx_burst_if_impl.h | 54 | ||||
-rw-r--r-- | lib/misc_utils/udp_socket.cc | 119 | ||||
-rw-r--r-- | lib/misc_utils/udp_socket.h | 69 |
11 files changed, 697 insertions, 4 deletions
diff --git a/lib/misc_utils/CMakeLists.txt b/lib/misc_utils/CMakeLists.txt index 720eee3..e0fc818 100644 --- a/lib/misc_utils/CMakeLists.txt +++ b/lib/misc_utils/CMakeLists.txt @@ -33,5 +33,9 @@ add_sources( message_printer_impl.cc msg_to_tag_impl.cc tmsi_dumper_impl.cc + time_spec.cc + fn_time.cc + udp_socket.cc + trx_burst_if_impl.cc ) diff --git a/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc b/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc index 3376c67..36cf170 100644 --- a/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc +++ b/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc @@ -177,6 +177,6 @@ namespace gr { set_relative_rate(1.0 / resamp_ratio); } - } /* namespace grgsm */ + } /* namespace gsm */ } /* namespace gr */ diff --git a/lib/misc_utils/controlled_fractional_resampler_cc_impl.h b/lib/misc_utils/controlled_fractional_resampler_cc_impl.h index 24e00ed..1ca8082 100644 --- a/lib/misc_utils/controlled_fractional_resampler_cc_impl.h +++ b/lib/misc_utils/controlled_fractional_resampler_cc_impl.h @@ -64,7 +64,7 @@ namespace gr { void set_resamp_ratio(float resamp_ratio); }; - } // namespace grgsm + } // namespace gsm } // namespace gr #endif /* INCLUDED_GRGSM_CONTROLLED_FRACTIONAL_RESAMPLER_CC_IMPL_H */ diff --git a/lib/misc_utils/fn_time.cc b/lib/misc_utils/fn_time.cc new file mode 100644 index 0000000..2773f33 --- /dev/null +++ b/lib/misc_utils/fn_time.cc @@ -0,0 +1,94 @@ +/* -*- c++ -*- */ +/* @file + * @author Piotr Krysik <ptrkrysik@gmail.com> + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#include <grgsm/misc_utils/fn_time.h> +#include <grgsm/misc_utils/time_spec.h> +#include <math.h> + +#define GSM_HYPER_FRAME (26 * 51 * 2048) +#define GSM_SYM_RATE (13.0e6 / 48.0) + +#define GSM_TS_PERIOD (156.25 / GSM_SYM_RATE) +#define GSM_FN_PERIOD (8 * GSM_TS_PERIOD) + +namespace gr { + namespace gsm { + /** + * Computes difference between two frame numbers modulo + * GSM_HYPER_FRAME / 2. The result is correct if difference + * between the frame numbers is not bigger than GSM_HYPER_FRAME / 2. + * @param fn1 first frame number + * @param fn2 second frame number + * @return computed difference + */ + static int fnmod_delta(uint32_t fn1, uint32_t fn2) + { + int delta, h2; + + delta = (fn1 % GSM_HYPER_FRAME) - (fn2 % GSM_HYPER_FRAME); + + h2 = GSM_HYPER_FRAME / 2; + + if (delta >= h2) { + delta -= GSM_HYPER_FRAME; + } else if(delta < -h2) { + delta += GSM_HYPER_FRAME; + } + + return delta; + } + + static int fn_time_diff_delta(uint32_t fn, uint32_t fn_ref, + time_spec_t time_diff_hint) + { + int frames_diff, fn_delta; + frames_diff = int(round(time_diff_hint.get_real_secs() / GSM_FN_PERIOD)); + fn_delta = fnmod_delta(fn, fn_ref + frames_diff) + frames_diff; + + return fn_delta; + } + + /** + * Computes difference between reference frame number + * and a second frame number. + * @param fn_ref reference frame number modulo GSM_HYPER_FRAME + * @param fn second frame number modulo GSM_HYPER_FRAME + * @param time_ref precise timestamp of the first sample in the fn_ref + * @param time_hint coarse time for fn that is used as a hint to avoid + * ambiguities caused by modulo operation applied to + * frame numbers + * @return difference between fn_ref and fn + */ + time_format fn_time_delta_cpp(uint32_t fn_ref, time_format time_ref, uint32_t fn_x, + time_format time_hint, uint32_t ts_num, uint32_t ts_ref) + { + time_spec_t time_diff_hint = time_spec_t(time_hint.first, time_hint.second) - time_spec_t(time_ref.first, time_ref.second); + int fn_delta = fn_time_diff_delta(fn_x, fn_ref, time_diff_hint); + time_spec_t time_x_precise = fn_delta * GSM_FN_PERIOD + time_spec_t(time_ref.first, time_ref.second) + (static_cast<int>(ts_num) - static_cast<int>(ts_ref)) * GSM_TS_PERIOD; + + return time_format(time_x_precise.get_full_secs(), time_x_precise.get_frac_secs()); + } + + } /* namespace gsm */ +} /* namespace gr */ + diff --git a/lib/misc_utils/msg_to_tag_impl.cc b/lib/misc_utils/msg_to_tag_impl.cc index 0ab65e7..e08c7e6 100644 --- a/lib/misc_utils/msg_to_tag_impl.cc +++ b/lib/misc_utils/msg_to_tag_impl.cc @@ -94,6 +94,6 @@ namespace gr { return noutput_items; } - } /* namespace grgsm */ + } /* namespace gsm */ } /* namespace gr */ diff --git a/lib/misc_utils/msg_to_tag_impl.h b/lib/misc_utils/msg_to_tag_impl.h index 39c6ca3..55e1fae 100644 --- a/lib/misc_utils/msg_to_tag_impl.h +++ b/lib/misc_utils/msg_to_tag_impl.h @@ -44,7 +44,7 @@ namespace gr { gr_vector_void_star &output_items); }; - } // namespace grgsm + } // namespace gsm } // namespace gr #endif /* INCLUDED_GRGSM_MSG_TO_TAG_IMPL_H */ diff --git a/lib/misc_utils/time_spec.cc b/lib/misc_utils/time_spec.cc new file mode 100644 index 0000000..5293da2 --- /dev/null +++ b/lib/misc_utils/time_spec.cc @@ -0,0 +1,122 @@ +// +// Copyright 2011-2013 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// + +#include <grgsm/misc_utils/time_spec.h> + +namespace gr { + namespace gsm { + + /*********************************************************************** + * Time spec constructors + **********************************************************************/ + #define time_spec_init(full, frac) { \ + const time_t _full = time_t(full); \ + const double _frac = double(frac); \ + const int _frac_int = int(_frac); \ + _full_secs = _full + _frac_int; \ + _frac_secs = _frac - _frac_int; \ + if (_frac_secs < 0) {\ + _full_secs -= 1; \ + _frac_secs += 1; \ + } \ + } + + inline long long fast_llround(const double x){ + return (long long)(x + 0.5); // assumption of non-negativity + } + + time_spec_t::time_spec_t(const time_spec_t & spec){ + time_spec_init(spec.get_full_secs(), spec.get_frac_secs()); + } + + time_spec_t::time_spec_t(double secs){ + time_spec_init(0, secs); + } + + time_spec_t::time_spec_t(time_t full_secs, double frac_secs){ + time_spec_init(full_secs, frac_secs); + } + + time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){ + const double frac_secs = tick_count/tick_rate; + time_spec_init(full_secs, frac_secs); + } + + time_spec_t time_spec_t::from_ticks(long long ticks, double tick_rate){ + const long long rate_i = (long long)(tick_rate); + const double rate_f = tick_rate - rate_i; + const time_t secs_full = time_t(ticks/rate_i); + const long long ticks_error = ticks - (secs_full*rate_i); + const double ticks_frac = ticks_error - secs_full*rate_f; + return time_spec_t(secs_full, ticks_frac/tick_rate); + } + + /*********************************************************************** + * Time spec accessors + **********************************************************************/ + long time_spec_t::get_tick_count(double tick_rate) const{ + return long(fast_llround(this->get_frac_secs()*tick_rate)); + } + + long long time_spec_t::to_ticks(double tick_rate) const{ + const long long rate_i = (long long)(tick_rate); + const double rate_f = tick_rate - rate_i; + const long long ticks_full = this->get_full_secs()*rate_i; + const double ticks_error = this->get_full_secs()*rate_f; + const double ticks_frac = this->get_frac_secs()*tick_rate; + return ticks_full + fast_llround(ticks_error + ticks_frac); + } + + double time_spec_t::get_real_secs(void) const{ + return this->get_full_secs() + this->get_frac_secs(); + } + + /*********************************************************************** + * Time spec math overloads + **********************************************************************/ + time_spec_t &time_spec_t::operator+=(const time_spec_t &rhs){ + time_spec_init( + this->get_full_secs() + rhs.get_full_secs(), + this->get_frac_secs() + rhs.get_frac_secs() + ); + return *this; + } + + time_spec_t &time_spec_t::operator-=(const time_spec_t &rhs){ + time_spec_init( + this->get_full_secs() - rhs.get_full_secs(), + this->get_frac_secs() - rhs.get_frac_secs() + ); + return *this; + } + + bool operator==(const time_spec_t &lhs, const time_spec_t &rhs){ + return + lhs.get_full_secs() == rhs.get_full_secs() and + lhs.get_frac_secs() == rhs.get_frac_secs() + ; + } + + bool operator<(const time_spec_t &lhs, const time_spec_t &rhs){ + return ( + (lhs.get_full_secs() < rhs.get_full_secs()) or ( + (lhs.get_full_secs() == rhs.get_full_secs()) and + (lhs.get_frac_secs() < rhs.get_frac_secs()) + )); + } + } +} diff --git a/lib/misc_utils/trx_burst_if_impl.cc b/lib/misc_utils/trx_burst_if_impl.cc new file mode 100644 index 0000000..8f458b0 --- /dev/null +++ b/lib/misc_utils/trx_burst_if_impl.cc @@ -0,0 +1,231 @@ +/* -*- c++ -*- */ +/* @file + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/io_signature.h> +#include <boost/lexical_cast.hpp> + +#include "udp_socket.h" +#include "trx_burst_if_impl.h" + +#define BURST_SIZE 148 +#define DATA_IF_MTU 160 + +/** + * 41-bit RACH synchronization sequence + * GSM 05.02 Chapter 5.2.7 Access burst (AB) + */ +static uint8_t rach_synch_seq[] = { + 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, + 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, +}; + +namespace gr { + namespace gsm { + + trx_burst_if::sptr + trx_burst_if::make( + const std::string &remote_addr, + const std::string &base_port) + { + int base_port_int = boost::lexical_cast<int> (base_port); + + return gnuradio::get_initial_sptr + (new trx_burst_if_impl(remote_addr, base_port_int)); + } + + /* + * The private constructor + */ + trx_burst_if_impl::trx_burst_if_impl( + const std::string &remote_addr, + int base_port + ) : gr::block("trx_burst_if", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)) + { + message_port_register_in(pmt::mp("bursts")); + message_port_register_out(pmt::mp("bursts")); + + // Bind a port handler + set_msg_handler(pmt::mp("bursts"), + boost::bind(&trx_burst_if_impl::handle_dl_burst, this, _1)); + + // Prepare port numbers + std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2); + std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102); + + // Init DATA interface + d_data_sock = new udp_socket(remote_addr, + data_src_port, data_dst_port, DATA_IF_MTU); + + // Bind DATA interface handler + d_data_sock->udp_rx_handler = boost::bind( + &trx_burst_if_impl::handle_ul_burst, this, _1, _2); + } + + /* + * Our virtual destructor. + */ + trx_burst_if_impl::~trx_burst_if_impl() + { + // Release all UDP sockets and free memory + delete d_data_sock; + } + + /* + * Check if burst is a RACH burst + */ + bool trx_burst_if_impl::detect_rach(uint8_t *burst) + { + // Compare synchronization sequence + for (int i = 0; i < 41; i++) + if (burst[i + 8] != rach_synch_seq[i]) + return false; + + // Make sure TB and GP are filled by 0x00 + for (int i = 0; i < 63; i++) + if (burst[i + 85] != 0x00) + return false; + + return true; + } + + /* + * Create an UDP payload with burst bits + * and some channel data. + */ + void + trx_burst_if_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf) + { + pmt::pmt_t header_plus_burst = pmt::cdr(msg); + + // Extract GSMTAP header from message + gsmtap_hdr *header = (gsmtap_hdr *) + pmt::blob_data(header_plus_burst); + + // Pack timeslot index + buf[0] = header->timeslot; + + // Extract frame number + uint32_t frame_nr = be32toh(header->frame_number); + + // Pack frame number + buf[1] = (frame_nr >> 24) & 0xff; + buf[2] = (frame_nr >> 16) & 0xff; + buf[3] = (frame_nr >> 8) & 0xff; + buf[4] = (frame_nr >> 0) & 0xff; + + // Pack RSSI (-dBm) + buf[5] = -(uint8_t) header->signal_dbm; + + // Pack correlator timing offset (TOA) + // FIXME: where to find this value? + buf[6] = 0; + buf[7] = 0; + + // Extract bits {0..1} from message + // Despite GR-GSM uses int8_t, they are not real sbits {-127..127} + uint8_t *burst = (uint8_t *) + (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr); + + // Convert to transceiver interface specific bits {255..0} + for (int i = 0; i < 148; i++) + buf[8 + i] = burst[i] ? 255 : 0; + + // Fill two unused bytes + buf[156] = 0x00; + buf[157] = 0x00; + } + + void + trx_burst_if_impl::handle_dl_burst(pmt::pmt_t msg) + { + // 8 bytes of header + 148 bytes of burst + // + two unused, but required bytes + // otherwise bursts would be rejected + uint8_t buf[158]; + + // Compose a new UDP payload with burst + burst_pack(msg, buf); + + // Send a burst + d_data_sock->udp_send(buf, 158); + } + + void + trx_burst_if_impl::handle_ul_burst(uint8_t *payload, size_t len) + { + // Check length according to the protocol + if (len != 154) + return; + + /* Make sure TS index is correct */ + if (payload[0] >= 8) + return; + + /* Unpack and check frame number */ + uint32_t fn = (payload[1] << 24) + | (payload[2] << 16) + | (payload[3] << 8) + | payload[4]; + + if (fn >= 2715648) + return; + + // Prepare a buffer for GSMTAP header and burst + uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE]; + + // Set up pointer to GSMTAP header structure + struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf; + memset(header, 0x00, sizeof(struct gsmtap_hdr)); + + // Fill in basic info + header->version = GSMTAP_VERSION; + header->hdr_len = sizeof(gsmtap_hdr) / 4; + header->type = GSMTAP_TYPE_UM_BURST; + + // Set timeslot index and frame number + header->timeslot = payload[0]; + header->frame_number = htobe32(fn); + + // Check if one is a RACH burst + header->sub_type = detect_rach(payload + 6) ? + GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL; + + // Copy burst bits (0 & 1) for source message + memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE); + + // Create a pmt blob + pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE); + pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob); + + /* Send a message to the output */ + message_port_pub(pmt::mp("bursts"), msg); + } + + } /* namespace gsm */ +} /* namespace gr */ diff --git a/lib/misc_utils/trx_burst_if_impl.h b/lib/misc_utils/trx_burst_if_impl.h new file mode 100644 index 0000000..35f939c --- /dev/null +++ b/lib/misc_utils/trx_burst_if_impl.h @@ -0,0 +1,54 @@ +/* -*- c++ -*- */ +/* @file + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef INCLUDED_GRGSM_TRX_BURST_IF_IMPL_H +#define INCLUDED_GRGSM_TRX_BURST_IF_IMPL_H + +#include <stddef.h> + +#include <grgsm/gsmtap.h> +#include <grgsm/misc_utils/trx_burst_if.h> + +namespace gr { + namespace gsm { + + class trx_burst_if_impl : public trx_burst_if + { + private: + udp_socket *d_data_sock; + + bool detect_rach(uint8_t *burst); + void burst_pack(pmt::pmt_t msg, uint8_t *buf); + + public: + trx_burst_if_impl(const std::string &remote_addr, int base_port); + ~trx_burst_if_impl(); + + void handle_dl_burst(pmt::pmt_t msg); + void handle_ul_burst(uint8_t *payload, size_t len); + }; + + } // namespace gsm +} // namespace gr + +#endif /* INCLUDED_GRGSM_TRX_BURST_IF_IMPL_H */ + diff --git a/lib/misc_utils/udp_socket.cc b/lib/misc_utils/udp_socket.cc new file mode 100644 index 0000000..5c689c7 --- /dev/null +++ b/lib/misc_utils/udp_socket.cc @@ -0,0 +1,119 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/thread/thread.h> +#include <gnuradio/io_signature.h> +#include <gnuradio/blocks/pdu.h> +#include <pmt/pmt.h> + +#include <boost/lexical_cast.hpp> +#include "udp_socket.h" + +using boost::asio::ip::udp; + +namespace gr { + namespace gsm { + + udp_socket::udp_socket( + const std::string &remote_addr, + const std::string &src_port, + const std::string &dst_port, + size_t mtu) + { + // Resize receive buffer according to MTU value + d_rxbuf.resize(mtu); + + // Resolve remote host address + udp::resolver resolver(d_io_service); + + udp::resolver::query rx_query( + udp::v4(), remote_addr, src_port, + boost::asio::ip::resolver_query_base::passive); + udp::resolver::query tx_query( + udp::v4(), remote_addr, dst_port, + boost::asio::ip::resolver_query_base::passive); + + d_udp_endpoint_rx = *resolver.resolve(rx_query); + d_udp_endpoint_tx = *resolver.resolve(tx_query); + + // Create a socket + d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx)); + + // Setup read handler + d_udp_socket->async_receive_from( + boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, + boost::bind(&udp_socket::handle_udp_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + + // Start server + d_thread = gr::thread::thread( + boost::bind(&udp_socket::run_io_service, this)); + } + + udp_socket::~udp_socket() + { + // Stop server + d_io_service.stop(); + d_thread.interrupt(); + d_thread.join(); + } + + void + udp_socket::run_io_service(void) + { + d_io_service.run(); + } + + void + udp_socket::udp_send(uint8_t *data, size_t len) + { + d_udp_socket->send_to( + boost::asio::buffer(data, len), + d_udp_endpoint_tx); + } + + void + udp_socket::handle_udp_read( + const boost::system::error_code& error, + size_t bytes_transferred) + { + if (error) + return; + + // Call incoming data handler + if (udp_rx_handler != NULL) + udp_rx_handler((uint8_t *) &d_rxbuf[0], bytes_transferred); + + d_udp_socket->async_receive_from( + boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, + boost::bind(&udp_socket::handle_udp_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + } + + } /* namespace gsm */ +}/* namespace gr */ diff --git a/lib/misc_utils/udp_socket.h b/lib/misc_utils/udp_socket.h new file mode 100644 index 0000000..15b2c66 --- /dev/null +++ b/lib/misc_utils/udp_socket.h @@ -0,0 +1,69 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GRGSM_TRX_UDP_SOCKET_H +#define INCLUDED_GRGSM_TRX_UDP_SOCKET_H + +#include <gnuradio/thread/thread.h> + +#include <boost/function.hpp> +#include <boost/array.hpp> +#include <boost/asio.hpp> +#include <boost/bind.hpp> +#include <pmt/pmt.h> + +namespace gr { + namespace gsm { + + class udp_socket + { + private: + boost::asio::io_service d_io_service; + std::vector<char> d_rxbuf; + gr::thread::thread d_thread; + bool d_started; + bool d_finished; + + boost::asio::ip::udp::endpoint d_udp_endpoint_rx; + boost::asio::ip::udp::endpoint d_udp_endpoint_tx; + boost::shared_ptr<boost::asio::ip::udp::socket> d_udp_socket; + + void handle_udp_read(const boost::system::error_code& error, + size_t bytes_transferred); + void run_io_service(void); + + public: + udp_socket( + const std::string &remote_addr, + const std::string &src_port, + const std::string &dst_port, + size_t mtu); + ~udp_socket(); + + void udp_send(uint8_t *data, size_t len); + boost::function<void (uint8_t *, size_t)> udp_rx_handler; + }; + + } /* namespace gsm */ +} /* namespace gr */ + +#endif /* INCLUDED_GRGSM_TRX_UDP_SOCKET_H */ |