4 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $
32 #include <sys/param.h>
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
38 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
41 hci_link2str(int link_type)
43 static char const * const t[] = {
44 /* NG_HCI_LINK_SCO */ "SCO",
45 /* NG_HCI_LINK_ACL */ "ACL"
48 return (link_type >= SIZE(t)? "?" : t[link_type]);
54 static char const * const t[] = {
55 /* 0x00 */ "Variable PIN",
56 /* 0x01 */ "Fixed PIN"
59 return (type >= SIZE(t)? "?" : t[type]);
63 hci_scan2str(int scan)
65 static char const * const t[] = {
66 /* 0x00 */ "No Scan enabled",
67 /* 0x01 */ "Inquiry Scan enabled. Page Scan disabled",
68 /* 0x02 */ "Inquiry Scan disabled. Page Scan enabled",
69 /* 0x03 */ "Inquiry Scan enabled. Page Scan enabled"
72 return (scan >= SIZE(t)? "?" : t[scan]);
76 hci_encrypt2str(int encrypt, int brief)
78 static char const * const t[] = {
79 /* 0x00 */ "Disabled",
80 /* 0x01 */ "Only for point-to-point packets",
81 /* 0x02 */ "Both point-to-point and broadcast packets"
84 static char const * const t1[] = {
85 /* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE",
86 /* NG_HCI_ENCRYPTION_MODE_P2P */ "P2P",
87 /* NG_HCI_ENCRYPTION_MODE_ALL */ "ALL",
91 return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
93 return (encrypt >= SIZE(t)? "?" : t[encrypt]);
94 } /* hci_encrypt2str */
97 hci_coding2str(int coding)
99 static char const * const t[] = {
103 /* 0x03 */ "Reserved"
106 return (coding >= SIZE(t)? "?" : t[coding]);
107 } /* hci_coding2str */
110 hci_vdata2str(int data)
112 static char const * const t[] = {
113 /* 0x00 */ "1's complement",
114 /* 0x01 */ "2's complement",
115 /* 0x02 */ "Sign-Magnitude",
116 /* 0x03 */ "Reserved"
119 return (data >= SIZE(t)? "?" : t[data]);
120 } /* hci_vdata2str */
123 hci_hmode2str(int mode, char *buffer, int size)
125 static char const * const t[] = {
126 /* 0x01 */ "Suspend Page Scan ",
127 /* 0x02 */ "Suspend Inquiry Scan ",
128 /* 0x04 */ "Suspend Periodic Inquiries "
131 if (buffer != NULL && size > 0) {
134 memset(buffer, 0, size);
135 for (n = 0; n < SIZE(t); n++) {
136 int len = strlen(buffer);
141 strncat(buffer, t[n], size - len);
146 } /* hci_hmode2str */
151 static char const * const t[] = {
152 /* 0x00 */ "Bluetooth HCI Specification 1.0B",
153 /* 0x01 */ "Bluetooth HCI Specification 1.1",
154 /* 0x02 */ "Bluetooth HCI Specification 1.2",
155 /* 0x03 */ "Bluetooth HCI Specification 2.0",
156 /* 0x04 */ "Bluetooth HCI Specification 2.1",
157 /* 0x05 */ "Bluetooth HCI Specification 3.0",
158 /* 0x06 */ "Bluetooth HCI Specification 4.0",
159 /* 0x07 */ "Bluetooth HCI Specification 4.1",
160 /* 0x08 */ "Bluetooth HCI Specification 4.2"
163 return (ver >= SIZE(t)? "?" : t[ver]);
167 hci_lmpver2str(int ver)
169 static char const * const t[] = {
170 /* 0x00 */ "Bluetooth LMP 1.0",
171 /* 0x01 */ "Bluetooth LMP 1.1",
172 /* 0x02 */ "Bluetooth LMP 1.2",
173 /* 0x03 */ "Bluetooth LMP 2.0",
174 /* 0x04 */ "Bluetooth LMP 2.1",
175 /* 0x04 */ "Bluetooth LMP 3.0",
176 /* 0x04 */ "Bluetooth LMP 4.0",
177 /* 0x04 */ "Bluetooth LMP 4.1",
178 /* 0x04 */ "Bluetooth LMP 4.2"
181 return (ver >= SIZE(t)? "?" : t[ver]);
182 } /* hci_lmpver2str */
185 hci_manufacturer2str(int m)
187 static char const * const t[] = {
188 /* 0000 */ "Ericsson Technology Licensing",
189 /* 0001 */ "Nokia Mobile Phones",
190 /* 0002 */ "Intel Corp.",
191 /* 0003 */ "IBM Corp.",
192 /* 0004 */ "Toshiba Corp.",
194 /* 0006 */ "Microsoft",
196 /* 0008 */ "Motorola",
197 /* 0009 */ "Infineon Technologies AG",
198 /* 0010 */ "Cambridge Silicon Radio",
199 /* 0011 */ "Silicon Wave",
200 /* 0012 */ "Digianswer A/S",
201 /* 0013 */ "Texas Instruments Inc.",
202 /* 0014 */ "Parthus Technologies Inc.",
203 /* 0015 */ "Broadcom Corporation",
204 /* 0016 */ "Mitel Semiconductor",
205 /* 0017 */ "Widcomm, Inc.",
206 /* 0018 */ "Zeevo, Inc.",
207 /* 0019 */ "Atmel Corporation",
208 /* 0020 */ "Mitsubishi Electric Corporation",
209 /* 0021 */ "RTX Telecom A/S",
210 /* 0022 */ "KC Technology Inc.",
211 /* 0023 */ "Newlogic",
212 /* 0024 */ "Transilica, Inc.",
213 /* 0025 */ "Rohde & Schwartz GmbH & Co. KG",
214 /* 0026 */ "TTPCom Limited",
215 /* 0027 */ "Signia Technologies, Inc.",
216 /* 0028 */ "Conexant Systems Inc.",
217 /* 0029 */ "Qualcomm",
218 /* 0030 */ "Inventel",
219 /* 0031 */ "AVM Berlin",
220 /* 0032 */ "BandSpeed, Inc.",
221 /* 0033 */ "Mansella Ltd",
222 /* 0034 */ "NEC Corporation",
223 /* 0035 */ "WavePlus Technology Co., Ltd.",
224 /* 0036 */ "Alcatel",
225 /* 0037 */ "Philips Semiconductors",
226 /* 0038 */ "C Technologies",
227 /* 0039 */ "Open Interface",
228 /* 0040 */ "R F Micro Devices",
229 /* 0041 */ "Hitachi Ltd",
230 /* 0042 */ "Symbol Technologies, Inc.",
231 /* 0043 */ "Tenovis",
232 /* 0044 */ "Macronix International Co. Ltd.",
233 /* 0045 */ "GCT Semiconductor",
234 /* 0046 */ "Norwood Systems",
235 /* 0047 */ "MewTel Technology Inc.",
236 /* 0048 */ "ST Microelectronics",
237 /* 0049 */ "Synopsys",
238 /* 0050 */ "Red-M (Communications) Ltd",
239 /* 0051 */ "Commil Ltd",
240 /* 0052 */ "Computer Access Technology Corporation (CATC)",
241 /* 0053 */ "Eclipse (HQ Espana) S.L.",
242 /* 0054 */ "Renesas Technology Corp.",
243 /* 0055 */ "Mobilian Corporation",
245 /* 0057 */ "Integrated System Solution Corp.",
246 /* 0058 */ "Matsushita Electric Industrial Co., Ltd.",
247 /* 0059 */ "Gennum Corporation",
248 /* 0060 */ "Research In Motion",
249 /* 0061 */ "IPextreme, Inc.",
250 /* 0062 */ "Systems and Chips, Inc",
251 /* 0063 */ "Bluetooth SIG, Inc",
252 /* 0064 */ "Seiko Epson Corporation"
255 return (m >= SIZE(t)? "?" : t[m]);
256 } /* hci_manufacturer2str */
259 hci_features2str(uint8_t *features, char *buffer, int size)
261 static char const * const t[][8] = {
265 /* 2 */ "<Encryption> ",
266 /* 3 */ "<Slot offset> ",
267 /* 4 */ "<Timing accuracy> ",
269 /* 6 */ "<Hold mode> ",
270 /* 7 */ "<Sniff mode> "
273 /* 0 */ "<Park mode> ",
275 /* 2 */ "<Channel quality> ",
276 /* 3 */ "<SCO link> ",
277 /* 4 */ "<HV2 packets> ",
278 /* 5 */ "<HV3 packets> ",
279 /* 6 */ "<u-law log> ",
280 /* 7 */ "<A-law log> "
284 /* 1 */ "<Paging scheme> ",
285 /* 2 */ "<Power control> ",
286 /* 3 */ "<Transparent SCO data> ",
287 /* 4 */ "<Flow control lag (bit0)> ",
288 /* 5 */ "<Flow control lag (bit1)> ",
289 /* 6 */ "<Flow control lag (bit2)> ",
290 /* 7 */ "<Unknown2.7> "
293 if (buffer != NULL && size > 0) {
294 int n, i, len0, len1;
296 memset(buffer, 0, size);
299 for (n = 0; n < SIZE(t); n++) {
300 for (i = 0; i < SIZE(t[n]); i++) {
301 len0 = strlen(buffer);
305 if (features[n] & (1 << i)) {
306 if (len1 + strlen(t[n][i]) > 60) {
308 buffer[len0 - 1] = '\n';
311 len1 += strlen(t[n][i]);
312 strncat(buffer, t[n][i], size - len0);
319 } /* hci_features2str */
324 static char const * const t[] = {
325 /* 0x00 */ "North America, Europe, Japan",
329 return (cc >= SIZE(t)? "?" : t[cc]);
333 hci_con_state2str(int state)
335 static char const * const t[] = {
336 /* NG_HCI_CON_CLOSED */ "CLOSED",
337 /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP",
338 /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
339 /* NG_HCI_CON_OPEN */ "OPEN"
342 return (state >= SIZE(t)? "UNKNOWN" : t[state]);
343 } /* hci_con_state2str */
346 hci_status2str(int status)
348 static char const * const t[] = {
349 /* 0x00 */ "No error",
350 /* 0x01 */ "Unknown HCI command",
351 /* 0x02 */ "No connection",
352 /* 0x03 */ "Hardware failure",
353 /* 0x04 */ "Page timeout",
354 /* 0x05 */ "Authentication failure",
355 /* 0x06 */ "Key missing",
356 /* 0x07 */ "Memory full",
357 /* 0x08 */ "Connection timeout",
358 /* 0x09 */ "Max number of connections",
359 /* 0x0a */ "Max number of SCO connections to a unit",
360 /* 0x0b */ "ACL connection already exists",
361 /* 0x0c */ "Command disallowed",
362 /* 0x0d */ "Host rejected due to limited resources",
363 /* 0x0e */ "Host rejected due to security reasons",
364 /* 0x0f */ "Host rejected due to remote unit is a personal unit",
365 /* 0x10 */ "Host timeout",
366 /* 0x11 */ "Unsupported feature or parameter value",
367 /* 0x12 */ "Invalid HCI command parameter",
368 /* 0x13 */ "Other end terminated connection: User ended connection",
369 /* 0x14 */ "Other end terminated connection: Low resources",
370 /* 0x15 */ "Other end terminated connection: About to power off",
371 /* 0x16 */ "Connection terminated by local host",
372 /* 0x17 */ "Repeated attempts",
373 /* 0x18 */ "Pairing not allowed",
374 /* 0x19 */ "Unknown LMP PDU",
375 /* 0x1a */ "Unsupported remote feature",
376 /* 0x1b */ "SCO offset rejected",
377 /* 0x1c */ "SCO interval rejected",
378 /* 0x1d */ "SCO air mode rejected",
379 /* 0x1e */ "Invalid LMP parameters",
380 /* 0x1f */ "Unspecified error",
381 /* 0x20 */ "Unsupported LMP parameter value",
382 /* 0x21 */ "Role change not allowed",
383 /* 0x22 */ "LMP response timeout",
384 /* 0x23 */ "LMP error transaction collision",
385 /* 0x24 */ "LMP PSU not allowed",
386 /* 0x25 */ "Encryption mode not acceptable",
387 /* 0x26 */ "Unit key used",
388 /* 0x27 */ "QoS is not supported",
389 /* 0x28 */ "Instant passed",
390 /* 0x29 */ "Pairing with unit key not supported"
393 return (status >= SIZE(t)? "Unknown error" : t[status]);
394 } /* hci_status2str */
397 hci_bdaddr2str(bdaddr_t const *ba)
399 extern int numeric_bdaddr;
400 static char buffer[MAXHOSTNAMELEN];
401 struct hostent *he = NULL;
403 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
410 if (!numeric_bdaddr &&
411 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
412 strlcpy(buffer, he->h_name, sizeof(buffer));
420 } /* hci_bdaddr2str */