Skip to content

Commit

Permalink
Merge pull request #2007 from mavlink/pr-v1.4-wait-for-info
Browse files Browse the repository at this point in the history
[BACKPORT v1.4] info: wait up to 1.5s for information
  • Loading branch information
julianoes authored Mar 25, 2023
2 parents 5099eb8 + 4a1527b commit e45d1f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/mavsdk/plugins/info/info_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ std::string InfoImpl::translate_binary_to_str(uint8_t* binary, unsigned binary_l

std::pair<Info::Result, Info::Identification> InfoImpl::get_identification() const
{
wait_for_information();

std::lock_guard<std::mutex> lock(_mutex);
return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
Expand All @@ -199,23 +201,30 @@ std::pair<Info::Result, Info::Identification> InfoImpl::get_identification() con

std::pair<Info::Result, Info::Version> InfoImpl::get_version() const
{
wait_for_information();

std::lock_guard<std::mutex> lock(_mutex);

return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
_version);
}

std::pair<Info::Result, Info::Product> InfoImpl::get_product() const
{
wait_for_information();
std::lock_guard<std::mutex> lock(_mutex);

return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
_product);
}

std::pair<Info::Result, Info::FlightInfo> InfoImpl::get_flight_information() const
{
wait_for_information();
std::lock_guard<std::mutex> lock(_mutex);

return std::make_pair<>(
(_flight_information_received ? Info::Result::Success :
Info::Result::InformationNotReceivedYet),
Expand Down Expand Up @@ -281,4 +290,15 @@ std::pair<Info::Result, double> InfoImpl::get_speed_factor() const
return std::make_pair<>(Info::Result::Success, speed_factor);
}

void InfoImpl::wait_for_information() const
{
// Wait 1.5 seconds max
for (unsigned i = 0; i < 150; ++i) {
if (_information_received) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

} // namespace mavsdk
4 changes: 3 additions & 1 deletion src/mavsdk/plugins/info/info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ class InfoImpl : public PluginImplBase {
void process_flight_information(const mavlink_message_t& message);
void process_attitude(const mavlink_message_t& message);

void wait_for_information() const;

mutable std::mutex _mutex{};

Info::Version _version{};
Info::Product _product{};
Info::Identification _identification{};
Info::FlightInfo _flight_info{};
bool _information_received{false};
std::atomic<bool> _information_received{false};
bool _flight_information_received{false};
bool _was_armed{false};

Expand Down

0 comments on commit e45d1f8

Please sign in to comment.