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

mavlink / MAVSDK / 16287155993

15 Jul 2025 07:40AM UTC coverage: 45.192% (-0.03%) from 45.22%
16287155993

push

github

web-flow
Merge pull request #2613 from mavlink/pr-fix-double-unlock

core: fix regression in locked_queue

7 of 8 new or added lines in 1 file covered. (87.5%)

10 existing lines in 3 files now uncovered.

15416 of 34112 relevant lines covered (45.19%)

117130.03 hits per line

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

58.03
/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 "overloaded.h"
6
#include <algorithm>
7
#include <future>
8
#include <limits>
9
#include <utility>
10

11
namespace mavsdk {
12

13
MavlinkParameterClient::MavlinkParameterClient(
10✔
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) :
10✔
22
    _sender(sender),
10✔
23
    _message_handler(message_handler),
10✔
24
    _timeout_handler(timeout_handler),
10✔
25
    _timeout_s_callback(std::move(timeout_s_callback)),
10✔
26
    _autopilot_callback(std::move(autopilot_callback)),
10✔
27
    _target_system_id(target_system_id),
10✔
28
    _target_component_id(target_component_id),
10✔
29
    _use_extended(use_extended)
20✔
30
{
31
    if (const char* env_p = std::getenv("MAVSDK_PARAMETER_DEBUGGING")) {
10✔
32
        if (std::string(env_p) == "1") {
×
33
            LogDebug() << "Parameter debugging is on.";
×
34
            _parameter_debugging = true;
×
35
        }
36
    }
37

38
    if (_parameter_debugging) {
10✔
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) {
10✔
45
        _message_handler.register_one(
6✔
46
            MAVLINK_MSG_ID_PARAM_EXT_VALUE,
47
            [this](const mavlink_message_t& message) { process_param_ext_value(message); },
76✔
48
            this);
49

50
        _message_handler.register_one(
6✔
51
            MAVLINK_MSG_ID_PARAM_EXT_ACK,
52
            [this](const mavlink_message_t& message) { process_param_ext_ack(message); },
5✔
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
}
10✔
61

62
MavlinkParameterClient::~MavlinkParameterClient()
10✔
63
{
64
    if (_parameter_debugging) {
10✔
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);
10✔
71
}
10✔
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(
9✔
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) {
9✔
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) {
9✔
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);
18✔
103
    _work_queue.push_back(new_work);
9✔
104
}
9✔
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(
57✔
235
    const std::string& name, const GetParamAnyCallback& callback, const void* cookie)
236
{
237
    if (_parameter_debugging) {
57✔
238
        LogDebug() << "Getting param " << name << ", extended: " << (_use_extended ? "yes" : "no");
×
239
    }
240
    if (name.size() > PARAM_ID_LEN) {
57✔
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);
114✔
249
    _work_queue.push_back(new_work);
57✔
250
}
57✔
251

252
void MavlinkParameterClient::get_param_async(
41✔
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,
41✔
260
                                                  value_type](Result result, ParamValue value) {
261
        if (result == Result::Success) {
41✔
262
            if (value.is_same_type(value_type)) {
41✔
263
                callback(Result::Success, std::move(value));
41✔
264
            } else {
265
                callback(Result::WrongType, {});
×
266
            }
267
        } else {
268
            callback(result, {});
×
269
        }
270
    };
82✔
271
    get_param_async(name, callback_future_result, cookie);
41✔
272
}
41✔
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()
61✔
426
{
427
    _param_cache.clear();
61✔
428
}
61✔
429

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

435
    if (!work) {
950✔
436
        return;
574✔
437
    }
438

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

443
    std::visit(
140✔
444
        overloaded{
70✔
445
            [&](WorkItemSet& item) {
9✔
446
                if (!send_set_param_message(item)) {
9✔
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;
9✔
457
                // We want to get notified if a timeout happens
458
                _timeout_cookie =
9✔
459
                    _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
18✔
460
            },
461
            [&](WorkItemGet& item) {
57✔
462
                // We can't rely on the cache as we haven't implemented the hash check.
463
                clear_cache();
57✔
464
                if (!send_get_param_message(item)) {
57✔
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;
57✔
475
                // We want to get notified if a timeout happens
476
                _timeout_cookie =
57✔
477
                    _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
121✔
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);
70✔
498
}
1,830✔
499

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

504
    mavlink_message_t message;
9✔
505
    if (_use_extended) {
9✔
506
        const auto param_value_buf = work_item.param_value.get_128_bytes();
5✔
507
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
10✔
508
            if (_parameter_debugging) {
5✔
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(
10✔
514
                mavlink_address.system_id,
5✔
515
                mavlink_address.component_id,
5✔
516
                channel,
517
                &message,
10✔
518
                _target_system_id,
5✔
519
                _target_component_id,
5✔
520
                param_id.data(),
5✔
521
                param_value_buf.data(),
5✔
522
                work_item.param_value.get_mav_param_ext_type());
5✔
523

524
            return message;
5✔
525
        });
5✔
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)
73✔
552
{
553
    std::array<char, PARAM_ID_LEN> param_id_buff{};
73✔
554
    int16_t param_index = -1;
73✔
555
    if (auto str = std::get_if<std::string>(&work_item.param_identifier)) {
73✔
556
        param_id_buff = param_id_to_message_buffer(*str);
73✔
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);
73✔
563
}
564

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

570
    if (_use_extended) {
78✔
571
        return _sender.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
102✔
572
            if (_parameter_debugging) {
51✔
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(
102✔
578
                mavlink_address.system_id,
51✔
579
                mavlink_address.component_id,
51✔
580
                channel,
581
                &message,
102✔
582
                _target_system_id,
51✔
583
                _target_component_id,
51✔
584
                param_id_buff.data(),
51✔
585
                param_index);
51✔
586
            return message;
51✔
587
        });
51✔
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✔
668
        LogDebug() << "process_param_value: " << safe_param_id << " " << received_value
×
669
                   << ", index: " << param_value.param_index;
×
670
    }
671

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

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

686
    if (!work) {
26✔
687
        // Prevent deadlock by releasing the lock before doing more work.
688
        work_queue_guard.reset();
×
689
        // update existing param
690
        find_and_call_subscriptions_value_changed(safe_param_id, received_value);
×
691
        return;
×
692
    }
693

694
    if (!work->already_requested) {
26✔
695
        return;
×
696
    }
697

698
    std::visit(
52✔
699
        overloaded{
26✔
700
            [&](WorkItemSet& item) {
4✔
701
                if (item.param_name != safe_param_id) {
4✔
702
                    // No match, let's just return the borrowed work item.
703
                    return;
×
704
                }
705

706
                if (_parameter_debugging) {
4✔
707
                    LogDebug() << "Item value is: " << item.param_value
×
708
                               << ", received: " << received_value;
×
709
                }
710

711
                if (!item.param_value.is_same_type(received_value)) {
4✔
712
                    LogErr() << "Wrong type in param set";
×
713
                    _timeout_handler.remove(_timeout_cookie);
×
714
                    work_queue_guard->pop_front();
×
715
                    if (item.callback) {
×
716
                        auto callback = item.callback;
×
717
                        work_queue_guard.reset();
×
718
                        callback(MavlinkParameterClient::Result::WrongType);
×
719
                    }
×
720
                    return;
×
721
                }
722

723
                if (item.param_value == received_value) {
4✔
724
                    // This was successful. Inform the caller.
725
                    _timeout_handler.remove(_timeout_cookie);
4✔
726
                    // LogDebug() << "time taken: " <<
727
                    // _sender.get_time().elapsed_since_s(_last_request_time);
728
                    work_queue_guard->pop_front();
4✔
729
                    if (item.callback) {
4✔
730
                        auto callback = item.callback;
4✔
731
                        work_queue_guard.reset();
4✔
732
                        callback(MavlinkParameterClient::Result::Success);
4✔
733
                    }
4✔
734
                } else {
735
                    // We might be receiving stale param_value messages, let's just
736
                    // try again. This can happen if the timeout is chosen low and we
737
                    // get out of sync when doing a param_get just before the param_set.
738
                    // In that case we have stale param_value messages in flux and
739
                    // receive them here.
740
                    if (work->retries_to_do > 0) {
×
741
                        LogWarn() << "sending again, retries to do: " << work->retries_to_do
×
742
                                  << "  (" << item.param_name << ").";
×
743

744
                        if (!send_set_param_message(item)) {
×
745
                            LogErr() << "connection send error in retransmit (" << item.param_name
×
746
                                     << ").";
×
747
                            work_queue_guard->pop_front();
×
748

749
                            if (item.callback) {
×
750
                                auto callback = item.callback;
×
751
                                work_queue_guard.reset();
×
752
                                callback(Result::ConnectionError);
×
753
                            }
×
754
                            _timeout_handler.refresh(_timeout_cookie);
×
755
                        } else {
756
                            --work->retries_to_do;
×
757
                            _timeout_handler.refresh(_timeout_cookie);
×
758
                        }
759
                    } else {
760
                        // We have tried retransmitting, giving up now.
761
                        LogErr() << "Error: Retrying failed set param failed: " << item.param_name;
×
762
                        work_queue_guard->pop_front();
×
763
                        if (item.callback) {
×
764
                            auto callback = item.callback;
×
765
                            work_queue_guard.reset();
×
766
                            callback(Result::Timeout);
×
767
                        }
×
768
                    }
769
                }
770
            },
771
            [&](WorkItemGet& item) {
10✔
772
                if (!validate_id_or_index(
10✔
773
                        item.param_identifier,
10✔
774
                        safe_param_id,
10✔
775
                        static_cast<int16_t>(param_value.param_index))) {
10✔
776
                    LogWarn() << "Got unexpected response on work item";
×
777
                    // No match, let's just return the borrowed work item.
778
                    return;
×
779
                }
780
                _timeout_handler.remove(_timeout_cookie);
10✔
781
                // LogDebug() << "time taken: " <<
782
                // _sender.get_time().elapsed_since_s(_last_request_time);
783
                work_queue_guard->pop_front();
10✔
784
                if (item.callback) {
10✔
785
                    auto callback = item.callback;
10✔
786
                    work_queue_guard.reset();
10✔
787
                    callback(Result::Success, received_value);
10✔
788
                }
10✔
789
            },
790
            [&](WorkItemGetAll& item) {
12✔
791
                auto maybe_current_missing_index = _param_cache.last_missing_requested();
12✔
792

793
                switch (_param_cache.add_new_param(
12✔
794
                    safe_param_id, received_value, param_value.param_index)) {
12✔
795
                    case MavlinkParameterCache::AddNewParamResult::AlreadyExists:
12✔
796
                        // FALLTHROUGH
797
                        // We don't care if it already exists, just overwrite it and carry on.
798
                        // The reason is that this can likely happen if the very first
799
                        // request_list is sent twice and hence we get a bunch of duplicate
800
                        // params.
801
                    case MavlinkParameterCache::AddNewParamResult::Ok:
802

803
                        if (item.count != param_value.param_count) {
12✔
804
                            item.count = param_value.param_count;
2✔
805
                            if (_parameter_debugging) {
2✔
806
                                LogDebug() << "Count is now " << item.count;
×
807
                            }
808
                        }
809

810
                        if (_parameter_debugging) {
12✔
811
                            LogDebug() << "Received param: " << param_value.param_index;
×
812
                        }
813

814
                        if (_param_cache.count(_use_extended) == param_value.param_count) {
12✔
815
                            _timeout_handler.remove(_timeout_cookie);
2✔
816
                            if (_parameter_debugging) {
2✔
817
                                LogDebug() << "Getting all parameters complete: "
×
818
                                           << (_use_extended ? "extended" : "not extended");
×
819
                            }
820
                            work_queue_guard->pop_front();
2✔
821
                            if (item.callback) {
2✔
822
                                auto callback = item.callback;
2✔
823
                                work_queue_guard.reset();
2✔
824
                                callback(
4✔
825
                                    Result::Success,
826
                                    _param_cache.all_parameters_map(_use_extended));
4✔
827
                            }
2✔
828
                        } else {
829
                            if (_parameter_debugging) {
10✔
830
                                LogDebug() << "Received " << _param_cache.count(_use_extended)
×
831
                                           << " of " << param_value.param_count;
×
832
                            }
833
                            if (item.rerequesting) {
10✔
834
                                if (maybe_current_missing_index == param_value.param_index) {
×
835
                                    // Looks like the last one of the previous retransmission chunk
836
                                    // was done, start another one.
837
                                    if (!request_next_missing(item.count)) {
×
838
                                        work_queue_guard->pop_front();
×
839
                                        if (item.callback) {
×
840
                                            auto callback = item.callback;
×
841
                                            work_queue_guard.reset();
×
842
                                            callback(Result::ConnectionError, {});
×
843
                                        }
×
844
                                        return;
×
845
                                    }
846
                                }
847
                            } else {
848
                                // update the timeout handler, messages are still coming in.
849
                            }
850
                            _timeout_handler.refresh(_timeout_cookie);
10✔
851
                        }
852
                        break;
12✔
853
                    case MavlinkParameterCache::AddNewParamResult::TooManyParams:
×
854
                        // We shouldn't be able to get here as the incoming type is only an
855
                        // uint16_t.
856
                        LogErr() << "Too many params received";
×
857
                        assert(false);
×
858
                        break;
859
                    default:
×
860
                        LogErr() << "Unknown AddNewParamResult";
×
861
                        assert(false);
×
862
                        break;
863
                }
864
            }},
865
        work->work_item_variant);
26✔
866
}
26✔
867

868
void MavlinkParameterClient::process_param_ext_value(const mavlink_message_t& message)
76✔
869
{
870
    mavlink_param_ext_value_t param_ext_value;
76✔
871
    mavlink_msg_param_ext_value_decode(&message, &param_ext_value);
76✔
872
    const auto safe_param_id = extract_safe_param_id(param_ext_value.param_id);
76✔
873
    if (safe_param_id.empty()) {
76✔
874
        LogWarn() << "Got ill-formed param_ext_value message (param_id empty)";
×
875
        return;
×
876
    }
877
    ParamValue received_value;
76✔
878
    if (!received_value.set_from_mavlink_param_ext_value(param_ext_value)) {
76✔
879
        LogWarn() << "Got ill-formed param_ext_value message (param_type unknown)";
×
880
        return;
×
881
    }
882

883
    if (_parameter_debugging) {
76✔
884
        LogDebug() << "process param_ext_value: " << safe_param_id << " " << received_value;
×
885
    }
886

887
    // See comments on process_param_value for use of unique_ptr
888
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
76✔
889
    auto work = work_queue_guard->get_front();
76✔
890

891
    if (!work) {
76✔
892
        // Prevent deadlock by releasing the lock before doing more work.
893
        work_queue_guard.reset();
13✔
894
        // update existing param
895
        find_and_call_subscriptions_value_changed(safe_param_id, received_value);
13✔
896
        return;
13✔
897
    }
898

899
    if (!work->already_requested) {
63✔
900
        return;
×
901
    }
902

903
    std::visit(
126✔
904
        overloaded{
63✔
905
            [&](WorkItemSet&) {
×
906
                if (_parameter_debugging) {
×
907
                    LogDebug() << "Unexpected ParamExtValue response.";
×
908
                }
909
            },
×
910
            [&](WorkItemGet& item) {
45✔
911
                if (!validate_id_or_index(
45✔
912
                        item.param_identifier,
45✔
913
                        safe_param_id,
45✔
914
                        static_cast<int16_t>(param_ext_value.param_index))) {
45✔
915
                    LogWarn() << "Got unexpected response on work item";
×
916
                    // No match, let's just return the borrowed work item.
917
                    return;
×
918
                }
919
                _timeout_handler.remove(_timeout_cookie);
45✔
920
                // LogDebug() << "time taken: " <<
921
                // _sender.get_time().elapsed_since_s(_last_request_time);
922
                work_queue_guard->pop_front();
45✔
923
                if (item.callback) {
45✔
924
                    auto callback = item.callback;
45✔
925
                    work_queue_guard.reset();
45✔
926
                    callback(Result::Success, received_value);
45✔
927
                }
45✔
928
            },
929
            [&](WorkItemGetAll& item) {
18✔
930
                switch (_param_cache.add_new_param(
18✔
931
                    safe_param_id, received_value, param_ext_value.param_index)) {
18✔
932
                    case MavlinkParameterCache::AddNewParamResult::AlreadyExists:
18✔
933
                        // PASSTHROUGH.
934
                    case MavlinkParameterCache::AddNewParamResult::Ok:
935
                        item.count = param_ext_value.param_count;
18✔
936
                        if (_parameter_debugging) {
18✔
937
                            LogDebug() << "Count is now " << item.count;
×
938
                        }
939

940
                        if (_param_cache.count(_use_extended) == param_ext_value.param_count) {
18✔
941
                            _timeout_handler.remove(_timeout_cookie);
2✔
942
                            if (_parameter_debugging) {
2✔
943
                                LogDebug() << "Getting all parameters complete: "
×
944
                                           << (_use_extended ? "extended" : "not extended");
×
945
                            }
946
                            work_queue_guard->pop_front();
2✔
947
                            if (item.callback) {
2✔
948
                                auto callback = item.callback;
2✔
949
                                work_queue_guard.reset();
2✔
950
                                callback(
4✔
951
                                    Result::Success,
952
                                    _param_cache.all_parameters_map(_use_extended));
4✔
953
                            }
2✔
954
                        } else {
955
                            if (_parameter_debugging) {
16✔
956
                                LogDebug() << "Received " << _param_cache.count(_use_extended)
×
957
                                           << " of " << param_ext_value.param_count;
×
958
                            }
959
                            // update the timeout handler, messages are still coming in.
960
                            _timeout_handler.refresh(_timeout_cookie);
16✔
961
                        }
962
                        break;
18✔
963
                    case MavlinkParameterCache::AddNewParamResult::TooManyParams:
×
964
                        // We shouldn't be able to get here as the incoming type is only an
965
                        // uint16_t.
966
                        LogErr() << "Too many params received";
×
967
                        assert(false);
×
968
                        break;
969
                    default:
×
970
                        LogErr() << "Unknown AddNewParamResult";
×
971
                        assert(false);
×
972
                        break;
973
                }
974
            },
18✔
975
        },
976
        work->work_item_variant);
63✔
977
}
115✔
978

979
void MavlinkParameterClient::process_param_ext_ack(const mavlink_message_t& message)
5✔
980
{
981
    mavlink_param_ext_ack_t param_ext_ack;
5✔
982
    mavlink_msg_param_ext_ack_decode(&message, &param_ext_ack);
5✔
983
    const auto safe_param_id = extract_safe_param_id(param_ext_ack.param_id);
5✔
984

985
    if (_parameter_debugging) {
5✔
986
        LogDebug() << "process param_ext_ack: " << safe_param_id << " "
×
987
                   << (int)param_ext_ack.param_result;
×
988
    }
989

990
    // See comments on process_param_value for use of unique_ptr
991
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
5✔
992
    auto work = work_queue_guard->get_front();
5✔
993
    if (!work) {
5✔
994
        return;
×
995
    }
996
    if (!work->already_requested) {
5✔
997
        return;
×
998
    }
999

1000
    std::visit(
10✔
1001
        overloaded{
5✔
1002
            [&](WorkItemSet& item) {
5✔
1003
                if (item.param_name != safe_param_id) {
5✔
1004
                    // No match, let's just return the borrowed work item.
1005
                    return;
×
1006
                }
1007
                if (param_ext_ack.param_result == PARAM_ACK_ACCEPTED) {
5✔
1008
                    _timeout_handler.remove(_timeout_cookie);
5✔
1009
                    // LogDebug() << "time taken: " <<
1010
                    // _sender.get_time().elapsed_since_s(_last_request_time);
1011
                    work_queue_guard->pop_front();
5✔
1012
                    if (item.callback) {
5✔
1013
                        auto callback = item.callback;
5✔
1014
                        // We are done, inform caller and go back to idle
1015
                        work_queue_guard.reset();
5✔
1016
                        callback(Result::Success);
5✔
1017
                    }
5✔
1018
                } else if (param_ext_ack.param_result == PARAM_ACK_IN_PROGRESS) {
×
1019
                    // Reset timeout and wait again.
1020
                    _timeout_handler.refresh(_timeout_cookie);
×
1021

1022
                } else {
1023
                    LogWarn() << "Somehow we did not get an ack, we got: "
×
1024
                              << int(param_ext_ack.param_result);
×
1025
                    _timeout_handler.remove(_timeout_cookie);
×
1026
                    // LogDebug() << "time taken: " <<
1027
                    // _sender.get_time().elapsed_since_s(_last_request_time);
1028
                    work_queue_guard->pop_front();
×
1029
                    work_queue_guard.reset();
×
1030
                    if (item.callback) {
×
1031
                        auto callback = item.callback;
×
1032
                        auto result = [&]() {
×
1033
                            switch (param_ext_ack.param_result) {
×
1034
                                case PARAM_ACK_FAILED:
×
1035
                                    return Result::Failed;
×
1036
                                case PARAM_ACK_VALUE_UNSUPPORTED:
×
1037
                                    return Result::ValueUnsupported;
×
1038
                                default:
×
1039
                                    return Result::UnknownError;
×
1040
                            }
1041
                        }();
×
1042
                        work_queue_guard.reset();
×
1043
                        callback(result);
×
1044
                    }
×
1045
                }
1046
            },
1047
            [&](WorkItemGet&) { LogWarn() << "Unexpected ParamExtAck response."; },
×
1048
            [&](WorkItemGetAll&) { LogWarn() << "Unexpected ParamExtAck response."; }},
×
1049
        work->work_item_variant);
5✔
1050
}
5✔
1051

1052
void MavlinkParameterClient::receive_timeout()
22✔
1053
{
1054
    // See comments on process_param_value for use of unique_ptr
1055
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
22✔
1056

1057
    auto work = work_queue_guard->get_front();
22✔
1058
    if (!work) {
22✔
1059
        LogErr() << "Received timeout without work";
×
1060
        return;
×
1061
    }
1062
    if (!work->already_requested) {
22✔
1063
        LogErr() << "Received timeout without already having work requested";
×
1064
        return;
×
1065
    }
1066

1067
    std::visit(
44✔
1068
        overloaded{
22✔
UNCOV
1069
            [&](WorkItemSet& item) {
×
UNCOV
1070
                if (work->retries_to_do > 0) {
×
1071
                    // We're not sure the command arrived, let's retransmit.
UNCOV
1072
                    LogWarn() << "sending again, retries to do: " << work->retries_to_do << "  ("
×
UNCOV
1073
                              << item.param_name << ").";
×
1074

UNCOV
1075
                    if (!send_set_param_message(item)) {
×
1076
                        LogErr() << "connection send error in retransmit (" << item.param_name
×
1077
                                 << ").";
×
1078
                        work_queue_guard->pop_front();
×
1079

1080
                        if (item.callback) {
×
1081
                            auto callback = item.callback;
×
1082
                            work_queue_guard.reset();
×
1083
                            callback(Result::ConnectionError);
×
1084
                        }
×
1085
                    } else {
UNCOV
1086
                        --work->retries_to_do;
×
UNCOV
1087
                        _timeout_cookie = _timeout_handler.add(
×
1088
                            [this] { receive_timeout(); }, _timeout_s_callback());
×
1089
                    }
1090
                } else {
1091
                    // We have tried retransmitting, giving up now.
1092
                    LogErr() << "Error: Retrying failed set param timeout: " << item.param_name;
×
1093
                    work_queue_guard->pop_front();
×
1094
                    if (item.callback) {
×
1095
                        auto callback = item.callback;
×
1096
                        work_queue_guard.reset();
×
1097
                        callback(Result::Timeout);
×
1098
                    }
×
1099
                }
UNCOV
1100
            },
×
1101
            [&](WorkItemGet& item) {
18✔
1102
                if (work->retries_to_do > 0) {
18✔
1103
                    // We're not sure the command arrived, let's retransmit.
1104
                    LogWarn() << "sending again, retries to do: " << work->retries_to_do;
16✔
1105
                    if (!send_get_param_message(item)) {
16✔
1106
                        LogErr() << "connection send error in retransmit ";
×
1107
                        work_queue_guard->pop_front();
×
1108
                        if (item.callback) {
×
1109
                            auto callback = item.callback;
×
1110
                            work_queue_guard.reset();
×
1111
                            callback(Result::ConnectionError, {});
×
1112
                        }
×
1113
                    } else {
1114
                        --work->retries_to_do;
16✔
1115
                        _timeout_cookie = _timeout_handler.add(
16✔
1116
                            [this] { receive_timeout(); }, _timeout_s_callback());
11✔
1117
                    }
1118
                } else {
1119
                    // We have tried retransmitting, giving up now.
1120
                    LogErr() << "retrying failed";
2✔
1121
                    work_queue_guard->pop_front();
2✔
1122
                    if (item.callback) {
2✔
1123
                        auto callback = item.callback;
2✔
1124
                        work_queue_guard.reset();
2✔
1125
                        callback(Result::Timeout, {});
2✔
1126
                    }
2✔
1127
                }
1128
            },
18✔
1129
            [&](WorkItemGetAll& item) {
4✔
1130
                // Request missing parameters.
1131
                // If retries are exceeded, give up with timeout.
1132

1133
                if (_parameter_debugging) {
4✔
1134
                    LogDebug() << "All params receive timeout with";
×
1135
                }
1136

1137
                if (item.count == 0) {
4✔
1138
                    // We got 0 messages back from the server (param count unknown). Most likely the
1139
                    // "list request" got lost before making it to the server,
1140
                    if (work->retries_to_do > 0) {
×
1141
                        --work->retries_to_do;
×
1142

1143
                        if (!send_request_list_message()) {
×
1144
                            LogErr() << "Send message failed";
×
1145
                            work_queue_guard->pop_front();
×
1146
                            if (item.callback) {
×
1147
                                auto callback = item.callback;
×
1148
                                work_queue_guard.reset();
×
1149
                                item.callback(Result::ConnectionError, {});
×
1150
                            }
×
1151
                            return;
×
1152
                        }
1153

1154
                        // We want to get notified if a timeout happens.
1155
                        _timeout_cookie = _timeout_handler.add(
×
1156
                            [this] { receive_timeout(); },
×
1157
                            _timeout_s_callback() * _get_all_timeout_factor);
×
1158
                    } else {
1159
                        if (item.callback) {
×
1160
                            auto callback = item.callback;
×
1161
                            work_queue_guard.reset();
×
1162
                            item.callback(Result::Timeout, {});
×
1163
                        }
×
1164
                        return;
×
1165
                    }
1166

1167
                } else {
1168
                    item.rerequesting = true;
4✔
1169

1170
                    LogInfo() << "Requesting " << _param_cache.missing_count(item.count) << " of "
12✔
1171
                              << item.count << " parameters missed during initial burst.";
12✔
1172

1173
                    if (_parameter_debugging) {
4✔
1174
                        _param_cache.print_missing(item.count);
×
1175
                    }
1176

1177
                    // To speed retransmissions up, we request params in chunks, otherwise the
1178
                    // latency back and forth makes this quite slow.
1179
                    if (!request_next_missing(item.count)) {
4✔
1180
                        work_queue_guard->pop_front();
×
1181
                        if (item.callback) {
×
1182
                            auto callback = item.callback;
×
1183
                            work_queue_guard.reset();
×
1184
                            callback(Result::ConnectionError, {});
×
1185
                        }
×
1186
                        return;
×
1187
                    }
1188

1189
                    _timeout_cookie =
4✔
1190
                        _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
10✔
1191
                }
1192
            }},
1193
        work->work_item_variant);
22✔
1194
}
22✔
1195

1196
bool MavlinkParameterClient::request_next_missing(uint16_t count)
4✔
1197
{
1198
    // Requesting 10 at a time seems to work on SiK radios.
1199
    const uint16_t chunk_size = 10;
4✔
1200

1201
    auto next_missing_indices = _param_cache.next_missing_indices(count, chunk_size);
4✔
1202
    if (next_missing_indices.empty()) {
4✔
1203
        LogErr() << "logic error, there should a missing index";
×
1204
        return false;
×
1205
    }
1206

1207
    for (auto next_missing_index : next_missing_indices) {
9✔
1208
        if (_parameter_debugging) {
5✔
1209
            LogDebug() << "Requesting missing parameter " << (int)next_missing_index;
×
1210
        }
1211

1212
        std::array<char, PARAM_ID_LEN> param_id_buff{};
5✔
1213

1214
        if (!send_get_param_message(param_id_buff, next_missing_index)) {
5✔
1215
            LogErr() << "Send message failed";
×
1216
            return false;
×
1217
        }
1218
    }
1219
    return true;
4✔
1220
}
4✔
1221

1222
std::ostream& operator<<(std::ostream& str, const MavlinkParameterClient::Result& result)
×
1223
{
1224
    switch (result) {
×
1225
        case MavlinkParameterClient::Result::Success:
×
1226
            return str << "Success";
×
1227
        case MavlinkParameterClient::Result::Timeout:
×
1228
            return str << "Timeout";
×
1229
        case MavlinkParameterClient::Result::ConnectionError:
×
1230
            return str << "ConnectionError";
×
1231
        case MavlinkParameterClient::Result::WrongType:
×
1232
            return str << "WrongType";
×
1233
        case MavlinkParameterClient::Result::ParamNameTooLong:
×
1234
            return str << "ParamNameTooLong";
×
1235
        case MavlinkParameterClient::Result::NotFound:
×
1236
            return str << "NotFound";
×
1237
        case MavlinkParameterClient::Result::ValueUnsupported:
×
1238
            return str << "ValueUnsupported";
×
1239
        case MavlinkParameterClient::Result::Failed:
×
1240
            return str << "Failed";
×
1241
        case MavlinkParameterClient::Result::UnknownError:
×
1242
            // Fallthrough
1243
        default:
1244
            return str << "UnknownError";
×
1245
    }
1246
}
1247

1248
bool MavlinkParameterClient::validate_id_or_index(
55✔
1249
    const std::variant<std::string, int16_t>& original,
1250
    const std::string& param_id,
1251
    const int16_t param_index)
1252
{
1253
    if (const auto str = std::get_if<std::string>(&original)) {
55✔
1254
        if (param_id != *str) {
55✔
1255
            // We requested by string id, but response doesn't match
1256
            return false;
×
1257
        }
1258
    } else {
1259
        const auto tmp = std::get<int16_t>(original);
×
1260
        if (param_index != tmp) {
×
1261
            // We requested by index, but response doesn't match
1262
            return false;
×
1263
        }
1264
    }
1265
    return true;
55✔
1266
}
1267

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