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