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 */ "<Unknown2.7> "
295 if (buffer != NULL && size > 0) {
296 int n, i, len0, len1;
298 memset(buffer, 0, size);
301 for (n = 0; n < SIZE(t); n++) {
302 for (i = 0; i < SIZE(t[n]); i++) {
303 len0 = strlen(buffer);
307 if (features[n] & (1 << i)) {
308 if (len1 + strlen(t[n][i]) > 60) {
310 buffer[len0 - 1] = '\n';
313 len1 += strlen(t[n][i]);
314 strncat(buffer, t[n][i], size - len0);
321 } /* hci_features2str */
326 static char const * const t[] = {
327 /* 0x00 */ "North America, Europe, Japan",
331 return (cc >= SIZE(t)? "?" : t[cc]);
335 hci_con_state2str(int state)
337 static char const * const t[] = {
338 /* NG_HCI_CON_CLOSED */ "CLOSED",
339 /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP",
340 /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
341 /* NG_HCI_CON_OPEN */ "OPEN"
344 return (state >= SIZE(t)? "UNKNOWN" : t[state]);
345 } /* hci_con_state2str */
348 hci_status2str(int status)
350 static char const * const t[] = {
351 /* 0x00 */ "No error",
352 /* 0x01 */ "Unknown HCI command",
353 /* 0x02 */ "No connection",
354 /* 0x03 */ "Hardware failure",
355 /* 0x04 */ "Page timeout",
356 /* 0x05 */ "Authentication failure",
357 /* 0x06 */ "Key missing",
358 /* 0x07 */ "Memory full",
359 /* 0x08 */ "Connection timeout",
360 /* 0x09 */ "Max number of connections",
361 /* 0x0a */ "Max number of SCO connections to a unit",
362 /* 0x0b */ "ACL connection already exists",
363 /* 0x0c */ "Command disallowed",
364 /* 0x0d */ "Host rejected due to limited resources",
365 /* 0x0e */ "Host rejected due to security reasons",
366 /* 0x0f */ "Host rejected due to remote unit is a personal unit",
367 /* 0x10 */ "Host timeout",
368 /* 0x11 */ "Unsupported feature or parameter value",
369 /* 0x12 */ "Invalid HCI command parameter",
370 /* 0x13 */ "Other end terminated connection: User ended connection",
371 /* 0x14 */ "Other end terminated connection: Low resources",
372 /* 0x15 */ "Other end terminated connection: About to power off",
373 /* 0x16 */ "Connection terminated by local host",
374 /* 0x17 */ "Repeated attempts",
375 /* 0x18 */ "Pairing not allowed",
376 /* 0x19 */ "Unknown LMP PDU",
377 /* 0x1a */ "Unsupported remote feature",
378 /* 0x1b */ "SCO offset rejected",
379 /* 0x1c */ "SCO interval rejected",
380 /* 0x1d */ "SCO air mode rejected",
381 /* 0x1e */ "Invalid LMP parameters",
382 /* 0x1f */ "Unspecified error",
383 /* 0x20 */ "Unsupported LMP parameter value",
384 /* 0x21 */ "Role change not allowed",
385 /* 0x22 */ "LMP response timeout",
386 /* 0x23 */ "LMP error transaction collision",
387 /* 0x24 */ "LMP PSU not allowed",
388 /* 0x25 */ "Encryption mode not acceptable",
389 /* 0x26 */ "Unit key used",
390 /* 0x27 */ "QoS is not supported",
391 /* 0x28 */ "Instant passed",
392 /* 0x29 */ "Pairing with unit key not supported"
395 return (status >= SIZE(t)? "Unknown error" : t[status]);
396 } /* hci_status2str */
399 hci_bdaddr2str(bdaddr_t const *ba)
401 extern int numeric_bdaddr;
402 static char buffer[MAXHOSTNAMELEN];
403 struct hostent *he = NULL;
405 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
412 if (!numeric_bdaddr &&
413 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
414 strlcpy(buffer, he->h_name, sizeof(buffer));
422 } /* hci_bdaddr2str */