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

mavlink / MAVSDK / 6568658631

19 Oct 2023 01:31AM UTC coverage: 31.23% (+0.02%) from 31.215%
6568658631

push

github

web-flow
Merge pull request #2155 from mavlink/pr-static-fixes

Threading fixes, MAVLink sequence number cleanup

1386 of 1386 new or added lines in 46 files covered. (100.0%)

7906 of 25315 relevant lines covered (31.23%)

23.54 hits per line

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

0.0
/src/mavsdk/plugins/component_information_server/component_information_server_impl.cpp
1
#include "component_information_server_impl.h"
2
#include "mavlink_address.h"
3
#include "mavlink_request_message_handler.h"
4
#include "callback_list.tpp"
5

6
#include <algorithm>
7
#include <string>
8
#include <json/json.h>
9

10
namespace mavsdk {
11

12
template class CallbackList<ComponentInformationServer::FloatParamUpdate>;
13

14
ComponentInformationServerImpl::ComponentInformationServerImpl(
×
15
    std::shared_ptr<ServerComponent> server_component) :
×
16
    ServerPluginImplBase(server_component)
×
17
{
18
    // FIXME: allow other component IDs
19
    _server_component_impl->register_plugin(this);
×
20
}
×
21

22
ComponentInformationServerImpl::~ComponentInformationServerImpl()
×
23
{
24
    _server_component_impl->register_plugin(this);
×
25
}
×
26

27
void ComponentInformationServerImpl::init()
×
28
{
29
    _server_component_impl->mavlink_request_message_handler().register_handler(
×
30
        MAVLINK_MSG_ID_COMPONENT_INFORMATION,
31
        [this](uint8_t, uint8_t, MavlinkRequestMessageHandler::Params) {
×
32
            return process_component_information_requested();
×
33
        },
34
        this);
35
}
×
36

37
void ComponentInformationServerImpl::deinit()
×
38
{
39
    _server_component_impl->mavlink_request_message_handler().unregister_all_handlers(this);
×
40
}
×
41

42
ComponentInformationServer::Result
43
ComponentInformationServerImpl::provide_float_param(ComponentInformationServer::FloatParam param)
×
44
{
45
    std::lock_guard<std::mutex> lock(_mutex);
×
46

47
    if (std::find_if(
×
48
            _float_params.begin(),
49
            _float_params.end(),
50
            [&](ComponentInformationServer::FloatParam& existing_param) {
×
51
                return existing_param.name == param.name;
×
52
            }) != std::end(_float_params)) {
×
53
        return ComponentInformationServer::Result::DuplicateParam;
×
54
    }
55

56
    if (param.start_value > param.max_value || param.start_value < param.min_value) {
×
57
        return ComponentInformationServer::Result::InvalidParamStartValue;
×
58
    }
59

60
    if (param.default_value > param.max_value || param.default_value < param.min_value) {
×
61
        return ComponentInformationServer::Result::InvalidParamDefaultValue;
×
62
    }
63

64
    if (param.name.size() > 16) {
×
65
        return ComponentInformationServer::Result::InvalidParamName;
×
66
    }
67

68
    _float_params.push_back(param);
×
69

70
    update_json_files_with_lock();
×
71

72
    _server_component_impl->mavlink_parameter_server().provide_server_param_float(
×
73
        param.name, param.start_value);
×
74
    _server_component_impl->mavlink_parameter_server().subscribe_param_float_changed(
×
75
        param.name,
×
76
        [this, name = param.name](float new_value) { param_update(name, new_value); },
×
77
        this);
78

79
    return ComponentInformationServer::Result::Success;
×
80
}
81

82
ComponentInformationServer::FloatParamHandle ComponentInformationServerImpl::subscribe_float_param(
×
83
    const ComponentInformationServer::FloatParamCallback& callback)
84
{
85
    std::lock_guard<std::mutex> lock(_mutex);
×
86
    return _float_param_update_callbacks.subscribe(callback);
×
87
}
88

89
void ComponentInformationServerImpl::unsubscribe_float_param(
×
90
    ComponentInformationServer::FloatParamHandle handle)
91
{
92
    std::lock_guard<std::mutex> lock(_mutex);
×
93
    _float_param_update_callbacks.unsubscribe(handle);
×
94
}
×
95

96
void ComponentInformationServerImpl::param_update(const std::string& name, float new_value)
×
97
{
98
    std::lock_guard<std::mutex> lock(_mutex);
×
99
    ComponentInformationServer::FloatParamUpdate param_update{name, new_value};
×
100
    _float_param_update_callbacks.queue(param_update, [this](const auto& func) {
×
101
        _server_component_impl->call_user_callback(func);
×
102
    });
×
103
}
×
104

105
std::optional<MAV_RESULT> ComponentInformationServerImpl::process_component_information_requested()
×
106
{
107
    const char general_metadata_uri[100] = "mftp://general.json";
×
108
    const char peripherals_metadata_uri[100] = "";
×
109
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
110
        mavlink_message_t message;
111
        mavlink_msg_component_information_pack_chan(
×
112
            mavlink_address.system_id,
×
113
            mavlink_address.component_id,
×
114
            channel,
115
            &message,
116
            _server_component_impl->get_time().elapsed_ms(),
×
117
            0,
118
            general_metadata_uri,
×
119
            0,
120
            peripherals_metadata_uri);
×
121
        return message;
×
122
    });
123

124
    // FIXME: REMOVE again
125
    update_json_files_with_lock();
×
126

127
    return MAV_RESULT_ACCEPTED;
×
128
}
129

130
void ComponentInformationServerImpl::update_json_files_with_lock()
×
131
{
132
    auto parameter_file = generate_parameter_file();
×
133
    auto meta_file = generate_meta_file();
×
134

135
    // std::cout << "parameter: " << parameter_file << '\n';
136
    // std::cout << "meta: " << meta_file << '\n';
137

138
    // FIXME: needs refactoring
139
    //_server_component_impl->mavlink_ftp().write_tmp_file("general.json",
140
    // meta_file);
141
    //_server_component_impl->mavlink_ftp().write_tmp_file("parameter.json",
142
    // parameter_file);
143
}
×
144

145
std::string ComponentInformationServerImpl::generate_parameter_file()
×
146
{
147
    Json::Value root;
×
148
    root["version"] = 1;
×
149
    Json::Value parameters = Json::arrayValue;
×
150
    for (const auto& param : _float_params) {
×
151
        Json::Value parameter;
×
152
        parameter["name"] = param.name;
×
153
        parameter["type"] = "Float";
×
154
        parameter["shortDesc"] = param.short_description;
×
155
        parameter["longDesc"] = param.long_description;
×
156
        parameter["units"] = param.unit;
×
157
        parameter["decimalPlaces"] = param.decimal_places;
×
158
        parameter["min"] = static_cast<double>(param.min_value);
×
159
        parameter["max"] = static_cast<double>(param.max_value);
×
160
        parameter["default"] = static_cast<double>(param.default_value);
×
161
        parameters.append(parameter);
×
162
    }
163
    root["parameters"] = parameters;
×
164

165
    return root.toStyledString();
×
166
}
167

168
std::string ComponentInformationServerImpl::generate_meta_file()
×
169
{
170
    Json::Value root;
×
171
    root["version"] = 1;
×
172
    root["vendorName"] = "Vendor";
×
173
    root["modelName"] = "Model";
×
174
    root["firmwareVersion"] = "1.0.0.0";
×
175
    root["hardwareVersion"] = "1.0.0.0";
×
176
    Json::Value metadata_types = Json::arrayValue;
×
177
    Json::Value metadata_type;
×
178
    metadata_type["type"] = Json::Int{COMP_METADATA_TYPE_PARAMETER};
×
179
    metadata_type["uri"] = "mftp://parameter.json";
×
180
    metadata_type["fileCrc"] = 0; // TODO
×
181
    metadata_type["uriFallback"] = ""; // TODO
×
182
    metadata_type["fileCrcFallback"] = 0;
×
183
    metadata_types.append(metadata_type);
×
184
    root["metadataTypes"] = metadata_types;
×
185

186
    return root.toStyledString();
×
187
}
188

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