Add SubHal header for Sensors HAL 2.1

Bug: 149758467
Test: compile
Change-Id: Id10d20e5d24571572565ac5d968f265ef7d7070f
diff --git a/sensors/2.0/multihal/Android.bp b/sensors/2.0/multihal/Android.bp
index 7213b44..3ce3390 100644
--- a/sensors/2.0/multihal/Android.bp
+++ b/sensors/2.0/multihal/Android.bp
@@ -28,6 +28,7 @@
     shared_libs: [
         "android.hardware.sensors@2.0",
         "android.hardware.sensors@2.0-ScopedWakelock",
+        "android.hardware.sensors@2.1",
         "libbase",
         "libcutils",
         "libfmq",
diff --git a/sensors/common/default/2.X/multihal/Android.bp b/sensors/common/default/2.X/multihal/Android.bp
index 2b4b3bf..6122323 100644
--- a/sensors/common/default/2.X/multihal/Android.bp
+++ b/sensors/common/default/2.X/multihal/Android.bp
@@ -16,11 +16,12 @@
 cc_defaults {
     name: "android.hardware.sensors@2.X-multihal-defaults",
     header_libs: [
-        "android.hardware.sensors@2.0-multihal.header",
+        "android.hardware.sensors@2.X-multihal.header",
     ],
     shared_libs: [
         "android.hardware.sensors@1.0",
         "android.hardware.sensors@2.0",
+        "android.hardware.sensors@2.1",
         "libbase",
         "libcutils",
         "libfmq",
@@ -32,9 +33,24 @@
     cflags: ["-DLOG_TAG=\"SensorsMultiHal\""],
 }
 
+// Header target for sub-HALs that implement the Multi-HAL 2.0 interface
 cc_library_headers {
     name: "android.hardware.sensors@2.0-multihal.header",
     vendor_available: true,
+    export_include_dirs: ["include/V2_0"],
+}
+
+// Header target for sub-HALs that implement the Multi-HAL 2.1 interface
+cc_library_headers {
+    name: "android.hardware.sensors@2.1-multihal.header",
+    vendor_available: true,
+    export_include_dirs: ["include/V2_1"],
+}
+
+// Header target for Multi-HAL so it can reference both 2.0/2.1 headers
+cc_library_headers {
+    name: "android.hardware.sensors@2.X-multihal.header",
+    vendor_available: true,
     export_include_dirs: ["include"],
 }
 
@@ -49,7 +65,7 @@
     ],
     vendor_available: true,
     export_header_lib_headers: [
-        "android.hardware.sensors@2.0-multihal.header",
+        "android.hardware.sensors@2.X-multihal.header",
     ],
 }
 
@@ -62,6 +78,9 @@
     srcs: [
         "ScopedWakelock.cpp",
     ],
+    header_libs: [
+        "android.hardware.sensors@2.0-multihal.header",
+    ],
     vendor_available: true,
     export_header_lib_headers: [
         "android.hardware.sensors@2.0-multihal.header",
@@ -78,6 +97,9 @@
         "ScopedWakelock.cpp",
     ],
     vendor_available: true,
+    header_libs: [
+        "android.hardware.sensors@2.0-multihal.header",
+    ],
     export_header_lib_headers: [
         "android.hardware.sensors@2.0-multihal.header",
     ],
diff --git a/sensors/common/default/2.X/multihal/HalProxy.cpp b/sensors/common/default/2.X/multihal/HalProxy.cpp
index ac6f17a..518e138 100644
--- a/sensors/common/default/2.X/multihal/HalProxy.cpp
+++ b/sensors/common/default/2.X/multihal/HalProxy.cpp
@@ -16,8 +16,6 @@
 
 #include "HalProxy.h"
 
-#include "SubHal.h"
-
 #include <android/hardware/sensors/2.0/types.h>
 
 #include <android-base/file.h>
diff --git a/sensors/common/default/2.X/multihal/ScopedWakelock.cpp b/sensors/common/default/2.X/multihal/ScopedWakelock.cpp
index d85d4a7..bf2ad35 100644
--- a/sensors/common/default/2.X/multihal/ScopedWakelock.cpp
+++ b/sensors/common/default/2.X/multihal/ScopedWakelock.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "ScopedWakelock.h"
+#include "V2_0/ScopedWakelock.h"
 
 namespace android {
 namespace hardware {
diff --git a/sensors/common/default/2.X/multihal/include/HalProxy.h b/sensors/common/default/2.X/multihal/include/HalProxy.h
index 978f7cf..d7e8795 100644
--- a/sensors/common/default/2.X/multihal/include/HalProxy.h
+++ b/sensors/common/default/2.X/multihal/include/HalProxy.h
@@ -16,8 +16,9 @@
 
 #pragma once
 
-#include "ScopedWakelock.h"
-#include "SubHal.h"
+#include "V2_0/ScopedWakelock.h"
+#include "V2_0/SubHal.h"
+#include "V2_1/SubHal.h"
 
 #include <android/hardware/sensors/2.0/ISensors.h>
 #include <android/hardware/sensors/2.0/types.h>
diff --git a/sensors/common/default/2.X/multihal/include/ScopedWakelock.h b/sensors/common/default/2.X/multihal/include/V2_0/ScopedWakelock.h
similarity index 100%
rename from sensors/common/default/2.X/multihal/include/ScopedWakelock.h
rename to sensors/common/default/2.X/multihal/include/V2_0/ScopedWakelock.h
diff --git a/sensors/common/default/2.X/multihal/include/SubHal.h b/sensors/common/default/2.X/multihal/include/V2_0/SubHal.h
similarity index 99%
rename from sensors/common/default/2.X/multihal/include/SubHal.h
rename to sensors/common/default/2.X/multihal/include/V2_0/SubHal.h
index 92ae3a6..2a80e6b 100644
--- a/sensors/common/default/2.X/multihal/include/SubHal.h
+++ b/sensors/common/default/2.X/multihal/include/V2_0/SubHal.h
@@ -159,7 +159,7 @@
  * library. This function will only be invoked once at initialization time.
  *
  * NOTE: The supported sensors HAL version must match SUB_HAL_2_0_VERSION exactly or the HalProxy
- * will fail to initialize.
+ *     will fail to initialize.
  *
  * @param uint32_t when this function returns, this parameter must contain the HAL version that
  *     this sub-HAL supports. To support this version of multi-HAL, this must be set to
diff --git a/sensors/common/default/2.X/multihal/include/SubHal.h b/sensors/common/default/2.X/multihal/include/V2_1/SubHal.h
similarity index 78%
copy from sensors/common/default/2.X/multihal/include/SubHal.h
copy to sensors/common/default/2.X/multihal/include/V2_1/SubHal.h
index 92ae3a6..cd49422 100644
--- a/sensors/common/default/2.X/multihal/include/SubHal.h
+++ b/sensors/common/default/2.X/multihal/include/V2_1/SubHal.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2019 The Android Open Source Project
+ * Copyright (C) 2020 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,27 +16,23 @@
 
 #pragma once
 
-#include "ScopedWakelock.h"
+#include "V2_0/ScopedWakelock.h"
 
-#include <android/hardware/sensors/1.0/types.h>
-#include <android/hardware/sensors/2.0/ISensors.h>
+#include <android/hardware/sensors/2.1/ISensors.h>
+#include <android/hardware/sensors/2.1/types.h>
 
 #include <vector>
 
 // Indicates the current version of the multiHAL interface formatted as (HAL major version) << 24 |
 // (HAL minor version) << 16 | (multiHAL version)
-#define SUB_HAL_2_0_VERSION 0x02000000
+#define SUB_HAL_2_1_VERSION 0x02010000
 
 namespace android {
 namespace hardware {
 namespace sensors {
-namespace V2_0 {
+namespace V2_1 {
 namespace implementation {
 
-using ::android::hardware::sensors::V1_0::Event;
-using ::android::hardware::sensors::V1_0::Result;
-using ::android::hardware::sensors::V1_0::SensorInfo;
-
 /**
  * Interface that contains several callbacks into the HalProxy class to communicate dynamic sensor
  * changes and sensor events to the framework and acquire wake locks. The HalProxy will ensure
@@ -45,6 +41,8 @@
  */
 class IHalProxyCallback : public ISensorsCallback {
   public:
+    using ScopedWakelock = V2_0::implementation::ScopedWakelock;
+
     /**
      * Thread-safe callback used to post events to the HalProxy. Sub-HALs should invoke this
      * whenever new sensor events need to be delivered to the sensors framework. Once invoked, the
@@ -55,7 +53,7 @@
      *
      * The provided ScopedWakelock must be locked if the events are from wakeup sensors. If it's
      * not locked accordingly, the HalProxy will crash as this indicates the sub-HAL isn't compliant
-     * with the sensors HAL 2.0 specification. Additionally, since ScopedWakelock isn't copyable,
+     * with the sensors HAL specification. Additionally, since ScopedWakelock isn't copyable,
      * the HalProxy will take ownership of the wake lock given when this method is invoked. Once the
      * method returns, the HalProxy will handle holding the wake lock, if necessary, until the
      * framework has successfully processed any wakeup events.
@@ -71,7 +69,7 @@
      * @param wakelock ScopedWakelock that should be locked to send events from wake sensors and
      *     unlocked otherwise.
      */
-    virtual void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) = 0;
+    virtual void postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) = 0;
 
     /**
      * Initializes a ScopedWakelock on the stack that, when locked, will increment the reference
@@ -86,9 +84,9 @@
 
 /**
  * ISensorsSubHal is an interface that sub-HALs must implement in order to be compliant with
- * multihal 2.0 and in order for the HalProxy to successfully load and communicate with the sub-HAL.
+ * multihal and in order for the HalProxy to successfully load and communicate with the sub-HAL.
  *
- * Any vendor wishing to implement this interface and support multihal 2.0 will need to create a
+ * Any vendor wishing to implement this interface and support multihal will need to create a
  * dynamic library that exposes sensorsHalGetSubHal (defined below). This library will be loaded by
  * the HalProxy when the sensors HAL is initialized and then the HalProxy will retrieve the vendor's
  * implementation of sensorsHalGetSubHal.
@@ -103,11 +101,25 @@
   public:
     // The ISensors version of initialize isn't used for multihal. Instead, sub-HALs must implement
     // the version below to allow communciation logic to centralized in the HalProxy
-    Return<Result> initialize(
-            const ::android::hardware::MQDescriptorSync<Event>& /* eventQueueDescriptor */,
+    Return<V1_0::Result> initialize_2_1(
+            const ::android::hardware::MQDescriptorSync<V2_1::Event>& /* eventQueueDescriptor */,
             const ::android::hardware::MQDescriptorSync<uint32_t>& /* wakeLockDescriptor */,
             const sp<ISensorsCallback>& /* sensorsCallback */) final {
-        return Result::INVALID_OPERATION;
+        return V1_0::Result::INVALID_OPERATION;
+    }
+
+    Return<V1_0::Result> initialize(
+            const ::android::hardware::MQDescriptorSync<V1_0::Event>& /* eventQueueDescriptor */,
+            const ::android::hardware::MQDescriptorSync<uint32_t>& /* wakeLockDescriptor */,
+            const sp<V2_0::ISensorsCallback>& /* sensorsCallback */) final {
+        return V1_0::Result::INVALID_OPERATION;
+    }
+
+    // Several HAL 2.0 methods won't be invoked on HAL 2.1 so they are stubbed out below.
+    Return<void> getSensorsList(getSensorsList_cb /* _hidl_cb */) final { return Void(); }
+
+    Return<V1_0::Result> injectSensorData(const V1_0::Event& /* event */) final {
+        return V1_0::Result::INVALID_OPERATION;
     }
 
     /**
@@ -143,28 +155,26 @@
      *     should be created.
      * @return result OK on success
      */
-    virtual Return<Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) = 0;
+    virtual Return<V1_0::Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) = 0;
 };
 
 }  // namespace implementation
-}  // namespace V2_0
+}  // namespace V2_1
 }  // namespace sensors
 }  // namespace hardware
 }  // namespace android
 
-using ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal;
-
 /**
  * Function that must be exported so the HalProxy class can invoke it on the sub-HAL dynamic
  * library. This function will only be invoked once at initialization time.
  *
- * NOTE: The supported sensors HAL version must match SUB_HAL_2_0_VERSION exactly or the HalProxy
- * will fail to initialize.
+ * NOTE: The supported sensors HAL version must match SUB_HAL_2_1_VERSION or the HalProxy
+ *     will fail to initialize.
  *
  * @param uint32_t when this function returns, this parameter must contain the HAL version that
- *     this sub-HAL supports. To support this version of multi-HAL, this must be set to
- *     SUB_HAL_2_0_VERSION.
+ *     this sub-HAL supports. This must be set to SUB_HAL_2_1_VERSION.
  * @return A statically allocated, valid ISensorsSubHal implementation.
  */
-__attribute__((visibility("default"))) extern "C" ISensorsSubHal* sensorsHalGetSubHal(
-        uint32_t* version);
+__attribute__((visibility(
+        "default"))) extern "C" ::android::hardware::sensors::V2_1::implementation::ISensorsSubHal*
+sensorsHalGetSubHal_2_1(uint32_t* version);
diff --git a/sensors/common/default/2.X/multihal/tests/Android.bp b/sensors/common/default/2.X/multihal/tests/Android.bp
index afb63cc..7692b51 100644
--- a/sensors/common/default/2.X/multihal/tests/Android.bp
+++ b/sensors/common/default/2.X/multihal/tests/Android.bp
@@ -26,6 +26,7 @@
         "android.hardware.sensors@1.0",
         "android.hardware.sensors@2.0",
         "android.hardware.sensors@2.0-ScopedWakelock",
+        "android.hardware.sensors@2.1",
         "libcutils",
         "libfmq",
         "libhardware",
@@ -85,6 +86,8 @@
     shared_libs: [
         "android.hardware.sensors@1.0",
         "android.hardware.sensors@2.0",
+        "android.hardware.sensors@2.0-ScopedWakelock",
+        "android.hardware.sensors@2.1",
         "libbase",
         "libcutils",
         "libfmq",
diff --git a/sensors/common/default/2.X/multihal/tests/HalProxy_test.cpp b/sensors/common/default/2.X/multihal/tests/HalProxy_test.cpp
index 4633a75..867c4a1 100644
--- a/sensors/common/default/2.X/multihal/tests/HalProxy_test.cpp
+++ b/sensors/common/default/2.X/multihal/tests/HalProxy_test.cpp
@@ -19,8 +19,8 @@
 #include <fmq/MessageQueue.h>
 
 #include "HalProxy.h"
-#include "ScopedWakelock.h"
 #include "SensorsSubHal.h"
+#include "V2_0/ScopedWakelock.h"
 
 #include <chrono>
 #include <set>
diff --git a/sensors/common/default/2.X/multihal/tests/fake_subhal/SensorsSubHal.h b/sensors/common/default/2.X/multihal/tests/fake_subhal/SensorsSubHal.h
index c1e3647..6da4404 100644
--- a/sensors/common/default/2.X/multihal/tests/fake_subhal/SensorsSubHal.h
+++ b/sensors/common/default/2.X/multihal/tests/fake_subhal/SensorsSubHal.h
@@ -16,7 +16,7 @@
 
 #pragma once
 
-#include "SubHal.h"
+#include "V2_0/SubHal.h"
 
 #include "Sensor.h"