• (OK) android-5.0 sensor工作原理—sensorservice的启动(一)



    http://blog.csdn.net/zsj100213/article/details/48179655


    sensorservice的启动:

    1. systemserver.Java的run函数:

    1.  private void run() {  
    2.   
    3. ……  
    4.   
    5.         // Initialize native services.  
    6.         System.loadLibrary("android_servers");  
    7.         nativeInit();  
    8.   
    9. ……  
    10.   
    11. }  

       Android在启动之后通过Zygote来管理和启动android server,他首先会运行SystemServer.java里面的run函数,这里主要是load android_server.so库,然后调用nativeInit()来启动sensorservice。


    2. nativeInit()是本地方法,上层Java调用本地C++方法,需要通过JNI层的转化,在com_android_server_SystemServer.cpp中:

    1. static void android_server_SystemServer_nativeInit(JNIEnv* env, jobject clazz) {  
    2.     char propBuf[PROPERTY_VALUE_MAX];  
    3.     property_get("system_init.startsensorservice", propBuf, "1");  
    4.     if (strcmp(propBuf, "1") == 0) {  
    5.         // Start the sensor service  
    6.         SensorService::instantiate();  
    7.     }  
    8. }  
    9.   
    10. /*  
    11.  * JNI registration.  
    12.  */  
    13. static JNINativeMethod gMethods[] = {  
    14.     /* name, signature, funcPtr */  
    15.     { "nativeInit", "()V", (void*) android_server_SystemServer_nativeInit },  
    16. };  
    17.   
    18. int register_android_server_SystemServer(JNIEnv* env)  
    19. {  
    20.     return jniRegisterNativeMethods(env, "com/android/server/SystemServer",  
    21.             gMethods, NELEM(gMethods));  
    22. }  

    首先建立nativeInit()方法对本地方法android_server_SystemServer_nativeInit()的映射,然后jniRegisterNativeMethods注册该方法,在上层调用nativeInit()方法就可以调用本地方法android_server_SystemServer_nativeInit()通过SensorService::instantiate();来实例化sensorservice,主要是向servicemanager注册sensorservice。

    1.   static status_t publish(bool allowIsolated = false) {  
    2.       sp<IServiceManager> sm(defaultServiceManager());  
    3.       return sm->addService(  
    4.               String16(SERVICE::getServiceName()),  
    5.               new SERVICE(), allowIsolated);  
    6.   }  
    7. static void instantiate() { publish(); }  

    addservice()方法主要是完成向servicemanager注册sensorservice服务,原型如下:

    1. virtual status_t            addService( const String16& name,  
    2.                                            const sp<IBinder>& service,  
    3.                                            bool allowIsolated = false) = 0;  

    其第二个参数也就是new SERVICE()是强引用sp sensorservice,所以会调用到SensorService::onFirstRef(),这样就开始了sensorservice的一系列初始化工作。

    3. SensorService::onFirstRef()

    1. void SensorService::onFirstRef()  
    2. {  
    3.     ALOGD("nuSensorService starting...");  
    4.   
    5.     SensorDevice& dev(SensorDevice::getInstance());  
    6.   
    7.     if (dev.initCheck() == NO_ERROR) {  
    8.         sensor_t const* list;  
    9.         ssize_t count = dev.getSensorList(&list);  
    10.         if (count > 0) {  
    11.             ssize_t orientationIndex = -1;  
    12.             bool hasGyro = false;  
    13.             uint32_t virtualSensorsNeeds =  
    14.                     (1<<SENSOR_TYPE_GRAVITY) |  
    15.                     (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |  
    16.                     (1<<SENSOR_TYPE_ROTATION_VECTOR);  
    17.   
    18.             mLastEventSeen.setCapacity(count);  
    19.             for (ssize_t i=0 ; i<count ; i++) {  
    20.                 registerSensor( new HardwareSensor(list[i]) );  
    21.                 switch (list[i].type) {  
    22.                     case SENSOR_TYPE_ORIENTATION:  
    23.                         orientationIndex = i;  
    24.                         break;  
    25.                     case SENSOR_TYPE_GYROSCOPE:  
    26.                     case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:  
    27.                         hasGyro = true;  
    28.                         break;  
    29.                     case SENSOR_TYPE_GRAVITY:  
    30.                     case SENSOR_TYPE_LINEAR_ACCELERATION:  
    31.                     case SENSOR_TYPE_ROTATION_VECTOR:  
    32.                         virtualSensorsNeeds &= ~(1<<list[i].type);  
    33.                         break;  
    34.                 }  
    35.             }  
    36.   
    37.             // it's safe to instantiate the SensorFusion object here  
    38.             // (it wants to be instantiated after h/w sensors have been  
    39.             // registered)  
    40.             const SensorFusion& fusion(SensorFusion::getInstance());  
    41.   
    42.             // build the sensor list returned to users  
    43.             mUserSensorList = mSensorList;  
    44.   
    45.             if (hasGyro) {  
    46.                 Sensor aSensor;  
    47.   
    48.                 // Add Android virtual sensors if they're not already  
    49.                 // available in the HAL  
    50.   
    51.                 aSensor = registerVirtualSensor( new RotationVectorSensor() );  
    52.                 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {  
    53.                     mUserSensorList.add(aSensor);  
    54.                 }  
    55.   
    56.                 aSensor = registerVirtualSensor( new GravitySensor(list, count) );  
    57.                 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {  
    58.                     mUserSensorList.add(aSensor);  
    59.                 }  
    60.   
    61.                 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );  
    62.                 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {  
    63.                     mUserSensorList.add(aSensor);  
    64.                 }  
    65.   
    66.                 aSensor = registerVirtualSensor( new OrientationSensor() );  
    67.                 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {  
    68.                     // if we are doing our own rotation-vector, also add  
    69.                     // the orientation sensor and remove the HAL provided one.  
    70.                     mUserSensorList.replaceAt(aSensor, orientationIndex);  
    71.                 }  
    72.   
    73.                 // virtual debugging sensors are not added to mUserSensorList  
    74.                 registerVirtualSensor( new CorrectedGyroSensor(list, count) );  
    75.                 registerVirtualSensor( new GyroDriftSensor() );  
    76.             }  
    77.   
    78.             // debugging sensor list  
    79.             mUserSensorListDebug = mSensorList;  
    80.   
    81.             // Check if the device really supports batching by looking at the FIFO event  
    82.             // counts for each sensor.  
    83.             bool batchingSupported = false;  
    84.             for (int i = 0; i < mSensorList.size(); ++i) {  
    85.                 if (mSensorList[i].getFifoMaxEventCount() > 0) {  
    86.                     batchingSupported = true;  
    87.                     break;  
    88.                 }  
    89.             }  
    90.   
    91.             if (batchingSupported) {  
    92.                 // Increase socket buffer size to a max of 100 KB for batching capabilities.  
    93.                 mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;  
    94.             } else {  
    95.                 mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;  
    96.             }  
    97.   
    98.             // Compare the socketBufferSize value against the system limits and limit  
    99.             // it to maxSystemSocketBufferSize if necessary.  
    100.             FILE *fp = fopen("/proc/sys/net/core/wmem_max""r");  
    101.             char line[128];  
    102.             if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {  
    103.                 line[sizeof(line) - 1] = '';  
    104.                 size_t maxSystemSocketBufferSize;  
    105.                 sscanf(line, "%zu", &maxSystemSocketBufferSize);  
    106.                 if (mSocketBufferSize > maxSystemSocketBufferSize) {  
    107.                     mSocketBufferSize = maxSystemSocketBufferSize;  
    108.                 }  
    109.             }  
    110.             if (fp) {  
    111.                 fclose(fp);  
    112.             }  
    113.   
    114.             mWakeLockAcquired = false;  
    115.             mLooper = new Looper(false);  
    116.             const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;  
    117.             mSensorEventBuffer = new sensors_event_t[minBufferSize];  
    118.             mSensorEventScratch = new sensors_event_t[minBufferSize];  
    119.             mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize];  
    120.   
    121.             mInitCheck = NO_ERROR;  
    122.             run("SensorService", PRIORITY_URGENT_DISPLAY);  
    123.         }  
    124.     }  
    125. }  
    3.1 首先是sensorDevice的实例化:

    1. SensorDevice::SensorDevice()  
    2.     :  mSensorDevice(0),  
    3.        mSensorModule(0)  
    4. {  
    5.     status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,  
    6.             (hw_module_t const**)&mSensorModule);  
    7.     ALOGE_IF(err, "couldn't load %s module (%s)",  
    8.             SENSORS_HARDWARE_MODULE_ID, strerror(-err));  
    9.   
    10.     if (mSensorModule) {  
    11.         err = sensors_open_1(&mSensorModule->common, &mSensorDevice);  
    12.   
    13.         ALOGE_IF(err, "couldn't open device for module %s (%s)",  
    14.                 SENSORS_HARDWARE_MODULE_ID, strerror(-err));  
    15.   
    16.         if (mSensorDevice) {  
    17.             if (mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_1 ||  
    18.                 mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_2) {  
    19.                 ALOGE(">>>> WARNING <<< Upgrade sensor HAL to version 1_3");  
    20.             }  
    21.   
    22.             sensor_t const* list;  
    23.             ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);  
    24.             mActivationCount.setCapacity(count);  
    25.             Info model;  
    26.             for (size_t i=0 ; i<size_t(count) ; i++) {  
    27.                 mActivationCount.add(list[i].handle, model);  
    28.                 mSensorDevice->activate(  
    29.                         reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),  
    30.                         list[i].handle, 0);  
    31.             }  
    32.         }  
    33.     }  
    34. }  

    sensorDevice的实例化主要完成以下三项工作:

    3.1.1. 主要是为了从/system/lib/hw或者/vendor/lib/hw路径load一个sensors.(platform).so的库文件,如sensors.mt6753.so,第一个参数是我们所获取的hardware模块的名字,第二个参数是我们所要获得的hw_module_t。

    1. hw_get_module(SENSORS_HARDWARE_MODULE_ID,  
    2.             (hw_module_t const**)&mSensorModule);  

    load()首先是通过dlopen()函数载入前面获取的path的动态链接库,如sensors.mt6753.so,然后使用dlsym()函数来获取.so文件中的符号HMI的地址,最后dlsym()函数返回hw_module_t的地址hmi,最后将hmi的值赋给参数*pHmi。load()函数的代码如下:

    1. /** 
    2.  * Name of the hal_module_info 
    3.  */  
    4. #define HAL_MODULE_INFO_SYM         HMI  
    5.   
    6. /** 
    7.  * Name of the hal_module_info as a string 
    8.  */  
    9. #define HAL_MODULE_INFO_SYM_AS_STR  "HMI"     //获取符号HMI的地址,然后dlsym()函数返回的是hw_module_t()的  


    1. static int load(const char *id,  
    2.         const char *path,  
    3.         const struct hw_module_t **pHmi)  
    4. {  
    5.     int status;  
    6.     void *handle;  
    7.     struct hw_module_t *hmi;  
    8.   
    9.     /* 
    10.      * load the symbols resolving undefined symbols before 
    11.      * dlopen returns. Since RTLD_GLOBAL is not or'd in with 
    12.      * RTLD_NOW the external symbols will not be global 
    13.      */  
    14.     handle = dlopen(path, RTLD_NOW);  
    15.     if (handle == NULL) {  
    16.         char const *err_str = dlerror();  
    17.         ALOGE("load: module=%s %s", path, err_str?err_str:"unknown");  
    18.         status = -EINVAL;  
    19.         goto done;  
    20.     }  
    21.   
    22.     /* Get the address of the struct hal_module_info. */  
    23.     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;  
    24.     hmi = (struct hw_module_t *)dlsym(handle, sym);  
    25.     if (hmi == NULL) {  
    26.         ALOGE("load: couldn't find symbol %s", sym);  
    27.         status = -EINVAL;  
    28.         goto done;  
    29.     }  
    30.   
    31.     /* Check that the id matches */  
    32.     if (strcmp(id, hmi->id) != 0) {  
    33.         ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);  
    34.         status = -EINVAL;  
    35.         goto done;  
    36.     }  
    37.   
    38.     hmi->dso = handle;  
    39.   
    40.     /* success */  
    41.     status = 0;  
    42.   
    43.     done:  
    44.     if (status != 0) {  
    45.         hmi = NULL;  
    46.         if (handle != NULL) {  
    47.             dlclose(handle);  
    48.             handle = NULL;  
    49.         }  
    50.     } else {  
    51.         ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",  
    52.                 id, path, *pHmi, handle);  
    53.     }  
    54.   
    55.     *pHmi = hmi;  
    56.   
    57.     return status;  
    58. }  

    此处的hw_get_module()最后返回的sensors_module_t是经过封装的hw_module_t,他除了包含一个hw_module_t还包含一个获取sensorlist的API这个结构体主要是几个API接口的定义,hw_module_t结构体的定义如下:
    1. struct sensors_module_t HAL_MODULE_INFO_SYM = {  
    2.         common:{  
    3.                 tag: HARDWARE_MODULE_TAG,  
    4.                 version_major: 1,  
    5.                 version_minor: 0,  
    6.                 id: SENSORS_HARDWARE_MODULE_ID,  
    7.                 name: "SPRD Sensor module",  
    8.                 author: "Spreadtrum",  
    9.                 methods: &sensors_module_methods,  
    10.                 dso: 0,  
    11.                 reserved:{},  
    12.         },  
    13.         get_sensors_list:sensors__get_sensors_list,  
    14. };  
    15.   
    16. static struct hw_module_methods_t sensors_module_methods = {  
    17. open:    open_sensors  
    18. };  
    19.   
    20. static int sensors__get_sensors_list(struct sensors_module_t *module,  
    21.                      struct sensor_t const **list)  
    22. {  
    23.     *list = sSensorList;  
    24.     return numSensors;  
    25. }  

    3.1.2. 通过sensors_open_1(&mSensorModule->common, &mSensorDevice)函数新建并初始化系统所有的sensor。
    1. static inline int sensors_open_1(const struct hw_module_t* module,  
    2.         sensors_poll_device_1_t** device) {  
    3.     return module->methods->open(module,  
    4.             SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);  
    5. }  

    这里的open函数就是上一步hw_get_module()获取的sensors_module_t所定义的sensors_module_methods的open_sensors接口,open_sensors接口的代码如下:

    1. static int open_sensors(const struct hw_module_t* module, const char* id,  
    2.                         struct hw_device_t** device)  
    3. {  
    4.         int status = -EINVAL;  
    5.         sensors_poll_context_t *dev = new sensors_poll_context_t();  
    6.   
    7.         memset(&dev->device, 0, sizeof(sensors_poll_device_t));  
    8.   
    9.         dev->device.common.tag = HARDWARE_DEVICE_TAG;  
    10.         dev->device.common.version  = 0;  
    11.         dev->device.common.module   = const_cast<hw_module_t*>(module);  
    12.         dev->device.common.close    = poll__close;  
    13.         dev->device.activate        = poll__activate;  
    14.         dev->device.setDelay        = poll__setDelay;  
    15.         dev->device.poll            = poll__poll;  
    16.   
    17.         *device = &dev->device.common;  
    18.         status = 0;  
    19.   
    20.         return status;  
    21. }  

    函数首先是通过sensors_poll_context_t()方法新建并初始化系统所有的sensor:
    1. sensors_poll_context_t::sensors_poll_context_t()  
    2. {  
    3. #ifndef ACC_NULL  
    4.     mSensors[acc] = new AccSensor();  
    5.     numSensors +=  
    6.         mSensors[acc]->populateSensorList(sSensorList + numSensors);  
    7.     mPollFds[acc].fd = mSensors[acc]->getFd();  
    8.     mPollFds[acc].events = POLLIN;  
    9.     mPollFds[acc].revents = 0;  
    10.     ALOGD("AnumSensors=%d; %d", numSensors, AccSensor::numSensors);  
    11. #endif  
    12. #ifndef ORI_NULL  
    13.     mSensors[ori] = new OriSensor();  
    14.     numSensors +=  
    15.         mSensors[ori]->populateSensorList(sSensorList + numSensors);  
    16.     mPollFds[ori].fd = mSensors[ori]->getFd();  
    17.     mPollFds[ori].events = POLLIN;  
    18.     mPollFds[ori].revents = 0;  
    19.     ALOGD("OnumSensors=%d; %d", numSensors, OriSensor::numSensors);  
    20. #endif  
    21. #ifdef SENSORHAL_PROXIMITY_STK  
    22.     mSensors[stk_als] = <span style="color:#FF0000;">new STKLightSensor();</span>  
    23.     numSensors +=  
    24.         mSensors[stk_als]->populateSensorList(sSensorList + numSensors);  
    25.     mPollFds[stk_als].fd = mSensors[stk_als]->getFd();  
    26.     mPollFds[stk_als].events = POLLIN;  
    27.     mPollFds[stk_als].revents = 0;  
    28.       
    29.     mSensors[stk_ps] = new STKProximitySensor();  
    30.     numSensors +=  
    31.         mSensors[stk_ps]->populateSensorList(sSensorList + numSensors);  
    32.     mPollFds[stk_ps].fd = mSensors[stk_ps]->getFd();  
    33.     mPollFds[stk_ps].events = POLLIN;  
    34.     mPollFds[stk_ps].revents = 0;  
    35. #else  
    36. #ifndef PLS_NULL  
    37.     mSensors[pls] = new PlsSensor();  
    38.     numSensors +=  
    39.         mSensors[pls]->populateSensorList(sSensorList + numSensors);  
    40.     mPollFds[pls].fd = mSensors[pls]->getFd();  
    41.     mPollFds[pls].events = POLLIN;  
    42.     mPollFds[pls].revents = 0;  
    43.     ALOGD("PnumSensors=%d; %d", numSensors, PlsSensor::numSensors);  
    44. #endif  
    45. #endif  
    46.     int wakeFds[2];  
    47.     int result = pipe(wakeFds);  
    48.     ALOGE_IF(result < 0, "error creating wake pipe (%s)", strerror(errno));  
    49.     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);  
    50.     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);  
    51.     mWritePipeFd = wakeFds[1];  
    52.   
    53.     mPollFds[wake].fd = wakeFds[0];  
    54.     mPollFds[wake].events = POLLIN;  
    55.     mPollFds[wake].revents = 0;  
    56. }  

    例如在new STKLightSensor()中主要定义了数据读取环形缓冲区的大小,sensor_event_t的初始化,以及input子系统上报事件所保存的路径。STKLightSensor()代码如下:
    1. STKLightSensor::STKLightSensor()  
    2.     : SensorBase(NULL, "lightsensor-level"),  
    3.       mEnabled(0),  
    4.       mInputReader(4),  
    5.       mHasPendingEvent(false) {  
    6.     mPendingEvent.version = sizeof(sensors_event_t);  
    7.     mPendingEvent.sensor = ID_L;  
    8.     mPendingEvent.type = SENSOR_TYPE_LIGHT;  
    9.     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));  
    10.   
    11.     if (data_fd) {  
    12. #if 1         
    13.         strcpy(input_sysfs_path, "/sys/class/input/");  
    14.         strcat(input_sysfs_path, input_name);  
    15.         strcat(input_sysfs_path, "/device/driver/");               //此项目初始化文件节点所在文件夹目录为:/sys/class/input/input*/device/driver/  
    16.         input_sysfs_path_len = strlen(input_sysfs_path);  
    17. #else         
    18.         strcpy(input_sysfs_path, INPUT_SYSFS_PATH_ALS);  
    19. #endif  
    20.         input_sysfs_path_len = strlen(input_sysfs_path);  
    21.         LOGD("%s: input=%s", __func__, input_name);  
    22.         LOGD("%s: input_sysfs_path = %s ", __func__, input_sysfs_path);  
    23.     }  
    24.       
    25. }  

    open_sensors第二步是对sensors_poll_device_t结构体的初始化,主要是对sensor打开,关闭,以及数据获取的API 借口的定义,这几个API接口将在后面详细描述。

    3.1.3.  mSensorModule->get_sensors_list(mSensorModule, &list)这个是前面获取的sensors_module_t中定义的get_sensor_list方法。他最终获取的是HAL层初始化好的sensor的列表,并返回sensor的数量,active方法打开sensor,

    1. mSensorDevice->activate(  
    2.                         reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),  
    3.                         list[i].handle, 0);  
    mSensorDevice->activate()调用HAL层activate()方法,最后调用HAL层sensor厂商添加的setEnable()方法来打开对应sensor。通过open函数获取sensor在sysfs对应的文件节点,然后使用write函数往文件节点里面写1即可。

    1. static int poll__activate(struct sensors_poll_device_t *dev,  
    2.               int handle, int enabled)  
    3. {  
    4.     sensors_poll_context_t *ctx = (sensors_poll_context_t *) dev;  
    5.     return ctx->activate(handle, enabled);  
    6. }  
    7.   
    8. int sensors_poll_context_t::activate(int handle, int enabled)  
    9. {  
    10.     int drv = handleToDriver(handle);                       //获取sensor的类型  
    11.     int err;  
    12.   
    13.     switch (handle) {  
    14.     case ID_A:  
    15.     case ID_M:  
    16.     case ID_L:  
    17.     case ID_P:  
    18.         /* No dependencies */  
    19.         break;  
    20.   
    21.     case ID_O:  
    22.         /* These sensors depend on ID_A and ID_M */  
    23.         mSensors[handleToDriver(ID_A)]->setEnable(ID_A, enabled);  
    24.         mSensors[handleToDriver(ID_M)]->setEnable(ID_M, enabled);  
    25.         break;  
    26.   
    27.     default:  
    28.         return -EINVAL;  
    29.     }  
    30.   
    31.     ALOGD("activate handle=%d; drv=%d", handle, drv);  
    32.   
    33.     err = mSensors[drv]->setEnable(handle, enabled);                      //使用对应的sensor的setEnable方法  
    34.   
    35.     if (enabled && !err) {  
    36.         const char wakeMessage(WAKE_MESSAGE);  
    37.         int result = write(mWritePipeFd, &wakeMessage, 1);  
    38.         ALOGE_IF(result < 0, "error sending wake message (%s)",  
    39.              strerror(errno));  
    40.     }  
    41.     return err;  
    42. }  
    43.   
    44. int STKLightSensor::setEnable(int32_t, int en) {  
    45.    int flags = en ? 1 : 0;  
    46.     if (flags != mEnabled) {  
    47.         int fd;  
    48.         strcpy(&input_sysfs_path[input_sysfs_path_len], "enable"); //初始化的input_sysfs_path是/sys/class/input/input*/device/driver/,此项目的文件节点是  
    49. /sys/class/input/input*/device/driver/enable,所以使用strcpy在input_sysfs_path后面添加了enable节点。  
    50.         fd = open(input_sysfs_path, O_RDWR);                        //打开对应的sysfs目录下的文件节点  
    51.         LOGE("STKLightSensor enable path %s fd %d", input_sysfs_path, fd);  
    52.         if (fd >= 0) {  
    53.             char buf[2];  
    54.             buf[1] = 0;  
    55.             if (flags) {  
    56.                 buf[0] = '1';  
    57.             } else {  
    58.                 buf[0] = '0';  
    59.             }  
    60.             write(fd, buf, sizeof(buf));                         //将buf的值写到sysfs的目录下面sensor对应的文件节点  
    61.             close(fd);  
    62.             mEnabled = flags;  
    63.             if(flags)  
    64.                 setInitialState();                        //处理sensor开启后的第一笔数据  
    65.             return 0;  
    66.         }  
    67.         return -1;  
    68.     }  
    69.     return 0;   
    70. }  


    如果打开了sensor,则会调用setInitialState()通过ioctl来从data_fd中获取数据,然后将是否有待处理数据设置为“true”,在poll后面将要讲到的poll数据时会直接先处理这个数据。这里获取的数据个人理解是在sensor开启之后的第一笔数据。

    1. int STKLightSensor::setInitialState() {  
    2.     struct input_absinfo absinfo;  
    3.     if (!ioctl(data_fd, EVIOCGABS(ABS_MISC), &absinfo)) {  
    4.         mPendingEvent.light = (float)absinfo.value;  
    5.         mHasPendingEvent = true;  
    6.     }  
    7.     else  
    8.         ALOGE("%s:ioctl failed!", __func__);          
    9.     return 0;  
    10. }  



    至此,sensorDevice的实例化就完成了。下一篇将继续讲
  • 相关阅读:
    Shell编程基础
    lenovo future leaer deveolpmetn program
    求1+2+...+n
    Linux下使用qq
    判断2个线段是否相交
    java大数相加
    Django路由系统
    Django框架
    HTTP协议及Django配置
    mysql索引
  • 原文地址:https://www.cnblogs.com/ztguang/p/12645161.html
Copyright © 2020-2023  润新知