xref: /freebsd/usr.sbin/bluetooth/hccontrol/status.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1*1de7b4b8SPedro F. Giffuni /*-
2878ed226SJulian Elischer  * status.c
3878ed226SJulian Elischer  *
4*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5*1de7b4b8SPedro F. Giffuni  *
6878ed226SJulian Elischer  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7878ed226SJulian Elischer  * All rights reserved.
8878ed226SJulian Elischer  *
9878ed226SJulian Elischer  * Redistribution and use in source and binary forms, with or without
10878ed226SJulian Elischer  * modification, are permitted provided that the following conditions
11878ed226SJulian Elischer  * are met:
12878ed226SJulian Elischer  * 1. Redistributions of source code must retain the above copyright
13878ed226SJulian Elischer  *    notice, this list of conditions and the following disclaimer.
14878ed226SJulian Elischer  * 2. Redistributions in binary form must reproduce the above copyright
15878ed226SJulian Elischer  *    notice, this list of conditions and the following disclaimer in the
16878ed226SJulian Elischer  *    documentation and/or other materials provided with the distribution.
17878ed226SJulian Elischer  *
18878ed226SJulian Elischer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19878ed226SJulian Elischer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20878ed226SJulian Elischer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21878ed226SJulian Elischer  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22878ed226SJulian Elischer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23878ed226SJulian Elischer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24878ed226SJulian Elischer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25878ed226SJulian Elischer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26878ed226SJulian Elischer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27878ed226SJulian Elischer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28878ed226SJulian Elischer  * SUCH DAMAGE.
29878ed226SJulian Elischer  *
300986ab12SMaksim Yevmenkin  * $Id: status.c,v 1.2 2003/05/21 22:40:30 max Exp $
31878ed226SJulian Elischer  * $FreeBSD$
32878ed226SJulian Elischer  */
33878ed226SJulian Elischer 
34878ed226SJulian Elischer #include <sys/types.h>
35878ed226SJulian Elischer #include <sys/endian.h>
36878ed226SJulian Elischer #include <errno.h>
370986ab12SMaksim Yevmenkin #include <netgraph/bluetooth/include/ng_hci.h>
38878ed226SJulian Elischer #include <stdio.h>
39878ed226SJulian Elischer #include "hccontrol.h"
40878ed226SJulian Elischer 
41878ed226SJulian Elischer /* Send Read_Failed_Contact_Counter command to the unit */
42878ed226SJulian Elischer static int
43878ed226SJulian Elischer hci_read_failed_contact_counter(int s, int argc, char **argv)
44878ed226SJulian Elischer {
45878ed226SJulian Elischer 	ng_hci_read_failed_contact_cntr_cp	cp;
46878ed226SJulian Elischer 	ng_hci_read_failed_contact_cntr_rp	rp;
47878ed226SJulian Elischer 	int					n;
48878ed226SJulian Elischer 
49878ed226SJulian Elischer 	switch (argc) {
50878ed226SJulian Elischer 	case 1:
51878ed226SJulian Elischer 		/* connection handle */
52878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
53878ed226SJulian Elischer 			return (USAGE);
54878ed226SJulian Elischer 
55079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
56878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
57878ed226SJulian Elischer 		break;
58878ed226SJulian Elischer 
59878ed226SJulian Elischer 	default:
60878ed226SJulian Elischer 		return (USAGE);
61878ed226SJulian Elischer 	}
62878ed226SJulian Elischer 
63878ed226SJulian Elischer 	/* send command */
64878ed226SJulian Elischer 	n = sizeof(rp);
65878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
66878ed226SJulian Elischer 			NG_HCI_OCF_READ_FAILED_CONTACT_CNTR),
67878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
68878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
69878ed226SJulian Elischer 		return (ERROR);
70878ed226SJulian Elischer 
71878ed226SJulian Elischer 	if (rp.status != 0x00) {
72878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
73878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
74878ed226SJulian Elischer 		return (FAILED);
75878ed226SJulian Elischer 	}
76878ed226SJulian Elischer 
77878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
78878ed226SJulian Elischer 	fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter));
79878ed226SJulian Elischer 
80878ed226SJulian Elischer 	return (OK);
81878ed226SJulian Elischer } /* hci_read_failed_contact_counter */
82878ed226SJulian Elischer 
83878ed226SJulian Elischer /* Send Reset_Failed_Contact_Counter command to the unit */
84878ed226SJulian Elischer static int
85878ed226SJulian Elischer hci_reset_failed_contact_counter(int s, int argc, char **argv)
86878ed226SJulian Elischer {
87878ed226SJulian Elischer 	ng_hci_reset_failed_contact_cntr_cp	cp;
88878ed226SJulian Elischer 	ng_hci_reset_failed_contact_cntr_rp	rp;
89878ed226SJulian Elischer 	int					n;
90878ed226SJulian Elischer 
91878ed226SJulian Elischer 	switch (argc) {
92878ed226SJulian Elischer 	case 1:
93878ed226SJulian Elischer 		/* connection handle */
94878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
95878ed226SJulian Elischer 			return (USAGE);
96878ed226SJulian Elischer 
97079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
98878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
99878ed226SJulian Elischer 		break;
100878ed226SJulian Elischer 
101878ed226SJulian Elischer 	default:
102878ed226SJulian Elischer 		return (USAGE);
103878ed226SJulian Elischer 	}
104878ed226SJulian Elischer 
105878ed226SJulian Elischer 	/* send command */
106878ed226SJulian Elischer 	n = sizeof(rp);
107878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
108878ed226SJulian Elischer 			NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR),
109878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
110878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
111878ed226SJulian Elischer 		return (ERROR);
112878ed226SJulian Elischer 
113878ed226SJulian Elischer 	if (rp.status != 0x00) {
114878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
115878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
116878ed226SJulian Elischer 		return (FAILED);
117878ed226SJulian Elischer 	}
118878ed226SJulian Elischer 
119878ed226SJulian Elischer 	return (OK);
120878ed226SJulian Elischer } /* hci_reset_failed_contact_counter */
121878ed226SJulian Elischer 
122878ed226SJulian Elischer /* Sent Get_Link_Quality command to the unit */
123878ed226SJulian Elischer static int
124878ed226SJulian Elischer hci_get_link_quality(int s, int argc, char **argv)
125878ed226SJulian Elischer {
126878ed226SJulian Elischer 	ng_hci_get_link_quality_cp	cp;
127878ed226SJulian Elischer 	ng_hci_get_link_quality_rp	rp;
128878ed226SJulian Elischer 	int				n;
129878ed226SJulian Elischer 
130878ed226SJulian Elischer 	switch (argc) {
131878ed226SJulian Elischer 	case 1:
132878ed226SJulian Elischer 		/* connection handle */
133878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
134878ed226SJulian Elischer 			return (USAGE);
135878ed226SJulian Elischer 
136079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
137878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
138878ed226SJulian Elischer 		break;
139878ed226SJulian Elischer 
140878ed226SJulian Elischer 	default:
141878ed226SJulian Elischer 		return (USAGE);
142878ed226SJulian Elischer 	}
143878ed226SJulian Elischer 
144878ed226SJulian Elischer 	/* send command */
145878ed226SJulian Elischer 	n = sizeof(rp);
146878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
147878ed226SJulian Elischer 			NG_HCI_OCF_GET_LINK_QUALITY),
148878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
149878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
150878ed226SJulian Elischer 		return (ERROR);
151878ed226SJulian Elischer 
152878ed226SJulian Elischer 	if (rp.status != 0x00) {
153878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
154878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
155878ed226SJulian Elischer 		return (FAILED);
156878ed226SJulian Elischer 	}
157878ed226SJulian Elischer 
158878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
159878ed226SJulian Elischer 	fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality));
160878ed226SJulian Elischer 
161878ed226SJulian Elischer 	return (OK);
162878ed226SJulian Elischer } /* hci_get_link_quality */
163878ed226SJulian Elischer 
164878ed226SJulian Elischer /* Send Read_RSSI command to the unit */
165878ed226SJulian Elischer static int
166878ed226SJulian Elischer hci_read_rssi(int s, int argc, char **argv)
167878ed226SJulian Elischer {
168878ed226SJulian Elischer 	ng_hci_read_rssi_cp	cp;
169878ed226SJulian Elischer 	ng_hci_read_rssi_rp	rp;
170878ed226SJulian Elischer 	int			n;
171878ed226SJulian Elischer 
172878ed226SJulian Elischer 	switch (argc) {
173878ed226SJulian Elischer 	case 1:
174878ed226SJulian Elischer 		/* connection handle */
175878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
176878ed226SJulian Elischer 			return (USAGE);
177878ed226SJulian Elischer 
178079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
179878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
180878ed226SJulian Elischer 		break;
181878ed226SJulian Elischer 
182878ed226SJulian Elischer 	default:
183878ed226SJulian Elischer 		return (USAGE);
184878ed226SJulian Elischer 	}
185878ed226SJulian Elischer 
186878ed226SJulian Elischer 	/* send command */
187878ed226SJulian Elischer 	n = sizeof(rp);
188878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
189878ed226SJulian Elischer 			NG_HCI_OCF_READ_RSSI),
190878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
191878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
192878ed226SJulian Elischer 		return (ERROR);
193878ed226SJulian Elischer 
194878ed226SJulian Elischer 	if (rp.status != 0x00) {
195878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
196878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
197878ed226SJulian Elischer 		return (FAILED);
198878ed226SJulian Elischer 	}
199878ed226SJulian Elischer 
200878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
201878ed226SJulian Elischer 	fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi);
202878ed226SJulian Elischer 
203878ed226SJulian Elischer 	return (OK);
204878ed226SJulian Elischer } /* hci_read_rssi */
205878ed226SJulian Elischer 
206878ed226SJulian Elischer struct hci_command	status_commands[] = {
207878ed226SJulian Elischer {
208878ed226SJulian Elischer "read_failed_contact_counter <connection_handle>",
209878ed226SJulian Elischer "\nThis command will read the value for the Failed_Contact_Counter\n" \
210878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \
211878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
212878ed226SJulian Elischer &hci_read_failed_contact_counter
213878ed226SJulian Elischer },
214878ed226SJulian Elischer {
215878ed226SJulian Elischer "reset_failed_contact_counter <connection_handle>",
216878ed226SJulian Elischer "\nThis command will reset the value for the Failed_Contact_Counter\n" \
217878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \
218878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
219878ed226SJulian Elischer &hci_reset_failed_contact_counter
220878ed226SJulian Elischer },
221878ed226SJulian Elischer {
222878ed226SJulian Elischer "get_link_quality <connection_handle>",
223878ed226SJulian Elischer "\nThis command will return the value for the Link_Quality for the\n" \
224878ed226SJulian Elischer "specified ACL connection handle. This command will return a Link_Quality\n" \
225878ed226SJulian Elischer "value from 0-255, which represents the quality of the link between two\n" \
226878ed226SJulian Elischer "Bluetooth devices. The higher the value, the better the link quality is.\n" \
227878ed226SJulian Elischer "Each Bluetooth module vendor will determine how to measure the link quality." \
228878ed226SJulian Elischer "\n\n" \
229878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
230878ed226SJulian Elischer &hci_get_link_quality
231878ed226SJulian Elischer },
232878ed226SJulian Elischer {
233878ed226SJulian Elischer "read_rssi <connection_handle>",
234878ed226SJulian Elischer "\nThis command will read the value for the difference between the\n" \
235878ed226SJulian Elischer "measured Received Signal Strength Indication (RSSI) and the limits of\n" \
2361a63eb31SJulian Elischer "the Golden Receive Power Range for a ACL connection handle to another\n" \
237878ed226SJulian Elischer "Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \
238878ed226SJulian Elischer "indicates how many dB the RSSI is above the upper limit, any negative\n" \
239878ed226SJulian Elischer "value indicates how many dB the RSSI is below the lower limit. The value\n" \
240878ed226SJulian Elischer "zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \
241878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
242878ed226SJulian Elischer &hci_read_rssi
243878ed226SJulian Elischer },
244878ed226SJulian Elischer {
245878ed226SJulian Elischer NULL,
246878ed226SJulian Elischer }};
247878ed226SJulian Elischer 
248