1 /**
2 * Copyright (c) 2020 Linaro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/shell/shell.h>
9
10 #include "eswifi.h"
11
12 static struct eswifi_dev *eswifi;
13
eswifi_shell_register(struct eswifi_dev * dev)14 void eswifi_shell_register(struct eswifi_dev *dev)
15 {
16 /* only one instance supported */
17 if (eswifi) {
18 return;
19 }
20
21 eswifi = dev;
22 }
23
eswifi_shell_atcmd(const struct shell * sh,size_t argc,char ** argv)24 static int eswifi_shell_atcmd(const struct shell *sh, size_t argc,
25 char **argv)
26 {
27 int i;
28 size_t len = 0;
29
30 if (eswifi == NULL) {
31 shell_print(sh, "no eswifi device registered");
32 return -ENOEXEC;
33 }
34
35 if (argc < 2) {
36 shell_help(sh);
37 return -ENOEXEC;
38 }
39
40 eswifi_lock(eswifi);
41
42 memset(eswifi->buf, 0, sizeof(eswifi->buf));
43 for (i = 1; i < argc; i++) {
44 size_t argv_len = strlen(argv[i]);
45
46 if ((len + argv_len) >= sizeof(eswifi->buf) - 1) {
47 break;
48 }
49
50 memcpy(eswifi->buf + len, argv[i], argv_len);
51 len += argv_len;
52 }
53 eswifi->buf[len] = '\r';
54
55 shell_print(sh, "> %s", eswifi->buf);
56 eswifi_at_cmd(eswifi, eswifi->buf);
57 shell_print(sh, "< %s", eswifi->buf);
58
59 eswifi_unlock(eswifi);
60
61 return 0;
62 }
63
64 SHELL_STATIC_SUBCMD_SET_CREATE(eswifi_shell,
65 SHELL_CMD(atcmd, NULL, "<atcmd>", eswifi_shell_atcmd),
66 SHELL_SUBCMD_SET_END /* Array terminated. */
67 );
68
69 SHELL_CMD_REGISTER(eswifi, &eswifi_shell, "esWiFi debug shell", NULL);
70