From b0575fe6581adfb4ef67ccd549fe69ab60745ae6 Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Sun, 8 Dec 2024 07:42:16 -0500 Subject: [PATCH] Added icsneo_can_messages_create, untested. --- api/icsneo/icsneo.cpp | 21 ++++++++++++++++++++- include/icsneo/icsneo.h | 11 +++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/api/icsneo/icsneo.cpp b/api/icsneo/icsneo.cpp index 5431c01..65ff836 100644 --- a/api/icsneo/icsneo.cpp +++ b/api/icsneo/icsneo.cpp @@ -9,6 +9,7 @@ #include #include +#include #include using namespace icsneo; @@ -18,7 +19,8 @@ typedef struct icsneo_device_t { // Received messages from the device, we can automatically free them without the user. std::vector> messages; // Seperate buffer for transmit messages for simplicity. User is responsible for freeing. - std::vector> tx_messages; + // This needs to be a deque so that pointers aren't invalidated on push_back. + std::deque> tx_messages; std::vector events; icsneo_open_options_t options; @@ -604,6 +606,23 @@ ICSNEO_API icsneo_error_t icsneo_can_message_get_error_state_indicator(icsneo_de return icsneo_error_success; } +ICSNEO_API icsneo_error_t icsneo_can_messages_create(icsneo_device_t* device, icsneo_message_t** messages, uint32_t messages_count) { + if (!device || !messages) { + return icsneo_error_invalid_parameters; + } + // TODO: Check if device is valid + auto dev = device->device; + // Get the device messages + for (uint32_t i = 0; i < messages_count; i++) { + auto can_message = std::static_pointer_cast(std::make_shared()); + auto message = std::make_shared(can_message, true); + device->tx_messages.push_back(message); + messages[i] = message.get(); + } + + return icsneo_error_success; +} + ICSNEO_API icsneo_error_t icsneo_get_events(icsneo_event_t** events, uint32_t* events_count) { if (!events || !events_count) { return icsneo_error_invalid_parameters; diff --git a/include/icsneo/icsneo.h b/include/icsneo/icsneo.h index 0ff4bd7..fbdddfa 100644 --- a/include/icsneo/icsneo.h +++ b/include/icsneo/icsneo.h @@ -434,6 +434,17 @@ ICSNEO_API icsneo_error_t icsneo_can_message_get_baudrate_switch(icsneo_device_t */ ICSNEO_API icsneo_error_t icsneo_can_message_get_error_state_indicator(icsneo_device_t* device, icsneo_message_t* message, bool* value); +/** @brief Create CAN messages for a device + * + * @param[in] icsneo_device_t device The device to get the messages of. + * @param[out] icsneo_message_t** messages Pointer to an array of icsneo_message_t to copy the messages into. + * Undefined behaviour if index is out of range of messages_count. + * @param[in] uint32_t* messages_count Size of the messages array. + * + * @return icsneo_error_t icsneo_error_success if successful, icsneo_error_invalid_parameters otherwise. + */ +ICSNEO_API icsneo_error_t icsneo_can_messages_create(icsneo_device_t* device, icsneo_message_t** messages, uint32_t messages_count); + /** @brief Get the global events not specifically related to a device. * * @param[out] icsneo_event_t** events Pointer to an array of icsneo_event_t to copy the events into.