JaiaBot 2.1.1
JaiaBot micro-AUV software
 
Loading...
Searching...
No Matches
comms.h
Go to the documentation of this file.
1// Copyright 2023:
2// JaiaRobotics LLC
3// File authors:
4// Toby Schneider <toby@gobysoft.org>
5//
6//
7// This file is part of the JaiaBot Project Libraries
8// ("The Jaia Libraries").
9//
10// The Jaia Libraries are free software: you can redistribute them and/or modify
11// them under the terms of the GNU Lesser General Public License as published by
12// the Free Software Foundation, either version 2.1 of the License, or
13// (at your option) any later version.
14//
15// The Jaia Libraries are distributed in the hope that they will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public License
21// along with the Jaia Libraries. If not, see <http://www.gnu.org/licenses/>.
22
23#ifndef JAIABOT_SRC_LIB_COMMS_COMMS_H
24#define JAIABOT_SRC_LIB_COMMS_COMMS_H
25
26#include <goby/acomms/acomms_constants.h>
27#include <goby/util/sci.h>
28
29#include "jaiabot/exception.h"
32
33namespace jaiabot
34{
35namespace comms
36{
37constexpr int broadcast_modem_id{goby::acomms::BROADCAST_ID};
38constexpr int hub_base_modem_id{1};
39constexpr int bot0_base_modem_id{2};
40
41constexpr int bot_id_min{0};
42constexpr int bot_id_max{150};
43constexpr int bot_id_total{bot_id_max - bot_id_min + 1};
44
45constexpr int hub_id_min{0};
46constexpr int hub_id_max{30};
47constexpr int hub_id_total{hub_id_max - hub_id_min + 1};
48
49// All bots use the same hub ID for bot-hub comms, regardless of the actual
50// Hub ID - this allows multicast-style comms to the hub(s).
51constexpr int default_hub_id{1};
52
53inline void check_bot_id_bounds(int bot_id)
54{
55 if (bot_id < bot_id_min)
56 throw(jaiabot::Exception("Bot ID " + std::to_string(bot_id) +
57 " is less than Bot ID minimum"));
58
59 if (bot_id > bot_id_max)
60 throw(jaiabot::Exception("Bot ID " + std::to_string(bot_id) +
61 " is greater than Bot ID maximum"));
62}
63
64inline void check_hub_id_bounds(int hub_id)
65{
66 if (hub_id < hub_id_min)
67 throw(jaiabot::Exception("Hub ID " + std::to_string(hub_id) +
68 " is less than Hub ID minimum"));
69
70 if (hub_id > hub_id_max)
71 throw(jaiabot::Exception("Hub ID " + std::to_string(hub_id) +
72 " is greater than Hub ID maximum"));
73}
74
75inline int hub_modem_id(unsigned subnet_mask, jaiabot::protobuf::Link link, int hub_id = -1)
76{
77 unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
79 {
80 if (hub_id == -1)
81 throw(jaiabot::Exception("Hub ID must be set for LINK_HUB2HUB"));
82
83 return hub_id + 1 + static_cast<int>(link) * num_modems_in_subnet;
84 }
85 else
86 {
87 return static_cast<int>(link) * num_modems_in_subnet + hub_base_modem_id;
88 }
89}
90
91inline int modem_id_from_bot_id(int bot_id, unsigned subnet_mask, jaiabot::protobuf::Link link)
92{
93 check_bot_id_bounds(bot_id);
94 unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
95 return static_cast<int>(link) * num_modems_in_subnet + bot_id + bot0_base_modem_id;
96}
97
98inline int bot_id_from_modem_id(int modem_id, unsigned subnet_mask)
99{
100 int base_modem_id = modem_id & (~subnet_mask & 0xFFFF);
101 int bot_id = base_modem_id - bot0_base_modem_id;
102 check_bot_id_bounds(bot_id);
103 return bot_id;
104}
105
106inline jaiabot::protobuf::Link link_from_modem_id(int modem_id, unsigned subnet_mask)
107{
109 unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
110 int link_id = (subnet_mask & modem_id) >> goby::util::ceil_log2(num_modems_in_subnet);
112 link = static_cast<jaiabot::protobuf::Link>(link_id);
113 return link;
114}
115
116template <typename DCCLMessageWithLinkField>
117void set_link_type(DCCLMessageWithLinkField& msg, int src_modem_id, unsigned subnet_mask)
118{
119 msg.set_link(link_from_modem_id(src_modem_id, subnet_mask));
120}
121
122inline goby::acomms::protobuf::DynamicBufferConfig
125{
126 auto buffer = link_aware_buffer.buffer_base();
127 for (const auto& override_buffer : link_aware_buffer.buffer_override())
128 {
129 if (override_buffer.link() == link)
130 buffer.MergeFrom(override_buffer.buffer());
131 }
132 return buffer;
133}
134
135} // namespace comms
136} // namespace jaiabot
137
138#endif
constexpr int broadcast_modem_id
Definition comms.h:37
int modem_id_from_bot_id(int bot_id, unsigned subnet_mask, jaiabot::protobuf::Link link)
Definition comms.h:91
constexpr int hub_id_total
Definition comms.h:47
void check_hub_id_bounds(int hub_id)
Definition comms.h:64
constexpr int hub_base_modem_id
Definition comms.h:38
constexpr int bot_id_total
Definition comms.h:43
jaiabot::protobuf::Link link_from_modem_id(int modem_id, unsigned subnet_mask)
Definition comms.h:106
constexpr int hub_id_min
Definition comms.h:45
void check_bot_id_bounds(int bot_id)
Definition comms.h:53
goby::acomms::protobuf::DynamicBufferConfig buffer_for_link(const jaiabot::protobuf::LinkAwareBufferConfig &link_aware_buffer, jaiabot::protobuf::Link link)
Definition comms.h:123
constexpr int hub_id_max
Definition comms.h:46
constexpr int bot0_base_modem_id
Definition comms.h:39
constexpr int default_hub_id
Definition comms.h:51
constexpr int bot_id_max
Definition comms.h:42
int bot_id_from_modem_id(int modem_id, unsigned subnet_mask)
Definition comms.h:98
constexpr int bot_id_min
Definition comms.h:41
int hub_modem_id(unsigned subnet_mask, jaiabot::protobuf::Link link, int hub_id=-1)
Definition comms.h:75
void set_link_type(DCCLMessageWithLinkField &msg, int src_modem_id, unsigned subnet_mask)
Definition comms.h:117
bool Link_IsValid(int value)