Modify HAL for Aware suspension mode

- Add an event to know the device is suspended
- More status code
- Add pub_sub_id in the datapath request to build the relationship

Bug: 247913151
Test: build
Change-Id: I77733c1bca821b77fdab7ed1ed03c1e54c9373b2
diff --git a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
index ed78d35..8c44330 100644
--- a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
+++ b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
@@ -46,6 +46,7 @@
   oneway void eventPublishTerminated(in byte sessionId, in android.hardware.wifi.NanStatus status);
   oneway void eventSubscribeTerminated(in byte sessionId, in android.hardware.wifi.NanStatus status);
   oneway void eventTransmitFollowup(in char id, in android.hardware.wifi.NanStatus status);
+  oneway void eventSuspensionModeChanged(in android.hardware.wifi.NanSuspensionModeChangeInd event);
   oneway void notifyCapabilitiesResponse(in char id, in android.hardware.wifi.NanStatus status, in android.hardware.wifi.NanCapabilities capabilities);
   oneway void notifyConfigResponse(in char id, in android.hardware.wifi.NanStatus status);
   oneway void notifyCreateDataInterfaceResponse(in char id, in android.hardware.wifi.NanStatus status);
diff --git a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanInitiateDataPathRequest.aidl b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanInitiateDataPathRequest.aidl
index 697e29a..740a140 100644
--- a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanInitiateDataPathRequest.aidl
+++ b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanInitiateDataPathRequest.aidl
@@ -42,4 +42,5 @@
   android.hardware.wifi.NanDataPathSecurityConfig securityConfig;
   byte[] appInfo;
   byte[] serviceNameOutOfBand;
+  byte discoverySessionId;
 }
diff --git a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
index 5542ff6..0f873b5 100644
--- a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
+++ b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
@@ -40,4 +40,5 @@
   android.hardware.wifi.NanDataPathSecurityConfig securityConfig;
   byte[] appInfo;
   byte[] serviceNameOutOfBand;
+  byte discoverySessionId;
 }
diff --git a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanStatusCode.aidl b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanStatusCode.aidl
index 40844cc..ec12eb0 100644
--- a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanStatusCode.aidl
+++ b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanStatusCode.aidl
@@ -49,5 +49,7 @@
   UNSUPPORTED_CONCURRENCY_NAN_DISABLED = 12,
   INVALID_PAIRING_ID = 13,
   INVALID_BOOTSTRAPPING_ID = 14,
-  INVALID_STATE = 15,
+  REDUNDANT_REQUEST = 15,
+  NOT_SUPPORTED = 16,
+  NO_CONNECTION = 17,
 }
diff --git a/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanSuspensionModeChangeInd.aidl b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanSuspensionModeChangeInd.aidl
new file mode 100644
index 0000000..557fc79
--- /dev/null
+++ b/wifi/aidl/aidl_api/android.hardware.wifi/current/android/hardware/wifi/NanSuspensionModeChangeInd.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.wifi;
+@VintfStability
+parcelable NanSuspensionModeChangeInd {
+  boolean isSuspended;
+}
diff --git a/wifi/aidl/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl b/wifi/aidl/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
index edca1d5..8a61a9c 100644
--- a/wifi/aidl/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
+++ b/wifi/aidl/android/hardware/wifi/IWifiNanIfaceEventCallback.aidl
@@ -28,7 +28,7 @@
 import android.hardware.wifi.NanPairingConfirmInd;
 import android.hardware.wifi.NanPairingRequestInd;
 import android.hardware.wifi.NanStatus;
-
+import android.hardware.wifi.NanSuspensionModeChangeInd;
 /**
  * NAN Response and Asynchronous Event Callbacks.
  *
@@ -141,6 +141,13 @@
     void eventTransmitFollowup(in char id, in NanStatus status);
 
     /**
+     * Callback indicating that device suspension mode status change
+     *
+     * @param event NanSuspensionModeChangeInd containing event details.
+     */
+    void eventSuspensionModeChanged(in NanSuspensionModeChangeInd event);
+
+    /**
      * Callback invoked in response to a capability request
      * |IWifiNanIface.getCapabilitiesRequest|.
      *
diff --git a/wifi/aidl/android/hardware/wifi/NanInitiateDataPathRequest.aidl b/wifi/aidl/android/hardware/wifi/NanInitiateDataPathRequest.aidl
index 264a873..2f25329 100644
--- a/wifi/aidl/android/hardware/wifi/NanInitiateDataPathRequest.aidl
+++ b/wifi/aidl/android/hardware/wifi/NanInitiateDataPathRequest.aidl
@@ -69,4 +69,9 @@
      * NAN Spec: Appendix: Mapping pass-phrase to PMK for NCS-SK Cipher Suites
      */
     byte[] serviceNameOutOfBand;
+    /**
+     * ID of an active publish or subscribe discovery session.
+     * NAN Spec: Service Descriptor Attribute (SDA) / Instance ID
+     */
+    byte discoverySessionId;
 }
diff --git a/wifi/aidl/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl b/wifi/aidl/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
index 6960e71..e543b7b 100644
--- a/wifi/aidl/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
+++ b/wifi/aidl/android/hardware/wifi/NanRespondToDataPathIndicationRequest.aidl
@@ -58,4 +58,9 @@
      * NAN Spec: Appendix: Mapping pass-phrase to PMK for NCS-SK Cipher Suites
      */
     byte[] serviceNameOutOfBand;
+    /**
+     * ID of an active publish or subscribe discovery session.
+     * NAN Spec: Service Descriptor Attribute (SDA) / Instance ID
+     */
+    byte discoverySessionId;
 }
diff --git a/wifi/aidl/android/hardware/wifi/NanStatusCode.aidl b/wifi/aidl/android/hardware/wifi/NanStatusCode.aidl
index d609797..6706839 100644
--- a/wifi/aidl/android/hardware/wifi/NanStatusCode.aidl
+++ b/wifi/aidl/android/hardware/wifi/NanStatusCode.aidl
@@ -79,8 +79,16 @@
      * If the bootstrapping id is invalid
      */
     INVALID_BOOTSTRAPPING_ID = 14,
-    /**
-     * If the system is not in a valid state for the given request.
+    /*
+     * If same request is received again
      */
-    INVALID_STATE = 15
+    REDUNDANT_REQUEST = 15,
+    /*
+     * If current request is not supported
+     */
+    NOT_SUPPORTED = 16,
+    /*
+     * If no Wifi Aware connection is active
+     */
+    NO_CONNECTION = 17
 }
diff --git a/wifi/aidl/android/hardware/wifi/NanSuspensionModeChangeInd.aidl b/wifi/aidl/android/hardware/wifi/NanSuspensionModeChangeInd.aidl
new file mode 100644
index 0000000..afabe8c
--- /dev/null
+++ b/wifi/aidl/android/hardware/wifi/NanSuspensionModeChangeInd.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.wifi;
+
+/**
+ * Event indication that the device entered or exited the suspension mode
+ */
+@VintfStability
+parcelable NanSuspensionModeChangeInd {
+    /**
+     * Indication whether the device has entered or existed the NAN suspension mode(deep sleep)
+     */
+    boolean isSuspended;
+}
diff --git a/wifi/aidl/default/aidl_struct_util.cpp b/wifi/aidl/default/aidl_struct_util.cpp
index 2d745dc..876ff75 100644
--- a/wifi/aidl/default/aidl_struct_util.cpp
+++ b/wifi/aidl/default/aidl_struct_util.cpp
@@ -1420,6 +1420,12 @@
             return NanStatusCode::INVALID_PAIRING_ID;
         case legacy_hal::NAN_STATUS_INVALID_BOOTSTRAPPING_ID:
             return NanStatusCode::INVALID_BOOTSTRAPPING_ID;
+        case legacy_hal::NAN_STATUS_REDUNDANT_REQUEST:
+            return NanStatusCode::REDUNDANT_REQUEST;
+        case legacy_hal::NAN_STATUS_NOT_SUPPORTED:
+            return NanStatusCode::NOT_SUPPORTED;
+        case legacy_hal::NAN_STATUS_NO_CONNECTION:
+            return NanStatusCode::NO_CONNECTION;
     }
     CHECK(false);
 }
@@ -2090,6 +2096,7 @@
         return false;
     }
     memcpy(legacy_request->scid, aidl_request.securityConfig.scid.data(), legacy_request->scid_len);
+    legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
 
     return true;
 }
@@ -2171,6 +2178,7 @@
         return false;
     }
     memcpy(legacy_request->scid, aidl_request.securityConfig.scid.data(), legacy_request->scid_len);
+    legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
 
     return true;
 }
diff --git a/wifi/aidl/default/tests/wifi_nan_iface_unit_tests.cpp b/wifi/aidl/default/tests/wifi_nan_iface_unit_tests.cpp
index fc58c3e..d58a9b0 100644
--- a/wifi/aidl/default/tests/wifi_nan_iface_unit_tests.cpp
+++ b/wifi/aidl/default/tests/wifi_nan_iface_unit_tests.cpp
@@ -137,6 +137,7 @@
     MOCK_METHOD2(notifySuspendResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
     MOCK_METHOD2(notifyResumeResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
     MOCK_METHOD2(notifyTerminatePairingResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
+    MOCK_METHOD1(eventSuspensionModeChanged, ndk::ScopedAStatus(const NanSuspensionModeChangeInd&));
 };
 
 class WifiNanIfaceTest : public Test {
diff --git a/wifi/aidl/default/wifi_legacy_hal.cpp b/wifi/aidl/default/wifi_legacy_hal.cpp
index 6436fb7..796098d 100644
--- a/wifi/aidl/default/wifi_legacy_hal.cpp
+++ b/wifi/aidl/default/wifi_legacy_hal.cpp
@@ -351,6 +351,15 @@
     }
 }
 
+std::function<void(const NanSuspensionModeChangeInd&)>
+        on_nan_event_suspension_mode_change_user_callback;
+void onAsyncNanEventSuspensionModeChange(NanSuspensionModeChangeInd* event) {
+    const auto lock = aidl_sync_util::acquireGlobalLock();
+    if (on_nan_event_suspension_mode_change_user_callback && event) {
+        on_nan_event_suspension_mode_change_user_callback(*event);
+    }
+}
+
 std::function<void(const NanPairingRequestInd&)> on_nan_event_pairing_request_user_callback;
 void onAsyncNanEventPairingRequest(NanPairingRequestInd* event) {
     const auto lock = aidl_sync_util::acquireGlobalLock();
@@ -1376,6 +1385,8 @@
     on_nan_event_range_request_user_callback = user_callbacks.on_event_range_request;
     on_nan_event_range_report_user_callback = user_callbacks.on_event_range_report;
     on_nan_event_schedule_update_user_callback = user_callbacks.on_event_schedule_update;
+    on_nan_event_suspension_mode_change_user_callback =
+            user_callbacks.on_event_suspension_mode_change;
 
     return global_func_table_.wifi_nan_register_handler(getIfaceHandle(iface_name),
                                                         {onAsyncNanNotifyResponse,
@@ -1399,7 +1410,8 @@
                                                          onAsyncNanEventPairingRequest,
                                                          onAsyncNanEventPairingConfirm,
                                                          onAsyncNanEventBootstrappingRequest,
-                                                         onAsyncNanEventBootstrappingConfirm});
+                                                         onAsyncNanEventBootstrappingConfirm,
+                                                         onAsyncNanEventSuspensionModeChange});
 }
 
 wifi_error WifiLegacyHal::nanEnableRequest(const std::string& iface_name, transaction_id id,
diff --git a/wifi/aidl/default/wifi_legacy_hal.h b/wifi/aidl/default/wifi_legacy_hal.h
index 468d8d7..85a8ac4 100644
--- a/wifi/aidl/default/wifi_legacy_hal.h
+++ b/wifi/aidl/default/wifi_legacy_hal.h
@@ -91,6 +91,7 @@
 using ::NAN_RESPONSE_SUBSCRIBE_CANCEL;
 using ::NAN_RESPONSE_TCA;
 using ::NAN_RESPONSE_TRANSMIT_FOLLOWUP;
+using ::NAN_RESUME_REQUEST_RESPONSE;
 using ::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
 using ::NAN_SECURITY_KEY_INPUT_PMK;
 using ::NAN_SERVICE_ACCEPT_POLICY_ALL;
@@ -111,13 +112,17 @@
 using ::NAN_STATUS_INVALID_PUBLISH_SUBSCRIBE_ID;
 using ::NAN_STATUS_INVALID_REQUESTOR_INSTANCE_ID;
 using ::NAN_STATUS_NAN_NOT_ALLOWED;
+using ::NAN_STATUS_NO_CONNECTION;
 using ::NAN_STATUS_NO_OTA_ACK;
 using ::NAN_STATUS_NO_RESOURCE_AVAILABLE;
+using ::NAN_STATUS_NOT_SUPPORTED;
 using ::NAN_STATUS_PROTOCOL_FAILURE;
+using ::NAN_STATUS_REDUNDANT_REQUEST;
 using ::NAN_STATUS_SUCCESS;
 using ::NAN_STATUS_UNSUPPORTED_CONCURRENCY_NAN_DISABLED;
 using ::NAN_SUBSCRIBE_TYPE_ACTIVE;
 using ::NAN_SUBSCRIBE_TYPE_PASSIVE;
+using ::NAN_SUSPEND_REQUEST_RESPONSE;
 using ::NAN_TRANSMIT_IN_DW;
 using ::NAN_TRANSMIT_IN_FAW;
 using ::NAN_TX_PRIORITY_HIGH;
@@ -175,6 +180,7 @@
 using ::NanSubscribeTerminatedInd;
 using ::NanSubscribeType;
 using ::NanSuspendRequest;
+using ::NanSuspensionModeChangeInd;
 using ::NanTransmitFollowupInd;
 using ::NanTransmitFollowupRequest;
 using ::NanTxType;
@@ -452,6 +458,7 @@
     std::function<void(const NanPairingConfirmInd&)> on_event_pairing_confirm;
     std::function<void(const NanBootstrappingRequestInd&)> on_event_bootstrapping_request;
     std::function<void(const NanBootstrappingConfirmInd&)> on_event_bootstrapping_confirm;
+    std::function<void(const NanSuspensionModeChangeInd&)> on_event_suspension_mode_change;
 };
 
 // Full scan results contain IE info and are hence passed by reference, to
diff --git a/wifi/aidl/default/wifi_nan_iface.cpp b/wifi/aidl/default/wifi_nan_iface.cpp
index 0d53803..8e3a191 100644
--- a/wifi/aidl/default/wifi_nan_iface.cpp
+++ b/wifi/aidl/default/wifi_nan_iface.cpp
@@ -253,6 +253,22 @@
                 }
                 break;
             }
+            case legacy_hal::NAN_SUSPEND_REQUEST_RESPONSE: {
+                for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
+                    if (!callback->notifySuspendResponse(id, nanStatus).isOk()) {
+                        LOG(ERROR) << "Failed to invoke the callback";
+                    }
+                }
+                break;
+            }
+            case legacy_hal::NAN_RESUME_REQUEST_RESPONSE: {
+                for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
+                    if (!callback->notifyResumeResponse(id, nanStatus).isOk()) {
+                        LOG(ERROR) << "Failed to invoke the callback";
+                    }
+                }
+                break;
+            }
             case legacy_hal::NAN_RESPONSE_BEACON_SDF_PAYLOAD:
             /* fall through */
             case legacy_hal::NAN_RESPONSE_TCA:
@@ -581,6 +597,22 @@
                     }
                 }
             };
+    callback_handlers.on_event_suspension_mode_change =
+            [weak_ptr_this](const legacy_hal::NanSuspensionModeChangeInd& msg) {
+                const auto shared_ptr_this = weak_ptr_this.lock();
+                if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
+                    LOG(ERROR) << "Callback invoked on an invalid object";
+                    return;
+                }
+                NanSuspensionModeChangeInd aidl_struct;
+                aidl_struct.isSuspended = msg.is_suspended;
+
+                for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
+                    if (!callback->eventSuspensionModeChanged(aidl_struct).isOk()) {
+                        LOG(ERROR) << "Failed to invoke the callback";
+                    }
+                }
+            };
 
     legacy_hal::wifi_error legacy_status =
             legacy_hal_.lock()->nanRegisterCallbackHandlers(ifname_, callback_handlers);
diff --git a/wifi/aidl/vts/functional/wifi_nan_iface_aidl_test.cpp b/wifi/aidl/vts/functional/wifi_nan_iface_aidl_test.cpp
index d2c3235..bebad7c 100644
--- a/wifi/aidl/vts/functional/wifi_nan_iface_aidl_test.cpp
+++ b/wifi/aidl/vts/functional/wifi_nan_iface_aidl_test.cpp
@@ -55,6 +55,7 @@
 using aidl::android::hardware::wifi::NanRespondToDataPathIndicationRequest;
 using aidl::android::hardware::wifi::NanStatus;
 using aidl::android::hardware::wifi::NanStatusCode;
+using aidl::android::hardware::wifi::NanSuspensionModeChangeInd;
 using aidl::android::hardware::wifi::NanTxType;
 
 #define TIMEOUT_PERIOD 10
@@ -124,6 +125,7 @@
         EVENT_PAIRING_CONFIRM,
         EVENT_BOOTSTRAPPING_REQUEST,
         EVENT_BOOTSTRAPPING_CONFIRM,
+        EVENT_SUSPENSION_MODE_CHANGE,
     };
 
     // Test code calls this function to wait for data/event callback.
@@ -255,6 +257,13 @@
             parent_.notify();
             return ndk::ScopedAStatus::ok();
         }
+        ::ndk::ScopedAStatus eventSuspensionModeChanged(
+                const NanSuspensionModeChangeInd& event) override {
+            parent_.callback_type_ = EVENT_SUSPENSION_MODE_CHANGE;
+            parent_.nan_suspension_mode_change_ind_ = event;
+            parent_.notify();
+            return ndk::ScopedAStatus::ok();
+        }
         ::ndk::ScopedAStatus notifyCapabilitiesResponse(
                 char16_t id, const NanStatus& status,
                 const NanCapabilities& capabilities) override {
@@ -451,6 +460,7 @@
     NanPairingConfirmInd nan_pairing_confirm_ind_;
     NanBootstrappingRequestInd nan_bootstrapping_request_ind_;
     NanBootstrappingConfirmInd nan_bootstrapping_confirm_ind_;
+    NanSuspensionModeChangeInd nan_suspension_mode_change_ind_;
 
     const char* getInstanceName() { return GetParam().c_str(); }