Add setTestMode to Context Hub AIDL definition

Bug: 258074235
Test: make android.hardware.contexthub-update-api
Test: atest VtsAidlHalContextHubTargetTest
Test: m
Change-Id: I276857a2a2254cdf41bc8767ab972436a9015418
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHub.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHub.aidl
index c1f4df8..d66e1ac 100644
--- a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHub.aidl
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHub.aidl
@@ -47,5 +47,6 @@
   void onHostEndpointDisconnected(char hostEndpointId);
   long[] getPreloadedNanoappIds();
   void onNanSessionStateChanged(in boolean state);
-  const int EX_CONTEXT_HUB_UNSPECIFIED = -1;
+  void setTestMode(in boolean enable);
+  const int EX_CONTEXT_HUB_UNSPECIFIED = (-1);
 }
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanoappBinary.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanoappBinary.aidl
index d53b28f..741a9cf 100644
--- a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanoappBinary.aidl
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanoappBinary.aidl
@@ -40,7 +40,7 @@
   byte targetChreApiMajorVersion;
   byte targetChreApiMinorVersion;
   byte[] customBinary;
-  const int FLAG_SIGNED = 1;
-  const int FLAG_ENCRYPTED = 2;
-  const int FLAG_TCM_CAPABLE = 4;
+  const int FLAG_SIGNED = (1 << 0);
+  const int FLAG_ENCRYPTED = (1 << 1);
+  const int FLAG_TCM_CAPABLE = (1 << 2);
 }
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/Setting.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/Setting.aidl
index d998478..aeb720b 100644
--- a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/Setting.aidl
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/Setting.aidl
@@ -35,10 +35,10 @@
 @Backing(type="byte") @VintfStability
 enum Setting {
   LOCATION = 1,
-  WIFI_MAIN = 2,
-  WIFI_SCANNING = 3,
-  AIRPLANE_MODE = 4,
-  MICROPHONE = 5,
-  BT_MAIN = 6,
-  BT_SCANNING = 7,
+  WIFI_MAIN,
+  WIFI_SCANNING,
+  AIRPLANE_MODE,
+  MICROPHONE,
+  BT_MAIN,
+  BT_SCANNING,
 }
diff --git a/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl b/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
index 7f50730..f9838bd 100644
--- a/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
+++ b/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
@@ -217,6 +217,22 @@
     void onNanSessionStateChanged(in boolean state);
 
     /**
+     * Puts the context hub in and out of test mode. Test mode is a clean state
+     * where tests can be executed in the same environment. If enable is true,
+     * this will enable test mode by unloading all nanoapps. If enable is false,
+     * this will disable test mode and reverse the actions of enabling test mode
+     * by loading all preloaded nanoapps. This puts CHRE in a normal state.
+     *
+     * This should only be used for a test environment, either through a
+     * @TestApi or development tools. This should not be used in a production
+     * environment.
+     *
+     * @param enable If true, put the context hub in test mode. If false, disable
+     *               test mode.
+     */
+    void setTestMode(in boolean enable);
+
+    /**
      * Error codes that are used as service specific errors with the AIDL return
      * value EX_SERVICE_SPECIFIC.
      */
diff --git a/contexthub/aidl/default/ContextHub.cpp b/contexthub/aidl/default/ContextHub.cpp
index 615ac5c..b98bfb2 100644
--- a/contexthub/aidl/default/ContextHub.cpp
+++ b/contexthub/aidl/default/ContextHub.cpp
@@ -113,6 +113,10 @@
     }
 }
 
+ScopedAStatus ContextHub::setTestMode(bool /* enable */) {
+    return ndk::ScopedAStatus::ok();
+}
+
 ScopedAStatus ContextHub::onHostEndpointConnected(const HostEndpointInfo& in_info) {
     mConnectedHostEndpoints.insert(in_info.hostEndpointId);
 
diff --git a/contexthub/aidl/default/include/contexthub-impl/ContextHub.h b/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
index b3998b9..dc9aef0 100644
--- a/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
+++ b/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
@@ -44,6 +44,7 @@
             int32_t in_contextHubId, const std::shared_ptr<IContextHubCallback>& in_cb) override;
     ::ndk::ScopedAStatus sendMessageToHub(int32_t in_contextHubId,
                                           const ContextHubMessage& in_message) override;
+    ::ndk::ScopedAStatus setTestMode(bool enable) override;
     ::ndk::ScopedAStatus onHostEndpointConnected(const HostEndpointInfo& in_info) override;
 
     ::ndk::ScopedAStatus onHostEndpointDisconnected(char16_t in_hostEndpointId) override;
diff --git a/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp b/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
index 4731648..f7ff73d 100644
--- a/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
+++ b/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
@@ -84,6 +84,26 @@
     }
 }
 
+TEST_P(ContextHubAidl, TestEnableTestMode) {
+    Status status = contextHub->setTestMode(true);
+    if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
+        status.transactionError() == android::UNKNOWN_TRANSACTION) {
+        return;  // not supported -> old API; or not implemented
+    }
+
+    ASSERT_TRUE(status.isOk());
+}
+
+TEST_P(ContextHubAidl, TestDisableTestMode) {
+    Status status = contextHub->setTestMode(false);
+    if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
+        status.transactionError() == android::UNKNOWN_TRANSACTION) {
+        return;  // not supported -> old API; or not implemented
+    }
+
+    ASSERT_TRUE(status.isOk());
+}
+
 class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
   public:
     Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {