From 14cf054635dbe52d64d2a18ed06003c5e884cc9c Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Mon, 9 Dec 2024 11:22:46 -0500 Subject: [PATCH] added icsneo_can_message_free --- api/icsneo/icsneo.cpp | 26 +++++++++++++++++++++----- include/icsneo/icsneo.h | 13 +++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/api/icsneo/icsneo.cpp b/api/icsneo/icsneo.cpp index 4a806e5..1cab7ab 100644 --- a/api/icsneo/icsneo.cpp +++ b/api/icsneo/icsneo.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include using namespace icsneo; @@ -19,8 +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. - // This needs to be a deque so that pointers aren't invalidated on push_back. - std::deque> tx_messages; + // This needs to be a list so that pointers aren't invalidated on push_back or erase. + std::list> tx_messages; std::vector events; icsneo_open_options_t options; @@ -34,7 +34,6 @@ typedef struct icsneo_device_t { events.clear(); events.shrink_to_fit(); tx_messages.clear(); - tx_messages.shrink_to_fit(); } } icsneo_device_t; @@ -438,7 +437,7 @@ ICSNEO_API icsneo_error_t icsneo_device_transmit_messages(icsneo_device_t* devic auto dev = device->device; uint32_t i = 0; bool success = false; - for (;i < *messages_count; i++) { + for (; i < *messages_count; i++) { // TODO: Check if message is valid success = dev->transmit(std::static_pointer_cast(messages[i]->message)); if (!success) { @@ -813,6 +812,23 @@ ICSNEO_API icsneo_error_t icsneo_can_messages_create(icsneo_device_t* device, ic return icsneo_error_success; } +ICSNEO_API icsneo_error_t icsneo_can_message_free(icsneo_device_t* device, icsneo_message_t* message) { + if (!device || !message) { + return icsneo_error_invalid_parameters; + } + // TODO: Check if device is valid + bool removed = false; + for (auto it = device->tx_messages.begin(); it != device->tx_messages.end(); it++) { + if (it->get() == message) { + device->tx_messages.erase(it); + removed = true; + message = nullptr; + break; + } + } + return removed ? icsneo_error_success : icsneo_error_invalid_parameters; +} + 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 47391cb..22ec9ec 100644 --- a/include/icsneo/icsneo.h +++ b/include/icsneo/icsneo.h @@ -560,6 +560,19 @@ ICSNEO_API icsneo_error_t icsneo_can_message_get_error_state_indicator(icsneo_de */ ICSNEO_API icsneo_error_t icsneo_can_messages_create(icsneo_device_t* device, icsneo_message_t** messages, uint32_t messages_count); +/** @brief Free CAN messages for a device + * + * @param[in] icsneo_device_t device The device to free the messages of. + * @param[in] icsneo_message_t* message The message to free. + * + * @return icsneo_error_t icsneo_error_success if successful, icsneo_error_invalid_parameters otherwise. + * + * @warning This function should only be called on messages created by icsneo_can_messages_create. + * + * @see icsneo_can_messages_create + */ +ICSNEO_API icsneo_error_t icsneo_can_message_free(icsneo_device_t* device, icsneo_message_t* message); + /** @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.