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 2002/09/17 16:33:44 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/endian.h> 34 #include <errno.h> 35 #include <ng_hci.h> 36 #include <stdio.h> 37 #include "hccontrol.h" 38 39 /* Send Role Discovery to the unit */ 40 static int 41 hci_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 = (u_int16_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 */ 84 static int 85 hci_switch_role(int s, int argc, char **argv) 86 { 87 int n0, n1, n2, n3, n4, n5; 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 (sscanf(argv[0], "%x:%x:%x:%x:%x:%x", 97 &n5, &n4, &n3, &n2, &n1, &n0) != 6) 98 return (USAGE); 99 100 cp.bdaddr.b[0] = n0 & 0xff; 101 cp.bdaddr.b[1] = n1 & 0xff; 102 cp.bdaddr.b[2] = n2 & 0xff; 103 cp.bdaddr.b[3] = n3 & 0xff; 104 cp.bdaddr.b[4] = n4 & 0xff; 105 cp.bdaddr.b[5] = n5 & 0xff; 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: %02x:%02x:%02x:%02x:%02x:%02x\n", 148 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3], 149 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]); 150 fprintf(stdout, "Role: %s [%#x]\n", 151 (ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave", 152 ep->role); 153 } else 154 goto again; 155 156 return (OK); 157 } /* hci_switch_role */ 158 159 /* Send Read_Link_Policy_Settings command to the unit */ 160 static int 161 hci_read_link_policy_settings(int s, int argc, char **argv) 162 { 163 ng_hci_read_link_policy_settings_cp cp; 164 ng_hci_read_link_policy_settings_rp rp; 165 int n; 166 167 /* parse command parameters */ 168 switch (argc) { 169 case 1: 170 /* connection handle */ 171 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 172 return (USAGE); 173 174 cp.con_handle = (u_int16_t) (n & 0x0fff); 175 cp.con_handle = htole16(cp.con_handle); 176 break; 177 178 default: 179 return (USAGE); 180 } 181 182 /* send request */ 183 n = sizeof(rp); 184 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY, 185 NG_HCI_OCF_READ_LINK_POLICY_SETTINGS), 186 (char const *) &cp, sizeof(cp), 187 (char *) &rp, &n) == ERROR) 188 return (ERROR); 189 190 if (rp.status != 0x00) { 191 fprintf(stdout, "Status: %s [%#02x]\n", 192 hci_status2str(rp.status), rp.status); 193 return (FAILED); 194 } 195 196 fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle)); 197 fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings)); 198 199 return (OK); 200 } /* hci_read_link_policy_settings */ 201 202 /* Send Write_Link_Policy_Settings command to the unit */ 203 static int 204 hci_write_link_policy_settings(int s, int argc, char **argv) 205 { 206 ng_hci_write_link_policy_settings_cp cp; 207 ng_hci_write_link_policy_settings_rp rp; 208 int n; 209 210 /* parse command parameters */ 211 switch (argc) { 212 case 2: 213 /* connection handle */ 214 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 215 return (USAGE); 216 217 cp.con_handle = (u_int16_t) (n & 0x0fff); 218 cp.con_handle = htole16(cp.con_handle); 219 220 /* link policy settings */ 221 if (sscanf(argv[1], "%x", &n) != 1) 222 return (USAGE); 223 224 cp.settings = (u_int16_t) (n & 0x0ffff); 225 cp.settings = htole16(cp.settings); 226 break; 227 228 default: 229 return (USAGE); 230 } 231 232 /* send request */ 233 n = sizeof(rp); 234 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY, 235 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS), 236 (char const *) &cp, sizeof(cp), 237 (char *) &rp, &n) == ERROR) 238 return (ERROR); 239 240 if (rp.status != 0x00) { 241 fprintf(stdout, "Status: %s [%#02x]\n", 242 hci_status2str(rp.status), rp.status); 243 return (FAILED); 244 } 245 246 return (OK); 247 } /* hci_write_link_policy_settings */ 248 249 struct hci_command link_policy_commands[] = { 250 { 251 "role_discovery <conection_handle>", 252 "\nThe Role_Discovery command is used for a Bluetooth device to determine\n" \ 253 "which role the device is performing for a particular Connection Handle.\n" \ 254 "The connection handle must be a connection handle for an ACL connection.\n\n" \ 255 "\t<connection_handle> - dddd; connection handle", 256 &hci_role_discovery 257 }, 258 { 259 "switch_role <bdaddr> <role>", 260 "\nThe Switch_Role command is used for a Bluetooth device to switch the\n" \ 261 "current role the device is performing for a particular connection with\n" \ 262 "another specified Bluetooth device. The BD_ADDR command parameter indicates\n"\ 263 "for which connection the role switch is to be performed. The Role indicates\n"\ 264 "the requested new role that the local device performs. Note: the BD_ADDR\n" \ 265 "command parameter must specify a Bluetooth device for which a connection\n" 266 "already exists.\n\n" \ 267 "\t<bdaddr> - xx:xx:xx:xx:xx:xx; device bdaddr\n" \ 268 "\t<role> - dd; role; 0 - Master, 1 - Slave", 269 &hci_switch_role 270 }, 271 { 272 "read_link_policy_settings <connection_handle>", 273 "\nThis command will read the Link Policy setting for the specified connection\n"\ 274 "handle. The link policy settings parameter determines the behavior of the\n" \ 275 "local Link Manager when it receives a request from a remote device or it\n" \ 276 "determines itself to change the master-slave role or to enter the hold,\n" \ 277 "sniff, or park mode. The local Link Manager will automatically accept or\n" \ 278 "reject such a request from the remote device, and may even autonomously\n" \ 279 "request itself, depending on the value of the link policy settings parameter\n"\ 280 "for the corresponding connection handle. The connection handle must be a\n" \ 281 "connection handle for an ACL connection.\n\n" \ 282 "\t<connection_handle> - dddd; connection handle", 283 &hci_read_link_policy_settings 284 }, 285 { 286 "write_link_policy_settings <connection_handle> <settings>", 287 "\nThis command will write the Link Policy setting for the specified connection\n"\ 288 "handle. The link policy settings parameter determines the behavior of the\n" \ 289 "local Link Manager when it receives a request from a remote device or it\n" \ 290 "determines itself to change the master-slave role or to enter the hold,\n" \ 291 "sniff, or park mode. The local Link Manager will automatically accept or\n" \ 292 "reject such a request from the remote device, and may even autonomously\n" \ 293 "request itself, depending on the value of the link policy settings parameter\n"\ 294 "for the corresponding connection handle. The connection handle must be a\n" \ 295 "connection handle for an ACL connection. Multiple Link Manager policies may\n"\ 296 "be specified for the link policy settings parameter by performing a bitwise\n"\ 297 "OR operation of the different activity types.\n\n" \ 298 "\t<connection_handle> - dddd; connection handle\n" \ 299 "\t<settings> - xxxx; settings\n" \ 300 "\t\t0x0000 - Disable All LM Modes (Default)\n" \ 301 "\t\t0x0001 - Enable Master Slave Switch\n" \ 302 "\t\t0x0002 - Enable Hold Mode\n" \ 303 "\t\t0x0004 - Enable Sniff Mode\n" \ 304 "\t\t0x0008 - Enable Park Mode\n", 305 &hci_write_link_policy_settings 306 }, 307 { 308 NULL, 309 }}; 310 311