blob: 72207ce23d7516421c19b06447aced2660c00699 [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 Mehatdc266072009-05-06 11:16:52 -070029
30WifiController::WifiController(char *modpath, char *modname, char *modargs) :
San Mehat48765672009-05-20 15:28:43 -070031 Controller("WIFI", "wifi") {
San Mehatdc266072009-05-06 11:16:52 -070032 strncpy(mModulePath, modpath, sizeof(mModulePath));
33 strncpy(mModuleName, modname, sizeof(mModuleName));
34 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
35
36 mSupplicant = new Supplicant();
San Mehat1441e762009-05-07 11:37:10 -070037 mScanner = new WifiScanner(mSupplicant, 10);
San Mehatdc266072009-05-06 11:16:52 -070038 mCurrentScanMode = 0;
San Mehat48765672009-05-20 15:28:43 -070039
40 registerProperty("scanmode");
San Mehatdc266072009-05-06 11:16:52 -070041}
42
43int WifiController::start() {
44 return 0;
45}
46
47int WifiController::stop() {
48 errno = ENOSYS;
49 return -1;
50}
51
52int WifiController::enable() {
San Mehat1441e762009-05-07 11:37:10 -070053 if (!isPoweredUp()) {
54 sendStatusBroadcast("POWERING_UP");
55 if (powerUp()) {
56 LOGE("Powerup failed (%s)", strerror(errno));
57 return -1;
58 }
San Mehatdc266072009-05-06 11:16:52 -070059 }
San Mehat1441e762009-05-07 11:37:10 -070060
San Mehatdc266072009-05-06 11:16:52 -070061 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -070062 sendStatusBroadcast("LOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -070063 if (loadKernelModule(mModulePath, mModuleArgs)) {
64 LOGE("Kernel module load failed (%s)", strerror(errno));
65 goto out_powerdown;
66 }
67 }
68
San Mehat1441e762009-05-07 11:37:10 -070069 if (!isFirmwareLoaded()) {
70 sendStatusBroadcast("LOADING_FIRMWARE");
71 if (loadFirmware()) {
72 LOGE("Firmware load failed (%s)", strerror(errno));
73 goto out_powerdown;
74 }
San Mehatdc266072009-05-06 11:16:52 -070075 }
76
San Mehat1441e762009-05-07 11:37:10 -070077 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 Mehatdc266072009-05-06 11:16:52 -070083 }
84
85 return 0;
86
87out_unloadmodule:
88 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
89 if (unloadKernelModule(mModuleName)) {
90 LOGE("Unable to unload module after failure!");
91 }
92 }
93
94out_powerdown:
95 if (powerDown()) {
96 LOGE("Unable to powerdown after failure!");
97 }
98 return -1;
99}
100
San Mehat48765672009-05-20 15:28:43 -0700101void WifiController::sendStatusBroadcast(const char *msg) {
San Mehat8d3fc3f2009-05-12 14:36:32 -0700102 NetworkManager::Instance()->
103 getBroadcaster()->
104 sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false);
San Mehat1441e762009-05-07 11:37:10 -0700105}
106
107int 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 Mehatdc266072009-05-06 11:16:52 -0700117
118 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -0700119 sendStatusBroadcast("UNLOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -0700120 if (unloadKernelModule(mModuleName)) {
121 LOGE("Unable to unload module (%s)", strerror(errno));
122 return -1;
123 }
124 }
125
San Mehat1441e762009-05-07 11:37:10 -0700126 if (isPoweredUp()) {
127 sendStatusBroadcast("POWERING_DOWN");
128 if (powerDown()) {
129 LOGE("Powerdown failed (%s)", strerror(errno));
130 return -1;
131 }
San Mehatdc266072009-05-06 11:16:52 -0700132 }
133 return 0;
134}
135
136int WifiController::loadFirmware() {
137 return 0;
138}
139
San Mehat1441e762009-05-07 11:37:10 -0700140int WifiController::setScanMode(uint32_t mode) {
San Mehatdc266072009-05-06 11:16:52 -0700141 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 Mehate67651c2009-05-12 15:50:49 -0700148 mScanner->stop();
San Mehatdc266072009-05-06 11:16:52 -0700149 } else if (mode & SCAN_REPEAT_MASK)
San Mehate67651c2009-05-12 15:50:49 -0700150 rc = mScanner->start(mode & SCAN_ACTIVE_MASK);
San Mehatdc266072009-05-06 11:16:52 -0700151 else
152 rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
San Mehate67651c2009-05-12 15:50:49 -0700153
154 mCurrentScanMode = mode;
San Mehatdc266072009-05-06 11:16:52 -0700155 return rc;
156}
157
San Mehat82a21162009-05-12 17:26:28 -0700158int WifiController::addNetwork() {
159 return mSupplicant->addNetwork();
160}
161
162int WifiController::removeNetwork(int networkId) {
163 return mSupplicant->removeNetwork(networkId);
164}
165
San Mehat1441e762009-05-07 11:37:10 -0700166ScanResultCollection *WifiController::createScanResults() {
167 return mSupplicant->createLatestScanResults();
San Mehatdc266072009-05-06 11:16:52 -0700168}
San Mehat82a21162009-05-12 17:26:28 -0700169
170// XXX: This should be a const list
171WifiNetworkCollection *WifiController::createNetworkList() {
172 return mSupplicant->createNetworkList();
173}
San Mehat48765672009-05-20 15:28:43 -0700174
175int 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
182const 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