diff --git a/pcsensor.c b/pcsensor.c index 2349d51..eba9d3f 100644 --- a/pcsensor.c +++ b/pcsensor.c @@ -66,91 +66,86 @@ static int calibration=0; void bad(const char *why) { - fprintf(stderr,"Fatal error> %s\n",why); - exit(17); + fprintf(stderr,"Fatal error> %s\n",why); + exit(17); } libusb_context *ctx = NULL; libusb_device_handle *find_lvr_winusb(); void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) { - int ret; + int ret; - ret = libusb_detach_kernel_driver(lvr_winusb, iInterface); - if(ret) { - if(errno == ENODATA) { - if(debug) { - printf("Device already detached\n"); - } - } else { - if(debug) { - printf("Detach failed: %s[%d]\n", - strerror(errno), errno); - printf("Continuing anyway\n"); - } - } - } else { - if(debug) { - printf("detach successful\n"); - } - } + ret = libusb_detach_kernel_driver(lvr_winusb, iInterface); + if(ret) { + if(errno == ENODATA) { + if(debug) { + printf("Device already detached\n"); + } + } else { + if(debug) { + printf("Detach failed: %s[%d]\n", + strerror(errno), errno); + printf("Continuing anyway\n"); + } + } + } else { + if(debug) { + printf("detach successful\n"); + } + } } libusb_device_handle* setup_libusb_access() { - libusb_device_handle *lvr_winusb; + libusb_device_handle *lvr_winusb; - libusb_init(&ctx); + libusb_init(&ctx); - if(debug) { + if(debug) { libusb_set_debug(ctx, 4); //LIBUSB_LOG_LEVEL_DEBUG - } else { + } else { libusb_set_debug(ctx, 0); //LIBUSB_LOG_LEVEL_NONE - } + } - if(!(lvr_winusb = find_lvr_winusb())) { - fprintf(stderr, "Couldn't find the USB device, Exiting\n"); - return NULL; - } + if(!(lvr_winusb = find_lvr_winusb())) { + fprintf(stderr, "Couldn't find the USB device, Exiting\n"); + return NULL; + } + usb_detach(lvr_winusb, INTERFACE1); + usb_detach(lvr_winusb, INTERFACE2); - usb_detach(lvr_winusb, INTERFACE1); + if (libusb_set_configuration(lvr_winusb, 0x01) < 0) { + fprintf(stderr, "Could not set configuration 1\n"); + return NULL; + } + // Microdia tiene 2 interfaces + int s; + if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE1) ) != 0) { + fprintf(stderr, "Could not claim interface. Error:%d\n", s); + return NULL; + } - usb_detach(lvr_winusb, INTERFACE2); + if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE2) ) != 0) { + fprintf(stderr, "Could not claim interface. Error:%d\n", s); + return NULL; + } - - if (libusb_set_configuration(lvr_winusb, 0x01) < 0) { - fprintf(stderr, "Could not set configuration 1\n"); - return NULL; - } - - - // Microdia tiene 2 interfaces - int errno; - if ( ( errno = libusb_claim_interface(lvr_winusb, INTERFACE1) ) != 0) { - fprintf(stderr, "Could not claim interface. Error:%d\n", errno); - return NULL; - } - - if ( ( errno = libusb_claim_interface(lvr_winusb, INTERFACE2) ) != 0) { - fprintf(stderr, "Could not claim interface. Error:%d\n", errno); - return NULL; - } - - return lvr_winusb; + return lvr_winusb; } libusb_device_handle *find_lvr_winusb() { - libusb_device_handle *handle; + libusb_device_handle *handle; - handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID); - if (!handle) { - fprintf(stderr, "Could not open USB device\n"); - return NULL; - } - return handle; + handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID); + if (!handle) { + fprintf(stderr, "Could not open USB device\n"); + return NULL; + } + return handle; } @@ -160,15 +155,13 @@ void ini_control_transfer(libusb_device_handle *dev) { char question[] = { 0x01,0x01 }; r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout); - if( r < 0 ) - { - perror("USB control write"); bad("USB write failed"); + if( r < 0 ) { + perror("USB control write"); bad("USB write failed"); } - if(debug) { - for (i=0;itm_hour, + local->tm_min); - if (mrtg) { - if (formato==2) { - printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0)); - printf("%.2f\n", (9.0 / 5.0 * tempOutC + 32.0)); - } else { - printf("%.2f\n", tempInC); - printf("%.2f\n", tempOutC); - } + printf("pcsensor\n"); + } else { + printf("%04d/%02d/%02d %02d:%02d:%02d\n", + local->tm_year +1900, + local->tm_mon + 1, + local->tm_mday, + local->tm_hour, + local->tm_min, + local->tm_sec); - printf("%02d:%02d\n", - local->tm_hour, - local->tm_min); + if (formato==2) { + printf("Temperature (internal) %.2fF\n", (9.0 / 5.0 * tempInC + 32.0)); + printf("Temperature (external) %.2fF\n", (9.0 / 5.0 * tempOutC + 32.0)); + } else if (formato==1) { + printf("Temperature (internal) %.2fC\n", tempInC); + printf("Temperature (external) %.2fC\n", tempOutC); + } else { + printf("Temperature (internal) %.2fF %.2fC\n", (9.0 / 5.0 * tempInC + 32.0), tempInC); + printf("Temperature (external) %.2fF %.2fC\n", (9.0 / 5.0 * tempOutC + 32.0), tempOutC); + } + } - printf("pcsensor\n"); - } else { - printf("%04d/%02d/%02d %02d:%02d:%02d\n", - local->tm_year +1900, - local->tm_mon + 1, - local->tm_mday, - local->tm_hour, - local->tm_min, - local->tm_sec); + if (!bsalir) + sleep(seconds); + } while (!bsalir); - if (formato==2) { - printf("Temperature (internal) %.2fF\n", (9.0 / 5.0 * tempInC + 32.0)); - printf("Temperature (external) %.2fF\n", (9.0 / 5.0 * tempOutC + 32.0)); - } else if (formato==1) { - printf("Temperature (internal) %.2fC\n", tempInC); - printf("Temperature (external) %.2fC\n", tempOutC); - } else { - printf("Temperature (internal) %.2fF %.2fC\n", (9.0 / 5.0 * tempInC + 32.0), tempInC); - printf("Temperature (external) %.2fF %.2fC\n", (9.0 / 5.0 * tempOutC + 32.0), tempOutC); - } - } + libusb_release_interface(lvr_winusb, INTERFACE1); + libusb_release_interface(lvr_winusb, INTERFACE2); - if (!bsalir) - sleep(seconds); - } while (!bsalir); + libusb_close(lvr_winusb); - libusb_release_interface(lvr_winusb, INTERFACE1); - libusb_release_interface(lvr_winusb, INTERFACE2); - - libusb_close(lvr_winusb); - - return 0; + return 0; }