启动SensorService服务
在SystemServer进程中的main函数中,通过JNI调用,调用到
com_android_server_SystemServer.cpp的android_server_SystemServer_init1()方法,该方法又调用system_init.cpp中的system_init():
extern \{
……
property_get(\ if (strcmp(propBuf, \ // Start the sensor service SensorService::instantiate(); } …..
return NO_ERROR; }
View Code
在这里创建了SensorService的实例。
SensorService初始化
SensorService创建完之后,将会调用SensorService::onFirstRef()方法,在该方法中完成初始化工作。
首先获取SensorDevice实例,在其构造函数中,完成了对Sensor模块HAL的初始化:
SensorDevice::SensorDevice() : mSensorDevice(0), mSensorModule(0) {
status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&mSensorModule);
if (mSensorModule) { err = sensors_open(&mSensorModule->common, &mSensorDevice);
ALOGE_IF(err, \ SENSORS_HARDWARE_MODULE_ID, strerror(-err));
if (mSensorDevice) {
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(mSensorDevice, list[i].handle, 0); } } } } View Code 这里主要做了三个工作: 1. 调用HAL层的hw_get_modele()方法,加载Sensor模块so文件 2. 调用sensor.h的sensors_open方法打开设备 3. 调用sensors_poll_device_t->activate()对Sensor模块使能 再看看SensorService::onFirstRef()方法: void SensorService::onFirstRef() { SensorDevice& dev(SensorDevice::getInstance()); if (dev.initCheck() == NO_ERROR) { sensor_t const* list; ssize_t count = dev.getSensorList(&list); if (count > 0) { …… for (ssize_t i=0 ; i registerSensor( new HardwareSensor(list[i]) ); …… } // it's safe to instantiate the SensorFusion object here // (it wants to be instantiated after h/w sensors have been // registered) const SensorFusion& fusion(SensorFusion::getInstance()); if (hasGyro) { …… } …… run(\ mInitCheck = NO_ERROR; } } } View Code 在这个方法中,主要做了4件事情: 1. 创建SensorDevice实例 2. 获取Sensor列表 3. 调用SensorDevice.getSensorList(),获取Sensor模块所有传感器列表 4. 为每个传感器注册监听器 registerSensor( new HardwareSensor(list[i]) ); void SensorService::registerSensor(SensorInterface* s) { sensors_event_t event; memset(&event, 0, sizeof(event)); const Sensor sensor(s->getSensor()); // 添加到Sensor列表,给客户端使用 mSensorList.add(sensor); // add to our handle->SensorInterface mapping mSensorMap.add(sensor.getHandle(), s); // create an entry in the mLastEventSeen array mLastEventSeen.add(sensor.getHandle(), event); } View Code HardwareSensor实现了SensorInterface接口。 1. 启动线程读取数据 调用run方法启动新线程,将调用SensorService::threadLoop()方法。 在新的线程中读取HAL层数据 SensorService实现了Thread类,当在onFirstRef中调用run方法后,将在新的线程中调用SensorService::threadLoop()方法。 bool SensorService::threadLoop() { …… do { count = device.poll(buffer, numEventMax); recordLastValue(buffer, count); …… // send our events to clients... const SortedVector< wp getActiveConnections()); size_t numConnections = activeConnections.size(); for (size_t i=0 ; i connection->sendEvents(buffer, count, scratch); } } } while (count >= 0 || Thread::exitPending()); return false; } View Code 在while循环中一直读取HAL层数据,再调用SensorEventConnection->sendEvents将数据写到管道中。 客户端与服务端通信