xref: /freebsd/usr.sbin/bluetooth/hccontrol/util.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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.1 2002/11/24 20:22:38 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/types.h>
33 #include <string.h>
34 
35 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
36 
37 char const * const
38 hci_link2str(int link_type)
39 {
40 	static char const * const	t[] = {
41 		/* NG_HCI_LINK_SCO */ "SCO",
42 		/* NG_HCI_LINK_ACL */ "ACL"
43 	};
44 
45 	return (link_type >= SIZE(t)? "?" : t[link_type]);
46 } /* hci_link2str */
47 
48 char const * const
49 hci_pin2str(int type)
50 {
51 	static char const * const	t[] = {
52 		/* 0x00 */ "Variable PIN",
53 		/* 0x01 */ "Fixed PIN"
54 	};
55 
56 	return (type >= SIZE(t)? "?" : t[type]);
57 } /* hci_pin2str */
58 
59 char const * const
60 hci_scan2str(int scan)
61 {
62 	static char const * const	t[] = {
63 		/* 0x00 */ "No Scan enabled",
64 		/* 0x01 */ "Inquiry Scan enabled. Page Scan disabled",
65 		/* 0x02 */ "Inquiry Scan disabled. Page Scan enabled",
66 		/* 0x03 */ "Inquiry Scan enabled. Page Scan enabled"
67 	};
68 
69 	return (scan >= SIZE(t)? "?" : t[scan]);
70 } /* hci_scan2str */
71 
72 char const * const
73 hci_encrypt2str(int encrypt, int brief)
74 {
75 	static char const * const	t[] = {
76 		/* 0x00 */ "Disabled",
77 		/* 0x01 */ "Only for point-to-point packets",
78 		/* 0x02 */ "Both point-to-point and broadcast packets"
79 	};
80 
81 	static char const * const	t1[] = {
82 		/* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE",
83 		/* NG_HCI_ENCRYPTION_MODE_P2P */  "P2P",
84 		/* NG_HCI_ENCRYPTION_MODE_ALL */  "ALL",
85 	};
86 
87 	if (brief)
88 		return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);
89 
90 	return (encrypt >= SIZE(t)? "?" : t[encrypt]);
91 } /* hci_encrypt2str */
92 
93 char const * const
94 hci_coding2str(int coding)
95 {
96 	static char const * const	t[] = {
97 		/* 0x00 */ "Linear",
98 		/* 0x01 */ "u-law",
99 		/* 0x02 */ "A-law",
100 		/* 0x03 */ "Reserved"
101 	};
102 
103 	return (coding >= SIZE(t)? "?" : t[coding]);
104 } /* hci_coding2str */
105 
106 char const * const
107 hci_vdata2str(int data)
108 {
109 	static char const * const	t[] = {
110 		/* 0x00 */ "1's complement",
111 		/* 0x01 */ "2's complement",
112 		/* 0x02 */ "Sign-Magnitude",
113 		/* 0x03 */ "Reserved"
114 	};
115 
116 	return (data >= SIZE(t)? "?" : t[data]);
117 } /* hci_vdata2str */
118 
119 char const * const
120 hci_hmode2str(int mode, char *buffer, int size)
121 {
122 	static char const * const	t[] = {
123 		/* 0x01 */ "Suspend Page Scan ",
124 		/* 0x02 */ "Suspend Inquiry Scan ",
125 		/* 0x04 */ "Suspend Periodic Inquiries "
126         };
127 
128 	if (buffer != NULL && size > 0) {
129 		int	n;
130 
131 		memset(buffer, 0, size);
132 		for (n = 0; n < SIZE(t); n++) {
133 			int	len = strlen(buffer);
134 
135 			if (len >= size)
136 				break;
137 			if (mode & (1 << n))
138 				strncat(buffer, t[n], size - len);
139 		}
140 	}
141 
142 	return (buffer);
143 } /* hci_hmode2str */
144 
145 char const * const
146 hci_ver2str(int ver)
147 {
148 	static char const * const	t[] = {
149 		/* 0x00 */ "v1.0B",
150 		/* 0x01 */ "v1.1"
151 	};
152 
153 	return (ver >= SIZE(t)? "?" : t[ver]);
154 } /* hci_ver2str */
155 
156 char const * const
157 hci_manufacturer2str(int m)
158 {
159 	static char const * const	t[] = {
160 		/* 0000 */ "Ericsson Mobile Comunications",
161 		/* 0001 */ "Nokia Mobile Phones",
162 		/* 0002 */ "Intel Corp.",
163 		/* 0003 */ "IBM Corp.",
164 		/* 0004 */ "Toshiba Corp.",
165 		/* 0005 */ "3Com",
166 		/* 0006 */ "Microsoft",
167 		/* 0007 */ "Lucent",
168 		/* 0008 */ "Motorola",
169 		/* 0009 */ "Infineon Technologies AG",
170 		/* 0010 */ "Cambridge Silicon Radio",
171 		/* 0011 */ "Silicon Wave",
172 		/* 0012 */ "Digianswer A/S",
173 		/* 0013 */ "Texas Instruments Inc.",
174 		/* 0014 */ "Parthus Technologies Inc.",
175 		/* 0015 */ "Broadcom Corporation",
176 		/* 0016 */ "Mitel Semiconductor",
177 		/* 0017 */ "Widcomm, Inc.",
178 		/* 0018 */ "Telencomm Inc.",
179 		/* 0019 */ "Atmel Corporation",
180 		/* 0020 */ "Mitsubishi Electric Corporation",
181 		/* 0021 */ "RTX Telecom A/S",
182 		/* 0022 */ "KC Technology Inc.",
183 		/* 0023 */ "Newlogic",
184 		/* 0024 */ "Transilica, Inc.",
185 		/* 0025 */ "Rohde & Schwartz GmbH & Co. KG",
186 		/* 0026 */ "TTPCom Limited",
187 		/* 0027 */ "Signia Technologies, Inc.",
188 		/* 0028 */ "Conexant Systems Inc.",
189 		/* 0029 */ "Qualcomm",
190 		/* 0030 */ "Inventel",
191 		/* 0031 */ "AVM Berlin",
192 		/* 0032 */ "BandSpeed, Inc.",
193 		/* 0033 */ "Mansella Ltd",
194 		/* 0034 */ "NEC Corporation",
195 		/* 0035 */ "WavePlus Technology Co., Ltd.",
196 		/* 0036 */ "Alcatel",
197 		/* 0037 */ "Philips Semiconductors",
198 		/* 0038 */ "C Technologies",
199 		/* 0039 */ "Open Interface",
200 		/* 0040 */ "R F Micro Devices",
201 		/* 0041 */ "Hitachi Ltd",
202 		/* 0042 */ "Symbol Technologies, Inc.",
203 		/* 0043 */ "Tenovis",
204 		/* 0044 */ "Macronix International Co. Ltd.",
205 		/* 0045 */ "GCT Semiconductor",
206 		/* 0046 */ "Norwood Systems",
207 		/* 0047 */ "MewTel Technology Inc."
208         };
209 
210 	return (m >= SIZE(t)? "?" : t[m]);
211 } /* hci_manufacturer2str */
212 
213 char const * const
214 hci_features2str(u_int8_t *features, char *buffer, int size)
215 {
216 	static char const * const	t[][8] = {
217 	{ /* byte 0 */
218 		/* 0 */ "<3-Slot> ",
219 		/* 1 */ "<5-Slot> ",
220 		/* 2 */ "<Encryption> ",
221 		/* 3 */ "<Slot offset> ",
222 		/* 4 */ "<Timing accuracy> ",
223 		/* 5 */ "<Switch> ",
224 		/* 6 */ "<Hold mode> ",
225 		/* 7 */ "<Sniff mode> "
226 	},
227 	{ /* byte 1 */
228 		/* 0 */ "<Park mode> ",
229 		/* 1 */ "<RSSI> ",
230 		/* 2 */ "<Channel quality> ",
231 		/* 3 */ "<SCO link> ",
232 		/* 4 */ "<HV2 packets> ",
233 		/* 5 */ "<HV3 packets> ",
234 		/* 6 */ "<u-law log> ",
235 		/* 7 */ "<A-law log> "
236 	},
237 	{ /* byte 2 */
238 		/* 0 */ "<CVSD> ",
239 		/* 1 */ "<Paging scheme> ",
240 		/* 2 */ "<Power control> ",
241 		/* 3 */ "<Transparent SCO data> ",
242 		/* 4 */ "<Flow control lag (bit0)> ",
243 		/* 5 */ "<Flow control lag (bit1)> ",
244 		/* 6 */ "<Flow control lag (bit2)> ",
245 		/* 7 */ "<Unknown2.7> "
246 	}};
247 
248 	if (buffer != NULL && size > 0) {
249 		int	n, i, len0, len1;
250 
251 		memset(buffer, 0, size);
252 		len1 = 0;
253 
254 		for (n = 0; n < SIZE(t); n++) {
255 			for (i = 0; i < SIZE(t[n]); i++) {
256 				len0 = strlen(buffer);
257 				if (len0 >= size)
258 					goto done;
259 
260 				if (features[n] & (1 << i)) {
261 					if (len1 + strlen(t[n][i]) > 60) {
262 						len1 = 0;
263 						buffer[len0 - 1] = '\n';
264 					}
265 
266 					len1 += strlen(t[n][i]);
267 					strncat(buffer, t[n][i], size - len0);
268 				}
269 			}
270 		}
271 	}
272 done:
273 	return (buffer);
274 } /* hci_features2str */
275 
276 char const * const
277 hci_cc2str(int cc)
278 {
279 	static char const * const	t[] = {
280 		/* 0x00 */ "North America, Europe, Japan",
281 		/* 0x01 */ "France"
282 	};
283 
284 	return (cc >= SIZE(t)? "?" : t[cc]);
285 } /* hci_cc2str */
286 
287 char const * const
288 hci_con_state2str(int state)
289 {
290 	static char const * const	t[] = {
291 		/* NG_HCI_CON_CLOSED */           "CLOSED",
292 		/* NG_HCI_CON_W4_LP_CON_RSP */    "W4_LP_CON_RSP",
293 		/* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
294 		/* NG_HCI_CON_OPEN */             "OPEN"
295         };
296 
297 	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
298 } /* hci_con_state2str */
299 
300 char const * const
301 hci_status2str(int status)
302 {
303 	static char const * const       t[] = {
304 		/* 0x00 */ "No error",
305 		/* 0x01 */ "Unknown HCI command",
306 		/* 0x02 */ "No connection",
307 		/* 0x03 */ "Hardware failure",
308 		/* 0x04 */ "Page timeout",
309 		/* 0x05 */ "Authentication failure",
310 		/* 0x06 */ "Key missing",
311 		/* 0x07 */ "Memory full",
312 		/* 0x08 */ "Connection timeout",
313 		/* 0x09 */ "Max number of connections",
314 		/* 0x0a */ "Max number of SCO connections to a unit",
315 		/* 0x0b */ "ACL connection already exists",
316 		/* 0x0c */ "Command disallowed",
317 		/* 0x0d */ "Host rejected due to limited resources",
318 		/* 0x0e */ "Host rejected due to securiity reasons",
319 		/* 0x0f */ "Host rejected due to remote unit is a personal unit",
320 		/* 0x10 */ "Host timeout",
321 		/* 0x11 */ "Unsupported feature or parameter value",
322 		/* 0x12 */ "Invalid HCI command parameter",
323 		/* 0x13 */ "Other end terminated connection: User ended connection",
324 		/* 0x14 */ "Other end terminated connection: Low resources",
325 		/* 0x15 */ "Other end terminated connection: About to power off",
326 		/* 0x16 */ "Connection terminated by local host",
327 		/* 0x17 */ "Repeated attempts",
328 		/* 0x18 */ "Pairing not allowed",
329 		/* 0x19 */ "Unknown LMP PDU",
330 		/* 0x1a */ "Unsupported remote feature",
331 		/* 0x1b */ "SCO offset rejected",
332 		/* 0x1c */ "SCO interval rejected",
333 		/* 0x1d */ "SCO air mode rejected",
334 		/* 0x1e */ "Invalid LMP parameters",
335 		/* 0x1f */ "Unspecified error",
336 		/* 0x20 */ "Unsupported LMP parameter value",
337 		/* 0x21 */ "Role change not allowed",
338 		/* 0x22 */ "LMP response timeout",
339 		/* 0x23 */ "LMP error transaction collision",
340 		/* 0x24 */ "LMP PSU not allowed",
341 		/* 0x25 */ "Encryption mode not acceptable",
342 		/* 0x26 */ "Unit key used",
343 		/* 0x27 */ "QoS is not supported",
344 		/* 0x28 */ "Instant passed",
345 		/* 0x29 */ "Paring with unit key not supported"
346 	};
347 
348 	return (status >= SIZE(t)? "Unknown error" : t[status]);
349 } /* hci_status2str */
350 
351