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 | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 29 | |
| 30 | WifiController::WifiController(char *modpath, char *modname, char *modargs) : |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 31 | Controller("WIFI", "wifi") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 32 | strncpy(mModulePath, modpath, sizeof(mModulePath)); |
| 33 | strncpy(mModuleName, modname, sizeof(mModuleName)); |
| 34 | strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); |
| 35 | |
| 36 | mSupplicant = new Supplicant(); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 37 | mScanner = new WifiScanner(mSupplicant, 10); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 38 | mCurrentScanMode = 0; |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 39 | |
| 40 | registerProperty("scanmode"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int WifiController::start() { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int WifiController::stop() { |
| 48 | errno = ENOSYS; |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | int WifiController::enable() { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 53 | if (!isPoweredUp()) { |
| 54 | sendStatusBroadcast("POWERING_UP"); |
| 55 | if (powerUp()) { |
| 56 | LOGE("Powerup failed (%s)", strerror(errno)); |
| 57 | return -1; |
| 58 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 59 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 60 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 61 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 62 | sendStatusBroadcast("LOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 63 | if (loadKernelModule(mModulePath, mModuleArgs)) { |
| 64 | LOGE("Kernel module load failed (%s)", strerror(errno)); |
| 65 | goto out_powerdown; |
| 66 | } |
| 67 | } |
| 68 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 69 | if (!isFirmwareLoaded()) { |
| 70 | sendStatusBroadcast("LOADING_FIRMWARE"); |
| 71 | if (loadFirmware()) { |
| 72 | LOGE("Firmware load failed (%s)", strerror(errno)); |
| 73 | goto out_powerdown; |
| 74 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 75 | } |
| 76 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 77 | if (!mSupplicant->isStarted()) { |
| 78 | sendStatusBroadcast("STARTING_SUPPLICANT"); |
| 79 | if (mSupplicant->start()) { |
| 80 | LOGE("Supplicant start failed (%s)", strerror(errno)); |
| 81 | goto out_unloadmodule; |
| 82 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | |
| 87 | out_unloadmodule: |
| 88 | if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { |
| 89 | if (unloadKernelModule(mModuleName)) { |
| 90 | LOGE("Unable to unload module after failure!"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | out_powerdown: |
| 95 | if (powerDown()) { |
| 96 | LOGE("Unable to powerdown after failure!"); |
| 97 | } |
| 98 | return -1; |
| 99 | } |
| 100 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 101 | void WifiController::sendStatusBroadcast(const char *msg) { |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 102 | NetworkManager::Instance()-> |
| 103 | getBroadcaster()-> |
| 104 | sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int WifiController::disable() { |
| 108 | |
| 109 | if (mSupplicant->isStarted()) { |
| 110 | sendStatusBroadcast("STOPPING_SUPPLICANT"); |
| 111 | if (mSupplicant->stop()) { |
| 112 | LOGE("Supplicant stop failed (%s)", strerror(errno)); |
| 113 | return -1; |
| 114 | } |
| 115 | } else |
| 116 | LOGW("disable(): Supplicant not running?"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 117 | |
| 118 | if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 119 | sendStatusBroadcast("UNLOADING_DRIVER"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 120 | if (unloadKernelModule(mModuleName)) { |
| 121 | LOGE("Unable to unload module (%s)", strerror(errno)); |
| 122 | return -1; |
| 123 | } |
| 124 | } |
| 125 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 126 | if (isPoweredUp()) { |
| 127 | sendStatusBroadcast("POWERING_DOWN"); |
| 128 | if (powerDown()) { |
| 129 | LOGE("Powerdown failed (%s)", strerror(errno)); |
| 130 | return -1; |
| 131 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int WifiController::loadFirmware() { |
| 137 | return 0; |
| 138 | } |
| 139 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 140 | int WifiController::setScanMode(uint32_t mode) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 141 | int rc = 0; |
| 142 | |
| 143 | if (mCurrentScanMode == mode) |
| 144 | return 0; |
| 145 | |
| 146 | if (!(mode & SCAN_ENABLE_MASK)) { |
| 147 | if (mCurrentScanMode & SCAN_REPEAT_MASK) |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 148 | mScanner->stop(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 149 | } else if (mode & SCAN_REPEAT_MASK) |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 150 | rc = mScanner->start(mode & SCAN_ACTIVE_MASK); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 151 | else |
| 152 | rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK); |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 153 | |
| 154 | mCurrentScanMode = mode; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 155 | return rc; |
| 156 | } |
| 157 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 158 | int WifiController::addNetwork() { |
| 159 | return mSupplicant->addNetwork(); |
| 160 | } |
| 161 | |
| 162 | int WifiController::removeNetwork(int networkId) { |
| 163 | return mSupplicant->removeNetwork(networkId); |
| 164 | } |
| 165 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 166 | ScanResultCollection *WifiController::createScanResults() { |
| 167 | return mSupplicant->createLatestScanResults(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 168 | } |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 169 | |
| 170 | // XXX: This should be a const list |
| 171 | WifiNetworkCollection *WifiController::createNetworkList() { |
| 172 | return mSupplicant->createNetworkList(); |
| 173 | } |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 174 | |
| 175 | int WifiController::setProperty(const char *name, char *value) { |
| 176 | if (!strcmp(name, "scanmode")) |
| 177 | return setScanMode((uint32_t) strtoul(value, NULL, 0)); |
| 178 | |
| 179 | return Controller::setProperty(name, value); |
| 180 | } |
| 181 | |
| 182 | const char *WifiController::getProperty(const char *name, char *buffer, size_t maxsize) { |
| 183 | if (!strcmp(name, "scanmode")) { |
| 184 | snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode); |
| 185 | return buffer; |
| 186 | } |
| 187 | |
| 188 | return Controller::getProperty(name, buffer, maxsize); |
| 189 | } |
| 190 | |