int open_usb_device() { hid_return ret; HIDInterfaceMatcher matcher = { VENDOR_ID, PRODUCT_ID, NULL, NULL, 0 }; hid_set_debug(HID_DEBUG_NONE); hid_set_debug_stream(stderr); hid_set_usb_debug(0); ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d\n", ret); return -1; } hid = hid_new_HIDInterface(); if (hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return -1; } ret = hid_force_open(hid, IFACE_CONTROL, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); return -1; } ret = hid_os_force_claim(hid, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_os_force_claim failed with return code %d\n", ret); return -1; } hid->interface = 1; return 0; }
int main(void){ HIDInterface* hid; hid_return ret; HIDInterfaceMatcher matcher = { VENDOR, PRODUCT, NULL, NULL, 0 }; char packet[PACKET_LEN]; hid_set_debug(HID_DEBUG_NONE); hid_set_debug_stream(stderr); hid_set_usb_debug(0); hid_init(); hid = hid_new_HIDInterface(); ret = hid_force_open(hid, 0, &matcher, 3); if (ret == HID_RET_FAIL_DETACH_DRIVER) { printf("Failed to detach Driver for Device %04x:%04x; Permission?\n", VENDOR, PRODUCT); return 1; } if (ret != HID_RET_SUCCESS) { printf("Could not open HID device %04x:%04x (ret was %d)\n", VENDOR, PRODUCT, ret); return 1; } // printf("hid_force_open ret=%d\n", ret); // hid_set_idle(hid,0,0); // Discard till first '\n' do { memset(packet,0,sizeof(packet)); ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000); } while (getTheChar(packet[1]) != '\n'); while (1) { memset(packet,0,sizeof(packet)); ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000); /* if (ret == HID_RET_FAIL_INT_READ) { printf("Fail hid_interrupt_read\n"); } else { printf("hid_interrupt_read ret=%d\n", ret); } */ if( ret == HID_RET_SUCCESS ) { //printPacket(packet,PACKET_LEN); showTemperature(packet); } } hid_close(hid); hid_delete_HIDInterface(&hid); hid_cleanup(); return 0; }
/* -------------------------------------------------------------------------- */ static void *usbhid_new(t_float f) { t_int i; HIDInterfaceMatcher matcher; t_usbhid *x = (t_usbhid *)pd_new(usbhid_class); if(x->debug_level) post("usbhid_new"); /* only display the version when the first instance is loaded */ if(!usbhid_instance_count) post("[usbhid] %d.%d, written by Hans-Christoph Steiner <*****@*****.**>", USBHID_MAJOR_VERSION, USBHID_MINOR_VERSION); /* create anything outlet used for HID data */ x->x_data_outlet = outlet_new(&x->x_obj, 0); x->x_status_outlet = outlet_new(&x->x_obj, 0); /* turn off the flood of libhid debug messages by default */ hid_set_debug(HID_DEBUG_NONE); hid_set_debug_stream(stdout); hid_set_usb_debug(0); /* data init */ x->output = NULL; x->output_count = 0; for (i = 0 ; i < HID_ID_MAX ; i++) hid_id[i] = NULL; x->x_hidinterface = hid_new_HIDInterface(); matcher.vendor_id = HID_ID_MATCH_ANY; matcher.product_id = HID_ID_MATCH_ANY; matcher.matcher_fn = device_iterator; x->x_write_path_count = 0; x->x_device_number = f; /* Open the device and save settings. If there is an error, return the object * anyway, so that the inlets and outlets are created, thus not breaking the * patch. */ /* if (usbhid_open(x,f)) */ /* error("[usbhid] device %d did not open",(t_int)f); */ usbhid_instance_count++; return (x); }
int main(void) { int i; hid_return ret; HIDInterface* hid; HIDInterfaceMatcher matcher; /* hid_write_library_config(stdout); */ /* hid_set_debug(HID_DEBUG_NOTRACES); */ // hid_set_debug(HID_DEBUG_NONE); hid_set_debug(HID_DEBUG_ALL); hid_set_debug_stream(stderr); hid_set_usb_debug(0); /* data init */ for (i = 0 ; i < 32 ; i++) hid_id[i] = NULL; ret = hid_init(); hid = hid_new_HIDInterface(); matcher.vendor_id = HID_ID_MATCH_ANY; matcher.product_id = HID_ID_MATCH_ANY; matcher.matcher_fn = device_iterator; /* open recursively all HID devices found */ while ( (ret = hid_force_open(hid, 0, 0, &matcher, 2)) != HID_RET_DEVICE_NOT_FOUND) { printf("************************************************************************\n"); hid_write_identification(stdout, hid); /* Only dump HID tree if asked */ /* hid_dump_tree(stdout, hid); */ hid_close(hid); } hid_delete_HIDInterface(&hid); ret = hid_cleanup(); return 0; }
int PMD_Find_Interface(HIDInterface** hid, int interface, int product_id) { hid_return ret; HIDInterfaceMatcher matcher = { MCC_VID, 0x0, NULL, NULL, 0 }; matcher.product_id = product_id; *hid = hid_new_HIDInterface(); if (*hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return -1; } ret = hid_force_open(*hid, interface, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); return -1; } else { return interface; } }
int main(int argc, char *argv[]) { HIDInterface* hid; int iface_num = 0; hid_return ret; unsigned short vendor_id = 0x1325; unsigned short product_id = 0xc029; char *vendor, *product; // Match USB by VID and PID HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 }; // Enable debugging // hid_set_debug(HID_DEBUG_ALL); // hid_set_debug_stream(stderr); // hid_set_usb_debug(0); // Initialize HID ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_init success! ***\n"); } hid = hid_new_HIDInterface(); if (hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return 1; } else { fprintf(stderr, "*** hid_new_HIDInterface success! ***\n"); } //TODO: Use hotplug or give permission to USB without sudo /* How to detach a device from the kernel HID driver: * * The hid.o or usbhid.ko kernel modules claim a HID device on insertion, * usually. To be able to use it with libhid, you need to blacklist the * device (which requires a kernel recompilation), or simply tell libhid to * detach it for you. hid_open just opens the device, hid_force_open will * try n times to detach the device before failing. * * To open the HID, you need permission to the file in the /proc usbfs * (which must be mounted -- most distros do that by default): * mount -t usbfs none /proc/bus/usb * You can use hotplug to automatically give permissions to the device on * connection. Please see * http://cvs.ailab.ch/cgi-bin/viewcvs.cgi/external/libphidgets/hotplug/ * for an example. Try NOT to work as root! */ // Detach kernel ret = hid_force_open(hid, iface_num, &matcher, 25); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_forcE_open success! ***\n"); } ret = hid_write_identification(stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_write_identification failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_write_identification success! ***\n"); } ret = hid_dump_tree(stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_dump_tree success! ***\n"); } RFIDCommunication(ret, &hid); printf("\n\n/* * * * * * * * END COMMUNICATION WITH RFID DEVICE * * * * * * * */\n\n"); ret = hid_close(hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_close failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_close successful! ***\n"); } hid_delete_HIDInterface(&hid); ret = hid_cleanup(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_cleanup failed with return code %d\n", ret); return 1; } else { fprintf(stderr, "*** hid_cleanup successful! ***\n"); } return 0; }
int main(int argc,char **argv) { ir_receiver_t* receiver; // parse command line args if (argc!=2) { printf("You must specify the receiver type: %s type\n",argv[0]); printf("Valid values for type are:\n"); unsigned int i=0; for (i=0;i<NB_IRR;i++) { printf("\t- %s\n",receivers[i].description); } return 1; } else { unsigned int i=0; for (i=0;i<NB_IRR;i++) { if (strncmp(receivers[i].description,argv[1],256)==0) { receiver=&receivers[i]; break; } } } printf("Running for type: %s\n",receiver->description); HIDInterface* hid; hid_return ret; hid_set_debug(0/*HID_DEBUG_ALL*/); hid_set_debug_stream(stderr); /* passed directly to libusb */ hid_set_usb_debug(0); ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d\n", ret); return 1; } hid = hid_new_HIDInterface(); if (hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return 1; } ret = hid_force_open(hid, 0, &receiver->matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); return 1; } ret = hid_write_identification(stderr, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_write_identification failed with return code %d\n", ret); return 1; } ret = hid_dump_tree(stderr, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret); return 1; } /* Infinite parsing loop use C-c to stop the program */ while (1) { char packet[receiver->packetSize]; ret = hid_interrupt_read(hid, 0x81, packet, receiver->packetSize,10000); if (ret == HID_RET_SUCCESS) { fflush(stdout); } } ret = hid_close(hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_close failed with return code %d\n", ret); return 1; } hid_delete_HIDInterface(&hid); ret = hid_cleanup(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_cleanup failed with return code %d\n", ret); return 1; } return 0; }
int main(int argc, char *argv[]) { HIDInterface* hid; int iface_num = 2; hid_return ret; int index=0; char buff[42]; unsigned short vendor_id = 0x0451; unsigned short product_id = 0x2100; int flag; uint16_t addr=0; uint8_t eepromBuff[512]; memset(eepromBuff, 0x00, 512); // HIDInterfaceMatcher matcher = { 0x0925, 0x1237, NULL, NULL, 0 }; HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 }; #if 1 /* see include/debug.h for possible values */ hid_set_debug(HID_DEBUG_ALL); hid_set_debug_stream(stderr); /* passed directly to libusb */ hid_set_usb_debug(0); ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d\n", ret); return 1; } hid = hid_new_HIDInterface(); if (hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return 1; } ret = hid_force_open(hid, iface_num, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); return 1; } ret = hid_write_identification(stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_write_identification failed with return code %d\n", ret); return 1; } ret = hid_dump_tree(stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret); return 1; } //#define _READ #ifdef _READ memset(eepromBuff, 0x00, 512); for (index=0;index < 64;index++) { memset(buff, 0x00, 42); addr = 8*index; buff[0] = PCTransfer; buff[1] = RequestEepromSectionRead; buff[2] = (0xFF00 & addr) >> 8; //M:offset buff[3] = 0x00FF & addr; //L:offset buff[4] = 8; //bytes fprintf(stderr, "sending:\n"); print_bytes(LOG_ERROR, buff, 42); ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret); } ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret); } fprintf(stderr, "readed:\n"); print_bytes(LOG_ERROR, buff, 42); if(buff[0] & PCRequestError) { logwrite(LOG_ERROR, "USB: request error"); } if(!(buff[1] & RequestDone)) { logwrite(LOG_ERROR, "USB: request is not done!"); } logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]); memcpy(&eepromBuff[8*index], &buff[3], 8); } #endif #define _WRITE #ifdef _WRITE memset(eepromBuff, 0xFF, 512); int commands = eeprom_data_size()/8; if(commands < 0 || commands > 62) { logwrite(LOG_ERROR, "WRONG COMMANDS NUMBER: %d", commands); exit(1); } logwrite(LOG_ERROR, "total commands: %d", commands); /* 2 bytes of sync + number of commands! */ eepromBuff[0] = 0xb0; eepromBuff[1] = 0x0b; eepromBuff[2] = commands; for (index=0;index < commands;index++) { fprintf(stderr, "w%d: eepromBuff[%d], eepromData[%d]\n", index, (index+1)*8, index*8); memcpy(&eepromBuff[(index+1)*8], &eepromData[8*index], 8); } for (index=0;index <= commands;index++) { addr = 8*index; memset(buff, 0x00, 42); buff[0] = PCTransfer; buff[1] = RequestEepromSectionWrite; buff[2] = (0xFF00 & addr) >> 8; //M:offset buff[3] = 0x00FF & addr; //L:offset buff[4] = 8; //bytes memcpy(&buff[5], &eepromBuff[8*index], 8); fprintf(stderr, "sending:\n"); print_bytes(LOG_ERROR, buff, 42); ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret); } ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret); } fprintf(stderr, "readed:\n"); print_bytes(LOG_ERROR, buff, 42); if(buff[0] & PCRequestError) { logwrite(LOG_ERROR, "USB: request error"); } if(!(buff[1] & RequestDone)) { logwrite(LOG_ERROR, "USB: request is not done!"); } logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]); } #endif ret = hid_close(hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_close failed with return code %d\n", ret); return 1; } hid_delete_HIDInterface(&hid); ret = hid_cleanup(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_cleanup failed with return code %d\n", ret); return 1; } #endif fprintf(stderr, "EEPROM DATA:\n"); for (index=0;index < 64;index++) { print_bytes(LOG_ERROR, &eepromBuff[8*index], 8); } return 0; }
int main (int argc, char *argv[]) { char buffer[64]; HIDInterface* hid; HIDInterfaceMatcher matcher = { CH_USB_VID, CH_USB_PID_FIRMWARE, NULL, NULL, 0 }; hid_return ret; int i; int iface_num = 0; int length; unsigned int timeout = 1000; /* ms */ hid_set_debug (HID_DEBUG_ALL); hid_set_debug_stream (stderr); hid_set_usb_debug (0); ret = hid_init (); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_init failed with return code %d\n", ret); return 1; } hid = hid_new_HIDInterface (); if (hid == 0) { fprintf (stderr, "hid_new_HIDInterface () failed\n"); return 1; } ret = hid_force_open (hid, iface_num, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_force_open failed with return code %d\n", ret); return 1; } ret = hid_write_identification (stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_write_identification failed with return code %d\n", ret); return 1; } ret = hid_dump_tree (stdout, hid); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_dump_tree failed with return code %d\n", ret); return 1; } /* turn all the LEDs on */ memset (buffer, 0x00, sizeof (buffer)); buffer[0] = 0x0e; buffer[1] = 0x03; ret = hid_interrupt_write (hid, 0x01, buffer, sizeof (buffer), timeout); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "failed calling hid_interrupt_write\n"); return 1; } /* read the return message */ length = 2; ret = hid_interrupt_read (hid, 0x01, buffer, length, timeout); if (ret != HID_RET_SUCCESS) { printf ("hid_interrupt_read failed\n"); return 1; } printf ("Data received from USB device: "); for (i=0; i < length; i++) { printf ("0x%02x, ", buffer[i]); } printf ("\n"); ret = hid_close (hid); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_close failed with return code %d\n", ret); return 1; } hid_delete_HIDInterface (&hid); ret = hid_cleanup (); if (ret != HID_RET_SUCCESS) { fprintf (stderr, "hid_cleanup failed with return code %d\n", ret); return 1; } return 0; }
int main(int argc, char** argp) { HIDInterface* hid; hid_return ret; char* const HEXPREFIX = "0x"; unsigned char const HEXPREFIXLEN = 2; unsigned char const HEXSTRINGLEN = 6; unsigned char const HEXNUMLEN = 4; char vendor_id_s[HEXSTRINGLEN + 1], product_id_s[HEXSTRINGLEN + 1]; unsigned short vendor_id = 0, product_id = 0; if (argc != 2 || strlen(argp[1]) != strlen("dead:beef")) { fprintf(stderr, "Usage: %s vendor_id:product_id\n", argp[0]); fprintf(stderr, " Eg: %s 06c2:0038\n", argp[0]); return 255; } memcpy(vendor_id_s, HEXPREFIX, HEXPREFIXLEN); strncpy(vendor_id_s + HEXPREFIXLEN, argp[1], HEXNUMLEN); vendor_id = 0xffff & strtol(vendor_id_s, NULL, 16); memcpy(product_id_s, HEXPREFIX, HEXPREFIXLEN); strncpy(product_id_s + HEXPREFIXLEN, argp[1] + HEXNUMLEN + 1, HEXNUMLEN); product_id = 0xffff & strtol(product_id_s, NULL, 16); fprintf(stderr, "Trying to detach HID with IDs %04x:%04x... ", vendor_id, product_id); HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 }; ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d.\n", ret); return 1; } hid = hid_new_HIDInterface(); if (hid == 0) { fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n"); return 1; } ret = hid_force_open(hid, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d.\n", ret); return 1; } ret = hid_close(hid); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_close failed with return code %d.\n", ret); return 1; } hid_delete_HIDInterface(&hid); ret = hid_cleanup(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_cleanup failed with return code %d.\n", ret); return 1; } fprintf(stderr, "done.\n"); return 0; }
int device_init(void) { struct usb_bus *usb_bus; struct usb_device *dev; usb_init(); int n=0; usb_find_busses(); usb_find_devices(); ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d \n", ret); return 1; } for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next) { for (dev = usb_bus->devices; dev; dev = dev->next) { if ((dev->descriptor.idVendor == VENDOR_ID) && (dev->descriptor.idProduct == PRODUCT_ID)) { /////////////////////////////////////////////// n++; usb_handle = usb_open(dev); int drstatus = usb_get_driver_np(usb_handle, 0, kdname,sizeof(kdname)); if (kdname != NULL && strlen(kdname) > 0) usb_detach_kernel_driver_np(usb_handle, 0); usb_reset(usb_handle); usb_close(usb_handle); HIDInterfaceMatcher matcher = { VENDOR_ID, PRODUCT_ID, NULL, NULL, 0 }; if (n==1) { hid1 = hid_new_HIDInterface(); if (hid1 != 0) { ret = hid_force_open(hid1, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); } } ////////////////////////////////////////////// } else // n=2 { hid2 = hid_new_HIDInterface(); if (hid2 != 0) { ret = hid_force_open(hid2, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); } ////////////////////////////////////////////// } } } } } return 0; }