Skip to content

Commit dc2f0b1

Browse files
committed
param_server: add error when params are provided too late
The mavlink parameter protocol uses an index to set/get specific parameters. When all parameters are requested, the total count is sent out, together with all the parameters. Now once this overall count has been sent out, we can't add/provide more parameters on the server side. Until now, MAVSDK didn't care, but with this commit, we are adding a new error to signal that we can no longer change the param list. This implementation has a bit of a race condition unfortunately: if a request list message is received right in the beginning when all parameters are added, we lock the list too early. The correct fix for this would be a specific method to "end param provisions" and lock them down. However, requiring such a method would be a breaking change, so I'm not adding this now but I'm considering it for the next major version of MAVSDK.
1 parent ffe6735 commit dc2f0b1

File tree

18 files changed

+1776
-131
lines changed

18 files changed

+1776
-131
lines changed

src/mavsdk/core/mavlink_parameter_server.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ void MavlinkParameterServer::process_param_ext_request_list(const mavlink_messag
482482
void MavlinkParameterServer::broadcast_all_parameters(const bool extended)
483483
{
484484
std::lock_guard<std::mutex> lock(_all_params_mutex);
485+
486+
// Param used with index, we should no longer change the index
487+
_params_locked_down = true;
488+
485489
const auto all_params = _param_cache.all_parameters(extended);
486490
if (_parameter_debugging) {
487491
LogDebug() << "broadcast_all_parameters " << (extended ? "extended" : "") << ": "
@@ -687,4 +691,11 @@ MavlinkParameterServer::extract_request_read_param_identifier(
687691
}
688692
}
689693

694+
bool MavlinkParameterServer::params_locked_down() const
695+
{
696+
std::lock_guard<std::mutex> lock(_all_params_mutex);
697+
698+
return _params_locked_down;
699+
}
700+
690701
} // namespace mavsdk

src/mavsdk/core/mavlink_parameter_server.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class MavlinkParameterServer : public MavlinkParameterSubscription {
6464
void set_extended_protocol(bool extended_protocol) { _extended_protocol = extended_protocol; };
6565
void do_work();
6666

67+
bool params_locked_down() const;
68+
6769
friend std::ostream& operator<<(std::ostream&, const Result&);
6870

6971
// Non-copyable
@@ -121,14 +123,16 @@ class MavlinkParameterServer : public MavlinkParameterSubscription {
121123
Sender& _sender;
122124
MavlinkMessageHandler& _message_handler;
123125

124-
std::mutex _all_params_mutex{};
126+
mutable std::mutex _all_params_mutex{};
125127
MavlinkParameterCache _param_cache{};
126128

127129
LockedQueue<WorkItem> _work_queue{};
128130

129131
bool _extended_protocol = true;
130132
bool _parameter_debugging = false;
131133
bool _last_extended = true;
134+
135+
bool _params_locked_down = false;
132136
};
133137

134138
} // namespace mavsdk

src/mavsdk/plugins/action/action.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ Action::Result Action::set_current_speed(float speed_m_s) const
256256
return _impl->set_current_speed(speed_m_s);
257257
}
258258

259+
Action::Result Action::set_gps_global_origin(
260+
double latitude_deg, double longitude_deg, float absolute_altitude_m) const
261+
{
262+
return _impl->set_gps_global_origin(latitude_deg, longitude_deg, absolute_altitude_m);
263+
}
264+
259265
std::ostream& operator<<(std::ostream& str, Action::Result const& result)
260266
{
261267
switch (result) {

src/mavsdk/plugins/action/include/plugins/action/action.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,20 @@ class Action : public PluginBase {
644644
*/
645645
Result set_current_speed(float speed_m_s) const;
646646

647+
/**
648+
* @brief Set GPS Global Origin.
649+
*
650+
* Sets the GPS coordinates of the vehicle local origin (0,0,0) position.
651+
*
652+
* This function is blocking.
653+
*
654+
655+
* @return Result of request.
656+
657+
*/
658+
Result set_gps_global_origin(
659+
double latitude_deg, double longitude_deg, float absolute_altitude_m) const;
660+
647661
/**
648662
* @brief Copy constructor.
649663
*/

src/mavsdk/plugins/param_server/include/plugins/param_server/param_server.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class ParamServer : public ServerPluginBase {
150150
ParamNameTooLong, /**< @brief Parameter name too long (> 16). */
151151
NoSystem, /**< @brief No system available. */
152152
ParamValueTooLong, /**< @brief Parameter name too long (> 128). */
153+
ParamProvidedTooLate, /**< @brief All parameters have to be provided upfront. */
153154
};
154155

155156
/**
@@ -195,6 +196,10 @@ class ParamServer : public ServerPluginBase {
195196
*
196197
* If the type is wrong, the result will be `WRONG_TYPE`.
197198
*
199+
* Note that all params need to be provided upfront. Once a client has
200+
* requested a param list, the indices are locked and no more params
201+
* can be added.
202+
*
198203
* This function is blocking.
199204
*
200205
@@ -208,6 +213,10 @@ class ParamServer : public ServerPluginBase {
208213
*
209214
* If the type is wrong, the result will be `WRONG_TYPE`.
210215
*
216+
* Note that all params need to be provided upfront. Once a client has
217+
* requested a param list, the indices are locked and no more params
218+
* can be added.
219+
*
211220
* This function is blocking.
212221
*
213222
* @return Result of request.
@@ -232,6 +241,10 @@ class ParamServer : public ServerPluginBase {
232241
*
233242
* If the type is wrong, the result will be `WRONG_TYPE`.
234243
*
244+
* Note that all params need to be provided upfront. Once a client has
245+
* requested a param list, the indices are locked and no more params
246+
* can be added.
247+
*
235248
* This function is blocking.
236249
*
237250
* @return Result of request.

src/mavsdk/plugins/param_server/param_server.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ std::ostream& operator<<(std::ostream& str, ParamServer::Result const& result)
187187
return str << "No System";
188188
case ParamServer::Result::ParamValueTooLong:
189189
return str << "Param Value Too Long";
190+
case ParamServer::Result::ParamProvidedTooLate:
191+
return str << "Param Provided Too Late";
190192
default:
191193
return str << "Unknown";
192194
}

src/mavsdk/plugins/param_server/param_server_impl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ ParamServer::Result ParamServerImpl::provide_param_int(std::string name, int32_t
6262
if (name.size() > 16) {
6363
return ParamServer::Result::ParamNameTooLong;
6464
}
65+
66+
if (_server_component_impl->mavlink_parameter_server().params_locked_down()) {
67+
return ParamServer::Result::ParamProvidedTooLate;
68+
}
69+
6570
const auto ret =
6671
_server_component_impl->mavlink_parameter_server().provide_server_param_int(name, value);
6772
if (ret == MavlinkParameterServer::Result::Ok) {
@@ -90,6 +95,11 @@ ParamServer::Result ParamServerImpl::provide_param_float(std::string name, float
9095
if (name.size() > 16) {
9196
return ParamServer::Result::ParamNameTooLong;
9297
}
98+
99+
if (_server_component_impl->mavlink_parameter_server().params_locked_down()) {
100+
return ParamServer::Result::ParamProvidedTooLate;
101+
}
102+
93103
const auto ret =
94104
_server_component_impl->mavlink_parameter_server().provide_server_param_float(name, value);
95105
if (ret == MavlinkParameterServer::Result::Ok) {

src/mavsdk_server/src/generated/action/action.grpc.pb.cc

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)