-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_impl.cpp
467 lines (399 loc) · 14.1 KB
/
system_impl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "system.h"
#include "global_include.h"
#include "mavsdk_impl.h"
#include "mavlink_include.h"
#include "mavlink_inspection_transfer_ground_station.h"
#include "mavlink_inspection_transfer_robotic_vehicle.h"
#include "mavlink_checklist_transfer_robotic_vehicle.h"
#include "mavlink_checklist_transfer_ground_station.h"
#include "mavlink_alarm_transfer_robotic_vehicle.h"
#include "mavlink_alarm_transfer_ground_station.h"
#include "mavlink_hl_action_transfer_robotic_vehicle.h"
#include "mavlink_hl_action_transfer_ground_station.h"
#include "mavlink_command_transfer_robotic_vehicle.h"
#include "mavlink_command_transfer_ground_station.h"
#include "system_impl.h"
#include "plugin_impl_base.h"
#include "log.h"
#include <cstdlib>
#include <functional>
#include <algorithm>
#include <future>
#include <cassert>
// Set to 1 to log incoming/outgoing mavlink messages.
#define MESSAGE_DEBUGGING 0
namespace mavsdk {
using namespace std::placeholders; // for `_1`
SystemImpl::SystemImpl(MavsdkImpl& parent, uint8_t system_id, uint8_t comp_id, bool connected) :
Sender(parent.own_address, _target_address),
_parent(parent),
_timesync(*this),
_ping(*this)
{
switch (_parent.get_own_usage_type()) {
case Mavsdk::Configuration::UsageType::GroundStation:
_command_transfer = std::make_shared<MAVLinkCommandTransferGroundStation>(
*this, _message_handler, _parent.timeout_handler);
_inspection_transfer = std::make_shared<MAVLinkInspectionTransferGroundStation>(
*this, _message_handler, _parent.timeout_handler);
_checklist_transfer = std::make_shared<MAVLinkChecklistTransferGroundStation>(
*this, _message_handler, _parent.timeout_handler);
_alarm_transfer = std::make_shared<MAVLinkAlarmTransferGroundStation>(
*this, _message_handler, _parent.timeout_handler);
_hl_action_transfer = std::make_shared<MAVLinkHLActionTransferGroundStation>(
*this, _message_handler, _parent.timeout_handler);
break;
default:
_command_transfer = std::make_shared<MAVLinkCommandTransferRoboticVehicle>(
*this, _message_handler, _parent.timeout_handler);
_inspection_transfer = std::make_shared<MAVLinkInspectionTransferRoboticVehicle>(
*this, _message_handler, _parent.timeout_handler);
_checklist_transfer = std::make_shared<MAVLinkChecklistTransferRoboticVehicle>(
*this, _message_handler, _parent.timeout_handler);
_alarm_transfer = std::make_shared<MAVLinkAlarmTransferRoboticVehicle>(
*this, _message_handler, _parent.timeout_handler);
_hl_action_transfer = std::make_shared<MAVLinkHLActionTransferRoboticVehicle>(
*this, _message_handler, _parent.timeout_handler);
break;
}
_target_address.system_id = system_id;
_target_address.component_id = comp_id;
if (connected) {
_always_connected = true;
set_connected(system_id);
}
_system_thread = new std::thread(&SystemImpl::system_thread, this);
_message_handler.register_one(
MAVLINK_MSG_ID_HEARTBEAT, std::bind(&SystemImpl::process_heartbeat, this, _1), this);
add_new_component(comp_id);
}
SystemImpl::~SystemImpl()
{
_should_exit = true;
_message_handler.unregister_all(this);
if (!_always_connected) {
unregister_timeout_handler(_heartbeat_timeout_cookie);
}
if (_system_thread != nullptr) {
_system_thread->join();
delete _system_thread;
_system_thread = nullptr;
}
}
bool SystemImpl::is_connected() const
{
return _connected;
}
void SystemImpl::register_mavlink_message_handler(
uint16_t msg_id, mavlink_message_handler_t callback, const void* cookie)
{
_message_handler.register_one(msg_id, callback, cookie);
}
void SystemImpl::unregister_mavlink_message_handler(uint16_t msg_id, const void* cookie)
{
_message_handler.unregister_one(msg_id, cookie);
}
void SystemImpl::unregister_all_mavlink_message_handlers(const void* cookie)
{
_message_handler.unregister_all(cookie);
}
void SystemImpl::register_timeout_handler(
const std::function<void()>& callback, double duration_s, void** cookie)
{
_parent.timeout_handler.add(callback, duration_s, cookie);
}
void SystemImpl::refresh_timeout_handler(const void* cookie)
{
_parent.timeout_handler.refresh(cookie);
}
void SystemImpl::unregister_timeout_handler(const void* cookie)
{
_parent.timeout_handler.remove(cookie);
}
void SystemImpl::enable_timesync()
{
_timesync.enable();
}
void SystemImpl::subscribe_is_connected(System::IsConnectedCallback callback)
{
std::lock_guard<std::mutex> lock(_connection_mutex);
_is_connected_callback = callback;
}
void SystemImpl::process_mavlink_message(mavlink_message_t& message)
{
// This is a low level interface where incoming messages can be tampered
// with or even dropped.
if (_incoming_messages_intercept_callback) {
const bool keep = _incoming_messages_intercept_callback(message);
if (!keep) {
LogDebug() << "Dropped incoming message: " << int(message.msgid);
return;
}
}
_message_handler.process_message(message);
}
void SystemImpl::add_call_every(std::function<void()> callback, float interval_s, void** cookie)
{
_parent.call_every_handler.add(callback, interval_s, cookie);
}
void SystemImpl::change_call_every(float interval_s, const void* cookie)
{
_parent.call_every_handler.change(interval_s, cookie);
}
void SystemImpl::reset_call_every(const void* cookie)
{
_parent.call_every_handler.reset(cookie);
}
void SystemImpl::remove_call_every(const void* cookie)
{
_parent.call_every_handler.remove(cookie);
}
void SystemImpl::process_heartbeat(const mavlink_message_t& message)
{
mavlink_heartbeat_t heartbeat;
mavlink_msg_heartbeat_decode(&message, &heartbeat);
set_connected(message.sysid);
}
void SystemImpl::heartbeats_timed_out()
{
LogInfo() << "heartbeats timed out";
set_disconnected(1);
}
void SystemImpl::system_thread()
{
dl_time_t last_ping_time{};
while (!_should_exit) {
_timesync.do_work();
if (_command_transfer)
_command_transfer->do_work();
if (_inspection_transfer)
_inspection_transfer->do_work();
if (_checklist_transfer)
_checklist_transfer->do_work();
if (_alarm_transfer)
_alarm_transfer->do_work();
if (_hl_action_transfer)
_hl_action_transfer->do_work();
if (_time.elapsed_since_s(last_ping_time) >= SystemImpl::_ping_interval_s) {
if (_connected) {
_ping.run_once();
}
last_ping_time = _time.steady_time();
}
if (_connected) {
// Work fairly fast if we're connected.
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} else {
// Be less aggressive when unconnected.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
}
std::string SystemImpl::component_name(uint8_t component_id)
{
switch (component_id) {
case MAV_COMP_ID_AUTOPILOT1:
return "Autopilot";
case MAV_COMP_ID_CAMERA:
return "Camera 1";
case MAV_COMP_ID_CAMERA2:
return "Camera 2";
case MAV_COMP_ID_CAMERA3:
return "Camera 3";
case MAV_COMP_ID_CAMERA4:
return "Camera 4";
case MAV_COMP_ID_CAMERA5:
return "Camera 5";
case MAV_COMP_ID_CAMERA6:
return "Camera 6";
case MAV_COMP_ID_GIMBAL:
return "Gimbal";
default:
return "Unknown component";
}
}
ComponentType SystemImpl::component_type(uint8_t component_id)
{
switch (component_id) {
case MAV_COMP_ID_AUTOPILOT1:
return ComponentType::AUTOPILOT;
case MAV_COMP_ID_CAMERA:
case MAV_COMP_ID_CAMERA2:
case MAV_COMP_ID_CAMERA3:
case MAV_COMP_ID_CAMERA4:
case MAV_COMP_ID_CAMERA5:
case MAV_COMP_ID_CAMERA6:
return ComponentType::CAMERA;
case MAV_COMP_ID_GIMBAL:
return ComponentType::GIMBAL;
default:
return ComponentType::UNKNOWN;
}
}
void SystemImpl::add_new_component(uint8_t component_id)
{
if (component_id == 0) {
return;
}
auto res_pair = _components.insert(component_id);
if (res_pair.second) {
std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
if (_component_discovered_callback != nullptr) {
const ComponentType type = component_type(component_id);
auto temp_callback = _component_discovered_callback;
call_user_callback([temp_callback, type]() { temp_callback(type); });
}
LogDebug() << "Component " << component_name(component_id) << " (" << int(component_id)
<< ") added.";
}
}
size_t SystemImpl::total_components() const
{
return _components.size();
}
void SystemImpl::register_component_discovered_callback(discover_callback_t callback)
{
std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
_component_discovered_callback = callback;
if (total_components() > 0) {
for (const auto& elem : _components) {
const ComponentType type = component_type(elem);
if (_component_discovered_callback) {
auto temp_callback = _component_discovered_callback;
call_user_callback([temp_callback, type]() { temp_callback(type); });
}
}
}
}
bool SystemImpl::send_message(mavlink_message_t& message)
{
// This is a low level interface where incoming messages can be tampered
// with or even dropped.
if (_outgoing_messages_intercept_callback) {
const bool keep = _outgoing_messages_intercept_callback(message);
if (!keep) {
// We fake that everything was sent as instructed because
// a potential loss would happen later and we would not be informed
// about it.
LogDebug() << "Dropped outgoing message: " << int(message.msgid);
return true;
}
}
#if MESSAGE_DEBUGGING == 1
LogDebug() << "Sending msg " << size_t(message.msgid);
#endif
return _parent.send_message(message);
}
void SystemImpl::set_connected(uint8_t sysid)
{
bool enable_needed = false;
{
std::lock_guard<std::mutex> lock(_connection_mutex);
if (!_connected) {
LogDebug() << "Discovered " << _components.size() << " component(s)";
_connected = true;
_parent.notify_on_discover(sysid); //_uuid
// Send a heartbeat back immediately.
_parent.start_sending_heartbeat();
if (!_always_connected) {
register_timeout_handler(
std::bind(&SystemImpl::heartbeats_timed_out, this),
_HEARTBEAT_TIMEOUT_S,
&_heartbeat_timeout_cookie);
}
enable_needed = true;
if (_is_connected_callback) {
const auto temp_callback = _is_connected_callback;
_parent.call_user_callback([temp_callback]() { temp_callback(true); });
}
} else if (_connected && !_always_connected) {
refresh_timeout_handler(_heartbeat_timeout_cookie);
}
// If not yet connected there is nothing to do/
}
if (enable_needed) {
std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
for (auto plugin_impl : _plugin_impls) {
plugin_impl->enable();
}
}
}
void SystemImpl::set_disconnected(uint8_t sysid)
{
{
std::lock_guard<std::mutex> lock(_connection_mutex);
// This might not be needed because this is probably called from the triggered
// timeout anyway but it should also do no harm.
// unregister_timeout_handler(_heartbeat_timeout_cookie);
//_heartbeat_timeout_cookie = nullptr;
_connected = false;
_parent.notify_on_timeout(sysid);
if (_is_connected_callback) {
const auto temp_callback = _is_connected_callback;
_parent.call_user_callback([temp_callback]() { temp_callback(false); });
}
}
{
std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
for (auto plugin_impl : _plugin_impls) {
plugin_impl->disable();
}
}
}
uint8_t SystemImpl::get_target_system_id() const
{
return _target_address.system_id;
}
uint8_t SystemImpl::get_target_component_id() const
{
return _target_address.component_id;
}
uint8_t SystemImpl::get_own_system_id() const
{
return _parent.get_own_system_id();
}
uint8_t SystemImpl::get_own_component_id() const
{
return _parent.get_own_component_id();
}
void SystemImpl::register_plugin(PluginImplBase* plugin_impl)
{
assert(plugin_impl);
plugin_impl->init();
{
std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
_plugin_impls.push_back(plugin_impl);
}
// If we're connected already, let's enable it straightaway.
if (_connected) {
plugin_impl->enable();
}
}
void SystemImpl::unregister_plugin(PluginImplBase* plugin_impl)
{
assert(plugin_impl);
plugin_impl->disable();
plugin_impl->deinit();
// Remove first, so it won't get enabled/disabled anymore.
{
std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
auto found = std::find(_plugin_impls.begin(), _plugin_impls.end(), plugin_impl);
if (found != _plugin_impls.end()) {
_plugin_impls.erase(found);
}
}
}
void SystemImpl::call_user_callback_located(
const std::string& filename, const int linenumber, const std::function<void()>& func)
{
_parent.call_user_callback_located(filename, linenumber, func);
}
void SystemImpl::intercept_incoming_messages(std::function<bool(mavlink_message_t&)> callback)
{
_incoming_messages_intercept_callback = callback;
}
void SystemImpl::intercept_outgoing_messages(std::function<bool(mavlink_message_t&)> callback)
{
_outgoing_messages_intercept_callback = callback;
}
} // namespace mavsdk