libhid-detach-device.c

Go to the documentation of this file.
00001 #define HID_INTERNAL
00002 
00003 #include <hid.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 #include <assert.h>
00009 #include <debug.h>
00010 
00011 int main(int argc, char** argp)
00012 {
00013   HIDInterface* hid;
00014   hid_return ret;
00015   char* const HEXPREFIX = "0x";
00016   unsigned char const HEXPREFIXLEN = 2;
00017   unsigned char const HEXSTRINGLEN = 6;
00018   unsigned char const HEXNUMLEN = 4;
00019   char vendor_id_s[HEXSTRINGLEN + 1], product_id_s[HEXSTRINGLEN + 1];
00020   unsigned short vendor_id = 0, product_id = 0;
00021 
00022   if (argc != 2 || strlen(argp[1]) != strlen("dead:beef")) {
00023     fprintf(stderr, "Usage: %s vendor_id:product_id\n", argp[0]);
00024     fprintf(stderr, "   Eg: %s 06c2:0038\n", argp[0]);
00025     return 255;
00026   }
00027 
00028   memcpy(vendor_id_s, HEXPREFIX, HEXPREFIXLEN);
00029   strncpy(vendor_id_s + HEXPREFIXLEN, argp[1], HEXNUMLEN);
00030   vendor_id = 0xffff & strtol(vendor_id_s, NULL, 16);
00031 
00032   memcpy(product_id_s, HEXPREFIX, HEXPREFIXLEN);
00033   strncpy(product_id_s + HEXPREFIXLEN, argp[1] + HEXNUMLEN + 1, HEXNUMLEN);
00034   product_id = 0xffff & strtol(product_id_s, NULL, 16);
00035 
00036   fprintf(stderr, "Trying to detach HID with IDs %04x:%04x... ",
00037       vendor_id, product_id);
00038     
00039   HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };
00040 
00041   ret = hid_init();
00042   if (ret != HID_RET_SUCCESS) {
00043     fprintf(stderr, "hid_init failed with return code %d.\n", ret);
00044     return 1;
00045   }
00046 
00047   hid = hid_new_HIDInterface();
00048   if (hid == 0) {
00049     fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
00050     return 1;
00051   }
00052 
00053   ret = hid_force_open(hid, 0, &matcher, 3);
00054   if (ret != HID_RET_SUCCESS) {
00055     fprintf(stderr, "hid_force_open failed with return code %d.\n", ret);
00056     return 1;
00057   }
00058 
00059   ret = hid_close(hid);
00060   if (ret != HID_RET_SUCCESS) {
00061     fprintf(stderr, "hid_close failed with return code %d.\n", ret);
00062     return 1;
00063   }
00064 
00065   hid_delete_HIDInterface(&hid);
00066 
00067   ret = hid_cleanup();
00068   if (ret != HID_RET_SUCCESS) {
00069     fprintf(stderr, "hid_cleanup failed with return code %d.\n", ret);
00070     return 1;
00071   }
00072 
00073   fprintf(stderr, "done.\n");
00074   
00075   return 0;
00076 }
00077 
00078 /* COPYRIGHT --
00079  *
00080  * This file is part of libhid, a user-space HID access library.
00081  * libhid is (c) 2003-2005
00082  *   Martin F. Krafft <libhid@pobox.madduck.net>
00083  *   Charles Lepple <clepple@ghz.cc>
00084  *   Arnaud Quette <arnaud.quette@free.fr> && <arnaud.quette@mgeups.com>
00085  * and distributed under the terms of the GNU General Public License.
00086  * See the file ./COPYING in the source distribution for more information.
00087  *
00088  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
00089  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
00090  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00091  */

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