1 /*
2  * Copyright (C) 2017 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 #include "chre/platform/platform_wifi.h"
18 
19 #include <cinttypes>
20 
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/shared/pal_system_api.h"
23 #include "chre/platform/log.h"
24 
25 namespace chre {
26 
27 const chrePalWifiCallbacks PlatformWifiBase::sWifiCallbacks = {
28   PlatformWifi::scanMonitorStatusChangeCallback,
29   PlatformWifiBase::scanResponseCallback,
30   PlatformWifiBase::scanEventCallback,
31   PlatformWifiBase::rangingEventCallback,
32 };
33 
~PlatformWifi()34 PlatformWifi::~PlatformWifi() {
35   if (mWifiApi != nullptr) {
36     LOGD("Platform WiFi closing");
37     prePalApiCall();
38     mWifiApi->close();
39     LOGD("Platform WiFi closed");
40   }
41 }
42 
init()43 void PlatformWifi::init() {
44   prePalApiCall();
45   mWifiApi = chrePalWifiGetApi(CHRE_PAL_WIFI_API_CURRENT_VERSION);
46   if (mWifiApi != nullptr) {
47     if (!mWifiApi->open(&gChrePalSystemApi, &sWifiCallbacks)) {
48       LOGE("WiFi PAL open returned false");
49       mWifiApi = nullptr;
50     } else {
51       LOGD("Opened WiFi PAL version 0x%08" PRIx32, mWifiApi->moduleVersion);
52     }
53   } else {
54     LOGW("Requested Wifi PAL (version 0x%08" PRIx32 ") not found",
55          CHRE_PAL_WIFI_API_CURRENT_VERSION);
56   }
57 }
58 
getCapabilities()59 uint32_t PlatformWifi::getCapabilities() {
60   if (mWifiApi != nullptr) {
61     prePalApiCall();
62     return mWifiApi->getCapabilities();
63   } else {
64     return CHRE_WIFI_CAPABILITIES_NONE;
65   }
66 }
67 
configureScanMonitor(bool enable)68 bool PlatformWifi::configureScanMonitor(bool enable) {
69   if (mWifiApi != nullptr) {
70     prePalApiCall();
71     return mWifiApi->configureScanMonitor(enable);
72   } else {
73     return false;
74   }
75 }
76 
requestRanging(const struct chreWifiRangingParams * params)77 bool PlatformWifi::requestRanging(const struct chreWifiRangingParams *params) {
78   if (mWifiApi != nullptr
79       && mWifiApi->moduleVersion >= CHRE_PAL_WIFI_API_V1_2) {
80     prePalApiCall();
81     return mWifiApi->requestRanging(params);
82   } else {
83     return false;
84   }
85 }
86 
requestScan(const struct chreWifiScanParams * params)87 bool PlatformWifi::requestScan(const struct chreWifiScanParams *params) {
88   if (mWifiApi != nullptr) {
89     prePalApiCall();
90     return mWifiApi->requestScan(params);
91   } else {
92     return false;
93   }
94 }
95 
releaseRangingEvent(struct chreWifiRangingEvent * event)96 void PlatformWifi::releaseRangingEvent(struct chreWifiRangingEvent *event) {
97   prePalApiCall();
98   mWifiApi->releaseRangingEvent(event);
99 }
100 
releaseScanEvent(struct chreWifiScanEvent * event)101 void PlatformWifi::releaseScanEvent(struct chreWifiScanEvent *event) {
102   prePalApiCall();
103   mWifiApi->releaseScanEvent(event);
104 }
105 
rangingEventCallback(uint8_t errorCode,struct chreWifiRangingEvent * event)106 void PlatformWifiBase::rangingEventCallback(
107     uint8_t errorCode, struct chreWifiRangingEvent *event) {
108   EventLoopManagerSingleton::get()->getWifiRequestManager()
109       .handleRangingEvent(errorCode, event);
110 }
111 
scanMonitorStatusChangeCallback(bool enabled,uint8_t errorCode)112 void PlatformWifiBase::scanMonitorStatusChangeCallback(bool enabled,
113                                                        uint8_t errorCode) {
114   EventLoopManagerSingleton::get()->getWifiRequestManager()
115       .handleScanMonitorStateChange(enabled, errorCode);
116 }
117 
scanResponseCallback(bool pending,uint8_t errorCode)118 void PlatformWifiBase::scanResponseCallback(bool pending, uint8_t errorCode) {
119   EventLoopManagerSingleton::get()->getWifiRequestManager()
120       .handleScanResponse(pending, errorCode);
121 }
122 
scanEventCallback(struct chreWifiScanEvent * event)123 void PlatformWifiBase::scanEventCallback(struct chreWifiScanEvent *event) {
124   EventLoopManagerSingleton::get()->getWifiRequestManager()
125       .handleScanEvent(event);
126 }
127 
128 }  // namespace chre
129