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

mavlink / MAVSDK / 4899817366

pending completion
4899817366

Pull #1772

github

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

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

7708 of 24812 relevant lines covered (31.07%)

19.96 hits per line

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

90.41
/src/mavsdk/core/mavlink_parameter_cache.cpp
1
#include "mavlink_parameter_cache.h"
2

3
#include <algorithm>
4

5
namespace mavsdk {
6

7
MavlinkParameterCache::AddNewParamResult
8
MavlinkParameterCache::add_new_param(const std::string& param_id, ParamValue value, int16_t index)
70✔
9
{
10
    if (exists(param_id)) {
70✔
11
        return AddNewParamResult::AlreadyExists;
8✔
12
    }
13

14
    if (_all_params.size() + 1 > std::numeric_limits<int16_t>::max()) {
62✔
15
        return AddNewParamResult::TooManyParams;
×
16
    }
17

18
    _all_params.push_back(Param{
186✔
19
        param_id,
20
        std::move(value),
62✔
21
        (index != -1 ? static_cast<uint16_t>(index) : static_cast<uint16_t>(_all_params.size()))});
62✔
22
    return MavlinkParameterCache::AddNewParamResult::Ok;
62✔
23
}
24

25
MavlinkParameterCache::UpdateExistingParamResult
26
MavlinkParameterCache::update_existing_param(const std::string& param_id, ParamValue value)
13✔
27
{
28
    auto it = std::find_if(_all_params.begin(), _all_params.end(), [&](const auto& param) {
13✔
29
        return (param_id == param.id);
18✔
30
    });
13✔
31

32
    if (it == _all_params.end()) {
13✔
33
        return UpdateExistingParamResult::MissingParam;
1✔
34
    }
35

36
    if (!it->value.is_same_type(value)) {
12✔
37
        return MavlinkParameterCache::UpdateExistingParamResult::WrongType;
1✔
38
    } else {
39
        it->value.update_value_typesafe(value);
11✔
40
        return MavlinkParameterCache::UpdateExistingParamResult::Ok;
11✔
41
    }
42
}
43

44
std::vector<MavlinkParameterCache::Param>
45
MavlinkParameterCache::all_parameters(bool including_extended) const
124✔
46
{
47
    if (including_extended) {
124✔
48
        return _all_params;
53✔
49
    } else {
50
        // TODO: we might want to cache this
51
        std::vector<MavlinkParameterCache::Param> vec{};
142✔
52
        for (const auto& entry : _all_params) {
253✔
53
            if (entry.value.needs_extended() && !including_extended) {
182✔
54
                continue;
18✔
55
            }
56
            vec.push_back(entry);
164✔
57
        }
58
        return vec;
71✔
59
    }
60
}
61

62
std::map<std::string, ParamValue>
63
MavlinkParameterCache::all_parameters_map(bool including_extended) const
4✔
64
{
65
    std::map<std::string, ParamValue> mp{};
4✔
66
    if (including_extended) {
4✔
67
        for (const auto& param : _all_params) {
20✔
68
            mp.insert({param.id, param.value});
18✔
69
        }
70

71
    } else {
72
        for (const auto& param : _all_params) {
14✔
73
            if (param.value.needs_extended()) {
12✔
74
                continue;
×
75
            }
76
            mp.insert({param.id, param.value});
12✔
77
        }
78
    }
79

80
    return mp;
4✔
81
}
82

83
std::optional<MavlinkParameterCache::Param>
84
MavlinkParameterCache::param_by_id(const std::string& param_id, bool including_extended) const
51✔
85
{
86
    const auto& params = all_parameters(including_extended);
102✔
87

88
    for (const auto& param : params) {
66✔
89
        if (param.id == param_id) {
56✔
90
            return param;
41✔
91
        }
92
    }
93

94
    return {};
10✔
95
}
96

97
std::optional<MavlinkParameterCache::Param>
98
MavlinkParameterCache::param_by_index(uint16_t param_index, bool including_extended) const
2✔
99
{
100
    const auto& params = all_parameters(including_extended);
4✔
101
    if (param_index > params.size()) {
2✔
102
        LogErr() << "param at " << (int)param_index << " out of bounds";
×
103
        return {};
×
104
    }
105

106
    const auto& param = params[param_index];
2✔
107
    // Check that the redundant index matches the actual vector index.
108
    assert(param.index == param_index);
2✔
109
    return {param};
2✔
110
}
111

112
uint16_t MavlinkParameterCache::count(bool including_extended) const
64✔
113
{
114
    const auto num = all_parameters(including_extended).size();
64✔
115
    assert(num < std::numeric_limits<uint16_t>::max());
64✔
116
    return static_cast<uint16_t>(num);
64✔
117
}
118

119
void MavlinkParameterCache::clear()
×
120
{
121
    _all_params.clear();
×
122
}
×
123

124
bool MavlinkParameterCache::exists(const std::string& param_id) const
70✔
125
{
126
    auto it = std::find_if(_all_params.begin(), _all_params.end(), [&](const auto& param) {
70✔
127
        return (param_id == param.id);
210✔
128
    });
70✔
129
    return it != _all_params.end();
70✔
130
}
131

132
std::optional<uint16_t> MavlinkParameterCache::next_missing_index(uint16_t count)
8✔
133
{
134
    // Extended doesn't matter here because we use this function in the sender
135
    // which is always either all extended or not.
136
    std::sort(_all_params.begin(), _all_params.end(), [](const auto& lhs, const auto& rhs) {
8✔
137
        return lhs.index < rhs.index;
31✔
138
    });
139

140
    for (unsigned i = 0; i < count; ++i) {
23✔
141
        if (_all_params.size() <= i) {
22✔
142
            // We have reached the end but it's not complete yet.
143
            return i;
4✔
144
        }
145
        if (_all_params[i].index > i) {
18✔
146
            // We have found a hole to fill.
147
            return i;
3✔
148
        }
149
    }
150
    return {};
1✔
151
}
152

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