| 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 <loc.h> | 
|---|
| 46 | #include <usb/dev/hub.h> | 
|---|
| 47 | #include <usb/hid/iface.h> | 
|---|
| 48 | #include <usb/dev/pipes.h> | 
|---|
| 49 | #include <async.h> | 
|---|
| 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 <io/console.h> | 
|---|
| 55 | #include <io/keycode.h> | 
|---|
| 56 | #include <assert.h> | 
|---|
| 57 |  | 
|---|
| 58 | #define NAME "mkbd" | 
|---|
| 59 |  | 
|---|
| 60 | static async_sess_t *dev_sess = NULL; | 
|---|
| 61 |  | 
|---|
| 62 | static int initialize_report_parser(async_sess_t *dev_sess, | 
|---|
| 63 | usb_hid_report_t **report) | 
|---|
| 64 | { | 
|---|
| 65 | *report = (usb_hid_report_t *) malloc(sizeof(usb_hid_report_t)); | 
|---|
| 66 | if (*report == NULL) | 
|---|
| 67 | return ENOMEM; | 
|---|
| 68 |  | 
|---|
| 69 | int rc = usb_hid_report_init(*report); | 
|---|
| 70 | if (rc != EOK) { | 
|---|
| 71 | usb_hid_free_report(*report); | 
|---|
| 72 | *report = NULL; | 
|---|
| 73 | return rc; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /* Get the report descriptor length from the device */ | 
|---|
| 77 | size_t report_desc_size; | 
|---|
| 78 | rc = usbhid_dev_get_report_descriptor_length(dev_sess, | 
|---|
| 79 | &report_desc_size); | 
|---|
| 80 | if (rc != EOK) { | 
|---|
| 81 | usb_hid_free_report(*report); | 
|---|
| 82 | *report = NULL; | 
|---|
| 83 | return rc; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | if (report_desc_size == 0) { | 
|---|
| 87 | usb_hid_free_report(*report); | 
|---|
| 88 | *report = NULL; | 
|---|
| 89 | // TODO: other error code? | 
|---|
| 90 | return EINVAL; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | uint8_t *desc = (uint8_t *) malloc(report_desc_size); | 
|---|
| 94 | if (desc == NULL) { | 
|---|
| 95 | usb_hid_free_report(*report); | 
|---|
| 96 | *report = NULL; | 
|---|
| 97 | return ENOMEM; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /* Get the report descriptor from the device */ | 
|---|
| 101 | size_t actual_size; | 
|---|
| 102 | rc = usbhid_dev_get_report_descriptor(dev_sess, desc, report_desc_size, | 
|---|
| 103 | &actual_size); | 
|---|
| 104 | if (rc != EOK) { | 
|---|
| 105 | usb_hid_free_report(*report); | 
|---|
| 106 | *report = NULL; | 
|---|
| 107 | free(desc); | 
|---|
| 108 | return rc; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | if (actual_size != report_desc_size) { | 
|---|
| 112 | usb_hid_free_report(*report); | 
|---|
| 113 | *report = NULL; | 
|---|
| 114 | free(desc); | 
|---|
| 115 | // TODO: other error code? | 
|---|
| 116 | return EINVAL; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | /* Initialize the report parser */ | 
|---|
| 120 |  | 
|---|
| 121 | rc = usb_hid_parse_report_descriptor(*report, desc, report_desc_size); | 
|---|
| 122 | free(desc); | 
|---|
| 123 |  | 
|---|
| 124 | if (rc != EOK) { | 
|---|
| 125 | free(desc); | 
|---|
| 126 | return rc; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | return EOK; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | static void print_key(uint8_t *buffer, size_t size, usb_hid_report_t *report) | 
|---|
| 133 | { | 
|---|
| 134 | assert(buffer != NULL); | 
|---|
| 135 | assert(report != NULL); | 
|---|
| 136 |  | 
|---|
| 137 | uint8_t report_id; | 
|---|
| 138 | int rc = usb_hid_parse_report(report, buffer, size, &report_id); | 
|---|
| 139 | if (rc != EOK) | 
|---|
| 140 | return; | 
|---|
| 141 |  | 
|---|
| 142 | usb_hid_report_path_t *path = usb_hid_report_path(); | 
|---|
| 143 | if (path == NULL) { | 
|---|
| 144 | return; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0); | 
|---|
| 148 |  | 
|---|
| 149 | usb_hid_report_path_set_report_id(path, report_id); | 
|---|
| 150 |  | 
|---|
| 151 | usb_hid_report_field_t *field = usb_hid_report_get_sibling( | 
|---|
| 152 | report, NULL, path, USB_HID_PATH_COMPARE_END | 
|---|
| 153 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, | 
|---|
| 154 | USB_HID_REPORT_TYPE_INPUT); | 
|---|
| 155 |  | 
|---|
| 156 | while (field != NULL) { | 
|---|
| 157 | if (field->value != 0) { | 
|---|
| 158 | const char *key_str = | 
|---|
| 159 | usbhid_multimedia_usage_to_str(field->usage); | 
|---|
| 160 | printf("Pressed key: %s\n", key_str); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | field = usb_hid_report_get_sibling( | 
|---|
| 164 | report, field, path, USB_HID_PATH_COMPARE_END | 
|---|
| 165 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, | 
|---|
| 166 | USB_HID_REPORT_TYPE_INPUT); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | usb_hid_report_path_free(path); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | static int wait_for_quit_fibril(void *arg) | 
|---|
| 173 | { | 
|---|
| 174 | console_ctrl_t *con = console_init(stdin, stdout); | 
|---|
| 175 |  | 
|---|
| 176 | printf("Press <ESC> to quit the application.\n"); | 
|---|
| 177 |  | 
|---|
| 178 | while (1) { | 
|---|
| 179 | kbd_event_t ev; | 
|---|
| 180 | bool ok = console_get_kbd_event(con, &ev); | 
|---|
| 181 | if (!ok) { | 
|---|
| 182 | printf("Connection with console broken: %s.\n", | 
|---|
| 183 | str_error(errno)); | 
|---|
| 184 | break; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | if (ev.key == KC_ESCAPE) { | 
|---|
| 188 | break; | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | console_done(con); | 
|---|
| 193 |  | 
|---|
| 194 | exit(0); | 
|---|
| 195 |  | 
|---|
| 196 | return EOK; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | #define MAX_PATH_LENGTH 1024 | 
|---|
| 200 |  | 
|---|
| 201 | static void print_usage(char *app_name) | 
|---|
| 202 | { | 
|---|
| 203 | #define _INDENT "      " | 
|---|
| 204 | printf(NAME ": Print out what multimedia keys were pressed.\n\n"); | 
|---|
| 205 | printf("Usage: %s device\n", app_name); | 
|---|
| 206 | printf(_INDENT "The device is a devman path to the device.\n"); | 
|---|
| 207 | #undef _OPTION | 
|---|
| 208 | #undef _INDENT | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | int main(int argc, char *argv[]) | 
|---|
| 212 | { | 
|---|
| 213 | int act_event = -1; | 
|---|
| 214 |  | 
|---|
| 215 | if (argc <= 1) { | 
|---|
| 216 | print_usage(argv[0]); | 
|---|
| 217 | return -1; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | char *devpath = argv[1]; | 
|---|
| 221 |  | 
|---|
| 222 | devman_handle_t dev_handle = 0; | 
|---|
| 223 |  | 
|---|
| 224 | int rc = usb_resolve_device_handle(devpath, NULL, NULL, &dev_handle); | 
|---|
| 225 | if (rc != EOK) { | 
|---|
| 226 | printf("Device not found or not of USB kind: %s.\n", | 
|---|
| 227 | str_error(rc)); | 
|---|
| 228 | return rc; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE, | 
|---|
| 232 | dev_handle, 0); | 
|---|
| 233 | if (!sess) { | 
|---|
| 234 | printf(NAME ": failed to connect to the device (handle %" | 
|---|
| 235 | PRIun "): %s.\n", dev_handle, str_error(errno)); | 
|---|
| 236 | return errno; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | dev_sess = sess; | 
|---|
| 240 |  | 
|---|
| 241 | char path[MAX_PATH_LENGTH]; | 
|---|
| 242 | rc = devman_fun_get_path(dev_handle, path, MAX_PATH_LENGTH); | 
|---|
| 243 | if (rc != EOK) { | 
|---|
| 244 | return ENOMEM; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | printf("Device path: %s\n", path); | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 | usb_hid_report_t *report = NULL; | 
|---|
| 251 | rc = initialize_report_parser(dev_sess, &report); | 
|---|
| 252 | if (rc != EOK) { | 
|---|
| 253 | printf("Failed to initialize report parser: %s\n", | 
|---|
| 254 | str_error(rc)); | 
|---|
| 255 | return rc; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | assert(report != NULL); | 
|---|
| 259 |  | 
|---|
| 260 | size_t size; | 
|---|
| 261 | rc = usbhid_dev_get_event_length(dev_sess, &size); | 
|---|
| 262 | if (rc != EOK) { | 
|---|
| 263 | printf("Failed to get event length: %s.\n", str_error(rc)); | 
|---|
| 264 | return rc; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | uint8_t *event = (uint8_t *)malloc(size); | 
|---|
| 268 | if (event == NULL) { | 
|---|
| 269 | // TODO: hangup phone? | 
|---|
| 270 | return ENOMEM; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | fid_t quit_fibril = fibril_create(wait_for_quit_fibril, NULL); | 
|---|
| 274 | if (quit_fibril == 0) { | 
|---|
| 275 | printf("Failed to start extra fibril.\n"); | 
|---|
| 276 | return -1; | 
|---|
| 277 | } | 
|---|
| 278 | fibril_add_ready(quit_fibril); | 
|---|
| 279 |  | 
|---|
| 280 | size_t actual_size; | 
|---|
| 281 | int event_nr; | 
|---|
| 282 |  | 
|---|
| 283 | while (true) { | 
|---|
| 284 | /** @todo Try blocking call. */ | 
|---|
| 285 | rc = usbhid_dev_get_event(dev_sess, event, size, &actual_size, | 
|---|
| 286 | &event_nr, 0); | 
|---|
| 287 | if (rc != EOK) { | 
|---|
| 288 | // TODO: hangup phone? | 
|---|
| 289 | printf("Error in getting event from the HID driver:" | 
|---|
| 290 | "%s.\n", str_error(rc)); | 
|---|
| 291 | break; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | if (event_nr > act_event) { | 
|---|
| 295 | print_key(event, size, report); | 
|---|
| 296 | act_event = event_nr; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | async_usleep(10000); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | return 0; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | /** @} | 
|---|
| 306 | */ | 
|---|