link_control.c revision 114879
1/*
2 * link_control.c
3 *
4 * Copyright (c) 2001-2002 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: link_control.c,v 1.2 2003/03/15 03:07:39 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/hccontrol/link_control.c 114879 2003-05-10 21:50:37Z julian $
30 */
31
32#include <sys/types.h>
33#include <sys/endian.h>
34#include <errno.h>
35#include <ng_hci.h>
36#include <stdio.h>
37#include <string.h>
38#include "hccontrol.h"
39
40static void hci_inquiry_response (int n, u_int8_t **b);
41
42/* Send Inquiry command to the unit */
43static int
44hci_inquiry(int s, int argc, char **argv)
45{
46	int			 n0, n1, n2, timo;
47	u_int8_t		 b[512];
48	ng_hci_inquiry_cp	 cp;
49	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
50
51	/* set defaults */
52	cp.lap[2] = 0x9e;
53	cp.lap[1] = 0x8b;
54	cp.lap[0] = 0x33;
55	cp.inquiry_length = 5;
56	cp.num_responses = 8;
57
58	/* parse command parameters */
59	switch (argc) {
60	case 3:
61		/* LAP */
62		if (sscanf(argv[0], "%x:%x:%x", &n2, &n1, &n0) != 3)
63			return (USAGE);
64
65		cp.lap[0] = (n0 & 0xff);
66		cp.lap[1] = (n1 & 0xff);
67		cp.lap[2] = (n2 & 0xff);
68
69	/* inquiry length (N * 1.28) sec, range 0x01 - 0x30 */
70	case 2:
71		if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x1 || n0 > 0x30)
72			return (USAGE);
73
74		cp.inquiry_length = (n0 & 0xff);
75
76	/* number of responses, range 0x00 - 0xff */
77	case 1:
78		if (sscanf(argv[2], "%d", &n0) != 1 || n0 > 0xff)
79			return (USAGE);
80
81		cp.num_responses = (n0 & 0xff);
82
83	/* use defaults */
84	case 0:
85		break;
86
87	default:
88		return (USAGE);
89	}
90
91	/* send request and expect status back */
92	n0 = sizeof(b);
93	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
94			NG_HCI_OCF_INQUIRY), (char const *) &cp, sizeof(cp),
95			b, &n0) == ERROR)
96		return (ERROR);
97
98	if (*b != 0x00)
99		return (FAILED);
100
101	timo = timeout;
102	timeout = cp.inquiry_length * 1.28 + 1;
103
104wait_for_more:
105	/* wait for inquiry events */
106	n0 = sizeof(b);
107	if (hci_recv(s, b, &n0) == ERROR) {
108		timeout = timo;
109		return (ERROR);
110	}
111
112	if (n0 < sizeof(*e)) {
113		timeout = timo;
114		errno = EIO;
115		return (ERROR);
116	}
117
118	switch (e->event) {
119	case NG_HCI_EVENT_INQUIRY_RESULT: {
120		ng_hci_inquiry_result_ep	*ir =
121				(ng_hci_inquiry_result_ep *)(e + 1);
122		u_int8_t			*r = (u_int8_t *)(ir + 1);
123
124		fprintf(stdout, "Inquiry result, num_responses=%d\n",
125			ir->num_responses);
126
127		for (n0 = 0; n0 < ir->num_responses; n0++)
128			hci_inquiry_response(n0, &r);
129
130		goto wait_for_more;
131		}
132
133	case NG_HCI_EVENT_INQUIRY_COMPL:
134		fprintf(stdout, "Inquiry complete. Status: %s [%#02x]\n",
135			hci_status2str(*(b + sizeof(*e))), *(b + sizeof(*e)));
136		break;
137
138	default:
139		goto wait_for_more;
140	}
141
142	timeout = timo;
143
144	return (OK);
145} /* hci_inquiry */
146
147/* Print Inquiry_Result event */
148static void
149hci_inquiry_response(int n, u_int8_t **b)
150{
151	struct inquiry_response {
152		bdaddr_t	bdaddr;
153		u_int8_t	page_scan_rep_mode;
154		u_int8_t	page_scan_period_mode;
155		u_int8_t	page_scan_mode;
156		u_int8_t	class[NG_HCI_CLASS_SIZE];
157		u_int16_t	clock_offset;
158	}			*ir = (struct inquiry_response *)(*b);
159
160	fprintf(stdout, "Inquiry result #%d\n", n);
161	fprintf(stdout, "\tBD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
162		ir->bdaddr.b[5], ir->bdaddr.b[4], ir->bdaddr.b[3],
163		ir->bdaddr.b[2], ir->bdaddr.b[1], ir->bdaddr.b[0]);
164	fprintf(stdout, "\tPage Scan Rep. Mode: %#02x\n",
165		ir->page_scan_rep_mode);
166	fprintf(stdout, "\tPage Scan Period Mode: %#02x\n",
167		ir->page_scan_period_mode);
168	fprintf(stdout, "\tPage Scan Mode: %#02x\n",
169		ir->page_scan_mode);
170	fprintf(stdout, "\tClass: %02x:%02x:%02x\n",
171		ir->class[2], ir->class[1], ir->class[0]);
172	fprintf(stdout, "\tClock offset: %#04x\n",
173		le16toh(ir->clock_offset));
174
175	*b += sizeof(*ir);
176} /* hci_inquiry_response */
177
178/* Send Create_Connection command to the unit */
179static int
180hci_create_connection(int s, int argc, char **argv)
181{
182	int			 n0, n1, n2, n3, n4, n5;
183	char			 b[512];
184	ng_hci_create_con_cp	 cp;
185	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
186
187	/* Set defaults */
188	memset(&cp, 0, sizeof(cp));
189	cp.pkt_type = htole16(	NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 |
190				NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 |
191				NG_HCI_PKT_DM5);
192	cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0;
193	cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE;
194	cp.clock_offset = 0;
195	cp.accept_role_switch = 1;
196
197	/* parse command parameters */
198	switch (argc) {
199	case 6:
200		/* accept role switch */
201		if (sscanf(argv[2], "%d", &n0) != 1)
202			return (USAGE);
203
204		cp.accept_role_switch = n0 ? 1 : 0;
205
206	case 5:
207		/* clock offset */
208		if (sscanf(argv[2], "%d", &n0) != 1)
209			return (USAGE);
210
211		cp.clock_offset = (n0 & 0xffff);
212		cp.clock_offset = htole16(cp.clock_offset);
213
214	case 4:
215		/* page scan mode */
216		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 3)
217			return (USAGE);
218
219		cp.page_scan_mode = (n0 & 0xff);
220
221	case 3:
222		/* page scan rep mode */
223		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 2)
224			return (USAGE);
225
226		cp.page_scan_rep_mode = (n0 & 0xff);
227
228	case 2:
229		/* packet type */
230		if (sscanf(argv[1], "%x", &n0) != 1)
231			return (USAGE);
232
233		n0 &= (	NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 |
234			NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 |
235			NG_HCI_PKT_DM5);
236		if (n0 == 0)
237			return (USAGE);
238
239		cp.pkt_type = (n0 & 0xffff);
240		cp.pkt_type = htole16(cp.pkt_type);
241
242	case 1:
243		/* BD_ADDR */
244		if (sscanf(argv[0], "%x:%x:%x:%x:%x:%x",
245				&n5, &n4, &n3, &n2, &n1, &n0) != 6)
246			return (USAGE);
247
248		cp.bdaddr.b[0] = (n0 & 0xff);
249		cp.bdaddr.b[1] = (n1 & 0xff);
250		cp.bdaddr.b[2] = (n2 & 0xff);
251		cp.bdaddr.b[3] = (n3 & 0xff);
252		cp.bdaddr.b[4] = (n4 & 0xff);
253		cp.bdaddr.b[5] = (n5 & 0xff);
254		break;
255
256	default:
257		return (USAGE);
258	}
259
260	/* send request and expect status response */
261	n0 = sizeof(b);
262	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
263			NG_HCI_OCF_CREATE_CON),
264			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
265		return (ERROR);
266
267	if (*b != 0x00)
268		return (FAILED);
269
270	/* wait for event */
271again:
272	n0 = sizeof(b);
273	if (hci_recv(s, b, &n0) == ERROR)
274		return (ERROR);
275	if (n0 < sizeof(*e)) {
276		errno = EIO;
277		return (ERROR);
278	}
279
280	if (e->event == NG_HCI_EVENT_CON_COMPL) {
281		ng_hci_con_compl_ep	*ep = (ng_hci_con_compl_ep *)(e + 1);
282
283		if (ep->status != 0x00) {
284			fprintf(stdout, "Status: %s [%#02x]\n",
285				hci_status2str(ep->status), ep->status);
286			return (FAILED);
287		}
288
289		fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
290			ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
291			ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
292		fprintf(stdout, "Connection handle: %d\n",
293			le16toh(ep->con_handle));
294		fprintf(stdout, "Encryption mode: %s [%d]\n",
295			hci_encrypt2str(ep->encryption_mode, 0),
296			ep->encryption_mode);
297	} else
298		goto again;
299
300	return (OK);
301} /* hci_create_connection */
302
303/* Send Disconnect command to the unit */
304static int
305hci_disconnect(int s, int argc, char **argv)
306{
307	int			 n;
308	char			 b[512];
309	ng_hci_discon_cp	 cp;
310	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
311
312	/* Set defaults */
313	memset(&cp, 0, sizeof(cp));
314	cp.reason = 0x13;
315
316	/* parse command parameters */
317	switch (argc) {
318	case 2:
319		/* reason */
320		if (sscanf(argv[1], "%d", &n) != 1 || n <= 0x00 || n > 0xff)
321			return (USAGE);
322
323		cp.reason = (u_int8_t) (n & 0xff);
324
325	case 1:
326		/* connection handle */
327		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
328			return (USAGE);
329
330		cp.con_handle = (u_int16_t) (n & 0x0fff);
331		cp.con_handle = htole16(cp.con_handle);
332		break;
333
334	default:
335		return (USAGE);
336	}
337
338	/* send request and expect status response */
339	n = sizeof(b);
340	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
341			NG_HCI_OCF_DISCON),
342			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
343		return (ERROR);
344
345	if (*b != 0x00)
346		return (FAILED);
347
348	/* wait for event */
349again:
350	n = sizeof(b);
351	if (hci_recv(s, b, &n) == ERROR)
352		return (ERROR);
353	if (n < sizeof(*e)) {
354		errno = EIO;
355		return (ERROR);
356	}
357
358	if (e->event == NG_HCI_EVENT_DISCON_COMPL) {
359		ng_hci_discon_compl_ep	*ep = (ng_hci_discon_compl_ep *)(e + 1);
360
361		if (ep->status != 0x00) {
362			fprintf(stdout, "Status: %s [%#02x]\n",
363				hci_status2str(ep->status), ep->status);
364			return (FAILED);
365		}
366
367		fprintf(stdout, "Connection handle: %d\n",
368			le16toh(ep->con_handle));
369		fprintf(stdout, "Reason: %s [%#02x]\n",
370			hci_status2str(ep->reason), ep->reason);
371	} else
372		goto again;
373
374	return (OK);
375} /* hci_diconnect */
376
377/* Send Add_SCO_Connection command to the unit */
378static int
379hci_add_sco_connection(int s, int argc, char **argv)
380{
381	int			 n;
382	char			 b[512];
383	ng_hci_add_sco_con_cp	 cp;
384	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
385
386	/* Set defaults */
387	memset(&cp, 0, sizeof(cp));
388	cp.pkt_type = htole16(NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3);
389
390	/* parse command parameters */
391	switch (argc) {
392	case 2:
393		/* packet type */
394		if (sscanf(argv[0], "%x", &n) != 1)
395			return (USAGE);
396
397		n &= (NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3);
398		if (n == 0)
399			return (USAGE);
400
401		cp.pkt_type = (u_int16_t) (n & 0x0fff);
402		cp.pkt_type = htole16(cp.pkt_type);
403
404	case 1:
405		/* acl connection handle */
406		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
407			return (USAGE);
408
409		cp.con_handle = (u_int16_t) (n & 0x0fff);
410		cp.con_handle = htole16(cp.con_handle);
411		break;
412
413	default:
414		return (USAGE);
415	}
416
417	/* send request and expect status response */
418	n = sizeof(b);
419	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
420			NG_HCI_OCF_ADD_SCO_CON),
421			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
422		return (ERROR);
423
424	if (*b != 0x00)
425		return (FAILED);
426
427	/* wait for event */
428again:
429	n = sizeof(b);
430	if (hci_recv(s, b, &n) == ERROR)
431		return (ERROR);
432	if (n < sizeof(*e)) {
433		errno = EIO;
434		return (ERROR);
435	}
436
437	if (e->event == NG_HCI_EVENT_CON_COMPL) {
438		ng_hci_con_compl_ep	*ep = (ng_hci_con_compl_ep *)(e + 1);
439
440		if (ep->status != 0x00) {
441			fprintf(stdout, "Status: %s [%#02x]\n",
442				hci_status2str(ep->status), ep->status);
443			return (FAILED);
444		}
445
446		fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
447			ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
448			ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
449		fprintf(stdout, "Connection handle: %d\n",
450			le16toh(ep->con_handle));
451		fprintf(stdout, "Encryption mode: %s [%d]\n",
452			hci_encrypt2str(ep->encryption_mode, 0),
453			ep->encryption_mode);
454	} else
455		goto again;
456
457	return (OK);
458} /* Add_SCO_Connection */
459
460/* Send Change_Connection_Packet_Type command to the unit */
461static int
462hci_change_connection_packet_type(int s, int argc, char **argv)
463{
464	int				 n;
465	char				 b[512];
466	ng_hci_change_con_pkt_type_cp	 cp;
467	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b;
468
469	switch (argc) {
470	case 2:
471		/* connection handle */
472		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
473			return (USAGE);
474
475		cp.con_handle = (u_int16_t) (n & 0x0fff);
476		cp.con_handle = htole16(cp.con_handle);
477
478		/* packet type */
479		if (sscanf(argv[1], "%x", &n) != 1)
480			return (USAGE);
481
482		cp.pkt_type = (u_int16_t) (n & 0xffff);
483		cp.pkt_type = htole16(cp.pkt_type);
484		break;
485
486	default:
487		return (USAGE);
488	}
489
490	/* send request and expect status response */
491	n = sizeof(b);
492	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
493			NG_HCI_OCF_CHANGE_CON_PKT_TYPE),
494			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
495		return (ERROR);
496
497	if (*b != 0x00)
498		return (FAILED);
499
500	/* wait for event */
501again:
502	n = sizeof(b);
503	if (hci_recv(s, b, &n) == ERROR)
504		return (ERROR);
505	if (n < sizeof(*e)) {
506		errno = EIO;
507		return (ERROR);
508	}
509
510	if (e->event == NG_HCI_EVENT_CON_PKT_TYPE_CHANGED) {
511		ng_hci_con_pkt_type_changed_ep	*ep =
512				(ng_hci_con_pkt_type_changed_ep *)(e + 1);
513
514		if (ep->status != 0x00) {
515			fprintf(stdout, "Status: %s [%#02x]\n",
516				hci_status2str(ep->status), ep->status);
517			return (FAILED);
518		}
519
520		fprintf(stdout, "Connection handle: %d\n",
521			le16toh(ep->con_handle));
522		fprintf(stdout, "Packet type: %#04x\n",
523			le16toh(ep->pkt_type));
524	} else
525		goto again;
526
527	return (OK);
528} /* hci_change_connection_packet_type */
529
530/* Send Remote_Name_Request command to the unit */
531static int
532hci_remote_name_request(int s, int argc, char **argv)
533{
534	int				 n0, n1, n2, n3, n4, n5;
535	char				 b[512];
536	ng_hci_remote_name_req_cp	 cp;
537	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b;
538
539	memset(&cp, 0, sizeof(cp));
540	cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0;
541	cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE;
542
543	/* parse command parameters */
544	switch (argc) {
545	case 4:
546		/* clock_offset */
547		if (sscanf(argv[3], "%x", &n0) != 1)
548			return (USAGE);
549
550		cp.clock_offset = (n0 & 0xffff);
551		cp.clock_offset = htole16(cp.clock_offset);
552
553	case 3:
554		/* page_scan_mode */
555		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x03)
556			return (USAGE);
557
558		cp.page_scan_mode = (n0 & 0xff);
559
560	case 2:
561		/* page_scan_rep_mode */
562		if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x02)
563			return (USAGE);
564
565		cp.page_scan_rep_mode = (n0 & 0xff);
566
567	case 1:
568		/* BD_ADDR */
569		if (sscanf(argv[0], "%x:%x:%x:%x:%x:%x",
570				&n5, &n4, &n3, &n2, &n1, &n0) != 6)
571			return (USAGE);
572
573		cp.bdaddr.b[0] = (n0 & 0xff);
574		cp.bdaddr.b[1] = (n1 & 0xff);
575		cp.bdaddr.b[2] = (n2 & 0xff);
576		cp.bdaddr.b[3] = (n3 & 0xff);
577		cp.bdaddr.b[4] = (n4 & 0xff);
578		cp.bdaddr.b[5] = (n5 & 0xff);
579		break;
580
581	default:
582		return (USAGE);
583	}
584
585	/* send request and expect status response */
586	n0 = sizeof(b);
587	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
588			NG_HCI_OCF_REMOTE_NAME_REQ),
589			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
590		return (ERROR);
591
592	if (*b != 0x00)
593		return (FAILED);
594
595	/* wait for event */
596again:
597	n0 = sizeof(b);
598	if (hci_recv(s, b, &n0) == ERROR)
599		return (ERROR);
600	if (n0 < sizeof(*e)) {
601		errno = EIO;
602		return (ERROR);
603	}
604
605	if (e->event == NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL) {
606		ng_hci_remote_name_req_compl_ep	*ep =
607				(ng_hci_remote_name_req_compl_ep *)(e + 1);
608
609		if (ep->status != 0x00) {
610			fprintf(stdout, "Status: %s [%#02x]\n",
611				hci_status2str(ep->status), ep->status);
612			return (FAILED);
613		}
614
615		fprintf(stdout, "BD_ADDR: %02x:%02x:%02x:%02x:%02x:%02x\n",
616			ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
617			ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
618		fprintf(stdout, "Name: %s\n", ep->name);
619	} else
620		goto again;
621
622	return (OK);
623} /* hci_remote_name_request */
624
625/* Send Read_Remote_Supported_Features command to the unit */
626static int
627hci_read_remote_supported_features(int s, int argc, char **argv)
628{
629	int				 n;
630	char				 b[512];
631	ng_hci_read_remote_features_cp	 cp;
632	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b;
633	char				 buffer[1024];
634
635	/* parse command parameters */
636	switch (argc) {
637	case 1:
638		/* connecton handle */
639		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
640			return (USAGE);
641
642		cp.con_handle = (n & 0x0fff);
643		cp.con_handle = htole16(cp.con_handle);
644		break;
645
646	default:
647		return (USAGE);
648	}
649
650	/* send request and expect status response */
651	n = sizeof(b);
652	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
653			NG_HCI_OCF_READ_REMOTE_FEATURES),
654			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
655		return (ERROR);
656
657	if (*b != 0x00)
658		return (FAILED);
659
660	/* wait for event */
661again:
662	n = sizeof(b);
663	if (hci_recv(s, b, &n) == ERROR)
664		return (ERROR);
665
666	if (n < sizeof(*e)) {
667		errno = EIO;
668		return (ERROR);
669	}
670
671	if (e->event == NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL) {
672		ng_hci_read_remote_features_compl_ep	*ep =
673				(ng_hci_read_remote_features_compl_ep *)(e + 1);
674
675		if (ep->status != 0x00) {
676			fprintf(stdout, "Status: %s [%#02x]\n",
677				hci_status2str(ep->status), ep->status);
678			return (FAILED);
679		}
680
681		fprintf(stdout, "Connection handle: %d\n",
682			le16toh(ep->con_handle));
683		fprintf(stdout, "Features: ");
684		for (n = 0; n < sizeof(ep->features); n++)
685			fprintf(stdout, "%#02x ", ep->features[n]);
686		fprintf(stdout, "\n%s\n", hci_features2str(ep->features,
687			buffer, sizeof(buffer)));
688	} else
689		goto again;
690
691	return (OK);
692} /* hci_read_remote_supported_features */
693
694/* Send Read_Remote_Version_Information command to the unit */
695static int
696hci_read_remote_version_information(int s, int argc, char **argv)
697{
698	int				 n;
699	char				 b[512];
700	ng_hci_read_remote_ver_info_cp	 cp;
701	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b;
702
703	/* parse command parameters */
704	switch (argc) {
705	case 1:
706		/* connecton handle */
707		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
708			return (USAGE);
709
710		cp.con_handle = (n & 0x0fff);
711		cp.con_handle = htole16(cp.con_handle);
712		break;
713
714	default:
715		return (USAGE);
716	}
717
718	/* send request and expect status response */
719	n = sizeof(b);
720	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
721			NG_HCI_OCF_READ_REMOTE_VER_INFO),
722			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
723		return (ERROR);
724
725	if (*b != 0x00)
726		return (FAILED);
727
728	/* wait for event */
729again:
730	n = sizeof(b);
731	if (hci_recv(s, b, &n) == ERROR)
732		return (ERROR);
733
734	if (n < sizeof(*e)) {
735		errno = EIO;
736		return (ERROR);
737	}
738
739	if (e->event == NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL) {
740		ng_hci_read_remote_ver_info_compl_ep	*ep =
741				(ng_hci_read_remote_ver_info_compl_ep *)(e + 1);
742
743		if (ep->status != 0x00) {
744			fprintf(stdout, "Status: %s [%#02x]\n",
745				hci_status2str(ep->status), ep->status);
746			return (FAILED);
747		}
748
749		ep->manufacturer = le16toh(ep->manufacturer);
750
751		fprintf(stdout, "Connection handle: %d\n",
752			le16toh(ep->con_handle));
753		fprintf(stdout, "LMP version: %#02x\n", ep->lmp_version);
754		fprintf(stdout, "LMP sub-version: %#04x\n",
755			le16toh(ep->lmp_subversion));
756		fprintf(stdout, "Manufacturer: %s [%#04x]\n",
757			hci_manufacturer2str(ep->manufacturer),
758			ep->manufacturer);
759	} else
760		goto again;
761
762	return (OK);
763} /* hci_read_remote_version_information */
764
765/* Send Read_Clock_Offset command to the unit */
766static int
767hci_read_clock_offset(int s, int argc, char **argv)
768{
769	int				 n;
770	char				 b[512];
771	ng_hci_read_clock_offset_cp	 cp;
772	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b;
773
774	/* parse command parameters */
775	switch (argc) {
776	case 1:
777		/* connecton handle */
778		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
779			return (USAGE);
780
781		cp.con_handle = (n & 0x0fff);
782		cp.con_handle = htole16(cp.con_handle);
783		break;
784
785	default:
786		return (USAGE);
787	}
788
789	/* send request and expect status response */
790	n = sizeof(b);
791	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
792			NG_HCI_OCF_READ_CLOCK_OFFSET),
793			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
794		return (ERROR);
795
796	if (*b != 0x00)
797		return (FAILED);
798
799	/* wait for event */
800again:
801	n = sizeof(b);
802	if (hci_recv(s, b, &n) == ERROR)
803		return (ERROR);
804
805	if (n < sizeof(*e)) {
806		errno = EIO;
807		return (ERROR);
808	}
809
810	if (e->event == NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL) {
811		ng_hci_read_clock_offset_compl_ep	*ep =
812				(ng_hci_read_clock_offset_compl_ep *)(e + 1);
813
814		if (ep->status != 0x00) {
815			fprintf(stdout, "Status: %s [%#02x]\n",
816				hci_status2str(ep->status), ep->status);
817			return (FAILED);
818		}
819
820		fprintf(stdout, "Connection handle: %d\n",
821			le16toh(ep->con_handle));
822		fprintf(stdout, "Clock offset: %#04x\n",
823			le16toh(ep->clock_offset));
824	} else
825		goto again;
826
827	return (OK);
828} /* hci_read_clock_offset */
829
830struct hci_command	link_control_commands[] = {
831{
832"inquiry <LAP> <inquiry_length> <num_reponses>",
833"\nThis command will cause the Bluetooth unit to enter Inquiry Mode.\n" \
834"Inquiry Mode is used to discover other nearby Bluetooth units. The LAP\n" \
835"input parameter contains the LAP from which the inquiry access code shall\n" \
836"be derived when the inquiry procedure is made. The Inquiry_Length parameter\n"\
837"specifies the total duration of the Inquiry Mode and, when this time\n" \
838"expires, Inquiry will be halted. The Num_Responses parameter specifies the\n" \
839"number of responses that can be received before the Inquiry is halted.\n\n" \
840"\t<LAP>            - xx:xx:xx; 9e:8b:33 (GIAC), 93:8b:00 (LDIAC)\n" \
841"\t<inquiry_length> - dd; total length == dd * 1.28 sec\n" \
842"\t<num_responses>  - dd",
843&hci_inquiry
844},
845{
846"create_connection <BD_ADDR> <pkt> <rep_mode> <ps_mode> <clck_off> <role_sw>",
847"" \
848"\t<BD_ADDR> - remote unit address\n\n" \
849"\t<pkt>     - xxxx; packet type\n" \
850"" \
851"\t\tACL packets\n" \
852"\t\t-----------\n" \
853"\t\t0x0008 DM1\n" \
854"\t\t0x0010 DH1\n" \
855"\t\t0x0400 DM3\n" \
856"\t\t0x0800 DH3\n" \
857"\t\t0x4000 DM5\n" \
858"\t\t0x8000 DH5\n\n" \
859"" \
860"\trep_mode  - d; page scan repetition mode\n" \
861"" \
862"\t\tPage scan repetition modes\n" \
863"\t\t--------------------------\n" \
864"\t\t0 Page scan repetition mode 0\n" \
865"\t\t1 Page scan repetition mode 1\n" \
866"\t\t2 Page scan repetition mode 2\n" \
867"\n" \
868"\tps_mode   - d; Page scan mode\n" \
869"" \
870"\t\tPage scan modes\n" \
871"\t\t---------------\n" \
872"\t\t0 Mandatory page scan mode\n" \
873"\t\t1 Optional page scan mode1\n" \
874"\t\t2 Optional page scan mode2\n" \
875"\t\t3 Optional page scan mode3\n" \
876"\n" \
877"\tclck_off  - dddd; clock offset. Use 0 if unknown\n\n" \
878"\trole_sw   - d; allow (1) or deny role switch\n",
879&hci_create_connection
880},
881{
882"disconnect <connection_handle> <reason>",
883"\nThe Disconnection command is used to terminate an existing connection.\n" \
884"The connection handle command parameter indicates which connection is to\n" \
885"be disconnected. The Reason command parameter indicates the reason for\n" \
886"ending the connection.\n\n" \
887"\t<connection_handle> - dddd; connection handle\n" \
888"\t<reason>            - dd; reason; usually 19 (0x13) - user ended;\n" \
889"\t                      also 0x05, 0x13-0x15, 0x1A, 0x29",
890&hci_disconnect
891},
892{
893"add_sco_connection <acl connection handle> <packet type>",
894"This command will cause the link manager to create a SCO connection using\n" \
895"the ACL connection specified by the connection handle command parameter.\n" \
896"The Link Manager will determine how the new connection is established. This\n"\
897"connection is determined by the current state of the device, its piconet,\n" \
898"and the state of the device to be connected. The packet type command parameter\n" \
899"specifies which packet types the Link Manager should use for the connection.\n"\
900"The Link Manager must only use the packet type(s) specified by the packet\n" \
901"type command parameter for sending HCI SCO data packets. Multiple packet\n" \
902"types may be specified for the packet type command parameter by performing\n" \
903"a bitwise OR operation of the different packet types. Note: An SCO connection\n" \
904"can only be created when an ACL connection already exists and when it is\n" \
905"not put in park mode.\n\n" \
906"\t<connection_handle> - dddd; ACL connection handle\n" \
907"\t<packet_type>       - xxxx; packet type\n" \
908"" \
909"\t\tSCO packets\n" \
910"\t\t-----------\n" \
911"\t\t0x0020 HV1\n" \
912"\t\t0x0040 HV2\n" \
913"\t\t0x0080 HV3\n",
914&hci_add_sco_connection
915},
916{
917"change_connection_packet_type <connection_hande> <packet_type>",
918"The Change_Connection_Packet_Type command is used to change which packet\n" \
919"types can be used for a connection that is currently established. This\n" \
920"allows current connections to be dynamically modified to support different\n" \
921"types of user data. The Packet_Type command parameter specifies which\n" \
922"packet types the Link Manager can use for the connection. Multiple packet\n" \
923"types may be specified for the Packet_Type command parameter by bitwise OR\n" \
924"operation of the different packet types.\n\n" \
925"\t<connection_handle> - dddd; connection handle\n" \
926"\t<packet_type>       - xxxx; packet type mask\n" \
927"" \
928"\t\tACL packets\n" \
929"\t\t-----------\n" \
930"\t\t0x0008 DM1\n" \
931"\t\t0x0010 DH1\n" \
932"\t\t0x0400 DM3\n" \
933"\t\t0x0800 DH3\n" \
934"\t\t0x4000 DM5\n" \
935"\t\t0x8000 DH5\n\n" \
936"" \
937"\t\tSCO packets\n" \
938"\t\t-----------\n" \
939"\t\t0x0020 HV1\n" \
940"\t\t0x0040 HV2\n" \
941"\t\t0x0080 HV3\n" \
942"",
943&hci_change_connection_packet_type
944},
945{
946"remote_name_request <bdaddr> <ps_rep_mode> <ps_mode> <clock_offset>",
947"\nThe Remote_Name_Request command is used to obtain the user-friendly\n" \
948"name of another Bluetooth unit.\n\n" \
949"\t<bdaddr>       - xx:xx:xx:xx:xx:xx remote unit BD_ADDR\n" \
950"\t<ps_rep_mode>  - dd; page scan repetition mode [0-2]\n" \
951"\t<ps_mode>      - dd; page scan mode [0-3]\n" \
952"\t<clock_offset> - xxxx; clock offset [0 - 0xffff]",
953&hci_remote_name_request
954},
955{
956"read_remote_supported_features <connection_handle>",
957"\nThis command requests a list of the supported features for the remote\n" \
958"unit identified by the connection handle parameter. The connection handle\n" \
959"must be a connection handle for an ACL connection.\n\n" \
960"\t<connection_handle> - dddd; connection handle",
961&hci_read_remote_supported_features
962},
963{
964"read_remote_version_information <connection_handle>",
965"\nThis command will obtain the values for the version information for the\n" \
966"remote Bluetooth unit identified by the connection handle parameter. The\n" \
967"connection handle must be a connection handle for an ACL connection.\n\n" \
968"\t<conneciton_handle> - dddd; connection handle",
969&hci_read_remote_version_information
970},
971{
972"read_clock_offset <connection_handle>",
973"\nThis command allows the Host to read clock offset to remote unit.\n" \
974"\t<conneciton_handle> - dddd; connection handle",
975&hci_read_clock_offset
976},
977{
978NULL,
979}};
980
981