Пример #1
0
int main( int argc, char **argv)
{
	usb_dev_handle *lvr_winusb;

	if ((lvr_winusb = setup_libusb_access()) == NULL) 
	{
		exit(-1);
	} 

	//printf("Testing control transfer: ");
	//test_control_transfer(lvr_winusb);

	//printf("Testing interrupt transfer: ");
	//test_interrupt_transfer(lvr_winusb);

	//printf("Testing bulk transfer: "); 
	//test_bulk_transfer(lvr_winusb);


	usb_release_interface(lvr_winusb, 0);
	usb_reset(lvr_winusb);
	usb_close(lvr_winusb);
	return 0;

}
Пример #2
0
int main( int argc, char **argv)
{
	usb_dev_handle *lvr_winusb;
	if ((lvr_winusb = setup_libusb_access()) == NULL) {
		exit(-1);
	} 
//	test_control_transfer(lvr_winusb);
//	test_interrupt_transfer(lvr_winusb);
	test_bulk_transfer(lvr_winusb);
	usb_close(lvr_winusb);

	return 0;
}
Пример #3
0
usb_dev_handle* pcsensor_open(){
	usb_dev_handle *lvr_winusb = NULL;
	char buf[256];
	int i, ret;

	if (!(lvr_winusb = setup_libusb_access())) {
		return NULL;
	} 

	switch(device_type(lvr_winusb)){
	case 0:
		control_transfer(lvr_winusb, uCmd1 );
		control_transfer(lvr_winusb, uCmd3 );
		control_transfer(lvr_winusb, uCmd2 );
		ret = get_data(lvr_winusb, buf, 256);
		if(debug){	
			printf("Other Stuff (%d bytes):\n", ret);
			for(i = 0; i < ret; i++) {
				printf(" %02x", buf[i] & 0xFF);
				if(i % 16 == 15) {
					printf("\n");
				}
			}
			printf("\n");
		}
		break;
	case 1:
		if (ini_control_transfer(lvr_winusb) < 0) {
			fprintf(stderr, "Failed to ini_control_transfer (device_type 1)");
			return NULL;
		}
      
		control_transfer(lvr_winusb, uTemperatura );
		interrupt_read(lvr_winusb);
 
		control_transfer(lvr_winusb, uIni1 );
		interrupt_read(lvr_winusb);
 
		control_transfer(lvr_winusb, uIni2 );
		interrupt_read(lvr_winusb);
		interrupt_read(lvr_winusb);
		break;
	}

	if(debug){
		printf("device_type=%d\n", device_type(lvr_winusb));
	}
	return lvr_winusb;
}
Пример #4
0
/**
 * Read the temperature in Celcius from the provided device number 0 indexed
 * @param int device_id
 * @return float
 */
float read_temperature_c(int device_id) {
    pcsensor_devices *available_devices = setup_libusb_access();
    float tempc = 0;
    if (available_devices->sensor_count == 0) {
        return FLT_MIN;
    }
    if ((device_id - 1) > available_devices->sensor_count) {
        return FLT_MIN;
    }
    if (available_devices->sensors[device_id] == NULL)
    {
        return FLT_MIN;
    }
    usb_dev_handle *device_handle = pcsensor_open(available_devices->sensors[device_id]);
    pcsensor_get_temperature(device_handle, &tempc);
    pcsensor_close(device_handle);

    return tempc;
}
Пример #5
0
int main( int argc, char **argv) {
 
     usb_dev_handle *lvr_winusb = NULL;
     float tempc[2];
     int c, i;
     struct tm *local;
     time_t t;
     int nr_of_sensors = 1;

     while ((c = getopt (argc, argv, "mfcvhl::a:n:")) != -1)
     switch (c)
       {
       case 'v':
         debug = 1;
         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 'n':
         if (!sscanf(optarg,"%i",&nr_of_sensors)==1) {
             fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
             exit(EXIT_FAILURE);
         } 
         else 
         {           
           if ((nr_of_sensors > 2) || (nr_of_sensors < 1))
           {
             fprintf (stderr, "Error: '%s' is not in range [1..2].\n", optarg);
             exit(EXIT_FAILURE);
           }
              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");
	 printf("	   -n[n] read number of sensors [1..2]\n");
  
	 exit(EXIT_FAILURE);
       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");
        exit(EXIT_FAILURE);
     }
 
     if ((lvr_winusb = setup_libusb_access()) == NULL) {
         exit(EXIT_FAILURE);
     } 

     (void) signal(SIGINT, ex_program);

     ini_control_transfer(lvr_winusb);
      
     control_transfer(lvr_winusb, uTemperatura );
     interrupt_read(lvr_winusb);
 
     control_transfer(lvr_winusb, uIni1 );
     interrupt_read(lvr_winusb);
 
     control_transfer(lvr_winusb, uIni2 );
     interrupt_read(lvr_winusb);
     interrupt_read(lvr_winusb);


 
     do {
           control_transfer(lvr_winusb, uTemperatura );
           interrupt_read_temperature(lvr_winusb, tempc);

           t = time(NULL);
           local = localtime(&t);

           if (mrtg) {
              if (formato==2) {
                  for (i=0;i<nr_of_sensors; i++) 
                  {
                    printf("%.2f\n", (9.0 / 5.0 * tempc[i] + 32.0));
                    printf("%.2f\n", (9.0 / 5.0 * tempc[i] + 32.0));
                  }
              } else {
                  for (i=0;i<nr_of_sensors; i++) 
                  {
                    printf("%.2f\n", tempc[i]);
                    printf("%.2f\n", tempc[i]);
                  }
              }
              
              printf("%02d:%02d\n", 
                          local->tm_hour,
                          local->tm_min);

              printf("pcsensor\n");
           } else {
              for (i=0;i<nr_of_sensors; i++) 
              {
                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%d %.2fF\n", i, (9.0 / 5.0 * tempc[i] + 32.0));
                } else if (formato==1) {
                    printf("Temperature%d %.2fC\n", i, tempc[i]);
                } else {
                    printf("Temperature%d %.2fF %.2fC\n", i, (9.0 / 5.0 * tempc[i] + 32.0), tempc[i]);
                }
             }
          }       
           if (!bsalir)
              sleep(seconds);
     } while (!bsalir);
                                       
     usb_release_interface(lvr_winusb, INTERFACE1);
     usb_release_interface(lvr_winusb, INTERFACE2);
     
     usb_close(lvr_winusb); 
      
     return 0; 
}
Пример #6
0
int main( int argc, char **argv) {

	libusb_device_handle *zena;

	int channel = -1;			// no default 802.15.4 channel
	int format = FORMAT_PCAP;	// PCAP is default output format
	int scan_mode = FALSE;
	int drop_bad_packets = TRUE;
	int exit_time = -1;
	int status;

	int c;
	// Setup signal handler. Catching SIGPIPE allows for exit when 
	// piping to Wireshark for live packet feed.
	// signal(SIGPIPE, signal_handler);
	struct sigaction act;
	memset(&act, 0, sizeof(act));
	act.sa_sigaction = signal_handler;
	act.sa_flags = SA_SIGINFO;
	sigaction(SIGPIPE, &act, NULL);


	// Parse command line arguments. See usage() for details.
	while ((c = getopt(argc, argv, "bc:d:f:hqs:t:vx:r")) != -1) {
		switch(c) {
			case 'b':
				drop_bad_packets = FALSE;
				break;
			case 'c':
				channel = atoi (optarg);
				if (channel < 11 || channel > 26) {
					fprintf (stderr, "ERROR: Invalid channel. Must be in rage 11 to 26. Use -h for help.\n");
					exit(-1);
				}
				break;
			case 'd':
				debug_level = atoi (optarg);
				break;
			case 'f':
				if (strcmp(optarg,"pcap")==0) {
					format = FORMAT_PCAP;
				} else if (strcmp(optarg,"usbhex")==0) {
					format = FORMAT_USBHEX;
           			} else if (strcmp(optarg, "ranger")==0) { 
                   			format = FORMAT_RANGER; 
           			} else {
					fprintf(stderr,"ERROR: unrecognized output format '%s'. Only pcap, usbhex, ranger 					allowed.\n",optarg);
					exit(-1);
				}
            			break;
			case 'h':
				version();
				usage();
				exit(EXIT_SUCCESS);
			case 'q':
				quiet_mode = TRUE;
				break;
			case 's':
				scan_mode = TRUE;
				usb_timeout = atoi (optarg);
				break;
			case 't':
				exit_time = atoi(optarg);
				break;
			case 'v':
				version();
				exit(EXIT_SUCCESS);
			case 'x':
				pcap_lqi_rssi_write = TRUE;
				break;
			case 'r':
				rangeDevice();
           			break;
			case '?':	// case when a command line switch argument is missing
				if (optopt == 'c') {
					fprintf (stderr,"ERROR: 802.15.4 channel 11 to 26 must be specified with -c\n");
					exit(-1);
				}
				if (optopt == 'd') {
					fprintf (stderr,"ERROR: debug level 0 .. 9 must be specified with -d\n");
					exit(-1);
				}
				if (optopt == 'f') {
					fprintf (stderr,"ERROR: pcap or usbhex format must be specified with -f\n");
					exit(-1);
				}
				break;
		}
	}

	if (debug_level > 0) {
		fprintf (stderr,"DEBUG: debug level %d\n",debug_level);
	}

	// Locate ZENA on the USB bus and get handle.
	if ((zena = setup_libusb_access()) == NULL) {
		fprintf (stderr, "ERROR: ZENA device not found or not accessible\n");
		exit(EXIT_FAILURE);
	}

	if (channel == -1 && (selected_profile->flags & CHANNEL_SELECTABLE)) {
		fprintf (stderr,"ERROR: 802.15.4 channel is mandatory. Specify with -c. Use -h for help.\n");
		exit(EXIT_FAILURE);
	}

	if (channel != -1) {
		// Set 802.15.4 channel
		status = zena_set_channel (zena,channel);
		if (status < 0) {
			fprintf (stderr, "ERROR: error setting ZENA to 802.15.4 channel %d, errorCode=%d\n",channel,status);
			exit(EXIT_FAILURE);
		}
	}

	// Write PCAP header
	if (format == FORMAT_PCAP) {
		fwrite(&PCAP_MAGIC, sizeof(int), 1, stdout);    
		fwrite(&PCAP_VERSION_MAJOR, sizeof(short), 1, stdout);
		fwrite(&PCAP_VERSION_MINOR, sizeof(short), 1, stdout);
		fwrite(&PCAP_TZ, sizeof(int), 1, stdout);				// thiszone: GMT to local correction
		fwrite(&PCAP_SIGFIGS, sizeof(int), 1, stdout);			// sigfigs: accuracy of timestamps
		fwrite(&PCAP_SNAPLEN, sizeof(int), 1, stdout);			// snaplen: max len of packets, in octets
		fwrite(&PCAP_LINKTYPE, sizeof(int), 1, stdout);		// data link type
	}

	int i,j,data_len,packet_len,packet_len_plus_2,ts_sec,ts_usec;

	// Allocate buffer for usb_interrupt_read requests
	unsigned char usbbuf[64];
	//unsigned char packetbuf[128];
	
	// Get start time of capture. Won't worry about subsecond resolution for this.
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	int start_sec = tp.tv_sec;

	// Store the number of bytes actually transferred here
	int nbytes;

   // Seitz added: Store corresponding RSS from MRF24J40
   float rss; 
   
   // Seitz added: Store estimated distance returned from calDist()
   float estDist;
   
	// Packet counter
	int npacket=0;

	zena_packet_t zena_packet;

	// Main loop
	while ( ! exit_flag ) {

		// If scan_mode is TRUE, cycle through all the 802.15.4 channels looking
		// for packets. For some reason it seems to be necessary to close the 
		// USB device and libusb library and reopen it for the channel change to 
		// work reliably. Why?

		if (scan_mode) {

			channel++;
			if (channel > 26) {
				channel = 11;
			}

			// It seems to be necessary to reset libusb (close library and 
			// re-initialize it) for zena_set_channel() to be successful.
			debug(9,"Closing ZENA to facilitate 802.15.4 channel change");
			libusb_close (zena);
			debug(9,"Closing libusb library to facilitate 802.15.4 channel change");
			libusb_exit(NULL);
			debug(9,"Reopening ZENA");
		        if ((zena = setup_libusb_access()) == NULL) {
				fprintf (stderr, "ERROR: unable to reopen ZENA during 80.15.4 channel change\n");
				exit(EXIT_FAILURE);
			}
	
			debug (1,"Setting 802.15.4 channel to %d",channel);
			status = zena_set_channel(zena,channel);
			if (status<0) {
				fprintf (stderr,"ERROR: error setting 802.15.4 channel to %d during scan, errorCode=%d\n",channel, status);
				exit(EXIT_FAILURE);
			} 

			// TODO: bug - we can have packet received from the
			// previous 802.15.4 channel in the buffer at this
			// point. When outputted it will be incorrectly
			// tagged with the new channel number. Can we purge
			// the buffer somehow?
			
		}

		switch (format) {

      
            //Seitz Added: Prints the packet: Time/chan/pkt len/src/possible dst/LQI/RSSI/pkt count
           case FORMAT_RANGER:

               status = zena_get_packet (zena, &zena_packet);
				if (status == LIBUSB_ERROR_TIMEOUT) {
					// A timeout is a normal event. No action.
					break;
				}
				if (status != 0) {
					fprintf (stderr,"ERROR: retrieving packet, errorCode=%d\n",status);
					break;
				}

				// Ensure that zena_packet.packet_len is a sane value. Occasionally getting crazy
				// values which causes segv when accessing the zena_packet.packet[] buffer.
				//Lu: added case of 0 byte length here which causes tshark to crash
				zena_packet.packet_len &= 0xff;

				if (zena_packet.packet_len > 125 || zena_packet.packet_len == 0) {
					fprintf (stderr,"ERROR: invalid packet length, len=%d\n",zena_packet.packet_len);
					break;
				}

				if (  ( ! zena_packet.fcs_ok) && drop_bad_packets ) {
					warning ("dropping corrupted packet\n");
					break;
				}
/*
           //Zena reported second.usecond
              fprintf (stdout,"%d.%d ", zena_packet.zena_ts_sec, zena_packet.zena_ts_usec);

				// 802.15.4 channel
				fprintf (stdout, "Channel: %02d ", channel);
                
            // Packet length
            fprintf (stdout, "Packet Length: %03d ", zena_packet.packet_len);
            
            // Source Addr: 2 bytes zena_packet.packet[7-8]
            fprintf(stdout, "Src: %04d \t", zena_packet.packet[7]);
              
            // Destination Addr: 2 bytes zena_packet.packet[5-6]
            fprintf (stdout, "Dst: %04d \t", zena_packet.packet[5]);
              
            // LQI: Based off of SNR and RSSI for MRF24J40
            // Values range from [0-255] Higher is better
            fprintf(stdout, "LQI: %02d \t", zena_packet.lqi);
              
            // RSSI Based off MRF24J40 received signal strength
            // Values range from [0-255] Higher is better
            fprintf(stdout, "RSSI: %02d\t", zena_packet.rssi);
            fprintf(stdout, "Packet count: %02d ", npacket); //Packet count
              
            // Calculate estimated distance from target


            fprintf(stdout, "\nCal Dist: %dm\n", estDist);
            fprintf(stdout, "RSS: %3.2f\t", rss);
            */

            rss = RSSI_TO_RSS[zena_packet.rssi];
            estDist = calDist(rss);
            fprintf(stdout, "Packet Count:%d,\tSrc ID:%04d,\tDst ID: %04d,\tLQI:%d,\tRSS:%2.2f,\tDist Est:%2.2fm",npacket,zena_packet.packet[7],zena_packet.packet[5],zena_packet.lqi,rss,estDist);           
				 fprintf(stdout, "\n");
            // fprintf(stdout, "Packet Count: %d, \tRSS = %2.2f\n",npacket,rss);
               fflush(stdout);
               npacket++;
               break;
// End Seitz


			case FORMAT_USBHEX:
				
				bzero(usbbuf, 64);
				
				status = selected_profile->transfer(zena, selected_profile->ep_packets, usbbuf, 64, &nbytes, usb_timeout);
				// check for timeout and silently ignore
				if (status == LIBUSB_ERROR_TIMEOUT) {
					debug(9,"libusb_transfer(): timeout");
					continue;
				}

				// get host time of packet reception
				clock_gettime(CLOCK_REALTIME, &tp);
				if ( (exit_time>0) && (tp.tv_sec > (start_sec + exit_time))) {
					debug(1,"Exit time reached. Exiting.");
					exit(EXIT_SUCCESS);
				}

				// a real error (ie not timeout)
				if (status < 0) {
					fprintf (stderr,"ERROR: error retrieving ZENA packet, errorCode=%d\n", status);
					continue;
				}

				// Packet timestamp
				fprintf (stdout,"%ld.%ld ",tp.tv_sec,tp.tv_nsec);

				// 802.15.4 channel
				fprintf (stdout, "%02x ", channel);

				// Echo USB 64 byte packet to screen. Each byte as hex separated by space. 
				// One line per packet.
				for (j = 0; j < 64; j++) {
					fprintf (stdout, "%02x ", usbbuf[j] & 0xff);
				}
				fprintf (stdout, "\n");
				fflush (stdout);
				break;
            
            //Seitz Added: Printing last two bytes of Rx beacon for LQI and RSSI values
//				for (j = 17; j < 19; j++) {
//					fprintf (stdout, "%02x ", usbbuf[j] & 0xff);
//                  fprintf (stdout, "%02d ", usbbuf[j]);
//				}
            //End Seitz
 
				fprintf (stdout, "\n");
				fflush (stdout);
				break;
                
			case FORMAT_PCAP:
				status = zena_get_packet (zena, &zena_packet);
				if (status == LIBUSB_ERROR_TIMEOUT) {
					// A timeout is a normal event. No action.
					break;
				}
				if (status != 0) {
					fprintf (stderr,"ERROR: retrieving packet, errorCode=%d\n",status);
					break;
				}

				// Ensure that zena_packet.packet_len is a sane value. Occasionally getting crazy
				// values which causes segv when accessing the zena_packet.packet[] buffer.
				//Lu: added case of 0 byte length here which causes tshark to crash
				zena_packet.packet_len &= 0xff;

				if (zena_packet.packet_len > 125 || zena_packet.packet_len == 0) {
					fprintf (stderr,"ERROR: invalid packet length, len=%d\n",zena_packet.packet_len);
					break;
				}

				if (  ( ! zena_packet.fcs_ok) && drop_bad_packets ) {
					warning ("dropping corrupted packet\n");
					break;
				}

				npacket++;

				// Write PCAP packet header
				fwrite (&zena_packet.host_ts_sec, sizeof(int), 1, stdout);	// ts_sec: timestamp seconds
				fwrite (&zena_packet.host_ts_usec, sizeof(int), 1, stdout);	// ts_usec: timestamp microseconds

				if (selected_profile->flags & HAS_FCS_FIELD) {
					fwrite (&zena_packet.packet_len, sizeof(int), 1, stdout);
					fwrite (&zena_packet.packet_len, sizeof(int), 1, stdout);
					fwrite (zena_packet.packet, 1, zena_packet.packet_len, stdout);
				} else if (pcap_lqi_rssi_write) {
					packet_len_plus_2 = zena_packet.packet_len + 2;
					fwrite (&packet_len_plus_2, sizeof(int), 1, stdout);
					fwrite (&packet_len_plus_2, sizeof(int), 1, stdout);
					fwrite (zena_packet.packet, 1, zena_packet.packet_len, stdout);
				} else
					
				// Small problem re FCS. Old HW ZENA does not provide this information.
				// Solution is in the case of a good packet not to include FCS
				// and Wireshark will ignore it. In the case were the FCS is 
				// known to be bad, we'll include a deliberatly wrong FCS. For
				// the moment this will be a fixed value (0x0000), but ideally
				// it should be computed from the packet and the +1 to guarantee
				// it is a bad FCS.
					
					if (zena_packet.fcs_ok) {
					packet_len_plus_2 = zena_packet.packet_len + 2;
					
					// write packet excluding FCS
					fwrite (&zena_packet.packet_len, sizeof(int), 1, stdout);
					fwrite (&packet_len_plus_2, sizeof(int), 1, stdout);	// full frame included 2 FCS octets
					fwrite (zena_packet.packet, 1, zena_packet.packet_len, stdout);
				} else {
					packet_len_plus_2 = zena_packet.packet_len + 2;
					
					// two extra bytes for deliberately wrong FCS
					fwrite (&packet_len_plus_2, sizeof(int), 1, stdout);
					fwrite (&packet_len_plus_2, sizeof(int), 1, stdout);
					zena_packet.packet[zena_packet.packet_len] = 0;
					zena_packet.packet[zena_packet.packet_len+1] = 0;
					fwrite (zena_packet.packet, 1, packet_len_plus_2, stdout);
				}

				fflush(stdout);
				break;


		} // end switch


	} // end main loop

	// Release USB interface and close USB connection.
	// This code never reached at the moment -- need to implement signal handler for this.
	// However I've noticed no resource leaks. Process kill seems to take care of this.
	libusb_close (zena);
	libusb_exit(NULL);

	debug (1, "Normal exit");
	return EXIT_SUCCESS; 
}
Пример #7
0
int main( int argc, char **argv) {

     usb_dev_handle *lvr_winusb = NULL;
     float tempc;
     float tempc_measure_offset = 0.0;
     int c;
     struct tm *local;
     time_t t;

     while ((c = getopt (argc, argv, "mnfcvhl::s::")) != -1)
     switch (c)
       {
       case 'v':
         debug = 1;
         break;
       case 'c':
         formato=1; //Celsius
         break;
       case 'f':
         formato=2; //Fahrenheit
         break;
       case 's':
         if (!sscanf(optarg,"%f",&tempc_measure_offset)==1) {
           fprintf (stderr, "Error: '%s' is not (float) numeric.\n", optarg);
           exit(EXIT_FAILURE);
         }
         if (tempc_measure_offset > 100000 || tempc_measure_offset < -100000) {
           fprintf (stderr, "Error: please provide a reasonable offset\n");
           exit(EXIT_FAILURE);
         }
       case 'n':
         formato=3;
         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 '?':
       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("          -s<f> substract 'f' °C (float) from measured temperature\n");
	 printf("          -c output only in Celsius\n");
	 printf("          -f output only in Fahrenheit\n");
	 printf("          -m output for mrtg integration\n");
	 printf("          -n only display value in Celsius for Nagios\n");

	 exit(EXIT_FAILURE);
       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");
        exit(EXIT_FAILURE);
     }

     if ((lvr_winusb = setup_libusb_access()) == NULL) {
         exit(EXIT_FAILURE);
     }

     (void) signal(SIGINT, ex_program);

     ini_control_transfer(lvr_winusb);

     control_transfer(lvr_winusb, uTemperatura );
     interrupt_read(lvr_winusb);

     control_transfer(lvr_winusb, uIni1 );
     interrupt_read(lvr_winusb);

     control_transfer(lvr_winusb, uIni2 );
     interrupt_read(lvr_winusb);
     interrupt_read(lvr_winusb);



     do {
           control_transfer(lvr_winusb, uTemperatura );
           interrupt_read_temperatura(lvr_winusb, &tempc);
           tempc = tempc - tempc_measure_offset;

           t = time(NULL);
           local = localtime(&t);

           if (mrtg) {
              if (formato==2) {
                  printf("%.2f\n", (9.0 / 5.0 * tempc + 32.0));
                  printf("%.2f\n", (9.0 / 5.0 * tempc + 32.0));
              } else {
                  printf("%.2f\n", tempc);
                  printf("%.2f\n", tempc);
              }

              printf("%02d:%02d\n",
                          local->tm_hour,
                          local->tm_min);

              printf("pcsensor\n");
           } else {

            if (formato != 3) {

              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==3) {
                  // for Nagios
                  printf("%.2f\n", tempc);
              }
              else 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);
              }
              fflush(stdout);
           }

           if (!bsalir)
              sleep(seconds);
     } while (!bsalir);

     usb_release_interface(lvr_winusb, INTERFACE1);
     usb_release_interface(lvr_winusb, INTERFACE2);

     usb_close(lvr_winusb);

     return 0;
}
Пример #8
0
int main(int argc, char **argv) {
  int c;

  while ((c = getopt (argc, argv, "mfcvhl::")) != -1) {
    switch (c) {
    case 'v':
      debug = 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 'h':
      printf("Available options:\n");
      printf("    -h help\n");
      printf("    -v verbose\n");
      printf("    -l[n] loop every 'n' seconds, default value is 5s\n");
      exit(EXIT_FAILURE);

    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");
    exit(EXIT_FAILURE);
  }



  usb_dev_handle *lvr_winusb = NULL;
  if ((lvr_winusb = setup_libusb_access()) == NULL) {
    exit(EXIT_FAILURE);
  }

  signal(SIGINT, ex_program);

  ini_control_transfer(lvr_winusb);

  control_transfer(lvr_winusb, uTemp);
  interrupt_read(lvr_winusb);

  control_transfer(lvr_winusb, uIni1);
  interrupt_read(lvr_winusb);

  control_transfer(lvr_winusb, uIni2);
  interrupt_read(lvr_winusb);
  interrupt_read(lvr_winusb);

  do {
    control_transfer(lvr_winusb, uTemp);
    float temp_c = interrupt_read_temperature(lvr_winusb);

    time_t t = time(NULL);
    struct tm *local = localtime(&t);

    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);

    printf("Temperature %f\n", temp_c);
    fflush(stdout);

    if (!bsalir)
      sleep(seconds);
  } while (!bsalir);

  usb_release_interface(lvr_winusb, INTERFACE1);
  usb_release_interface(lvr_winusb, INTERFACE2);

  usb_close(lvr_winusb);

  return 0;
}
Пример #9
0
/**
 * Returns the number of Temper USB devices found
 * 
 * @return int
 */
int get_device_count() {
    pcsensor_devices *available_devices = setup_libusb_access();
    return available_devices->sensor_count;
}