1.概览
这部分是SensorHal部分的二阶段初始化,不放在SensorDevice中一起,主要是想强调下这俩所在进程的不同。所以在程序调用流程上也不应该柔和在一起。看起来无缝调用,只不过Android中的binder在默默耕耘罢了。
2.SensorHal的初始化–HalProxyV2_0::initialize
2.1 SensorHal 中 initialize 接口定义
initialize 接口是在 ISensors.hal 中定义的,意味着本次从SensorDevice调用到 HalProxyV2_0::initialize是跨进程的。
//hardware/interfaces/sensors/2.0/ISensors.hal
interface ISensors {
...
@entry
@callflow(next = {"getSensorsList"})
initialize(fmq_sync<Event> eventQueueDescriptor,
fmq_sync<uint32_t> wakeLockDescriptor,
ISensorsCallback sensorsCallback)
generates
(Result result);
...
};
eventQueueDescriptor Fast Message Queue descriptor that is used to create the Event FMQ which is where sensor events are written. The descriptor is obtained from the framework’s FMQ that is used to read sensor events.
wakeLockDescriptor Fast Message Queue descriptor that is used to create the Wake Lock FMQ which is where wake_lock events are read from. The descriptor is obtained from the framework’s FMQ that is used to write wake_lock events.
sensorsCallback sensors callback that receives asynchronous data from the Sensors HAL.
所以入参都是从SensorDevice所在进程传递而来的。
2.2 HalProxyV2_0::initialize 接口的实现
在第四章节中描述过ISensor HIDL接口的实现,它的实现类就是 HalProxyV2_0 ,类图如下
它的接口实现位于 IHalProxy 类中
//hardware\interfaces\sensors\common\default\2.x\multihal\include\HalProxy.h
template <class ISensorsVersion>
class IHalProxy : public HalProxy, public ISensorsVersion {
public:
Return<Result> initialize(
const ::android::hardware::MQDescriptorSync<V1_0::Event>& eventQueueDescriptor,
const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
const sp<V2_0::ISensorsCallback>& sensorsCallback) override {
return HalProxy::initialize(eventQueueDescriptor, wakeLockDescriptor, sensorsCallback);
};
可见最终调用 HalProxy::initialize。
3. HalProxy::initialize
Return<Result> HalProxy::initialize(
const ::android::hardware::MQDescriptorSync<V1_0::Event>& eventQueueDescriptor,
const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
const sp<V2_0::ISensorsCallback>& sensorsCallback) {
sp<ISensorsCallbackWrapperBase> dynamicCallback =
new ISensorsCallbackWrapperV2_0(sensorsCallback);
// Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
auto eventQueue =
std::make_unique<EventMessageQueueV2_0>(eventQueueDescriptor, true /* resetPointers */);
std::unique_ptr<EventMessageQueueWrapperBase> queue =
std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue);
// Create the Wake Lock FMQ from the wakeLockDescriptor. Reset the read/write positions.
auto hidlWakeLockQueue =
std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
std::unique_ptr<WakeLockMessageQueueWrapperBase> wakeLockQueue =
std::make_unique<WakeLockMessageQueueWrapperHidl>(hidlWakeLockQueue);
return initializeCommon(queue, wakeLockQueue, dynamicCallback);
}
3.1 EventMessageQueueWrapperV1_0
FMQ是支持通过传递描述符来获取 MessageQueue 的,这里也是如此,先获取 MessageQueue,再将其做一层封装 EventMessageQueueWrapperV1_0 和 SensorDevice端的 ISensorsWrapperV2_0 是如出一辙的,读者记住这就是用于SensorHal来传递sensor event到 SensorFramework的。
3.2 WakeLockMessageQueueWrapperHidl
此处和 EventMessageQueueWrapperV1_0 也是同理,最终在SensorHal这一层使用 WakeLockMessageQueueWrapperHidl 对 WakeLockQueue 进行封装,下面是对应的类图
WakeLockQueue 就是 MessageQueue,定义这里再次贴一下
typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
3.3 HalProxy::initializeCommon
Return<Result> HalProxy::initializeCommon(
std::unique_ptr<EventMessageQueueWrapperBase>& eventQueue,
std::unique_ptr<WakeLockMessageQueueWrapperBase>& wakeLockQueue,
const sp<ISensorsCallbackWrapperBase>& sensorsCallback) {
...
// Clears the queue if any events were pending write before.
mPendingWriteEventsQueue = std::queue<std::pair<std::vector<V2_1::Event>, size_t>>();
// Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
mEventQueue = std::move(eventQueue);
// Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
// events have been successfully read and handled by the framework.
mWakeLockQueue = std::move(wakeLockQueue);
mPendingWritesThread = std::thread(startPendingWritesThread, this);
mWakelockThread = std::thread(startWakelockThread, this);
for (size_t i = 0; i < mSubHalList.size(); i++) {
Result currRes = mSubHalList[i]->initialize(this, this, i);
}
// set mode
mCurrentOperationMode = OperationMode::NORMAL;
...
}
mPendingWriteEventsQueue:
A FIFO queue of pairs of vector of events and the number of wakeup events in that vector which are waiting to be written to the events fmq in the background thread.
mEventQueue:
The Event FMQ where sensor events are written.
mWakeLockQueue:
The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events.
mPendingWritesThread:
The thread object ptr that handles pending writes.
mWakelockThread:
The thread object that handles wakelocks.
mSubHalList[i]->initialize
第四章分析过,mSubHalList中存储的类为 SensorsSubHalV2_1 ,其实现的 initialize 如下
//hardware\interfaces\sensors\common\default\2.x\multihal\include\SubHalWrapper.h
class SubHalWrapperV2_1 : public SubHalWrapperBase<V2_1::implementation::ISensorsSubHal>{
Return<Result> initialize(V2_0::implementation::ISubHalCallback* callback,
V2_0::implementation::IScopedWakelockRefCounter* refCounter,
int32_t subHalIndex) override {
return mSubHal->initialize(
new V2_0::implementation::HalProxyCallbackV2_1(callback, refCounter, subHalIndex));
}
};
可见最终传递给子 SensorHal 的就是 HalProxyCallbackV2_1 类的一个实例,HalProxyCallbackV2_1 的类图如下
在SubHalWrapperV2_1中才会最终调用到每个子SensorHal的 initialize,其定义如下
//hardware\interfaces\sensors\common\default\2.x\multihal\include\v2_1\SubHal.h
class ISensorsSubHal : public ISensors {
virtual Return<V1_0::Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) = 0;
};
由此可见,子SensorHal的实现类的父类必须包括ISensorsSubHal并且实现其内部的 initialize方法,注意initialize有多个版本,其需要实现的为上面刚刚提到的。