1 /* 2 * link_control.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_control.c,v 1.12 2002/09/17 16:36:46 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 <string.h> 38 #include "hccontrol.h" 39 40 static void hci_inquiry_response (int n, u_int8_t **b); 41 42 /* Send Inquiry command to the unit */ 43 static int 44 hci_inquiry(int s, int argc, char **argv) 45 { 46 int n0, n1, n2, timo; 47 u_int8_t b[512]; 48 ng_hci_inquiry_cp cp; 49 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 50 51 /* set defaults */ 52 cp.lap[2] = 0x9e; 53 cp.lap[1] = 0x8b; 54 cp.lap[0] = 0x33; 55 cp.inquiry_length = 5; 56 cp.num_responses = 8; 57 58 /* parse command parameters */ 59 switch (argc) { 60 case 3: 61 /* LAP */ 62 if (sscanf(argv[0], "%x:%x:%x", &n2, &n1, &n0) != 3) 63 return (USAGE); 64 65 cp.lap[0] = (n0 & 0xff); 66 cp.lap[1] = (n1 & 0xff); 67 cp.lap[2] = (n2 & 0xff); 68 69 /* inquiry length (N * 1.28) sec, range 0x01 - 0x30 */ 70 case 2: 71 if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x1 || n0 > 0x30) 72 return (USAGE); 73 74 cp.inquiry_length = (n0 & 0xff); 75 76 /* number of responses, range 0x00 - 0xff */ 77 case 1: 78 if (sscanf(argv[2], "%d", &n0) != 1 || n0 > 0xff) 79 return (USAGE); 80 81 cp.num_responses = (n0 & 0xff); 82 83 /* use defaults */ 84 case 0: 85 break; 86 87 default: 88 return (USAGE); 89 } 90 91 /* send request and expect status back */ 92 n0 = sizeof(b); 93 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 94 NG_HCI_OCF_INQUIRY), (char const *) &cp, sizeof(cp), 95 b, &n0) == ERROR) 96 return (ERROR); 97 98 if (*b != 0x00) 99 return (FAILED); 100 101 timo = timeout; 102 timeout = cp.inquiry_length * 1.28 + 1; 103 104 wait_for_more: 105 /* wait for inquiry events */ 106 n0 = sizeof(b); 107 if (hci_recv(s, b, &n0) == ERROR) { 108 timeout = timo; 109 return (ERROR); 110 } 111 112 if (n0 < sizeof(*e)) { 113 timeout = timo; 114 errno = EIO; 115 return (ERROR); 116 } 117 118 switch (e->event) { 119 case NG_HCI_EVENT_INQUIRY_RESULT: { 120 ng_hci_inquiry_result_ep *ir = 121 (ng_hci_inquiry_result_ep *)(e + 1); 122 u_int8_t *r = (u_int8_t *)(ir + 1); 123 124 fprintf(stdout, "Inquiry result, num_responses=%d\n", 125 ir->num_responses); 126 127 for (n0 = 0; n0 < ir->num_responses; n0++) 128 hci_inquiry_response(n0, &r); 129 130 goto wait_for_more; 131 } 132 133 case NG_HCI_EVENT_INQUIRY_COMPL: 134 fprintf(stdout, "Inquiry complete. Status: %s [%#02x]\n", 135 hci_status2str(*(b + sizeof(*e))), *(b + sizeof(*e))); 136 break; 137 138 default: 139 goto wait_for_more; 140 } 141 142 timeout = timo; 143 144 return (OK); 145 } /* hci_inquiry */ 146 147 /* Print Inquiry_Result event */ 148 static void 149 hci_inquiry_response(int n, u_int8_t **b) 150 { 151 struct inquiry_response { 152 bdaddr_t bdaddr; 153 u_int8_t page_scan_rep_mode; 154 u_int8_t page_scan_period_mode; 155 u_int8_t page_scan_mode; 156 u_int8_t class[NG_HCI_CLASS_SIZE]; 157 u_int16_t clock_offset; 158 } *ir = (struct inquiry_response *)(*b); 159 160 fprintf(stdout, "Inquiry result #%d\n", n); 161 fprintf(stdout, "\tBD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n", 162 ir->bdaddr.b[5], ir->bdaddr.b[4], ir->bdaddr.b[3], 163 ir->bdaddr.b[2], ir->bdaddr.b[1], ir->bdaddr.b[0]); 164 fprintf(stdout, "\tPage Scan Rep. Mode: %#02x\n", 165 ir->page_scan_rep_mode); 166 fprintf(stdout, "\tPage Scan Period Mode: %#02x\n", 167 ir->page_scan_period_mode); 168 fprintf(stdout, "\tPage Scan Mode: %#02x\n", 169 ir->page_scan_mode); 170 fprintf(stdout, "\tClass: %02x:%02x:%02x\n", 171 ir->class[2], ir->class[1], ir->class[0]); 172 fprintf(stdout, "\tClock offset: %#04x\n", 173 le16toh(ir->clock_offset)); 174 175 *b += sizeof(*ir); 176 } /* hci_inquiry_response */ 177 178 /* Send Create_Connection command to the unit */ 179 static int 180 hci_create_connection(int s, int argc, char **argv) 181 { 182 int n0, n1, n2, n3, n4, n5; 183 char b[512]; 184 ng_hci_create_con_cp cp; 185 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 186 187 /* Set defaults */ 188 memset(&cp, 0, sizeof(cp)); 189 cp.pkt_type = htole16( NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 | 190 NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 | 191 NG_HCI_PKT_DM5); 192 cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0; 193 cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE; 194 cp.clock_offset = 0; 195 cp.accept_role_switch = 1; 196 197 /* parse command parameters */ 198 switch (argc) { 199 case 6: 200 /* accept role switch */ 201 if (sscanf(argv[2], "%d", &n0) != 1) 202 return (USAGE); 203 204 cp.accept_role_switch = n0 ? 1 : 0; 205 206 case 5: 207 /* clock offset */ 208 if (sscanf(argv[2], "%d", &n0) != 1) 209 return (USAGE); 210 211 cp.clock_offset = (n0 & 0xffff); 212 cp.clock_offset = htole16(cp.clock_offset); 213 214 case 4: 215 /* page scan mode */ 216 if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 3) 217 return (USAGE); 218 219 cp.page_scan_mode = (n0 & 0xff); 220 221 case 3: 222 /* page scan rep mode */ 223 if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 2) 224 return (USAGE); 225 226 cp.page_scan_rep_mode = (n0 & 0xff); 227 228 case 2: 229 /* packet type */ 230 if (sscanf(argv[1], "%x", &n0) != 1) 231 return (USAGE); 232 233 n0 &= ( NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 | 234 NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 | 235 NG_HCI_PKT_DM5); 236 if (n0 == 0) 237 return (USAGE); 238 239 cp.pkt_type = (n0 & 0xffff); 240 cp.pkt_type = htole16(cp.pkt_type); 241 242 case 1: 243 /* BD_ADDR */ 244 if (sscanf(argv[0], "%x:%x:%x:%x:%x:%x", 245 &n5, &n4, &n3, &n2, &n1, &n0) != 6) 246 return (USAGE); 247 248 cp.bdaddr.b[0] = (n0 & 0xff); 249 cp.bdaddr.b[1] = (n1 & 0xff); 250 cp.bdaddr.b[2] = (n2 & 0xff); 251 cp.bdaddr.b[3] = (n3 & 0xff); 252 cp.bdaddr.b[4] = (n4 & 0xff); 253 cp.bdaddr.b[5] = (n5 & 0xff); 254 break; 255 256 default: 257 return (USAGE); 258 } 259 260 /* send request and expect status response */ 261 n0 = sizeof(b); 262 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 263 NG_HCI_OCF_CREATE_CON), 264 (char const *) &cp, sizeof(cp), b, &n0) == ERROR) 265 return (ERROR); 266 267 if (*b != 0x00) 268 return (FAILED); 269 270 /* wait for event */ 271 again: 272 n0 = sizeof(b); 273 if (hci_recv(s, b, &n0) == ERROR) 274 return (ERROR); 275 if (n0 < sizeof(*e)) { 276 errno = EIO; 277 return (ERROR); 278 } 279 280 if (e->event == NG_HCI_EVENT_CON_COMPL) { 281 ng_hci_con_compl_ep *ep = (ng_hci_con_compl_ep *)(e + 1); 282 283 if (ep->status != 0x00) { 284 fprintf(stdout, "Status: %s [%#02x]\n", 285 hci_status2str(ep->status), ep->status); 286 return (FAILED); 287 } 288 289 fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n", 290 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3], 291 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]); 292 fprintf(stdout, "Connection handle: %d\n", 293 le16toh(ep->con_handle)); 294 fprintf(stdout, "Encryption mode: %s [%d]\n", 295 hci_encrypt2str(ep->encryption_mode, 0), 296 ep->encryption_mode); 297 } else 298 goto again; 299 300 return (OK); 301 } /* hci_create_connection */ 302 303 /* Send Disconnect command to the unit */ 304 static int 305 hci_disconnect(int s, int argc, char **argv) 306 { 307 int n; 308 char b[512]; 309 ng_hci_discon_cp cp; 310 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 311 312 /* Set defaults */ 313 memset(&cp, 0, sizeof(cp)); 314 cp.reason = 0x13; 315 316 /* parse command parameters */ 317 switch (argc) { 318 case 2: 319 /* reason */ 320 if (sscanf(argv[1], "%d", &n) != 1 || n <= 0x00 || n > 0xff) 321 return (USAGE); 322 323 cp.reason = (u_int8_t) (n & 0xff); 324 325 case 1: 326 /* connection handle */ 327 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 328 return (USAGE); 329 330 cp.con_handle = (u_int16_t) (n & 0x0fff); 331 cp.con_handle = htole16(cp.con_handle); 332 break; 333 334 default: 335 return (USAGE); 336 } 337 338 /* send request and expect status response */ 339 n = sizeof(b); 340 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 341 NG_HCI_OCF_DISCON), 342 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 343 return (ERROR); 344 345 if (*b != 0x00) 346 return (FAILED); 347 348 /* wait for event */ 349 again: 350 n = sizeof(b); 351 if (hci_recv(s, b, &n) == ERROR) 352 return (ERROR); 353 if (n < sizeof(*e)) { 354 errno = EIO; 355 return (ERROR); 356 } 357 358 if (e->event == NG_HCI_EVENT_DISCON_COMPL) { 359 ng_hci_discon_compl_ep *ep = (ng_hci_discon_compl_ep *)(e + 1); 360 361 if (ep->status != 0x00) { 362 fprintf(stdout, "Status: %s [%#02x]\n", 363 hci_status2str(ep->status), ep->status); 364 return (FAILED); 365 } 366 367 fprintf(stdout, "Connection handle: %d\n", 368 le16toh(ep->con_handle)); 369 fprintf(stdout, "Reason: %s [%#02x]\n", 370 hci_status2str(ep->reason), ep->reason); 371 } else 372 goto again; 373 374 return (OK); 375 } /* hci_diconnect */ 376 377 /* Send Add_SCO_Connection command to the unit */ 378 static int 379 hci_add_sco_connection(int s, int argc, char **argv) 380 { 381 int n; 382 char b[512]; 383 ng_hci_add_sco_con_cp cp; 384 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 385 386 /* Set defaults */ 387 memset(&cp, 0, sizeof(cp)); 388 cp.pkt_type = htole16(NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3); 389 390 /* parse command parameters */ 391 switch (argc) { 392 case 2: 393 /* packet type */ 394 if (sscanf(argv[0], "%x", &n) != 1) 395 return (USAGE); 396 397 n &= (NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3); 398 if (n == 0) 399 return (USAGE); 400 401 cp.pkt_type = (u_int16_t) (n & 0x0fff); 402 cp.pkt_type = htole16(cp.pkt_type); 403 404 case 1: 405 /* acl connection handle */ 406 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 407 return (USAGE); 408 409 cp.con_handle = (u_int16_t) (n & 0x0fff); 410 cp.con_handle = htole16(cp.con_handle); 411 break; 412 413 default: 414 return (USAGE); 415 } 416 417 /* send request and expect status response */ 418 n = sizeof(b); 419 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 420 NG_HCI_OCF_ADD_SCO_CON), 421 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 422 return (ERROR); 423 424 if (*b != 0x00) 425 return (FAILED); 426 427 /* wait for event */ 428 again: 429 n = sizeof(b); 430 if (hci_recv(s, b, &n) == ERROR) 431 return (ERROR); 432 if (n < sizeof(*e)) { 433 errno = EIO; 434 return (ERROR); 435 } 436 437 if (e->event == NG_HCI_EVENT_CON_COMPL) { 438 ng_hci_con_compl_ep *ep = (ng_hci_con_compl_ep *)(e + 1); 439 440 if (ep->status != 0x00) { 441 fprintf(stdout, "Status: %s [%#02x]\n", 442 hci_status2str(ep->status), ep->status); 443 return (FAILED); 444 } 445 446 fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n", 447 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3], 448 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]); 449 fprintf(stdout, "Connection handle: %d\n", 450 le16toh(ep->con_handle)); 451 fprintf(stdout, "Encryption mode: %s [%d]\n", 452 hci_encrypt2str(ep->encryption_mode, 0), 453 ep->encryption_mode); 454 } else 455 goto again; 456 457 return (OK); 458 } /* Add_SCO_Connection */ 459 460 /* Send Change_Connection_Packet_Type command to the unit */ 461 static int 462 hci_change_connection_packet_type(int s, int argc, char **argv) 463 { 464 int n; 465 char b[512]; 466 ng_hci_change_con_pkt_type_cp cp; 467 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 468 469 switch (argc) { 470 case 2: 471 /* connection handle */ 472 if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 473 return (USAGE); 474 475 cp.con_handle = (u_int16_t) (n & 0x0fff); 476 cp.con_handle = htole16(cp.con_handle); 477 478 /* packet type */ 479 if (sscanf(argv[1], "%x", &n) != 1) 480 return (USAGE); 481 482 cp.pkt_type = (u_int16_t) (n & 0xffff); 483 cp.pkt_type = htole16(cp.pkt_type); 484 break; 485 486 default: 487 return (USAGE); 488 } 489 490 /* send request and expect status response */ 491 n = sizeof(b); 492 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 493 NG_HCI_OCF_CHANGE_CON_PKT_TYPE), 494 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 495 return (ERROR); 496 497 if (*b != 0x00) 498 return (FAILED); 499 500 /* wait for event */ 501 again: 502 n = sizeof(b); 503 if (hci_recv(s, b, &n) == ERROR) 504 return (ERROR); 505 if (n < sizeof(*e)) { 506 errno = EIO; 507 return (ERROR); 508 } 509 510 if (e->event == NG_HCI_EVENT_CON_PKT_TYPE_CHANGED) { 511 ng_hci_con_pkt_type_changed_ep *ep = 512 (ng_hci_con_pkt_type_changed_ep *)(e + 1); 513 514 if (ep->status != 0x00) { 515 fprintf(stdout, "Status: %s [%#02x]\n", 516 hci_status2str(ep->status), ep->status); 517 return (FAILED); 518 } 519 520 fprintf(stdout, "Connection handle: %d\n", 521 le16toh(ep->con_handle)); 522 fprintf(stdout, "Packet type: %#04x\n", 523 le16toh(ep->pkt_type)); 524 } else 525 goto again; 526 527 return (OK); 528 } /* hci_change_connection_packet_type */ 529 530 /* Send Remote_Name_Request command to the unit */ 531 static int 532 hci_remote_name_request(int s, int argc, char **argv) 533 { 534 int n0, n1, n2, n3, n4, n5; 535 char b[512]; 536 ng_hci_remote_name_req_cp cp; 537 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 538 539 /* parse command parameters */ 540 switch (argc) { 541 case 4: 542 /* BD_ADDR */ 543 if (sscanf(argv[0], "%x:%x:%x:%x:%x:%x", 544 &n5, &n4, &n3, &n2, &n1, &n0) != 6) 545 return (USAGE); 546 547 cp.bdaddr.b[0] = (n0 & 0xff); 548 cp.bdaddr.b[1] = (n1 & 0xff); 549 cp.bdaddr.b[2] = (n2 & 0xff); 550 cp.bdaddr.b[3] = (n3 & 0xff); 551 cp.bdaddr.b[4] = (n4 & 0xff); 552 cp.bdaddr.b[5] = (n5 & 0xff); 553 554 /* page_scan_rep_mode */ 555 if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x02) 556 return (USAGE); 557 558 cp.page_scan_rep_mode = (n0 & 0xff); 559 560 /* page_scan_mode */ 561 if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x03) 562 return (USAGE); 563 564 cp.page_scan_mode = (n0 & 0xff); 565 566 /* clock_offset */ 567 if (sscanf(argv[3], "%x", &n0) != 1) 568 return (USAGE); 569 570 cp.clock_offset = (n0 & 0xffff); 571 cp.clock_offset = htole16(cp.clock_offset); 572 break; 573 574 default: 575 return (USAGE); 576 } 577 578 /* send request and expect status response */ 579 n0 = sizeof(b); 580 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 581 NG_HCI_OCF_REMOTE_NAME_REQ), 582 (char const *) &cp, sizeof(cp), b, &n0) == ERROR) 583 return (ERROR); 584 585 if (*b != 0x00) 586 return (FAILED); 587 588 /* wait for event */ 589 again: 590 n0 = sizeof(b); 591 if (hci_recv(s, b, &n0) == ERROR) 592 return (ERROR); 593 if (n0 < sizeof(*e)) { 594 errno = EIO; 595 return (ERROR); 596 } 597 598 if (e->event == NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL) { 599 ng_hci_remote_name_req_compl_ep *ep = 600 (ng_hci_remote_name_req_compl_ep *)(e + 1); 601 602 if (ep->status != 0x00) { 603 fprintf(stdout, "Status: %s [%#02x]\n", 604 hci_status2str(ep->status), ep->status); 605 return (FAILED); 606 } 607 608 fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n", 609 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3], 610 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]); 611 fprintf(stdout, "Name: %s\n", ep->name); 612 } else 613 goto again; 614 615 return (OK); 616 } /* hci_remote_name_request */ 617 618 /* Send Read_Remote_Supported_Features command to the unit */ 619 static int 620 hci_read_remote_supported_features(int s, int argc, char **argv) 621 { 622 int n; 623 char b[512]; 624 ng_hci_read_remote_features_cp cp; 625 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 626 char buffer[1024]; 627 628 /* parse command parameters */ 629 switch (argc) { 630 case 1: 631 /* connecton handle */ 632 if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) 633 return (USAGE); 634 635 cp.con_handle = (n & 0x0fff); 636 cp.con_handle = htole16(cp.con_handle); 637 break; 638 639 default: 640 return (USAGE); 641 } 642 643 /* send request and expect status response */ 644 n = sizeof(b); 645 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 646 NG_HCI_OCF_READ_REMOTE_FEATURES), 647 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 648 return (ERROR); 649 650 if (*b != 0x00) 651 return (FAILED); 652 653 /* wait for event */ 654 again: 655 n = sizeof(b); 656 if (hci_recv(s, b, &n) == ERROR) 657 return (ERROR); 658 659 if (n < sizeof(*e)) { 660 errno = EIO; 661 return (ERROR); 662 } 663 664 if (e->event == NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL) { 665 ng_hci_read_remote_features_compl_ep *ep = 666 (ng_hci_read_remote_features_compl_ep *)(e + 1); 667 668 if (ep->status != 0x00) { 669 fprintf(stdout, "Status: %s [%#02x]\n", 670 hci_status2str(ep->status), ep->status); 671 return (FAILED); 672 } 673 674 fprintf(stdout, "Connection handle: %d\n", 675 le16toh(ep->con_handle)); 676 fprintf(stdout, "Features: "); 677 for (n = 0; n < sizeof(ep->features); n++) 678 fprintf(stdout, "%#02x ", ep->features[n]); 679 fprintf(stdout, "\n%s\n", hci_features2str(ep->features, 680 buffer, sizeof(buffer))); 681 } else 682 goto again; 683 684 return (OK); 685 } /* hci_read_remote_supported_features */ 686 687 /* Send Read_Remote_Version_Information command to the unit */ 688 static int 689 hci_read_remote_version_information(int s, int argc, char **argv) 690 { 691 int n; 692 char b[512]; 693 ng_hci_read_remote_ver_info_cp cp; 694 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 695 696 /* parse command parameters */ 697 switch (argc) { 698 case 1: 699 /* connecton handle */ 700 if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) 701 return (USAGE); 702 703 cp.con_handle = (n & 0x0fff); 704 cp.con_handle = htole16(cp.con_handle); 705 break; 706 707 default: 708 return (USAGE); 709 } 710 711 /* send request and expect status response */ 712 n = sizeof(b); 713 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 714 NG_HCI_OCF_READ_REMOTE_VER_INFO), 715 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 716 return (ERROR); 717 718 if (*b != 0x00) 719 return (FAILED); 720 721 /* wait for event */ 722 again: 723 n = sizeof(b); 724 if (hci_recv(s, b, &n) == ERROR) 725 return (ERROR); 726 727 if (n < sizeof(*e)) { 728 errno = EIO; 729 return (ERROR); 730 } 731 732 if (e->event == NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL) { 733 ng_hci_read_remote_ver_info_compl_ep *ep = 734 (ng_hci_read_remote_ver_info_compl_ep *)(e + 1); 735 736 if (ep->status != 0x00) { 737 fprintf(stdout, "Status: %s [%#02x]\n", 738 hci_status2str(ep->status), ep->status); 739 return (FAILED); 740 } 741 742 ep->manufacturer = le16toh(ep->manufacturer); 743 744 fprintf(stdout, "Connection handle: %d\n", 745 le16toh(ep->con_handle)); 746 fprintf(stdout, "LMP version: %#02x\n", ep->lmp_version); 747 fprintf(stdout, "LMP sub-version: %#04x\n", 748 le16toh(ep->lmp_subversion)); 749 fprintf(stdout, "Manufacturer: %s [%#04x]\n", 750 hci_manufacturer2str(ep->manufacturer), 751 ep->manufacturer); 752 } else 753 goto again; 754 755 return (OK); 756 } /* hci_read_remote_version_information */ 757 758 /* Send Read_Clock_Offset command to the unit */ 759 static int 760 hci_read_clock_offset(int s, int argc, char **argv) 761 { 762 int n; 763 char b[512]; 764 ng_hci_read_clock_offset_cp cp; 765 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; 766 767 /* parse command parameters */ 768 switch (argc) { 769 case 1: 770 /* connecton handle */ 771 if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) 772 return (USAGE); 773 774 cp.con_handle = (n & 0x0fff); 775 cp.con_handle = htole16(cp.con_handle); 776 break; 777 778 default: 779 return (USAGE); 780 } 781 782 /* send request and expect status response */ 783 n = sizeof(b); 784 if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, 785 NG_HCI_OCF_READ_CLOCK_OFFSET), 786 (char const *) &cp, sizeof(cp), b, &n) == ERROR) 787 return (ERROR); 788 789 if (*b != 0x00) 790 return (FAILED); 791 792 /* wait for event */ 793 again: 794 n = sizeof(b); 795 if (hci_recv(s, b, &n) == ERROR) 796 return (ERROR); 797 798 if (n < sizeof(*e)) { 799 errno = EIO; 800 return (ERROR); 801 } 802 803 if (e->event == NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL) { 804 ng_hci_read_clock_offset_compl_ep *ep = 805 (ng_hci_read_clock_offset_compl_ep *)(e + 1); 806 807 if (ep->status != 0x00) { 808 fprintf(stdout, "Status: %s [%#02x]\n", 809 hci_status2str(ep->status), ep->status); 810 return (FAILED); 811 } 812 813 fprintf(stdout, "Connection handle: %d\n", 814 le16toh(ep->con_handle)); 815 fprintf(stdout, "Clock offset: %#04x\n", 816 le16toh(ep->clock_offset)); 817 } else 818 goto again; 819 820 return (OK); 821 } /* hci_read_clock_offset */ 822 823 struct hci_command link_control_commands[] = { 824 { 825 "inquiry <LAP> <inquiry_length> <num_reponses>", 826 "\nThis command will cause the Bluetooth unit to enter Inquiry Mode.\n" \ 827 "Inquiry Mode is used to discover other nearby Bluetooth units. The LAP\n" \ 828 "input parameter contains the LAP from which the inquiry access code shall\n" \ 829 "be derived when the inquiry procedure is made. The Inquiry_Length parameter\n"\ 830 "specifies the total duration of the Inquiry Mode and, when this time\n" \ 831 "expires, Inquiry will be halted. The Num_Responses parameter specifies the\n" \ 832 "number of responses that can be received before the Inquiry is halted.\n\n" \ 833 "\t<LAP> - xx:xx:xx; 9e:8b:33 (GIAC), 93:8b:00 (LDIAC)\n" \ 834 "\t<inquiry_length> - dd; total length == dd * 1.28 sec\n" \ 835 "\t<num_responses> - dd", 836 &hci_inquiry 837 }, 838 { 839 "create_connection <BD_ADDR> <pkt> <rep_mode> <ps_mode> <clck_off> <role_sw>", 840 "" \ 841 "\t<BD_ADDR> - remote unit address\n\n" \ 842 "\t<pkt> - xxxx; packet type\n" \ 843 "" \ 844 "\t\tACL packets\n" \ 845 "\t\t-----------\n" \ 846 "\t\t0x0008 DM1\n" \ 847 "\t\t0x0010 DH1\n" \ 848 "\t\t0x0400 DM3\n" \ 849 "\t\t0x0800 DH3\n" \ 850 "\t\t0x4000 DM5\n" \ 851 "\t\t0x8000 DH5\n\n" \ 852 "" \ 853 "\trep_mode - d; page scan repetition mode\n" \ 854 "" \ 855 "\t\tPage scan repetition modes\n" \ 856 "\t\t--------------------------\n" \ 857 "\t\t0 Page scan repetition mode 0\n" \ 858 "\t\t1 Page scan repetition mode 1\n" \ 859 "\t\t2 Page scan repetition mode 2\n" \ 860 "\n" \ 861 "\tps_mode - d; Page scan mode\n" \ 862 "" \ 863 "\t\tPage scan modes\n" \ 864 "\t\t---------------\n" \ 865 "\t\t0 Mandatory page scan mode\n" \ 866 "\t\t1 Optional page scan mode1\n" \ 867 "\t\t2 Optional page scan mode2\n" \ 868 "\t\t3 Optional page scan mode3\n" \ 869 "\n" \ 870 "\tclck_off - dddd; clock offset. Use 0 if unknown\n\n" \ 871 "\trole_sw - d; allow (1) or deny role switch\n", 872 &hci_create_connection 873 }, 874 { 875 "disconnect <connection_handle> <reason>", 876 "\nThe Disconnection command is used to terminate an existing connection.\n" \ 877 "The connection handle command parameter indicates which connection is to\n" \ 878 "be disconnected. The Reason command parameter indicates the reason for\n" \ 879 "ending the connection.\n\n" \ 880 "\t<connection_handle> - dddd; connection handle\n" \ 881 "\t<reason> - dd; reason; usually 19 (0x13) - user ended;\n" \ 882 "\t also 0x05, 0x13-0x15, 0x1A, 0x29", 883 &hci_disconnect 884 }, 885 { 886 "add_sco_connection <acl connection handle> <packet type>", 887 "This command will cause the link manager to create a SCO connection using\n" \ 888 "the ACL connection specified by the connection handle command parameter.\n" \ 889 "The Link Manager will determine how the new connection is established. This\n"\ 890 "connection is determined by the current state of the device, its piconet,\n" \ 891 "and the state of the device to be connected. The packet type command parameter\n" \ 892 "specifies which packet types the Link Manager should use for the connection.\n"\ 893 "The Link Manager must only use the packet type(s) specified by the packet\n" \ 894 "type command parameter for sending HCI SCO data packets. Multiple packet\n" \ 895 "types may be specified for the packet type command parameter by performing\n" \ 896 "a bitwise OR operation of the different packet types. Note: An SCO connection\n" \ 897 "can only be created when an ACL connection already exists and when it is\n" \ 898 "not put in park mode.\n\n" \ 899 "\t<connection_handle> - dddd; ACL connection handle\n" \ 900 "\t<packet_type> - xxxx; packet type\n" \ 901 "" \ 902 "\t\tSCO packets\n" \ 903 "\t\t-----------\n" \ 904 "\t\t0x0020 HV1\n" \ 905 "\t\t0x0040 HV2\n" \ 906 "\t\t0x0080 HV3\n", 907 &hci_add_sco_connection 908 }, 909 { 910 "change_connection_packet_type <connection_hande> <packet_type>", 911 "The Change_Connection_Packet_Type command is used to change which packet\n" \ 912 "types can be used for a connection that is currently established. This\n" \ 913 "allows current connections to be dynamically modified to support different\n" \ 914 "types of user data. The Packet_Type command parameter specifies which\n" \ 915 "packet types the Link Manager can use for the connection. Multiple packet\n" \ 916 "types may be specified for the Packet_Type command parameter by bitwise OR\n" \ 917 "operation of the different packet types.\n\n" \ 918 "\t<connection_handle> - dddd; connection handle\n" \ 919 "\t<packet_type> - xxxx; packet type mask\n" \ 920 "" \ 921 "\t\tACL packets\n" \ 922 "\t\t-----------\n" \ 923 "\t\t0x0008 DM1\n" \ 924 "\t\t0x0010 DH1\n" \ 925 "\t\t0x0400 DM3\n" \ 926 "\t\t0x0800 DH3\n" \ 927 "\t\t0x4000 DM5\n" \ 928 "\t\t0x8000 DH5\n\n" \ 929 "" \ 930 "\t\tSCO packets\n" \ 931 "\t\t-----------\n" \ 932 "\t\t0x0020 HV1\n" \ 933 "\t\t0x0040 HV2\n" \ 934 "\t\t0x0080 HV3\n" \ 935 "", 936 &hci_change_connection_packet_type 937 }, 938 { 939 "remote_name_request <bdaddr> <ps_rep_mode> <ps_mode> <clock_offset>", 940 "\nThe Remote_Name_Request command is used to obtain the user-friendly\n" \ 941 "name of another Bluetooth unit.\n\n" \ 942 "\t<bdaddr> - xx:xx:xx:xx:xx:xx remote unit BD_ADDR\n" \ 943 "\t<ps_rep_mode> - dd; page scan repetition mode [0-2]\n" \ 944 "\t<ps_mode> - dd; page scan mode [0-3]\n" \ 945 "\t<clock_offset> - xxxx; clock offset [0 - 0xffff]", 946 &hci_remote_name_request 947 }, 948 { 949 "read_remote_supported_features <connection_handle>", 950 "\nThis command requests a list of the supported features for the remote\n" \ 951 "unit identified by the connection handle parameter. The connection handle\n" \ 952 "must be a connection handle for an ACL connection.\n\n" \ 953 "\t<connection_handle> - dddd; connection handle", 954 &hci_read_remote_supported_features 955 }, 956 { 957 "read_remote_version_information <connection_handle>", 958 "\nThis command will obtain the values for the version information for the\n" \ 959 "remote Bluetooth unit identified by the connection handle parameter. The\n" \ 960 "connection handle must be a connection handle for an ACL connection.\n\n" \ 961 "\t<conneciton_handle> - dddd; connection handle", 962 &hci_read_remote_version_information 963 }, 964 { 965 "read_clock_offset <connection_handle>", 966 "\nThis command allows the Host to read clock offset to remote unit.\n" \ 967 "\t<conneciton_handle> - dddd; connection handle", 968 &hci_read_clock_offset 969 }, 970 { 971 NULL, 972 }}; 973 974