-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
main.c
391 lines (323 loc) · 8.77 KB
/
main.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
/*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/version.h>
#include <zephyr/logging/log.h>
#include <stdlib.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/usb/usb_device.h>
#include <ctype.h>
#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#else
#include <zephyr/posix/unistd.h>
#endif
LOG_MODULE_REGISTER(app);
extern void foo(void);
void timer_expired_handler(struct k_timer *timer)
{
LOG_INF("Timer expired.");
/* Call another module to present logging from multiple sources. */
foo();
}
K_TIMER_DEFINE(log_timer, timer_expired_handler, NULL);
static int cmd_log_test_start(const struct shell *sh, size_t argc,
char **argv, uint32_t period)
{
ARG_UNUSED(argv);
k_timer_start(&log_timer, K_MSEC(period), K_MSEC(period));
shell_print(sh, "Log test started\n");
return 0;
}
static int cmd_log_test_start_demo(const struct shell *sh, size_t argc,
char **argv)
{
return cmd_log_test_start(sh, argc, argv, 200);
}
static int cmd_log_test_start_flood(const struct shell *sh, size_t argc,
char **argv)
{
return cmd_log_test_start(sh, argc, argv, 10);
}
static int cmd_log_test_stop(const struct shell *sh, size_t argc,
char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
k_timer_stop(&log_timer);
shell_print(sh, "Log test stopped");
return 0;
}
SHELL_STATIC_SUBCMD_SET_CREATE(sub_log_test_start,
SHELL_CMD_ARG(demo, NULL,
"Start log timer which generates log message every 200ms.",
cmd_log_test_start_demo, 1, 0),
SHELL_CMD_ARG(flood, NULL,
"Start log timer which generates log message every 10ms.",
cmd_log_test_start_flood, 1, 0),
SHELL_SUBCMD_SET_END /* Array terminated. */
);
SHELL_STATIC_SUBCMD_SET_CREATE(sub_log_test,
SHELL_CMD_ARG(start, &sub_log_test_start, "Start log test", NULL, 2, 0),
SHELL_CMD_ARG(stop, NULL, "Stop log test.", cmd_log_test_stop, 1, 0),
SHELL_SUBCMD_SET_END /* Array terminated. */
);
SHELL_CMD_REGISTER(log_test, &sub_log_test, "Log test", NULL);
static int cmd_demo_ping(const struct shell *sh, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(sh, "pong");
return 0;
}
static int cmd_demo_board(const struct shell *sh, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(sh, CONFIG_BOARD);
return 0;
}
#if defined CONFIG_SHELL_GETOPT
/* Thread save usage */
static int cmd_demo_getopt_ts(const struct shell *sh, size_t argc,
char **argv)
{
struct getopt_state *state;
char *cvalue = NULL;
int aflag = 0;
int bflag = 0;
int c;
while ((c = getopt(argc, argv, "abhc:")) != -1) {
state = getopt_state_get();
switch (c) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = state->optarg;
break;
case 'h':
/* When getopt is active shell is not parsing
* command handler to print help message. It must
* be done explicitly.
*/
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
case '?':
if (state->optopt == 'c') {
shell_print(sh,
"Option -%c requires an argument.",
state->optopt);
} else if (isprint(state->optopt) != 0) {
shell_print(sh,
"Unknown option `-%c'.",
state->optopt);
} else {
shell_print(sh,
"Unknown option character `\\x%x'.",
state->optopt);
}
return 1;
default:
break;
}
}
shell_print(sh, "aflag = %d, bflag = %d", aflag, bflag);
return 0;
}
static int cmd_demo_getopt(const struct shell *sh, size_t argc,
char **argv)
{
char *cvalue = NULL;
int aflag = 0;
int bflag = 0;
int c;
while ((c = getopt(argc, argv, "abhc:")) != -1) {
switch (c) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case 'h':
/* When getopt is active shell is not parsing
* command handler to print help message. It must
* be done explicitly.
*/
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
case '?':
if (optopt == 'c') {
shell_print(sh,
"Option -%c requires an argument.",
optopt);
} else if (isprint(optopt) != 0) {
shell_print(sh, "Unknown option `-%c'.",
optopt);
} else {
shell_print(sh,
"Unknown option character `\\x%x'.",
optopt);
}
return 1;
default:
break;
}
}
shell_print(sh, "aflag = %d, bflag = %d", aflag, bflag);
return 0;
}
#endif
static int cmd_demo_params(const struct shell *sh, size_t argc, char **argv)
{
shell_print(sh, "argc = %zd", argc);
for (size_t cnt = 0; cnt < argc; cnt++) {
shell_print(sh, " argv[%zd] = %s", cnt, argv[cnt]);
}
return 0;
}
static int cmd_demo_hexdump(const struct shell *sh, size_t argc, char **argv)
{
shell_print(sh, "argc = %zd", argc);
for (size_t cnt = 0; cnt < argc; cnt++) {
shell_print(sh, "argv[%zd]", cnt);
shell_hexdump(sh, argv[cnt], strlen(argv[cnt]));
}
return 0;
}
static int cmd_version(const struct shell *sh, size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(sh, "Zephyr version %s", KERNEL_VERSION_STRING);
return 0;
}
static int set_bypass(const struct shell *sh, shell_bypass_cb_t bypass)
{
static bool in_use;
if (bypass && in_use) {
shell_error(sh, "Sample supports setting bypass on single instance.");
return -EBUSY;
}
in_use = !in_use;
if (in_use) {
shell_print(sh, "Bypass started, press ctrl-x ctrl-q to escape");
in_use = true;
}
shell_set_bypass(sh, bypass);
return 0;
}
#define CHAR_1 0x18
#define CHAR_2 0x11
static void bypass_cb(const struct shell *sh, uint8_t *data, size_t len)
{
static uint8_t tail;
bool escape = false;
/* Check if escape criteria is met. */
if (tail == CHAR_1 && data[0] == CHAR_2) {
escape = true;
} else {
for (int i = 0; i < (len - 1); i++) {
if (data[i] == CHAR_1 && data[i + 1] == CHAR_2) {
escape = true;
break;
}
}
}
if (escape) {
shell_print(sh, "Exit bypass");
set_bypass(sh, NULL);
tail = 0;
return;
}
/* Store last byte for escape sequence detection */
tail = data[len - 1];
/* Do the data processing. */
for (int i = 0; i < len; i++) {
shell_fprintf(sh, SHELL_INFO, "%02x ", data[i]);
}
shell_fprintf(sh, SHELL_INFO, "| ");
for (int i = 0; i < len; i++) {
shell_fprintf(sh, SHELL_INFO, "%c", data[i]);
}
shell_fprintf(sh, SHELL_INFO, "\n");
}
static int cmd_bypass(const struct shell *sh, size_t argc, char **argv)
{
return set_bypass(sh, bypass_cb);
}
static int cmd_dict(const struct shell *sh, size_t argc, char **argv,
void *data)
{
int val = (intptr_t)data;
shell_print(sh, "(syntax, value) : (%s, %d)", argv[0], val);
return 0;
}
SHELL_SUBCMD_DICT_SET_CREATE(sub_dict_cmds, cmd_dict,
(value_0, 0, "value 0"), (value_1, 1, "value 1"),
(value_2, 2, "value 2"), (value_3, 3, "value 3")
);
SHELL_STATIC_SUBCMD_SET_CREATE(sub_demo,
SHELL_CMD(dictionary, &sub_dict_cmds, "Dictionary commands", NULL),
SHELL_CMD(hexdump, NULL, "Hexdump params command.", cmd_demo_hexdump),
SHELL_CMD(params, NULL, "Print params command.", cmd_demo_params),
SHELL_CMD(ping, NULL, "Ping command.", cmd_demo_ping),
SHELL_CMD(board, NULL, "Show board name command.", cmd_demo_board),
#if defined CONFIG_SHELL_GETOPT
SHELL_CMD(getopt_thread_safe, NULL,
"Cammand using getopt in thread safe way"
" looking for: \"abhc:\".",
cmd_demo_getopt_ts),
SHELL_CMD(getopt, NULL, "Cammand using getopt in non thread safe way"
" looking for: \"abhc:\".\n", cmd_demo_getopt),
#endif
SHELL_SUBCMD_SET_END /* Array terminated. */
);
SHELL_CMD_REGISTER(demo, &sub_demo, "Demo commands", NULL);
SHELL_CMD_ARG_REGISTER(version, NULL, "Show kernel version", cmd_version, 1, 0);
SHELL_CMD_ARG_REGISTER(bypass, NULL, "Bypass shell", cmd_bypass, 1, 0);
/* Create a set of commands. Commands to this set are added using @ref SHELL_SUBCMD_ADD
* and @ref SHELL_SUBCMD_COND_ADD.
*/
SHELL_SUBCMD_SET_CREATE(sub_section_cmd, (section_cmd));
static int cmd1_handler(const struct shell *sh, size_t argc, char **argv)
{
ARG_UNUSED(sh);
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_print(sh, "cmd1 executed");
return 0;
}
/* Create a set of subcommands for "section_cmd cm1". */
SHELL_SUBCMD_SET_CREATE(sub_section_cmd1, (section_cmd, cmd1));
/* Add command to the set. Subcommand set is identify by parent shell command. */
SHELL_SUBCMD_ADD((section_cmd), cmd1, &sub_section_cmd1, "help for cmd1", cmd1_handler, 1, 0);
SHELL_CMD_REGISTER(section_cmd, &sub_section_cmd,
"Demo command using section for subcommand registration", NULL);
int main(void)
{
#if DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_shell_uart), zephyr_cdc_acm_uart)
const struct device *dev;
uint32_t dtr = 0;
dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_shell_uart));
if (!device_is_ready(dev) || usb_enable(NULL)) {
return 0;
}
while (!dtr) {
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
k_sleep(K_MSEC(100));
}
#endif
return 0;
}