@ -10,7 +10,8 @@ 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  <sys/types.h>  
					 
					 
					 
					# include  <sys/types.h>  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  <sys/resource.h>  
					 
					 
					 
					# include  <sys/resource.h>  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  <pthread.h>  
					 
					 
					 
					# include  <map>  
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					# include  <set>  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  <cutils/log.h>  
					 
					 
					 
					# include  <cutils/log.h>  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  <hardware/sensors.h>  
					 
					 
					 
					# include  <hardware/sensors.h>  
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -20,15 +21,14 @@ 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  "common/timing.h"  
					 
					 
					 
					# include  "common/timing.h"  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# include  "common/swaglog.h"  
					 
					 
					 
					# include  "common/swaglog.h"  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// ACCELEROMETER_UNCALIBRATED is only in Android O
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					// https://developer.android.com/reference/android/hardware/Sensor.html#STRING_TYPE_ACCELEROMETER_UNCALIBRATED
  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_ACCELEROMETER 1  
					 
					 
					 
					# define SENSOR_ACCELEROMETER 1  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_MAGNETOMETER 2  
					 
					 
					 
					# define SENSOR_MAGNETOMETER 2  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_GYRO 4  
					 
					 
					 
					# define SENSOR_GYRO 4  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					// ACCELEROMETER_UNCALIBRATED is only in Android O
  
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					// https://developer.android.com/reference/android/hardware/Sensor.html#STRING_TYPE_ACCELEROMETER_UNCALIBRATED
  
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_MAGNETOMETER_UNCALIBRATED 3  
					 
					 
					 
					# define SENSOR_MAGNETOMETER_UNCALIBRATED 3  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_GYRO_UNCALIBRATED 5  
					 
					 
					 
					# define SENSOR_GYRO_UNCALIBRATED 5  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_PROXIMITY 6  
					 
					 
					 
					# define SENSOR_PROXIMITY 6  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# define SENSOR_LIGHT 7  
					 
					 
					 
					# define SENSOR_LIGHT 7  
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -70,30 +70,33 @@ void sensor_loop() { 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      LOGD ( " sensor %4d: %4d %60s  %d-%ld us " ,  i ,  list [ i ] . handle ,  list [ i ] . name ,  list [ i ] . minDelay ,  list [ i ] . maxDelay ) ;   
					 
					 
					 
					      LOGD ( " sensor %4d: %4d %60s  %d-%ld us " ,  i ,  list [ i ] . handle ,  list [ i ] . name ,  list [ i ] . minDelay ,  list [ i ] . maxDelay ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    }   
					 
					 
					 
					    }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_MAGNETOMETER_UNCALIBRATED ,  0 ) ;   
					 
					 
					 
					    std : : set < int >  sensor_types  =  {   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_GYRO_UNCALIBRATED ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_ACCELEROMETER ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_ACCELEROMETER ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_MAGNETOMETER ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_MAGNETIC_FIELD ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_GYRO ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_GYROSCOPE_UNCALIBRATED ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_PROXIMITY ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_GYROSCOPE ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_LIGHT ,  0 ) ;   
					 
					 
					 
					      SENSOR_TYPE_PROXIMITY ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					      SENSOR_TYPE_LIGHT ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_MAGNETOMETER_UNCALIBRATED ,  1 ) ;   
					 
					 
					 
					    } ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_GYRO_UNCALIBRATED ,  1 ) ;   
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_ACCELEROMETER ,  1 ) ;   
					 
					 
					 
					    std : : map < int ,  int64_t >  sensors  =  {   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_MAGNETOMETER ,  1 ) ;   
					 
					 
					 
					      { SENSOR_GYRO_UNCALIBRATED ,  ms2ns ( 10 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_GYRO ,  1 ) ;   
					 
					 
					 
					      { SENSOR_MAGNETOMETER_UNCALIBRATED ,  ms2ns ( 100 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_PROXIMITY ,  1 ) ;   
					 
					 
					 
					      { SENSOR_ACCELEROMETER ,  ms2ns ( 10 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > activate ( device ,  SENSOR_LIGHT ,  1 ) ;   
					 
					 
					 
					      { SENSOR_GYRO ,  ms2ns ( 10 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					      { SENSOR_MAGNETOMETER ,  ms2ns ( 100 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_GYRO_UNCALIBRATED ,  ms2ns ( 10 ) ) ;   
					 
					 
					 
					      { SENSOR_PROXIMITY ,  ms2ns ( 100 ) } ,   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_MAGNETOMETER_UNCALIBRATED ,  ms2ns ( 100 ) ) ;   
					 
					 
					 
					      { SENSOR_LIGHT ,  ms2ns ( 100 ) }   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_ACCELEROMETER ,  ms2ns ( 10 ) ) ;   
					 
					 
					 
					    } ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_GYRO ,  ms2ns ( 10 ) ) ;   
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_MAGNETOMETER ,  ms2ns ( 100 ) ) ;   
					 
					 
					 
					    for  ( auto  & s  :  sensors )  {   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_PROXIMITY ,  ms2ns ( 100 ) ) ;   
					 
					 
					 
					      device - > activate ( device ,  s . first ,  0 ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    device - > setDelay ( device ,  SENSOR_LIGHT ,  ms2ns ( 100 ) ) ;   
					 
					 
					 
					      device - > activate ( device ,  s . first ,  1 ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					      device - > setDelay ( device ,  s . first ,  s . second ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    // TODO: why is this 16?
   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    static  const  size_t  numEvents  =  16 ;   
					 
					 
					 
					    static  const  size_t  numEvents  =  16 ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    sensors_event_t  buffer [ numEvents ] ;   
					 
					 
					 
					    sensors_event_t  buffer [ numEvents ] ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -107,26 +110,14 @@ void sensor_loop() { 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      int  log_events  =  0 ;   
					 
					 
					 
					      int  log_events  =  0 ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      for  ( int  i = 0 ;  i  <  n ;  i + + )  {   
					 
					 
					 
					      for  ( int  i = 0 ;  i  <  n ;  i + + )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        switch  ( buffer [ i ] . type )  {   
					 
					 
					 
					        if  ( sensor_types . find ( buffer [ i ] . type )  ! =  sensor_types . end ( ) )  {   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_ACCELEROMETER :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_MAGNETIC_FIELD :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_GYROSCOPE_UNCALIBRATED :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_GYROSCOPE :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_PROXIMITY :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_LIGHT :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					          log_events + + ;   
					 
					 
					 
					          log_events + + ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					          break ;   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        default :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					          continue ;   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        }   
					 
					 
					 
					        }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      }   
					 
					 
					 
					      }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      uint64_t  log_time  =  nanos_since_boot ( ) ;   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      capnp : : MallocMessageBuilder  msg ;   
					 
					 
					 
					      capnp : : MallocMessageBuilder  msg ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      cereal : : Event : : Builder  event  =  msg . initRoot < cereal : : Event > ( ) ;   
					 
					 
					 
					      cereal : : Event : : Builder  event  =  msg . initRoot < cereal : : Event > ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      event . setLogMonoTime ( log_time ) ;   
					 
					 
					 
					      event . setLogMonoTime ( nanos_since_boot ( ) ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					      auto  sensor_events  =  event . initSensorEvents ( log_events ) ;   
					 
					 
					 
					      auto  sensor_events  =  event . initSensorEvents ( log_events ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -135,21 +126,11 @@ void sensor_loop() { 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        const  sensors_event_t &  data  =  buffer [ i ] ;   
					 
					 
					 
					        const  sensors_event_t &  data  =  buffer [ i ] ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        switch  ( data . type )  {   
					 
					 
					 
					        if  ( sensor_types . find ( data . type )  = =  sensor_types . end ( ) )  {   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_ACCELEROMETER :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_MAGNETIC_FIELD :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_GYROSCOPE_UNCALIBRATED :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_GYROSCOPE :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_PROXIMITY :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        case  SENSOR_TYPE_LIGHT :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					          break ;   
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        default :   
					 
					 
					 
					 
				
			 
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					          continue ;   
					 
					 
					 
					          continue ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        }   
					 
					 
					 
					        }   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        auto  log_event  =  sensor_events [ log_i ] ;   
					 
					 
					 
					        auto  log_event  =  sensor_events [ log_i ] ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        log_event . setSource ( cereal : : SensorEventData : : SensorSource : : ANDROID ) ;   
					 
					 
					 
					        log_event . setSource ( cereal : : SensorEventData : : SensorSource : : ANDROID ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        log_event . setVersion ( data . version ) ;   
					 
					 
					 
					        log_event . setVersion ( data . version ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        log_event . setSensor ( data . sensor ) ;   
					 
					 
					 
					        log_event . setSensor ( data . sensor ) ;