diff --git a/src/mavsdk/plugins/info/info_impl.cpp b/src/mavsdk/plugins/info/info_impl.cpp index 4adaebf475..9f640aff4b 100644 --- a/src/mavsdk/plugins/info/info_impl.cpp +++ b/src/mavsdk/plugins/info/info_impl.cpp @@ -191,6 +191,8 @@ std::string InfoImpl::translate_binary_to_str(uint8_t* binary, unsigned binary_l std::pair InfoImpl::get_identification() const { + wait_for_information(); + std::lock_guard lock(_mutex); return std::make_pair<>( (_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet), @@ -199,7 +201,10 @@ std::pair InfoImpl::get_identification() con std::pair InfoImpl::get_version() const { + wait_for_information(); + std::lock_guard lock(_mutex); + return std::make_pair<>( (_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet), _version); @@ -207,7 +212,9 @@ std::pair InfoImpl::get_version() const std::pair InfoImpl::get_product() const { + wait_for_information(); std::lock_guard lock(_mutex); + return std::make_pair<>( (_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet), _product); @@ -215,7 +222,9 @@ std::pair InfoImpl::get_product() const std::pair InfoImpl::get_flight_information() const { + wait_for_information(); std::lock_guard lock(_mutex); + return std::make_pair<>( (_flight_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet), @@ -281,4 +290,15 @@ std::pair 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 diff --git a/src/mavsdk/plugins/info/info_impl.h b/src/mavsdk/plugins/info/info_impl.h index 64012d4b10..c25ab8be46 100644 --- a/src/mavsdk/plugins/info/info_impl.h +++ b/src/mavsdk/plugins/info/info_impl.h @@ -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 _information_received{false}; bool _flight_information_received{false}; bool _was_armed{false};