-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,694 additions
and
9 deletions.
There are no files selected for viewing
Submodule proto
updated
1 files
+41 −0 | protos/component_information_server/component_information_server.proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "mavlink_request_message_handler.h" | ||
#include "system_impl.h" | ||
|
||
namespace mavsdk { | ||
|
||
MavlinkRequestMessageHandler::MavlinkRequestMessageHandler(SystemImpl& system_impl) : | ||
_system_impl(system_impl) | ||
{ | ||
_system_impl.register_mavlink_command_handler( | ||
MAV_CMD_REQUEST_MESSAGE, | ||
[this](const MavlinkCommandReceiver::CommandLong& command) { | ||
return handle_command_long(command); | ||
}, | ||
this); | ||
|
||
_system_impl.register_mavlink_command_handler( | ||
MAV_CMD_REQUEST_MESSAGE, | ||
[this](const MavlinkCommandReceiver::CommandLong& command) { | ||
return handle_command_long(command); | ||
}, | ||
this); | ||
} | ||
|
||
MavlinkRequestMessageHandler::~MavlinkRequestMessageHandler() | ||
{ | ||
_system_impl.unregister_all_mavlink_message_handlers(this); | ||
} | ||
|
||
bool MavlinkRequestMessageHandler::register_handler( | ||
uint32_t message_id, const Callback& callback, const void* cookie) | ||
{ | ||
std::lock_guard<std::mutex> lock(_table_mutex); | ||
|
||
if (std::find_if(_table.begin(), _table.end(), [message_id](const Entry& entry) { | ||
return entry.message_id == message_id; | ||
}) != _table.end()) { | ||
LogErr() << "message id " << message_id << " already registered, registration ignored"; | ||
return false; | ||
} | ||
|
||
_table.emplace_back(Entry{message_id, callback, cookie}); | ||
return true; | ||
} | ||
|
||
void MavlinkRequestMessageHandler::unregister_handler(uint32_t message_id, const void* cookie) | ||
{ | ||
std::lock_guard<std::mutex> lock(_table_mutex); | ||
|
||
_table.erase( | ||
std::remove_if( | ||
_table.begin(), | ||
_table.end(), | ||
[&message_id, &cookie](const Entry& entry) { | ||
return entry.message_id == message_id && entry.cookie == cookie; | ||
}), | ||
_table.end()); | ||
} | ||
|
||
void MavlinkRequestMessageHandler::unregister_all_handlers(const void* cookie) | ||
{ | ||
std::lock_guard<std::mutex> lock(_table_mutex); | ||
|
||
_table.erase( | ||
std::remove_if( | ||
_table.begin(), | ||
_table.end(), | ||
[&cookie](const Entry& entry) { return entry.cookie == cookie; }), | ||
_table.end()); | ||
} | ||
|
||
std::optional<mavlink_message_t> MavlinkRequestMessageHandler::handle_command_long( | ||
const MavlinkCommandReceiver::CommandLong& command) | ||
{ | ||
std::lock_guard<std::mutex> lock(_table_mutex); | ||
|
||
for (auto& entry : _table) { | ||
if (entry.message_id == static_cast<uint32_t>(std::round(command.params.param1))) { | ||
if (entry.callback != nullptr) { | ||
const auto result = entry.callback( | ||
{command.params.param2, | ||
command.params.param3, | ||
command.params.param4, | ||
command.params.param5, | ||
command.params.param6}); | ||
|
||
if (result.has_value()) { | ||
return _system_impl.make_command_ack_message(command, result.value()); | ||
} | ||
} | ||
return {}; | ||
} | ||
} | ||
|
||
// We could respond with MAV_RESULT_UNSUPPORTED here, however, it's not clear if maybe someone | ||
// else might be answering the command. | ||
|
||
return {}; | ||
} | ||
|
||
std::optional<mavlink_message_t> | ||
MavlinkRequestMessageHandler::handle_command_int(const MavlinkCommandReceiver::CommandInt& command) | ||
{ | ||
std::lock_guard<std::mutex> lock(_table_mutex); | ||
|
||
for (auto& entry : _table) { | ||
if (entry.message_id == static_cast<uint32_t>(std::round(command.params.param1))) { | ||
if (entry.callback != nullptr) { | ||
const auto result = entry.callback( | ||
{command.params.param2, | ||
command.params.param3, | ||
command.params.param4, | ||
static_cast<float>(command.params.x), | ||
static_cast<float>(command.params.y)}); | ||
|
||
if (result.has_value()) { | ||
return _system_impl.make_command_ack_message(command, result.value()); | ||
} | ||
} | ||
return {}; | ||
} | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <functional> | ||
#include <mutex> | ||
#include <optional> | ||
#include <vector> | ||
#include "mavlink_include.h" | ||
#include "mavlink_command_receiver.h" | ||
|
||
namespace mavsdk { | ||
|
||
class SystemImpl; | ||
|
||
class MavlinkRequestMessageHandler { | ||
public: | ||
MavlinkRequestMessageHandler() = delete; | ||
explicit MavlinkRequestMessageHandler(SystemImpl& system_impl); | ||
~MavlinkRequestMessageHandler(); | ||
|
||
using Params = std::array<float, 5>; | ||
using Callback = std::function<std::optional<MAV_RESULT>(Params)>; | ||
|
||
bool register_handler(uint32_t message_id, const Callback& callback, const void* cookie); | ||
void unregister_handler(uint32_t message_id, const void* cookie); | ||
void unregister_all_handlers(const void* cookie); | ||
|
||
private: | ||
std::optional<mavlink_message_t> | ||
handle_command_long(const MavlinkCommandReceiver::CommandLong& command); | ||
std::optional<mavlink_message_t> | ||
handle_command_int(const MavlinkCommandReceiver::CommandInt& command); | ||
|
||
struct Entry { | ||
uint32_t message_id; | ||
Callback callback; | ||
const void* cookie; | ||
}; | ||
|
||
std::mutex _table_mutex{}; | ||
std::vector<Entry> _table{}; | ||
|
||
SystemImpl& _system_impl; | ||
}; | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/mavsdk/plugins/component_information_server/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
target_sources(mavsdk | ||
PRIVATE | ||
component_information_server.cpp | ||
component_information_server_impl.cpp | ||
) | ||
|
||
target_include_directories(mavsdk PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include/mavsdk> | ||
) | ||
|
||
install(FILES | ||
include/plugins/component_information_server/component_information_server.h | ||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mavsdk/plugins/component_information_server | ||
) |
53 changes: 53 additions & 0 deletions
53
src/mavsdk/plugins/component_information_server/component_information_server.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// WARNING: THIS FILE IS AUTOGENERATED! As such, it should not be edited. | ||
// Edits need to be made to the proto files | ||
// (see | ||
// https://github.com/mavlink/MAVSDK-Proto/blob/master/protos/component_information_server/component_information_server.proto) | ||
|
||
#include <iomanip> | ||
|
||
#include "component_information_server_impl.h" | ||
#include "plugins/component_information_server/component_information_server.h" | ||
|
||
namespace mavsdk { | ||
|
||
ComponentInformationServer::ComponentInformationServer(System& system) : | ||
PluginBase(), | ||
_impl{std::make_unique<ComponentInformationServerImpl>(system)} | ||
{} | ||
|
||
ComponentInformationServer::ComponentInformationServer(std::shared_ptr<System> system) : | ||
PluginBase(), | ||
_impl{std::make_unique<ComponentInformationServerImpl>(system)} | ||
{} | ||
|
||
ComponentInformationServer::~ComponentInformationServer() {} | ||
|
||
ComponentInformationServer::Result | ||
ComponentInformationServer::provide_peripheral_file(std::string path) const | ||
{ | ||
return _impl->provide_peripheral_file(path); | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& str, ComponentInformationServer::Result const& result) | ||
{ | ||
switch (result) { | ||
case ComponentInformationServer::Result::Unknown: | ||
return str << "Unknown"; | ||
case ComponentInformationServer::Result::Success: | ||
return str << "Success"; | ||
case ComponentInformationServer::Result::NotFound: | ||
return str << "Not Found"; | ||
case ComponentInformationServer::Result::OpenFailure: | ||
return str << "Open Failure"; | ||
case ComponentInformationServer::Result::ReadFailure: | ||
return str << "Read Failure"; | ||
case ComponentInformationServer::Result::FailedJsonParsing: | ||
return str << "Failed Json Parsing"; | ||
case ComponentInformationServer::Result::FailedJsonSchema: | ||
return str << "Failed Json Schema"; | ||
default: | ||
return str << "Unknown"; | ||
} | ||
} | ||
|
||
} // namespace mavsdk |
Oops, something went wrong.