JaiaBot 2.0.0
JaiaBot micro-AUV software
 
Loading...
Searching...
No Matches
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
45extern "C"
46{
47 const char* goby_driver_name();
48 goby::acomms::ModemDriverBase* goby_make_driver();
49}
50
51namespace boost
52{
53namespace system
54{
55class error_code;
56} // namespace system
57} // namespace boost
58
59namespace goby
60{
61namespace acomms
62{
63namespace protobuf
64{
65class ModemTransmission;
66} // namespace protobuf
67} // namespace acomms
68} // namespace goby
69
70namespace jaiabot
71{
72namespace comms
73{
74class UDPDriver : public goby::acomms::ModemDriverBase
75{
76 public:
78 ~UDPDriver() override;
79
80 void startup(const goby::acomms::protobuf::DriverConfig& cfg) override;
81 void shutdown() override;
82 void do_work() override;
83 void handle_initiate_transmission(const goby::acomms::protobuf::ModemTransmission& m) override;
84
85 void report(goby::acomms::protobuf::ModemReport* report) override;
86
87 private:
88 void update_remote(const jaiabot::udp::protobuf::Config::EndPoint& remote,
89 bool clear_existing = false);
90
91 void start_send(const goby::acomms::protobuf::ModemTransmission& msg);
92 void send_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
93 void start_receive();
94 void receive_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
95 void receive_message(const goby::acomms::protobuf::ModemTransmission& m);
96
97 const jaiabot::udp::protobuf::Config& config_extension()
98 {
99 return driver_cfg_.GetExtension(jaiabot::udp::protobuf::config);
100 }
101
102 // for multihub
103 void update_active_hub(int hub_id, goby::acomms::protobuf::ModemTransmission* msg);
104 void set_active_hub_peer(int hub_id);
105
106 private:
107 goby::acomms::protobuf::DriverConfig driver_cfg_;
108 boost::asio::io_context io_context_;
109 std::unique_ptr<boost::asio::ip::udp::socket> socket_;
110 // modem id to endpoint
111 std::multimap<int, boost::asio::ip::udp::endpoint> receivers_;
112 boost::asio::ip::udp::endpoint sender_;
113
114 // (16 bit length = 65535 - 8 byte UDP header - 20 byte IP
115 static constexpr size_t UDP_MAX_PACKET_SIZE = 65507;
116
117 std::array<char, UDP_MAX_PACKET_SIZE> receive_buffer_;
118
119 // ids we are providing acks for, normally just our modem_id()
120 std::set<unsigned> application_ack_ids_;
121
122 std::uint32_t next_frame_{0};
123
124 bool have_active_hub_{false};
125 int active_hub_id_{-1};
126 int hub_wifi_base_modem_id_{-1};
127 int hub_wifi_modem_id_{-1};
128 // maps hub ID to endpoint
129 std::map<int, jaiabot::udp::protobuf::Config::EndPoint> hub_endpoints_;
130};
131} // namespace comms
132} // namespace jaiabot
133#endif
void do_work() override
void report(goby::acomms::protobuf::ModemReport *report) override
void startup(const goby::acomms::protobuf::DriverConfig &cfg) override
void shutdown() override
void handle_initiate_transmission(const goby::acomms::protobuf::ModemTransmission &m) override
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::jaiabot::udp::protobuf::Config >, 11, false > config
const char * goby_driver_name()
goby::acomms::ModemDriverBase * goby_make_driver()