kfsw-platform/src/hardware.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <sys/types.h> | ||
| 4 | |||
| 5 | #include <zephyr/drivers/hwinfo.h> | ||
| 6 | |||
| 7 | #include <kfsw/platform/hardware.h> | ||
| 8 | |||
| 9 | 20 | int kfsw_platform_get_hardware_id(char *text, size_t size) | |
| 10 | { | ||
| 11 | static const char digits[] = "0123456789abcdef"; | ||
| 12 | uint8_t id[KFSW_HARDWARE_ID_MAX_BYTES]; | ||
| 13 | ssize_t length; | ||
| 14 | size_t needed; | ||
| 15 | size_t index; | ||
| 16 | |||
| 17 | 20 | if (text == NULL) { | |
| 18 | 2 | return -EINVAL; | |
| 19 | } | ||
| 20 | |||
| 21 | /* No driver (-ENOSYS) and an empty ID both mean the SoC has no ID. */ | ||
| 22 | 18 | length = hwinfo_get_device_id(id, sizeof(id)); | |
| 23 | 18 | if (length <= 0) { | |
| 24 | ✗ | return -ENOTSUP; | |
| 25 | } | ||
| 26 | |||
| 27 | 18 | needed = ((size_t)length * 2U) + 1U; | |
| 28 | 18 | if (size < needed) { | |
| 29 | 4 | return -ENOSPC; | |
| 30 | } | ||
| 31 | |||
| 32 | 70 | for (index = 0U; index < (size_t)length; index++) { | |
| 33 | 56 | text[index * 2U] = digits[id[index] >> 4]; | |
| 34 | 56 | text[(index * 2U) + 1U] = digits[id[index] & 0x0fU]; | |
| 35 | } | ||
| 36 | 14 | text[needed - 1U] = '\0'; | |
| 37 | |||
| 38 | 14 | return 0; | |
| 39 | } | ||
| 40 |