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