GNSS-SDR  0.0.13
An Open Source GNSS Software Defined Receiver
rtl_tcp_signal_source_c.h
Go to the documentation of this file.
1 /*!
2  * \file rtl_tcp_signal_source_c.h
3  * \brief Interface of an rtl_tcp signal source reader.
4  * \author Anthony Arnold, 2015. anthony.arnold(at)uqconnect.edu.au
5  *
6  * The implementation of this block is a combination of various helpful
7  * sources. The data format and command structure is taken from the
8  * original Osmocom rtl_tcp_source_f (https://git.osmocom.org/gr-osmosdr).
9  * The aynchronous reading code comes from the examples provides
10  * by Boost.Asio and the bounded buffer producer-consumer solution is
11  * taken from the Boost.CircularBuffer examples (https://www.boost.org/).
12  *
13  * -----------------------------------------------------------------------------
14  *
15  * Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
16  *
17  * GNSS-SDR is a software defined Global Navigation
18  * Satellite Systems receiver
19  *
20  * This file is part of GNSS-SDR.
21  *
22  * SPDX-License-Identifier: GPL-3.0-or-later
23  *
24  * -----------------------------------------------------------------------------
25  */
26 
27 #ifndef GNSS_SDR_RTL_TCP_SIGNAL_SOURCE_C_H
28 #define GNSS_SDR_RTL_TCP_SIGNAL_SOURCE_C_H
29 
30 #include "rtl_tcp_dongle_info.h"
31 #include <boost/array.hpp>
32 #include <boost/asio.hpp>
33 #include <boost/circular_buffer.hpp>
34 #include <boost/thread/condition.hpp>
35 #include <boost/thread/mutex.hpp>
36 #include <gnuradio/sync_block.h>
37 #include <cstdint>
38 #include <string>
39 #include <vector>
40 #if GNURADIO_USES_STD_POINTERS
41 #include <memory>
42 #else
43 #include <boost/shared_ptr.hpp>
44 #endif
45 
47 
48 #if GNURADIO_USES_STD_POINTERS
49 using rtl_tcp_signal_source_c_sptr = std::shared_ptr<rtl_tcp_signal_source_c>;
50 #else
51 using rtl_tcp_signal_source_c_sptr = boost::shared_ptr<rtl_tcp_signal_source_c>;
52 #endif
53 
54 #if USE_BOOST_ASIO_IO_CONTEXT
55 using b_io_context = boost::asio::io_context;
56 #else
57 using b_io_context = boost::asio::io_service;
58 #endif
59 
60 rtl_tcp_signal_source_c_sptr
61 rtl_tcp_make_signal_source_c(const std::string &address,
62  int16_t port,
63  bool flip_iq = false);
64 
65 /*!
66  * \brief This class reads interleaved I/Q samples
67  * from an rtl_tcp server and outputs complex types.
68  */
69 class rtl_tcp_signal_source_c : public gr::sync_block
70 {
71 public:
73 
74  int work(int noutput_items,
75  gr_vector_const_void_star &input_items,
76  gr_vector_void_star &output_items);
77 
78  void set_frequency(int frequency);
79  void set_sample_rate(int sample_rate);
80  void set_agc_mode(bool agc);
81  void set_gain(int gain);
82  void set_if_gain(int gain);
83 
84 private:
85  friend rtl_tcp_signal_source_c_sptr
86  rtl_tcp_make_signal_source_c(const std::string &address,
87  int16_t port,
88  bool flip_iq);
89 
90  rtl_tcp_signal_source_c(const std::string &address,
91  int16_t port,
92  bool flip_iq);
93 
94  // async read callback
95  void handle_read(const boost::system::error_code &ec,
96  size_t bytes_transferred);
97 
98  inline bool not_full() const
99  {
100  return unread_ < buffer_.capacity();
101  }
102 
103  inline bool not_empty() const
104  {
105  return unread_ > 0 || io_context_.stopped();
106  }
107 
108  boost::circular_buffer_space_optimized<float> buffer_;
109  // producer-consumer helpers
110  boost::mutex mutex_;
111  boost::condition not_full_;
112  boost::condition not_empty_;
113 
114  // lookup for scaling data
115  boost::array<float, 0xff> lookup_{};
116 
117  // IO members
118  b_io_context io_context_;
119  boost::asio::ip::tcp::socket socket_;
120  std::vector<unsigned char> data_;
121 
122  Rtl_Tcp_Dongle_Info info_;
123  size_t unread_;
124  bool flip_iq_;
125 };
126 
127 #endif // GNSS_SDR_RTL_TCP_SIGNAL_SOURCE_C_H
Interface for a structure sent by rtl_tcp defining the hardware.
This class represents the dongle information which is sent by rtl_tcp.
This class reads interleaved I/Q samples from an rtl_tcp server and outputs complex types...