main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Lubos Slovak
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <inttypes.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <errno.h>
00041 #include <str_error.h>
00042 #include <bool.h>
00043 #include <getopt.h>
00044 #include <devman.h>
00045 #include <devmap.h>
00046 #include <usb/dev/hub.h>
00047 //#include <usb/host.h>
00048 //#include <usb/driver.h>
00049 #include <usb/hid/iface.h>
00050 #include <usb/dev/pipes.h>
00051 #include <async.h>
00052 #include <usb/hid/usages/core.h>
00053 #include <usb/hid/hidparser.h>
00054 #include <usb/hid/hiddescriptor.h>
00055 #include <usb/hid/usages/consumer.h>
00056 #include <assert.h>
00057 
00058 #define NAME "mkbd"
00059 
00060 static int dev_phone = -1;
00061 
00062 static int initialize_report_parser(int dev_phone, usb_hid_report_t **report)
00063 {
00064         *report = (usb_hid_report_t *)malloc(sizeof(usb_hid_report_t));
00065         if (*report == NULL) {
00066                 return ENOMEM;
00067         }
00068         
00069         int rc = usb_hid_report_init(*report);
00070         if (rc != EOK) {
00071                 usb_hid_free_report(*report);
00072                 *report = NULL;
00073                 //printf("usb_hid_report_init() failed.\n");
00074                 return rc;
00075         }
00076         
00077         // get the report descriptor length from the device
00078         size_t report_desc_size;
00079         rc = usbhid_dev_get_report_descriptor_length(
00080             dev_phone, &report_desc_size);
00081         if (rc != EOK) {
00082                 usb_hid_free_report(*report);
00083                 *report = NULL;
00084                 //printf("usbhid_dev_get_report_descriptor_length() failed.\n");
00085                 return rc;
00086         }
00087         
00088         if (report_desc_size == 0) {
00089                 usb_hid_free_report(*report);
00090                 *report = NULL;
00091                 //printf("usbhid_dev_get_report_descriptor_length() returned 0.\n");
00092                 return EINVAL;  // TODO: other error code?
00093         }
00094         
00095         uint8_t *desc = (uint8_t *)malloc(report_desc_size);
00096         if (desc == NULL) {
00097                 usb_hid_free_report(*report);
00098                 *report = NULL;
00099                 return ENOMEM;
00100         }
00101         
00102         // get the report descriptor from the device
00103         size_t actual_size;
00104         rc = usbhid_dev_get_report_descriptor(dev_phone, desc, report_desc_size,
00105             &actual_size);
00106         if (rc != EOK) {
00107                 usb_hid_free_report(*report);
00108                 *report = NULL;
00109                 free(desc);
00110                 //printf("usbhid_dev_get_report_descriptor() failed.\n");
00111                 return rc;
00112         }
00113         
00114         if (actual_size != report_desc_size) {
00115                 usb_hid_free_report(*report);
00116                 *report = NULL;
00117                 free(desc);
00118 //              printf("usbhid_dev_get_report_descriptor() returned wrong size:"
00119 //                  " %zu, expected: %zu.\n", actual_size, report_desc_size);
00120                 return EINVAL;  // TODO: other error code?
00121         }
00122         
00123         // initialize the report parser
00124         
00125         rc = usb_hid_parse_report_descriptor(*report, desc, report_desc_size);
00126         free(desc);
00127         
00128         if (rc != EOK) {
00129                 free(desc);
00130 //              printf("usb_hid_parse_report_descriptor() failed.\n");
00131                 return rc;
00132         }
00133         
00134         return EOK;
00135 }
00136 
00137 static void print_key(uint8_t *buffer, size_t size, usb_hid_report_t *report)
00138 {
00139         assert(buffer != NULL);
00140         assert(report != NULL);
00141         
00142 //      printf("Calling usb_hid_parse_report() with size %zu and "
00143 //          "buffer: \n", size);
00144 //      for (size_t i = 0; i < size; ++i) {
00145 //              printf(" %X ", buffer[i]);
00146 //      }
00147 //      printf("\n");
00148         
00149         uint8_t report_id;
00150         int rc = usb_hid_parse_report(report, buffer, size, &report_id);
00151         if (rc != EOK) {
00152 //              printf("Error parsing report: %s\n", str_error(rc));
00153                 return;
00154         }
00155         
00156         usb_hid_report_path_t *path = usb_hid_report_path();
00157         if (path == NULL) {
00158                 return;
00159         }
00160         
00161         usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
00162         
00163         usb_hid_report_path_set_report_id(path, report_id);
00164 
00165         usb_hid_report_field_t *field = usb_hid_report_get_sibling(
00166             report, NULL, path, USB_HID_PATH_COMPARE_END 
00167             | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
00168             USB_HID_REPORT_TYPE_INPUT);
00169         
00170 //      printf("Field: %p\n", field);
00171         
00172         while (field != NULL) {
00173 //              printf("Field usage: %u, field value: %d\n", field->usage, 
00174 //                  field->value);
00175                 if (field->value != 0) {
00176                         const char *key_str = 
00177                             usbhid_multimedia_usage_to_str(field->usage);
00178                         printf("Pressed key: %s\n", key_str);
00179                 }
00180                 
00181                 field = usb_hid_report_get_sibling(
00182                     report, field, path, USB_HID_PATH_COMPARE_END
00183                     | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
00184                     USB_HID_REPORT_TYPE_INPUT);
00185 //              printf("Next field: %p\n", field);
00186         }
00187         
00188         usb_hid_report_path_free(path);
00189 }
00190 
00191 #define MAX_PATH_LENGTH 1024
00192 
00193 static void print_usage(char *app_name)
00194 {
00195 #define _INDENT "      "
00196 
00197        printf(NAME ": Print out what multimedia keys were pressed.\n\n");
00198        printf("Usage: %s device\n", app_name);
00199        printf(_INDENT "The device is a devman path to the device.\n");
00200 
00201 #undef _OPTION
00202 #undef _INDENT
00203 }
00204 
00205 int main(int argc, char *argv[])
00206 {
00207         int act_event = -1;
00208         
00209         if (argc <= 1) {
00210                 print_usage(argv[0]);
00211                 return -1;
00212         }
00213         
00214         char *devpath = argv[1];
00215         
00216         devman_handle_t dev_handle = 0;
00217         
00218         int rc = usb_resolve_device_handle(devpath, NULL, NULL, &dev_handle);
00219         if (rc != EOK) {
00220                 printf("Device not found or not of USB kind: %s.\n",
00221                     str_error(rc));
00222                 return rc;
00223         }
00224         
00225         rc = devman_device_connect(dev_handle, 0);
00226         if (rc < 0) {
00227                 printf(NAME ": failed to connect to the device (handle %"
00228                        PRIun "): %s.\n", dev_handle, str_error(rc));
00229                 return rc;
00230         }
00231         
00232         dev_phone = rc;
00233 //      printf("Got phone to the device: %d\n", dev_phone);
00234         
00235         char path[MAX_PATH_LENGTH];
00236         rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
00237         if (rc != EOK) {
00238                 return ENOMEM;
00239         }
00240         
00241         printf("Device path: %s\n", path);
00242         
00243         
00244         usb_hid_report_t *report = NULL;
00245         rc = initialize_report_parser(dev_phone, &report);
00246         if (rc != EOK) {
00247                 printf("Failed to initialize report parser: %s\n",
00248                     str_error(rc));
00249                 return rc;
00250         }
00251         
00252         assert(report != NULL);
00253         
00254         size_t size;
00255         rc = usbhid_dev_get_event_length(dev_phone, &size);
00256         if (rc != EOK) {
00257                 printf("Failed to get event length: %s.\n", str_error(rc));
00258                 return rc;
00259         }
00260         
00261 //      printf("Event length: %zu\n", size);
00262         uint8_t *event = (uint8_t *)malloc(size);
00263         if (event == NULL) {
00264                 // hangup phone?
00265                 return ENOMEM;
00266         }
00267         
00268 //      printf("Event length: %zu\n", size);
00269         
00270         size_t actual_size;
00271         int event_nr;
00272         
00273         while (1) {
00274                 // get event from the driver
00275 //              printf("Getting event from the driver.\n");
00276                 
00278                 rc = usbhid_dev_get_event(dev_phone, event, size, &actual_size, 
00279                     &event_nr, 0);
00280                 if (rc != EOK) {
00281                         // hangup phone?
00282                         printf("Error in getting event from the HID driver:"
00283                             "%s.\n", str_error(rc));
00284                         break;
00285                 }
00286                 
00287 //              printf("Got buffer: %p, size: %zu, max size: %zu\n", event, 
00288 //                  actual_size, size);
00289                 
00290 //              printf("Event number: %d, my actual event: %d\n", event_nr, 
00291 //                  act_event);
00292                 if (event_nr > act_event) {
00293                         print_key(event, size, report);
00294                         act_event = event_nr;
00295                 }
00296                 
00297                 async_usleep(10000);
00298         }
00299         
00300         return 0;
00301 }
00302 
00303 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7