1 /*-
2  * status.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: status.c,v 1.2 2003/05/21 22:40:30 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/types.h>
35 #include <sys/endian.h>
36 #include <errno.h>
37 #include <netgraph/bluetooth/include/ng_hci.h>
38 #include <stdio.h>
39 #include "hccontrol.h"
40 
41 /* Send Read_Failed_Contact_Counter command to the unit */
42 static int
43 hci_read_failed_contact_counter(int s, int argc, char **argv)
44 {
45 	ng_hci_read_failed_contact_cntr_cp	cp;
46 	ng_hci_read_failed_contact_cntr_rp	rp;
47 	int					n;
48 
49 	switch (argc) {
50 	case 1:
51 		/* connection handle */
52 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
53 			return (USAGE);
54 
55 		cp.con_handle = (uint16_t) (n & 0x0fff);
56 		cp.con_handle = htole16(cp.con_handle);
57 		break;
58 
59 	default:
60 		return (USAGE);
61 	}
62 
63 	/* send command */
64 	n = sizeof(rp);
65 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
66 			NG_HCI_OCF_READ_FAILED_CONTACT_CNTR),
67 			(char const *) &cp, sizeof(cp),
68 			(char *) &rp, &n) == ERROR)
69 		return (ERROR);
70 
71 	if (rp.status != 0x00) {
72 		fprintf(stdout, "Status: %s [%#02x]\n",
73 			hci_status2str(rp.status), rp.status);
74 		return (FAILED);
75 	}
76 
77 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
78 	fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter));
79 
80 	return (OK);
81 } /* hci_read_failed_contact_counter */
82 
83 /* Send Reset_Failed_Contact_Counter command to the unit */
84 static int
85 hci_reset_failed_contact_counter(int s, int argc, char **argv)
86 {
87 	ng_hci_reset_failed_contact_cntr_cp	cp;
88 	ng_hci_reset_failed_contact_cntr_rp	rp;
89 	int					n;
90 
91 	switch (argc) {
92 	case 1:
93 		/* connection handle */
94 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
95 			return (USAGE);
96 
97 		cp.con_handle = (uint16_t) (n & 0x0fff);
98 		cp.con_handle = htole16(cp.con_handle);
99 		break;
100 
101 	default:
102 		return (USAGE);
103 	}
104 
105 	/* send command */
106 	n = sizeof(rp);
107 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
108 			NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR),
109 			(char const *) &cp, sizeof(cp),
110 			(char *) &rp, &n) == ERROR)
111 		return (ERROR);
112 
113 	if (rp.status != 0x00) {
114 		fprintf(stdout, "Status: %s [%#02x]\n",
115 			hci_status2str(rp.status), rp.status);
116 		return (FAILED);
117 	}
118 
119 	return (OK);
120 } /* hci_reset_failed_contact_counter */
121 
122 /* Sent Get_Link_Quality command to the unit */
123 static int
124 hci_get_link_quality(int s, int argc, char **argv)
125 {
126 	ng_hci_get_link_quality_cp	cp;
127 	ng_hci_get_link_quality_rp	rp;
128 	int				n;
129 
130 	switch (argc) {
131 	case 1:
132 		/* connection handle */
133 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
134 			return (USAGE);
135 
136 		cp.con_handle = (uint16_t) (n & 0x0fff);
137 		cp.con_handle = htole16(cp.con_handle);
138 		break;
139 
140 	default:
141 		return (USAGE);
142 	}
143 
144 	/* send command */
145 	n = sizeof(rp);
146 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
147 			NG_HCI_OCF_GET_LINK_QUALITY),
148 			(char const *) &cp, sizeof(cp),
149 			(char *) &rp, &n) == ERROR)
150 		return (ERROR);
151 
152 	if (rp.status != 0x00) {
153 		fprintf(stdout, "Status: %s [%#02x]\n",
154 			hci_status2str(rp.status), rp.status);
155 		return (FAILED);
156 	}
157 
158 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
159 	fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality));
160 
161 	return (OK);
162 } /* hci_get_link_quality */
163 
164 /* Send Read_RSSI command to the unit */
165 static int
166 hci_read_rssi(int s, int argc, char **argv)
167 {
168 	ng_hci_read_rssi_cp	cp;
169 	ng_hci_read_rssi_rp	rp;
170 	int			n;
171 
172 	switch (argc) {
173 	case 1:
174 		/* connection handle */
175 		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
176 			return (USAGE);
177 
178 		cp.con_handle = (uint16_t) (n & 0x0fff);
179 		cp.con_handle = htole16(cp.con_handle);
180 		break;
181 
182 	default:
183 		return (USAGE);
184 	}
185 
186 	/* send command */
187 	n = sizeof(rp);
188 	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
189 			NG_HCI_OCF_READ_RSSI),
190 			(char const *) &cp, sizeof(cp),
191 			(char *) &rp, &n) == ERROR)
192 		return (ERROR);
193 
194 	if (rp.status != 0x00) {
195 		fprintf(stdout, "Status: %s [%#02x]\n",
196 			hci_status2str(rp.status), rp.status);
197 		return (FAILED);
198 	}
199 
200 	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
201 	fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi);
202 
203 	return (OK);
204 } /* hci_read_rssi */
205 
206 struct hci_command	status_commands[] = {
207 {
208 "read_failed_contact_counter <connection_handle>",
209 "\nThis command will read the value for the Failed_Contact_Counter\n" \
210 "parameter for a particular ACL connection to another device.\n\n" \
211 "\t<connection_handle> - dddd; ACL connection handle\n",
212 &hci_read_failed_contact_counter
213 },
214 {
215 "reset_failed_contact_counter <connection_handle>",
216 "\nThis command will reset the value for the Failed_Contact_Counter\n" \
217 "parameter for a particular ACL connection to another device.\n\n" \
218 "\t<connection_handle> - dddd; ACL connection handle\n",
219 &hci_reset_failed_contact_counter
220 },
221 {
222 "get_link_quality <connection_handle>",
223 "\nThis command will return the value for the Link_Quality for the\n" \
224 "specified ACL connection handle. This command will return a Link_Quality\n" \
225 "value from 0-255, which represents the quality of the link between two\n" \
226 "Bluetooth devices. The higher the value, the better the link quality is.\n" \
227 "Each Bluetooth module vendor will determine how to measure the link quality." \
228 "\n\n" \
229 "\t<connection_handle> - dddd; ACL connection handle\n",
230 &hci_get_link_quality
231 },
232 {
233 "read_rssi <connection_handle>",
234 "\nThis command will read the value for the difference between the\n" \
235 "measured Received Signal Strength Indication (RSSI) and the limits of\n" \
236 "the Golden Receive Power Range for a ACL connection handle to another\n" \
237 "Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \
238 "indicates how many dB the RSSI is above the upper limit, any negative\n" \
239 "value indicates how many dB the RSSI is below the lower limit. The value\n" \
240 "zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \
241 "\t<connection_handle> - dddd; ACL connection handle\n",
242 &hci_read_rssi
243 },
244 {
245 NULL,
246 }};
247 
248