2012-09-13 20:03:51 +02:00
|
|
|
/*
|
|
|
|
* pcsensor.c by Juan Carlos Perez (c) 2011 (cray@isp-sl.com)
|
|
|
|
* based on Temper.c by Robert Kavaler (c) 2009 (relavak.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 2011/08/30 Thanks to EdorFaus: bugfix to support negative temperatures
|
2017-08-31 09:57:03 +09:00
|
|
|
* 2017/08/30 Improved by K.Cima: changed libusb-0.1 -> libusb-1.0
|
|
|
|
* https://github.com/shakemid/pcsensor
|
2012-09-13 20:03:51 +02:00
|
|
|
*
|
|
|
|
* Temper driver for linux. This program can be compiled either as a library
|
|
|
|
* or as a standalone program (-DUNIT_TEST). The driver will work with some
|
|
|
|
* TEMPer usb devices from RDing (www.PCsensor.com).
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY Juan Carlos Perez ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL Robert kavaler BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-08-30 17:08:18 +09:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2017-08-31 11:08:33 +09:00
|
|
|
#include <ctype.h>
|
2012-09-13 20:03:51 +02:00
|
|
|
#include <time.h>
|
2017-08-31 11:10:11 +09:00
|
|
|
#include <string.h>
|
2017-08-30 17:08:18 +09:00
|
|
|
#include <strings.h>
|
2012-09-13 20:03:51 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2017-08-30 17:08:18 +09:00
|
|
|
#include <libusb.h>
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:57:03 +09:00
|
|
|
#define VERSION "1.1.0"
|
2012-09-13 20:03:51 +02:00
|
|
|
|
|
|
|
#define VENDOR_ID 0x0c45
|
|
|
|
#define PRODUCT_ID 0x7401
|
|
|
|
|
|
|
|
#define INTERFACE1 0x00
|
|
|
|
#define INTERFACE2 0x01
|
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
#define MAX_DEV 8
|
|
|
|
|
2012-09-13 20:03:51 +02:00
|
|
|
const static int reqIntLen=8;
|
|
|
|
const static int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
|
|
|
|
const static int timeout=5000; /* timeout in ms */
|
|
|
|
|
2017-08-31 17:16:47 +09:00
|
|
|
const static char uTemperature[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
2012-09-13 20:03:51 +02:00
|
|
|
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;
|
2017-09-14 11:56:09 +09:00
|
|
|
static int formato=1; //Celsius
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
static libusb_context *ctx = NULL;
|
2012-09-13 20:03:51 +02:00
|
|
|
|
|
|
|
void bad(const char *why) {
|
2017-08-31 09:56:15 +09:00
|
|
|
fprintf(stderr,"Fatal error> %s\n",why);
|
|
|
|
exit(17);
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 17:08:18 +09:00
|
|
|
void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
2017-08-31 09:56:15 +09:00
|
|
|
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) {
|
2017-08-31 16:01:57 +09:00
|
|
|
printf("Detach failed: %s[%d]\n", strerror(errno), errno);
|
2017-08-31 09:56:15 +09:00
|
|
|
printf("Continuing anyway\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(debug) {
|
|
|
|
printf("detach successful\n");
|
|
|
|
}
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
int find_lvr_winusb(libusb_device_handle **handles) {
|
|
|
|
int i, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID) {
|
|
|
|
if(debug) {
|
|
|
|
printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s = libusb_open(devs[i], &handles[numdev])) < 0) {
|
|
|
|
fprintf(stderr, "Could not open USB device: %d\n", s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
numdev++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_free_device_list(devs, 1);
|
|
|
|
|
|
|
|
return numdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int setup_libusb_access(libusb_device_handle **handles) {
|
|
|
|
int i,numdev;
|
2017-08-30 17:08:18 +09:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
libusb_init(&ctx);
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
if(debug) {
|
2017-08-30 17:08:18 +09:00
|
|
|
libusb_set_debug(ctx, 4); //LIBUSB_LOG_LEVEL_DEBUG
|
2017-08-31 09:56:15 +09:00
|
|
|
} else {
|
2017-08-30 17:08:18 +09:00
|
|
|
libusb_set_debug(ctx, 0); //LIBUSB_LOG_LEVEL_NONE
|
2017-08-31 09:56:15 +09:00
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
if((numdev = find_lvr_winusb(handles)) < 1) {
|
|
|
|
fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev);
|
|
|
|
return -1;
|
2017-08-31 09:56:15 +09:00
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
for (i = 0; i < numdev; i++) {
|
|
|
|
usb_detach(handles[i], INTERFACE1);
|
|
|
|
usb_detach(handles[i], INTERFACE2);
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
if (libusb_set_configuration(handles[i], 0x01) < 0) {
|
|
|
|
fprintf(stderr, "Could not set configuration 1\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
// Microdia tiene 2 interfaces
|
|
|
|
int s;
|
2017-09-13 16:55:32 +09:00
|
|
|
if ((s = libusb_claim_interface(handles[i], INTERFACE1)) < 0) {
|
2017-08-31 16:01:57 +09:00
|
|
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-09-13 16:55:32 +09:00
|
|
|
if ((s = libusb_claim_interface(handles[i], INTERFACE2)) < 0) {
|
2017-08-31 16:01:57 +09:00
|
|
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-31 09:56:15 +09:00
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
return numdev;
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 17:08:18 +09:00
|
|
|
void ini_control_transfer(libusb_device_handle *dev) {
|
2012-09-13 20:03:51 +02:00
|
|
|
int r,i;
|
|
|
|
|
|
|
|
char question[] = { 0x01,0x01 };
|
|
|
|
|
2017-08-31 11:08:33 +09:00
|
|
|
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (unsigned char *) question, 2, timeout);
|
2017-09-13 16:55:32 +09:00
|
|
|
if(r < 0) {
|
2017-08-31 09:56:15 +09:00
|
|
|
perror("USB control write"); bad("USB write failed");
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(debug) {
|
2017-08-31 09:56:15 +09:00
|
|
|
for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF);
|
|
|
|
printf("\n");
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 17:08:18 +09:00
|
|
|
void control_transfer(libusb_device_handle *dev, const char *pquestion) {
|
2012-09-13 20:03:51 +02:00
|
|
|
int r,i;
|
|
|
|
|
|
|
|
char question[reqIntLen];
|
|
|
|
|
|
|
|
memcpy(question, pquestion, sizeof question);
|
|
|
|
|
2017-08-31 11:08:33 +09:00
|
|
|
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (unsigned char *) question, reqIntLen, timeout);
|
2017-09-13 16:55:32 +09:00
|
|
|
if(r < 0) {
|
2017-08-31 09:56:15 +09:00
|
|
|
perror("USB control write"); bad("USB write failed");
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:25:24 +09:00
|
|
|
void interrupt_read(libusb_device_handle *dev, unsigned char *answer) {
|
2017-08-30 17:08:18 +09:00
|
|
|
int r,s,i;
|
2012-09-13 20:03:51 +02:00
|
|
|
bzero(answer, reqIntLen);
|
|
|
|
|
2017-08-30 17:08:18 +09:00
|
|
|
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout);
|
2017-09-13 16:55:32 +09:00
|
|
|
if(r != reqIntLen) {
|
2017-08-31 09:56:15 +09:00
|
|
|
fprintf(stderr, "USB read failed: %d\n", s);
|
|
|
|
perror("USB interrupt read"); bad("USB read failed");
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(debug) {
|
2017-08-31 09:56:15 +09:00
|
|
|
for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF);
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
printf("\n");
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ex_program(int sig) {
|
2017-08-31 09:56:15 +09:00
|
|
|
bsalir=1;
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
(void) signal(SIGINT, SIG_DFL);
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv) {
|
2017-08-31 16:01:57 +09:00
|
|
|
libusb_device_handle **handles;
|
|
|
|
int numdev,i;
|
2017-09-13 21:25:24 +09:00
|
|
|
unsigned char *answer;
|
|
|
|
int temperature;
|
2017-09-14 11:56:09 +09:00
|
|
|
char strdate[20];
|
2017-09-13 21:25:24 +09:00
|
|
|
float tempInC, tempExC;
|
2017-08-31 09:56:15 +09:00
|
|
|
int c;
|
|
|
|
struct tm *local;
|
|
|
|
time_t t;
|
|
|
|
|
2017-09-14 11:56:09 +09:00
|
|
|
while ((c = getopt (argc, argv, "fvhl::")) != -1)
|
2017-08-31 09:56:15 +09:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
formato=2; //Fahrenheit
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (optarg!=NULL){
|
|
|
|
if (!sscanf(optarg,"%i",&seconds)==1) {
|
|
|
|
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
bsalir = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bsalir = 0;
|
|
|
|
seconds = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
printf("pcsensor version %s\n",VERSION);
|
2017-08-31 17:16:47 +09:00
|
|
|
printf(" Available options:\n");
|
|
|
|
printf(" -h help\n");
|
|
|
|
printf(" -v verbose\n");
|
|
|
|
printf(" -l[n] loop every 'n' seconds, default value is 5s\n");
|
2017-09-14 11:56:09 +09:00
|
|
|
printf(" -f output in Fahrenheit (default: Celsius)\n");
|
2017-08-31 09:56:15 +09:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
if (isprint (optopt))
|
|
|
|
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
|
|
|
|
else
|
2017-08-31 16:01:57 +09:00
|
|
|
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
|
2017-08-31 09:56:15 +09:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
if (optind < argc) {
|
2012-09-13 20:03:51 +02:00
|
|
|
fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2017-08-31 09:56:15 +09:00
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
handles = calloc(MAX_DEV, sizeof(libusb_device_handle*));
|
|
|
|
if ((numdev = setup_libusb_access(handles)) < 1) {
|
2017-08-31 09:56:15 +09:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
(void) signal(SIGINT, ex_program);
|
|
|
|
|
2017-09-13 21:25:24 +09:00
|
|
|
answer = calloc(reqIntLen, sizeof(unsigned char));
|
2017-09-14 09:56:38 +09:00
|
|
|
/* This looks unnecessary
|
2017-08-31 16:01:57 +09:00
|
|
|
for (i = 0; i < numdev; i++) {
|
|
|
|
ini_control_transfer(handles[i]);
|
2017-08-31 09:56:15 +09:00
|
|
|
|
2017-09-13 16:55:32 +09:00
|
|
|
control_transfer(handles[i], uTemperature);
|
2017-09-13 21:25:24 +09:00
|
|
|
interrupt_read(handles[i], answer);
|
2017-08-31 09:56:15 +09:00
|
|
|
|
2017-09-13 16:55:32 +09:00
|
|
|
control_transfer(handles[i], uIni1);
|
2017-09-13 21:25:24 +09:00
|
|
|
interrupt_read(handles[i], answer);
|
2017-08-31 09:56:15 +09:00
|
|
|
|
2017-09-13 16:55:32 +09:00
|
|
|
control_transfer(handles[i], uIni2);
|
2017-09-13 21:25:24 +09:00
|
|
|
interrupt_read(handles[i], answer);
|
|
|
|
interrupt_read(handles[i], answer);
|
2017-08-31 16:01:57 +09:00
|
|
|
}
|
2017-09-14 09:56:38 +09:00
|
|
|
*/
|
2017-08-31 09:56:15 +09:00
|
|
|
|
|
|
|
do {
|
2017-08-31 16:01:57 +09:00
|
|
|
for (i = 0; i < numdev; i++) {
|
2017-09-13 16:55:32 +09:00
|
|
|
control_transfer(handles[i], uTemperature);
|
2017-09-13 21:25:24 +09:00
|
|
|
interrupt_read(handles[i], answer);
|
|
|
|
|
|
|
|
temperature = (answer[3] & 0xFF) + ((signed char)answer[2] << 8);
|
|
|
|
tempInC = temperature * (125.0 / 32000.0);
|
|
|
|
|
|
|
|
temperature = (answer[5] & 0xFF) + ((signed char)answer[4] << 8);
|
|
|
|
tempExC = temperature * (125.0 / 32000.0);
|
2017-08-31 09:56:15 +09:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
t = time(NULL);
|
|
|
|
local = localtime(&t);
|
2017-08-31 09:56:15 +09:00
|
|
|
|
2017-09-14 11:56:09 +09:00
|
|
|
sprintf(strdate, "%04d-%02d-%02dT%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("%s\t%d\tinternal\t%.2f\n", strdate, i, (9.0 / 5.0 * tempInC + 32.0));
|
|
|
|
printf("%s\t%d\texternal\t%.2f\n", strdate, i, (9.0 / 5.0 * tempExC + 32.0));
|
2017-08-31 09:56:15 +09:00
|
|
|
} else {
|
2017-09-14 11:56:09 +09:00
|
|
|
printf("%s\t%d\tinternal\t%.2f\n", strdate, i, tempInC);
|
|
|
|
printf("%s\t%d\texternal\t%.2f\n", strdate, i, tempExC);
|
2017-08-31 09:56:15 +09:00
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
if (!bsalir)
|
|
|
|
sleep(seconds);
|
|
|
|
}
|
2017-08-31 09:56:15 +09:00
|
|
|
} while (!bsalir);
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
for (i = 0; i < numdev; i++) {
|
|
|
|
libusb_release_interface(handles[i], INTERFACE1);
|
|
|
|
libusb_release_interface(handles[i], INTERFACE2);
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 16:01:57 +09:00
|
|
|
libusb_close(handles[i]);
|
|
|
|
}
|
2012-09-13 20:03:51 +02:00
|
|
|
|
2017-08-31 09:56:15 +09:00
|
|
|
return 0;
|
2012-09-13 20:03:51 +02:00
|
|
|
}
|