mavsdk::MavlinkDirectServer Class Reference ​
#include: mavlink_direct_server.hpp
Enable direct MAVLink communication using libmav from a server component.
Unlike MavlinkDirect, this does not require a discovered system: it sends from the local server component, which makes it suitable for broadcasting messages (e.g. as an autopilot or custom component) without a connected counterpart.
Data Structures ​
struct MavlinkMessage
Public Types ​
| Type | Description |
|---|---|
| enum Result | Possible results returned for action requests. |
| std::function< void(Result)> ResultCallback | Callback type for asynchronous MavlinkDirectServer calls. |
| std::function< void(MavlinkMessage)> MessageCallback | Callback type for subscribe_message. |
| Handle< MavlinkMessage > MessageHandle | Handle type for subscribe_message. |
| std::function< void(MavlinkMessage)> CommandCallback | Callback type for subscribe_command. |
| Handle< MavlinkMessage > CommandHandle | Handle type for subscribe_command. |
Public Member Functions ​
| Type | Name | Description |
|---|---|---|
| Â | MavlinkDirectServer (std::shared_ptr< ServerComponent > server_component) | Constructor. Creates the plugin for a ServerComponent instance. |
| Â | ~MavlinkDirectServer () override | Destructor (internal use only). |
| Â | MavlinkDirectServer (const MavlinkDirectServer & other) | Copy constructor. |
| Result | send_message (MavlinkMessage message)const | Send a MAVLink message directly. |
| MessageHandle | subscribe_message (std::string message_name, const MessageCallback & callback) | Subscribe to incoming MAVLink messages. |
| void | unsubscribe_message (MessageHandle handle) | Unsubscribe from subscribe_message. |
| Result | load_custom_xml (std::string xml_content)const | Load custom MAVLink message definitions from XML. |
| CommandHandle | subscribe_command (uint32_t command, const CommandCallback & callback) | Subscribe to incoming commands with a given command ID. |
| void | unsubscribe_command (CommandHandle handle) | Unsubscribe from subscribe_command. |
| const MavlinkDirectServer & | operator= (const MavlinkDirectServer &)=delete | Equality operator (object is not copyable). |
Constructor & Destructor Documentation ​
MavlinkDirectServer() ​
mavsdk::MavlinkDirectServer::MavlinkDirectServer(std::shared_ptr< ServerComponent > server_component)Constructor. Creates the plugin for a ServerComponent instance.
The plugin is typically created as shown below:
auto mavlink_direct_server = MavlinkDirectServer(server_component);Parameters
- std::shared_ptr< ServerComponent > server_component - The ServerComponent instance associated with this server plugin.
~MavlinkDirectServer() ​
mavsdk::MavlinkDirectServer::~MavlinkDirectServer() overrideDestructor (internal use only).
MavlinkDirectServer() ​
mavsdk::MavlinkDirectServer::MavlinkDirectServer(const MavlinkDirectServer &other)Copy constructor.
Parameters
- const MavlinkDirectServer& other -
Member Typdef Documentation ​
typedef ResultCallback ​
using mavsdk::MavlinkDirectServer::ResultCallback = std::function<void(Result)>Callback type for asynchronous MavlinkDirectServer calls.
typedef MessageCallback ​
using mavsdk::MavlinkDirectServer::MessageCallback = std::function<void(MavlinkMessage)>Callback type for subscribe_message.
typedef MessageHandle ​
using mavsdk::MavlinkDirectServer::MessageHandle = Handle<MavlinkMessage>Handle type for subscribe_message.
typedef CommandCallback ​
using mavsdk::MavlinkDirectServer::CommandCallback = std::function<void(MavlinkMessage)>Callback type for subscribe_command.
typedef CommandHandle ​
using mavsdk::MavlinkDirectServer::CommandHandle = Handle<MavlinkMessage>Handle type for subscribe_command.
Member Enumeration Documentation ​
enum Result ​
Possible results returned for action requests.
| Value | Description |
|---|---|
Unknown | Unknown result. |
Success | Request succeeded. |
Error | Error. |
InvalidMessage | Invalid MAVLink message. |
InvalidField | Invalid field name or value. |
ConnectionError | Connection error. |
NoSystem | No system connected. |
Timeout | Request timed out. |
Member Function Documentation ​
send_message() ​
Result mavsdk::MavlinkDirectServer::send_message(MavlinkMessage message) constSend a MAVLink message directly.
This allows sending any MAVLink message with full control over the message content. Leave target_system_id and target_component_id at 0 to broadcast.
This function is blocking.
Parameters
- MavlinkMessage message -
Returns
 Result - Result of request.
subscribe_message() ​
MessageHandle mavsdk::MavlinkDirectServer::subscribe_message(std::string message_name, const MessageCallback &callback)Subscribe to incoming MAVLink messages.
This provides direct access to incoming MAVLink messages. Use an empty string in message_name to subscribe to all messages, or specify a message name (e.g., "HEARTBEAT") to filter for specific message types. Unlike the MavlinkDirect (client) plugin, this is not scoped to a single system: it receives matching messages from all systems.
Parameters
- std::string message_name -
- const MessageCallback& callback -
Returns
 MessageHandle -
unsubscribe_message() ​
void mavsdk::MavlinkDirectServer::unsubscribe_message(MessageHandle handle)Unsubscribe from subscribe_message.
Parameters
- MessageHandle handle -
load_custom_xml() ​
Result mavsdk::MavlinkDirectServer::load_custom_xml(std::string xml_content) constLoad custom MAVLink message definitions from XML.
This allows loading custom MAVLink message definitions at runtime, extending the available message types beyond the built-in definitions.
This function is blocking.
Parameters
- std::string xml_content -
Returns
 Result - Result of request.
subscribe_command() ​
CommandHandle mavsdk::MavlinkDirectServer::subscribe_command(uint32_t command, const CommandCallback &callback)Subscribe to incoming commands with a given command ID.
Delivers COMMAND_LONG and COMMAND_INT messages with this command ID that are addressed to this server component; check message_name to see which one it is.
While subscribed, MAVSDK does not ack the command itself, so every command received must be answered by sending a COMMAND_ACK with SendMessage, addressed to the sender (system_id/component_id of the received command) and with "command" set to the command ID. For a command that takes a while, send acks with MAV_RESULT_IN_PROGRESS and "progress" in percent (255 if unknown) about once per second while it runs, so the sender knows it is still going, then the final result.
The sender may retransmit the command if the ack is lost, so the same command can arrive more than once.
Parameters
- uint32_t command -
- const CommandCallback& callback -
Returns
 CommandHandle -
unsubscribe_command() ​
void mavsdk::MavlinkDirectServer::unsubscribe_command(CommandHandle handle)Unsubscribe from subscribe_command.
Parameters
- CommandHandle handle -
operator=() ​
const MavlinkDirectServer & mavsdk::MavlinkDirectServer::operator=(const MavlinkDirectServer &)=deleteEquality operator (object is not copyable).
Parameters
- const MavlinkDirectServer& -
Returns
 const MavlinkDirectServer & -

