diff --git a/api/icsneo/icsneo.cpp b/api/icsneo/icsneo.cpp index 5cbc097..dc911d3 100644 --- a/api/icsneo/icsneo.cpp +++ b/api/icsneo/icsneo.cpp @@ -17,6 +17,42 @@ typedef struct icsneo_device_t { static std::deque> g_devices; +ICSNEO_API icsneo_error_t icsneo_error_code(icsneo_error_t error_code, const char* value, uint32_t* value_length) { + if (!value || !value_length) { + return icsneo_error_invalid_parameters; + } + + std::string error; + switch (error_code) { + case icsneo_error_success: + error = "success"; + break; + case icsneo_error_invalid_parameters: + error = "invalid parameters"; + break; + case icsneo_error_open_failed: + error = "open failed"; + break; + case icsneo_error_go_online_failed: + error = "go online failed"; + break; + case icsneo_error_enable_message_polling_failed: + error = "enable message polling failed"; + break; + case icsneo_error_sync_rtc_failed: + error = "sync RTC failed"; + break; + // Don't default, let the compiler warn us if we forget to handle an error code + } + // Find the minimum length of the error string and set value_length + auto min_length = std::minmax(static_cast(error.length()), *value_length).first; + *value_length = min_length; + // Copy the string into value + strncpy(const_cast(value), error.c_str(), min_length); + + return icsneo_error_success; +} + ICSNEO_API icsneo_error_t icsneo_find(icsneo_device_t** devices, uint32_t* devices_count, void* reserved) { if (!devices || !devices_count) { return icsneo_error_invalid_parameters; diff --git a/include/icsneo/icsneo.h b/include/icsneo/icsneo.h index 97bbb11..dbeae3a 100644 --- a/include/icsneo/icsneo.h +++ b/include/icsneo/icsneo.h @@ -40,6 +40,8 @@ typedef struct icsneo_device_t icsneo_device_t; /** @brief Error codes for icsneo functions. + * + * This enum is guaranteed to be ABI stable, any new values will be appended to the end. */ typedef enum _icsneo_error_t { // Function was successful @@ -63,6 +65,15 @@ typedef enum _icsneo_error_t { typedef uint32_t icsneo_error_t; +/** @brief Get the error string for an error code. + * + * @param[in] icsneo_device_t device The device 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_error_code(icsneo_error_t error_code, const char* value, uint32_t* value_length); /** @brief Find all hardware attached to the system. *