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

mavlink / MAVSDK / 12303784192

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

push

github

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

get_all_params fixes

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

2 existing lines in 2 files now uncovered.

12136 of 31350 relevant lines covered (38.71%)

243.5 hits per line

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

80.58
/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)
66✔
9
{
10
    if (exists(param_id)) {
66✔
11
        return AddNewParamResult::AlreadyExists;
1✔
12
    }
13

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

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

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

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

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

45
std::vector<MavlinkParameterCache::Param>
46
MavlinkParameterCache::all_parameters(bool including_extended) const
117✔
47
{
48
    if (including_extended) {
117✔
49
        return _all_params;
49✔
50
    } else {
51
        // TODO: we might want to cache this
52
        std::vector<MavlinkParameterCache::Param> params_without_extended{};
68✔
53
        std::copy_if(
68✔
54
            _all_params.begin(),
55
            _all_params.end(),
56
            std::back_inserter(params_without_extended),
57
            [](auto& entry) { return !entry.value.needs_extended(); });
154✔
58

59
        return params_without_extended;
68✔
60
    }
68✔
61
}
62

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

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

81
    return mp;
4✔
82
}
83

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

89
    for (const auto& param : params) {
65✔
90
        if (param.id == param_id) {
51✔
91
            return param;
37✔
92
        }
93
    }
94

95
    return {};
14✔
96
}
51✔
97

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

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

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

120
void MavlinkParameterCache::clear()
20✔
121
{
122
    _all_params.clear();
20✔
123
    _last_missing_requested = {};
20✔
124
}
20✔
125

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

134
bool MavlinkParameterCache::exists(uint16_t param_index) const
104✔
135
{
136
    auto it = std::find_if(_all_params.begin(), _all_params.end(), [&](const auto& param) {
104✔
137
        return (param_index == param.index);
360✔
138
    });
139
    return it != _all_params.end();
104✔
140
}
141

142
uint16_t MavlinkParameterCache::missing_count(uint16_t count) const
4✔
143
{
144
    uint16_t missing = 0;
4✔
145

146
    for (uint16_t i = 0; i < count; ++i) {
34✔
147
        if (!exists(i)) {
30✔
148
            ++missing;
5✔
149
        }
150
    }
151

152
    return missing;
4✔
153
}
154

155
std::vector<uint16_t>
156
MavlinkParameterCache::next_missing_indices(uint16_t count, uint16_t chunk_size)
12✔
157
{
158
    // Extended doesn't matter here because we use this function in the sender
159
    // which is always either all extended or not.
160
    std::sort(_all_params.begin(), _all_params.end(), [](const auto& lhs, const auto& rhs) {
12✔
161
        return lhs.index < rhs.index;
70✔
162
    });
163

164
    std::vector<uint16_t> result;
12✔
165

166
    for (unsigned i = 0; i < count; ++i) {
85✔
167
        if (exists(i)) {
74✔
168
            continue;
45✔
169
        }
170

171
        result.push_back(i);
29✔
172
        _last_missing_requested = {i};
29✔
173

174
        if (result.size() == chunk_size) {
29✔
175
            break;
1✔
176
        }
177
    }
178

179
    return result;
12✔
180
}
181

NEW
182
void MavlinkParameterCache::print_missing(uint16_t count)
×
183
{
184
    // Extended doesn't matter here because we use this function in the sender
185
    // which is always either all extended or not.
NEW
186
    std::sort(_all_params.begin(), _all_params.end(), [](const auto& lhs, const auto& rhs) {
×
NEW
187
        return lhs.index < rhs.index;
×
188
    });
189

NEW
190
    LogDebug() << "Available: ";
×
NEW
191
    for (auto param : _all_params) {
×
NEW
192
        LogDebug() << param.index << ": " << param.id;
×
NEW
193
    }
×
NEW
194
    LogDebug() << "Available count: " << _all_params.size();
×
195

NEW
196
    unsigned missing = 0;
×
NEW
197
    LogDebug() << "Missing: ";
×
NEW
198
    for (unsigned i = 0; i < count; ++i) {
×
NEW
199
        if (!exists(i)) {
×
200
            // We have reached the end but it's not complete yet.
NEW
201
            LogDebug() << i;
×
NEW
202
            ++missing;
×
203
        }
204
    }
205

NEW
206
    LogDebug() << "Missing count: " << missing;
×
UNCOV
207
}
×
208

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