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