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

mavlink / MAVSDK / 12060541516

28 Nov 2024 12:52AM UTC coverage: 43.553% (+4.9%) from 38.691%
12060541516

Pull #2386

github

web-flow
Merge cc93a465c into 5a124d9bc
Pull Request #2386: camera: support multiple cameras within one instance

1353 of 2024 new or added lines in 47 files covered. (66.85%)

118 existing lines in 8 files now uncovered.

13859 of 31821 relevant lines covered (43.55%)

289.55 hits per line

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

60.08
/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) :
88✔
8
    _mavsdk_impl(mavsdk_impl),
88✔
9
    _own_component_id(component_id),
88✔
10
    _our_sender(*this),
88✔
11
    _mavlink_command_receiver(*this),
88✔
12
    _mission_transfer_server(
176✔
13
        _our_sender,
14
        mavsdk_impl.mavlink_message_handler,
88✔
15
        mavsdk_impl.timeout_handler,
88✔
16
        [this]() { return _mavsdk_impl.timeout_s(); }),
1✔
17
    _mavlink_parameter_server(_our_sender, mavsdk_impl.mavlink_message_handler),
88✔
18
    _mavlink_request_message_handler(mavsdk_impl, *this, _mavlink_command_receiver),
88✔
19
    _mavlink_ftp_server(*this)
176✔
20
{
21
    _autopilot_version.capabilities |= MAV_PROTOCOL_CAPABILITY_MAVLINK2;
88✔
22

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

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

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

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

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

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

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

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

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

83
void ServerComponentImpl::register_mavlink_command_handler(
88✔
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);
88✔
89
}
88✔
90

91
void ServerComponentImpl::register_mavlink_command_handler(
309✔
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);
309✔
97
}
309✔
98

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

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

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

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

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

125
void ServerComponentImpl::do_work()
20,010✔
126
{
127
    _mavlink_parameter_server.do_work();
20,010✔
128
    _mission_transfer_server.do_work();
19,586✔
129
}
19,583✔
130

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

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

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

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

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

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

174
bool ServerComponentImpl::queue_message(
2,047✔
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,047✔
178
    MavlinkAddress mavlink_address{get_own_system_id(), get_own_component_id()};
2,047✔
179
    mavlink_message_t message = fun(mavlink_address, _channel);
2,047✔
180
    return _mavsdk_impl.send_message(message);
2,047✔
181
}
2,047✔
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));
2✔
188
}
189

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

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

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

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

216
    return command_ack;
112✔
217
}
218

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

UNCOV
230
    return command_ack;
×
231
}
232

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

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

257
uint8_t ServerComponentImpl::get_system_status() const
253✔
258
{
259
    return _system_status;
253✔
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
{
UNCOV
274
    _custom_mode = custom_mode;
×
275
}
×
276

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

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

UNCOV
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

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

UNCOV
299
void ServerComponentImpl::unregister_timeout_handler(TimeoutHandler::Cookie cookie)
×
300
{
UNCOV
301
    _mavsdk_impl.timeout_handler.remove(cookie);
×
UNCOV
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);
×
UNCOV
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);
×
UNCOV
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);
×
UNCOV
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);
×
UNCOV
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);
×
UNCOV
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

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

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

367
    queue_message([&, this](MavlinkAddress mavlink_address, uint8_t channel) {
38✔
368
        mavlink_message_t message;
369
        mavlink_msg_autopilot_version_pack_chan(
38✔
370
            mavlink_address.system_id,
38✔
371
            mavlink_address.component_id,
38✔
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,
38✔
380
            custom_values,
381
            custom_values,
382
            _autopilot_version.vendor_id,
38✔
383
            _autopilot_version.product_id,
38✔
384
            0,
385
            _autopilot_version.uid2.data());
38✔
386
        return message;
38✔
387
    });
388
}
38✔
389

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

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

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

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

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

428
uint8_t ServerComponentImpl::OurSender::get_own_component_id() const
72✔
429
{
430
    return _server_component_impl.get_own_component_id();
72✔
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