Skip to content

Commit

Permalink
camera server interation test working
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Apr 9, 2022
1 parent 13c7bf4 commit 9597656
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 43 deletions.
53 changes: 34 additions & 19 deletions src/integration_tests/camera_take_photo.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
#include <iostream>
#include <functional>
#include <vector>
#include <atomic>
#include <future>
#include "integration_test_helper.h"
#include "mavsdk.h"
#include "system.h"
#include "camera_test_helpers.h"
#include "plugins/camera/camera.h"
#include "plugins/camera_server/camera_server.h"

using namespace mavsdk;

static void receive_capture_info(Camera::CaptureInfo capture_info, bool& received_capture_info);

TEST(CameraTest, TakePhotoSingle)
{
Mavsdk mavsdk;
Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});

ConnectionResult ret = mavsdk.add_udp_connection();
ASSERT_EQ(ret, ConnectionResult::Success);
Mavsdk mavsdk_camera;
mavsdk_camera.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::Camera});

ASSERT_EQ(mavsdk_groundstation.add_any_connection("udp://:17000"), ConnectionResult::Success);
ASSERT_EQ(mavsdk_camera.add_any_connection("udp://127.0.0.1:17000"), ConnectionResult::Success);

auto camera_server = CameraServer{mavsdk_camera};
camera_server.subscribe_take_photo([&camera_server](CameraServer::Result, int32_t) {
LogInfo() << "Let's take a photo now!";

CameraServer::CaptureInfo info;
info.index = 99;
info.is_success = true;

camera_server.respond_take_photo(info);
});

// Wait for system to connect via heartbeat.
std::this_thread::sleep_for(std::chrono::seconds(2));

auto system = mavsdk.systems().at(0);
ASSERT_EQ(mavsdk_groundstation.systems().size(), 1);
auto system = mavsdk_groundstation.systems().at(0);
ASSERT_TRUE(system->has_camera());
auto camera = std::make_shared<Camera>(system);

auto camera = Camera{system};

std::this_thread::sleep_for(std::chrono::seconds(1));

// We want to take the picture in photo mode.
EXPECT_EQ(camera->set_mode(Camera::Mode::Photo), Camera::Result::Success);
// EXPECT_EQ(camera.set_mode(Camera::Mode::Photo), Camera::Result::Success);

bool received_capture_info = false;
camera->subscribe_capture_info([&received_capture_info](Camera::CaptureInfo capture_info) {
receive_capture_info(capture_info, received_capture_info);
});
// bool received_capture_info = false;
// camera->subscribe_capture_info([&received_capture_info](Camera::CaptureInfo capture_info) {
// receive_capture_info(capture_info, received_capture_info);
//});

EXPECT_EQ(camera->take_photo(), Camera::Result::Success);
std::this_thread::sleep_for(std::chrono::seconds(5));
EXPECT_TRUE(received_capture_info);
EXPECT_EQ(camera.take_photo(), Camera::Result::Success);
// std::this_thread::sleep_for(std::chrono::seconds(5));
// EXPECT_TRUE(received_capture_info);
}

TEST(CameraTest, TakePhotosMultiple)
Expand Down
1 change: 1 addition & 0 deletions src/mavsdk/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ target_sources(mavsdk
ping.cpp
plugin_impl_base.cpp
serial_connection.cpp
server_component.cpp
server_component_impl.cpp
server_plugin_impl_base.cpp
tcp_connection.cpp
Expand Down
5 changes: 5 additions & 0 deletions src/mavsdk/core/include/mavsdk/mavsdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

#include "deprecated.h"
#include "system.h"
#include "server_component.h"
#include "connection_result.h"

namespace mavsdk {

class ServerPluginImplBase;

/**
* @brief ForwardingOption for Connection, used to set message forwarding option.
*/
Expand Down Expand Up @@ -304,6 +307,8 @@ class Mavsdk {
/* @private. */
std::shared_ptr<MavsdkImpl> _impl{};

friend ServerPluginImplBase;

// Non-copyable
Mavsdk(const Mavsdk&) = delete;
const Mavsdk& operator=(const Mavsdk&) = delete;
Expand Down
13 changes: 9 additions & 4 deletions src/mavsdk/core/include/mavsdk/server_component.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#pragma once

#include <cstdint>
#include <memory>

namespace mavsdk {

class MavsdkImpl;
class ServerComponentImpl;
class ServerPluginImplBase;

/**
* TODO: add comments
*/
class ServerComponent {
public:
private:
std::shared_ptr<ServerComponentImpl> server_component_impl() { return _server_component_impl; };

std::shared_ptr<ServerComponentImpl> _server_component_impl;
ServerComponent(MavsdkImpl& mavsdk_impl, uint8_t component_id);

private:
friend ServerPluginImplBase;

std::shared_ptr<ServerComponentImpl> _impl;
};

} // namespace mavsdk
1 change: 1 addition & 0 deletions src/mavsdk/core/mavlink_command_receiver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mavlink_command_receiver.h"
#include "mavsdk_impl.h"
#include "log.h"
#include <cmath>
#include <future>
#include <memory>
Expand Down
21 changes: 21 additions & 0 deletions src/mavsdk/core/mavsdk_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ std::vector<std::shared_ptr<System>> MavsdkImpl::systems() const
return systems_result;
}

std::shared_ptr<ServerComponent> MavsdkImpl::server_component(uint8_t component_id)
{
if (component_id == 0) {
LogErr() << "Server component with component ID 0 not allowed";
return nullptr;
}

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

for (auto& it : _server_components) {
if (it.first == component_id) {
return it.second;
}
}

_server_components.emplace_back(std::pair<uint8_t, std::shared_ptr<ServerComponent>>(
component_id, std::make_shared<ServerComponent>(*this, component_id)));

return _server_components.back().second;
}

void MavsdkImpl::forward_message(mavlink_message_t& message, Connection* connection)
{
// Forward_message Function implementing Mavlink routing rules.
Expand Down
6 changes: 5 additions & 1 deletion src/mavsdk/core/mavsdk_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class MavsdkImpl {

std::vector<std::shared_ptr<System>> systems() const;

std::shared_ptr<ServerComponent> server_component(uint8_t component_id);

void set_configuration(Mavsdk::Configuration new_configuration);

uint8_t get_own_system_id() const;
Expand Down Expand Up @@ -117,9 +119,11 @@ class MavsdkImpl {
std::vector<std::shared_ptr<Connection>> _connections{};

mutable std::mutex _systems_mutex{};

std::vector<std::pair<uint8_t, std::shared_ptr<System>>> _systems{};

mutable std::mutex _server_components_mutex{};
std::vector<std::pair<uint8_t, std::shared_ptr<ServerComponent>>> _server_components{};

std::mutex _new_system_callback_mutex{};
Mavsdk::NewSystemCallback _new_system_callback{nullptr};

Expand Down
11 changes: 11 additions & 0 deletions src/mavsdk/core/server_component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "server_component.h"
#include "server_component_impl.h"
#include "mavsdk_impl.h"

namespace mavsdk {

ServerComponent::ServerComponent(MavsdkImpl& mavsdk_impl, uint8_t component_id) :
_impl(std::make_shared<ServerComponentImpl>(mavsdk_impl, component_id))
{}

} // namespace mavsdk
18 changes: 16 additions & 2 deletions src/mavsdk/core/server_component_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
#include "server_component_impl.h"
#include "server_plugin_impl_base.h"
#include "mavsdk_impl.h"

namespace mavsdk {

ServerComponentImpl::ServerComponentImpl(MavsdkImpl& mavsdk_impl) :
ServerComponentImpl::ServerComponentImpl(MavsdkImpl& mavsdk_impl, uint8_t component_id) :
_mavsdk_impl(mavsdk_impl),
_mavlink_command_receiver(mavsdk_impl)
_mavlink_command_receiver(mavsdk_impl),
_own_component_id(component_id)
{}

ServerComponentImpl::~ServerComponentImpl() {}

void ServerComponentImpl::register_plugin(ServerPluginImplBase* server_plugin_impl)
{
// This is a bit pointless now but just mirrors what is done for the client plugins.
server_plugin_impl->init();
}

void ServerComponentImpl::unregister_plugin(ServerPluginImplBase* server_plugin_impl)
{
// This is a bit pointless now but just mirrors what is done for the client plugins.
server_plugin_impl->deinit();
}

void ServerComponentImpl::register_mavlink_command_handler(
uint16_t cmd_id,
const MavlinkCommandReceiver::MavlinkCommandIntHandler& callback,
Expand Down
6 changes: 5 additions & 1 deletion src/mavsdk/core/server_component_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
namespace mavsdk {

class MavsdkImpl;
class ServerPluginImplBase;

class ServerComponentImpl {
public:
explicit ServerComponentImpl(MavsdkImpl& parent);
ServerComponentImpl(MavsdkImpl& mavsdk_impl, uint8_t component_id);
~ServerComponentImpl();

void register_plugin(ServerPluginImplBase* server_plugin_impl);
void unregister_plugin(ServerPluginImplBase* server_plugin_impl);

void register_mavlink_command_handler(
uint16_t cmd_id,
const MavlinkCommandReceiver::MavlinkCommandIntHandler& callback,
Expand Down
6 changes: 4 additions & 2 deletions src/mavsdk/core/server_plugin_impl_base.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "server_plugin_impl_base.h"
#include "mavsdk.h"
#include "mavsdk_impl.h"
#include "server_component.h"

namespace mavsdk {

ServerPluginImplBase::ServerPluginImplBase(std::shared_ptr<ServerComponent> server_component) :
_server_component_impl(server_component->server_component_impl())
ServerPluginImplBase::ServerPluginImplBase(Mavsdk& mavsdk) :
_server_component_impl(mavsdk._impl->server_component(MAV_COMP_ID_CAMERA)->_impl)
{}

} // namespace mavsdk
4 changes: 2 additions & 2 deletions src/mavsdk/core/server_plugin_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace mavsdk {

class ServerComponent;
class Mavsdk;

class ServerPluginImplBase {
public:
explicit ServerPluginImplBase(std::shared_ptr<ServerComponent> server_component);
explicit ServerPluginImplBase(Mavsdk& mavsdk);
virtual ~ServerPluginImplBase() = default;

/*
Expand Down
4 changes: 2 additions & 2 deletions src/mavsdk/plugins/camera_server/camera_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ using Position = CameraServer::Position;
using Quaternion = CameraServer::Quaternion;
using CaptureInfo = CameraServer::CaptureInfo;

CameraServer::CameraServer(std::shared_ptr<ServerComponent> server_component) :
CameraServer::CameraServer(Mavsdk& mavsdk) :
ServerPluginBase(),
_impl{std::make_unique<CameraServerImpl>(server_component)}
_impl{std::make_unique<CameraServerImpl>(mavsdk)}
{}

CameraServer::~CameraServer() {}
Expand Down
8 changes: 4 additions & 4 deletions src/mavsdk/plugins/camera_server/camera_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

namespace mavsdk {

CameraServerImpl::CameraServerImpl(std::shared_ptr<ServerComponent> server_component) :
ServerPluginImplBase(server_component)
CameraServerImpl::CameraServerImpl(Mavsdk& mavsdk) : ServerPluginImplBase(mavsdk)
{
//_server_component_impl->register_plugin(this);
_server_component_impl->register_plugin(this);
}

CameraServerImpl::~CameraServerImpl()
{
//_server_component_impl->unregister_plugin(this);
_server_component_impl->unregister_plugin(this);
}

void CameraServerImpl::init()
Expand Down Expand Up @@ -135,6 +134,7 @@ void CameraServerImpl::init()
void CameraServerImpl::deinit()
{
stop_image_capture_interval();
_server_component_impl->unregister_all_mavlink_command_handlers(this);
}

bool CameraServerImpl::parse_version_string(const std::string& version_str)
Expand Down
2 changes: 1 addition & 1 deletion src/mavsdk/plugins/camera_server/camera_server_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace mavsdk {

class CameraServerImpl : public ServerPluginImplBase {
public:
explicit CameraServerImpl(std::shared_ptr<ServerComponent> server_component);
explicit CameraServerImpl(Mavsdk& mavsdk);
~CameraServerImpl();

void init() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace mavsdk {

class ServerComponent;
class Mavsdk;
class CameraServerImpl;

/**
Expand All @@ -26,17 +26,17 @@ class CameraServerImpl;
class CameraServer : public ServerPluginBase {
public:
/**
* @brief Constructor. Creates the plugin for a server component.
* @brief Constructor. Creates a server plugin.
*
* The plugin is typically created as shown below:
*
* ```cpp
* auto camera_server = CameraServer(server_component);
* auto camera_server = CameraServer(mavsdk);
* ```
*
* @param server_component The specific server component associated with this plugin.
* @param mavsdk The MAVSDK instance associated with this server plugin.
*/
explicit CameraServer(std::shared_ptr<ServerComponent> server_component);
explicit CameraServer(Mavsdk& mavsdk);

/**
* @brief Destructor (internal use only).
Expand Down

0 comments on commit 9597656

Please sign in to comment.