1/*
2 * status.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: status.c,v 1.2 2003/05/21 22:40:30 max Exp $
29 * $FreeBSD$
30 */
31
32#include <sys/types.h>
33#include <sys/endian.h>
34#include <errno.h>
35#include <netgraph/bluetooth/include/ng_hci.h>
36#include <stdio.h>
37#include "hccontrol.h"
38
39/* Send Read_Failed_Contact_Counter command to the unit */
40static int
41hci_read_failed_contact_counter(int s, int argc, char **argv)
42{
43	ng_hci_read_failed_contact_cntr_cp	cp;
44	ng_hci_read_failed_contact_cntr_rp	rp;
45	int					n;
46
47	switch (argc) {
48	case 1:
49		/* connection handle */
50		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
51			return (USAGE);
52
53		cp.con_handle = (uint16_t) (n & 0x0fff);
54		cp.con_handle = htole16(cp.con_handle);
55		break;
56
57	default:
58		return (USAGE);
59	}
60
61	/* send command */
62	n = sizeof(rp);
63	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
64			NG_HCI_OCF_READ_FAILED_CONTACT_CNTR),
65			(char const *) &cp, sizeof(cp),
66			(char *) &rp, &n) == ERROR)
67		return (ERROR);
68
69	if (rp.status != 0x00) {
70		fprintf(stdout, "Status: %s [%#02x]\n",
71			hci_status2str(rp.status), rp.status);
72		return (FAILED);
73	}
74
75	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
76	fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter));
77
78	return (OK);
79} /* hci_read_failed_contact_counter */
80
81/* Send Reset_Failed_Contact_Counter command to the unit */
82static int
83hci_reset_failed_contact_counter(int s, int argc, char **argv)
84{
85	ng_hci_reset_failed_contact_cntr_cp	cp;
86	ng_hci_reset_failed_contact_cntr_rp	rp;
87	int					n;
88
89	switch (argc) {
90	case 1:
91		/* connection handle */
92		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
93			return (USAGE);
94
95		cp.con_handle = (uint16_t) (n & 0x0fff);
96		cp.con_handle = htole16(cp.con_handle);
97		break;
98
99	default:
100		return (USAGE);
101	}
102
103	/* send command */
104	n = sizeof(rp);
105	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
106			NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR),
107			(char const *) &cp, sizeof(cp),
108			(char *) &rp, &n) == ERROR)
109		return (ERROR);
110
111	if (rp.status != 0x00) {
112		fprintf(stdout, "Status: %s [%#02x]\n",
113			hci_status2str(rp.status), rp.status);
114		return (FAILED);
115	}
116
117	return (OK);
118} /* hci_reset_failed_contact_counter */
119
120/* Sent Get_Link_Quality command to the unit */
121static int
122hci_get_link_quality(int s, int argc, char **argv)
123{
124	ng_hci_get_link_quality_cp	cp;
125	ng_hci_get_link_quality_rp	rp;
126	int				n;
127
128	switch (argc) {
129	case 1:
130		/* connection handle */
131		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
132			return (USAGE);
133
134		cp.con_handle = (uint16_t) (n & 0x0fff);
135		cp.con_handle = htole16(cp.con_handle);
136		break;
137
138	default:
139		return (USAGE);
140	}
141
142	/* send command */
143	n = sizeof(rp);
144	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
145			NG_HCI_OCF_GET_LINK_QUALITY),
146			(char const *) &cp, sizeof(cp),
147			(char *) &rp, &n) == ERROR)
148		return (ERROR);
149
150	if (rp.status != 0x00) {
151		fprintf(stdout, "Status: %s [%#02x]\n",
152			hci_status2str(rp.status), rp.status);
153		return (FAILED);
154	}
155
156	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
157	fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality));
158
159	return (OK);
160} /* hci_get_link_quality */
161
162/* Send Read_RSSI command to the unit */
163static int
164hci_read_rssi(int s, int argc, char **argv)
165{
166	ng_hci_read_rssi_cp	cp;
167	ng_hci_read_rssi_rp	rp;
168	int			n;
169
170	switch (argc) {
171	case 1:
172		/* connection handle */
173		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
174			return (USAGE);
175
176		cp.con_handle = (uint16_t) (n & 0x0fff);
177		cp.con_handle = htole16(cp.con_handle);
178		break;
179
180	default:
181		return (USAGE);
182	}
183
184	/* send command */
185	n = sizeof(rp);
186	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,
187			NG_HCI_OCF_READ_RSSI),
188			(char const *) &cp, sizeof(cp),
189			(char *) &rp, &n) == ERROR)
190		return (ERROR);
191
192	if (rp.status != 0x00) {
193		fprintf(stdout, "Status: %s [%#02x]\n",
194			hci_status2str(rp.status), rp.status);
195		return (FAILED);
196	}
197
198	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
199	fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi);
200
201	return (OK);
202} /* hci_read_rssi */
203
204struct hci_command	status_commands[] = {
205{
206"read_failed_contact_counter <connection_handle>",
207"\nThis command will read the value for the Failed_Contact_Counter\n" \
208"parameter for a particular ACL connection to another device.\n\n" \
209"\t<connection_handle> - dddd; ACL connection handle\n",
210&hci_read_failed_contact_counter
211},
212{
213"reset_failed_contact_counter <connection_handle>",
214"\nThis command will reset the value for the Failed_Contact_Counter\n" \
215"parameter for a particular ACL connection to another device.\n\n" \
216"\t<connection_handle> - dddd; ACL connection handle\n",
217&hci_reset_failed_contact_counter
218},
219{
220"get_link_quality <connection_handle>",
221"\nThis command will return the value for the Link_Quality for the\n" \
222"specified ACL connection handle. This command will return a Link_Quality\n" \
223"value from 0-255, which represents the quality of the link between two\n" \
224"Bluetooth devices. The higher the value, the better the link quality is.\n" \
225"Each Bluetooth module vendor will determine how to measure the link quality." \
226"\n\n" \
227"\t<connection_handle> - dddd; ACL connection handle\n",
228&hci_get_link_quality
229},
230{
231"read_rssi <connection_handle>",
232"\nThis command will read the value for the difference between the\n" \
233"measured Received Signal Strength Indication (RSSI) and the limits of\n" \
234"the Golden Receive Power Range for a ACL connection handle to another\n" \
235"Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \
236"indicates how many dB the RSSI is above the upper limit, any negative\n" \
237"value indicates how many dB the RSSI is below the lower limit. The value\n" \
238"zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \
239"\t<connection_handle> - dddd; ACL connection handle\n",
240&hci_read_rssi
241},
242{
243NULL,
244}};
245
246