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 #define L2CAP_SOCKET_CHECKED
33 #include <bluetooth.h>
37 #include "hccontrol.h"
39 /* Send Role Discovery to the unit */
41 hci_role_discovery(int s, int argc, char **argv)
43 ng_hci_role_discovery_cp cp;
44 ng_hci_role_discovery_rp rp;
47 /* parse command parameters */
50 /* connection handle */
51 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
54 cp.con_handle = (uint16_t) (n & 0x0fff);
55 cp.con_handle = htole16(cp.con_handle);
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)
70 if (rp.status != 0x00) {
71 fprintf(stdout, "Status: %s [%#02x]\n",
72 hci_status2str(rp.status), rp.status);
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);
81 } /* hci_role_discovery */
83 /* Send Swith Role to the unit */
85 hci_switch_role(int s, int argc, char **argv)
89 ng_hci_switch_role_cp cp;
90 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b;
92 /* parse command parameters */
96 if (!bt_aton(argv[0], &cp.bdaddr)) {
97 struct hostent *he = NULL;
99 if ((he = bt_gethostbyname(argv[0])) == NULL)
102 memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
106 if (sscanf(argv[1], "%d", &n0) != 1)
109 cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
116 /* send request and expect status response */
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)
129 if (hci_recv(s, b, &n0) == ERROR)
131 if (n0 < sizeof(*e)) {
136 if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
137 ng_hci_role_change_ep *ep = (ng_hci_role_change_ep *)(e + 1);
139 if (ep->status != 0x00) {
140 fprintf(stdout, "Status: %s [%#02x]\n",
141 hci_status2str(ep->status), ep->status);
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",
153 } /* hci_switch_role */
155 /* Send Read_Link_Policy_Settings command to the unit */
157 hci_read_link_policy_settings(int s, int argc, char **argv)
159 ng_hci_read_link_policy_settings_cp cp;
160 ng_hci_read_link_policy_settings_rp rp;
163 /* parse command parameters */
166 /* connection handle */
167 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
170 cp.con_handle = (uint16_t) (n & 0x0fff);
171 cp.con_handle = htole16(cp.con_handle);
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)
186 if (rp.status != 0x00) {
187 fprintf(stdout, "Status: %s [%#02x]\n",
188 hci_status2str(rp.status), rp.status);
192 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
193 fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings));
196 } /* hci_read_link_policy_settings */
198 /* Send Write_Link_Policy_Settings command to the unit */
200 hci_write_link_policy_settings(int s, int argc, char **argv)
202 ng_hci_write_link_policy_settings_cp cp;
203 ng_hci_write_link_policy_settings_rp rp;
206 /* parse command parameters */
209 /* connection handle */
210 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
213 cp.con_handle = (uint16_t) (n & 0x0fff);
214 cp.con_handle = htole16(cp.con_handle);
216 /* link policy settings */
217 if (sscanf(argv[1], "%x", &n) != 1)
220 cp.settings = (uint16_t) (n & 0x0ffff);
221 cp.settings = htole16(cp.settings);
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)
236 if (rp.status != 0x00) {
237 fprintf(stdout, "Status: %s [%#02x]\n",
238 hci_status2str(rp.status), rp.status);
243 } /* hci_write_link_policy_settings */
245 struct hci_command link_policy_commands[] = {
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",
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",
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
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