1 /*- 2 * link_policy.c 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 17 * 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 28 * SUCH DAMAGE. 29 * 30 * $Id: link_policy.c,v 1.3 2003/08/18 19:19:54 max Exp $ 31 * $FreeBSD$ 32 */ 33 34 #define L2CAP_SOCKET_CHECKED 35 #include <bluetooth.h> 36 #include <errno.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include "hccontrol.h" 40 41 /* Send Role Discovery to the unit */ 42 static int 43 hci_role_discovery(int s, int argc, char **argv) 44 { 45 ng_hci_role_discovery_cp cp; 46 ng_hci_role_discovery_rp rp; 47 int n; 48 49 /* parse command parameters */ 50 switch (argc) { 51 case 1: 52 /* connection handle */ 53 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 54 return (USAGE); 55 56 cp.con_handle = (uint16_t) (n & 0x0fff); 57 cp.con_handle = htole16(cp.con_handle); 58 break; 59 60 default: 61 return (USAGE); 62 } 63 64 /* send request */ 65 n = sizeof(rp); 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) 70 return (ERROR); 71 72 if (rp.status != 0x00) { 73 fprintf(stdout, "Status: %s [%#02x]\n", 74 hci_status2str(rp.status), rp.status); 75 return (FAILED); 76 } 77 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); 81 82 return (OK); 83 } /* hci_role_discovery */ 84 85 /* Send Switch Role to the unit */ 86 static int 87 hci_switch_role(int s, int argc, char **argv) 88 { 89 int n0; 90 char b[512]; 91 ng_hci_switch_role_cp cp; 92 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 93 94 /* parse command parameters */ 95 switch (argc) { 96 case 2: 97 /* bdaddr */ 98 if (!bt_aton(argv[0], &cp.bdaddr)) { 99 struct hostent *he = NULL; 100 101 if ((he = bt_gethostbyname(argv[0])) == NULL) 102 return (USAGE); 103 104 memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr)); 105 } 106 107 /* role */ 108 if (sscanf(argv[1], "%d", &n0) != 1) 109 return (USAGE); 110 111 cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER; 112 break; 113 114 default: 115 return (USAGE); 116 } 117 118 /* send request and expect status response */ 119 n0 = sizeof(b); 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) 123 return (ERROR); 124 125 if (*b != 0x00) 126 return (FAILED); 127 128 /* wait for event */ 129 again: 130 n0 = sizeof(b); 131 if (hci_recv(s, b, &n0) == ERROR) 132 return (ERROR); 133 if (n0 < sizeof(*e)) { 134 errno = EIO; 135 return (ERROR); 136 } 137 138 if (e->event == NG_HCI_EVENT_ROLE_CHANGE) { 139 ng_hci_role_change_ep *ep = (ng_hci_role_change_ep *)(e + 1); 140 141 if (ep->status != 0x00) { 142 fprintf(stdout, "Status: %s [%#02x]\n", 143 hci_status2str(ep->status), ep->status); 144 return (FAILED); 145 } 146 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", 150 ep->role); 151 } else 152 goto again; 153 154 return (OK); 155 } /* hci_switch_role */ 156 157 /* Send Read_Link_Policy_Settings command to the unit */ 158 static int 159 hci_read_link_policy_settings(int s, int argc, char **argv) 160 { 161 ng_hci_read_link_policy_settings_cp cp; 162 ng_hci_read_link_policy_settings_rp rp; 163 int n; 164 165 /* parse command parameters */ 166 switch (argc) { 167 case 1: 168 /* connection handle */ 169 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 170 return (USAGE); 171 172 cp.con_handle = (uint16_t) (n & 0x0fff); 173 cp.con_handle = htole16(cp.con_handle); 174 break; 175 176 default: 177 return (USAGE); 178 } 179 180 /* send request */ 181 n = sizeof(rp); 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) 186 return (ERROR); 187 188 if (rp.status != 0x00) { 189 fprintf(stdout, "Status: %s [%#02x]\n", 190 hci_status2str(rp.status), rp.status); 191 return (FAILED); 192 } 193 194 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle)); 195 fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings)); 196 197 return (OK); 198 } /* hci_read_link_policy_settings */ 199 200 /* Send Write_Link_Policy_Settings command to the unit */ 201 static int 202 hci_write_link_policy_settings(int s, int argc, char **argv) 203 { 204 ng_hci_write_link_policy_settings_cp cp; 205 ng_hci_write_link_policy_settings_rp rp; 206 int n; 207 208 /* parse command parameters */ 209 switch (argc) { 210 case 2: 211 /* connection handle */ 212 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 213 return (USAGE); 214 215 cp.con_handle = (uint16_t) (n & 0x0fff); 216 cp.con_handle = htole16(cp.con_handle); 217 218 /* link policy settings */ 219 if (sscanf(argv[1], "%x", &n) != 1) 220 return (USAGE); 221 222 cp.settings = (uint16_t) (n & 0x0ffff); 223 cp.settings = htole16(cp.settings); 224 break; 225 226 default: 227 return (USAGE); 228 } 229 230 /* send request */ 231 n = sizeof(rp); 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) 236 return (ERROR); 237 238 if (rp.status != 0x00) { 239 fprintf(stdout, "Status: %s [%#02x]\n", 240 hci_status2str(rp.status), rp.status); 241 return (FAILED); 242 } 243 244 return (OK); 245 } /* hci_write_link_policy_settings */ 246 247 struct hci_command link_policy_commands[] = { 248 { 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", 254 &hci_role_discovery 255 }, 256 { 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", 267 &hci_switch_role 268 }, 269 { 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 282 }, 283 { 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 304 }, 305 { 306 NULL, 307 }}; 308 309