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