Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * Copyright 2013 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This file is part of GNU Radio |
| 6 | * |
| 7 | * GNU Radio is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * GNU Radio is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Radio; see the file COPYING. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/thread/thread.h> |
| 28 | #include <gnuradio/io_signature.h> |
| 29 | #include <gnuradio/blocks/pdu.h> |
| 30 | #include <pmt/pmt.h> |
| 31 | |
| 32 | #include <boost/lexical_cast.hpp> |
| 33 | #include "udp_socket.h" |
| 34 | |
| 35 | using boost::asio::ip::udp; |
| 36 | |
| 37 | namespace gr { |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 38 | namespace gsm { |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 39 | |
| 40 | udp_socket::udp_socket( |
| 41 | const std::string &remote_addr, |
| 42 | const std::string &src_port, |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 43 | const std::string &dst_port, |
| 44 | size_t mtu) |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 45 | { |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 46 | // Resize receive buffer according to MTU value |
| 47 | d_rxbuf.resize(mtu); |
| 48 | |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 49 | // Resolve remote host address |
| 50 | udp::resolver resolver(d_io_service); |
| 51 | |
| 52 | udp::resolver::query rx_query( |
| 53 | udp::v4(), remote_addr, src_port, |
| 54 | boost::asio::ip::resolver_query_base::passive); |
| 55 | udp::resolver::query tx_query( |
| 56 | udp::v4(), remote_addr, dst_port, |
| 57 | boost::asio::ip::resolver_query_base::passive); |
| 58 | |
| 59 | d_udp_endpoint_rx = *resolver.resolve(rx_query); |
| 60 | d_udp_endpoint_tx = *resolver.resolve(tx_query); |
| 61 | |
| 62 | // Create a socket |
| 63 | d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx)); |
| 64 | |
| 65 | // Setup read handler |
| 66 | d_udp_socket->async_receive_from( |
| 67 | boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, |
| 68 | boost::bind(&udp_socket::handle_udp_read, this, |
| 69 | boost::asio::placeholders::error, |
| 70 | boost::asio::placeholders::bytes_transferred)); |
| 71 | |
| 72 | // Start server |
| 73 | d_thread = gr::thread::thread( |
| 74 | boost::bind(&udp_socket::run_io_service, this)); |
| 75 | } |
| 76 | |
| 77 | udp_socket::~udp_socket() |
| 78 | { |
| 79 | // Stop server |
| 80 | d_io_service.stop(); |
| 81 | d_thread.interrupt(); |
| 82 | d_thread.join(); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | udp_socket::run_io_service(void) |
| 87 | { |
| 88 | d_io_service.run(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | udp_socket::udp_send(uint8_t *data, size_t len) |
| 93 | { |
| 94 | d_udp_socket->send_to( |
| 95 | boost::asio::buffer(data, len), |
| 96 | d_udp_endpoint_tx); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | udp_socket::handle_udp_read( |
| 101 | const boost::system::error_code& error, |
| 102 | size_t bytes_transferred) |
| 103 | { |
| 104 | if (error) |
| 105 | return; |
| 106 | |
Vadim Yanitskiy | ac3a527 | 2017-07-19 03:05:07 +0700 | [diff] [blame] | 107 | // Call incoming data handler |
| 108 | if (udp_rx_handler != NULL) |
| 109 | udp_rx_handler((uint8_t *) &d_rxbuf[0], bytes_transferred); |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 110 | |
| 111 | d_udp_socket->async_receive_from( |
| 112 | boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, |
| 113 | boost::bind(&udp_socket::handle_udp_read, this, |
| 114 | boost::asio::placeholders::error, |
| 115 | boost::asio::placeholders::bytes_transferred)); |
| 116 | } |
| 117 | |
Piotr Krysik | 264fbf6 | 2017-11-05 12:25:51 +0100 | [diff] [blame] | 118 | } /* namespace gsm */ |
Vadim Yanitskiy | 89fc14b | 2017-06-16 21:00:29 +0700 | [diff] [blame] | 119 | }/* namespace gr */ |