RDMnet  0.3.0
Implementation of ANSI E1.33 (RDMnet)
View other versions:
Data Ownership Paradigms in the RDMnet Library

RDMnet APIs are non-ownership-transferring; this means that when calling API functions that take pointers to data buffers as arguments, the data buffer memory is owned by the caller, and the caller is responsible for managing that memory both before and after the API function call.

For example, consider sending an RDM command with parameter data using the RDMnet controller API:

#include <stdlib.h>
#include <stdint.h>
uint8_t* device_label_data = (uint8_t*)malloc(32);
strcpy(device_label_data, "New Device Label");
uint32_t cmd_seq_num;
etcpal_error_t result = rdmnet_controller_send_set_command(my_controller_handle, my_scope_handle, &dest,
E120_DEVICE_LABEL, device_label_data,
strlen(device_label_data), &cmd_seq_num);
// The library uses and is finished with the device_label_data buffer by the time
// rdmnet_controller_send_set_command() returns. Now we are responsible for freeing the memory.
free(device_label_data);
// Of course, this would not be necessary if we used a static buffer or a buffer on the stack, e.g.:
uint8_t device_label_data[32];
Definitions for the RDMnet Controller API.
T free(T... args)
etcpal_error_t
#define RDMNET_ADDR_TO_DEFAULT_RESPONDER(manu_id, dev_id)
Initialize an RdmnetDestinationAddr with a default responder address.
Definition: client.h:106
etcpal_error_t rdmnet_controller_send_set_command(rdmnet_controller_t controller_handle, rdmnet_client_scope_t scope_handle, const RdmnetDestinationAddr *destination, uint16_t param_id, const uint8_t *data, uint8_t data_len, uint32_t *seq_num)
Send an RDM SET command from a controller on a scope.
Definition: controller.c:653
T malloc(T... args)
T strcpy(T... args)
T strlen(T... args)
A destination address for an RDM command in RDMnet's RPT protocol.
Definition: client.h:84

#include <string>
std::string device_label = "New Device Label";
auto dest_addr = rdmnet::DestinationAddr::ToDefaultResponder(0x6574, 0x12345678);
etcpal::Expected<uint32_t> result = controller.SendSetCommand(my_scope_handle, dest_addr, E120_DEVICE_LABEL,
reinterpret_cast<const uint8_t*>(device_label.c_str()),
device_label.size());
// The library uses and is finished with the device_label string by the time SendSetCommand()
// returns. The memory associated with the string will then be freed when the string goes out of
// scope.
T c_str(T... args)
static constexpr DestinationAddr ToDefaultResponder(const rdm::Uid &rdmnet_uid, uint16_t subdevice=0)
Get a DestinationAddr representing a message addressed to a component's default responder.
Definition: client.h:94
C++ wrapper for the RDMnet Controller API.
T size(T... args)

Callbacks

The same non-ownership-transferring principle applies to callbacks delivered from the RDMnet library. The library retains ownership of any data buffers supplied to a callback; if a library user wants to access that data after the callback returns, it must be saved.

The API provides convenient saving functions to make copies of data buffers present in commands. Of course, data can also be saved manually from the data members.

void controller_rdm_response_callback(rdmnet_controller_t controller_handle, rdmnet_client_scope_t scope_handle,
const RdmnetRdmResponse* resp, void* context)
{
// resp->rdm_data and resp->original_cmd_data are pointers to data buffers owned by the library.
// These buffers will be invalid once this callback finishes.
// You can copy out data manually:
memcpy(my_data_buf, resp->rdm_data, resp->rdm_data_len);
// Or using the save function to create a version with owned data:
rdmnet_save_rdm_response(resp, &saved_resp);
// If you use this method, you must then free the saved_resp data when you're done with it:
}
etcpal_error_t rdmnet_free_saved_rdm_response(RdmnetSavedRdmResponse *saved_response)
Free the memory owned by a saved RDM response.
Definition: message.c:265
int rdmnet_client_scope_t
A handle to a scope that an RDMnet client participates in.
Definition: client.h:41
etcpal_error_t rdmnet_save_rdm_response(const RdmnetRdmResponse *response, RdmnetSavedRdmResponse *saved_response)
Save the data in a received RDM response for later use from a different context.
Definition: message.c:102
int rdmnet_controller_t
A handle to an RDMnet controller.
Definition: controller.h:54
T memcpy(T... args)
An RDMnet RDM response received by a local component.
Definition: message.h:90
size_t rdm_data_len
The length of the parameter data associated with the RDM response.
Definition: message.h:118
const uint8_t * rdm_data
Any parameter data associated with the RDM response.
Definition: message.h:116
An RDM response received over RDMnet and saved for later processing.
Definition: message.h:138

void MyControllerNotifyHandler::HandleRdmResponse(rdmnet::Controller::Handle controller_handle,
rdmnet::ScopeHandle scope_handle,
const rdmnet::RdmResponse& resp)
{
// resp.data() and resp.original_cmd_data() return pointers to data buffers owned by the library.
// These buffers will be invalid once this callback finishes.
// You can copy out the data manually:
std::memcpy(my_data_buf, resp.data(), resp.data_len());
// Or using the data copy accessor:
std::vector<uint8_t> saved_data = resp.GetData();
// Or using the Save() function to create a version with owned data:
rdmnet::SavedRdmResponse saved_resp = resp.Save();
// The SavedRdmResponse class has allocating containers to hold the data - it will be cleaned up
// when it goes out of scope.
}
rdmnet_controller_t Handle
A handle type used by the RDMnet library to identify controller instances.
Definition: controller.h:62
An RDM response received over RDMnet and delivered to an RDMnet callback function.
Definition: rdm_response.h:47
constexpr const uint8_t * data() const noexcept
Get a pointer to the RDM parameter data buffer contained within this response.
Definition: rdm_response.h:279
SavedRdmResponse Save() const
Save the data in this response for later use from a different context.
Definition: rdm_response.h:411
std::vector< uint8_t > GetData() const
Copy out the data in a RdmResponse.
Definition: rdm_response.h:374
constexpr size_t data_len() const noexcept
Get the length of the RDM parameter data contained within this response.
Definition: rdm_response.h:285
An RDM response received over RDMnet and saved for later processing.
Definition: rdm_response.h:111
rdmnet_client_scope_t ScopeHandle
A handle to a scope that an RDMnet client participates in.
Definition: client.h:447