added icsneo_get_events, icsneo_device_get_events, and icsneo_event_get_description

David Rebbe 2024-12-04 12:32:42 -05:00
parent 360610c0ac
commit b88f8dbb05
3 changed files with 219 additions and 12 deletions

View File

@ -1,7 +1,7 @@
#include <icsneo/icsneo.h>
#include <icsneo/device/device.h>
#include "icsneo/device/devicefinder.h"
#include "icsneo/icsneocpp.h"
#include "icsneo/communication/message/message.h"
#include "icsneo/communication/message/canmessage.h"
#include "icsneo/communication/message/linmessage.h"
@ -16,6 +16,7 @@ using namespace icsneo;
typedef struct icsneo_device_t {
std::shared_ptr<Device> device;
std::vector<std::shared_ptr<icsneo_message_t>> messages;
std::vector<icsneo_event_t> events;
icsneo_open_options_t options;
} icsneo_device_t;
@ -24,8 +25,13 @@ typedef struct icsneo_message_t {
std::shared_ptr<Message> message;
} icsneo_message_t;
typedef struct icsneo_event_t {
APIEvent event;
} icsneo_event_t;
static std::vector<std::shared_ptr<icsneo_device_t>> g_devices;
static std::vector<icsneo_event_t> g_events;
ICSNEO_API icsneo_error_t icsneo_error_code(icsneo_error_t error_code, const char* value, uint32_t* value_length) {
if (!value || !value_length) {
@ -350,7 +356,7 @@ ICSNEO_API icsneo_error_t icsneo_get_messages(icsneo_device_t* device, icsneo_me
uint32_t min_size = std::minmax(static_cast<uint32_t>(queried_messages.size()), *messages_count).first;
*messages_count = min_size;
// Copy the messages into our global message container
// Copy the messages into our device message container
device->messages.clear();
for (auto& message : queried_messages) {
auto message_t = std::make_shared<icsneo_message_t>();
@ -406,3 +412,103 @@ ICSNEO_API icsneo_error_t icsneo_message_get_bus_type(icsneo_device_t* device, i
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;
}
// Clear the device events
g_events.clear();
// Get the global events
auto global_events = EventManager::GetInstance().get();
// Get the mininum number of events
auto min_size = std::minmax(static_cast<uint32_t>(global_events.size()), *events_count).first;
*events_count = min_size;
// GetEvents uses 0 as unlimited, where the API can't allocate anything.
if (min_size == 0) {
return icsneo_error_success;
}
// Copy the events into the global event container
for (uint32_t i = 0; i < min_size; i++) {
auto e = icsneo_event_t {
.event = global_events[i],
};
g_events.push_back(e);
}
// Copy the device events references into the events array
for (uint32_t i = 0; i < min_size; i++) {
events[i] = &g_events[i];
}
return icsneo_error_success;
}
ICSNEO_API icsneo_error_t icsneo_device_get_events(icsneo_device_t* device, icsneo_event_t** events, uint32_t* events_count) {
if (!device || !events || !events_count) {
return icsneo_error_invalid_parameters;
}
// TODO: Check if device is valid
// Setup the event filter
EventFilter filter(device->device.get());
// Clear the device events
device->events.clear();
// Get the mininum number of events
auto min_size = std::minmax(static_cast<uint32_t>(icsneo::EventCount(filter)), *events_count).first;
*events_count = min_size;
// GetEvents uses 0 as unlimited, where the API can't allocate anything.
if (min_size == 0) {
return icsneo_error_success;
}
// Copy the events into the device event container
auto device_events = icsneo::GetEvents(filter, min_size);
for (uint32_t i = 0; i < min_size; i++) {
auto e = icsneo_event_t {
.event = device_events[i],
};
device->events.push_back(e);
}
// Copy the device events references into the events array
for (uint32_t i = 0; i < min_size; i++) {
events[i] = &device->events[i];
}
return icsneo_error_success;
}
ICSNEO_API icsneo_error_t icsneo_event_get_description(icsneo_event_t* event, const char* value, uint32_t* value_length) {
if (!event || !value || !value_length) {
return icsneo_error_invalid_parameters;
}
// TODO: Check if event is valid
auto ev = event->event;
// Get and set the length
auto min_length = std::minmax(static_cast<uint32_t>(ev.describe().length()), *value_length).first;
*value_length = min_length;
// Copy the string into value
strncpy(const_cast<char *>(value), ev.describe().c_str(), min_length);
return icsneo_error_success;
}
/*
Type getType() const noexcept { return Type(eventStruct.eventNumber); }
Severity getSeverity() const noexcept { return Severity(eventStruct.severity); }
std::string getDescription() const noexcept { return std::string(eventStruct.description); }
const Device* getDevice() const noexcept { return device; } // Will return nullptr if this is an API-wide event
EventTimePoint getTimestamp() const noexcept { return timepoint; }
void downgradeFromError() noexcept;
bool isForDevice(const Device* forDevice) const noexcept { return forDevice == device; }
bool isForDevice(std::string serial) const noexcept;
// As opposed to getDescription, this will also add text such as "neoVI FIRE 2 CY2468 Error: " to fully describe the problem
std::string describe() const noexcept;
friend std::ostream& operator<<(std::ostream& os, const APIEvent& event) {
os << event.describe();
return os;
}
static const char* DescriptionForType(Type type);
*/

View File

@ -42,7 +42,7 @@ int print_error_code(const char* message, icsneo_error_t error) {
printf("%s: Failed to get string for error code %d with error code %d\n", message, error, res);
return res;
}
printf("%s: %s\n", message, error_str);
printf("%s: \"%s\" (%u)\n", message, error_str, error);
return (int)error;
}
@ -61,6 +61,8 @@ int print_error_code(const char* message, icsneo_error_t error) {
*/
int process_messages(icsneo_device_t* device, icsneo_message_t** messages, uint32_t messages_count);
void print_device_events(icsneo_device_t* device, const char* device_description);
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
@ -74,42 +76,46 @@ int main(int argc, char* argv[]) {
};
printf("Found %u devices\n", devices_count);
// Loop over each device
for (uint32_t i = 0; i < devices_count; i++) {
icsneo_device_t* device = devices[i];
// Get description of the device
const char description[255] = {0};
uint32_t description_length = 255;
res = icsneo_device_describe(device, description, &description_length);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to get device description", res);
};
// Get timestamp resolution of the device
uint32_t timestamp_resolution = 0;
res = icsneo_get_timestamp_resolution(device, &timestamp_resolution);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to get timestamp resolution", res);
}
printf("%s timestamp resolution: %uns\n", description, timestamp_resolution);
// Get/Set open options
icsneo_open_options_t options = icsneo_open_options_none;
res = icsneo_get_open_options(device, &options);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to get open options", res);
}
// Disable Syncing RTC
options &= ~icsneo_open_options_sync_rtc;
//options &= ~icsneo_open_options_sync_rtc;
res = icsneo_set_open_options(device, options);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to set open options", res);
}
// Open the device
printf("Opening device: %s...\n", description);
res = icsneo_open(device);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to open device", res);
};
// Wait for the bus to collect some messages, requires an active bus to get messages
printf("Waiting 1 second for messages...\n");
sleep_ms(1000);
@ -119,23 +125,72 @@ int main(int argc, char* argv[]) {
printf("Getting messages from device with timeout of 3000ms on %s...\n", description);
res = icsneo_get_messages(device, messages, &message_count, 3000);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to get messages from device", res);
};
// Process the messages
res = process_messages(device, messages, message_count);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to process messages", res);
}
// Finally, close the device.
printf("Closing device: %s...\n", description);
res = icsneo_close(device);
if (res != icsneo_error_success) {
print_device_events(device, description);
return print_error_code("Failed to close device", res);
};
// Print device events
print_device_events(device, description);
}
return 0;
}
void print_device_events(icsneo_device_t* device, const char* device_description) {
// Get device events
icsneo_event_t* events[1024] = {0};
uint32_t events_count = 1024;
icsneo_error_t res = icsneo_device_get_events(device, events, &events_count);
if (res != icsneo_error_success) {
(void)print_error_code("Failed to get device events", res);
return;
}
// Loop over each event and describe it.
for (uint32_t i = 0; i < events_count; i++) {
const char event_description[255] = {0};
uint32_t event_description_length = 255;
res = icsneo_event_get_description(events[i], event_description, &event_description_length);
if (res != icsneo_error_success) {
print_error_code("Failed to get event description", res);
continue;
}
printf("\t%s: Event %u: %s\n", device_description, i, event_description);
}
// Get global events
icsneo_event_t* global_events[1024] = {0};
uint32_t global_events_count = 1024;
res = icsneo_get_events(global_events, &global_events_count);
if (res != icsneo_error_success) {
(void)print_error_code("Failed to get global events", res);
return;
}
// Loop over each event and describe it.
for (uint32_t i = 0; i < global_events_count; i++) {
const char event_description[255] = {0};
uint32_t event_description_length = 255;
res = icsneo_event_get_description(global_events[i], event_description, &event_description_length);
if (res != icsneo_error_success) {
print_error_code("Failed to get global event description", res);
continue;
}
printf("\t%s: Global Event %u: %s\n", device_description, i, event_description);
}
printf("%s: Received %u events and %u global events\n", device_description, events_count, global_events_count);
}
int process_messages(icsneo_device_t* device, icsneo_message_t** messages, uint32_t messages_count) {
printf("Received %u messages\n", messages_count);
// Print the type and bus type of each message
@ -155,4 +210,4 @@ int process_messages(icsneo_device_t* device, icsneo_message_t** messages, uint3
}
return icsneo_error_success;
}
}

View File

@ -43,9 +43,24 @@ extern "C" {
typedef struct icsneo_device_t icsneo_device_t;
/** @brief icsneo_message_t opaque struct definition
*
* This object represents a single event from the device.
*
* @see icsneo_device_get_events
*/
typedef struct icsneo_message_t icsneo_message_t;
/** @brief icsneo_event_t opaque struct definition
*
* This object represents a single event from the device.
*
* @see icsneo_device_get_events
*/
typedef struct icsneo_event_t icsneo_event_t;
/** @brief Error codes for icsneo functions.
*
* This enum is guaranteed to be ABI stable, any new values will be appended to the end.
@ -312,6 +327,37 @@ ICSNEO_API icsneo_error_t icsneo_message_get_type(icsneo_device_t* device, icsne
*/
ICSNEO_API icsneo_error_t icsneo_message_get_bus_type(icsneo_device_t* device, icsneo_message_t* message, icsneo_msg_bus_type_t* bus_type);
/** @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.
* Undefined behaviour if index is out of range of events_count.
* @param[in,out] uint32_t* events_count Size of the events array. Modified with the number of events found.
*
* @return icsneo_error_t icsneo_error_success if successful, icsneo_error_invalid_parameters otherwise.
*/
ICSNEO_API icsneo_error_t icsneo_get_events(icsneo_event_t** events, uint32_t* events_count);
/** @brief Get the events of a device.
*
* @param[in] icsneo_device_t device The device to get the events of.
* @param[out] icsneo_event_t** events Pointer to an array of icsneo_event_t to copy the events into.
* Undefined behaviour if index is out of range of events_count.
* @param[in,out] uint32_t* events_count Size of the events array. Modified with the number of events found.
*
* @return icsneo_error_t icsneo_error_success if successful, icsneo_error_invalid_parameters otherwise.
*/
ICSNEO_API icsneo_error_t icsneo_device_get_events(icsneo_device_t* device, icsneo_event_t** events, uint32_t* events_count);
/** @brief Get the error string for an error code.
*
* @param[in] icsneo_event_t* event The event to get the description of.
* @param[out] const char* value Pointer to a buffer to copy the description into. Null terminated.
* @param[in,out] uint32_t* value_length Size of the value buffer. Modified with the length of the description.
*
* @return icsneo_error_t icsneo_error_success if successful, icsneo_error_invalid_parameters otherwise.
*/
ICSNEO_API icsneo_error_t icsneo_event_get_description(icsneo_event_t* event, const char* value, uint32_t* value_length);
#ifdef __cplusplus
}
#endif
#endif