AndroidO Treble架构下Tranport类型查询过程

通过前面文章的分析,我们知道,Client进程在查询hidl服务接口对象时,会根据该hidl服务的Tranport类型选择加载方式,如果是HWBINDER,那么就从hwservicemanager中查询,如果是PASSTHROUGH,那么就通过PassthroughServiceManager将hal实现库dlopen到当前进程地址空间。那么hidl服务的Tranport类型是如何得到的呢?

  1. const sp<::android::hidl::manager::V1_0::IServiceManager> sm = defaultServiceManager();  
  2. if (sm == nullptr) {  
  3.     ALOGE(”getService: defaultServiceManager() is null”);  
  4.     return nullptr;  
  5. }  
  6.   
  7. Return<Transport> transportRet = sm->getTransport(IComposer::descriptor, serviceName);  
    const sp<::android::hidl::manager::V1_0::IServiceManager> sm = defaultServiceManager();
    if (sm == nullptr) {
        ALOGE("getService: defaultServiceManager() is null");
        return nullptr;
    }

    Return<Transport> transportRet = sm->getTransport(IComposer::descriptor, serviceName);

是调用hwservicemanager的getTransport函数来得到Tranport类型,这里其实就是IPC进入hwservicemanager进程空间,执行ServiceManager对象的getTransport函数:

system\hwservicemanager\ServiceManager.cpp

  1. Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName,  
  2.                                                                const hidl_string& name) {  
  3.     using ::android::hardware::getTransport;  
  4.   
  5.     pid_t pid = IPCThreadState::self()->getCallingPid();  
  6.     if (!mAcl.canGet(fqName, pid)) {  
  7.         return Transport::EMPTY;  
  8.     }  
  9.   
  10.     switch (getTransport(fqName, name)) {  
  11.         case vintf::Transport::HWBINDER:  
  12.              return Transport::HWBINDER;  
  13.         case vintf::Transport::PASSTHROUGH:  
  14.              return Transport::PASSTHROUGH;  
  15.         case vintf::Transport::EMPTY:  
  16.         default:  
  17.              return Transport::EMPTY;  
  18.     }  
  19. }  
Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName,
                                                               const hidl_string& name) {
    using ::android::hardware::getTransport;

    pid_t pid = IPCThreadState::self()->getCallingPid();
    if (!mAcl.canGet(fqName, pid)) {
        return Transport::EMPTY;
    }

    switch (getTransport(fqName, name)) {
        case vintf::Transport::HWBINDER:
             return Transport::HWBINDER;
        case vintf::Transport::PASSTHROUGH:
             return Transport::PASSTHROUGH;
        case vintf::Transport::EMPTY:
        default:
             return Transport::EMPTY;
    }
}

system\hwservicemanager\Vintf.cpp

  1. vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) {  
  2.     FQName fqName(interfaceName);  
  3.     if (!fqName.isValid()) {  
  4.         LOG(DEBUG) << ”getTransport: ” << interfaceName << “ is not a valid fully-qualified name.”;  
  5.         return vintf::Transport::EMPTY;  
  6.     }  
  7.     if (!fqName.hasVersion()) {  
  8.         LOG(DEBUG) << ”getTransport: ” << fqName.string()  
  9.                    << ” does not specify a version. Using default transport.”;  
  10.         return vintf::Transport::EMPTY;  
  11.     }  
  12.     if (fqName.name().empty()) {  
  13.         LOG(DEBUG) << ”getTransport: ” << fqName.string()  
  14.                    << ” does not specify an interface name. Using default transport.”;  
  15.         return vintf::Transport::EMPTY;  
  16.     }  
  17.   
  18.     vintf::Transport tr = getTransportFromManifest(fqName, instanceName,  
  19.             vintf::VintfObject::GetFrameworkHalManifest());  
  20.     if (tr != vintf::Transport::EMPTY) {  
  21.         return tr;  
  22.     }  
  23.     tr = getTransportFromManifest(fqName, instanceName,  
  24.             vintf::VintfObject::GetDeviceHalManifest());  
  25.     if (tr != vintf::Transport::EMPTY) {  
  26.         return tr;  
  27.     }  
  28.   
  29.     LOG(WARNING) << ”getTransportFromManifest: Cannot find entry ”  
  30.                  << fqName.string()  
  31.                  << ” in either framework or device manifest, using default transport.”;  
  32.     return vintf::Transport::EMPTY;  
  33. }  
vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) {
    FQName fqName(interfaceName);
    if (!fqName.isValid()) {
        LOG(DEBUG) << "getTransport: " << interfaceName << " is not a valid fully-qualified name.";
        return vintf::Transport::EMPTY;
    }
    if (!fqName.hasVersion()) {
        LOG(DEBUG) << "getTransport: " << fqName.string()
                   << " does not specify a version. Using default transport.";
        return vintf::Transport::EMPTY;
    }
    if (fqName.name().empty()) {
        LOG(DEBUG) << "getTransport: " << fqName.string()
                   << " does not specify an interface name. Using default transport.";
        return vintf::Transport::EMPTY;
    }

    vintf::Transport tr = getTransportFromManifest(fqName, instanceName,
            vintf::VintfObject::GetFrameworkHalManifest());
    if (tr != vintf::Transport::EMPTY) {
        return tr;
    }
    tr = getTransportFromManifest(fqName, instanceName,
            vintf::VintfObject::GetDeviceHalManifest());
    if (tr != vintf::Transport::EMPTY) {
        return tr;
    }

    LOG(WARNING) << "getTransportFromManifest: Cannot find entry "
                 << fqName.string()
                 << " in either framework or device manifest, using default transport.";
    return vintf::Transport::EMPTY;
}

首先校验接口包名是否合法,然后调用getTransportFromManifest函数分别从/system/manifest.xml和/vendor/manifest.xml文件中读取当前hidl服务的Transport类型。GetFrameworkHalManifest函数将读取/system/manifest.xml文件,GetDeviceHalManifest函数将读取/vendor/manifest.xml文件。这些manifest.xml定义了每个hidl的传输类型。

system\libvintf\VintfObject.cpp

  1. // static  
  2. const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) {  
  3.     return Get(&gDeviceManifest, skipCache,  
  4.             std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,  
  5.                 ”/vendor/manifest.xml”));  
  6. }  
  7.   
  8. // static  
  9. const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) {  
  10.     return Get(&gFrameworkManifest, skipCache,  
  11.             std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,  
  12.                 ”/system/manifest.xml”));  
  13. }  
// static
const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) {
    return Get(&gDeviceManifest, skipCache,
            std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
                "/vendor/manifest.xml"));
}

// static
const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) {
    return Get(&gFrameworkManifest, skipCache,
            std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
                "/system/manifest.xml"));
}

这里就是xml的读取解析,具体过程不在展开分析。

system\hwservicemanager\Vintf.cpp

  1. vintf::Transport getTransportFromManifest(  
  2.         const FQName &fqName, const std::string &instanceName,  
  3.         const vintf::HalManifest *vm) {  
  4.     if (vm == nullptr) {  
  5.         return vintf::Transport::EMPTY;  
  6.     }  
  7.     return vm->getTransport(fqName.package(),  
  8.             vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()},  
  9.             fqName.name(), instanceName);  
  10. }  
vintf::Transport getTransportFromManifest(
        const FQName &fqName, const std::string &instanceName,
        const vintf::HalManifest *vm) {
    if (vm == nullptr) {
        return vintf::Transport::EMPTY;
    }
    return vm->getTransport(fqName.package(),
            vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()},
            fqName.name(), instanceName);
}

system\libvintf\HalManifest.cpp

  1. Transport HalManifest::getTransport(const std::string &package, const Version &v,  
  2.             const std::string &interfaceName, const std::string &instanceName) const {  
  3.   
  4.     for (const ManifestHal *hal : getHals(package)) {  
  5.         bool found = false;  
  6.         for (auto& ver : hal->versions) {  
  7.             if (ver.majorVer == v.majorVer && ver.minorVer >= v.minorVer) {  
  8.                 found = true;  
  9.                 break;  
  10.             }  
  11.         }  
  12.         if (!found) {  
  13.             LOG(DEBUG) << ”HalManifest::getTransport(“ << to_string(mType) << “): Cannot find ”  
  14.                       << to_string(v) << ” in supported versions of ” << package;  
  15.             continue;  
  16.         }  
  17.         auto it = hal->interfaces.find(interfaceName);  
  18.         if (it == hal->interfaces.end()) {  
  19.             LOG(DEBUG) << ”HalManifest::getTransport(“ << to_string(mType) << “): Cannot find interface ’”  
  20.                       << interfaceName << ”’ in ” << package << “@” << to_string(v);  
  21.             continue;  
  22.         }  
  23.         const auto &instances = it->second.instances;  
  24.         if (instances.find(instanceName) == instances.end()) {  
  25.             LOG(DEBUG) << ”HalManifest::getTransport(“ << to_string(mType) << “): Cannot find instance ’”  
  26.                       << instanceName << ”’ in ”  
  27.                       << package << ”@” << to_string(v) << “::” << interfaceName;  
  28.             continue;  
  29.         }  
  30.         return hal->transportArch.transport;  
  31.     }  
  32.     LOG(DEBUG) << ”HalManifest::getTransport(“ << to_string(mType) << “): Cannot get transport for ”  
  33.                  << package << ”@” << v << “::” << interfaceName << “/” << instanceName;  
  34.     return Transport::EMPTY;  
  35.   
  36. }  
Transport HalManifest::getTransport(const std::string &package, const Version &v,
            const std::string &interfaceName, const std::string &instanceName) const {

    for (const ManifestHal *hal : getHals(package)) {
        bool found = false;
        for (auto& ver : hal->versions) {
            if (ver.majorVer == v.majorVer && ver.minorVer >= v.minorVer) {
                found = true;
                break;
            }
        }
        if (!found) {
            LOG(DEBUG) << "HalManifest::getTransport(" << to_string(mType) << "): Cannot find "
                      << to_string(v) << " in supported versions of " << package;
            continue;
        }
        auto it = hal->interfaces.find(interfaceName);
        if (it == hal->interfaces.end()) {
            LOG(DEBUG) << "HalManifest::getTransport(" << to_string(mType) << "): Cannot find interface '"
                      << interfaceName << "' in " << package << "@" << to_string(v);
            continue;
        }
        const auto &instances = it->second.instances;
        if (instances.find(instanceName) == instances.end()) {
            LOG(DEBUG) << "HalManifest::getTransport(" << to_string(mType) << "): Cannot find instance '"
                      << instanceName << "' in "
                      << package << "@" << to_string(v) << "::" << interfaceName;
            continue;
        }
        return hal->transportArch.transport;
    }
    LOG(DEBUG) << "HalManifest::getTransport(" << to_string(mType) << "): Cannot get transport for "
                 << package << "@" << v << "::" << interfaceName << "/" << instanceName;
    return Transport::EMPTY;

}

这里就是读取解析manifest.xml,并从manifest.xml中查找对应hidl服务的transport类型。


            </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值