util.c revision 361157
1/*
2 * util.c
3 *
4 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $
29 * $FreeBSD: stable/10/usr.sbin/bluetooth/hccontrol/util.c 361157 2020-05-18 08:45:24Z hselasky $
30 */
31
32#include <sys/param.h>
33#include <bluetooth.h>
34#include <stdio.h>
35#include <string.h>
36
37#define SIZE(x) (sizeof((x))/sizeof((x)[0]))
38
39char const *
40hci_link2str(int link_type)
41{
42	static char const * const	t[] = {
43		/* NG_HCI_LINK_SCO */ "SCO",
44		/* NG_HCI_LINK_ACL */ "ACL"
45	};
46
47	return (link_type >= SIZE(t)? "?" : t[link_type]);
48} /* hci_link2str */
49
50char const *
51hci_pin2str(int type)
52{
53	static char const * const	t[] = {
54		/* 0x00 */ "Variable PIN",
55		/* 0x01 */ "Fixed PIN"
56	};
57
58	return (type >= SIZE(t)? "?" : t[type]);
59} /* hci_pin2str */
60
61char const *
62hci_scan2str(int scan)
63{
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"
69	};
70
71	return (scan >= SIZE(t)? "?" : t[scan]);
72} /* hci_scan2str */
73
74char const *
75hci_encrypt2str(int encrypt, int brief)
76{
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"
81	};
82
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",
87	};
88
89	if (brief)
90		return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
91
92	return (encrypt >= SIZE(t)? "?" : t[encrypt]);
93} /* hci_encrypt2str */
94
95char const *
96hci_coding2str(int coding)
97{
98	static char const * const	t[] = {
99		/* 0x00 */ "Linear",
100		/* 0x01 */ "u-law",
101		/* 0x02 */ "A-law",
102		/* 0x03 */ "Reserved"
103	};
104
105	return (coding >= SIZE(t)? "?" : t[coding]);
106} /* hci_coding2str */
107
108char const *
109hci_vdata2str(int data)
110{
111	static char const * const	t[] = {
112		/* 0x00 */ "1's complement",
113		/* 0x01 */ "2's complement",
114		/* 0x02 */ "Sign-Magnitude",
115		/* 0x03 */ "Reserved"
116	};
117
118	return (data >= SIZE(t)? "?" : t[data]);
119} /* hci_vdata2str */
120
121char const *
122hci_hmode2str(int mode, char *buffer, int size)
123{
124	static char const * const	t[] = {
125		/* 0x01 */ "Suspend Page Scan ",
126		/* 0x02 */ "Suspend Inquiry Scan ",
127		/* 0x04 */ "Suspend Periodic Inquiries "
128        };
129
130	if (buffer != NULL && size > 0) {
131		int	n;
132
133		memset(buffer, 0, size);
134		for (n = 0; n < SIZE(t); n++) {
135			int	len = strlen(buffer);
136
137			if (len >= size)
138				break;
139			if (mode & (1 << n))
140				strncat(buffer, t[n], size - len);
141		}
142	}
143
144	return (buffer);
145} /* hci_hmode2str */
146
147char const *
148hci_ver2str(int ver)
149{
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"
155	};
156
157	return (ver >= SIZE(t)? "?" : t[ver]);
158} /* hci_ver2str */
159
160char const *
161hci_lmpver2str(int ver)
162{
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"
168	};
169
170	return (ver >= SIZE(t)? "?" : t[ver]);
171} /* hci_lmpver2str */
172
173char const *
174hci_manufacturer2str(int m)
175{
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.",
182		/* 0005 */ "3Com",
183		/* 0006 */ "Microsoft",
184		/* 0007 */ "Lucent",
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",
233		/* 0056 */ "Terax",
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"
242        };
243
244	return (m >= SIZE(t)? "?" : t[m]);
245} /* hci_manufacturer2str */
246
247char const *
248hci_features2str(uint8_t *features, char *buffer, int size)
249{
250	static char const * const	t[][8] = {
251	{ /* byte 0 */
252		/* 0 */ "<3-Slot> ",
253		/* 1 */ "<5-Slot> ",
254		/* 2 */ "<Encryption> ",
255		/* 3 */ "<Slot offset> ",
256		/* 4 */ "<Timing accuracy> ",
257		/* 5 */ "<Switch> ",
258		/* 6 */ "<Hold mode> ",
259		/* 7 */ "<Sniff mode> "
260	},
261	{ /* byte 1 */
262		/* 0 */ "<Park mode> ",
263		/* 1 */ "<RSSI> ",
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> "
270	},
271	{ /* byte 2 */
272		/* 0 */ "<CVSD> ",
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 */ "<Broadcast Encryption> "
280	},
281	{ /* byte 3 */
282		/* 0 */ "<Unknown 3.0> ",
283		/* 1 */ "<EDR ACL 2 Mb/s> ",
284		/* 2 */ "<EDR ACL 3 Mb/s> ",
285		/* 3 */ "<Enhanced inquiry scan> ",
286		/* 4 */ "<Interlaced inquiry scan> ",
287		/* 5 */ "<Interlaced page scan> ",
288		/* 6 */ "<RSSI with inquiry results> ",
289		/* 7 */ "<Extended SCO link (EV3 packets)> "
290	},
291	{ /* byte 4 */
292		/* 0 */ "<EV4 packets> ",
293		/* 1 */ "<EV5 packets> ",
294		/* 2 */ "<Unknown 4.2> ",
295		/* 3 */ "<AFH capable slave> ",
296		/* 4 */ "<AFH classification slave> ",
297		/* 5 */ "<BR/EDR Not Supported> ",
298		/* 6 */ "<LE Supported (Controller)> ",
299		/* 7 */ "<3-Slot EDR ACL packets> "
300	},
301	{ /* byte 5 */
302		/* 0 */ "<5-Slot EDR ACL packets> ",
303		/* 1 */ "<Sniff subrating> ",
304		/* 2 */ "<Pause encryption> ",
305		/* 3 */ "<AFH capable master> ",
306		/* 4 */ "<AFH classification master> ",
307		/* 5 */ "<EDR eSCO 2 Mb/s mode> ",
308		/* 6 */ "<EDR eSCO 3 Mb/s mode> ",
309		/* 7 */ "<3-Slot EDR eSCO packets> "
310	},
311	{ /* byte 6 */
312		/* 0 */ "<Enhanced Inquiry Response> ",
313		/* 1 */ "<Simultaneous LE and BR/EDR (Controller)> ",
314		/* 2 */ "<Unknown 6.2> ",
315		/* 3 */ "<Secure Simple Pairing (Controller Support)> ",
316		/* 4 */ "<Encapsulated PDU> ",
317		/* 5 */ "<Erroneous Data Reporting> ",
318		/* 6 */ "<Non-flushable Packed Boundary Flag> ",
319		/* 7 */ "<Unknown 6.7> "
320	},
321	{ /* byte 7 */
322		/* 0 */ "<HCI_Link_Supervision_Timeout_Changed event> ",
323		/* 1 */ "<Variable Inquiry TX Power Level> ",
324		/* 2 */ "<Enhanced Power Control> ",
325		/* 3 */ "<Unknown 7.3> ",
326		/* 4 */ "<Unknown 7.4> ",
327		/* 5 */ "<Unknown 7.5> ",
328		/* 6 */ "<Unknown 7.6> ",
329		/* 7 */ "<Extended features> "
330	}};
331
332	if (buffer != NULL && size > 0) {
333		int	n, i, len0, len1;
334
335		memset(buffer, 0, size);
336		len1 = 0;
337
338		for (n = 0; n < SIZE(t); n++) {
339			for (i = 0; i < SIZE(t[n]); i++) {
340				len0 = strlen(buffer);
341				if (len0 >= size)
342					goto done;
343
344				if (features[n] & (1 << i)) {
345					if (len1 + strlen(t[n][i]) > 60) {
346						len1 = 0;
347						buffer[len0 - 1] = '\n';
348					}
349
350					len1 += strlen(t[n][i]);
351					strncat(buffer, t[n][i], size - len0);
352				}
353			}
354		}
355	}
356done:
357	return (buffer);
358} /* hci_features2str */
359
360char const *
361hci_cc2str(int cc)
362{
363	static char const * const	t[] = {
364		/* 0x00 */ "North America, Europe, Japan",
365		/* 0x01 */ "France"
366	};
367
368	return (cc >= SIZE(t)? "?" : t[cc]);
369} /* hci_cc2str */
370
371char const *
372hci_con_state2str(int state)
373{
374	static char const * const	t[] = {
375		/* NG_HCI_CON_CLOSED */           "CLOSED",
376		/* NG_HCI_CON_W4_LP_CON_RSP */    "W4_LP_CON_RSP",
377		/* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
378		/* NG_HCI_CON_OPEN */             "OPEN"
379        };
380
381	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
382} /* hci_con_state2str */
383
384char const *
385hci_status2str(int status)
386{
387	static char const * const       t[] = {
388		/* 0x00 */ "No error",
389		/* 0x01 */ "Unknown HCI command",
390		/* 0x02 */ "No connection",
391		/* 0x03 */ "Hardware failure",
392		/* 0x04 */ "Page timeout",
393		/* 0x05 */ "Authentication failure",
394		/* 0x06 */ "Key missing",
395		/* 0x07 */ "Memory full",
396		/* 0x08 */ "Connection timeout",
397		/* 0x09 */ "Max number of connections",
398		/* 0x0a */ "Max number of SCO connections to a unit",
399		/* 0x0b */ "ACL connection already exists",
400		/* 0x0c */ "Command disallowed",
401		/* 0x0d */ "Host rejected due to limited resources",
402		/* 0x0e */ "Host rejected due to security reasons",
403		/* 0x0f */ "Host rejected due to remote unit is a personal unit",
404		/* 0x10 */ "Host timeout",
405		/* 0x11 */ "Unsupported feature or parameter value",
406		/* 0x12 */ "Invalid HCI command parameter",
407		/* 0x13 */ "Other end terminated connection: User ended connection",
408		/* 0x14 */ "Other end terminated connection: Low resources",
409		/* 0x15 */ "Other end terminated connection: About to power off",
410		/* 0x16 */ "Connection terminated by local host",
411		/* 0x17 */ "Repeated attempts",
412		/* 0x18 */ "Pairing not allowed",
413		/* 0x19 */ "Unknown LMP PDU",
414		/* 0x1a */ "Unsupported remote feature",
415		/* 0x1b */ "SCO offset rejected",
416		/* 0x1c */ "SCO interval rejected",
417		/* 0x1d */ "SCO air mode rejected",
418		/* 0x1e */ "Invalid LMP parameters",
419		/* 0x1f */ "Unspecified error",
420		/* 0x20 */ "Unsupported LMP parameter value",
421		/* 0x21 */ "Role change not allowed",
422		/* 0x22 */ "LMP response timeout",
423		/* 0x23 */ "LMP error transaction collision",
424		/* 0x24 */ "LMP PSU not allowed",
425		/* 0x25 */ "Encryption mode not acceptable",
426		/* 0x26 */ "Unit key used",
427		/* 0x27 */ "QoS is not supported",
428		/* 0x28 */ "Instant passed",
429		/* 0x29 */ "Pairing with unit key not supported",
430		/* 0x2a */ "Different Transaction Collision",
431		/* 0x2b */ "Unknown error (Reserved for future use)",
432		/* 0x2c */ "QoS Unacceptable Parameter",
433		/* 0x2d */ "QoS Rejected",
434		/* 0x2e */ "Channel Classification Not Supported",
435		/* 0x2f */ "Insufficient Security",
436		/* 0x30 */ "Parameter Out Of Mandatory Range",
437		/* 0x31 */ "Unknown error (Reserved for future use)",
438		/* 0x32 */ "Role Switch Pending",
439		/* 0x33 */ "Unknown error (Reserved for future use)",
440		/* 0x34 */ "Reserved Slot Violation",
441		/* 0x35 */ "Role Switch Failed",
442		/* 0x36 */ "Extended Inquiry Response Too Large",
443		/* 0x37 */ "Secure Simple Pairing Not Supported By Host",
444		/* 0x38 */ "Host Busy - Pairing",
445		/* 0x39 */ "Connection Rejected due to No Suitable Channel Found",
446		/* 0x3a */ "Controller Busy",
447		/* 0x3b */ "Unacceptable Connection Parameters",
448		/* 0x3c */ "Advertising Timeout",
449		/* 0x3d */ "Connection Terminated due to MIC Failure",
450		/* 0x3e */ "Connection Failed to be Established / Synchronization Timeout",
451		/* 0x3f */ "MAC Connection Failed",
452		/* 0x40 */ "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging",
453		/* 0x41 */ "Type0 Submap Not Defined",
454		/* 0x42 */ "Unknown Advertising Identifier",
455		/* 0x43 */ "Limit Reached",
456		/* 0x44 */ "Operation Cancelled by Host",
457		/* 0x45 */ "Packet Too Long"
458	};
459
460	return (status >= SIZE(t)? "Unknown error" : t[status]);
461} /* hci_status2str */
462
463char const *
464hci_bdaddr2str(bdaddr_t const *ba)
465{
466	extern int	 numeric_bdaddr;
467	static char	 buffer[MAXHOSTNAMELEN];
468	struct hostent	*he = NULL;
469
470	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
471		buffer[0] = '*';
472		buffer[1] = 0;
473
474		return (buffer);
475	}
476
477	if (!numeric_bdaddr &&
478	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
479		strlcpy(buffer, he->h_name, sizeof(buffer));
480
481		return (buffer);
482	}
483
484	bt_ntoa(ba, buffer);
485
486	return (buffer);
487} /* hci_bdaddr2str */
488
489