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