Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions c/src/cmavsdk/mavsdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,50 @@ void mavsdk_unsubscribe_outgoing_messages_json(
delete pair;
}

// --- Raw Bytes ---
void mavsdk_pass_received_raw_bytes(mavsdk_t mavsdk, const char* bytes, size_t length) {
auto* cpp_mavsdk = reinterpret_cast<Mavsdk*>(mavsdk);
cpp_mavsdk->pass_received_raw_bytes(bytes, length);
}

mavsdk_raw_bytes_handle_t mavsdk_subscribe_raw_bytes_to_be_sent(
mavsdk_t mavsdk,
mavsdk_raw_bytes_callback_t callback,
void* user_data
) {
auto* cpp_mavsdk = reinterpret_cast<Mavsdk*>(mavsdk);

struct CallbackContext {
mavsdk_raw_bytes_callback_t callback;
void* user_data;
};

auto* ctx = new CallbackContext{callback, user_data};

auto handle = cpp_mavsdk->subscribe_raw_bytes_to_be_sent(
[ctx](const char* bytes, size_t length) {
ctx->callback(bytes, length, ctx->user_data);
}
);

auto* result = new std::pair<Mavsdk::RawBytesHandle, CallbackContext*>(
std::move(handle), ctx
);
return reinterpret_cast<mavsdk_raw_bytes_handle_t>(result);
}

void mavsdk_unsubscribe_raw_bytes_to_be_sent(
mavsdk_t mavsdk,
mavsdk_raw_bytes_handle_t handle
) {
auto* cpp_mavsdk = reinterpret_cast<Mavsdk*>(mavsdk);
auto* pair = reinterpret_cast<std::pair<Mavsdk::RawBytesHandle, CallbackContext*>*>(handle);

cpp_mavsdk->unsubscribe_raw_bytes_to_be_sent(pair->first);
delete pair->second;
delete pair;
}

// --- Timeout ---
void mavsdk_set_timeout_s(mavsdk_t mavsdk, double timeout_s) {
auto* cpp_mavsdk = reinterpret_cast<Mavsdk*>(mavsdk);
Expand Down
18 changes: 18 additions & 0 deletions c/src/cmavsdk/mavsdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct mavsdk_connection_handle_s *mavsdk_connection_handle_t;
typedef struct mavsdk_connection_error_handle_s *mavsdk_connection_error_handle_t;
typedef struct mavsdk_new_system_handle_s *mavsdk_new_system_handle_t;
typedef struct mavsdk_intercept_json_handle_s *mavsdk_intercept_json_handle_t;
typedef struct mavsdk_raw_bytes_handle_s *mavsdk_raw_bytes_handle_t;

typedef enum {
MAVSDK_COMPONENT_TYPE_AUTOPILOT = 0,
Expand Down Expand Up @@ -73,6 +74,7 @@ typedef struct {
typedef void (*mavsdk_connection_error_callback_t)(const mavsdk_connection_error_t error, void *user_data);
typedef void (*mavsdk_new_system_callback_t)(void *user_data);
typedef int (*mavsdk_intercept_json_callback_t)(const mavsdk_message_t message, void *user_data);
typedef void (*mavsdk_raw_bytes_callback_t)(const char *bytes, size_t length, void *user_data);

// ===== Configuration =====
CMAVSDK_EXPORT mavsdk_configuration_t mavsdk_configuration_create_with_component_type(mavsdk_component_type_t type);
Expand Down Expand Up @@ -197,6 +199,22 @@ CMAVSDK_EXPORT void mavsdk_unsubscribe_outgoing_messages_json(
mavsdk_intercept_json_handle_t handle
);

// ===== Raw Bytes =====
CMAVSDK_EXPORT void mavsdk_pass_received_raw_bytes(
mavsdk_t mavsdk,
const char *bytes,
size_t length
);
CMAVSDK_EXPORT mavsdk_raw_bytes_handle_t mavsdk_subscribe_raw_bytes_to_be_sent(
mavsdk_t mavsdk,
mavsdk_raw_bytes_callback_t callback,
void *user_data
);
CMAVSDK_EXPORT void mavsdk_unsubscribe_raw_bytes_to_be_sent(
mavsdk_t mavsdk,
mavsdk_raw_bytes_handle_t handle
);

// ===== Memory Management Helpers =====
CMAVSDK_EXPORT void mavsdk_free_connection_error(
mavsdk_connection_error_t *error
Expand Down
48 changes: 48 additions & 0 deletions py/mavsdk/mavsdk/mavsdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .types import (
ConnectionResultWithHandle,
NewSystemCallback,
RawBytesCallback,
)


Expand Down Expand Up @@ -183,6 +184,32 @@ def server_component_by_id(self, component_id: int):
return ServerComponent(self._lib, handle)
return None

def pass_received_raw_bytes(self, data: bytes):
"""Pass received raw MAVLink bytes into MAVSDK for processing.
Requires a 'raw://' connection."""
self._lib.mavsdk_pass_received_raw_bytes(self._handle, data, len(data))

def subscribe_raw_bytes_to_be_sent(self, callback: Callable[[bytes], None]):
"""Subscribe to raw bytes to be sent out.
Requires a 'raw://' connection.
Returns a handle for unsubscribing."""

def _wrapper(bytes_ptr, length, user_data):
data = ctypes.string_at(bytes_ptr, length)
callback(data)

c_callback = RawBytesCallback(_wrapper)
handle = self._lib.mavsdk_subscribe_raw_bytes_to_be_sent(
self._handle, c_callback, None
)
self._callbacks[handle] = c_callback
return handle

def unsubscribe_raw_bytes_to_be_sent(self, handle):
"""Unsubscribe from raw bytes to be sent."""
self._lib.mavsdk_unsubscribe_raw_bytes_to_be_sent(self._handle, handle)
self._callbacks.pop(handle, None)

def destroy(self):
"""Destroy the Mavsdk instance"""
if not self._destroyed and self._handle:
Expand Down Expand Up @@ -299,3 +326,24 @@ def __del__(self):
ctypes.c_void_p,
]
_cmavsdk_lib.mavsdk_unsubscribe_on_new_system.restype = None

# Raw bytes functions
_cmavsdk_lib.mavsdk_pass_received_raw_bytes.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_size_t,
]
_cmavsdk_lib.mavsdk_pass_received_raw_bytes.restype = None

_cmavsdk_lib.mavsdk_subscribe_raw_bytes_to_be_sent.argtypes = [
ctypes.c_void_p,
RawBytesCallback,
ctypes.c_void_p,
]
_cmavsdk_lib.mavsdk_subscribe_raw_bytes_to_be_sent.restype = ctypes.c_void_p

_cmavsdk_lib.mavsdk_unsubscribe_raw_bytes_to_be_sent.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
]
_cmavsdk_lib.mavsdk_unsubscribe_raw_bytes_to_be_sent.restype = None
4 changes: 4 additions & 0 deletions py/mavsdk/mavsdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class ConnectionResultWithHandle(ctypes.Structure):

InterceptJsonCallback = ctypes.CFUNCTYPE(ctypes.c_int, MavsdkMessage, ctypes.c_void_p)

RawBytesCallback = ctypes.CFUNCTYPE(
None, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_void_p
)

ComponentDiscoveredCallback = ctypes.CFUNCTYPE(
None,
ctypes.c_int, # mavsdk_component_type_t
Expand Down
Loading