xref: /freebsd/usr.sbin/bluetooth/hccontrol/util.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
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$
30  */
31 
32 #include <sys/param.h>
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
39 
40 char const *
41 hci_link2str(int link_type)
42 {
43 	static char const * const	t[] = {
44 		/* NG_HCI_LINK_SCO */ "SCO",
45 		/* NG_HCI_LINK_ACL */ "ACL"
46 	};
47 
48 	return (link_type >= SIZE(t)? "?" : t[link_type]);
49 } /* hci_link2str */
50 
51 char const *
52 hci_pin2str(int type)
53 {
54 	static char const * const	t[] = {
55 		/* 0x00 */ "Variable PIN",
56 		/* 0x01 */ "Fixed PIN"
57 	};
58 
59 	return (type >= SIZE(t)? "?" : t[type]);
60 } /* hci_pin2str */
61 
62 char const *
63 hci_scan2str(int scan)
64 {
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"
70 	};
71 
72 	return (scan >= SIZE(t)? "?" : t[scan]);
73 } /* hci_scan2str */
74 
75 char const *
76 hci_encrypt2str(int encrypt, int brief)
77 {
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"
82 	};
83 
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",
88 	};
89 
90 	if (brief)
91 		return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
92 
93 	return (encrypt >= SIZE(t)? "?" : t[encrypt]);
94 } /* hci_encrypt2str */
95 
96 char const *
97 hci_coding2str(int coding)
98 {
99 	static char const * const	t[] = {
100 		/* 0x00 */ "Linear",
101 		/* 0x01 */ "u-law",
102 		/* 0x02 */ "A-law",
103 		/* 0x03 */ "Reserved"
104 	};
105 
106 	return (coding >= SIZE(t)? "?" : t[coding]);
107 } /* hci_coding2str */
108 
109 char const *
110 hci_vdata2str(int data)
111 {
112 	static char const * const	t[] = {
113 		/* 0x00 */ "1's complement",
114 		/* 0x01 */ "2's complement",
115 		/* 0x02 */ "Sign-Magnitude",
116 		/* 0x03 */ "Reserved"
117 	};
118 
119 	return (data >= SIZE(t)? "?" : t[data]);
120 } /* hci_vdata2str */
121 
122 char const *
123 hci_hmode2str(int mode, char *buffer, int size)
124 {
125 	static char const * const	t[] = {
126 		/* 0x01 */ "Suspend Page Scan ",
127 		/* 0x02 */ "Suspend Inquiry Scan ",
128 		/* 0x04 */ "Suspend Periodic Inquiries "
129         };
130 
131 	if (buffer != NULL && size > 0) {
132 		int	n;
133 
134 		memset(buffer, 0, size);
135 		for (n = 0; n < SIZE(t); n++) {
136 			int	len = strlen(buffer);
137 
138 			if (len >= size)
139 				break;
140 			if (mode & (1 << n))
141 				strncat(buffer, t[n], size - len);
142 		}
143 	}
144 
145 	return (buffer);
146 } /* hci_hmode2str */
147 
148 char const *
149 hci_ver2str(int ver)
150 {
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"
161 	};
162 
163 	return (ver >= SIZE(t)? "?" : t[ver]);
164 } /* hci_ver2str */
165 
166 char const *
167 hci_lmpver2str(int ver)
168 {
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"
179 	};
180 
181 	return (ver >= SIZE(t)? "?" : t[ver]);
182 } /* hci_lmpver2str */
183 
184 char const *
185 hci_manufacturer2str(int m)
186 {
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.",
193 		/* 0005 */ "3Com",
194 		/* 0006 */ "Microsoft",
195 		/* 0007 */ "Lucent",
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",
244 		/* 0056 */ "Terax",
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"
253         };
254 
255 	return (m >= SIZE(t)? "?" : t[m]);
256 } /* hci_manufacturer2str */
257 
258 char const *
259 hci_features2str(uint8_t *features, char *buffer, int size)
260 {
261 	static char const * const	t[][8] = {
262 	{ /* byte 0 */
263 		/* 0 */ "<3-Slot> ",
264 		/* 1 */ "<5-Slot> ",
265 		/* 2 */ "<Encryption> ",
266 		/* 3 */ "<Slot offset> ",
267 		/* 4 */ "<Timing accuracy> ",
268 		/* 5 */ "<Switch> ",
269 		/* 6 */ "<Hold mode> ",
270 		/* 7 */ "<Sniff mode> "
271 	},
272 	{ /* byte 1 */
273 		/* 0 */ "<Park mode> ",
274 		/* 1 */ "<RSSI> ",
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> "
281 	},
282 	{ /* byte 2 */
283 		/* 0 */ "<CVSD> ",
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> "
291 	}};
292 
293 	if (buffer != NULL && size > 0) {
294 		int	n, i, len0, len1;
295 
296 		memset(buffer, 0, size);
297 		len1 = 0;
298 
299 		for (n = 0; n < SIZE(t); n++) {
300 			for (i = 0; i < SIZE(t[n]); i++) {
301 				len0 = strlen(buffer);
302 				if (len0 >= size)
303 					goto done;
304 
305 				if (features[n] & (1 << i)) {
306 					if (len1 + strlen(t[n][i]) > 60) {
307 						len1 = 0;
308 						buffer[len0 - 1] = '\n';
309 					}
310 
311 					len1 += strlen(t[n][i]);
312 					strncat(buffer, t[n][i], size - len0);
313 				}
314 			}
315 		}
316 	}
317 done:
318 	return (buffer);
319 } /* hci_features2str */
320 
321 char const *
322 hci_cc2str(int cc)
323 {
324 	static char const * const	t[] = {
325 		/* 0x00 */ "North America, Europe, Japan",
326 		/* 0x01 */ "France"
327 	};
328 
329 	return (cc >= SIZE(t)? "?" : t[cc]);
330 } /* hci_cc2str */
331 
332 char const *
333 hci_con_state2str(int state)
334 {
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"
340         };
341 
342 	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
343 } /* hci_con_state2str */
344 
345 char const *
346 hci_status2str(int status)
347 {
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"
391 	};
392 
393 	return (status >= SIZE(t)? "Unknown error" : t[status]);
394 } /* hci_status2str */
395 
396 char const *
397 hci_bdaddr2str(bdaddr_t const *ba)
398 {
399 	extern int	 numeric_bdaddr;
400 	static char	 buffer[MAXHOSTNAMELEN];
401 	struct hostent	*he = NULL;
402 
403 	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
404 		buffer[0] = '*';
405 		buffer[1] = 0;
406 
407 		return (buffer);
408 	}
409 
410 	if (!numeric_bdaddr &&
411 	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
412 		strlcpy(buffer, he->h_name, sizeof(buffer));
413 
414 		return (buffer);
415 	}
416 
417 	bt_ntoa(ba, buffer);
418 
419 	return (buffer);
420 } /* hci_bdaddr2str */
421 
422