blob: 4bbf206b3aa7a15baf54f13a6ee1fffd976b7353 [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>
33#include "udp_socket.h"
34
35using boost::asio::ip::udp;
36
37namespace gr {
38 namespace grgsm {
39
40 udp_socket::udp_socket(
41 const std::string &remote_addr,
42 const std::string &src_port,
43 const std::string &dst_port)
44 {
45 // Resolve remote host address
46 udp::resolver resolver(d_io_service);
47
48 udp::resolver::query rx_query(
49 udp::v4(), remote_addr, src_port,
50 boost::asio::ip::resolver_query_base::passive);
51 udp::resolver::query tx_query(
52 udp::v4(), remote_addr, dst_port,
53 boost::asio::ip::resolver_query_base::passive);
54
55 d_udp_endpoint_rx = *resolver.resolve(rx_query);
56 d_udp_endpoint_tx = *resolver.resolve(tx_query);
57
58 // Create a socket
59 d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx));
60
61 // Setup read handler
62 d_udp_socket->async_receive_from(
63 boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx,
64 boost::bind(&udp_socket::handle_udp_read, this,
65 boost::asio::placeholders::error,
66 boost::asio::placeholders::bytes_transferred));
67
68 // Start server
69 d_thread = gr::thread::thread(
70 boost::bind(&udp_socket::run_io_service, this));
71 }
72
73 udp_socket::~udp_socket()
74 {
75 // Stop server
76 d_io_service.stop();
77 d_thread.interrupt();
78 d_thread.join();
79 }
80
81 void
82 udp_socket::run_io_service(void)
83 {
84 d_io_service.run();
85 }
86
87 void
88 udp_socket::udp_send(uint8_t *data, size_t len)
89 {
90 d_udp_socket->send_to(
91 boost::asio::buffer(data, len),
92 d_udp_endpoint_tx);
93 }
94
95 void
96 udp_socket::handle_udp_read(
97 const boost::system::error_code& error,
98 size_t bytes_transferred)
99 {
100 if (error)
101 return;
102
103 pmt::pmt_t vector = pmt::init_u8vector(bytes_transferred,
104 (const uint8_t *) &d_rxbuf[0]);
105 pmt::pmt_t pdu = pmt::cons(pmt::PMT_NIL, vector);
106
107 // TODO: call handler here
108
109 d_udp_socket->async_receive_from(
110 boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx,
111 boost::bind(&udp_socket::handle_udp_read, this,
112 boost::asio::placeholders::error,
113 boost::asio::placeholders::bytes_transferred));
114 }
115
116 } /* namespace blocks */
117}/* namespace gr */