blob: cba00ec30732bfd2ac9c0c59908ea867a3079962 [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 Mehatdc266072009-05-06 11:16:52 -070026
27WifiController::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 Mehat1441e762009-05-07 11:37:10 -070034 mScanner = new WifiScanner(mSupplicant, 10);
San Mehatdc266072009-05-06 11:16:52 -070035 mCurrentScanMode = 0;
36}
37
38int WifiController::start() {
39 return 0;
40}
41
42int WifiController::stop() {
43 errno = ENOSYS;
44 return -1;
45}
46
47int WifiController::enable() {
San Mehat1441e762009-05-07 11:37:10 -070048 if (!isPoweredUp()) {
49 sendStatusBroadcast("POWERING_UP");
50 if (powerUp()) {
51 LOGE("Powerup failed (%s)", strerror(errno));
52 return -1;
53 }
San Mehatdc266072009-05-06 11:16:52 -070054 }
San Mehat1441e762009-05-07 11:37:10 -070055
San Mehatdc266072009-05-06 11:16:52 -070056 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -070057 sendStatusBroadcast("LOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -070058 if (loadKernelModule(mModulePath, mModuleArgs)) {
59 LOGE("Kernel module load failed (%s)", strerror(errno));
60 goto out_powerdown;
61 }
62 }
63
San Mehat1441e762009-05-07 11:37:10 -070064 if (!isFirmwareLoaded()) {
65 sendStatusBroadcast("LOADING_FIRMWARE");
66 if (loadFirmware()) {
67 LOGE("Firmware load failed (%s)", strerror(errno));
68 goto out_powerdown;
69 }
San Mehatdc266072009-05-06 11:16:52 -070070 }
71
San Mehat1441e762009-05-07 11:37:10 -070072 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 Mehatdc266072009-05-06 11:16:52 -070078 }
79
80 return 0;
81
82out_unloadmodule:
83 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
84 if (unloadKernelModule(mModuleName)) {
85 LOGE("Unable to unload module after failure!");
86 }
87 }
88
89out_powerdown:
90 if (powerDown()) {
91 LOGE("Unable to powerdown after failure!");
92 }
93 return -1;
94}
95
San Mehat69772dc2009-05-10 09:27:07 -070096void WifiController::sendStatusBroadcast(char *msg) {
97 NetworkManager::Instance()->getBroadcaster()->sendBroadcast(600, msg, false);
San Mehat1441e762009-05-07 11:37:10 -070098}
99
100int 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 Mehatdc266072009-05-06 11:16:52 -0700110
111 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
San Mehat1441e762009-05-07 11:37:10 -0700112 sendStatusBroadcast("UNLOADING_DRIVER");
San Mehatdc266072009-05-06 11:16:52 -0700113 if (unloadKernelModule(mModuleName)) {
114 LOGE("Unable to unload module (%s)", strerror(errno));
115 return -1;
116 }
117 }
118
San Mehat1441e762009-05-07 11:37:10 -0700119 if (isPoweredUp()) {
120 sendStatusBroadcast("POWERING_DOWN");
121 if (powerDown()) {
122 LOGE("Powerdown failed (%s)", strerror(errno));
123 return -1;
124 }
San Mehatdc266072009-05-06 11:16:52 -0700125 }
126 return 0;
127}
128
129int WifiController::loadFirmware() {
130 return 0;
131}
132
San Mehat1441e762009-05-07 11:37:10 -0700133int WifiController::setScanMode(uint32_t mode) {
San Mehatdc266072009-05-06 11:16:52 -0700134 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 Mehat1441e762009-05-07 11:37:10 -0700141 mScanner->stopPeriodicScan();
San Mehatdc266072009-05-06 11:16:52 -0700142 } else if (mode & SCAN_REPEAT_MASK)
San Mehat1441e762009-05-07 11:37:10 -0700143 rc = mScanner->startPeriodicScan(mode & SCAN_ACTIVE_MASK);
San Mehatdc266072009-05-06 11:16:52 -0700144 else
145 rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
146
147 return rc;
148}
149
San Mehat1441e762009-05-07 11:37:10 -0700150ScanResultCollection *WifiController::createScanResults() {
151 return mSupplicant->createLatestScanResults();
San Mehatdc266072009-05-06 11:16:52 -0700152}