util.c revision 361156
1/*- 2 * util.c 3 * 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $ 31 * $FreeBSD: stable/11/usr.sbin/bluetooth/hccontrol/util.c 361156 2020-05-18 08:44:49Z hselasky $ 32 */ 33 34#include <sys/param.h> 35#define L2CAP_SOCKET_CHECKED 36#include <bluetooth.h> 37#include <stdio.h> 38#include <string.h> 39 40#define SIZE(x) (sizeof((x))/sizeof((x)[0])) 41 42char const * 43hci_link2str(int link_type) 44{ 45 static char const * const t[] = { 46 /* NG_HCI_LINK_SCO */ "SCO", 47 /* NG_HCI_LINK_ACL */ "ACL" 48 }; 49 50 return (link_type >= SIZE(t)? "?" : t[link_type]); 51} /* hci_link2str */ 52 53char const * 54hci_pin2str(int type) 55{ 56 static char const * const t[] = { 57 /* 0x00 */ "Variable PIN", 58 /* 0x01 */ "Fixed PIN" 59 }; 60 61 return (type >= SIZE(t)? "?" : t[type]); 62} /* hci_pin2str */ 63 64char const * 65hci_scan2str(int scan) 66{ 67 static char const * const t[] = { 68 /* 0x00 */ "No Scan enabled", 69 /* 0x01 */ "Inquiry Scan enabled. Page Scan disabled", 70 /* 0x02 */ "Inquiry Scan disabled. Page Scan enabled", 71 /* 0x03 */ "Inquiry Scan enabled. Page Scan enabled" 72 }; 73 74 return (scan >= SIZE(t)? "?" : t[scan]); 75} /* hci_scan2str */ 76 77char const * 78hci_encrypt2str(int encrypt, int brief) 79{ 80 static char const * const t[] = { 81 /* 0x00 */ "Disabled", 82 /* 0x01 */ "Only for point-to-point packets", 83 /* 0x02 */ "Both point-to-point and broadcast packets" 84 }; 85 86 static char const * const t1[] = { 87 /* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE", 88 /* NG_HCI_ENCRYPTION_MODE_P2P */ "P2P", 89 /* NG_HCI_ENCRYPTION_MODE_ALL */ "ALL", 90 }; 91 92 if (brief) 93 return (encrypt >= SIZE(t1)? "?" : t1[encrypt]); 94 95 return (encrypt >= SIZE(t)? "?" : t[encrypt]); 96} /* hci_encrypt2str */ 97 98char const * 99hci_coding2str(int coding) 100{ 101 static char const * const t[] = { 102 /* 0x00 */ "Linear", 103 /* 0x01 */ "u-law", 104 /* 0x02 */ "A-law", 105 /* 0x03 */ "Reserved" 106 }; 107 108 return (coding >= SIZE(t)? "?" : t[coding]); 109} /* hci_coding2str */ 110 111char const * 112hci_vdata2str(int data) 113{ 114 static char const * const t[] = { 115 /* 0x00 */ "1's complement", 116 /* 0x01 */ "2's complement", 117 /* 0x02 */ "Sign-Magnitude", 118 /* 0x03 */ "Reserved" 119 }; 120 121 return (data >= SIZE(t)? "?" : t[data]); 122} /* hci_vdata2str */ 123 124char const * 125hci_hmode2str(int mode, char *buffer, int size) 126{ 127 static char const * const t[] = { 128 /* 0x01 */ "Suspend Page Scan ", 129 /* 0x02 */ "Suspend Inquiry Scan ", 130 /* 0x04 */ "Suspend Periodic Inquiries " 131 }; 132 133 if (buffer != NULL && size > 0) { 134 int n; 135 136 memset(buffer, 0, size); 137 for (n = 0; n < SIZE(t); n++) { 138 int len = strlen(buffer); 139 140 if (len >= size) 141 break; 142 if (mode & (1 << n)) 143 strncat(buffer, t[n], size - len); 144 } 145 } 146 147 return (buffer); 148} /* hci_hmode2str */ 149 150char const * 151hci_ver2str(int ver) 152{ 153 static char const * const t[] = { 154 /* 0x00 */ "Bluetooth HCI Specification 1.0B", 155 /* 0x01 */ "Bluetooth HCI Specification 1.1", 156 /* 0x02 */ "Bluetooth HCI Specification 1.2", 157 /* 0x03 */ "Bluetooth HCI Specification 2.0", 158 /* 0x04 */ "Bluetooth HCI Specification 2.1", 159 /* 0x05 */ "Bluetooth HCI Specification 3.0", 160 /* 0x06 */ "Bluetooth HCI Specification 4.0", 161 /* 0x07 */ "Bluetooth HCI Specification 4.1", 162 /* 0x08 */ "Bluetooth HCI Specification 4.2" 163 }; 164 165 return (ver >= SIZE(t)? "?" : t[ver]); 166} /* hci_ver2str */ 167 168char const * 169hci_lmpver2str(int ver) 170{ 171 static char const * const t[] = { 172 /* 0x00 */ "Bluetooth LMP 1.0", 173 /* 0x01 */ "Bluetooth LMP 1.1", 174 /* 0x02 */ "Bluetooth LMP 1.2", 175 /* 0x03 */ "Bluetooth LMP 2.0", 176 /* 0x04 */ "Bluetooth LMP 2.1", 177 /* 0x04 */ "Bluetooth LMP 3.0", 178 /* 0x04 */ "Bluetooth LMP 4.0", 179 /* 0x04 */ "Bluetooth LMP 4.1", 180 /* 0x04 */ "Bluetooth LMP 4.2" 181 }; 182 183 return (ver >= SIZE(t)? "?" : t[ver]); 184} /* hci_lmpver2str */ 185 186char const * 187hci_manufacturer2str(int m) 188{ 189 static char const * const t[] = { 190 /* 0000 */ "Ericsson Technology Licensing", 191 /* 0001 */ "Nokia Mobile Phones", 192 /* 0002 */ "Intel Corp.", 193 /* 0003 */ "IBM Corp.", 194 /* 0004 */ "Toshiba Corp.", 195 /* 0005 */ "3Com", 196 /* 0006 */ "Microsoft", 197 /* 0007 */ "Lucent", 198 /* 0008 */ "Motorola", 199 /* 0009 */ "Infineon Technologies AG", 200 /* 0010 */ "Cambridge Silicon Radio", 201 /* 0011 */ "Silicon Wave", 202 /* 0012 */ "Digianswer A/S", 203 /* 0013 */ "Texas Instruments Inc.", 204 /* 0014 */ "Parthus Technologies Inc.", 205 /* 0015 */ "Broadcom Corporation", 206 /* 0016 */ "Mitel Semiconductor", 207 /* 0017 */ "Widcomm, Inc.", 208 /* 0018 */ "Zeevo, Inc.", 209 /* 0019 */ "Atmel Corporation", 210 /* 0020 */ "Mitsubishi Electric Corporation", 211 /* 0021 */ "RTX Telecom A/S", 212 /* 0022 */ "KC Technology Inc.", 213 /* 0023 */ "Newlogic", 214 /* 0024 */ "Transilica, Inc.", 215 /* 0025 */ "Rohde & Schwartz GmbH & Co. KG", 216 /* 0026 */ "TTPCom Limited", 217 /* 0027 */ "Signia Technologies, Inc.", 218 /* 0028 */ "Conexant Systems Inc.", 219 /* 0029 */ "Qualcomm", 220 /* 0030 */ "Inventel", 221 /* 0031 */ "AVM Berlin", 222 /* 0032 */ "BandSpeed, Inc.", 223 /* 0033 */ "Mansella Ltd", 224 /* 0034 */ "NEC Corporation", 225 /* 0035 */ "WavePlus Technology Co., Ltd.", 226 /* 0036 */ "Alcatel", 227 /* 0037 */ "Philips Semiconductors", 228 /* 0038 */ "C Technologies", 229 /* 0039 */ "Open Interface", 230 /* 0040 */ "R F Micro Devices", 231 /* 0041 */ "Hitachi Ltd", 232 /* 0042 */ "Symbol Technologies, Inc.", 233 /* 0043 */ "Tenovis", 234 /* 0044 */ "Macronix International Co. Ltd.", 235 /* 0045 */ "GCT Semiconductor", 236 /* 0046 */ "Norwood Systems", 237 /* 0047 */ "MewTel Technology Inc.", 238 /* 0048 */ "ST Microelectronics", 239 /* 0049 */ "Synopsys", 240 /* 0050 */ "Red-M (Communications) Ltd", 241 /* 0051 */ "Commil Ltd", 242 /* 0052 */ "Computer Access Technology Corporation (CATC)", 243 /* 0053 */ "Eclipse (HQ Espana) S.L.", 244 /* 0054 */ "Renesas Technology Corp.", 245 /* 0055 */ "Mobilian Corporation", 246 /* 0056 */ "Terax", 247 /* 0057 */ "Integrated System Solution Corp.", 248 /* 0058 */ "Matsushita Electric Industrial Co., Ltd.", 249 /* 0059 */ "Gennum Corporation", 250 /* 0060 */ "Research In Motion", 251 /* 0061 */ "IPextreme, Inc.", 252 /* 0062 */ "Systems and Chips, Inc", 253 /* 0063 */ "Bluetooth SIG, Inc", 254 /* 0064 */ "Seiko Epson Corporation" 255 }; 256 257 return (m >= SIZE(t)? "?" : t[m]); 258} /* hci_manufacturer2str */ 259 260char const * 261hci_features2str(uint8_t *features, char *buffer, int size) 262{ 263 static char const * const t[][8] = { 264 { /* byte 0 */ 265 /* 0 */ "<3-Slot> ", 266 /* 1 */ "<5-Slot> ", 267 /* 2 */ "<Encryption> ", 268 /* 3 */ "<Slot offset> ", 269 /* 4 */ "<Timing accuracy> ", 270 /* 5 */ "<Switch> ", 271 /* 6 */ "<Hold mode> ", 272 /* 7 */ "<Sniff mode> " 273 }, 274 { /* byte 1 */ 275 /* 0 */ "<Park mode> ", 276 /* 1 */ "<RSSI> ", 277 /* 2 */ "<Channel quality> ", 278 /* 3 */ "<SCO link> ", 279 /* 4 */ "<HV2 packets> ", 280 /* 5 */ "<HV3 packets> ", 281 /* 6 */ "<u-law log> ", 282 /* 7 */ "<A-law log> " 283 }, 284 { /* byte 2 */ 285 /* 0 */ "<CVSD> ", 286 /* 1 */ "<Paging scheme> ", 287 /* 2 */ "<Power control> ", 288 /* 3 */ "<Transparent SCO data> ", 289 /* 4 */ "<Flow control lag (bit0)> ", 290 /* 5 */ "<Flow control lag (bit1)> ", 291 /* 6 */ "<Flow control lag (bit2)> ", 292 /* 7 */ "<Broadcast Encryption> " 293 }, 294 { /* byte 3 */ 295 /* 0 */ "<Unknown 3.0> ", 296 /* 1 */ "<EDR ACL 2 Mb/s> ", 297 /* 2 */ "<EDR ACL 3 Mb/s> ", 298 /* 3 */ "<Enhanced inquiry scan> ", 299 /* 4 */ "<Interlaced inquiry scan> ", 300 /* 5 */ "<Interlaced page scan> ", 301 /* 6 */ "<RSSI with inquiry results> ", 302 /* 7 */ "<Extended SCO link (EV3 packets)> " 303 }, 304 { /* byte 4 */ 305 /* 0 */ "<EV4 packets> ", 306 /* 1 */ "<EV5 packets> ", 307 /* 2 */ "<Unknown 4.2> ", 308 /* 3 */ "<AFH capable slave> ", 309 /* 4 */ "<AFH classification slave> ", 310 /* 5 */ "<BR/EDR Not Supported> ", 311 /* 6 */ "<LE Supported (Controller)> ", 312 /* 7 */ "<3-Slot EDR ACL packets> " 313 }, 314 { /* byte 5 */ 315 /* 0 */ "<5-Slot EDR ACL packets> ", 316 /* 1 */ "<Sniff subrating> ", 317 /* 2 */ "<Pause encryption> ", 318 /* 3 */ "<AFH capable master> ", 319 /* 4 */ "<AFH classification master> ", 320 /* 5 */ "<EDR eSCO 2 Mb/s mode> ", 321 /* 6 */ "<EDR eSCO 3 Mb/s mode> ", 322 /* 7 */ "<3-Slot EDR eSCO packets> " 323 }, 324 { /* byte 6 */ 325 /* 0 */ "<Enhanced Inquiry Response> ", 326 /* 1 */ "<Simultaneous LE and BR/EDR (Controller)> ", 327 /* 2 */ "<Unknown 6.2> ", 328 /* 3 */ "<Secure Simple Pairing (Controller Support)> ", 329 /* 4 */ "<Encapsulated PDU> ", 330 /* 5 */ "<Erroneous Data Reporting> ", 331 /* 6 */ "<Non-flushable Packed Boundary Flag> ", 332 /* 7 */ "<Unknown 6.7> " 333 }, 334 { /* byte 7 */ 335 /* 0 */ "<HCI_Link_Supervision_Timeout_Changed event> ", 336 /* 1 */ "<Variable Inquiry TX Power Level> ", 337 /* 2 */ "<Enhanced Power Control> ", 338 /* 3 */ "<Unknown 7.3> ", 339 /* 4 */ "<Unknown 7.4> ", 340 /* 5 */ "<Unknown 7.5> ", 341 /* 6 */ "<Unknown 7.6> ", 342 /* 7 */ "<Extended features> " 343 }}; 344 345 if (buffer != NULL && size > 0) { 346 int n, i, len0, len1; 347 348 memset(buffer, 0, size); 349 len1 = 0; 350 351 for (n = 0; n < SIZE(t); n++) { 352 for (i = 0; i < SIZE(t[n]); i++) { 353 len0 = strlen(buffer); 354 if (len0 >= size) 355 goto done; 356 357 if (features[n] & (1 << i)) { 358 if (len1 + strlen(t[n][i]) > 60) { 359 len1 = 0; 360 buffer[len0 - 1] = '\n'; 361 } 362 363 len1 += strlen(t[n][i]); 364 strncat(buffer, t[n][i], size - len0); 365 } 366 } 367 } 368 } 369done: 370 return (buffer); 371} /* hci_features2str */ 372 373char const * 374hci_cc2str(int cc) 375{ 376 static char const * const t[] = { 377 /* 0x00 */ "North America, Europe, Japan", 378 /* 0x01 */ "France" 379 }; 380 381 return (cc >= SIZE(t)? "?" : t[cc]); 382} /* hci_cc2str */ 383 384char const * 385hci_con_state2str(int state) 386{ 387 static char const * const t[] = { 388 /* NG_HCI_CON_CLOSED */ "CLOSED", 389 /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP", 390 /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE", 391 /* NG_HCI_CON_OPEN */ "OPEN" 392 }; 393 394 return (state >= SIZE(t)? "UNKNOWN" : t[state]); 395} /* hci_con_state2str */ 396 397char const * 398hci_status2str(int status) 399{ 400 static char const * const t[] = { 401 /* 0x00 */ "No error", 402 /* 0x01 */ "Unknown HCI command", 403 /* 0x02 */ "No connection", 404 /* 0x03 */ "Hardware failure", 405 /* 0x04 */ "Page timeout", 406 /* 0x05 */ "Authentication failure", 407 /* 0x06 */ "Key missing", 408 /* 0x07 */ "Memory full", 409 /* 0x08 */ "Connection timeout", 410 /* 0x09 */ "Max number of connections", 411 /* 0x0a */ "Max number of SCO connections to a unit", 412 /* 0x0b */ "ACL connection already exists", 413 /* 0x0c */ "Command disallowed", 414 /* 0x0d */ "Host rejected due to limited resources", 415 /* 0x0e */ "Host rejected due to security reasons", 416 /* 0x0f */ "Host rejected due to remote unit is a personal unit", 417 /* 0x10 */ "Host timeout", 418 /* 0x11 */ "Unsupported feature or parameter value", 419 /* 0x12 */ "Invalid HCI command parameter", 420 /* 0x13 */ "Other end terminated connection: User ended connection", 421 /* 0x14 */ "Other end terminated connection: Low resources", 422 /* 0x15 */ "Other end terminated connection: About to power off", 423 /* 0x16 */ "Connection terminated by local host", 424 /* 0x17 */ "Repeated attempts", 425 /* 0x18 */ "Pairing not allowed", 426 /* 0x19 */ "Unknown LMP PDU", 427 /* 0x1a */ "Unsupported remote feature", 428 /* 0x1b */ "SCO offset rejected", 429 /* 0x1c */ "SCO interval rejected", 430 /* 0x1d */ "SCO air mode rejected", 431 /* 0x1e */ "Invalid LMP parameters", 432 /* 0x1f */ "Unspecified error", 433 /* 0x20 */ "Unsupported LMP parameter value", 434 /* 0x21 */ "Role change not allowed", 435 /* 0x22 */ "LMP response timeout", 436 /* 0x23 */ "LMP error transaction collision", 437 /* 0x24 */ "LMP PSU not allowed", 438 /* 0x25 */ "Encryption mode not acceptable", 439 /* 0x26 */ "Unit key used", 440 /* 0x27 */ "QoS is not supported", 441 /* 0x28 */ "Instant passed", 442 /* 0x29 */ "Pairing with unit key not supported", 443 /* 0x2a */ "Different Transaction Collision", 444 /* 0x2b */ "Unknown error (Reserved for future use)", 445 /* 0x2c */ "QoS Unacceptable Parameter", 446 /* 0x2d */ "QoS Rejected", 447 /* 0x2e */ "Channel Classification Not Supported", 448 /* 0x2f */ "Insufficient Security", 449 /* 0x30 */ "Parameter Out Of Mandatory Range", 450 /* 0x31 */ "Unknown error (Reserved for future use)", 451 /* 0x32 */ "Role Switch Pending", 452 /* 0x33 */ "Unknown error (Reserved for future use)", 453 /* 0x34 */ "Reserved Slot Violation", 454 /* 0x35 */ "Role Switch Failed", 455 /* 0x36 */ "Extended Inquiry Response Too Large", 456 /* 0x37 */ "Secure Simple Pairing Not Supported By Host", 457 /* 0x38 */ "Host Busy - Pairing", 458 /* 0x39 */ "Connection Rejected due to No Suitable Channel Found", 459 /* 0x3a */ "Controller Busy", 460 /* 0x3b */ "Unacceptable Connection Parameters", 461 /* 0x3c */ "Advertising Timeout", 462 /* 0x3d */ "Connection Terminated due to MIC Failure", 463 /* 0x3e */ "Connection Failed to be Established / Synchronization Timeout", 464 /* 0x3f */ "MAC Connection Failed", 465 /* 0x40 */ "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging", 466 /* 0x41 */ "Type0 Submap Not Defined", 467 /* 0x42 */ "Unknown Advertising Identifier", 468 /* 0x43 */ "Limit Reached", 469 /* 0x44 */ "Operation Cancelled by Host", 470 /* 0x45 */ "Packet Too Long" 471 }; 472 473 return (status >= SIZE(t)? "Unknown error" : t[status]); 474} /* hci_status2str */ 475 476char const * 477hci_bdaddr2str(bdaddr_t const *ba) 478{ 479 extern int numeric_bdaddr; 480 static char buffer[MAXHOSTNAMELEN]; 481 struct hostent *he = NULL; 482 483 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) { 484 buffer[0] = '*'; 485 buffer[1] = 0; 486 487 return (buffer); 488 } 489 490 if (!numeric_bdaddr && 491 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) { 492 strlcpy(buffer, he->h_name, sizeof(buffer)); 493 494 return (buffer); 495 } 496 497 bt_ntoa(ba, buffer); 498 499 return (buffer); 500} /* hci_bdaddr2str */ 501 502