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 #include <bluetooth.h>
37 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
40 hci_link2str(int link_type)
42 static char const * const t[] = {
43 /* NG_HCI_LINK_SCO */ "SCO",
44 /* NG_HCI_LINK_ACL */ "ACL"
47 return (link_type >= SIZE(t)? "?" : t[link_type]);
53 static char const * const t[] = {
54 /* 0x00 */ "Variable PIN",
55 /* 0x01 */ "Fixed PIN"
58 return (type >= SIZE(t)? "?" : t[type]);
62 hci_scan2str(int scan)
64 static char const * const t[] = {
65 /* 0x00 */ "No Scan enabled",
66 /* 0x01 */ "Inquiry Scan enabled. Page Scan disabled",
67 /* 0x02 */ "Inquiry Scan disabled. Page Scan enabled",
68 /* 0x03 */ "Inquiry Scan enabled. Page Scan enabled"
71 return (scan >= SIZE(t)? "?" : t[scan]);
75 hci_encrypt2str(int encrypt, int brief)
77 static char const * const t[] = {
78 /* 0x00 */ "Disabled",
79 /* 0x01 */ "Only for point-to-point packets",
80 /* 0x02 */ "Both point-to-point and broadcast packets"
83 static char const * const t1[] = {
84 /* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE",
85 /* NG_HCI_ENCRYPTION_MODE_P2P */ "P2P",
86 /* NG_HCI_ENCRYPTION_MODE_ALL */ "ALL",
90 return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
92 return (encrypt >= SIZE(t)? "?" : t[encrypt]);
93 } /* hci_encrypt2str */
96 hci_coding2str(int coding)
98 static char const * const t[] = {
102 /* 0x03 */ "Reserved"
105 return (coding >= SIZE(t)? "?" : t[coding]);
106 } /* hci_coding2str */
109 hci_vdata2str(int data)
111 static char const * const t[] = {
112 /* 0x00 */ "1's complement",
113 /* 0x01 */ "2's complement",
114 /* 0x02 */ "Sign-Magnitude",
115 /* 0x03 */ "Reserved"
118 return (data >= SIZE(t)? "?" : t[data]);
119 } /* hci_vdata2str */
122 hci_hmode2str(int mode, char *buffer, int size)
124 static char const * const t[] = {
125 /* 0x01 */ "Suspend Page Scan ",
126 /* 0x02 */ "Suspend Inquiry Scan ",
127 /* 0x04 */ "Suspend Periodic Inquiries "
130 if (buffer != NULL && size > 0) {
133 memset(buffer, 0, size);
134 for (n = 0; n < SIZE(t); n++) {
135 int len = strlen(buffer);
140 strncat(buffer, t[n], size - len);
145 } /* hci_hmode2str */
150 static char const * const t[] = {
151 /* 0x00 */ "Bluetooth HCI Specification 1.0B",
152 /* 0x01 */ "Bluetooth HCI Specification 1.1",
153 /* 0x02 */ "Bluetooth HCI Specification 1.2",
154 /* 0x03 */ "Bluetooth HCI Specification 2.0"
157 return (ver >= SIZE(t)? "?" : t[ver]);
161 hci_lmpver2str(int ver)
163 static char const * const t[] = {
164 /* 0x00 */ "Bluetooth LMP 1.0",
165 /* 0x01 */ "Bluetooth LMP 1.1",
166 /* 0x02 */ "Bluetooth LMP 1.2",
167 /* 0x03 */ "Bluetooth LMP 2.0"
170 return (ver >= SIZE(t)? "?" : t[ver]);
171 } /* hci_lmpver2str */
174 hci_manufacturer2str(int m)
176 static char const * const t[] = {
177 /* 0000 */ "Ericsson Technology Licensing",
178 /* 0001 */ "Nokia Mobile Phones",
179 /* 0002 */ "Intel Corp.",
180 /* 0003 */ "IBM Corp.",
181 /* 0004 */ "Toshiba Corp.",
183 /* 0006 */ "Microsoft",
185 /* 0008 */ "Motorola",
186 /* 0009 */ "Infineon Technologies AG",
187 /* 0010 */ "Cambridge Silicon Radio",
188 /* 0011 */ "Silicon Wave",
189 /* 0012 */ "Digianswer A/S",
190 /* 0013 */ "Texas Instruments Inc.",
191 /* 0014 */ "Parthus Technologies Inc.",
192 /* 0015 */ "Broadcom Corporation",
193 /* 0016 */ "Mitel Semiconductor",
194 /* 0017 */ "Widcomm, Inc.",
195 /* 0018 */ "Zeevo, Inc.",
196 /* 0019 */ "Atmel Corporation",
197 /* 0020 */ "Mitsubishi Electric Corporation",
198 /* 0021 */ "RTX Telecom A/S",
199 /* 0022 */ "KC Technology Inc.",
200 /* 0023 */ "Newlogic",
201 /* 0024 */ "Transilica, Inc.",
202 /* 0025 */ "Rohde & Schwartz GmbH & Co. KG",
203 /* 0026 */ "TTPCom Limited",
204 /* 0027 */ "Signia Technologies, Inc.",
205 /* 0028 */ "Conexant Systems Inc.",
206 /* 0029 */ "Qualcomm",
207 /* 0030 */ "Inventel",
208 /* 0031 */ "AVM Berlin",
209 /* 0032 */ "BandSpeed, Inc.",
210 /* 0033 */ "Mansella Ltd",
211 /* 0034 */ "NEC Corporation",
212 /* 0035 */ "WavePlus Technology Co., Ltd.",
213 /* 0036 */ "Alcatel",
214 /* 0037 */ "Philips Semiconductors",
215 /* 0038 */ "C Technologies",
216 /* 0039 */ "Open Interface",
217 /* 0040 */ "R F Micro Devices",
218 /* 0041 */ "Hitachi Ltd",
219 /* 0042 */ "Symbol Technologies, Inc.",
220 /* 0043 */ "Tenovis",
221 /* 0044 */ "Macronix International Co. Ltd.",
222 /* 0045 */ "GCT Semiconductor",
223 /* 0046 */ "Norwood Systems",
224 /* 0047 */ "MewTel Technology Inc.",
225 /* 0048 */ "ST Microelectronics",
226 /* 0049 */ "Synopsys",
227 /* 0050 */ "Red-M (Communications) Ltd",
228 /* 0051 */ "Commil Ltd",
229 /* 0052 */ "Computer Access Technology Corporation (CATC)",
230 /* 0053 */ "Eclipse (HQ Espana) S.L.",
231 /* 0054 */ "Renesas Technology Corp.",
232 /* 0055 */ "Mobilian Corporation",
234 /* 0057 */ "Integrated System Solution Corp.",
235 /* 0058 */ "Matsushita Electric Industrial Co., Ltd.",
236 /* 0059 */ "Gennum Corporation",
237 /* 0060 */ "Research In Motion",
238 /* 0061 */ "IPextreme, Inc.",
239 /* 0062 */ "Systems and Chips, Inc",
240 /* 0063 */ "Bluetooth SIG, Inc",
241 /* 0064 */ "Seiko Epson Corporation"
244 return (m >= SIZE(t)? "?" : t[m]);
245 } /* hci_manufacturer2str */
248 hci_features2str(uint8_t *features, char *buffer, int size)
250 static char const * const t[][8] = {
254 /* 2 */ "<Encryption> ",
255 /* 3 */ "<Slot offset> ",
256 /* 4 */ "<Timing accuracy> ",
258 /* 6 */ "<Hold mode> ",
259 /* 7 */ "<Sniff mode> "
262 /* 0 */ "<Park mode> ",
264 /* 2 */ "<Channel quality> ",
265 /* 3 */ "<SCO link> ",
266 /* 4 */ "<HV2 packets> ",
267 /* 5 */ "<HV3 packets> ",
268 /* 6 */ "<u-law log> ",
269 /* 7 */ "<A-law log> "
273 /* 1 */ "<Paging scheme> ",
274 /* 2 */ "<Power control> ",
275 /* 3 */ "<Transparent SCO data> ",
276 /* 4 */ "<Flow control lag (bit0)> ",
277 /* 5 */ "<Flow control lag (bit1)> ",
278 /* 6 */ "<Flow control lag (bit2)> ",
279 /* 7 */ "<Unknown2.7> "
282 if (buffer != NULL && size > 0) {
283 int n, i, len0, len1;
285 memset(buffer, 0, size);
288 for (n = 0; n < SIZE(t); n++) {
289 for (i = 0; i < SIZE(t[n]); i++) {
290 len0 = strlen(buffer);
294 if (features[n] & (1 << i)) {
295 if (len1 + strlen(t[n][i]) > 60) {
297 buffer[len0 - 1] = '\n';
300 len1 += strlen(t[n][i]);
301 strncat(buffer, t[n][i], size - len0);
308 } /* hci_features2str */
313 static char const * const t[] = {
314 /* 0x00 */ "North America, Europe, Japan",
318 return (cc >= SIZE(t)? "?" : t[cc]);
322 hci_con_state2str(int state)
324 static char const * const t[] = {
325 /* NG_HCI_CON_CLOSED */ "CLOSED",
326 /* NG_HCI_CON_W4_LP_CON_RSP */ "W4_LP_CON_RSP",
327 /* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
328 /* NG_HCI_CON_OPEN */ "OPEN"
331 return (state >= SIZE(t)? "UNKNOWN" : t[state]);
332 } /* hci_con_state2str */
335 hci_status2str(int status)
337 static char const * const t[] = {
338 /* 0x00 */ "No error",
339 /* 0x01 */ "Unknown HCI command",
340 /* 0x02 */ "No connection",
341 /* 0x03 */ "Hardware failure",
342 /* 0x04 */ "Page timeout",
343 /* 0x05 */ "Authentication failure",
344 /* 0x06 */ "Key missing",
345 /* 0x07 */ "Memory full",
346 /* 0x08 */ "Connection timeout",
347 /* 0x09 */ "Max number of connections",
348 /* 0x0a */ "Max number of SCO connections to a unit",
349 /* 0x0b */ "ACL connection already exists",
350 /* 0x0c */ "Command disallowed",
351 /* 0x0d */ "Host rejected due to limited resources",
352 /* 0x0e */ "Host rejected due to security reasons",
353 /* 0x0f */ "Host rejected due to remote unit is a personal unit",
354 /* 0x10 */ "Host timeout",
355 /* 0x11 */ "Unsupported feature or parameter value",
356 /* 0x12 */ "Invalid HCI command parameter",
357 /* 0x13 */ "Other end terminated connection: User ended connection",
358 /* 0x14 */ "Other end terminated connection: Low resources",
359 /* 0x15 */ "Other end terminated connection: About to power off",
360 /* 0x16 */ "Connection terminated by local host",
361 /* 0x17 */ "Repeated attempts",
362 /* 0x18 */ "Pairing not allowed",
363 /* 0x19 */ "Unknown LMP PDU",
364 /* 0x1a */ "Unsupported remote feature",
365 /* 0x1b */ "SCO offset rejected",
366 /* 0x1c */ "SCO interval rejected",
367 /* 0x1d */ "SCO air mode rejected",
368 /* 0x1e */ "Invalid LMP parameters",
369 /* 0x1f */ "Unspecified error",
370 /* 0x20 */ "Unsupported LMP parameter value",
371 /* 0x21 */ "Role change not allowed",
372 /* 0x22 */ "LMP response timeout",
373 /* 0x23 */ "LMP error transaction collision",
374 /* 0x24 */ "LMP PSU not allowed",
375 /* 0x25 */ "Encryption mode not acceptable",
376 /* 0x26 */ "Unit key used",
377 /* 0x27 */ "QoS is not supported",
378 /* 0x28 */ "Instant passed",
379 /* 0x29 */ "Pairing with unit key not supported"
382 return (status >= SIZE(t)? "Unknown error" : t[status]);
383 } /* hci_status2str */
386 hci_bdaddr2str(bdaddr_t const *ba)
388 extern int numeric_bdaddr;
389 static char buffer[MAXHOSTNAMELEN];
390 struct hostent *he = NULL;
392 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
399 if (!numeric_bdaddr &&
400 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
401 strlcpy(buffer, he->h_name, sizeof(buffer));
409 } /* hci_bdaddr2str */