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 | */ |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdlib.h> |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 18 | #include <string.h> |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #define LOG_TAG "WifiController" |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include "Supplicant.h" |
| 25 | #include "WifiController.h" |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 26 | #include "WifiScanner.h" |
| 27 | #include "NetworkManager.h" |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 28 | #include "ErrorCode.h" |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 29 | #include "WifiNetwork.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 30 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 31 | WifiController::WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) : |
| 32 | Controller("WIFI", propmngr) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 33 | strncpy(mModulePath, modpath, sizeof(mModulePath)); |
| 34 | strncpy(mModuleName, modname, sizeof(mModuleName)); |
| 35 | strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); |
| 36 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 37 | mSupplicant = new Supplicant(this, propmngr); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 38 | mScanner = new WifiScanner(mSupplicant, 10); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 39 | mCurrentScanMode = 0; |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 40 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 41 | mEnabled = false; |
| 42 | |
| 43 | propmngr->registerProperty("wifi.enabled", this); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int WifiController::start() { |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int WifiController::stop() { |
| 51 | errno = ENOSYS; |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | int WifiController::enable() { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 56 | if (!isPoweredUp()) { |
| 57 | sendStatusBroadcast("POWERING_UP"); |
| 58 | if (powerUp()) { |
| 59 | LOGE("Powerup failed (%s)", strerror(errno)); |
| 60 | return -1; |
| 61 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 62 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 63 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 64 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 65 | sendStatusBroadcast("LOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 66 | if (loadKernelModule(mModulePath, mModuleArgs)) { |
| 67 | LOGE("Kernel module load failed (%s)", strerror(errno)); |
| 68 | goto out_powerdown; |
| 69 | } |
| 70 | } |
| 71 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 72 | if (!isFirmwareLoaded()) { |
| 73 | sendStatusBroadcast("LOADING_FIRMWARE"); |
| 74 | if (loadFirmware()) { |
| 75 | LOGE("Firmware load failed (%s)", strerror(errno)); |
| 76 | goto out_powerdown; |
| 77 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 78 | } |
| 79 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 80 | if (!mSupplicant->isStarted()) { |
| 81 | sendStatusBroadcast("STARTING_SUPPLICANT"); |
| 82 | if (mSupplicant->start()) { |
| 83 | LOGE("Supplicant start failed (%s)", strerror(errno)); |
| 84 | goto out_unloadmodule; |
| 85 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 86 | } |
| 87 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 88 | if (Controller::bindInterface(mSupplicant->getInterfaceName())) { |
| 89 | LOGE("Error binding interface (%s)", strerror(errno)); |
| 90 | goto out_unloadmodule; |
| 91 | } |
| 92 | |
| 93 | if (mSupplicant->refreshNetworkList()) |
| 94 | LOGW("Error getting list of networks (%s)", strerror(errno)); |
| 95 | |
| 96 | mPropMngr->registerProperty("wifi.scanmode", this); |
| 97 | mPropMngr->registerProperty("wifi.interface", this); |
| 98 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | |
| 101 | out_unloadmodule: |
| 102 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
| 103 | if (unloadKernelModule(mModuleName)) { |
| 104 | LOGE("Unable to unload module after failure!"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | out_powerdown: |
| 109 | if (powerDown()) { |
| 110 | LOGE("Unable to powerdown after failure!"); |
| 111 | } |
| 112 | return -1; |
| 113 | } |
| 114 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 115 | void WifiController::sendStatusBroadcast(const char *msg) { |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 116 | NetworkManager::Instance()-> |
| 117 | getBroadcaster()-> |
| 118 | sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int WifiController::disable() { |
| 122 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 123 | mPropMngr->unregisterProperty("wifi.scanmode"); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 124 | if (mSupplicant->isStarted()) { |
| 125 | sendStatusBroadcast("STOPPING_SUPPLICANT"); |
| 126 | if (mSupplicant->stop()) { |
| 127 | LOGE("Supplicant stop failed (%s)", strerror(errno)); |
| 128 | return -1; |
| 129 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 130 | } else |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 131 | LOGW("disable(): Supplicant not running?"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 132 | |
| 133 | if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 134 | sendStatusBroadcast("UNLOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 135 | if (unloadKernelModule(mModuleName)) { |
| 136 | LOGE("Unable to unload module (%s)", strerror(errno)); |
| 137 | return -1; |
| 138 | } |
| 139 | } |
| 140 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 141 | if (isPoweredUp()) { |
| 142 | sendStatusBroadcast("POWERING_DOWN"); |
| 143 | if (powerDown()) { |
| 144 | LOGE("Powerdown failed (%s)", strerror(errno)); |
| 145 | return -1; |
| 146 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int WifiController::loadFirmware() { |
| 152 | return 0; |
| 153 | } |
| 154 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 155 | int WifiController::setScanMode(uint32_t mode) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 156 | int rc = 0; |
| 157 | |
| 158 | if (mCurrentScanMode == mode) |
| 159 | return 0; |
| 160 | |
| 161 | if (!(mode & SCAN_ENABLE_MASK)) { |
| 162 | if (mCurrentScanMode & SCAN_REPEAT_MASK) |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 163 | mScanner->stop(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 164 | } else if (mode & SCAN_REPEAT_MASK) |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 165 | rc = mScanner->start(mode & SCAN_ACTIVE_MASK); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 166 | else |
| 167 | rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK); |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 168 | |
| 169 | mCurrentScanMode = mode; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 170 | return rc; |
| 171 | } |
| 172 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 173 | WifiNetwork *WifiController::createNetwork() { |
| 174 | WifiNetwork *wn = mSupplicant->createNetwork(); |
| 175 | return wn; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int WifiController::removeNetwork(int networkId) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 179 | WifiNetwork *wn = mSupplicant->lookupNetwork(networkId); |
| 180 | |
| 181 | if (!wn) |
| 182 | return -1; |
| 183 | return mSupplicant->removeNetwork(wn); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 184 | } |
| 185 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 186 | ScanResultCollection *WifiController::createScanResults() { |
| 187 | return mSupplicant->createLatestScanResults(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 188 | } |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 189 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 190 | WifiNetworkCollection *WifiController::createNetworkList() { |
| 191 | return mSupplicant->createNetworkList(); |
| 192 | } |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 193 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 194 | int WifiController::set(const char *name, const char *value) { |
| 195 | int rc; |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 196 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 197 | if (!strcmp(name, "wifi.enabled")) { |
| 198 | int en = atoi(value); |
| 199 | |
| 200 | if (en == mEnabled) |
| 201 | return 0; |
| 202 | rc = (en ? enable() : disable()); |
| 203 | if (!rc) |
| 204 | mEnabled = en; |
| 205 | } else if (!strcmp(name, "wifi.interface")) { |
| 206 | errno = EROFS; |
| 207 | return -1; |
| 208 | } else if (!strcmp(name, "wifi.scanmode")) |
| 209 | return setScanMode((uint32_t) strtoul(value, NULL, 0)); |
| 210 | else |
| 211 | return Controller::set(name, value); |
| 212 | return rc; |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 215 | const char *WifiController::get(const char *name, char *buffer, size_t maxsize) { |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 216 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame^] | 217 | if (!strcmp(name, "wifi.enabled")) |
| 218 | snprintf(buffer, maxsize, "%d", mEnabled); |
| 219 | else if (!strcmp(name, "wifi.interface")) { |
| 220 | snprintf(buffer, maxsize, "%s", |
| 221 | (getBoundInterface() ? getBoundInterface() : "none")); |
| 222 | } else if (!strcmp(name, "wifi.scanmode")) |
| 223 | snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode); |
| 224 | else |
| 225 | return Controller::get(name, buffer, maxsize); |
| 226 | |
| 227 | return buffer; |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 228 | } |
| 229 | |