如何理解Sensor架构 - 华清远见 - 图文 下载本文

内容发布更新时间 : 2024/12/26 10:27:40星期一 下面是文章的全部内容请认真阅读。

sp createEventQueue(); private:

// DeathRecipient interface void sensorManagerDied();

// 调用getService获取SensorService客户端并保存在mSensorServer中

// 调用mSensorServer->getSensorList获取sensor列表,并保存在mSensors和mSensorList中 status_t assertStateLocked() const; private:

mutable Mutex mLock;

mutable sp mSensorServer; // SensorService客户端 mutable Sensor const** mSensorList; // sensor列表 mutable Vector mSensors; // sensor列表 mutable sp mDeathObserver; } [cpp]

view plaincopyclass ISensorEventConnection : public IInterface { public:

DECLARE_META_INTERFACE(SensorEventConnection); virtual sp getSensorChannel() const = 0;

virtual status_t enableDisable(int handle, bool enabled) = 0; virtual status_t setEventRate(int handle, nsecs_t ns) = 0; };

2.4 SensorService.cpp

SensorService作为一个轻量级的system service,它运行于SystemServer内,即在system_init调用SensorService::instantiate(); SensorService主要功能如下:

1) SensorService::instantiate创建实例对象,并增加到ServiceManager中,且创建并启动线程,并执行threadLoop

2) threadLoop从sensor驱动获取原始数据,然后通过SensorEventConnection把事件发送给客户端 3) BnSensorServer的成员函数负责让客户端获取sensor列表和创建SensorEventConnection SensorService与客户端的接口定义如下: [cpp]

view plaincopyclass ISensorServer : public IInterface { public:

DECLARE_META_INTERFACE(SensorServer); virtual Vector getSensorList() = 0;

virtual sp createSensorEventConnection() = 0; };

SensorService定义如下: [cpp]

view plaincopyclass SensorService :

public BinderService, //创建SensorService对象,并增加到ServiceManager中

public BnSensorServer, // 申明了SensorService与客户端(SensorManager)间的binder接口

protected Thread // 线程辅助类,调用run创建并启动线程,然后在线程主函数内回调threadLoop函数, // 所以在使用它时,做一个派生,并根据需要重写threadLoop即可 {

friend class BinderService;

static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz SensorService();

virtual ~SensorService(); /*

在addService时,第一次构建sp强引用对象时,会调用onFirstRef函数 实现功能如下:

1) 获取SensorDevice实例

2) 调用SensorDevice.getSensorList获取sensor_t列表

3) 根据硬件sensor_t创建HardwareSensor,然后加入mSensorList(Sensor) 和mSensorMap(HardwareSensor)中

4) 根据硬件sensor_t创建对应的senosr(如GravitySensor), 然后加入mVirtualSensorList和mSensorList中 5) mUserSensorList = mSensorList;

6) run(\运行线程,并执行threadLoop */

virtual void onFirstRef(); // Thread interface /*

1) 调用SensorDevice.poll获取sensors_event_t事件 2) 获取已经激活的sensor列表mActiveVirtualSensors 3) 对每一个事件,执行SensorFusion.process

4) 对每一个事件,执行HardwareSensor.process(事件无变化,直接copy) 5) 调用SensorService::SensorEventConnection::sendEvents,把事件发 送给所有的listener

*/

virtual bool threadLoop(); // ISensorServer interface // 返回mUserSensorList

virtual Vector getSensorList(); // 实例化SensorEventConnection并返回 virtual sp createSensorEventConnection();

virtual status_t dump(int fd, const Vector& args);

//==================================================================== //============== SensorEventConnection start ======================== class SensorEventConnection : public BnSensorEventConnection { virtual ~SensorEventConnection(); virtual void onFirstRef(); // 返回mChannel

virtual sp getSensorChannel() const;

// 调用SensorService::enable或SensorService::disable virtual status_t enableDisable(int handle, bool enabled); // 调用SensorService::setEventRate

virtual status_t setEventRate(int handle, nsecs_t ns); sp const mService; // 保存当前SensorService实例 sp const mChannel; // SensorChannel实例 mutable Mutex mConnectionLock; // protected by SensorService::mLock SortedVector mSensorInfo;

public: /*

1) 把当前service保存在mService中

2) 创建SensorChannel实例,并保存在mChannel中

(在SensorChannel::SensorChannel中创建pipe,并把收和发都设置非阻塞) */

SensorEventConnection(const sp& service);

// 调用连接中的mChannel->write (SensorChannel::write),把符合条件的事件写入pipe status_t sendEvents(sensors_event_t const* buffer, size_t count, sensors_event_t* scratch = NULL);

bool hasSensor(int32_t handle) const; //检查handle是否在mSensorInfo中 bool hasAnySensor() const; //检查mSensorInfo中是否有sensor bool addSensor(int32_t handle); //把handle增加到mSensorInfo列表中 bool removeSensor(int32_t handle); //把handle从mSensorInfo中删除 };

//============== SensorEventConnection end ======================== //==================================================================== class SensorRecord {

SortedVector< wp > mConnections; public:

SensorRecord(const sp& connection); bool addConnection(const sp& connection); bool removeConnection(const wp& connection);

size_t getNumConnections() const { return mConnections.size(); }