• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 4309225683

pending completion
4309225683

push

github

GitHub
Merge pull request #1995 from dvinc/offboard-ardupilot-rover

7 of 7 new or added lines in 3 files covered. (100.0%)

7418 of 24192 relevant lines covered (30.66%)

21.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.68
/src/mavsdk/core/system_impl.cpp
1
#include "system.h"
2
#include "mavsdk_impl.h"
3
#include "mavlink_include.h"
4
#include "system_impl.h"
5
#include "plugin_impl_base.h"
6
#include "px4_custom_mode.h"
7
#include "ardupilot_custom_mode.h"
8
#include "request_message.h"
9
#include "callback_list.tpp"
10
#include <cstdlib>
11
#include <functional>
12
#include <future>
13
#include <utility>
14

15
namespace mavsdk {
16

17
template class CallbackList<bool>;
18
template class CallbackList<System::ComponentType>;
19
template class CallbackList<System::ComponentType, uint8_t>;
20

21
SystemImpl::SystemImpl(MavsdkImpl& parent) :
14✔
22
    Sender(),
23
    _parent(parent),
24
    _params(
25
        *this,
26
        _parent.mavlink_message_handler,
14✔
27
        _parent.timeout_handler,
14✔
28
        [this]() { return timeout_s(); },
209✔
29
        false),
30
    _command_sender(*this),
31
    _timesync(*this),
32
    _ping(*this),
33
    _mission_transfer(
34
        *this,
35
        _parent.mavlink_message_handler,
14✔
36
        _parent.timeout_handler,
14✔
37
        [this]() { return timeout_s(); }),
1✔
38
    _request_message(
39
        *this, _command_sender, _parent.mavlink_message_handler, _parent.timeout_handler),
14✔
40
    _mavlink_ftp(*this)
14✔
41
{
42
    _system_thread = new std::thread(&SystemImpl::system_thread, this);
14✔
43
}
14✔
44

45
SystemImpl::~SystemImpl()
14✔
46
{
47
    _should_exit = true;
14✔
48
    _parent.mavlink_message_handler.unregister_all(this);
14✔
49

50
    if (!_always_connected) {
14✔
51
        unregister_timeout_handler(_heartbeat_timeout_cookie);
7✔
52
    }
53

54
    if (_system_thread != nullptr) {
14✔
55
        _system_thread->join();
14✔
56
        delete _system_thread;
14✔
57
        _system_thread = nullptr;
14✔
58
    }
59
}
14✔
60

61
void SystemImpl::init(uint8_t system_id, uint8_t comp_id, bool connected)
14✔
62
{
63
    _target_address.system_id = system_id;
14✔
64
    // FIXME: for now use this as a default.
65
    _target_address.component_id = MAV_COMP_ID_AUTOPILOT1;
14✔
66

67
    if (connected) {
14✔
68
        _always_connected = true;
7✔
69
        set_connected();
7✔
70
    }
71

72
    _parent.mavlink_message_handler.register_one(
14✔
73
        MAVLINK_MSG_ID_HEARTBEAT,
74
        [this](const mavlink_message_t& message) { process_heartbeat(message); },
19✔
75
        this);
76

77
    _parent.mavlink_message_handler.register_one(
14✔
78
        MAVLINK_MSG_ID_STATUSTEXT,
79
        [this](const mavlink_message_t& message) { process_statustext(message); },
3✔
80
        this);
81

82
    _parent.mavlink_message_handler.register_one(
14✔
83
        MAVLINK_MSG_ID_AUTOPILOT_VERSION,
84
        [this](const mavlink_message_t& message) { process_autopilot_version(message); },
7✔
85
        this);
86

87
    // register_mavlink_command_handler(
88
    //    MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES,
89
    //    [this](const MavlinkCommandReceiver::CommandLong& command) {
90
    //        return process_autopilot_version_request(command);
91
    //    },
92
    //    this);
93

94
    //// TO-DO!
95
    // register_mavlink_command_handler(
96
    //    MAV_CMD_REQUEST_MESSAGE,
97
    //    [this](const MavlinkCommandReceiver::CommandLong& command) {
98
    //        return make_command_ack_message(command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
99
    //    },
100
    //    this);
101

102
    add_new_component(comp_id);
14✔
103
}
14✔
104

105
bool SystemImpl::is_connected() const
12✔
106
{
107
    return _connected;
12✔
108
}
109

110
void SystemImpl::register_mavlink_message_handler(
71✔
111
    uint16_t msg_id, const MavlinkMessageHandler& callback, const void* cookie)
112
{
113
    _parent.mavlink_message_handler.register_one(msg_id, callback, cookie);
71✔
114
}
71✔
115

116
void SystemImpl::register_mavlink_message_handler(
7✔
117
    uint16_t msg_id, uint8_t cmp_id, const MavlinkMessageHandler& callback, const void* cookie)
118
{
119
    _parent.mavlink_message_handler.register_one(msg_id, cmp_id, callback, cookie);
7✔
120
}
7✔
121

122
void SystemImpl::unregister_mavlink_message_handler(uint16_t msg_id, const void* cookie)
×
123
{
124
    _parent.mavlink_message_handler.unregister_one(msg_id, cookie);
×
125
}
×
126

127
void SystemImpl::unregister_all_mavlink_message_handlers(const void* cookie)
46✔
128
{
129
    _parent.mavlink_message_handler.unregister_all(cookie);
46✔
130
}
46✔
131

132
void SystemImpl::update_componentid_messages_handler(
×
133
    uint16_t msg_id, uint8_t cmp_id, const void* cookie)
134
{
135
    _parent.mavlink_message_handler.update_component_id(msg_id, cmp_id, cookie);
×
136
}
×
137

138
void SystemImpl::register_timeout_handler(
24✔
139
    const std::function<void()>& callback, double duration_s, void** cookie)
140
{
141
    _parent.timeout_handler.add(callback, duration_s, cookie);
24✔
142
}
24✔
143

144
void SystemImpl::refresh_timeout_handler(const void* cookie)
3✔
145
{
146
    _parent.timeout_handler.refresh(cookie);
3✔
147
}
3✔
148

149
void SystemImpl::unregister_timeout_handler(const void* cookie)
22✔
150
{
151
    _parent.timeout_handler.remove(cookie);
22✔
152
}
22✔
153

154
double SystemImpl::timeout_s() const
225✔
155
{
156
    return _parent.timeout_s();
225✔
157
}
158

159
void SystemImpl::enable_timesync()
×
160
{
161
    _timesync.enable();
×
162
}
×
163

164
System::IsConnectedHandle
165
SystemImpl::subscribe_is_connected(const System::IsConnectedCallback& callback)
×
166
{
167
    std::lock_guard<std::mutex> lock(_connection_mutex);
×
168
    return _is_connected_callbacks.subscribe(callback);
×
169
}
170

171
void SystemImpl::unsubscribe_is_connected(System::IsConnectedHandle handle)
×
172
{
173
    _is_connected_callbacks.unsubscribe(handle);
×
174
}
×
175

176
void SystemImpl::add_call_every(std::function<void()> callback, float interval_s, void** cookie)
5✔
177
{
178
    _parent.call_every_handler.add(std::move(callback), static_cast<double>(interval_s), cookie);
5✔
179
}
5✔
180

181
void SystemImpl::change_call_every(float interval_s, const void* cookie)
×
182
{
183
    _parent.call_every_handler.change(static_cast<double>(interval_s), cookie);
×
184
}
×
185

186
void SystemImpl::reset_call_every(const void* cookie)
×
187
{
188
    _parent.call_every_handler.reset(cookie);
×
189
}
×
190

191
void SystemImpl::remove_call_every(const void* cookie)
10✔
192
{
193
    _parent.call_every_handler.remove(cookie);
10✔
194
}
10✔
195

196
void SystemImpl::register_statustext_handler(
1✔
197
    std::function<void(const MavlinkStatustextHandler::Statustext&)> callback, void* cookie)
198
{
199
    std::lock_guard<std::mutex> lock(_statustext_handler_callbacks_mutex);
2✔
200
    _statustext_handler_callbacks.push_back(StatustextCallback{std::move(callback), cookie});
1✔
201
}
1✔
202

203
void SystemImpl::unregister_statustext_handler(void* cookie)
1✔
204
{
205
    std::lock_guard<std::mutex> lock(_statustext_handler_callbacks_mutex);
2✔
206
    _statustext_handler_callbacks.erase(std::remove_if(
1✔
207
        _statustext_handler_callbacks.begin(),
208
        _statustext_handler_callbacks.end(),
209
        [&](const auto& entry) { return entry.cookie == cookie; }));
2✔
210
}
1✔
211

212
void SystemImpl::process_heartbeat(const mavlink_message_t& message)
19✔
213
{
214
    mavlink_heartbeat_t heartbeat;
19✔
215
    mavlink_msg_heartbeat_decode(&message, &heartbeat);
19✔
216

217
    if (heartbeat.autopilot == MAV_AUTOPILOT_PX4) {
19✔
218
        _autopilot = Autopilot::Px4;
×
219
    } else if (heartbeat.autopilot == MAV_AUTOPILOT_ARDUPILOTMEGA) {
19✔
220
        _autopilot = Autopilot::ArduPilot;
×
221
    }
222

223
    // Only set the vehicle type if the heartbeat is from an autopilot component
224
    // This check only works if the MAV_TYPE::MAV_TYPE_ENUM_END is actually the
225
    // last enumerator.
226
    if (MAV_TYPE::MAV_TYPE_ENUM_END < heartbeat.type) {
19✔
227
        LogErr() << "type received in HEARTBEAT was not recognized";
×
228
    } else {
229
        const auto new_vehicle_type = static_cast<MAV_TYPE>(heartbeat.type);
19✔
230
        if (heartbeat.autopilot != MAV_AUTOPILOT_INVALID && _vehicle_type != new_vehicle_type &&
19✔
231
            new_vehicle_type != MAV_TYPE_GENERIC) {
232
            LogWarn() << "Vehicle type changed (new type: " << static_cast<unsigned>(heartbeat.type)
×
233
                      << ", old type: " << static_cast<unsigned>(_vehicle_type) << ")";
×
234
            _vehicle_type = new_vehicle_type;
×
235
        }
236
    }
237

238
    if (message.compid == MavlinkCommandSender::DEFAULT_COMPONENT_ID_AUTOPILOT) {
19✔
239
        _armed = (heartbeat.base_mode & MAV_MODE_FLAG_SAFETY_ARMED) != 0;
9✔
240
        _hitl_enabled = (heartbeat.base_mode & MAV_MODE_FLAG_HIL_ENABLED) != 0;
9✔
241
    }
242
    if (heartbeat.base_mode & MAV_MODE_FLAG_CUSTOM_MODE_ENABLED) {
19✔
243
        _flight_mode =
×
244
            to_flight_mode_from_custom_mode(_autopilot, _vehicle_type, heartbeat.custom_mode);
245
    }
246

247
    set_connected();
19✔
248
}
19✔
249

250
void SystemImpl::process_statustext(const mavlink_message_t& message)
3✔
251
{
252
    mavlink_statustext_t statustext;
3✔
253
    mavlink_msg_statustext_decode(&message, &statustext);
3✔
254

255
    const auto maybe_result = _statustext_handler.process(statustext);
6✔
256

257
    if (maybe_result.has_value()) {
3✔
258
        LogDebug() << "MAVLink: "
6✔
259
                   << MavlinkStatustextHandler::severity_str(maybe_result.value().severity) << ": "
6✔
260
                   << maybe_result.value().text;
9✔
261

262
        std::lock_guard<std::mutex> lock(_statustext_handler_callbacks_mutex);
6✔
263
        for (const auto& entry : _statustext_handler_callbacks) {
6✔
264
            entry.callback(maybe_result.value());
3✔
265
        }
266
    }
267
}
3✔
268

269
void SystemImpl::process_autopilot_version(const mavlink_message_t& message)
7✔
270
{
271
    mavlink_autopilot_version_t autopilot_version;
7✔
272
    mavlink_msg_autopilot_version_decode(&message, &autopilot_version);
7✔
273

274
    _mission_transfer.set_int_messages_supported(
7✔
275
        autopilot_version.capabilities & MAV_PROTOCOL_CAPABILITY_MISSION_INT);
7✔
276
}
7✔
277

278
void SystemImpl::heartbeats_timed_out()
×
279
{
280
    LogInfo() << "heartbeats timed out";
×
281
    set_disconnected();
×
282
}
×
283

284
void SystemImpl::system_thread()
14✔
285
{
286
    SteadyTimePoint last_ping_time{};
14✔
287

288
    while (!_should_exit) {
756✔
289
        _params.do_work();
742✔
290
        _command_sender.do_work();
741✔
291
        _timesync.do_work();
741✔
292
        _mission_transfer.do_work();
741✔
293

294
        if (_parent.time.elapsed_since_s(last_ping_time) >= SystemImpl::_ping_interval_s) {
741✔
295
            if (_connected) {
14✔
296
                _ping.run_once();
7✔
297
            }
298
            last_ping_time = _parent.time.steady_time();
14✔
299
        }
300

301
        if (_connected) {
740✔
302
            // Work fairly fast if we're connected.
303
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
735✔
304
        } else {
305
            // Be less aggressive when unconnected.
306
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
7✔
307
        }
308
    }
309
}
14✔
310

311
// std::optional<mavlink_message_t>
312
// SystemImpl::process_autopilot_version_request(const MavlinkCommandReceiver::CommandLong& command)
313
//{
314
//    if (_should_send_autopilot_version) {
315
//        send_autopilot_version();
316
//        return make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
317
//    }
318
//
319
//    return {};
320
//}
321

322
std::string SystemImpl::component_name(uint8_t component_id)
14✔
323
{
324
    switch (component_id) {
14✔
325
        case MAV_COMP_ID_AUTOPILOT1:
6✔
326
            return "Autopilot";
6✔
327
        case MAV_COMP_ID_CAMERA:
1✔
328
            return "Camera 1";
1✔
329
        case MAV_COMP_ID_CAMERA2:
×
330
            return "Camera 2";
×
331
        case MAV_COMP_ID_CAMERA3:
×
332
            return "Camera 3";
×
333
        case MAV_COMP_ID_CAMERA4:
×
334
            return "Camera 4";
×
335
        case MAV_COMP_ID_CAMERA5:
×
336
            return "Camera 5";
×
337
        case MAV_COMP_ID_CAMERA6:
×
338
            return "Camera 6";
×
339
        case MAV_COMP_ID_GIMBAL:
×
340
            return "Gimbal";
×
341
        case MAV_COMP_ID_MISSIONPLANNER:
7✔
342
            return "Ground station";
7✔
343
        case MAV_COMP_ID_WINCH:
×
344
            return "Winch";
×
345
        default:
×
346
            return "Unsupported component";
×
347
    }
348
}
349

350
System::ComponentType SystemImpl::component_type(uint8_t component_id)
28✔
351
{
352
    switch (component_id) {
28✔
353
        case MAV_COMP_ID_AUTOPILOT1:
12✔
354
            return System::ComponentType::AUTOPILOT;
12✔
355
        case MAV_COMP_ID_CAMERA:
2✔
356
        case MAV_COMP_ID_CAMERA2:
357
        case MAV_COMP_ID_CAMERA3:
358
        case MAV_COMP_ID_CAMERA4:
359
        case MAV_COMP_ID_CAMERA5:
360
        case MAV_COMP_ID_CAMERA6:
361
            return System::ComponentType::CAMERA;
2✔
362
        case MAV_COMP_ID_GIMBAL:
×
363
            return System::ComponentType::GIMBAL;
×
364
        default:
14✔
365
            return System::ComponentType::UNKNOWN;
14✔
366
    }
367
}
368

369
void SystemImpl::add_new_component(uint8_t component_id)
315✔
370
{
371
    if (component_id == 0) {
315✔
372
        return;
7✔
373
    }
374

375
    auto res_pair = _components.insert(component_id);
308✔
376
    if (res_pair.second) {
308✔
377
        std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
28✔
378
        _component_discovered_callbacks.queue(
14✔
379
            component_type(component_id), [this](const auto& func) { call_user_callback(func); });
×
380
        _component_discovered_id_callbacks.queue(
14✔
381
            component_type(component_id), component_id, [this](const auto& func) {
×
382
                call_user_callback(func);
×
383
            });
×
384
        LogDebug() << "Component " << component_name(component_id) << " (" << int(component_id)
56✔
385
                   << ") added.";
56✔
386
    }
387
}
388

389
size_t SystemImpl::total_components() const
×
390
{
391
    return _components.size();
×
392
}
393

394
System::ComponentDiscoveredHandle
395
SystemImpl::subscribe_component_discovered(const System::ComponentDiscoveredCallback& callback)
×
396
{
397
    std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
×
398
    const auto handle = _component_discovered_callbacks.subscribe(callback);
×
399

400
    if (total_components() > 0) {
×
401
        for (const auto& elem : _components) {
×
402
            _component_discovered_callbacks.queue(
×
403
                component_type(elem), [this](const auto& func) { call_user_callback(func); });
×
404
        }
405
    }
406
    return handle;
×
407
}
408

409
void SystemImpl::unsubscribe_component_discovered(System::ComponentDiscoveredHandle handle)
×
410
{
411
    std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
×
412
    _component_discovered_callbacks.unsubscribe(handle);
×
413
}
×
414

415
System::ComponentDiscoveredIdHandle
416
SystemImpl::subscribe_component_discovered_id(const System::ComponentDiscoveredIdCallback& callback)
×
417
{
418
    std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
×
419
    const auto handle = _component_discovered_id_callbacks.subscribe(callback);
×
420

421
    if (total_components() > 0) {
×
422
        for (const auto& elem : _components) {
×
423
            _component_discovered_id_callbacks.queue(
×
424
                component_type(elem), elem, [this](const auto& func) { call_user_callback(func); });
×
425
        }
426
    }
427
    return handle;
×
428
}
429

430
void SystemImpl::unsubscribe_component_discovered_id(System::ComponentDiscoveredIdHandle handle)
×
431
{
432
    std::lock_guard<std::mutex> lock(_component_discovered_callback_mutex);
×
433
    _component_discovered_id_callbacks.unsubscribe(handle);
×
434
}
×
435

436
bool SystemImpl::is_standalone() const
×
437
{
438
    return !has_autopilot();
×
439
}
440

441
bool SystemImpl::has_autopilot() const
22✔
442
{
443
    return get_autopilot_id() != uint8_t(0);
22✔
444
}
445

446
bool SystemImpl::is_autopilot(uint8_t comp_id)
×
447
{
448
    return comp_id == MAV_COMP_ID_AUTOPILOT1;
×
449
}
450

451
bool SystemImpl::is_camera(uint8_t comp_id)
1✔
452
{
453
    return (comp_id >= MAV_COMP_ID_CAMERA) && (comp_id <= MAV_COMP_ID_CAMERA6);
1✔
454
}
455

456
bool SystemImpl::has_camera(int camera_id) const
2✔
457
{
458
    int camera_comp_id = (camera_id == -1) ? camera_id : (MAV_COMP_ID_CAMERA + camera_id);
2✔
459

460
    if (camera_comp_id == -1) { // Check whether the system has any camera.
2✔
461
        if (std::any_of(_components.begin(), _components.end(), is_camera)) {
1✔
462
            return true;
1✔
463
        }
464
    } else { // Look for the camera whose id is `camera_id`.
465
        for (auto compid : _components) {
1✔
466
            if (compid == camera_comp_id) {
1✔
467
                return true;
1✔
468
            }
469
        }
470
    }
471
    return false;
×
472
}
473

474
bool SystemImpl::has_gimbal() const
×
475
{
476
    return get_gimbal_id() == MAV_COMP_ID_GIMBAL;
×
477
}
478

479
bool SystemImpl::send_message(mavlink_message_t& message)
45✔
480
{
481
    return _parent.send_message(message);
45✔
482
}
483

484
void SystemImpl::send_autopilot_version_request()
×
485
{
486
    auto prom = std::promise<MavlinkCommandSender::Result>();
×
487
    auto fut = prom.get_future();
×
488

489
    send_autopilot_version_request_async(
×
490
        [&prom](MavlinkCommandSender::Result result, float) { prom.set_value(result); });
×
491

492
    if (fut.get() == MavlinkCommandSender::Result::Unsupported) {
×
493
        _old_message_520_supported = false;
×
494
        LogWarn() << "Trying alternative command (512).";
×
495
        send_autopilot_version_request();
×
496
    }
497
}
×
498

499
void SystemImpl::send_autopilot_version_request_async(
6✔
500
    const MavlinkCommandSender::CommandResultCallback& callback)
501
{
502
    MavlinkCommandSender::CommandLong command{};
6✔
503
    command.target_component_id = get_autopilot_id();
6✔
504

505
    if (_old_message_520_supported) {
6✔
506
        // Note: This MAVLINK message is deprecated and would be removed from MAVSDK in a future
507
        // release.
508
        command.command = MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES;
6✔
509
        command.params.maybe_param1 = 1.0f;
6✔
510
    } else {
511
        command.command = MAV_CMD_REQUEST_MESSAGE;
×
512
        command.params.maybe_param1 = {static_cast<float>(MAVLINK_MSG_ID_AUTOPILOT_VERSION)};
×
513
    }
514

515
    send_command_async(command, callback);
6✔
516
}
6✔
517

518
void SystemImpl::send_flight_information_request()
×
519
{
520
    auto prom = std::promise<MavlinkCommandSender::Result>();
×
521
    auto fut = prom.get_future();
×
522

523
    MavlinkCommandSender::CommandLong command{};
×
524
    command.target_component_id = get_autopilot_id();
×
525

526
    if (_old_message_528_supported) {
×
527
        // Note: This MAVLINK message is deprecated and would be removed from MAVSDK in a future
528
        // release.
529
        command.command = MAV_CMD_REQUEST_FLIGHT_INFORMATION;
×
530
        command.params.maybe_param1 = 1.0f;
×
531
    } else {
532
        command.command = MAV_CMD_REQUEST_MESSAGE;
×
533
        command.params.maybe_param1 = {static_cast<float>(MAVLINK_MSG_ID_FLIGHT_INFORMATION)};
×
534
    }
535

536
    send_command_async(
×
537
        command, [&prom](MavlinkCommandSender::Result result, float) { prom.set_value(result); });
×
538
    if (fut.get() == MavlinkCommandSender::Result::Unsupported) {
×
539
        _old_message_528_supported = false;
×
540
        LogWarn() << "Trying alternative command (512)..";
×
541
        send_flight_information_request();
×
542
    }
543
}
×
544

545
void SystemImpl::set_connected()
26✔
546
{
547
    bool enable_needed = false;
26✔
548
    {
549
        std::lock_guard<std::mutex> lock(_connection_mutex);
52✔
550

551
        if (!_connected) {
26✔
552
            if (!_components.empty()) {
14✔
553
                LogDebug() << "Discovered " << _components.size() << " component(s)";
7✔
554
            }
555

556
            _connected = true;
14✔
557

558
            // System with sysid 0 is a bit special: it is a placeholder for a connection initiated
559
            // by MAVSDK. As such, it should not be advertised as a newly discovered system.
560
            if (static_cast<int>(get_system_id()) != 0) {
14✔
561
                _parent.notify_on_discover();
7✔
562
            }
563

564
            // We call this later to avoid deadlocks on creating the server components.
565
            _parent.call_user_callback([this]() {
28✔
566
                // Send a heartbeat back immediately.
567
                _parent.start_sending_heartbeats();
568
            });
569

570
            if (!_always_connected) {
14✔
571
                register_timeout_handler(
7✔
572
                    [this] { heartbeats_timed_out(); },
×
573
                    HEARTBEAT_TIMEOUT_S,
574
                    &_heartbeat_timeout_cookie);
575
            }
576
            enable_needed = true;
14✔
577

578
            _is_connected_callbacks.queue(
14✔
579
                true, [this](const auto& func) { _parent.call_user_callback(func); });
×
580

581
        } else if (_connected && !_always_connected) {
12✔
582
            refresh_timeout_handler(_heartbeat_timeout_cookie);
3✔
583
        }
584
        // If not yet connected there is nothing to do/
585
    }
586
    if (enable_needed) {
26✔
587
        if (has_autopilot()) {
14✔
588
            send_autopilot_version_request_async(nullptr);
6✔
589
        }
590

591
        std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
28✔
592
        for (auto plugin_impl : _plugin_impls) {
14✔
593
            plugin_impl->enable();
×
594
        }
595
    }
596
}
26✔
597

598
void SystemImpl::set_disconnected()
×
599
{
600
    {
601
        std::lock_guard<std::mutex> lock(_connection_mutex);
×
602

603
        // This might not be needed because this is probably called from the triggered
604
        // timeout anyway, but it should also do no harm.
605
        // unregister_timeout_handler(_heartbeat_timeout_cookie);
606
        //_heartbeat_timeout_cookie = nullptr;
607

608
        _connected = false;
×
609
        _parent.notify_on_timeout();
×
610
        _is_connected_callbacks.queue(
×
611
            false, [this](const auto& func) { _parent.call_user_callback(func); });
×
612
    }
613

614
    _parent.stop_sending_heartbeats();
×
615

616
    {
617
        std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
×
618
        for (auto plugin_impl : _plugin_impls) {
×
619
            plugin_impl->disable();
×
620
        }
621
    }
622
}
×
623

624
uint8_t SystemImpl::get_system_id() const
47✔
625
{
626
    return _target_address.system_id;
47✔
627
}
628

629
std::vector<uint8_t> SystemImpl::component_ids() const
×
630
{
631
    return std::vector<uint8_t>{_components.begin(), _components.end()};
×
632
}
633

634
void SystemImpl::set_system_id(uint8_t system_id)
7✔
635
{
636
    _target_address.system_id = system_id;
7✔
637
}
7✔
638

639
uint8_t SystemImpl::get_own_system_id() const
58✔
640
{
641
    return _parent.get_own_system_id();
58✔
642
}
643

644
uint8_t SystemImpl::get_own_component_id() const
58✔
645
{
646
    return _parent.get_own_component_id();
58✔
647
}
648

649
MAV_TYPE SystemImpl::get_vehicle_type() const
×
650
{
651
    return _vehicle_type;
×
652
}
653

654
uint8_t SystemImpl::get_own_mav_type() const
×
655
{
656
    return _parent.get_mav_type();
×
657
}
658

659
MAVLinkParameters::Result SystemImpl::set_param(
×
660
    const std::string& name,
661
    MAVLinkParameters::ParamValue value,
662
    std::optional<uint8_t> maybe_component_id,
663
    bool extended)
664
{
665
    return _params.set_param(name, value, maybe_component_id, extended);
×
666
}
667

668
MAVLinkParameters::Result SystemImpl::set_param_float(
1✔
669
    const std::string& name, float value, std::optional<uint8_t> maybe_component_id, bool extended)
670
{
671
    return _params.set_param_float(name, value, maybe_component_id, extended);
1✔
672
}
673

674
MAVLinkParameters::Result SystemImpl::set_param_int(
1✔
675
    const std::string& name,
676
    int32_t value,
677
    std::optional<uint8_t> maybe_component_id,
678
    bool extended)
679
{
680
    return _params.set_param_int(name, value, maybe_component_id, extended);
1✔
681
}
682

683
MAVLinkParameters::Result
684
SystemImpl::set_param_custom(const std::string& name, const std::string& value)
1✔
685
{
686
    return _params.set_param_custom(name, value);
1✔
687
}
688

689
std::map<std::string, MAVLinkParameters::ParamValue> SystemImpl::get_all_params()
1✔
690
{
691
    return _params.get_all_params();
1✔
692
}
693

694
void SystemImpl::set_param_async(
×
695
    const std::string& name,
696
    MAVLinkParameters::ParamValue value,
697
    const SetParamCallback& callback,
698
    const void* cookie,
699
    std::optional<uint8_t> maybe_component_id,
700
    bool extended)
701
{
702
    _params.set_param_async(name, value, callback, cookie, maybe_component_id, extended);
×
703
}
×
704

705
void SystemImpl::set_param_float_async(
×
706
    const std::string& name,
707
    float value,
708
    const SetParamCallback& callback,
709
    const void* cookie,
710
    std::optional<uint8_t> maybe_component_id,
711
    bool extended)
712
{
713
    _params.set_param_float_async(name, value, callback, cookie, maybe_component_id, extended);
×
714
}
×
715

716
void SystemImpl::set_param_int_async(
×
717
    const std::string& name,
718
    int32_t value,
719
    const SetParamCallback& callback,
720
    const void* cookie,
721
    std::optional<uint8_t> maybe_component_id,
722
    bool extended)
723
{
724
    _params.set_param_int_async(name, value, callback, cookie, maybe_component_id, extended);
×
725
}
×
726

727
std::pair<MAVLinkParameters::Result, float> SystemImpl::get_param_float(const std::string& name)
2✔
728
{
729
    return _params.get_param_float(name, {}, false);
2✔
730
}
731

732
std::pair<MAVLinkParameters::Result, int> SystemImpl::get_param_int(const std::string& name)
2✔
733
{
734
    return _params.get_param_int(name, {}, false);
2✔
735
}
736

737
std::pair<MAVLinkParameters::Result, std::string>
738
SystemImpl::get_param_custom(const std::string& name)
2✔
739
{
740
    return _params.get_param_custom(name);
2✔
741
}
742

743
void SystemImpl::get_param_async(
×
744
    const std::string& name,
745
    MAVLinkParameters::ParamValue value,
746
    const GetParamAnyCallback& callback,
747
    const void* cookie,
748
    std::optional<uint8_t> maybe_component_id,
749
    bool extended)
750
{
751
    _params.get_param_async(name, value, callback, cookie, maybe_component_id, extended);
×
752
}
×
753

754
void SystemImpl::get_param_float_async(
×
755
    const std::string& name,
756
    const GetParamFloatCallback& callback,
757
    const void* cookie,
758
    std::optional<uint8_t> maybe_component_id,
759
    bool extended)
760
{
761
    _params.get_param_float_async(name, callback, cookie, maybe_component_id, extended);
×
762
}
×
763

764
void SystemImpl::get_param_int_async(
×
765
    const std::string& name,
766
    const GetParamIntCallback& callback,
767
    const void* cookie,
768
    std::optional<uint8_t> maybe_component_id,
769
    bool extended)
770
{
771
    _params.get_param_int_async(name, callback, cookie, maybe_component_id, extended);
×
772
}
×
773

774
void SystemImpl::get_param_custom_async(
×
775
    const std::string& name, const GetParamCustomCallback& callback, const void* cookie)
776
{
777
    _params.get_param_custom_async(name, callback, cookie);
×
778
}
×
779

780
void SystemImpl::cancel_all_param(const void* cookie)
2✔
781
{
782
    _params.cancel_all_param(cookie);
2✔
783
}
2✔
784

785
MavlinkCommandSender::Result
786
SystemImpl::set_flight_mode(FlightMode system_mode, uint8_t component_id)
×
787
{
788
    std::pair<MavlinkCommandSender::Result, MavlinkCommandSender::CommandLong> result =
×
789
        make_command_flight_mode(system_mode, component_id);
×
790

791
    if (result.first != MavlinkCommandSender::Result::Success) {
×
792
        return result.first;
×
793
    }
794

795
    return send_command(result.second);
×
796
}
797

798
void SystemImpl::set_flight_mode_async(
×
799
    FlightMode system_mode, const CommandResultCallback& callback, uint8_t component_id)
800
{
801
    std::pair<MavlinkCommandSender::Result, MavlinkCommandSender::CommandLong> result =
×
802
        make_command_flight_mode(system_mode, component_id);
×
803

804
    if (result.first != MavlinkCommandSender::Result::Success) {
×
805
        if (callback) {
×
806
            callback(result.first, NAN);
×
807
        }
808
        return;
×
809
    }
810

811
    send_command_async(result.second, callback);
×
812
}
813

814
std::pair<MavlinkCommandSender::Result, MavlinkCommandSender::CommandLong>
815
SystemImpl::make_command_flight_mode(FlightMode flight_mode, uint8_t component_id)
×
816
{
817
    if (_autopilot == Autopilot::ArduPilot) {
×
818
        return make_command_ardupilot_mode(flight_mode, component_id);
×
819
    } else {
820
        return make_command_px4_mode(flight_mode, component_id);
×
821
    }
822
}
823

824
std::pair<MavlinkCommandSender::Result, MavlinkCommandSender::CommandLong>
825
SystemImpl::make_command_ardupilot_mode(FlightMode flight_mode, uint8_t component_id)
×
826
{
827
    const uint8_t flag_safety_armed = is_armed() ? MAV_MODE_FLAG_SAFETY_ARMED : 0;
×
828
    const uint8_t flag_hitl_enabled = _hitl_enabled ? MAV_MODE_FLAG_HIL_ENABLED : 0;
×
829
    const uint8_t mode_type =
×
830
        MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | flag_safety_armed | flag_hitl_enabled;
×
831

832
    MavlinkCommandSender::CommandLong command{};
×
833

834
    command.command = MAV_CMD_DO_SET_MODE;
×
835
    command.params.maybe_param1 = static_cast<float>(mode_type);
×
836

837
    switch (_vehicle_type) {
×
838
        case MAV_TYPE::MAV_TYPE_SURFACE_BOAT:
×
839
        case MAV_TYPE::MAV_TYPE_GROUND_ROVER:
840
            if (flight_mode_to_ardupilot_rover_mode(flight_mode) == ardupilot::RoverMode::Unknown) {
×
841
                LogErr() << "Cannot translate flight mode to ArduPilot Rover mode.";
×
842
                MavlinkCommandSender::CommandLong empty_command{};
×
843
                return std::make_pair<>(MavlinkCommandSender::Result::UnknownError, empty_command);
×
844
            } else {
845
                command.params.maybe_param2 =
×
846
                    static_cast<float>(flight_mode_to_ardupilot_rover_mode(flight_mode));
×
847
            }
848
            break;
×
849
        case MAV_TYPE::MAV_TYPE_FIXED_WING:
×
850
        case MAV_TYPE::MAV_TYPE_VTOL_TAILSITTER_DUOROTOR:
851
        case MAV_TYPE::MAV_TYPE_VTOL_TAILSITTER_QUADROTOR:
852
        case MAV_TYPE::MAV_TYPE_VTOL_TILTROTOR:
853
        case MAV_TYPE::MAV_TYPE_VTOL_FIXEDROTOR:
854
        case MAV_TYPE::MAV_TYPE_VTOL_TAILSITTER:
855
        case MAV_TYPE::MAV_TYPE_VTOL_RESERVED4:
856
        case MAV_TYPE::MAV_TYPE_VTOL_RESERVED5: {
857
            const auto new_mode = flight_mode_to_ardupilot_plane_mode(flight_mode);
×
858
            if (new_mode == ardupilot::PlaneMode::Unknown) {
×
859
                LogErr() << "Cannot translate flight mode to ArduPilot Plane mode.";
×
860
                MavlinkCommandSender::CommandLong empty_command{};
×
861
                return std::make_pair<>(MavlinkCommandSender::Result::UnknownError, empty_command);
×
862
            } else {
863
                command.params.maybe_param2 = static_cast<float>(new_mode);
×
864
            }
865
            break;
×
866
        }
867

868
        case MAV_TYPE::MAV_TYPE_QUADROTOR:
×
869
        case MAV_TYPE::MAV_TYPE_COAXIAL:
870
        case MAV_TYPE::MAV_TYPE_HELICOPTER:
871
        case MAV_TYPE::MAV_TYPE_HEXAROTOR:
872
        case MAV_TYPE::MAV_TYPE_OCTOROTOR:
873
        case MAV_TYPE::MAV_TYPE_TRICOPTER:
874
        case MAV_TYPE::MAV_TYPE_DODECAROTOR:
875
        case MAV_TYPE::MAV_TYPE_DECAROTOR: {
876
            const auto new_mode = flight_mode_to_ardupilot_copter_mode(flight_mode);
×
877
            if (new_mode == ardupilot::CopterMode::Unknown) {
×
878
                LogErr() << "Cannot translate flight mode to ArduPilot Copter mode.";
×
879
                MavlinkCommandSender::CommandLong empty_command{};
×
880
                return std::make_pair<>(MavlinkCommandSender::Result::UnknownError, empty_command);
×
881
            } else {
882
                command.params.maybe_param2 =
×
883
                    static_cast<float>(flight_mode_to_ardupilot_copter_mode(flight_mode));
×
884
            }
885
            break;
×
886
        }
887

888
        default:
×
889
            LogErr() << "Cannot translate flight mode to ArduPilot mode, for MAV_TYPE: "
×
890
                     << _vehicle_type;
×
891
            MavlinkCommandSender::CommandLong empty_command{};
×
892
            return std::make_pair<>(MavlinkCommandSender::Result::UnknownError, empty_command);
×
893
    }
894
    command.target_component_id = component_id;
×
895

896
    return std::make_pair<>(MavlinkCommandSender::Result::Success, command);
×
897
}
898
ardupilot::RoverMode SystemImpl::flight_mode_to_ardupilot_rover_mode(FlightMode flight_mode)
×
899
{
900
    switch (flight_mode) {
×
901
        case FlightMode::Mission:
×
902
            return ardupilot::RoverMode::Auto;
×
903
        case FlightMode::Acro:
×
904
            return ardupilot::RoverMode::Acro;
×
905
        case FlightMode::Hold:
×
906
            return ardupilot::RoverMode::Hold;
×
907
        case FlightMode::ReturnToLaunch:
×
908
            return ardupilot::RoverMode::RTL;
×
909
        case FlightMode::Manual:
×
910
            return ardupilot::RoverMode::Manual;
×
911
        case FlightMode::FollowMe:
×
912
            return ardupilot::RoverMode::Follow;
×
913
        case FlightMode::Offboard:
×
914
            return ardupilot::RoverMode::Guided;
×
915
        case FlightMode::Unknown:
×
916
        case FlightMode::Ready:
917
        case FlightMode::Takeoff:
918
        case FlightMode::Land:
919
        case FlightMode::Altctl:
920
        case FlightMode::Posctl:
921
        case FlightMode::Rattitude:
922
        case FlightMode::Stabilized:
923
        default:
924
            return ardupilot::RoverMode::Unknown;
×
925
    }
926
}
927
ardupilot::CopterMode SystemImpl::flight_mode_to_ardupilot_copter_mode(FlightMode flight_mode)
×
928
{
929
    switch (flight_mode) {
×
930
        case FlightMode::Mission:
×
931
            return ardupilot::CopterMode::Auto;
×
932
        case FlightMode::Acro:
×
933
            return ardupilot::CopterMode::Acro;
×
934
        case FlightMode::Hold:
×
935
            return ardupilot::CopterMode::Loiter;
×
936
        case FlightMode::ReturnToLaunch:
×
937
            return ardupilot::CopterMode::Rtl;
×
938
        case FlightMode::Land:
×
939
            return ardupilot::CopterMode::Land;
×
940
        case FlightMode::Manual:
×
941
            return ardupilot::CopterMode::Stabilize;
×
942
        case FlightMode::FollowMe:
×
943
            return ardupilot::CopterMode::Follow;
×
944
        case FlightMode::Offboard:
×
945
            return ardupilot::CopterMode::Guided;
×
946
        case FlightMode::Altctl:
×
947
            return ardupilot::CopterMode::AltHold;
×
948
        case FlightMode::Posctl:
×
949
            return ardupilot::CopterMode::PosHold;
×
950
        case FlightMode::Stabilized:
×
951
            return ardupilot::CopterMode::Stabilize;
×
952
        case FlightMode::Unknown:
×
953
        case FlightMode::Ready:
954
        case FlightMode::Takeoff:
955
        case FlightMode::Rattitude:
956
        default:
957
            return ardupilot::CopterMode::Unknown;
×
958
    }
959
}
960
ardupilot::PlaneMode SystemImpl::flight_mode_to_ardupilot_plane_mode(FlightMode flight_mode)
×
961
{
962
    switch (flight_mode) {
×
963
        case FlightMode::Mission:
×
964
            return ardupilot::PlaneMode::Auto;
×
965
        case FlightMode::Acro:
×
966
            return ardupilot::PlaneMode::Acro;
×
967
        case FlightMode::Hold:
×
968
            return ardupilot::PlaneMode::Loiter;
×
969
        case FlightMode::ReturnToLaunch:
×
970
            return ardupilot::PlaneMode::Rtl;
×
971
        case FlightMode::Manual:
×
972
            return ardupilot::PlaneMode::Manual;
×
973
        case FlightMode::FBWA:
×
974
            return ardupilot::PlaneMode::Fbwa;
×
975
        case FlightMode::Stabilized:
×
976
            return ardupilot::PlaneMode::Stabilize;
×
977
        case FlightMode::Unknown:
×
978
            return ardupilot::PlaneMode::Unknown;
×
979
        default:
×
980
            return ardupilot::PlaneMode::Unknown;
×
981
    }
982
}
983

984
std::pair<MavlinkCommandSender::Result, MavlinkCommandSender::CommandLong>
985
SystemImpl::make_command_px4_mode(FlightMode flight_mode, uint8_t component_id)
×
986
{
987
    const uint8_t flag_safety_armed = is_armed() ? MAV_MODE_FLAG_SAFETY_ARMED : 0;
×
988
    const uint8_t flag_hitl_enabled = _hitl_enabled ? MAV_MODE_FLAG_HIL_ENABLED : 0;
×
989

990
    const uint8_t mode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | flag_safety_armed | flag_hitl_enabled;
×
991

992
    // Note: the safety flag is not needed in future versions of the PX4 Firmware
993
    //       but want to be rather safe than sorry.
994
    uint8_t custom_mode = px4::PX4_CUSTOM_MAIN_MODE_AUTO;
×
995
    uint8_t custom_sub_mode = 0;
×
996

997
    switch (flight_mode) {
×
998
        case FlightMode::Hold:
×
999
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_LOITER;
×
1000
            break;
×
1001
        case FlightMode::ReturnToLaunch:
×
1002
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_RTL;
×
1003
            break;
×
1004
        case FlightMode::Takeoff:
×
1005
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF;
×
1006
            break;
×
1007
        case FlightMode::Land:
×
1008
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_LAND;
×
1009
            break;
×
1010
        case FlightMode::Mission:
×
1011
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_MISSION;
×
1012
            break;
×
1013
        case FlightMode::FollowMe:
×
1014
            custom_sub_mode = px4::PX4_CUSTOM_SUB_MODE_AUTO_FOLLOW_TARGET;
×
1015
            break;
×
1016
        case FlightMode::Offboard:
×
1017
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_OFFBOARD;
×
1018
            break;
×
1019
        case FlightMode::Manual:
×
1020
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_MANUAL;
×
1021
            break;
×
1022
        case FlightMode::Posctl:
×
1023
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_POSCTL;
×
1024
            break;
×
1025
        case FlightMode::Altctl:
×
1026
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_ALTCTL;
×
1027
            break;
×
1028
        case FlightMode::Rattitude:
×
1029
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_RATTITUDE;
×
1030
            break;
×
1031
        case FlightMode::Acro:
×
1032
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_ACRO;
×
1033
            break;
×
1034
        case FlightMode::Stabilized:
×
1035
            custom_mode = px4::PX4_CUSTOM_MAIN_MODE_STABILIZED;
×
1036
            break;
×
1037
        default:
×
1038
            LogErr() << "Unknown Flight mode.";
×
1039
            MavlinkCommandSender::CommandLong empty_command{};
×
1040
            return std::make_pair<>(MavlinkCommandSender::Result::UnknownError, empty_command);
×
1041
    }
1042

1043
    MavlinkCommandSender::CommandLong command{};
×
1044

1045
    command.command = MAV_CMD_DO_SET_MODE;
×
1046
    command.params.maybe_param1 = static_cast<float>(mode);
×
1047
    command.params.maybe_param2 = static_cast<float>(custom_mode);
×
1048
    command.params.maybe_param3 = static_cast<float>(custom_sub_mode);
×
1049
    command.target_component_id = component_id;
×
1050

1051
    return std::make_pair<>(MavlinkCommandSender::Result::Success, command);
×
1052
}
1053

1054
FlightMode SystemImpl::get_flight_mode() const
6✔
1055
{
1056
    return _flight_mode;
6✔
1057
}
1058

1059
void SystemImpl::receive_float_param(
×
1060
    MAVLinkParameters::Result result,
1061
    MAVLinkParameters::ParamValue value,
1062
    const GetParamFloatCallback& callback)
1063
{
1064
    if (callback) {
×
1065
        if (result == MAVLinkParameters::Result::Success) {
×
1066
            callback(result, value.get<float>());
×
1067
        } else {
1068
            callback(result, NAN);
×
1069
        }
1070
    }
1071
}
×
1072

1073
void SystemImpl::receive_int_param(
×
1074
    MAVLinkParameters::Result result,
1075
    MAVLinkParameters::ParamValue value,
1076
    const GetParamIntCallback& callback)
1077
{
1078
    if (callback) {
×
1079
        if (result == MAVLinkParameters::Result::Success) {
×
1080
            callback(result, value.get<int32_t>());
×
1081
        } else {
1082
            callback(result, 0);
×
1083
        }
1084
    }
1085
}
×
1086

1087
uint8_t SystemImpl::get_autopilot_id() const
32✔
1088
{
1089
    for (auto compid : _components)
35✔
1090
        if (compid == MavlinkCommandSender::DEFAULT_COMPONENT_ID_AUTOPILOT) {
25✔
1091
            return compid;
22✔
1092
        }
1093
    // FIXME: Not sure what should be returned if autopilot is not found
1094
    return uint8_t(0);
10✔
1095
}
1096

1097
std::vector<uint8_t> SystemImpl::get_camera_ids() const
×
1098
{
1099
    std::vector<uint8_t> camera_ids{};
×
1100

1101
    for (auto compid : _components)
×
1102
        if (compid >= MAV_COMP_ID_CAMERA && compid <= MAV_COMP_ID_CAMERA6) {
×
1103
            camera_ids.push_back(compid);
×
1104
        }
1105
    return camera_ids;
×
1106
}
1107

1108
uint8_t SystemImpl::get_gimbal_id() const
×
1109
{
1110
    for (auto compid : _components)
×
1111
        if (compid == MAV_COMP_ID_GIMBAL) {
×
1112
            return compid;
×
1113
        }
1114
    return uint8_t(0);
×
1115
}
1116

1117
MavlinkCommandSender::Result SystemImpl::send_command(MavlinkCommandSender::CommandLong& command)
1✔
1118
{
1119
    if (_target_address.system_id == 0 && _components.empty()) {
1✔
1120
        return MavlinkCommandSender::Result::NoSystem;
×
1121
    }
1122
    command.target_system_id = get_system_id();
1✔
1123
    return _command_sender.send_command(command);
1✔
1124
}
1125

1126
MavlinkCommandSender::Result SystemImpl::send_command(MavlinkCommandSender::CommandInt& command)
×
1127
{
1128
    if (_target_address.system_id == 0 && _components.empty()) {
×
1129
        return MavlinkCommandSender::Result::NoSystem;
×
1130
    }
1131
    command.target_system_id = get_system_id();
×
1132
    return _command_sender.send_command(command);
×
1133
}
1134

1135
void SystemImpl::send_command_async(
15✔
1136
    MavlinkCommandSender::CommandLong command, const CommandResultCallback& callback)
1137
{
1138
    if (_target_address.system_id == 0 && _components.empty()) {
15✔
1139
        if (callback) {
×
1140
            callback(MavlinkCommandSender::Result::NoSystem, NAN);
×
1141
        }
1142
        return;
×
1143
    }
1144
    command.target_system_id = get_system_id();
15✔
1145

1146
    _command_sender.queue_command_async(command, callback);
15✔
1147
}
1148

1149
void SystemImpl::send_command_async(
×
1150
    MavlinkCommandSender::CommandInt command, const CommandResultCallback& callback)
1151
{
1152
    if (_target_address.system_id == 0 && _components.empty()) {
×
1153
        if (callback) {
×
1154
            callback(MavlinkCommandSender::Result::NoSystem, NAN);
×
1155
        }
1156
        return;
×
1157
    }
1158
    command.target_system_id = get_system_id();
×
1159

1160
    _command_sender.queue_command_async(command, callback);
×
1161
}
1162

1163
MavlinkCommandSender::Result
1164
SystemImpl::set_msg_rate(uint16_t message_id, double rate_hz, uint8_t component_id)
×
1165
{
1166
    MavlinkCommandSender::CommandLong command =
×
1167
        make_command_msg_rate(message_id, rate_hz, component_id);
×
1168
    return send_command(command);
×
1169
}
1170

1171
void SystemImpl::set_msg_rate_async(
1✔
1172
    uint16_t message_id,
1173
    double rate_hz,
1174
    const CommandResultCallback& callback,
1175
    uint8_t component_id)
1176
{
1177
    MavlinkCommandSender::CommandLong command =
1✔
1178
        make_command_msg_rate(message_id, rate_hz, component_id);
1✔
1179
    send_command_async(command, callback);
1✔
1180
}
1✔
1181

1182
MavlinkCommandSender::CommandLong
1183
SystemImpl::make_command_msg_rate(uint16_t message_id, double rate_hz, uint8_t component_id)
1✔
1184
{
1185
    MavlinkCommandSender::CommandLong command{};
1✔
1186

1187
    // 0 to request default rate, -1 to stop stream
1188

1189
    float interval_us = 0.0f;
1✔
1190

1191
    if (rate_hz > 0) {
1✔
1192
        interval_us = 1e6f / static_cast<float>(rate_hz);
1✔
1193
    } else if (rate_hz < 0) {
×
1194
        interval_us = -1.0f;
×
1195
    }
1196

1197
    command.command = MAV_CMD_SET_MESSAGE_INTERVAL;
1✔
1198
    command.params.maybe_param1 = static_cast<float>(message_id);
1✔
1199
    command.params.maybe_param2 = interval_us;
1✔
1200
    command.target_component_id = component_id;
1✔
1201

1202
    return command;
1✔
1203
}
1204

1205
void SystemImpl::register_plugin(PluginImplBase* plugin_impl)
7✔
1206
{
1207
    assert(plugin_impl);
7✔
1208

1209
    plugin_impl->init();
7✔
1210

1211
    {
1212
        std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
14✔
1213
        _plugin_impls.push_back(plugin_impl);
7✔
1214
    }
1215

1216
    // If we're connected already, let's enable it straightaway.
1217
    if (_connected) {
7✔
1218
        plugin_impl->enable();
7✔
1219
    }
1220
}
7✔
1221

1222
void SystemImpl::unregister_plugin(PluginImplBase* plugin_impl)
7✔
1223
{
1224
    assert(plugin_impl);
7✔
1225

1226
    plugin_impl->disable();
7✔
1227
    plugin_impl->deinit();
7✔
1228

1229
    // Remove first, so it won't get enabled/disabled anymore.
1230
    {
1231
        std::lock_guard<std::mutex> lock(_plugin_impls_mutex);
14✔
1232
        auto found = std::find(_plugin_impls.begin(), _plugin_impls.end(), plugin_impl);
7✔
1233
        if (found != _plugin_impls.end()) {
7✔
1234
            _plugin_impls.erase(found);
7✔
1235
        }
1236
    }
1237
}
7✔
1238

1239
void SystemImpl::call_user_callback_located(
17✔
1240
    const std::string& filename, const int linenumber, const std::function<void()>& func)
1241
{
1242
    _parent.call_user_callback_located(filename, linenumber, func);
17✔
1243
}
17✔
1244

1245
void SystemImpl::param_changed(const std::string& name)
×
1246
{
1247
    std::lock_guard<std::mutex> lock(_param_changed_callbacks_mutex);
×
1248

1249
    for (auto& callback : _param_changed_callbacks) {
×
1250
        callback.second(name);
×
1251
    }
1252
}
×
1253

1254
void SystemImpl::register_param_changed_handler(
1✔
1255
    const ParamChangedCallback& callback, const void* cookie)
1256
{
1257
    if (!callback) {
1✔
1258
        LogErr() << "No callback for param_changed_handler supplied.";
×
1259
        return;
×
1260
    }
1261

1262
    if (!cookie) {
1✔
1263
        LogErr() << "No callback for param_changed_handler supplied.";
×
1264
        return;
×
1265
    }
1266

1267
    std::lock_guard<std::mutex> lock(_param_changed_callbacks_mutex);
2✔
1268

1269
    _param_changed_callbacks[cookie] = callback;
1✔
1270
}
1271

1272
void SystemImpl::unregister_param_changed_handler(const void* cookie)
1✔
1273
{
1274
    std::lock_guard<std::mutex> lock(_param_changed_callbacks_mutex);
1✔
1275

1276
    auto it = _param_changed_callbacks.find(cookie);
1✔
1277
    if (it == _param_changed_callbacks.end()) {
1✔
1278
        LogWarn() << "param_changed_handler for cookie not found";
×
1279
        return;
×
1280
    }
1281
    _param_changed_callbacks.erase(it);
1✔
1282
}
1283

1284
Time& SystemImpl::get_time()
36✔
1285
{
1286
    return _parent.time;
36✔
1287
}
1288

1289
void SystemImpl::subscribe_param_float(
×
1290
    const std::string& name,
1291
    const MAVLinkParameters::ParamFloatChangedCallback& callback,
1292
    const void* cookie)
1293
{
1294
    _params.subscribe_param_float_changed(name, callback, cookie);
×
1295
}
×
1296

1297
void SystemImpl::subscribe_param_int(
×
1298
    const std::string& name,
1299
    const MAVLinkParameters::ParamIntChangedCallback& callback,
1300
    const void* cookie)
1301
{
1302
    _params.subscribe_param_int_changed(name, callback, cookie);
×
1303
}
×
1304
void SystemImpl::subscribe_param_custom(
×
1305
    const std::string& name,
1306
    const MAVLinkParameters::ParamCustomChangedCallback& callback,
1307
    const void* cookie)
1308
{
1309
    _params.subscribe_param_custom_changed(name, callback, cookie);
×
1310
}
×
1311

1312
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc