esp-idf/components/esp32/esp_err_to_name.c.in
Roland Dobai 4322e31c98 Make it easier to look up error messages
tools/gen_esp_err_to_name.py generates
components/esp32/esp_err_to_name.c for lookup of the error codes from
all of the IDF project and easily identify all codes in one place
2018-02-15 14:19:03 +01:00

54 lines
1.1 KiB
C

@COMMENT@
#include <string.h>
#if __has_include("soc/soc.h")
#include "soc/soc.h"
#endif
@HEADERS@
#define ERR_TBL_IT(err) {err, #err}
typedef struct {
esp_err_t code;
const char *msg;
} esp_err_msg_t;
static const esp_err_msg_t esp_err_msg_table[] = {
@ERROR_ITEMS@
};
static const char esp_unknown_msg[] = "UNKNOWN ERROR";
const char *esp_err_to_name(esp_err_t code)
{
int i;
for (i = 0; i < sizeof(esp_err_msg_table)/sizeof(esp_err_msg_table[0]); ++i) {
if (esp_err_msg_table[i].code == code) {
return esp_err_msg_table[i].msg;
}
}
return esp_unknown_msg;
}
const char *esp_err_to_name_r(esp_err_t code, char *buf, size_t buflen)
{
int i;
for (i = 0; i < sizeof(esp_err_msg_table)/sizeof(esp_err_msg_table[0]); ++i) {
if (esp_err_msg_table[i].code == code) {
strlcpy(buf, esp_err_msg_table[i].msg, buflen);
return buf;
}
}
if (strerror_r(code, buf, buflen) == 0) {
return buf;
}
snprintf(buf, buflen, "Unknown error %d", code);
return buf;
}