From d434a9441ad258968c1c4d1b309fe361c0d082d3 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 19 Feb 2023 16:01:44 +0100 Subject: [PATCH] fix c code --- src/pcsensor.c | 143 +++++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 95 deletions(-) diff --git a/src/pcsensor.c b/src/pcsensor.c index 239630d..d4d51ed 100644 --- a/src/pcsensor.c +++ b/src/pcsensor.c @@ -14,8 +14,7 @@ /* TEMPer type definition */ -typedef struct -{ +typedef struct { const int vendor_id; const int product_id; const char product_name[256]; @@ -25,8 +24,7 @@ typedef struct void (*decode_func)(); } temper_type_t; -typedef struct -{ +typedef struct { libusb_device_handle *handle; temper_type_t *type; } temper_device_t; @@ -67,79 +65,61 @@ static libusb_context *ctx = NULL; /* functions */ -void bad(const char *why) -{ +void bad(const char *why) { fprintf(stderr, "Fatal error> %s\n", why); exit(17); } -void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) -{ +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) - { + if (ret) { + if (errno == ENODATA) { + if (debug) { fprintf(stderr, "Device already detached\n"); } - } - else - { - if (debug) - { + } else { + if (debug) { fprintf(stderr, "Detach failed: %s[%d]\n", strerror(errno), errno); fprintf(stderr, "Continuing anyway\n"); } } - } - else - { - if (debug) - { + } else { + if (debug) { fprintf(stderr, "detach successful\n"); } } } -int find_lvr_winusb(temper_device_t *devices) -{ +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) - { + if (cnt < 1) { fprintf(stderr, "Could not find USB device: %d\n", cnt); } numdev = 0; - for (i = 0; i < cnt && numdev < MAX_DEV; i++) - { + for (i = 0; i < cnt && numdev < MAX_DEV; i++) { struct libusb_device_descriptor desc; - if ((s = libusb_get_device_descriptor(devs[i], &desc)) < 0) - { + 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) - { + 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) - { + if ((s = libusb_open(devs[i], &devices[numdev].handle)) < 0) { fprintf(stderr, "Could not open USB device: %d\n", s); continue; } @@ -148,26 +128,19 @@ int find_lvr_winusb(temper_device_t *devices) 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) - { + 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 - { + } else { // vid and pid match, but product name unmatch libusb_close(devices[numdev].handle); continue; } - } - else - { + } else { devices[numdev].type = &tempers[j]; } - if (debug) - { + 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); } @@ -182,16 +155,14 @@ int find_lvr_winusb(temper_device_t *devices) return numdev; } -int setup_libusb_access(temper_device_t *devices) -{ +int setup_libusb_access(temper_device_t *devices) { int i; int log_level = 0; int numdev; libusb_init(&ctx); - if (debug) - { + if (debug) { log_level = 4; } #if LIBUSBX_API_VERSION < 0x01000106 @@ -200,33 +171,28 @@ int setup_libusb_access(temper_device_t *devices) libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, log_level); #endif - if ((numdev = find_lvr_winusb(devices)) < 1) - { + if ((numdev = find_lvr_winusb(devices)) < 1) { fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev); return -1; } - for (i = 0; i < numdev; i++) - { + for (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) - { + 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) - { + 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) - { + if ((s = libusb_claim_interface(devices[i].handle, INTERFACE2)) < 0) { fprintf(stderr, "Could not claim interface. Error:%d\n", s); return -1; } @@ -235,28 +201,26 @@ int setup_libusb_access(temper_device_t *devices) return numdev; } -void ini_control_transfer(libusb_device_handle *dev) -{ +void ini_control_transfer(libusb_device_handle *dev) { int r, i; - char question[] = {0x01, 0x01}; + char question[reqIntLen]; + question[0] = 0x01; + question[1] = 0x01; r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (unsigned char *)question, 2, timeout); - if (r < 0) - { + if (r < 0) { perror("USB control write"); bad("USB write failed"); } - if (debug) - { + if (debug) { for (i = 0; i < reqIntLen; i++) fprintf(stderr, "%02x ", question[i] & 0xFF); fprintf(stderr, "\n"); } } -void control_transfer(libusb_device_handle *dev, const char *pquestion) -{ +void control_transfer(libusb_device_handle *dev, const char *pquestion) { int r, i; char question[reqIntLen]; @@ -264,35 +228,30 @@ void control_transfer(libusb_device_handle *dev, const char *pquestion) memcpy(question, pquestion, sizeof question); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *)question, reqIntLen, timeout); - if (r < 0) - { + if (r < 0) { perror("USB control write"); bad("USB write failed"); } - if (debug) - { + 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) -{ +void interrupt_read(libusb_device_handle *dev, unsigned char *answer) { int r, s, i; memset(answer, 0, reqIntLen); s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); - if (r != reqIntLen) - { + if (r != reqIntLen) { fprintf(stderr, "USB read failed: %d\n", s); perror("USB interrupt read"); bad("USB read failed"); } - if (debug) - { + if (debug) { for (i = 0; i < reqIntLen; i++) fprintf(stderr, "%02x ", answer[i] & 0xFF); @@ -300,8 +259,7 @@ void interrupt_read(libusb_device_handle *dev, unsigned char *answer) } } -void cleanup_usb_devices(temper_device_t *devices, int numdev) -{ +void cleanup_usb_devices(temper_device_t *devices, int numdev) { int i; for (i = 0; i < numdev; i++) @@ -315,8 +273,7 @@ void cleanup_usb_devices(temper_device_t *devices, int numdev) libusb_exit(ctx); } -void ex_program() -{ +void ex_program() { bsalir = 1; (void)signal(SIGINT, SIG_DFL); @@ -325,8 +282,7 @@ void ex_program() /* decode funcs */ /* Thanks to https://github.com/edorfaus/TEMPered */ -void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration) -{ +void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration) { int buf; // temp C internal @@ -340,8 +296,7 @@ void decode_answer_fm75(unsigned char *answer, float *tempd, float *calibration) tempd[1] = tempd[1] * calibration[0] + calibration[1]; } -void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration) -{ +void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration) { int buf; // temp C @@ -359,8 +314,7 @@ void decode_answer_sht1x(unsigned char *answer, float *tempd, float *calibration tempd[1] = 100; } -float get_temp_c() -{ +float get_temp_c() { temper_device_t *devices; int numdev, i; unsigned char *answer; @@ -368,8 +322,7 @@ float get_temp_c() float calibration[2] = {1, 0}; devices = calloc(MAX_DEV, sizeof(temper_device_t)); - if ((numdev = setup_libusb_access(devices)) < 1) - { + if ((numdev = setup_libusb_access(devices)) < 1) { exit(EXIT_FAILURE); }