xref: /aosp_15_r20/system/chre/pal/tests/include/wifi_pal_impl_test.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef WIFI_PAL_IMPL_TEST_H_
18 #define WIFI_PAL_IMPL_TEST_H_
19 
20 #include "chre/pal/wifi.h"
21 #include "chre/platform/condition_variable.h"
22 #include "chre/platform/mutex.h"
23 #include "chre/util/dynamic_vector.h"
24 #include "chre/util/optional.h"
25 #include "chre/util/time.h"
26 #include "gtest/gtest.h"
27 
28 namespace wifi_pal_impl_test {
29 
30 class PalWifiTest : public ::testing::Test {
31  public:
32   /**
33    * Implements CHRE PAL API callbacks
34    */
35   void scanMonitorStatusChangeCallback(bool enabled, uint8_t errorCode);
36   void scanResponseCallback(bool pending, uint8_t errorCode);
37   void scanEventCallback(struct chreWifiScanEvent *event);
38   void rangingEventCallback(uint8_t errorCode,
39                             struct chreWifiRangingEvent *event);
40   void nanServiceIdentifierCallback(uint8_t errorCode, uint32_t subscriptionId);
41   void nanServiceDiscoveryCallback(struct chreWifiNanDiscoveryEvent *event);
42   void nanServiceLostCallback(uint32_t subscriptionId, uint32_t publisherId);
43   void nanServiceTerminatedCallback(uint32_t reason, uint32_t subscriptionId);
44   void nanSubscriptionCanceledCallback(uint8_t errorCode,
45                                        uint32_t subscriptionId);
46 
47  protected:
48   void SetUp() override;
49 
50   void TearDown() override;
51 
52   /**
53    * Validates an incoming WiFi scan event.
54    *
55    * @param event The WiFi scan event.
56    */
57   void validateWifiScanEvent(const chreWifiScanEvent &event);
58 
59   /**
60    * Prepares for a subsequent PAL API call that expects an async response.
61    */
prepareForAsyncResponse()62   void prepareForAsyncResponse() {
63     errorCode_ = CHRE_ERROR_LAST;
64   }
65 
66   /**
67    * Waits for an async response by the PAL implementation (e.g. via scan
68    * response/monitor status change callback), and asserts that a success
69    * error code was received.
70    */
71   void waitForAsyncResponseAssertSuccess(chre::Nanoseconds timeoutNs);
72 
73   //! The pointer to the CHRE PAL implementation API
74   const struct chrePalWifiApi *api_;
75 
76   //! The error code of the most recent callback
77   uint8_t errorCode_ = CHRE_ERROR_LAST;
78 
79   //! The number of scan events currently stored
80   uint32_t numScanResultCount_ = 0;
81 
82   //! True if the last scan event has been received
83   bool lastScanEventReceived_ = false;
84 
85   //! A list to store the scan results
86   chre::DynamicVector<chreWifiScanEvent *> scanEventList_;
87 
88   //! Stores active scan params
89   chre::Optional<chreWifiScanParams> scanParams_;
90 
91   //! The last scan event index received, UINT8_MAX if invalid
92   uint8_t lastEventIndex_;
93 
94   //! True if scan monitoring is currently enabled
95   bool scanMonitorEnabled_ = false;
96 
97   //! The last received service subscription and publisher IDs from the NAN
98   //! engine.
99   chre::Optional<uint32_t> subscriptionId_;
100   chre::Optional<uint32_t> publishId_;
101 
102   //! Mutex to protect class variables
103   chre::Mutex mutex_;
104   chre::ConditionVariable condVar_;
105 };
106 
107 }  // namespace wifi_pal_impl_test
108 
109 #endif  // WIFI_PAL_IMPL_TEST_H_
110