android sensors 总结(四)

4 sensor hal

 

相关结构体:

struct sensor_delay

{

  int handle;

  uint32_t delay;

};

 

struct sensors_data_context_t {

         structsensors_poll_device_t device;

         intevents_fd;

         intdevice_io_fd;

         uint32_tactive_sensors;

         sensors_event_tsensors[MAX_ANDROID_SENSOR_NUM];

         uint32_tpendingSensors;

         int(*activate)(struct sensors_data_context_t *dev,int handle, int enabled);

         int(*set_delay)(structsensors_data_context_t *dev, int handle, int64_t ns);

         int(*poll)(struct sensors_data_context_t *dev, sensors_event_t* values, int count);

};

 

//sensors module info, sensor service会通过此moduleinfo获取平台支持的sensor devices

struct sensors_module_t HAL_MODULE_INFO_SYM= {

   .common = {

       .tag = HARDWARE_MODULE_TAG,

       .version_major = 1,

       .version_minor = 0,

       .id = SENSORS_HARDWARE_MODULE_ID,

       .name = "XXX SENSORS Module",

       .author = "The Android Open Source Project",

       .methods = &sensors_module_methods,

   },

   .get_sensors_list = sensors__get_sensors_list,

};

 

static struct hw_module_methods_tsensors_module_methods = {

   .open = open_sensors

};

 

/** Open a new instance of a sensor deviceusing name */

static int open_sensors(const structhw_module_t* module, const char* name,

       struct hw_device_t** device)

{

         intstatus = -EINVAL;

         ALOGD("%s:name: %s!\r\n", __func__, name);

         pthread_t               thread;

        

         structsensors_data_context_t *dev;

         dev= malloc(sizeof(*dev));

         memset(dev,0, sizeof(*dev));

         dev->events_fd= open_input(O_RDONLY);//打开sensor input dev event

         if(dev->events_fd < 0)

         {

                   ALOGE("%s:Open input device error!",__func__);

            return -1;

         }

         dev->device_io_fd= -1;

         if(dev->device_io_fd<= 0)

         {

                   dev->device_io_fd= open(HWM_SENSOR_DEV, O_RDONLY);//打开sensor dev

                   ALOGD("%s:device handle %d",__func__, dev->device_io_fd);

         }

         dev->activate=  hwm__activate;

         dev->set_delay= hwm__set_delay;

         dev->poll= hwm__poll;

        

         dev->device.common.tag= HARDWARE_DEVICE_TAG;

   dev->device.common.version  =0;

   dev->device.common.module   =(struct hw_module_t*)module;

   dev->device.common.close    =data__close;

   dev->device.activate        =control__activate;

   dev->device.setDelay        =control__setDelay;

   dev->device.poll            =data__poll;

 

   *device = &dev->device.common;

status = 0;

 

          return status;

}

 

 

//三个重要的接口,atcive激活sensors,setdelay设置poll时间,poll获取sensors data

static inthwm__activate(struct sensors_data_context_t *dev,

        int handle, int enabled)

{

          //LOGE("+++++++++++++++++++++++++++++++++++++++++hwm__activatein HAL");

 

          int io_value;

          int err = 0;

          if((handle < SENSORS_HANDLE_BASE)|| (handle >= SENSORS_HANDLE_BASE + ID_SENSOR_MAX_HANDLE))

          {

                   ALOGE("%s: handle %dpara error!",__func__, handle);

                   return -1;

          }

 

          //open_sensor_ctl(dev);

          if(dev->device_io_fd == 0)

          {

                   ALOGE("%s: file handleis null!",__func__);

                   return -1;

          }

         

          ALOGD("%s: handle %d, enable ordisable %d!", __func__, handle, enabled);

         

          uint32_t sensor = (1 << handle);      // new active/inactive sensor

 

          if(enabled == 1)

          {

 

                   // TODO:  Device IO control to enable sensor

                   if(ioctl(dev->device_io_fd,HWM_IO_ENABLE_SENSOR, &handle))//enable sensor

                   {

                            ALOGE("%s:Enable sensor %d error!",__func__, handle);

                            return -1;

                   }

 

                   // When start orientationsensor, should start the G and M first.

                   if(((dev->active_sensors& SENSOR_ORIENTATION) == 0)     //hvae no orientation sensor

                            && (sensor& SENSOR_ORIENTATION))                                       //new orientation sensor start

                   {

                                                       

                            io_value =ID_ACCELEROMETER;

                            if(ioctl(dev->device_io_fd,HWM_IO_ENABLE_SENSOR_NODATA, &io_value))

                            {

                                      ALOGE("%s:Enable ACCELEROMETR sensor error!",__func__);

                                      return -1;

                            }

                           

 

                            io_value =ID_MAGNETIC;

                            if(ioctl(dev->device_io_fd,HWM_IO_ENABLE_SENSOR_NODATA, &io_value))

                            {

                                      ALOGE("%s:Enable MAGNETIC sensor error!",__func__);

                                      return -1;

                            }

                   }

                   dev->active_sensors |=sensor;

          }

          else

          {

                   dev->active_sensors &=~sensor;

 

                   // When stop Orientation,should stop G and M sensor if they are inactive

                   if(((dev->active_sensors& SENSOR_ORIENTATION) == 0)

                            && (sensor& SENSOR_ORIENTATION))

                   {

                           

 

                            io_value =ID_ACCELEROMETER;

                            if(ioctl(dev->device_io_fd,HWM_IO_DISABLE_SENSOR_NODATA, &io_value))

                            {

                                      ALOGE("%s:Disable ACCELEROMETR sensor error!",__func__);

                                      return -1;

                            }

                           

                                              

                            io_value =ID_MAGNETIC;

                            if(ioctl(dev->device_io_fd,HWM_IO_DISABLE_SENSOR_NODATA, &io_value))

                            {

                                      ALOGE("%s:Disable MAGNETIC sensor error!",__func__);

                                      return -1;

                            }

                           

                   }                

                   // TODO: Device IO controldisable sensor

                   if(ioctl(dev->device_io_fd,HWM_IO_DISABLE_SENSOR, &handle))

                   {

                            ALOGE("%s:Disable sensor %d error!",__func__, handle);

                            return -1;

                   }

          }

 

          return 0;

         

}

 

 

static inthwm__set_delay(struct sensors_data_context_t *dev, int handle, int64_t ns)

 

{

          //LOGE("+++++++++++++++++++++++++++++++++++++++++hwm__set_delayin HAL");

          ALOGD("%s: Set delay %lldns", __func__,ns);

          struct sensor_delay delayPara;

          delayPara.delay =  ns/1000000;

          delayPara.handle = handle;

         

          if (dev->device_io_fd <= 0)

          {

                   return -1;

          }

 

          if(delayPara.delay < 10)  //set max sampling rate = 50Hz

    {

        delayPara.delay = 10;

        ALOGD("Control set delay %lld nsis too small \n", ns);

    }       

 

          // TODO: Set delay time to device

          if(ioctl(dev->device_io_fd,HWM_IO_SET_DELAY, &delayPara))

          {

                   ALOGE("%s: Set delay %dms error ", __func__, delayPara.delay);

                   return -errno;

          }

         

          return 0;

}

 

static inthwm__poll(struct sensors_data_context_t *dev, sensors_event_t* values, intcount)

{

          //LOGE("+++++++++++++++++++++++++++++++++++++++++hwm__pollin HAL!");

 

          int res,i, nread;

          hwm_trans_data sensors_data;

          struct input_event event;

         

          int fd_event = dev->events_fd;

          int fd_io = dev->device_io_fd;

          //LOGD("%s: polling get sensordata, fd %d. \r\n", __func__, fd_event);

          if(fd_event < 0)

          {

                   ALOGE("%s: invalid filedescriptor, fd=%d",__func__, fd_event);

                   return -1;

          }

 

          // There are pending sensors, returnthem now..

          if(dev->pendingSensors)

          {

                   return pick_sensor(dev,values, count);

          }

 

          memset(&sensors_data, 0 ,sizeof(hwm_trans_data));

          // read the input event   

          nread = read(fd_event, &event,sizeof(event));

          if(nread == sizeof(event))

          {

                   uint32_t v;

                   //LOGD("%s: event type:%d, code %d, value %d!\r\n", __func__,event.type, event.code,event.value);

                   if(event.type == EV_REL)

                   {

                            if((event.code ==EVENT_TYPE_SENSOR) && (event.value != 0))

                            {

                                      // TODO:get the sensor data;                                    

                                      sensors_data.date_type= event.value;

                                      res =ioctl(fd_io, HWM_IO_GET_SENSORS_DATA, &sensors_data);

                                      if(res !=0)

                                      {

                                               ALOGE("%s:Get sensors data error",__func__);

                                      }

                                      else

                                      {

                                               for(i=0; i < MAX_ANDROID_SENSOR_NUM; i++)

                                               {

                                                        //LOGD("%s:getsensor value,type: %d, value1 %d, updata %d!\r\n", __func__,

                                                                  //sensors_data.data[i].sensor,sensors_data.data[i].values[0], sensors_data.data[i].update);

                                                        if(sensors_data.data[i].update== 0)

                                                        {

                                                                  continue;

                                                        }

                                                        dev->pendingSensors|= 1 << i;

                                                        dev->sensors[i].sensor= sensors_data.data[i].sensor;

                                                        dev->sensors[i].type= dev->sensors[i].sensor + 1;

                                                        dev->sensors[i].data[0]= (float)sensors_data.data[i].values[0];

                                                        dev->sensors[i].data[1]= (float)sensors_data.data[i].values[1];

                                                        dev->sensors[i].data[2]= (float)sensors_data.data[i].values[2];

                                                        dev->sensors[i].acceleration.status= sensors_data.data[i].status;

                                                        dev->sensors[i].timestamp= sensors_data.data[i].time;

                                                        //{@for M-sensor Accurancy Debug log

                                                        //sensors_debug_thread_func(i,dev);

                                                        if(1== i)

                                                        {

                                                           pthread_mutex_lock(&g_hMutex);

                                                           if(pthread_cond_signal(&g_sensor_event_cond))

                           {

                             ALOGE("%s:set signal error",__func__);

                           }

                                                           pthread_mutex_unlock(&g_hMutex);

                                                           ALOGD("%s: set signal  \r\n",__func__);

                                                        }

                                     

                                                        if(sensors_data.data[i].value_divide> 1)

                                                        {

                                                                 

                                                                  dev->sensors[i].data[0]= dev->sensors[i].data[0] / sensors_data.data[i].value_divide;

                                                                  dev->sensors[i].data[1]= dev->sensors[i].data[1] / sensors_data.data[i].value_divide;

                                                                  dev->sensors[i].data[2]= dev->sensors[i].data[2] / sensors_data.data[i].value_divide;

                                     

                                                        }       

                                               }

                                      }

                            }

                            else

                            {

 

                            }

                   }

          }

 

          if(dev->pendingSensors)

          {

                   return pick_sensor(dev,values, count);

          }

 

          return 0;

}

你可能感兴趣的:(android sensors 总结(四))