JaiaBot  1.19.0
JaiaBot micro-AUV software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
31 
32 namespace jaiabot
33 {
34 namespace comms
35 {
36 constexpr int broadcast_modem_id{goby::acomms::BROADCAST_ID};
37 constexpr int hub_base_modem_id{1};
38 constexpr int bot0_base_modem_id{2};
39 
40 constexpr int bot_id_min{0};
41 constexpr int bot_id_max{150};
42 constexpr int bot_id_total{bot_id_max - bot_id_min + 1};
43 
44 constexpr int hub_id_min{0};
45 constexpr int hub_id_max{30};
46 constexpr int hub_id_total{hub_id_max - hub_id_min + 1};
47 
48 inline void check_bot_id_bounds(int bot_id)
49 {
50  if (bot_id < bot_id_min)
51  throw(jaiabot::Exception("Bot ID " + std::to_string(bot_id) +
52  " is less than Bot ID minimum"));
53 
54  if (bot_id > bot_id_max)
55  throw(jaiabot::Exception("Bot ID " + std::to_string(bot_id) +
56  " is greater than Bot ID maximum"));
57 }
58 
59 inline void check_hub_id_bounds(int hub_id)
60 {
61  if (hub_id < hub_id_min)
62  throw(jaiabot::Exception("Hub ID " + std::to_string(hub_id) +
63  " is less than Hub ID minimum"));
64 
65  if (hub_id > hub_id_max)
66  throw(jaiabot::Exception("Hub ID " + std::to_string(hub_id) +
67  " is greater than Hub ID maximum"));
68 }
69 
70 inline int hub_modem_id(unsigned subnet_mask, jaiabot::protobuf::Link link)
71 {
72  unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
73  return static_cast<int>(link) * num_modems_in_subnet + hub_base_modem_id;
74 }
75 
76 inline int modem_id_from_bot_id(int bot_id, unsigned subnet_mask, jaiabot::protobuf::Link link)
77 {
78  check_bot_id_bounds(bot_id);
79  unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
80  return static_cast<int>(link) * num_modems_in_subnet + bot_id + bot0_base_modem_id;
81 }
82 
83 inline int bot_id_from_modem_id(int modem_id, unsigned subnet_mask)
84 {
85  int base_modem_id = modem_id & (~subnet_mask & 0xFFFF);
86  int bot_id = base_modem_id - bot0_base_modem_id;
87  check_bot_id_bounds(bot_id);
88  return bot_id;
89 }
90 
91 inline jaiabot::protobuf::Link link_from_modem_id(int modem_id, unsigned subnet_mask)
92 {
94  unsigned num_modems_in_subnet = (0xFFFF ^ subnet_mask) + 1;
95  int link_id = (subnet_mask & modem_id) >> goby::util::ceil_log2(num_modems_in_subnet);
97  link = static_cast<jaiabot::protobuf::Link>(link_id);
98  return link;
99 }
100 
101 template <typename DCCLMessageWithLinkField>
102 void set_link_type(DCCLMessageWithLinkField& msg, int src_modem_id, unsigned subnet_mask)
103 {
104  msg.set_link(link_from_modem_id(src_modem_id, subnet_mask));
105 }
106 
107 } // namespace comms
108 } // namespace jaiabot
109 
110 #endif
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::jaia::MessageOptions >, 11, false > msg
constexpr int broadcast_modem_id
Definition: comms.h:36
int modem_id_from_bot_id(int bot_id, unsigned subnet_mask, jaiabot::protobuf::Link link)
Definition: comms.h:76
int hub_modem_id(unsigned subnet_mask, jaiabot::protobuf::Link link)
Definition: comms.h:70
constexpr int hub_id_total
Definition: comms.h:46
void check_hub_id_bounds(int hub_id)
Definition: comms.h:59
constexpr int hub_base_modem_id
Definition: comms.h:37
constexpr int bot_id_total
Definition: comms.h:42
jaiabot::protobuf::Link link_from_modem_id(int modem_id, unsigned subnet_mask)
Definition: comms.h:91
constexpr int hub_id_min
Definition: comms.h:44
void check_bot_id_bounds(int bot_id)
Definition: comms.h:48
constexpr int hub_id_max
Definition: comms.h:45
constexpr int bot0_base_modem_id
Definition: comms.h:38
constexpr int bot_id_max
Definition: comms.h:41
int bot_id_from_modem_id(int modem_id, unsigned subnet_mask)
Definition: comms.h:83
constexpr int bot_id_min
Definition: comms.h:40
void set_link_type(DCCLMessageWithLinkField &msg, int src_modem_id, unsigned subnet_mask)
Definition: comms.h:102
bool Link_IsValid(int value)