blob: 806141cf8cfe7efac638605e3a592f3dfefedd4b [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 Mehat1441e762009-05-07 11:37:10 -070096void WifiController::sendStatusBroadcast(const char *msg) {
97 char tmp[255];
San Mehatdc266072009-05-06 11:16:52 -070098
San Mehat1441e762009-05-07 11:37:10 -070099 sprintf(tmp, "WIFI_STATUS:%s", msg);
100 NetworkManager::Instance()->getBroadcaster()->sendBroadcast(tmp);
101}
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 Mehat1441e762009-05-07 11:37:10 -0700144 mScanner->stopPeriodicScan();
San Mehatdc266072009-05-06 11:16:52 -0700145 } else if (mode & SCAN_REPEAT_MASK)
San Mehat1441e762009-05-07 11:37:10 -0700146 rc = mScanner->startPeriodicScan(mode & SCAN_ACTIVE_MASK);
San Mehatdc266072009-05-06 11:16:52 -0700147 else
148 rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
149
150 return rc;
151}
152
San Mehat1441e762009-05-07 11:37:10 -0700153ScanResultCollection *WifiController::createScanResults() {
154 return mSupplicant->createLatestScanResults();
San Mehatdc266072009-05-06 11:16:52 -0700155}