Audio service: Refactor register interface

Remove all per interface version boilerplate.
Adding a new version now requires only including the file
and adding the version name to the list.

Bug: 134940862
Test: adb shell lshal
Change-Id: Ib6b99d7a2c2079d914970fbe804aaf3c78c143ce
Merged-In: Ib6b99d7a2c2079d914970fbe804aaf3c78c143ce
Signed-off-by: Kevin Rocard <krocard@google.com>
diff --git a/audio/common/all-versions/default/service/service.cpp b/audio/common/all-versions/default/service/service.cpp
index 8a7b2ea..2a6571b 100644
--- a/audio/common/all-versions/default/service/service.cpp
+++ b/audio/common/all-versions/default/service/service.cpp
@@ -36,6 +36,15 @@
 using namespace android::hardware;
 using android::OK;
 
+/** Try to register the provided factories in the provided order.
+ *  If any registers successfully, do not register any other and return true.
+ *  If all fail, return false.
+ */
+template <class... Factories>
+bool registerPassthroughServiceImplementations() {
+    return ((registerPassthroughServiceImplementation<Factories>() != OK) && ...);
+}
+
 int main(int /* argc */, char* /* argv */ []) {
     ::android::ProcessState::initWithDriver("/dev/vndbinder");
     // start a threadpool for vndbinder interactions
@@ -50,30 +59,30 @@
     }
     configureRpcThreadpool(16, true /*callerWillJoin*/);
 
-    bool fail = registerPassthroughServiceImplementation<audio::V5_0::IDevicesFactory>() != OK &&
-                registerPassthroughServiceImplementation<audio::V4_0::IDevicesFactory>() != OK &&
-                registerPassthroughServiceImplementation<audio::V2_0::IDevicesFactory>() != OK;
-    LOG_ALWAYS_FATAL_IF(fail, "Could not register audio core API 2, 4 nor 5");
+    LOG_ALWAYS_FATAL_IF((registerPassthroughServiceImplementations<audio::V5_0::IDevicesFactory,
+                                                                   audio::V4_0::IDevicesFactory,
+                                                                   audio::V2_0::IDevicesFactory>()),
+                        "Could not register audio core API");
 
-    fail = registerPassthroughServiceImplementation<audio::effect::V5_0::IEffectsFactory>() != OK &&
-           registerPassthroughServiceImplementation<audio::effect::V4_0::IEffectsFactory>() != OK &&
-           registerPassthroughServiceImplementation<audio::effect::V2_0::IEffectsFactory>() != OK,
-    LOG_ALWAYS_FATAL_IF(fail, "Could not register audio effect API 2, 4 nor 5");
+    LOG_ALWAYS_FATAL_IF(
+            (registerPassthroughServiceImplementations<audio::effect::V5_0::IEffectsFactory,
+                                                       audio::effect::V4_0::IEffectsFactory,
+                                                       audio::effect::V2_0::IEffectsFactory>()),
+            "Could not register audio effect API");
 
-    fail = registerPassthroughServiceImplementation<soundtrigger::V2_2::ISoundTriggerHw>() != OK &&
-           registerPassthroughServiceImplementation<soundtrigger::V2_1::ISoundTriggerHw>() != OK &&
-           registerPassthroughServiceImplementation<soundtrigger::V2_0::ISoundTriggerHw>() != OK,
-    ALOGW_IF(fail, "Could not register soundtrigger API 2.0, 2.1 nor 2.2");
+    ALOGW_IF((registerPassthroughServiceImplementations<soundtrigger::V2_2::ISoundTriggerHw,
+                                                        soundtrigger::V2_1::ISoundTriggerHw,
+                                                        soundtrigger::V2_0::ISoundTriggerHw>()),
+             "Could not register soundtrigger API");
 
-    fail = registerPassthroughServiceImplementation<
-                   bluetooth::audio::V2_0::IBluetoothAudioProvidersFactory>() != OK;
-    ALOGW_IF(fail, "Could not register Bluetooth Audio API 2.0");
+    ALOGW_IF(registerPassthroughServiceImplementations<
+                     bluetooth::audio::V2_0::IBluetoothAudioProvidersFactory>(),
+             "Could not register Bluetooth audio API");
 
     // remove the old HIDL when Bluetooth Audio Hal V2 has offloading supported
-    fail =
-        registerPassthroughServiceImplementation<bluetooth::a2dp::V1_0::IBluetoothAudioOffload>() !=
-        OK;
-    ALOGW_IF(fail, "Could not register Bluetooth audio offload 1.0");
+    ALOGW_IF(registerPassthroughServiceImplementations<
+                     bluetooth::a2dp::V1_0::IBluetoothAudioOffload>(),
+             "Could not register Bluetooth audio offload API");
 
     joinRpcThreadpool();
 }