xref: /freebsd/usr.sbin/bluetooth/hccontrol/util.c (revision 5a3935b6d66c1810125b0a92a0f26e207236f6fb)
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 	};
157 
158 	return (ver >= SIZE(t)? "?" : t[ver]);
159 } /* hci_ver2str */
160 
161 char const *
162 hci_lmpver2str(int ver)
163 {
164 	static char const * const	t[] = {
165 		/* 0x00 */ "Bluetooth LMP 1.0",
166 		/* 0x01 */ "Bluetooth LMP 1.1",
167 		/* 0x02 */ "Bluetooth LMP 1.2",
168 		/* 0x03 */ "Bluetooth LMP 2.0"
169 	};
170 
171 	return (ver >= SIZE(t)? "?" : t[ver]);
172 } /* hci_lmpver2str */
173 
174 char const *
175 hci_manufacturer2str(int m)
176 {
177 	static char const * const	t[] = {
178 		/* 0000 */ "Ericsson Technology Licensing",
179 		/* 0001 */ "Nokia Mobile Phones",
180 		/* 0002 */ "Intel Corp.",
181 		/* 0003 */ "IBM Corp.",
182 		/* 0004 */ "Toshiba Corp.",
183 		/* 0005 */ "3Com",
184 		/* 0006 */ "Microsoft",
185 		/* 0007 */ "Lucent",
186 		/* 0008 */ "Motorola",
187 		/* 0009 */ "Infineon Technologies AG",
188 		/* 0010 */ "Cambridge Silicon Radio",
189 		/* 0011 */ "Silicon Wave",
190 		/* 0012 */ "Digianswer A/S",
191 		/* 0013 */ "Texas Instruments Inc.",
192 		/* 0014 */ "Parthus Technologies Inc.",
193 		/* 0015 */ "Broadcom Corporation",
194 		/* 0016 */ "Mitel Semiconductor",
195 		/* 0017 */ "Widcomm, Inc.",
196 		/* 0018 */ "Zeevo, Inc.",
197 		/* 0019 */ "Atmel Corporation",
198 		/* 0020 */ "Mitsubishi Electric Corporation",
199 		/* 0021 */ "RTX Telecom A/S",
200 		/* 0022 */ "KC Technology Inc.",
201 		/* 0023 */ "Newlogic",
202 		/* 0024 */ "Transilica, Inc.",
203 		/* 0025 */ "Rohde & Schwartz GmbH & Co. KG",
204 		/* 0026 */ "TTPCom Limited",
205 		/* 0027 */ "Signia Technologies, Inc.",
206 		/* 0028 */ "Conexant Systems Inc.",
207 		/* 0029 */ "Qualcomm",
208 		/* 0030 */ "Inventel",
209 		/* 0031 */ "AVM Berlin",
210 		/* 0032 */ "BandSpeed, Inc.",
211 		/* 0033 */ "Mansella Ltd",
212 		/* 0034 */ "NEC Corporation",
213 		/* 0035 */ "WavePlus Technology Co., Ltd.",
214 		/* 0036 */ "Alcatel",
215 		/* 0037 */ "Philips Semiconductors",
216 		/* 0038 */ "C Technologies",
217 		/* 0039 */ "Open Interface",
218 		/* 0040 */ "R F Micro Devices",
219 		/* 0041 */ "Hitachi Ltd",
220 		/* 0042 */ "Symbol Technologies, Inc.",
221 		/* 0043 */ "Tenovis",
222 		/* 0044 */ "Macronix International Co. Ltd.",
223 		/* 0045 */ "GCT Semiconductor",
224 		/* 0046 */ "Norwood Systems",
225 		/* 0047 */ "MewTel Technology Inc.",
226 		/* 0048 */ "ST Microelectronics",
227 		/* 0049 */ "Synopsys",
228 		/* 0050 */ "Red-M (Communications) Ltd",
229 		/* 0051 */ "Commil Ltd",
230 		/* 0052 */ "Computer Access Technology Corporation (CATC)",
231 		/* 0053 */ "Eclipse (HQ Espana) S.L.",
232 		/* 0054 */ "Renesas Technology Corp.",
233 		/* 0055 */ "Mobilian Corporation",
234 		/* 0056 */ "Terax",
235 		/* 0057 */ "Integrated System Solution Corp.",
236 		/* 0058 */ "Matsushita Electric Industrial Co., Ltd.",
237 		/* 0059 */ "Gennum Corporation",
238 		/* 0060 */ "Research In Motion",
239 		/* 0061 */ "IPextreme, Inc.",
240 		/* 0062 */ "Systems and Chips, Inc",
241 		/* 0063 */ "Bluetooth SIG, Inc",
242 		/* 0064 */ "Seiko Epson Corporation"
243         };
244 
245 	return (m >= SIZE(t)? "?" : t[m]);
246 } /* hci_manufacturer2str */
247 
248 char const *
249 hci_features2str(uint8_t *features, char *buffer, int size)
250 {
251 	static char const * const	t[][8] = {
252 	{ /* byte 0 */
253 		/* 0 */ "<3-Slot> ",
254 		/* 1 */ "<5-Slot> ",
255 		/* 2 */ "<Encryption> ",
256 		/* 3 */ "<Slot offset> ",
257 		/* 4 */ "<Timing accuracy> ",
258 		/* 5 */ "<Switch> ",
259 		/* 6 */ "<Hold mode> ",
260 		/* 7 */ "<Sniff mode> "
261 	},
262 	{ /* byte 1 */
263 		/* 0 */ "<Park mode> ",
264 		/* 1 */ "<RSSI> ",
265 		/* 2 */ "<Channel quality> ",
266 		/* 3 */ "<SCO link> ",
267 		/* 4 */ "<HV2 packets> ",
268 		/* 5 */ "<HV3 packets> ",
269 		/* 6 */ "<u-law log> ",
270 		/* 7 */ "<A-law log> "
271 	},
272 	{ /* byte 2 */
273 		/* 0 */ "<CVSD> ",
274 		/* 1 */ "<Paging scheme> ",
275 		/* 2 */ "<Power control> ",
276 		/* 3 */ "<Transparent SCO data> ",
277 		/* 4 */ "<Flow control lag (bit0)> ",
278 		/* 5 */ "<Flow control lag (bit1)> ",
279 		/* 6 */ "<Flow control lag (bit2)> ",
280 		/* 7 */ "<Unknown2.7> "
281 	}};
282 
283 	if (buffer != NULL && size > 0) {
284 		int	n, i, len0, len1;
285 
286 		memset(buffer, 0, size);
287 		len1 = 0;
288 
289 		for (n = 0; n < SIZE(t); n++) {
290 			for (i = 0; i < SIZE(t[n]); i++) {
291 				len0 = strlen(buffer);
292 				if (len0 >= size)
293 					goto done;
294 
295 				if (features[n] & (1 << i)) {
296 					if (len1 + strlen(t[n][i]) > 60) {
297 						len1 = 0;
298 						buffer[len0 - 1] = '\n';
299 					}
300 
301 					len1 += strlen(t[n][i]);
302 					strncat(buffer, t[n][i], size - len0);
303 				}
304 			}
305 		}
306 	}
307 done:
308 	return (buffer);
309 } /* hci_features2str */
310 
311 char const *
312 hci_cc2str(int cc)
313 {
314 	static char const * const	t[] = {
315 		/* 0x00 */ "North America, Europe, Japan",
316 		/* 0x01 */ "France"
317 	};
318 
319 	return (cc >= SIZE(t)? "?" : t[cc]);
320 } /* hci_cc2str */
321 
322 char const *
323 hci_con_state2str(int state)
324 {
325 	static char const * const	t[] = {
326 		/* NG_HCI_CON_CLOSED */           "CLOSED",
327 		/* NG_HCI_CON_W4_LP_CON_RSP */    "W4_LP_CON_RSP",
328 		/* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
329 		/* NG_HCI_CON_OPEN */             "OPEN"
330         };
331 
332 	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
333 } /* hci_con_state2str */
334 
335 char const *
336 hci_status2str(int status)
337 {
338 	static char const * const       t[] = {
339 		/* 0x00 */ "No error",
340 		/* 0x01 */ "Unknown HCI command",
341 		/* 0x02 */ "No connection",
342 		/* 0x03 */ "Hardware failure",
343 		/* 0x04 */ "Page timeout",
344 		/* 0x05 */ "Authentication failure",
345 		/* 0x06 */ "Key missing",
346 		/* 0x07 */ "Memory full",
347 		/* 0x08 */ "Connection timeout",
348 		/* 0x09 */ "Max number of connections",
349 		/* 0x0a */ "Max number of SCO connections to a unit",
350 		/* 0x0b */ "ACL connection already exists",
351 		/* 0x0c */ "Command disallowed",
352 		/* 0x0d */ "Host rejected due to limited resources",
353 		/* 0x0e */ "Host rejected due to security reasons",
354 		/* 0x0f */ "Host rejected due to remote unit is a personal unit",
355 		/* 0x10 */ "Host timeout",
356 		/* 0x11 */ "Unsupported feature or parameter value",
357 		/* 0x12 */ "Invalid HCI command parameter",
358 		/* 0x13 */ "Other end terminated connection: User ended connection",
359 		/* 0x14 */ "Other end terminated connection: Low resources",
360 		/* 0x15 */ "Other end terminated connection: About to power off",
361 		/* 0x16 */ "Connection terminated by local host",
362 		/* 0x17 */ "Repeated attempts",
363 		/* 0x18 */ "Pairing not allowed",
364 		/* 0x19 */ "Unknown LMP PDU",
365 		/* 0x1a */ "Unsupported remote feature",
366 		/* 0x1b */ "SCO offset rejected",
367 		/* 0x1c */ "SCO interval rejected",
368 		/* 0x1d */ "SCO air mode rejected",
369 		/* 0x1e */ "Invalid LMP parameters",
370 		/* 0x1f */ "Unspecified error",
371 		/* 0x20 */ "Unsupported LMP parameter value",
372 		/* 0x21 */ "Role change not allowed",
373 		/* 0x22 */ "LMP response timeout",
374 		/* 0x23 */ "LMP error transaction collision",
375 		/* 0x24 */ "LMP PSU not allowed",
376 		/* 0x25 */ "Encryption mode not acceptable",
377 		/* 0x26 */ "Unit key used",
378 		/* 0x27 */ "QoS is not supported",
379 		/* 0x28 */ "Instant passed",
380 		/* 0x29 */ "Pairing with unit key not supported"
381 	};
382 
383 	return (status >= SIZE(t)? "Unknown error" : t[status]);
384 } /* hci_status2str */
385 
386 char const *
387 hci_bdaddr2str(bdaddr_t const *ba)
388 {
389 	extern int	 numeric_bdaddr;
390 	static char	 buffer[MAXHOSTNAMELEN];
391 	struct hostent	*he = NULL;
392 
393 	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
394 		buffer[0] = '*';
395 		buffer[1] = 0;
396 
397 		return (buffer);
398 	}
399 
400 	if (!numeric_bdaddr &&
401 	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
402 		strlcpy(buffer, he->h_name, sizeof(buffer));
403 
404 		return (buffer);
405 	}
406 
407 	bt_ntoa(ba, buffer);
408 
409 	return (buffer);
410 } /* hci_bdaddr2str */
411 
412