platform_wifi.cc revision ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464
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
25namespace chre {
26
27PlatformWifi::~PlatformWifi() {
28  if (mWifiApi != nullptr) {
29    LOGD("Platform WiFi closing");
30    prePalApiCall();
31    mWifiApi->close();
32    LOGD("Platform WiFi closed");
33  }
34}
35
36void PlatformWifi::init() {
37  prePalApiCall();
38  mWifiApi = chrePalWifiGetApi(CHRE_PAL_WIFI_API_CURRENT_VERSION);
39  if (mWifiApi != nullptr) {
40    mWifiCallbacks.scanMonitorStatusChangeCallback =
41        PlatformWifi::scanMonitorStatusChangeCallback;
42    mWifiCallbacks.scanResponseCallback =
43        PlatformWifiBase::scanResponseCallback;
44    mWifiCallbacks.scanEventCallback =
45        PlatformWifiBase::scanEventCallback;
46    if (!mWifiApi->open(&gChrePalSystemApi, &mWifiCallbacks)) {
47      LOGE("WiFi PAL open returned false");
48      mWifiApi = nullptr;
49    }
50  } else {
51    LOGW("Requested Wifi PAL (version %08" PRIx32 ") not found",
52         CHRE_PAL_WIFI_API_CURRENT_VERSION);
53  }
54}
55
56uint32_t PlatformWifi::getCapabilities() {
57  if (mWifiApi != nullptr) {
58    prePalApiCall();
59    return mWifiApi->getCapabilities();
60  } else {
61    return CHRE_WIFI_CAPABILITIES_NONE;
62  }
63}
64
65bool PlatformWifi::configureScanMonitor(bool enable) {
66  if (mWifiApi != nullptr) {
67    prePalApiCall();
68    return mWifiApi->configureScanMonitor(enable);
69  } else {
70    return false;
71  }
72}
73
74bool PlatformWifi::requestScan(const struct chreWifiScanParams *params) {
75  if (mWifiApi != nullptr) {
76    prePalApiCall();
77    return mWifiApi->requestScan(params);
78  } else {
79    return false;
80  }
81}
82
83void PlatformWifi::releaseScanEvent(struct chreWifiScanEvent *event) {
84  if (mWifiApi != nullptr) {
85    prePalApiCall();
86    mWifiApi->releaseScanEvent(event);
87  }
88}
89
90void PlatformWifiBase::scanMonitorStatusChangeCallback(bool enabled,
91                                                       uint8_t errorCode) {
92  EventLoopManagerSingleton::get()->getWifiRequestManager()
93      .handleScanMonitorStateChange(enabled, errorCode);
94}
95
96void PlatformWifiBase::scanResponseCallback(bool pending, uint8_t errorCode) {
97  EventLoopManagerSingleton::get()->getWifiRequestManager()
98      .handleScanResponse(pending, errorCode);
99}
100
101void PlatformWifiBase::scanEventCallback(struct chreWifiScanEvent *event) {
102  EventLoopManagerSingleton::get()->getWifiRequestManager()
103      .handleScanEvent(event);
104}
105
106}  // namespace chre
107