blob: c43f1833be664019a3c7c1906aae76c158fda6aa [file] [log] [blame]
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +07001/* -*- 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>
Piotr Krysik99305532018-05-05 12:38:11 +020033#include "grgsm/misc_utils/udp_socket.h"
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070034
35using boost::asio::ip::udp;
36
37namespace gr {
Piotr Krysik264fbf62017-11-05 12:25:51 +010038 namespace gsm {
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070039
40 udp_socket::udp_socket(
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070041 const std::string &bind_addr,
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070042 const std::string &src_port,
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070043 const std::string &remote_addr,
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070044 const std::string &dst_port,
45 size_t mtu)
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070046 {
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +070047 // Resize receive buffer according to MTU value
48 d_rxbuf.resize(mtu);
49
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070050 // Resolve remote host address
51 udp::resolver resolver(d_io_service);
52
53 udp::resolver::query rx_query(
Vadim Yanitskiy5394c602018-08-10 00:01:26 +070054 udp::v4(), bind_addr, src_port,
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070055 boost::asio::ip::resolver_query_base::passive);
56 udp::resolver::query tx_query(
57 udp::v4(), remote_addr, dst_port,
58 boost::asio::ip::resolver_query_base::passive);
59
60 d_udp_endpoint_rx = *resolver.resolve(rx_query);
61 d_udp_endpoint_tx = *resolver.resolve(tx_query);
62
63 // Create a socket
64 d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx));
65
66 // Setup read handler
67 d_udp_socket->async_receive_from(
68 boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx,
69 boost::bind(&udp_socket::handle_udp_read, this,
70 boost::asio::placeholders::error,
71 boost::asio::placeholders::bytes_transferred));
72
73 // Start server
74 d_thread = gr::thread::thread(
75 boost::bind(&udp_socket::run_io_service, this));
76 }
77
78 udp_socket::~udp_socket()
79 {
80 // Stop server
81 d_io_service.stop();
82 d_thread.interrupt();
83 d_thread.join();
84 }
85
86 void
87 udp_socket::run_io_service(void)
88 {
89 d_io_service.run();
90 }
91
92 void
93 udp_socket::udp_send(uint8_t *data, size_t len)
94 {
95 d_udp_socket->send_to(
96 boost::asio::buffer(data, len),
97 d_udp_endpoint_tx);
98 }
99
100 void
101 udp_socket::handle_udp_read(
102 const boost::system::error_code& error,
103 size_t bytes_transferred)
104 {
105 if (error)
106 return;
107
Vadim Yanitskiyac3a5272017-07-19 03:05:07 +0700108 // Call incoming data handler
109 if (udp_rx_handler != NULL)
110 udp_rx_handler((uint8_t *) &d_rxbuf[0], bytes_transferred);
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700111
112 d_udp_socket->async_receive_from(
113 boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx,
114 boost::bind(&udp_socket::handle_udp_read, this,
115 boost::asio::placeholders::error,
116 boost::asio::placeholders::bytes_transferred));
117 }
118
Piotr Krysik264fbf62017-11-05 12:25:51 +0100119 } /* namespace gsm */
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700120}/* namespace gr */