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 
41  protected:
42   void SetUp() override;
43 
44   void TearDown() override;
45 
46   /**
47    * Validates an incoming WiFi scan event.
48    *
49    * @param event The WiFi scan event.
50    */
51   void validateWifiScanEvent(const chreWifiScanEvent &event);
52 
53   /**
54    * Prepares for a subsequent PAL API call that expects an async response.
55    */
prepareForAsyncResponse()56   void prepareForAsyncResponse() {
57     errorCode_ = CHRE_ERROR_LAST;
58   }
59 
60   /**
61    * Waits for an async response by the PAL implementation (e.g. via scan
62    * response/monitor status change callback), and asserts that a success
63    * error code was received.
64    */
65   void waitForAsyncResponseAssertSuccess(chre::Nanoseconds timeoutNs);
66 
67   //! The pointer to the CHRE PAL implementation API
68   const struct chrePalWifiApi *api_;
69 
70   //! The error code of the most recent callback
71   uint8_t errorCode_ = CHRE_ERROR_LAST;
72 
73   //! The number of scan events currently stored
74   uint32_t numScanResultCount_ = 0;
75 
76   //! True if the last scan event has been received
77   bool lastScanEventReceived_ = false;
78 
79   //! A list to store the scan results
80   chre::DynamicVector<chreWifiScanEvent *> scanEventList_;
81 
82   //! Stores active scan params
83   chre::Optional<chreWifiScanParams> scanParams_;
84 
85   //! The last scan event index received, UINT8_MAX if invalid
86   uint8_t lastEventIndex_;
87 
88   //! True if scan monitoring is currently enabled
89   bool scanMonitorEnabled_ = false;
90 
91   //! Mutex to protect class variables
92   chre::Mutex mutex_;
93   chre::ConditionVariable condVar_;
94 };
95 
96 }  // namespace wifi_pal_impl_test
97 
98 #endif  // WIFI_PAL_IMPL_TEST_H_
99