k-fsw/app/src/hk_entries.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <ctype.h> | ||
| 2 | #include <errno.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | |||
| 9 | #include <kfsw/services/parameter.h> | ||
| 10 | |||
| 11 | #include "hk_entries.h" | ||
| 12 | |||
| 13 | 88 | int kfsw_app_hk_parse_entry(const char *text, struct kfsw_hk_entry *entry) | |
| 14 | { | ||
| 15 | unsigned long parts[3]; | ||
| 16 | 88 | size_t count = 0U; | |
| 17 | 88 | const char *cursor = text; | |
| 18 | |||
| 19 | 88 | if ((text == NULL) || (entry == NULL)) { | |
| 20 | 4 | return -EINVAL; | |
| 21 | } | ||
| 22 | |||
| 23 | 84 | for (;;) { | |
| 24 | char *end; | ||
| 25 | |||
| 26 | 168 | if ((count == ARRAY_SIZE(parts)) || !isdigit((unsigned char)*cursor)) { | |
| 27 | 36 | return -EINVAL; | |
| 28 | } | ||
| 29 | 140 | errno = 0; | |
| 30 | 140 | parts[count] = strtoul(cursor, &end, 0); | |
| 31 | 140 | if ((errno == ERANGE) || (parts[count] > UINT16_MAX)) { | |
| 32 | 8 | return -EINVAL; | |
| 33 | } | ||
| 34 | 132 | count++; | |
| 35 | 132 | cursor = end; | |
| 36 | 132 | if (*cursor != ':') { | |
| 37 | 48 | break; | |
| 38 | } | ||
| 39 | 84 | cursor++; | |
| 40 | } | ||
| 41 | |||
| 42 | 48 | if ((*cursor != '\0') || (count < 2U) || (parts[count - 2U] > UINT8_MAX) || | |
| 43 | 36 | (parts[count - 1U] > KFSW_PARAM_OFFSET_MAX)) { | |
| 44 | 20 | return -EINVAL; | |
| 45 | } | ||
| 46 | |||
| 47 | 28 | if (count == 2U) { | |
| 48 | 20 | entry->node = KFSW_HK_NODE_LOCAL; | |
| 49 | 20 | entry->param_id = KFSW_PARAM_ID(parts[0], parts[1]); | |
| 50 | } else { | ||
| 51 | 8 | entry->node = (uint16_t)parts[0]; | |
| 52 | 8 | entry->param_id = KFSW_PARAM_ID(parts[1], parts[2]); | |
| 53 | } | ||
| 54 | 28 | return 0; | |
| 55 | } | ||
| 56 | |||
| 57 | 12 | int kfsw_app_hk_parse_entries(const char *text, struct kfsw_hk_entry *entries, size_t max, | |
| 58 | size_t *count) | ||
| 59 | { | ||
| 60 | /* One entry at a time into a local buffer, because the text arrives as | ||
| 61 | * a single const string and strtok would write to it. | ||
| 62 | */ | ||
| 63 | char token[24]; | ||
| 64 | 12 | const char *cursor = text; | |
| 65 | 12 | size_t parsed = 0U; | |
| 66 | |||
| 67 | 12 | if ((text == NULL) || (entries == NULL) || (count == NULL) || (max == 0U)) { | |
| 68 | ✗ | return -EINVAL; | |
| 69 | } | ||
| 70 | |||
| 71 | 32 | while (*cursor != '\0') { | |
| 72 | 28 | size_t length = 0U; | |
| 73 | |||
| 74 | 48 | while (*cursor == ' ') { | |
| 75 | 20 | cursor++; | |
| 76 | } | ||
| 77 | 28 | if (*cursor == '\0') { | |
| 78 | ✗ | break; | |
| 79 | } | ||
| 80 | 128 | while ((cursor[length] != '\0') && (cursor[length] != ' ')) { | |
| 81 | 100 | length++; | |
| 82 | } | ||
| 83 | 28 | if (length >= sizeof(token)) { | |
| 84 | ✗ | return -EINVAL; | |
| 85 | } | ||
| 86 | 28 | if (parsed == max) { | |
| 87 | 4 | return -E2BIG; | |
| 88 | } | ||
| 89 | 24 | memcpy(token, cursor, length); | |
| 90 | 24 | token[length] = '\0'; | |
| 91 | 24 | if (kfsw_app_hk_parse_entry(token, &entries[parsed]) != 0) { | |
| 92 | 4 | return -EINVAL; | |
| 93 | } | ||
| 94 | 20 | parsed++; | |
| 95 | 20 | cursor += length; | |
| 96 | } | ||
| 97 | |||
| 98 | 4 | if (parsed == 0U) { | |
| 99 | ✗ | return -EINVAL; | |
| 100 | } | ||
| 101 | 4 | *count = parsed; | |
| 102 | 4 | return 0; | |
| 103 | } | ||
| 104 | |||
| 105 | ✗ | int kfsw_app_hk_parse_class(const char **text, uint8_t *retrieval_class) | |
| 106 | { | ||
| 107 | char *end; | ||
| 108 | unsigned long value; | ||
| 109 | |||
| 110 | ✗ | *retrieval_class = KFSW_HK_CLASS_DEFAULT; | |
| 111 | ✗ | if (strncmp(*text, "class=", 6U) != 0) { | |
| 112 | ✗ | return 0; | |
| 113 | } | ||
| 114 | ✗ | value = strtoul(*text + 6U, &end, 0); | |
| 115 | ✗ | if (end == *text + 6U || (*end != ' ' && *end != '\0')) { | |
| 116 | ✗ | return -EINVAL; | |
| 117 | } | ||
| 118 | ✗ | if (value > KFSW_HK_CLASS_MAX) { | |
| 119 | ✗ | return -ERANGE; | |
| 120 | } | ||
| 121 | ✗ | *retrieval_class = (uint8_t)value; | |
| 122 | ✗ | while (*end == ' ') { | |
| 123 | ✗ | end++; | |
| 124 | } | ||
| 125 | ✗ | *text = end; | |
| 126 | ✗ | return 0; | |
| 127 | } | ||
| 128 |