#include #include #include #include #include #include #include #include #include #include #include #define VERSION "1.2.0" /* TEMPer type definition */ typedef struct { const int vendor_id; const int product_id; const char product_name[256]; const int check_product_name; // if set, check product name in forward match const int has_sensor; // number of temperature sensor const int has_humid; // flag for humidity sensor void (*decode_func)(); float divisor; } temper_type_t; typedef struct { libusb_device_handle *handle; temper_type_t *type; } temper_device_t; void decode_answer(); #define TEMPER_TYPES 4 const char GetFirmware[8] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 }; const char GetTemperature[8] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 }; //#define Temperature { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 } //const char uTemperature1[8] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 }; //const char uTemperature2[8] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 }; //const char uTemperature3[8] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 }; temper_type_t tempers[TEMPER_TYPES] = { {0x1a86, 0xe025, "TEMPerGold", 1, 1, 0, decode_answer, 100.0}, {0x0c45, 0x7401, "TEMPer2", 1, 2, 0, decode_answer, 256.0}, {0x0c45, 0x7401, "TEMPer1", 0, 1, 0, decode_answer, 256.0}, //{0x0c45, 0x7402, "TEMPerHUM", 0, 1, 1, decode_answer_sht1x, 100.0}, }; /* memo: TEMPer2 cannot be distinguished with VID:PID, thus product name (like TEMPer2V1.3) should be checked. */ /* global variables */ #define MAX_DEV 8 #define INTERFACE1 0x00 #define INTERFACE2 0x01 const int reqIntLen = 8; const int endpoint_Int_out = 0x02; const int endpoint_Int_in = 0x82; const int timeout = 300; /* timeout in ms */ static int bsalir = 1; static int debug = 0; static libusb_context *ctx = NULL; /* functions */ void bad(const char *why) { fprintf(stderr, "Fatal error> %s\n", why); exit(17); } void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) { int ret; ret = libusb_detach_kernel_driver(lvr_winusb, iInterface); if (ret) { if (errno == ENODATA) { if (debug) { fprintf(stderr, "Device already detached\n"); } } else { if (debug) { fprintf(stderr, "Detach failed: %s[%d]\n", strerror(errno), errno); fprintf(stderr, "Continuing anyway\n"); } } } else { if (debug) { fprintf(stderr, "detach successful\n"); } } } int find_lvr_winusb(temper_device_t *devices) { int i, j, s, cnt, numdev; libusb_device **devs; //handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID); cnt = libusb_get_device_list(ctx, &devs); if (cnt < 1) { fprintf(stderr, "Could not find USB device: %d\n", cnt); } numdev = 0; for (i = 0; i < cnt && numdev < MAX_DEV; i++) { struct libusb_device_descriptor desc; if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0) { fprintf(stderr, "Could not get USB device descriptor: %d\n", s); continue; } for (j = 0; j < TEMPER_TYPES; j++) { if (desc.idVendor == tempers[j].vendor_id && desc.idProduct == tempers[j].product_id) { unsigned char bus, addr, descmanu[256], descprod[256], descseri[256]; bus = libusb_get_bus_number(devs[i]); addr = libusb_get_device_address(devs[i]); if ((s = libusb_open(devs[i], &devices[numdev].handle)) < 0) { fprintf(stderr, "Could not open USB device: %d\n", s); continue; } libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iManufacturer, descmanu, 256); libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iProduct, descprod, 256); libusb_get_string_descriptor_ascii(devices[numdev].handle, desc.iSerialNumber, descseri, 256); if (tempers[j].check_product_name) { if (strncmp((const char *)descprod, tempers[j].product_name, strlen(tempers[j].product_name)) == 0) { devices[numdev].type = &tempers[j]; } else { // vid and pid match, but product name unmatch libusb_close(devices[numdev].handle); continue; } } else { devices[numdev].type = &tempers[j]; } if (debug) { fprintf(stderr, "lvr_winusb with Bus:%03d Addr:%03d VendorID:%04x ProductID:%04x Manufacturer:%s Product:%s Serial:%s found.\n", bus, addr, desc.idVendor, desc.idProduct, descmanu, descprod, descseri); } numdev++; } } } libusb_free_device_list(devs, 1); return numdev; } int setup_libusb_access(temper_device_t *devices) { int log_level = 0; int numdev; libusb_init(&ctx); if (debug) { log_level = 4; } #if LIBUSBX_API_VERSION < 0x01000106 libusb_set_debug(ctx, log_level); #else libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, log_level); #endif if ((numdev = find_lvr_winusb(devices)) < 1) { fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev); return -1; } for (int i = 0; i < numdev; i++) { usb_detach(devices[i].handle, INTERFACE1); usb_detach(devices[i].handle, INTERFACE2); //libusb_reset_device(devices[i].handle); if (libusb_set_configuration(devices[i].handle, 0x01) < 0) { fprintf(stderr, "Could not set configuration 1\n"); return -1; } int s; if ((s = libusb_claim_interface(devices[i].handle, INTERFACE1)) < 0) { fprintf(stderr, "Could not claim interface. Error:%d\n", s); return -1; } if ((s = libusb_claim_interface(devices[i].handle, INTERFACE2)) < 0) { fprintf(stderr, "Could not claim interface. Error:%d\n", s); return -1; } } return numdev; } void control_transfer(libusb_device_handle *dev, const char *pquestion) { int r, i; char question[reqIntLen]; memcpy(question, pquestion, sizeof question); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *)question, reqIntLen, timeout); if (r < 0) { perror("USB control write"); return; } if (debug) { for (i = 0; i < reqIntLen; i++) fprintf(stderr, "%02x ", question[i] & 0xFF); fprintf(stderr, "\n"); } } void interrupt_read(libusb_device_handle *dev, unsigned char *answer, const char *pquestion, const char *product_name) { int r, s, i; char question[reqIntLen]; memset(answer, 0, reqIntLen); memcpy(question, pquestion, sizeof question); s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); if (strcmp(product_name, "TEMPerGold") == 0) { libusb_interrupt_transfer(dev, endpoint_Int_out, (unsigned char *)question, reqIntLen, &r, timeout); s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); } if (s != 0) { fprintf(stderr, "USB read failed: %s\n", libusb_error_name(s)); perror("USB interrupt read"); exit(1); return; } if (debug) { for (i = 0; i < reqIntLen; i++) fprintf(stderr, "%02x ", answer[i] & 0xFF); fprintf(stderr, "\n"); } } void cleanup_usb_devices(temper_device_t *devices, int numdev) { for (int i = 0; i < numdev; i++) { libusb_release_interface(devices[i].handle, INTERFACE1); libusb_release_interface(devices[i].handle, INTERFACE2); libusb_attach_kernel_driver(devices[i].handle, INTERFACE1); libusb_attach_kernel_driver(devices[i].handle, INTERFACE2); libusb_close(devices[i].handle); } libusb_exit(ctx); } void ex_program() { bsalir = 1; (void)signal(SIGINT, SIG_DFL); } void decode_answer(unsigned char *answer, float *divisor, float *tempd, float *calibration) { int buf; // temp C internal buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF); tempd[0] = buf / *divisor; tempd[0] = tempd[0] * calibration[0] + calibration[1]; // temp C external buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF); tempd[1] = buf / *divisor; tempd[1] = tempd[1] * calibration[0] + calibration[1]; } /* void decode_answer_sht1x(unsigned char *answer, float *divisor, float *tempd, float *calibration) { int buf; // temp C buf = ((signed char)answer[2] << 8) + (answer[3] & 0xFF); tempd[0] = -39.7 + 0.01 * buf; tempd[0] = tempd[0] * calibration[0] + calibration[1]; // relative humidity buf = ((signed char)answer[4] << 8) + (answer[5] & 0xFF); tempd[1] = -2.0468 + 0.0367 * buf - 1.5955e-6 * buf * buf; tempd[1] = (tempd[0] - 25) * (0.01 + 0.00008 * buf) + tempd[1]; if (tempd[1] < 0) tempd[1] = 0; if (tempd[1] > 99) tempd[1] = 100; } */ float get_temp_c() { temper_device_t *devices; int numdev; int i = 0; unsigned char *answer; float tempd[2]; float calibration[2] = {1, 0}; devices = calloc(MAX_DEV, sizeof(temper_device_t)); if ((numdev = setup_libusb_access(devices)) < 1) { exit(EXIT_FAILURE); } (void)signal(SIGINT, ex_program); answer = calloc(reqIntLen, sizeof(unsigned char)); control_transfer(devices[i].handle, GetTemperature); interrupt_read(devices[i].handle, answer, GetTemperature, devices[i].type->product_name); devices[i].type->decode_func(answer, &devices[i].type->divisor, tempd, calibration); cleanup_usb_devices(devices, numdev); return tempd[0]; }