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

mavsdk_server: default to the first autopilot #2159

Merged
merged 2 commits into from
Oct 20, 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
15 changes: 14 additions & 1 deletion src/mavsdk/core/lazy_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mutex>

#include <mavsdk.h>
#include <log.h>

namespace mavsdk::mavsdk_server {

Expand All @@ -18,7 +19,7 @@ template<typename Plugin> class LazyPlugin {
if (_mavsdk.systems().empty()) {
return nullptr;
}
_plugin = std::make_unique<Plugin>(_mavsdk.systems()[0]);
_plugin = std::make_unique<Plugin>(first_autopilot());
}
return _plugin.get();
}
Expand All @@ -27,6 +28,18 @@ template<typename Plugin> class LazyPlugin {
Mavsdk& _mavsdk;
std::unique_ptr<Plugin> _plugin{};
std::mutex _mutex{};

std::shared_ptr<System> first_autopilot()
{
for (auto system : _mavsdk.systems()) {
if (system->has_autopilot()) {
return system;
}
}

LogErr() << "No autopilot found!";
return nullptr;
}
};

} // namespace mavsdk::mavsdk_server
1 change: 1 addition & 0 deletions src/mavsdk/core/mocks/system_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace testing {
class MockSystem {
public:
MOCK_CONST_METHOD0(is_connected, bool()){};
MOCK_CONST_METHOD0(has_autopilot, bool()){};
};

} // namespace testing
Expand Down
15 changes: 8 additions & 7 deletions src/mavsdk_server/src/connection_initiator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ template<typename Mavsdk> class ConnectionInitiator {

mavsdk.subscribe_on_new_system([this, &mavsdk]() {
std::lock_guard<std::mutex> guard(_mutex);
const auto system = mavsdk.systems().at(0);

if (!_is_discovery_finished && system->is_connected()) {
LogInfo() << "System discovered";

_is_discovery_finished = true;
_discovery_promise->set_value(true);
for (auto system : mavsdk.systems()) {
if (!_is_discovery_finished && system->has_autopilot() && system->is_connected()) {
LogInfo() << "System discovered";

_is_discovery_finished = true;
_discovery_promise->set_value(true);
break;
}
}
});

Expand Down
3 changes: 3 additions & 0 deletions src/mavsdk_server/test/connection_initiator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ TEST(ConnectionInitiator, startHangsUntilSystemDiscovered)
EXPECT_CALL(mavsdk, systems()).WillOnce(testing::Return(systems));

EXPECT_CALL(*system, is_connected()).WillOnce(testing::Return(true));
EXPECT_CALL(*system, has_autopilot()).WillOnce(testing::Return(true));

auto async_future = std::async(std::launch::async, [&initiator, &mavsdk]() {
initiator.start(mavsdk, ARBITRARY_CONNECTION_URL);
Expand All @@ -80,6 +81,7 @@ TEST(ConnectionInitiator, connectionDetectedIfDiscoverCallbackCalledBeforeWait)
EXPECT_CALL(mavsdk, systems()).WillOnce(testing::Return(systems));

EXPECT_CALL(*system, is_connected()).WillOnce(testing::Return(true));
EXPECT_CALL(*system, has_autopilot()).WillOnce(testing::Return(true));

initiator.start(mavsdk, ARBITRARY_CONNECTION_URL);
change_callback();
Expand All @@ -99,6 +101,7 @@ TEST(ConnectionInitiator, doesNotCrashIfDiscoverCallbackCalledMoreThanOnce)
EXPECT_CALL(mavsdk, systems()).WillRepeatedly(testing::Return(systems));

EXPECT_CALL(*system, is_connected()).WillRepeatedly(testing::Return(true));
EXPECT_CALL(*system, has_autopilot()).WillOnce(testing::Return(true));

initiator.start(mavsdk, ARBITRARY_CONNECTION_URL);
change_callback();
Expand Down
Loading