link_policy.c revision 281210
1/*
2 * link_policy.c
3 *
4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Id: link_policy.c,v 1.3 2003/08/18 19:19:54 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/hccontrol/link_policy.c 281210 2015-04-07 16:48:23Z takawata $
30 */
31
32#define L2CAP_SOCKET_CHECKED
33#include <bluetooth.h>
34#include <errno.h>
35#include <stdio.h>
36#include <string.h>
37#include "hccontrol.h"
38
39/* Send Role Discovery to the unit */
40static int
41hci_role_discovery(int s, int argc, char **argv)
42{
43	ng_hci_role_discovery_cp	cp;
44	ng_hci_role_discovery_rp	rp;
45	int				n;
46
47	/* parse command parameters */
48	switch (argc) {
49	case 1:
50		/* connection handle */
51		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
52			return (USAGE);
53
54		cp.con_handle = (uint16_t) (n & 0x0fff);
55		cp.con_handle = htole16(cp.con_handle);
56		break;
57
58	default:
59		return (USAGE);
60	}
61
62	/* send request */
63	n = sizeof(rp);
64	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
65			NG_HCI_OCF_ROLE_DISCOVERY),
66			(char const *) &cp, sizeof(cp),
67			(char *) &rp, &n) == ERROR)
68		return (ERROR);
69
70	if (rp.status != 0x00) {
71		fprintf(stdout, "Status: %s [%#02x]\n",
72			hci_status2str(rp.status), rp.status);
73		return (FAILED);
74	}
75
76	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
77	fprintf(stdout, "Role: %s [%#x]\n",
78		(rp.role == NG_HCI_ROLE_MASTER)? "Master" : "Slave", rp.role);
79
80	return (OK);
81} /* hci_role_discovery */
82
83/* Send Swith Role to the unit */
84static int
85hci_switch_role(int s, int argc, char **argv)
86{
87	int			 n0;
88	char			 b[512];
89	ng_hci_switch_role_cp	 cp;
90	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
91
92	/* parse command parameters */
93	switch (argc) {
94	case 2:
95		/* bdaddr */
96		if (!bt_aton(argv[0], &cp.bdaddr)) {
97			struct hostent	*he = NULL;
98
99			if ((he = bt_gethostbyname(argv[0])) == NULL)
100				return (USAGE);
101
102			memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
103		}
104
105		/* role */
106		if (sscanf(argv[1], "%d", &n0) != 1)
107			return (USAGE);
108
109		cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
110		break;
111
112	default:
113		return (USAGE);
114	}
115
116	/* send request and expect status response */
117	n0 = sizeof(b);
118	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
119			NG_HCI_OCF_SWITCH_ROLE),
120			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
121		return (ERROR);
122
123	if (*b != 0x00)
124		return (FAILED);
125
126	/* wait for event */
127again:
128	n0 = sizeof(b);
129	if (hci_recv(s, b, &n0) == ERROR)
130		return (ERROR);
131	if (n0 < sizeof(*e)) {
132		errno = EIO;
133		return (ERROR);
134	}
135
136	if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
137		ng_hci_role_change_ep	*ep = (ng_hci_role_change_ep *)(e + 1);
138
139		if (ep->status != 0x00) {
140			fprintf(stdout, "Status: %s [%#02x]\n",
141				hci_status2str(ep->status), ep->status);
142			return (FAILED);
143		}
144
145		fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr));
146		fprintf(stdout, "Role: %s [%#x]\n",
147			(ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave",
148			ep->role);
149	} else
150		goto again;
151
152	return (OK);
153} /* hci_switch_role */
154
155/* Send Read_Link_Policy_Settings command to the unit */
156static int
157hci_read_link_policy_settings(int s, int argc, char **argv)
158{
159	ng_hci_read_link_policy_settings_cp	cp;
160	ng_hci_read_link_policy_settings_rp	rp;
161	int					n;
162
163	/* parse command parameters */
164	switch (argc) {
165	case 1:
166		/* connection handle */
167		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
168			return (USAGE);
169
170		cp.con_handle = (uint16_t) (n & 0x0fff);
171		cp.con_handle = htole16(cp.con_handle);
172		break;
173
174	default:
175		return (USAGE);
176	}
177
178	/* send request */
179	n = sizeof(rp);
180	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
181			NG_HCI_OCF_READ_LINK_POLICY_SETTINGS),
182			(char const *) &cp, sizeof(cp),
183			(char *) &rp, &n) == ERROR)
184		return (ERROR);
185
186	if (rp.status != 0x00) {
187		fprintf(stdout, "Status: %s [%#02x]\n",
188			hci_status2str(rp.status), rp.status);
189		return (FAILED);
190	}
191
192	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
193	fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings));
194
195	return (OK);
196} /* hci_read_link_policy_settings */
197
198/* Send Write_Link_Policy_Settings command to the unit */
199static int
200hci_write_link_policy_settings(int s, int argc, char **argv)
201{
202	ng_hci_write_link_policy_settings_cp	cp;
203	ng_hci_write_link_policy_settings_rp	rp;
204	int					n;
205
206	/* parse command parameters */
207	switch (argc) {
208	case 2:
209		/* connection handle */
210		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
211			return (USAGE);
212
213		cp.con_handle = (uint16_t) (n & 0x0fff);
214		cp.con_handle = htole16(cp.con_handle);
215
216		/* link policy settings */
217		if (sscanf(argv[1], "%x", &n) != 1)
218			return (USAGE);
219
220		cp.settings = (uint16_t) (n & 0x0ffff);
221		cp.settings = htole16(cp.settings);
222		break;
223
224	default:
225		return (USAGE);
226	}
227
228	/* send request */
229	n = sizeof(rp);
230	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
231			NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS),
232			(char const *) &cp, sizeof(cp),
233			(char *) &rp, &n) == ERROR)
234		return (ERROR);
235
236	if (rp.status != 0x00) {
237		fprintf(stdout, "Status: %s [%#02x]\n",
238			hci_status2str(rp.status), rp.status);
239		return (FAILED);
240	}
241
242	return (OK);
243} /* hci_write_link_policy_settings */
244
245struct hci_command	link_policy_commands[] = {
246{
247"role_discovery <connection_handle>",
248"\nThe Role_Discovery command is used for a Bluetooth device to determine\n" \
249"which role the device is performing for a particular Connection Handle.\n" \
250"The connection handle must be a connection handle for an ACL connection.\n\n" \
251"\t<connection_handle> - dddd; connection handle",
252&hci_role_discovery
253},
254{
255"switch_role <BD_ADDR> <role>",
256"\nThe Switch_Role command is used for a Bluetooth device to switch the\n" \
257"current role the device is performing for a particular connection with\n" \
258"another specified Bluetooth device. The BD_ADDR command parameter indicates\n"\
259"for which connection the role switch is to be performed. The Role indicates\n"\
260"the requested new role that the local device performs. Note: the BD_ADDR\n" \
261"command parameter must specify a Bluetooth device for which a connection\n"
262"already exists.\n\n" \
263"\t<BD_ADDR> - xx:xx:xx:xx:xx:xx BD_ADDR or name\n" \
264"\t<role>    - dd; role; 0 - Master, 1 - Slave",
265&hci_switch_role
266},
267{
268"read_link_policy_settings <connection_handle>",
269"\nThis command will read the Link Policy setting for the specified connection\n"\
270"handle. The link policy settings parameter determines the behavior of the\n" \
271"local Link Manager when it receives a request from a remote device or it\n" \
272"determines itself to change the master-slave role or to enter the hold,\n" \
273"sniff, or park mode. The local Link Manager will automatically accept or\n" \
274"reject such a request from the remote device, and may even autonomously\n" \
275"request itself, depending on the value of the link policy settings parameter\n"\
276"for the corresponding connection handle. The connection handle must be a\n" \
277"connection handle for an ACL connection.\n\n" \
278"\t<connection_handle> - dddd; connection handle",
279&hci_read_link_policy_settings
280},
281{
282"write_link_policy_settings <connection_handle> <settings>",
283"\nThis command will write the Link Policy setting for the specified connection\n"\
284"handle. The link policy settings parameter determines the behavior of the\n" \
285"local Link Manager when it receives a request from a remote device or it\n" \
286"determines itself to change the master-slave role or to enter the hold,\n" \
287"sniff, or park mode. The local Link Manager will automatically accept or\n" \
288"reject such a request from the remote device, and may even autonomously\n" \
289"request itself, depending on the value of the link policy settings parameter\n"\
290"for the corresponding connection handle. The connection handle must be a\n" \
291"connection handle for an ACL connection. Multiple Link Manager policies may\n"\
292"be specified for the link policy settings parameter by performing a bitwise\n"\
293"OR operation of the different activity types.\n\n" \
294"\t<connection_handle> - dddd; connection handle\n" \
295"\t<settings>          - xxxx; settings\n" \
296"\t\t0x0000 - Disable All LM Modes (Default)\n" \
297"\t\t0x0001 - Enable Master Slave Switch\n" \
298"\t\t0x0002 - Enable Hold Mode\n" \
299"\t\t0x0004 - Enable Sniff Mode\n" \
300"\t\t0x0008 - Enable Park Mode\n",
301&hci_write_link_policy_settings
302},
303{
304NULL,
305}};
306
307