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