背景:
近来有学员朋友在群里讨论到一个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