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

mavlink / MAVSDK / 4065365825

pending completion
4065365825

push

github

GitHub
Winch and Gripper plugins (#1958)

529 of 529 new or added lines in 5 files covered. (100.0%)

7387 of 24193 relevant lines covered (30.53%)

21.47 hits per line

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

40.8
/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
14✔
106
{
107
    return _connected;
14✔
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)
21✔
150
{
151
    _parent.timeout_handler.remove(cookie);
21✔
152
}
21✔
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) {
732✔
289
        _params.do_work();
718✔
290
        _command_sender.do_work();
717✔
291
        _timesync.do_work();
718✔
292
        _mission_transfer.do_work();
718✔
293

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

301
        if (_connected) {
715✔
302
            // Work fairly fast if we're connected.
303
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
708✔
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)
313✔
370
{
371
    if (component_id == 0) {
313✔
372
        return;
7✔
373
    }
374

375
    auto res_pair = _components.insert(component_id);
306✔
376
    if (res_pair.second) {
306✔
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
57✔
640
{
641
    return _parent.get_own_system_id();
57✔
642
}
643

644
uint8_t SystemImpl::get_own_component_id() const
57✔
645
{
646
    return _parent.get_own_component_id();
57✔
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::Unknown:
×
914
        case FlightMode::Ready:
915
        case FlightMode::Takeoff:
916
        case FlightMode::Land:
917
        case FlightMode::Offboard:
918
        case FlightMode::Altctl:
919
        case FlightMode::Posctl:
920
        case FlightMode::Rattitude:
921
        case FlightMode::Stabilized:
922
        default:
923
            return ardupilot::RoverMode::Unknown;
×
924
    }
925
}
926
ardupilot::CopterMode SystemImpl::flight_mode_to_ardupilot_copter_mode(FlightMode flight_mode)
×
927
{
928
    switch (flight_mode) {
×
929
        case FlightMode::Mission:
×
930
            return ardupilot::CopterMode::Auto;
×
931
        case FlightMode::Acro:
×
932
            return ardupilot::CopterMode::Acro;
×
933
        case FlightMode::Hold:
×
934
            return ardupilot::CopterMode::Loiter;
×
935
        case FlightMode::ReturnToLaunch:
×
936
            return ardupilot::CopterMode::Rtl;
×
937
        case FlightMode::Land:
×
938
            return ardupilot::CopterMode::Land;
×
939
        case FlightMode::Manual:
×
940
            return ardupilot::CopterMode::Stabilize;
×
941
        case FlightMode::FollowMe:
×
942
            return ardupilot::CopterMode::Follow;
×
943
        case FlightMode::Offboard:
×
944
            return ardupilot::CopterMode::Guided;
×
945
        case FlightMode::Altctl:
×
946
            return ardupilot::CopterMode::AltHold;
×
947
        case FlightMode::Posctl:
×
948
            return ardupilot::CopterMode::PosHold;
×
949
        case FlightMode::Stabilized:
×
950
            return ardupilot::CopterMode::Stabilize;
×
951
        case FlightMode::Unknown:
×
952
        case FlightMode::Ready:
953
        case FlightMode::Takeoff:
954
        case FlightMode::Rattitude:
955
        default:
956
            return ardupilot::CopterMode::Unknown;
×
957
    }
958
}
959
ardupilot::PlaneMode SystemImpl::flight_mode_to_ardupilot_plane_mode(FlightMode flight_mode)
×
960
{
961
    switch (flight_mode) {
×
962
        case FlightMode::Mission:
×
963
            return ardupilot::PlaneMode::Auto;
×
964
        case FlightMode::Acro:
×
965
            return ardupilot::PlaneMode::Acro;
×
966
        case FlightMode::Hold:
×
967
            return ardupilot::PlaneMode::Loiter;
×
968
        case FlightMode::ReturnToLaunch:
×
969
            return ardupilot::PlaneMode::Rtl;
×
970
        case FlightMode::Manual:
×
971
            return ardupilot::PlaneMode::Manual;
×
972
        case FlightMode::FBWA:
×
973
            return ardupilot::PlaneMode::Fbwa;
×
974
        case FlightMode::Stabilized:
×
975
            return ardupilot::PlaneMode::Stabilize;
×
976
        case FlightMode::Unknown:
×
977
            return ardupilot::PlaneMode::Unknown;
×
978
        default:
×
979
            return ardupilot::PlaneMode::Unknown;
×
980
    }
981
}
982

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

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

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

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

1042
    MavlinkCommandSender::CommandLong command{};
×
1043

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1188
    float interval_us = 0.0f;
1✔
1189

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

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

1201
    return command;
1✔
1202
}
1203

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

1208
    plugin_impl->init();
7✔
1209

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1311
} // 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