San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include <string.h> |
| 17 | #include <errno.h> |
| 18 | |
| 19 | #define LOG_TAG "WifiController" |
| 20 | #include <cutils/log.h> |
| 21 | |
| 22 | #include "Supplicant.h" |
| 23 | #include "WifiController.h" |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 24 | #include "WifiScanner.h" |
| 25 | #include "NetworkManager.h" |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 26 | #include "ErrorCode.h"; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 27 | |
| 28 | WifiController::WifiController(char *modpath, char *modname, char *modargs) : |
| 29 | Controller("WIFI") { |
| 30 | strncpy(mModulePath, modpath, sizeof(mModulePath)); |
| 31 | strncpy(mModuleName, modname, sizeof(mModuleName)); |
| 32 | strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); |
| 33 | |
| 34 | mSupplicant = new Supplicant(); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 35 | mScanner = new WifiScanner(mSupplicant, 10); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 36 | mCurrentScanMode = 0; |
| 37 | } |
| 38 | |
| 39 | int WifiController::start() { |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int WifiController::stop() { |
| 44 | errno = ENOSYS; |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | int WifiController::enable() { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 49 | if (!isPoweredUp()) { |
| 50 | sendStatusBroadcast("POWERING_UP"); |
| 51 | if (powerUp()) { |
| 52 | LOGE("Powerup failed (%s)", strerror(errno)); |
| 53 | return -1; |
| 54 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 55 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 56 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 57 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 58 | sendStatusBroadcast("LOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 59 | if (loadKernelModule(mModulePath, mModuleArgs)) { |
| 60 | LOGE("Kernel module load failed (%s)", strerror(errno)); |
| 61 | goto out_powerdown; |
| 62 | } |
| 63 | } |
| 64 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 65 | if (!isFirmwareLoaded()) { |
| 66 | sendStatusBroadcast("LOADING_FIRMWARE"); |
| 67 | if (loadFirmware()) { |
| 68 | LOGE("Firmware load failed (%s)", strerror(errno)); |
| 69 | goto out_powerdown; |
| 70 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 73 | if (!mSupplicant->isStarted()) { |
| 74 | sendStatusBroadcast("STARTING_SUPPLICANT"); |
| 75 | if (mSupplicant->start()) { |
| 76 | LOGE("Supplicant start failed (%s)", strerror(errno)); |
| 77 | goto out_unloadmodule; |
| 78 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | |
| 83 | out_unloadmodule: |
| 84 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
| 85 | if (unloadKernelModule(mModuleName)) { |
| 86 | LOGE("Unable to unload module after failure!"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | out_powerdown: |
| 91 | if (powerDown()) { |
| 92 | LOGE("Unable to powerdown after failure!"); |
| 93 | } |
| 94 | return -1; |
| 95 | } |
| 96 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 97 | void WifiController::sendStatusBroadcast(char *msg) { |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 98 | NetworkManager::Instance()-> |
| 99 | getBroadcaster()-> |
| 100 | sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int WifiController::disable() { |
| 104 | |
| 105 | if (mSupplicant->isStarted()) { |
| 106 | sendStatusBroadcast("STOPPING_SUPPLICANT"); |
| 107 | if (mSupplicant->stop()) { |
| 108 | LOGE("Supplicant stop failed (%s)", strerror(errno)); |
| 109 | return -1; |
| 110 | } |
| 111 | } else |
| 112 | LOGW("disable(): Supplicant not running?"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 113 | |
| 114 | if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 115 | sendStatusBroadcast("UNLOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 116 | if (unloadKernelModule(mModuleName)) { |
| 117 | LOGE("Unable to unload module (%s)", strerror(errno)); |
| 118 | return -1; |
| 119 | } |
| 120 | } |
| 121 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 122 | if (isPoweredUp()) { |
| 123 | sendStatusBroadcast("POWERING_DOWN"); |
| 124 | if (powerDown()) { |
| 125 | LOGE("Powerdown failed (%s)", strerror(errno)); |
| 126 | return -1; |
| 127 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int WifiController::loadFirmware() { |
| 133 | return 0; |
| 134 | } |
| 135 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 136 | int WifiController::setScanMode(uint32_t mode) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 137 | int rc = 0; |
| 138 | |
| 139 | if (mCurrentScanMode == mode) |
| 140 | return 0; |
| 141 | |
| 142 | if (!(mode & SCAN_ENABLE_MASK)) { |
| 143 | if (mCurrentScanMode & SCAN_REPEAT_MASK) |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 144 | mScanner->stopPeriodicScan(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 145 | } else if (mode & SCAN_REPEAT_MASK) |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 146 | rc = mScanner->startPeriodicScan(mode & SCAN_ACTIVE_MASK); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 147 | else |
| 148 | rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK); |
| 149 | |
| 150 | return rc; |
| 151 | } |
| 152 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 153 | ScanResultCollection *WifiController::createScanResults() { |
| 154 | return mSupplicant->createLatestScanResults(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 155 | } |