blob: f67761ab1778ef4054a4e0a9e44b15680420f76e [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 */
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 Mehat1441e762009-05-07 11:37:10 -070024#include "WifiScanner.h"
25#include "NetworkManager.h"
San Mehat8d3fc3f2009-05-12 14:36:32 -070026#include "ErrorCode.h";
San Mehatdc266072009-05-06 11:16:52 -070027
28WifiController::WifiController(char *modpath, char *modname, char *modargs) :
29 Controller("WIFI") {
30 strncpy(mModulePath, modpath, sizeof(mModulePath));
31 strncpy(mModuleName, modname, sizeof(mModuleName));
32 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
33
34 mSupplicant = new Supplicant();
San Mehat1441e762009-05-07 11:37:10 -070035 mScanner = new WifiScanner(mSupplicant, 10);
San Mehatdc266072009-05-06 11:16:52 -070036 mCurrentScanMode = 0;
37}
38
39int WifiController::start() {
40 return 0;
41}
42
43int WifiController::stop() {
44 errno = ENOSYS;
45 return -1;
46}
47
48int WifiController::enable() {
San Mehat1441e762009-05-07 11:37:10 -070049 if (!isPoweredUp()) {
50 sendStatusBroadcast("POWERING_UP");
51 if (powerUp()) {
52 LOGE("Powerup failed (%s)", strerror(errno));
53 return -1;
54 }
San Mehatdc266072009-05-06 11:16:52 -070055 }
San Mehat1441e762009-05-07 11:37:10 -070056
San Mehatdc266072009-05-06 11:16:52 -070057 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -070058 sendStatusBroadcast("LOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -070059 if (loadKernelModule(mModulePath, mModuleArgs)) {
60 LOGE("Kernel module load failed (%s)", strerror(errno));
61 goto out_powerdown;
62 }
63 }
64
San Mehat1441e762009-05-07 11:37:10 -070065 if (!isFirmwareLoaded()) {
66 sendStatusBroadcast("LOADING_FIRMWARE");
67 if (loadFirmware()) {
68 LOGE("Firmware load failed (%s)", strerror(errno));
69 goto out_powerdown;
70 }
San Mehatdc266072009-05-06 11:16:52 -070071 }
72
San Mehat1441e762009-05-07 11:37:10 -070073 if (!mSupplicant->isStarted()) {
74 sendStatusBroadcast("STARTING_SUPPLICANT");
75 if (mSupplicant->start()) {
76 LOGE("Supplicant start failed (%s)", strerror(errno));
77 goto out_unloadmodule;
78 }
San Mehatdc266072009-05-06 11:16:52 -070079 }
80
81 return 0;
82
83out_unloadmodule:
84 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
85 if (unloadKernelModule(mModuleName)) {
86 LOGE("Unable to unload module after failure!");
87 }
88 }
89
90out_powerdown:
91 if (powerDown()) {
92 LOGE("Unable to powerdown after failure!");
93 }
94 return -1;
95}
96
San Mehat69772dc2009-05-10 09:27:07 -070097void WifiController::sendStatusBroadcast(char *msg) {
San Mehat8d3fc3f2009-05-12 14:36:32 -070098 NetworkManager::Instance()->
99 getBroadcaster()->
100 sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false);
San Mehat1441e762009-05-07 11:37:10 -0700101}
102
103int WifiController::disable() {
104
105 if (mSupplicant->isStarted()) {
106 sendStatusBroadcast("STOPPING_SUPPLICANT");
107 if (mSupplicant->stop()) {
108 LOGE("Supplicant stop failed (%s)", strerror(errno));
109 return -1;
110 }
111 } else
112 LOGW("disable(): Supplicant not running?");
San Mehatdc266072009-05-06 11:16:52 -0700113
114 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -0700115 sendStatusBroadcast("UNLOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -0700116 if (unloadKernelModule(mModuleName)) {
117 LOGE("Unable to unload module (%s)", strerror(errno));
118 return -1;
119 }
120 }
121
San Mehat1441e762009-05-07 11:37:10 -0700122 if (isPoweredUp()) {
123 sendStatusBroadcast("POWERING_DOWN");
124 if (powerDown()) {
125 LOGE("Powerdown failed (%s)", strerror(errno));
126 return -1;
127 }
San Mehatdc266072009-05-06 11:16:52 -0700128 }
129 return 0;
130}
131
132int WifiController::loadFirmware() {
133 return 0;
134}
135
San Mehat1441e762009-05-07 11:37:10 -0700136int WifiController::setScanMode(uint32_t mode) {
San Mehatdc266072009-05-06 11:16:52 -0700137 int rc = 0;
138
139 if (mCurrentScanMode == mode)
140 return 0;
141
142 if (!(mode & SCAN_ENABLE_MASK)) {
143 if (mCurrentScanMode & SCAN_REPEAT_MASK)
San Mehate67651c2009-05-12 15:50:49 -0700144 mScanner->stop();
San Mehatdc266072009-05-06 11:16:52 -0700145 } else if (mode & SCAN_REPEAT_MASK)
San Mehate67651c2009-05-12 15:50:49 -0700146 rc = mScanner->start(mode & SCAN_ACTIVE_MASK);
San Mehatdc266072009-05-06 11:16:52 -0700147 else
148 rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
San Mehate67651c2009-05-12 15:50:49 -0700149
150 mCurrentScanMode = mode;
San Mehatdc266072009-05-06 11:16:52 -0700151 return rc;
152}
153
San Mehat1441e762009-05-07 11:37:10 -0700154ScanResultCollection *WifiController::createScanResults() {
155 return mSupplicant->createLatestScanResults();
San Mehatdc266072009-05-06 11:16:52 -0700156}