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

mavlink / MAVSDK / 16613462903

30 Jul 2025 04:27AM UTC coverage: 44.748% (-1.6%) from 46.31%
16613462903

Pull #2626

github

web-flow
Merge 7fbdf7ed2 into c0a7c02a0
Pull Request #2626: core: flush after each Log* line

191 of 302 new or added lines in 23 files covered. (63.25%)

397 existing lines in 18 files now uncovered.

15494 of 34625 relevant lines covered (44.75%)

322.5 hits per line

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

56.93
/src/mavsdk/core/mavsdk_impl.cpp
1
#include "mavsdk_impl.h"
2

3
#include <algorithm>
4
#include <mutex>
5
#include <tcp_server_connection.h>
6

7
#include "connection.h"
8
#include "log.h"
9
#include "tcp_client_connection.h"
10
#include "tcp_server_connection.h"
11
#include "udp_connection.h"
12
#include "system.h"
13
#include "system_impl.h"
14
#include "serial_connection.h"
15
#include "version.h"
16
#include "server_component_impl.h"
17
#include "overloaded.h"
18
#include "mavlink_channels.h"
19
#include "callback_list.tpp"
20
#include "hostname_to_ip.h"
21
#include "embedded_mavlink_xml.h"
22
#include <mav/MessageSet.h>
23

24
namespace mavsdk {
25

26
template class CallbackList<>;
27

28
MavsdkImpl::MavsdkImpl(const Mavsdk::Configuration& configuration) :
87✔
29
    timeout_handler(time),
87✔
30
    call_every_handler(time)
174✔
31
{
32
    LogInfo() << "MAVSDK version: " << mavsdk_version;
87✔
33

34
    if (const char* env_p = std::getenv("MAVSDK_CALLBACK_DEBUGGING")) {
87✔
35
        if (std::string(env_p) == "1") {
×
36
            LogDebug() << "Callback debugging is on.";
×
37
            _callback_debugging = true;
×
38
        }
39
    }
40

41
    if (const char* env_p = std::getenv("MAVSDK_MESSAGE_DEBUGGING")) {
87✔
42
        if (std::string(env_p) == "1") {
×
43
            LogDebug() << "Message debugging is on.";
×
44
            _message_logging_on = true;
×
45
        }
46
    }
47

48
    if (const char* env_p = std::getenv("MAVSDK_SYSTEM_DEBUGGING")) {
87✔
49
        if (std::string(env_p) == "1") {
×
50
            LogDebug() << "System debugging is on.";
×
51
            _system_debugging = true;
×
52
        }
53
    }
54

55
    set_configuration(configuration);
87✔
56

57
    // Initialize MessageSet with embedded XML content in dependency order
58
    _message_set = std::make_unique<mav::MessageSet>();
87✔
59
    //_message_set->addFromXMLString(mav_embedded::MINIMAL_XML);
60
    //_message_set->addFromXMLString(mav_embedded::STANDARD_XML);
61
    //_message_set->addFromXMLString(mav_embedded::COMMON_XML);
62
    //_message_set->addFromXMLString(mav_embedded::ARDUPILOTMEGA_XML);
63

64
    // Start the user callback thread first, so it is ready for anything generated by
65
    // the work thread.
66

67
    _process_user_callbacks_thread =
174✔
68
        new std::thread(&MavsdkImpl::process_user_callbacks_thread, this);
87✔
69

70
    _work_thread = new std::thread(&MavsdkImpl::work_thread, this);
87✔
71
}
87✔
72

73
MavsdkImpl::~MavsdkImpl()
87✔
74
{
75
    {
76
        std::lock_guard<std::mutex> lock(_heartbeat_mutex);
87✔
77
        call_every_handler.remove(_heartbeat_send_cookie);
87✔
78
    }
87✔
79

80
    _should_exit = true;
87✔
81

82
    // Stop work first because we don't want to trigger anything that would
83
    // potentially want to call into user code.
84

85
    if (_work_thread != nullptr) {
87✔
86
        _work_thread->join();
87✔
87
        delete _work_thread;
87✔
88
        _work_thread = nullptr;
87✔
89
    }
90

91
    if (_process_user_callbacks_thread != nullptr) {
87✔
92
        _user_callback_queue.stop();
87✔
93
        _process_user_callbacks_thread->join();
87✔
94
        delete _process_user_callbacks_thread;
87✔
95
        _process_user_callbacks_thread = nullptr;
87✔
96
    }
97

98
    std::lock_guard lock(_mutex);
87✔
99

100
    _systems.clear();
87✔
101
    _connections.clear();
87✔
102
}
174✔
103

104
std::string MavsdkImpl::version()
1✔
105
{
106
    static unsigned version_counter = 0;
107

108
    ++version_counter;
1✔
109

110
    switch (version_counter) {
1✔
111
        case 10:
×
112
            return "You were wondering about the name of this library?";
×
113
        case 11:
×
114
            return "Let's look at the history:";
×
115
        case 12:
×
116
            return "DroneLink";
×
117
        case 13:
×
118
            return "DroneCore";
×
119
        case 14:
×
120
            return "DronecodeSDK";
×
121
        case 15:
×
122
            return "MAVSDK";
×
123
        case 16:
×
124
            return "And that's it...";
×
125
        case 17:
×
126
            return "At least for now ¯\\_(ツ)_/¯.";
×
127
        default:
1✔
128
            return mavsdk_version;
1✔
129
    }
130
}
131

132
std::vector<std::shared_ptr<System>> MavsdkImpl::systems() const
86✔
133
{
134
    std::vector<std::shared_ptr<System>> systems_result{};
86✔
135

136
    std::lock_guard lock(_mutex);
86✔
137
    for (auto& system : _systems) {
129✔
138
        // We ignore the 0 entry because it's just a null system.
139
        // It's only created because the older, deprecated API needs a
140
        // reference.
141
        if (system.first == 0) {
43✔
142
            continue;
×
143
        }
144
        systems_result.push_back(system.second);
43✔
145
    }
146

147
    return systems_result;
86✔
148
}
86✔
149

150
std::optional<std::shared_ptr<System>> MavsdkImpl::first_autopilot(double timeout_s)
33✔
151
{
152
    {
153
        std::lock_guard lock(_mutex);
33✔
154
        for (auto system : _systems) {
33✔
155
            if (system.second->is_connected() && system.second->has_autopilot()) {
×
156
                return system.second;
×
157
            }
158
        }
×
159
    }
33✔
160

161
    if (timeout_s == 0.0) {
33✔
162
        // Don't wait at all.
163
        return {};
×
164
    }
165

166
    auto prom = std::promise<std::shared_ptr<System>>();
33✔
167

168
    std::once_flag flag;
33✔
169
    auto handle = subscribe_on_new_system([this, &prom, &flag]() {
66✔
170
        // Check all systems, not just the first one
171
        auto all_systems = systems();
33✔
172
        for (auto& system : all_systems) {
33✔
173
            if (system->is_connected() && system->has_autopilot()) {
33✔
174
                std::call_once(flag, [&prom, &system]() { prom.set_value(system); });
66✔
175
                break;
33✔
176
            }
177
        }
178
    });
66✔
179

180
    auto fut = prom.get_future();
33✔
181

182
    if (timeout_s > 0.0) {
33✔
183
        if (fut.wait_for(std::chrono::milliseconds(int64_t(timeout_s * 1e3))) ==
33✔
184
            std::future_status::ready) {
185
            unsubscribe_on_new_system(handle);
33✔
186
            return fut.get();
33✔
187

188
        } else {
189
            unsubscribe_on_new_system(handle);
×
190
            return std::nullopt;
×
191
        }
192
    } else {
193
        fut.wait();
×
194
        unsubscribe_on_new_system(handle);
×
195
        return std::optional(fut.get());
×
196
    }
197
}
33✔
198

199
std::shared_ptr<ServerComponent> MavsdkImpl::server_component(unsigned instance)
47✔
200
{
201
    std::lock_guard lock(_mutex);
47✔
202

203
    auto component_type = _configuration.get_component_type();
47✔
204
    switch (component_type) {
47✔
205
        case ComponentType::Autopilot:
47✔
206
        case ComponentType::GroundStation:
207
        case ComponentType::CompanionComputer:
208
        case ComponentType::Camera:
209
        case ComponentType::Gimbal:
210
        case ComponentType::RemoteId:
211
        case ComponentType::Custom:
212
            return server_component_by_type(component_type, instance);
47✔
213
        default:
×
214
            LogErr() << "Unknown component type";
×
215
            return {};
×
216
    }
217
}
47✔
218

219
std::shared_ptr<ServerComponent>
220
MavsdkImpl::server_component_by_type(ComponentType server_component_type, unsigned instance)
47✔
221
{
222
    switch (server_component_type) {
47✔
223
        case ComponentType::Autopilot:
33✔
224
            if (instance == 0) {
33✔
225
                return server_component_by_id(MAV_COMP_ID_AUTOPILOT1);
33✔
226
            } else {
227
                LogErr() << "Only autopilot instance 0 is valid";
×
228
                return {};
×
229
            }
230

231
        case ComponentType::GroundStation:
×
232
            if (instance == 0) {
×
233
                return server_component_by_id(MAV_COMP_ID_MISSIONPLANNER);
×
234
            } else {
235
                LogErr() << "Only one ground station supported at this time";
×
236
                return {};
×
237
            }
238

239
        case ComponentType::CompanionComputer:
1✔
240
            if (instance == 0) {
1✔
241
                return server_component_by_id(MAV_COMP_ID_ONBOARD_COMPUTER);
1✔
242
            } else if (instance == 1) {
×
243
                return server_component_by_id(MAV_COMP_ID_ONBOARD_COMPUTER2);
×
244
            } else if (instance == 2) {
×
245
                return server_component_by_id(MAV_COMP_ID_ONBOARD_COMPUTER3);
×
246
            } else if (instance == 3) {
×
247
                return server_component_by_id(MAV_COMP_ID_ONBOARD_COMPUTER4);
×
248
            } else {
249
                LogErr() << "Only companion computer 0..3 are supported";
×
250
                return {};
×
251
            }
252

253
        case ComponentType::Camera:
13✔
254
            if (instance == 0) {
13✔
255
                return server_component_by_id(MAV_COMP_ID_CAMERA);
13✔
256
            } else if (instance == 1) {
×
257
                return server_component_by_id(MAV_COMP_ID_CAMERA2);
×
258
            } else if (instance == 2) {
×
259
                return server_component_by_id(MAV_COMP_ID_CAMERA3);
×
260
            } else if (instance == 3) {
×
261
                return server_component_by_id(MAV_COMP_ID_CAMERA4);
×
262
            } else if (instance == 4) {
×
263
                return server_component_by_id(MAV_COMP_ID_CAMERA5);
×
264
            } else if (instance == 5) {
×
265
                return server_component_by_id(MAV_COMP_ID_CAMERA6);
×
266
            } else {
267
                LogErr() << "Only camera 0..5 are supported";
×
268
                return {};
×
269
            }
270

271
        default:
×
272
            LogErr() << "Unknown server component type";
×
273
            return {};
×
274
    }
275
}
276

277
std::shared_ptr<ServerComponent> MavsdkImpl::server_component_by_id(uint8_t component_id)
47✔
278
{
279
    if (component_id == 0) {
47✔
280
        LogErr() << "Server component with component ID 0 not allowed";
×
281
        return nullptr;
×
282
    }
283

284
    std::lock_guard lock(_server_components_mutex);
47✔
285

286
    return server_component_by_id_with_lock(component_id);
47✔
287
}
47✔
288

289
std::shared_ptr<ServerComponent> MavsdkImpl::server_component_by_id_with_lock(uint8_t component_id)
176✔
290
{
291
    for (auto& it : _server_components) {
177✔
292
        if (it.first == component_id) {
89✔
293
            if (it.second != nullptr) {
88✔
294
                return it.second;
88✔
295
            } else {
296
                it.second = std::make_shared<ServerComponent>(*this, component_id);
×
297
            }
298
        }
299
    }
300

301
    _server_components.emplace_back(std::pair<uint8_t, std::shared_ptr<ServerComponent>>(
176✔
302
        component_id, std::make_shared<ServerComponent>(*this, component_id)));
176✔
303

304
    return _server_components.back().second;
88✔
305
}
306

307
void MavsdkImpl::forward_message(mavlink_message_t& message, Connection* connection)
×
308
{
309
    // Forward_message Function implementing Mavlink routing rules.
310
    // See https://mavlink.io/en/guide/routing.html
311

312
    bool forward_heartbeats_enabled = true;
×
313
    const uint8_t target_system_id = get_target_system_id(message);
×
314
    const uint8_t target_component_id = get_target_component_id(message);
×
315

316
    // If it's a message only for us, we keep it, otherwise, we forward it.
317
    const bool targeted_only_at_us =
318
        (target_system_id == get_own_system_id() && target_component_id == get_own_component_id());
×
319

320
    // We don't forward heartbeats unless it's specifically enabled.
321
    const bool heartbeat_check_ok =
×
322
        (message.msgid != MAVLINK_MSG_ID_HEARTBEAT || forward_heartbeats_enabled);
×
323

324
    if (!targeted_only_at_us && heartbeat_check_ok) {
×
325
        unsigned successful_emissions = 0;
×
326
        for (auto& entry : _connections) {
×
327
            // Check whether the connection is not the one from which we received the message.
328
            // And also check if the connection was set to forward messages.
329
            if (entry.connection.get() == connection ||
×
330
                !entry.connection->should_forward_messages()) {
×
331
                continue;
×
332
            }
333
            auto result = (*entry.connection).send_message(message);
×
334
            if (result.first) {
×
335
                successful_emissions++;
×
336
            } else {
337
                _connections_errors_subscriptions.queue(
×
338
                    Mavsdk::ConnectionError{result.second, entry.handle},
×
339
                    [this](const auto& func) { call_user_callback(func); });
×
340
            }
341
        }
×
342
        if (successful_emissions == 0) {
×
343
            LogErr() << "Message forwarding failed";
×
344
        }
345
    }
346
}
×
347

348
void MavsdkImpl::receive_message(mavlink_message_t& message, Connection* connection)
2,251✔
349
{
350
    {
351
        std::lock_guard lock(_received_messages_mutex);
2,251✔
352
        _received_messages.emplace(ReceivedMessage{std::move(message), connection});
2,257✔
353
    }
2,259✔
354
    _received_messages_cv.notify_one();
2,258✔
355
}
2,259✔
356

UNCOV
357
void MavsdkImpl::receive_libmav_message(const LibmavMessage& message, Connection* connection)
×
358
{
359
    {
UNCOV
360
        std::lock_guard lock(_received_libmav_messages_mutex);
×
UNCOV
361
        _received_libmav_messages.emplace(ReceivedLibmavMessage{message, connection});
×
UNCOV
362
    }
×
UNCOV
363
    _received_libmav_messages_cv.notify_one();
×
UNCOV
364
}
×
365

366
void MavsdkImpl::process_messages()
24,026✔
367
{
368
    std::lock_guard lock(_received_messages_mutex);
24,026✔
369
    while (!_received_messages.empty()) {
26,055✔
370
        auto message_copied = _received_messages.front();
2,248✔
371
        process_message(message_copied.message, message_copied.connection_ptr);
2,248✔
372
        _received_messages.pop();
2,249✔
373
    }
374
}
23,876✔
375

376
void MavsdkImpl::process_libmav_messages()
23,889✔
377
{
378
    std::lock_guard lock(_received_libmav_messages_mutex);
23,889✔
379
    while (!_received_libmav_messages.empty()) {
23,712✔
UNCOV
380
        auto message_copied = _received_libmav_messages.front();
×
UNCOV
381
        process_libmav_message(message_copied.message, message_copied.connection_ptr);
×
UNCOV
382
        _received_libmav_messages.pop();
×
UNCOV
383
    }
×
384
}
23,708✔
385

386
void MavsdkImpl::process_message(mavlink_message_t& message, Connection* connection)
2,249✔
387
{
388
    // Assumes _received_messages_mutex
389

390
    if (_message_logging_on) {
2,249✔
391
        LogDebug() << "Processing message " << message.msgid << " from "
×
392
                   << static_cast<int>(message.sysid) << "/" << static_cast<int>(message.compid);
×
393
    }
394

395
    if (_should_exit) {
2,249✔
396
        // If we're meant to clean up, let's not try to acquire any more locks but bail.
397
        return;
×
398
    }
399

400
    {
401
        std::lock_guard lock(_mutex);
2,249✔
402

403
        // This is a low level interface where incoming messages can be tampered
404
        // with or even dropped.
405
        {
406
            bool keep = true;
2,248✔
407
            {
408
                std::lock_guard<std::mutex> intercept_lock(_intercept_callbacks_mutex);
2,248✔
409
                if (_intercept_incoming_messages_callback != nullptr) {
2,248✔
410
                    keep = _intercept_incoming_messages_callback(message);
243✔
411
                }
412
            }
2,247✔
413

414
            if (!keep) {
2,247✔
415
                LogDebug() << "Dropped incoming message: " << int(message.msgid);
35✔
416
                return;
35✔
417
            }
418
        }
419

420
        if (_should_exit) {
2,212✔
421
            // If we're meant to clean up, let's not try to acquire any more locks but bail.
422
            return;
×
423
        }
424

425
        /** @note: Forward message if option is enabled and multiple interfaces are connected.
426
         *  Performs message forwarding checks for every messages if message forwarding
427
         *  is enabled on at least one connection, and in case of a single forwarding connection,
428
         *  we check that it is not the one which received the current message.
429
         *
430
         * Conditions:
431
         * 1. At least 2 connections.
432
         * 2. At least 1 forwarding connection.
433
         * 3. At least 2 forwarding connections or current connection is not forwarding.
434
         */
435

436
        if (_connections.size() > 1 && mavsdk::Connection::forwarding_connections_count() > 0 &&
2,212✔
437
            (mavsdk::Connection::forwarding_connections_count() > 1 ||
×
UNCOV
438
             !connection->should_forward_messages())) {
×
439
            if (_message_logging_on) {
×
440
                LogDebug() << "Forwarding message " << message.msgid << " from "
×
441
                           << static_cast<int>(message.sysid) << "/"
×
442
                           << static_cast<int>(message.compid);
×
443
            }
444
            forward_message(message, connection);
×
445
        }
446

447
        // Don't ever create a system with sysid 0.
448
        if (message.sysid == 0) {
2,209✔
449
            if (_message_logging_on) {
×
450
                LogDebug() << "Ignoring message with sysid == 0";
×
451
            }
452
            return;
×
453
        }
454

455
        // Filter out messages by QGroundControl, however, only do that if MAVSDK
456
        // is also implementing a ground station and not if it is used in another
457
        // configuration, e.g. on a companion.
458
        //
459
        // This is a workaround because PX4 started forwarding messages between
460
        // mavlink instances which leads to existing implementations (including
461
        // examples and integration tests) to connect to QGroundControl by accident
462
        // instead of PX4 because the check `has_autopilot()` is not used.
463

464
        if (_configuration.get_component_type() == ComponentType::GroundStation &&
2,209✔
465
            message.sysid == 255 && message.compid == MAV_COMP_ID_MISSIONPLANNER) {
2,212✔
466
            if (_message_logging_on) {
×
467
                LogDebug() << "Ignoring messages from QGC as we are also a ground station";
×
468
            }
469
            return;
×
470
        }
471

472
        bool found_system = false;
2,212✔
473
        for (auto& system : _systems) {
2,216✔
474
            if (system.first == message.sysid) {
2,127✔
475
                system.second->system_impl()->add_new_component(message.compid);
2,123✔
476
                found_system = true;
2,122✔
477
                break;
2,122✔
478
            }
479
        }
480

481
        if (!found_system) {
2,205✔
482
            if (_system_debugging) {
86✔
483
                LogWarn() << "Create new system/component " << (int)message.sysid << "/"
×
484
                          << (int)message.compid;
×
485
                LogWarn() << "From message " << (int)message.msgid << " with len "
×
486
                          << (int)message.len;
×
487
                std::string bytes = "";
×
488
                for (unsigned i = 0; i < 12 + message.len; ++i) {
×
489
                    bytes += std::to_string(reinterpret_cast<uint8_t*>(&message)[i]) + ' ';
×
490
                }
491
                LogWarn() << "Bytes: " << bytes;
×
492
            }
×
493
            make_system_with_component(message.sysid, message.compid);
86✔
494

495
            // We now better talk back.
496
            start_sending_heartbeats();
86✔
497
        }
498

499
        if (_should_exit) {
2,205✔
500
            // Don't try to call at() if systems have already been destroyed
501
            // in destructor.
502
            return;
×
503
        }
504
    }
2,242✔
505

506
    mavlink_message_handler.process_message(message);
2,211✔
507

508
    for (auto& system : _systems) {
2,212✔
509
        if (system.first == message.sysid) {
2,212✔
510
            system.second->system_impl()->process_mavlink_message(message);
2,212✔
511
            break;
2,214✔
512
        }
513
    }
514
}
515

UNCOV
516
void MavsdkImpl::process_libmav_message(const LibmavMessage& message, Connection* /* connection */)
×
517
{
518
    // Assumes _received_libmav_messages_mutex
519

UNCOV
520
    if (_message_logging_on) {
×
521
        LogDebug() << "MavsdkImpl::process_libmav_message: " << message.message_name << " from "
×
522
                   << static_cast<int>(message.system_id) << "/"
×
523
                   << static_cast<int>(message.component_id);
×
524
    }
525

UNCOV
526
    if (_message_logging_on) {
×
527
        LogDebug() << "Processing libmav message " << message.message_name << " from "
×
528
                   << static_cast<int>(message.system_id) << "/"
×
529
                   << static_cast<int>(message.component_id);
×
530
    }
531

UNCOV
532
    if (_should_exit) {
×
533
        // If we're meant to clean up, let's not try to acquire any more locks but bail.
534
        return;
×
535
    }
536

537
    {
UNCOV
538
        std::lock_guard lock(_mutex);
×
539

540
        // Don't ever create a system with sysid 0.
UNCOV
541
        if (message.system_id == 0) {
×
542
            if (_message_logging_on) {
×
543
                LogDebug() << "Ignoring libmav message with sysid == 0";
×
544
            }
545
            return;
×
546
        }
547

548
        // Filter out QGroundControl messages similar to regular mavlink processing
UNCOV
549
        if (_configuration.get_component_type() == ComponentType::GroundStation &&
×
UNCOV
550
            message.system_id == 255 && message.component_id == MAV_COMP_ID_MISSIONPLANNER) {
×
551
            if (_message_logging_on) {
×
552
                LogDebug() << "Ignoring libmav messages from QGC as we are also a ground station";
×
553
            }
554
            return;
×
555
        }
556

UNCOV
557
        bool found_system = false;
×
UNCOV
558
        for (auto& system : _systems) {
×
UNCOV
559
            if (system.first == message.system_id) {
×
UNCOV
560
                system.second->system_impl()->add_new_component(message.component_id);
×
UNCOV
561
                found_system = true;
×
UNCOV
562
                break;
×
563
            }
564
        }
565

UNCOV
566
        if (!found_system) {
×
UNCOV
567
            if (_system_debugging) {
×
568
                LogWarn() << "Create new system/component from libmav " << (int)message.system_id
×
569
                          << "/" << (int)message.component_id;
×
570
            }
UNCOV
571
            make_system_with_component(message.system_id, message.component_id);
×
572

573
            // We now better talk back.
UNCOV
574
            start_sending_heartbeats();
×
575
        }
576

UNCOV
577
        if (_should_exit) {
×
578
            // Don't try to call at() if systems have already been destroyed
579
            // in destructor.
580
            return;
×
581
        }
UNCOV
582
    }
×
583

584
    // Distribute libmav message to systems for libmav-specific handling
UNCOV
585
    bool found_system = false;
×
UNCOV
586
    for (auto& system : _systems) {
×
UNCOV
587
        if (system.first == message.system_id) {
×
UNCOV
588
            if (_message_logging_on) {
×
589
                LogDebug() << "Distributing libmav message " << message.message_name
×
590
                           << " to SystemImpl for system " << system.first;
×
591
            }
UNCOV
592
            system.second->system_impl()->process_libmav_message(message);
×
UNCOV
593
            found_system = true;
×
594
            // Don't break - distribute to all matching system instances
595
        }
596
    }
597

UNCOV
598
    if (!found_system) {
×
599
        LogWarn() << "No system found for libmav message " << message.message_name
×
600
                  << " from system " << message.system_id;
×
601
    }
602
}
603

604
bool MavsdkImpl::send_message(mavlink_message_t& message)
2,386✔
605
{
606
    // Create a copy of the message to avoid reference issues
607
    mavlink_message_t message_copy = message;
2,386✔
608

609
    {
610
        std::lock_guard lock(_messages_to_send_mutex);
2,386✔
611
        _messages_to_send.push(std::move(message_copy));
2,387✔
612
    }
2,385✔
613

614
    // For heartbeat messages, we want to process them immediately to speed up system discovery
615
    if (message.msgid == MAVLINK_MSG_ID_HEARTBEAT) {
2,387✔
616
        // Trigger message processing in the work thread
617
        // This is a hint to process messages sooner, but doesn't block
618
        std::this_thread::yield();
303✔
619
    }
620

621
    return true;
2,389✔
622
}
623

624
void MavsdkImpl::deliver_messages()
26,379✔
625
{
626
    // Process messages one at a time to avoid holding the mutex while delivering
627
    while (true) {
628
        mavlink_message_t message;
26,379✔
629
        {
630
            std::lock_guard lock(_messages_to_send_mutex);
26,210✔
631
            if (_messages_to_send.empty()) {
25,920✔
632
                break;
23,571✔
633
            }
634
            message = _messages_to_send.front();
2,388✔
635
            _messages_to_send.pop();
2,388✔
636
        }
25,958✔
637
        deliver_message(message);
2,380✔
638
    }
2,387✔
639
}
23,310✔
640

641
void MavsdkImpl::deliver_message(mavlink_message_t& message)
2,388✔
642
{
643
    if (_message_logging_on) {
2,388✔
644
        LogDebug() << "Sending message " << message.msgid << " from "
×
645
                   << static_cast<int>(message.sysid) << "/" << static_cast<int>(message.compid)
×
646
                   << " to " << static_cast<int>(get_target_system_id(message)) << "/"
×
647
                   << static_cast<int>(get_target_component_id(message));
×
648
    }
649

650
    // This is a low level interface where outgoing messages can be tampered
651
    // with or even dropped.
652
    bool keep = true;
2,388✔
653
    {
654
        std::lock_guard<std::mutex> lock(_intercept_callbacks_mutex);
2,388✔
655
        if (_intercept_outgoing_messages_callback != nullptr) {
2,388✔
656
            keep = _intercept_outgoing_messages_callback(message);
220✔
657
        }
658
    }
2,387✔
659

660
    if (!keep) {
2,387✔
661
        // We fake that everything was sent as instructed because
662
        // a potential loss would happen later, and we would not be informed
663
        // about it.
664
        LogDebug() << "Dropped outgoing message: " << int(message.msgid);
86✔
665
        return;
86✔
666
    }
667

668
    std::lock_guard lock(_mutex);
2,301✔
669

670
    if (_connections.empty()) {
2,302✔
671
        // We obviously can't send any messages without a connection added, so
672
        // we silently ignore this.
673
        return;
43✔
674
    }
675

676
    uint8_t successful_emissions = 0;
2,259✔
677
    for (auto& _connection : _connections) {
4,518✔
678
        const uint8_t target_system_id = get_target_system_id(message);
2,258✔
679

680
        if (target_system_id != 0 && !(*_connection.connection).has_system_id(target_system_id)) {
2,257✔
681
            continue;
×
682
        }
683
        const auto result = (*_connection.connection).send_message(message);
2,253✔
684
        if (result.first) {
2,259✔
685
            successful_emissions++;
2,259✔
686
        } else {
687
            _connections_errors_subscriptions.queue(
×
688
                Mavsdk::ConnectionError{result.second, _connection.handle},
×
689
                [this](const auto& func) { call_user_callback(func); });
×
690
        }
691
    }
2,259✔
692

693
    if (successful_emissions == 0) {
2,258✔
694
        LogErr() << "Sending message failed";
×
695
    }
696
}
2,302✔
697

698
std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_any_connection(
86✔
699
    const std::string& connection_url, ForwardingOption forwarding_option)
700
{
701
    CliArg cli_arg;
86✔
702
    if (!cli_arg.parse(connection_url)) {
86✔
703
        return {ConnectionResult::ConnectionUrlInvalid, Mavsdk::ConnectionHandle{}};
×
704
    }
705

706
    return std::visit(
86✔
707
        overloaded{
172✔
708
            [](std::monostate) {
×
709
                // Should not happen anyway.
710
                return std::pair<ConnectionResult, Mavsdk::ConnectionHandle>{
×
711
                    ConnectionResult::ConnectionUrlInvalid, Mavsdk::ConnectionHandle()};
×
712
            },
713
            [this, forwarding_option](const CliArg::Udp& udp) {
84✔
714
                return add_udp_connection(udp, forwarding_option);
84✔
715
            },
716
            [this, forwarding_option](const CliArg::Tcp& tcp) {
2✔
717
                return add_tcp_connection(tcp, forwarding_option);
2✔
718
            },
719
            [this, forwarding_option](const CliArg::Serial& serial) {
×
720
                return add_serial_connection(
×
721
                    serial.path, serial.baudrate, serial.flow_control_enabled, forwarding_option);
×
722
            }},
723
        cli_arg.protocol);
86✔
724
}
86✔
725

726
std::pair<ConnectionResult, Mavsdk::ConnectionHandle>
727
MavsdkImpl::add_udp_connection(const CliArg::Udp& udp, ForwardingOption forwarding_option)
84✔
728
{
729
    auto new_conn = std::make_unique<UdpConnection>(
84✔
730
        [this](mavlink_message_t& message, Connection* connection) {
2,235✔
731
            receive_message(message, connection);
2,235✔
732
        },
2,239✔
733
        //[this](const LibmavMessage& message, Connection* connection) {
734
        //    receive_libmav_message(message, connection);
735
        //},
736
        *_message_set,
84✔
737
        udp.mode == CliArg::Udp::Mode::In ? udp.host : "0.0.0.0",
210✔
738
        udp.mode == CliArg::Udp::Mode::In ? udp.port : 0,
168✔
739
        forwarding_option);
252✔
740

741
    if (!new_conn) {
84✔
742
        return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}};
×
743
    }
744

745
    ConnectionResult ret = new_conn->start();
84✔
746

747
    if (ret != ConnectionResult::Success) {
84✔
748
        return {ret, Mavsdk::ConnectionHandle{}};
×
749
    }
750

751
    if (udp.mode == CliArg::Udp::Mode::Out) {
84✔
752
        // We need to add the IP rather than a hostname, otherwise we end up with two remotes:
753
        // one for the IP, and one for a hostname.
754
        auto remote_ip = resolve_hostname_to_ip(udp.host);
42✔
755

756
        if (!remote_ip) {
42✔
757
            return {ConnectionResult::DestinationIpUnknown, Mavsdk::ConnectionHandle{}};
×
758
        }
759

760
        new_conn->add_remote_to_keep(remote_ip.value(), udp.port);
42✔
761
        std::lock_guard lock(_mutex);
42✔
762

763
        // With a UDP remote, we need to initiate the connection by sending heartbeats.
764
        auto new_configuration = get_configuration();
42✔
765
        new_configuration.set_always_send_heartbeats(true);
42✔
766
        set_configuration(new_configuration);
42✔
767
    }
42✔
768

769
    auto handle = add_connection(std::move(new_conn));
84✔
770

771
    return {ConnectionResult::Success, handle};
84✔
772
}
84✔
773

774
std::pair<ConnectionResult, Mavsdk::ConnectionHandle>
775
MavsdkImpl::add_tcp_connection(const CliArg::Tcp& tcp, ForwardingOption forwarding_option)
2✔
776
{
777
    if (tcp.mode == CliArg::Tcp::Mode::Out) {
2✔
778
        auto new_conn = std::make_unique<TcpClientConnection>(
1✔
779
            [this](mavlink_message_t& message, Connection* connection) {
7✔
780
                receive_message(message, connection);
7✔
781
            },
7✔
782
            //[this](const LibmavMessage& message, Connection* connection) {
783
            //    receive_libmav_message(message, connection);
784
            //},
785
            *_message_set,
1✔
786
            tcp.host,
1✔
787
            tcp.port,
1✔
788
            forwarding_option);
1✔
789
        if (!new_conn) {
1✔
790
            return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}};
×
791
        }
792
        ConnectionResult ret = new_conn->start();
1✔
793
        if (ret == ConnectionResult::Success) {
1✔
794
            return {ret, add_connection(std::move(new_conn))};
1✔
795
        } else {
796
            return {ret, Mavsdk::ConnectionHandle{}};
×
797
        }
798
    } else {
1✔
799
        auto new_conn = std::make_unique<TcpServerConnection>(
1✔
800
            [this](mavlink_message_t& message, Connection* connection) {
13✔
801
                receive_message(message, connection);
13✔
802
            },
13✔
803
            //[this](const LibmavMessage& message, Connection* connection) {
804
            //    receive_libmav_message(message, connection);
805
            //},
806
            *_message_set,
1✔
807
            tcp.host,
1✔
808
            tcp.port,
1✔
809
            forwarding_option);
1✔
810
        if (!new_conn) {
1✔
811
            return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}};
×
812
        }
813
        ConnectionResult ret = new_conn->start();
1✔
814
        if (ret == ConnectionResult::Success) {
1✔
815
            return {ret, add_connection(std::move(new_conn))};
1✔
816
        } else {
817
            return {ret, Mavsdk::ConnectionHandle{}};
×
818
        }
819
    }
1✔
820
}
821

822
std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_serial_connection(
×
823
    const std::string& dev_path,
824
    int baudrate,
825
    bool flow_control,
826
    ForwardingOption forwarding_option)
827
{
828
    auto new_conn = std::make_unique<SerialConnection>(
×
829
        [this](mavlink_message_t& message, Connection* connection) {
×
830
            receive_message(message, connection);
×
831
        },
×
832
        //[this](const LibmavMessage& message, Connection* connection) {
833
        //    receive_libmav_message(message, connection);
834
        //},
UNCOV
835
        *_message_set,
×
836
        dev_path,
837
        baudrate,
838
        flow_control,
839
        forwarding_option);
×
840
    if (!new_conn) {
×
841
        return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}};
×
842
    }
843
    ConnectionResult ret = new_conn->start();
×
844
    if (ret == ConnectionResult::Success) {
×
845
        auto handle = add_connection(std::move(new_conn));
×
846

847
        auto new_configuration = get_configuration();
×
848

849
        // PX4 starting with v1.13 does not send heartbeats by default, so we need
850
        // to initiate the MAVLink connection by sending heartbeats.
851
        // Therefore, we override the default here and enable sending heartbeats.
852
        new_configuration.set_always_send_heartbeats(true);
×
853
        set_configuration(new_configuration);
×
854

855
        return {ret, handle};
×
856

857
    } else {
858
        return {ret, Mavsdk::ConnectionHandle{}};
×
859
    }
860
}
×
861

862
Mavsdk::ConnectionHandle MavsdkImpl::add_connection(std::unique_ptr<Connection>&& new_connection)
86✔
863
{
864
    std::lock_guard lock(_mutex);
86✔
865
    auto handle = _connections_handle_factory.create();
86✔
866
    _connections.emplace_back(ConnectionEntry{std::move(new_connection), handle});
86✔
867

868
    return handle;
172✔
869
}
86✔
870

871
void MavsdkImpl::remove_connection(Mavsdk::ConnectionHandle handle)
×
872
{
873
    std::lock_guard lock(_mutex);
×
874

875
    _connections.erase(std::remove_if(_connections.begin(), _connections.end(), [&](auto&& entry) {
×
876
        return (entry.handle == handle);
×
877
    }));
878
}
×
879

880
Mavsdk::Configuration MavsdkImpl::get_configuration() const
42✔
881
{
882
    std::lock_guard configuration_lock(_mutex);
42✔
883
    return _configuration;
84✔
884
}
42✔
885

886
void MavsdkImpl::set_configuration(Mavsdk::Configuration new_configuration)
129✔
887
{
888
    std::lock_guard server_components_lock(_server_components_mutex);
129✔
889
    // We just point the default to the newly created component. This means
890
    // that the previous default component will be deleted if it is not
891
    // used/referenced anywhere.
892
    _default_server_component =
129✔
893
        server_component_by_id_with_lock(new_configuration.get_component_id());
129✔
894

895
    if (new_configuration.get_always_send_heartbeats() &&
214✔
896
        !_configuration.get_always_send_heartbeats()) {
85✔
897
        start_sending_heartbeats();
43✔
898
    } else if (
86✔
899
        !new_configuration.get_always_send_heartbeats() &&
130✔
900
        _configuration.get_always_send_heartbeats() && !is_any_system_connected()) {
130✔
901
        stop_sending_heartbeats();
×
902
    }
903

904
    _configuration = new_configuration;
129✔
905
    // We cache these values as atomic to avoid having to lock any mutex for them.
906
    _our_system_id = new_configuration.get_system_id();
129✔
907
    _our_component_id = new_configuration.get_component_id();
129✔
908
}
129✔
909

910
uint8_t MavsdkImpl::get_own_system_id() const
5,413✔
911
{
912
    return _our_system_id;
5,413✔
913
}
914

915
uint8_t MavsdkImpl::get_own_component_id() const
1,414✔
916
{
917
    return _our_component_id;
1,414✔
918
}
919

920
uint8_t MavsdkImpl::channel() const
×
921
{
922
    // TODO
923
    return 0;
×
924
}
925

926
Autopilot MavsdkImpl::autopilot() const
×
927
{
928
    // TODO
929
    return Autopilot::Px4;
×
930
}
931

932
// FIXME: this should be per component
933
uint8_t MavsdkImpl::get_mav_type() const
306✔
934
{
935
    return _configuration.get_mav_type();
306✔
936
}
937

938
void MavsdkImpl::make_system_with_component(uint8_t system_id, uint8_t comp_id)
86✔
939
{
940
    // Needs _systems_lock
941

942
    if (_should_exit) {
86✔
943
        // When the system got destroyed in the destructor, we have to give up.
944
        return;
×
945
    }
946

947
    if (static_cast<int>(system_id) == 0 && static_cast<int>(comp_id) == 0) {
86✔
948
        LogDebug() << "Initializing connection to remote system...";
×
949
    } else {
950
        LogDebug() << "New system ID: " << static_cast<int>(system_id)
258✔
951
                   << " Comp ID: " << static_cast<int>(comp_id);
258✔
952
    }
953

954
    // Make a system with its first component
955
    auto new_system = std::make_shared<System>(*this);
86✔
956
    new_system->init(system_id, comp_id);
86✔
957

958
    _systems.emplace_back(system_id, new_system);
86✔
959
}
86✔
960

961
void MavsdkImpl::notify_on_discover()
86✔
962
{
963
    // Queue the callbacks without holding the mutex to avoid deadlocks
964
    _new_system_callbacks.queue([this](const auto& func) { call_user_callback(func); });
129✔
965
}
86✔
966

967
void MavsdkImpl::notify_on_timeout()
×
968
{
969
    // Queue the callbacks without holding the mutex to avoid deadlocks
970
    _new_system_callbacks.queue([this](const auto& func) { call_user_callback(func); });
×
971
}
×
972

973
Mavsdk::NewSystemHandle
974
MavsdkImpl::subscribe_on_new_system(const Mavsdk::NewSystemCallback& callback)
43✔
975
{
976
    std::lock_guard lock(_mutex);
43✔
977

978
    const auto handle = _new_system_callbacks.subscribe(callback);
43✔
979

980
    if (is_any_system_connected()) {
43✔
981
        _new_system_callbacks.queue([this](const auto& func) { call_user_callback(func); });
×
982
    }
983

984
    return handle;
86✔
985
}
43✔
986

987
void MavsdkImpl::unsubscribe_on_new_system(Mavsdk::NewSystemHandle handle)
42✔
988
{
989
    _new_system_callbacks.unsubscribe(handle);
42✔
990
}
42✔
991

992
bool MavsdkImpl::is_any_system_connected() const
43✔
993
{
994
    std::vector<std::shared_ptr<System>> connected_systems = systems();
43✔
995
    return std::any_of(connected_systems.cbegin(), connected_systems.cend(), [](auto& system) {
43✔
996
        return system->is_connected();
×
997
    });
43✔
998
}
43✔
999

1000
void MavsdkImpl::work_thread()
87✔
1001
{
1002
    while (!_should_exit) {
24,067✔
1003
        // Process incoming messages
1004
        process_messages();
23,570✔
1005

1006
        // Process incoming libmav messages
1007
        process_libmav_messages();
24,019✔
1008

1009
        // Run timers
1010
        timeout_handler.run_once();
23,643✔
1011
        call_every_handler.run_once();
23,874✔
1012

1013
        // Do component work
1014
        {
1015
            std::lock_guard lock(_server_components_mutex);
24,004✔
1016
            for (auto& it : _server_components) {
47,864✔
1017
                if (it.second != nullptr) {
23,985✔
1018
                    it.second->_impl->do_work();
23,494✔
1019
                }
1020
            }
1021
        }
23,359✔
1022

1023
        // Deliver outgoing messages
1024
        deliver_messages();
23,577✔
1025

1026
        // If no messages to send, check if there are messages to receive
1027
        std::unique_lock lock_received(_received_messages_mutex);
23,992✔
1028
        if (_received_messages.empty()) {
23,878✔
1029
            // No messages to process, wait for a signal or timeout
1030
            _received_messages_cv.wait_for(lock_received, std::chrono::milliseconds(10), [this]() {
23,449✔
1031
                return !_received_messages.empty() || _should_exit;
47,655✔
1032
            });
1033
        }
1034
    }
24,008✔
1035
}
229✔
1036

1037
void MavsdkImpl::call_user_callback_located(
1,075✔
1038
    const std::string& filename, const int linenumber, const std::function<void()>& func)
1039
{
1040
    // Don't enqueue callbacks if we're shutting down
1041
    if (_should_exit) {
1,075✔
1042
        return;
×
1043
    }
1044

1045
    auto callback_size = _user_callback_queue.size();
1,075✔
1046
    if (callback_size == 10) {
1,075✔
1047
        LogWarn()
×
1048
            << "User callback queue too slow.\n"
×
1049
               "See: https://mavsdk.mavlink.io/main/en/cpp/troubleshooting.html#user_callbacks";
×
1050

1051
    } else if (callback_size == 99) {
1,075✔
1052
        LogErr()
×
1053
            << "User callback queue overflown\n"
×
1054
               "See: https://mavsdk.mavlink.io/main/en/cpp/troubleshooting.html#user_callbacks";
×
1055

1056
    } else if (callback_size == 100) {
1,075✔
1057
        return;
×
1058
    }
1059

1060
    // We only need to keep track of filename and linenumber if we're actually debugging this.
1061
    UserCallback user_callback =
1,075✔
1062
        _callback_debugging ? UserCallback{func, filename, linenumber} : UserCallback{func};
2,150✔
1063

1064
    _user_callback_queue.enqueue(user_callback);
1,075✔
1065
}
1,075✔
1066

1067
void MavsdkImpl::process_user_callbacks_thread()
87✔
1068
{
1069
    while (!_should_exit) {
1,249✔
1070
        auto callback = _user_callback_queue.dequeue();
1,162✔
1071
        if (!callback) {
1,162✔
1072
            continue;
87✔
1073
        }
1074

1075
        // Check if we're in the process of shutting down before executing the callback
1076
        if (_should_exit) {
1,075✔
1077
            continue;
×
1078
        }
1079

1080
        const double timeout_s = 1.0;
1,075✔
1081
        auto cookie = timeout_handler.add(
2,150✔
NEW
1082
            [this,
×
1083
             filename = callback.value().filename,
1,075✔
1084
             linenumber = callback.value().linenumber,
1,075✔
NEW
1085
             timeout_s]() {
×
1086
                if (_callback_debugging) {
×
NEW
1087
                    LogWarn() << "Callback called from " << filename << ":" << linenumber
×
NEW
1088
                              << " took more than " << timeout_s << " second to run.";
×
1089
                    fflush(stdout);
×
1090
                    fflush(stderr);
×
1091
                    abort();
×
1092
                } else {
1093
                    LogWarn()
×
1094
                        << "Callback took more than " << timeout_s << " second to run.\n"
×
1095
                        << "See: https://mavsdk.mavlink.io/main/en/cpp/troubleshooting.html#user_callbacks";
×
1096
                }
1097
            },
×
1098
            timeout_s);
1,075✔
1099
        callback.value().func();
1,075✔
1100
        timeout_handler.remove(cookie);
1,075✔
1101
    }
1,162✔
1102
}
87✔
1103

1104
void MavsdkImpl::start_sending_heartbeats()
215✔
1105
{
1106
    // Check if we're in the process of shutting down
1107
    if (_should_exit) {
215✔
1108
        return;
×
1109
    }
1110

1111
    // Before sending out first heartbeats we need to make sure we have a
1112
    // default server component.
1113
    default_server_component_impl();
215✔
1114

1115
    {
1116
        std::lock_guard<std::mutex> lock(_heartbeat_mutex);
215✔
1117
        call_every_handler.remove(_heartbeat_send_cookie);
215✔
1118
        _heartbeat_send_cookie =
430✔
1119
            call_every_handler.add([this]() { send_heartbeats(); }, HEARTBEAT_SEND_INTERVAL_S);
519✔
1120
    }
215✔
1121
}
1122

1123
void MavsdkImpl::stop_sending_heartbeats()
×
1124
{
1125
    if (!_configuration.get_always_send_heartbeats()) {
×
1126
        std::lock_guard<std::mutex> lock(_heartbeat_mutex);
×
1127
        call_every_handler.remove(_heartbeat_send_cookie);
×
1128
    }
×
1129
}
×
1130

1131
ServerComponentImpl& MavsdkImpl::default_server_component_impl()
1,117✔
1132
{
1133
    std::lock_guard lock(_server_components_mutex);
1,117✔
1134
    return default_server_component_with_lock();
1,116✔
1135
}
1,117✔
1136

1137
ServerComponentImpl& MavsdkImpl::default_server_component_with_lock()
1,117✔
1138
{
1139
    if (_default_server_component == nullptr) {
1,117✔
1140
        _default_server_component = server_component_by_id_with_lock(_our_component_id);
×
1141
    }
1142
    return *_default_server_component->_impl;
1,116✔
1143
}
1144

1145
void MavsdkImpl::send_heartbeats()
304✔
1146
{
1147
    std::lock_guard lock(_server_components_mutex);
304✔
1148

1149
    for (auto& it : _server_components) {
608✔
1150
        if (it.second != nullptr) {
305✔
1151
            it.second->_impl->send_heartbeat();
306✔
1152
        }
1153
    }
1154
}
302✔
1155

1156
void MavsdkImpl::intercept_incoming_messages_async(std::function<bool(mavlink_message_t&)> callback)
22✔
1157
{
1158
    std::lock_guard<std::mutex> lock(_intercept_callbacks_mutex);
22✔
1159
    _intercept_incoming_messages_callback = callback;
22✔
1160
}
22✔
1161

1162
void MavsdkImpl::intercept_outgoing_messages_async(std::function<bool(mavlink_message_t&)> callback)
14✔
1163
{
1164
    std::lock_guard<std::mutex> lock(_intercept_callbacks_mutex);
14✔
1165
    _intercept_outgoing_messages_callback = callback;
14✔
1166
}
14✔
1167

1168
Mavsdk::ConnectionErrorHandle
1169
MavsdkImpl::subscribe_connection_errors(Mavsdk::ConnectionErrorCallback callback)
×
1170
{
1171
    std::lock_guard lock(_mutex);
×
1172

1173
    const auto handle = _connections_errors_subscriptions.subscribe(callback);
×
1174

1175
    return handle;
×
1176
}
×
1177

1178
void MavsdkImpl::unsubscribe_connection_errors(Mavsdk::ConnectionErrorHandle handle)
×
1179
{
1180
    std::lock_guard lock(_mutex);
×
1181
    _connections_errors_subscriptions.unsubscribe(handle);
×
1182
}
×
1183

1184
uint8_t MavsdkImpl::get_target_system_id(const mavlink_message_t& message)
2,258✔
1185
{
1186
    // Checks whether connection knows target system ID by extracting target system if set.
1187
    const mavlink_msg_entry_t* meta = mavlink_get_msg_entry(message.msgid);
2,258✔
1188

1189
    if (meta == nullptr || !(meta->flags & MAV_MSG_ENTRY_FLAG_HAVE_TARGET_SYSTEM)) {
2,258✔
1190
        return 0;
457✔
1191
    }
1192

1193
    // Don't look at the target system offset if it is outside the payload length.
1194
    // This can happen if the fields are trimmed.
1195
    if (meta->target_system_ofs >= message.len) {
1,801✔
1196
        return 0;
13✔
1197
    }
1198

1199
    return (_MAV_PAYLOAD(&message))[meta->target_system_ofs];
1,788✔
1200
}
1201

1202
uint8_t MavsdkImpl::get_target_component_id(const mavlink_message_t& message)
×
1203
{
1204
    // Checks whether connection knows target system ID by extracting target system if set.
1205
    const mavlink_msg_entry_t* meta = mavlink_get_msg_entry(message.msgid);
×
1206

1207
    if (meta == nullptr || !(meta->flags & MAV_MSG_ENTRY_FLAG_HAVE_TARGET_COMPONENT)) {
×
1208
        return 0;
×
1209
    }
1210

1211
    // Don't look at the target component offset if it is outside the payload length.
1212
    // This can happen if the fields are trimmed.
1213
    if (meta->target_component_ofs >= message.len) {
×
1214
        return 0;
×
1215
    }
1216

1217
    return (_MAV_PAYLOAD(&message))[meta->target_component_ofs];
×
1218
}
1219

1220
Sender& MavsdkImpl::sender()
×
1221
{
1222
    std::lock_guard lock(_server_components_mutex);
×
1223
    return default_server_component_with_lock().sender();
×
1224
}
×
1225

1226
std::vector<Connection*> MavsdkImpl::get_connections() const
×
1227
{
1228
    std::vector<Connection*> connections;
×
1229
    for (const auto& connection_entry : _connections) {
×
1230
        connections.push_back(connection_entry.connection.get());
×
1231
    }
1232
    return connections;
×
1233
}
1234

UNCOV
1235
mav::MessageSet& MavsdkImpl::get_message_set() const
×
1236
{
UNCOV
1237
    return *_message_set;
×
1238
}
1239

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