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

mavlink / MAVSDK / 4899817366

pending completion
4899817366

Pull #1772

github

GitHub
Merge c0bb90862 into e4e9c71dd
Pull Request #1772: Refactor MAVLinkParameters into client and server classes

2192 of 2192 new or added lines in 34 files covered. (100.0%)

7708 of 24812 relevant lines covered (31.07%)

19.96 hits per line

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

56.86
/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) :
20✔
8
    _mavsdk_impl(mavsdk_impl),
9
    _own_component_id(component_id),
10
    _our_sender(mavsdk_impl, *this),
11
    _mavlink_command_receiver(mavsdk_impl),
12
    _mission_transfer(
13
        _our_sender,
14
        mavsdk_impl.mavlink_message_handler,
20✔
15
        mavsdk_impl.timeout_handler,
20✔
16
        [this]() { return _mavsdk_impl.timeout_s(); }),
1✔
17
    _mavlink_parameter_receiver(_our_sender, mavsdk_impl.mavlink_message_handler),
20✔
18
    _mavlink_request_message_handler(mavsdk_impl, *this, _mavlink_command_receiver)
20✔
19
{
20
    register_mavlink_command_handler(
20✔
21
        MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES,
22
        [this](const MavlinkCommandReceiver::CommandLong& command) {
20✔
23
            send_autopilot_version();
10✔
24
            return make_command_ack_message(command, MAV_RESULT_ACCEPTED);
10✔
25
        },
26
        this);
27

28
    register_mavlink_command_handler(
20✔
29
        MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES,
30
        [this](const MavlinkCommandReceiver::CommandInt& command) {
×
31
            send_autopilot_version();
×
32
            return make_command_ack_message(command, MAV_RESULT_ACCEPTED);
×
33
        },
34
        this);
35

36
    _mavlink_request_message_handler.register_handler(
20✔
37
        MAVLINK_MSG_ID_AUTOPILOT_VERSION,
38
        [this](uint8_t, uint8_t, const MavlinkRequestMessageHandler::Params&) {
×
39
            send_autopilot_version();
×
40
            return MAV_RESULT_ACCEPTED;
×
41
        },
42
        this);
43
}
20✔
44

45
ServerComponentImpl::~ServerComponentImpl()
20✔
46
{
47
    unregister_all_mavlink_command_handlers(this);
20✔
48
    _mavlink_request_message_handler.unregister_all_handlers(this);
20✔
49
}
20✔
50

51
void ServerComponentImpl::register_plugin(ServerPluginImplBase* server_plugin_impl)
10✔
52
{
53
    // This is a bit pointless now but just mirrors what is done for the client plugins.
54
    server_plugin_impl->init();
10✔
55
}
10✔
56

57
void ServerComponentImpl::unregister_plugin(ServerPluginImplBase* server_plugin_impl)
10✔
58
{
59
    // This is a bit pointless now but just mirrors what is done for the client plugins.
60
    server_plugin_impl->deinit();
10✔
61
}
10✔
62

63
void ServerComponentImpl::register_mavlink_command_handler(
20✔
64
    uint16_t cmd_id,
65
    const MavlinkCommandReceiver::MavlinkCommandIntHandler& callback,
66
    const void* cookie)
67
{
68
    _mavlink_command_receiver.register_mavlink_command_handler(cmd_id, callback, cookie);
20✔
69
}
20✔
70

71
void ServerComponentImpl::register_mavlink_command_handler(
43✔
72
    uint16_t cmd_id,
73
    const MavlinkCommandReceiver::MavlinkCommandLongHandler& callback,
74
    const void* cookie)
75
{
76
    _mavlink_command_receiver.register_mavlink_command_handler(cmd_id, callback, cookie);
43✔
77
}
43✔
78

79
void ServerComponentImpl::unregister_mavlink_command_handler(uint16_t cmd_id, const void* cookie)
×
80
{
81
    _mavlink_command_receiver.unregister_mavlink_command_handler(cmd_id, cookie);
×
82
}
×
83

84
void ServerComponentImpl::unregister_all_mavlink_command_handlers(const void* cookie)
22✔
85
{
86
    _mavlink_command_receiver.unregister_all_mavlink_command_handlers(cookie);
22✔
87
}
22✔
88

89
void ServerComponentImpl::register_mavlink_message_handler(
3✔
90
    uint16_t msg_id, const MavlinkMessageHandler& callback, const void* cookie)
91
{
92
    _mavsdk_impl.mavlink_message_handler.register_one(msg_id, callback, cookie);
3✔
93
}
3✔
94

95
void ServerComponentImpl::register_mavlink_message_handler(
×
96
    uint16_t msg_id, uint8_t cmp_id, const MavlinkMessageHandler& callback, const void* cookie)
97
{
98
    _mavsdk_impl.mavlink_message_handler.register_one(msg_id, cmp_id, callback, cookie);
×
99
}
×
100

101
void ServerComponentImpl::unregister_mavlink_message_handler(uint16_t msg_id, const void* cookie)
×
102
{
103
    _mavsdk_impl.mavlink_message_handler.unregister_one(msg_id, cookie);
×
104
}
×
105

106
void ServerComponentImpl::unregister_all_mavlink_message_handlers(const void* cookie)
1✔
107
{
108
    _mavsdk_impl.mavlink_message_handler.unregister_all(cookie);
1✔
109
}
1✔
110

111
void ServerComponentImpl::do_work()
657✔
112
{
113
    _mavlink_parameter_receiver.do_work();
657✔
114
    _mission_transfer.do_work();
657✔
115
}
657✔
116

117
uint8_t ServerComponentImpl::get_own_system_id() const
40✔
118
{
119
    return _mavsdk_impl.get_own_system_id();
40✔
120
}
121

122
void ServerComponentImpl::set_own_component_id(uint8_t own_component_id)
×
123
{
124
    _own_component_id = own_component_id;
×
125
}
×
126
uint8_t ServerComponentImpl::get_own_component_id() const
233✔
127
{
128
    return _own_component_id;
233✔
129
}
130

131
Time& ServerComponentImpl::get_time()
×
132
{
133
    return _mavsdk_impl.time;
×
134
}
135

136
bool ServerComponentImpl::send_message(mavlink_message_t& message)
26✔
137
{
138
    return _mavsdk_impl.send_message(message);
26✔
139
}
140

141
void ServerComponentImpl::add_call_every(
1✔
142
    std::function<void()> callback, float interval_s, void** cookie)
143
{
144
    _mavsdk_impl.call_every_handler.add(
2✔
145
        std::move(callback), static_cast<double>(interval_s), cookie);
1✔
146
}
1✔
147

148
void ServerComponentImpl::change_call_every(float interval_s, const void* cookie)
×
149
{
150
    _mavsdk_impl.call_every_handler.change(static_cast<double>(interval_s), cookie);
×
151
}
×
152

153
void ServerComponentImpl::reset_call_every(const void* cookie)
×
154
{
155
    _mavsdk_impl.call_every_handler.reset(cookie);
×
156
}
×
157

158
void ServerComponentImpl::remove_call_every(const void* cookie)
1✔
159
{
160
    _mavsdk_impl.call_every_handler.remove(cookie);
1✔
161
}
1✔
162

163
mavlink_message_t ServerComponentImpl::make_command_ack_message(
14✔
164
    const MavlinkCommandReceiver::CommandLong& command, MAV_RESULT result)
165
{
166
    const uint8_t progress = std::numeric_limits<uint8_t>::max();
14✔
167
    const uint8_t result_param2 = 0;
14✔
168

169
    mavlink_message_t msg{};
14✔
170
    mavlink_msg_command_ack_pack(
14✔
171
        get_own_system_id(),
14✔
172
        get_own_component_id(),
14✔
173
        &msg,
174
        command.command,
14✔
175
        result,
176
        progress,
177
        result_param2,
178
        command.origin_system_id,
14✔
179
        command.origin_component_id);
14✔
180
    return msg;
14✔
181
}
182

183
mavlink_message_t ServerComponentImpl::make_command_ack_message(
×
184
    const MavlinkCommandReceiver::CommandInt& command, MAV_RESULT result)
185
{
186
    const uint8_t progress = std::numeric_limits<uint8_t>::max();
×
187
    const uint8_t result_param2 = 0;
×
188

189
    mavlink_message_t msg{};
×
190
    mavlink_msg_command_ack_pack(
×
191
        get_own_system_id(),
×
192
        get_own_component_id(),
×
193
        &msg,
194
        command.command,
×
195
        result,
196
        progress,
197
        result_param2,
198
        command.origin_system_id,
×
199
        command.origin_component_id);
×
200
    return msg;
×
201
}
202

203
void ServerComponentImpl::send_heartbeat()
22✔
204
{
205
    mavlink_message_t message;
22✔
206
    mavlink_msg_heartbeat_pack(
88✔
207
        get_own_system_id(),
22✔
208
        get_own_component_id(),
22✔
209
        &message,
210
        _mavsdk_impl.get_mav_type(),
22✔
211
        get_own_component_id() == MAV_COMP_ID_AUTOPILOT1 ? MAV_AUTOPILOT_GENERIC :
22✔
212
                                                           MAV_AUTOPILOT_INVALID,
213
        get_own_component_id() == MAV_COMP_ID_AUTOPILOT1 ? _base_mode.load() : 0,
42✔
214
        get_own_component_id() == MAV_COMP_ID_AUTOPILOT1 ? _custom_mode.load() : 0,
42✔
215
        get_system_status());
22✔
216
    send_message(message);
22✔
217
}
22✔
218

219
void ServerComponentImpl::set_system_status(uint8_t system_status)
×
220
{
221
    _system_status = static_cast<MAV_STATE>(system_status);
×
222
}
×
223

224
uint8_t ServerComponentImpl::get_system_status() const
22✔
225
{
226
    return _system_status;
22✔
227
}
228

229
void ServerComponentImpl::set_base_mode(uint8_t base_mode)
2✔
230
{
231
    _base_mode = base_mode;
2✔
232
}
2✔
233

234
uint8_t ServerComponentImpl::get_base_mode() const
2✔
235
{
236
    return _base_mode;
2✔
237
}
238

239
void ServerComponentImpl::set_custom_mode(uint32_t custom_mode)
×
240
{
241
    _custom_mode = custom_mode;
×
242
}
×
243

244
uint32_t ServerComponentImpl::get_custom_mode() const
×
245
{
246
    return _custom_mode;
×
247
}
248

249
void ServerComponentImpl::call_user_callback_located(
×
250
    const std::string& filename, const int linenumber, const std::function<void()>& func)
251
{
252
    _mavsdk_impl.call_user_callback_located(filename, linenumber, func);
×
253
}
×
254

255
void ServerComponentImpl::register_timeout_handler(
×
256
    const std::function<void()>& callback, double duration_s, void** cookie)
257
{
258
    _mavsdk_impl.timeout_handler.add(callback, duration_s, cookie);
×
259
}
×
260

261
void ServerComponentImpl::refresh_timeout_handler(const void* cookie)
×
262
{
263
    _mavsdk_impl.timeout_handler.refresh(cookie);
×
264
}
×
265

266
void ServerComponentImpl::unregister_timeout_handler(const void* cookie)
×
267
{
268
    _mavsdk_impl.timeout_handler.remove(cookie);
×
269
}
×
270

271
void ServerComponentImpl::add_capabilities(uint64_t add_capabilities)
1✔
272
{
273
    {
274
        std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
1✔
275
        _autopilot_version.capabilities |= add_capabilities;
1✔
276
    }
277

278
    // We need to resend capabilities.
279
    send_autopilot_version();
1✔
280
}
1✔
281

282
void ServerComponentImpl::set_flight_sw_version(uint32_t flight_sw_version)
×
283
{
284
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
285
    _autopilot_version.flight_sw_version = flight_sw_version;
×
286
}
×
287

288
void ServerComponentImpl::set_middleware_sw_version(uint32_t middleware_sw_version)
×
289
{
290
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
291
    _autopilot_version.middleware_sw_version = middleware_sw_version;
×
292
}
×
293

294
void ServerComponentImpl::set_os_sw_version(uint32_t os_sw_version)
×
295
{
296
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
297
    _autopilot_version.os_sw_version = os_sw_version;
×
298
}
×
299

300
void ServerComponentImpl::set_board_version(uint32_t board_version)
×
301
{
302
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
303
    _autopilot_version.board_version = board_version;
×
304
}
×
305

306
void ServerComponentImpl::set_vendor_id(uint16_t vendor_id)
×
307
{
308
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
309
    _autopilot_version.vendor_id = vendor_id;
×
310
}
×
311

312
void ServerComponentImpl::set_product_id(uint16_t product_id)
×
313
{
314
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
315
    _autopilot_version.product_id = product_id;
×
316
}
×
317

318
bool ServerComponentImpl::set_uid2(std::string uid2)
×
319
{
320
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
×
321
    if (uid2.size() > _autopilot_version.uid2.size()) {
×
322
        return false;
×
323
    }
324
    _autopilot_version.uid2 = {0};
×
325
    std::copy(uid2.begin(), uid2.end(), _autopilot_version.uid2.data());
×
326
    return true;
×
327
}
328

329
void ServerComponentImpl::send_autopilot_version()
12✔
330
{
331
    std::lock_guard<std::mutex> lock(_autopilot_version_mutex);
24✔
332
    const uint8_t custom_values[8] = {0}; // TO-DO: maybe?
12✔
333

334
    mavlink_message_t msg;
12✔
335
    mavlink_msg_autopilot_version_pack(
12✔
336
        _mavsdk_impl.get_own_system_id(),
12✔
337
        get_own_component_id(),
12✔
338
        &msg,
339
        _autopilot_version.capabilities,
340
        _autopilot_version.flight_sw_version,
341
        _autopilot_version.middleware_sw_version,
342
        _autopilot_version.os_sw_version,
343
        _autopilot_version.board_version,
344
        custom_values,
345
        custom_values,
346
        custom_values,
347
        _autopilot_version.vendor_id,
12✔
348
        _autopilot_version.product_id,
12✔
349
        0,
350
        _autopilot_version.uid2.data());
12✔
351

352
    _mavsdk_impl.send_message(msg);
12✔
353
}
12✔
354

355
ServerComponentImpl::OurSender::OurSender(
20✔
356
    MavsdkImpl& mavsdk_impl, ServerComponentImpl& server_component_impl) :
20✔
357
    _mavsdk_impl(mavsdk_impl),
358
    _server_component_impl(server_component_impl)
20✔
359
{}
20✔
360

361
bool ServerComponentImpl::OurSender::send_message(mavlink_message_t& message)
74✔
362
{
363
    return _mavsdk_impl.send_message(message);
74✔
364
}
365

366
uint8_t ServerComponentImpl::OurSender::get_own_system_id() const
115✔
367
{
368
    return _mavsdk_impl.get_own_system_id();
115✔
369
}
370

371
uint8_t ServerComponentImpl::OurSender::get_own_component_id() const
115✔
372
{
373
    return _server_component_impl.get_own_component_id();
115✔
374
}
375
uint8_t ServerComponentImpl::OurSender::get_system_id() const
7✔
376
{
377
    return current_target_system_id;
7✔
378
}
379

380
Sender::Autopilot ServerComponentImpl::OurSender::autopilot() const
32✔
381
{
382
    // FIXME: hard-coded to PX4 for now to avoid the dependency into mavsdk_impl.
383
    return Sender::Autopilot::Px4;
32✔
384
}
385

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