From d04323c42a56959ff882bd2574f906b6fffb55c3 Mon Sep 17 00:00:00 2001 From: yue Date: Thu, 12 May 2022 18:32:34 +0800 Subject: [PATCH] fix:modify load library mode of hdi Signed-off-by: yue --- uhdf2/hdi/src/hdi_support.cpp | 37 +++++++++---------- .../unittest/sample_proxy.cpp | 2 +- .../sample_service_cpp/foo_stub.cpp | 15 -------- .../test/hdi_sample/sample_service_cpp/ifoo.h | 2 - .../sample_service_stub.cpp | 2 +- uhdf2/include/hdi/hdi_support.h | 2 +- 6 files changed, 21 insertions(+), 39 deletions(-) diff --git a/uhdf2/hdi/src/hdi_support.cpp b/uhdf2/hdi/src/hdi_support.cpp index 91d8fca..d3b0c69 100644 --- a/uhdf2/hdi/src/hdi_support.cpp +++ b/uhdf2/hdi/src/hdi_support.cpp @@ -18,9 +18,7 @@ #include #include #include -#include #include -#include #include "hdf_base.h" #include "hdf_log.h" @@ -43,17 +41,15 @@ static const std::regex reInfDesc("[a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0 "([a-zA-Z_][a-zA-Z0-9_]*)"); } // namespace -static int32_t ParseInterface(const std::string &fullName, std::string &interface, uint32_t &versionMajor, +static int32_t ParseInterface(const std::string &desc, std::string &interface, uint32_t &versionMajor, uint32_t &versionMinor) { std::smatch result; - if (!std::regex_match(fullName, result, reInfDesc)) { - HDF_LOGE("invalid format of interface descriptor '%{public}s'", fullName.c_str()); + if (!std::regex_match(desc, result, reInfDesc)) { return HDF_FAILURE; } if (result.size() < INTERFACE_MATCH_RESIZE) { - HDF_LOGE("failed to parse interface descriptor '%{public}s'", fullName.c_str()); return HDF_FAILURE; } @@ -63,7 +59,6 @@ static int32_t ParseInterface(const std::string &fullName, std::string &interfac interface = interfaceName[0] == 'I' ? interfaceName.substr(1) : interfaceName; if (interface.empty()) { - HDF_LOGE("failed to get invalid interface name"); return HDF_FAILURE; } @@ -91,37 +86,39 @@ static std::string TransFileName(const std::string& interfaceName) return result; } -/* +/* service name: xxx_service * interface descriptor name: ohos.hdi.sample.v1_0.IFoo * interface: Foo * versionMajor: 1 * versionMinor: 0 - * library name: libfoo_service_1.0.z.so + * library name: libfoo_xxx_service_1.0.z.so * method name: FooImplGetInstance */ -void *LoadHdiImpl(const char *fullIfName) +void *LoadHdiImpl(const char *desc, const char *serviceName) { char path[PATH_MAX + 1] = {0}; char resolvedPath[PATH_MAX + 1] = {0}; // interface descriptor name like "ohos.hdi.sample.v1_0.IFoo", the last two are version and interface base name - if (fullIfName == nullptr) { - HDF_LOGE("fullIfName is nullptr"); + if (desc == nullptr || serviceName == nullptr) { + HDF_LOGE("%{public}s interface descriptor or service name is nullptr", __func__); + return nullptr; + } + + if (strlen(desc) == 0 || strlen(serviceName) == 0) { + HDF_LOGE("%{public}s invalid interface descriptor or service name", __func__); return nullptr; } - HDF_LOGD("load interface impl: %{public}s", fullIfName); - std::string fullName = fullIfName; std::string interfaceName; uint32_t versionMajor = 0; uint32_t versionMinor = 0; - if (ParseInterface(fullIfName, interfaceName, versionMajor, versionMinor) != HDF_SUCCESS) { - HDF_LOGE("failed to parse hdi interface info"); + if (ParseInterface(desc, interfaceName, versionMajor, versionMinor) != HDF_SUCCESS) { + HDF_LOGE("failed to parse hdi interface info from '%{public}s'", desc); return nullptr; } - // hdi implement name like libfoo_service_1.0.z.so - if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/lib%s_service_%u.%u.z.so", HDI_SO_PATH, - TransFileName(interfaceName).data(), versionMajor, versionMinor) < 0) { + if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/lib%s_%s_%u.%u.z.so", HDI_SO_PATH, + TransFileName(interfaceName).c_str(), serviceName, versionMajor, versionMinor) < 0) { HDF_LOGE("%{public}s snprintf_s failed", __func__); return nullptr; } @@ -129,6 +126,8 @@ void *LoadHdiImpl(const char *fullIfName) HDF_LOGE("%{public}s invalid hdi impl so name %{public}s", __func__, path); return nullptr; } + + HDF_LOGD("load interface impl lib: %{public}s", resolvedPath); void *handler = dlopen(resolvedPath, RTLD_LAZY); if (handler == nullptr) { HDF_LOGE("%{public}s dlopen failed %{public}s", __func__, dlerror()); diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp index 73744e2..3e2640b 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp @@ -46,7 +46,7 @@ sptr ISample::Get(const std::string &serviceName, bool isStub) } std::string desc = Str16ToStr8(ISample::GetDescriptor()); - void *impl = LoadHdiImpl(desc.data()); + void *impl = LoadHdiImpl(desc.data(), serviceName.c_str()); if (impl == nullptr) { HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); return nullptr; diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp index 3be47a7..0fd075e 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp @@ -28,21 +28,6 @@ namespace OHOS { namespace HDI { namespace Sample { namespace V1_0 { -sptr IFoo::Get(bool isStub) -{ - if (!isStub) { - return nullptr; - } - std::string desc = Str16ToStr8(IFoo::GetDescriptor()); - void *impl = LoadHdiImpl(desc.data()); - if (impl == nullptr) { - HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); - return nullptr; - } - - return reinterpret_cast(impl); -} - FooStub::FooStub(const sptr serviceImpl) : IPCObjectStub(IFoo::GetDescriptor()), impl_(serviceImpl) {} FooStub::~FooStub() diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h index 7d9ed9f..06be1b5 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h @@ -33,8 +33,6 @@ public: virtual ~IFoo() = default; virtual int32_t PingTest(bool input, bool &output) = 0; - - static sptr Get(bool isStub); }; } // namespace V1_0 } // namespace Sample diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp index 5947f3b..25caedd 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp @@ -37,7 +37,7 @@ sptr ISample::Get(const std::string &serviceName, bool isStub) } std::string desc = Str16ToStr8(ISample::GetDescriptor()); - void *impl = LoadHdiImpl(desc.data()); + void *impl = LoadHdiImpl(desc.data(), serviceName.c_str()); if (impl == nullptr) { HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); return nullptr; diff --git a/uhdf2/include/hdi/hdi_support.h b/uhdf2/include/hdi/hdi_support.h index 1787540..d4c301e 100644 --- a/uhdf2/include/hdi/hdi_support.h +++ b/uhdf2/include/hdi/hdi_support.h @@ -22,7 +22,7 @@ extern "C" { #endif /* __cplusplus */ -void *LoadHdiImpl(const char *fullIfName); +void *LoadHdiImpl(const char *desc, const char *serviceName); #ifdef __cplusplus } -- Gitee