summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndroid (Google) Code Review2009-06-15 16:40:51 -0500
committerAndroid (Google) Code Review2009-06-15 16:40:51 -0500
commit43c16197b2a6da3a3125b4a4a9fa6b70f447043e (patch)
tree4f6edca731c7949deb59b76682eea7ffaa85b3f6
parent2d0a6ca27c6f75eb0ec881fe4ad0b4f478d941c0 (diff)
parent3aff2d1de59972684bf2ab798351be5544158239 (diff)
downloadplatform-system-core-43c16197b2a6da3a3125b4a4a9fa6b70f447043e.tar.gz
platform-system-core-43c16197b2a6da3a3125b4a4a9fa6b70f447043e.tar.xz
platform-system-core-43c16197b2a6da3a3125b4a4a9fa6b70f447043e.zip
Merge change 4228 into donut
* changes: Nexus: Clean up supplicant events, protocol, and continue plumbing
-rw-r--r--nexus/Android.mk49
-rw-r--r--nexus/CommandListener.cpp60
-rw-r--r--nexus/CommandListener.h16
-rw-r--r--nexus/Controller.cpp4
-rw-r--r--nexus/Controller.h6
-rw-r--r--nexus/IControllerHandler.h30
-rw-r--r--nexus/ISupplicantEventHandler.h39
-rw-r--r--nexus/LoopController.cpp5
-rw-r--r--nexus/LoopController.h3
-rw-r--r--nexus/NetworkManager.cpp17
-rw-r--r--nexus/NetworkManager.h17
-rw-r--r--nexus/OpenVpnController.cpp9
-rw-r--r--nexus/OpenVpnController.h3
-rw-r--r--nexus/PropertyManager.cpp3
-rw-r--r--nexus/Supplicant.cpp280
-rw-r--r--nexus/Supplicant.h43
-rw-r--r--nexus/SupplicantAssociatedEvent.cpp40
-rw-r--r--nexus/SupplicantAssociatedEvent.h34
-rw-r--r--nexus/SupplicantAssociatingEvent.cpp99
-rw-r--r--nexus/SupplicantAssociatingEvent.h37
-rw-r--r--nexus/SupplicantConnectedEvent.cpp61
-rw-r--r--nexus/SupplicantConnectedEvent.h36
-rw-r--r--nexus/SupplicantConnectionTimeoutEvent.cpp36
-rw-r--r--nexus/SupplicantConnectionTimeoutEvent.h34
-rw-r--r--nexus/SupplicantDisconnectedEvent.cpp33
-rw-r--r--nexus/SupplicantDisconnectedEvent.h30
-rw-r--r--nexus/SupplicantEvent.cpp73
-rw-r--r--nexus/SupplicantEvent.h39
-rw-r--r--nexus/SupplicantEventFactory.cpp119
-rw-r--r--nexus/SupplicantEventFactory.h31
-rw-r--r--nexus/SupplicantListener.cpp67
-rw-r--r--nexus/SupplicantListener.h5
-rw-r--r--nexus/SupplicantScanResultsEvent.cpp34
-rw-r--r--nexus/SupplicantScanResultsEvent.h30
-rw-r--r--nexus/SupplicantStateChangeEvent.cpp45
-rw-r--r--nexus/SupplicantStateChangeEvent.h34
-rw-r--r--nexus/SupplicantStatus.cpp72
-rw-r--r--nexus/SupplicantStatus.h42
-rw-r--r--nexus/TiwlanWifiController.cpp8
-rw-r--r--nexus/TiwlanWifiController.h4
-rw-r--r--nexus/VpnController.cpp16
-rw-r--r--nexus/VpnController.h4
-rw-r--r--nexus/WifiController.cpp212
-rw-r--r--nexus/WifiController.h45
-rw-r--r--nexus/WifiNetwork.cpp54
-rw-r--r--nexus/WifiNetwork.h6
-rw-r--r--nexus/main.cpp8
47 files changed, 1471 insertions, 501 deletions
diff --git a/nexus/Android.mk b/nexus/Android.mk
index bd4e3d4ea..6172e62f7 100644
--- a/nexus/Android.mk
+++ b/nexus/Android.mk
@@ -5,26 +5,35 @@ LOCAL_PATH:= $(call my-dir)
5 5
6include $(CLEAR_VARS) 6include $(CLEAR_VARS)
7 7
8LOCAL_SRC_FILES:= \ 8LOCAL_SRC_FILES:= \
9 main.cpp \ 9 main.cpp \
10 NetworkManager.cpp \ 10 NetworkManager.cpp \
11 CommandListener.cpp \ 11 CommandListener.cpp \
12 Controller.cpp \ 12 Controller.cpp \
13 WifiController.cpp \ 13 WifiController.cpp \
14 LoopController.cpp \ 14 LoopController.cpp \
15 NexusCommand.cpp \ 15 NexusCommand.cpp \
16 TiwlanWifiController.cpp \ 16 TiwlanWifiController.cpp \
17 Supplicant.cpp \ 17 Supplicant.cpp \
18 SupplicantEvent.cpp \ 18 SupplicantEvent.cpp \
19 SupplicantListener.cpp \ 19 SupplicantListener.cpp \
20 VpnController.cpp \ 20 VpnController.cpp \
21 ScanResult.cpp \ 21 ScanResult.cpp \
22 WifiScanner.cpp \ 22 WifiScanner.cpp \
23 WifiNetwork.cpp \ 23 WifiNetwork.cpp \
24 OpenVpnController.cpp \ 24 OpenVpnController.cpp \
25 InterfaceConfig.cpp \ 25 InterfaceConfig.cpp \
26 PropertyManager.cpp \ 26 PropertyManager.cpp \
27 SupplicantState.cpp 27 SupplicantState.cpp \
28 SupplicantEventFactory.cpp \
29 SupplicantConnectedEvent.cpp \
30 SupplicantAssociatingEvent.cpp \
31 SupplicantAssociatedEvent.cpp \
32 SupplicantStateChangeEvent.cpp \
33 SupplicantScanResultsEvent.cpp \
34 SupplicantConnectionTimeoutEvent.cpp \
35 SupplicantDisconnectedEvent.cpp \
36 SupplicantStatus.cpp
28 37
29LOCAL_MODULE:= nexus 38LOCAL_MODULE:= nexus
30 39
diff --git a/nexus/CommandListener.cpp b/nexus/CommandListener.cpp
index e8de7f585..8eb378bcd 100644
--- a/nexus/CommandListener.cpp
+++ b/nexus/CommandListener.cpp
@@ -53,7 +53,8 @@ CommandListener::WifiCreateNetworkCmd::WifiCreateNetworkCmd() :
53 NexusCommand("wifi_create_network") { 53 NexusCommand("wifi_create_network") {
54} 54}
55 55
56int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli, char *data) { 56int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli,
57 int argc, char **argv) {
57 NetworkManager *nm = NetworkManager::Instance(); 58 NetworkManager *nm = NetworkManager::Instance();
58 WifiController *wc = (WifiController *) nm->findController("WIFI"); 59 WifiController *wc = (WifiController *) nm->findController("WIFI");
59 WifiNetwork *wn; 60 WifiNetwork *wn;
@@ -72,11 +73,12 @@ CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() :
72 NexusCommand("wifi_remove_network") { 73 NexusCommand("wifi_remove_network") {
73} 74}
74 75
75int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *data) { 76int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli,
77 int argc, char **argv) {
76 NetworkManager *nm = NetworkManager::Instance(); 78 NetworkManager *nm = NetworkManager::Instance();
77 WifiController *wc = (WifiController *) nm->findController("WIFI"); 79 WifiController *wc = (WifiController *) nm->findController("WIFI");
78 80
79 if (wc->removeNetwork(atoi(data))) 81 if (wc->removeNetwork(atoi(argv[1])))
80 cli->sendMsg(ErrorCode::OperationFailed, "Failed to remove network", true); 82 cli->sendMsg(ErrorCode::OperationFailed, "Failed to remove network", true);
81 else { 83 else {
82 cli->sendMsg(ErrorCode::CommandOkay, "Network removed.", false); 84 cli->sendMsg(ErrorCode::CommandOkay, "Network removed.", false);
@@ -88,7 +90,8 @@ CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
88 NexusCommand("wifi_scan_results") { 90 NexusCommand("wifi_scan_results") {
89} 91}
90 92
91int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) { 93int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli,
94 int argc, char **argv) {
92 NetworkManager *nm = NetworkManager::Instance(); 95 NetworkManager *nm = NetworkManager::Instance();
93 WifiController *wc = (WifiController *) nm->findController("WIFI"); 96 WifiController *wc = (WifiController *) nm->findController("WIFI");
94 97
@@ -114,7 +117,8 @@ CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() :
114 NexusCommand("wifi_list_networks") { 117 NexusCommand("wifi_list_networks") {
115} 118}
116 119
117int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *data) { 120int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli,
121 int argc, char **argv) {
118 NetworkManager *nm = NetworkManager::Instance(); 122 NetworkManager *nm = NetworkManager::Instance();
119 WifiController *wc = (WifiController *) nm->findController("WIFI"); 123 WifiController *wc = (WifiController *) nm->findController("WIFI");
120 124
@@ -144,23 +148,19 @@ CommandListener::GetCmd::GetCmd() :
144 NexusCommand("get") { 148 NexusCommand("get") {
145} 149}
146 150
147int CommandListener::GetCmd::runCommand(SocketClient *cli, char *data) { 151int CommandListener::GetCmd::runCommand(SocketClient *cli, int argc, char **argv) {
148 char *next = data; 152 char val[Property::ValueMaxSize];
149 char *propname;
150 153
151 if (!(propname = strsep(&next, ":"))) 154 if (!NetworkManager::Instance()->getPropMngr()->get(argv[1],
152 goto out_inval; 155 val,
153 156 sizeof(val))) {
154 char pb[Property::NameMaxSize + 6];
155 snprintf(pb, sizeof(pb), "%s:", propname);
156
157 if (!NetworkManager::Instance()->getPropMngr()->get(propname,
158 &pb[strlen(pb)],
159 sizeof(pb) - strlen(pb))) {
160 goto out_inval; 157 goto out_inval;
161 } 158 }
162 159
163 cli->sendMsg(ErrorCode::PropertyRead, pb, false); 160 char *tmp;
161 asprintf(&tmp, "%s %s", argv[1], val);
162 cli->sendMsg(ErrorCode::PropertyRead, tmp, false);
163 free(tmp);
164 164
165 cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false); 165 cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false);
166 return 0; 166 return 0;
@@ -174,23 +174,9 @@ CommandListener::SetCmd::SetCmd() :
174 NexusCommand("set") { 174 NexusCommand("set") {
175} 175}
176 176
177int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) { 177int CommandListener::SetCmd::runCommand(SocketClient *cli, int argc,
178 char *bword; 178 char **argv) {
179 char *last; 179 if (NetworkManager::Instance()->getPropMngr()->set(argv[1], argv[2]))
180 char propname[Property::NameMaxSize];
181 char propval[Property::ValueMaxSize];
182
183 if (!(bword = strtok_r(data, ":", &last)))
184 goto out_inval;
185
186 strncpy(propname, bword, sizeof(propname));
187
188 if (!(bword = strtok_r(NULL, ":", &last)))
189 goto out_inval;
190
191 strncpy(propval, bword, sizeof(propval));
192
193 if (NetworkManager::Instance()->getPropMngr()->set(propname, propval))
194 goto out_inval; 180 goto out_inval;
195 181
196 cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false); 182 cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false);
@@ -206,7 +192,7 @@ CommandListener::ListCmd::ListCmd() :
206 NexusCommand("list") { 192 NexusCommand("list") {
207} 193}
208 194
209int CommandListener::ListCmd::runCommand(SocketClient *cli, char *data) { 195int CommandListener::ListCmd::runCommand(SocketClient *cli, int argc, char **argv) {
210 android::List<char *> *pc; 196 android::List<char *> *pc;
211 197
212 if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList())) { 198 if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList())) {
@@ -227,7 +213,7 @@ int CommandListener::ListCmd::runCommand(SocketClient *cli, char *data) {
227 } 213 }
228 214
229 char *buf; 215 char *buf;
230 if (asprintf(&buf, "%s:%s", (*it), p_v) < 0) { 216 if (asprintf(&buf, "%s %s", (*it), p_v) < 0) {
231 LOGE("Failed to allocate memory"); 217 LOGE("Failed to allocate memory");
232 free((*it)); 218 free((*it));
233 continue; 219 continue;
diff --git a/nexus/CommandListener.h b/nexus/CommandListener.h
index b57c25ffb..30c6dc054 100644
--- a/nexus/CommandListener.h
+++ b/nexus/CommandListener.h
@@ -31,56 +31,56 @@ private:
31 public: 31 public:
32 WifiScanCmd(); 32 WifiScanCmd();
33 virtual ~WifiScanCmd() {} 33 virtual ~WifiScanCmd() {}
34 int runCommand(SocketClient *c, char *data); 34 int runCommand(SocketClient *c, int argc, char ** argv);
35 }; 35 };
36 36
37 class WifiScanResultsCmd : public NexusCommand { 37 class WifiScanResultsCmd : public NexusCommand {
38 public: 38 public:
39 WifiScanResultsCmd(); 39 WifiScanResultsCmd();
40 virtual ~WifiScanResultsCmd() {} 40 virtual ~WifiScanResultsCmd() {}
41 int runCommand(SocketClient *c, char *data); 41 int runCommand(SocketClient *c, int argc, char ** argv);
42 }; 42 };
43 43
44 class WifiCreateNetworkCmd : public NexusCommand { 44 class WifiCreateNetworkCmd : public NexusCommand {
45 public: 45 public:
46 WifiCreateNetworkCmd(); 46 WifiCreateNetworkCmd();
47 virtual ~WifiCreateNetworkCmd() {} 47 virtual ~WifiCreateNetworkCmd() {}
48 int runCommand(SocketClient *c, char *data); 48 int runCommand(SocketClient *c, int argc, char ** argv);
49 }; 49 };
50 50
51 class WifiRemoveNetworkCmd : public NexusCommand { 51 class WifiRemoveNetworkCmd : public NexusCommand {
52 public: 52 public:
53 WifiRemoveNetworkCmd(); 53 WifiRemoveNetworkCmd();
54 virtual ~WifiRemoveNetworkCmd() {} 54 virtual ~WifiRemoveNetworkCmd() {}
55 int runCommand(SocketClient *c, char *data); 55 int runCommand(SocketClient *c, int argc, char ** argv);
56 }; 56 };
57 57
58 class WifiListNetworksCmd : public NexusCommand { 58 class WifiListNetworksCmd : public NexusCommand {
59 public: 59 public:
60 WifiListNetworksCmd(); 60 WifiListNetworksCmd();
61 virtual ~WifiListNetworksCmd() {} 61 virtual ~WifiListNetworksCmd() {}
62 int runCommand(SocketClient *c, char *data); 62 int runCommand(SocketClient *c, int argc, char ** argv);
63 }; 63 };
64 64
65 class SetCmd : public NexusCommand { 65 class SetCmd : public NexusCommand {
66 public: 66 public:
67 SetCmd(); 67 SetCmd();
68 virtual ~SetCmd() {} 68 virtual ~SetCmd() {}
69 int runCommand(SocketClient *c, char *data); 69 int runCommand(SocketClient *c, int argc, char ** argv);
70 }; 70 };
71 71
72 class GetCmd : public NexusCommand { 72 class GetCmd : public NexusCommand {
73 public: 73 public:
74 GetCmd(); 74 GetCmd();
75 virtual ~GetCmd() {} 75 virtual ~GetCmd() {}
76 int runCommand(SocketClient *c, char *data); 76 int runCommand(SocketClient *c, int argc, char ** argv);
77 }; 77 };
78 78
79 class ListCmd : public NexusCommand { 79 class ListCmd : public NexusCommand {
80 public: 80 public:
81 ListCmd(); 81 ListCmd();
82 virtual ~ListCmd() {} 82 virtual ~ListCmd() {}
83 int runCommand(SocketClient *c, char *data); 83 int runCommand(SocketClient *c, int argc, char ** argv);
84 }; 84 };
85}; 85};
86 86
diff --git a/nexus/Controller.cpp b/nexus/Controller.cpp
index 9d4ff3c65..17fb51939 100644
--- a/nexus/Controller.cpp
+++ b/nexus/Controller.cpp
@@ -34,9 +34,11 @@
34extern "C" int init_module(void *, unsigned int, const char *); 34extern "C" int init_module(void *, unsigned int, const char *);
35extern "C" int delete_module(const char *, unsigned int); 35extern "C" int delete_module(const char *, unsigned int);
36 36
37Controller::Controller(const char *name, PropertyManager *propMngr) { 37Controller::Controller(const char *name, PropertyManager *propMngr,
38 IControllerHandler *handlers) {
38 mPropMngr = propMngr; 39 mPropMngr = propMngr;
39 mName = strdup(name); 40 mName = strdup(name);
41 mHandlers = handlers;
40 mBoundInterface = NULL; 42 mBoundInterface = NULL;
41} 43}
42 44
diff --git a/nexus/Controller.h b/nexus/Controller.h
index 9137f9a86..af03d2e59 100644
--- a/nexus/Controller.h
+++ b/nexus/Controller.h
@@ -23,12 +23,12 @@
23#include <utils/List.h> 23#include <utils/List.h>
24 24
25class PropertyManager; 25class PropertyManager;
26class IControllerHandler;
26 27
27#include "PropertyManager.h" 28#include "PropertyManager.h"
28#include "IPropertyProvider.h" 29#include "IPropertyProvider.h"
29 30
30class Controller : public IPropertyProvider { 31class Controller : public IPropertyProvider {
31private:
32 /* 32 /*
33 * Name of this controller - WIFI/VPN/USBNET/BTNET/BTDUN/LOOP/etc 33 * Name of this controller - WIFI/VPN/USBNET/BTNET/BTDUN/LOOP/etc
34 */ 34 */
@@ -42,9 +42,11 @@ private:
42 42
43protected: 43protected:
44 PropertyManager *mPropMngr; 44 PropertyManager *mPropMngr;
45 IControllerHandler *mHandlers;
45 46
46public: 47public:
47 Controller(const char *name, PropertyManager *propMngr); 48 Controller(const char *name, PropertyManager *propMngr,
49 IControllerHandler *handlers);
48 virtual ~Controller(); 50 virtual ~Controller();
49 51
50 virtual int start(); 52 virtual int start();
diff --git a/nexus/IControllerHandler.h b/nexus/IControllerHandler.h
new file mode 100644
index 000000000..92d015f43
--- /dev/null
+++ b/nexus/IControllerHandler.h
@@ -0,0 +1,30 @@
1/*
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
17#ifndef _ICONTROLLER_HANDLER_H
18#define _ICONTROLLER_HANDLER_H
19
20class Controller;
21class InterfaceConfig;
22
23class IControllerHandler {
24public:
25 virtual void onInterfaceStarted(Controller *c, const InterfaceConfig *cfg) = 0;
26 virtual void onInterfaceStopping(Controller *c, const char *name) = 0;
27};
28
29#endif
30
diff --git a/nexus/ISupplicantEventHandler.h b/nexus/ISupplicantEventHandler.h
index 7e1bd5ab8..b7fd17b56 100644
--- a/nexus/ISupplicantEventHandler.h
+++ b/nexus/ISupplicantEventHandler.h
@@ -17,21 +17,34 @@
17#ifndef _ISUPPLICANT_EVENT_HANDLER_H 17#ifndef _ISUPPLICANT_EVENT_HANDLER_H
18#define _ISUPPLICANT_EVENT_HANDLER_H 18#define _ISUPPLICANT_EVENT_HANDLER_H
19 19
20class SupplicantAssociatingEvent;
21class SupplicantAssociatedEvent;
22class SupplicantConnectedEvent;
23class SupplicantScanResultsEvent;
24class SupplicantStateChangeEvent;
25class SupplicantConnectionTimeoutEvent;
26class SupplicantDisconnectedEvent;
27
20class ISupplicantEventHandler { 28class ISupplicantEventHandler {
21public: 29public:
22 virtual int onConnectedEvent(SupplicantEvent *evt) = 0; 30 virtual void onAssociatingEvent(SupplicantAssociatingEvent *evt) = 0;
23 virtual int onDisconnectedEvent(SupplicantEvent *evt) = 0; 31 virtual void onAssociatedEvent(SupplicantAssociatedEvent *evt) = 0;
24 virtual int onTerminatingEvent(SupplicantEvent *evt) = 0; 32 virtual void onConnectedEvent(SupplicantConnectedEvent *evt) = 0;
25 virtual int onPasswordChangedEvent(SupplicantEvent *evt) = 0; 33 virtual void onScanResultsEvent(SupplicantScanResultsEvent *evt) = 0;
26 virtual int onEapNotificationEvent(SupplicantEvent *evt) = 0; 34 virtual void onStateChangeEvent(SupplicantStateChangeEvent *evt) = 0;
27 virtual int onEapStartedEvent(SupplicantEvent *evt) = 0; 35 virtual void onConnectionTimeoutEvent(SupplicantConnectionTimeoutEvent *evt) = 0;
28 virtual int onEapMethodEvent(SupplicantEvent *evt) = 0; 36 virtual void onDisconnectedEvent(SupplicantDisconnectedEvent *evt) = 0;
29 virtual int onEapSuccessEvent(SupplicantEvent *evt) = 0; 37#if 0
30 virtual int onEapFailureEvent(SupplicantEvent *evt) = 0; 38 virtual void onTerminatingEvent(SupplicantEvent *evt) = 0;
31 virtual int onScanResultsEvent(SupplicantEvent *evt) = 0; 39 virtual void onPasswordChangedEvent(SupplicantEvent *evt) = 0;
32 virtual int onStateChangeEvent(SupplicantEvent *evt) = 0; 40 virtual void onEapNotificationEvent(SupplicantEvent *evt) = 0;
33 virtual int onLinkSpeedEvent(SupplicantEvent *evt) = 0; 41 virtual void onEapStartedEvent(SupplicantEvent *evt) = 0;
34 virtual int onDriverStateEvent(SupplicantEvent *evt) = 0; 42 virtual void onEapMethodEvent(SupplicantEvent *evt) = 0;
43 virtual void onEapSuccessEvent(SupplicantEvent *evt) = 0;
44 virtual void onEapFailureEvent(SupplicantEvent *evt) = 0;
45 virtual void onLinkSpeedEvent(SupplicantEvent *evt) = 0;
46 virtual void onDriverStateEvent(SupplicantEvent *evt) = 0;
47#endif
35}; 48};
36 49
37#endif 50#endif
diff --git a/nexus/LoopController.cpp b/nexus/LoopController.cpp
index a86202a3f..5cfb1fecb 100644
--- a/nexus/LoopController.cpp
+++ b/nexus/LoopController.cpp
@@ -19,8 +19,9 @@
19#include "LoopController.h" 19#include "LoopController.h"
20#include "PropertyManager.h" 20#include "PropertyManager.h"
21 21
22LoopController::LoopController(PropertyManager *propmngr) : 22LoopController::LoopController(PropertyManager *propmngr,
23 Controller("LOOP", propmngr) { 23 IControllerHandler *handlers) :
24 Controller("LOOP", propmngr, handlers) {
24} 25}
25 26
26int LoopController::set(const char *name, const char *value) { 27int LoopController::set(const char *name, const char *value) {
diff --git a/nexus/LoopController.h b/nexus/LoopController.h
index bb8314f22..53d16f138 100644
--- a/nexus/LoopController.h
+++ b/nexus/LoopController.h
@@ -19,10 +19,11 @@
19 19
20#include "Controller.h" 20#include "Controller.h"
21 21
22class ControllerHandler;
22 23
23class LoopController : public Controller { 24class LoopController : public Controller {
24public: 25public:
25 LoopController(PropertyManager *propmngr); 26 LoopController(PropertyManager *propmngr, IControllerHandler *h);
26 virtual ~LoopController() {} 27 virtual ~LoopController() {}
27 28
28 int set(const char *name, const char *value); 29 int set(const char *name, const char *value);
diff --git a/nexus/NetworkManager.cpp b/nexus/NetworkManager.cpp
index f4ae88fd7..2f13a408e 100644
--- a/nexus/NetworkManager.cpp
+++ b/nexus/NetworkManager.cpp
@@ -89,23 +89,26 @@ Controller *NetworkManager::findController(const char *name) {
89 return NULL; 89 return NULL;
90} 90}
91 91
92int NetworkManager::onInterfaceStart(Controller *c, const InterfaceConfig *cfg) { 92void NetworkManager::onInterfaceStarted(Controller *c, const InterfaceConfig *cfg) {
93 LOGD("Interface %s started by controller %s", c->getBoundInterface(), c->getName()); 93 LOGD("Interface %s started by controller %s", c->getBoundInterface(), c->getName());
94 94
95 // Look up the interface 95 // Look up the interface
96 96
97 if (0) { // already started? 97 if (0) { // already started?
98 errno = EADDRINUSE;
99 return -1;
100 } 98 }
101 99
102 if (cfg->getUseDhcp()) { 100 if (cfg) {
101 if (cfg->getUseDhcp()) {
102 // Launch DHCP thread
103 } else {
104 // Static configuration
105 }
103 } else { 106 } else {
107 LOGD("No InterfaceConfig for %s:%s - assuming self-managed",
108 c->getName(), c->getBoundInterface());
104 } 109 }
105 return 0;
106} 110}
107 111
108int NetworkManager::onInterfaceStop(Controller *c, const char *name) { 112void NetworkManager::onInterfaceStopping(Controller *c, const char *name) {
109 LOGD("Interface %s stopped by controller %s", name, c->getName()); 113 LOGD("Interface %s stopped by controller %s", name, c->getName());
110 return 0;
111} 114}
diff --git a/nexus/NetworkManager.h b/nexus/NetworkManager.h
index e75382d59..edc80c9b5 100644
--- a/nexus/NetworkManager.h
+++ b/nexus/NetworkManager.h
@@ -20,12 +20,12 @@
20#include <sysutils/SocketListener.h> 20#include <sysutils/SocketListener.h>
21 21
22#include "Controller.h" 22#include "Controller.h"
23
24#include "PropertyManager.h" 23#include "PropertyManager.h"
24#include "IControllerHandler.h"
25 25
26class InterfaceConfig; 26class InterfaceConfig;
27 27
28class NetworkManager { 28class NetworkManager : public IControllerHandler {
29private: 29private:
30 static NetworkManager *sInstance; 30 static NetworkManager *sInstance;
31 31
@@ -55,16 +55,7 @@ private:
55 55
56 NetworkManager(PropertyManager *propMngr); 56 NetworkManager(PropertyManager *propMngr);
57 57
58public: 58 void onInterfaceStarted(Controller *c, const InterfaceConfig *cfg);
59 /* 59 void onInterfaceStopping(Controller *c, const char *name);
60 * Called from a controller when an interface is available/ready for use.
61 * 'cfg' contains information on how this interface should be configured.
62 */
63 int onInterfaceStart(Controller *c, const InterfaceConfig *cfg);
64
65 /*
66 * Called from a controller when an interface should be shut down
67 */
68 int onInterfaceStop(Controller *c, const char *name);
69}; 60};
70#endif 61#endif
diff --git a/nexus/OpenVpnController.cpp b/nexus/OpenVpnController.cpp
index 4c144a46a..f1ea51022 100644
--- a/nexus/OpenVpnController.cpp
+++ b/nexus/OpenVpnController.cpp
@@ -30,8 +30,9 @@
30#define DAEMON_PROP_NAME "vpn.openvpn.status" 30#define DAEMON_PROP_NAME "vpn.openvpn.status"
31#define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf" 31#define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf"
32 32
33OpenVpnController::OpenVpnController(PropertyManager *propmngr) : 33OpenVpnController::OpenVpnController(PropertyManager *propmngr,
34 VpnController(propmngr) { 34 IControllerHandler *handlers) :
35 VpnController(propmngr, handlers) {
35 mServiceManager = new ServiceManager(); 36 mServiceManager = new ServiceManager();
36} 37}
37 38
@@ -40,11 +41,11 @@ OpenVpnController::~OpenVpnController() {
40} 41}
41 42
42int OpenVpnController::start() { 43int OpenVpnController::start() {
43 return 0; 44 return VpnController::start();
44} 45}
45 46
46int OpenVpnController::stop() { 47int OpenVpnController::stop() {
47 return 0; 48 return VpnController::stop();
48} 49}
49 50
50int OpenVpnController::enable() { 51int OpenVpnController::enable() {
diff --git a/nexus/OpenVpnController.h b/nexus/OpenVpnController.h
index 323c44cbf..529aab53b 100644
--- a/nexus/OpenVpnController.h
+++ b/nexus/OpenVpnController.h
@@ -21,13 +21,14 @@
21#include "VpnController.h" 21#include "VpnController.h"
22 22
23class ServiceManager; 23class ServiceManager;
24class IControllerHandler;
24 25
25class OpenVpnController : public VpnController { 26class OpenVpnController : public VpnController {
26private: 27private:
27 ServiceManager *mServiceManager; 28 ServiceManager *mServiceManager;
28 29
29public: 30public:
30 OpenVpnController(PropertyManager *propmngr); 31 OpenVpnController(PropertyManager *propmngr, IControllerHandler *handlers);
31 virtual ~OpenVpnController(); 32 virtual ~OpenVpnController();
32 33
33 int start(); 34 int start();
diff --git a/nexus/PropertyManager.cpp b/nexus/PropertyManager.cpp
index 3366babe7..6faf9b830 100644
--- a/nexus/PropertyManager.cpp
+++ b/nexus/PropertyManager.cpp
@@ -37,7 +37,8 @@ int PropertyManager::registerProperty(const char *name, IPropertyProvider *pp) {
37 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) { 37 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
38 if (!strcmp(name, (*it)->getName())) { 38 if (!strcmp(name, (*it)->getName())) {
39 errno = EADDRINUSE; 39 errno = EADDRINUSE;
40 LOGE("Failed to register property (%s)", strerror(errno)); 40 LOGE("Failed to register property %s (%s)",
41 name, strerror(errno));
41 pthread_mutex_unlock(&mLock); 42 pthread_mutex_unlock(&mLock);
42 return -1; 43 return -1;
43 } 44 }
diff --git a/nexus/Supplicant.cpp b/nexus/Supplicant.cpp
index e69f2c0df..9bb6bf2c1 100644
--- a/nexus/Supplicant.cpp
+++ b/nexus/Supplicant.cpp
@@ -29,13 +29,10 @@
29 29
30#include "Supplicant.h" 30#include "Supplicant.h"
31#include "SupplicantListener.h" 31#include "SupplicantListener.h"
32#include "SupplicantState.h"
33#include "SupplicantEvent.h"
34#include "ScanResult.h"
35#include "PropertyManager.h"
36#include "NetworkManager.h" 32#include "NetworkManager.h"
37#include "ErrorCode.h" 33#include "ErrorCode.h"
38#include "WifiController.h" 34#include "WifiController.h"
35#include "SupplicantStatus.h"
39 36
40#include "libwpa_client/wpa_ctrl.h" 37#include "libwpa_client/wpa_ctrl.h"
41 38
@@ -45,21 +42,16 @@
45#define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf" 42#define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf"
46#define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf" 43#define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf"
47 44
48Supplicant::Supplicant(WifiController *wc, PropertyManager *propmngr) { 45Supplicant::Supplicant(WifiController *wc, ISupplicantEventHandler *handlers) {
46 mHandlers = handlers;
49 mController = wc; 47 mController = wc;
50 mPropMngr = propmngr;
51 mInterfaceName = NULL; 48 mInterfaceName = NULL;
52 mCtrl = NULL; 49 mCtrl = NULL;
53 mMonitor = NULL; 50 mMonitor = NULL;
54 mListener = NULL; 51 mListener = NULL;
55 52
56 mState = SupplicantState::UNKNOWN;
57
58 mServiceManager = new ServiceManager(); 53 mServiceManager = new ServiceManager();
59 54
60 mLatestScanResults = new ScanResultCollection();
61 pthread_mutex_init(&mLatestScanResultsLock, NULL);
62
63 mNetworks = new WifiNetworkCollection(); 55 mNetworks = new WifiNetworkCollection();
64 pthread_mutex_init(&mNetworksLock, NULL); 56 pthread_mutex_init(&mNetworksLock, NULL);
65} 57}
@@ -92,14 +84,11 @@ int Supplicant::start() {
92 return -1; 84 return -1;
93 } 85 }
94 86
95 mPropMngr->registerProperty("wifi.supplicant.state", this);
96 return 0; 87 return 0;
97} 88}
98 89
99int Supplicant::stop() { 90int Supplicant::stop() {
100 91
101 mPropMngr->unregisterProperty("wifi.supplicant.state");
102
103 if (mListener->stopListener()) { 92 if (mListener->stopListener()) {
104 LOGW("Unable to stop supplicant listener (%s)", strerror(errno)); 93 LOGW("Unable to stop supplicant listener (%s)", strerror(errno));
105 return -1; 94 return -1;
@@ -125,6 +114,30 @@ bool Supplicant::isStarted() {
125 return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME); 114 return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME);
126} 115}
127 116
117SupplicantStatus *Supplicant::getStatus() {
118 char *reply;
119 size_t len = 4096;
120
121 if (!(reply = (char *) malloc(len))) {
122 errno = ENOMEM;
123 return NULL;
124 }
125
126 if (sendCommand("STATUS", reply, &len)) {
127 free(reply);
128 return NULL;
129 }
130
131 SupplicantStatus *ss = SupplicantStatus::createStatus(reply, len);
132
133 free (reply);
134 return ss;
135}
136
137/*
138 * Retrieves the list of networks from Supplicant
139 * and merge them into our current list
140 */
128int Supplicant::refreshNetworkList() { 141int Supplicant::refreshNetworkList() {
129 char *reply; 142 char *reply;
130 size_t len = 4096; 143 size_t len = 4096;
@@ -144,27 +157,59 @@ int Supplicant::refreshNetworkList() {
144 157
145 if (!strtok_r(reply, "\n", &linep_next)) { 158 if (!strtok_r(reply, "\n", &linep_next)) {
146 LOGW("Malformatted network list\n"); 159 LOGW("Malformatted network list\n");
147 } else { 160 free(reply);
148 pthread_mutex_lock(&mNetworksLock); 161 errno = EIO;
149 if (!mNetworks->empty()) { 162 return -1;
150 WifiNetworkCollection::iterator i; 163 }
151
152 for (i = mNetworks->begin(); i !=mNetworks->end(); ++i)
153 delete *i;
154 mNetworks->clear();
155 }
156 164
157 while((linep = strtok_r(NULL, "\n", &linep_next))) { 165 pthread_mutex_lock(&mNetworksLock);
158 WifiNetwork *wn = new WifiNetwork(mController, this, linep); 166
159 mNetworks->push_back(wn); 167 int num_added = 0;
160 if (wn->refresh()) 168 int num_refreshed = 0;
161 LOGW("Unable to refresh network id %d", wn->getNetworkId()); 169 int num_removed = 0;
170 while((linep = strtok_r(NULL, "\n", &linep_next))) {
171 // TODO: Move the decode into a static method so we
172 // don't create new_wn when we don't have to.
173 WifiNetwork *new_wn = new WifiNetwork(mController, this, linep);
174 WifiNetwork *merge_wn;
175
176 if ((merge_wn = this->lookupNetwork_UNLOCKED(new_wn->getNetworkId()))) {
177 num_refreshed++;
178 if (merge_wn->refresh()) {
179 LOGW("Error refreshing network %d (%s)",
180 merge_wn->getNetworkId(), strerror(errno));
181 }
182 delete new_wn;
183 } else {
184 num_added++;
185 new_wn->registerProperties();
186 mNetworks->push_back(new_wn);
187 if (new_wn->refresh()) {
188 LOGW("Unable to refresh network id %d (%s)",
189 new_wn->getNetworkId(), strerror(errno));
190 }
162 } 191 }
192 }
163 193
164 LOGD("Loaded %d networks\n", mNetworks->size()); 194 if (!mNetworks->empty()) {
165 pthread_mutex_unlock(&mNetworksLock); 195 // TODO: Add support for detecting removed networks
196 WifiNetworkCollection::iterator i;
197
198 for (i = mNetworks->begin(); i != mNetworks->end(); ++i) {
199 if (0) {
200 num_removed++;
201 (*i)->unregisterProperties();
202 delete (*i);
203 i = mNetworks->erase(i);
204 }
205 }
166 } 206 }
167 207
208
209 LOGD("Networks added %d, refreshed %d, removed %d\n",
210 num_added, num_refreshed, num_removed);
211 pthread_mutex_unlock(&mNetworksLock);
212
168 free(reply); 213 free(reply);
169 return 0; 214 return 0;
170} 215}
@@ -192,7 +237,7 @@ int Supplicant::connectToSupplicant() {
192 return -1; 237 return -1;
193 } 238 }
194 239
195 mListener = new SupplicantListener(this, mMonitor); 240 mListener = new SupplicantListener(mHandlers, mMonitor);
196 241
197 if (mListener->startListener()) { 242 if (mListener->startListener()) {
198 LOGE("Error - unable to start supplicant listener"); 243 LOGE("Error - unable to start supplicant listener");
@@ -245,165 +290,6 @@ int Supplicant::triggerScan(bool active) {
245 return 0; 290 return 0;
246} 291}
247 292
248int Supplicant::set(const char *name, const char *value) {
249 const char *n = name + strlen("wifi.supplicant.");
250
251 errno = -EROFS;
252 return -1;
253}
254
255const char *Supplicant::get(const char *name, char *buffer, size_t max) {
256 const char *n = name + strlen("wifi.supplicant.");
257
258 if (!strcasecmp(n, "state"))
259 return SupplicantState::toString(mState, buffer, max);
260 errno = ENOENT;
261 return NULL;
262}
263
264int Supplicant::onConnectedEvent(SupplicantEvent *evt) {
265 LOGD("onConnectedEvent(%s)", evt->getEvent());
266 return 0;
267}
268
269int Supplicant::onDisconnectedEvent(SupplicantEvent *evt) {
270 LOGD("onDisconnectedEvent(%s)", evt->getEvent());
271 return 0;
272}
273
274int Supplicant::onTerminatingEvent(SupplicantEvent *evt) {
275 LOGD("onTerminatingEvent(%s)", evt->getEvent());
276 return 0;
277}
278
279int Supplicant::onPasswordChangedEvent(SupplicantEvent *evt) {
280 LOGD("onPasswordChangedEvent(%s)", evt->getEvent());
281 return 0;
282}
283
284int Supplicant::onEapNotificationEvent(SupplicantEvent *evt) {
285 LOGD("onEapNotificationEvent(%s)", evt->getEvent());
286 return 0;
287}
288
289int Supplicant::onEapStartedEvent(SupplicantEvent *evt) {
290 LOGD("onEapStartedEvent(%s)", evt->getEvent());
291 return 0;
292}
293
294int Supplicant::onEapMethodEvent(SupplicantEvent *evt) {
295 LOGD("onEapMethodEvent(%s)", evt->getEvent());
296 return 0;
297}
298
299int Supplicant::onEapSuccessEvent(SupplicantEvent *evt) {
300 LOGD("onEapSuccessEvent(%s)", evt->getEvent());
301 return 0;
302}
303
304int Supplicant::onEapFailureEvent(SupplicantEvent *evt) {
305 LOGD("onEapFailureEvent(%s)", evt->getEvent());
306 return 0;
307}
308
309int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
310 if (!strcmp(evt->getEvent(), "Ready")) {
311 char *reply;
312
313 if (!(reply = (char *) malloc(4096))) {
314 errno = ENOMEM;
315 return -1;
316 }
317
318 size_t len = 4096;
319
320 if (sendCommand("SCAN_RESULTS", reply, &len)) {
321 LOGW("onScanResultsEvent(%s): Error getting scan results (%s)",
322 evt->getEvent(), strerror(errno));
323 free(reply);
324 return -1;
325 }
326
327 pthread_mutex_lock(&mLatestScanResultsLock);
328 if (!mLatestScanResults->empty()) {
329 ScanResultCollection::iterator i;
330
331 for (i = mLatestScanResults->begin();
332 i !=mLatestScanResults->end(); ++i) {
333 delete *i;
334 }
335 mLatestScanResults->clear();
336 }
337
338 char *linep;
339 char *linep_next = NULL;
340
341 if (!strtok_r(reply, "\n", &linep_next)) {
342 free(reply);
343 pthread_mutex_unlock(&mLatestScanResultsLock);
344 return 0;
345 }
346
347 while((linep = strtok_r(NULL, "\n", &linep_next)))
348 mLatestScanResults->push_back(new ScanResult(linep));
349
350 char tmp[128];
351 sprintf(tmp, "Scan results ready (%d)", mLatestScanResults->size());
352 NetworkManager::Instance()->getBroadcaster()->
353 sendBroadcast(ErrorCode::UnsolicitedInformational, tmp, false);
354 pthread_mutex_unlock(&mLatestScanResultsLock);
355 free(reply);
356 } else {
357 LOGW("Unknown SCAN_RESULTS event (%s)", evt->getEvent());
358 }
359 return 0;
360}
361
362int Supplicant::onStateChangeEvent(SupplicantEvent *evt) {
363 char *bword, *last;
364 char *tmp = strdup(evt->getEvent());
365
366 if (!(bword = strtok_r(tmp, " ", &last))) {
367 LOGE("Malformatted state update (%s)", evt->getEvent());
368 free(tmp);
369 return 0;
370 }
371
372 if (!(bword = strtok_r(NULL, " ", &last))) {
373 LOGE("Malformatted state update (%s)", evt->getEvent());
374 free(tmp);
375 return 0;
376 }
377
378 mState = atoi(&bword[strlen("state=")]);
379 LOGD("State changed to %d", mState);
380 free(tmp);
381 return 0;
382}
383
384int Supplicant::onLinkSpeedEvent(SupplicantEvent *evt) {
385 LOGD("onLinkSpeedEvent(%s)", evt->getEvent());
386 return 0;
387}
388
389int Supplicant::onDriverStateEvent(SupplicantEvent *evt) {
390 LOGD("onDriverStateEvent(%s)", evt->getEvent());
391 return 0;
392}
393
394// XXX: Use a cursor + smartptr instead
395ScanResultCollection *Supplicant::createLatestScanResults() {
396 ScanResultCollection *d = new ScanResultCollection();
397 ScanResultCollection::iterator i;
398
399 pthread_mutex_lock(&mLatestScanResultsLock);
400 for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i)
401 d->push_back((*i)->clone());
402
403 pthread_mutex_unlock(&mLatestScanResultsLock);
404 return d;
405}
406
407WifiNetwork *Supplicant::createNetwork() { 293WifiNetwork *Supplicant::createNetwork() {
408 char reply[255]; 294 char reply[255];
409 size_t len = sizeof(reply) -1; 295 size_t len = sizeof(reply) -1;
@@ -445,14 +331,18 @@ int Supplicant::removeNetwork(WifiNetwork *wn) {
445 331
446WifiNetwork *Supplicant::lookupNetwork(int networkId) { 332WifiNetwork *Supplicant::lookupNetwork(int networkId) {
447 pthread_mutex_lock(&mNetworksLock); 333 pthread_mutex_lock(&mNetworksLock);
334 WifiNetwork *wn = lookupNetwork_UNLOCKED(networkId);
335 pthread_mutex_unlock(&mNetworksLock);
336 return wn;
337}
338
339WifiNetwork *Supplicant::lookupNetwork_UNLOCKED(int networkId) {
448 WifiNetworkCollection::iterator it; 340 WifiNetworkCollection::iterator it;
449 for (it = mNetworks->begin(); it != mNetworks->end(); ++it) { 341 for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
450 if ((*it)->getNetworkId() == networkId) { 342 if ((*it)->getNetworkId() == networkId) {
451 pthread_mutex_unlock(&mNetworksLock);
452 return *it; 343 return *it;
453 } 344 }
454 } 345 }
455 pthread_mutex_unlock(&mNetworksLock);
456 errno = ENOENT; 346 errno = ENOENT;
457 return NULL; 347 return NULL;
458} 348}
@@ -528,6 +418,12 @@ int Supplicant::setNetworkVar(int networkId, const char *var, const char *val) {
528 return -1; 418 return -1;
529 } 419 }
530 free(tmp); 420 free(tmp);
421
422 len = sizeof(reply) -1;
423 if (sendCommand("SAVE_CONFIG", reply, &len)) {
424 LOGE("Error saving config after %s = %s", var, val);
425 return -1;
426 }
531 return 0; 427 return 0;
532} 428}
533 429
diff --git a/nexus/Supplicant.h b/nexus/Supplicant.h
index 42f2f79a2..3efbe4cd4 100644
--- a/nexus/Supplicant.h
+++ b/nexus/Supplicant.h
@@ -19,38 +19,31 @@
19 19
20struct wpa_ctrl; 20struct wpa_ctrl;
21class SupplicantListener; 21class SupplicantListener;
22class SupplicantEvent;
23class ServiceManager; 22class ServiceManager;
24class PropertyManager;
25class Controller; 23class Controller;
26class WifiController; 24class WifiController;
25class SupplicantStatus;
27 26
28#include <pthread.h> 27#include <pthread.h>
29 28
30#include "ScanResult.h"
31#include "WifiNetwork.h" 29#include "WifiNetwork.h"
32#include "IPropertyProvider.h"
33#include "ISupplicantEventHandler.h" 30#include "ISupplicantEventHandler.h"
34 31
35class Supplicant : public IPropertyProvider, public ISupplicantEventHandler { 32class Supplicant {
36private: 33private:
37 struct wpa_ctrl *mCtrl; 34 struct wpa_ctrl *mCtrl;
38 struct wpa_ctrl *mMonitor; 35 struct wpa_ctrl *mMonitor;
39 SupplicantListener *mListener; 36 SupplicantListener *mListener;
40 int mState;
41 ServiceManager *mServiceManager; 37 ServiceManager *mServiceManager;
42 PropertyManager *mPropMngr;
43 WifiController *mController; 38 WifiController *mController;
44 char *mInterfaceName; 39 char *mInterfaceName;
45 40
46 ScanResultCollection *mLatestScanResults; 41 WifiNetworkCollection *mNetworks;
47 pthread_mutex_t mLatestScanResultsLock; 42 pthread_mutex_t mNetworksLock;
48 43 ISupplicantEventHandler *mHandlers;
49 WifiNetworkCollection *mNetworks;
50 pthread_mutex_t mNetworksLock;
51 44
52public: 45public:
53 Supplicant(WifiController *wc, PropertyManager *propmngr); 46 Supplicant(WifiController *wc, ISupplicantEventHandler *handlers);
54 virtual ~Supplicant(); 47 virtual ~Supplicant();
55 48
56 int start(); 49 int start();
@@ -58,7 +51,6 @@ public:
58 bool isStarted(); 51 bool isStarted();
59 52
60 int triggerScan(bool active); 53 int triggerScan(bool active);
61 ScanResultCollection *createLatestScanResults();
62 54
63 WifiNetwork *createNetwork(); 55 WifiNetwork *createNetwork();
64 WifiNetwork *lookupNetwork(int networkId); 56 WifiNetwork *lookupNetwork(int networkId);
@@ -71,33 +63,18 @@ public:
71 size_t max); 63 size_t max);
72 int enableNetwork(int networkId, bool enabled); 64 int enableNetwork(int networkId, bool enabled);
73 65
74 int getState() { return mState; } 66 SupplicantStatus *getStatus();
67
75 Controller *getController() { return (Controller *) mController; } 68 Controller *getController() { return (Controller *) mController; }
76 const char *getInterfaceName() { return mInterfaceName; } 69 const char *getInterfaceName() { return mInterfaceName; }
77 70
78 int set(const char *name, const char *value); 71 int sendCommand(const char *cmd, char *reply, size_t *reply_len);
79 const char *get(const char *name, char *buffer, size_t max);
80 72
81private: 73private:
82 int connectToSupplicant(); 74 int connectToSupplicant();
83 int sendCommand(const char *cmd, char *reply, size_t *reply_len);
84 int setupConfig(); 75 int setupConfig();
85 int retrieveInterfaceName(); 76 int retrieveInterfaceName();
86 77 WifiNetwork *lookupNetwork_UNLOCKED(int networkId);
87 // ISupplicantEventHandler methods
88 virtual int onConnectedEvent(SupplicantEvent *evt);
89 virtual int onDisconnectedEvent(SupplicantEvent *evt);
90 virtual int onTerminatingEvent(SupplicantEvent *evt);
91 virtual int onPasswordChangedEvent(SupplicantEvent *evt);
92 virtual int onEapNotificationEvent(SupplicantEvent *evt);
93 virtual int onEapStartedEvent(SupplicantEvent *evt);
94 virtual int onEapMethodEvent(SupplicantEvent *evt);
95 virtual int onEapSuccessEvent(SupplicantEvent *evt);
96 virtual int onEapFailureEvent(SupplicantEvent *evt);
97 virtual int onScanResultsEvent(SupplicantEvent *evt);
98 virtual int onStateChangeEvent(SupplicantEvent *evt);
99 virtual int onLinkSpeedEvent(SupplicantEvent *evt);
100 virtual int onDriverStateEvent(SupplicantEvent *evt);
101}; 78};
102 79
103#endif 80#endif
diff --git a/nexus/SupplicantAssociatedEvent.cpp b/nexus/SupplicantAssociatedEvent.cpp
new file mode 100644
index 000000000..e40411efe
--- /dev/null
+++ b/nexus/SupplicantAssociatedEvent.cpp
@@ -0,0 +1,40 @@
1/*
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
17#include <stdlib.h>
18
19#define LOG_TAG "SupplicantAssociatedEvent"
20#include <cutils/log.h>
21
22#include "SupplicantAssociatedEvent.h"
23
24SupplicantAssociatedEvent::SupplicantAssociatedEvent(int level, char *event,
25 size_t len) :
26 SupplicantEvent(SupplicantEvent::EVENT_ASSOCIATED,
27 level) {
28 char *p = event;
29
30 // "00:13:46:40:40:aa"
31 mBssid = (char *) malloc(18);
32 strncpy(mBssid, p, 17);
33 mBssid[17] = '\0';
34}
35
36SupplicantAssociatedEvent::~SupplicantAssociatedEvent() {
37 if (mBssid)
38 free(mBssid);
39}
40
diff --git a/nexus/SupplicantAssociatedEvent.h b/nexus/SupplicantAssociatedEvent.h
new file mode 100644
index 000000000..aa33c59b7
--- /dev/null
+++ b/nexus/SupplicantAssociatedEvent.h
@@ -0,0 +1,34 @@
1/*
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
17#ifndef _SupplicantAssociatedEvent_H
18#define _SupplicantAssociatedEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantAssociatedEvent : public SupplicantEvent {
23 char *mBssid;
24 char *mSsid;
25 int mFreq;
26
27public:
28 SupplicantAssociatedEvent(int level, char *event, size_t len);
29 virtual ~SupplicantAssociatedEvent();
30
31 const char *getBssid() { return mBssid; }
32};
33
34#endif
diff --git a/nexus/SupplicantAssociatingEvent.cpp b/nexus/SupplicantAssociatingEvent.cpp
new file mode 100644
index 000000000..00a85b654
--- /dev/null
+++ b/nexus/SupplicantAssociatingEvent.cpp
@@ -0,0 +1,99 @@
1/*
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
17#include <stdlib.h>
18
19#define LOG_TAG "SupplicantAssociatingEvent"
20#include <cutils/log.h>
21
22#include "SupplicantAssociatingEvent.h"
23
24SupplicantAssociatingEvent::SupplicantAssociatingEvent(int level, char *event,
25 size_t len) :
26 SupplicantEvent(SupplicantEvent::EVENT_ASSOCIATING,
27 level) {
28 char *p = event;
29
30 mBssid = NULL;
31 mSsid = NULL;
32
33 // SSID 'default'
34 // OR
35 // "00:13:46:40:40:aa (SSID='default' freq=2437 MHz)"
36
37 if (strncmp(event, "SSID", 4)) {
38 mBssid = (char *) malloc(18);
39 strncpy(mBssid, p, 17);
40 mBssid[17] = '\0';
41 p += 25;
42
43 // "00:13:46:40:40:aa (SSID='default' freq=2437 MHz)"
44 // ^
45 // p
46 char *q = index(p, '\'');
47 if (!q) {
48 LOGE("Unable to decode SSID (p = {%s})\n", p);
49 return;
50 }
51 mSsid = (char *) malloc((q - p) +1);
52 strncpy(mSsid, p, q-p);
53 mSsid[q-p] = '\0';
54
55 p = q + 7;
56
57 // "00:13:46:40:40:aa (SSID='default' freq=2437 MHz)"
58 // ^
59 // p
60 if (!(q = index(p, ' '))) {
61 LOGE("Unable to decode frequency\n");
62 return;
63 }
64 *q = '\0';
65 mFreq = atoi(p);
66 } else {
67 p+= 6;
68
69 // SSID 'default'
70 // ^
71 // p
72
73 char *q = index(p, '\'');
74 if (!q) {
75 LOGE("Unable to decode SSID (p = {%s})\n", p);
76 return;
77 }
78 mSsid = (char *) malloc((q - p) +1);
79 strncpy(mSsid, p, q-p);
80 mSsid[q-p] = '\0';
81 }
82}
83
84SupplicantAssociatingEvent::SupplicantAssociatingEvent(const char *bssid,
85 const char *ssid,
86 int freq) :
87 SupplicantEvent(SupplicantEvent::EVENT_ASSOCIATING, -1) {
88 mBssid = strdup(bssid);
89 mSsid= strdup(ssid);
90 mFreq = freq;
91}
92
93SupplicantAssociatingEvent::~SupplicantAssociatingEvent() {
94 if (mBssid)
95 free(mBssid);
96 if (mSsid)
97 free(mSsid);
98}
99
diff --git a/nexus/SupplicantAssociatingEvent.h b/nexus/SupplicantAssociatingEvent.h
new file mode 100644
index 000000000..d3a4d5cd9
--- /dev/null
+++ b/nexus/SupplicantAssociatingEvent.h
@@ -0,0 +1,37 @@
1/*
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
17#ifndef _SupplicantAssociatingEvent_H
18#define _SupplicantAssociatingEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantAssociatingEvent : public SupplicantEvent {
23 char *mBssid;
24 char *mSsid;
25 int mFreq;
26
27public:
28 SupplicantAssociatingEvent(int level, char *event, size_t len);
29 SupplicantAssociatingEvent(const char *bssid, const char *ssid, int freq);
30 virtual ~SupplicantAssociatingEvent();
31
32 const char *getBssid() { return mBssid; }
33 const char *getSsid() { return mSsid; }
34 int getFreq() { return mFreq;}
35};
36
37#endif
diff --git a/nexus/SupplicantConnectedEvent.cpp b/nexus/SupplicantConnectedEvent.cpp
new file mode 100644
index 000000000..e58bab22e
--- /dev/null
+++ b/nexus/SupplicantConnectedEvent.cpp
@@ -0,0 +1,61 @@
1/*
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
17#define LOG_TAG "SupplicantConnectedEvent"
18#include <cutils/log.h>
19
20#include "SupplicantConnectedEvent.h"
21
22SupplicantConnectedEvent::SupplicantConnectedEvent(int level, char *event,
23 size_t len) :
24 SupplicantEvent(SupplicantEvent::EVENT_CONNECTED,
25 level) {
26 char *p;
27
28 // "- Connection to 00:13:46:40:40:aa completed (auth) [id=1 id_str=], 89"
29
30 if ((p = index(event + 2, ' ')) && (++p = index(p, ' '))) {
31 mBssid = (char *) malloc(18);
32 strncpy(mBssid, ++p, 17);
33 mBssid[17] = '\0';
34
35 // "- Connection to 00:13:46:40:40:aa completed (auth) [id=1 id_str=], 89"
36 // ^
37 // p
38
39 if ((p = index(p, ' ')) && ((++p = index(p, ' ')))) {
40 if (!strncmp(++p, "(auth)", 6))
41 mReassociated = false;
42 else
43 mReassociated = true;
44 } else
45 LOGE("Unable to decode re-assocation");
46 } else
47 LOGE("Unable to decode event");
48}
49
50SupplicantConnectedEvent::SupplicantConnectedEvent(const char *bssid,
51 bool reassocated) :
52 SupplicantEvent(SupplicantEvent::EVENT_CONNECTED, -1) {
53 mBssid = strdup(bssid);
54 mReassociated = reassocated;
55}
56
57SupplicantConnectedEvent::~SupplicantConnectedEvent() {
58 if (mBssid)
59 free(mBssid);
60}
61
diff --git a/nexus/SupplicantConnectedEvent.h b/nexus/SupplicantConnectedEvent.h
new file mode 100644
index 000000000..03e9842a1
--- /dev/null
+++ b/nexus/SupplicantConnectedEvent.h
@@ -0,0 +1,36 @@
1/*
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
17#ifndef _SupplicantConnectedEvent_H
18#define _SupplicantConnectedEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantConnectedEvent : public SupplicantEvent {
23private:
24 char *mBssid;
25 bool mReassociated;
26
27public:
28 SupplicantConnectedEvent(int level, char *event, size_t len);
29 SupplicantConnectedEvent(const char *bssid, bool reassicated);
30 virtual ~SupplicantConnectedEvent();
31
32 const char *getBssid() { return mBssid; }
33 bool getReassociated() { return mReassociated; }
34};
35
36#endif
diff --git a/nexus/SupplicantConnectionTimeoutEvent.cpp b/nexus/SupplicantConnectionTimeoutEvent.cpp
new file mode 100644
index 000000000..8b8a7d755
--- /dev/null
+++ b/nexus/SupplicantConnectionTimeoutEvent.cpp
@@ -0,0 +1,36 @@
1/*
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
17#define LOG_TAG "SupplicantConnectionTimeoutEvent"
18#include <cutils/log.h>
19
20#include "SupplicantConnectionTimeoutEvent.h"
21
22SupplicantConnectionTimeoutEvent::SupplicantConnectionTimeoutEvent(int level, char *event,
23 size_t len) :
24 SupplicantEvent(SupplicantEvent::EVENT_CONNECTIONTIMEOUT,
25 level) {
26 // 00:13:46:40:40:aa timed out.'
27 mBssid = (char *) malloc(18);
28 strncpy(mBssid, event, 17);
29 mBssid[17] = '\0';
30}
31
32SupplicantConnectionTimeoutEvent::~SupplicantConnectionTimeoutEvent() {
33 if (mBssid)
34 free(mBssid);
35}
36
diff --git a/nexus/SupplicantConnectionTimeoutEvent.h b/nexus/SupplicantConnectionTimeoutEvent.h
new file mode 100644
index 000000000..0d2606d04
--- /dev/null
+++ b/nexus/SupplicantConnectionTimeoutEvent.h
@@ -0,0 +1,34 @@
1/*
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
17#ifndef _SupplicantConnectionTimeoutEvent_H
18#define _SupplicantConnectionTimeoutEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantConnectionTimeoutEvent : public SupplicantEvent {
23private:
24 char *mBssid;
25 bool mReassociated;
26
27public:
28 SupplicantConnectionTimeoutEvent(int level, char *event, size_t len);
29 virtual ~SupplicantConnectionTimeoutEvent();
30
31 const char *getBssid() { return mBssid; }
32};
33
34#endif
diff --git a/nexus/SupplicantDisconnectedEvent.cpp b/nexus/SupplicantDisconnectedEvent.cpp
new file mode 100644
index 000000000..11e499db2
--- /dev/null
+++ b/nexus/SupplicantDisconnectedEvent.cpp
@@ -0,0 +1,33 @@
1/*
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
17#define LOG_TAG "SupplicantDisconnectedEvent"
18#include <cutils/log.h>
19
20#include "SupplicantDisconnectedEvent.h"
21
22SupplicantDisconnectedEvent::SupplicantDisconnectedEvent(int level, char *event,
23 size_t len) :
24 SupplicantEvent(SupplicantEvent::EVENT_DISCONNECTED,
25 level) {
26}
27
28SupplicantDisconnectedEvent::SupplicantDisconnectedEvent() :
29 SupplicantEvent(SupplicantEvent::EVENT_DISCONNECTED, -1) {
30}
31
32SupplicantDisconnectedEvent::~SupplicantDisconnectedEvent() {
33}
diff --git a/nexus/SupplicantDisconnectedEvent.h b/nexus/SupplicantDisconnectedEvent.h
new file mode 100644
index 000000000..c09ec622f
--- /dev/null
+++ b/nexus/SupplicantDisconnectedEvent.h
@@ -0,0 +1,30 @@
1/*
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
17#ifndef _SupplicantDisconnectedEvent_H
18#define _SupplicantDisconnectedEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantDisconnectedEvent : public SupplicantEvent {
23
24public:
25 SupplicantDisconnectedEvent(int level, char *event, size_t len);
26 SupplicantDisconnectedEvent();
27 virtual ~SupplicantDisconnectedEvent();
28};
29
30#endif
diff --git a/nexus/SupplicantEvent.cpp b/nexus/SupplicantEvent.cpp
index 2e6d66542..faf7b45f4 100644
--- a/nexus/SupplicantEvent.cpp
+++ b/nexus/SupplicantEvent.cpp
@@ -23,74 +23,7 @@
23 23
24#include "libwpa_client/wpa_ctrl.h" 24#include "libwpa_client/wpa_ctrl.h"
25 25
26SupplicantEvent::SupplicantEvent(char *event, size_t len) { 26SupplicantEvent::SupplicantEvent(int type, int level) {
27 27 mType = type;
28 if (event[0] == '<') { 28 mLevel = level;
29 char *match = strchr(event, '>');
30 if (match) {
31 char tmp[16];
32
33 strncpy(tmp, &event[1], (match - event));
34 mLevel = atoi(tmp);
35 event += (match - event) + 1;
36 } else
37 LOGW("Unclosed level brace in event");
38 } else
39 LOGW("No level specified in event");
40
41 /*
42 * <N>CTRL-EVENT-XXX
43 * ^
44 * +---- event
45 */
46
47 if (!strncmp(event, WPA_EVENT_CONNECTED, strlen(WPA_EVENT_CONNECTED)))
48 mType = SupplicantEvent::EVENT_CONNECTED;
49 else if (!strncmp(event, WPA_EVENT_DISCONNECTED, strlen(WPA_EVENT_DISCONNECTED)))
50 mType = SupplicantEvent::EVENT_DISCONNECTED;
51 else if (!strncmp(event, WPA_EVENT_TERMINATING, strlen(WPA_EVENT_TERMINATING)))
52 mType = SupplicantEvent::EVENT_TERMINATING;
53 else if (!strncmp(event, WPA_EVENT_PASSWORD_CHANGED, strlen(WPA_EVENT_PASSWORD_CHANGED)))
54 mType = SupplicantEvent::EVENT_PASSWORD_CHANGED;
55 else if (!strncmp(event, WPA_EVENT_EAP_NOTIFICATION, strlen(WPA_EVENT_EAP_NOTIFICATION)))
56 mType = SupplicantEvent::EVENT_EAP_NOTIFICATION;
57 else if (!strncmp(event, WPA_EVENT_EAP_STARTED, strlen(WPA_EVENT_EAP_STARTED)))
58 mType = SupplicantEvent::EVENT_EAP_STARTED;
59 else if (!strncmp(event, WPA_EVENT_EAP_METHOD, strlen(WPA_EVENT_EAP_METHOD)))
60 mType = SupplicantEvent::EVENT_EAP_METHOD;
61 else if (!strncmp(event, WPA_EVENT_EAP_SUCCESS, strlen(WPA_EVENT_EAP_SUCCESS)))
62 mType = SupplicantEvent::EVENT_EAP_SUCCESS;
63 else if (!strncmp(event, WPA_EVENT_EAP_FAILURE, strlen(WPA_EVENT_EAP_FAILURE)))
64 mType = SupplicantEvent::EVENT_EAP_FAILURE;
65 else if (!strncmp(event, WPA_EVENT_SCAN_RESULTS, strlen(WPA_EVENT_SCAN_RESULTS)))
66 mType = SupplicantEvent::EVENT_SCAN_RESULTS;
67 else if (!strncmp(event, WPA_EVENT_STATE_CHANGE, strlen(WPA_EVENT_STATE_CHANGE)))
68 mType = SupplicantEvent::EVENT_STATE_CHANGE;
69 else if (!strncmp(event, WPA_EVENT_LINK_SPEED, strlen(WPA_EVENT_LINK_SPEED)))
70 mType = SupplicantEvent::EVENT_LINK_SPEED;
71 else if (!strncmp(event, WPA_EVENT_DRIVER_STATE, strlen(WPA_EVENT_DRIVER_STATE)))
72 mType = SupplicantEvent::EVENT_DRIVER_STATE;
73 else {
74 LOGW("Unknown supplicant event '%s'", event);
75 mType = SupplicantEvent::EVENT_UNKNOWN;
76 }
77
78 for (event; *event != ' '; event++);
79 event++;
80
81 /*
82 * <N>CTRL-EVENT-XXX YYYY
83 * ^
84 * +---- event
85 */
86
87 for (event; *event == ' '; event++);
88
89 mEvent = strdup(event);
90 mLen = len;
91}
92
93SupplicantEvent::~SupplicantEvent() {
94 if (mEvent)
95 free(mEvent);
96} 29}
diff --git a/nexus/SupplicantEvent.h b/nexus/SupplicantEvent.h
index 2dc67225c..9d7cbd9f2 100644
--- a/nexus/SupplicantEvent.h
+++ b/nexus/SupplicantEvent.h
@@ -22,33 +22,32 @@
22class SupplicantEvent { 22class SupplicantEvent {
23private: 23private:
24 int mType; 24 int mType;
25 char *mEvent;
26 size_t mLen;
27 int mLevel; 25 int mLevel;
28 26
29public: 27public:
30 static const int EVENT_UNKNOWN = 0; 28 static const int EVENT_UNKNOWN = 0;
31 static const int EVENT_CONNECTED = 1; 29 static const int EVENT_CONNECTED = 1;
32 static const int EVENT_DISCONNECTED = 2; 30 static const int EVENT_DISCONNECTED = 2;
33 static const int EVENT_TERMINATING = 3; 31 static const int EVENT_TERMINATING = 3;
34 static const int EVENT_PASSWORD_CHANGED = 4; 32 static const int EVENT_PASSWORD_CHANGED = 4;
35 static const int EVENT_EAP_NOTIFICATION = 5; 33 static const int EVENT_EAP_NOTIFICATION = 5;
36 static const int EVENT_EAP_STARTED = 6; 34 static const int EVENT_EAP_STARTED = 6;
37 static const int EVENT_EAP_METHOD = 7; 35 static const int EVENT_EAP_METHOD = 7;
38 static const int EVENT_EAP_SUCCESS = 8; 36 static const int EVENT_EAP_SUCCESS = 8;
39 static const int EVENT_EAP_FAILURE = 9; 37 static const int EVENT_EAP_FAILURE = 9;
40 static const int EVENT_SCAN_RESULTS = 10; 38 static const int EVENT_SCAN_RESULTS = 10;
41 static const int EVENT_STATE_CHANGE = 11; 39 static const int EVENT_STATE_CHANGE = 11;
42 static const int EVENT_LINK_SPEED = 12; 40 static const int EVENT_LINK_SPEED = 12;
43 static const int EVENT_DRIVER_STATE = 13; 41 static const int EVENT_DRIVER_STATE = 13;
42 static const int EVENT_ASSOCIATING = 14;
43 static const int EVENT_ASSOCIATED = 15;
44 static const int EVENT_CONNECTIONTIMEOUT = 16;
44 45
45public: 46public:
46 SupplicantEvent(char *event, size_t len); 47 SupplicantEvent(int type, int level);
47 virtual ~SupplicantEvent(); 48 virtual ~SupplicantEvent() {}
48 49
49 int getType() { return mType; } 50 int getType() { return mType; }
50 const char *getEvent() { return mEvent; }
51 int getLen() { return mLen; }
52 int getLevel() { return mLevel; } 51 int getLevel() { return mLevel; }
53}; 52};
54 53
diff --git a/nexus/SupplicantEventFactory.cpp b/nexus/SupplicantEventFactory.cpp
new file mode 100644
index 000000000..8695aca19
--- /dev/null
+++ b/nexus/SupplicantEventFactory.cpp
@@ -0,0 +1,119 @@
1/*
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
17#include <stdlib.h>
18
19#define LOG_TAG "SupplicantEventFactory"
20#include <cutils/log.h>
21
22#include "SupplicantEvent.h"
23#include "SupplicantEventFactory.h"
24#include "SupplicantAssociatingEvent.h"
25#include "SupplicantAssociatedEvent.h"
26#include "SupplicantConnectedEvent.h"
27#include "SupplicantStateChangeEvent.h"
28#include "SupplicantScanResultsEvent.h"
29#include "SupplicantConnectionTimeoutEvent.h"
30#include "SupplicantDisconnectedEvent.h"
31#if 0
32#include "SupplicantTerminatingEvent.h"
33#include "SupplicantPasswordChangedEvent.h"
34#include "SupplicantEapNotificationEvent.h"
35#include "SupplicantEapStartedEvent.h"
36#include "SupplicantEapMethodEvent.h"
37#include "SupplicantEapSuccessEvent.h"
38#include "SupplicantEapFailureEvent.h"
39#include "SupplicantLinkSpeedEvent.h"
40#include "SupplicantDriverStateEvent.h"
41#endif
42
43#include "libwpa_client/wpa_ctrl.h"
44
45SupplicantEventFactory::SupplicantEventFactory() {
46}
47
48SupplicantEvent *SupplicantEventFactory::createEvent(char *event, size_t len) {
49 int level = 0;
50
51 if (event[0] == '<') {
52 char *match = strchr(event, '>');
53 if (match) {
54 char tmp[16];
55
56 strncpy(tmp, &event[1], (match - event));
57 level = atoi(tmp);
58 event += (match - event) + 1;
59 } else
60 LOGW("Unclosed level brace in event");
61 } else
62 LOGW("No level specified in event");
63
64 /*
65 * <N>CTRL-EVENT-XXX
66 * ^
67 * +---- event
68 */
69
70 if (!strncmp(event, "Authentication with ", 20)) {
71 if (!strcmp(event + strlen(event) - strlen(" timed out."),
72 " timed out.")) {
73 return new SupplicantConnectionTimeoutEvent(level,
74 event + 20,
75 len);
76 } else
77 return NULL;
78
79 } else if (!strncmp(event, "Associated with ", 16))
80 return new SupplicantAssociatedEvent(level, event + 16, len);
81 else if (!strncmp(event, "Trying to associate with ", 25))
82 return new SupplicantAssociatingEvent(level, event + 25, len);
83 else if (!strncmp(event, WPA_EVENT_CONNECTED, strlen(WPA_EVENT_CONNECTED))) {
84 return new SupplicantConnectedEvent(level,
85 event + strlen(WPA_EVENT_CONNECTED),
86 len);
87 } else if (!strncmp(event, WPA_EVENT_SCAN_RESULTS, strlen(WPA_EVENT_SCAN_RESULTS))) {
88 return new SupplicantScanResultsEvent(level,
89 event + strlen(WPA_EVENT_SCAN_RESULTS),
90 len);
91 } else if (!strncmp(event, WPA_EVENT_STATE_CHANGE, strlen(WPA_EVENT_STATE_CHANGE))) {
92 return new SupplicantStateChangeEvent(level,
93 event + strlen(WPA_EVENT_STATE_CHANGE),
94 len);
95 }
96 else if (!strncmp(event, WPA_EVENT_DISCONNECTED, strlen(WPA_EVENT_DISCONNECTED)))
97 return new SupplicantDisconnectedEvent(level, event, len);
98#if 0
99 else if (!strncmp(event, WPA_EVENT_TERMINATING, strlen(WPA_EVENT_TERMINATING)))
100 return new SupplicantTerminatingEvent(event, len);
101 else if (!strncmp(event, WPA_EVENT_PASSWORD_CHANGED, strlen(WPA_EVENT_PASSWORD_CHANGED)))
102 return new SupplicantPasswordChangedEvent(event, len);
103 else if (!strncmp(event, WPA_EVENT_EAP_NOTIFICATION, strlen(WPA_EVENT_EAP_NOTIFICATION)))
104 return new SupplicantEapNotificationEvent(event, len);
105 else if (!strncmp(event, WPA_EVENT_EAP_STARTED, strlen(WPA_EVENT_EAP_STARTED)))
106 return new SupplicantEapStartedEvent(event, len);
107 else if (!strncmp(event, WPA_EVENT_EAP_METHOD, strlen(WPA_EVENT_EAP_METHOD)))
108 return new SupplicantEapMethodEvent(event, len);
109 else if (!strncmp(event, WPA_EVENT_EAP_SUCCESS, strlen(WPA_EVENT_EAP_SUCCESS)))
110 return new SupplicantEapSuccessEvent(event, len);
111 else if (!strncmp(event, WPA_EVENT_EAP_FAILURE, strlen(WPA_EVENT_EAP_FAILURE)))
112 return new SupplicantEapFailureEvent(event, len);
113 else if (!strncmp(event, WPA_EVENT_LINK_SPEED, strlen(WPA_EVENT_LINK_SPEED)))
114 return new SupplicantLinkSpeedEvent(event, len);
115 else if (!strncmp(event, WPA_EVENT_DRIVER_STATE, strlen(WPA_EVENT_DRIVER_STATE)))
116 return new SupplicantDriverStateEvent(event, len);
117#endif
118 return NULL;
119}
diff --git a/nexus/SupplicantEventFactory.h b/nexus/SupplicantEventFactory.h
new file mode 100644
index 000000000..22e5707e8
--- /dev/null
+++ b/nexus/SupplicantEventFactory.h
@@ -0,0 +1,31 @@
1
2/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _SupplicantEventFactory_H
19#define _SupplicantEventFactory_H
20
21class SupplicantEvent;
22
23class SupplicantEventFactory {
24public:
25 SupplicantEventFactory();
26 virtual ~SupplicantEventFactory() {}
27
28 SupplicantEvent *createEvent(char *event, size_t len);
29};
30
31#endif
diff --git a/nexus/SupplicantListener.cpp b/nexus/SupplicantListener.cpp
index b94648b29..b91fc02db 100644
--- a/nexus/SupplicantListener.cpp
+++ b/nexus/SupplicantListener.cpp
@@ -24,14 +24,21 @@
24#include "libwpa_client/wpa_ctrl.h" 24#include "libwpa_client/wpa_ctrl.h"
25 25
26#include "SupplicantListener.h" 26#include "SupplicantListener.h"
27#include "SupplicantEvent.h"
28#include "ISupplicantEventHandler.h" 27#include "ISupplicantEventHandler.h"
28#include "SupplicantEventFactory.h"
29#include "SupplicantEvent.h"
30#include "SupplicantAssociatingEvent.h"
31#include "SupplicantAssociatedEvent.h"
32#include "SupplicantConnectedEvent.h"
33#include "SupplicantScanResultsEvent.h"
34#include "SupplicantStateChangeEvent.h"
29 35
30SupplicantListener::SupplicantListener(ISupplicantEventHandler *handlers, 36SupplicantListener::SupplicantListener(ISupplicantEventHandler *handlers,
31 struct wpa_ctrl *monitor) : 37 struct wpa_ctrl *monitor) :
32 SocketListener(wpa_ctrl_get_fd(monitor), false) { 38 SocketListener(wpa_ctrl_get_fd(monitor), false) {
33 mHandlers = handlers; 39 mHandlers = handlers;
34 mMonitor = monitor; 40 mMonitor = monitor;
41 mFactory = new SupplicantEventFactory();
35} 42}
36 43
37bool SupplicantListener::onDataAvailable(SocketClient *cli) { 44bool SupplicantListener::onDataAvailable(SocketClient *cli) {
@@ -53,44 +60,50 @@ bool SupplicantListener::onDataAvailable(SocketClient *cli) {
53 return false; 60 return false;
54 } 61 }
55 62
56 SupplicantEvent *evt = new SupplicantEvent(buf, nread); 63 SupplicantEvent *evt = mFactory->createEvent(buf, nread);
64
65 if (!evt) {
66 LOGW("Dropping unknown supplicant event '%s'", buf);
67 return true;
68 }
57 69
58 // XXX: Make this a factory 70 // Call the appropriate handler
59 // XXX: Instead of calling Supplicant directly 71 if (evt->getType() == SupplicantEvent::EVENT_ASSOCIATING)
60 // extract an Interface and use that instead 72 mHandlers->onAssociatingEvent((SupplicantAssociatingEvent *) evt);
61 if (evt->getType() == SupplicantEvent::EVENT_CONNECTED) 73 else if (evt->getType() == SupplicantEvent::EVENT_ASSOCIATED)
62 rc = mHandlers->onConnectedEvent(evt); 74 mHandlers->onAssociatedEvent((SupplicantAssociatedEvent *) evt);
75 else if (evt->getType() == SupplicantEvent::EVENT_CONNECTED)
76 mHandlers->onConnectedEvent((SupplicantConnectedEvent *) evt);
77 else if (evt->getType() == SupplicantEvent::EVENT_SCAN_RESULTS)
78 mHandlers->onScanResultsEvent((SupplicantScanResultsEvent *) evt);
79 else if (evt->getType() == SupplicantEvent::EVENT_STATE_CHANGE)
80 mHandlers->onStateChangeEvent((SupplicantStateChangeEvent *) evt);
81 else if (evt->getType() == SupplicantEvent::EVENT_CONNECTIONTIMEOUT)
82 mHandlers->onConnectionTimeoutEvent((SupplicantConnectionTimeoutEvent *) evt);
63 else if (evt->getType() == SupplicantEvent::EVENT_DISCONNECTED) 83 else if (evt->getType() == SupplicantEvent::EVENT_DISCONNECTED)
64 rc = mHandlers->onDisconnectedEvent(evt); 84 mHandlers->onDisconnectedEvent((SupplicantDisconnectedEvent *) evt);
85 else
86 LOGW("Whoops - no handler available for event '%s'\n", buf);
87#if 0
65 else if (evt->getType() == SupplicantEvent::EVENT_TERMINATING) 88 else if (evt->getType() == SupplicantEvent::EVENT_TERMINATING)
66 rc = mHandlers->onTerminatingEvent(evt); 89 mHandlers->onTerminatingEvent(evt);
67 else if (evt->getType() == SupplicantEvent::EVENT_PASSWORD_CHANGED) 90 else if (evt->getType() == SupplicantEvent::EVENT_PASSWORD_CHANGED)
68 rc = mHandlers->onPasswordChangedEvent(evt); 91 mHandlers->onPasswordChangedEvent(evt);
69 else if (evt->getType() == SupplicantEvent::EVENT_EAP_NOTIFICATION) 92 else if (evt->getType() == SupplicantEvent::EVENT_EAP_NOTIFICATION)
70 rc = mHandlers->onEapNotificationEvent(evt); 93 mHandlers->onEapNotificationEvent(evt);
71 else if (evt->getType() == SupplicantEvent::EVENT_EAP_STARTED) 94 else if (evt->getType() == SupplicantEvent::EVENT_EAP_STARTED)
72 rc = mHandlers->onEapStartedEvent(evt); 95 mHandlers->onEapStartedEvent(evt);
73 else if (evt->getType() == SupplicantEvent::EVENT_EAP_SUCCESS) 96 else if (evt->getType() == SupplicantEvent::EVENT_EAP_SUCCESS)
74 rc = mHandlers->onEapSuccessEvent(evt); 97 mHandlers->onEapSuccessEvent(evt);
75 else if (evt->getType() == SupplicantEvent::EVENT_EAP_FAILURE) 98 else if (evt->getType() == SupplicantEvent::EVENT_EAP_FAILURE)
76 rc = mHandlers->onEapFailureEvent(evt); 99 mHandlers->onEapFailureEvent(evt);
77 else if (evt->getType() == SupplicantEvent::EVENT_SCAN_RESULTS)
78 rc = mHandlers->onScanResultsEvent(evt);
79 else if (evt->getType() == SupplicantEvent::EVENT_STATE_CHANGE)
80 rc = mHandlers->onStateChangeEvent(evt);
81 else if (evt->getType() == SupplicantEvent::EVENT_LINK_SPEED) 100 else if (evt->getType() == SupplicantEvent::EVENT_LINK_SPEED)
82 rc = mHandlers->onLinkSpeedEvent(evt); 101 mHandlers->onLinkSpeedEvent(evt);
83 else if (evt->getType() == SupplicantEvent::EVENT_DRIVER_STATE) 102 else if (evt->getType() == SupplicantEvent::EVENT_DRIVER_STATE)
84 rc = mHandlers->onDriverStateEvent(evt); 103 mHandlers->onDriverStateEvent(evt);
85 else { 104#endif
86 LOGW("Ignoring unknown event");
87 }
88 105
89 delete evt; 106 delete evt;
90 107
91 if (rc) {
92 LOGW("Handler %d (%s) error: %s", evt->getType(), evt->getEvent(), strerror(errno));
93 return false;
94 }
95 return true; 108 return true;
96} 109}
diff --git a/nexus/SupplicantListener.h b/nexus/SupplicantListener.h
index 3d186adfa..a1da7734e 100644
--- a/nexus/SupplicantListener.h
+++ b/nexus/SupplicantListener.h
@@ -23,12 +23,13 @@ struct wpa_ctrl;
23class Supplicant; 23class Supplicant;
24class SocketClient; 24class SocketClient;
25class ISupplicantEventHandler; 25class ISupplicantEventHandler;
26class SupplicantEventFactory;
26 27
27class SupplicantListener: public SocketListener { 28class SupplicantListener: public SocketListener {
28private:
29 struct wpa_ctrl *mMonitor; 29 struct wpa_ctrl *mMonitor;
30 ISupplicantEventHandler *mHandlers; 30 ISupplicantEventHandler *mHandlers;
31 31 SupplicantEventFactory *mFactory;
32
32public: 33public:
33 SupplicantListener(ISupplicantEventHandler *handlers, 34 SupplicantListener(ISupplicantEventHandler *handlers,
34 struct wpa_ctrl *monitor); 35 struct wpa_ctrl *monitor);
diff --git a/nexus/SupplicantScanResultsEvent.cpp b/nexus/SupplicantScanResultsEvent.cpp
new file mode 100644
index 000000000..c53adad2d
--- /dev/null
+++ b/nexus/SupplicantScanResultsEvent.cpp
@@ -0,0 +1,34 @@
1/*
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
17#define LOG_TAG "SupplicantScanResultsEvent"
18#include <cutils/log.h>
19
20#include "SupplicantScanResultsEvent.h"
21
22SupplicantScanResultsEvent::SupplicantScanResultsEvent(int level, char *event,
23 size_t len) :
24 SupplicantEvent(SupplicantEvent::EVENT_SCAN_RESULTS,
25 level) {
26}
27
28SupplicantScanResultsEvent::SupplicantScanResultsEvent() :
29 SupplicantEvent(SupplicantEvent::EVENT_SCAN_RESULTS, -1) {
30}
31
32SupplicantScanResultsEvent::~SupplicantScanResultsEvent() {
33}
34
diff --git a/nexus/SupplicantScanResultsEvent.h b/nexus/SupplicantScanResultsEvent.h
new file mode 100644
index 000000000..5f82041f8
--- /dev/null
+++ b/nexus/SupplicantScanResultsEvent.h
@@ -0,0 +1,30 @@
1/*
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
17#ifndef _SupplicantScanResultsEvent_H
18#define _SupplicantScanResultsEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantScanResultsEvent : public SupplicantEvent {
23
24public:
25 SupplicantScanResultsEvent(int level, char *event, size_t len);
26 SupplicantScanResultsEvent();
27 virtual ~SupplicantScanResultsEvent();
28};
29
30#endif
diff --git a/nexus/SupplicantStateChangeEvent.cpp b/nexus/SupplicantStateChangeEvent.cpp
new file mode 100644
index 000000000..cf0b9daa6
--- /dev/null
+++ b/nexus/SupplicantStateChangeEvent.cpp
@@ -0,0 +1,45 @@
1/*
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
17#include <stdlib.h>
18
19#define LOG_TAG "SupplicantStateChangeEvent"
20#include <cutils/log.h>
21
22#include "SupplicantStateChangeEvent.h"
23
24SupplicantStateChangeEvent::SupplicantStateChangeEvent(int level, char *event,
25 size_t len) :
26 SupplicantEvent(SupplicantEvent::EVENT_STATE_CHANGE,
27 level) {
28 // XXX: move this stuff into a static creation method
29 char *p = index(event, ' ');
30 if (!p) {
31 LOGW("Bad event '%s'\n", event);
32 return;
33 }
34
35 mState = atoi(p + strlen("state=") + 1);
36}
37
38SupplicantStateChangeEvent::SupplicantStateChangeEvent(int state) :
39 SupplicantEvent(SupplicantEvent::EVENT_STATE_CHANGE, -1) {
40 mState = state;
41}
42
43SupplicantStateChangeEvent::~SupplicantStateChangeEvent() {
44}
45
diff --git a/nexus/SupplicantStateChangeEvent.h b/nexus/SupplicantStateChangeEvent.h
new file mode 100644
index 000000000..77bff654a
--- /dev/null
+++ b/nexus/SupplicantStateChangeEvent.h
@@ -0,0 +1,34 @@
1/*
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
17#ifndef _SupplicantStateChangeEvent_H
18#define _SupplicantStateChangeEvent_H
19
20#include "SupplicantEvent.h"
21
22class SupplicantStateChangeEvent : public SupplicantEvent {
23private:
24 int mState;
25
26public:
27 SupplicantStateChangeEvent(int level, char *event, size_t len);
28 SupplicantStateChangeEvent(int state);
29 virtual ~SupplicantStateChangeEvent();
30
31 int getState() { return mState; }
32};
33
34#endif
diff --git a/nexus/SupplicantStatus.cpp b/nexus/SupplicantStatus.cpp
new file mode 100644
index 000000000..87f6c98dc
--- /dev/null
+++ b/nexus/SupplicantStatus.cpp
@@ -0,0 +1,72 @@
1/*
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
17#include <stdlib.h>
18#include <string.h>
19
20#define LOG_TAG "SupplicantState"
21#include <cutils/log.h>
22
23#include "SupplicantStatus.h"
24#include "SupplicantState.h"
25
26SupplicantStatus::SupplicantStatus() {
27 mWpaState = SupplicantState::UNKNOWN;
28 mId = -1;
29 mBssid = NULL;
30 mSsid = NULL;
31}
32
33SupplicantStatus::SupplicantStatus(int state, int id, char *bssid, char *ssid) :
34 mWpaState(state), mId(id), mBssid(bssid), mSsid(ssid) {
35
36LOGD("state %d, id %d, bssid %p, ssid %p\n", mWpaState, mId, mBssid, mSsid);
37}
38
39SupplicantStatus::~SupplicantStatus() {
40 if (mBssid)
41 free(mBssid);
42 if (mSsid)
43 free(mSsid);
44}
45
46SupplicantStatus *SupplicantStatus::createStatus(char *data, int len) {
47 char *bssid = NULL;
48 char *ssid = NULL;
49 int id = -1;
50 int state = SupplicantState::UNKNOWN;
51
52 char *next = data;
53 char *line;
54 while((line = strsep(&next, "\n"))) {
55 char *token = strsep(&next, "=");
56 char *value = strsep(&next, "=");
57
58 if (!strcmp(token, "bssid"))
59 bssid = strdup(value);
60 else if (!strcmp(token, "ssid"))
61 ssid = strdup(value);
62 else if (!strcmp(token, "id"))
63 id = atoi(value);
64 else if (!strcmp(token, "wpa_state"))
65 state = atoi(value);
66 else
67 LOGD("Ignoring unsupported status token '%s'", token);
68 }
69
70 return new SupplicantStatus(state, id, bssid, ssid);
71
72}
diff --git a/nexus/SupplicantStatus.h b/nexus/SupplicantStatus.h
new file mode 100644
index 000000000..ef018410e
--- /dev/null
+++ b/nexus/SupplicantStatus.h
@@ -0,0 +1,42 @@
1/*
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
17#ifndef _SupplicantStatus_H
18#define _SupplicantStatus_H
19
20class SupplicantStatus {
21private:
22 int mWpaState;
23 int mId;
24 char *mBssid;
25 char *mSsid;
26
27private:
28 SupplicantStatus();
29 SupplicantStatus(int state, int id, char *bssid, char *ssid);
30
31public:
32 virtual ~SupplicantStatus();
33 static SupplicantStatus *createStatus(char *data, int len);
34
35 int getWpaState() { return mWpaState; }
36 int getId() { return mId; }
37 const char *getBssid() { return mBssid; }
38 const char *getSsid() { return mSsid; }
39
40};
41
42#endif
diff --git a/nexus/TiwlanWifiController.cpp b/nexus/TiwlanWifiController.cpp
index 307c48c0d..6945e3ea2 100644
--- a/nexus/TiwlanWifiController.cpp
+++ b/nexus/TiwlanWifiController.cpp
@@ -30,8 +30,12 @@
30 30
31extern "C" int sched_yield(void); 31extern "C" int sched_yield(void);
32 32
33TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) : 33TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr,
34 WifiController(propmngr, modpath, modname, modargs) { 34 IControllerHandler *handlers,
35 char *modpath, char *modname,
36 char *modargs) :
37 WifiController(propmngr, handlers, modpath, modname,
38 modargs) {
35} 39}
36 40
37int TiwlanWifiController::powerUp() { 41int TiwlanWifiController::powerUp() {
diff --git a/nexus/TiwlanWifiController.h b/nexus/TiwlanWifiController.h
index d3ebe88fb..852a288b2 100644
--- a/nexus/TiwlanWifiController.h
+++ b/nexus/TiwlanWifiController.h
@@ -20,9 +20,11 @@
20#include "PropertyManager.h" 20#include "PropertyManager.h"
21#include "WifiController.h" 21#include "WifiController.h"
22 22
23class IControllerHandler;
24
23class TiwlanWifiController : public WifiController { 25class TiwlanWifiController : public WifiController {
24public: 26public:
25 TiwlanWifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs); 27 TiwlanWifiController(PropertyManager *propmngr, IControllerHandler *handlers, char *modpath, char *modname, char *modargs);
26 virtual ~TiwlanWifiController() {} 28 virtual ~TiwlanWifiController() {}
27 29
28 virtual int powerUp(); 30 virtual int powerUp();
diff --git a/nexus/VpnController.cpp b/nexus/VpnController.cpp
index 1246703ab..add4dc39a 100644
--- a/nexus/VpnController.cpp
+++ b/nexus/VpnController.cpp
@@ -25,18 +25,19 @@
25#include "PropertyManager.h" 25#include "PropertyManager.h"
26#include "VpnController.h" 26#include "VpnController.h"
27 27
28VpnController::VpnController(PropertyManager *propmngr) : 28VpnController::VpnController(PropertyManager *propmngr,
29 Controller("VPN", propmngr) { 29 IControllerHandler *handlers) :
30 Controller("VPN", propmngr, handlers) {
30 mEnabled = false; 31 mEnabled = false;
31 propmngr->registerProperty("vpn.enabled", this);
32 propmngr->registerProperty("vpn.gateway", this);
33} 32}
34 33
35int VpnController::start() { 34int VpnController::start() {
35 mPropMngr->registerProperty("vpn.enabled", this);
36 return 0; 36 return 0;
37} 37}
38 38
39int VpnController::stop() { 39int VpnController::stop() {
40 mPropMngr->unregisterProperty("vpn.enabled");
40 return 0; 41 return 0;
41} 42}
42 43
@@ -49,8 +50,13 @@ int VpnController::set(const char *name, const char *value) {
49 return 0; 50 return 0;
50 rc = (en ? enable() : disable()); 51 rc = (en ? enable() : disable());
51 52
52 if (!rc) 53 if (!rc) {
53 mEnabled = en; 54 mEnabled = en;
55 if (en)
56 mPropMngr->unregisterProperty("vpn.gateway");
57 else
58 mPropMngr->unregisterProperty("vpn.gateway");
59 }
54 return rc; 60 return rc;
55 } if (!strcmp(name, "vpn.gateway")) { 61 } if (!strcmp(name, "vpn.gateway")) {
56 if (!inet_aton(value, &mVpnGateway)) { 62 if (!inet_aton(value, &mVpnGateway)) {
diff --git a/nexus/VpnController.h b/nexus/VpnController.h
index b36856fa1..1af4d9fbc 100644
--- a/nexus/VpnController.h
+++ b/nexus/VpnController.h
@@ -21,6 +21,8 @@
21 21
22#include "Controller.h" 22#include "Controller.h"
23 23
24class IControllerHandler;
25
24class VpnController : public Controller { 26class VpnController : public Controller {
25 bool mEnabled; 27 bool mEnabled;
26 /* 28 /*
@@ -29,7 +31,7 @@ class VpnController : public Controller {
29 struct in_addr mVpnGateway; 31 struct in_addr mVpnGateway;
30 32
31public: 33public:
32 VpnController(PropertyManager *propmngr); 34 VpnController(PropertyManager *propmngr, IControllerHandler *handlers);
33 virtual ~VpnController() {} 35 virtual ~VpnController() {}
34 36
35 virtual int start(); 37 virtual int start();
diff --git a/nexus/WifiController.cpp b/nexus/WifiController.cpp
index 3d068061b..5c5db1a1d 100644
--- a/nexus/WifiController.cpp
+++ b/nexus/WifiController.cpp
@@ -27,34 +27,50 @@
27#include "NetworkManager.h" 27#include "NetworkManager.h"
28#include "ErrorCode.h" 28#include "ErrorCode.h"
29#include "WifiNetwork.h" 29#include "WifiNetwork.h"
30 30#include "ISupplicantEventHandler.h"
31WifiController::WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) : 31#include "SupplicantState.h"
32 Controller("WIFI", propmngr) { 32#include "SupplicantStatus.h"
33#include "SupplicantAssociatingEvent.h"
34#include "SupplicantAssociatedEvent.h"
35#include "SupplicantConnectedEvent.h"
36#include "SupplicantScanResultsEvent.h"
37#include "SupplicantStateChangeEvent.h"
38#include "SupplicantConnectionTimeoutEvent.h"
39#include "SupplicantDisconnectedEvent.h"
40
41WifiController::WifiController(PropertyManager *mPropMngr,
42 IControllerHandler *handlers,
43 char *modpath, char *modname, char *modargs) :
44 Controller("WIFI", mPropMngr, handlers) {
33 strncpy(mModulePath, modpath, sizeof(mModulePath)); 45 strncpy(mModulePath, modpath, sizeof(mModulePath));
34 strncpy(mModuleName, modname, sizeof(mModuleName)); 46 strncpy(mModuleName, modname, sizeof(mModuleName));
35 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); 47 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
36 48
37 mSupplicant = new Supplicant(this, propmngr); 49 mLatestScanResults = new ScanResultCollection();
50 pthread_mutex_init(&mLatestScanResultsLock, NULL);
51
52 mSupplicant = new Supplicant(this, this);
38 mScanner = new WifiScanner(mSupplicant, 10); 53 mScanner = new WifiScanner(mSupplicant, 10);
39 mCurrentScanMode = 0; 54 mCurrentScanMode = 0;
40 55
41 mEnabled = false; 56 mEnabled = false;
42 57
43 propmngr->registerProperty("wifi.enabled", this); 58 mSupplicantState = SupplicantState::UNKNOWN;
44} 59}
45 60
46int WifiController::start() { 61int WifiController::start() {
62 mPropMngr->registerProperty("wifi.enabled", this);
47 return 0; 63 return 0;
48} 64}
49 65
50int WifiController::stop() { 66int WifiController::stop() {
51 errno = ENOSYS; 67 mPropMngr->unregisterProperty("wifi.enabled");
52 return -1; 68 return 0;
53} 69}
54 70
55int WifiController::enable() { 71int WifiController::enable() {
56 if (!isPoweredUp()) { 72 if (!isPoweredUp()) {
57 sendStatusBroadcast("POWERING_UP"); 73 sendStatusBroadcast("Powering up WiFi hardware");
58 if (powerUp()) { 74 if (powerUp()) {
59 LOGE("Powerup failed (%s)", strerror(errno)); 75 LOGE("Powerup failed (%s)", strerror(errno));
60 return -1; 76 return -1;
@@ -62,7 +78,7 @@ int WifiController::enable() {
62 } 78 }
63 79
64 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { 80 if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
65 sendStatusBroadcast("LOADING_DRIVER"); 81 sendStatusBroadcast("Loading WiFi driver");
66 if (loadKernelModule(mModulePath, mModuleArgs)) { 82 if (loadKernelModule(mModulePath, mModuleArgs)) {
67 LOGE("Kernel module load failed (%s)", strerror(errno)); 83 LOGE("Kernel module load failed (%s)", strerror(errno));
68 goto out_powerdown; 84 goto out_powerdown;
@@ -70,7 +86,7 @@ int WifiController::enable() {
70 } 86 }
71 87
72 if (!isFirmwareLoaded()) { 88 if (!isFirmwareLoaded()) {
73 sendStatusBroadcast("LOADING_FIRMWARE"); 89 sendStatusBroadcast("Loading WiFI firmware");
74 if (loadFirmware()) { 90 if (loadFirmware()) {
75 LOGE("Firmware load failed (%s)", strerror(errno)); 91 LOGE("Firmware load failed (%s)", strerror(errno));
76 goto out_powerdown; 92 goto out_powerdown;
@@ -78,7 +94,7 @@ int WifiController::enable() {
78 } 94 }
79 95
80 if (!mSupplicant->isStarted()) { 96 if (!mSupplicant->isStarted()) {
81 sendStatusBroadcast("STARTING_SUPPLICANT"); 97 sendStatusBroadcast("Starting WPA Supplicant");
82 if (mSupplicant->start()) { 98 if (mSupplicant->start()) {
83 LOGE("Supplicant start failed (%s)", strerror(errno)); 99 LOGE("Supplicant start failed (%s)", strerror(errno));
84 goto out_unloadmodule; 100 goto out_unloadmodule;
@@ -93,6 +109,7 @@ int WifiController::enable() {
93 if (mSupplicant->refreshNetworkList()) 109 if (mSupplicant->refreshNetworkList())
94 LOGW("Error getting list of networks (%s)", strerror(errno)); 110 LOGW("Error getting list of networks (%s)", strerror(errno));
95 111
112 mPropMngr->registerProperty("wifi.supplicant.state", this);
96 mPropMngr->registerProperty("wifi.scanmode", this); 113 mPropMngr->registerProperty("wifi.scanmode", this);
97 mPropMngr->registerProperty("wifi.interface", this); 114 mPropMngr->registerProperty("wifi.interface", this);
98 115
@@ -121,8 +138,11 @@ void WifiController::sendStatusBroadcast(const char *msg) {
121int WifiController::disable() { 138int WifiController::disable() {
122 139
123 mPropMngr->unregisterProperty("wifi.scanmode"); 140 mPropMngr->unregisterProperty("wifi.scanmode");
141 mPropMngr->unregisterProperty("wifi.supplicant.state");
142 mPropMngr->unregisterProperty("wifi.scanmode");
143
124 if (mSupplicant->isStarted()) { 144 if (mSupplicant->isStarted()) {
125 sendStatusBroadcast("STOPPING_SUPPLICANT"); 145 sendStatusBroadcast("Stopping WPA Supplicant");
126 if (mSupplicant->stop()) { 146 if (mSupplicant->stop()) {
127 LOGE("Supplicant stop failed (%s)", strerror(errno)); 147 LOGE("Supplicant stop failed (%s)", strerror(errno));
128 return -1; 148 return -1;
@@ -131,7 +151,7 @@ int WifiController::disable() {
131 LOGW("disable(): Supplicant not running?"); 151 LOGW("disable(): Supplicant not running?");
132 152
133 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { 153 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
134 sendStatusBroadcast("UNLOADING_DRIVER"); 154 sendStatusBroadcast("Unloading WiFi driver");
135 if (unloadKernelModule(mModuleName)) { 155 if (unloadKernelModule(mModuleName)) {
136 LOGE("Unable to unload module (%s)", strerror(errno)); 156 LOGE("Unable to unload module (%s)", strerror(errno));
137 return -1; 157 return -1;
@@ -139,7 +159,7 @@ int WifiController::disable() {
139 } 159 }
140 160
141 if (isPoweredUp()) { 161 if (isPoweredUp()) {
142 sendStatusBroadcast("POWERING_DOWN"); 162 sendStatusBroadcast("Powering down WiFi hardware");
143 if (powerDown()) { 163 if (powerDown()) {
144 LOGE("Powerdown failed (%s)", strerror(errno)); 164 LOGE("Powerdown failed (%s)", strerror(errno));
145 return -1; 165 return -1;
@@ -184,7 +204,15 @@ int WifiController::removeNetwork(int networkId) {
184} 204}
185 205
186ScanResultCollection *WifiController::createScanResults() { 206ScanResultCollection *WifiController::createScanResults() {
187 return mSupplicant->createLatestScanResults(); 207 ScanResultCollection *d = new ScanResultCollection();
208 ScanResultCollection::iterator i;
209
210 pthread_mutex_lock(&mLatestScanResultsLock);
211 for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i)
212 d->push_back((*i)->clone());
213
214 pthread_mutex_unlock(&mLatestScanResultsLock);
215 return d;
188} 216}
189 217
190WifiNetworkCollection *WifiController::createNetworkList() { 218WifiNetworkCollection *WifiController::createNetworkList() {
@@ -207,7 +235,10 @@ int WifiController::set(const char *name, const char *value) {
207 return -1; 235 return -1;
208 } else if (!strcmp(name, "wifi.scanmode")) 236 } else if (!strcmp(name, "wifi.scanmode"))
209 return setScanMode((uint32_t) strtoul(value, NULL, 0)); 237 return setScanMode((uint32_t) strtoul(value, NULL, 0));
210 else 238 else if (!strcmp(name, "wifi.supplicant.state")) {
239 errno = EROFS;
240 return -1;
241 } else
211 return Controller::set(name, value); 242 return Controller::set(name, value);
212 return rc; 243 return rc;
213} 244}
@@ -221,9 +252,158 @@ const char *WifiController::get(const char *name, char *buffer, size_t maxsize)
221 (getBoundInterface() ? getBoundInterface() : "none")); 252 (getBoundInterface() ? getBoundInterface() : "none"));
222 } else if (!strcmp(name, "wifi.scanmode")) 253 } else if (!strcmp(name, "wifi.scanmode"))
223 snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode); 254 snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode);
255 else if (!strcmp(name, "wifi.supplicant.state"))
256 return SupplicantState::toString(mSupplicantState, buffer, maxsize);
224 else 257 else
225 return Controller::get(name, buffer, maxsize); 258 return Controller::get(name, buffer, maxsize);
226 259
227 return buffer; 260 return buffer;
228} 261}
229 262
263void WifiController::onAssociatingEvent(SupplicantAssociatingEvent *evt) {
264 LOGD("onAssociatingEvent(%s, %s, %d)",
265 (evt->getBssid() ? evt->getBssid() : "n/a"),
266 (evt->getSsid() ? evt->getSsid() : "n/a"),
267 evt->getFreq());
268}
269
270void WifiController::onAssociatedEvent(SupplicantAssociatedEvent *evt) {
271 LOGD("onAssociatedEvent(%s)", evt->getBssid());
272}
273
274void WifiController::onConnectedEvent(SupplicantConnectedEvent *evt) {
275 LOGD("onConnectedEvent(%s, %d)", evt->getBssid(), evt->getReassociated());
276 if (!evt->getReassociated()) {
277 SupplicantStatus *ss = mSupplicant->getStatus();
278 WifiNetwork *wn;
279
280 if (ss->getWpaState() != SupplicantState::COMPLETED) {
281 char tmp[32];
282
283 LOGW("onConnected() with SupplicantState = %s!",
284 SupplicantState::toString(ss->getWpaState(), tmp,
285 sizeof(tmp)));
286 return;
287 }
288
289 if (ss->getId() == -1) {
290 LOGW("onConnected() with id = -1!");
291 return;
292 }
293
294 if (!(wn = mSupplicant->lookupNetwork(ss->getId()))) {
295 LOGW("Error looking up connected network id %d (%s)",
296 ss->getId(), strerror(errno));
297 return;
298 }
299
300 delete ss;
301 mHandlers->onInterfaceStarted(this, wn->getIfaceCfg());
302 }
303}
304
305void WifiController::onScanResultsEvent(SupplicantScanResultsEvent *evt) {
306 char *reply;
307
308 if (!(reply = (char *) malloc(4096))) {
309 LOGE("Out of memory");
310 return;
311 }
312
313 size_t len = 4096;
314
315 if (mSupplicant->sendCommand("SCAN_RESULTS", reply, &len)) {
316 LOGW("onScanResultsEvent: Error getting scan results (%s)",
317 strerror(errno));
318 free(reply);
319 return;
320 }
321
322 pthread_mutex_lock(&mLatestScanResultsLock);
323 if (!mLatestScanResults->empty()) {
324 ScanResultCollection::iterator i;
325
326 for (i = mLatestScanResults->begin();
327 i !=mLatestScanResults->end(); ++i) {
328 delete *i;
329 }
330 mLatestScanResults->clear();
331 }
332
333 char *linep;
334 char *linep_next = NULL;
335
336 if (!strtok_r(reply, "\n", &linep_next)) {
337 free(reply);
338 pthread_mutex_unlock(&mLatestScanResultsLock);
339 return;
340 }
341
342 while((linep = strtok_r(NULL, "\n", &linep_next)))
343 mLatestScanResults->push_back(new ScanResult(linep));
344
345 char *tmp;
346 asprintf(&tmp, "Scan results ready (%d)", mLatestScanResults->size());
347 NetworkManager::Instance()->getBroadcaster()->
348 sendBroadcast(ErrorCode::UnsolicitedInformational, tmp, false);
349 free(tmp);
350 pthread_mutex_unlock(&mLatestScanResultsLock);
351 free(reply);
352}
353
354void WifiController::onStateChangeEvent(SupplicantStateChangeEvent *evt) {
355 char tmp[32];
356 char tmp2[32];
357
358 LOGD("onStateChangeEvent(%s -> %s)",
359 SupplicantState::toString(mSupplicantState, tmp, sizeof(tmp)),
360 SupplicantState::toString(evt->getState(), tmp2, sizeof(tmp2)));
361
362 mSupplicantState = evt->getState();
363}
364
365void WifiController::onConnectionTimeoutEvent(SupplicantConnectionTimeoutEvent *evt) {
366 LOGD("onConnectionTimeoutEvent(%s)", evt->getBssid());
367}
368
369void WifiController::onDisconnectedEvent(SupplicantDisconnectedEvent *evt) {
370 LOGD("onDisconnectedEvent()");
371}
372
373#if 0
374void WifiController::onTerminatingEvent(SupplicantEvent *evt) {
375 LOGD("onTerminatingEvent(%s)", evt->getEvent());
376}
377
378void WifiController::onPasswordChangedEvent(SupplicantEvent *evt) {
379 LOGD("onPasswordChangedEvent(%s)", evt->getEvent());
380}
381
382void WifiController::onEapNotificationEvent(SupplicantEvent *evt) {
383 LOGD("onEapNotificationEvent(%s)", evt->getEvent());
384}
385
386void WifiController::onEapStartedEvent(SupplicantEvent *evt) {
387 LOGD("onEapStartedEvent(%s)", evt->getEvent());
388}
389
390void WifiController::onEapMethodEvent(SupplicantEvent *evt) {
391 LOGD("onEapMethodEvent(%s)", evt->getEvent());
392}
393
394void WifiController::onEapSuccessEvent(SupplicantEvent *evt) {
395 LOGD("onEapSuccessEvent(%s)", evt->getEvent());
396}
397
398void WifiController::onEapFailureEvent(SupplicantEvent *evt) {
399 LOGD("onEapFailureEvent(%s)", evt->getEvent());
400}
401
402void WifiController::onLinkSpeedEvent(SupplicantEvent *evt) {
403 LOGD("onLinkSpeedEvent(%s)", evt->getEvent());
404}
405
406void WifiController::onDriverStateEvent(SupplicantEvent *evt) {
407 LOGD("onDriverStateEvent(%s)", evt->getEvent());
408}
409#endif
diff --git a/nexus/WifiController.h b/nexus/WifiController.h
index b2f453070..c61d97a5a 100644
--- a/nexus/WifiController.h
+++ b/nexus/WifiController.h
@@ -20,15 +20,21 @@
20#include <sys/types.h> 20#include <sys/types.h>
21 21
22#include "Controller.h" 22#include "Controller.h"
23#include "ScanResult.h"
24#include "WifiNetwork.h"
25#include "ISupplicantEventHandler.h"
23 26
24class NetInterface; 27class NetInterface;
25class Supplicant; 28class Supplicant;
26class WifiScanner; 29class WifiScanner;
27 30class SupplicantAssociatingEvent;
28#include "ScanResult.h" 31class SupplicantAssociatedEvent;
29#include "WifiNetwork.h" 32class SupplicantConnectedEvent;
30 33class SupplicantScanResultsEvent;
31class WifiController : public Controller { 34class SupplicantStateChangeEvent;
35class SupplicantDisconnectedEvent;
36
37class WifiController : public Controller, public ISupplicantEventHandler {
32public: 38public:
33 static const uint32_t SCAN_ENABLE_MASK = 0x01; 39 static const uint32_t SCAN_ENABLE_MASK = 0x01;
34 static const uint32_t SCAN_ACTIVE_MASK = 0x02; 40 static const uint32_t SCAN_ACTIVE_MASK = 0x02;
@@ -45,12 +51,18 @@ private:
45 char mModulePath[255]; 51 char mModulePath[255];
46 char mModuleName[64]; 52 char mModuleName[64];
47 char mModuleArgs[255]; 53 char mModuleArgs[255];
54
48 uint32_t mCurrentScanMode; 55 uint32_t mCurrentScanMode;
49 WifiScanner *mScanner; 56 WifiScanner *mScanner;
57 int mSupplicantState;
58
59 ScanResultCollection *mLatestScanResults;
60 pthread_mutex_t mLatestScanResultsLock;
61
50 bool mEnabled; 62 bool mEnabled;
51 63
52public: 64public:
53 WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs); 65 WifiController(PropertyManager *propmngr, IControllerHandler *handlers, char *modpath, char *modname, char *modargs);
54 virtual ~WifiController() {} 66 virtual ~WifiController() {}
55 67
56 int start(); 68 int start();
@@ -85,6 +97,27 @@ private:
85 int setScanMode(uint32_t mode); 97 int setScanMode(uint32_t mode);
86 int enable(); 98 int enable();
87 int disable(); 99 int disable();
100
101 // ISupplicantEventHandler methods
102 virtual void onAssociatingEvent(SupplicantAssociatingEvent *evt);
103 virtual void onAssociatedEvent(SupplicantAssociatedEvent *evt);
104 virtual void onConnectedEvent(SupplicantConnectedEvent *evt);
105 virtual void onScanResultsEvent(SupplicantScanResultsEvent *evt);
106 virtual void onStateChangeEvent(SupplicantStateChangeEvent *evt);
107 virtual void onConnectionTimeoutEvent(SupplicantConnectionTimeoutEvent *evt);
108 virtual void onDisconnectedEvent(SupplicantDisconnectedEvent *evt);
109#if 0
110 virtual void onTerminatingEvent(SupplicantEvent *evt);
111 virtual void onPasswordChangedEvent(SupplicantEvent *evt);
112 virtual void onEapNotificationEvent(SupplicantEvent *evt);
113 virtual void onEapStartedEvent(SupplicantEvent *evt);
114 virtual void onEapMethodEvent(SupplicantEvent *evt);
115 virtual void onEapSuccessEvent(SupplicantEvent *evt);
116 virtual void onEapFailureEvent(SupplicantEvent *evt);
117 virtual void onLinkSpeedEvent(SupplicantEvent *evt);
118 virtual void onDriverStateEvent(SupplicantEvent *evt);
119#endif
120
88}; 121};
89 122
90#endif 123#endif
diff --git a/nexus/WifiNetwork.cpp b/nexus/WifiNetwork.cpp
index 818b91d95..7059bd04b 100644
--- a/nexus/WifiNetwork.cpp
+++ b/nexus/WifiNetwork.cpp
@@ -76,7 +76,7 @@ WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, const char *data)
76 mDefaultKeyIndex = -1; 76 mDefaultKeyIndex = -1;
77 mPriority = -1; 77 mPriority = -1;
78 mHiddenSsid = NULL; 78 mHiddenSsid = NULL;
79 mAllowedKeyManagement = 0; 79 mAllowedKeyManagement = KeyManagementMask::UNKNOWN;
80 mAllowedProtocols = 0; 80 mAllowedProtocols = 0;
81 mAllowedAuthAlgorithms = 0; 81 mAllowedAuthAlgorithms = 0;
82 mAllowedPairwiseCiphers = 0; 82 mAllowedPairwiseCiphers = 0;
@@ -94,8 +94,6 @@ WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, const char *data)
94 asprintf(&tmp2, "wifi.net.%d", mNetid); 94 asprintf(&tmp2, "wifi.net.%d", mNetid);
95 mIfaceCfg = new InterfaceConfig(tmp2); 95 mIfaceCfg = new InterfaceConfig(tmp2);
96 free(tmp2); 96 free(tmp2);
97
98 registerProperties();
99 free(tmp); 97 free(tmp);
100} 98}
101 99
@@ -121,8 +119,6 @@ WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, int networkId) {
121 asprintf(&tmp2, "wifi.net.%d", mNetid); 119 asprintf(&tmp2, "wifi.net.%d", mNetid);
122 mIfaceCfg = new InterfaceConfig(tmp2); 120 mIfaceCfg = new InterfaceConfig(tmp2);
123 free(tmp2); 121 free(tmp2);
124
125 registerProperties();
126} 122}
127 123
128WifiNetwork *WifiNetwork::clone() { 124WifiNetwork *WifiNetwork::clone() {
@@ -153,7 +149,6 @@ WifiNetwork *WifiNetwork::clone() {
153} 149}
154 150
155WifiNetwork::~WifiNetwork() { 151WifiNetwork::~WifiNetwork() {
156 unregisterProperties();
157 if (mSsid) 152 if (mSsid)
158 free(mSsid); 153 free(mSsid);
159 if (mBssid) 154 if (mBssid)
@@ -203,7 +198,26 @@ int WifiNetwork::refresh() {
203 198
204 len = sizeof(buffer); 199 len = sizeof(buffer);
205 if (mSuppl->getNetworkVar(mNetid, "key_mgmt", buffer, len)) { 200 if (mSuppl->getNetworkVar(mNetid, "key_mgmt", buffer, len)) {
206 // TODO 201 if (!strcmp(buffer, "NONE"))
202 setAllowedKeyManagement(KeyManagementMask::NONE);
203 else if (index(buffer, ' ')) {
204 char *next = buffer;
205 char *token;
206 uint32_t mask = 0;
207
208 while((token = strsep(&next, " "))) {
209 if (!strcmp(token, "WPA-PSK"))
210 mask |= KeyManagementMask::WPA_PSK;
211 else if (!strcmp(token, "WPA-EAP"))
212 mask |= KeyManagementMask::WPA_EAP;
213 else if (!strcmp(token, "IEE8021X"))
214 mask |= KeyManagementMask::IEEE8021X;
215 else
216 LOGW("Unsupported key management scheme '%s'" , token);
217 }
218 setAllowedKeyManagement(mask);
219 } else
220 LOGE("Unsupported key management '%s'", buffer);
207 } 221 }
208 222
209 len = sizeof(buffer); 223 len = sizeof(buffer);
@@ -273,7 +287,7 @@ int WifiNetwork::set(const char *name, const char *value) {
273 287
274 while((v_token = strsep(&v_next, " "))) { 288 while((v_token = strsep(&v_next, " "))) {
275 if (!strcasecmp(v_token, "NONE")) { 289 if (!strcasecmp(v_token, "NONE")) {
276 mask = 0; 290 mask = KeyManagementMask::NONE;
277 none = true; 291 none = true;
278 } else if (!none) { 292 } else if (!none) {
279 if (!strcasecmp(v_token, "WPA_PSK")) 293 if (!strcasecmp(v_token, "WPA_PSK"))
@@ -363,7 +377,29 @@ const char *WifiNetwork::get(const char *name, char *buffer, size_t maxsize) {
363 snprintf(buffer, maxsize, "%d", getDefaultKeyIndex()); 377 snprintf(buffer, maxsize, "%d", getDefaultKeyIndex());
364 else if (!strcasecmp(fc, "pri")) 378 else if (!strcasecmp(fc, "pri"))
365 snprintf(buffer, maxsize, "%d", getPriority()); 379 snprintf(buffer, maxsize, "%d", getPriority());
366 else if (!strcasecmp(fc, "hiddenssid")) { 380 else if (!strcasecmp(fc, "AllowedKeyManagement")) {
381 if (getAllowedKeyManagement() == KeyManagementMask::NONE)
382 strncpy(buffer, "NONE", maxsize);
383 else {
384 char tmp[80] = { '\0' };
385
386 if (getAllowedKeyManagement() & KeyManagementMask::WPA_PSK)
387 strcat(tmp, "WPA_PSK ");
388 if (getAllowedKeyManagement() & KeyManagementMask::WPA_EAP)
389 strcat(tmp, "WPA_EAP ");
390 if (getAllowedKeyManagement() & KeyManagementMask::IEEE8021X)
391 strcat(tmp, "IEEE8021X");
392 if (tmp[0] == '\0') {
393 strncpy(buffer, "(internal error)", maxsize);
394 errno = ENOENT;
395 return NULL;
396 }
397 if (tmp[strlen(tmp)] == ' ')
398 tmp[strlen(tmp)] = '\0';
399
400 strncpy(buffer, tmp, maxsize);
401 }
402 } else if (!strcasecmp(fc, "hiddenssid")) {
367 strncpy(buffer, 403 strncpy(buffer,
368 getHiddenSsid() ? getHiddenSsid() : "none", 404 getHiddenSsid() ? getHiddenSsid() : "none",
369 maxsize); 405 maxsize);
diff --git a/nexus/WifiNetwork.h b/nexus/WifiNetwork.h
index 360ccc28c..c2f5d235a 100644
--- a/nexus/WifiNetwork.h
+++ b/nexus/WifiNetwork.h
@@ -157,8 +157,6 @@ private:
157 157
158private: 158private:
159 WifiNetwork(); 159 WifiNetwork();
160 int registerProperties();
161 int unregisterProperties();
162 160
163public: 161public:
164 WifiNetwork(WifiController *c, Supplicant *suppl, int networkId); 162 WifiNetwork(WifiController *c, Supplicant *suppl, int networkId);
@@ -167,6 +165,8 @@ public:
167 virtual ~WifiNetwork(); 165 virtual ~WifiNetwork();
168 166
169 WifiNetwork *clone(); 167 WifiNetwork *clone();
168 int registerProperties();
169 int unregisterProperties();
170 170
171 int getNetworkId() { return mNetid; } 171 int getNetworkId() { return mNetid; }
172 const char *getSsid() { return mSsid; } 172 const char *getSsid() { return mSsid; }
@@ -187,7 +187,7 @@ public:
187 int set(const char *name, const char *value); 187 int set(const char *name, const char *value);
188 const char *get(const char *name, char *buffer, size_t maxsize); 188 const char *get(const char *name, char *buffer, size_t maxsize);
189 189
190// InterfaceConfig *getIfaceCfg() { return mIfaceCfg; } 190 InterfaceConfig *getIfaceCfg() { return mIfaceCfg; }
191 191
192 int setEnabled(bool enabled); 192 int setEnabled(bool enabled);
193 int setSsid(const char *ssid); 193 int setSsid(const char *ssid);
diff --git a/nexus/main.cpp b/nexus/main.cpp
index e460d4209..936d33f65 100644
--- a/nexus/main.cpp
+++ b/nexus/main.cpp
@@ -40,10 +40,10 @@ int main() {
40 40
41 nm->setBroadcaster((SocketListener *) cl); 41 nm->setBroadcaster((SocketListener *) cl);
42 42
43 nm->attachController(new LoopController(nm->getPropMngr())); 43 nm->attachController(new LoopController(nm->getPropMngr(), nm));
44 nm->attachController(new TiwlanWifiController(nm->getPropMngr(), "/system/lib/modules/wlan.ko", "wlan", "")); 44 nm->attachController(new TiwlanWifiController(nm->getPropMngr(), nm, "/system/lib/modules/wlan.ko", "wlan", ""));
45// nm->attachController(new AndroidL2TPVpnController()); 45// nm->attachController(new AndroidL2TPVpnController(nm->getPropMngr(), nm));
46 nm->attachController(new OpenVpnController(nm->getPropMngr())); 46 nm->attachController(new OpenVpnController(nm->getPropMngr(), nm));
47 47
48 48
49 if (NetworkManager::Instance()->run()) { 49 if (NetworkManager::Instance()->run()) {