-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmdparse.c
399 lines (358 loc) · 11.5 KB
/
cmdparse.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Amiigo Link command text parser
*
* @date March 8, 2014
* @author: dashesy
* @copyright Amiigo Inc.
*/
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "amcmd.h"
#include "cmdparse.h"
AMIIGO_CMD g_cmd = AMIIGO_CMD_NONE;
amcfg_t g_cfg;
extern char g_src[512];
// Initialize the command configs to defaults
void cmd_init(void) {
//---------- config packets ------------
memset(&g_cfg, 0, sizeof(g_cfg));
// Some defulats
g_cfg.maint_led.duration = 5;
g_cfg.maint_led.led = 6;
g_cfg.maint_led.speed = 1;
}
void trim(char *str)
{
int i;
int begin = 0;
int end = strlen(str) - 1;
while (isspace(str[begin]))
begin++;
while ((end >= begin) && isspace(str[end]))
end--;
// Shift all characters back to the start of the string array.
for (i = begin; i <= end; i++)
str[i - begin] = str[i];
str[i - begin] = '\0'; // Null terminate string.
}
int parse_command(const char * szName) {
if (strcasecmp(szName, "download") == 0) {
g_cmd = AMIIGO_CMD_DOWNLOAD;
} else if (strcasecmp(szName, "resetcpu") == 0) {
g_cmd = AMIIGO_CMD_RESET_CPU;
} else if (strcasecmp(szName, "resetlogs") == 0) {
g_cmd = AMIIGO_CMD_RESET_LOGS;
} else if (strcasecmp(szName, "resetconfigs") == 0) {
g_cmd = AMIIGO_CMD_RESET_CONFIGS;
} else if (strcasecmp(szName, "configls") == 0) {
g_cmd = AMIIGO_CMD_CONFIGLS;
} else if (strcasecmp(szName, "configaccel") == 0) {
g_cmd = AMIIGO_CMD_CONFIGACCEL;
} else if (strcasecmp(szName, "configtemp") == 0) {
g_cmd = AMIIGO_CMD_CONFIGTEMP;
} else if (strcasecmp(szName, "blink") == 0) {
g_cmd = AMIIGO_CMD_BLINK;
} else if (strcasecmp(szName, "deepsleep") == 0) {
g_cmd = AMIIGO_CMD_DEEPSLEEP;
} else if (strcasecmp(szName, "status") == 0) {
g_cmd = AMIIGO_CMD_NONE;
} else if (strcasecmp(szName, "rename") == 0) {
g_cmd = AMIIGO_CMD_RENAME;
} else if (strcasecmp(szName, "tag") == 0) {
g_cmd = AMIIGO_CMD_TAG;
} else if (strcasecmp(szName, "test_seq") == 0) {
g_cmd = AMIIGO_CMD_TEST_SEQ;
} else if (strcasecmp(szName, "extstatus") == 0) {
g_cmd = AMIIGO_CMD_EXTSTATUS;
} else {
fprintf(stderr, "Invalid command (%s)!\n", szName);
return -1;
}
return 0;
}
int parse_mode(const char * szName) {
if (strcasecmp(szName, "fast") == 0) {
g_cfg.general.flags = CFG_NEW_MODE;
g_cfg.general.usemode = RATE_FAST;
} else if (strcasecmp(szName, "slow") == 0) {
g_cfg.general.flags = CFG_NEW_MODE;
g_cfg.general.usemode = RATE_SLOW;
} else if (strcasecmp(szName, "sleep") == 0) {
g_cfg.general.flags = CFG_NEW_MODE;
g_cfg.general.usemode = RATE_SLEEP;
} else {
fprintf(stderr, "Invalid mode (%s)!\n", szName);
return -1;
}
return 0;
}
int parse_adapter(const char * szName) {
strcpy(g_src, szName);
return 0;
}
int parse_device(const char * szName) {
char * str = strdup(szName);
char * pch;
pch = strtok (str, ",");
int dev_count = 0;
while (pch != NULL) {
if (dev_count >= MAX_DEV_COUNT) {
fprintf(stderr, "Maximum of %d devices can be connected to\n", MAX_DEV_COUNT);
return -1;
}
g_cfg.dst[dev_count] = pch;
dev_count++;
pch = strtok (NULL, ",");
}
if (dev_count == 0) {
fprintf(stderr, "Invalid device address");
return -1;
}
g_cfg.count_dst = dev_count;
return 0;
}
int parse_i2c_write(const char * szArg) {
char * str = strdup(szArg);
char * pch;
pch = strtok (str, ":");
int arg_count = 0;
while (pch != NULL) {
arg_count++;
if (arg_count == 1)
g_cfg.i2c.address = (uint8)atoi(pch);
else if (arg_count == 2) {
g_cfg.i2c.reg = (uint8)atoi(pch);
} else if (arg_count == 3) {
g_cfg.i2c.data = (uint8)atoi(pch);
} else {
break;
}
pch = strtok (NULL, ":");
}
if (arg_count != 3) {
fprintf(stderr, "Invalid i2c address:reg:value format");
return -1;
}
g_cfg.i2c.write = 1;
g_cmd = AMIIGO_CMD_I2C_WRITE;
return 0;
}
int parse_i2c_read(const char * szArg) {
char * str = strdup(szArg);
char * pch;
pch = strtok (str, ":");
int arg_count = 0;
while (pch != NULL) {
arg_count++;
if (arg_count == 1) {
g_cfg.i2c.address = (uint8)atoi(pch);
} else if (arg_count == 2) {
g_cfg.i2c.reg = (uint8)atoi(pch);
} else {
break;
}
pch = strtok (NULL, ":");
}
if (arg_count != 2) {
printf("Invalid i2c address:reg format");
return -1;
}
g_cfg.i2c.write = 0;
g_cmd = AMIIGO_CMD_I2C_READ;
return 0;
}
// Parse a single parameter
int parse_config_single(const char * szParam) {
int err = 0;
switch (g_cmd) {
case AMIIGO_CMD_RENAME:
if (strlen(szParam) > DEV_NAME_LEN)
return -1;
strcpy(&g_cfg.name.name[0], szParam);
break;
default:
fprintf(stderr,"Configuration parameter %s not recognized!\n", szParam);
err = -1;
break;
}
return err;
}
// Parse param = value pairs
int set_config_pairs(const char * szParam, const char * szVal) {
if (szVal == NULL || szVal[0] == 0 || isspace(szVal[0]))
return parse_config_single(szParam);
long val = strtol(szVal, NULL, 0);
//---------- Light ----------------------------
if (strcasecmp(szParam, "ls_fast_interval") == 0) {
// Seconds between samples in fast mode
g_cfg.config_ls.fast_interval = (uint16_t) val;
} else if (strcasecmp(szParam, "ls_slow_interval") == 0) {
// Seconds between samples in slow mode
g_cfg.config_ls.slow_interval = (uint16_t) val;
} else if (strcasecmp(szParam, "ls_sleep_interval") == 0) {
// Seconds between samples in sleep mode
g_cfg.config_ls.sleep_interval = (uint16_t) val;
} else if (strcasecmp(szParam, "ls_duration") == 0) {
g_cfg.config_ls.manual_duration = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_fast_duration") == 0) {
g_cfg.config_ls.durations[RATE_FAST] = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_slow_duration") == 0) {
g_cfg.config_ls.durations[RATE_SLOW] = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_sleep_duration") == 0) {
g_cfg.config_ls.durations[RATE_SLEEP] = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_debug") == 0) {
g_cfg.config_ls.debug = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_flags") == 0) {
g_cfg.config_ls.flags = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_movement") == 0) {
g_cfg.config_ls.movement = (uint8_t) val;
} else if (strcasecmp(szParam, "ls_duration_mul") == 0) {
g_cfg.config_ls.duration_mul = (uint8_t) val;
}
// --------------- Blink ----------------------
else if (strcasecmp(szParam, "blink_duration") == 0) {
g_cfg.maint_led.duration = (uint8_t) val;
}
else if (strcasecmp(szParam, "blink_led") == 0) {
g_cfg.maint_led.led = (uint8_t) val;
}
else if (strcasecmp(szParam, "blink_speed") == 0) {
g_cfg.maint_led.speed = (uint8_t) val;
}
// ---------------- Accel ---------------------
else if (strcasecmp(szParam, "accel_slow_rate") == 0) {
g_cfg.config_accel.slow_rate = WEDConfigRateParam(
(uint32_t) val);
} else if (strcasecmp(szParam, "accel_fast_rate") == 0) {
g_cfg.config_accel.fast_rate = WEDConfigRateParam(
(uint32_t) val);
} else if (strcasecmp(szParam, "accel_sleep_rate") == 0) {
g_cfg.config_accel.sleep_rate = WEDConfigRateParam(
(uint32_t) val);
}
// ---------------- Temperature ---------------------
else if (strcasecmp(szParam, "temp_slow_rate") == 0) {
g_cfg.config_temp.slow_rate = WEDConfigRateParam(
(uint32_t) val);
} else if (strcasecmp(szParam, "temp_fast_rate") == 0) {
g_cfg.config_temp.fast_rate = WEDConfigRateParam(
(uint32_t) val);
} else if (strcasecmp(szParam, "temp_sleep_rate") == 0) {
g_cfg.config_temp.sleep_rate = WEDConfigRateParam(
(uint32_t) val);
}
// ---------------- rename ---------------------
else if (strcasecmp(szParam, "name") == 0) {
if (strlen(szVal) > DEV_NAME_LEN) {
fprintf(stderr, "Name cannot be more than %d characters", DEV_NAME_LEN);
return -1;
}
strcpy(&g_cfg.name.name[0], szVal);
}
// ---------------- mode ---------------------
else if (strcasecmp(szParam, "mode") == 0) {
if (parse_mode(szVal))
return -1;
}
// ----------- accel test mode ---------------
else if (strcasecmp(szParam, "test_mode") == 0) {
g_cfg.test_mode = (uint8_t) val;
}
// ---------------- tag ---------------------
else if (strcasecmp(szParam, "tag") == 0) {
uint32_t uval = (uint32_t)val;
memcpy(&g_cfg.general.tag[0], &uval, 4);
} else {
fprintf(stderr,"Configuration parameter %s not recognized!\n", szParam);
return -1;
}
return 0;
}
// Parse one line of param or param=value
int parse_one_pair(char * szLine) {
char * pch = strchr(szLine, '=');
if (pch == NULL)
return parse_config_single(szLine);
*pch = 0;
return set_config_pairs(szLine, pch + 1);
}
// Parse single command line
int parse_input_line(const char * szName) {
int err = 0;
char * szArg = strdup(szName);
char * pch;
pch = strtok (szArg, ",");
while (pch != NULL) {
err = parse_one_pair(pch);
if (err)
break;
pch = strtok (NULL, ",");
}
free(szArg);
return err;
}
// If file exists
int parse_file_exists(const char * szName) {
FILE * fp = fopen(szName, "r");
if (fp == NULL)
return 0;
fclose(fp);
return 0;
}
// If the name is likely a file
int parse_is_file_name(const char * szName) {
int is_path = (strpbrk(szName, "./") != NULL);
if (is_path)
return 1;
if (strpbrk(szName, "=,"))
return 0;
return 0;
}
// If the name is likely a file
int parse_is_input_file(const char * szName) {
if (parse_file_exists(szName))
return 1;
if (parse_is_file_name(szName))
return 1;
return 0;
}
// Parse file
int parse_input_file(const char * szName) {
// If it is detected to be sequence of param=value[,...] parse the line
if (!parse_is_input_file(szName))
return parse_input_line(szName);
char szCmd[256];
char szParam[256];
char szVal[256];
FILE * fp = fopen(szName, "r");
if (fp == NULL) {
fprintf(stderr, "Configuration file (%s) not accessible!\n", szName);
return -1;
}
while (fgets(szCmd, 256, fp) != NULL) {
trim(szCmd);
if (szCmd[0] == '#')
continue; // Ignore comments
if (sscanf(szCmd, "%s %s", szParam, szVal) != 2)
{
if (strlen(szCmd) > 1)
{
fprintf(stderr, "Command %s in %s not recognized!\n", szCmd, szName);
fclose(fp);
return -1;
}
continue;
}
int err = set_config_pairs(szParam, szVal);
if (err) {
fclose(fp);
return err;
}
}
fclose(fp);
return 0;
}