AndroidT(13)--支持MultiHal 的 SensorHal 的初始化--HIDL(六)

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有多个版本,其需要实现的为上面刚刚提到的。

4.时序图

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值