diff --git a/backend/proto b/backend/proto index 33ab61c3ed..0dd8410af2 160000 --- a/backend/proto +++ b/backend/proto @@ -1 +1 @@ -Subproject commit 33ab61c3ed40d0faedefc7c4bc6dde37e6d6cb1f +Subproject commit 0dd8410af27396ba52be154220a69bf220da1560 diff --git a/backend/src/backend.cpp b/backend/src/backend.cpp index 7fbac21db1..3b4d106434 100644 --- a/backend/src/backend.cpp +++ b/backend/src/backend.cpp @@ -14,7 +14,7 @@ #include "core/corerpc_impl.h" #include "dronecore.h" #include "log.h" -#include "mission/missionrpc_impl.h" +#include "mission/mission_service_impl.h" #include "telemetry/telemetry_service_impl.h" namespace dronecore { @@ -42,7 +42,7 @@ class DroneCoreBackend::Impl builder.RegisterService(&actionService); Mission mission(_dc.device()); - MissionServiceImpl missionService(mission); + MissionServiceImpl missionService(mission); builder.RegisterService(&missionService); Telemetry telemetry(_dc.device()); diff --git a/backend/src/plugins/mission/mission_service_impl.h b/backend/src/plugins/mission/mission_service_impl.h new file mode 100644 index 0000000000..db3a32398f --- /dev/null +++ b/backend/src/plugins/mission/mission_service_impl.h @@ -0,0 +1,21 @@ +#include + +#include "mission/mission.h" +#include "mission/mission.grpc.pb.h" + +namespace dronecore { +namespace backend { + +template +class MissionServiceImpl final : public dronecore::rpc::mission::MissionService::Service +{ +public: + MissionServiceImpl(Mission &mission) + : _mission(mission) {} + +private: + Mission &_mission; +}; + +} // namespace backend +} // namespace dronecore diff --git a/backend/src/plugins/mission/missionrpc_impl.h b/backend/src/plugins/mission/missionrpc_impl.h deleted file mode 100644 index ff8eaac570..0000000000 --- a/backend/src/plugins/mission/missionrpc_impl.h +++ /dev/null @@ -1,73 +0,0 @@ -#include - -#include "mission/mission.h" -#include "mission/mission.grpc.pb.h" - -using grpc::Status; -using grpc::ServerContext; - -using namespace dronecore; - -class MissionServiceImpl final : public rpc::mission::MissionService::Service -{ -public: - MissionServiceImpl(Mission &mission) - : mission(mission) {} - - Status SendMission(ServerContext *context, const rpc::mission::SendMissionRequest *request, - rpc::mission::MissionResult *response) override - { - // TODO: there has to be a beter way than using std::future. - auto prom = std::make_shared>(); - auto future_result = prom->get_future(); - - std::vector> mission_items; - for (auto &mission_item : request->mission().mission_items()) { - auto new_item = std::make_shared(); - new_item->set_position(mission_item.latitude_deg(), mission_item.longitude_deg()); - new_item->set_relative_altitude(mission_item.relative_altitude_m()); - new_item->set_speed(mission_item.speed_m_s()); - new_item->set_fly_through(mission_item.is_fly_through()); - new_item->set_gimbal_pitch_and_yaw(mission_item.gimbal_pitch_deg(), - mission_item.gimbal_yaw_deg()); - new_item->set_camera_action(static_cast - (mission_item.camera_action())); - mission_items.push_back(new_item); - } - - mission.upload_mission_async( - mission_items, [prom, response](Mission::Result result) { - response->set_result(static_cast(result)); - response->set_result_str(Mission::result_str(result)); - prom->set_value(); - } - ); - - future_result.wait(); - future_result.get(); - return Status::OK; - } - - Status StartMission(ServerContext *context, const rpc::mission::StartMissionRequest *request, - rpc::mission::MissionResult *response) override - { - // TODO: there has to be a beter way than using std::future. - auto prom = std::make_shared>(); - auto future_result = prom->get_future(); - - mission.start_mission_async( - [prom, response](Mission::Result result) { - response->set_result(static_cast(result)); - response->set_result_str(Mission::result_str(result)); - prom->set_value(); - } - ); - - future_result.wait(); - future_result.get(); - return Status::OK; - } - -private: - Mission &mission; -}; diff --git a/backend/test/CMakeLists.txt b/backend/test/CMakeLists.txt index e5b48fef77..4b54e36616 100644 --- a/backend/test/CMakeLists.txt +++ b/backend/test/CMakeLists.txt @@ -1,8 +1,9 @@ cmake_minimum_required(VERSION 3.1) add_executable(unit_tests_backend - connection_initiator_test.cpp action_service_impl_test.cpp + connection_initiator_test.cpp + mission_service_impl_test.cpp telemetry_service_impl_test.cpp ) diff --git a/backend/test/action_service_impl_test.cpp b/backend/test/action_service_impl_test.cpp index a44e19841a..8e8f6ecb11 100644 --- a/backend/test/action_service_impl_test.cpp +++ b/backend/test/action_service_impl_test.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include "action/action_service_impl.h" #include "action/mocks/action_mock.h" diff --git a/backend/test/mission_service_impl_test.cpp b/backend/test/mission_service_impl_test.cpp new file mode 100644 index 0000000000..2f47309416 --- /dev/null +++ b/backend/test/mission_service_impl_test.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +#include "mission/mission_service_impl.h" +#include "mission/mocks/mission_mock.h" + +namespace { + +using testing::NiceMock; +using testing::Return; + +using MockMission = NiceMock; +using MissionServiceImpl = dronecore::backend::MissionServiceImpl; + +using MissionResult = dronecore::rpc::mission::MissionResult; +using InputPair = std::pair; + +std::vector generateInputPairs(); + +class MissionServiceImplTest : public ::testing::TestWithParam {}; + +TEST_P(MissionServiceImplTest, uploadResultIsTranslatedCorrectly) +{ + // TODO +} + +INSTANTIATE_TEST_CASE_P(MissionResultCorrespondences, + MissionServiceImplTest, + ::testing::ValuesIn(generateInputPairs())); + +std::vector generateInputPairs() +{ + std::vector input_pairs; + input_pairs.push_back(std::make_pair("UNKNOWN", dronecore::Mission::Result::UNKNOWN)); + input_pairs.push_back(std::make_pair("SUCCESS", dronecore::Mission::Result::SUCCESS)); + input_pairs.push_back(std::make_pair("ERROR", dronecore::Mission::Result::ERROR)); + input_pairs.push_back(std::make_pair("TOO_MANY_MISSION_ITEMS", + dronecore::Mission::Result::TOO_MANY_MISSION_ITEMS)); + input_pairs.push_back(std::make_pair("BUSY", dronecore::Mission::Result::BUSY)); + input_pairs.push_back(std::make_pair("TIMEOUT", dronecore::Mission::Result::TIMEOUT)); + input_pairs.push_back(std::make_pair("INVALID_ARGUMENT", + dronecore::Mission::Result::INVALID_ARGUMENT)); + input_pairs.push_back(std::make_pair("UNSUPPORTED", dronecore::Mission::Result::UNSUPPORTED)); + input_pairs.push_back(std::make_pair("NO_MISSION_AVAILABLE", + dronecore::Mission::Result::NO_MISSION_AVAILABLE)); + input_pairs.push_back(std::make_pair("FAILED_TO_OPEN_QGC_PLAN", + dronecore::Mission::Result::FAILED_TO_OPEN_QGC_PLAN)); + input_pairs.push_back(std::make_pair("FAILED_TO_PARSE_QGC_PLAN", + dronecore::Mission::Result::FAILED_TO_PARSE_QGC_PLAN)); + input_pairs.push_back(std::make_pair("UNSUPPORTED_MISSION_CMD", + dronecore::Mission::Result::UNSUPPORTED_MISSION_CMD)); + + return input_pairs; +} + +} // namespace diff --git a/plugins/mission/mission.h b/plugins/mission/mission.h index 7e2bd564f5..49baabb12e 100644 --- a/plugins/mission/mission.h +++ b/plugins/mission/mission.h @@ -39,7 +39,8 @@ class Mission : public PluginBase * @brief Possible results returned for mission requests. */ enum class Result { - SUCCESS = 0, /**< @brief %Request succeeded. */ + UNKNOWN, /**< @brief Unknown error. */ + SUCCESS, /**< @brief %Request succeeded. */ ERROR, /**< @brief Error. */ TOO_MANY_MISSION_ITEMS, /**< @brief Too many MissionItem objects in the mission. */ BUSY, /**< @brief %Vehicle busy. */ @@ -49,8 +50,7 @@ class Mission : public PluginBase NO_MISSION_AVAILABLE, /**< @brief No mission available on device. */ FAILED_TO_OPEN_QGC_PLAN, /**< @brief Failed to open QGroundControl plan */ FAILED_TO_PARSE_QGC_PLAN, /**< @brief Failed to parse QGroundControl plan */ - UNSUPPORTED_MISSION_CMD, /**< @brief Unsupported mission command */ - UNKNOWN /**< @brief Unknown error. */ + UNSUPPORTED_MISSION_CMD /**< @brief Unsupported mission command */ }; /** diff --git a/plugins/mission/mocks/mission_mock.h b/plugins/mission/mocks/mission_mock.h new file mode 100644 index 0000000000..af02b42000 --- /dev/null +++ b/plugins/mission/mocks/mission_mock.h @@ -0,0 +1,21 @@ +#include +#include +#include + +#include "mission/mission.h" // TODO: remove this dependency by moving the datastructs out of Mission +#include "mission/mission_item.h" + +namespace dronecore { +namespace testing { + +typedef std::function result_callback_t; + +class MockMission +{ +public: + MOCK_CONST_METHOD2(upload_mission_async, void(const std::vector> &, + result_callback_t)); +}; + +} // namespace testing +} // namespace dronecore