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