Fix indent to 4 spaces

This commit is contained in:
shakemid 2017-08-31 09:56:15 +09:00
parent 3519bbbd47
commit cc37eec1cf

View file

@ -66,91 +66,86 @@ static int calibration=0;
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_context *ctx = NULL;
libusb_device_handle *find_lvr_winusb(); 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;
ret = libusb_detach_kernel_driver(lvr_winusb, iInterface); ret = libusb_detach_kernel_driver(lvr_winusb, iInterface);
if(ret) { if(ret) {
if(errno == ENODATA) { if(errno == ENODATA) {
if(debug) { if(debug) {
printf("Device already detached\n"); printf("Device already detached\n");
} }
} 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");
} }
} }
} else { } else {
if(debug) { if(debug) {
printf("detach successful\n"); printf("detach successful\n");
} }
} }
} }
libusb_device_handle* setup_libusb_access() { libusb_device_handle* setup_libusb_access() {
libusb_device_handle *lvr_winusb; libusb_device_handle *lvr_winusb;
libusb_init(&ctx); libusb_init(&ctx);
if(debug) { if(debug) {
libusb_set_debug(ctx, 4); //LIBUSB_LOG_LEVEL_DEBUG libusb_set_debug(ctx, 4); //LIBUSB_LOG_LEVEL_DEBUG
} else { } else {
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(!(lvr_winusb = find_lvr_winusb())) {
fprintf(stderr, "Couldn't find the USB device, Exiting\n"); fprintf(stderr, "Couldn't find the USB device, Exiting\n");
return NULL; return NULL;
} }
usb_detach(lvr_winusb, INTERFACE1);
usb_detach(lvr_winusb, INTERFACE2);
usb_detach(lvr_winusb, INTERFACE1); if (libusb_set_configuration(lvr_winusb, 0x01) < 0) {
fprintf(stderr, "Could not set configuration 1\n");
return NULL;
}
// Microdia tiene 2 interfaces
int s;
if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE1) ) != 0) {
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
return NULL;
}
usb_detach(lvr_winusb, INTERFACE2); if ( ( s = libusb_claim_interface(lvr_winusb, INTERFACE2) ) != 0) {
fprintf(stderr, "Could not claim interface. Error:%d\n", s);
return NULL;
}
return lvr_winusb;
if (libusb_set_configuration(lvr_winusb, 0x01) < 0) {
fprintf(stderr, "Could not set configuration 1\n");
return NULL;
}
// Microdia tiene 2 interfaces
int errno;
if ( ( errno = libusb_claim_interface(lvr_winusb, INTERFACE1) ) != 0) {
fprintf(stderr, "Could not claim interface. Error:%d\n", errno);
return NULL;
}
if ( ( errno = libusb_claim_interface(lvr_winusb, INTERFACE2) ) != 0) {
fprintf(stderr, "Could not claim interface. Error:%d\n", errno);
return NULL;
}
return lvr_winusb;
} }
libusb_device_handle *find_lvr_winusb() { libusb_device_handle *find_lvr_winusb() {
libusb_device_handle *handle; libusb_device_handle *handle;
handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID); handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
if (!handle) { if (!handle) {
fprintf(stderr, "Could not open USB device\n"); fprintf(stderr, "Could not open USB device\n");
return NULL; return NULL;
} }
return handle; return handle;
} }
@ -160,15 +155,13 @@ void ini_control_transfer(libusb_device_handle *dev) {
char question[] = { 0x01,0x01 }; char question[] = { 0x01,0x01 };
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout);
if( r < 0 ) if( r < 0 ) {
{ perror("USB control write"); bad("USB write failed");
perror("USB control write"); bad("USB write failed");
} }
if(debug) { if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF); for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF);
printf("\n"); printf("\n");
} }
} }
@ -180,9 +173,8 @@ void control_transfer(libusb_device_handle *dev, const char *pquestion) {
memcpy(question, pquestion, sizeof question); memcpy(question, pquestion, sizeof question);
r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (char *) question, reqIntLen, timeout); r = libusb_control_transfer(dev, 0x21, 0x09, 0x0200, 0x01, (char *) question, reqIntLen, timeout);
if( r < 0 ) if( r < 0 ) {
{ perror("USB control write"); bad("USB write failed");
perror("USB control write"); bad("USB write failed");
} }
if(debug) { if(debug) {
@ -192,70 +184,65 @@ void control_transfer(libusb_device_handle *dev, const char *pquestion) {
} }
void interrupt_transfer(libusb_device_handle *dev) { void interrupt_transfer(libusb_device_handle *dev) {
int r,s,i; int r,s,i;
char answer[reqIntLen]; char answer[reqIntLen];
char question[reqIntLen]; char question[reqIntLen];
int transferred; int transferred;
for (i=0;i<reqIntLen; i++) question[i]=i; for (i=0;i<reqIntLen; i++) question[i]=i;
s = libusb_interrupt_transfer(dev, endpoint_Int_out, question, reqIntLen, &r, timeout); s = libusb_interrupt_transfer(dev, endpoint_Int_out, question, reqIntLen, &r, timeout);
if( r < 0 ) if( r < 0 ) {
{ fprintf(stderr, "USB write failed:%d", s);
fprintf(stderr, "USB write failed:%d", s); perror("USB interrupt write"); bad("USB write failed");
perror("USB interrupt write"); bad("USB write failed");
} }
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout);
if( r != reqIntLen ) if( r != reqIntLen ) {
{ fprintf(stderr, "USB read failed:%d", s);
fprintf(stderr, "USB read failed:%d", s); perror("USB interrupt read"); bad("USB read failed");
perror("USB interrupt read"); bad("USB read failed");
} }
if(debug) { if(debug) {
for (i=0;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]); for (i=0;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]);
} }
libusb_release_interface(dev, 0); libusb_release_interface(dev, 0);
} }
void interrupt_read(libusb_device_handle *dev) { void interrupt_read(libusb_device_handle *dev) {
int r,s,i; int r,s,i;
unsigned char answer[reqIntLen]; unsigned char answer[reqIntLen];
bzero(answer, reqIntLen); bzero(answer, reqIntLen);
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); 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);
fprintf(stderr, "USB read failed: %d\n", s); perror("USB interrupt read"); bad("USB read failed");
perror("USB interrupt read"); bad("USB read failed");
} }
if(debug) { if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF); for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF);
printf("\n"); printf("\n");
} }
} }
void interrupt_read_temperatura(libusb_device_handle *dev, float *tempInC, float *tempOutC) { void interrupt_read_temperatura(libusb_device_handle *dev, float *tempInC, float *tempOutC) {
int r,s,i, temperature; int r,s,i, temperature;
unsigned char answer[reqIntLen]; unsigned char answer[reqIntLen];
bzero(answer, reqIntLen); bzero(answer, reqIntLen);
s = libusb_interrupt_transfer(dev, endpoint_Int_in, answer, reqIntLen, &r, timeout); 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);
fprintf(stderr, "USB read failed: %d\n", s); perror("USB interrupt read"); bad("USB read failed");
perror("USB interrupt read"); bad("USB read failed");
} }
if(debug) { if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF); for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF);
printf("\n"); printf("\n");
} }
temperature = (answer[3] & 0xFF) + ((signed char)answer[2] << 8); temperature = (answer[3] & 0xFF) + ((signed char)answer[2] << 8);
@ -269,24 +256,21 @@ void interrupt_read_temperatura(libusb_device_handle *dev, float *tempInC, float
} }
void bulk_transfer(libusb_device_handle *dev) { void bulk_transfer(libusb_device_handle *dev) {
int r,s,i; int r,s,i;
char answer[reqBulkLen]; char answer[reqBulkLen];
s = libusb_bulk_transfer(dev, endpoint_Bulk_out, NULL, 0, &r, timeout); s = libusb_bulk_transfer(dev, endpoint_Bulk_out, NULL, 0, &r, timeout);
if( r < 0 ) if( r < 0 ) {
{ perror("USB bulk write"); bad("USB write failed");
perror("USB bulk write"); bad("USB write failed");
}
s = libusb_bulk_transfer(dev, endpoint_Bulk_in, answer, reqBulkLen, &r, timeout);
if( r != reqBulkLen )
{
perror("USB bulk read"); bad("USB read failed");
} }
s = libusb_bulk_transfer(dev, endpoint_Bulk_in, answer, reqBulkLen, &r, timeout);
if( r != reqBulkLen ) {
perror("USB bulk read"); bad("USB read failed");
}
if(debug) { if(debug) {
for (i=0;i<reqBulkLen; i++) printf("%02x ",answer[i] & 0xFF); for (i=0;i<reqBulkLen; i++) printf("%02x ",answer[i] & 0xFF);
} }
libusb_release_interface(dev, 0); libusb_release_interface(dev, 0);
@ -294,154 +278,151 @@ void bulk_transfer(libusb_device_handle *dev) {
void ex_program(int sig) { void ex_program(int sig) {
bsalir=1; bsalir=1;
(void) signal(SIGINT, SIG_DFL); (void) signal(SIGINT, SIG_DFL);
} }
int main( int argc, char **argv) { int main( int argc, char **argv) {
libusb_device_handle *lvr_winusb = NULL;
float tempInC;
float tempOutC;
int c;
struct tm *local;
time_t t;
libusb_device_handle *lvr_winusb = NULL; while ((c = getopt (argc, argv, "mfcvhl::a:")) != -1)
float tempInC; switch (c)
float tempOutC; {
int c; case 'v':
struct tm *local; debug = 1;
time_t t; break;
case 'c':
formato=1; //Celsius
break;
case 'f':
formato=2; //Fahrenheit
break;
case 'm':
mrtg=1;
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 'a':
if (!sscanf(optarg,"%i",&calibration)==1) {
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
exit(EXIT_FAILURE);
} else {
break;
}
case '?':
case 'h':
printf("pcsensor version %s\n",VERSION);
printf(" Aviable options:\n");
printf(" -h help\n");
printf(" -v verbose\n");
printf(" -l[n] loop every 'n' seconds, default value is 5s\n");
printf(" -c output only in Celsius\n");
printf(" -f output only in Fahrenheit\n");
printf(" -a[n] increase or decrease temperature in 'n' degrees for device calibration\n");
printf(" -m output for mrtg integration\n");
while ((c = getopt (argc, argv, "mfcvhl::a:")) != -1) exit(EXIT_FAILURE);
switch (c) default:
{ if (isprint (optopt))
case 'v': fprintf (stderr, "Unknown option `-%c'.\n", optopt);
debug = 1; else
break; fprintf (stderr,
case 'c': "Unknown option character `\\x%x'.\n",
formato=1; //Celsius optopt);
break; exit(EXIT_FAILURE);
case 'f': }
formato=2; //Fahrenheit
break;
case 'm':
mrtg=1;
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 'a':
if (!sscanf(optarg,"%i",&calibration)==1) {
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
exit(EXIT_FAILURE);
} else {
break;
}
case '?':
case 'h':
printf("pcsensor version %s\n",VERSION);
printf(" Aviable options:\n");
printf(" -h help\n");
printf(" -v verbose\n");
printf(" -l[n] loop every 'n' seconds, default value is 5s\n");
printf(" -c output only in Celsius\n");
printf(" -f output only in Fahrenheit\n");
printf(" -a[n] increase or decrease temperature in 'n' degrees for device calibration\n");
printf(" -m output for mrtg integration\n");
exit(EXIT_FAILURE); if (optind < argc) {
default:
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
exit(EXIT_FAILURE);
}
if (optind < argc) {
fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n"); fprintf(stderr, "Non-option ARGV-elements, try -h for help.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((lvr_winusb = setup_libusb_access()) == NULL) { if ((lvr_winusb = setup_libusb_access()) == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
(void) signal(SIGINT, ex_program); (void) signal(SIGINT, ex_program);
ini_control_transfer(lvr_winusb);
ini_control_transfer(lvr_winusb); control_transfer(lvr_winusb, uTemperatura );
interrupt_read(lvr_winusb);
control_transfer(lvr_winusb, uTemperatura ); control_transfer(lvr_winusb, uIni1 );
interrupt_read(lvr_winusb); interrupt_read(lvr_winusb);
control_transfer(lvr_winusb, uIni1 ); control_transfer(lvr_winusb, uIni2 );
interrupt_read(lvr_winusb); interrupt_read(lvr_winusb);
interrupt_read(lvr_winusb);
control_transfer(lvr_winusb, uIni2 ); do {
interrupt_read(lvr_winusb); control_transfer(lvr_winusb, uTemperatura );
interrupt_read(lvr_winusb); interrupt_read_temperatura(lvr_winusb, &tempInC, &tempOutC);
t = time(NULL);
local = localtime(&t);
do { if (mrtg) {
control_transfer(lvr_winusb, uTemperatura ); if (formato==2) {
interrupt_read_temperatura(lvr_winusb, &tempInC, &tempOutC); printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0));
printf("%.2f\n", (9.0 / 5.0 * tempOutC + 32.0));
} else {
printf("%.2f\n", tempInC);
printf("%.2f\n", tempOutC);
}
t = time(NULL); printf("%02d:%02d\n",
local = localtime(&t); local->tm_hour,
local->tm_min);
if (mrtg) { printf("pcsensor\n");
if (formato==2) { } else {
printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0)); printf("%04d/%02d/%02d %02d:%02d:%02d\n",
printf("%.2f\n", (9.0 / 5.0 * tempOutC + 32.0)); local->tm_year +1900,
} else { local->tm_mon + 1,
printf("%.2f\n", tempInC); local->tm_mday,
printf("%.2f\n", tempOutC); local->tm_hour,
} local->tm_min,
local->tm_sec);
printf("%02d:%02d\n", if (formato==2) {
local->tm_hour, printf("Temperature (internal) %.2fF\n", (9.0 / 5.0 * tempInC + 32.0));
local->tm_min); 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);
}
}
printf("pcsensor\n"); if (!bsalir)
} else { sleep(seconds);
printf("%04d/%02d/%02d %02d:%02d:%02d\n", } while (!bsalir);
local->tm_year +1900,
local->tm_mon + 1,
local->tm_mday,
local->tm_hour,
local->tm_min,
local->tm_sec);
if (formato==2) { libusb_release_interface(lvr_winusb, INTERFACE1);
printf("Temperature (internal) %.2fF\n", (9.0 / 5.0 * tempInC + 32.0)); libusb_release_interface(lvr_winusb, INTERFACE2);
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) libusb_close(lvr_winusb);
sleep(seconds);
} while (!bsalir);
libusb_release_interface(lvr_winusb, INTERFACE1); return 0;
libusb_release_interface(lvr_winusb, INTERFACE2);
libusb_close(lvr_winusb);
return 0;
} }