1 /*
2 * Copyright (c) 2017 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_bt_shell, CONFIG_NET_L2_BT_LOG_LEVEL);
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/toolchain.h>
12 #include <zephyr/linker/sections.h>
13 #include <string.h>
14 #include <errno.h>
15
16 #include <zephyr/shell/shell.h>
17 #include <zephyr/sys/printk.h>
18
19 #include <zephyr/net/net_core.h>
20 #include <zephyr/net/net_l2.h>
21 #include <zephyr/net/net_if.h>
22 #include <zephyr/net/bt.h>
23
24 #include <zephyr/bluetooth/bluetooth.h>
25 #include <zephyr/bluetooth/hci.h>
26
shell_cmd_connect(const struct shell * sh,size_t argc,char * argv[])27 static int shell_cmd_connect(const struct shell *sh,
28 size_t argc, char *argv[])
29 {
30 int err;
31 bt_addr_le_t addr;
32 struct net_if *iface = net_if_get_default();
33
34 if (argc < 3) {
35 shell_help(sh);
36 return -ENOEXEC;
37 }
38
39 err = bt_addr_le_from_str(argv[1], argv[2], &addr);
40 if (err) {
41 shell_fprintf(sh, SHELL_WARNING,
42 "Invalid peer address (err %d)\n", err);
43 return 0;
44 }
45
46 if (net_mgmt(NET_REQUEST_BT_CONNECT, iface, &addr, sizeof(addr))) {
47 shell_fprintf(sh, SHELL_WARNING,
48 "Connection failed\n");
49 } else {
50 shell_fprintf(sh, SHELL_NORMAL,
51 "Connection pending\n");
52 }
53
54 return 0;
55 }
56
shell_cmd_scan(const struct shell * sh,size_t argc,char * argv[])57 static int shell_cmd_scan(const struct shell *sh,
58 size_t argc, char *argv[])
59 {
60 struct net_if *iface = net_if_get_default();
61
62 if (argc < 2) {
63 shell_help(sh);
64 return -ENOEXEC;
65 }
66
67 if (net_mgmt(NET_REQUEST_BT_SCAN, iface, argv[1], strlen(argv[1]))) {
68 shell_fprintf(sh, SHELL_WARNING,
69 "Scan failed\n");
70 } else {
71 shell_fprintf(sh, SHELL_NORMAL,
72 "Scan in progress\n");
73 }
74
75 return 0;
76 }
77
shell_cmd_disconnect(const struct shell * sh,size_t argc,char * argv[])78 static int shell_cmd_disconnect(const struct shell *sh,
79 size_t argc, char *argv[])
80 {
81 struct net_if *iface = net_if_get_default();
82
83 if (net_mgmt(NET_REQUEST_BT_DISCONNECT, iface, NULL, 0)) {
84 shell_fprintf(sh, SHELL_WARNING,
85 "Disconnect failed\n");
86 } else {
87 shell_fprintf(sh, SHELL_NORMAL,
88 "Disconnected\n");
89 }
90
91 return 0;
92 }
93
shell_cmd_advertise(const struct shell * sh,size_t argc,char * argv[])94 static int shell_cmd_advertise(const struct shell *sh,
95 size_t argc, char *argv[])
96 {
97 struct net_if *iface = net_if_get_default();
98
99 if (argc < 2) {
100 shell_help(sh);
101 return -ENOEXEC;
102 }
103
104 if (net_mgmt(NET_REQUEST_BT_ADVERTISE, iface, argv[1],
105 strlen(argv[1]))) {
106 shell_fprintf(sh, SHELL_WARNING,
107 "Advertise failed\n");
108 } else {
109 shell_fprintf(sh, SHELL_NORMAL,
110 "Advertise in progress\n");
111 }
112
113 return 0;
114 }
115
116 SHELL_STATIC_SUBCMD_SET_CREATE(bt_commands,
117 SHELL_CMD(advertise, NULL,
118 "on/off",
119 shell_cmd_advertise),
120 SHELL_CMD(connect, NULL,
121 "<address: XX:XX:XX:XX:XX:XX> <type: (public|random)>",
122 shell_cmd_connect),
123 SHELL_CMD(scan, NULL,
124 "<on/off/active/passive>",
125 shell_cmd_scan),
126 SHELL_CMD(disconnect, NULL,
127 "",
128 shell_cmd_disconnect),
129 SHELL_SUBCMD_SET_END
130 );
131
132 SHELL_CMD_REGISTER(net_bt, &bt_commands, "Net Bluetooth commands", NULL);
133
net_bt_shell_init(void)134 void net_bt_shell_init(void)
135 {
136 }
137