JaiaBot  1.19.0
JaiaBot micro-AUV software
udp_driver.h
Go to the documentation of this file.
1 // This file was forked from Goby on Feb 26, 2025
2 // Original copyright follows:
3 //
4 // Copyright 2011-2023:
5 // GobySoft, LLC (2013-)
6 // Massachusetts Institute of Technology (2007-2014)
7 // Community contributors (see AUTHORS file)
8 // File authors:
9 // Toby Schneider <toby@gobysoft.org>
10 //
11 //
12 // This file is part of the Goby Underwater Autonomy Project Libraries
13 // ("The Goby Libraries").
14 //
15 // The Goby Libraries are free software: you can redistribute them and/or modify
16 // them under the terms of the GNU Lesser General Public License as published by
17 // the Free Software Foundation, either version 2.1 of the License, or
18 // (at your option) any later version.
19 //
20 // The Goby Libraries are distributed in the hope that they will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU Lesser General Public License for more details.
24 //
25 // You should have received a copy of the GNU Lesser General Public License
26 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
27 
28 #ifndef GOBY_ACOMMS_MODEMDRIVER_UDP_DRIVER_H
29 #define GOBY_ACOMMS_MODEMDRIVER_UDP_DRIVER_H
30 
31 #include <array> // for array
32 #include <cstddef> // for size_t
33 #include <cstdint> // for uint32_t
34 #include <map> // for multimap
35 #include <memory> // for unique_ptr
36 #include <set> // for set
37 
38 #include <boost/asio/ip/udp.hpp> // for udp, udp::endpoint
39 
40 #include "goby/acomms/modemdriver/driver_base.h" // for ModemDriverBase
41 #include "goby/acomms/protobuf/driver_base.pb.h" // for DriverConfig
42 #include "goby/util/asio_compat.h" // for io_context
44 
45 extern "C"
46 {
47  const char* goby_driver_name();
48  goby::acomms::ModemDriverBase* goby_make_driver();
49 }
50 
51 namespace boost
52 {
53 namespace system
54 {
55 class error_code;
56 } // namespace system
57 } // namespace boost
58 
59 namespace goby
60 {
61 namespace acomms
62 {
63 namespace protobuf
64 {
65 class ModemTransmission;
66 } // namespace protobuf
67 } // namespace acomms
68 } // namespace goby
69 
70 namespace jaiabot
71 {
72 namespace comms
73 {
74 
75 class UDPDriver : public goby::acomms::ModemDriverBase
76 {
77  public:
78  UDPDriver();
79  ~UDPDriver() override;
80 
81  void startup(const goby::acomms::protobuf::DriverConfig& cfg) override;
82  void shutdown() override;
83  void do_work() override;
84  void handle_initiate_transmission(const goby::acomms::protobuf::ModemTransmission& m) override;
85 
86  void report(goby::acomms::protobuf::ModemReport* report) override;
87 
88  private:
89  void update_remote(const jaiabot::udp::protobuf::Config::EndPoint& remote,
90  bool clear_existing = false);
91 
92  void start_send(const goby::acomms::protobuf::ModemTransmission& msg);
93  void send_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
94  void start_receive();
95  void receive_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
96  void receive_message(const goby::acomms::protobuf::ModemTransmission& m);
97 
98  const jaiabot::udp::protobuf::Config& config_extension()
99  {
100  return driver_cfg_.GetExtension(jaiabot::udp::protobuf::config);
101  }
102 
103  // for multihub
104  void update_active_hub(int hub_id, goby::acomms::protobuf::ModemTransmission* msg);
105  void set_active_hub_peer(int hub_id);
106 
107  private:
108  goby::acomms::protobuf::DriverConfig driver_cfg_;
109  boost::asio::io_context io_context_;
110  std::unique_ptr<boost::asio::ip::udp::socket> socket_;
111  // modem id to endpoint
112  std::multimap<int, boost::asio::ip::udp::endpoint> receivers_;
113  boost::asio::ip::udp::endpoint sender_;
114 
115  // (16 bit length = 65535 - 8 byte UDP header - 20 byte IP
116  static constexpr size_t UDP_MAX_PACKET_SIZE = 65507;
117 
118  std::array<char, UDP_MAX_PACKET_SIZE> receive_buffer_;
119 
120  // ids we are providing acks for, normally just our modem_id()
121  std::set<unsigned> application_ack_ids_;
122 
123  std::uint32_t next_frame_{0};
124 
125  bool have_active_hub_{false};
126  int active_hub_id_{-1};
127  int hub_wifi_base_modem_id_{-1};
128  int hub_wifi_modem_id_{-1};
129  // maps hub ID to endpoint
130  std::map<int, jaiabot::udp::protobuf::Config::EndPoint> hub_endpoints_;
131 };
132 } // namespace comms
133 } // namespace jaiabot
134 #endif
jaiabot::comms::UDPDriver::do_work
void do_work() override
goby
Definition: udp_driver.h:59
jaiabot::udp::protobuf::Config
Definition: udp_driver.pb.h:359
jaiabot::comms::UDPDriver::UDPDriver
UDPDriver()
boost
Definition: udp_driver.h:51
jaiabot::udp::protobuf::config
extern ::google::protobuf::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::google::protobuf::internal::MessageTypeTraits< ::jaiabot::udp::protobuf::Config >, 11, false > config
Definition: udp_driver.pb.h:566
jaiabot::comms::UDPDriver::~UDPDriver
~UDPDriver() override
goby_driver_name
const char * goby_driver_name()
jaiabot::comms::UDPDriver
Definition: udp_driver.h:75
jaiabot::comms::UDPDriver::shutdown
void shutdown() override
jaiabot
Definition: config.pb.h:56
jaiabot::comms::UDPDriver::handle_initiate_transmission
void handle_initiate_transmission(const goby::acomms::protobuf::ModemTransmission &m) override
jaiabot::comms::UDPDriver::startup
void startup(const goby::acomms::protobuf::DriverConfig &cfg) override
jaia::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::jaia::MessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:796
jaiabot::comms::UDPDriver::report
void report(goby::acomms::protobuf::ModemReport *report) override
udp_driver.pb.h
jaiabot::udp::protobuf::Config_EndPoint
Definition: udp_driver.pb.h:82
goby_make_driver
goby::acomms::ModemDriverBase * goby_make_driver()