sensor----HAL分析

sensor----framework(一)的后面,我们跟进到了mSensorDevice->activate

,下面我们就来具体看看这个怎么样链接到HAL,DRIVER里面去的。如何使能一个SENOSR的。

step 1:

\frameworks\native\services\sensorservice\SensorDevice.cpp

 

SensorDevice::SensorDevice()

   :  mSensorDevice(0),

      mSensorModule(0)

{

/*

 HAL层的SENSOR MODULE中获取mSensorModule

*/

    status_t err =hw_get_module(SENSORS_HARDWARE_MODULE_ID,

            (hw_module_tconst**)&mSensorModule);

 

   ALOGE_IF(err, "couldn't load %s module (%s)",

           SENSORS_HARDWARE_MODULE_ID, strerror(-err));

 

   if (mSensorModule) {

       err = sensors_open_1(&mSensorModule->common,&mSensorDevice);

 

       ALOGE_IF(err, "couldn't open device for module %s (%s)",

                SENSORS_HARDWARE_MODULE_ID,strerror(-err));

 

       if (mSensorDevice) {

           if (mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_1||

               mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_2) {

                ALOGE(">>>>WARNING <<< Upgrade sensor HAL to version 1_3");

           }

 

           sensor_t const* list;

            ssize_t count =mSensorModule->get_sensors_list(mSensorModule, &list);

           mActivationCount.setCapacity(count);

           Info model;

           for (size_t i=0 ; i

               mActivationCount.add(list[i].handle, model);

                mSensorDevice->activate(

                       reinterpret_cast(mSensorDevice),

                        list[i].handle, 0);

           }

       }

    }

}

 

STEP 2:

\vendor\mediatek\proprietary\hardware\sensor\sensors.c

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 = "MTK SENSORS Module",

       .author = "Mediatek",

       .methods = &sensors_module_methods,

   },

   .get_sensors_list = sensors__get_sensors_list,

};

 

step 3:

hardware\libhardware\include\hardware\ sensors.h

 

static inline int sensors_open_1(conststruct hw_module_t* module,

       sensors_poll_device_1_t** device) {

    return module->methods->open(module,

            SENSORS_HARDWARE_POLL, (structhw_device_t**)device);

}

/*

这里的module->methods->open当然对于的就是senosr.c中的openstatic structhw_module_methods_t sensors_module_methods = {

    .open = open_sensors

};

*/

step 4:

\vendor\mediatek\proprietary\hardware\sensor\sensors.c

 

 

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

       struct hw_device_t** device)

{

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

 

  return init_nusensors(module, device);

}

 

step 5:

 

\vendor\mediatek\proprietary\hardware\sensor\nusensors.cpp

int init_nusensors(hw_module_t const*module, hw_device_t** device)

{

   int status = -EINVAL;

 

    dev = new sensors_poll_context_t();

/*

new 这个sensors_poll_context_t的时候,会初始化各个类型的senosr.还有其对于的FD,后续就是通过这个FD来访问DRIVERENABLE的。

sensors_poll_context_t::sensors_poll_context_t()

{

    memset(&device, 0, sizeof(device));

 

    mSensors[hwmsen] = new Hwmsen();

    mPollFds[hwmsen].fd = ((Hwmsen*)mSensors[hwmsen])->mdata_fd;

    mPollFds[hwmsen].events = POLLIN;

    mPollFds[hwmsen].revents = 0;

 

    mSensors[accel] = new AccelerationSensor();

    mPollFds[accel].fd =((AccelerationSensor*)mSensors[accel])->mdata_fd;

    mPollFds[accel].events = POLLIN;

    mPollFds[accel].revents = 0;

 

    mSensors[magnetic] = new MagneticSensor();

    mPollFds[magnetic].fd =((MagneticSensor*)mSensors[magnetic])->mdata_fd;

    mPollFds[magnetic].events = POLLIN;

    mPollFds[magnetic].revents = 0;

 

    mSensors[proximity] = new ProximitySensor();

    mPollFds[proximity].fd =((ProximitySensor*)mSensors[proximity])->mdata_fd;

    mPollFds[proximity].events = POLLIN;

    mPollFds[proximity].revents = 0;

 

    mSensors[light] = new AmbiLightSensor();

    mPollFds[light].fd =((AmbiLightSensor*)mSensors[light])->mdata_fd;

    mPollFds[light].events = POLLIN;

    mPollFds[light].revents = 0;

 

    mSensors[gyro] = new GyroscopeSensor();

    mPollFds[gyro].fd =((GyroscopeSensor*)mSensors[gyro])->mdata_fd;

    mPollFds[gyro].events = POLLIN;

    mPollFds[gyro].revents = 0;

 

    mSensors[pressure] = new PressureSensor();

    mPollFds[pressure].fd =((PressureSensor*)mSensors[pressure])->mdata_fd;

    mPollFds[pressure].events = POLLIN;

    mPollFds[pressure].revents = 0;

 

    mSensors[temperature] = newTempratureSensor();

    mPollFds[temperature].fd =((TempratureSensor*)mSensors[temperature])->mdata_fd;

    mPollFds[temperature].events = POLLIN;

    mPollFds[temperature].revents = 0;

 

    mSensors[humidity] = new HumiditySensor();

    mPollFds[humidity].fd =((HumiditySensor*)mSensors[humidity])->mdata_fd;

    mPollFds[humidity].events = POLLIN;

    mPollFds[humidity].revents = 0;

 

    mSensors[stepcounter] = newStepCounterSensor();

    mPollFds[stepcounter].fd =((StepCounterSensor*)mSensors[stepcounter])->mdata_fd;

    mPollFds[stepcounter].events = POLLIN;

    mPollFds[stepcounter].revents = 0;

 

    mSensors[batchsensor] = new BatchSensor();

    mPollFds[batchsensor].fd =((BatchSensor*)mSensors[batchsensor])->mdata_fd;

    mPollFds[batchsensor].events = POLLIN;

    mPollFds[batchsensor].revents = 0;

 

         mSensors[pedometer] = newPedometerSensor();

    mPollFds[pedometer].fd =((PedometerSensor*)mSensors[pedometer])->mdata_fd;

    mPollFds[pedometer].events = POLLIN;

    mPollFds[pedometer].revents = 0;

 

         mSensors[activity] = newActivitySensor();

    mPollFds[activity].fd =((ActivitySensor*)mSensors[activity])->mdata_fd;

    mPollFds[activity].events = POLLIN;

    mPollFds[activity].revents = 0;

 

         mSensors[in_pocket] = newInPocketSensor();

    mPollFds[in_pocket].fd =((InPocketSensor*)mSensors[in_pocket])->mdata_fd;

    mPollFds[in_pocket].events = POLLIN;

    mPollFds[in_pocket].revents = 0;

 

         mSensors[pick_up] = new PickUpSensor();

    mPollFds[pick_up].fd = ((PickUpSensor*)mSensors[pick_up])->mdata_fd;

    mPollFds[pick_up].events = POLLIN;

    mPollFds[pick_up].revents = 0;

 

         mSensors[face_down] = newFaceDownSensor();

    mPollFds[face_down].fd =((FaceDownSensor*)mSensors[face_down])->mdata_fd;

    mPollFds[face_down].events = POLLIN;

    mPollFds[face_down].revents = 0;

 

         mSensors[shake] = new ShakeSensor();

    mPollFds[shake].fd =((ShakeSensor*)mSensors[shake])->mdata_fd;

    mPollFds[shake].events = POLLIN;

    mPollFds[shake].revents = 0;

 

    mSensors[heartrate] = newHeartRateSensor();

    mPollFds[heartrate].fd =((HeartRateSensor*)mSensors[heartrate])->mdata_fd;

    mPollFds[heartrate].events = POLLIN;

    mPollFds[heartrate].revents = 0;

 

    mSensors[tilt] = new TiltSensor();

    mPollFds[tilt].fd =((TiltSensor*)mSensors[tilt])->mdata_fd;

    mPollFds[tilt].events = POLLIN;

    mPollFds[tilt].revents = 0;

 

    mSensors[wake_gesture] = newWakeGestureSensor();

    mPollFds[wake_gesture].fd =((WakeGestureSensor*)mSensors[wake_gesture])->mdata_fd;

    mPollFds[wake_gesture].events = POLLIN;

    mPollFds[wake_gesture].revents = 0;

 

    mSensors[glance_gesture] = newGlanceGestureSensor();

    mPollFds[glance_gesture].fd =((GlanceGestureSensor*)mSensors[glance_gesture])->mdata_fd;

    mPollFds[glance_gesture].events = POLLIN;

    mPollFds[glance_gesture].revents = 0;

 

    mSensors[linear_acceleration] = newLinearaccelSensor();

    mPollFds[linear_acceleration].fd =((LinearaccelSensor*)mSensors[linear_acceleration])->mdata_fd;

    mPollFds[linear_acceleration].events =POLLIN;

    mPollFds[linear_acceleration].revents = 0;

 

    mSensors[rotation_vector] = newRotationVectorSensor();

    mPollFds[rotation_vector].fd =((RotationVectorSensor*)mSensors[rotation_vector])->mdata_fd;

    mPollFds[rotation_vector].events = POLLIN;

    mPollFds[rotation_vector].revents = 0;

 

    mSensors[game_rotation_vector] = newGameRotationVectorSensor();

    mPollFds[game_rotation_vector].fd =((GameRotationVectorSensor*)mSensors[game_rotation_vector])->mdata_fd;

    mPollFds[game_rotation_vector].events =POLLIN;

    mPollFds[game_rotation_vector].revents = 0;

 

    mSensors[gravity] = new GravitySensor();

    mPollFds[gravity].fd =((GravitySensor*)mSensors[gravity])->mdata_fd;

    mPollFds[gravity].events = POLLIN;

    mPollFds[gravity].revents = 0;

 

    mSensors[geomagnetic_rotation_vector] = newGeomagneticRotationVectorSensor();

    mPollFds[geomagnetic_rotation_vector].fd =((GeomagneticRotationVectorSensor*)mSensors[geomagnetic_rotation_vector])->mdata_fd;

   mPollFds[geomagnetic_rotation_vector].events = POLLIN;

   mPollFds[geomagnetic_rotation_vector].revents = 0;

 

    mSensors[bringtosee] = newBringtoseeSensor();

    mPollFds[bringtosee].fd =((BringtoseeSensor*)mSensors[bringtosee])->mdata_fd;

    mPollFds[bringtosee].events = POLLIN;

    mPollFds[bringtosee].revents = 0;

 

    int wakeFds[2];

    int result = pipe(wakeFds);

    if (result < 0) {

        ALOGE_IF(result < 0, "errorcreating wake pipe (%s)", strerror(errno));

    mWritePipeFd = -1;

    } else {

        result = fcntl(wakeFds[0], F_SETFL,O_NONBLOCK);

        ALOGE_IF(result < 0,"fcntl(wakeFds[0] fail (%s)", strerror(errno));

        result = fcntl(wakeFds[1], F_SETFL,O_NONBLOCK);

        ALOGE_IF(result < 0,"fcntl(wakeFds[1] fail (%s)", strerror(errno));

        mWritePipeFd = wakeFds[1];

    }

 

    mPollFds[wake].fd = wakeFds[0];

    mPollFds[wake].events = POLLIN;

    mPollFds[wake].revents = 0;

}

*/

   memset(&dev->device, 0, sizeof(sensors_poll_device_1));

 

   dev->device.common.tag = HARDWARE_DEVICE_TAG;

#if defined(SENSOR_BATCH_SUPPORT) ||defined(CUSTOM_KERNEL_SENSORHUB)

   dev->device.common.version  =SENSORS_DEVICE_API_VERSION_1_1;

#else

         dev->device.common.version  = SENSORS_DEVICE_API_VERSION_1_0;

#endif

   dev->device.common.module   =const_cast(module);

   dev->device.common.close    =poll__close;

/*

mSensorDevice->activate对应的就是这个poll__activate

*/

   dev->device.activate        =poll__activate;

   dev->device.setDelay        =poll__setDelay;

   dev->device.poll            =poll__poll;

   dev->device.batch           =poll__batch;

   dev->device.flush            =poll__flush;

 

   *device = &dev->device.common;

   status = 0;

   return status;

}

 

step 6:

\vendor\mediatek\proprietary\hardware\sensor\nusensors.cpp

 

static int poll__activate(structsensors_poll_device_t *dev,

       int handle, int enabled) {

   sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;

   return ctx->activate(handle-ID_OFFSET,enabled);

}

 

step 7:

\vendor\mediatek\proprietary\hardware\sensor\nusensors.cpp

 

 

int sensors_poll_context_t::activate(inthandle, int enabled)

{

   ALOGD( "activate handle =%d, enable = %d",handle, enabled );

   int err=0;

 

    int index = handleToDriver(handle);

 /*

这里只是做一个映射:

    int handleToDriver(int handle) const {

    ALOGE("handleToDriverhandle(%d)\n",handle);

        switch (handle) {

            case ID_ACCELEROMETER:

                 return accel;

            case ID_MAGNETIC:

            case ID_ORIENTATION:

                 return magnetic;

            case ID_PROXIMITY:

                 return proximity;

            case ID_LIGHT:

                 return light;

            case ID_GYROSCOPE:

                 return gyro;

        case ID_PRESSURE:

                 return pressure;

        case ID_TEMPRERATURE:

                 return temperature;

        case ID_HUMIDITY:

                 return humidity;

        case ID_STEP_COUNTER:

        case ID_STEP_DETECTOR:

        case ID_SIGNIFICANT_MOTION:

                 return stepcounter;

            case ID_PEDOMETER:

                                      return pedometer;

            case ID_IN_POCKET:

                                      return in_pocket;

            case ID_ACTIVITY:

                                      return activity;

            case ID_PICK_UP_GESTURE:

                                      return pick_up;

            case ID_FACE_DOWN:

                                      return face_down;

            case ID_SHAKE:

                                      return shake;

            case ID_HEART_RATE:

                 return heartrate;

            case ID_TILT_DETECTOR:

                 return tilt;

            case ID_WAKE_GESTURE:

                 return wake_gesture;

            case ID_GLANCE_GESTURE:

                 return glance_gesture;

            case ID_LINEAR_ACCELERATION:

                 return linear_acceleration;

            case ID_ROTATION_VECTOR:

                 return rotation_vector;

            case ID_GAME_ROTATION_VECTOR:

                 return game_rotation_vector;

            case ID_GRAVITY:

                 return gravity;

            caseID_GEOMAGNETIC_ROTATION_VECTOR:

                 returngeomagnetic_rotation_vector;

            case ID_BRINGTOSEE:

                 return bringtosee;

        default:

                break;

                 //return pressure;

        }

        return -EINVAL;

    }

*/

   //

   if(ID_ORIENTATION == handle)

    {

        //ALOGD( "fwq1111" );

       ((AccelerationSensor*)(mSensors[accel]))->enableNoHALDataAcc(enabled);

       ((Hwmsen*)(mSensors[hwmsen]))->enableNoHALDataAcc(enabled);

    }

 

   if( (index >= numSensorDrivers) || (index < 0) )

    {

       ALOGD("activate error index=%d\n", index);

       return 0;

    }

   if(NULL != mSensors[index])

    {

      ALOGD( "use new sensor index=%d, mSensors[index](%x)", index,mSensors[index]);

      if(this->device.common.version >= SENSORS_DEVICE_API_VERSION_1_1)

      {

               ALOGD("support batch active\n" );

               mSensors[batchsensor]->enable(handle,enabled);

      }

/*

重点是下面的函数,我们已MSENSOR为例往下面看看他的enable.

            case ID_MAGNETIC:

            case ID_ORIENTATION:

                 return magnetic;

*/

      err=  mSensors[index]->enable(handle,enabled);

    }

 

   if(err || index<0 )

    {

         ALOGD("use old sensor err(%d),index(%d) go to oldhwmsen\n",err,index);

       // notify to hwmsen sensor to support old architecture

       err = mSensors[hwmsen]->enable(handle, enabled);

    }

 

   if (enabled && !err) {

       const char wakeMessage(WAKE_MESSAGE);

       int result = write(mWritePipeFd, &wakeMessage, 1);

       ALOGE_IF(result<0, "error sending wake message (%s)",strerror(errno));

    }

   return err;

}

 

 

step 8:

\vendor\mediatek\proprietary\hardware\sensor\Magnetic.cpp

 

int MagneticSensor::enable(int32_t handle,int en)

{

   int fd=-1;

   int flags = en ? 1 : 0;

   int err=0;

   char buf[2]={0};

   int index=0;

   ALOGD("fwq enable: handle:%d, en:%d \r\n",handle,en);

   if(ID_ORIENTATION == handle)

    {

      strcpy(&input_sysfs_path[input_sysfs_path_len],"magoactive");

      index = Orientation;

    }

   if(ID_MAGNETIC== handle)

    {

/*

input_sysfs_pathMagneticSensor的构造函数中定义,被赋值:/sys/class/misc/m_mag_misc/

,然后在strcpy magactive就得到了节点的完整路径。

MagneticSensor::MagneticSensor()

    : SensorBase(NULL,"m_mag_input"),//ACC_INPUTDEV_NAME

      mEnabled(0),

      mOrientationEnabled(0),

      mInputReader(32),

      mPendingMask(0)

{

    input_sysfs_path_len = 0;

    memset(input_sysfs_path, 0, PATH_MAX);

    mPendingEvent[0].version =sizeof(sensors_event_t);

    mPendingEvent[0].sensor = ID_MAGNETIC;

    mPendingEvent[0].type =SENSOR_TYPE_MAGNETIC_FIELD;

    mPendingEvent[0].magnetic.status =SENSOR_STATUS_ACCURACY_HIGH;

    memset(mPendingEvent[0].data, 0x00,sizeof(mPendingEvent[0].data));

 

    mPendingEvent[1].version =sizeof(sensors_event_t);

    mPendingEvent[1].sensor = ID_ORIENTATION;

    mPendingEvent[1].type =SENSOR_TYPE_ORIENTATION;

    mPendingEvent[1].magnetic.status =SENSOR_STATUS_ACCURACY_HIGH;

    memset(mPendingEvent[1].data, 0x00,sizeof(mPendingEvent[1].data));

 

    mDataDiv_M = 1;

    mDataDiv_O = 1;

    mEnabledTime = 0;

    mMagSensorDebug = NULL;

    chardatapath1[64]={"/sys/class/misc/m_mag_misc/magactive"};

    int fd_m = -1;

    charbuf_m[64]={0};

    int len_m;

 

    mdata_fd = FindDataFd();

    if (mdata_fd >= 0) {

        strcpy(input_sysfs_path,"/sys/class/misc/m_mag_misc/");

        input_sysfs_path_len =strlen(input_sysfs_path);

    }

    else

    {

        ALOGE("couldn't find input device");

        return;

    }

    ALOGD("mag misc path =%s",input_sysfs_path);

 

    fd_m = open(datapath1, O_RDWR);

    if (fd_m >= 0)

    {

        len_m =read(fd_m,buf_m,sizeof(buf_m)-1);

        if (len_m <= 0)

        {

            ALOGD("read div err, len_m =%d", len_m);

        }

        else

        {

            buf_m[len_m] = '\0';

            sscanf(buf_m, "%d",&mDataDiv_M);

            ALOGD("read div buf(%s),mdiv_M %d",datapath1,mDataDiv_M);

        }

       close(fd_m);

    }

    else

    {

    ALOGE("open mag misc path %s fail", datapath1);

    }

 

    chardatapath2[64]={"/sys/class/misc/m_mag_misc/magoactive"};

    int fd_o = open(datapath2, O_RDWR);

    char buf_o[64]={0};

    int len_o=0;

    if (fd_o >= 0)

    {

        len_o =read(fd_o,buf_o,sizeof(buf_o)-1);

        if (len_o <= 0)

        {

            ALOGD("read div err, len_o =%d", len_o);

        }

        else

        {

            buf_o[len_o] = '\0';

            sscanf(buf_o, "%d",&mDataDiv_O);

            ALOGD("read div buf(%s),mdiv_O %d",datapath2,mDataDiv_O);

        }

        close(fd_o);

    }

    else

    {

    ALOGE("open mag_o misc path %s fail", datapath2);

    }

 

    //{@input value 2 stands for MagneticFieldsensor,please refer to nusensor.cpp

    //enmu in sensors_poll_context_t to seeother sensors,this is for sensor debug log

    //mMagSensorDebug = newSensorDebugObject((SensorBase*)this, 2);

    //@}

 

}

*/

      strcpy(&input_sysfs_path[input_sysfs_path_len],"magactive");

      index = MagneticField;

    }

   ALOGD("handle(%d),path:%s \r\n",handle,input_sysfs_path);

    fd = open(input_sysfs_path, O_RDWR);

   if(fd<0)

    {

       ALOGD("no magntic enable attr \r\n");

       return -1;

    }

 

   if(0== en )

    {

      mEnabled &= ~(1<

      buf[1] = 0;

      buf[0] = '0';

    }

 

   if(1== en)

    {

      mEnabled |= (1<

      buf[1] = 0;

      buf[0] = '1';

    }

/*

/sys/class/misc/m_mag_misc/magactive写值,就像我们用ADB的时候,ECHO一个值到节点是一样的。而这个节点是我们在kernel里面所创建的。

*/

    write(fd, buf,sizeof(buf));

     close(fd);

 

   ALOGD("mag(%d) mEnabled(0x%x) ----\r\n",handle,mEnabled);

   return 0;

}

 

 

step 9:

/*

进入到KERNEL里面了,后面的另外有过介绍了。

DEVICE_ATTR(magactive,       S_IWUSR | S_IRUGO, mag_show_active,mag_store_active);

*/

kernel-3.18\drivers\misc\mediatek\magnetometer\mag.c

 

static ssize_t mag_store_active(structdevice *dev, struct device_attribute *attr,

                                       const char *buf, size_t count)

{

         structmag_context *cxt = NULL;

 

         MAG_LOG("mag_store_activebuf=%s\n", buf);

         mutex_lock(&mag_context_obj->mag_op_mutex);

         cxt= mag_context_obj;

         if(NULL == cxt->mag_ctl.m_enable) {

                   mutex_unlock(&mag_context_obj->mag_op_mutex);

                   MAG_LOG("mag_ctlpath is NULL\n");

                   returncount;

         }

 

         if (!strncmp(buf, "1", 1))

                   mag_enable_data(ID_M_V_MAGNETIC, 1);

         else if (!strncmp(buf, "0", 1))

                   mag_enable_data(ID_M_V_MAGNETIC, 0);

         else

                   MAG_ERR("mag_store_active error !!\n");

 

         mutex_unlock(&mag_context_obj->mag_op_mutex);

         MAG_LOG("mag_store_active done\n");

         returncount;

}

你可能感兴趣的:(sensor学习)