transport: Remove template code which has become static
Implement template registerPassthroughServiceImplementation
via non-template implementation that takes interface names
and operates on IBase.
Test: build and flash
Change-Id: Id69e383e2e4e334bf1ccce75409a0ab967210310
diff --git a/transport/LegacySupport.cpp b/transport/LegacySupport.cpp
index 866de5f..399b499 100644
--- a/transport/LegacySupport.cpp
+++ b/transport/LegacySupport.cpp
@@ -29,8 +29,8 @@
namespace details {
__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
- const std::string& interfaceName, RegisterServiceBaseCb registerServiceCb,
- const std::string& serviceName) {
+ const std::string& interfaceName, const std::string& expectInterfaceName,
+ RegisterServiceCb registerServiceCb, const std::string& serviceName) {
sp<IBase> service =
getRawServiceInternal(interfaceName, serviceName, true /*retry*/, true /*getStub*/);
@@ -39,20 +39,26 @@
serviceName.c_str());
return EXIT_FAILURE;
}
-
- LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!", interfaceName.c_str(),
- serviceName.c_str());
+ if (service->isRemote()) {
+ ALOGE("Implementation of %s/%s is remote!", interfaceName.c_str(), serviceName.c_str());
+ return EXIT_FAILURE;
+ }
std::string actualName;
Return<void> result = service->interfaceDescriptor(
[&actualName](const hidl_string& descriptor) { actualName = descriptor; });
- LOG_FATAL_IF(!result.isOk(), "Error retrieving interface name from %s/%s: %s",
- interfaceName.c_str(), serviceName.c_str(), result.description());
- LOG_FATAL_IF(actualName != interfaceName, "Implementation of %s/%s is actually %s!",
- interfaceName.c_str(), serviceName.c_str(), actualName.c_str());
+ if (!result.isOk()) {
+ ALOGE("Error retrieving interface name from %s/%s: %s", interfaceName.c_str(),
+ serviceName.c_str(), result.description().c_str());
+ return EXIT_FAILURE;
+ }
+ if (actualName != expectInterfaceName) {
+ ALOGE("Implementation of %s/%s is actually %s, not a %s!", interfaceName.c_str(),
+ serviceName.c_str(), actualName.c_str(), expectInterfaceName.c_str());
+ return EXIT_FAILURE;
+ }
status_t status = registerServiceCb(service, serviceName);
-
if (status == OK) {
ALOGI("Registration complete for %s/%s.", interfaceName.c_str(), serviceName.c_str());
} else {
@@ -66,9 +72,10 @@
} // namespace details
__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
- const std::string& interfaceName, const std::string& serviceName) {
+ const std::string& interfaceName, const std::string& expectInterfaceName,
+ const std::string& serviceName) {
return details::registerPassthroughServiceImplementation(
- interfaceName,
+ interfaceName, expectInterfaceName,
[](const sp<IBase>& service, const std::string& name) {
return details::registerAsServiceInternal(service, name);
},
diff --git a/transport/include/hidl/LegacySupport.h b/transport/include/hidl/LegacySupport.h
index c7ad594..bf63339 100644
--- a/transport/include/hidl/LegacySupport.h
+++ b/transport/include/hidl/LegacySupport.h
@@ -28,64 +28,35 @@
namespace android {
namespace hardware {
namespace details {
-template <typename Interface>
-using RegisterServiceCb = std::function<status_t(const sp<Interface>&, const std::string&)>;
-template <class Interface, class ExpectInterface = Interface>
-__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
- RegisterServiceCb<Interface> registerServiceCb, const std::string& name = "default") {
- sp<Interface> service = Interface::getService(name, true /* getStub */);
-
- if (service == nullptr) {
- ALOGE("Could not get passthrough implementation for %s/%s.",
- Interface::descriptor, name.c_str());
- return EXIT_FAILURE;
- }
-
- LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
- Interface::descriptor, name.c_str());
-
- sp<ExpectInterface> expected = ExpectInterface::castFrom(service);
- LOG_FATAL_IF(expected == nullptr, "Implementation of %s/%s is not a %s!", Interface::descriptor,
- name.c_str(), ExpectInterface::descriptor);
-
- status_t status = registerServiceCb(service, name);
-
- if (status == OK) {
- ALOGI("Registration complete for %s/%s.",
- Interface::descriptor, name.c_str());
- } else {
- ALOGE("Could not register service %s/%s (%d).",
- Interface::descriptor, name.c_str(), status);
- }
-
- return status;
-}
-
-using RegisterServiceBaseCb = RegisterServiceCb<::android::hidl::base::V1_0::IBase>;
+using RegisterServiceCb =
+ std::function<status_t(const sp<::android::hidl::base::V1_0::IBase>&, const std::string&)>;
__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
- const std::string& interfaceName, RegisterServiceBaseCb registerServiceCb,
- const std::string& serviceName = "default");
+ const std::string& interfaceName, const std::string& expectInterfaceName,
+ RegisterServiceCb registerServiceCb, const std::string& serviceName = "default");
} // namespace details
/**
* Registers passthrough service implementation.
*/
+__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
+ const std::string& interfaceName, const std::string& expectInterfaceName,
+ const std::string& serviceName);
+
+inline __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
+ const std::string& interfaceName, const std::string& serviceName = "default") {
+ return registerPassthroughServiceImplementation(interfaceName, interfaceName, serviceName);
+}
+
template <class Interface, class ExpectInterface = Interface>
__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
const std::string& name = "default") {
- return details::registerPassthroughServiceImplementation<Interface, ExpectInterface>(
- [](const sp<Interface>& service, const std::string& name) {
- return service->registerAsService(name);
- },
- name);
+ return registerPassthroughServiceImplementation(Interface::descriptor,
+ ExpectInterface::descriptor, name);
}
-__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
- const std::string& interfaceName, const std::string& serviceName = "default");
-
/**
* Creates default passthrough service implementation. This method never returns.
*
@@ -122,8 +93,9 @@
template <class Interface, class ExpectInterface = Interface>
__attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
const std::string& name = "default") {
- return details::registerPassthroughServiceImplementation<Interface, ExpectInterface>(
- [](const sp<Interface>& service, const std::string& name) {
+ return details::registerPassthroughServiceImplementation(
+ Interface::descriptor, ExpectInterface::descriptor,
+ [](const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
using android::hardware::LazyServiceRegistrar;
return LazyServiceRegistrar::getInstance().registerService(service, name);
},