./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
49 / 67
73.1%
Branches:
42 / 62
67.7%
Functions:
2 / 3
66.7%

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
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
88 if ((text == NULL) || (entry == NULL)) {
20 4 return -EINVAL;
21 }
22
23 84 for (;;) {
24 char *end;
25
26
4/4
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 140 times.
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
4/4
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 132 times.
140 if ((errno == ERANGE) || (parts[count] > UINT16_MAX)) {
32 8 return -EINVAL;
33 }
34 132 count++;
35 132 cursor = end;
36
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 84 times.
132 if (*cursor != ':') {
37 48 break;
38 }
39 84 cursor++;
40 }
41
42
6/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 4 times.
48 if ((*cursor != '\0') || (count < 2U) || (parts[count - 2U] > UINT8_MAX) ||
43
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
36 (parts[count - 1U] > KFSW_PARAM_OFFSET_MAX)) {
44 20 return -EINVAL;
45 }
46
47
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
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
4/8
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
12 if ((text == NULL) || (entries == NULL) || (count == NULL) || (max == 0U)) {
68 ✗ return -EINVAL;
69 }
70
71
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 while (*cursor != '\0') {
72 28 size_t length = 0U;
73
74
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 28 times.
48 while (*cursor == ' ') {
75 20 cursor++;
76 }
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (*cursor == '\0') {
78 ✗ break;
79 }
80
4/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 16 times.
128 while ((cursor[length] != '\0') && (cursor[length] != ' ')) {
81 100 length++;
82 }
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (length >= sizeof(token)) {
84 ✗ return -EINVAL;
85 }
86
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (parsed == max) {
87 4 return -E2BIG;
88 }
89 24 memcpy(token, cursor, length);
90 24 token[length] = '\0';
91
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 20 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (strncmp(*text, "class=", 6U) != 0) {
112 ✗ return 0;
113 }
114 ✗ value = strtoul(*text + 6U, &end, 0);
115
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (end == *text + 6U || (*end != ' ' && *end != '\0')) {
116 ✗ return -EINVAL;
117 }
118
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (value > KFSW_HK_CLASS_MAX) {
119 ✗ return -ERANGE;
120 }
121 ✗ *retrieval_class = (uint8_t)value;
122
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ while (*end == ' ') {
123 ✗ end++;
124 }
125 ✗ *text = end;
126 ✗ return 0;
127 }
128