1 /* 2 * Copyright (C) 2022 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 // Undefine the NAN macro (similar to how it's done in the wifi utils library) 18 // to avoid symbol clashes between the NAN (Not-A-Number) macro in the bionic 19 // library headers, and the NAN (Neighbor-Aware-Networking) enum value in the 20 // WiFi ext interface. 21 #ifdef NAN 22 #undef NAN 23 #endif 24 25 #include <condition_variable> 26 #include <cstdint> 27 #include <mutex> 28 #include <string> 29 #include <thread> 30 31 #include <aidl/vendor/google/wifi_ext/BnWifiExtChreCallback.h> 32 #include <aidl/vendor/google/wifi_ext/IWifiExt.h> 33 #include <android/binder_manager.h> 34 35 #include "chre_host/log.h" 36 37 namespace android { 38 namespace chre { 39 40 /** 41 * Handles interactions with the Wifi Ext HAL, to issue configuration 42 * requests to enable or disable NAN (Neighbor-Aware Networking) functionality. 43 */ 44 class WifiExtHalHandler { 45 public: 46 using IWifiExt = aidl::vendor::google::wifi_ext::IWifiExt; 47 using BnWifiExtChreNanCallback = 48 aidl::vendor::google::wifi_ext::BnWifiExtChreCallback; 49 using WifiChreNanRttState = 50 aidl::vendor::google::wifi_ext::WifiChreNanRttState; 51 52 ~WifiExtHalHandler(); 53 54 /** 55 * Construct a new Wifi Ext Hal Handler object, initiate a connection to 56 * the Wifi ext HAL service. 57 * 58 * @param statusChangeCallback Callback set by the daemon to be invoked on a 59 * status change to NAN's enablement. 60 */ 61 WifiExtHalHandler(const std::function<void(bool)> &statusChangeCallback); 62 63 /** 64 * Invoked by the CHRE daemon when it receives a request to enable or disable 65 * NAN from CHRE. 66 * 67 * @param enable true if CHRE is requesting NAN to be enabled, false if the 68 * request is for a disable. 69 */ 70 void handleConfigurationRequest(bool enable); 71 72 private: 73 //! CHRE NAN availability status change handler. 74 class WifiExtCallback : public BnWifiExtChreNanCallback { 75 public: WifiExtCallback(std::function<void (bool)> cb)76 WifiExtCallback(std::function<void(bool)> cb) : mCallback(cb) {} 77 onChreNanRttStateChanged(WifiChreNanRttState state)78 ndk::ScopedAStatus onChreNanRttStateChanged(WifiChreNanRttState state) { 79 bool enabled = (state == WifiChreNanRttState::CHRE_AVAILABLE); 80 onStatusChanged(enabled); 81 return ndk::ScopedAStatus::ok(); 82 } 83 onStatusChanged(bool enabled)84 void onStatusChanged(bool enabled) { 85 mCallback(enabled); 86 } 87 88 private: 89 std::function<void(bool)> mCallback; 90 }; 91 92 bool mThreadRunning = true; 93 std::thread mThread; 94 std::mutex mMutex; 95 std::condition_variable mCondVar; 96 97 //! Flag used to indicate the state of the configuration request ('enable' if 98 //! true, 'disable' otherwise) if it has a value. 99 std::optional<bool> mEnableConfig; 100 101 AIBinder_DeathRecipient *mDeathRecipient; 102 std::shared_ptr<IWifiExt> mService; 103 std::shared_ptr<WifiExtCallback> mCallback; 104 105 /** 106 * Entry point for the thread that handles all interactions with the WiFi ext 107 * HAL. This is required since a connection initiation can potentially block 108 * indefinitely. 109 */ 110 void wifiExtHandlerThreadEntry(); 111 112 /** 113 * Notifies the WifiExtHalHandler processing thread of a daemon shutdown. 114 */ 115 void notifyThreadToExit(); 116 117 /** 118 * Checks for a valid connection to the Wifi ext HAL service, reconnects if 119 * not already connected. 120 * 121 * @return true if connected or upon successful reconnection, false 122 * otherwise. 123 */ 124 bool checkWifiExtHalConnected(); 125 126 /** 127 * Invoked by the HAL service death callback. 128 */ 129 static void onWifiExtHalServiceDeath(void *cookie); 130 131 /** 132 * Dispatch a configuration request to the WiFi Ext HAL. 133 * 134 * @param enable true if the request is to enable NAN, false if 135 * to disable. 136 */ 137 void dispatchConfigurationRequest(bool enable); 138 }; 139 140 } // namespace chre 141 } // namespace android 142