forked from bristlemouth/bm_protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_configuration.cpp
More file actions
238 lines (226 loc) · 8.19 KB
/
debug_configuration.cpp
File metadata and controls
238 lines (226 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* FreeRTOS+CLI includes. */
#include "FreeRTOS_CLI.h"
#include "app_util.h"
#include "debug_configuration.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static BaseType_t configurationCommand(char *writeBuffer, size_t writeBufferLen,
const char *commandString);
static const CLI_Command_Definition_t cmdConfiguration = {
// Command string
"cfg",
// Help string
"cfg:\n"
" * cfg <usr/hw/sys> set <key> <type:uint,int,float,str,bytestr> <value>\n"
" * cfg <usr/hw/sys> get <key> <type>\n"
" * cfg <usr/hw/sys> del <key>\n"
" * cfg <usr/hw/sys> clear\n"
" * cfg <usr/hw/sys> save\n"
" * cfg <usr/hw/sys> listkeys\n",
// Command function
configurationCommand,
// Number of parameters (variable)
-1};
void debugConfigurationInit(void) { FreeRTOS_CLIRegisterCommand(&cmdConfiguration); }
static BaseType_t configurationCommand(char *writeBuffer, size_t writeBufferLen,
const char *commandString) {
const char *parameter;
BaseType_t parameterStringLength;
// Remove unused argument warnings
(void)commandString;
(void)writeBuffer;
(void)writeBufferLen;
do {
BaseType_t partitionStrLen;
const char *partitionStr = FreeRTOS_CLIGetParameter(commandString, 1, &partitionStrLen);
if (partitionStr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
BmConfigPartition partition;
if (strncmp("usr", partitionStr, partitionStrLen) == 0) {
partition = BM_CFG_PARTITION_USER;
} else if (strncmp("hw", partitionStr, partitionStrLen) == 0) {
partition = BM_CFG_PARTITION_HARDWARE;
} else if (strncmp("sys", partitionStr, partitionStrLen) == 0) {
partition = BM_CFG_PARTITION_SYSTEM;
} else {
printf("ERR Invalid paramters\n");
break;
}
parameter = FreeRTOS_CLIGetParameter(commandString, 2, ¶meterStringLength);
if (parameter == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (strncmp("set", parameter, parameterStringLength) == 0) {
BaseType_t keyStrLen;
const char *keystr = FreeRTOS_CLIGetParameter(commandString, 3, &keyStrLen);
if (keystr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
BaseType_t typeStrLen;
const char *typestr = FreeRTOS_CLIGetParameter(commandString, 4, &typeStrLen);
if (typestr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
BaseType_t valueStrLen;
const char *valuestr = FreeRTOS_CLIGetParameter(commandString, 5, &valueStrLen);
if (valuestr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (strncmp("uint", typestr, typeStrLen) == 0) {
char *ptr;
uint32_t value = strtol(valuestr, &ptr, 10);
if (set_config_uint(partition, keystr, keyStrLen, value)) {
printf("set %lu\n", value);
} else {
printf("failed to set %lu\n", value);
}
} else if (strncmp("int", typestr, typeStrLen) == 0) {
char *ptr;
int32_t value = strtol(valuestr, &ptr, 10);
if (set_config_int(partition, keystr, keyStrLen, value)) {
printf("set %ld\n", value);
} else {
printf("failed to set %ld\n", value);
}
} else if (strncmp("float", typestr, typeStrLen) == 0) {
float value;
if (!bStrtof((char *)valuestr, &value)) {
printf("ERR Invalid paramters\n");
break;
}
if (set_config_float(partition, keystr, keyStrLen, value)) {
printf("set %f\n", value);
} else {
printf("failed to set %fd\n", value);
}
} else if (strncmp("str", typestr, typeStrLen) == 0) {
if (set_config_string(partition, keystr, keyStrLen, valuestr, valueStrLen)) {
printf("set %s\n", valuestr);
} else {
printf("failed to set %sd\n", valuestr);
}
} else if (strncmp("bytestr", typestr, typeStrLen) == 0) {
if (set_config_buffer(partition, keystr, keyStrLen, (uint8_t *)valuestr,
strlen(valuestr) + 1)) {
printf("set %s\n", valuestr);
} else {
printf("failed to set %sd\n", valuestr);
}
} else {
printf("ERR Invalid paramters\n");
break;
}
} else if (strncmp("get", parameter, parameterStringLength) == 0) {
BaseType_t keyStrLen;
const char *keystr = FreeRTOS_CLIGetParameter(commandString, 3, &keyStrLen);
if (keystr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
BaseType_t typeStrLen;
const char *typestr = FreeRTOS_CLIGetParameter(commandString, 4, &typeStrLen);
if (typestr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (strncmp("uint", typestr, typeStrLen) == 0) {
uint32_t value;
if (get_config_uint(partition, keystr, keyStrLen, &value)) {
printf("get %lu\n", value);
} else {
printf("failed to get %s\n", keystr);
}
} else if (strncmp("int", typestr, typeStrLen) == 0) {
int32_t value;
if (get_config_int(partition, keystr, keyStrLen, &value)) {
printf("get %ld\n", value);
} else {
printf("failed to get %s\n", keystr);
}
} else if (strncmp("float", typestr, typeStrLen) == 0) {
float value;
if (get_config_float(partition, keystr, keyStrLen, &value)) {
printf("get %f\n", value);
} else {
printf("failed to get %s\n", keystr);
}
} else if (strncmp("str", typestr, typeStrLen) == 0) {
char strbuf[MAX_CONFIG_BUFFER_SIZE_BYTES];
size_t strlen = sizeof(strbuf);
if (get_config_string(partition, keystr, keyStrLen, strbuf, &strlen)) {
strbuf[strlen] = '\0';
printf("get %s\n", strbuf);
} else {
printf("failed to get %s\n", keystr);
}
} else if (strncmp("bytestr", typestr, typeStrLen) == 0) {
uint8_t bytes[MAX_CONFIG_BUFFER_SIZE_BYTES];
size_t bytelen = sizeof(bytes);
if (get_config_buffer(partition, keystr, keyStrLen, bytes, &bytelen)) {
printf("get bytes:");
for (size_t i = 0; i < bytelen; i++) {
printf("%02x:", bytes[i]);
}
printf("\n");
} else {
printf("failed to get %s\n", keystr);
}
} else {
printf("ERR Invalid paramters\n");
break;
}
} else if (strncmp("listkeys", parameter, parameterStringLength) == 0) {
uint8_t num_keys = 0;
const ConfigKey *keys = get_stored_keys(partition, &num_keys);
printf("num keys: %u\n", num_keys);
for (int i = 0; i < num_keys; i++) {
size_t keybufSize = keys[i].key_len + 1;
char *keybuf = (char *)pvPortMalloc(keybufSize);
configASSERT(keybuf);
memcpy(keybuf, keys[i].key_buf, keybufSize);
keybuf[keys[i].key_len] = '\0';
printf("%s : %s\n", keybuf, data_type_enum_to_str(keys[i].value_type));
vPortFree(keybuf);
}
} else if (strncmp("del", parameter, parameterStringLength) == 0) {
BaseType_t keyStrLen;
const char *keystr = FreeRTOS_CLIGetParameter(commandString, 3, &keyStrLen);
if (keystr == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (remove_key(partition, keystr, keyStrLen)) {
printf("Removed key %s\n", keystr);
} else {
printf("Failed to remove key %s\n", keystr);
}
} else if (strncmp("clear", parameter, parameterStringLength) == 0) {
if (!clear_partition(partition)) {
printf("Failed to clear config partition.\n");
} else {
printf("Successfully cleared config partition.\n");
}
// Succesfull "save" will reset the system.
} else if (strncmp("save", parameter, parameterStringLength) == 0) {
if (!save_config(partition, true)) {
printf("Failed to save config.\n");
}
// Succesfull "save" will reset the system.
} else {
printf("ERR Invalid paramters\n");
break;
}
} while (0);
return pdFALSE;
}