xref: /freebsd/usr.sbin/bluetooth/bthidcontrol/hid.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * hid.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2004 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: hid.c,v 1.3 2004/02/17 22:14:57 max Exp $
31  */
32 
33 #include <sys/queue.h>
34 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
36 #include <dev/usb/usb.h>
37 #include <dev/usb/usbhid.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <usbhid.h>
41 #include "bthid_config.h"
42 #include "bthidcontrol.h"
43 
44 extern uint32_t verbose;
45 
46 static void hid_dump_descriptor	(report_desc_t r);
47 static void hid_dump_item	(char const *label, struct hid_item *h);
48 
49 static int
50 hid_dump(bdaddr_t *bdaddr, int argc, char **argv)
51 {
52 	struct hid_device	*hd = NULL;
53 	int			 e = FAILED;
54 
55 	if (read_config_file() == 0) {
56 		if ((hd = get_hid_device(bdaddr)) != NULL) {
57 			hid_dump_descriptor(hd->desc);
58 			e = OK;
59 		}
60 
61 		clean_config();
62 	}
63 
64 	return (e);
65 }
66 
67 static int
68 hid_forget(bdaddr_t *bdaddr, int argc, char **argv)
69 {
70 	struct hid_device	*hd = NULL;
71 	int			 e = FAILED;
72 
73 	if (read_config_file() == 0) {
74 		if (read_hids_file() == 0) {
75 			if ((hd = get_hid_device(bdaddr)) != NULL) {
76 				hd->new_device = 1;
77 				if (write_hids_file() == 0)
78 					e = OK;
79 			}
80 		}
81 
82 		clean_config();
83 	}
84 
85 	return (e);
86 }
87 
88 static int
89 hid_known(bdaddr_t *bdaddr, int argc, char **argv)
90 {
91 	struct hid_device	*hd = NULL;
92 	struct hostent		*he = NULL;
93 	int			 e = FAILED;
94 
95 	if (read_config_file() == 0) {
96 		if (read_hids_file() == 0) {
97 			e = OK;
98 
99 			for (hd = get_next_hid_device(hd);
100 			     hd != NULL;
101 			     hd = get_next_hid_device(hd)) {
102 				if (hd->new_device)
103 					continue;
104 
105 				he = bt_gethostbyaddr((char *) &hd->bdaddr,
106 						sizeof(hd->bdaddr),
107 						AF_BLUETOOTH);
108 
109 				fprintf(stdout,
110 "%s %s\n",				bt_ntoa(&hd->bdaddr, NULL),
111 					(he != NULL && he->h_name != NULL)?
112 						he->h_name : "");
113 			}
114 		}
115 
116 		clean_config();
117 	}
118 
119 	return (e);
120 }
121 
122 static void
123 hid_dump_descriptor(report_desc_t r)
124 {
125 	struct hid_data	*d = NULL;
126 	struct hid_item	 h;
127 
128 	for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
129 		switch (h.kind) {
130 		case hid_collection:
131 			fprintf(stdout,
132 "Collection page=%s usage=%s\n", hid_usage_page(HID_PAGE(h.usage)),
133 				 hid_usage_in_page(h.usage));
134 			break;
135 
136 		case hid_endcollection:
137 			fprintf(stdout, "End collection\n");
138 			break;
139 
140 		case hid_input:
141 			hid_dump_item("Input  ", &h);
142 			break;
143 
144 		case hid_output:
145 			hid_dump_item("Output ", &h);
146 			break;
147 
148 		case hid_feature:
149 			hid_dump_item("Feature", &h);
150 			break;
151 		}
152 	}
153 
154 	hid_end_parse(d);
155 }
156 
157 static void
158 hid_dump_item(char const *label, struct hid_item *h)
159 {
160 	if ((h->flags & HIO_CONST) && !verbose)
161 		return;
162 
163 	fprintf(stdout,
164 "%s id=%u size=%u count=%u page=%s usage=%s%s%s%s%s%s%s%s%s%s",
165 		label, (uint8_t) h->report_ID, h->report_size, h->report_count,
166 		hid_usage_page(HID_PAGE(h->usage)),
167 		hid_usage_in_page(h->usage),
168 		h->flags & HIO_CONST ? " Const" : "",
169 		h->flags & HIO_VARIABLE ? " Variable" : "",
170 		h->flags & HIO_RELATIVE ? " Relative" : "",
171 		h->flags & HIO_WRAP ? " Wrap" : "",
172 		h->flags & HIO_NONLINEAR ? " NonLinear" : "",
173 		h->flags & HIO_NOPREF ? " NoPref" : "",
174 		h->flags & HIO_NULLSTATE ? " NullState" : "",
175 		h->flags & HIO_VOLATILE ? " Volatile" : "",
176 		h->flags & HIO_BUFBYTES ? " BufBytes" : "");
177 
178 	fprintf(stdout,
179 ", logical range %d..%d",
180 		h->logical_minimum, h->logical_maximum);
181 
182 	if (h->physical_minimum != h->physical_maximum)
183 		fprintf(stdout,
184 ", physical range %d..%d",
185 			h->physical_minimum, h->physical_maximum);
186 
187 	if (h->unit)
188 		fprintf(stdout,
189 ", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
190 
191 	fprintf(stdout, "\n");
192 }
193 
194 struct bthid_command	hid_commands[] = {
195 {
196 "Dump",
197 "Dump HID descriptor for the specified device in human readable form. The\n" \
198 "device must have an entry in the Bluetooth HID daemon configuration file.\n",
199 hid_dump
200 },
201 {
202 "Known",
203 "List all known to the Bluetooth HID daemon devices.\n",
204 hid_known
205 },
206 {
207 "Forget",
208 "Forget (mark as new) specified HID device. This command is useful when it\n" \
209 "is required to remove device from the known HIDs file. This should be done\n" \
210 "when reset button was pressed on the device or the battery was changed. The\n"\
211 "Bluetooth HID daemon should be restarted.\n",
212 hid_forget
213 },
214 { NULL, NULL, NULL }
215 };
216 
217