Skip to content

Commit

Permalink
WIP server component
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Apr 9, 2022
1 parent d191494 commit 13c7bf4
Show file tree
Hide file tree
Showing 25 changed files with 526 additions and 334 deletions.
2 changes: 1 addition & 1 deletion src/integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_executable(integration_tests_runner
camera_take_photo_interval.cpp
camera_format.cpp
camera_test_helpers.cpp
component_information.cpp
#component_information.cpp
connection.cpp
follow_me.cpp
geofence_inclusion.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/mavsdk/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ target_sources(mavsdk
ping.cpp
plugin_impl_base.cpp
serial_connection.cpp
server_component_impl.cpp
server_plugin_impl_base.cpp
tcp_connection.cpp
timeout_handler.cpp
udp_connection.cpp
Expand Down
20 changes: 20 additions & 0 deletions src/mavsdk/core/include/mavsdk/server_component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

namespace mavsdk {

class ServerComponentImpl;

/**
* 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;

friend ServerPluginImplBase;
};

} // namespace mavsdk
33 changes: 33 additions & 0 deletions src/mavsdk/core/include/mavsdk/server_plugin_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "server_component_impl.h"
#include <memory>

namespace mavsdk {

class ServerComponent;
class ServerComponentImpl;

class ServerPluginBase {
public:
/**
* @brief Default Constructor.
*/
ServerPluginBase() = default;

/**
* @brief Default Destructor.
*/
virtual ~ServerPluginBase() = default;

/**
* @brief Copy constructor (object is not copyable).
*/
ServerPluginBase(const ServerPluginBase&) = delete;

/**
* @brief Assign operator (object is not copyable).
*/
const ServerPluginBase& operator=(const ServerPluginBase&) = delete;
};

} // namespace mavsdk
14 changes: 7 additions & 7 deletions src/mavsdk/core/mavlink_command_receiver.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "mavlink_command_receiver.h"
#include "system_impl.h"
#include "mavsdk_impl.h"
#include <cmath>
#include <future>
#include <memory>

namespace mavsdk {

MavlinkCommandReceiver::MavlinkCommandReceiver(SystemImpl& system_impl) : _parent(system_impl)
MavlinkCommandReceiver::MavlinkCommandReceiver(MavsdkImpl& mavsdk_impl) : _mavsdk_impl(mavsdk_impl)
{
_parent.register_mavlink_message_handler(
_mavsdk_impl.mavlink_message_handler.register_one(
MAVLINK_MSG_ID_COMMAND_LONG,
[this](const mavlink_message_t& message) { receive_command_long(message); },
this);

_parent.register_mavlink_message_handler(
_mavsdk_impl.mavlink_message_handler.register_one(
MAVLINK_MSG_ID_COMMAND_INT,
[this](const mavlink_message_t& message) { receive_command_int(message); },
this);
Expand All @@ -23,7 +23,7 @@ MavlinkCommandReceiver::~MavlinkCommandReceiver()
{
unregister_all_mavlink_command_handlers(this);

_parent.unregister_all_mavlink_message_handlers(this);
_mavsdk_impl.mavlink_message_handler.unregister_all(this);
}

void MavlinkCommandReceiver::receive_command_int(const mavlink_message_t& message)
Expand All @@ -37,7 +37,7 @@ void MavlinkCommandReceiver::receive_command_int(const mavlink_message_t& messag
// The client side can pack a COMMAND_ACK as a response to receiving the command.
auto maybe_message = handler.callback(cmd);
if (maybe_message) {
_parent.send_message(maybe_message.value());
_mavsdk_impl.send_message(maybe_message.value());
}
}
}
Expand All @@ -54,7 +54,7 @@ void MavlinkCommandReceiver::receive_command_long(const mavlink_message_t& messa
// The client side can pack a COMMAND_ACK as a response to receiving the command.
auto maybe_message = handler.callback(cmd);
if (maybe_message) {
_parent.send_message(maybe_message.value());
_mavsdk_impl.send_message(maybe_message.value());
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/mavsdk/core/mavlink_command_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

namespace mavsdk {

class SystemImpl;
class MavsdkImpl;

class MavlinkCommandReceiver {
public:
explicit MavlinkCommandReceiver(SystemImpl& system_impl);
explicit MavlinkCommandReceiver(MavsdkImpl& mavsdk_impl);
~MavlinkCommandReceiver();

struct CommandInt {
Expand Down Expand Up @@ -115,7 +115,7 @@ class MavlinkCommandReceiver {
void unregister_all_mavlink_command_handlers(const void* cookie);

private:
SystemImpl& _parent;
MavsdkImpl& _mavsdk_impl;

void receive_command_int(const mavlink_message_t& message);
void receive_command_long(const mavlink_message_t& message);
Expand Down
2 changes: 1 addition & 1 deletion src/mavsdk/core/mavlink_ftp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void MavlinkFtp::process_mavlink_ftp_message(const mavlink_message_t& msg)

MavlinkFtp::~MavlinkFtp()
{
_system_impl.unregister_all_mavlink_request_message_handlers(this);
//_mavlink_request_message_handler.unregister_all_handlers(this);
}

void MavlinkFtp::_process_ack(PayloadHeader* payload)
Expand Down
4 changes: 2 additions & 2 deletions src/mavsdk/core/mavlink_message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void MavlinkMessageHandler::register_one(
_table.push_back(entry);
}

void MAVLinkMessageHandler::register_one(
void MavlinkMessageHandler::register_one(
uint16_t msg_id,
std::optional<uint8_t> component_id,
const Callback& callback,
Expand Down Expand Up @@ -78,7 +78,7 @@ void MavlinkMessageHandler::process_message(const mavlink_message_t& message)
#endif
}

void MAVLinkMessageHandler::update_component_id(
void MavlinkMessageHandler::update_component_id(
uint16_t msg_id, uint8_t component_id, const void* cookie)
{
std::lock_guard<std::mutex> lock(_mutex);
Expand Down
2 changes: 1 addition & 1 deletion src/mavsdk/core/mavlink_mission_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace mavsdk {

MavlinkMissionTransfer::MavlinkMissionTransfer(
Sender& sender,
MAVLinkMessageHandler& message_handler,
MavlinkMessageHandler& message_handler,
TimeoutHandler& timeout_handler,
TimeoutSCallback timeout_s_callback) :
_sender(sender),
Expand Down
22 changes: 15 additions & 7 deletions src/mavsdk/core/mavlink_request_message_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#include "mavlink_request_message_handler.h"
#include "system_impl.h"
#include "mavsdk_impl.h"
#include "server_component_impl.h"
#include "mavlink_command_receiver.h"
#include "log.h"

namespace mavsdk {

MavlinkRequestMessageHandler::MavlinkRequestMessageHandler(SystemImpl& system_impl) :
_system_impl(system_impl)
MavlinkRequestMessageHandler::MavlinkRequestMessageHandler(
MavsdkImpl& mavsdk_impl,
ServerComponentImpl& server_component_impl,
MavlinkCommandReceiver& mavlink_command_receiver) :
_mavsdk_impl(mavsdk_impl),
_server_component_impl(server_component_impl),
_mavlink_command_receiver(mavlink_command_receiver)
{
_system_impl.register_mavlink_command_handler(
_mavlink_command_receiver.register_mavlink_command_handler(
MAV_CMD_REQUEST_MESSAGE,
[this](const MavlinkCommandReceiver::CommandLong& command) {
return handle_command_long(command);
Expand All @@ -16,7 +24,7 @@ MavlinkRequestMessageHandler::MavlinkRequestMessageHandler(SystemImpl& system_im

MavlinkRequestMessageHandler::~MavlinkRequestMessageHandler()
{
_system_impl.unregister_all_mavlink_message_handlers(this);
_mavsdk_impl.mavlink_message_handler.unregister_all(this);
}

bool MavlinkRequestMessageHandler::register_handler(
Expand Down Expand Up @@ -77,7 +85,7 @@ std::optional<mavlink_message_t> MavlinkRequestMessageHandler::handle_command_lo
command.params.param6});

if (result.has_value()) {
return _system_impl.make_command_ack_message(command, result.value());
return _server_component_impl.make_command_ack_message(command, result.value());
}
}
return {};
Expand Down Expand Up @@ -106,7 +114,7 @@ MavlinkRequestMessageHandler::handle_command_int(const MavlinkCommandReceiver::C
static_cast<float>(command.params.y)});

if (result.has_value()) {
return _system_impl.make_command_ack_message(command, result.value());
return _server_component_impl.make_command_ack_message(command, result.value());
}
}
return {};
Expand Down
13 changes: 10 additions & 3 deletions src/mavsdk/core/mavlink_request_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

namespace mavsdk {

class SystemImpl;
class MavsdkImpl;
class ServerComponentImpl;
class MavlinkCommandReceiver;

class MavlinkRequestMessageHandler {
public:
MavlinkRequestMessageHandler() = delete;
explicit MavlinkRequestMessageHandler(SystemImpl& system_impl);
explicit MavlinkRequestMessageHandler(
MavsdkImpl& mavsdk_impl,
ServerComponentImpl& server_component_impl,
MavlinkCommandReceiver& mavlink_command_receiver);
~MavlinkRequestMessageHandler();

using Params = std::array<float, 5>;
Expand All @@ -40,7 +45,9 @@ class MavlinkRequestMessageHandler {
std::mutex _table_mutex{};
std::vector<Entry> _table{};

SystemImpl& _system_impl;
MavsdkImpl& _mavsdk_impl;
ServerComponentImpl& _server_component_impl;
MavlinkCommandReceiver& _mavlink_command_receiver;
};

} // namespace mavsdk
5 changes: 3 additions & 2 deletions src/mavsdk/core/mavsdk_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ void MavsdkImpl::receive_message(mavlink_message_t& message, Connection* connect

if (_should_exit) {
// Don't try to call at() if systems have already been destroyed
// in descructor.
// in destructor.
return;
}

for (auto& system : _systems) {
if (system.first == message.sysid) {
system.second->system_impl()->process_mavlink_message(message);
// system.second->system_impl()->process_mavlink_message(message);
mavlink_message_handler.process_message(message);
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/mavsdk/core/mavsdk_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "mavsdk.h"
#include "mavlink_include.h"
#include "mavlink_address.h"
#include "mavlink_message_handler.h"
#include "mavlink_command_receiver.h"
#include "safe_queue.h"
#include "system.h"
#include "timeout_handler.h"
Expand Down Expand Up @@ -94,6 +96,9 @@ class MavsdkImpl {
void set_system_status(uint8_t system_status);
uint8_t get_system_status();

MavlinkMessageHandler mavlink_message_handler{};
Time time{};

private:
void add_connection(const std::shared_ptr<Connection>&);
void make_system_with_component(
Expand Down
2 changes: 1 addition & 1 deletion src/mavsdk/core/plugin_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PluginImplBase {
* when a system is constructed. This does not mean that the system actually
* exists and is connected, it might just be an empty dummy system.
*
* Plugins should do initialization steps with other parts of the Dronecode SDK
* Plugins should do initialization steps with other parts of MAVSDK
* at this state, e.g. set up callbacks with _parent (System).
*/
virtual void init() = 0;
Expand Down
Loading

0 comments on commit 13c7bf4

Please sign in to comment.