4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: link_policy.c,v 1.3 2003/08/18 19:19:54 max Exp $
34 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
39 #include "hccontrol.h"
41 /* Send Role Discovery to the unit */
43 hci_role_discovery(int s, int argc, char **argv)
45 ng_hci_role_discovery_cp cp;
46 ng_hci_role_discovery_rp rp;
49 /* parse command parameters */
52 /* connection handle */
53 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
56 cp.con_handle = (uint16_t) (n & 0x0fff);
57 cp.con_handle = htole16(cp.con_handle);
66 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
67 NG_HCI_OCF_ROLE_DISCOVERY),
68 (char const *) &cp, sizeof(cp),
69 (char *) &rp, &n) == ERROR)
72 if (rp.status != 0x00) {
73 fprintf(stdout, "Status: %s [%#02x]\n",
74 hci_status2str(rp.status), rp.status);
78 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
79 fprintf(stdout, "Role: %s [%#x]\n",
80 (rp.role == NG_HCI_ROLE_MASTER)? "Master" : "Slave", rp.role);
83 } /* hci_role_discovery */
85 /* Send Swith Role to the unit */
87 hci_switch_role(int s, int argc, char **argv)
91 ng_hci_switch_role_cp cp;
92 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b;
94 /* parse command parameters */
98 if (!bt_aton(argv[0], &cp.bdaddr)) {
99 struct hostent *he = NULL;
101 if ((he = bt_gethostbyname(argv[0])) == NULL)
104 memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
108 if (sscanf(argv[1], "%d", &n0) != 1)
111 cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
118 /* send request and expect status response */
120 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
121 NG_HCI_OCF_SWITCH_ROLE),
122 (char const *) &cp, sizeof(cp), b, &n0) == ERROR)
131 if (hci_recv(s, b, &n0) == ERROR)
133 if (n0 < sizeof(*e)) {
138 if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
139 ng_hci_role_change_ep *ep = (ng_hci_role_change_ep *)(e + 1);
141 if (ep->status != 0x00) {
142 fprintf(stdout, "Status: %s [%#02x]\n",
143 hci_status2str(ep->status), ep->status);
147 fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr));
148 fprintf(stdout, "Role: %s [%#x]\n",
149 (ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave",
155 } /* hci_switch_role */
157 /* Send Read_Link_Policy_Settings command to the unit */
159 hci_read_link_policy_settings(int s, int argc, char **argv)
161 ng_hci_read_link_policy_settings_cp cp;
162 ng_hci_read_link_policy_settings_rp rp;
165 /* parse command parameters */
168 /* connection handle */
169 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
172 cp.con_handle = (uint16_t) (n & 0x0fff);
173 cp.con_handle = htole16(cp.con_handle);
182 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
183 NG_HCI_OCF_READ_LINK_POLICY_SETTINGS),
184 (char const *) &cp, sizeof(cp),
185 (char *) &rp, &n) == ERROR)
188 if (rp.status != 0x00) {
189 fprintf(stdout, "Status: %s [%#02x]\n",
190 hci_status2str(rp.status), rp.status);
194 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
195 fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings));
198 } /* hci_read_link_policy_settings */
200 /* Send Write_Link_Policy_Settings command to the unit */
202 hci_write_link_policy_settings(int s, int argc, char **argv)
204 ng_hci_write_link_policy_settings_cp cp;
205 ng_hci_write_link_policy_settings_rp rp;
208 /* parse command parameters */
211 /* connection handle */
212 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
215 cp.con_handle = (uint16_t) (n & 0x0fff);
216 cp.con_handle = htole16(cp.con_handle);
218 /* link policy settings */
219 if (sscanf(argv[1], "%x", &n) != 1)
222 cp.settings = (uint16_t) (n & 0x0ffff);
223 cp.settings = htole16(cp.settings);
232 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
233 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS),
234 (char const *) &cp, sizeof(cp),
235 (char *) &rp, &n) == ERROR)
238 if (rp.status != 0x00) {
239 fprintf(stdout, "Status: %s [%#02x]\n",
240 hci_status2str(rp.status), rp.status);
245 } /* hci_write_link_policy_settings */
247 struct hci_command link_policy_commands[] = {
249 "role_discovery <connection_handle>",
250 "\nThe Role_Discovery command is used for a Bluetooth device to determine\n" \
251 "which role the device is performing for a particular Connection Handle.\n" \
252 "The connection handle must be a connection handle for an ACL connection.\n\n" \
253 "\t<connection_handle> - dddd; connection handle",
257 "switch_role <BD_ADDR> <role>",
258 "\nThe Switch_Role command is used for a Bluetooth device to switch the\n" \
259 "current role the device is performing for a particular connection with\n" \
260 "another specified Bluetooth device. The BD_ADDR command parameter indicates\n"\
261 "for which connection the role switch is to be performed. The Role indicates\n"\
262 "the requested new role that the local device performs. Note: the BD_ADDR\n" \
263 "command parameter must specify a Bluetooth device for which a connection\n"
264 "already exists.\n\n" \
265 "\t<BD_ADDR> - xx:xx:xx:xx:xx:xx BD_ADDR or name\n" \
266 "\t<role> - dd; role; 0 - Master, 1 - Slave",
270 "read_link_policy_settings <connection_handle>",
271 "\nThis command will read the Link Policy setting for the specified connection\n"\
272 "handle. The link policy settings parameter determines the behavior of the\n" \
273 "local Link Manager when it receives a request from a remote device or it\n" \
274 "determines itself to change the master-slave role or to enter the hold,\n" \
275 "sniff, or park mode. The local Link Manager will automatically accept or\n" \
276 "reject such a request from the remote device, and may even autonomously\n" \
277 "request itself, depending on the value of the link policy settings parameter\n"\
278 "for the corresponding connection handle. The connection handle must be a\n" \
279 "connection handle for an ACL connection.\n\n" \
280 "\t<connection_handle> - dddd; connection handle",
281 &hci_read_link_policy_settings
284 "write_link_policy_settings <connection_handle> <settings>",
285 "\nThis command will write the Link Policy setting for the specified connection\n"\
286 "handle. The link policy settings parameter determines the behavior of the\n" \
287 "local Link Manager when it receives a request from a remote device or it\n" \
288 "determines itself to change the master-slave role or to enter the hold,\n" \
289 "sniff, or park mode. The local Link Manager will automatically accept or\n" \
290 "reject such a request from the remote device, and may even autonomously\n" \
291 "request itself, depending on the value of the link policy settings parameter\n"\
292 "for the corresponding connection handle. The connection handle must be a\n" \
293 "connection handle for an ACL connection. Multiple Link Manager policies may\n"\
294 "be specified for the link policy settings parameter by performing a bitwise\n"\
295 "OR operation of the different activity types.\n\n" \
296 "\t<connection_handle> - dddd; connection handle\n" \
297 "\t<settings> - xxxx; settings\n" \
298 "\t\t0x0000 - Disable All LM Modes (Default)\n" \
299 "\t\t0x0001 - Enable Master Slave Switch\n" \
300 "\t\t0x0002 - Enable Hold Mode\n" \
301 "\t\t0x0004 - Enable Sniff Mode\n" \
302 "\t\t0x0008 - Enable Park Mode\n",
303 &hci_write_link_policy_settings