| [4880210] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Lubos Slovak
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup mkbd
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Sample application using the data from multimedia keys on keyboard
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <inttypes.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <bool.h>
|
|---|
| 43 | #include <getopt.h>
|
|---|
| 44 | #include <devman.h>
|
|---|
| 45 | #include <devmap.h>
|
|---|
| [b2995c3] | 46 | #include <usb/dev/hub.h>
|
|---|
| [e765ccb] | 47 | #include <usb/hid/iface.h>
|
|---|
| [b2995c3] | 48 | #include <usb/dev/pipes.h>
|
|---|
| [4e78236] | 49 | #include <async.h>
|
|---|
| [d7c72db] | 50 | #include <usb/hid/usages/core.h>
|
|---|
| 51 | #include <usb/hid/hidparser.h>
|
|---|
| 52 | #include <usb/hid/hiddescriptor.h>
|
|---|
| 53 | #include <usb/hid/usages/consumer.h>
|
|---|
| 54 | #include <assert.h>
|
|---|
| [4880210] | 55 |
|
|---|
| 56 | #define NAME "mkbd"
|
|---|
| 57 |
|
|---|
| [79ae36dd] | 58 | static async_sess_t *dev_sess = NULL;
|
|---|
| [4880210] | 59 |
|
|---|
| [79ae36dd] | 60 | static int initialize_report_parser(async_sess_t *dev_sess,
|
|---|
| 61 | usb_hid_report_t **report)
|
|---|
| [d7c72db] | 62 | {
|
|---|
| [79ae36dd] | 63 | *report = (usb_hid_report_t *) malloc(sizeof(usb_hid_report_t));
|
|---|
| 64 | if (*report == NULL)
|
|---|
| [d7c72db] | 65 | return ENOMEM;
|
|---|
| 66 |
|
|---|
| [fa8d346] | 67 | int rc = usb_hid_report_init(*report);
|
|---|
| [d7c72db] | 68 | if (rc != EOK) {
|
|---|
| [fa8d346] | 69 | usb_hid_free_report(*report);
|
|---|
| 70 | *report = NULL;
|
|---|
| [d7c72db] | 71 | return rc;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| [79ae36dd] | 74 | /* Get the report descriptor length from the device */
|
|---|
| [d7c72db] | 75 | size_t report_desc_size;
|
|---|
| [79ae36dd] | 76 | rc = usbhid_dev_get_report_descriptor_length(dev_sess,
|
|---|
| 77 | &report_desc_size);
|
|---|
| [d7c72db] | 78 | if (rc != EOK) {
|
|---|
| [fa8d346] | 79 | usb_hid_free_report(*report);
|
|---|
| 80 | *report = NULL;
|
|---|
| [d7c72db] | 81 | return rc;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (report_desc_size == 0) {
|
|---|
| [fa8d346] | 85 | usb_hid_free_report(*report);
|
|---|
| 86 | *report = NULL;
|
|---|
| [79ae36dd] | 87 | // TODO: other error code?
|
|---|
| 88 | return EINVAL;
|
|---|
| [d7c72db] | 89 | }
|
|---|
| 90 |
|
|---|
| [79ae36dd] | 91 | uint8_t *desc = (uint8_t *) malloc(report_desc_size);
|
|---|
| [d7c72db] | 92 | if (desc == NULL) {
|
|---|
| [fa8d346] | 93 | usb_hid_free_report(*report);
|
|---|
| 94 | *report = NULL;
|
|---|
| [d7c72db] | 95 | return ENOMEM;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| [79ae36dd] | 98 | /* Get the report descriptor from the device */
|
|---|
| [d7c72db] | 99 | size_t actual_size;
|
|---|
| [79ae36dd] | 100 | rc = usbhid_dev_get_report_descriptor(dev_sess, desc, report_desc_size,
|
|---|
| [d7c72db] | 101 | &actual_size);
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| [fa8d346] | 103 | usb_hid_free_report(*report);
|
|---|
| 104 | *report = NULL;
|
|---|
| [d7c72db] | 105 | free(desc);
|
|---|
| 106 | return rc;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if (actual_size != report_desc_size) {
|
|---|
| [fa8d346] | 110 | usb_hid_free_report(*report);
|
|---|
| 111 | *report = NULL;
|
|---|
| [d7c72db] | 112 | free(desc);
|
|---|
| [79ae36dd] | 113 | // TODO: other error code?
|
|---|
| 114 | return EINVAL;
|
|---|
| [d7c72db] | 115 | }
|
|---|
| 116 |
|
|---|
| [79ae36dd] | 117 | /* Initialize the report parser */
|
|---|
| [d7c72db] | 118 |
|
|---|
| [fa8d346] | 119 | rc = usb_hid_parse_report_descriptor(*report, desc, report_desc_size);
|
|---|
| [d7c72db] | 120 | free(desc);
|
|---|
| 121 |
|
|---|
| 122 | if (rc != EOK) {
|
|---|
| 123 | free(desc);
|
|---|
| 124 | return rc;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| [4880210] | 129 |
|
|---|
| [d7c72db] | 130 | static void print_key(uint8_t *buffer, size_t size, usb_hid_report_t *report)
|
|---|
| 131 | {
|
|---|
| 132 | assert(buffer != NULL);
|
|---|
| 133 | assert(report != NULL);
|
|---|
| 134 |
|
|---|
| 135 | uint8_t report_id;
|
|---|
| [da56be2] | 136 | int rc = usb_hid_parse_report(report, buffer, size, &report_id);
|
|---|
| [79ae36dd] | 137 | if (rc != EOK)
|
|---|
| [da56be2] | 138 | return;
|
|---|
| [d7c72db] | 139 |
|
|---|
| 140 | usb_hid_report_path_t *path = usb_hid_report_path();
|
|---|
| 141 | if (path == NULL) {
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
|
|---|
| 146 |
|
|---|
| 147 | usb_hid_report_path_set_report_id(path, report_id);
|
|---|
| [4880210] | 148 |
|
|---|
| [d7c72db] | 149 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
|---|
| [79ae36dd] | 150 | report, NULL, path, USB_HID_PATH_COMPARE_END
|
|---|
| 151 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
|---|
| [d7c72db] | 152 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 153 |
|
|---|
| 154 | while (field != NULL) {
|
|---|
| 155 | if (field->value != 0) {
|
|---|
| 156 | const char *key_str =
|
|---|
| 157 | usbhid_multimedia_usage_to_str(field->usage);
|
|---|
| 158 | printf("Pressed key: %s\n", key_str);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | field = usb_hid_report_get_sibling(
|
|---|
| 162 | report, field, path, USB_HID_PATH_COMPARE_END
|
|---|
| 163 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
|---|
| 164 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | usb_hid_report_path_free(path);
|
|---|
| 168 | }
|
|---|
| [4880210] | 169 |
|
|---|
| [d7c72db] | 170 | #define MAX_PATH_LENGTH 1024
|
|---|
| [4880210] | 171 |
|
|---|
| 172 | static void print_usage(char *app_name)
|
|---|
| 173 | {
|
|---|
| 174 | #define _INDENT " "
|
|---|
| [79ae36dd] | 175 | printf(NAME ": Print out what multimedia keys were pressed.\n\n");
|
|---|
| 176 | printf("Usage: %s device\n", app_name);
|
|---|
| 177 | printf(_INDENT "The device is a devman path to the device.\n");
|
|---|
| [4880210] | 178 | #undef _OPTION
|
|---|
| 179 | #undef _INDENT
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | int main(int argc, char *argv[])
|
|---|
| 183 | {
|
|---|
| [266fcd8] | 184 | int act_event = -1;
|
|---|
| [4880210] | 185 |
|
|---|
| 186 | if (argc <= 1) {
|
|---|
| 187 | print_usage(argv[0]);
|
|---|
| 188 | return -1;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| [5c2c459] | 191 | char *devpath = argv[1];
|
|---|
| [e765ccb] | 192 |
|
|---|
| 193 | devman_handle_t dev_handle = 0;
|
|---|
| [f9a627a] | 194 |
|
|---|
| 195 | int rc = usb_resolve_device_handle(devpath, NULL, NULL, &dev_handle);
|
|---|
| [4880210] | 196 | if (rc != EOK) {
|
|---|
| [f9a627a] | 197 | printf("Device not found or not of USB kind: %s.\n",
|
|---|
| [e765ccb] | 198 | str_error(rc));
|
|---|
| 199 | return rc;
|
|---|
| [4880210] | 200 | }
|
|---|
| 201 |
|
|---|
| [79ae36dd] | 202 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 203 | dev_handle, 0);
|
|---|
| 204 | if (!sess) {
|
|---|
| [e765ccb] | 205 | printf(NAME ": failed to connect to the device (handle %"
|
|---|
| [79ae36dd] | 206 | PRIun "): %s.\n", dev_handle, str_error(errno));
|
|---|
| 207 | return errno;
|
|---|
| [4880210] | 208 | }
|
|---|
| 209 |
|
|---|
| [79ae36dd] | 210 | dev_sess = sess;
|
|---|
| [4880210] | 211 |
|
|---|
| [e765ccb] | 212 | char path[MAX_PATH_LENGTH];
|
|---|
| 213 | rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
|
|---|
| 214 | if (rc != EOK) {
|
|---|
| 215 | return ENOMEM;
|
|---|
| 216 | }
|
|---|
| [4880210] | 217 |
|
|---|
| [e765ccb] | 218 | printf("Device path: %s\n", path);
|
|---|
| 219 |
|
|---|
| [d7c72db] | 220 |
|
|---|
| 221 | usb_hid_report_t *report = NULL;
|
|---|
| [79ae36dd] | 222 | rc = initialize_report_parser(dev_sess, &report);
|
|---|
| [d7c72db] | 223 | if (rc != EOK) {
|
|---|
| 224 | printf("Failed to initialize report parser: %s\n",
|
|---|
| 225 | str_error(rc));
|
|---|
| 226 | return rc;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | assert(report != NULL);
|
|---|
| 230 |
|
|---|
| [e765ccb] | 231 | size_t size;
|
|---|
| [79ae36dd] | 232 | rc = usbhid_dev_get_event_length(dev_sess, &size);
|
|---|
| [e765ccb] | 233 | if (rc != EOK) {
|
|---|
| [d7c72db] | 234 | printf("Failed to get event length: %s.\n", str_error(rc));
|
|---|
| [e765ccb] | 235 | return rc;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| [d7c72db] | 238 | uint8_t *event = (uint8_t *)malloc(size);
|
|---|
| [e765ccb] | 239 | if (event == NULL) {
|
|---|
| [79ae36dd] | 240 | // TODO: hangup phone?
|
|---|
| [e765ccb] | 241 | return ENOMEM;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | size_t actual_size;
|
|---|
| [266fcd8] | 245 | int event_nr;
|
|---|
| [e765ccb] | 246 |
|
|---|
| [79ae36dd] | 247 | while (true) {
|
|---|
| [d7c72db] | 248 | /** @todo Try blocking call. */
|
|---|
| [79ae36dd] | 249 | rc = usbhid_dev_get_event(dev_sess, event, size, &actual_size,
|
|---|
| [266fcd8] | 250 | &event_nr, 0);
|
|---|
| [e765ccb] | 251 | if (rc != EOK) {
|
|---|
| [79ae36dd] | 252 | // TODO: hangup phone?
|
|---|
| [e765ccb] | 253 | printf("Error in getting event from the HID driver:"
|
|---|
| 254 | "%s.\n", str_error(rc));
|
|---|
| 255 | break;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| [266fcd8] | 258 | if (event_nr > act_event) {
|
|---|
| 259 | print_key(event, size, report);
|
|---|
| 260 | act_event = event_nr;
|
|---|
| 261 | }
|
|---|
| [d7c72db] | 262 |
|
|---|
| [61f49a0] | 263 | async_usleep(10000);
|
|---|
| [e765ccb] | 264 | }
|
|---|
| [4880210] | 265 |
|
|---|
| 266 | return 0;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /** @}
|
|---|
| 270 | */
|
|---|