xref: /freebsd/usr.sbin/bluetooth/hccontrol/util.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 #include <bluetooth.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
38 
39 char const *
40 hci_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 
50 char const *
51 hci_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 
61 char const *
62 hci_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 
74 char const *
75 hci_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 
95 char const *
96 hci_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 
108 char const *
109 hci_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 
121 char const *
122 hci_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 
147 char const *
148 hci_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 
160 char const *
161 hci_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 
173 char const *
174 hci_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 
247 char const *
248 hci_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 */ "<Unknown2.7> "
280 	}};
281 
282 	if (buffer != NULL && size > 0) {
283 		int	n, i, len0, len1;
284 
285 		memset(buffer, 0, size);
286 		len1 = 0;
287 
288 		for (n = 0; n < SIZE(t); n++) {
289 			for (i = 0; i < SIZE(t[n]); i++) {
290 				len0 = strlen(buffer);
291 				if (len0 >= size)
292 					goto done;
293 
294 				if (features[n] & (1 << i)) {
295 					if (len1 + strlen(t[n][i]) > 60) {
296 						len1 = 0;
297 						buffer[len0 - 1] = '\n';
298 					}
299 
300 					len1 += strlen(t[n][i]);
301 					strncat(buffer, t[n][i], size - len0);
302 				}
303 			}
304 		}
305 	}
306 done:
307 	return (buffer);
308 } /* hci_features2str */
309 
310 char const *
311 hci_cc2str(int cc)
312 {
313 	static char const * const	t[] = {
314 		/* 0x00 */ "North America, Europe, Japan",
315 		/* 0x01 */ "France"
316 	};
317 
318 	return (cc >= SIZE(t)? "?" : t[cc]);
319 } /* hci_cc2str */
320 
321 char const *
322 hci_con_state2str(int state)
323 {
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"
329         };
330 
331 	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
332 } /* hci_con_state2str */
333 
334 char const *
335 hci_status2str(int status)
336 {
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"
380 	};
381 
382 	return (status >= SIZE(t)? "Unknown error" : t[status]);
383 } /* hci_status2str */
384 
385 char const *
386 hci_bdaddr2str(bdaddr_t const *ba)
387 {
388 	extern int	 numeric_bdaddr;
389 	static char	 buffer[MAXHOSTNAMELEN];
390 	struct hostent	*he = NULL;
391 
392 	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
393 		buffer[0] = '*';
394 		buffer[1] = 0;
395 
396 		return (buffer);
397 	}
398 
399 	if (!numeric_bdaddr &&
400 	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
401 		strlcpy(buffer, he->h_name, sizeof(buffer));
402 
403 		return (buffer);
404 	}
405 
406 	bt_ntoa(ba, buffer);
407 
408 	return (buffer);
409 } /* hci_bdaddr2str */
410 
411