Home | History | Annotate | Download | only in shared
      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 
     25 namespace chre {
     26 
     27 PlatformWifi::~PlatformWifi() {
     28   if (mWifiApi != nullptr) {
     29     LOGD("Platform WiFi closing");
     30     prePalApiCall();
     31     mWifiApi->close();
     32     LOGD("Platform WiFi closed");
     33   }
     34 }
     35 
     36 void 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     mWifiCallbacks.rangingEventCallback =
     47         PlatformWifiBase::rangingEventCallback;
     48     if (!mWifiApi->open(&gChrePalSystemApi, &mWifiCallbacks)) {
     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 %08" PRIx32 ") not found",
     56          CHRE_PAL_WIFI_API_CURRENT_VERSION);
     57   }
     58 }
     59 
     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 
     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 
     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 
     88 bool PlatformWifi::requestScan(const struct chreWifiScanParams *params) {
     89   if (mWifiApi != nullptr) {
     90     prePalApiCall();
     91     return mWifiApi->requestScan(params);
     92   } else {
     93     return false;
     94   }
     95 }
     96 
     97 void PlatformWifi::releaseRangingEvent(struct chreWifiRangingEvent *event) {
     98   prePalApiCall();
     99   mWifiApi->releaseRangingEvent(event);
    100 }
    101 
    102 void PlatformWifi::releaseScanEvent(struct chreWifiScanEvent *event) {
    103   prePalApiCall();
    104   mWifiApi->releaseScanEvent(event);
    105 }
    106 
    107 void PlatformWifiBase::rangingEventCallback(
    108     uint8_t errorCode, struct chreWifiRangingEvent *event) {
    109   EventLoopManagerSingleton::get()->getWifiRequestManager()
    110       .handleRangingEvent(errorCode, event);
    111 }
    112 
    113 void PlatformWifiBase::scanMonitorStatusChangeCallback(bool enabled,
    114                                                        uint8_t errorCode) {
    115   EventLoopManagerSingleton::get()->getWifiRequestManager()
    116       .handleScanMonitorStateChange(enabled, errorCode);
    117 }
    118 
    119 void PlatformWifiBase::scanResponseCallback(bool pending, uint8_t errorCode) {
    120   EventLoopManagerSingleton::get()->getWifiRequestManager()
    121       .handleScanResponse(pending, errorCode);
    122 }
    123 
    124 void PlatformWifiBase::scanEventCallback(struct chreWifiScanEvent *event) {
    125   EventLoopManagerSingleton::get()->getWifiRequestManager()
    126       .handleScanEvent(event);
    127 }
    128 
    129 }  // namespace chre
    130