Refine ContextHub HAL API updates

Makes the NAN session request / state updates a parceable to make it
easier to add parameters to the APIs that use them over time.

Fixes: 271435739
Test: presubmits
Change-Id: I96592742095fd01a45c9fc245516304249b7434f
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 d66e1ac..9f2eb76 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
@@ -46,7 +46,7 @@
   void onHostEndpointConnected(in android.hardware.contexthub.HostEndpointInfo hostEndpointInfo);
   void onHostEndpointDisconnected(char hostEndpointId);
   long[] getPreloadedNanoappIds();
-  void onNanSessionStateChanged(in boolean state);
+  void onNanSessionStateChanged(in android.hardware.contexthub.NanSessionStateUpdate update);
   void setTestMode(in boolean enable);
-  const int EX_CONTEXT_HUB_UNSPECIFIED = (-1);
+  const int EX_CONTEXT_HUB_UNSPECIFIED = (-1) /* -1 */;
 }
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHubCallback.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHubCallback.aidl
index e72ae73..6163cfc 100644
--- a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHubCallback.aidl
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/IContextHubCallback.aidl
@@ -38,6 +38,6 @@
   void handleContextHubMessage(in android.hardware.contexthub.ContextHubMessage msg, in String[] msgContentPerms);
   void handleContextHubAsyncEvent(in android.hardware.contexthub.AsyncEventType evt);
   void handleTransactionResult(in int transactionId, in boolean success);
-  void handleNanSessionRequest(in boolean enable);
+  void handleNanSessionRequest(in android.hardware.contexthub.NanSessionRequest request);
   const int CONTEXTHUB_NAN_TRANSACTION_TIMEOUT_MS = 10000;
 }
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionRequest.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionRequest.aidl
new file mode 100644
index 0000000..d539707
--- /dev/null
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionRequest.aidl
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.contexthub;
+@VintfStability
+parcelable NanSessionRequest {
+  boolean enable;
+}
diff --git a/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionStateUpdate.aidl b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionStateUpdate.aidl
new file mode 100644
index 0000000..80771e2
--- /dev/null
+++ b/contexthub/aidl/aidl_api/android.hardware.contexthub/current/android/hardware/contexthub/NanSessionStateUpdate.aidl
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.contexthub;
+@VintfStability
+parcelable NanSessionStateUpdate {
+  boolean state;
+}
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 741a9cf..fdf3860 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 << 0);
-  const int FLAG_ENCRYPTED = (1 << 1);
-  const int FLAG_TCM_CAPABLE = (1 << 2);
+  const int FLAG_SIGNED = (1 << 0) /* 1 */;
+  const int FLAG_ENCRYPTED = (1 << 1) /* 2 */;
+  const int FLAG_TCM_CAPABLE = (1 << 2) /* 4 */;
 }
diff --git a/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl b/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
index f9838bd..17bffd6 100644
--- a/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
+++ b/contexthub/aidl/android/hardware/contexthub/IContextHub.aidl
@@ -20,6 +20,7 @@
 import android.hardware.contexthub.ContextHubMessage;
 import android.hardware.contexthub.HostEndpointInfo;
 import android.hardware.contexthub.IContextHubCallback;
+import android.hardware.contexthub.NanSessionStateUpdate;
 import android.hardware.contexthub.NanoappBinary;
 import android.hardware.contexthub.NanoappInfo;
 import android.hardware.contexthub.Setting;
@@ -212,9 +213,9 @@
      * explicitly invoke handleNanSessionRequest() at a later point in time to attempt to
      * re-enable NAN.
      *
-     * @param state True if the NAN session is currently enabled.
+     * @param update Information about the latest NAN session state.
      */
-    void onNanSessionStateChanged(in boolean state);
+    void onNanSessionStateChanged(in NanSessionStateUpdate update);
 
     /**
      * Puts the context hub in and out of test mode. Test mode is a clean state
diff --git a/contexthub/aidl/android/hardware/contexthub/IContextHubCallback.aidl b/contexthub/aidl/android/hardware/contexthub/IContextHubCallback.aidl
index cff1893..bfcb51e 100644
--- a/contexthub/aidl/android/hardware/contexthub/IContextHubCallback.aidl
+++ b/contexthub/aidl/android/hardware/contexthub/IContextHubCallback.aidl
@@ -18,6 +18,7 @@
 
 import android.hardware.contexthub.AsyncEventType;
 import android.hardware.contexthub.ContextHubMessage;
+import android.hardware.contexthub.NanSessionRequest;
 import android.hardware.contexthub.NanoappInfo;
 
 @VintfStability
@@ -85,10 +86,9 @@
      * request times out, onNanSessionStateChanged() will be invoked with the state that the session
      * was previously in.
      *
-     * @param enable Whether the NAN session should be enabled or disabled depending on whether the
-     *        Contexthub needs access to NAN.
+     * @param request Request from the HAL indicating the latest NAN session state it would like.
      */
-    void handleNanSessionRequest(in boolean enable);
+    void handleNanSessionRequest(in NanSessionRequest request);
 
     /**
      * Amount of time, in milliseconds, that a handleNanSessionRequest can be pending before the
diff --git a/contexthub/aidl/android/hardware/contexthub/NanSessionRequest.aidl b/contexthub/aidl/android/hardware/contexthub/NanSessionRequest.aidl
new file mode 100644
index 0000000..c462ba1
--- /dev/null
+++ b/contexthub/aidl/android/hardware/contexthub/NanSessionRequest.aidl
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2023 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.contexthub;
+
+/**
+ * Contains information needed to request a state change for a NAN session.
+ */
+@VintfStability
+parcelable NanSessionRequest {
+    /**
+     * Whether the NAN session should be enabled or disabled depending on whether the Contexthub
+     * needs access to NAN.
+     */
+    boolean enable;
+}
diff --git a/contexthub/aidl/android/hardware/contexthub/NanSessionStateUpdate.aidl b/contexthub/aidl/android/hardware/contexthub/NanSessionStateUpdate.aidl
new file mode 100644
index 0000000..a58eda5
--- /dev/null
+++ b/contexthub/aidl/android/hardware/contexthub/NanSessionStateUpdate.aidl
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2023 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.contexthub;
+
+/**
+ * Contains information provided as a response to a NanSessionRequest.
+ */
+@VintfStability
+parcelable NanSessionStateUpdate {
+    /**
+     * True if the NAN session is currently enabled.
+     */
+    boolean state;
+}
diff --git a/contexthub/aidl/default/ContextHub.cpp b/contexthub/aidl/default/ContextHub.cpp
index b98bfb2..66f9857 100644
--- a/contexthub/aidl/default/ContextHub.cpp
+++ b/contexthub/aidl/default/ContextHub.cpp
@@ -87,7 +87,7 @@
     return ndk::ScopedAStatus::ok();
 }
 
-ScopedAStatus ContextHub::onNanSessionStateChanged(bool /*sin_state*/) {
+ScopedAStatus ContextHub::onNanSessionStateChanged(const NanSessionStateUpdate& /*in_update*/) {
     return ndk::ScopedAStatus::ok();
 }
 
diff --git a/contexthub/aidl/default/include/contexthub-impl/ContextHub.h b/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
index dc9aef0..02ecf53 100644
--- a/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
+++ b/contexthub/aidl/default/include/contexthub-impl/ContextHub.h
@@ -48,7 +48,7 @@
     ::ndk::ScopedAStatus onHostEndpointConnected(const HostEndpointInfo& in_info) override;
 
     ::ndk::ScopedAStatus onHostEndpointDisconnected(char16_t in_hostEndpointId) override;
-    ::ndk::ScopedAStatus onNanSessionStateChanged(bool in_state) override;
+    ::ndk::ScopedAStatus onNanSessionStateChanged(const NanSessionStateUpdate& in_update) override;
 
   private:
     static constexpr uint32_t kMockHubId = 0;
diff --git a/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp b/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
index b227e4a..9707ecf 100644
--- a/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
+++ b/contexthub/aidl/vts/VtsAidlHalContextHubTargetTest.cpp
@@ -42,6 +42,8 @@
 using ::android::hardware::contexthub::NanoappBinary;
 using ::android::hardware::contexthub::NanoappInfo;
 using ::android::hardware::contexthub::NanoappRpcService;
+using ::android::hardware::contexthub::NanSessionRequest;
+using ::android::hardware::contexthub::NanSessionStateUpdate;
 using ::android::hardware::contexthub::Setting;
 using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
 using ::android::hardware::contexthub::vts_utils::waitForCallback;
@@ -121,7 +123,9 @@
         return Status::ok();
     }
 
-    Status handleNanSessionRequest(bool /* enable */) override { return Status::ok(); }
+    Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
+        return Status::ok();
+    }
 };
 
 TEST_P(ContextHubAidl, TestRegisterCallback) {
@@ -153,7 +157,9 @@
         return Status::ok();
     }
 
-    Status handleNanSessionRequest(bool /* enable */) override { return Status::ok(); }
+    Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
+        return Status::ok();
+    }
 
     std::promise<std::vector<NanoappInfo>> promise;
 };
@@ -218,7 +224,9 @@
         return Status::ok();
     }
 
-    Status handleNanSessionRequest(bool /* enable */) override { return Status::ok(); }
+    Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
+        return Status::ok();
+    }
 
     uint32_t expectedTransactionId = 0;
     std::promise<bool> promise;
@@ -382,8 +390,11 @@
 }
 
 TEST_P(ContextHubAidl, TestNanSessionStateChange) {
-    ASSERT_TRUE(contextHub->onNanSessionStateChanged(true /*state*/).isOk());
-    ASSERT_TRUE(contextHub->onNanSessionStateChanged(false /*state*/).isOk());
+    NanSessionStateUpdate update;
+    update.state = true;
+    ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
+    update.state = false;
+    ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
 }
 
 std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {