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

mavlink / MAVSDK / 10885174836

16 Sep 2024 01:46PM UTC coverage: 38.077% (+0.03%) from 38.046%
10885174836

push

github

web-flow
Merge pull request #2400 from pavloblindnology/pr-fix-read-int8-16-param-ap

core: Fix reading int8/16 params from ArduPilot

9 of 14 new or added lines in 1 file covered. (64.29%)

2 existing lines in 1 file now uncovered.

11459 of 30094 relevant lines covered (38.08%)

260.68 hits per line

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

56.67
/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 <utility>
9

10
namespace mavsdk {
11

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

424
void MavlinkParameterClient::clear_cache()
×
425
{
426
    _param_cache.clear();
×
427
}
×
428

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

434
    if (!work) {
407✔
435
        return;
137✔
436
    }
437

438
    if (work->already_requested) {
270✔
439
        return;
244✔
440
    }
441

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

495
bool MavlinkParameterClient::send_set_param_message(WorkItemSet& work_item)
7✔
496
{
497
    auto param_id = param_id_to_message_buffer(work_item.param_name);
7✔
498

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

508
            mavlink_msg_param_ext_set_pack_chan(
6✔
509
                mavlink_address.system_id,
3✔
510
                mavlink_address.component_id,
3✔
511
                channel,
512
                &message,
6✔
513
                _target_system_id,
3✔
514
                _target_component_id,
3✔
515
                param_id.data(),
3✔
516
                param_value_buf.data(),
3✔
517
                work_item.param_value.get_mav_param_ext_type());
3✔
518

519
            return message;
3✔
520
        });
3✔
521
    } else {
522
        const float value_set = (_autopilot_callback() == Autopilot::ArduPilot) ?
4✔
523
                                    work_item.param_value.get_4_float_bytes_cast() :
×
524
                                    work_item.param_value.get_4_float_bytes_bytewise();
4✔
525

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

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

557
    return send_get_param_message(param_id_buff, param_index);
29✔
558
}
559

560
bool MavlinkParameterClient::send_get_param_message(
34✔
561
    const std::array<char, PARAM_ID_LEN>& param_id_buff, int16_t param_index)
562
{
563
    mavlink_message_t message;
34✔
564

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

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

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

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

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

662
    if (_parameter_debugging) {
26✔
663
        LogDebug() << "process_param_value: " << safe_param_id << " " << received_value;
×
664
    }
665

666
    // We need to use a unique pointer here to remove the lock from the work queue manually "early"
667
    // before calling the (perhaps user-provided) callback. Otherwise, we might end up in a deadlock
668
    // if the callback wants to push another work item onto the queue. By using a unique ptr there
669
    // is no risk of forgetting to remove the lock - it is destroyed (if still valid) after going
670
    // out of scope.
671
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
26✔
672
    const auto work = work_queue_guard->get_front();
26✔
673
    if (!work) {
26✔
674
        return;
×
675
    }
676

677
    if (!work->already_requested) {
26✔
678
        return;
×
679
    }
680

681
    std::visit(
52✔
682
        overloaded{
26✔
683
            [&](WorkItemSet& item) {
4✔
684
                if (item.param_name != safe_param_id) {
4✔
685
                    // No match, let's just return the borrowed work item.
686
                    return;
×
687
                }
688

689
                if (_parameter_debugging) {
4✔
690
                    LogDebug() << "Item value is: " << item.param_value
×
691
                               << ", received: " << received_value;
×
692
                }
693

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

715
                        if (!send_set_param_message(item)) {
×
716
                            LogErr() << "connection send error in retransmit (" << item.param_name
×
717
                                     << ").";
×
718
                            work_queue_guard->pop_front();
×
719

720
                            if (item.callback) {
×
721
                                auto callback = item.callback;
×
722
                                work_queue_guard.reset();
×
723
                                callback(Result::ConnectionError);
×
724
                            }
725
                            _timeout_handler.refresh(_timeout_cookie);
×
726
                        } else {
727
                            --work->retries_to_do;
×
728
                            _timeout_handler.refresh(_timeout_cookie);
×
729
                        }
730
                    } else {
731
                        // We have tried retransmitting, giving up now.
732
                        LogErr() << "Error: Retrying failed set param failed: " << item.param_name;
×
733
                        work_queue_guard->pop_front();
×
734
                        if (item.callback) {
×
735
                            auto callback = item.callback;
×
736
                            work_queue_guard.reset();
×
737
                            callback(Result::Timeout);
×
738
                        }
739
                    }
740
                }
741
            },
742
            [&](WorkItemGet& item) {
10✔
743
                if (!validate_id_or_index(
10✔
744
                        item.param_identifier,
10✔
745
                        safe_param_id,
10✔
746
                        static_cast<int16_t>(param_value.param_index))) {
10✔
747
                    LogWarn() << "Got unexpected response on work item";
×
748
                    // No match, let's just return the borrowed work item.
749
                    return;
×
750
                }
751
                _timeout_handler.remove(_timeout_cookie);
10✔
752
                // LogDebug() << "time taken: " <<
753
                // _sender.get_time().elapsed_since_s(_last_request_time);
754
                work_queue_guard->pop_front();
10✔
755
                if (item.callback) {
10✔
756
                    auto callback = item.callback;
20✔
757
                    work_queue_guard.reset();
10✔
758
                    callback(Result::Success, received_value);
10✔
759
                }
760
            },
761
            [&](WorkItemGetAll& item) {
12✔
762
                switch (_param_cache.add_new_param(
12✔
763
                    safe_param_id, received_value, param_value.param_index)) {
12✔
764
                    case MavlinkParameterCache::AddNewParamResult::AlreadyExists:
12✔
765
                        // FALLTHROUGH
766
                        // We don't care if it already exists, just overwrite it and carry on.
767
                        // The reason is that this can likely happen if the very first
768
                        // request_list is sent twice and hence we get a bunch of duplicate
769
                        // params.
770
                    case MavlinkParameterCache::AddNewParamResult::Ok:
771

772
                        item.count = param_value.param_count;
12✔
773
                        if (_parameter_debugging) {
12✔
774
                            LogDebug() << "Count is now " << item.count;
×
775
                        }
776
                        if (_param_cache.count(_use_extended) == param_value.param_count) {
12✔
777
                            _timeout_handler.remove(_timeout_cookie);
2✔
778
                            if (_parameter_debugging) {
2✔
779
                                LogDebug() << "Param set complete: "
×
780
                                           << (_use_extended ? "extended" : "not extended");
×
781
                            }
782
                            work_queue_guard->pop_front();
2✔
783
                            if (item.callback) {
2✔
784
                                auto callback = item.callback;
4✔
785
                                work_queue_guard.reset();
2✔
786
                                callback(
4✔
787
                                    Result::Success,
788
                                    _param_cache.all_parameters_map(_use_extended));
4✔
789
                            }
790
                        } else {
791
                            if (_parameter_debugging) {
10✔
792
                                LogDebug() << "Count expected " << _param_cache.count(_use_extended)
×
793
                                           << " so far " << param_value.param_count;
×
794
                            }
795
                            if (item.rerequesting) {
10✔
796
                                auto maybe_next_missing_index =
×
797
                                    _param_cache.next_missing_index(item.count);
×
798
                                if (!maybe_next_missing_index.has_value()) {
×
799
                                    LogErr() << "logic error, there should a missing index";
×
800
                                    assert(false);
×
801
                                }
802

803
                                if (_parameter_debugging) {
×
804
                                    LogDebug() << "Requesting missing parameter "
×
805
                                               << (int)maybe_next_missing_index.value();
×
806
                                }
807

808
                                std::array<char, PARAM_ID_LEN> param_id_buff{};
×
809
                                if (!send_get_param_message(
×
810
                                        param_id_buff, maybe_next_missing_index.value())) {
×
811
                                    LogErr() << "Send message failed";
×
812
                                    work_queue_guard->pop_front();
×
813
                                    if (item.callback) {
×
814
                                        auto callback = item.callback;
×
815
                                        work_queue_guard.reset();
×
816
                                        callback(Result::ConnectionError, {});
×
817
                                    }
818
                                    return;
×
819
                                }
820
                            } else {
821
                                // update the timeout handler, messages are still coming in.
822
                            }
823
                            _timeout_handler.refresh(_timeout_cookie);
10✔
824
                        }
825
                        break;
12✔
826
                    case MavlinkParameterCache::AddNewParamResult::TooManyParams:
×
827
                        // We shouldn't be able to get here as the incoming type is only an
828
                        // uint16_t.
829
                        LogErr() << "Too many params received";
×
830
                        assert(false);
×
831
                        break;
832
                    default:
×
833
                        LogErr() << "Unknown AddNewParamResult";
×
834
                        assert(false);
×
835
                        break;
836
                }
837
            }},
838
        work->work_item_variant);
26✔
839

840
    // find_and_call_subscriptions_value_changed(safe_param_id, received_value);
841
}
842

843
void MavlinkParameterClient::process_param_ext_value(const mavlink_message_t& message)
22✔
844
{
845
    mavlink_param_ext_value_t param_ext_value;
22✔
846
    mavlink_msg_param_ext_value_decode(&message, &param_ext_value);
22✔
847
    const auto safe_param_id = extract_safe_param_id(param_ext_value.param_id);
22✔
848
    if (safe_param_id.empty()) {
22✔
849
        LogWarn() << "Got ill-formed param_ext_value message (param_id empty)";
×
850
        return;
×
851
    }
852
    ParamValue received_value;
22✔
853
    if (!received_value.set_from_mavlink_param_ext_value(param_ext_value)) {
22✔
854
        LogWarn() << "Got ill-formed param_ext_value message (param_type unknown)";
×
855
        return;
×
856
    }
857

858
    if (_parameter_debugging) {
22✔
859
        LogDebug() << "process param_ext_value: " << safe_param_id << " " << received_value;
×
860
    }
861

862
    // See comments on process_param_value for use of unique_ptr
863
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
22✔
864
    auto work = work_queue_guard->get_front();
22✔
865
    if (!work) {
22✔
866
        return;
×
867
    }
868
    if (!work->already_requested) {
22✔
869
        return;
×
870
    }
871

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

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

947
    // TODO I think we need to consider more edge cases here
948
    // find_and_call_subscriptions_value_changed(safe_param_id, received_value);
949
}
950

951
void MavlinkParameterClient::process_param_ext_ack(const mavlink_message_t& message)
2✔
952
{
953
    mavlink_param_ext_ack_t param_ext_ack;
2✔
954
    mavlink_msg_param_ext_ack_decode(&message, &param_ext_ack);
2✔
955
    const auto safe_param_id = extract_safe_param_id(param_ext_ack.param_id);
2✔
956

957
    if (_parameter_debugging) {
2✔
958
        LogDebug() << "process param_ext_ack: " << safe_param_id << " "
×
959
                   << (int)param_ext_ack.param_result;
×
960
    }
961

962
    // See comments on process_param_value for use of unique_ptr
963
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
2✔
964
    auto work = work_queue_guard->get_front();
2✔
965
    if (!work) {
2✔
966
        return;
×
967
    }
968
    if (!work->already_requested) {
2✔
969
        return;
×
970
    }
971

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

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

1024
void MavlinkParameterClient::receive_timeout()
21✔
1025
{
1026
    // See comments on process_param_value for use of unique_ptr
1027
    auto work_queue_guard = std::make_unique<LockedQueue<WorkItem>::Guard>(_work_queue);
21✔
1028

1029
    auto work = work_queue_guard->get_front();
21✔
1030
    if (!work) {
21✔
1031
        LogErr() << "Received timeout without work";
×
1032
        return;
×
1033
    }
1034
    if (!work->already_requested) {
21✔
1035
        LogErr() << "Received timeout without already having work requested";
×
1036
        return;
×
1037
    }
1038

1039
    std::visit(
42✔
1040
        overloaded{
21✔
1041
            [&](WorkItemSet& item) {
1✔
1042
                if (work->retries_to_do > 0) {
1✔
1043
                    // We're not sure the command arrived, let's retransmit.
1044
                    LogWarn() << "sending again, retries to do: " << work->retries_to_do << "  ("
2✔
1045
                              << item.param_name << ").";
2✔
1046

1047
                    if (!send_set_param_message(item)) {
1✔
1048
                        LogErr() << "connection send error in retransmit (" << item.param_name
×
1049
                                 << ").";
×
1050
                        work_queue_guard->pop_front();
×
1051

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

1105
                if (_parameter_debugging) {
5✔
1106
                    LogDebug() << "All params receive timeout with";
×
1107
                }
1108

1109
                if (item.count == 0) {
5✔
1110
                    // We got 0 messages back from the server (param count unknown). Most likely the
1111
                    // "list request" got lost before making it to the server,
1112
                    if (work->retries_to_do > 0) {
×
1113
                        --work->retries_to_do;
×
1114

1115
                        if (!send_request_list_message()) {
×
1116
                            LogErr() << "Send message failed";
×
1117
                            work_queue_guard->pop_front();
×
1118
                            if (item.callback) {
×
1119
                                auto callback = item.callback;
×
1120
                                work_queue_guard.reset();
×
1121
                                item.callback(Result::ConnectionError, {});
×
1122
                            }
1123
                            return;
×
1124
                        }
1125

1126
                        // We want to get notified if a timeout happens
1127
                        _timeout_cookie = _timeout_handler.add(
×
1128
                            [this] { receive_timeout(); }, _timeout_s_callback());
×
1129
                    } else {
1130
                        if (item.callback) {
×
1131
                            auto callback = item.callback;
×
1132
                            work_queue_guard.reset();
×
1133
                            item.callback(Result::Timeout, {});
×
1134
                        }
1135
                        return;
×
1136
                    }
1137

1138
                } else {
1139
                    item.rerequesting = true;
5✔
1140
                    auto maybe_next_missing_index = _param_cache.next_missing_index(item.count);
5✔
1141
                    if (!maybe_next_missing_index.has_value()) {
5✔
1142
                        LogErr() << "logic error, there should a missing index";
×
1143
                        assert(false);
×
1144
                    }
1145

1146
                    if (_parameter_debugging) {
5✔
1147
                        LogDebug() << "Requesting missing parameter "
×
1148
                                   << (int)maybe_next_missing_index.value();
×
1149
                    }
1150

1151
                    std::array<char, PARAM_ID_LEN> param_id_buff{};
5✔
1152

1153
                    if (!send_get_param_message(param_id_buff, maybe_next_missing_index.value())) {
5✔
1154
                        LogErr() << "Send message failed";
×
1155
                        work_queue_guard->pop_front();
×
1156
                        if (item.callback) {
×
1157
                            auto callback = item.callback;
×
1158
                            work_queue_guard.reset();
×
1159
                            callback(Result::ConnectionError, {});
×
1160
                        }
1161
                        return;
×
1162
                    }
1163
                    _timeout_cookie =
5✔
1164
                        _timeout_handler.add([this] { receive_timeout(); }, _timeout_s_callback());
13✔
1165
                }
1166
            }},
1167
        work->work_item_variant);
21✔
1168
}
1169

1170
std::ostream& operator<<(std::ostream& str, const MavlinkParameterClient::Result& result)
×
1171
{
1172
    switch (result) {
×
1173
        case MavlinkParameterClient::Result::Success:
×
1174
            return str << "Success";
×
1175
        case MavlinkParameterClient::Result::Timeout:
×
1176
            return str << "Timeout";
×
1177
        case MavlinkParameterClient::Result::ConnectionError:
×
1178
            return str << "ConnectionError";
×
1179
        case MavlinkParameterClient::Result::WrongType:
×
1180
            return str << "WrongType";
×
1181
        case MavlinkParameterClient::Result::ParamNameTooLong:
×
1182
            return str << "ParamNameTooLong";
×
1183
        case MavlinkParameterClient::Result::NotFound:
×
1184
            return str << "NotFound";
×
1185
        case MavlinkParameterClient::Result::ValueUnsupported:
×
1186
            return str << "ValueUnsupported";
×
1187
        case MavlinkParameterClient::Result::Failed:
×
1188
            return str << "Failed";
×
1189
        case MavlinkParameterClient::Result::UnknownError:
×
1190
            // Fallthrough
1191
        default:
1192
            return str << "UnknownError";
×
1193
    }
1194
}
1195

1196
bool MavlinkParameterClient::validate_id_or_index(
14✔
1197
    const std::variant<std::string, int16_t>& original,
1198
    const std::string& param_id,
1199
    const int16_t param_index)
1200
{
1201
    if (const auto str = std::get_if<std::string>(&original)) {
14✔
1202
        if (param_id != *str) {
14✔
1203
            // We requested by string id, but response doesn't match
1204
            return false;
×
1205
        }
1206
    } else {
1207
        const auto tmp = std::get<int16_t>(original);
×
1208
        if (param_index != tmp) {
×
1209
            // We requested by index, but response doesn't match
1210
            return false;
×
1211
        }
1212
    }
1213
    return true;
14✔
1214
}
1215

1216
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc