added icsneo_can_message_free

David Rebbe 2024-12-09 11:22:46 -05:00
parent b75305d1d3
commit 14cf054635
2 changed files with 34 additions and 5 deletions

View File

@ -9,7 +9,7 @@
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
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<std::shared_ptr<icsneo_message_t>> 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<std::shared_ptr<icsneo_message_t>> tx_messages;
// This needs to be a list so that pointers aren't invalidated on push_back or erase.
std::list<std::shared_ptr<icsneo_message_t>> tx_messages;
std::vector<icsneo_event_t> 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<icsneo::BusMessage>(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;

View File

@ -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.