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

mavlink / MAVSDK / 12303784192

12 Dec 2024 07:54PM UTC coverage: 38.711% (+0.06%) from 38.647%
12303784192

push

github

web-flow
Merge pull request #2468 from mavlink/pr-get-all-param-fixes

get_all_params fixes

61 of 104 new or added lines in 4 files covered. (58.65%)

2 existing lines in 2 files now uncovered.

12136 of 31350 relevant lines covered (38.71%)

243.5 hits per line

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

58.63
/src/mavsdk/core/mavlink_parameter_client.cpp
1
#include "mavlink_parameter_helper.h"
2
#include "mavlink_parameter_client.h"
3
#include "mavlink_message_handler.h"
4
#include "system_impl.h"
5
#include "plugin_base.h"
6
#include <algorithm>
7
#include <future>
8
#include <limits>
9
#include <utility>
10

11
namespace mavsdk {
12

13
MavlinkParameterClient::MavlinkParameterClient(
8✔
14
    Sender& sender,
15
    MavlinkMessageHandler& message_handler,
16
    TimeoutHandler& timeout_handler,
17
    TimeoutSCallback timeout_s_callback,
18
    AutopilotCallback autopilot_callback,
19
    uint8_t target_system_id,
20
    uint8_t target_component_id,
21
    bool use_extended) :
8✔
22
    _sender(sender),
8✔
23
    _message_handler(message_handler),
8✔
24
    _timeout_handler(timeout_handler),
8✔
25
    _timeout_s_callback(std::move(timeout_s_callback)),
8✔
26
    _autopilot_callback(std::move(autopilot_callback)),
8✔
27
    _target_system_id(target_system_id),
8✔
28
    _target_component_id(target_component_id),
8✔
29
    _use_extended(use_extended)
16✔
30
{
31
    if (const char* env_p = std::getenv("MAVSDK_PARAMETER_DEBUGGING")) {
8✔
32
        if (std::string(env_p) == "1") {
×
33
            LogDebug() << "Parameter debugging is on.";
×
34
            _parameter_debugging = true;
×
35
        }
36
    }
37

38
    if (_parameter_debugging) {
8✔
39
        LogDebug() << "MavlinkParameterClient created for target compid: "
×
40
                   << (int)_target_component_id << " and "
×
41
                   << (_use_extended ? "extended" : "not extended");
×
42
    }
43

44
    if (_use_extended) {
8✔
45
        _message_handler.register_one(
4✔
46
            MAVLINK_MSG_ID_PARAM_EXT_VALUE,
47
            [this](const mavlink_message_t& message) { process_param_ext_value(message); },
22✔
48
            this);
49

50
        _message_handler.register_one(
4✔
51
            MAVLINK_MSG_ID_PARAM_EXT_ACK,
52
            [this](const mavlink_message_t& message) { process_param_ext_ack(message); },
2✔
53
            this);
54
    } else {
55
        _message_handler.register_one(
4✔
56
            MAVLINK_MSG_ID_PARAM_VALUE,
57
            [this](const mavlink_message_t& message) { process_param_value(message); },
26✔
58
            this);
59
    }
60
}
8✔
61

62
MavlinkParameterClient::~MavlinkParameterClient()
8✔
63
{
64
    if (_parameter_debugging) {
8✔
65
        LogDebug() << "MavlinkParameterClient destructed for target compid: "
×
66
                   << (int)_target_component_id << " and "
×
67
                   << (_use_extended ? "extended" : "not extended");
×
68
    }
69

70
    _message_handler.unregister_all(this);
8✔
71
}
8✔
72

73
MavlinkParameterClient::Result
74
MavlinkParameterClient::set_param(const std::string& name, const ParamValue& value)
×
75
{
76
    auto prom = std::promise<Result>();
×
77
    auto res = prom.get_future();
×
78
    set_param_async(name, value, [&prom](Result result) { prom.set_value(result); }, this);
×
79
    return res.get();
×
80
}
×
81

82
void MavlinkParameterClient::set_param_async(
6✔
83
    const std::string& name,
84
    const ParamValue& value,
85
    const SetParamCallback& callback,
86
    const void* cookie)
87
{
88
    if (name.size() > PARAM_ID_LEN) {
6✔
89
        LogErr() << "Param name too long";
×
90
        if (callback) {
×
91
            callback(Result::ParamNameTooLong);
×
92
        }
93
        return;
×
94
    }
95
    if (value.is<std::string>() && !_use_extended) {
6✔
96
        LogErr() << "String needs extended parameter protocol";
×
97
        if (callback) {
×
98
            callback(Result::StringTypeUnsupported);
×
99
        }
100
        return;
×
101
    }
102
    auto new_work = std::make_shared<WorkItem>(WorkItemSet{name, value, callback}, cookie);
12✔
103
    _work_queue.push_back(new_work);
6✔
104
}
6✔
105

106
void MavlinkParameterClient::set_param_int_async(
2✔
107
    const std::string& name, int32_t value, const SetParamCallback& callback, const void* cookie)
108
{
109
    if (name.size() > PARAM_ID_LEN) {
2✔
110
        LogErr() << "Param name too long";
×
111
        if (callback) {
×
112
            callback(Result::ParamNameTooLong);
×
113
        }
114
        return;
×
115
    }
116

117
    // PX4 only uses int32_t, so we can be sure and don't need to check the exact type first
118
    // by getting the param, or checking the cache.
119
    if (_autopilot_callback() == Autopilot::Px4) {
2✔
120
        ParamValue value_to_set;
×
121
        value_to_set.set(static_cast<int32_t>(value));
×
122
        set_param_async(name, value_to_set, callback, cookie);
×
123
    } else {
×
124
        // We don't know which exact int type the server wants, so we have to get the param
125
        // first to see the type before setting it.
126
        auto param_opt = _param_cache.param_by_id(name, false);
2✔
127
        if (param_opt.has_value()) {
2✔
128
            // we have the parameter cached
129
            auto param = param_opt.value();
×
130
            if (param.value.set_int(value)) {
×
131
                // we have successfully written whatever int the user provided into the int type
132
                // that is actually stored
133
                set_param_async(name, param.value, callback, cookie);
×
134
            } else {
135
                // We didn't find compatibility and give up.
136
                LogErr() << "Wrong type for int in cache";
×
137
                if (callback) {
×
138
                    callback(Result::WrongType);
×
139
                }
140
                return;
×
141
            }
142
        } else {
×
143
            // parameter is not cached. Request it and then perform the appropriate action once we
144
            // know it
145
            auto send_message_once_type_is_known = [this, name, value, callback, cookie](
2✔
146
                                                       Result result,
147
                                                       ParamValue fetched_param_value) {
4✔
148
                if (result == Result::Success) {
2✔
149
                    if (fetched_param_value.set_int(value)) {
2✔
150
                        // Since the callback itself is called with the work queue locked, we have
151
                        // to make sure that the work queue guard is removed before we call the
152
                        // finalizing callback of a work item.
153
                        set_param_async(name, fetched_param_value, callback, cookie);
2✔
154
                    } else {
155
                        // The param type returned does is not compatible with an int, give up.
156
                        LogErr() << "Wrong type for int returned";
×
157
                        if (callback) {
×
158
                            callback(Result::WrongType);
×
159
                        }
160
                    }
161
                } else {
162
                    // Failed to get the param to get the type, pass on the error.
163
                    callback(result);
×
164
                }
165
            };
2✔
166
            get_param_async(name, send_message_once_type_is_known, cookie);
2✔
167
        }
2✔
168
    }
2✔
169
}
170

171
MavlinkParameterClient::Result
172
MavlinkParameterClient::set_param_int(const std::string& name, int32_t value)
2✔
173
{
174
    auto prom = std::promise<Result>();
2✔
175
    auto res = prom.get_future();
2✔
176
    set_param_int_async(name, value, [&prom](Result result) { prom.set_value(result); }, this);
4✔
177
    return res.get();
2✔
178
}
2✔
179

180
void MavlinkParameterClient::set_param_float_async(
2✔
181
    const std::string& name, float value, const SetParamCallback& callback, const void* cookie)
182
{
183
    ParamValue value_to_set;
2✔
184
    value_to_set.set_float(value);
2✔
185
    set_param_async(name, value_to_set, callback, cookie);
2✔
186
}
2✔
187

188
MavlinkParameterClient::Result
189
MavlinkParameterClient::set_param_float(const std::string& name, float value)
2✔
190
{
191
    auto prom = std::promise<Result>();
2✔
192
    auto res = prom.get_future();
2✔
193

194
    set_param_float_async(name, value, [&prom](Result result) { prom.set_value(result); }, this);
4✔
195

196
    return res.get();
2✔
197
}
2✔
198

199
void MavlinkParameterClient::set_param_custom_async(
2✔
200
    const std::string& name,
201
    const std::string& value,
202
    const SetParamCallback& callback,
203
    const void* cookie)
204
{
205
    if (name.size() > PARAM_ID_LEN) {
2✔
206
        LogErr() << "Param name too long";
×
207
        if (callback) {
×
208
            callback(Result::ParamNameTooLong);
×
209
        }
210
        return;
×
211
    }
212

213
    if (value.size() > sizeof(mavlink_param_ext_set_t::param_value)) {
2✔
214
        LogErr() << "Param value too long";
×
215
        if (callback) {
×
216
            callback(Result::ParamValueTooLong);
×
217
        }
218
        return;
×
219
    }
220
    ParamValue value_to_set;
2✔
221
    value_to_set.set_custom(value);
2✔
222
    set_param_async(name, value_to_set, callback, cookie);
2✔
223
}
2✔
224

225
MavlinkParameterClient::Result
226
MavlinkParameterClient::set_param_custom(const std::string& name, const std::string& value)
2✔
227
{
228
    auto prom = std::promise<Result>();
2✔
229
    auto res = prom.get_future();
2✔
230
    set_param_custom_async(name, value, [&prom](Result result) { prom.set_value(result); }, this);
4✔
231
    return res.get();
2✔
232
}
2✔
233

234
void MavlinkParameterClient::get_param_async(
16✔
235
    const std::string& name, const GetParamAnyCallback& callback, const void* cookie)
236
{
237
    if (_parameter_debugging) {
16✔
238
        LogDebug() << "Getting param " << name << ", extended: " << (_use_extended ? "yes" : "no");
×
239
    }
240
    if (name.size() > PARAM_ID_LEN) {
16✔
241
        LogErr() << "Param name too long";
×
242
        if (callback) {
×
243
            callback(Result::ParamNameTooLong, {});
×
244
        }
245
        return;
×
246
    }
247

248
    auto new_work = std::make_shared<WorkItem>(WorkItemGet{name, callback}, cookie);
32✔
249
    _work_queue.push_back(new_work);
16✔
250
}
16✔
251

252
void MavlinkParameterClient::get_param_async(
×
253
    const std::string& name,
254
    const ParamValue& value_type,
255
    const GetParamAnyCallback& callback,
256
    const void* cookie)
257
{
258
    // We need to delay the type checking until we get a response from the server.
259
    GetParamAnyCallback callback_future_result = [callback,
×
260
                                                  value_type](Result result, ParamValue value) {
261
        if (result == Result::Success) {
×
262
            if (value.is_same_type(value_type)) {
×
263
                callback(Result::Success, std::move(value));
×
264
            } else {
265
                callback(Result::WrongType, {});
×
266
            }
267
        } else {
268
            callback(result, {});
×
269
        }
270
    };
×
271
    get_param_async(name, callback_future_result, cookie);
×
272
}
×
273

274
template<class T>
275
void MavlinkParameterClient::get_param_async_typesafe(
9✔
276
    const std::string& name, const GetParamTypesafeCallback<T> callback, const void* cookie)
277
{
278
    // We need to delay the type checking until we get a response from the server.
279
    GetParamAnyCallback callback_future_result = [callback](Result result, ParamValue value) {
36✔
280
        if (result == Result::Success) {
9✔
281
            if (value.is<T>()) {
8✔
282
                callback(Result::Success, value.get<T>());
8✔
283
            } else {
284
                callback(Result::WrongType, {});
×
285
            }
286
        } else {
287
            callback(result, {});
1✔
288
        }
289
    };
290
    get_param_async(name, callback_future_result, cookie);
9✔
291
}
9✔
292

293
template<>
294
void MavlinkParameterClient::get_param_async_typesafe(
5✔
295
    const std::string& name, const GetParamTypesafeCallback<int32_t> callback, const void* cookie)
296
{
297
    // We need to delay the type checking until we get a response from the server.
298
    GetParamAnyCallback callback_future_result = [callback](Result result, ParamValue value) {
5✔
299
        if (result == Result::Success) {
5✔
300
            if (value.is<int32_t>()) {
4✔
301
                callback(Result::Success, value.get<int32_t>());
4✔
302
            } else if (value.is<int16_t>()) {
×
303
                callback(Result::Success, value.get<int16_t>());
×
304
            } else if (value.is<int8_t>()) {
×
305
                callback(Result::Success, value.get<int8_t>());
×
306
            } else {
307
                callback(Result::WrongType, {});
×
308
            }
309
        } else {
310
            callback(result, {});
1✔
311
        }
312
    };
10✔
313
    get_param_async(name, callback_future_result, cookie);
5✔
314
}
5✔
315

316
void MavlinkParameterClient::get_param_float_async(
5✔
317
    const std::string& name, const GetParamFloatCallback& callback, const void* cookie)
318
{
319
    get_param_async_typesafe<float>(name, callback, cookie);
5✔
320
}
5✔
321

322
void MavlinkParameterClient::get_param_int_async(
5✔
323
    const std::string& name, const GetParamIntCallback& callback, const void* cookie)
324
{
325
    get_param_async_typesafe<int32_t>(name, callback, cookie);
5✔
326
}
5✔
327

328
void MavlinkParameterClient::get_param_custom_async(
4✔
329
    const std::string& name, const GetParamCustomCallback& callback, const void* cookie)
330
{
331
    get_param_async_typesafe<std::string>(name, callback, cookie);
4✔
332
}
4✔
333

334
std::pair<MavlinkParameterClient::Result, ParamValue>
335
MavlinkParameterClient::get_param(const std::string& name)
×
336
{
337
    auto prom = std::promise<std::pair<Result, ParamValue>>();
×
338
    auto res = prom.get_future();
×
339
    get_param_async(
×
340
        name,
341
        [&prom](Result result, ParamValue new_value) {
×
342
            prom.set_value(std::make_pair<>(result, std::move(new_value)));
×
343
        },
×
344
        this);
345
    return res.get();
×
346
}
×
347

348
std::pair<MavlinkParameterClient::Result, int32_t>
349
MavlinkParameterClient::get_param_int(const std::string& name)
5✔
350
{
351
    auto prom = std::promise<std::pair<Result, int32_t>>();
5✔
352
    auto res = prom.get_future();
5✔
353
    get_param_int_async(
5✔
354
        name,
355
        [&prom](Result result, int32_t value) { prom.set_value(std::make_pair<>(result, value)); },
5✔
356
        this);
357
    return res.get();
5✔
358
}
5✔
359

360
std::pair<MavlinkParameterClient::Result, float>
361
MavlinkParameterClient::get_param_float(const std::string& name)
5✔
362
{
363
    auto prom = std::promise<std::pair<Result, float>>();
5✔
364
    auto res = prom.get_future();
5✔
365
    get_param_float_async(
5✔
366
        name,
367
        [&prom](Result result, float value) { prom.set_value(std::make_pair<>(result, value)); },
5✔
368
        this);
369
    return res.get();
5✔
370
}
5✔
371

372
std::pair<MavlinkParameterClient::Result, std::string>
373
MavlinkParameterClient::get_param_custom(const std::string& name)
4✔
374
{
375
    auto prom = std::promise<std::pair<Result, std::string>>();
4✔
376
    auto res = prom.get_future();
4✔
377
    get_param_custom_async(
4✔
378
        name,
379
        [&prom](Result result, const std::string& value) {
4✔
380
            prom.set_value(std::make_pair<>(result, value));
4✔
381
        },
4✔
382
        this);
383
    return res.get();
4✔
384
}
4✔
385

386
void MavlinkParameterClient::get_all_params_async(GetAllParamsCallback callback, void* cookie)
4✔
387
{
388
    if (_parameter_debugging) {
4✔
389
        LogDebug() << "Getting all params, extended: " << (_use_extended ? "yes" : "no");
×
390
    }
391

392
    auto new_work =
4✔
393
        std::make_shared<WorkItem>(WorkItemGetAll{std::move(callback), 0, false}, cookie);
8✔
394
    _work_queue.push_back(new_work);
4✔
395
}
4✔
396

397
std::pair<MavlinkParameterClient::Result, std::map<std::string, ParamValue>>
398
MavlinkParameterClient::get_all_params()
4✔
399
{
400
    std::promise<std::pair<MavlinkParameterClient::Result, std::map<std::string, ParamValue>>> prom;
4✔
401
    auto res = prom.get_future();
4✔
402
    get_all_params_async(
4✔
403
        // Make sure to NOT use a reference for all_params here, pass by value.
404
        // Since for example on a timeout, the empty all_params result is constructed in-place and
405
        // then goes out of scope when the callback returns.
406
        [&prom](Result result, std::map<std::string, ParamValue> set) {
4✔
407
            prom.set_value({result, std::move(set)});
4✔
408
        },
4✔
409
        this);
410
    auto ret = res.get();
4✔
411
    return ret;
4✔
412
}
4✔
413

414
void MavlinkParameterClient::cancel_all_param(const void* cookie)
×
415
{
416
    LockedQueue<WorkItem>::Guard work_queue_guard(_work_queue);
×
417

418
    // We don't call any callbacks before erasing them as this is just used on destruction
419
    // where we don't care anymore.
420
    _work_queue.erase(std::remove_if(_work_queue.begin(), _work_queue.end(), [&](auto&& item) {
×
421
        return (item->cookie == cookie);
×
422
    }));
423
}
×
424

425
void MavlinkParameterClient::clear_cache()
20✔
426
{
427
    _param_cache.clear();
20✔
428
}
20✔
429

430
void MavlinkParameterClient::do_work()
477✔
431
{
432
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
477✔
433
    auto work = work_queue_guard->get_front();
477✔
434

435
    if (!work) {
477✔
436
        return;
159✔
437
    }
438

439
    if (work->already_requested) {
318✔
440
        return;
292✔
441
    }
442

443
    std::visit(
52✔
444
        overloaded{
26✔
445
            [&](WorkItemSet& item) {
6✔
446
                if (!send_set_param_message(item)) {
6✔
447
                    LogErr() << "Send message failed";
×
448
                    work_queue_guard->pop_front();
×
449
                    if (item.callback) {
×
450
                        auto callback = item.callback;
×
451
                        work_queue_guard.reset();
×
452
                        callback(Result::ConnectionError);
×
453
                    }
×
454
                    return;
×
455
                }
456
                work->already_requested = true;
6✔
457
                // We want to get notified if a timeout happens
458
                _timeout_cookie =
6✔
459
                    _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
13✔
460
            },
461
            [&](WorkItemGet& item) {
16✔
462
                // We can't rely on the cache as we haven't implemented the hash check.
463
                clear_cache();
16✔
464
                if (!send_get_param_message(item)) {
16✔
465
                    LogErr() << "Send message failed";
×
466
                    work_queue_guard->pop_front();
×
467
                    if (item.callback) {
×
468
                        auto callback = item.callback;
×
469
                        work_queue_guard.reset();
×
470
                        item.callback(Result::ConnectionError, ParamValue{});
×
471
                    }
×
472
                    return;
×
473
                }
474
                work->already_requested = true;
16✔
475
                // We want to get notified if a timeout happens
476
                _timeout_cookie =
16✔
477
                    _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
37✔
478
            },
479
            [&](WorkItemGetAll& item) {
4✔
480
                // We can't rely on the cache as we haven't implemented the hash check.
481
                clear_cache();
4✔
482
                if (!send_request_list_message()) {
4✔
483
                    LogErr() << "Send message failed";
×
484
                    work_queue_guard->pop_front();
×
485
                    if (item.callback) {
×
486
                        auto callback = item.callback;
×
487
                        work_queue_guard.reset();
×
488
                        item.callback(Result::ConnectionError, {});
×
489
                    }
×
490
                    return;
×
491
                }
492
                work->already_requested = true;
4✔
493
                // We want to get notified if a timeout happens
494
                _timeout_cookie = _timeout_handler.add(
4✔
495
                    [this] { receive_timeout(); }, _timeout_s_callback() * _get_all_timeout_factor);
6✔
496
            }},
497
        work->work_item_variant);
26✔
498
}
928✔
499

500
bool MavlinkParameterClient::send_set_param_message(WorkItemSet& work_item)
7✔
501
{
502
    auto param_id = param_id_to_message_buffer(work_item.param_name);
7✔
503

504
    mavlink_message_t message;
7✔
505
    if (_use_extended) {
7✔
506
        const auto param_value_buf = work_item.param_value.get_128_bytes();
3✔
507
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
6✔
508
            if (_parameter_debugging) {
3✔
509
                LogDebug() << "Sending param_ext_set to:" << (int)mavlink_address.system_id << ":"
×
510
                           << (int)mavlink_address.component_id;
×
511
            }
512

513
            mavlink_msg_param_ext_set_pack_chan(
6✔
514
                mavlink_address.system_id,
3✔
515
                mavlink_address.component_id,
3✔
516
                channel,
517
                &message,
6✔
518
                _target_system_id,
3✔
519
                _target_component_id,
3✔
520
                param_id.data(),
3✔
521
                param_value_buf.data(),
3✔
522
                work_item.param_value.get_mav_param_ext_type());
3✔
523

524
            return message;
3✔
525
        });
3✔
526
    } else {
527
        const float value_set = (_autopilot_callback() == Autopilot::ArduPilot) ?
4✔
528
                                    work_item.param_value.get_4_float_bytes_cast() :
×
529
                                    work_item.param_value.get_4_float_bytes_bytewise();
4✔
530

531
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
8✔
532
            if (_parameter_debugging) {
4✔
533
                LogDebug() << "Sending param_set to:" << (int)mavlink_address.system_id << ":"
×
534
                           << (int)mavlink_address.component_id;
×
535
            }
536
            mavlink_msg_param_set_pack_chan(
8✔
537
                mavlink_address.system_id,
4✔
538
                mavlink_address.component_id,
4✔
539
                channel,
540
                &message,
8✔
541
                _target_system_id,
4✔
542
                _target_component_id,
4✔
543
                param_id.data(),
4✔
544
                value_set,
4✔
545
                work_item.param_value.get_mav_param_type());
4✔
546
            return message;
4✔
547
        });
4✔
548
    }
549
}
550

551
bool MavlinkParameterClient::send_get_param_message(WorkItemGet& work_item)
29✔
552
{
553
    std::array<char, PARAM_ID_LEN> param_id_buff{};
29✔
554
    int16_t param_index = -1;
29✔
555
    if (auto str = std::get_if<std::string>(&work_item.param_identifier)) {
29✔
556
        param_id_buff = param_id_to_message_buffer(*str);
29✔
557
    } else {
558
        // param_id_buff doesn't matter
559
        param_index = std::get<int16_t>(work_item.param_identifier);
×
560
    }
561

562
    return send_get_param_message(param_id_buff, param_index);
29✔
563
}
564

565
bool MavlinkParameterClient::send_get_param_message(
34✔
566
    const std::array<char, PARAM_ID_LEN>& param_id_buff, int16_t param_index)
567
{
568
    mavlink_message_t message;
34✔
569

570
    if (_use_extended) {
34✔
571
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
14✔
572
            if (_parameter_debugging) {
7✔
573
                LogDebug() << "Send param_ext_request_read: " << (int)mavlink_address.system_id
×
574
                           << ":" << (int)mavlink_address.component_id << " to "
×
575
                           << (int)_target_system_id << ":" << (int)_target_component_id;
×
576
            }
577
            mavlink_msg_param_ext_request_read_pack_chan(
14✔
578
                mavlink_address.system_id,
7✔
579
                mavlink_address.component_id,
7✔
580
                channel,
581
                &message,
14✔
582
                _target_system_id,
7✔
583
                _target_component_id,
7✔
584
                param_id_buff.data(),
7✔
585
                param_index);
7✔
586
            return message;
7✔
587
        });
7✔
588

589
    } else {
590
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
54✔
591
            if (_parameter_debugging) {
27✔
592
                LogDebug() << "Send param_request_read: " << (int)mavlink_address.system_id << ":"
×
593
                           << (int)mavlink_address.component_id << " to " << (int)_target_system_id
×
594
                           << ":" << (int)_target_component_id;
×
595
            }
596
            mavlink_msg_param_request_read_pack_chan(
54✔
597
                mavlink_address.system_id,
27✔
598
                mavlink_address.component_id,
27✔
599
                channel,
600
                &message,
54✔
601
                _target_system_id,
27✔
602
                _target_component_id,
27✔
603
                param_id_buff.data(),
27✔
604
                param_index);
27✔
605
            return message;
27✔
606
        });
27✔
607
    }
608
}
609

610
bool MavlinkParameterClient::send_request_list_message()
4✔
611
{
612
    if (_use_extended) {
4✔
613
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
4✔
614
            if (_parameter_debugging) {
2✔
615
                LogDebug() << "Sending param_ext_request_list to:" << (int)mavlink_address.system_id
×
616
                           << ":" << (int)mavlink_address.component_id;
×
617
            }
618
            mavlink_message_t message;
619
            mavlink_msg_param_ext_request_list_pack_chan(
2✔
620
                mavlink_address.system_id,
2✔
621
                mavlink_address.component_id,
2✔
622
                channel,
623
                &message,
624
                _target_system_id,
2✔
625
                _target_component_id);
2✔
626
            return message;
2✔
627
        });
2✔
628
    } else {
629
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
4✔
630
            if (_parameter_debugging) {
2✔
631
                LogDebug() << "Sending param_request_list to:" << (int)mavlink_address.system_id
×
632
                           << ":" << (int)mavlink_address.component_id;
×
633
            }
634
            mavlink_message_t message;
635
            mavlink_msg_param_request_list_pack_chan(
2✔
636
                mavlink_address.system_id,
2✔
637
                mavlink_address.component_id,
2✔
638
                channel,
639
                &message,
640
                _target_system_id,
2✔
641
                _target_component_id);
2✔
642
            return message;
2✔
643
        });
2✔
644
    }
645
}
646

647
void MavlinkParameterClient::process_param_value(const mavlink_message_t& message)
26✔
648
{
649
    mavlink_param_value_t param_value;
26✔
650
    mavlink_msg_param_value_decode(&message, &param_value);
26✔
651
    const std::string safe_param_id = extract_safe_param_id(param_value.param_id);
26✔
652
    if (safe_param_id.empty()) {
26✔
653
        LogWarn() << "Got ill-formed param_value message (param_id empty)";
×
654
        return;
×
655
    }
656

657
    ParamValue received_value;
26✔
658
    const bool set_value_success = received_value.set_from_mavlink_param_value(
26✔
659
        param_value,
660
        (_autopilot_callback() == Autopilot::ArduPilot) ? ParamValue::Conversion::Cast :
26✔
661
                                                          ParamValue::Conversion::Bitwise);
26✔
662
    if (!set_value_success) {
26✔
663
        LogWarn() << "Got ill-formed param_ext_value message (param_type unknown)";
×
664
        return;
×
665
    }
666

667
    if (_parameter_debugging) {
26✔
NEW
668
        LogDebug() << "process_param_value: " << safe_param_id << " " << received_value
×
NEW
669
                   << ", index: " << param_value.param_index;
×
670
    }
671

672
    if (param_value.param_index == std::numeric_limits<uint16_t>::max()) {
26✔
673
        // Ignore PX4's _HASH_CHECK param.
NEW
674
        return;
×
675
    }
676

677
    // We need to use a unique pointer here to remove the lock from the work queue manually "early"
678
    // before calling the (perhaps user-provided) callback. Otherwise, we might end up in a deadlock
679
    // if the callback wants to push another work item onto the queue. By using a unique ptr there
680
    // is no risk of forgetting to remove the lock - it is destroyed (if still valid) after going
681
    // out of scope.
682
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
26✔
683
    const auto work = work_queue_guard->get_front();
26✔
684
    if (!work) {
26✔
685
        return;
×
686
    }
687

688
    if (!work->already_requested) {
26✔
689
        return;
×
690
    }
691

692
    std::visit(
52✔
693
        overloaded{
26✔
694
            [&](WorkItemSet& item) {
4✔
695
                if (item.param_name != safe_param_id) {
4✔
696
                    // No match, let's just return the borrowed work item.
697
                    return;
×
698
                }
699

700
                if (_parameter_debugging) {
4✔
701
                    LogDebug() << "Item value is: " << item.param_value
×
702
                               << ", received: " << received_value;
×
703
                }
704

705
                if (item.param_value == received_value) {
4✔
706
                    // This was successful. Inform the caller.
707
                    _timeout_handler.remove(_timeout_cookie);
4✔
708
                    // LogDebug() << "time taken: " <<
709
                    // _sender.get_time().elapsed_since_s(_last_request_time);
710
                    work_queue_guard->pop_front();
4✔
711
                    if (item.callback) {
4✔
712
                        auto callback = item.callback;
4✔
713
                        work_queue_guard.reset();
4✔
714
                        callback(MavlinkParameterClient::Result::Success);
4✔
715
                    }
4✔
716
                } else {
717
                    // We might be receiving stale param_value messages, let's just
718
                    // try again. This can happen if the timeout is chosen low and we
719
                    // get out of sync when doing a param_get just before the param_set.
720
                    // In that case we have stale param_value messages in flux and
721
                    // receive them here.
722
                    if (work->retries_to_do > 0) {
×
723
                        LogWarn() << "sending again, retries to do: " << work->retries_to_do
×
724
                                  << "  (" << item.param_name << ").";
×
725

726
                        if (!send_set_param_message(item)) {
×
727
                            LogErr() << "connection send error in retransmit (" << item.param_name
×
728
                                     << ").";
×
729
                            work_queue_guard->pop_front();
×
730

731
                            if (item.callback) {
×
732
                                auto callback = item.callback;
×
733
                                work_queue_guard.reset();
×
734
                                callback(Result::ConnectionError);
×
735
                            }
×
736
                            _timeout_handler.refresh(_timeout_cookie);
×
737
                        } else {
738
                            --work->retries_to_do;
×
739
                            _timeout_handler.refresh(_timeout_cookie);
×
740
                        }
741
                    } else {
742
                        // We have tried retransmitting, giving up now.
743
                        LogErr() << "Error: Retrying failed set param failed: " << item.param_name;
×
744
                        work_queue_guard->pop_front();
×
745
                        if (item.callback) {
×
746
                            auto callback = item.callback;
×
747
                            work_queue_guard.reset();
×
748
                            callback(Result::Timeout);
×
749
                        }
×
750
                    }
751
                }
752
            },
753
            [&](WorkItemGet& item) {
10✔
754
                if (!validate_id_or_index(
10✔
755
                        item.param_identifier,
10✔
756
                        safe_param_id,
10✔
757
                        static_cast<int16_t>(param_value.param_index))) {
10✔
758
                    LogWarn() << "Got unexpected response on work item";
×
759
                    // No match, let's just return the borrowed work item.
760
                    return;
×
761
                }
762
                _timeout_handler.remove(_timeout_cookie);
10✔
763
                // LogDebug() << "time taken: " <<
764
                // _sender.get_time().elapsed_since_s(_last_request_time);
765
                work_queue_guard->pop_front();
10✔
766
                if (item.callback) {
10✔
767
                    auto callback = item.callback;
10✔
768
                    work_queue_guard.reset();
10✔
769
                    callback(Result::Success, received_value);
10✔
770
                }
10✔
771
            },
772
            [&](WorkItemGetAll& item) {
12✔
773
                auto maybe_current_missing_index = _param_cache.last_missing_requested();
12✔
774

775
                switch (_param_cache.add_new_param(
12✔
776
                    safe_param_id, received_value, param_value.param_index)) {
12✔
777
                    case MavlinkParameterCache::AddNewParamResult::AlreadyExists:
12✔
778
                        // FALLTHROUGH
779
                        // We don't care if it already exists, just overwrite it and carry on.
780
                        // The reason is that this can likely happen if the very first
781
                        // request_list is sent twice and hence we get a bunch of duplicate
782
                        // params.
783
                    case MavlinkParameterCache::AddNewParamResult::Ok:
784

785
                        if (item.count != param_value.param_count) {
12✔
786
                            item.count = param_value.param_count;
2✔
787
                            if (_parameter_debugging) {
2✔
NEW
788
                                LogDebug() << "Count is now " << item.count;
×
789
                            }
790
                        }
791

792
                        if (_parameter_debugging) {
12✔
NEW
793
                            LogDebug() << "Received param: " << param_value.param_index;
×
794
                        }
795

796
                        if (_param_cache.count(_use_extended) == param_value.param_count) {
12✔
797
                            _timeout_handler.remove(_timeout_cookie);
2✔
798
                            if (_parameter_debugging) {
2✔
NEW
799
                                LogDebug() << "Getting all parameters complete: "
×
800
                                           << (_use_extended ? "extended" : "not extended");
×
801
                            }
802
                            work_queue_guard->pop_front();
2✔
803
                            if (item.callback) {
2✔
804
                                auto callback = item.callback;
2✔
805
                                work_queue_guard.reset();
2✔
806
                                callback(
4✔
807
                                    Result::Success,
808
                                    _param_cache.all_parameters_map(_use_extended));
4✔
809
                            }
2✔
810
                        } else {
811
                            if (_parameter_debugging) {
10✔
NEW
812
                                LogDebug() << "Received " << _param_cache.count(_use_extended)
×
NEW
813
                                           << " of " << param_value.param_count;
×
814
                            }
815
                            if (item.rerequesting) {
10✔
NEW
816
                                if (maybe_current_missing_index == param_value.param_index) {
×
817
                                    // Looks like the last one of the previous retransmission chunk
818
                                    // was done, start another one.
NEW
819
                                    if (!request_next_missing(item.count)) {
×
NEW
820
                                        work_queue_guard->pop_front();
×
NEW
821
                                        if (item.callback) {
×
NEW
822
                                            auto callback = item.callback;
×
NEW
823
                                            work_queue_guard.reset();
×
NEW
824
                                            callback(Result::ConnectionError, {});
×
NEW
825
                                        }
×
NEW
826
                                        return;
×
827
                                    }
828
                                }
829
                            } else {
830
                                // update the timeout handler, messages are still coming in.
831
                            }
832
                            _timeout_handler.refresh(_timeout_cookie);
10✔
833
                        }
834
                        break;
12✔
835
                    case MavlinkParameterCache::AddNewParamResult::TooManyParams:
×
836
                        // We shouldn't be able to get here as the incoming type is only an
837
                        // uint16_t.
838
                        LogErr() << "Too many params received";
×
839
                        assert(false);
×
840
                        break;
841
                    default:
×
842
                        LogErr() << "Unknown AddNewParamResult";
×
843
                        assert(false);
×
844
                        break;
845
                }
846
            }},
847
        work->work_item_variant);
26✔
848

849
    // find_and_call_subscriptions_value_changed(safe_param_id, received_value);
850
}
26✔
851

852
void MavlinkParameterClient::process_param_ext_value(const mavlink_message_t& message)
22✔
853
{
854
    mavlink_param_ext_value_t param_ext_value;
22✔
855
    mavlink_msg_param_ext_value_decode(&message, &param_ext_value);
22✔
856
    const auto safe_param_id = extract_safe_param_id(param_ext_value.param_id);
22✔
857
    if (safe_param_id.empty()) {
22✔
858
        LogWarn() << "Got ill-formed param_ext_value message (param_id empty)";
×
859
        return;
×
860
    }
861
    ParamValue received_value;
22✔
862
    if (!received_value.set_from_mavlink_param_ext_value(param_ext_value)) {
22✔
863
        LogWarn() << "Got ill-formed param_ext_value message (param_type unknown)";
×
864
        return;
×
865
    }
866

867
    if (_parameter_debugging) {
22✔
868
        LogDebug() << "process param_ext_value: " << safe_param_id << " " << received_value;
×
869
    }
870

871
    // See comments on process_param_value for use of unique_ptr
872
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
22✔
873
    auto work = work_queue_guard->get_front();
22✔
874
    if (!work) {
22✔
875
        return;
×
876
    }
877
    if (!work->already_requested) {
22✔
878
        return;
×
879
    }
880

881
    std::visit(
44✔
882
        overloaded{
22✔
883
            [&](WorkItemSet&) {
×
884
                if (_parameter_debugging) {
×
885
                    LogDebug() << "Unexpected ParamExtValue response.";
×
886
                }
887
            },
×
888
            [&](WorkItemGet& item) {
4✔
889
                if (!validate_id_or_index(
4✔
890
                        item.param_identifier,
4✔
891
                        safe_param_id,
4✔
892
                        static_cast<int16_t>(param_ext_value.param_index))) {
4✔
893
                    LogWarn() << "Got unexpected response on work item";
×
894
                    // No match, let's just return the borrowed work item.
895
                    return;
×
896
                }
897
                _timeout_handler.remove(_timeout_cookie);
4✔
898
                // LogDebug() << "time taken: " <<
899
                // _sender.get_time().elapsed_since_s(_last_request_time);
900
                work_queue_guard->pop_front();
4✔
901
                if (item.callback) {
4✔
902
                    auto callback = item.callback;
4✔
903
                    work_queue_guard.reset();
4✔
904
                    callback(Result::Success, received_value);
4✔
905
                }
4✔
906
            },
907
            [&](WorkItemGetAll& item) {
18✔
908
                switch (_param_cache.add_new_param(
18✔
909
                    safe_param_id, received_value, param_ext_value.param_index)) {
18✔
910
                    case MavlinkParameterCache::AddNewParamResult::AlreadyExists:
18✔
911
                        // PASSTHROUGH.
912
                    case MavlinkParameterCache::AddNewParamResult::Ok:
913
                        item.count = param_ext_value.param_count;
18✔
914
                        if (_parameter_debugging) {
18✔
915
                            LogDebug() << "Count is now " << item.count;
×
916
                        }
917

918
                        if (_param_cache.count(_use_extended) == param_ext_value.param_count) {
18✔
919
                            _timeout_handler.remove(_timeout_cookie);
2✔
920
                            if (_parameter_debugging) {
2✔
NEW
921
                                LogDebug() << "Getting all parameters complete: "
×
922
                                           << (_use_extended ? "extended" : "not extended");
×
923
                            }
924
                            work_queue_guard->pop_front();
2✔
925
                            if (item.callback) {
2✔
926
                                auto callback = item.callback;
2✔
927
                                work_queue_guard.reset();
2✔
928
                                callback(
4✔
929
                                    Result::Success,
930
                                    _param_cache.all_parameters_map(_use_extended));
4✔
931
                            }
2✔
932
                        } else {
933
                            if (_parameter_debugging) {
16✔
NEW
934
                                LogDebug() << "Received " << _param_cache.count(_use_extended)
×
NEW
935
                                           << " of " << param_ext_value.param_count;
×
936
                            }
937
                            // update the timeout handler, messages are still coming in.
938
                            _timeout_handler.refresh(_timeout_cookie);
16✔
939
                        }
940
                        break;
18✔
941
                    case MavlinkParameterCache::AddNewParamResult::TooManyParams:
×
942
                        // We shouldn't be able to get here as the incoming type is only an
943
                        // uint16_t.
944
                        LogErr() << "Too many params received";
×
945
                        assert(false);
×
946
                        break;
947
                    default:
×
948
                        LogErr() << "Unknown AddNewParamResult";
×
949
                        assert(false);
×
950
                        break;
951
                }
952
            },
18✔
953
        },
954
        work->work_item_variant);
22✔
955

956
    // TODO I think we need to consider more edge cases here
957
    // find_and_call_subscriptions_value_changed(safe_param_id, received_value);
958
}
22✔
959

960
void MavlinkParameterClient::process_param_ext_ack(const mavlink_message_t& message)
2✔
961
{
962
    mavlink_param_ext_ack_t param_ext_ack;
2✔
963
    mavlink_msg_param_ext_ack_decode(&message, &param_ext_ack);
2✔
964
    const auto safe_param_id = extract_safe_param_id(param_ext_ack.param_id);
2✔
965

966
    if (_parameter_debugging) {
2✔
967
        LogDebug() << "process param_ext_ack: " << safe_param_id << " "
×
968
                   << (int)param_ext_ack.param_result;
×
969
    }
970

971
    // See comments on process_param_value for use of unique_ptr
972
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
2✔
973
    auto work = work_queue_guard->get_front();
2✔
974
    if (!work) {
2✔
975
        return;
×
976
    }
977
    if (!work->already_requested) {
2✔
978
        return;
×
979
    }
980

981
    std::visit(
4✔
982
        overloaded{
2✔
983
            [&](WorkItemSet& item) {
2✔
984
                if (item.param_name != safe_param_id) {
2✔
985
                    // No match, let's just return the borrowed work item.
986
                    return;
×
987
                }
988
                if (param_ext_ack.param_result == PARAM_ACK_ACCEPTED) {
2✔
989
                    _timeout_handler.remove(_timeout_cookie);
2✔
990
                    // LogDebug() << "time taken: " <<
991
                    // _sender.get_time().elapsed_since_s(_last_request_time);
992
                    work_queue_guard->pop_front();
2✔
993
                    if (item.callback) {
2✔
994
                        auto callback = item.callback;
2✔
995
                        // We are done, inform caller and go back to idle
996
                        work_queue_guard.reset();
2✔
997
                        callback(Result::Success);
2✔
998
                    }
2✔
999
                } else if (param_ext_ack.param_result == PARAM_ACK_IN_PROGRESS) {
×
1000
                    // Reset timeout and wait again.
1001
                    _timeout_handler.refresh(_timeout_cookie);
×
1002

1003
                } else {
1004
                    LogWarn() << "Somehow we did not get an ack, we got: "
×
1005
                              << int(param_ext_ack.param_result);
×
1006
                    _timeout_handler.remove(_timeout_cookie);
×
1007
                    // LogDebug() << "time taken: " <<
1008
                    // _sender.get_time().elapsed_since_s(_last_request_time);
1009
                    work_queue_guard->pop_front();
×
1010
                    work_queue_guard.reset();
×
1011
                    if (item.callback) {
×
1012
                        auto callback = item.callback;
×
1013
                        auto result = [&]() {
×
1014
                            switch (param_ext_ack.param_result) {
×
1015
                                case PARAM_ACK_FAILED:
×
1016
                                    return Result::Failed;
×
1017
                                case PARAM_ACK_VALUE_UNSUPPORTED:
×
1018
                                    return Result::ValueUnsupported;
×
1019
                                default:
×
1020
                                    return Result::UnknownError;
×
1021
                            }
1022
                        }();
×
1023
                        work_queue_guard.reset();
×
1024
                        callback(result);
×
1025
                    }
×
1026
                }
1027
            },
1028
            [&](WorkItemGet&) { LogWarn() << "Unexpected ParamExtAck response."; },
×
1029
            [&](WorkItemGetAll&) { LogWarn() << "Unexpected ParamExtAck response."; }},
×
1030
        work->work_item_variant);
2✔
1031
}
2✔
1032

1033
void MavlinkParameterClient::receive_timeout()
20✔
1034
{
1035
    // See comments on process_param_value for use of unique_ptr
1036
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
20✔
1037

1038
    auto work = work_queue_guard->get_front();
20✔
1039
    if (!work) {
20✔
1040
        LogErr() << "Received timeout without work";
×
1041
        return;
×
1042
    }
1043
    if (!work->already_requested) {
20✔
1044
        LogErr() << "Received timeout without already having work requested";
×
1045
        return;
×
1046
    }
1047

1048
    std::visit(
40✔
1049
        overloaded{
20✔
1050
            [&](WorkItemSet& item) {
1✔
1051
                if (work->retries_to_do > 0) {
1✔
1052
                    // We're not sure the command arrived, let's retransmit.
1053
                    LogWarn() << "sending again, retries to do: " << work->retries_to_do << "  ("
2✔
1054
                              << item.param_name << ").";
2✔
1055

1056
                    if (!send_set_param_message(item)) {
1✔
1057
                        LogErr() << "connection send error in retransmit (" << item.param_name
×
1058
                                 << ").";
×
1059
                        work_queue_guard->pop_front();
×
1060

1061
                        if (item.callback) {
×
1062
                            auto callback = item.callback;
×
1063
                            work_queue_guard.reset();
×
1064
                            callback(Result::ConnectionError);
×
1065
                        }
×
1066
                    } else {
1067
                        --work->retries_to_do;
1✔
1068
                        _timeout_cookie = _timeout_handler.add(
1✔
1069
                            [this] { receive_timeout(); }, _timeout_s_callback());
×
1070
                    }
1071
                } else {
1072
                    // We have tried retransmitting, giving up now.
1073
                    LogErr() << "Error: Retrying failed set param timeout: " << item.param_name;
×
1074
                    work_queue_guard->pop_front();
×
1075
                    if (item.callback) {
×
1076
                        auto callback = item.callback;
×
1077
                        work_queue_guard.reset();
×
1078
                        callback(Result::Timeout);
×
1079
                    }
×
1080
                }
1081
            },
1✔
1082
            [&](WorkItemGet& item) {
15✔
1083
                if (work->retries_to_do > 0) {
15✔
1084
                    // We're not sure the command arrived, let's retransmit.
1085
                    LogWarn() << "sending again, retries to do: " << work->retries_to_do;
13✔
1086
                    if (!send_get_param_message(item)) {
13✔
1087
                        LogErr() << "connection send error in retransmit ";
×
1088
                        work_queue_guard->pop_front();
×
1089
                        if (item.callback) {
×
1090
                            auto callback = item.callback;
×
1091
                            work_queue_guard.reset();
×
1092
                            callback(Result::ConnectionError, {});
×
1093
                        }
×
1094
                    } else {
1095
                        --work->retries_to_do;
13✔
1096
                        _timeout_cookie = _timeout_handler.add(
13✔
1097
                            [this] { receive_timeout(); }, _timeout_s_callback());
10✔
1098
                    }
1099
                } else {
1100
                    // We have tried retransmitting, giving up now.
1101
                    LogErr() << "retrying failed";
2✔
1102
                    work_queue_guard->pop_front();
2✔
1103
                    if (item.callback) {
2✔
1104
                        auto callback = item.callback;
2✔
1105
                        work_queue_guard.reset();
2✔
1106
                        callback(Result::Timeout, {});
2✔
1107
                    }
2✔
1108
                }
1109
            },
15✔
1110
            [&](WorkItemGetAll& item) {
4✔
1111
                // Request missing parameters.
1112
                // If retries are exceeded, give up with timeout.
1113

1114
                if (_parameter_debugging) {
4✔
1115
                    LogDebug() << "All params receive timeout with";
×
1116
                }
1117

1118
                if (item.count == 0) {
4✔
1119
                    // We got 0 messages back from the server (param count unknown). Most likely the
1120
                    // "list request" got lost before making it to the server,
1121
                    if (work->retries_to_do > 0) {
×
1122
                        --work->retries_to_do;
×
1123

1124
                        if (!send_request_list_message()) {
×
1125
                            LogErr() << "Send message failed";
×
1126
                            work_queue_guard->pop_front();
×
1127
                            if (item.callback) {
×
1128
                                auto callback = item.callback;
×
1129
                                work_queue_guard.reset();
×
1130
                                item.callback(Result::ConnectionError, {});
×
1131
                            }
×
1132
                            return;
×
1133
                        }
1134

1135
                        // We want to get notified if a timeout happens.
1136
                        _timeout_cookie = _timeout_handler.add(
×
NEW
1137
                            [this] { receive_timeout(); },
×
NEW
1138
                            _timeout_s_callback() * _get_all_timeout_factor);
×
1139
                    } else {
1140
                        if (item.callback) {
×
1141
                            auto callback = item.callback;
×
1142
                            work_queue_guard.reset();
×
1143
                            item.callback(Result::Timeout, {});
×
1144
                        }
×
1145
                        return;
×
1146
                    }
1147

1148
                } else {
1149
                    item.rerequesting = true;
4✔
1150

1151
                    LogInfo() << "Requesting " << _param_cache.missing_count(item.count) << " of "
12✔
1152
                              << item.count << " parameters missed during initial burst.";
12✔
1153

1154
                    if (_parameter_debugging) {
4✔
NEW
1155
                        _param_cache.print_missing(item.count);
×
1156
                    }
1157

1158
                    // To speed retransmissions up, we request params in chunks, otherwise the
1159
                    // latency back and forth makes this quite slow.
1160
                    if (!request_next_missing(item.count)) {
4✔
1161
                        work_queue_guard->pop_front();
×
1162
                        if (item.callback) {
×
1163
                            auto callback = item.callback;
×
1164
                            work_queue_guard.reset();
×
1165
                            callback(Result::ConnectionError, {});
×
1166
                        }
×
1167
                        return;
×
1168
                    }
1169

1170
                    _timeout_cookie =
4✔
1171
                        _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
10✔
1172
                }
1173
            }},
1174
        work->work_item_variant);
20✔
1175
}
20✔
1176

1177
bool MavlinkParameterClient::request_next_missing(uint16_t count)
4✔
1178
{
1179
    // Requesting 10 at a time seems to work on SiK radios.
1180
    const uint16_t chunk_size = 10;
4✔
1181

1182
    auto next_missing_indices = _param_cache.next_missing_indices(count, chunk_size);
4✔
1183
    if (next_missing_indices.empty()) {
4✔
NEW
1184
        LogErr() << "logic error, there should a missing index";
×
NEW
1185
        return false;
×
1186
    }
1187

1188
    for (auto next_missing_index : next_missing_indices) {
9✔
1189
        if (_parameter_debugging) {
5✔
NEW
1190
            LogDebug() << "Requesting missing parameter " << (int)next_missing_index;
×
1191
        }
1192

1193
        std::array<char, PARAM_ID_LEN> param_id_buff{};
5✔
1194

1195
        if (!send_get_param_message(param_id_buff, next_missing_index)) {
5✔
NEW
1196
            LogErr() << "Send message failed";
×
NEW
1197
            return false;
×
1198
        }
1199
    }
1200
    return true;
4✔
1201
}
4✔
1202

UNCOV
1203
std::ostream& operator<<(std::ostream& str, const MavlinkParameterClient::Result& result)
×
1204
{
1205
    switch (result) {
×
1206
        case MavlinkParameterClient::Result::Success:
×
1207
            return str << "Success";
×
1208
        case MavlinkParameterClient::Result::Timeout:
×
1209
            return str << "Timeout";
×
1210
        case MavlinkParameterClient::Result::ConnectionError:
×
1211
            return str << "ConnectionError";
×
1212
        case MavlinkParameterClient::Result::WrongType:
×
1213
            return str << "WrongType";
×
1214
        case MavlinkParameterClient::Result::ParamNameTooLong:
×
1215
            return str << "ParamNameTooLong";
×
1216
        case MavlinkParameterClient::Result::NotFound:
×
1217
            return str << "NotFound";
×
1218
        case MavlinkParameterClient::Result::ValueUnsupported:
×
1219
            return str << "ValueUnsupported";
×
1220
        case MavlinkParameterClient::Result::Failed:
×
1221
            return str << "Failed";
×
1222
        case MavlinkParameterClient::Result::UnknownError:
×
1223
            // Fallthrough
1224
        default:
1225
            return str << "UnknownError";
×
1226
    }
1227
}
1228

1229
bool MavlinkParameterClient::validate_id_or_index(
14✔
1230
    const std::variant<std::string, int16_t>& original,
1231
    const std::string& param_id,
1232
    const int16_t param_index)
1233
{
1234
    if (const auto str = std::get_if<std::string>(&original)) {
14✔
1235
        if (param_id != *str) {
14✔
1236
            // We requested by string id, but response doesn't match
1237
            return false;
×
1238
        }
1239
    } else {
1240
        const auto tmp = std::get<int16_t>(original);
×
1241
        if (param_index != tmp) {
×
1242
            // We requested by index, but response doesn't match
1243
            return false;
×
1244
        }
1245
    }
1246
    return true;
14✔
1247
}
1248

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