-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_cli_cmds.c
592 lines (511 loc) · 18.5 KB
/
demo_cli_cmds.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/**
* Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <ctype.h>
#include "nrf_cli.h"
#include "nrf_log.h"
#include "sdk_common.h"
#include "nrf_stack_guard.h"
#define CLI_EXAMPLE_MAX_CMD_CNT (20u)
#define CLI_EXAMPLE_MAX_CMD_LEN (33u)
#define CLI_EXAMPLE_VALUE_BIGGER_THAN_STACK (20000u)
/* buffer holding dynamicly created user commands */
static char m_dynamic_cmd_buffer[CLI_EXAMPLE_MAX_CMD_CNT][CLI_EXAMPLE_MAX_CMD_LEN];
/* commands counter */
static uint8_t m_dynamic_cmd_cnt;
uint32_t m_counter;
bool m_counter_active = false;
bool adxl362_start_read = false;
static void cmd_adxl_start(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
adxl362_start_read = true;
}
static void cmd_adxl_stop(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
adxl362_start_read = false;
}
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_adxl)
{
NRF_CLI_CMD(start, NULL, "Start axdl reader.", cmd_adxl_start),
NRF_CLI_CMD(stop, NULL, "Stop adxl reader.", cmd_adxl_stop),
NRF_CLI_SUBCMD_SET_END
};
static void cmd_adxl(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
ASSERT(p_cli);
ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
/* Extra defined dummy option */
static const nrf_cli_getopt_option_t opt[] = {
NRF_CLI_OPT(
"--test",
"-t",
"dummy option help string"
)
};
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, opt, ARRAY_SIZE(opt));
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
if (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--test"))
{
nrf_cli_print(p_cli, "Dummy test option.");
return;
}
/* subcommands have their own handlers and they are not processed here */
nrf_cli_error(p_cli, "%s: unknown parameter: %s", argv[0], argv[1]);
}
NRF_CLI_CMD_REGISTER(adxl,
&m_sub_adxl,
"Display adxl reading on terminal screen",
cmd_adxl);
/* Command handlers */
static void cmd_print_param(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
for (size_t i = 1; i < argc; i++)
{
nrf_cli_print(p_cli, "argv[%d] = %s", i, argv[i]);
}
}
static void cmd_print_all(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
for (size_t i = 1; i < argc; i++)
{
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s ", argv[i]);
}
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
}
static void cmd_print(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
ASSERT(p_cli);
ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
nrf_cli_error(p_cli, "%s: unknown parameter: %s", argv[0], argv[1]);
}
static void cmd_python(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
UNUSED_PARAMETER(argc);
UNUSED_PARAMETER(argv);
nrf_cli_error(p_cli, "Nice joke ;)");
}
static void cmd_dynamic(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc > 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
}
else
{
nrf_cli_error(p_cli, "%s: please specify subcommand", argv[0]);
}
}
/* function required by qsort */
static int string_cmp(const void * p_a, const void * p_b)
{
ASSERT(p_a);
ASSERT(p_b);
return strcmp((const char *)p_a, (const char *)p_b);
}
static void cmd_dynamic_add(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
if (m_dynamic_cmd_cnt >= CLI_EXAMPLE_MAX_CMD_CNT)
{
nrf_cli_error(p_cli, "command limit reached");
return;
}
uint8_t idx;
nrf_cli_cmd_len_t cmd_len = strlen(argv[1]);
if (cmd_len >= CLI_EXAMPLE_MAX_CMD_LEN)
{
nrf_cli_error(p_cli, "too long command");
return;
}
for (idx = 0; idx < cmd_len; idx++)
{
if (!isalnum((int)(argv[1][idx])))
{
nrf_cli_error(p_cli, "bad command name - please use only alphanumerical characters");
return;
}
}
for (idx = 0; idx < CLI_EXAMPLE_MAX_CMD_CNT; idx++)
{
if (!strcmp(m_dynamic_cmd_buffer[idx], argv[1]))
{
nrf_cli_error(p_cli, "duplicated command");
return;
}
}
sprintf(m_dynamic_cmd_buffer[m_dynamic_cmd_cnt++], "%s", argv[1]);
qsort(m_dynamic_cmd_buffer,
m_dynamic_cmd_cnt,
sizeof (m_dynamic_cmd_buffer[0]),
string_cmp);
nrf_cli_print(p_cli, "command added successfully");
}
static void cmd_dynamic_show(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
if (m_dynamic_cmd_cnt == 0)
{
nrf_cli_warn(p_cli, "Please add some commands first.");
return;
}
nrf_cli_print(p_cli, "Dynamic command list:");
for (uint8_t i = 0; i < m_dynamic_cmd_cnt; i++)
{
nrf_cli_print(p_cli, "[%3d] %s", i, m_dynamic_cmd_buffer[i]);
}
}
static void cmd_dynamic_execute(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
for (uint8_t idx = 0; idx < m_dynamic_cmd_cnt; idx++)
{
if (!strcmp(m_dynamic_cmd_buffer[idx], argv[1]))
{
nrf_cli_print(p_cli, "dynamic command: %s", argv[1]);
return;
}
}
nrf_cli_error(p_cli, "%s: uknown parameter: %s", argv[0], argv[1]);
}
static void cmd_dynamic_remove(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
for (uint8_t idx = 0; idx < m_dynamic_cmd_cnt; idx++)
{
if (!strcmp(m_dynamic_cmd_buffer[idx], argv[1]))
{
if (idx == CLI_EXAMPLE_MAX_CMD_CNT - 1)
{
m_dynamic_cmd_buffer[idx][0] = '\0';
}
else
{
memmove(m_dynamic_cmd_buffer[idx],
m_dynamic_cmd_buffer[idx + 1],
sizeof(m_dynamic_cmd_buffer[idx]) * (m_dynamic_cmd_cnt - idx));
}
--m_dynamic_cmd_cnt;
nrf_cli_print(p_cli, "command removed successfully");
return;
}
}
nrf_cli_error(p_cli, "did not find command: %s", argv[1]);
}
static void cmd_counter_start(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
m_counter_active = true;
}
static void cmd_counter_stop(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
m_counter_active = false;
}
static void cmd_counter_reset(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
m_counter = 0;
}
static void cmd_counter(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
ASSERT(p_cli);
ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
/* Extra defined dummy option */
static const nrf_cli_getopt_option_t opt[] = {
NRF_CLI_OPT(
"--test",
"-t",
"dummy option help string"
)
};
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, opt, ARRAY_SIZE(opt));
return;
}
if (argc != 2)
{
nrf_cli_error(p_cli, "%s: bad parameter count", argv[0]);
return;
}
if (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--test"))
{
nrf_cli_print(p_cli, "Dummy test option.");
return;
}
/* subcommands have their own handlers and they are not processed here */
nrf_cli_error(p_cli, "%s: unknown parameter: %s", argv[0], argv[1]);
}
static void cmd_nordic(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
UNUSED_PARAMETER(argc);
UNUSED_PARAMETER(argv);
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
nrf_cli_fprintf(p_cli, NRF_CLI_OPTION,
"\n"
" .co:. 'xo, \n"
" .,collllc,. 'ckOOo::,.. \n"
" .:ooooollllllll:'. .;dOOOOOOo:::;;;'. \n"
" 'okxddoooollllllllllll;'ckOOOOOOOOOo:::;;;,,,' \n"
" OOOkxdoooolllllllllllllllldxOOOOOOOo:::;;;,,,'.\n"
" OOOOOOkdoolllllllllllllllllllldxOOOo:::;;;,,,'.\n"
" OOOOOOOOOkxollllllllllllllllllcccldl:::;;;,,,'.\n"
" OOOOOOOOOOOOOxdollllllllllllllccccc::::;;;,,,'.\n"
" OOOOOOOOOOOOOOOOkxdlllllllllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOkdolllllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOxdllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOOOOkxolcccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOkdlc::::;;;,,,'.\n"
" xOOOOOOOOOOOxdkOOOOOOOOOOOOOOOOOOOOxoc:;;;,,,'.\n"
" xOOOOOOOOOOOdc::ldkOOOOOOOOOOOOOOOOOOOkdc;,,,''\n"
" xOOOOOOOOOOOdc::;;,;cdkOOOOOOOOOOOOOOOOOOOxl;''\n"
" .lkOOOOOOOOOdc::;;,,''..;oOOOOOOOOOOOOOOOOOOOx'\n"
" .;oOOOOOOdc::;;,. .:xOOOOOOOOOOOOd;. \n"
" .:xOOdc:,. 'ckOOOOkl' \n"
" .od' 'xk, \n"
"\n");
nrf_cli_print(p_cli, " Nordic Semiconductor \n");
}
/* This function cannot be static otherwise it can be inlined. As a result, variable:
tab[CLI_EXAMPLE_VALUE_BIGGER_THAN_STACK] will be always created on stack. This will block
possiblity to call functions: nrf_cli_help_requested and nrf_cli_help_print within
cmd_stack_overflow, because stack guard will be triggered. */
void cli_example_stack_overflow_force(void)
{
char tab[CLI_EXAMPLE_VALUE_BIGGER_THAN_STACK];
volatile char * p_tab = tab;
/* This function accesses stack area protected by nrf_stack_guard. As a result
MPU (memory protection unit) triggers an exception (hardfault). Hardfault handler will log
exception reason.*/
for (size_t idx = 0; idx < STACK_SIZE; idx++)
{
*(p_tab + idx) = (uint8_t)idx;
}
}
static void cmd_stack_overflow(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
UNUSED_PARAMETER(argc);
UNUSED_PARAMETER(argv);
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
cli_example_stack_overflow_force();
}
#if NRF_FPRINTF_DOUBLE_ENABLED
/* Variable created to avoid compiler warnings due to division by 0. */
static double zero = 0.0;
static void cmd_float_print(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
nrf_cli_print(p_cli, "%.10f", 0.0000000002);
nrf_cli_print(p_cli, "%.10f", 153.0000000002);
nrf_cli_print(p_cli, "%f", 0.0);
nrf_cli_print(p_cli, "%f", 1.0);
nrf_cli_print(p_cli, "%f", 0.0/zero);
nrf_cli_print(p_cli, "%F", -0.0/zero);
nrf_cli_print(p_cli, "%f", 1.0/zero);
nrf_cli_print(p_cli, "%F", -1.0/zero);
nrf_cli_print(p_cli, "%f", 0.123123123);
nrf_cli_print(p_cli, "%f", 0.1111118);
nrf_cli_print(p_cli, "%f", 3.14);
nrf_cli_print(p_cli, "%f", -3.14);
nrf_cli_print(p_cli, "%f", -210.25);
nrf_cli_print(p_cli, "%f", 2.828);
nrf_cli_print(p_cli, "%10.2f", 132.123);
nrf_cli_print(p_cli, "%+10.2f", 132.123);
nrf_cli_print(p_cli, "%010.2f", 132.123);
nrf_cli_print(p_cli, "%-10.2f", 132.123);
nrf_cli_print(p_cli, "%+-10.2f", 132.123);
nrf_cli_print(p_cli, "%10.2f", 0.0/zero);
}
#endif // NRF_FPRINTF_DOUBLE_ENABLED
/**
* @brief Command set array
* */
NRF_CLI_CMD_REGISTER(nordic, NULL, "Print Nordic Semiconductor logo.", cmd_nordic);
#if NRF_FPRINTF_DOUBLE_ENABLED
NRF_CLI_CMD_REGISTER(float_print, NULL, "Print float values.", cmd_float_print);
#endif // NRF_FPRINTF_DOUBLE_ENABLED
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_print)
{
NRF_CLI_CMD(all, NULL, "Print all entered parameters.", cmd_print_all),
NRF_CLI_CMD(param, NULL, "Print each parameter in new line.", cmd_print_param),
NRF_CLI_SUBCMD_SET_END
};
NRF_CLI_CMD_REGISTER(print, &m_sub_print, "print", cmd_print);
NRF_CLI_CMD_REGISTER(python, NULL, "python", cmd_python);
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_counter)
{
NRF_CLI_CMD(reset, NULL, "Reset seconds counter.", cmd_counter_reset),
NRF_CLI_CMD(start, NULL, "Start seconds counter.", cmd_counter_start),
NRF_CLI_CMD(stop, NULL, "Stop seconds counter.", cmd_counter_stop),
NRF_CLI_SUBCMD_SET_END
};
NRF_CLI_CMD_REGISTER(counter,
&m_sub_counter,
"Display seconds on terminal screen",
cmd_counter);
NRF_CLI_CMD_REGISTER(stack_overflow,
NULL,
"Command tests nrf_stack_guard module. Upon command call stack will be "
"overflowed and microcontroller shall log proper reset reason. \n\rTo observe "
"stack_guard execution, stack shall be set to value lower than 20000 bytes.",
cmd_stack_overflow);
/* dynamic command creation */
static void dynamic_cmd_get(size_t idx, nrf_cli_static_entry_t * p_static)
{
ASSERT(p_static);
if (idx < m_dynamic_cmd_cnt)
{
/* m_dynamic_cmd_buffer must be sorted alphabetically to ensure correct CLI completion */
p_static->p_syntax = m_dynamic_cmd_buffer[idx];
p_static->handler = NULL;
p_static->p_subcmd = NULL;
p_static->p_help = "Show dynamic command name.";
}
else
{
/* if there are no more dynamic commands available p_syntax must be set to NULL */
p_static->p_syntax = NULL;
}
}
NRF_CLI_CREATE_DYNAMIC_CMD(m_sub_dynamic_set, dynamic_cmd_get);
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_dynamic)
{
NRF_CLI_CMD(add, NULL,
"Add a new dynamic command.\nExample usage: [ dynamic add test ] will add "
"a dynamic command 'test'.\nIn this example, command name length is limited to 32 chars. "
"You can add up to 20 commands. Commands are automatically sorted to ensure correct "
"CLI completion.",
cmd_dynamic_add),
NRF_CLI_CMD(execute, &m_sub_dynamic_set, "Execute a command.", cmd_dynamic_execute),
NRF_CLI_CMD(remove, &m_sub_dynamic_set, "Remove a command.", cmd_dynamic_remove),
NRF_CLI_CMD(show, NULL, "Show all added dynamic commands.", cmd_dynamic_show),
NRF_CLI_SUBCMD_SET_END
};
NRF_CLI_CMD_REGISTER(dynamic,
&m_sub_dynamic,
"Demonstrate dynamic command usage.",
cmd_dynamic);