JaiaBot  1.12.0+18+g85da5f82
JaiaBot micro-AUV software
xbee.h
Go to the documentation of this file.
1 // Copyright 2011-2021:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 // Ed Sanville <edsanville@gmail.com>
8 //
9 //
10 // This file is part of the Goby Underwater Autonomy Project Libraries
11 // ("The Goby Libraries").
12 //
13 // The Goby Libraries are free software: you can redistribute them and/or modify
14 // them under the terms of the GNU Lesser General Public License as published by
15 // the Free Software Foundation, either version 2.1 of the License, or
16 // (at your option) any later version.
17 //
18 // The Goby Libraries are distributed in the hope that they will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef XBEE_H
27 #define XBEE_H
28 
29 #include <boost/asio.hpp> // for serial_port
30 #include <map> // for map
31 #include <queue>
32 #include <string> // for string
33 
34 // basic file operations
35 #include <fstream>
36 #include <iostream>
37 
38 #include <functional>
39 
40 #include "xbee.pb.h"
41 
42 namespace jaiabot
43 {
44 namespace comms
45 {
46 typedef unsigned char byte;
47 
48 // SerialNumber is an immutable value that uniquely identifies an XBee modem
49 // The XBee can only address packets to serial numbers, not to the user-configurable NodeId
50 typedef uint64_t SerialNumber;
51 
52 // NodeId is a user-configurable id for an XBee modem, which corresponds to the modem_id from Goby
53 typedef std::string NodeId;
54 
55 // Packet structure for queuing up packets for transmit
56 struct Packet
57 {
59  std::string data;
60 
61  Packet() {}
62  Packet(const NodeId& dest, const std::string& data) : dest(dest), data(data) {}
63 };
64 
66 {
67  public:
69  void startup(const std::string& port_name, const int baud_rate, const NodeId& my_node_id,
70  const uint16_t network_id, const std::string& xbee_info_location,
71  const bool& use_encryption, const std::string& encryption_password);
72  void shutdown();
73 
74  std::vector<NodeId> get_peers();
75 
76  void send_packet(const NodeId& dest, const std::string& s);
77  void send_test_links(const NodeId& dest, const NodeId& com_dest);
78 
79  std::vector<std::string> get_packets();
80 
81  void do_work();
82 
83  static const NodeId broadcast;
84 
85  uint16_t max_payload_size;
86 
87  // Adding a peer to the lookup table
88  void add_peer(const NodeId node_id, const SerialNumber serial_number);
89 
90  // Get Diagnostics
92 
93  private:
94  static const SerialNumber broadcast_serial_number;
95 
96  std::shared_ptr<boost::asio::io_context> io;
97  boost::asio::serial_port* port;
98  NodeId my_node_id;
99  SerialNumber my_serial_number;
100  byte frame_id;
101  std::string glog_group;
102 
103  // Map of node_id onto serial number
104  std::map<NodeId, SerialNumber> node_id_to_serial_number_map;
105  std::map<SerialNumber, NodeId> serial_number_to_node_id_map;
106 
107  std::vector<std::string> received_packets;
108 
109  // Called during startup
110  void get_my_serial_number();
111  void get_maximum_payload_size();
112  void broadcast_node_id();
113 
114  // Packet sending
115  void send_packet(const SerialNumber& dest, const std::string& data);
116 
117  // Low level reads and writes
118  void write(const std::string& raw);
119  std::string read_until(const std::string& delimiter);
120  size_t bytes_available();
121  void read(void* ptr, const size_t n_bytes);
122  void async_read_with_timeout(std::string& buffer, const std::string& delimiter,
123  int timeout_seconds,
124  std::function<void(const std::string&)> handler);
125 
126  // Convert string to hex
127  std::string convertToHex(const std::string& str);
128 
129  // Command mode stuff
130  void enter_command_mode();
131  void assert_ok();
132  void exit_command_mode();
133 
134  // Frame stuff
135  std::string read_frame();
136 
137  // API mode stuff
138  SerialNumber read_frame_discover_node_response(const NodeId& node_id);
139  SerialNumber get_serial_number(const NodeId& node_id);
140  std::string api_transmit_request(const SerialNumber& dest, const byte frame_id, const byte* ptr,
141  const size_t length);
142  std::string api_explicit_transmit_request(const SerialNumber& dest,
143  const SerialNumber& com_dest, const byte frame_id);
144  // Processing frames
145  void process_frame();
146  void process_frame_if_available();
147  void process_frame_extended_transmit_status(const std::string& response_string);
148  void process_frame_at_command_response(const std::string& response_string);
149  void process_frame_receive_packet(const std::string& response_string);
150  void process_frame_node_identification_indicator(const std::string& response_string);
151  void process_frame_explicit_rx_indicator(const std::string& response_string);
152 
153  // Query RSSI from Radio
154  void query_rssi();
155  // Query Received Error Count
156  void query_er();
157  // Query Received Good Count
158  void query_gd();
159  // Query Bytes Transmitted
160  void query_bc();
161  // Query Transmission Failure Count
162  void query_tr();
163 
164  // Check if we received diagnostics
165  bool received_rssi_{false};
166  bool received_er_{false};
167  bool received_gd_{false};
168  bool received_bc_{false};
169  bool received_tr_{false};
170 
171  // RSSI fields
172  uint16_t current_rssi_{0};
173  uint16_t history_rssi_{0};
174  int rssi_query_count_{1};
175  uint16_t max_rssi_{0};
176  uint16_t min_rssi_{150};
177  uint16_t average_rssi_{0};
178 
179  // Bytes Transmitted
180  uint32_t bytes_transmitted_{0};
181 
182  // Received Error Count
183  uint16_t received_error_count_{0};
184 
185  // Received Good Count
186  uint16_t received_good_count_{0};
187 
188  // Transmission Failure Count
189  uint16_t transmission_failure_count_{0};
190 
191  std::string my_xbee_info_location_{""};
192 };
193 } // namespace comms
194 } // namespace jaiabot
195 
196 #endif
std::vector< NodeId > get_peers()
void add_peer(const NodeId node_id, const SerialNumber serial_number)
static const NodeId broadcast
Definition: xbee.h:83
uint16_t max_payload_size
Definition: xbee.h:85
void send_packet(const NodeId &dest, const std::string &s)
std::vector< std::string > get_packets()
void startup(const std::string &port_name, const int baud_rate, const NodeId &my_node_id, const uint16_t network_id, const std::string &xbee_info_location, const bool &use_encryption, const std::string &encryption_password)
void send_test_links(const NodeId &dest, const NodeId &com_dest)
uint64_t SerialNumber
Definition: xbee.h:50
unsigned char byte
Definition: xbee.h:46
std::string NodeId
Definition: xbee.h:53
Packet(const NodeId &dest, const std::string &data)
Definition: xbee.h:62
std::string data
Definition: xbee.h:59