mirror of
https://github.com/shakemid/pcsensor-temper.git
synced 2025-01-21 17:18:16 +01:00
Change to support multiple devices
This commit is contained in:
parent
736f32ec25
commit
8955a0d5a6
2 changed files with 133 additions and 95 deletions
|
@ -21,9 +21,12 @@ $ make
|
||||||
# Usage
|
# Usage
|
||||||
```
|
```
|
||||||
$ sudo ./pcsensor
|
$ sudo ./pcsensor
|
||||||
2017/08/30 17:21:35
|
2017/08/31 19:00:35
|
||||||
Temperature (internal) 96.58F 35.88C
|
Temperature (0:internal) 81.28F 27.38C
|
||||||
Temperature (external) 214.60F 101.45C
|
Temperature (0:external) 214.60F 101.45C
|
||||||
|
2017/08/31 19:00:35
|
||||||
|
Temperature (1:internal) 81.16F 27.31C
|
||||||
|
Temperature (1:external) 214.60F 101.45C
|
||||||
```
|
```
|
||||||
|
|
||||||
libusb_detach_kernel_driver does not seem to work on Solaris. Thus, it looks necessary to unload hid driver manually.
|
libusb_detach_kernel_driver does not seem to work on Solaris. Thus, it looks necessary to unload hid driver manually.
|
||||||
|
|
219
pcsensor.c
219
pcsensor.c
|
@ -49,6 +49,8 @@
|
||||||
#define INTERFACE1 0x00
|
#define INTERFACE1 0x00
|
||||||
#define INTERFACE2 0x01
|
#define INTERFACE2 0x01
|
||||||
|
|
||||||
|
#define MAX_DEV 8
|
||||||
|
|
||||||
const static int reqIntLen=8;
|
const static int reqIntLen=8;
|
||||||
const static int reqBulkLen=8;
|
const static int reqBulkLen=8;
|
||||||
const static int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
|
const static int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
|
||||||
|
@ -68,15 +70,13 @@ static int formato=0;
|
||||||
static int mrtg=0;
|
static int mrtg=0;
|
||||||
static int calibration=0;
|
static int calibration=0;
|
||||||
|
|
||||||
|
static libusb_context *ctx = NULL;
|
||||||
|
|
||||||
void bad(const char *why) {
|
void bad(const char *why) {
|
||||||
fprintf(stderr,"Fatal error> %s\n",why);
|
fprintf(stderr,"Fatal error> %s\n",why);
|
||||||
exit(17);
|
exit(17);
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_context *ctx = NULL;
|
|
||||||
libusb_device_handle *find_lvr_winusb();
|
|
||||||
|
|
||||||
void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
@ -88,8 +88,7 @@ void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(debug) {
|
if(debug) {
|
||||||
printf("Detach failed: %s[%d]\n",
|
printf("Detach failed: %s[%d]\n", strerror(errno), errno);
|
||||||
strerror(errno), errno);
|
|
||||||
printf("Continuing anyway\n");
|
printf("Continuing anyway\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,8 +99,47 @@ void usb_detach(libusb_device_handle *lvr_winusb, int iInterface) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_device_handle* setup_libusb_access() {
|
int find_lvr_winusb(libusb_device_handle **handles) {
|
||||||
libusb_device_handle *lvr_winusb;
|
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;
|
||||||
|
|
||||||
libusb_init(&ctx);
|
libusb_init(&ctx);
|
||||||
|
|
||||||
|
@ -111,43 +149,34 @@ libusb_device_handle* setup_libusb_access() {
|
||||||
libusb_set_debug(ctx, 0); //LIBUSB_LOG_LEVEL_NONE
|
libusb_set_debug(ctx, 0); //LIBUSB_LOG_LEVEL_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(lvr_winusb = find_lvr_winusb())) {
|
if((numdev = find_lvr_winusb(handles)) < 1) {
|
||||||
fprintf(stderr, "Couldn't find the USB device, Exiting\n");
|
fprintf(stderr, "Couldn't find the USB device, Exiting: %d\n", numdev);
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
usb_detach(lvr_winusb, INTERFACE1);
|
for (i = 0; i < numdev; i++) {
|
||||||
usb_detach(lvr_winusb, INTERFACE2);
|
usb_detach(handles[i], INTERFACE1);
|
||||||
|
usb_detach(handles[i], INTERFACE2);
|
||||||
|
|
||||||
if (libusb_set_configuration(lvr_winusb, 0x01) < 0) {
|
if (libusb_set_configuration(handles[i], 0x01) < 0) {
|
||||||
fprintf(stderr, "Could not set configuration 1\n");
|
fprintf(stderr, "Could not set configuration 1\n");
|
||||||
return NULL;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Microdia tiene 2 interfaces
|
||||||
|
int s;
|
||||||
|
if ( ( s = libusb_claim_interface(handles[i], INTERFACE1) ) < 0) {
|
||||||
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ( s = libusb_claim_interface(handles[i], INTERFACE2) ) < 0) {
|
||||||
|
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Microdia tiene 2 interfaces
|
return numdev;
|
||||||
int s;
|
|
||||||
if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE1) ) != 0) {
|
|
||||||
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE2) ) != 0) {
|
|
||||||
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return lvr_winusb;
|
|
||||||
}
|
|
||||||
|
|
||||||
libusb_device_handle *find_lvr_winusb() {
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ini_control_transfer(libusb_device_handle *dev) {
|
void ini_control_transfer(libusb_device_handle *dev) {
|
||||||
|
@ -236,7 +265,8 @@ void ex_program(int sig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char **argv) {
|
int main( int argc, char **argv) {
|
||||||
libusb_device_handle *lvr_winusb = NULL;
|
libusb_device_handle **handles;
|
||||||
|
int numdev,i;
|
||||||
float tempInC;
|
float tempInC;
|
||||||
float tempOutC;
|
float tempOutC;
|
||||||
int c;
|
int c;
|
||||||
|
@ -296,9 +326,7 @@ int main( int argc, char **argv) {
|
||||||
if (isprint (optopt))
|
if (isprint (optopt))
|
||||||
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
|
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
|
||||||
else
|
else
|
||||||
fprintf (stderr,
|
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
|
||||||
"Unknown option character `\\x%x'.\n",
|
|
||||||
optopt);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,74 +335,81 @@ int main( int argc, char **argv) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((lvr_winusb = setup_libusb_access()) == NULL) {
|
handles = calloc(MAX_DEV, sizeof(libusb_device_handle*));
|
||||||
|
if ((numdev = setup_libusb_access(handles)) < 1) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) signal(SIGINT, ex_program);
|
(void) signal(SIGINT, ex_program);
|
||||||
|
|
||||||
ini_control_transfer(lvr_winusb);
|
for (i = 0; i < numdev; i++) {
|
||||||
|
ini_control_transfer(handles[i]);
|
||||||
|
|
||||||
control_transfer(lvr_winusb, uTemperatura );
|
control_transfer(handles[i], uTemperatura );
|
||||||
interrupt_read(lvr_winusb);
|
interrupt_read(handles[i]);
|
||||||
|
|
||||||
control_transfer(lvr_winusb, uIni1 );
|
control_transfer(handles[i], uIni1 );
|
||||||
interrupt_read(lvr_winusb);
|
interrupt_read(handles[i]);
|
||||||
|
|
||||||
control_transfer(lvr_winusb, uIni2 );
|
control_transfer(handles[i], uIni2 );
|
||||||
interrupt_read(lvr_winusb);
|
interrupt_read(handles[i]);
|
||||||
interrupt_read(lvr_winusb);
|
interrupt_read(handles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
control_transfer(lvr_winusb, uTemperatura );
|
for (i = 0; i < numdev; i++) {
|
||||||
interrupt_read_temperatura(lvr_winusb, &tempInC, &tempOutC);
|
control_transfer(handles[i], uTemperatura );
|
||||||
|
interrupt_read_temperatura(handles[i], &tempInC, &tempOutC);
|
||||||
|
|
||||||
t = time(NULL);
|
t = time(NULL);
|
||||||
local = localtime(&t);
|
local = localtime(&t);
|
||||||
|
|
||||||
if (mrtg) {
|
if (mrtg) {
|
||||||
if (formato==2) {
|
if (formato==2) {
|
||||||
printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0));
|
printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0));
|
||||||
printf("%.2f\n", (9.0 / 5.0 * tempOutC + 32.0));
|
printf("%.2f\n", (9.0 / 5.0 * tempOutC + 32.0));
|
||||||
|
} else {
|
||||||
|
printf("%.2f\n", tempInC);
|
||||||
|
printf("%.2f\n", tempOutC);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%02d:%02d\n",
|
||||||
|
local->tm_hour,
|
||||||
|
local->tm_min);
|
||||||
|
|
||||||
|
printf("pcsensor:%d\n", i);
|
||||||
} else {
|
} else {
|
||||||
printf("%.2f\n", tempInC);
|
printf("%04d/%02d/%02d %02d:%02d:%02d\n",
|
||||||
printf("%.2f\n", tempOutC);
|
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 (%d:internal) %.2fF\n", i, (9.0 / 5.0 * tempInC + 32.0));
|
||||||
|
printf("Temperature (%d:external) %.2fF\n", i, (9.0 / 5.0 * tempOutC + 32.0));
|
||||||
|
} else if (formato==1) {
|
||||||
|
printf("Temperature (%d:internal) %.2fC\n", i, tempInC);
|
||||||
|
printf("Temperature (%d:external) %.2fC\n", i, tempOutC);
|
||||||
|
} else {
|
||||||
|
printf("Temperature (%d:internal) %.2fF %.2fC\n", i, (9.0 / 5.0 * tempInC + 32.0), tempInC);
|
||||||
|
printf("Temperature (%d:external) %.2fF %.2fC\n", i, (9.0 / 5.0 * tempOutC + 32.0), tempOutC);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%02d:%02d\n",
|
if (!bsalir)
|
||||||
local->tm_hour,
|
sleep(seconds);
|
||||||
local->tm_min);
|
|
||||||
|
|
||||||
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 (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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bsalir)
|
|
||||||
sleep(seconds);
|
|
||||||
} while (!bsalir);
|
} while (!bsalir);
|
||||||
|
|
||||||
libusb_release_interface(lvr_winusb, INTERFACE1);
|
for (i = 0; i < numdev; i++) {
|
||||||
libusb_release_interface(lvr_winusb, INTERFACE2);
|
libusb_release_interface(handles[i], INTERFACE1);
|
||||||
|
libusb_release_interface(handles[i], INTERFACE2);
|
||||||
|
|
||||||
libusb_close(lvr_winusb);
|
libusb_close(handles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue