diff --git a/api/icsneo/icsneo.cpp b/api/icsneo/icsneo.cpp index 59e76e3..f1de52d 100644 --- a/api/icsneo/icsneo.cpp +++ b/api/icsneo/icsneo.cpp @@ -115,6 +115,12 @@ ICSNEO_API icsneo_error_t icsneo_get_error_code(icsneo_error_t error_code, const case icsneo_error_sync_rtc_failed: error = "Syncronizing RTC failed"; break; + case icsneo_error_get_messages_failed: + error = "Getting messages failed"; + break; + case icsneo_error_invalid_type: + error = "Invalid type"; + break; case icsneo_error_rtc_failure: error = "RTC failure"; break; @@ -474,6 +480,15 @@ ICSNEO_API icsneo_error_t icsneo_message_get_type(icsneo_device_t* device, icsne return icsneo_error_success; } +ICSNEO_API icsneo_error_t icsneo_message_get_type_name(icsneo_msg_type_t msg_type, const char* value, uint32_t* value_length) { + if (!value || !value_length) { + return icsneo_error_invalid_parameters; + } + // TODO: Check if message is valid + // Copy the string into value + return safe_str_copy(value, value_length, Message::getMsgTypeName(msg_type)) ? icsneo_error_success : icsneo_error_string_copy_failed; +} + 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) { if (!device || !message || !bus_type) { return icsneo_error_invalid_parameters; diff --git a/include/icsneo/communication/message/message.h b/include/icsneo/communication/message/message.h index cb5a334..e92fc17 100644 --- a/include/icsneo/communication/message/message.h +++ b/include/icsneo/communication/message/message.h @@ -9,9 +9,13 @@ typedef uint16_t neomessagetype_t; #include "icsneo/communication/network.h" #include "icsneo/icsneotypes.h" #include +#include +#include namespace icsneo { + + class AbstractMessage { public: virtual const icsneo_msg_type_t getMsgType() const = 0; @@ -21,6 +25,28 @@ class Message : public AbstractMessage { public: virtual const icsneo_msg_type_t getMsgType() const { return icsneo_msg_type_device; } + /** + * @brief Get the string representation of the message type + * + * @return String representation of the message type + * + * @see AbstractMessage::getMsgType() + */ + static std::string getMsgTypeName(icsneo_msg_type_t msg_type) { + switch (msg_type) { + case icsneo_msg_type_device: + return "Device"; + case icsneo_msg_type_internal: + return "Internal"; + case icsneo_msg_type_bus: + return "Bus"; + // Don't default here so we can rely on the compiler to warn us about missing cases + }; + std::stringstream ss; + ss << "Unknown (" << (int)msg_type << ")"; + return ss.str(); + } + enum class Type : neomessagetype_t { BusMessage = 0, diff --git a/include/icsneo/icsneo.h b/include/icsneo/icsneo.h index 56a560c..219c024 100644 --- a/include/icsneo/icsneo.h +++ b/include/icsneo/icsneo.h @@ -90,6 +90,8 @@ typedef enum _icsneo_error_t { icsneo_error_transmit_messages_failed, // Failed to copy string to buffer icsneo_error_string_copy_failed + + // NOTE: Any new values added here should be updated in icsneo_get_error_code } _icsneo_error_t; /** @brief Integer representation of _icsneo_error_t enum. @@ -343,6 +345,17 @@ ICSNEO_API icsneo_error_t icsneo_message_is_valid(icsneo_device_t* device, icsne */ ICSNEO_API icsneo_error_t icsneo_message_get_type(icsneo_device_t* device, icsneo_message_t* message, icsneo_msg_type_t* msg_type); +/** @brief Get the message type string for a icsneo_msg_bus_type_t. + * + * @param[in] icsneo_device_t* device The device to check against. + * @param[in] icsneo_message_t* message The message to check. + * @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_message_get_type_name(icsneo_msg_type_t msg_type, const char* value, uint32_t* value_length); + /** @brief Get the type of a bus message * * @param[in] icsneo_device_t* device The device to check against.