Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACKPORT v1.4] info: wait up to 1.5s for information #2007

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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