4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $
34 #include <sys/param.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
40 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
43 hci_link2str(int link_type)
45 static char const * const t[] = {
46 /* NG_HCI_LINK_SCO */ "SCO",
47 /* NG_HCI_LINK_ACL */ "ACL"
50 return (link_type >= SIZE(t)? "?" : t[link_type]);
56 static char const * const t[] = {
57 /* 0x00 */ "Variable PIN",
58 /* 0x01 */ "Fixed PIN"
61 return (type >= SIZE(t)? "?" : t[type]);
65 hci_scan2str(int scan)
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"
74 return (scan >= SIZE(t)? "?" : t[scan]);
78 hci_encrypt2str(int encrypt, int brief)
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"
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",
93 return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
95 return (encrypt >= SIZE(t)? "?" : t[encrypt]);
96 } /* hci_encrypt2str */
99 hci_coding2str(int coding)
101 static char const * const t[] = {
105 /* 0x03 */ "Reserved"
108 return (coding >= SIZE(t)? "?" : t[coding]);
109 } /* hci_coding2str */
112 hci_vdata2str(int data)
114 static char const * const t[] = {
115 /* 0x00 */ "1's complement",
116 /* 0x01 */ "2's complement",
117 /* 0x02 */ "Sign-Magnitude",
118 /* 0x03 */ "Reserved"
121 return (data >= SIZE(t)? "?" : t[data]);
122 } /* hci_vdata2str */
125 hci_hmode2str(int mode, char *buffer, int size)
127 static char const * const t[] = {
128 /* 0x01 */ "Suspend Page Scan ",
129 /* 0x02 */ "Suspend Inquiry Scan ",
130 /* 0x04 */ "Suspend Periodic Inquiries "
133 if (buffer != NULL && size > 0) {
136 memset(buffer, 0, size);
137 for (n = 0; n < SIZE(t); n++) {
138 int len = strlen(buffer);
143 strncat(buffer, t[n], size - len);
148 } /* hci_hmode2str */
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"
165 return (ver >= SIZE(t)? "?" : t[ver]);
169 hci_lmpver2str(int ver)
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"
183 return (ver >= SIZE(t)? "?" : t[ver]);
184 } /* hci_lmpver2str */
187 hci_manufacturer2str(int m)
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.",
196 /* 0006 */ "Microsoft",
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",
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"
257 return (m >= SIZE(t)? "?" : t[m]);
258 } /* hci_manufacturer2str */
261 hci_features2str(uint8_t *features, char *buffer, int size)
263 static char const * const t[][8] = {
267 /* 2 */ "<Encryption> ",
268 /* 3 */ "<Slot offset> ",
269 /* 4 */ "<Timing accuracy> ",
271 /* 6 */ "<Hold mode> ",
272 /* 7 */ "<Sniff mode> "
275 /* 0 */ "<Park mode> ",
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> "
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> "
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)> "
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> "
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> "
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> "
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> "
345 if (buffer != NULL && size > 0) {
346 int n, i, len0, len1;
348 memset(buffer, 0, size);
351 for (n = 0; n < SIZE(t); n++) {
352 for (i = 0; i < SIZE(t[n]); i++) {
353 len0 = strlen(buffer);
357 if (features[n] & (1 << i)) {
358 if (len1 + strlen(t[n][i]) > 60) {
360 buffer[len0 - 1] = '\n';
363 len1 += strlen(t[n][i]);
364 strncat(buffer, t[n][i], size - len0);
371 } /* hci_features2str */
374 hci_le_features2str(uint8_t *features, char *buffer, int size)
376 static char const * const t[][8] = {
378 /* 0 */ "<LE Encryption> ",
379 /* 1 */ "<Connection Parameters Request Procedure> ",
380 /* 2 */ "<Extended Reject Indication> ",
381 /* 3 */ "<Slave-initiated Features Exchange> ",
382 /* 4 */ "<LE Ping> ",
383 /* 5 */ "<LE Data Packet Length Extension> ",
384 /* 6 */ "<LL Privacy> ",
385 /* 7 */ "<Extended Scanner Filter Policies> "
388 /* 0 */ "<LE 2M PHY> ",
389 /* 1 */ "<Stable Modulation Index - Transmitter> ",
390 /* 2 */ "<Stable Modulation Index - Receiver> ",
391 /* 3 */ "<LE Coded PHY> ",
392 /* 4 */ "<LE Extended Advertising> ",
393 /* 5 */ "<LE Periodic Advertising> ",
394 /* 6 */ "<Channel Selection Algorithm #2> ",
395 /* 7 */ "<LE Power Class 1> "
398 /* 0 */ "<Minimum Number of Used Channels Procedure> ",
399 /* 1 */ "<Connection CTE Request> ",
400 /* 2 */ "<Connection CTE Response> ",
401 /* 3 */ "<Connectionless CTE Transmitter> ",
402 /* 4 */ "<Connectionless CTE Receiver> ",
403 /* 5 */ "<Antenna Switching During CTE Transmission (AoD)> ",
404 /* 6 */ "<Antenna Switching During CTE Reception (AoA)> ",
405 /* 7 */ "<Receiving Constant Tone Extensions> "
408 /* 0 */ "<Periodic Advertising Sync Transfer - Sender> ",
409 /* 1 */ "<Periodic Advertising Sync Transfer - Recipient> ",
410 /* 2 */ "<Sleep Clock Accuracy Updates> ",
411 /* 3 */ "<Remote Public Key Validation> ",
412 /* 4 */ "<Connected Isochronous Stream - Master> ",
413 /* 5 */ "<Connected Isochronous Stream - Slave> ",
414 /* 6 */ "<Isochronous Broadcaster> ",
415 /* 7 */ "<Synchronized Receiver> "
418 /* 0 */ "<Isochronous Channels (Host Support)> ",
419 /* 1 */ "<LE Power Control Request> ",
420 /* 2 */ "<LE Power Change Indication> ",
421 /* 3 */ "<LE Path Loss Monitoring> ",
422 /* 4 */ "<Reserved for future use> ",
423 /* 5 */ "<Unknown 4.5> ",
424 /* 6 */ "<Unknown 4.6> ",
425 /* 7 */ "<Unknown 4.7> "
428 /* 0 */ "<Unknown 5.0> ",
429 /* 1 */ "<Unknown 5.1> ",
430 /* 2 */ "<Unknown 5.2> ",
431 /* 3 */ "<Unknown 5.3> ",
432 /* 4 */ "<Unknown 5.4> ",
433 /* 5 */ "<Unknown 5.5> ",
434 /* 6 */ "<Unknown 5.6> ",
435 /* 7 */ "<Unknown 5.7> "
438 /* 0 */ "<Unknown 6.0> ",
439 /* 1 */ "<Unknown 6.1> ",
440 /* 2 */ "<Unknown 6.2> ",
441 /* 3 */ "<Unknown 6.3> ",
442 /* 4 */ "<Unknown 6.4> ",
443 /* 5 */ "<Unknown 6.5> ",
444 /* 6 */ "<Unknown 6.6> ",
445 /* 7 */ "<Unknown 6.7> "
448 /* 0 */ "<Unknown 7.0> ",
449 /* 1 */ "<Unknown 7.1> ",
450 /* 2 */ "<Unknown 7.2> ",
451 /* 3 */ "<Unknown 7.3> ",
452 /* 4 */ "<Unknown 7.4> ",
453 /* 5 */ "<Unknown 7.5> ",
454 /* 6 */ "<Unknown 7.6> ",
455 /* 7 */ "<Unknown 7.7> "
458 if (buffer != NULL && size > 0) {
459 int n, i, len0, len1;
461 memset(buffer, 0, size);
464 for (n = 0; n < SIZE(t); n++) {
465 for (i = 0; i < SIZE(t[n]); i++) {
466 len0 = strlen(buffer);
470 if (features[n] & (1 << i)) {
471 if (len1 + strlen(t[n][i]) > 60) {
473 buffer[len0 - 1] = '\n';
476 len1 += strlen(t[n][i]);
477 strncat(buffer, t[n][i], size - len0);
489 static char const * const t[] = {
490 /* 0x00 */ "North America, Europe, Japan",
494 return (cc >= SIZE(t)? "?" : t[cc]);
498 hci_con_state2str(int state)
500 static char const * const t[] = {
501 /* NG_HCI_CON_CLOSED */ "CLOSED",
502 /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP",
503 /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
504 /* NG_HCI_CON_OPEN */ "OPEN"
507 return (state >= SIZE(t)? "UNKNOWN" : t[state]);
508 } /* hci_con_state2str */
511 hci_status2str(int status)
513 static char const * const t[] = {
514 /* 0x00 */ "No error",
515 /* 0x01 */ "Unknown HCI command",
516 /* 0x02 */ "No connection",
517 /* 0x03 */ "Hardware failure",
518 /* 0x04 */ "Page timeout",
519 /* 0x05 */ "Authentication failure",
520 /* 0x06 */ "Key missing",
521 /* 0x07 */ "Memory full",
522 /* 0x08 */ "Connection timeout",
523 /* 0x09 */ "Max number of connections",
524 /* 0x0a */ "Max number of SCO connections to a unit",
525 /* 0x0b */ "ACL connection already exists",
526 /* 0x0c */ "Command disallowed",
527 /* 0x0d */ "Host rejected due to limited resources",
528 /* 0x0e */ "Host rejected due to security reasons",
529 /* 0x0f */ "Host rejected due to remote unit is a personal unit",
530 /* 0x10 */ "Host timeout",
531 /* 0x11 */ "Unsupported feature or parameter value",
532 /* 0x12 */ "Invalid HCI command parameter",
533 /* 0x13 */ "Other end terminated connection: User ended connection",
534 /* 0x14 */ "Other end terminated connection: Low resources",
535 /* 0x15 */ "Other end terminated connection: About to power off",
536 /* 0x16 */ "Connection terminated by local host",
537 /* 0x17 */ "Repeated attempts",
538 /* 0x18 */ "Pairing not allowed",
539 /* 0x19 */ "Unknown LMP PDU",
540 /* 0x1a */ "Unsupported remote feature",
541 /* 0x1b */ "SCO offset rejected",
542 /* 0x1c */ "SCO interval rejected",
543 /* 0x1d */ "SCO air mode rejected",
544 /* 0x1e */ "Invalid LMP parameters",
545 /* 0x1f */ "Unspecified error",
546 /* 0x20 */ "Unsupported LMP parameter value",
547 /* 0x21 */ "Role change not allowed",
548 /* 0x22 */ "LMP response timeout",
549 /* 0x23 */ "LMP error transaction collision",
550 /* 0x24 */ "LMP PSU not allowed",
551 /* 0x25 */ "Encryption mode not acceptable",
552 /* 0x26 */ "Unit key used",
553 /* 0x27 */ "QoS is not supported",
554 /* 0x28 */ "Instant passed",
555 /* 0x29 */ "Pairing with unit key not supported",
556 /* 0x2a */ "Different Transaction Collision",
557 /* 0x2b */ "Unknown error (Reserved for future use)",
558 /* 0x2c */ "QoS Unacceptable Parameter",
559 /* 0x2d */ "QoS Rejected",
560 /* 0x2e */ "Channel Classification Not Supported",
561 /* 0x2f */ "Insufficient Security",
562 /* 0x30 */ "Parameter Out Of Mandatory Range",
563 /* 0x31 */ "Unknown error (Reserved for future use)",
564 /* 0x32 */ "Role Switch Pending",
565 /* 0x33 */ "Unknown error (Reserved for future use)",
566 /* 0x34 */ "Reserved Slot Violation",
567 /* 0x35 */ "Role Switch Failed",
568 /* 0x36 */ "Extended Inquiry Response Too Large",
569 /* 0x37 */ "Secure Simple Pairing Not Supported By Host",
570 /* 0x38 */ "Host Busy - Pairing",
571 /* 0x39 */ "Connection Rejected due to No Suitable Channel Found",
572 /* 0x3a */ "Controller Busy",
573 /* 0x3b */ "Unacceptable Connection Parameters",
574 /* 0x3c */ "Advertising Timeout",
575 /* 0x3d */ "Connection Terminated due to MIC Failure",
576 /* 0x3e */ "Connection Failed to be Established / Synchronization Timeout",
577 /* 0x3f */ "MAC Connection Failed",
578 /* 0x40 */ "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging",
579 /* 0x41 */ "Type0 Submap Not Defined",
580 /* 0x42 */ "Unknown Advertising Identifier",
581 /* 0x43 */ "Limit Reached",
582 /* 0x44 */ "Operation Cancelled by Host",
583 /* 0x45 */ "Packet Too Long"
586 return (status >= SIZE(t)? "Unknown error" : t[status]);
587 } /* hci_status2str */
590 hci_bdaddr2str(bdaddr_t const *ba)
592 extern int numeric_bdaddr;
593 static char buffer[MAXHOSTNAMELEN];
594 struct hostent *he = NULL;
596 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
603 if (!numeric_bdaddr &&
604 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
605 strlcpy(buffer, he->h_name, sizeof(buffer));
613 } /* hci_bdaddr2str */