From 1fc407cac055599496c8bda955e0a3689e7c79ed Mon Sep 17 00:00:00 2001 From: Peter Farsinsen Date: Thu, 13 Sep 2012 20:03:51 +0200 Subject: [PATCH] Initial commit of pcsensor v. 1.0.1 --- 99-tempsensor.rules | 1 + Makefile | 12 ++ pcsensor | Bin 0 -> 17090 bytes pcsensor.c | 450 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 463 insertions(+) create mode 100644 99-tempsensor.rules create mode 100644 Makefile create mode 100755 pcsensor create mode 100644 pcsensor.c diff --git a/99-tempsensor.rules b/99-tempsensor.rules new file mode 100644 index 0000000..30a2e1e --- /dev/null +++ b/99-tempsensor.rules @@ -0,0 +1 @@ +SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="7401", MODE="666" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0557550 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +all: pcsensor + +CFLAGS = -O2 -Wall + +pcsensor: pcsensor.c + ${CC} -DUNIT_TEST -o $@ $^ -lusb + +clean: + rm -f pcsensor *.o + +rules-install: # must be superuser to do this + cp 99-tempsensor.rules /etc/udev/rules.d diff --git a/pcsensor b/pcsensor new file mode 100755 index 0000000000000000000000000000000000000000..ea22415784610780d5666d1afd7dbbda6ff4db40 GIT binary patch literal 17090 zcmeHO4|G)3nZHRUc8bA73^lsgK4~JTAqE-|sICDr{Lvs36U71!lgUeFmdQ+%fIn>fFY>`bBcPCBMsA*v>HT(PB`|jk; z1GVkhb9VO}?woJ#{oVWB?|%2Y|K5A|-iJK(^YZfYgm(F&KoE64=5p7-KRQ*V)Cfa- zQcM=th^xgVh+j#QYE|Hja0h@XmLW70*qfVAD;Z<|(0III*rO7O9%1;-R|CKmV>qnV0oonV$rq z`l1>%9`p%NDd@8xrZFE~5vUAw0|DG;K+H!s0dy6}O#lZ~M5PP~aV`9ED@^@AZH4KL z0Zp{<9_h;8CD_3jiFI3$3*s5?TEKI@H-B? z!G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&?Ngag0k!0$TnWf-X6f_-U{art9K1u*X@d>KCLa|3+)ox5sa z?P$O3HeHopM9l{lhU4|Pi-yn~?E5i@{mS+TUQfJ8!dc}dRNzZ2MXYV*Y-g46>cyIImY;TJy&e>dv4gS-$GY3x#qURb8yh&G)gl8oe#;-oRRsE6O{% z)yRu+IrsNQ<=-N*x9vtFt8KRrh0@l#Y}5aL0yNm$3V4M#2dgVfRKqxnBMki1Y5gpQX~1! zPMNxjyk_e4)-^0%>h;`J$Lp@6{Yx^dtCM#5%%0mv1o!W~S!*@nvZ}g+Y0qX|!n8+m z1lzIw-le>&X-^z2GFy-wki0x<=hHRIa><*bc8=T%sKXBEb+|}s=d$jCW*b0`D +#include +#include + +#include +#include +#include + + +#define VERSION "1.0.1" + +#define VENDOR_ID 0x0c45 +#define PRODUCT_ID 0x7401 + +#define INTERFACE1 0x00 +#define INTERFACE2 0x01 + +const static int reqIntLen=8; +const static int reqBulkLen=8; +const static int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */ +const static int endpoint_Int_out=0x00; /* endpoint 1 address for OUT */ +const static int endpoint_Bulk_in=0x82; /* endpoint 0x81 address for IN */ +const static int endpoint_Bulk_out=0x00; /* endpoint 1 address for OUT */ +const static int timeout=5000; /* timeout in ms */ + +const static char uTemperatura[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 }; +const static char uIni1[] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 }; +const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 }; + +static int bsalir=1; +static int debug=0; +static int seconds=5; +static int formato=0; +static int mrtg=0; +static int calibration=0; + + +void bad(const char *why) { + fprintf(stderr,"Fatal error> %s\n",why); + exit(17); +} + + +usb_dev_handle *find_lvr_winusb(); + +void usb_detach(usb_dev_handle *lvr_winusb, int iInterface) { + int ret; + + ret = usb_detach_kernel_driver_np(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"); + } + } +} + +usb_dev_handle* setup_libusb_access() { + usb_dev_handle *lvr_winusb; + + if(debug) { + usb_set_debug(255); + } else { + usb_set_debug(0); + } + usb_init(); + usb_find_busses(); + usb_find_devices(); + + + if(!(lvr_winusb = find_lvr_winusb())) { + printf("Couldn't find the USB device, Exiting\n"); + return NULL; + } + + + usb_detach(lvr_winusb, INTERFACE1); + + + usb_detach(lvr_winusb, INTERFACE2); + + + if (usb_set_configuration(lvr_winusb, 0x01) < 0) { + printf("Could not set configuration 1\n"); + return NULL; + } + + + // Microdia tiene 2 interfaces + if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) { + printf("Could not claim interface\n"); + return NULL; + } + + if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) { + printf("Could not claim interface\n"); + return NULL; + } + + return lvr_winusb; +} + + + +usb_dev_handle *find_lvr_winusb() { + + struct usb_bus *bus; + struct usb_device *dev; + + for (bus = usb_busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.idVendor == VENDOR_ID && + dev->descriptor.idProduct == PRODUCT_ID ) { + usb_dev_handle *handle; + if(debug) { + printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID); + } + + if (!(handle = usb_open(dev))) { + printf("Could not open USB device\n"); + return NULL; + } + return handle; + } + } + } + return NULL; +} + + +void ini_control_transfer(usb_dev_handle *dev) { + int r,i; + + char question[] = { 0x01,0x01 }; + + r = usb_control_msg(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout); + if( r < 0 ) + { + perror("USB control write"); bad("USB write failed"); + } + + + if(debug) { + for (i=0;itm_hour, + local->tm_min); + + printf("pcsensor\n"); + } else { + printf("%04d/%02d/%02d %02d:%02d:%02d ", + local->tm_year +1900, + local->tm_mon + 1, + local->tm_mday, + local->tm_hour, + local->tm_min, + local->tm_sec); + + if (formato==2) { + printf("Temperature %.2fF\n", (9.0 / 5.0 * tempc + 32.0)); + } else if (formato==1) { + printf("Temperature %.2fC\n", tempc); + } else { + printf("Temperature %.2fF %.2fC\n", (9.0 / 5.0 * tempc + 32.0), tempc); + } + } + + if (!bsalir) + sleep(seconds); + } while (!bsalir); + + usb_release_interface(lvr_winusb, INTERFACE1); + usb_release_interface(lvr_winusb, INTERFACE2); + + usb_close(lvr_winusb); + + return 0; +}