1 /** @file
2  * @brief Bluetooth RFCOMM shell module
3  *
4  * Provide some Bluetooth shell commands that can be useful to applications.
5  */
6 
7 /*
8  * Copyright (c) 2018 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #include <errno.h>
14 #include <zephyr/types.h>
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <zephyr/sys/byteorder.h>
19 #include <zephyr/kernel.h>
20 
21 #include <zephyr/settings/settings.h>
22 
23 #include <zephyr/bluetooth/hci.h>
24 #include <zephyr/bluetooth/bluetooth.h>
25 #include <zephyr/bluetooth/conn.h>
26 #include <zephyr/bluetooth/l2cap.h>
27 #include <zephyr/bluetooth/classic/rfcomm.h>
28 #include <zephyr/bluetooth/classic/sdp.h>
29 
30 #include <zephyr/shell/shell.h>
31 
32 #include "host/shell/bt.h"
33 
34 #define DATA_MTU 48
35 
36 NET_BUF_POOL_FIXED_DEFINE(pool, 1, DATA_MTU, CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
37 
38 static struct bt_sdp_attribute spp_attrs[] = {
39 	BT_SDP_NEW_SERVICE,
40 	BT_SDP_LIST(
41 		BT_SDP_ATTR_SVCLASS_ID_LIST,
42 		BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 3),
43 		BT_SDP_DATA_ELEM_LIST(
44 		{
45 			BT_SDP_TYPE_SIZE(BT_SDP_UUID16),
46 			BT_SDP_ARRAY_16(BT_SDP_SERIAL_PORT_SVCLASS)
47 		},
48 		)
49 	),
50 	BT_SDP_LIST(
51 		BT_SDP_ATTR_PROTO_DESC_LIST,
52 		BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 12),
53 		BT_SDP_DATA_ELEM_LIST(
54 		{
55 			BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 3),
56 			BT_SDP_DATA_ELEM_LIST(
57 			{
58 				BT_SDP_TYPE_SIZE(BT_SDP_UUID16),
59 				BT_SDP_ARRAY_16(BT_SDP_PROTO_L2CAP)
60 			},
61 			)
62 		},
63 		{
64 			BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 5),
65 			BT_SDP_DATA_ELEM_LIST(
66 			{
67 				BT_SDP_TYPE_SIZE(BT_SDP_UUID16),
68 				BT_SDP_ARRAY_16(BT_SDP_PROTO_RFCOMM)
69 			},
70 			{
71 				BT_SDP_TYPE_SIZE(BT_SDP_UINT8),
72 				BT_SDP_ARRAY_8(BT_RFCOMM_CHAN_SPP)
73 			},
74 			)
75 		},
76 		)
77 	),
78 	BT_SDP_LIST(
79 		BT_SDP_ATTR_PROFILE_DESC_LIST,
80 		BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 8),
81 		BT_SDP_DATA_ELEM_LIST(
82 		{
83 			BT_SDP_TYPE_SIZE_VAR(BT_SDP_SEQ8, 6),
84 			BT_SDP_DATA_ELEM_LIST(
85 			{
86 				BT_SDP_TYPE_SIZE(BT_SDP_UUID16),
87 				BT_SDP_ARRAY_16(BT_SDP_SERIAL_PORT_SVCLASS)
88 			},
89 			{
90 				BT_SDP_TYPE_SIZE(BT_SDP_UINT16),
91 				BT_SDP_ARRAY_16(0x0102)
92 			},
93 			)
94 		},
95 		)
96 	),
97 	BT_SDP_SERVICE_NAME("Serial Port"),
98 };
99 
100 static struct bt_sdp_record spp_rec = BT_SDP_RECORD(spp_attrs);
101 
rfcomm_recv(struct bt_rfcomm_dlc * dlci,struct net_buf * buf)102 static void rfcomm_recv(struct bt_rfcomm_dlc *dlci, struct net_buf *buf)
103 {
104 	shell_print(ctx_shell, "Incoming data dlc %p len %u", dlci, buf->len);
105 }
106 
rfcomm_connected(struct bt_rfcomm_dlc * dlci)107 static void rfcomm_connected(struct bt_rfcomm_dlc *dlci)
108 {
109 	shell_print(ctx_shell, "Dlc %p connected", dlci);
110 }
111 
rfcomm_disconnected(struct bt_rfcomm_dlc * dlci)112 static void rfcomm_disconnected(struct bt_rfcomm_dlc *dlci)
113 {
114 	shell_print(ctx_shell, "Dlc %p disconnected", dlci);
115 }
116 
117 static struct bt_rfcomm_dlc_ops rfcomm_ops = {
118 	.recv		= rfcomm_recv,
119 	.connected	= rfcomm_connected,
120 	.disconnected	= rfcomm_disconnected,
121 };
122 
123 static struct bt_rfcomm_dlc rfcomm_dlc = {
124 	.ops = &rfcomm_ops,
125 	.mtu = 30,
126 };
127 
rfcomm_accept(struct bt_conn * conn,struct bt_rfcomm_dlc ** dlc)128 static int rfcomm_accept(struct bt_conn *conn, struct bt_rfcomm_dlc **dlc)
129 {
130 	shell_print(ctx_shell, "Incoming RFCOMM conn %p", conn);
131 
132 	if (rfcomm_dlc.session) {
133 		shell_error(ctx_shell, "No channels available");
134 		return -ENOMEM;
135 	}
136 
137 	*dlc = &rfcomm_dlc;
138 
139 	return 0;
140 }
141 
142 struct bt_rfcomm_server rfcomm_server = {
143 	.accept = &rfcomm_accept,
144 };
145 
cmd_register(const struct shell * sh,size_t argc,char * argv[])146 static int cmd_register(const struct shell *sh, size_t argc, char *argv[])
147 {
148 	int ret;
149 
150 	if (rfcomm_server.channel) {
151 		shell_error(sh, "Already registered");
152 		return -ENOEXEC;
153 	}
154 
155 	rfcomm_server.channel = BT_RFCOMM_CHAN_SPP;
156 
157 	ret = bt_rfcomm_server_register(&rfcomm_server);
158 	if (ret < 0) {
159 		shell_error(sh, "Unable to register channel %x", ret);
160 		rfcomm_server.channel = 0U;
161 		return -ENOEXEC;
162 	} else {
163 		shell_print(sh, "RFCOMM channel %u registered",
164 			    rfcomm_server.channel);
165 		bt_sdp_register_service(&spp_rec);
166 	}
167 
168 	return 0;
169 }
170 
cmd_connect(const struct shell * sh,size_t argc,char * argv[])171 static int cmd_connect(const struct shell *sh, size_t argc, char *argv[])
172 {
173 	uint8_t channel;
174 	int err;
175 
176 	if (!default_conn) {
177 		shell_error(sh, "Not connected");
178 		return -ENOEXEC;
179 	}
180 
181 	channel = strtoul(argv[1], NULL, 16);
182 
183 	err = bt_rfcomm_dlc_connect(default_conn, &rfcomm_dlc, channel);
184 	if (err < 0) {
185 		shell_error(sh, "Unable to connect to channel %d (err %u)",
186 			    channel, err);
187 	} else {
188 		shell_print(sh, "RFCOMM connection pending");
189 	}
190 
191 	return err;
192 }
193 
cmd_send(const struct shell * sh,size_t argc,char * argv[])194 static int cmd_send(const struct shell *sh, size_t argc, char *argv[])
195 {
196 	uint8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
197 	int ret, len, count = 1;
198 	struct net_buf *buf;
199 
200 	if (argc > 1) {
201 		count = strtoul(argv[1], NULL, 10);
202 	}
203 
204 	while (count--) {
205 		buf = bt_rfcomm_create_pdu(&pool);
206 		/* Should reserve one byte in tail for FCS */
207 		len = MIN(rfcomm_dlc.mtu, net_buf_tailroom(buf) - 1);
208 
209 		net_buf_add_mem(buf, buf_data, len);
210 		ret = bt_rfcomm_dlc_send(&rfcomm_dlc, buf);
211 		if (ret < 0) {
212 			shell_error(sh, "Unable to send: %d", -ret);
213 			net_buf_unref(buf);
214 			return -ENOEXEC;
215 		}
216 	}
217 
218 	return 0;
219 }
220 
cmd_disconnect(const struct shell * sh,size_t argc,char * argv[])221 static int cmd_disconnect(const struct shell *sh, size_t argc, char *argv[])
222 {
223 	int err;
224 
225 	err = bt_rfcomm_dlc_disconnect(&rfcomm_dlc);
226 	if (err) {
227 		shell_error(sh, "Unable to disconnect: %u", -err);
228 	}
229 
230 	return err;
231 }
232 
233 #define HELP_NONE "[none]"
234 #define HELP_ADDR_LE "<address: XX:XX:XX:XX:XX:XX> <type: (public|random)>"
235 
236 SHELL_STATIC_SUBCMD_SET_CREATE(rfcomm_cmds,
237 	SHELL_CMD_ARG(register, NULL, HELP_NONE, cmd_register, 1, 0),
238 	SHELL_CMD_ARG(connect, NULL, "<channel>", cmd_connect, 2, 0),
239 	SHELL_CMD_ARG(disconnect, NULL, HELP_NONE, cmd_disconnect, 1, 0),
240 	SHELL_CMD_ARG(send, NULL, "<number of packets>", cmd_send, 2, 0),
241 	SHELL_SUBCMD_SET_END
242 );
243 
cmd_rfcomm(const struct shell * sh,size_t argc,char ** argv)244 static int cmd_rfcomm(const struct shell *sh, size_t argc, char **argv)
245 {
246 	if (argc == 1) {
247 		shell_help(sh);
248 		/* shell returns 1 when help is printed */
249 		return 1;
250 	}
251 
252 	shell_error(sh, "%s unknown parameter: %s", argv[0], argv[1]);
253 
254 	return -ENOEXEC;
255 }
256 
257 SHELL_CMD_ARG_REGISTER(rfcomm, &rfcomm_cmds, "Bluetooth RFCOMM shell commands",
258 		       cmd_rfcomm, 1, 1);
259