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