/******************************************************* Windows HID simplification Alan Ott Signal 11 Software 8/22/2009 Copyright 2009, All Rights Reserved. This contents of this file may be used by anyone for any reason without any conditions and may be used as a starting point for your own applications which use HIDAPI. ********************************************************/ #include #include #include #include #include "hidapi.h" // Headers needed for sleeping. #ifdef _WIN32 #include #else #include #endif int verbose=0; int main(int argc, char* argv[]) { int res; unsigned char buf[256]; #define MAX_STR 255 wchar_t wstr[MAX_STR]; hid_device *handle; int i; if (argc >= 2 && !strcmp(argv[1], "-v")) verbose=1; #ifdef WIN32 UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); #endif struct hid_device_info *devs, *cur_dev; devs = hid_enumerate(0x0, 0x0); cur_dev = devs; while (cur_dev) { if (verbose) printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number); if (verbose) printf("\n"); if (verbose) printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); if (verbose) printf(" Product: %ls\n", cur_dev->product_string); if (verbose) printf(" Release: %hx\n", cur_dev->release_number); if (verbose) printf(" Interface: %d\n", cur_dev->interface_number); if (verbose) printf("\n"); cur_dev = cur_dev->next; } hid_free_enumeration(devs); // Set up the command buffer. memset(buf,0x00,sizeof(buf)); buf[0] = 0x01; buf[1] = 0x81; // Open the device using the VID, PID, // and optionally the Serial number. ////handle = hid_open(0x4d8, 0x3f, L"12345"); //handle = hid_open(0x4d8, 0x3f, NULL); handle = hid_open(0x1da8, 0x1301, NULL); if (!handle) { if (verbose) printf("unable to open device\n"); return 1; } // Read the Manufacturer String wstr[0] = 0x0000; res = hid_get_manufacturer_string(handle, wstr, MAX_STR); if (res < 0) if (verbose) printf("Unable to read manufacturer string\n"); if (verbose) printf("Manufacturer String: %ls\n", wstr); // Read the Product String wstr[0] = 0x0000; res = hid_get_product_string(handle, wstr, MAX_STR); if (res < 0) if (verbose) printf("Unable to read product string\n"); if (verbose) printf("Product String: %ls\n", wstr); // Read the Serial Number String wstr[0] = 0x0000; res = hid_get_serial_number_string(handle, wstr, MAX_STR); if (res < 0) if (verbose) printf("Unable to read serial number string\n"); if (verbose) printf("Serial Number String: (%d) %ls", wstr[0], wstr); if (verbose) printf("\n"); // Read Indexed String 1 wstr[0] = 0x0000; res = hid_get_indexed_string(handle, 1, wstr, MAX_STR); if (res < 0) if (verbose) printf("Unable to read indexed string 1\n"); if (verbose) printf("Indexed String 1: %ls\n", wstr); // Set the hid_read() function to be non-blocking. //hid_set_nonblocking(handle, 1); // Try to read from the device. There shoud be no // data here, but execution should not block. res = hid_read(handle, buf, 17); // Send a Feature Report to the device buf[0] = 0x2; buf[1] = 0xa0; buf[2] = 0x0a; buf[3] = 0x00; buf[4] = 0x00; res = hid_send_feature_report(handle, buf, 17); if (res < 0) { if (verbose) printf("Unable to send a feature report.\n"); } memset(buf,0,sizeof(buf)); // Read a Feature Report from the device buf[0] = 0x2; res = hid_get_feature_report(handle, buf, sizeof(buf)); if (res < 0) { if (verbose) printf("Unable to get a feature report.\n"); if (verbose) printf("%ls", hid_error(handle)); } else { // Print out the returned buffer. if (verbose) printf("Feature Report\n "); for (i = 0; i < res; i++) if (verbose) printf("%02hhx ", buf[i]); if (verbose) printf("\n"); } memset(buf,0,sizeof(buf)); #if 0 // Toggle LED (cmd 0x80). The first byte is the report number (0x1). buf[0] = 0x1; buf[1] = 0x80; res = hid_write(handle, buf, 17); if (res < 0) { if (verbose) printf("Unable to write()\n"); if (verbose) printf("Error: %ls\n", hid_error(handle)); } // Request state (cmd 0x81). The first byte is the report number (0x1). buf[0] = 0x1; buf[1] = 0x81; hid_write(handle, buf, 17); if (res < 0) if (verbose) printf("Unable to write() (2)\n"); #endif // endormi buf[0] = 0x03; buf[1] = 0x01; // eveil //buf[0] = 0x03; //buf[1] = 0x03; hid_write(handle, buf, 17); //hid_set_nonblocking(handle, 0); while(1) { // Read requested state. hid_read() has been set to be // non-blocking by the call to hid_set_nonblocking() above. // This loop demonstrates the non-blocking nature of hid_read(). res = 0; while (res == 0) { res = hid_read(handle, buf, sizeof(buf)); if (res == 0) if (verbose) printf("waiting...\n"); if (res < 0) if (verbose) printf("Unable to read()\n"); // usleep(500*1000); } if (res < 0) break; if (buf[0] == 0x00) continue; #if 0 printf("Data read: %d\n ", res); // Print out the returned buffer. for (i = 0; i < res; i++) printf("%02hhx ", buf[i]); printf("\n"); #else if (res == 64) { printf("Tag identified "); char response[1]; char url[256]; for(i=5; i<13; i++){ printf("%02hhx", buf[i]); response[i-5]=buf[i]; } if (buf[1] == 1){ printf(" IN"); sprintf(url,"~/Library/\"Application Support\"/\"Mirror By LCProd\"/mirror_tags.sh show %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" ,response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7]); system(url); } if (buf[1] == 2){ printf(" OUT"); sprintf(url,"~/Library/\"Application Support\"/\"Mirror By LCProd\"/mirror_tags.sh hide %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" ,response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7]); system(url); } //printf("%s",url); //system(url); //printf(" TAG N° %d \n", buf[8]); /* if (buf[1] == 1 && buf[8] == 5) { printf(" IN 5"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh show blue"+response[0]); } if (buf[1] == 1 && buf[8] == 3) { printf(" IN 3"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh show grey"); } if (buf[1] == 1 && buf[8] == 193) { printf(" IN 1"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh show tag_orange"); } if (buf[1] == 2 && buf[8] == 5) { printf(" OUT 5"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh hide blue"); } if (buf[1] == 2 && buf[8] == 3) { printf(" OUT 3"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh hide grey"); } if (buf[1] == 2 && buf[8] == 193) { printf(" OUT 1"); system("~/Library/\"Application Support\"/Mirror/actions_do.sh hide tag_orange"); } */ printf("\n"); fflush(stdout); } #endif } hid_close(handle); /* Free static HIDAPI objects. */ hid_exit(); #ifdef WIN32 system("pause"); #endif return 0; }