Sensor框架Framework层解读 - 图文(3)

2019-08-30 22:56

启动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 > activeConnections(

getActiveConnections());

size_t numConnections = activeConnections.size(); for (size_t i=0 ; i connection( activeConnections[i].promote()); if (connection != 0) {

connection->sendEvents(buffer, count, scratch); } }

} while (count >= 0 || Thread::exitPending()); return false; }

View Code

在while循环中一直读取HAL层数据,再调用SensorEventConnection->sendEvents将数据写到管道中。

客户端与服务端通信


Sensor框架Framework层解读 - 图文(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:注册分类1、2、3、5.1类申报资料要求(试行)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: