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 PlatformWifiBase::nanServiceIdentifierCallback,
34 PlatformWifiBase::nanServiceDiscoveryCallback,
35 PlatformWifiBase::nanServiceLostCallback,
36 PlatformWifiBase::nanServiceTerminatedCallback,
37 PlatformWifiBase::nanServiceSubscriptionCanceledCallback,
38 };
39
~PlatformWifi()40 PlatformWifi::~PlatformWifi() {
41 if (mWifiApi != nullptr) {
42 LOGD("Platform WiFi closing");
43 prePalApiCall(PalType::WIFI);
44 mWifiApi->close();
45 LOGD("Platform WiFi closed");
46 }
47 }
48
init()49 void PlatformWifi::init() {
50 prePalApiCall(PalType::WIFI);
51 mWifiApi = chrePalWifiGetApi(CHRE_PAL_WIFI_API_CURRENT_VERSION);
52 if (mWifiApi != nullptr) {
53 if (!mWifiApi->open(&gChrePalSystemApi, &sWifiCallbacks)) {
54 LOGE("WiFi PAL open returned false");
55
56 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
57 EventLoopManagerSingleton::get()->getTelemetryManager().onPalOpenFailure(
58 TelemetryManager::PalType::WIFI);
59 #endif // CHRE_TELEMETRY_SUPPORT_ENABLED
60
61 mWifiApi = nullptr;
62 } else {
63 LOGD("Opened WiFi PAL version 0x%08" PRIx32, mWifiApi->moduleVersion);
64 }
65 } else {
66 LOGW("Requested Wifi PAL (version 0x%08" PRIx32 ") not found",
67 CHRE_PAL_WIFI_API_CURRENT_VERSION);
68 }
69 }
70
getCapabilities()71 uint32_t PlatformWifi::getCapabilities() {
72 if (mWifiApi != nullptr) {
73 prePalApiCall(PalType::WIFI);
74 return mWifiApi->getCapabilities();
75 } else {
76 return CHRE_WIFI_CAPABILITIES_NONE;
77 }
78 }
79
configureScanMonitor(bool enable)80 bool PlatformWifi::configureScanMonitor(bool enable) {
81 if (mWifiApi != nullptr) {
82 prePalApiCall(PalType::WIFI);
83 return mWifiApi->configureScanMonitor(enable);
84 } else {
85 return false;
86 }
87 }
88
requestRanging(const struct chreWifiRangingParams * params)89 bool PlatformWifi::requestRanging(const struct chreWifiRangingParams *params) {
90 if (mWifiApi != nullptr &&
91 mWifiApi->moduleVersion >= CHRE_PAL_WIFI_API_V1_2) {
92 prePalApiCall(PalType::WIFI);
93 return mWifiApi->requestRanging(params);
94 } else {
95 return false;
96 }
97 }
98
requestNanRanging(const struct chreWifiNanRangingParams * params)99 bool PlatformWifi::requestNanRanging(
100 const struct chreWifiNanRangingParams *params) {
101 bool success = false;
102 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
103 if (mWifiApi != nullptr &&
104 mWifiApi->moduleVersion >= CHRE_PAL_WIFI_API_V1_6) {
105 prePalApiCall(PalType::WIFI);
106 success = mWifiApi->requestNanRanging(params);
107 }
108 #endif
109 return success;
110 }
111
requestScan(const struct chreWifiScanParams * params)112 bool PlatformWifi::requestScan(const struct chreWifiScanParams *params) {
113 if (mWifiApi != nullptr) {
114 prePalApiCall(PalType::WIFI);
115
116 if (mWifiApi->moduleVersion < CHRE_PAL_WIFI_API_V1_5) {
117 const struct chreWifiScanParams paramsCompat =
118 translateToLegacyWifiScanParams(params);
119 return mWifiApi->requestScan(¶msCompat);
120 } else {
121 return mWifiApi->requestScan(params);
122 }
123 } else {
124 return false;
125 }
126 }
127
releaseRangingEvent(struct chreWifiRangingEvent * event)128 void PlatformWifi::releaseRangingEvent(struct chreWifiRangingEvent *event) {
129 prePalApiCall(PalType::WIFI);
130 mWifiApi->releaseRangingEvent(event);
131 }
132
releaseScanEvent(struct chreWifiScanEvent * event)133 void PlatformWifi::releaseScanEvent(struct chreWifiScanEvent *event) {
134 prePalApiCall(PalType::WIFI);
135 mWifiApi->releaseScanEvent(event);
136 }
137
releaseNanDiscoveryEvent(struct chreWifiNanDiscoveryEvent * event)138 void PlatformWifi::releaseNanDiscoveryEvent(
139 struct chreWifiNanDiscoveryEvent *event) {
140 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
141 prePalApiCall(PalType::WIFI);
142 mWifiApi->releaseNanDiscoveryEvent(event);
143 #else
144 UNUSED_VAR(event);
145 #endif
146 }
147
nanSubscribe(const struct chreWifiNanSubscribeConfig * config)148 bool PlatformWifi::nanSubscribe(
149 const struct chreWifiNanSubscribeConfig *config) {
150 bool success = false;
151 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
152 if (mWifiApi != nullptr &&
153 mWifiApi->moduleVersion >= CHRE_PAL_WIFI_API_V1_6) {
154 prePalApiCall(PalType::WIFI);
155 success = mWifiApi->nanSubscribe(config);
156 }
157 #else
158 UNUSED_VAR(config);
159 #endif
160 return success;
161 }
162
nanSubscribeCancel(uint32_t subscriptionId)163 bool PlatformWifi::nanSubscribeCancel(uint32_t subscriptionId) {
164 bool success = false;
165 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
166 if (mWifiApi != nullptr &&
167 mWifiApi->moduleVersion >= CHRE_PAL_WIFI_API_V1_6) {
168 prePalApiCall(PalType::WIFI);
169 success = mWifiApi->nanSubscribeCancel(subscriptionId);
170 }
171 #else
172 UNUSED_VAR(subscriptionId);
173 #endif
174 return success;
175 }
176
rangingEventCallback(uint8_t errorCode,struct chreWifiRangingEvent * event)177 void PlatformWifiBase::rangingEventCallback(
178 uint8_t errorCode, struct chreWifiRangingEvent *event) {
179 EventLoopManagerSingleton::get()->getWifiRequestManager().handleRangingEvent(
180 errorCode, event);
181 }
182
scanMonitorStatusChangeCallback(bool enabled,uint8_t errorCode)183 void PlatformWifiBase::scanMonitorStatusChangeCallback(bool enabled,
184 uint8_t errorCode) {
185 EventLoopManagerSingleton::get()
186 ->getWifiRequestManager()
187 .handleScanMonitorStateChange(enabled, errorCode);
188 }
189
scanResponseCallback(bool pending,uint8_t errorCode)190 void PlatformWifiBase::scanResponseCallback(bool pending, uint8_t errorCode) {
191 EventLoopManagerSingleton::get()->getWifiRequestManager().handleScanResponse(
192 pending, errorCode);
193 }
194
scanEventCallback(struct chreWifiScanEvent * event)195 void PlatformWifiBase::scanEventCallback(struct chreWifiScanEvent *event) {
196 EventLoopManagerSingleton::get()->getWifiRequestManager().handleScanEvent(
197 event);
198 }
199
nanServiceIdentifierCallback(uint8_t errorCode,uint32_t subscriptionId)200 void PlatformWifiBase::nanServiceIdentifierCallback(uint8_t errorCode,
201 uint32_t subscriptionId) {
202 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
203 EventLoopManagerSingleton::get()
204 ->getWifiRequestManager()
205 .handleNanServiceIdentifierEvent(errorCode, subscriptionId);
206 #else
207 UNUSED_VAR(errorCode);
208 UNUSED_VAR(subscriptionId);
209 #endif
210 }
211
nanServiceDiscoveryCallback(struct chreWifiNanDiscoveryEvent * event)212 void PlatformWifiBase::nanServiceDiscoveryCallback(
213 struct chreWifiNanDiscoveryEvent *event) {
214 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
215 EventLoopManagerSingleton::get()
216 ->getWifiRequestManager()
217 .handleNanServiceDiscoveryEvent(event);
218 #else
219 UNUSED_VAR(event);
220 #endif
221 }
222
nanServiceLostCallback(uint32_t subscriptionId,uint32_t publisherId)223 void PlatformWifiBase::nanServiceLostCallback(uint32_t subscriptionId,
224 uint32_t publisherId) {
225 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
226 EventLoopManagerSingleton::get()
227 ->getWifiRequestManager()
228 .handleNanServiceLostEvent(subscriptionId, publisherId);
229 #else
230 UNUSED_VAR(subscriptionId);
231 UNUSED_VAR(publisherId);
232 #endif
233 }
234
nanServiceTerminatedCallback(uint32_t errorCode,uint32_t subscriptionId)235 void PlatformWifiBase::nanServiceTerminatedCallback(uint32_t errorCode,
236 uint32_t subscriptionId) {
237 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
238 EventLoopManagerSingleton::get()
239 ->getWifiRequestManager()
240 .handleNanServiceTerminatedEvent(errorCode, subscriptionId);
241 #else
242 UNUSED_VAR(errorCode);
243 UNUSED_VAR(subscriptionId);
244 #endif
245 }
246
nanServiceSubscriptionCanceledCallback(uint8_t errorCode,uint32_t subscriptionId)247 void PlatformWifiBase::nanServiceSubscriptionCanceledCallback(
248 uint8_t errorCode, uint32_t subscriptionId) {
249 #ifdef CHRE_WIFI_NAN_SUPPORT_ENABLED
250 EventLoopManagerSingleton::get()
251 ->getWifiRequestManager()
252 .handleNanServiceSubscriptionCanceledEvent(errorCode, subscriptionId);
253 #else
254 UNUSED_VAR(errorCode);
255 UNUSED_VAR(subscriptionId);
256 #endif
257 }
258
259 } // namespace chre
260