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

mavlink / MAVSDK / 17565334070

08 Sep 2025 10:02PM UTC coverage: 47.262% (+0.02%) from 47.241%
17565334070

Pull #2649

github

web-flow
Merge 31bace7d2 into 57e1189fd
Pull Request #2649: CI: Tell sonar we use C++17

16788 of 35521 relevant lines covered (47.26%)

444.32 hits per line

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

61.34
/src/mavsdk/core/server_component_impl.cpp
1
#include "server_component_impl.h"
2
#include "server_plugin_impl_base.h"
3
#include "mavsdk_impl.h"
4

5
namespace mavsdk {
6

7
ServerComponentImpl::ServerComponentImpl(MavsdkImpl& mavsdk_impl, uint8_t component_id) :
117✔
8
    _mavsdk_impl(mavsdk_impl),
117✔
9
    _own_component_id(component_id),
117✔
10
    _our_sender(*this),
117✔
11
    _mavlink_command_receiver(*this),
117✔
12
    _mission_transfer_server(
234✔
13
        _our_sender,
14
        mavsdk_impl.mavlink_message_handler,
117✔
15
        mavsdk_impl.timeout_handler,
117✔
16
        [this]() { return _mavsdk_impl.timeout_s(); }),
1✔
17
    _mavlink_parameter_server(_our_sender, mavsdk_impl.mavlink_message_handler),
117✔
18
    _mavlink_request_message_handler(mavsdk_impl, *this, _mavlink_command_receiver),
117✔
19
    _mavlink_ftp_server(*this)
234✔
20
{
21
    _autopilot_version.capabilities |= MAV_PROTOCOL_CAPABILITY_MAVLINK2;
117✔
22

23
    if (!MavlinkChannels::Instance().checkout_free_channel(_channel)) {
117✔
24
        // We use a default of channel 0 which will still work but not track
25
        // seq correctly.
26
        _channel = 0;
×
27
        LogErr() << "Could not get a MAVLink channel, using default 0";
×
28
    }
29

30
    register_mavlink_command_handler(
117✔
31
        MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES,
32
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
33
            send_autopilot_version();
×
34
            return make_command_ack_message(command, MAV_RESULT_ACCEPTED);
×
35
        },
36
        this);
37

38
    register_mavlink_command_handler(
117✔
39
        MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES,
40
        [this](const MavlinkCommandReceiver::CommandInt& command) {
×
41
            send_autopilot_version();
×
42
            return make_command_ack_message(command, MAV_RESULT_ACCEPTED);
×
43
        },
44
        this);
45

46
    _mavlink_request_message_handler.register_handler(
117✔
47
        MAVLINK_MSG_ID_PROTOCOL_VERSION,
48
        [this](uint8_t, uint8_t, const MavlinkRequestMessageHandler::Params&) {
×
49
            send_protocol_version();
×
50
            return MAV_RESULT_ACCEPTED;
×
51
        },
52
        this);
53

54
    _mavlink_request_message_handler.register_handler(
117✔
55
        MAVLINK_MSG_ID_AUTOPILOT_VERSION,
56
        [this](uint8_t, uint8_t, const MavlinkRequestMessageHandler::Params&) {
49✔
57
            send_autopilot_version();
49✔
58
            return MAV_RESULT_ACCEPTED;
49✔
59
        },
60
        this);
61
}
117✔
62

63
ServerComponentImpl::~ServerComponentImpl()
117✔
64
{
65
    unregister_all_mavlink_command_handlers(this);
117✔
66
    _mavlink_request_message_handler.unregister_all_handlers(this);
117✔
67

68
    MavlinkChannels::Instance().checkin_used_channel(_channel);
117✔
69
}
117✔
70

71
void ServerComponentImpl::register_plugin(ServerPluginImplBase* server_plugin_impl)
51✔
72
{
73
    // This is a bit pointless now but just mirrors what is done for the client plugins.
74
    server_plugin_impl->init();
51✔
75
}
51✔
76

77
void ServerComponentImpl::unregister_plugin(ServerPluginImplBase* server_plugin_impl)
51✔
78
{
79
    // This is a bit pointless now but just mirrors what is done for the client plugins.
80
    server_plugin_impl->deinit();
51✔
81
}
51✔
82

83
void ServerComponentImpl::register_mavlink_command_handler(
117✔
84
    uint16_t cmd_id,
85
    const MavlinkCommandReceiver::MavlinkCommandIntHandler& callback,
86
    const void* cookie)
87
{
88
    _mavlink_command_receiver.register_mavlink_command_handler(cmd_id, callback, cookie);
117✔
89
}
117✔
90

91
void ServerComponentImpl::register_mavlink_command_handler(
346✔
92
    uint16_t cmd_id,
93
    const MavlinkCommandReceiver::MavlinkCommandLongHandler& callback,
94
    const void* cookie)
95
{
96
    _mavlink_command_receiver.register_mavlink_command_handler(cmd_id, callback, cookie);
346✔
97
}
346✔
98

99
void ServerComponentImpl::unregister_mavlink_command_handler(uint16_t cmd_id, const void* cookie)
10✔
100
{
101
    _mavlink_command_receiver.unregister_mavlink_command_handler(cmd_id, cookie);
10✔
102
}
10✔
103

104
void ServerComponentImpl::unregister_all_mavlink_command_handlers(const void* cookie)
245✔
105
{
106
    _mavlink_command_receiver.unregister_all_mavlink_command_handlers(cookie);
245✔
107
}
245✔
108

109
void ServerComponentImpl::register_mavlink_message_handler(
355✔
110
    uint16_t msg_id, const MavlinkMessageHandler& callback, const void* cookie)
111
{
112
    _mavsdk_impl.mavlink_message_handler.register_one(msg_id, callback, cookie);
355✔
113
}
355✔
114

115
void ServerComponentImpl::unregister_mavlink_message_handler(uint16_t msg_id, const void* cookie)
×
116
{
117
    _mavsdk_impl.mavlink_message_handler.unregister_one(msg_id, cookie);
×
118
}
×
119

120
void ServerComponentImpl::unregister_all_mavlink_message_handlers(const void* cookie)
235✔
121
{
122
    _mavsdk_impl.mavlink_message_handler.unregister_all(cookie);
235✔
123
}
235✔
124

125
void ServerComponentImpl::do_work()
24,071✔
126
{
127
    _mavlink_parameter_server.do_work();
24,071✔
128
    _mission_transfer_server.do_work();
23,778✔
129
}
23,810✔
130

131
Sender& ServerComponentImpl::sender()
131✔
132
{
133
    return _our_sender;
131✔
134
}
135

136
uint8_t ServerComponentImpl::get_own_system_id() const
4,149✔
137
{
138
    return _mavsdk_impl.get_own_system_id();
4,149✔
139
}
140

141
void ServerComponentImpl::set_own_component_id(uint8_t own_component_id)
×
142
{
143
    _own_component_id = own_component_id;
×
144
}
×
145
uint8_t ServerComponentImpl::get_own_component_id() const
4,024✔
146
{
147
    return _own_component_id;
4,024✔
148
}
149

150
Time& ServerComponentImpl::get_time()
38✔
151
{
152
    return _mavsdk_impl.time;
38✔
153
}
154

155
bool ServerComponentImpl::send_message(mavlink_message_t& message)
2✔
156
{
157
    return _mavsdk_impl.send_message(message);
2✔
158
}
159

160
bool ServerComponentImpl::send_command_ack(mavlink_command_ack_t& command_ack)
31✔
161
{
162
    return queue_message([&, this](MavlinkAddress mavlink_address, uint8_t channel) {
62✔
163
        mavlink_message_t message;
164
        mavlink_msg_command_ack_encode_chan(
31✔
165
            mavlink_address.system_id,
31✔
166
            mavlink_address.component_id,
31✔
167
            channel,
168
            &message,
169
            &command_ack);
31✔
170
        return message;
31✔
171
    });
31✔
172
}
173

174
bool ServerComponentImpl::queue_message(
2,480✔
175
    std::function<mavlink_message_t(MavlinkAddress mavlink_address, uint8_t channel)> fun)
176
{
177
    std::lock_guard<std::mutex> lock(_mavlink_pack_mutex);
2,480✔
178
    MavlinkAddress mavlink_address{get_own_system_id(), get_own_component_id()};
2,482✔
179
    mavlink_message_t message = fun(mavlink_address, _channel);
2,481✔
180
    return _mavsdk_impl.send_message(message);
2,482✔
181
}
2,482✔
182

183
CallEveryHandler::Cookie
184
ServerComponentImpl::add_call_every(std::function<void()> callback, float interval_s)
2✔
185
{
186
    return _mavsdk_impl.call_every_handler.add(
4✔
187
        std::move(callback), static_cast<double>(interval_s));
4✔
188
}
189

190
void ServerComponentImpl::change_call_every(float interval_s, CallEveryHandler::Cookie cookie)
×
191
{
192
    _mavsdk_impl.call_every_handler.change(static_cast<double>(interval_s), cookie);
×
193
}
×
194

195
void ServerComponentImpl::reset_call_every(CallEveryHandler::Cookie cookie)
×
196
{
197
    _mavsdk_impl.call_every_handler.reset(cookie);
×
198
}
×
199

200
void ServerComponentImpl::remove_call_every(CallEveryHandler::Cookie cookie)
22✔
201
{
202
    _mavsdk_impl.call_every_handler.remove(cookie);
22✔
203
}
22✔
204

205
mavlink_command_ack_t ServerComponentImpl::make_command_ack_message(
229✔
206
    const MavlinkCommandReceiver::CommandLong& command, MAV_RESULT result)
207
{
208
    mavlink_command_ack_t command_ack{};
229✔
209
    command_ack.command = command.command;
229✔
210
    command_ack.result = result;
229✔
211
    command_ack.progress = std::numeric_limits<uint8_t>::max();
229✔
212
    command_ack.result_param2 = 0;
229✔
213
    command_ack.target_system = command.origin_system_id;
229✔
214
    command_ack.target_component = command.origin_component_id;
229✔
215

216
    return command_ack;
229✔
217
}
218

219
mavlink_command_ack_t ServerComponentImpl::make_command_ack_message(
×
220
    const MavlinkCommandReceiver::CommandInt& command, MAV_RESULT result)
221
{
222
    mavlink_command_ack_t command_ack{};
×
223
    command_ack.command = command.command;
×
224
    command_ack.result = result;
×
225
    command_ack.progress = std::numeric_limits<uint8_t>::max();
×
226
    command_ack.result_param2 = 0;
×
227
    command_ack.target_system = command.origin_system_id;
×
228
    command_ack.target_component = command.origin_component_id;
×
229

230
    return command_ack;
×
231
}
232

233
void ServerComponentImpl::send_heartbeat()
343✔
234
{
235
    queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
343✔
236
        mavlink_message_t message;
237
        mavlink_msg_heartbeat_pack_chan(
1,032✔
238
            get_own_system_id(),
1,366✔
239
            mavlink_address.component_id,
342✔
240
            channel,
241
            &message,
242
            _mavsdk_impl.get_mav_type(),
343✔
243
            mavlink_address.component_id == MAV_COMP_ID_AUTOPILOT1 ? MAV_AUTOPILOT_GENERIC :
343✔
244
                                                                     MAV_AUTOPILOT_INVALID,
245
            mavlink_address.component_id == MAV_COMP_ID_AUTOPILOT1 ? _base_mode.load() : 0,
513✔
246
            mavlink_address.component_id == MAV_COMP_ID_AUTOPILOT1 ? _custom_mode.load() : 0,
513✔
247
            get_system_status());
341✔
248
        return message;
343✔
249
    });
250
}
340✔
251

252
void ServerComponentImpl::set_system_status(uint8_t system_status)
×
253
{
254
    _system_status = static_cast<MAV_STATE>(system_status);
×
255
}
×
256

257
uint8_t ServerComponentImpl::get_system_status() const
343✔
258
{
259
    return _system_status;
343✔
260
}
261

262
void ServerComponentImpl::set_base_mode(uint8_t base_mode)
2✔
263
{
264
    _base_mode = base_mode;
2✔
265
}
2✔
266

267
uint8_t ServerComponentImpl::get_base_mode() const
2✔
268
{
269
    return _base_mode;
2✔
270
}
271

272
void ServerComponentImpl::set_custom_mode(uint32_t custom_mode)
×
273
{
274
    _custom_mode = custom_mode;
×
275
}
×
276

277
uint32_t ServerComponentImpl::get_custom_mode() const
×
278
{
279
    return _custom_mode;
×
280
}
281

282
void ServerComponentImpl::call_user_callback_located(
10✔
283
    const std::string& filename, const int linenumber, const std::function<void()>& func)
284
{
285
    _mavsdk_impl.call_user_callback_located(filename, linenumber, func);
10✔
286
}
10✔
287

288
TimeoutHandler::Cookie ServerComponentImpl::register_timeout_handler(
×
289
    const std::function<void()>& callback, double duration_s)
290
{
291
    return _mavsdk_impl.timeout_handler.add(callback, duration_s);
×
292
}
293

294
void ServerComponentImpl::refresh_timeout_handler(TimeoutHandler::Cookie cookie)
×
295
{
296
    _mavsdk_impl.timeout_handler.refresh(cookie);
×
297
}
×
298

299
void ServerComponentImpl::unregister_timeout_handler(TimeoutHandler::Cookie cookie)
×
300
{
301
    _mavsdk_impl.timeout_handler.remove(cookie);
×
302
}
×
303

304
void ServerComponentImpl::add_capabilities(uint64_t add_capabilities)
1✔
305
{
306
    {
307
        std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
1✔
308
        _autopilot_version.capabilities |= add_capabilities;
1✔
309
    }
1✔
310

311
    // We need to resend capabilities.
312
    send_autopilot_version();
1✔
313
}
1✔
314

315
void ServerComponentImpl::set_flight_sw_version(uint32_t flight_sw_version)
×
316
{
317
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
318
    _autopilot_version.flight_sw_version = flight_sw_version;
×
319
}
×
320

321
void ServerComponentImpl::set_middleware_sw_version(uint32_t middleware_sw_version)
×
322
{
323
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
324
    _autopilot_version.middleware_sw_version = middleware_sw_version;
×
325
}
×
326

327
void ServerComponentImpl::set_os_sw_version(uint32_t os_sw_version)
×
328
{
329
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
330
    _autopilot_version.os_sw_version = os_sw_version;
×
331
}
×
332

333
void ServerComponentImpl::set_board_version(uint32_t board_version)
×
334
{
335
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
336
    _autopilot_version.board_version = board_version;
×
337
}
×
338

339
void ServerComponentImpl::set_vendor_id(uint16_t vendor_id)
×
340
{
341
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
342
    _autopilot_version.vendor_id = vendor_id;
×
343
}
×
344

345
void ServerComponentImpl::set_product_id(uint16_t product_id)
×
346
{
347
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
348
    _autopilot_version.product_id = product_id;
×
349
}
×
350

351
bool ServerComponentImpl::set_uid2(std::string uid2)
×
352
{
353
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
354
    if (uid2.size() > _autopilot_version.uid2.size()) {
×
355
        return false;
×
356
    }
357
    _autopilot_version.uid2 = {0};
×
358
    std::copy(uid2.begin(), uid2.end(), _autopilot_version.uid2.data());
×
359
    return true;
×
360
}
×
361

362
void ServerComponentImpl::send_autopilot_version()
51✔
363
{
364
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
51✔
365
    const uint8_t custom_values[8] = {0}; // TO-DO: maybe?
51✔
366

367
    queue_message([&, this](MavlinkAddress mavlink_address, uint8_t channel) {
51✔
368
        mavlink_message_t message;
369
        mavlink_msg_autopilot_version_pack_chan(
51✔
370
            mavlink_address.system_id,
51✔
371
            mavlink_address.component_id,
51✔
372
            channel,
373
            &message,
374
            _autopilot_version.capabilities,
375
            _autopilot_version.flight_sw_version,
376
            _autopilot_version.middleware_sw_version,
377
            _autopilot_version.os_sw_version,
378
            _autopilot_version.board_version,
379
            custom_values,
51✔
380
            custom_values,
381
            custom_values,
382
            _autopilot_version.vendor_id,
51✔
383
            _autopilot_version.product_id,
51✔
384
            0,
385
            _autopilot_version.uid2.data());
51✔
386
        return message;
51✔
387
    });
388
}
51✔
389

390
void ServerComponentImpl::send_protocol_version()
×
391
{
392
    queue_message([&, this](MavlinkAddress mavlink_address, uint8_t channel) {
×
393
        mavlink_message_t message;
394
        mavlink_msg_protocol_version_pack_chan(
×
395
            mavlink_address.system_id,
×
396
            mavlink_address.component_id,
×
397
            channel,
398
            &message,
399
            MAVLINK_VERSION_INFO.version,
×
400
            MAVLINK_VERSION_INFO.min_version,
×
401
            MAVLINK_VERSION_INFO.max_version,
×
402
            MAVLINK_VERSION_INFO.spec_version_hash,
403
            MAVLINK_VERSION_INFO.library_version_hash);
404
        return message;
×
405
    });
406
}
×
407

408
ServerComponentImpl::OurSender::OurSender(ServerComponentImpl& server_component_impl) :
117✔
409
    _server_component_impl(server_component_impl)
117✔
410
{}
117✔
411

412
bool ServerComponentImpl::OurSender::send_message(mavlink_message_t& message)
×
413
{
414
    return _server_component_impl.send_message(message);
×
415
}
416

417
bool ServerComponentImpl::OurSender::queue_message(
201✔
418
    std::function<mavlink_message_t(MavlinkAddress, uint8_t)> fun)
419
{
420
    return _server_component_impl.queue_message(fun);
201✔
421
}
422

423
uint8_t ServerComponentImpl::OurSender::get_own_system_id() const
78✔
424
{
425
    return _server_component_impl.get_own_system_id();
78✔
426
}
427

428
uint8_t ServerComponentImpl::OurSender::get_own_component_id() const
78✔
429
{
430
    return _server_component_impl.get_own_component_id();
78✔
431
}
432

433
Autopilot ServerComponentImpl::OurSender::autopilot() const
30✔
434
{
435
    // FIXME: hard-coded to PX4 for now to avoid the dependency into mavsdk_impl.
436
    return Autopilot::Px4;
30✔
437
}
438

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

© 2025 Coveralls, Inc