Skip to content

Commit e45d1f8

Browse files
authored
Merge pull request #2007 from mavlink/pr-v1.4-wait-for-info
[BACKPORT v1.4] info: wait up to 1.5s for information
2 parents 5099eb8 + 4a1527b commit e45d1f8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/mavsdk/plugins/info/info_impl.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ std::string InfoImpl::translate_binary_to_str(uint8_t* binary, unsigned binary_l
191191

192192
std::pair<Info::Result, Info::Identification> InfoImpl::get_identification() const
193193
{
194+
wait_for_information();
195+
194196
std::lock_guard<std::mutex> lock(_mutex);
195197
return std::make_pair<>(
196198
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
@@ -199,23 +201,30 @@ std::pair<Info::Result, Info::Identification> InfoImpl::get_identification() con
199201

200202
std::pair<Info::Result, Info::Version> InfoImpl::get_version() const
201203
{
204+
wait_for_information();
205+
202206
std::lock_guard<std::mutex> lock(_mutex);
207+
203208
return std::make_pair<>(
204209
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
205210
_version);
206211
}
207212

208213
std::pair<Info::Result, Info::Product> InfoImpl::get_product() const
209214
{
215+
wait_for_information();
210216
std::lock_guard<std::mutex> lock(_mutex);
217+
211218
return std::make_pair<>(
212219
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
213220
_product);
214221
}
215222

216223
std::pair<Info::Result, Info::FlightInfo> InfoImpl::get_flight_information() const
217224
{
225+
wait_for_information();
218226
std::lock_guard<std::mutex> lock(_mutex);
227+
219228
return std::make_pair<>(
220229
(_flight_information_received ? Info::Result::Success :
221230
Info::Result::InformationNotReceivedYet),
@@ -281,4 +290,15 @@ std::pair<Info::Result, double> InfoImpl::get_speed_factor() const
281290
return std::make_pair<>(Info::Result::Success, speed_factor);
282291
}
283292

293+
void InfoImpl::wait_for_information() const
294+
{
295+
// Wait 1.5 seconds max
296+
for (unsigned i = 0; i < 150; ++i) {
297+
if (_information_received) {
298+
break;
299+
}
300+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
301+
}
302+
}
303+
284304
} // namespace mavsdk

src/mavsdk/plugins/info/info_impl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ class InfoImpl : public PluginImplBase {
3838
void process_flight_information(const mavlink_message_t& message);
3939
void process_attitude(const mavlink_message_t& message);
4040

41+
void wait_for_information() const;
42+
4143
mutable std::mutex _mutex{};
4244

4345
Info::Version _version{};
4446
Info::Product _product{};
4547
Info::Identification _identification{};
4648
Info::FlightInfo _flight_info{};
47-
bool _information_received{false};
49+
std::atomic<bool> _information_received{false};
4850
bool _flight_information_received{false};
4951
bool _was_armed{false};
5052

0 commit comments

Comments
 (0)