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

mavlink / MAVSDK / 6739182515

02 Nov 2023 11:04PM UTC coverage: 36.952% (+5.7%) from 31.229%
6739182515

push

github

web-flow
Merge pull request #2060 from mavlink/pr-split-ftp

Split Ftp plugin into Ftp and FtpServer

2002 of 2573 new or added lines in 26 files covered. (77.81%)

3 existing lines in 3 files now uncovered.

9920 of 26846 relevant lines covered (36.95%)

112.43 hits per line

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

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

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

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

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

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

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

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

59
        return params_without_extended;
64✔
60
    }
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
48✔
86
{
87
    const auto& params = all_parameters(including_extended);
96✔
88

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

95
    return {};
12✔
96
}
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);
6✔
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
}
112

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

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

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

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

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

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