From 652c257b017ae394ca9b66f13b4a5f50044ca875 Mon Sep 17 00:00:00 2001 From: Manolis Surligas Date: Fri, 29 Apr 2016 02:21:00 +0300 Subject: [PATCH] Add UDP PMT message sink block --- grc/CMakeLists.txt | 3 +- grc/satnogs_udp_msg_sink.xml | 33 ++++++++++++ include/satnogs/CMakeLists.txt | 3 +- include/satnogs/udp_msg_sink.h | 54 +++++++++++++++++++ lib/CMakeLists.txt | 3 +- lib/udp_msg_sink_impl.cc | 94 ++++++++++++++++++++++++++++++++++ lib/udp_msg_sink_impl.h | 59 +++++++++++++++++++++ swig/satnogs_swig.i | 3 ++ 8 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 grc/satnogs_udp_msg_sink.xml create mode 100644 include/satnogs/udp_msg_sink.h create mode 100644 lib/udp_msg_sink_impl.cc create mode 100644 lib/udp_msg_sink_impl.h diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index e76e60c..ee380e8 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -34,5 +34,6 @@ install(FILES satnogs_doppler_correction_cc.xml satnogs_upsat_fsk_frame_acquisition.xml satnogs_upsat_fsk_frame_encoder.xml - satnogs_whitening.xml DESTINATION share/gnuradio/grc/blocks + satnogs_whitening.xml + satnogs_udp_msg_sink.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/satnogs_udp_msg_sink.xml b/grc/satnogs_udp_msg_sink.xml new file mode 100644 index 0000000..f8b4a1a --- /dev/null +++ b/grc/satnogs_udp_msg_sink.xml @@ -0,0 +1,33 @@ + + + UDP Message Sink + satnogs_udp_msg_sink + satnogs + import satnogs + satnogs.udp_msg_sink($addr, $port, $mtu) + + IP Address + addr + "127.0.0.1" + string + + + + UDP port + port + 16886 + int + + + + MTU + mtu + 1500 + int + + + + in + message + + diff --git a/include/satnogs/CMakeLists.txt b/include/satnogs/CMakeLists.txt index 802df56..f0243a2 100644 --- a/include/satnogs/CMakeLists.txt +++ b/include/satnogs/CMakeLists.txt @@ -47,5 +47,6 @@ install(FILES freq_drift.h upsat_fsk_frame_acquisition.h upsat_fsk_frame_encoder.h - whitening.h DESTINATION include/satnogs + whitening.h + udp_msg_sink.h DESTINATION include/satnogs ) diff --git a/include/satnogs/udp_msg_sink.h b/include/satnogs/udp_msg_sink.h new file mode 100644 index 0000000..30d61ae --- /dev/null +++ b/include/satnogs/udp_msg_sink.h @@ -0,0 +1,54 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * 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 . + */ + +#ifndef INCLUDED_SATNOGS_UDP_MSG_SINK_H +#define INCLUDED_SATNOGS_UDP_MSG_SINK_H + +#include +#include + +namespace gr { + namespace satnogs { + + /*! + * \brief <+description of block+> + * \ingroup satnogs + * + */ + class SATNOGS_API udp_msg_sink : virtual public gr::block + { + public: + typedef boost::shared_ptr sptr; + + + /** + * UDP sink that accepts PMT messages + * @param addr the address of the destination host + * @param port the destination UDP port + * @param mtu the maximum MTU + */ + static sptr make(const std::string& addr, uint16_t port, size_t mtu); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_UDP_MSG_SINK_H */ + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 870bf06..d47dedd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -45,7 +45,8 @@ list(APPEND satnogs_sources freq_drift.cc upsat_fsk_frame_acquisition_impl.cc upsat_fsk_frame_encoder_impl.cc - whitening.cc ) + whitening.cc + udp_msg_sink_impl.cc ) set(satnogs_sources "${satnogs_sources}" PARENT_SCOPE) if(NOT satnogs_sources) diff --git a/lib/udp_msg_sink_impl.cc b/lib/udp_msg_sink_impl.cc new file mode 100644 index 0000000..d7f116a --- /dev/null +++ b/lib/udp_msg_sink_impl.cc @@ -0,0 +1,94 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * 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 . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include "udp_msg_sink_impl.h" +#include + +namespace gr { + namespace satnogs { + + udp_msg_sink::sptr + udp_msg_sink::make(const std::string& addr, uint16_t port, size_t mtu) + { + return gnuradio::get_initial_sptr + (new udp_msg_sink_impl(addr, port, mtu)); + } + + /* + * The private constructor + */ + udp_msg_sink_impl::udp_msg_sink_impl (const std::string& addr, + uint16_t port, size_t mtu) : + gr::block ("udp_msg_sink", + gr::io_signature::make (0, 0, 0), + gr::io_signature::make (0, 0, 0)), + d_iface_addr(addr), + d_udp_port(port), + d_mtu(mtu) + { + message_port_register_in(pmt::mp("in")); + set_msg_handler(pmt::mp("in"), + boost::bind(&udp_msg_sink_impl::msg_handler, + this, _1)); + + /* Open the socket */ + if ((d_sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + perror ("opening UDP socket"); + throw std::runtime_error("Could not open UDP socket"); + } + + memset (&d_sin, 0, sizeof(struct sockaddr_in)); + d_sin.sin_family = AF_INET; + d_sin.sin_port = htons (d_udp_port); + + if( inet_aton(d_iface_addr.c_str(), &(d_sin.sin_addr)) == 0){ + LOG_ERROR("Wrong IP address"); + close(d_sock); + throw std::runtime_error("Wrong IP address"); + } + } + + void + udp_msg_sink_impl::msg_handler (pmt::pmt_t msg) + { + const void *buf = pmt::blob_data(msg); + size_t len = pmt::blob_length(msg); + + if(len < d_mtu){ + sendto(d_sock, buf, len, 0, (sockaddr *) &d_sin, sizeof(sockaddr_in)); + } + } + + /* + * Our virtual destructor. + */ + udp_msg_sink_impl::~udp_msg_sink_impl() + { + close(d_sock); + } + + } /* namespace satnogs */ +} /* namespace gr */ + diff --git a/lib/udp_msg_sink_impl.h b/lib/udp_msg_sink_impl.h new file mode 100644 index 0000000..65e002d --- /dev/null +++ b/lib/udp_msg_sink_impl.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * 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 . + */ + +#ifndef INCLUDED_SATNOGS_UDP_MSG_SINK_IMPL_H +#define INCLUDED_SATNOGS_UDP_MSG_SINK_IMPL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gr +{ + namespace satnogs + { + + class udp_msg_sink_impl : public udp_msg_sink + { + private: + const std::string d_iface_addr; + const uint16_t d_udp_port; + const size_t d_mtu; + int d_sock; + struct sockaddr_in d_sin; + + void msg_handler(pmt::pmt_t msg); + + public: + udp_msg_sink_impl (const std::string& addr, uint16_t port, size_t mtu); + ~udp_msg_sink_impl (); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_UDP_MSG_SINK_IMPL_H */ + diff --git a/swig/satnogs_swig.i b/swig/satnogs_swig.i index 9e1f567..5e6ec2f 100644 --- a/swig/satnogs_swig.i +++ b/swig/satnogs_swig.i @@ -28,6 +28,7 @@ #include "satnogs/upsat_fsk_frame_acquisition.h" #include "satnogs/upsat_fsk_frame_encoder.h" #include "satnogs/whitening.h" +#include "satnogs/udp_msg_sink.h" %} @@ -67,3 +68,5 @@ GR_SWIG_BLOCK_MAGIC2(satnogs, upsat_fsk_frame_acquisition); %include "satnogs/upsat_fsk_frame_encoder.h" GR_SWIG_BLOCK_MAGIC2(satnogs, upsat_fsk_frame_encoder); %include "satnogs/whitening.h" +%include "satnogs/udp_msg_sink.h" +GR_SWIG_BLOCK_MAGIC2(satnogs, udp_msg_sink);