blob: 3d068061b5f46191ad9391d9609db71ddd1fd18f [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
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 Mehat48765672009-05-20 15:28:43 -070016
17#include <stdlib.h>
San Mehatdc266072009-05-06 11:16:52 -070018#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 Mehat1441e762009-05-07 11:37:10 -070026#include "WifiScanner.h"
27#include "NetworkManager.h"
San Mehat82a21162009-05-12 17:26:28 -070028#include "ErrorCode.h"
San Mehat3c5a6f02009-05-22 15:36:13 -070029#include "WifiNetwork.h"
San Mehatdc266072009-05-06 11:16:52 -070030
San Mehat3c5a6f02009-05-22 15:36:13 -070031WifiController::WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) :
32 Controller("WIFI", propmngr) {
San Mehatdc266072009-05-06 11:16:52 -070033 strncpy(mModulePath, modpath, sizeof(mModulePath));
34 strncpy(mModuleName, modname, sizeof(mModuleName));
35 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
36
San Mehat3c5a6f02009-05-22 15:36:13 -070037 mSupplicant = new Supplicant(this, propmngr);
San Mehat1441e762009-05-07 11:37:10 -070038 mScanner = new WifiScanner(mSupplicant, 10);
San Mehatdc266072009-05-06 11:16:52 -070039 mCurrentScanMode = 0;
San Mehat48765672009-05-20 15:28:43 -070040
San Mehat3c5a6f02009-05-22 15:36:13 -070041 mEnabled = false;
42
43 propmngr->registerProperty("wifi.enabled", this);
San Mehatdc266072009-05-06 11:16:52 -070044}
45
46int WifiController::start() {
47 return 0;
48}
49
50int WifiController::stop() {
51 errno = ENOSYS;
52 return -1;
53}
54
55int WifiController::enable() {
San Mehat1441e762009-05-07 11:37:10 -070056 if (!isPoweredUp()) {
57 sendStatusBroadcast("POWERING_UP");
58 if (powerUp()) {
59 LOGE("Powerup failed (%s)", strerror(errno));
60 return -1;
61 }
San Mehatdc266072009-05-06 11:16:52 -070062 }
San Mehat1441e762009-05-07 11:37:10 -070063
San Mehatdc266072009-05-06 11:16:52 -070064 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -070065 sendStatusBroadcast("LOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -070066 if (loadKernelModule(mModulePath, mModuleArgs)) {
67 LOGE("Kernel module load failed (%s)", strerror(errno));
68 goto out_powerdown;
69 }
70 }
71
San Mehat1441e762009-05-07 11:37:10 -070072 if (!isFirmwareLoaded()) {
73 sendStatusBroadcast("LOADING_FIRMWARE");
74 if (loadFirmware()) {
75 LOGE("Firmware load failed (%s)", strerror(errno));
76 goto out_powerdown;
77 }
San Mehatdc266072009-05-06 11:16:52 -070078 }
79
San Mehat1441e762009-05-07 11:37:10 -070080 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 Mehatdc266072009-05-06 11:16:52 -070086 }
87
San Mehat3c5a6f02009-05-22 15:36:13 -070088 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 Mehatdc266072009-05-06 11:16:52 -070099 return 0;
100
101out_unloadmodule:
102 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
103 if (unloadKernelModule(mModuleName)) {
104 LOGE("Unable to unload module after failure!");
105 }
106 }
107
108out_powerdown:
109 if (powerDown()) {
110 LOGE("Unable to powerdown after failure!");
111 }
112 return -1;
113}
114
San Mehat48765672009-05-20 15:28:43 -0700115void WifiController::sendStatusBroadcast(const char *msg) {
San Mehat8d3fc3f2009-05-12 14:36:32 -0700116 NetworkManager::Instance()->
117 getBroadcaster()->
118 sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false);
San Mehat1441e762009-05-07 11:37:10 -0700119}
120
121int WifiController::disable() {
122
San Mehat3c5a6f02009-05-22 15:36:13 -0700123 mPropMngr->unregisterProperty("wifi.scanmode");
San Mehat1441e762009-05-07 11:37:10 -0700124 if (mSupplicant->isStarted()) {
125 sendStatusBroadcast("STOPPING_SUPPLICANT");
126 if (mSupplicant->stop()) {
127 LOGE("Supplicant stop failed (%s)", strerror(errno));
128 return -1;
129 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700130 } else
San Mehat1441e762009-05-07 11:37:10 -0700131 LOGW("disable(): Supplicant not running?");
San Mehatdc266072009-05-06 11:16:52 -0700132
133 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -0700134 sendStatusBroadcast("UNLOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -0700135 if (unloadKernelModule(mModuleName)) {
136 LOGE("Unable to unload module (%s)", strerror(errno));
137 return -1;
138 }
139 }
140
San Mehat1441e762009-05-07 11:37:10 -0700141 if (isPoweredUp()) {
142 sendStatusBroadcast("POWERING_DOWN");
143 if (powerDown()) {
144 LOGE("Powerdown failed (%s)", strerror(errno));
145 return -1;
146 }
San Mehatdc266072009-05-06 11:16:52 -0700147 }
148 return 0;
149}
150
151int WifiController::loadFirmware() {
152 return 0;
153}
154
San Mehat1441e762009-05-07 11:37:10 -0700155int WifiController::setScanMode(uint32_t mode) {
San Mehatdc266072009-05-06 11:16:52 -0700156 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 Mehate67651c2009-05-12 15:50:49 -0700163 mScanner->stop();
San Mehatdc266072009-05-06 11:16:52 -0700164 } else if (mode & SCAN_REPEAT_MASK)
San Mehate67651c2009-05-12 15:50:49 -0700165 rc = mScanner->start(mode & SCAN_ACTIVE_MASK);
San Mehatdc266072009-05-06 11:16:52 -0700166 else
167 rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
San Mehate67651c2009-05-12 15:50:49 -0700168
169 mCurrentScanMode = mode;
San Mehatdc266072009-05-06 11:16:52 -0700170 return rc;
171}
172
San Mehat3c5a6f02009-05-22 15:36:13 -0700173WifiNetwork *WifiController::createNetwork() {
174 WifiNetwork *wn = mSupplicant->createNetwork();
175 return wn;
San Mehat82a21162009-05-12 17:26:28 -0700176}
177
178int WifiController::removeNetwork(int networkId) {
San Mehat3c5a6f02009-05-22 15:36:13 -0700179 WifiNetwork *wn = mSupplicant->lookupNetwork(networkId);
180
181 if (!wn)
182 return -1;
183 return mSupplicant->removeNetwork(wn);
San Mehat82a21162009-05-12 17:26:28 -0700184}
185
San Mehat1441e762009-05-07 11:37:10 -0700186ScanResultCollection *WifiController::createScanResults() {
187 return mSupplicant->createLatestScanResults();
San Mehatdc266072009-05-06 11:16:52 -0700188}
San Mehat82a21162009-05-12 17:26:28 -0700189
San Mehat82a21162009-05-12 17:26:28 -0700190WifiNetworkCollection *WifiController::createNetworkList() {
191 return mSupplicant->createNetworkList();
192}
San Mehat48765672009-05-20 15:28:43 -0700193
San Mehat3c5a6f02009-05-22 15:36:13 -0700194int WifiController::set(const char *name, const char *value) {
195 int rc;
San Mehat48765672009-05-20 15:28:43 -0700196
San Mehat3c5a6f02009-05-22 15:36:13 -0700197 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 Mehat48765672009-05-20 15:28:43 -0700213}
214
San Mehat3c5a6f02009-05-22 15:36:13 -0700215const char *WifiController::get(const char *name, char *buffer, size_t maxsize) {
San Mehat48765672009-05-20 15:28:43 -0700216
San Mehat3c5a6f02009-05-22 15:36:13 -0700217 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 Mehat48765672009-05-20 15:28:43 -0700228}
229