4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: link_policy.c,v 1.3 2003/08/18 19:19:54 max Exp $
32 #include <bluetooth.h>
36 #include "hccontrol.h"
38 /* Send Role Discovery to the unit */
40 hci_role_discovery(int s, int argc, char **argv)
42 ng_hci_role_discovery_cp cp;
43 ng_hci_role_discovery_rp rp;
46 /* parse command parameters */
49 /* connection handle */
50 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
53 cp.con_handle = (uint16_t) (n & 0x0fff);
54 cp.con_handle = htole16(cp.con_handle);
63 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
64 NG_HCI_OCF_ROLE_DISCOVERY),
65 (char const *) &cp, sizeof(cp),
66 (char *) &rp, &n) == ERROR)
69 if (rp.status != 0x00) {
70 fprintf(stdout, "Status: %s [%#02x]\n",
71 hci_status2str(rp.status), rp.status);
75 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
76 fprintf(stdout, "Role: %s [%#x]\n",
77 (rp.role == NG_HCI_ROLE_MASTER)? "Master" : "Slave", rp.role);
80 } /* hci_role_discovery */
82 /* Send Swith Role to the unit */
84 hci_switch_role(int s, int argc, char **argv)
88 ng_hci_switch_role_cp cp;
89 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b;
91 /* parse command parameters */
95 if (!bt_aton(argv[0], &cp.bdaddr)) {
96 struct hostent *he = NULL;
98 if ((he = bt_gethostbyname(argv[0])) == NULL)
101 memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
105 if (sscanf(argv[1], "%d", &n0) != 1)
108 cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
115 /* send request and expect status response */
117 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
118 NG_HCI_OCF_SWITCH_ROLE),
119 (char const *) &cp, sizeof(cp), b, &n0) == ERROR)
128 if (hci_recv(s, b, &n0) == ERROR)
130 if (n0 < sizeof(*e)) {
135 if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
136 ng_hci_role_change_ep *ep = (ng_hci_role_change_ep *)(e + 1);
138 if (ep->status != 0x00) {
139 fprintf(stdout, "Status: %s [%#02x]\n",
140 hci_status2str(ep->status), ep->status);
144 fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr));
145 fprintf(stdout, "Role: %s [%#x]\n",
146 (ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave",
152 } /* hci_switch_role */
154 /* Send Read_Link_Policy_Settings command to the unit */
156 hci_read_link_policy_settings(int s, int argc, char **argv)
158 ng_hci_read_link_policy_settings_cp cp;
159 ng_hci_read_link_policy_settings_rp rp;
162 /* parse command parameters */
165 /* connection handle */
166 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
169 cp.con_handle = (uint16_t) (n & 0x0fff);
170 cp.con_handle = htole16(cp.con_handle);
179 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
180 NG_HCI_OCF_READ_LINK_POLICY_SETTINGS),
181 (char const *) &cp, sizeof(cp),
182 (char *) &rp, &n) == ERROR)
185 if (rp.status != 0x00) {
186 fprintf(stdout, "Status: %s [%#02x]\n",
187 hci_status2str(rp.status), rp.status);
191 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
192 fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings));
195 } /* hci_read_link_policy_settings */
197 /* Send Write_Link_Policy_Settings command to the unit */
199 hci_write_link_policy_settings(int s, int argc, char **argv)
201 ng_hci_write_link_policy_settings_cp cp;
202 ng_hci_write_link_policy_settings_rp rp;
205 /* parse command parameters */
208 /* connection handle */
209 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
212 cp.con_handle = (uint16_t) (n & 0x0fff);
213 cp.con_handle = htole16(cp.con_handle);
215 /* link policy settings */
216 if (sscanf(argv[1], "%x", &n) != 1)
219 cp.settings = (uint16_t) (n & 0x0ffff);
220 cp.settings = htole16(cp.settings);
229 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
230 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS),
231 (char const *) &cp, sizeof(cp),
232 (char *) &rp, &n) == ERROR)
235 if (rp.status != 0x00) {
236 fprintf(stdout, "Status: %s [%#02x]\n",
237 hci_status2str(rp.status), rp.status);
242 } /* hci_write_link_policy_settings */
244 struct hci_command link_policy_commands[] = {
246 "role_discovery <connection_handle>",
247 "\nThe Role_Discovery command is used for a Bluetooth device to determine\n" \
248 "which role the device is performing for a particular Connection Handle.\n" \
249 "The connection handle must be a connection handle for an ACL connection.\n\n" \
250 "\t<connection_handle> - dddd; connection handle",
254 "switch_role <BD_ADDR> <role>",
255 "\nThe Switch_Role command is used for a Bluetooth device to switch the\n" \
256 "current role the device is performing for a particular connection with\n" \
257 "another specified Bluetooth device. The BD_ADDR command parameter indicates\n"\
258 "for which connection the role switch is to be performed. The Role indicates\n"\
259 "the requested new role that the local device performs. Note: the BD_ADDR\n" \
260 "command parameter must specify a Bluetooth device for which a connection\n"
261 "already exists.\n\n" \
262 "\t<BD_ADDR> - xx:xx:xx:xx:xx:xx BD_ADDR or name\n" \
263 "\t<role> - dd; role; 0 - Master, 1 - Slave",
267 "read_link_policy_settings <connection_handle>",
268 "\nThis command will read the Link Policy setting for the specified connection\n"\
269 "handle. The link policy settings parameter determines the behavior of the\n" \
270 "local Link Manager when it receives a request from a remote device or it\n" \
271 "determines itself to change the master-slave role or to enter the hold,\n" \
272 "sniff, or park mode. The local Link Manager will automatically accept or\n" \
273 "reject such a request from the remote device, and may even autonomously\n" \
274 "request itself, depending on the value of the link policy settings parameter\n"\
275 "for the corresponding connection handle. The connection handle must be a\n" \
276 "connection handle for an ACL connection.\n\n" \
277 "\t<connection_handle> - dddd; connection handle",
278 &hci_read_link_policy_settings
281 "write_link_policy_settings <connection_handle> <settings>",
282 "\nThis command will write the Link Policy setting for the specified connection\n"\
283 "handle. The link policy settings parameter determines the behavior of the\n" \
284 "local Link Manager when it receives a request from a remote device or it\n" \
285 "determines itself to change the master-slave role or to enter the hold,\n" \
286 "sniff, or park mode. The local Link Manager will automatically accept or\n" \
287 "reject such a request from the remote device, and may even autonomously\n" \
288 "request itself, depending on the value of the link policy settings parameter\n"\
289 "for the corresponding connection handle. The connection handle must be a\n" \
290 "connection handle for an ACL connection. Multiple Link Manager policies may\n"\
291 "be specified for the link policy settings parameter by performing a bitwise\n"\
292 "OR operation of the different activity types.\n\n" \
293 "\t<connection_handle> - dddd; connection handle\n" \
294 "\t<settings> - xxxx; settings\n" \
295 "\t\t0x0000 - Disable All LM Modes (Default)\n" \
296 "\t\t0x0001 - Enable Master Slave Switch\n" \
297 "\t\t0x0002 - Enable Hold Mode\n" \
298 "\t\t0x0004 - Enable Sniff Mode\n" \
299 "\t\t0x0008 - Enable Park Mode\n",
300 &hci_write_link_policy_settings