hid_parsing.c

Go to the documentation of this file.
00001 #define HID_INTERNAL
00002 
00003 #include <hid.h>
00004 #include <hid_helpers.h>
00005 
00006 #include <string.h>
00007 
00008 #include <debug.h>
00009 #include <assert.h>
00010 
00011 static void hid_prepare_parse_path(HIDInterface* const hidif,
00012     int const path[], unsigned int const depth)
00013 {
00014   ASSERT(hid_is_opened(hidif));
00015   ASSERT(hidif->hid_data);
00016 
00017   unsigned int i = 0;
00018 
00019   TRACE("preparing search path of depth %d for parse tree of USB device %s...",
00020       depth, hidif->id);
00021   for (; i < depth; ++i) {
00022     hidif->hid_data->Path.Node[i].UPage = path[i] >> 16;
00023     hidif->hid_data->Path.Node[i].Usage = path[i] & 0x0000ffff;
00024   }
00025 
00026   hidif->hid_data->Path.Size = depth;
00027 
00028   TRACE("search path prepared for parse tree of USB device %s.", hidif->id);
00029 }
00030 
00031 hid_return hid_init_parser(HIDInterface* const hidif)
00032 {
00033   if (!hid_is_opened(hidif)) {
00034     ERROR("cannot initialise parser of unopened HIDinterface.");
00035     return HID_RET_DEVICE_NOT_OPENED;
00036   }
00037   
00038   ASSERT(!hidif->hid_parser);
00039   ASSERT(!hidif->hid_data);
00040 
00041   TRACE("initialising the HID parser for USB Device %s...", hidif->id);
00042 
00043   TRACE("allocating space for HIDData structure...");
00044   hidif->hid_data = (HIDData*)malloc(sizeof(HIDData));
00045   if (!hidif->hid_data) {
00046     ERROR("failed to allocate memory for HIDData structure of USB device %s.",
00047         hidif->id);
00048     return HID_RET_FAIL_ALLOC;
00049   }
00050   TRACE("successfully allocated memory for HIDData strcture.");
00051 
00052   TRACE("allocating space for HIDParser structure...");
00053   hidif->hid_parser = (HIDParser*)malloc(sizeof(HIDParser));
00054   if (!hidif->hid_parser) {
00055     ERROR("failed to allocate memory for HIDParser structure of USB device %s.",
00056         hidif->id);
00057     return HID_RET_FAIL_ALLOC;
00058   }
00059   TRACE("successfully allocated memory for HIDParser strcture.");
00060 
00061   NOTICE("successfully initialised the HID parser for USB Device %s.",
00062       hidif->id);
00063   
00064   return HID_RET_SUCCESS;
00065 }
00066 
00067 hid_return hid_prepare_parser(HIDInterface* const hidif)
00068 {
00069   if (!hid_is_opened(hidif)) {
00070     ERROR("cannot prepare parser of unopened HIDinterface.");
00071     return HID_RET_DEVICE_NOT_OPENED;
00072   }
00073   ASSERT(hidif->hid_parser);
00074   
00075   TRACE("setting up the HID parser for USB device %s...", hidif->id);
00076 
00077   hid_reset_parser(hidif);
00078 
00079   TRACE("dumping the raw report descriptor");
00080   {
00081     char buffer[160], tmp[10];
00082     int i;
00083 
00084     sprintf(buffer, "0x%03x: ", 0);
00085     for(i=0; i<hidif->hid_parser->ReportDescSize; i++) {
00086       if(!(i % 8)) {
00087         if(i != 0) TRACE("%s", buffer);
00088         sprintf(buffer, "0x%03x: ", i);
00089       }
00090       sprintf(tmp, "0x%02x ", (int)(hidif->hid_parser->ReportDesc[i]));
00091       strcat(buffer, tmp);
00092     }
00093     TRACE("%s", buffer);
00094   }
00095   
00096   /* TODO: the return value here should be used, no? */
00097   TRACE("parsing the HID tree of USB device %s...", hidif->id);
00098   HIDParse(hidif->hid_parser, hidif->hid_data);
00099 
00100   NOTICE("successfully set up the HID parser for USB device %s.", hidif->id);
00101 
00102   return HID_RET_SUCCESS;
00103 }
00104 
00105 void hid_reset_parser(HIDInterface* const hidif)
00106 {
00107   if (!hid_is_opened(hidif)) {
00108     ERROR("cannot prepare parser of unopened HIDinterface.");
00109     return;
00110   }
00111   ASSERT(hidif->hid_parser);
00112   
00113   TRACE("resetting the HID parser for USB device %s...", hidif->id);
00114   ResetParser(hidif->hid_parser);
00115 }
00116 
00117 hid_return hid_find_object(HIDInterface* const hidif,
00118     int const path[], unsigned int const depth)
00119 {
00120   if (!hid_is_opened(hidif)) {
00121     ERROR("cannot prepare parser of unopened HIDinterface.");
00122     return HID_RET_DEVICE_NOT_OPENED;
00123   }
00124   ASSERT(hidif->hid_parser);
00125   ASSERT(hidif->hid_data);
00126   
00127   hid_prepare_parse_path(hidif, path, depth);
00128 
00129   if (FindObject(hidif->hid_parser, hidif->hid_data) == 1) {
00130     NOTICE("found requested item.");
00131     return HID_RET_SUCCESS;
00132   }
00133 
00134   byte const ITEMSIZE = sizeof("0xdeadbeef");
00135   char* buffer = (char*)malloc(depth * ITEMSIZE);
00136   hid_format_path(buffer, depth * ITEMSIZE, path, depth); 
00137   WARNING("can't find requested item %s of USB device %s.\n", buffer, hidif->id);
00138   free(buffer);
00139   
00140   return HID_RET_NOT_FOUND;
00141 }
00142 
00143 hid_return hid_extract_value(HIDInterface* const hidif,
00144     unsigned char *const buffer, double *const value)
00145 {
00146   if (!hid_is_opened(hidif)) {
00147     ERROR("cannot extract value from unopened HIDinterface.");
00148     return HID_RET_DEVICE_NOT_OPENED;
00149   }
00150   ASSERT(hidif->hid_parser);
00151   ASSERT(hidif->hid_data);
00152 
00153   if (!buffer) {
00154     ERROR("cannot extract value from USB device %s into NULL raw buffer.",
00155         hidif->id);
00156     return HID_RET_INVALID_PARAMETER;
00157   }
00158 
00159   if (!value) {
00160     ERROR("cannot extract value from USB device %s into NULL value buffer.",
00161         hidif->id);
00162     return HID_RET_INVALID_PARAMETER;
00163   }
00164   
00165   TRACE("extracting data value...");
00166 
00167   /* Extract the data value */
00168   GetValue(buffer, hidif->hid_data);
00169 
00170   /* FIXME: unit conversion and exponent?! */
00171   *value = hidif->hid_data->Value;
00172   
00173   return HID_RET_SUCCESS;
00174 }
00175 
00176 hid_return hid_get_report_size(HIDInterface* const hidif,
00177     unsigned int const reportID, unsigned int const reportType,
00178     unsigned int *size)
00179 {
00180   if (!hid_is_opened(hidif)) {
00181     ERROR("cannot get report size of unopened HIDinterface.");
00182     return HID_RET_DEVICE_NOT_OPENED;
00183   }
00184   ASSERT(hidif->hid_parser);
00185   ASSERT(hidif->hid_data);
00186   
00187   if (!size) {
00188     ERROR("cannot read report size from USB device %s into NULL size buffer.",
00189         hidif->id);
00190     return HID_RET_INVALID_PARAMETER;
00191   }
00192   
00193   /* FIXME: GetReportOffset has to be rewritten! */
00194   *size = *GetReportOffset(hidif->hid_parser, reportID, reportType);
00195   
00196   return HID_RET_SUCCESS;
00197 }
00198 
00199 hid_return hid_format_path(char* const buffer, unsigned int length,
00200     int const path[], unsigned int const depth)
00201 {
00202   if (!buffer) {
00203     ERROR("cannot format path into NULL buffer.");
00204     return HID_RET_INVALID_PARAMETER;
00205   }
00206 
00207   byte const ITEMSIZE = sizeof("0xdeadbeef");
00208   unsigned int i = 0;
00209 
00210   TRACE("formatting device path...");
00211   for (; i < depth; ++i) {
00212     if (length < ITEMSIZE) {
00213       WARNING("not enough space in buffer to finish formatting of path.")
00214       return HID_RET_OUT_OF_SPACE;
00215     }
00216     snprintf(buffer + i * ITEMSIZE, ITEMSIZE + 1, "0x%08x.", path[i]);
00217     length -= ITEMSIZE;
00218   }
00219   buffer[i * ITEMSIZE - 1] = '\0';
00220 
00221   return HID_RET_SUCCESS;
00222 }
00223 
00224 /* COPYRIGHT --
00225  *
00226  * This file is part of libhid, a user-space HID access library.
00227  * libhid is (c) 2003-2005
00228  *   Martin F. Krafft <libhid@pobox.madduck.net>
00229  *   Charles Lepple <clepple@ghz.cc>
00230  *   Arnaud Quette <arnaud.quette@free.fr> && <arnaud.quette@mgeups.com>
00231  * and distributed under the terms of the GNU General Public License.
00232  * See the file ./COPYING in the source distribution for more information.
00233  *
00234  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
00235  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
00236  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00237  */

Generated on Sun Mar 30 15:28:24 2008 for libhid by  doxygen 1.5.1