Index: head/usr.sbin/bluetooth/hccontrol/info.c =================================================================== --- head/usr.sbin/bluetooth/hccontrol/info.c (revision 360069) +++ head/usr.sbin/bluetooth/hccontrol/info.c (revision 360070) @@ -1,219 +1,219 @@ /*- * info.c * * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2001-2002 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: info.c,v 1.3 2003/08/18 19:19:54 max Exp $ * $FreeBSD$ */ #define L2CAP_SOCKET_CHECKED #include #include #include #include #include "hccontrol.h" /* Send Read_Local_Version_Information command to the unit */ static int hci_read_local_version_information(int s, int argc, char **argv) { ng_hci_read_local_ver_rp rp; int n; n = sizeof(rp); if (hci_simple_request(s, NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_VER), (char *) &rp, &n) == ERROR) return (ERROR); if (rp.status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(rp.status), rp.status); return (FAILED); } rp.manufacturer = le16toh(rp.manufacturer); fprintf(stdout, "HCI version: %s [%#02x]\n", hci_ver2str(rp.hci_version), rp.hci_version); fprintf(stdout, "HCI revision: %#04x\n", le16toh(rp.hci_revision)); fprintf(stdout, "LMP version: %s [%#02x]\n", hci_lmpver2str(rp.lmp_version), rp.lmp_version); fprintf(stdout, "LMP sub-version: %#04x\n", le16toh(rp.lmp_subversion)); fprintf(stdout, "Manufacturer: %s [%#04x]\n", hci_manufacturer2str(rp.manufacturer), rp.manufacturer); return (OK); } /* hci_read_local_version_information */ /* Send Read_Local_Supported_Features command to the unit */ static int hci_read_local_supported_features(int s, int argc, char **argv) { ng_hci_read_local_features_rp rp; int n; - char buffer[1024]; + char buffer[2048]; n = sizeof(rp); if (hci_simple_request(s, NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_LOCAL_FEATURES), (char *) &rp, &n) == ERROR) return (ERROR); if (rp.status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(rp.status), rp.status); return (FAILED); } fprintf(stdout, "Features: "); for (n = 0; n < sizeof(rp.features); n++) fprintf(stdout, "%#02x ", rp.features[n]); fprintf(stdout, "\n%s\n", hci_features2str(rp.features, buffer, sizeof(buffer))); return (OK); } /* hci_read_local_supported_features */ /* Sent Read_Buffer_Size command to the unit */ static int hci_read_buffer_size(int s, int argc, char **argv) { ng_hci_read_buffer_size_rp rp; int n; n = sizeof(rp); if (hci_simple_request(s, NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_BUFFER_SIZE), (char *) &rp, &n) == ERROR) return (ERROR); if (rp.status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(rp.status), rp.status); return (FAILED); } fprintf(stdout, "Max. ACL packet size: %d bytes\n", le16toh(rp.max_acl_size)); fprintf(stdout, "Number of ACL packets: %d\n", le16toh(rp.num_acl_pkt)); fprintf(stdout, "Max. SCO packet size: %d bytes\n", rp.max_sco_size); fprintf(stdout, "Number of SCO packets: %d\n", le16toh(rp.num_sco_pkt)); return (OK); } /* hci_read_buffer_size */ /* Send Read_Country_Code command to the unit */ static int hci_read_country_code(int s, int argc, char **argv) { ng_hci_read_country_code_rp rp; int n; n = sizeof(rp); if (hci_simple_request(s, NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_COUNTRY_CODE), (char *) &rp, &n) == ERROR) return (ERROR); if (rp.status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(rp.status), rp.status); return (FAILED); } fprintf(stdout, "Country code: %s [%#02x]\n", hci_cc2str(rp.country_code), rp.country_code); return (OK); } /* hci_read_country_code */ /* Send Read_BD_ADDR command to the unit */ static int hci_read_bd_addr(int s, int argc, char **argv) { ng_hci_read_bdaddr_rp rp; int n; n = sizeof(rp); if (hci_simple_request(s, NG_HCI_OPCODE(NG_HCI_OGF_INFO, NG_HCI_OCF_READ_BDADDR), (char *) &rp, &n) == ERROR) return (ERROR); if (rp.status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(rp.status), rp.status); return (FAILED); } fprintf(stdout, "BD_ADDR: %s\n", bt_ntoa(&rp.bdaddr, NULL)); return (OK); } /* hci_read_bd_addr */ struct hci_command info_commands[] = { { "read_local_version_information", "\nThis command will read the values for the version information for the\n" \ "local Bluetooth unit.", &hci_read_local_version_information }, { "read_local_supported_features", "\nThis command requests a list of the supported features for the local\n" \ "unit. This command will return a list of the LMP features.", &hci_read_local_supported_features }, { "read_buffer_size", "\nThe Read_Buffer_Size command is used to read the maximum size of the\n" \ "data portion of HCI ACL and SCO Data Packets sent from the Host to the\n" \ "Host Controller.", &hci_read_buffer_size }, { "read_country_code", "\nThis command will read the value for the Country_Code return parameter.\n" \ "The Country_Code defines which range of frequency band of the ISM 2.4 GHz\n" \ "band will be used by the unit.", &hci_read_country_code }, { "read_bd_addr", "\nThis command will read the value for the BD_ADDR parameter. The BD_ADDR\n" \ "is a 48-bit unique identifier for a Bluetooth unit.", &hci_read_bd_addr }, { NULL, }}; Index: head/usr.sbin/bluetooth/hccontrol/link_control.c =================================================================== --- head/usr.sbin/bluetooth/hccontrol/link_control.c (revision 360069) +++ head/usr.sbin/bluetooth/hccontrol/link_control.c (revision 360070) @@ -1,963 +1,963 @@ /*- * link_control.c * * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2001-2002 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: link_control.c,v 1.4 2003/08/18 19:19:54 max Exp $ * $FreeBSD$ */ #define L2CAP_SOCKET_CHECKED #include #include #include #include #include "hccontrol.h" static void hci_inquiry_response (int n, uint8_t **b); /* Send Inquiry command to the unit */ static int hci_inquiry(int s, int argc, char **argv) { int n0, n1, n2, timo; char b[512]; ng_hci_inquiry_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* set defaults */ cp.lap[2] = 0x9e; cp.lap[1] = 0x8b; cp.lap[0] = 0x33; cp.inquiry_length = 5; cp.num_responses = 8; /* parse command parameters */ switch (argc) { case 3: /* number of responses, range 0x00 - 0xff */ if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 0xff) return (USAGE); cp.num_responses = (n0 & 0xff); case 2: /* inquiry length (N * 1.28) sec, range 0x01 - 0x30 */ if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x1 || n0 > 0x30) return (USAGE); cp.inquiry_length = (n0 & 0xff); case 1: /* LAP */ if (sscanf(argv[0], "%x:%x:%x", &n2, &n1, &n0) != 3) return (USAGE); cp.lap[0] = (n0 & 0xff); cp.lap[1] = (n1 & 0xff); cp.lap[2] = (n2 & 0xff); case 0: /* use defaults */ break; default: return (USAGE); } /* send request and expect status back */ n0 = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_INQUIRY), (char const *) &cp, sizeof(cp), b, &n0) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); timo = timeout; timeout = cp.inquiry_length * 1.28 + 1; wait_for_more: /* wait for inquiry events */ n0 = sizeof(b); if (hci_recv(s, b, &n0) == ERROR) { timeout = timo; return (ERROR); } if (n0 < sizeof(*e)) { timeout = timo; errno = EIO; return (ERROR); } switch (e->event) { case NG_HCI_EVENT_INQUIRY_RESULT: { ng_hci_inquiry_result_ep *ir = (ng_hci_inquiry_result_ep *)(e + 1); uint8_t *r = (uint8_t *)(ir + 1); fprintf(stdout, "Inquiry result, num_responses=%d\n", ir->num_responses); for (n0 = 0; n0 < ir->num_responses; n0++) hci_inquiry_response(n0, &r); goto wait_for_more; } case NG_HCI_EVENT_INQUIRY_COMPL: fprintf(stdout, "Inquiry complete. Status: %s [%#02x]\n", hci_status2str(*(b + sizeof(*e))), *(b + sizeof(*e))); break; default: goto wait_for_more; } timeout = timo; return (OK); } /* hci_inquiry */ /* Print Inquiry_Result event */ static void hci_inquiry_response(int n, uint8_t **b) { ng_hci_inquiry_response *ir = (ng_hci_inquiry_response *)(*b); fprintf(stdout, "Inquiry result #%d\n", n); fprintf(stdout, "\tBD_ADDR: %s\n", hci_bdaddr2str(&ir->bdaddr)); fprintf(stdout, "\tPage Scan Rep. Mode: %#02x\n", ir->page_scan_rep_mode); fprintf(stdout, "\tPage Scan Period Mode: %#02x\n", ir->page_scan_period_mode); fprintf(stdout, "\tPage Scan Mode: %#02x\n", ir->page_scan_mode); fprintf(stdout, "\tClass: %02x:%02x:%02x\n", ir->uclass[2], ir->uclass[1], ir->uclass[0]); fprintf(stdout, "\tClock offset: %#04x\n", le16toh(ir->clock_offset)); *b += sizeof(*ir); } /* hci_inquiry_response */ /* Send Create_Connection command to the unit */ static int hci_create_connection(int s, int argc, char **argv) { int n0; char b[512]; ng_hci_create_con_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* Set defaults */ memset(&cp, 0, sizeof(cp)); cp.pkt_type = htole16( NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 | NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 | NG_HCI_PKT_DM5); cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0; cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE; cp.clock_offset = 0; cp.accept_role_switch = 1; /* parse command parameters */ switch (argc) { case 6: /* accept role switch */ if (sscanf(argv[5], "%d", &n0) != 1) return (USAGE); cp.accept_role_switch = n0 ? 1 : 0; case 5: /* clock offset */ if (sscanf(argv[4], "%d", &n0) != 1) return (USAGE); cp.clock_offset = (n0 & 0xffff); cp.clock_offset = htole16(cp.clock_offset); case 4: /* page scan mode */ if (sscanf(argv[3], "%d", &n0) != 1 || n0 < 0 || n0 > 3) return (USAGE); cp.page_scan_mode = (n0 & 0xff); case 3: /* page scan rep mode */ if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 2) return (USAGE); cp.page_scan_rep_mode = (n0 & 0xff); case 2: /* packet type */ if (sscanf(argv[1], "%x", &n0) != 1) return (USAGE); n0 &= ( NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 | NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 | NG_HCI_PKT_DM5); if (n0 == 0) return (USAGE); cp.pkt_type = (n0 & 0xffff); cp.pkt_type = htole16(cp.pkt_type); case 1: /* BD_ADDR */ if (!bt_aton(argv[0], &cp.bdaddr)) { struct hostent *he = NULL; if ((he = bt_gethostbyname(argv[0])) == NULL) return (USAGE); memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr)); } break; default: return (USAGE); } /* send request and expect status response */ n0 = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_CREATE_CON), (char const *) &cp, sizeof(cp), b, &n0) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n0 = sizeof(b); if (hci_recv(s, b, &n0) == ERROR) return (ERROR); if (n0 < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_CON_COMPL) { ng_hci_con_compl_ep *ep = (ng_hci_con_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr)); fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Encryption mode: %s [%d]\n", hci_encrypt2str(ep->encryption_mode, 0), ep->encryption_mode); } else goto again; return (OK); } /* hci_create_connection */ /* Send Disconnect command to the unit */ static int hci_disconnect(int s, int argc, char **argv) { int n; char b[512]; ng_hci_discon_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* Set defaults */ memset(&cp, 0, sizeof(cp)); cp.reason = 0x13; /* parse command parameters */ switch (argc) { case 2: /* reason */ if (sscanf(argv[1], "%d", &n) != 1 || n <= 0x00 || n > 0xff) return (USAGE); cp.reason = (uint8_t) (n & 0xff); case 1: /* connection handle */ if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) return (USAGE); cp.con_handle = (uint16_t) (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_DISCON), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_DISCON_COMPL) { ng_hci_discon_compl_ep *ep = (ng_hci_discon_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Reason: %s [%#02x]\n", hci_status2str(ep->reason), ep->reason); } else goto again; return (OK); } /* hci_disconnect */ /* Send Add_SCO_Connection command to the unit */ static int hci_add_sco_connection(int s, int argc, char **argv) { int n; char b[512]; ng_hci_add_sco_con_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* Set defaults */ memset(&cp, 0, sizeof(cp)); cp.pkt_type = htole16(NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3); /* parse command parameters */ switch (argc) { case 2: /* packet type */ if (sscanf(argv[1], "%x", &n) != 1) return (USAGE); n &= (NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3); if (n == 0) return (USAGE); cp.pkt_type = (uint16_t) (n & 0x0fff); cp.pkt_type = htole16(cp.pkt_type); case 1: /* acl connection handle */ if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) return (USAGE); cp.con_handle = (uint16_t) (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_ADD_SCO_CON), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_CON_COMPL) { ng_hci_con_compl_ep *ep = (ng_hci_con_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr)); fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Encryption mode: %s [%d]\n", hci_encrypt2str(ep->encryption_mode, 0), ep->encryption_mode); } else goto again; return (OK); } /* Add_SCO_Connection */ /* Send Change_Connection_Packet_Type command to the unit */ static int hci_change_connection_packet_type(int s, int argc, char **argv) { int n; char b[512]; ng_hci_change_con_pkt_type_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; switch (argc) { case 2: /* connection handle */ if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) return (USAGE); cp.con_handle = (uint16_t) (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); /* packet type */ if (sscanf(argv[1], "%x", &n) != 1) return (USAGE); cp.pkt_type = (uint16_t) (n & 0xffff); cp.pkt_type = htole16(cp.pkt_type); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_CHANGE_CON_PKT_TYPE), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_CON_PKT_TYPE_CHANGED) { ng_hci_con_pkt_type_changed_ep *ep = (ng_hci_con_pkt_type_changed_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Packet type: %#04x\n", le16toh(ep->pkt_type)); } else goto again; return (OK); } /* hci_change_connection_packet_type */ /* Send Remote_Name_Request command to the unit */ static int hci_remote_name_request(int s, int argc, char **argv) { int n0; char b[512]; ng_hci_remote_name_req_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; memset(&cp, 0, sizeof(cp)); cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0; cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE; /* parse command parameters */ switch (argc) { case 4: /* clock_offset */ if (sscanf(argv[3], "%x", &n0) != 1) return (USAGE); cp.clock_offset = (n0 & 0xffff); cp.clock_offset = htole16(cp.clock_offset); case 3: /* page_scan_mode */ if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x03) return (USAGE); cp.page_scan_mode = (n0 & 0xff); case 2: /* page_scan_rep_mode */ if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x02) return (USAGE); cp.page_scan_rep_mode = (n0 & 0xff); case 1: /* BD_ADDR */ if (!bt_aton(argv[0], &cp.bdaddr)) { struct hostent *he = NULL; if ((he = bt_gethostbyname(argv[0])) == NULL) return (USAGE); memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr)); } break; default: return (USAGE); } /* send request and expect status response */ n0 = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_REMOTE_NAME_REQ), (char const *) &cp, sizeof(cp), b, &n0) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n0 = sizeof(b); if (hci_recv(s, b, &n0) == ERROR) return (ERROR); if (n0 < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL) { ng_hci_remote_name_req_compl_ep *ep = (ng_hci_remote_name_req_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr)); fprintf(stdout, "Name: %s\n", ep->name); } else goto again; return (OK); } /* hci_remote_name_request */ /* Send Read_Remote_Supported_Features command to the unit */ static int hci_read_remote_supported_features(int s, int argc, char **argv) { int n; char b[512]; ng_hci_read_remote_features_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; - char buffer[1024]; + char buffer[2048]; /* parse command parameters */ switch (argc) { case 1: /* connecton handle */ if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) return (USAGE); cp.con_handle = (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_READ_REMOTE_FEATURES), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL) { ng_hci_read_remote_features_compl_ep *ep = (ng_hci_read_remote_features_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Features: "); for (n = 0; n < sizeof(ep->features); n++) fprintf(stdout, "%#02x ", ep->features[n]); fprintf(stdout, "\n%s\n", hci_features2str(ep->features, buffer, sizeof(buffer))); } else goto again; return (OK); } /* hci_read_remote_supported_features */ /* Send Read_Remote_Version_Information command to the unit */ static int hci_read_remote_version_information(int s, int argc, char **argv) { int n; char b[512]; ng_hci_read_remote_ver_info_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* parse command parameters */ switch (argc) { case 1: /* connecton handle */ if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) return (USAGE); cp.con_handle = (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_READ_REMOTE_VER_INFO), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL) { ng_hci_read_remote_ver_info_compl_ep *ep = (ng_hci_read_remote_ver_info_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } ep->manufacturer = le16toh(ep->manufacturer); fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "LMP version: %s [%#02x]\n", hci_lmpver2str(ep->lmp_version), ep->lmp_version); fprintf(stdout, "LMP sub-version: %#04x\n", le16toh(ep->lmp_subversion)); fprintf(stdout, "Manufacturer: %s [%#04x]\n", hci_manufacturer2str(ep->manufacturer), ep->manufacturer); } else goto again; return (OK); } /* hci_read_remote_version_information */ /* Send Read_Clock_Offset command to the unit */ static int hci_read_clock_offset(int s, int argc, char **argv) { int n; char b[512]; ng_hci_read_clock_offset_cp cp; ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; /* parse command parameters */ switch (argc) { case 1: /* connecton handle */ if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff) return (USAGE); cp.con_handle = (n & 0x0fff); cp.con_handle = htole16(cp.con_handle); break; default: return (USAGE); } /* send request and expect status response */ n = sizeof(b); if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_READ_CLOCK_OFFSET), (char const *) &cp, sizeof(cp), b, &n) == ERROR) return (ERROR); if (*b != 0x00) return (FAILED); /* wait for event */ again: n = sizeof(b); if (hci_recv(s, b, &n) == ERROR) return (ERROR); if (n < sizeof(*e)) { errno = EIO; return (ERROR); } if (e->event == NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL) { ng_hci_read_clock_offset_compl_ep *ep = (ng_hci_read_clock_offset_compl_ep *)(e + 1); if (ep->status != 0x00) { fprintf(stdout, "Status: %s [%#02x]\n", hci_status2str(ep->status), ep->status); return (FAILED); } fprintf(stdout, "Connection handle: %d\n", le16toh(ep->con_handle)); fprintf(stdout, "Clock offset: %#04x\n", le16toh(ep->clock_offset)); } else goto again; return (OK); } /* hci_read_clock_offset */ struct hci_command link_control_commands[] = { { "inquiry ", "\nThis command will cause the Bluetooth unit to enter Inquiry Mode.\n" \ "Inquiry Mode is used to discover other nearby Bluetooth units. The LAP\n" \ "input parameter contains the LAP from which the inquiry access code shall\n" \ "be derived when the inquiry procedure is made. The Inquiry_Length parameter\n"\ "specifies the total duration of the Inquiry Mode and, when this time\n" \ "expires, Inquiry will be halted. The Num_Responses parameter specifies the\n" \ "number of responses that can be received before the Inquiry is halted.\n\n" \ "\t - xx:xx:xx; 9e:8b:33 (GIAC), 93:8b:00 (LDIAC)\n" \ "\t - dd; total length == dd * 1.28 sec\n" \ "\t - dd", &hci_inquiry }, { "create_connection ", "" \ "\t - xx:xx:xx:xx:xx:xx BD_ADDR or name\n\n" \ "\t - xxxx; packet type\n" \ "" \ "\t\tACL packets\n" \ "\t\t-----------\n" \ "\t\t0x0008 DM1\n" \ "\t\t0x0010 DH1\n" \ "\t\t0x0400 DM3\n" \ "\t\t0x0800 DH3\n" \ "\t\t0x4000 DM5\n" \ "\t\t0x8000 DH5\n\n" \ "" \ "\trep_mode - d; page scan repetition mode\n" \ "" \ "\t\tPage scan repetition modes\n" \ "\t\t--------------------------\n" \ "\t\t0 Page scan repetition mode 0\n" \ "\t\t1 Page scan repetition mode 1\n" \ "\t\t2 Page scan repetition mode 2\n" \ "\n" \ "\tps_mode - d; Page scan mode\n" \ "" \ "\t\tPage scan modes\n" \ "\t\t---------------\n" \ "\t\t0 Mandatory page scan mode\n" \ "\t\t1 Optional page scan mode1\n" \ "\t\t2 Optional page scan mode2\n" \ "\t\t3 Optional page scan mode3\n" \ "\n" \ "\tclck_off - dddd; clock offset. Use 0 if unknown\n\n" \ "\trole_sw - d; allow (1) or deny role switch\n", &hci_create_connection }, { "disconnect ", "\nThe Disconnection command is used to terminate an existing connection.\n" \ "The connection handle command parameter indicates which connection is to\n" \ "be disconnected. The Reason command parameter indicates the reason for\n" \ "ending the connection.\n\n" \ "\t - dddd; connection handle\n" \ "\t - dd; reason; usually 19 (0x13) - user ended;\n" \ "\t also 0x05, 0x13-0x15, 0x1A, 0x29", &hci_disconnect }, { "add_sco_connection ", "This command will cause the link manager to create a SCO connection using\n" \ "the ACL connection specified by the connection handle command parameter.\n" \ "The Link Manager will determine how the new connection is established. This\n"\ "connection is determined by the current state of the device, its piconet,\n" \ "and the state of the device to be connected. The packet type command parameter\n" \ "specifies which packet types the Link Manager should use for the connection.\n"\ "The Link Manager must only use the packet type(s) specified by the packet\n" \ "type command parameter for sending HCI SCO data packets. Multiple packet\n" \ "types may be specified for the packet type command parameter by performing\n" \ "a bitwise OR operation of the different packet types. Note: An SCO connection\n" \ "can only be created when an ACL connection already exists and when it is\n" \ "not put in park mode.\n\n" \ "\t - dddd; ACL connection handle\n" \ "\t - xxxx; packet type\n" \ "" \ "\t\tSCO packets\n" \ "\t\t-----------\n" \ "\t\t0x0020 HV1\n" \ "\t\t0x0040 HV2\n" \ "\t\t0x0080 HV3\n", &hci_add_sco_connection }, { "change_connection_packet_type ", "The Change_Connection_Packet_Type command is used to change which packet\n" \ "types can be used for a connection that is currently established. This\n" \ "allows current connections to be dynamically modified to support different\n" \ "types of user data. The Packet_Type command parameter specifies which\n" \ "packet types the Link Manager can use for the connection. Multiple packet\n" \ "types may be specified for the Packet_Type command parameter by bitwise OR\n" \ "operation of the different packet types.\n\n" \ "\t - dddd; connection handle\n" \ "\t - xxxx; packet type mask\n" \ "" \ "\t\tACL packets\n" \ "\t\t-----------\n" \ "\t\t0x0008 DM1\n" \ "\t\t0x0010 DH1\n" \ "\t\t0x0400 DM3\n" \ "\t\t0x0800 DH3\n" \ "\t\t0x4000 DM5\n" \ "\t\t0x8000 DH5\n\n" \ "" \ "\t\tSCO packets\n" \ "\t\t-----------\n" \ "\t\t0x0020 HV1\n" \ "\t\t0x0040 HV2\n" \ "\t\t0x0080 HV3\n" \ "", &hci_change_connection_packet_type }, { "remote_name_request ", "\nThe Remote_Name_Request command is used to obtain the user-friendly\n" \ "name of another Bluetooth unit.\n\n" \ "\t - xx:xx:xx:xx:xx:xx BD_ADDR or name\n" \ "\t - dd; page scan repetition mode [0-2]\n" \ "\t - dd; page scan mode [0-3]\n" \ "\t - xxxx; clock offset [0 - 0xffff]", &hci_remote_name_request }, { "read_remote_supported_features ", "\nThis command requests a list of the supported features for the remote\n" \ "unit identified by the connection handle parameter. The connection handle\n" \ "must be a connection handle for an ACL connection.\n\n" \ "\t - dddd; connection handle", &hci_read_remote_supported_features }, { "read_remote_version_information ", "\nThis command will obtain the values for the version information for the\n" \ "remote Bluetooth unit identified by the connection handle parameter. The\n" \ "connection handle must be a connection handle for an ACL connection.\n\n" \ "\t - dddd; connection handle", &hci_read_remote_version_information }, { "read_clock_offset ", "\nThis command allows the Host to read the clock offset from the remote unit.\n" \ "\t - dddd; connection handle", &hci_read_clock_offset }, { NULL, }}; Index: head/usr.sbin/bluetooth/hccontrol/node.c =================================================================== --- head/usr.sbin/bluetooth/hccontrol/node.c (revision 360069) +++ head/usr.sbin/bluetooth/hccontrol/node.c (revision 360070) @@ -1,665 +1,665 @@ /*- * node.c * * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2001-2002 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: node.c,v 1.6 2003/07/22 21:14:02 max Exp $ * $FreeBSD$ */ #include #define L2CAP_SOCKET_CHECKED #include #include #include #include #include #include #include #include "hccontrol.h" /* Send Read_Node_State command to the node */ static int hci_read_node_state(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_state r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STATE, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "State: %#x\n", r.state); return (OK); } /* hci_read_node_state */ /* Send Intitialize command to the node */ static int hci_node_initialize(int s, int argc, char **argv) { if (ioctl(s, SIOC_HCI_RAW_NODE_INIT) < 0) return (ERROR); return (OK); } /* hci_node_initialize */ /* Send Read_Debug_Level command to the node */ static int hci_read_debug_level(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_debug r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_DEBUG, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Debug level: %d\n", r.debug); return (OK); } /* hci_read_debug_level */ /* Send Write_Debug_Level command to the node */ static int hci_write_debug_level(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_debug r; memset(&r, 0, sizeof(r)); switch (argc) { case 1: r.debug = atoi(argv[0]); break; default: return (USAGE); } if (ioctl(s, SIOC_HCI_RAW_NODE_SET_DEBUG, &r, sizeof(r)) < 0) return (ERROR); return (OK); } /* hci_write_debug_level */ /* Send Read_Node_Buffer_Size command to the node */ static int hci_read_node_buffer_size(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_buffer r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BUFFER, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Number of free command buffers: %d\n", r.buffer.cmd_free); fprintf(stdout, "Max. ACL packet size: %d\n", r.buffer.acl_size); fprintf(stdout, "Numbef of free ACL buffers: %d\n", r.buffer.acl_free); fprintf(stdout, "Total number of ACL buffers: %d\n", r.buffer.acl_pkts); fprintf(stdout, "Max. SCO packet size: %d\n", r.buffer.sco_size); fprintf(stdout, "Numbef of free SCO buffers: %d\n", r.buffer.sco_free); fprintf(stdout, "Total number of SCO buffers: %d\n", r.buffer.sco_pkts); return (OK); } /* hci_read_node_buffer_size */ /* Send Read_Node_BD_ADDR command to the node */ static int hci_read_node_bd_addr(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_bdaddr r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BDADDR, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "BD_ADDR: %s\n", bt_ntoa(&r.bdaddr, NULL)); return (OK); } /* hci_read_node_bd_addr */ /* Send Read_Node_Features command to the node */ static int hci_read_node_features(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_features r; int n; - char buffer[1024]; + char buffer[2048]; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_FEATURES, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Features: "); for (n = 0; n < sizeof(r.features)/sizeof(r.features[0]); n++) fprintf(stdout, "%#02x ", r.features[n]); fprintf(stdout, "\n%s\n", hci_features2str(r.features, buffer, sizeof(buffer))); return (OK); } /* hci_read_node_features */ /* Send Read_Node_Stat command to the node */ static int hci_read_node_stat(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_stat r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STAT, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Commands sent: %d\n", r.stat.cmd_sent); fprintf(stdout, "Events received: %d\n", r.stat.evnt_recv); fprintf(stdout, "ACL packets received: %d\n", r.stat.acl_recv); fprintf(stdout, "ACL packets sent: %d\n", r.stat.acl_sent); fprintf(stdout, "SCO packets received: %d\n", r.stat.sco_recv); fprintf(stdout, "SCO packets sent: %d\n", r.stat.sco_sent); fprintf(stdout, "Bytes received: %d\n", r.stat.bytes_recv); fprintf(stdout, "Bytes sent: %d\n", r.stat.bytes_sent); return (OK); } /* hci_read_node_stat */ /* Send Reset_Node_Stat command to the node */ static int hci_reset_node_stat(int s, int argc, char **argv) { if (ioctl(s, SIOC_HCI_RAW_NODE_RESET_STAT) < 0) return (ERROR); return (OK); } /* hci_reset_node_stat */ /* Send Flush_Neighbor_Cache command to the node */ static int hci_flush_neighbor_cache(int s, int argc, char **argv) { if (ioctl(s, SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE) < 0) return (ERROR); return (OK); } /* hci_flush_neighbor_cache */ #define MIN(a,b) (((a)>(b)) ? (b) :(a) ) static int hci_dump_adv(uint8_t *data, int length) { int elemlen; int type; int i; while(length>0){ elemlen = *data; data++; length --; elemlen--; if(length<=0) break; type = *data; data++; length --; elemlen--; if(length<=0) break; switch(type){ case 0x1: printf("NDflag:%x\n", *data); break; case 0x9: printf("LocalName:"); for(i = 0; i < MIN(length,elemlen); i++){ putchar(data[i]); } printf("\n"); break; default: printf("Type%d:", type); for(i=0; i < MIN(length,elemlen); i++){ printf("%02x ",data[i]); } printf("\n"); break; } data += elemlen; length -= elemlen; } return 0; } #undef MIN /* Send Read_Neighbor_Cache command to the node */ static int hci_read_neighbor_cache(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_neighbor_cache r; int n, error = OK; const char *addrtype2str[] = {"B", "P", "R", "E"}; memset(&r, 0, sizeof(r)); r.num_entries = NG_HCI_MAX_NEIGHBOR_NUM; r.entries = calloc(NG_HCI_MAX_NEIGHBOR_NUM, sizeof(ng_hci_node_neighbor_cache_entry_ep)); if (r.entries == NULL) { errno = ENOMEM; return (ERROR); } if (ioctl(s, SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE, &r, sizeof(r)) < 0) { error = ERROR; goto out; } fprintf(stdout, "T " \ "BD_ADDR " \ "Features " \ "Clock offset " \ "Page scan " \ "Rep. scan\n"); for (n = 0; n < r.num_entries; n++) { uint8_t addrtype = r.entries[n].addrtype; if(addrtype >= sizeof(addrtype2str)/sizeof(addrtype2str[0])) addrtype = sizeof(addrtype2str)/sizeof(addrtype2str[0]) - 1; fprintf(stdout, "%1s %-17.17s " \ "%02x %02x %02x %02x %02x %02x %02x %02x " \ "%#12x " \ "%#9x " \ "%#9x\n", addrtype2str[addrtype], hci_bdaddr2str(&r.entries[n].bdaddr), r.entries[n].features[0], r.entries[n].features[1], r.entries[n].features[2], r.entries[n].features[3], r.entries[n].features[4], r.entries[n].features[5], r.entries[n].features[6], r.entries[n].features[7], r.entries[n].clock_offset, r.entries[n].page_scan_mode, r.entries[n].page_scan_rep_mode); hci_dump_adv(r.entries[n].extinq_data, r.entries[n].extinq_size); fprintf(stdout,"\n"); } out: free(r.entries); return (error); } /* hci_read_neightbor_cache */ /* Send Read_Connection_List command to the node */ static int hci_read_connection_list(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_con_list r; int n, error = OK; memset(&r, 0, sizeof(r)); r.num_connections = NG_HCI_MAX_CON_NUM; r.connections = calloc(NG_HCI_MAX_CON_NUM, sizeof(ng_hci_node_con_ep)); if (r.connections == NULL) { errno = ENOMEM; return (ERROR); } if (ioctl(s, SIOC_HCI_RAW_NODE_GET_CON_LIST, &r, sizeof(r)) < 0) { error = ERROR; goto out; } fprintf(stdout, "Remote BD_ADDR " \ "Handle " \ "Type " \ "Mode " \ "Role " \ "Encrypt " \ "Pending " \ "Queue " \ "State\n"); for (n = 0; n < r.num_connections; n++) { fprintf(stdout, "%-17.17s " \ "%6d " \ "%4.4s " \ "%4d " \ "%4.4s " \ "%7.7s " \ "%7d " \ "%5d " \ "%s\n", hci_bdaddr2str(&r.connections[n].bdaddr), r.connections[n].con_handle, (r.connections[n].link_type == NG_HCI_LINK_ACL)? "ACL" : "SCO", r.connections[n].mode, (r.connections[n].role == NG_HCI_ROLE_MASTER)? "MAST" : "SLAV", hci_encrypt2str(r.connections[n].encryption_mode, 1), r.connections[n].pending, r.connections[n].queue_len, hci_con_state2str(r.connections[n].state)); } out: free(r.connections); return (error); } /* hci_read_connection_list */ /* Send Read_Node_Link_Policy_Settings_Mask command to the node */ int hci_read_node_link_policy_settings_mask(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_link_policy_mask r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Link Policy Settings mask: %#04x\n", r.policy_mask); return (OK); } /* hci_read_node_link_policy_settings_mask */ /* Send Write_Node_Link_Policy_Settings_Mask command to the node */ int hci_write_node_link_policy_settings_mask(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_link_policy_mask r; int m; memset(&r, 0, sizeof(r)); switch (argc) { case 1: if (sscanf(argv[0], "%x", &m) != 1) return (USAGE); r.policy_mask = (m & 0xffff); break; default: return (USAGE); } if (ioctl(s, SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK, &r, sizeof(r)) < 0) return (ERROR); return (OK); } /* hci_write_node_link_policy_settings_mask */ /* Send Read_Node_Packet_Mask command to the node */ int hci_read_node_packet_mask(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_packet_mask r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_PACKET_MASK, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Packet mask: %#04x\n", r.packet_mask); return (OK); } /* hci_read_node_packet_mask */ /* Send Write_Node_Packet_Mask command to the node */ int hci_write_node_packet_mask(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_packet_mask r; int m; memset(&r, 0, sizeof(r)); switch (argc) { case 1: if (sscanf(argv[0], "%x", &m) != 1) return (USAGE); r.packet_mask = (m & 0xffff); break; default: return (USAGE); } if (ioctl(s, SIOC_HCI_RAW_NODE_SET_PACKET_MASK, &r, sizeof(r)) < 0) return (ERROR); return (OK); } /* hci_write_node_packet_mask */ /* Send Read_Node_Role_Switch command to the node */ int hci_read_node_role_switch(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_role_switch r; memset(&r, 0, sizeof(r)); if (ioctl(s, SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH, &r, sizeof(r)) < 0) return (ERROR); fprintf(stdout, "Role switch: %d\n", r.role_switch); return (OK); } /* hci_read_node_role_switch */ /* Send Write_Node_Role_Switch command to the node */ int hci_write_node_role_switch(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_role_switch r; int m; memset(&r, 0, sizeof(r)); switch (argc) { case 1: if (sscanf(argv[0], "%d", &m) != 1) return (USAGE); r.role_switch = m? 1 : 0; break; default: return (USAGE); } if (ioctl(s, SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH, &r, sizeof(r)) < 0) return (ERROR); return (OK); } /* hci_write_node_role_switch */ /* Send Read_Node_List command to the node */ int hci_read_node_list(int s, int argc, char **argv) { struct ng_btsocket_hci_raw_node_list_names r; int i; r.num_names = MAX_NODE_NUM; r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo)); if (r.names == NULL) return (ERROR); if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) { free(r.names); return (ERROR); } fprintf(stdout, "Name ID Num hooks\n"); for (i = 0; i < r.num_names; ++i) fprintf(stdout, "%-15s %08x %9d\n", r.names[i].name, r.names[i].id, r.names[i].hooks); free(r.names); return (OK); } /* hci_read_node_list */ struct hci_command node_commands[] = { { "read_node_state", "Get the HCI node state", &hci_read_node_state }, { "initialize", "Initialize the HCI node", &hci_node_initialize }, { "read_debug_level", "Read the HCI node debug level", &hci_read_debug_level }, { "write_debug_level ", "Write the HCI node debug level", &hci_write_debug_level }, { "read_node_buffer_size", "Read the HCI node buffer information. This will return current state of the\n"\ "HCI buffer for the HCI node", &hci_read_node_buffer_size }, { "read_node_bd_addr", "Read the HCI node BD_ADDR. Returns device BD_ADDR as cached by the HCI node", &hci_read_node_bd_addr }, { "read_node_features", "Read the HCI node features. This will return list of supported features as\n" \ "cached by the HCI node", &hci_read_node_features }, { "read_node_stat", "Read packets and bytes counters for the HCI node", &hci_read_node_stat }, { "reset_node_stat", "Reset packets and bytes counters for the HCI node", &hci_reset_node_stat }, { "flush_neighbor_cache", "Flush content of the HCI node neighbor cache", &hci_flush_neighbor_cache }, { "read_neighbor_cache", "Read content of the HCI node neighbor cache", &hci_read_neighbor_cache }, { "read_connection_list", "Read the baseband connection descriptors list for the HCI node", &hci_read_connection_list }, { "read_node_link_policy_settings_mask", "Read the value of the Link Policy Settinngs mask for the HCI node", &hci_read_node_link_policy_settings_mask }, { "write_node_link_policy_settings_mask ", "Write the value of the Link Policy Settings mask for the HCI node. By default\n" \ "all supported Link Policy modes (as reported by the local device features) are\n"\ "enabled. The particular Link Policy mode is enabled if local device supports\n"\ "it and correspinding bit in the mask was set\n\n" \ "\t - xxxx; Link Policy mask\n" \ "\t\t0x0000 - Disable All LM Modes\n" \ "\t\t0x0001 - Enable Master Slave Switch\n" \ "\t\t0x0002 - Enable Hold Mode\n" \ "\t\t0x0004 - Enable Sniff Mode\n" \ "\t\t0x0008 - Enable Park Mode\n", &hci_write_node_link_policy_settings_mask }, { "read_node_packet_mask", "Read the value of the Packet mask for the HCI node", &hci_read_node_packet_mask }, { "write_node_packet_mask ", "Write the value of the Packet mask for the HCI node. By default all supported\n" \ "packet types (as reported by the local device features) are enabled. The\n" \ "particular packet type is enabled if local device supports it and corresponding\n" \ "bit in the mask was set\n\n" \ "\t - xxxx; packet type mask\n" \ "" \ "\t\tACL packets\n" \ "\t\t-----------\n" \ "\t\t0x0008 DM1\n" \ "\t\t0x0010 DH1\n" \ "\t\t0x0400 DM3\n" \ "\t\t0x0800 DH3\n" \ "\t\t0x4000 DM5\n" \ "\t\t0x8000 DH5\n" \ "\n" \ "\t\tSCO packets\n" \ "\t\t-----------\n" \ "\t\t0x0020 HV1\n" \ "\t\t0x0040 HV2\n" \ "\t\t0x0080 HV3\n", &hci_write_node_packet_mask }, { "read_node_role_switch", "Read the value of the Role Switch parameter for the HCI node", &hci_read_node_role_switch }, { "write_node_role_switch {0|1}", "Write the value of the Role Switch parameter for the HCI node. By default,\n" \ "if Role Switch is supported, local device will try to perform Role Switch\n" \ "and become Master on incoming connection. Some devices do not support Role\n" \ "Switch and thus incoming connections from such devices will fail. Setting\n" \ "this parameter to zero will prevent Role Switch and thus accepting device\n" \ "will remain Slave", &hci_write_node_role_switch }, { "read_node_list", "Get a list of HCI nodes, their Netgraph IDs and connected hooks.", &hci_read_node_list }, { NULL, }}; Index: head/usr.sbin/bluetooth/hccontrol/util.c =================================================================== --- head/usr.sbin/bluetooth/hccontrol/util.c (revision 360069) +++ head/usr.sbin/bluetooth/hccontrol/util.c (revision 360070) @@ -1,423 +1,473 @@ /*- * util.c * * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2001 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $ * $FreeBSD$ */ #include #define L2CAP_SOCKET_CHECKED #include #include #include #define SIZE(x) (sizeof((x))/sizeof((x)[0])) char const * hci_link2str(int link_type) { static char const * const t[] = { /* NG_HCI_LINK_SCO */ "SCO", /* NG_HCI_LINK_ACL */ "ACL" }; return (link_type >= SIZE(t)? "?" : t[link_type]); } /* hci_link2str */ char const * hci_pin2str(int type) { static char const * const t[] = { /* 0x00 */ "Variable PIN", /* 0x01 */ "Fixed PIN" }; return (type >= SIZE(t)? "?" : t[type]); } /* hci_pin2str */ char const * hci_scan2str(int scan) { static char const * const t[] = { /* 0x00 */ "No Scan enabled", /* 0x01 */ "Inquiry Scan enabled. Page Scan disabled", /* 0x02 */ "Inquiry Scan disabled. Page Scan enabled", /* 0x03 */ "Inquiry Scan enabled. Page Scan enabled" }; return (scan >= SIZE(t)? "?" : t[scan]); } /* hci_scan2str */ char const * hci_encrypt2str(int encrypt, int brief) { static char const * const t[] = { /* 0x00 */ "Disabled", /* 0x01 */ "Only for point-to-point packets", /* 0x02 */ "Both point-to-point and broadcast packets" }; static char const * const t1[] = { /* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE", /* NG_HCI_ENCRYPTION_MODE_P2P */ "P2P", /* NG_HCI_ENCRYPTION_MODE_ALL */ "ALL", }; if (brief) return (encrypt >= SIZE(t1)? "?" : t1[encrypt]); return (encrypt >= SIZE(t)? "?" : t[encrypt]); } /* hci_encrypt2str */ char const * hci_coding2str(int coding) { static char const * const t[] = { /* 0x00 */ "Linear", /* 0x01 */ "u-law", /* 0x02 */ "A-law", /* 0x03 */ "Reserved" }; return (coding >= SIZE(t)? "?" : t[coding]); } /* hci_coding2str */ char const * hci_vdata2str(int data) { static char const * const t[] = { /* 0x00 */ "1's complement", /* 0x01 */ "2's complement", /* 0x02 */ "Sign-Magnitude", /* 0x03 */ "Reserved" }; return (data >= SIZE(t)? "?" : t[data]); } /* hci_vdata2str */ char const * hci_hmode2str(int mode, char *buffer, int size) { static char const * const t[] = { /* 0x01 */ "Suspend Page Scan ", /* 0x02 */ "Suspend Inquiry Scan ", /* 0x04 */ "Suspend Periodic Inquiries " }; if (buffer != NULL && size > 0) { int n; memset(buffer, 0, size); for (n = 0; n < SIZE(t); n++) { int len = strlen(buffer); if (len >= size) break; if (mode & (1 << n)) strncat(buffer, t[n], size - len); } } return (buffer); } /* hci_hmode2str */ char const * hci_ver2str(int ver) { static char const * const t[] = { /* 0x00 */ "Bluetooth HCI Specification 1.0B", /* 0x01 */ "Bluetooth HCI Specification 1.1", /* 0x02 */ "Bluetooth HCI Specification 1.2", /* 0x03 */ "Bluetooth HCI Specification 2.0", /* 0x04 */ "Bluetooth HCI Specification 2.1", /* 0x05 */ "Bluetooth HCI Specification 3.0", /* 0x06 */ "Bluetooth HCI Specification 4.0", /* 0x07 */ "Bluetooth HCI Specification 4.1", /* 0x08 */ "Bluetooth HCI Specification 4.2" }; return (ver >= SIZE(t)? "?" : t[ver]); } /* hci_ver2str */ char const * hci_lmpver2str(int ver) { static char const * const t[] = { /* 0x00 */ "Bluetooth LMP 1.0", /* 0x01 */ "Bluetooth LMP 1.1", /* 0x02 */ "Bluetooth LMP 1.2", /* 0x03 */ "Bluetooth LMP 2.0", /* 0x04 */ "Bluetooth LMP 2.1", /* 0x04 */ "Bluetooth LMP 3.0", /* 0x04 */ "Bluetooth LMP 4.0", /* 0x04 */ "Bluetooth LMP 4.1", /* 0x04 */ "Bluetooth LMP 4.2" }; return (ver >= SIZE(t)? "?" : t[ver]); } /* hci_lmpver2str */ char const * hci_manufacturer2str(int m) { static char const * const t[] = { /* 0000 */ "Ericsson Technology Licensing", /* 0001 */ "Nokia Mobile Phones", /* 0002 */ "Intel Corp.", /* 0003 */ "IBM Corp.", /* 0004 */ "Toshiba Corp.", /* 0005 */ "3Com", /* 0006 */ "Microsoft", /* 0007 */ "Lucent", /* 0008 */ "Motorola", /* 0009 */ "Infineon Technologies AG", /* 0010 */ "Cambridge Silicon Radio", /* 0011 */ "Silicon Wave", /* 0012 */ "Digianswer A/S", /* 0013 */ "Texas Instruments Inc.", /* 0014 */ "Parthus Technologies Inc.", /* 0015 */ "Broadcom Corporation", /* 0016 */ "Mitel Semiconductor", /* 0017 */ "Widcomm, Inc.", /* 0018 */ "Zeevo, Inc.", /* 0019 */ "Atmel Corporation", /* 0020 */ "Mitsubishi Electric Corporation", /* 0021 */ "RTX Telecom A/S", /* 0022 */ "KC Technology Inc.", /* 0023 */ "Newlogic", /* 0024 */ "Transilica, Inc.", /* 0025 */ "Rohde & Schwartz GmbH & Co. KG", /* 0026 */ "TTPCom Limited", /* 0027 */ "Signia Technologies, Inc.", /* 0028 */ "Conexant Systems Inc.", /* 0029 */ "Qualcomm", /* 0030 */ "Inventel", /* 0031 */ "AVM Berlin", /* 0032 */ "BandSpeed, Inc.", /* 0033 */ "Mansella Ltd", /* 0034 */ "NEC Corporation", /* 0035 */ "WavePlus Technology Co., Ltd.", /* 0036 */ "Alcatel", /* 0037 */ "Philips Semiconductors", /* 0038 */ "C Technologies", /* 0039 */ "Open Interface", /* 0040 */ "R F Micro Devices", /* 0041 */ "Hitachi Ltd", /* 0042 */ "Symbol Technologies, Inc.", /* 0043 */ "Tenovis", /* 0044 */ "Macronix International Co. Ltd.", /* 0045 */ "GCT Semiconductor", /* 0046 */ "Norwood Systems", /* 0047 */ "MewTel Technology Inc.", /* 0048 */ "ST Microelectronics", /* 0049 */ "Synopsys", /* 0050 */ "Red-M (Communications) Ltd", /* 0051 */ "Commil Ltd", /* 0052 */ "Computer Access Technology Corporation (CATC)", /* 0053 */ "Eclipse (HQ Espana) S.L.", /* 0054 */ "Renesas Technology Corp.", /* 0055 */ "Mobilian Corporation", /* 0056 */ "Terax", /* 0057 */ "Integrated System Solution Corp.", /* 0058 */ "Matsushita Electric Industrial Co., Ltd.", /* 0059 */ "Gennum Corporation", /* 0060 */ "Research In Motion", /* 0061 */ "IPextreme, Inc.", /* 0062 */ "Systems and Chips, Inc", /* 0063 */ "Bluetooth SIG, Inc", /* 0064 */ "Seiko Epson Corporation" }; return (m >= SIZE(t)? "?" : t[m]); } /* hci_manufacturer2str */ char const * hci_features2str(uint8_t *features, char *buffer, int size) { static char const * const t[][8] = { { /* byte 0 */ /* 0 */ "<3-Slot> ", /* 1 */ "<5-Slot> ", /* 2 */ " ", /* 3 */ " ", /* 4 */ " ", /* 5 */ " ", /* 6 */ " ", /* 7 */ " " }, { /* byte 1 */ /* 0 */ " ", /* 1 */ " ", /* 2 */ " ", /* 3 */ " ", /* 4 */ " ", /* 5 */ " ", /* 6 */ " ", /* 7 */ " " }, { /* byte 2 */ /* 0 */ " ", /* 1 */ " ", /* 2 */ " ", /* 3 */ " ", /* 4 */ " ", /* 5 */ " ", /* 6 */ " ", - /* 7 */ " " + /* 7 */ " " + }, + { /* byte 3 */ + /* 0 */ " ", + /* 1 */ " ", + /* 2 */ " ", + /* 3 */ " ", + /* 4 */ " ", + /* 5 */ " ", + /* 6 */ " ", + /* 7 */ " " + }, + { /* byte 4 */ + /* 0 */ " ", + /* 1 */ " ", + /* 2 */ " ", + /* 3 */ " ", + /* 4 */ " ", + /* 5 */ "
", + /* 6 */ " ", + /* 7 */ "<3-Slot EDR ACL packets> " + }, + { /* byte 5 */ + /* 0 */ "<5-Slot EDR ACL packets> ", + /* 1 */ " ", + /* 2 */ " ", + /* 3 */ " ", + /* 4 */ " ", + /* 5 */ " ", + /* 6 */ " ", + /* 7 */ "<3-Slot EDR eSCO packets> " + }, + { /* byte 6 */ + /* 0 */ " ", + /* 1 */ " ", + /* 2 */ " ", + /* 3 */ " ", + /* 4 */ " ", + /* 5 */ " ", + /* 6 */ " ", + /* 7 */ " " + }, + { /* byte 7 */ + /* 0 */ " ", + /* 1 */ " ", + /* 2 */ " ", + /* 3 */ " ", + /* 4 */ " ", + /* 5 */ " ", + /* 6 */ " ", + /* 7 */ " " }}; if (buffer != NULL && size > 0) { int n, i, len0, len1; memset(buffer, 0, size); len1 = 0; for (n = 0; n < SIZE(t); n++) { for (i = 0; i < SIZE(t[n]); i++) { len0 = strlen(buffer); if (len0 >= size) goto done; if (features[n] & (1 << i)) { if (len1 + strlen(t[n][i]) > 60) { len1 = 0; buffer[len0 - 1] = '\n'; } len1 += strlen(t[n][i]); strncat(buffer, t[n][i], size - len0); } } } } done: return (buffer); } /* hci_features2str */ char const * hci_cc2str(int cc) { static char const * const t[] = { /* 0x00 */ "North America, Europe, Japan", /* 0x01 */ "France" }; return (cc >= SIZE(t)? "?" : t[cc]); } /* hci_cc2str */ char const * hci_con_state2str(int state) { static char const * const t[] = { /* NG_HCI_CON_CLOSED */ "CLOSED", /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP", /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE", /* NG_HCI_CON_OPEN */ "OPEN" }; return (state >= SIZE(t)? "UNKNOWN" : t[state]); } /* hci_con_state2str */ char const * hci_status2str(int status) { static char const * const t[] = { /* 0x00 */ "No error", /* 0x01 */ "Unknown HCI command", /* 0x02 */ "No connection", /* 0x03 */ "Hardware failure", /* 0x04 */ "Page timeout", /* 0x05 */ "Authentication failure", /* 0x06 */ "Key missing", /* 0x07 */ "Memory full", /* 0x08 */ "Connection timeout", /* 0x09 */ "Max number of connections", /* 0x0a */ "Max number of SCO connections to a unit", /* 0x0b */ "ACL connection already exists", /* 0x0c */ "Command disallowed", /* 0x0d */ "Host rejected due to limited resources", /* 0x0e */ "Host rejected due to security reasons", /* 0x0f */ "Host rejected due to remote unit is a personal unit", /* 0x10 */ "Host timeout", /* 0x11 */ "Unsupported feature or parameter value", /* 0x12 */ "Invalid HCI command parameter", /* 0x13 */ "Other end terminated connection: User ended connection", /* 0x14 */ "Other end terminated connection: Low resources", /* 0x15 */ "Other end terminated connection: About to power off", /* 0x16 */ "Connection terminated by local host", /* 0x17 */ "Repeated attempts", /* 0x18 */ "Pairing not allowed", /* 0x19 */ "Unknown LMP PDU", /* 0x1a */ "Unsupported remote feature", /* 0x1b */ "SCO offset rejected", /* 0x1c */ "SCO interval rejected", /* 0x1d */ "SCO air mode rejected", /* 0x1e */ "Invalid LMP parameters", /* 0x1f */ "Unspecified error", /* 0x20 */ "Unsupported LMP parameter value", /* 0x21 */ "Role change not allowed", /* 0x22 */ "LMP response timeout", /* 0x23 */ "LMP error transaction collision", /* 0x24 */ "LMP PSU not allowed", /* 0x25 */ "Encryption mode not acceptable", /* 0x26 */ "Unit key used", /* 0x27 */ "QoS is not supported", /* 0x28 */ "Instant passed", /* 0x29 */ "Pairing with unit key not supported" }; return (status >= SIZE(t)? "Unknown error" : t[status]); } /* hci_status2str */ char const * hci_bdaddr2str(bdaddr_t const *ba) { extern int numeric_bdaddr; static char buffer[MAXHOSTNAMELEN]; struct hostent *he = NULL; if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) { buffer[0] = '*'; buffer[1] = 0; return (buffer); } if (!numeric_bdaddr && (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) { strlcpy(buffer, he->h_name, sizeof(buffer)); return (buffer); } bt_ntoa(ba, buffer); return (buffer); } /* hci_bdaddr2str */