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