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 #include "wifi_mode_controller.h" 18 19 #include <android-base/logging.h> 20 #include <android-base/macros.h> 21 #include <private/android_filesystem_config.h> 22 23 namespace { 24 using aidl::android::hardware::wifi::IfaceType; 25 using android::wifi_hal::DriverTool; 26 convertIfaceTypeToFirmwareMode(IfaceType type)27int convertIfaceTypeToFirmwareMode(IfaceType type) { 28 int mode; 29 switch (type) { 30 case IfaceType::AP: 31 mode = DriverTool::kFirmwareModeAp; 32 break; 33 case IfaceType::P2P: 34 mode = DriverTool::kFirmwareModeP2p; 35 break; 36 case IfaceType::NAN_IFACE: 37 // NAN is exposed in STA mode currently. 38 mode = DriverTool::kFirmwareModeSta; 39 break; 40 case IfaceType::STA: 41 mode = DriverTool::kFirmwareModeSta; 42 break; 43 } 44 return mode; 45 } 46 } // namespace 47 48 namespace aidl { 49 namespace android { 50 namespace hardware { 51 namespace wifi { 52 namespace mode_controller { 53 WifiModeController()54WifiModeController::WifiModeController() : driver_tool_(new DriverTool) {} 55 isFirmwareModeChangeNeeded(IfaceType type)56bool WifiModeController::isFirmwareModeChangeNeeded(IfaceType type) { 57 return driver_tool_->IsFirmwareModeChangeNeeded(convertIfaceTypeToFirmwareMode(type)); 58 } 59 initialize()60bool WifiModeController::initialize() { 61 if (!driver_tool_->LoadDriver()) { 62 LOG(ERROR) << "Failed to load WiFi driver"; 63 return false; 64 } 65 return true; 66 } 67 changeFirmwareMode(IfaceType type)68bool WifiModeController::changeFirmwareMode(IfaceType type) { 69 if (!driver_tool_->ChangeFirmwareMode(convertIfaceTypeToFirmwareMode(type))) { 70 LOG(ERROR) << "Failed to change firmware mode"; 71 return false; 72 } 73 return true; 74 } 75 deinitialize()76bool WifiModeController::deinitialize() { 77 if (!driver_tool_->UnloadDriver()) { 78 LOG(ERROR) << "Failed to unload WiFi driver"; 79 return false; 80 } 81 return true; 82 } 83 84 } // namespace mode_controller 85 } // namespace wifi 86 } // namespace hardware 87 } // namespace android 88 } // namespace aidl 89