blob: cedd01387e7c3a9553d39901e7eb144e64647ffa [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 "NetworkManager.h"
San Mehatc4a895b2009-06-23 21:10:57 -070027#include "ResponseCode.h"
San Mehat3c5a6f02009-05-22 15:36:13 -070028#include "WifiNetwork.h"
San Mehat3aff2d12009-06-15 14:10:44 -070029#include "ISupplicantEventHandler.h"
30#include "SupplicantState.h"
31#include "SupplicantStatus.h"
32#include "SupplicantAssociatingEvent.h"
33#include "SupplicantAssociatedEvent.h"
34#include "SupplicantConnectedEvent.h"
35#include "SupplicantScanResultsEvent.h"
36#include "SupplicantStateChangeEvent.h"
37#include "SupplicantConnectionTimeoutEvent.h"
38#include "SupplicantDisconnectedEvent.h"
San Mehatc4a895b2009-06-23 21:10:57 -070039#include "WifiStatusPoller.h"
San Mehatdc266072009-05-06 11:16:52 -070040
San Mehat3aff2d12009-06-15 14:10:44 -070041WifiController::WifiController(PropertyManager *mPropMngr,
42 IControllerHandler *handlers,
43 char *modpath, char *modname, char *modargs) :
San Mehatc4a895b2009-06-23 21:10:57 -070044 Controller("wifi", mPropMngr, handlers) {
San Mehatdc266072009-05-06 11:16:52 -070045 strncpy(mModulePath, modpath, sizeof(mModulePath));
46 strncpy(mModuleName, modname, sizeof(mModuleName));
47 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
48
San Mehat3aff2d12009-06-15 14:10:44 -070049 mLatestScanResults = new ScanResultCollection();
50 pthread_mutex_init(&mLatestScanResultsLock, NULL);
51
San Mehatc4a895b2009-06-23 21:10:57 -070052 pthread_mutex_init(&mLock, NULL);
San Mehat48765672009-05-20 15:28:43 -070053
San Mehatc4a895b2009-06-23 21:10:57 -070054 mSupplicant = new Supplicant(this, this);
55 mActiveScan = false;
San Mehat3c5a6f02009-05-22 15:36:13 -070056 mEnabled = false;
San Mehatc4a895b2009-06-23 21:10:57 -070057 mScanOnly = false;
58 mPacketFilter = false;
59 mBluetoothCoexScan = false;
60 mBluetoothCoexMode = 0;
61 mCurrentlyConnectedNetworkId = -1;
62 mStatusPoller = new WifiStatusPoller(this);
63 mRssiEventThreshold = 5;
64 mLastLinkSpeed = 0;
San Mehat3c5a6f02009-05-22 15:36:13 -070065
San Mehat3aff2d12009-06-15 14:10:44 -070066 mSupplicantState = SupplicantState::UNKNOWN;
San Mehatc4a895b2009-06-23 21:10:57 -070067
68 mStaticProperties.propEnabled = new WifiEnabledProperty(this);
69 mStaticProperties.propScanOnly = new WifiScanOnlyProperty(this);
70 mStaticProperties.propAllowedChannels = new WifiAllowedChannelsProperty(this);
71
72 mStaticProperties.propRssiEventThreshold =
73 new IntegerPropertyHelper("RssiEventThreshold", false, &mRssiEventThreshold);
74
75 mDynamicProperties.propSupplicantState = new WifiSupplicantStateProperty(this);
76 mDynamicProperties.propActiveScan = new WifiActiveScanProperty(this);
77 mDynamicProperties.propInterface = new WifiInterfaceProperty(this);
78 mDynamicProperties.propSearching = new WifiSearchingProperty(this);
79 mDynamicProperties.propPacketFilter = new WifiPacketFilterProperty(this);
80 mDynamicProperties.propBluetoothCoexScan = new WifiBluetoothCoexScanProperty(this);
81 mDynamicProperties.propBluetoothCoexMode = new WifiBluetoothCoexModeProperty(this);
82 mDynamicProperties.propCurrentNetwork = new WifiCurrentNetworkProperty(this);
83
84 mDynamicProperties.propRssi = new IntegerPropertyHelper("Rssi", true, &mLastRssi);
85 mDynamicProperties.propLinkSpeed = new IntegerPropertyHelper("LinkSpeed", true, &mLastLinkSpeed);
86
87 mDynamicProperties.propSuspended = new WifiSuspendedProperty(this);
88 mDynamicProperties.propNetCount = new WifiNetCountProperty(this);
89 mDynamicProperties.propTriggerScan = new WifiTriggerScanProperty(this);
San Mehatdc266072009-05-06 11:16:52 -070090}
91
92int WifiController::start() {
San Mehatc4a895b2009-06-23 21:10:57 -070093 mPropMngr->attachProperty("wifi", mStaticProperties.propEnabled);
94 mPropMngr->attachProperty("wifi", mStaticProperties.propScanOnly);
95 mPropMngr->attachProperty("wifi", mStaticProperties.propAllowedChannels);
96 mPropMngr->attachProperty("wifi", mStaticProperties.propRssiEventThreshold);
San Mehatdc266072009-05-06 11:16:52 -070097 return 0;
98}
99
100int WifiController::stop() {
San Mehatc4a895b2009-06-23 21:10:57 -0700101 mPropMngr->detachProperty("wifi", mStaticProperties.propEnabled);
102 mPropMngr->detachProperty("wifi", mStaticProperties.propScanOnly);
103 mPropMngr->detachProperty("wifi", mStaticProperties.propAllowedChannels);
104 mPropMngr->detachProperty("wifi", mStaticProperties.propRssiEventThreshold);
San Mehat3aff2d12009-06-15 14:10:44 -0700105 return 0;
San Mehatdc266072009-05-06 11:16:52 -0700106}
107
108int WifiController::enable() {
San Mehat0f486582009-06-16 10:50:47 -0700109
San Mehat1441e762009-05-07 11:37:10 -0700110 if (!isPoweredUp()) {
Steve Blockfe71a612012-01-04 19:19:03 +0000111 ALOGI("Powering up");
San Mehat3aff2d12009-06-15 14:10:44 -0700112 sendStatusBroadcast("Powering up WiFi hardware");
San Mehat1441e762009-05-07 11:37:10 -0700113 if (powerUp()) {
Steve Block01dda202012-01-06 14:13:42 +0000114 ALOGE("Powerup failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -0700115 return -1;
116 }
San Mehatdc266072009-05-06 11:16:52 -0700117 }
San Mehat1441e762009-05-07 11:37:10 -0700118
San Mehatdc266072009-05-06 11:16:52 -0700119 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
Steve Blockfe71a612012-01-04 19:19:03 +0000120 ALOGI("Loading driver");
San Mehat3aff2d12009-06-15 14:10:44 -0700121 sendStatusBroadcast("Loading WiFi driver");
San Mehatdc266072009-05-06 11:16:52 -0700122 if (loadKernelModule(mModulePath, mModuleArgs)) {
Steve Block01dda202012-01-06 14:13:42 +0000123 ALOGE("Kernel module load failed (%s)", strerror(errno));
San Mehatdc266072009-05-06 11:16:52 -0700124 goto out_powerdown;
125 }
126 }
127
San Mehat1441e762009-05-07 11:37:10 -0700128 if (!isFirmwareLoaded()) {
Steve Blockfe71a612012-01-04 19:19:03 +0000129 ALOGI("Loading firmware");
San Mehat3aff2d12009-06-15 14:10:44 -0700130 sendStatusBroadcast("Loading WiFI firmware");
San Mehat1441e762009-05-07 11:37:10 -0700131 if (loadFirmware()) {
Steve Block01dda202012-01-06 14:13:42 +0000132 ALOGE("Firmware load failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -0700133 goto out_powerdown;
134 }
San Mehatdc266072009-05-06 11:16:52 -0700135 }
136
San Mehat1441e762009-05-07 11:37:10 -0700137 if (!mSupplicant->isStarted()) {
Steve Blockfe71a612012-01-04 19:19:03 +0000138 ALOGI("Starting WPA Supplicant");
San Mehat3aff2d12009-06-15 14:10:44 -0700139 sendStatusBroadcast("Starting WPA Supplicant");
San Mehat1441e762009-05-07 11:37:10 -0700140 if (mSupplicant->start()) {
Steve Block01dda202012-01-06 14:13:42 +0000141 ALOGE("Supplicant start failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -0700142 goto out_unloadmodule;
143 }
San Mehatdc266072009-05-06 11:16:52 -0700144 }
145
San Mehat3c5a6f02009-05-22 15:36:13 -0700146 if (Controller::bindInterface(mSupplicant->getInterfaceName())) {
Steve Block01dda202012-01-06 14:13:42 +0000147 ALOGE("Error binding interface (%s)", strerror(errno));
San Mehat3c5a6f02009-05-22 15:36:13 -0700148 goto out_unloadmodule;
149 }
150
151 if (mSupplicant->refreshNetworkList())
Steve Blockae8b56c2012-01-05 22:25:38 +0000152 ALOGW("Error getting list of networks (%s)", strerror(errno));
San Mehat3c5a6f02009-05-22 15:36:13 -0700153
Steve Blockae8b56c2012-01-05 22:25:38 +0000154 ALOGW("TODO: Set # of allowed regulatory channels!");
San Mehatc4a895b2009-06-23 21:10:57 -0700155
156 mPropMngr->attachProperty("wifi", mDynamicProperties.propSupplicantState);
157 mPropMngr->attachProperty("wifi", mDynamicProperties.propActiveScan);
158 mPropMngr->attachProperty("wifi", mDynamicProperties.propInterface);
159 mPropMngr->attachProperty("wifi", mDynamicProperties.propSearching);
160 mPropMngr->attachProperty("wifi", mDynamicProperties.propPacketFilter);
161 mPropMngr->attachProperty("wifi", mDynamicProperties.propBluetoothCoexScan);
162 mPropMngr->attachProperty("wifi", mDynamicProperties.propBluetoothCoexMode);
163 mPropMngr->attachProperty("wifi", mDynamicProperties.propCurrentNetwork);
164 mPropMngr->attachProperty("wifi", mDynamicProperties.propRssi);
165 mPropMngr->attachProperty("wifi", mDynamicProperties.propLinkSpeed);
166 mPropMngr->attachProperty("wifi", mDynamicProperties.propSuspended);
167 mPropMngr->attachProperty("wifi", mDynamicProperties.propNetCount);
168 mPropMngr->attachProperty("wifi", mDynamicProperties.propTriggerScan);
San Mehat3c5a6f02009-05-22 15:36:13 -0700169
Steve Blockfe71a612012-01-04 19:19:03 +0000170 ALOGI("Enabled successfully");
San Mehatdc266072009-05-06 11:16:52 -0700171 return 0;
172
173out_unloadmodule:
174 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
175 if (unloadKernelModule(mModuleName)) {
Steve Block01dda202012-01-06 14:13:42 +0000176 ALOGE("Unable to unload module after failure!");
San Mehatdc266072009-05-06 11:16:52 -0700177 }
178 }
179
180out_powerdown:
181 if (powerDown()) {
Steve Block01dda202012-01-06 14:13:42 +0000182 ALOGE("Unable to powerdown after failure!");
San Mehatdc266072009-05-06 11:16:52 -0700183 }
184 return -1;
185}
186
San Mehatc4a895b2009-06-23 21:10:57 -0700187bool WifiController::getSuspended() {
188 pthread_mutex_lock(&mLock);
189 bool r = mSuspended;
190 pthread_mutex_unlock(&mLock);
191 return r;
192}
193
194int WifiController::setSuspend(bool suspend) {
195
196 pthread_mutex_lock(&mLock);
197 if (suspend == mSuspended) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000198 ALOGW("Suspended state already = %d", suspend);
San Mehatc4a895b2009-06-23 21:10:57 -0700199 pthread_mutex_unlock(&mLock);
200 return 0;
201 }
202
203 if (suspend) {
204 mHandlers->onControllerSuspending(this);
205
206 char tmp[80];
Steve Block8d66c492011-12-20 16:07:45 +0000207 ALOGD("Suspending from supplicant state %s",
San Mehatc4a895b2009-06-23 21:10:57 -0700208 SupplicantState::toString(mSupplicantState,
209 tmp,
210 sizeof(tmp)));
211
212 if (mSupplicantState != SupplicantState::IDLE) {
Steve Block8d66c492011-12-20 16:07:45 +0000213 ALOGD("Forcing Supplicant disconnect");
San Mehatc4a895b2009-06-23 21:10:57 -0700214 if (mSupplicant->disconnect()) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000215 ALOGW("Error disconnecting (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -0700216 }
217 }
218
Steve Block8d66c492011-12-20 16:07:45 +0000219 ALOGD("Stopping Supplicant driver");
San Mehatc4a895b2009-06-23 21:10:57 -0700220 if (mSupplicant->stopDriver()) {
Steve Block01dda202012-01-06 14:13:42 +0000221 ALOGE("Error stopping driver (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -0700222 pthread_mutex_unlock(&mLock);
223 return -1;
224 }
225 } else {
Steve Block8d66c492011-12-20 16:07:45 +0000226 ALOGD("Resuming");
San Mehatc4a895b2009-06-23 21:10:57 -0700227
228 if (mSupplicant->startDriver()) {
Steve Block01dda202012-01-06 14:13:42 +0000229 ALOGE("Error resuming driver (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -0700230 pthread_mutex_unlock(&mLock);
231 return -1;
232 }
233 // XXX: set regulatory max channels
234 if (mScanOnly)
235 mSupplicant->triggerScan();
236 else
237 mSupplicant->reconnect();
238
239 mHandlers->onControllerResumed(this);
240 }
241
242 mSuspended = suspend;
243 pthread_mutex_unlock(&mLock);
Steve Block8d66c492011-12-20 16:07:45 +0000244 ALOGD("Suspend / Resume completed");
San Mehatc4a895b2009-06-23 21:10:57 -0700245 return 0;
246}
247
San Mehat48765672009-05-20 15:28:43 -0700248void WifiController::sendStatusBroadcast(const char *msg) {
San Mehat8d3fc3f2009-05-12 14:36:32 -0700249 NetworkManager::Instance()->
250 getBroadcaster()->
San Mehatc4a895b2009-06-23 21:10:57 -0700251 sendBroadcast(ResponseCode::UnsolicitedInformational, msg, false);
San Mehat1441e762009-05-07 11:37:10 -0700252}
253
254int WifiController::disable() {
255
San Mehatc4a895b2009-06-23 21:10:57 -0700256 mPropMngr->detachProperty("wifi", mDynamicProperties.propSupplicantState);
257 mPropMngr->detachProperty("wifi", mDynamicProperties.propActiveScan);
258 mPropMngr->detachProperty("wifi", mDynamicProperties.propInterface);
259 mPropMngr->detachProperty("wifi", mDynamicProperties.propSearching);
260 mPropMngr->detachProperty("wifi", mDynamicProperties.propPacketFilter);
261 mPropMngr->detachProperty("wifi", mDynamicProperties.propBluetoothCoexScan);
262 mPropMngr->detachProperty("wifi", mDynamicProperties.propBluetoothCoexMode);
263 mPropMngr->detachProperty("wifi", mDynamicProperties.propCurrentNetwork);
264 mPropMngr->detachProperty("wifi", mDynamicProperties.propRssi);
265 mPropMngr->detachProperty("wifi", mDynamicProperties.propLinkSpeed);
266 mPropMngr->detachProperty("wifi", mDynamicProperties.propSuspended);
267 mPropMngr->detachProperty("wifi", mDynamicProperties.propNetCount);
San Mehat3aff2d12009-06-15 14:10:44 -0700268
San Mehat1441e762009-05-07 11:37:10 -0700269 if (mSupplicant->isStarted()) {
San Mehat3aff2d12009-06-15 14:10:44 -0700270 sendStatusBroadcast("Stopping WPA Supplicant");
San Mehat1441e762009-05-07 11:37:10 -0700271 if (mSupplicant->stop()) {
Steve Block01dda202012-01-06 14:13:42 +0000272 ALOGE("Supplicant stop failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -0700273 return -1;
274 }
San Mehat3c5a6f02009-05-22 15:36:13 -0700275 } else
Steve Blockae8b56c2012-01-05 22:25:38 +0000276 ALOGW("disable(): Supplicant not running?");
San Mehatdc266072009-05-06 11:16:52 -0700277
278 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
San Mehat3aff2d12009-06-15 14:10:44 -0700279 sendStatusBroadcast("Unloading WiFi driver");
San Mehatdc266072009-05-06 11:16:52 -0700280 if (unloadKernelModule(mModuleName)) {
Steve Block01dda202012-01-06 14:13:42 +0000281 ALOGE("Unable to unload module (%s)", strerror(errno));
San Mehatdc266072009-05-06 11:16:52 -0700282 return -1;
283 }
284 }
285
San Mehat1441e762009-05-07 11:37:10 -0700286 if (isPoweredUp()) {
San Mehat3aff2d12009-06-15 14:10:44 -0700287 sendStatusBroadcast("Powering down WiFi hardware");
San Mehat1441e762009-05-07 11:37:10 -0700288 if (powerDown()) {
Steve Block01dda202012-01-06 14:13:42 +0000289 ALOGE("Powerdown failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -0700290 return -1;
291 }
San Mehatdc266072009-05-06 11:16:52 -0700292 }
293 return 0;
294}
295
296int WifiController::loadFirmware() {
297 return 0;
298}
299
San Mehatc4a895b2009-06-23 21:10:57 -0700300int WifiController::triggerScan() {
301 pthread_mutex_lock(&mLock);
302 if (verifyNotSuspended()) {
303 pthread_mutex_unlock(&mLock);
304 return -1;
305 }
San Mehatdc266072009-05-06 11:16:52 -0700306
San Mehatc4a895b2009-06-23 21:10:57 -0700307 switch (mSupplicantState) {
308 case SupplicantState::DISCONNECTED:
309 case SupplicantState::INACTIVE:
310 case SupplicantState::SCANNING:
311 case SupplicantState::IDLE:
312 break;
313 default:
314 // Switch to scan only mode
315 mSupplicant->setApScanMode(2);
316 break;
317 }
318
319 int rc = mSupplicant->triggerScan();
320 pthread_mutex_unlock(&mLock);
321 return rc;
322}
323
324int WifiController::setActiveScan(bool active) {
325 pthread_mutex_lock(&mLock);
326 if (mActiveScan == active) {
327 pthread_mutex_unlock(&mLock);
San Mehatdc266072009-05-06 11:16:52 -0700328 return 0;
San Mehatc4a895b2009-06-23 21:10:57 -0700329 }
330 mActiveScan = active;
San Mehatdc266072009-05-06 11:16:52 -0700331
San Mehatc4a895b2009-06-23 21:10:57 -0700332 int rc = mSupplicant->setScanMode(active);
333 pthread_mutex_unlock(&mLock);
San Mehatdc266072009-05-06 11:16:52 -0700334 return rc;
335}
336
San Mehat3c5a6f02009-05-22 15:36:13 -0700337WifiNetwork *WifiController::createNetwork() {
San Mehatc4a895b2009-06-23 21:10:57 -0700338 pthread_mutex_lock(&mLock);
San Mehat3c5a6f02009-05-22 15:36:13 -0700339 WifiNetwork *wn = mSupplicant->createNetwork();
San Mehatc4a895b2009-06-23 21:10:57 -0700340 pthread_mutex_unlock(&mLock);
San Mehat3c5a6f02009-05-22 15:36:13 -0700341 return wn;
San Mehat82a21162009-05-12 17:26:28 -0700342}
343
344int WifiController::removeNetwork(int networkId) {
San Mehatc4a895b2009-06-23 21:10:57 -0700345 pthread_mutex_lock(&mLock);
San Mehat3c5a6f02009-05-22 15:36:13 -0700346 WifiNetwork *wn = mSupplicant->lookupNetwork(networkId);
347
San Mehatc4a895b2009-06-23 21:10:57 -0700348 if (!wn) {
349 pthread_mutex_unlock(&mLock);
San Mehat3c5a6f02009-05-22 15:36:13 -0700350 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700351 }
352 int rc = mSupplicant->removeNetwork(wn);
353 pthread_mutex_unlock(&mLock);
354 return rc;
San Mehat82a21162009-05-12 17:26:28 -0700355}
356
San Mehat1441e762009-05-07 11:37:10 -0700357ScanResultCollection *WifiController::createScanResults() {
San Mehat3aff2d12009-06-15 14:10:44 -0700358 ScanResultCollection *d = new ScanResultCollection();
359 ScanResultCollection::iterator i;
360
361 pthread_mutex_lock(&mLatestScanResultsLock);
362 for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i)
363 d->push_back((*i)->clone());
364
365 pthread_mutex_unlock(&mLatestScanResultsLock);
366 return d;
San Mehatdc266072009-05-06 11:16:52 -0700367}
San Mehat82a21162009-05-12 17:26:28 -0700368
San Mehat82a21162009-05-12 17:26:28 -0700369WifiNetworkCollection *WifiController::createNetworkList() {
370 return mSupplicant->createNetworkList();
371}
San Mehat48765672009-05-20 15:28:43 -0700372
San Mehatc4a895b2009-06-23 21:10:57 -0700373int WifiController::setPacketFilter(bool enable) {
San Mehat3c5a6f02009-05-22 15:36:13 -0700374 int rc;
San Mehat48765672009-05-20 15:28:43 -0700375
San Mehatc4a895b2009-06-23 21:10:57 -0700376 pthread_mutex_lock(&mLock);
377 if (enable)
378 rc = mSupplicant->enablePacketFilter();
379 else
380 rc = mSupplicant->disablePacketFilter();
San Mehat3c5a6f02009-05-22 15:36:13 -0700381
San Mehatc4a895b2009-06-23 21:10:57 -0700382 if (!rc)
383 mPacketFilter = enable;
384 pthread_mutex_unlock(&mLock);
San Mehat3c5a6f02009-05-22 15:36:13 -0700385 return rc;
San Mehat48765672009-05-20 15:28:43 -0700386}
387
San Mehatc4a895b2009-06-23 21:10:57 -0700388int WifiController::setBluetoothCoexistenceScan(bool enable) {
389 int rc;
San Mehat48765672009-05-20 15:28:43 -0700390
San Mehatc4a895b2009-06-23 21:10:57 -0700391 pthread_mutex_lock(&mLock);
392
393 if (enable)
394 rc = mSupplicant->enableBluetoothCoexistenceScan();
San Mehat3c5a6f02009-05-22 15:36:13 -0700395 else
San Mehatc4a895b2009-06-23 21:10:57 -0700396 rc = mSupplicant->disableBluetoothCoexistenceScan();
San Mehat3c5a6f02009-05-22 15:36:13 -0700397
San Mehatc4a895b2009-06-23 21:10:57 -0700398 if (!rc)
399 mBluetoothCoexScan = enable;
400 pthread_mutex_unlock(&mLock);
401 return rc;
402}
403
404int WifiController::setScanOnly(bool scanOnly) {
405 pthread_mutex_lock(&mLock);
406 int rc = mSupplicant->setApScanMode((scanOnly ? 2 : 1));
407 if (!rc)
408 mScanOnly = scanOnly;
409 if (!mSuspended) {
410 if (scanOnly)
411 mSupplicant->disconnect();
412 else
413 mSupplicant->reconnect();
414 }
415 pthread_mutex_unlock(&mLock);
416 return rc;
417}
418
419int WifiController::setBluetoothCoexistenceMode(int mode) {
420 pthread_mutex_lock(&mLock);
421 int rc = mSupplicant->setBluetoothCoexistenceMode(mode);
422 if (!rc)
423 mBluetoothCoexMode = mode;
424 pthread_mutex_unlock(&mLock);
425 return rc;
San Mehat48765672009-05-20 15:28:43 -0700426}
427
San Mehat3aff2d12009-06-15 14:10:44 -0700428void WifiController::onAssociatingEvent(SupplicantAssociatingEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000429 ALOGD("onAssociatingEvent(%s, %s, %d)",
San Mehat3aff2d12009-06-15 14:10:44 -0700430 (evt->getBssid() ? evt->getBssid() : "n/a"),
431 (evt->getSsid() ? evt->getSsid() : "n/a"),
432 evt->getFreq());
433}
434
435void WifiController::onAssociatedEvent(SupplicantAssociatedEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000436 ALOGD("onAssociatedEvent(%s)", evt->getBssid());
San Mehat3aff2d12009-06-15 14:10:44 -0700437}
438
439void WifiController::onConnectedEvent(SupplicantConnectedEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000440 ALOGD("onConnectedEvent(%s, %d)", evt->getBssid(), evt->getReassociated());
San Mehatd6c67962009-06-22 10:39:36 -0700441 SupplicantStatus *ss = mSupplicant->getStatus();
442 WifiNetwork *wn;
San Mehat3aff2d12009-06-15 14:10:44 -0700443
San Mehatd6c67962009-06-22 10:39:36 -0700444 if (ss->getWpaState() != SupplicantState::COMPLETED) {
445 char tmp[32];
San Mehat3aff2d12009-06-15 14:10:44 -0700446
Steve Blockae8b56c2012-01-05 22:25:38 +0000447 ALOGW("onConnected() with SupplicantState = %s!",
San Mehatd6c67962009-06-22 10:39:36 -0700448 SupplicantState::toString(ss->getWpaState(), tmp,
449 sizeof(tmp)));
450 return;
San Mehat3aff2d12009-06-15 14:10:44 -0700451 }
San Mehatd6c67962009-06-22 10:39:36 -0700452
453 if (ss->getId() == -1) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000454 ALOGW("onConnected() with id = -1!");
San Mehatd6c67962009-06-22 10:39:36 -0700455 return;
456 }
457
San Mehatc4a895b2009-06-23 21:10:57 -0700458 mCurrentlyConnectedNetworkId = ss->getId();
San Mehatd6c67962009-06-22 10:39:36 -0700459 if (!(wn = mSupplicant->lookupNetwork(ss->getId()))) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000460 ALOGW("Error looking up connected network id %d (%s)",
San Mehatd6c67962009-06-22 10:39:36 -0700461 ss->getId(), strerror(errno));
462 return;
463 }
464
465 delete ss;
San Mehatc4a895b2009-06-23 21:10:57 -0700466 mHandlers->onInterfaceConnected(this);
San Mehat3aff2d12009-06-15 14:10:44 -0700467}
468
469void WifiController::onScanResultsEvent(SupplicantScanResultsEvent *evt) {
470 char *reply;
471
472 if (!(reply = (char *) malloc(4096))) {
Steve Block01dda202012-01-06 14:13:42 +0000473 ALOGE("Out of memory");
San Mehat3aff2d12009-06-15 14:10:44 -0700474 return;
475 }
476
San Mehatc4a895b2009-06-23 21:10:57 -0700477 mNumScanResultsSinceLastStateChange++;
478 if (mNumScanResultsSinceLastStateChange >= 3)
479 mIsSupplicantSearching = false;
480
San Mehat3aff2d12009-06-15 14:10:44 -0700481 size_t len = 4096;
482
483 if (mSupplicant->sendCommand("SCAN_RESULTS", reply, &len)) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000484 ALOGW("onScanResultsEvent: Error getting scan results (%s)",
San Mehat3aff2d12009-06-15 14:10:44 -0700485 strerror(errno));
486 free(reply);
487 return;
488 }
489
490 pthread_mutex_lock(&mLatestScanResultsLock);
491 if (!mLatestScanResults->empty()) {
492 ScanResultCollection::iterator i;
493
494 for (i = mLatestScanResults->begin();
495 i !=mLatestScanResults->end(); ++i) {
496 delete *i;
497 }
498 mLatestScanResults->clear();
499 }
500
501 char *linep;
502 char *linep_next = NULL;
503
504 if (!strtok_r(reply, "\n", &linep_next)) {
505 free(reply);
506 pthread_mutex_unlock(&mLatestScanResultsLock);
507 return;
508 }
509
510 while((linep = strtok_r(NULL, "\n", &linep_next)))
511 mLatestScanResults->push_back(new ScanResult(linep));
512
San Mehatc4a895b2009-06-23 21:10:57 -0700513 // Switch handling of scan results back to normal mode
514 mSupplicant->setApScanMode(1);
515
San Mehat3aff2d12009-06-15 14:10:44 -0700516 char *tmp;
517 asprintf(&tmp, "Scan results ready (%d)", mLatestScanResults->size());
518 NetworkManager::Instance()->getBroadcaster()->
San Mehatc4a895b2009-06-23 21:10:57 -0700519 sendBroadcast(ResponseCode::ScanResultsReady,
520 tmp, false);
San Mehat3aff2d12009-06-15 14:10:44 -0700521 free(tmp);
522 pthread_mutex_unlock(&mLatestScanResultsLock);
523 free(reply);
524}
525
526void WifiController::onStateChangeEvent(SupplicantStateChangeEvent *evt) {
527 char tmp[32];
528 char tmp2[32];
529
San Mehatc4a895b2009-06-23 21:10:57 -0700530 if (evt->getState() == mSupplicantState)
531 return;
532
Steve Block8d66c492011-12-20 16:07:45 +0000533 ALOGD("onStateChangeEvent(%s -> %s)",
San Mehat3aff2d12009-06-15 14:10:44 -0700534 SupplicantState::toString(mSupplicantState, tmp, sizeof(tmp)),
535 SupplicantState::toString(evt->getState(), tmp2, sizeof(tmp2)));
536
San Mehatc4a895b2009-06-23 21:10:57 -0700537 if (evt->getState() != SupplicantState::SCANNING) {
538 mIsSupplicantSearching = true;
539 mNumScanResultsSinceLastStateChange = 0;
540 }
541
542 char *tmp3;
543 asprintf(&tmp3,
544 "Supplicant state changed from %d (%s) -> %d (%s)",
545 mSupplicantState, tmp, evt->getState(), tmp2);
546
San Mehat3aff2d12009-06-15 14:10:44 -0700547 mSupplicantState = evt->getState();
San Mehatc4a895b2009-06-23 21:10:57 -0700548
549 if (mSupplicantState == SupplicantState::COMPLETED) {
550 mStatusPoller->start();
551 } else if (mStatusPoller->isStarted()) {
552 mStatusPoller->stop();
553 }
554
555 NetworkManager::Instance()->getBroadcaster()->
556 sendBroadcast(ResponseCode::SupplicantStateChange,
557 tmp3, false);
558 free(tmp3);
San Mehat3aff2d12009-06-15 14:10:44 -0700559}
560
561void WifiController::onConnectionTimeoutEvent(SupplicantConnectionTimeoutEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000562 ALOGD("onConnectionTimeoutEvent(%s)", evt->getBssid());
San Mehat3aff2d12009-06-15 14:10:44 -0700563}
564
565void WifiController::onDisconnectedEvent(SupplicantDisconnectedEvent *evt) {
San Mehatc4a895b2009-06-23 21:10:57 -0700566 mCurrentlyConnectedNetworkId = -1;
567 mHandlers->onInterfaceDisconnected(this);
San Mehat3aff2d12009-06-15 14:10:44 -0700568}
569
570#if 0
571void WifiController::onTerminatingEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000572 ALOGD("onTerminatingEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700573}
574
575void WifiController::onPasswordChangedEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000576 ALOGD("onPasswordChangedEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700577}
578
579void WifiController::onEapNotificationEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000580 ALOGD("onEapNotificationEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700581}
582
583void WifiController::onEapStartedEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000584 ALOGD("onEapStartedEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700585}
586
587void WifiController::onEapMethodEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000588 ALOGD("onEapMethodEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700589}
590
591void WifiController::onEapSuccessEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000592 ALOGD("onEapSuccessEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700593}
594
595void WifiController::onEapFailureEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000596 ALOGD("onEapFailureEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700597}
598
599void WifiController::onLinkSpeedEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000600 ALOGD("onLinkSpeedEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700601}
602
603void WifiController::onDriverStateEvent(SupplicantEvent *evt) {
Steve Block8d66c492011-12-20 16:07:45 +0000604 ALOGD("onDriverStateEvent(%s)", evt->getEvent());
San Mehat3aff2d12009-06-15 14:10:44 -0700605}
606#endif
San Mehatc4a895b2009-06-23 21:10:57 -0700607
608void WifiController::onStatusPollInterval() {
609 pthread_mutex_lock(&mLock);
610 int rssi;
611 if (mSupplicant->getRssi(&rssi)) {
Steve Block01dda202012-01-06 14:13:42 +0000612 ALOGE("Failed to get rssi (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -0700613 pthread_mutex_unlock(&mLock);
614 return;
615 }
616
617 if (abs(mLastRssi - rssi) > mRssiEventThreshold) {
618 char *tmp3;
619 asprintf(&tmp3, "RSSI changed from %d -> %d",
620 mLastRssi, rssi);
621 mLastRssi = rssi;
622 NetworkManager::Instance()->getBroadcaster()->
623 sendBroadcast(ResponseCode::RssiChange,
624 tmp3, false);
625 free(tmp3);
626 }
627
628 int linkspeed = mSupplicant->getLinkSpeed();
629 if (linkspeed != mLastLinkSpeed) {
630 char *tmp3;
631 asprintf(&tmp3, "Link speed changed from %d -> %d",
632 mLastLinkSpeed, linkspeed);
633 mLastLinkSpeed = linkspeed;
634 NetworkManager::Instance()->getBroadcaster()->
635 sendBroadcast(ResponseCode::LinkSpeedChange,
636 tmp3, false);
637 free(tmp3);
638
639 }
640 pthread_mutex_unlock(&mLock);
641}
642
643int WifiController::verifyNotSuspended() {
644 if (mSuspended) {
645 errno = ESHUTDOWN;
646 return -1;
647 }
648 return 0;
649}
650
651/*
652 * Property inner classes
653 */
654
655WifiController::WifiIntegerProperty::WifiIntegerProperty(WifiController *c,
656 const char *name,
657 bool ro,
658 int elements) :
659 IntegerProperty(name, ro, elements) {
660 mWc = c;
661}
662
663WifiController::WifiStringProperty::WifiStringProperty(WifiController *c,
664 const char *name,
665 bool ro, int elements) :
666 StringProperty(name, ro, elements) {
667 mWc = c;
668}
669
670WifiController::WifiEnabledProperty::WifiEnabledProperty(WifiController *c) :
671 WifiIntegerProperty(c, "Enabled", false, 1) {
672}
673
674int WifiController::WifiEnabledProperty::get(int idx, int *buffer) {
675 *buffer = mWc->mEnabled;
676 return 0;
677}
678int WifiController::WifiEnabledProperty::set(int idx, int value) {
679 int rc = (value ? mWc->enable() : mWc->disable());
680 if (!rc)
681 mWc->mEnabled = value;
682 return rc;
683}
684
685WifiController::WifiScanOnlyProperty::WifiScanOnlyProperty(WifiController *c) :
686 WifiIntegerProperty(c, "ScanOnly", false, 1) {
687}
688int WifiController::WifiScanOnlyProperty::get(int idx, int *buffer) {
689 *buffer = mWc->mScanOnly;
690 return 0;
691}
692int WifiController::WifiScanOnlyProperty::set(int idx, int value) {
693 return mWc->setScanOnly(value == 1);
694}
695
696WifiController::WifiAllowedChannelsProperty::WifiAllowedChannelsProperty(WifiController *c) :
697 WifiIntegerProperty(c, "AllowedChannels", false, 1) {
698}
699int WifiController::WifiAllowedChannelsProperty::get(int idx, int *buffer) {
700 *buffer = mWc->mNumAllowedChannels;
701 return 0;
702}
703int WifiController::WifiAllowedChannelsProperty::set(int idx, int value) {
704 // XXX: IMPL
705 errno = ENOSYS;
706 return -1;
707}
708
709WifiController::WifiSupplicantStateProperty::WifiSupplicantStateProperty(WifiController *c) :
710 WifiStringProperty(c, "SupplicantState", true, 1) {
711}
712int WifiController::WifiSupplicantStateProperty::get(int idx, char *buffer, size_t max) {
713 if (!SupplicantState::toString(mWc->mSupplicantState, buffer, max))
714 return -1;
715 return 0;
716}
717
718WifiController::WifiActiveScanProperty::WifiActiveScanProperty(WifiController *c) :
719 WifiIntegerProperty(c, "ActiveScan", false, 1) {
720}
721int WifiController::WifiActiveScanProperty::get(int idx, int *buffer) {
722 *buffer = mWc->mActiveScan;
723 return 0;
724}
725int WifiController::WifiActiveScanProperty::set(int idx, int value) {
726 return mWc->setActiveScan(value);
727}
728
729WifiController::WifiInterfaceProperty::WifiInterfaceProperty(WifiController *c) :
730 WifiStringProperty(c, "Interface", true, 1) {
731}
732int WifiController::WifiInterfaceProperty::get(int idx, char *buffer, size_t max) {
733 strncpy(buffer, (mWc->getBoundInterface() ? mWc->getBoundInterface() : "none"), max);
734 return 0;
735}
736
737WifiController::WifiSearchingProperty::WifiSearchingProperty(WifiController *c) :
738 WifiIntegerProperty(c, "Searching", true, 1) {
739}
740int WifiController::WifiSearchingProperty::get(int idx, int *buffer) {
741 *buffer = mWc->mIsSupplicantSearching;
742 return 0;
743}
744
745WifiController::WifiPacketFilterProperty::WifiPacketFilterProperty(WifiController *c) :
746 WifiIntegerProperty(c, "PacketFilter", false, 1) {
747}
748int WifiController::WifiPacketFilterProperty::get(int idx, int *buffer) {
749 *buffer = mWc->mPacketFilter;
750 return 0;
751}
752int WifiController::WifiPacketFilterProperty::set(int idx, int value) {
753 return mWc->setPacketFilter(value);
754}
755
756WifiController::WifiBluetoothCoexScanProperty::WifiBluetoothCoexScanProperty(WifiController *c) :
757 WifiIntegerProperty(c, "BluetoothCoexScan", false, 1) {
758}
759int WifiController::WifiBluetoothCoexScanProperty::get(int idx, int *buffer) {
760 *buffer = mWc->mBluetoothCoexScan;
761 return 0;
762}
763int WifiController::WifiBluetoothCoexScanProperty::set(int idx, int value) {
764 return mWc->setBluetoothCoexistenceScan(value == 1);
765}
766
767WifiController::WifiBluetoothCoexModeProperty::WifiBluetoothCoexModeProperty(WifiController *c) :
768 WifiIntegerProperty(c, "BluetoothCoexMode", false, 1) {
769}
770int WifiController::WifiBluetoothCoexModeProperty::get(int idx, int *buffer) {
771 *buffer = mWc->mBluetoothCoexMode;
772 return 0;
773}
774int WifiController::WifiBluetoothCoexModeProperty::set(int idx, int value) {
775 return mWc->setBluetoothCoexistenceMode(value);
776}
777
778WifiController::WifiCurrentNetworkProperty::WifiCurrentNetworkProperty(WifiController *c) :
779 WifiIntegerProperty(c, "CurrentlyConnectedNetworkId", true, 1) {
780}
781int WifiController::WifiCurrentNetworkProperty::get(int idx, int *buffer) {
782 *buffer = mWc->mCurrentlyConnectedNetworkId;
783 return 0;
784}
785
786WifiController::WifiSuspendedProperty::WifiSuspendedProperty(WifiController *c) :
787 WifiIntegerProperty(c, "Suspended", false, 1) {
788}
789int WifiController::WifiSuspendedProperty::get(int idx, int *buffer) {
790 *buffer = mWc->getSuspended();
791 return 0;
792}
793int WifiController::WifiSuspendedProperty::set(int idx, int value) {
794 return mWc->setSuspend(value == 1);
795}
796
797WifiController::WifiNetCountProperty::WifiNetCountProperty(WifiController *c) :
798 WifiIntegerProperty(c, "NetCount", true, 1) {
799}
800int WifiController::WifiNetCountProperty::get(int idx, int *buffer) {
801 pthread_mutex_lock(&mWc->mLock);
802 *buffer = mWc->mSupplicant->getNetworkCount();
803 pthread_mutex_unlock(&mWc->mLock);
804 return 0;
805}
806
807WifiController::WifiTriggerScanProperty::WifiTriggerScanProperty(WifiController *c) :
808 WifiIntegerProperty(c, "TriggerScan", false, 1) {
809}
810int WifiController::WifiTriggerScanProperty::get(int idx, int *buffer) {
811 // XXX: Need action type
812 *buffer = 0;
813 return 0;
814}
815
816int WifiController::WifiTriggerScanProperty::set(int idx, int value) {
817 return mWc->triggerScan();
818}
819