GNU Radio C++ API Reference  g90d26cb
The Free & Open Software Radio Ecosystem
block_gateway.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Free Software Foundation, Inc.
3  *
4  * This file is part of GNU Radio
5  *
6  * SPDX-License-Identifier: GPL-3.0-or-later
7  *
8  */
9 
10 #ifndef INCLUDED_BLOCK_GATEWAY_H
11 #define INCLUDED_BLOCK_GATEWAY_H
12 
13 #include <gnuradio/block.h>
14 #include <gnuradio/io_signature.h>
15 #include <gnuradio/logger.h>
16 #include <gnuradio/types.h>
17 #include <pmt/pmt.h>
18 
19 #include <pybind11/pybind11.h>
20 
21 #include <functional>
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 namespace py = pybind11;
27 
28 /*!
29  * The gateway block which performs all the magic.
30  *
31  * The gateway provides access to all the gr::block routines.
32  */
33 using gw_block_t = enum {
34  GW_BLOCK_GENERAL = 0,
35  GW_BLOCK_SYNC,
36  GW_BLOCK_DECIM,
37  GW_BLOCK_INTERP
38 };
39 
40 class block_gateway : public gr::block
41 {
42 public:
43  block_gateway(const py::handle& py_handle,
44  const std::string& name,
46  gr::io_signature::sptr out_sig);
47 
48  // gr::block_gateway::sptr
49  using sptr = std::shared_ptr<block_gateway>;
50 
51  /*!
52  * Make a new gateway block.
53  * \param py_handle the pybind11 object with callback
54  * \param name the name of the block (Ex: "Shirley")
55  * \param in_sig the input signature for this block
56  * \param out_sig the output signature for this block
57  * \return a new gateway block
58  */
59  static sptr make(const py::object& py_handle,
60  const std::string& name,
62  gr::io_signature::sptr out_sig);
63 
64  /*******************************************************************
65  * Public versions of gr::block protected methods, necessary because
66  * we need public methods for the python bindings
67  ******************************************************************/
68  void _add_item_tag(unsigned int which_output, const gr::tag_t& tag);
69 
70  void _add_item_tag(unsigned int which_output,
71  uint64_t abs_offset,
72  const pmt::pmt_t& key,
73  const pmt::pmt_t& value,
74  const pmt::pmt_t& srcid);
75 
76  std::vector<gr::tag_t>
77  _get_tags_in_range(unsigned int which_input, uint64_t abs_start, uint64_t abs_end);
78 
79  std::vector<gr::tag_t> _get_tags_in_range(unsigned int which_input,
80  uint64_t abs_start,
81  uint64_t abs_end,
82  const pmt::pmt_t& key);
83 
84  std::vector<gr::tag_t>
85  _get_tags_in_window(unsigned int which_input, uint64_t rel_start, uint64_t rel_end);
86 
87  std::vector<gr::tag_t> _get_tags_in_window(unsigned int which_input,
88  uint64_t rel_start,
89  uint64_t rel_end,
90  const pmt::pmt_t& key);
91 
92  [[nodiscard]] std::optional<gr::tag_t> _get_first_tag_in_range(
93  unsigned int which_input, uint64_t start, uint64_t end, const pmt::pmt_t& key);
94  [[nodiscard]] std::optional<gr::tag_t>
95  _get_first_tag_in_range(unsigned int which_input,
96  uint64_t start,
97  uint64_t end,
98  std::function<bool(const gr::tag_t&)>);
99 
100  /*******************************************************************
101  * Overloads for various scheduler-called functions
102  ******************************************************************/
103  void forecast(int noutput_items, gr_vector_int& ninput_items_required) override;
104 
105  int general_work(int noutput_items,
106  gr_vector_int& ninput_items,
107  gr_vector_const_void_star& input_items,
108  gr_vector_void_star& output_items) override;
109 
110  bool start() override;
111 
112  bool stop() override;
113 
114  void set_msg_handler_pybind(const pmt::pmt_t& which_port, std::string& handler_name);
115 
116  inline gr::logger_ptr _get_logger() const { return d_logger; }
117 
118 private:
119  py::handle d_py_handle;
120 
121  // Message handlers back into python using pybind API
122  using msg_handlers_pybind_t = std::map<pmt::pmt_t, std::string, pmt::comparator>;
123  msg_handlers_pybind_t d_msg_handlers_pybind;
124 
125  bool has_msg_handler(pmt::pmt_t which_port) override;
126 
127  void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg) override;
128 };
129 
130 #endif /* INCLUDED_BLOCK_GATEWAY_H */
enum { GW_BLOCK_GENERAL=0, GW_BLOCK_SYNC, GW_BLOCK_DECIM, GW_BLOCK_INTERP } gw_block_t
Definition: block_gateway.h:38
Definition: block_gateway.h:41
void set_msg_handler_pybind(const pmt::pmt_t &which_port, std::string &handler_name)
std::vector< gr::tag_t > _get_tags_in_window(unsigned int which_input, uint64_t rel_start, uint64_t rel_end, const pmt::pmt_t &key)
std::optional< gr::tag_t > _get_first_tag_in_range(unsigned int which_input, uint64_t start, uint64_t end, const pmt::pmt_t &key)
std::shared_ptr< block_gateway > sptr
Definition: block_gateway.h:49
std::vector< gr::tag_t > _get_tags_in_window(unsigned int which_input, uint64_t rel_start, uint64_t rel_end)
bool start() override
Called to enable drivers, etc for i/o devices.
gr::logger_ptr _get_logger() const
Definition: block_gateway.h:116
void forecast(int noutput_items, gr_vector_int &ninput_items_required) override
Estimate input requirements given output request.
std::optional< gr::tag_t > _get_first_tag_in_range(unsigned int which_input, uint64_t start, uint64_t end, std::function< bool(const gr::tag_t &)>)
bool stop() override
Called to disable drivers, etc for i/o devices.
block_gateway(const py::handle &py_handle, const std::string &name, gr::io_signature::sptr in_sig, gr::io_signature::sptr out_sig)
std::vector< gr::tag_t > _get_tags_in_range(unsigned int which_input, uint64_t abs_start, uint64_t abs_end, const pmt::pmt_t &key)
static sptr make(const py::object &py_handle, const std::string &name, gr::io_signature::sptr in_sig, gr::io_signature::sptr out_sig)
std::vector< gr::tag_t > _get_tags_in_range(unsigned int which_input, uint64_t abs_start, uint64_t abs_end)
void _add_item_tag(unsigned int which_output, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid)
void _add_item_tag(unsigned int which_output, const gr::tag_t &tag)
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) override
compute output items from input items
gr::logger_ptr d_logger
Definition: basic_block.h:96
std::string name() const
Definition: basic_block.h:168
The abstract base class for all 'terminal' processing blocks.
Definition: gnuradio-runtime/include/gnuradio/block.h:65
std::shared_ptr< io_signature > sptr
Definition: io_signature.h:52
GR_RUNTIME_API const pmt::pmt_t msg()
std::shared_ptr< logger > logger_ptr
Definition: logger.h:248
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:85
Definition: tags.h:28
std::vector< const void * > gr_vector_const_void_star
Definition: types.h:28
std::vector< void * > gr_vector_void_star
Definition: types.h:27
std::vector< int > gr_vector_int
Definition: types.h:23