xref: /freebsd/usr.sbin/bluetooth/hccontrol/status.c (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
11de7b4b8SPedro F. Giffuni /*-
2878ed226SJulian Elischer  * status.c
3878ed226SJulian Elischer  *
4*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro 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  */
32878ed226SJulian Elischer 
33878ed226SJulian Elischer #include <sys/types.h>
34878ed226SJulian Elischer #include <sys/endian.h>
35878ed226SJulian Elischer #include <errno.h>
360986ab12SMaksim Yevmenkin #include <netgraph/bluetooth/include/ng_hci.h>
37878ed226SJulian Elischer #include <stdio.h>
38878ed226SJulian Elischer #include "hccontrol.h"
39878ed226SJulian Elischer 
40878ed226SJulian Elischer /* Send Read_Failed_Contact_Counter command to the unit */
41878ed226SJulian Elischer static int
hci_read_failed_contact_counter(int s,int argc,char ** argv)42878ed226SJulian Elischer hci_read_failed_contact_counter(int s, int argc, char **argv)
43878ed226SJulian Elischer {
44878ed226SJulian Elischer 	ng_hci_read_failed_contact_cntr_cp	cp;
45878ed226SJulian Elischer 	ng_hci_read_failed_contact_cntr_rp	rp;
46878ed226SJulian Elischer 	int					n;
47878ed226SJulian Elischer 
48878ed226SJulian Elischer 	switch (argc) {
49878ed226SJulian Elischer 	case 1:
50878ed226SJulian Elischer 		/* connection handle */
51878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
52878ed226SJulian Elischer 			return (USAGE);
53878ed226SJulian Elischer 
54079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
55878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
56878ed226SJulian Elischer 		break;
57878ed226SJulian Elischer 
58878ed226SJulian Elischer 	default:
59878ed226SJulian Elischer 		return (USAGE);
60878ed226SJulian Elischer 	}
61878ed226SJulian Elischer 
62878ed226SJulian Elischer 	/* send command */
63878ed226SJulian Elischer 	n = sizeof(rp);
64878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
65878ed226SJulian Elischer 			NG_HCI_OCF_READ_FAILED_CONTACT_CNTR),
66878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
67878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
68878ed226SJulian Elischer 		return (ERROR);
69878ed226SJulian Elischer 
70878ed226SJulian Elischer 	if (rp.status != 0x00) {
71878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
72878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
73878ed226SJulian Elischer 		return (FAILED);
74878ed226SJulian Elischer 	}
75878ed226SJulian Elischer 
76878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
77878ed226SJulian Elischer 	fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter));
78878ed226SJulian Elischer 
79878ed226SJulian Elischer 	return (OK);
80878ed226SJulian Elischer } /* hci_read_failed_contact_counter */
81878ed226SJulian Elischer 
82878ed226SJulian Elischer /* Send Reset_Failed_Contact_Counter command to the unit */
83878ed226SJulian Elischer static int
hci_reset_failed_contact_counter(int s,int argc,char ** argv)84878ed226SJulian Elischer hci_reset_failed_contact_counter(int s, int argc, char **argv)
85878ed226SJulian Elischer {
86878ed226SJulian Elischer 	ng_hci_reset_failed_contact_cntr_cp	cp;
87878ed226SJulian Elischer 	ng_hci_reset_failed_contact_cntr_rp	rp;
88878ed226SJulian Elischer 	int					n;
89878ed226SJulian Elischer 
90878ed226SJulian Elischer 	switch (argc) {
91878ed226SJulian Elischer 	case 1:
92878ed226SJulian Elischer 		/* connection handle */
93878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
94878ed226SJulian Elischer 			return (USAGE);
95878ed226SJulian Elischer 
96079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
97878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
98878ed226SJulian Elischer 		break;
99878ed226SJulian Elischer 
100878ed226SJulian Elischer 	default:
101878ed226SJulian Elischer 		return (USAGE);
102878ed226SJulian Elischer 	}
103878ed226SJulian Elischer 
104878ed226SJulian Elischer 	/* send command */
105878ed226SJulian Elischer 	n = sizeof(rp);
106878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
107878ed226SJulian Elischer 			NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR),
108878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
109878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
110878ed226SJulian Elischer 		return (ERROR);
111878ed226SJulian Elischer 
112878ed226SJulian Elischer 	if (rp.status != 0x00) {
113878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
114878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
115878ed226SJulian Elischer 		return (FAILED);
116878ed226SJulian Elischer 	}
117878ed226SJulian Elischer 
118878ed226SJulian Elischer 	return (OK);
119878ed226SJulian Elischer } /* hci_reset_failed_contact_counter */
120878ed226SJulian Elischer 
121878ed226SJulian Elischer /* Sent Get_Link_Quality command to the unit */
122878ed226SJulian Elischer static int
hci_get_link_quality(int s,int argc,char ** argv)123878ed226SJulian Elischer hci_get_link_quality(int s, int argc, char **argv)
124878ed226SJulian Elischer {
125878ed226SJulian Elischer 	ng_hci_get_link_quality_cp	cp;
126878ed226SJulian Elischer 	ng_hci_get_link_quality_rp	rp;
127878ed226SJulian Elischer 	int				n;
128878ed226SJulian Elischer 
129878ed226SJulian Elischer 	switch (argc) {
130878ed226SJulian Elischer 	case 1:
131878ed226SJulian Elischer 		/* connection handle */
132878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
133878ed226SJulian Elischer 			return (USAGE);
134878ed226SJulian Elischer 
135079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
136878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
137878ed226SJulian Elischer 		break;
138878ed226SJulian Elischer 
139878ed226SJulian Elischer 	default:
140878ed226SJulian Elischer 		return (USAGE);
141878ed226SJulian Elischer 	}
142878ed226SJulian Elischer 
143878ed226SJulian Elischer 	/* send command */
144878ed226SJulian Elischer 	n = sizeof(rp);
145878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
146878ed226SJulian Elischer 			NG_HCI_OCF_GET_LINK_QUALITY),
147878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
148878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
149878ed226SJulian Elischer 		return (ERROR);
150878ed226SJulian Elischer 
151878ed226SJulian Elischer 	if (rp.status != 0x00) {
152878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
153878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
154878ed226SJulian Elischer 		return (FAILED);
155878ed226SJulian Elischer 	}
156878ed226SJulian Elischer 
157878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
158878ed226SJulian Elischer 	fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality));
159878ed226SJulian Elischer 
160878ed226SJulian Elischer 	return (OK);
161878ed226SJulian Elischer } /* hci_get_link_quality */
162878ed226SJulian Elischer 
163878ed226SJulian Elischer /* Send Read_RSSI command to the unit */
164878ed226SJulian Elischer static int
hci_read_rssi(int s,int argc,char ** argv)165878ed226SJulian Elischer hci_read_rssi(int s, int argc, char **argv)
166878ed226SJulian Elischer {
167878ed226SJulian Elischer 	ng_hci_read_rssi_cp	cp;
168878ed226SJulian Elischer 	ng_hci_read_rssi_rp	rp;
169878ed226SJulian Elischer 	int			n;
170878ed226SJulian Elischer 
171878ed226SJulian Elischer 	switch (argc) {
172878ed226SJulian Elischer 	case 1:
173878ed226SJulian Elischer 		/* connection handle */
174878ed226SJulian Elischer 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
175878ed226SJulian Elischer 			return (USAGE);
176878ed226SJulian Elischer 
177079a8a3eSMaksim Yevmenkin 		cp.con_handle = (uint16_t) (n & 0x0fff);
178878ed226SJulian Elischer 		cp.con_handle = htole16(cp.con_handle);
179878ed226SJulian Elischer 		break;
180878ed226SJulian Elischer 
181878ed226SJulian Elischer 	default:
182878ed226SJulian Elischer 		return (USAGE);
183878ed226SJulian Elischer 	}
184878ed226SJulian Elischer 
185878ed226SJulian Elischer 	/* send command */
186878ed226SJulian Elischer 	n = sizeof(rp);
187878ed226SJulian Elischer 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
188878ed226SJulian Elischer 			NG_HCI_OCF_READ_RSSI),
189878ed226SJulian Elischer 			(char const *) &cp, sizeof(cp),
190878ed226SJulian Elischer 			(char *) &rp, &n) == ERROR)
191878ed226SJulian Elischer 		return (ERROR);
192878ed226SJulian Elischer 
193878ed226SJulian Elischer 	if (rp.status != 0x00) {
194878ed226SJulian Elischer 		fprintf(stdout, "Status: %s [%#02x]\n",
195878ed226SJulian Elischer 			hci_status2str(rp.status), rp.status);
196878ed226SJulian Elischer 		return (FAILED);
197878ed226SJulian Elischer 	}
198878ed226SJulian Elischer 
199878ed226SJulian Elischer 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
200878ed226SJulian Elischer 	fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi);
201878ed226SJulian Elischer 
202878ed226SJulian Elischer 	return (OK);
203878ed226SJulian Elischer } /* hci_read_rssi */
204878ed226SJulian Elischer 
205878ed226SJulian Elischer struct hci_command	status_commands[] = {
206878ed226SJulian Elischer {
207878ed226SJulian Elischer "read_failed_contact_counter <connection_handle>",
208878ed226SJulian Elischer "\nThis command will read the value for the Failed_Contact_Counter\n" \
209878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \
210878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
211878ed226SJulian Elischer &hci_read_failed_contact_counter
212878ed226SJulian Elischer },
213878ed226SJulian Elischer {
214878ed226SJulian Elischer "reset_failed_contact_counter <connection_handle>",
215878ed226SJulian Elischer "\nThis command will reset the value for the Failed_Contact_Counter\n" \
216878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \
217878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
218878ed226SJulian Elischer &hci_reset_failed_contact_counter
219878ed226SJulian Elischer },
220878ed226SJulian Elischer {
221878ed226SJulian Elischer "get_link_quality <connection_handle>",
222878ed226SJulian Elischer "\nThis command will return the value for the Link_Quality for the\n" \
223878ed226SJulian Elischer "specified ACL connection handle. This command will return a Link_Quality\n" \
224878ed226SJulian Elischer "value from 0-255, which represents the quality of the link between two\n" \
225878ed226SJulian Elischer "Bluetooth devices. The higher the value, the better the link quality is.\n" \
226878ed226SJulian Elischer "Each Bluetooth module vendor will determine how to measure the link quality." \
227878ed226SJulian Elischer "\n\n" \
228878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
229878ed226SJulian Elischer &hci_get_link_quality
230878ed226SJulian Elischer },
231878ed226SJulian Elischer {
232878ed226SJulian Elischer "read_rssi <connection_handle>",
233878ed226SJulian Elischer "\nThis command will read the value for the difference between the\n" \
234878ed226SJulian Elischer "measured Received Signal Strength Indication (RSSI) and the limits of\n" \
2351a63eb31SJulian Elischer "the Golden Receive Power Range for a ACL connection handle to another\n" \
236878ed226SJulian Elischer "Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \
237878ed226SJulian Elischer "indicates how many dB the RSSI is above the upper limit, any negative\n" \
238878ed226SJulian Elischer "value indicates how many dB the RSSI is below the lower limit. The value\n" \
239878ed226SJulian Elischer "zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \
240878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n",
241878ed226SJulian Elischer &hci_read_rssi
242878ed226SJulian Elischer },
243878ed226SJulian Elischer {
244878ed226SJulian Elischer NULL,
245878ed226SJulian Elischer }};
246878ed226SJulian Elischer 
247