xref: /freebsd/usr.sbin/bluetooth/hccontrol/util.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * util.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
41 
42 char const *
43 hci_link2str(int link_type)
44 {
45 	static char const * const	t[] = {
46 		/* NG_HCI_LINK_SCO */ "SCO",
47 		/* NG_HCI_LINK_ACL */ "ACL"
48 	};
49 
50 	return (link_type >= SIZE(t)? "?" : t[link_type]);
51 } /* hci_link2str */
52 
53 char const *
54 hci_pin2str(int type)
55 {
56 	static char const * const	t[] = {
57 		/* 0x00 */ "Variable PIN",
58 		/* 0x01 */ "Fixed PIN"
59 	};
60 
61 	return (type >= SIZE(t)? "?" : t[type]);
62 } /* hci_pin2str */
63 
64 char const *
65 hci_scan2str(int scan)
66 {
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"
72 	};
73 
74 	return (scan >= SIZE(t)? "?" : t[scan]);
75 } /* hci_scan2str */
76 
77 char const *
78 hci_encrypt2str(int encrypt, int brief)
79 {
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"
84 	};
85 
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",
90 	};
91 
92 	if (brief)
93 		return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
94 
95 	return (encrypt >= SIZE(t)? "?" : t[encrypt]);
96 } /* hci_encrypt2str */
97 
98 char const *
99 hci_coding2str(int coding)
100 {
101 	static char const * const	t[] = {
102 		/* 0x00 */ "Linear",
103 		/* 0x01 */ "u-law",
104 		/* 0x02 */ "A-law",
105 		/* 0x03 */ "Reserved"
106 	};
107 
108 	return (coding >= SIZE(t)? "?" : t[coding]);
109 } /* hci_coding2str */
110 
111 char const *
112 hci_vdata2str(int data)
113 {
114 	static char const * const	t[] = {
115 		/* 0x00 */ "1's complement",
116 		/* 0x01 */ "2's complement",
117 		/* 0x02 */ "Sign-Magnitude",
118 		/* 0x03 */ "Reserved"
119 	};
120 
121 	return (data >= SIZE(t)? "?" : t[data]);
122 } /* hci_vdata2str */
123 
124 char const *
125 hci_hmode2str(int mode, char *buffer, int size)
126 {
127 	static char const * const	t[] = {
128 		/* 0x01 */ "Suspend Page Scan ",
129 		/* 0x02 */ "Suspend Inquiry Scan ",
130 		/* 0x04 */ "Suspend Periodic Inquiries "
131         };
132 
133 	if (buffer != NULL && size > 0) {
134 		int	n;
135 
136 		memset(buffer, 0, size);
137 		for (n = 0; n < SIZE(t); n++) {
138 			int	len = strlen(buffer);
139 
140 			if (len >= size)
141 				break;
142 			if (mode & (1 << n))
143 				strncat(buffer, t[n], size - len);
144 		}
145 	}
146 
147 	return (buffer);
148 } /* hci_hmode2str */
149 
150 char const *
151 hci_ver2str(int ver)
152 {
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"
163 	};
164 
165 	return (ver >= SIZE(t)? "?" : t[ver]);
166 } /* hci_ver2str */
167 
168 char const *
169 hci_lmpver2str(int ver)
170 {
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"
181 	};
182 
183 	return (ver >= SIZE(t)? "?" : t[ver]);
184 } /* hci_lmpver2str */
185 
186 char const *
187 hci_manufacturer2str(int m)
188 {
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.",
195 		/* 0005 */ "3Com",
196 		/* 0006 */ "Microsoft",
197 		/* 0007 */ "Lucent",
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",
246 		/* 0056 */ "Terax",
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"
255         };
256 
257 	return (m >= SIZE(t)? "?" : t[m]);
258 } /* hci_manufacturer2str */
259 
260 char const *
261 hci_features2str(uint8_t *features, char *buffer, int size)
262 {
263 	static char const * const	t[][8] = {
264 	{ /* byte 0 */
265 		/* 0 */ "<3-Slot> ",
266 		/* 1 */ "<5-Slot> ",
267 		/* 2 */ "<Encryption> ",
268 		/* 3 */ "<Slot offset> ",
269 		/* 4 */ "<Timing accuracy> ",
270 		/* 5 */ "<Switch> ",
271 		/* 6 */ "<Hold mode> ",
272 		/* 7 */ "<Sniff mode> "
273 	},
274 	{ /* byte 1 */
275 		/* 0 */ "<Park mode> ",
276 		/* 1 */ "<RSSI> ",
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> "
283 	},
284 	{ /* byte 2 */
285 		/* 0 */ "<CVSD> ",
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> "
293 	}};
294 
295 	if (buffer != NULL && size > 0) {
296 		int	n, i, len0, len1;
297 
298 		memset(buffer, 0, size);
299 		len1 = 0;
300 
301 		for (n = 0; n < SIZE(t); n++) {
302 			for (i = 0; i < SIZE(t[n]); i++) {
303 				len0 = strlen(buffer);
304 				if (len0 >= size)
305 					goto done;
306 
307 				if (features[n] & (1 << i)) {
308 					if (len1 + strlen(t[n][i]) > 60) {
309 						len1 = 0;
310 						buffer[len0 - 1] = '\n';
311 					}
312 
313 					len1 += strlen(t[n][i]);
314 					strncat(buffer, t[n][i], size - len0);
315 				}
316 			}
317 		}
318 	}
319 done:
320 	return (buffer);
321 } /* hci_features2str */
322 
323 char const *
324 hci_cc2str(int cc)
325 {
326 	static char const * const	t[] = {
327 		/* 0x00 */ "North America, Europe, Japan",
328 		/* 0x01 */ "France"
329 	};
330 
331 	return (cc >= SIZE(t)? "?" : t[cc]);
332 } /* hci_cc2str */
333 
334 char const *
335 hci_con_state2str(int state)
336 {
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"
342         };
343 
344 	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
345 } /* hci_con_state2str */
346 
347 char const *
348 hci_status2str(int status)
349 {
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"
393 	};
394 
395 	return (status >= SIZE(t)? "Unknown error" : t[status]);
396 } /* hci_status2str */
397 
398 char const *
399 hci_bdaddr2str(bdaddr_t const *ba)
400 {
401 	extern int	 numeric_bdaddr;
402 	static char	 buffer[MAXHOSTNAMELEN];
403 	struct hostent	*he = NULL;
404 
405 	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
406 		buffer[0] = '*';
407 		buffer[1] = 0;
408 
409 		return (buffer);
410 	}
411 
412 	if (!numeric_bdaddr &&
413 	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
414 		strlcpy(buffer, he->h_name, sizeof(buffer));
415 
416 		return (buffer);
417 	}
418 
419 	bt_ntoa(ba, buffer);
420 
421 	return (buffer);
422 } /* hci_bdaddr2str */
423 
424