defaultPassthroughServiceImplementation在android新老版本的差异

背景:

近来有学员朋友在群里讨论到一个hal相关的知识点,就是看到有的程序注册自己服务时候使用如下方式:


int main() {
   
    return defaultPassthroughServiceImplementation<IGatekeeper>();
}

有学员朋友很可能就被它的名字误解了,以为这个就是直通式,这块比较疑惑,同时

这个注册服务方式以前安卓8.1老版本的其实还比较常见,新版本安卓没有再使用这种方式都是使用如下方式

int main() {
   
    ::android::hardware::configureRpcThreadpool(1, true /* willJoinThreadpool */);
    android::sp<SoftGateKeeperDevice> gatekeeper(new SoftGateKeeperDevice());
    auto status = gatekeeper->registerAsService();
    if (status != android::OK) {
   
        LOG(FATAL) << "Could not register service for Gatekeeper 1.0 (software) (" << status << ")";
    }

    android::hardware::joinRpcThreadpool();
    return -1;  // Should never get here.
}

下面来正式分析一下老版本为啥可以用defaultPassthroughServiceImplementation进行注册服务

源码分析

android-8.1.0_r1/system/libhidl/transport/include/hidl/LegacySupport.h

template<class Interface>
__attribute__((warn_unused_result))
status_t registerPassthroughServiceImplementation(
        std
HIDL(Hardware Interface Definition Language)是一种用于定义硬件接口的语言,它允许不同的进程之间通过共享内存进行通信。在Android系统中,HIDL被广泛用于实现跨进程的硬件服务。 下面是一个简单的HIDL Service和Java Service共享内存的代码示例: 1. HIDL Service端代码(C++): ```cpp // IMyService.hal package com.example; interface IMyService { int sendData(in vec<uint8_t> data); }; // MyService.cpp #include <android/hidl/manager/1.0/IServiceManager.h> #include <android/hidl/base/1.0/IBase.h> #include <com/example/IMyService.h> using android::hardware::hidl_vec; using com::example::IMyService; class MyService : public IMyService { public: // 实现IMyService接口的sendData方法 int32_t sendData(const hidl_vec<uint8_t>& data) override { // 在这里处理接收到的数据 // ... return 0; } }; int main() { // 注册MyService到HIDL服务管理器 android::sp<IMyService> service = new MyService(); android::hardware::defaultPassthroughServiceImplementation<IMyService>(); android::hardware::joinRpcThreadpool(); return 0; } ``` 2. Java Service端代码: ```java // IMyService.aidl package com.example; interface IMyService { int sendData(byte[] data); } // MyService.java package com.example; import android.os.RemoteException; public class MyService extends IMyService.Stub { // 实现IMyService接口的sendData方法 @Override public int sendData(byte[] data) throws RemoteException { // 在这里处理接收到的数据 // ... return 0; } } ``` 以上是一个简单的HIDL Service和Java Service共享内存的代码示例。在这个示例中,HIDL Service通过实现`IMyService`接口来提供服务,Java Service通过继承`IMyService.Stub`类来实现服务。两者都需要实现`sendData`方法来处理接收到的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值