aboutsummaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/hccontrol/util.c
blob: c0c64969059d935d148f7081a6573ec834699850 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * util.c
 *
 * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: util.c,v 1.2 2003/05/19 17:29:29 max Exp $
 * $FreeBSD$
 */
 
#include <sys/param.h>
#include <bluetooth.h>
#include <stdio.h>
#include <string.h>

#define SIZE(x) (sizeof((x))/sizeof((x)[0]))

char const *
hci_link2str(int link_type)
{
	static char const * const	t[] = {
		/* NG_HCI_LINK_SCO */ "SCO",
		/* NG_HCI_LINK_ACL */ "ACL"
	};

	return (link_type >= SIZE(t)? "?" : t[link_type]);
} /* hci_link2str */

char const *
hci_pin2str(int type)
{
	static char const * const	t[] = {
		/* 0x00 */ "Variable PIN",
		/* 0x01 */ "Fixed PIN"
	};

	return (type >= SIZE(t)? "?" : t[type]);
} /* hci_pin2str */

char const *
hci_scan2str(int scan)
{
	static char const * const	t[] = {
		/* 0x00 */ "No Scan enabled",
		/* 0x01 */ "Inquiry Scan enabled. Page Scan disabled",
		/* 0x02 */ "Inquiry Scan disabled. Page Scan enabled",
		/* 0x03 */ "Inquiry Scan enabled. Page Scan enabled"
	};

	return (scan >= SIZE(t)? "?" : t[scan]);
} /* hci_scan2str */

char const *
hci_encrypt2str(int encrypt, int brief)
{
	static char const * const	t[] = {
		/* 0x00 */ "Disabled",
		/* 0x01 */ "Only for point-to-point packets",
		/* 0x02 */ "Both point-to-point and broadcast packets"
	};

	static char const * const	t1[] = {
		/* NG_HCI_ENCRYPTION_MODE_NONE */ "NONE",
		/* NG_HCI_ENCRYPTION_MODE_P2P */  "P2P",
		/* NG_HCI_ENCRYPTION_MODE_ALL */  "ALL",
	};

	if (brief)
		return (encrypt >= SIZE(t1)? "?" : t1[encrypt]);

	return (encrypt >= SIZE(t)? "?" : t[encrypt]);
} /* hci_encrypt2str */

char const *
hci_coding2str(int coding)
{
	static char const * const	t[] = {
		/* 0x00 */ "Linear",
		/* 0x01 */ "u-law",
		/* 0x02 */ "A-law",
		/* 0x03 */ "Reserved"
	};

	return (coding >= SIZE(t)? "?" : t[coding]);
} /* hci_coding2str */

char const *
hci_vdata2str(int data)
{
	static char const * const	t[] = {
		/* 0x00 */ "1's complement",
		/* 0x01 */ "2's complement",
		/* 0x02 */ "Sign-Magnitude",
		/* 0x03 */ "Reserved"
	};

	return (data >= SIZE(t)? "?" : t[data]);
} /* hci_vdata2str */

char const *
hci_hmode2str(int mode, char *buffer, int size)
{
	static char const * const	t[] = {
		/* 0x01 */ "Suspend Page Scan ",
		/* 0x02 */ "Suspend Inquiry Scan ",
		/* 0x04 */ "Suspend Periodic Inquiries "
        };

	if (buffer != NULL && size > 0) {
		int	n;

		memset(buffer, 0, size);
		for (n = 0; n < SIZE(t); n++) {
			int	len = strlen(buffer);

			if (len >= size)
				break;
			if (mode & (1 << n))
				strncat(buffer, t[n], size - len);
		}
	}

	return (buffer);
} /* hci_hmode2str */

char const *
hci_ver2str(int ver)
{
	static char const * const	t[] = {
		/* 0x00 */ "Bluetooth HCI Specification 1.0B",
		/* 0x01 */ "Bluetooth HCI Specification 1.1",
		/* 0x02 */ "Bluetooth HCI Specification 1.2",
		/* 0x03 */ "Bluetooth HCI Specification 2.0"
	};

	return (ver >= SIZE(t)? "?" : t[ver]);
} /* hci_ver2str */

char const *
hci_lmpver2str(int ver)
{
	static char const * const	t[] = {
		/* 0x00 */ "Bluetooth LMP 1.0",
		/* 0x01 */ "Bluetooth LMP 1.1",
		/* 0x02 */ "Bluetooth LMP 1.2",
		/* 0x03 */ "Bluetooth LMP 2.0"
	};

	return (ver >= SIZE(t)? "?" : t[ver]);
} /* hci_lmpver2str */

char const *
hci_manufacturer2str(int m)
{
	static char const * const	t[] = {
		/* 0000 */ "Ericsson Technology Licensing",
		/* 0001 */ "Nokia Mobile Phones",
		/* 0002 */ "Intel Corp.",
		/* 0003 */ "IBM Corp.",
		/* 0004 */ "Toshiba Corp.",
		/* 0005 */ "3Com",
		/* 0006 */ "Microsoft",
		/* 0007 */ "Lucent",
		/* 0008 */ "Motorola",
		/* 0009 */ "Infineon Technologies AG",
		/* 0010 */ "Cambridge Silicon Radio",
		/* 0011 */ "Silicon Wave",
		/* 0012 */ "Digianswer A/S",
		/* 0013 */ "Texas Instruments Inc.",
		/* 0014 */ "Parthus Technologies Inc.",
		/* 0015 */ "Broadcom Corporation",
		/* 0016 */ "Mitel Semiconductor",
		/* 0017 */ "Widcomm, Inc.",
		/* 0018 */ "Zeevo, Inc.",
		/* 0019 */ "Atmel Corporation",
		/* 0020 */ "Mitsubishi Electric Corporation",
		/* 0021 */ "RTX Telecom A/S",
		/* 0022 */ "KC Technology Inc.",
		/* 0023 */ "Newlogic",
		/* 0024 */ "Transilica, Inc.",
		/* 0025 */ "Rohde & Schwartz GmbH & Co. KG",
		/* 0026 */ "TTPCom Limited",
		/* 0027 */ "Signia Technologies, Inc.",
		/* 0028 */ "Conexant Systems Inc.",
		/* 0029 */ "Qualcomm",
		/* 0030 */ "Inventel",
		/* 0031 */ "AVM Berlin",
		/* 0032 */ "BandSpeed, Inc.",
		/* 0033 */ "Mansella Ltd",
		/* 0034 */ "NEC Corporation",
		/* 0035 */ "WavePlus Technology Co., Ltd.",
		/* 0036 */ "Alcatel",
		/* 0037 */ "Philips Semiconductors",
		/* 0038 */ "C Technologies",
		/* 0039 */ "Open Interface",
		/* 0040 */ "R F Micro Devices",
		/* 0041 */ "Hitachi Ltd",
		/* 0042 */ "Symbol Technologies, Inc.",
		/* 0043 */ "Tenovis",
		/* 0044 */ "Macronix International Co. Ltd.",
		/* 0045 */ "GCT Semiconductor",
		/* 0046 */ "Norwood Systems",
		/* 0047 */ "MewTel Technology Inc.",
		/* 0048 */ "ST Microelectronics",
		/* 0049 */ "Synopsys",
		/* 0050 */ "Red-M (Communications) Ltd",
		/* 0051 */ "Commil Ltd",
		/* 0052 */ "Computer Access Technology Corporation (CATC)",
		/* 0053 */ "Eclipse (HQ Espana) S.L.",
		/* 0054 */ "Renesas Technology Corp.",
		/* 0055 */ "Mobilian Corporation",
		/* 0056 */ "Terax",
		/* 0057 */ "Integrated System Solution Corp.",
		/* 0058 */ "Matsushita Electric Industrial Co., Ltd.",
		/* 0059 */ "Gennum Corporation",
		/* 0060 */ "Research In Motion",
		/* 0061 */ "IPextreme, Inc.",
		/* 0062 */ "Systems and Chips, Inc",
		/* 0063 */ "Bluetooth SIG, Inc",
		/* 0064 */ "Seiko Epson Corporation"
        };

	return (m >= SIZE(t)? "?" : t[m]);
} /* hci_manufacturer2str */

char const *
hci_features2str(uint8_t *features, char *buffer, int size)
{
	static char const * const	t[][8] = {
	{ /* byte 0 */
		/* 0 */ "<3-Slot> ",
		/* 1 */ "<5-Slot> ",
		/* 2 */ "<Encryption> ",
		/* 3 */ "<Slot offset> ",
		/* 4 */ "<Timing accuracy> ",
		/* 5 */ "<Switch> ",
		/* 6 */ "<Hold mode> ",
		/* 7 */ "<Sniff mode> "
	},
	{ /* byte 1 */
		/* 0 */ "<Park mode> ",
		/* 1 */ "<RSSI> ",
		/* 2 */ "<Channel quality> ",
		/* 3 */ "<SCO link> ",
		/* 4 */ "<HV2 packets> ",
		/* 5 */ "<HV3 packets> ",
		/* 6 */ "<u-law log> ",
		/* 7 */ "<A-law log> "
	},
	{ /* byte 2 */
		/* 0 */ "<CVSD> ",
		/* 1 */ "<Paging scheme> ",
		/* 2 */ "<Power control> ",
		/* 3 */ "<Transparent SCO data> ",
		/* 4 */ "<Flow control lag (bit0)> ",
		/* 5 */ "<Flow control lag (bit1)> ",
		/* 6 */ "<Flow control lag (bit2)> ",
		/* 7 */ "<Broadcast Encryption> "
	},
	{ /* byte 3 */
		/* 0 */ "<Unknown 3.0> ",
		/* 1 */ "<EDR ACL 2 Mb/s> ",
		/* 2 */ "<EDR ACL 3 Mb/s> ",
		/* 3 */ "<Enhanced inquiry scan> ",
		/* 4 */ "<Interlaced inquiry scan> ",
		/* 5 */ "<Interlaced page scan> ",
		/* 6 */ "<RSSI with inquiry results> ",
		/* 7 */ "<Extended SCO link (EV3 packets)> "
	},
	{ /* byte 4 */
		/* 0 */ "<EV4 packets> ",
		/* 1 */ "<EV5 packets> ",
		/* 2 */ "<Unknown 4.2> ",
		/* 3 */ "<AFH capable slave> ",
		/* 4 */ "<AFH classification slave> ",
		/* 5 */ "<BR/EDR Not Supported> ",
		/* 6 */ "<LE Supported (Controller)> ",
		/* 7 */ "<3-Slot EDR ACL packets> "
	},
	{ /* byte 5 */
		/* 0 */ "<5-Slot EDR ACL packets> ",
		/* 1 */ "<Sniff subrating> ",
		/* 2 */ "<Pause encryption> ",
		/* 3 */ "<AFH capable master> ",
		/* 4 */ "<AFH classification master> ",
		/* 5 */ "<EDR eSCO 2 Mb/s mode> ",
		/* 6 */ "<EDR eSCO 3 Mb/s mode> ",
		/* 7 */ "<3-Slot EDR eSCO packets> "
	},
	{ /* byte 6 */
		/* 0 */ "<Enhanced Inquiry Response> ",
		/* 1 */ "<Simultaneous LE and BR/EDR (Controller)> ",
		/* 2 */ "<Unknown 6.2> ",
		/* 3 */ "<Secure Simple Pairing (Controller Support)> ",
		/* 4 */ "<Encapsulated PDU> ",
		/* 5 */ "<Erroneous Data Reporting> ",
		/* 6 */ "<Non-flushable Packed Boundary Flag> ",
		/* 7 */ "<Unknown 6.7> "
	},
	{ /* byte 7 */
		/* 0 */ "<HCI_Link_Supervision_Timeout_Changed event> ",
		/* 1 */ "<Variable Inquiry TX Power Level> ",
		/* 2 */ "<Enhanced Power Control> ",
		/* 3 */ "<Unknown 7.3> ",
		/* 4 */ "<Unknown 7.4> ",
		/* 5 */ "<Unknown 7.5> ",
		/* 6 */ "<Unknown 7.6> ",
		/* 7 */ "<Extended features> "
	}};

	if (buffer != NULL && size > 0) {
		int	n, i, len0, len1;

		memset(buffer, 0, size);
		len1 = 0;

		for (n = 0; n < SIZE(t); n++) {
			for (i = 0; i < SIZE(t[n]); i++) {
				len0 = strlen(buffer);
				if (len0 >= size)
					goto done;

				if (features[n] & (1 << i)) {
					if (len1 + strlen(t[n][i]) > 60) {
						len1 = 0;
						buffer[len0 - 1] = '\n';
					}

					len1 += strlen(t[n][i]);
					strncat(buffer, t[n][i], size - len0);
				}
			}
		}
	}
done:
	return (buffer);
} /* hci_features2str */

char const *
hci_cc2str(int cc)
{
	static char const * const	t[] = {
		/* 0x00 */ "North America, Europe, Japan",
		/* 0x01 */ "France"
	};

	return (cc >= SIZE(t)? "?" : t[cc]);
} /* hci_cc2str */

char const *
hci_con_state2str(int state)
{
	static char const * const	t[] = {
		/* NG_HCI_CON_CLOSED */           "CLOSED",
		/* NG_HCI_CON_W4_LP_CON_RSP */    "W4_LP_CON_RSP",
		/* NG_HCI_CON_W4_CONN_COMPLETE */ "W4_CONN_COMPLETE",
		/* NG_HCI_CON_OPEN */             "OPEN"
        };

	return (state >= SIZE(t)? "UNKNOWN" : t[state]);
} /* hci_con_state2str */

char const *
hci_status2str(int status)
{
	static char const * const       t[] = {
		/* 0x00 */ "No error",
		/* 0x01 */ "Unknown HCI command",
		/* 0x02 */ "No connection",
		/* 0x03 */ "Hardware failure",
		/* 0x04 */ "Page timeout",
		/* 0x05 */ "Authentication failure",
		/* 0x06 */ "Key missing",
		/* 0x07 */ "Memory full",
		/* 0x08 */ "Connection timeout",
		/* 0x09 */ "Max number of connections",
		/* 0x0a */ "Max number of SCO connections to a unit",
		/* 0x0b */ "ACL connection already exists",
		/* 0x0c */ "Command disallowed",
		/* 0x0d */ "Host rejected due to limited resources",
		/* 0x0e */ "Host rejected due to security reasons",
		/* 0x0f */ "Host rejected due to remote unit is a personal unit",
		/* 0x10 */ "Host timeout",
		/* 0x11 */ "Unsupported feature or parameter value",
		/* 0x12 */ "Invalid HCI command parameter",
		/* 0x13 */ "Other end terminated connection: User ended connection",
		/* 0x14 */ "Other end terminated connection: Low resources",
		/* 0x15 */ "Other end terminated connection: About to power off",
		/* 0x16 */ "Connection terminated by local host",
		/* 0x17 */ "Repeated attempts",
		/* 0x18 */ "Pairing not allowed",
		/* 0x19 */ "Unknown LMP PDU",
		/* 0x1a */ "Unsupported remote feature",
		/* 0x1b */ "SCO offset rejected",
		/* 0x1c */ "SCO interval rejected",
		/* 0x1d */ "SCO air mode rejected",
		/* 0x1e */ "Invalid LMP parameters",
		/* 0x1f */ "Unspecified error",
		/* 0x20 */ "Unsupported LMP parameter value",
		/* 0x21 */ "Role change not allowed",
		/* 0x22 */ "LMP response timeout",
		/* 0x23 */ "LMP error transaction collision",
		/* 0x24 */ "LMP PSU not allowed",
		/* 0x25 */ "Encryption mode not acceptable",
		/* 0x26 */ "Unit key used",
		/* 0x27 */ "QoS is not supported",
		/* 0x28 */ "Instant passed",
		/* 0x29 */ "Pairing with unit key not supported"
	};

	return (status >= SIZE(t)? "Unknown error" : t[status]);
} /* hci_status2str */

char const *
hci_bdaddr2str(bdaddr_t const *ba)
{
	extern int	 numeric_bdaddr;
	static char	 buffer[MAXHOSTNAMELEN];
	struct hostent	*he = NULL;

	if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
		buffer[0] = '*';
		buffer[1] = 0;

		return (buffer);
	}

	if (!numeric_bdaddr &&
	    (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
		strlcpy(buffer, he->h_name, sizeof(buffer));

		return (buffer);
	}

	bt_ntoa(ba, buffer);

	return (buffer);
} /* hci_bdaddr2str */