• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/corennnnn


コミットメタ情報

リビジョン7c5e61394069f6351cb518fbfd6a89e8c2eda1bf (tree)
日時2009-06-02 06:30:09
作者Mathias Agopian <mathias@goog...>
コミッターMathias Agopian

ログメッセージ

Merge commit 'goog/master' into merge_master

変更サマリ

差分

--- a/adb/get_my_path_darwin.c
+++ b/adb/get_my_path_darwin.c
@@ -14,7 +14,6 @@
1414 * limitations under the License.
1515 */
1616
17-#include <utils/executablepath.h>
1817 #import <Carbon/Carbon.h>
1918 #include <unistd.h>
2019
--- a/fastboot/util_osx.c
+++ b/fastboot/util_osx.c
@@ -26,7 +26,6 @@
2626 * SUCH DAMAGE.
2727 */
2828
29-#include <utils/executablepath.h>
3029 #import <Carbon/Carbon.h>
3130 #include <unistd.h>
3231
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -59,9 +59,11 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
5959 }
6060
6161 void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
62- char *cm, *last;
62+ char *next = cmd;
63+ char *cm;
64+ char *arg;
6365
64- if (!(cm = strtok_r(cmd, ":", &last))) {
66+ if (!(cm = strsep(&next, ":"))) {
6567 cli->sendMsg(500, "Malformatted message", false);
6668 return;
6769 }
@@ -72,8 +74,7 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
7274 FrameworkCommand *c = *i;
7375
7476 if (!strcmp(cm, c->getCommand())) {
75- cm += strlen(cm) +1;
76- if (c->runCommand(cli, cm)) {
77+ if (c->runCommand(cli, next)) {
7778 LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
7879 }
7980 return;
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -67,9 +67,9 @@ bool NetlinkEvent::decode(char *buffer, int size) {
6767 mAction = NlActionRemove;
6868 else if (!strcmp(a, "change"))
6969 mAction = NlActionChange;
70- } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM=")))
70+ } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM=")))
7171 mSeq = atoi(s + strlen("SEQNUM="));
72- else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM=")))
72+ else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM=")))
7373 mSubsystem = strdup(s + strlen("SUBSYSTEM="));
7474 else
7575 mParams[param_idx++] = strdup(s);
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -16,7 +16,7 @@ SocketClient::SocketClient(int socket) {
1616
1717 int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
1818 char *buf;
19-
19+
2020 if (addErrno) {
2121 buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8);
2222 sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno));
@@ -42,7 +42,7 @@ int SocketClient::sendMsg(const char *msg) {
4242 strcat(tmp, "\n");
4343 bp = tmp;
4444 }
45-
45+
4646 int rc = 0;
4747 const char *p = bp;
4848 int brtw = strlen(bp);
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -125,7 +125,7 @@ void SocketListener::runListener() {
125125 max = (*it)->getSocket();
126126 }
127127 pthread_mutex_unlock(&mClientsLock);
128-
128+
129129 if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
130130 LOGE("select failed (%s)", strerror(errno));
131131 sleep(1);
--- a/nexus/Android.mk
+++ b/nexus/Android.mk
@@ -22,10 +22,13 @@ LOCAL_SRC_FILES:= \
2222 WifiScanner.cpp \
2323 WifiNetwork.cpp \
2424 OpenVpnController.cpp \
25+ InterfaceConfig.cpp \
26+ PropertyManager.cpp \
27+ SupplicantState.cpp
2528
2629 LOCAL_MODULE:= nexus
2730
28-LOCAL_C_INCLUDES := $(KERNEL_HEADERS)
31+LOCAL_C_INCLUDES := $(KERNEL_HEADERS) -I../../../frameworks/base/include/
2932
3033 LOCAL_CFLAGS :=
3134
--- a/nexus/CommandListener.cpp
+++ b/nexus/CommandListener.cpp
@@ -1,5 +1,5 @@
11 /*
2- * Copyright (C) ErrorCode::CommandOkay8 The Android Open Source Project
2+ * Copyright (C) 2008 The Android Open Source Project
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdlib.h>
1718 #include <sys/socket.h>
1819 #include <netinet/in.h>
@@ -26,6 +27,7 @@
2627
2728 #include "CommandListener.h"
2829 #include "Controller.h"
30+#include "Property.h"
2931 #include "NetworkManager.h"
3032 #include "WifiController.h"
3133 #include "VpnController.h"
@@ -35,31 +37,32 @@ CommandListener::CommandListener() :
3537 FrameworkListener("nexus") {
3638 registerCmd(new WifiScanResultsCmd());
3739 registerCmd(new WifiListNetworksCmd());
38- registerCmd(new WifiAddNetworkCmd());
40+ registerCmd(new WifiCreateNetworkCmd());
3941 registerCmd(new WifiRemoveNetworkCmd());
4042
4143 registerCmd(new GetCmd());
4244 registerCmd(new SetCmd());
45+ registerCmd(new ListCmd());
4346 }
44-
47+
4548 /* -------------
4649 * Wifi Commands
4750 * ------------ */
4851
49-CommandListener::WifiAddNetworkCmd::WifiAddNetworkCmd() :
50- NexusCommand("wifi_add_network") {
51-}
52-
53-int CommandListener::WifiAddNetworkCmd::runCommand(SocketClient *cli, char *data) {
52+CommandListener::WifiCreateNetworkCmd::WifiCreateNetworkCmd() :
53+ NexusCommand("wifi_create_network") {
54+}
55+
56+int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli, char *data) {
5457 NetworkManager *nm = NetworkManager::Instance();
5558 WifiController *wc = (WifiController *) nm->findController("WIFI");
56- int networkId;
59+ WifiNetwork *wn;
5760
58- if ((networkId = wc->addNetwork()) < 0)
59- cli->sendMsg(ErrorCode::OperationFailed, "Failed to add network", true);
61+ if (!(wn = wc->createNetwork()))
62+ cli->sendMsg(ErrorCode::OperationFailed, "Failed to create network", true);
6063 else {
6164 char tmp[128];
62- sprintf(tmp, "Added network id %d.", networkId);
65+ sprintf(tmp, "Created network id %d.", wn->getNetworkId());
6366 cli->sendMsg(ErrorCode::CommandOkay, tmp, false);
6467 }
6568 return 0;
@@ -67,8 +70,8 @@ int CommandListener::WifiAddNetworkCmd::runCommand(SocketClient *cli, char *data
6770
6871 CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() :
6972 NexusCommand("wifi_remove_network") {
70-}
71-
73+}
74+
7275 int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *data) {
7376 NetworkManager *nm = NetworkManager::Instance();
7477 WifiController *wc = (WifiController *) nm->findController("WIFI");
@@ -83,7 +86,7 @@ int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *d
8386
8487 CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
8588 NexusCommand("wifi_scan_results") {
86-}
89+}
8790
8891 int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) {
8992 NetworkManager *nm = NetworkManager::Instance();
@@ -92,7 +95,7 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
9295 ScanResultCollection *src = wc->createScanResults();
9396 ScanResultCollection::iterator it;
9497 char buffer[256];
95-
98+
9699 for(it = src->begin(); it != src->end(); ++it) {
97100 sprintf(buffer, "%s:%u:%d:%s:%s",
98101 (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(),
@@ -103,13 +106,13 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
103106 }
104107
105108 delete src;
106- cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete", false);
109+ cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete.", false);
107110 return 0;
108111 }
109112
110113 CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() :
111114 NexusCommand("wifi_list_networks") {
112-}
115+}
113116
114117 int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *data) {
115118 NetworkManager *nm = NetworkManager::Instance();
@@ -118,12 +121,11 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da
118121 WifiNetworkCollection *src = wc->createNetworkList();
119122 WifiNetworkCollection::iterator it;
120123 char buffer[256];
121-
124+
122125 for(it = src->begin(); it != src->end(); ++it) {
123126 sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid());
124127 cli->sendMsg(ErrorCode::WifiNetworkList, buffer, false);
125128 delete (*it);
126- it = src->erase(it);
127129 }
128130
129131 delete src;
@@ -140,34 +142,31 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da
140142 * ---------------- */
141143 CommandListener::GetCmd::GetCmd() :
142144 NexusCommand("get") {
143-}
145+}
144146
145147 int CommandListener::GetCmd::runCommand(SocketClient *cli, char *data) {
146- char *bword;
147- char *last;
148- char propname[32];
148+ char *next = data;
149+ char *propname;
149150
150- if (!(bword = strtok_r(data, ":", &last)))
151+ if (!(propname = strsep(&next, ":")))
151152 goto out_inval;
152-
153- strncpy(propname, bword, sizeof(propname));
154153
155- char pb[255];
154+ char pb[Property::NameMaxSize + 6];
156155 snprintf(pb, sizeof(pb), "%s:", propname);
157156
158- if (!NetworkManager::Instance()->getProperty(propname,
159- &pb[strlen(pb)],
160- sizeof(pb) - strlen(pb))) {
157+ if (!NetworkManager::Instance()->getPropMngr()->get(propname,
158+ &pb[strlen(pb)],
159+ sizeof(pb) - strlen(pb))) {
161160 goto out_inval;
162161 }
163162
164- cli->sendMsg(ErrorCode::VariableRead, pb, false);
163+ cli->sendMsg(ErrorCode::PropertyRead, pb, false);
165164
166165 cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false);
167166 return 0;
168167 out_inval:
169168 errno = EINVAL;
170- cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true);
169+ cli->sendMsg(ErrorCode::CommandParameterError, "Failed to read property.", true);
171170 return 0;
172171 }
173172
@@ -178,8 +177,8 @@ CommandListener::SetCmd::SetCmd() :
178177 int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {
179178 char *bword;
180179 char *last;
181- char propname[32];
182- char propval[250];
180+ char propname[Property::NameMaxSize];
181+ char propval[Property::ValueMaxSize];
183182
184183 if (!(bword = strtok_r(data, ":", &last)))
185184 goto out_inval;
@@ -191,7 +190,7 @@ int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {
191190
192191 strncpy(propval, bword, sizeof(propval));
193192
194- if (NetworkManager::Instance()->setProperty(propname, propval))
193+ if (NetworkManager::Instance()->getPropMngr()->set(propname, propval))
195194 goto out_inval;
196195
197196 cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false);
@@ -202,3 +201,45 @@ out_inval:
202201 cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set property.", true);
203202 return 0;
204203 }
204+
205+CommandListener::ListCmd::ListCmd() :
206+ NexusCommand("list") {
207+}
208+
209+int CommandListener::ListCmd::runCommand(SocketClient *cli, char *data) {
210+ android::List<char *> *pc;
211+
212+ if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList())) {
213+ errno = ENODATA;
214+ cli->sendMsg(ErrorCode::CommandParameterError, "Failed to list properties.", true);
215+ return 0;
216+ }
217+
218+ android::List<char *>::iterator it;
219+
220+ for (it = pc->begin(); it != pc->end(); ++it) {
221+ char p_v[Property::ValueMaxSize];
222+
223+ if (!NetworkManager::Instance()->getPropMngr()->get((*it),
224+ p_v,
225+ sizeof(p_v))) {
226+ LOGW("Failed to get %s (%s)", (*it), strerror(errno));
227+ }
228+
229+ char *buf;
230+ if (asprintf(&buf, "%s:%s", (*it), p_v) < 0) {
231+ LOGE("Failed to allocate memory");
232+ free((*it));
233+ continue;
234+ }
235+ cli->sendMsg(ErrorCode::PropertyList, buf, false);
236+ free(buf);
237+
238+ free((*it));
239+ }
240+
241+ delete pc;
242+
243+ cli->sendMsg(ErrorCode::CommandOkay, "Properties list complete.", false);
244+ return 0;
245+}
--- a/nexus/CommandListener.h
+++ b/nexus/CommandListener.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _COMMANDLISTENER_H__
1718 #define _COMMANDLISTENER_H__
1819
@@ -40,10 +41,10 @@ private:
4041 int runCommand(SocketClient *c, char *data);
4142 };
4243
43- class WifiAddNetworkCmd : public NexusCommand {
44+ class WifiCreateNetworkCmd : public NexusCommand {
4445 public:
45- WifiAddNetworkCmd();
46- virtual ~WifiAddNetworkCmd() {}
46+ WifiCreateNetworkCmd();
47+ virtual ~WifiCreateNetworkCmd() {}
4748 int runCommand(SocketClient *c, char *data);
4849 };
4950
@@ -74,6 +75,13 @@ private:
7475 virtual ~GetCmd() {}
7576 int runCommand(SocketClient *c, char *data);
7677 };
78+
79+ class ListCmd : public NexusCommand {
80+ public:
81+ ListCmd();
82+ virtual ~ListCmd() {}
83+ int runCommand(SocketClient *c, char *data);
84+ };
7785 };
7886
7987 #endif
--- a/nexus/Controller.cpp
+++ b/nexus/Controller.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdio.h>
1718 #include <stdlib.h>
1819 #include <string.h>
@@ -33,13 +34,17 @@
3334 extern "C" int init_module(void *, unsigned int, const char *);
3435 extern "C" int delete_module(const char *, unsigned int);
3536
36-Controller::Controller(const char *name, const char *prefix) {
37- mName = name;
38- mPropertyPrefix = prefix;
39- mProperties = new PropertyCollection();
37+Controller::Controller(const char *name, PropertyManager *propMngr) {
38+ mPropMngr = propMngr;
39+ mName = strdup(name);
40+ mBoundInterface = NULL;
41+}
4042
41- mEnabled = false;
42- registerProperty("enable");
43+Controller::~Controller() {
44+ if (mBoundInterface)
45+ free(mBoundInterface);
46+ if (mName)
47+ free(mName);
4348 }
4449
4550 int Controller::start() {
@@ -50,65 +55,16 @@ int Controller::stop() {
5055 return 0;
5156 }
5257
53-const PropertyCollection & Controller::getProperties() {
54- return *mProperties;
55-}
56-
57-int Controller::setProperty(const char *name, char *value) {
58- if (!strcmp(name, "enable")) {
59- int en = atoi(value);
60- int rc;
61-
62- rc = (en ? enable() : disable());
63-
64- if (!rc)
65- mEnabled = en;
66-
67- return rc;
68- }
69-
58+int Controller::set(const char *name, const char *value) {
7059 errno = ENOENT;
7160 return -1;
7261 }
7362
74-const char *Controller::getProperty(const char *name, char *buffer, size_t maxsize) {
75- if (!strcmp(name, "enable")) {
76- snprintf(buffer, maxsize, "%d", mEnabled);
77- return buffer;
78- }
79-
63+const char *Controller::get(const char *name, char *buffer, size_t maxsize) {
8064 errno = ENOENT;
8165 return NULL;
8266 }
8367
84-int Controller::registerProperty(const char *name) {
85- PropertyCollection::iterator it;
86-
87- for (it = mProperties->begin(); it != mProperties->end(); ++it) {
88- if (!strcmp(name, (*it))) {
89- errno = EADDRINUSE;
90- LOGE("Failed to register property (%s)", strerror(errno));
91- return -1;
92- }
93- }
94-
95- mProperties->push_back(name);
96- return 0;
97-}
98-
99-int Controller::unregisterProperty(const char *name) {
100- PropertyCollection::iterator it;
101-
102- for (it = mProperties->begin(); it != mProperties->end(); ++it) {
103- if (!strcmp(name, (*it))) {
104- mProperties->erase(it);
105- return 0;
106- }
107- }
108- errno = ENOENT;
109- return -1;
110-}
111-
11268 int Controller::loadKernelModule(char *modpath, const char *args) {
11369 void *module;
11470 unsigned int size;
@@ -137,7 +93,7 @@ int Controller::unloadKernelModule(const char *modtag) {
13793 }
13894
13995 if (rc != 0) {
140- LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
96+ LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
14197 strerror(errno));
14298 }
14399 return rc;
@@ -203,3 +159,16 @@ bail:
203159 close(fd);
204160 return buffer;
205161 }
162+
163+int Controller::bindInterface(const char *ifname) {
164+ mBoundInterface = strdup(ifname);
165+ LOGD("Controller %s bound to %s", mName, ifname);
166+ return 0;
167+}
168+
169+int Controller::unbindInterface(const char *ifname) {
170+ free(mBoundInterface);
171+ mBoundInterface = NULL;
172+ LOGD("Controller %s unbound from %s", mName, ifname);
173+ return 0;
174+}
--- a/nexus/Controller.h
+++ b/nexus/Controller.h
@@ -13,51 +13,59 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _CONTROLLER_H
1718 #define _CONTROLLER_H
1819
1920 #include <unistd.h>
2021 #include <sys/types.h>
2122
22-#include "../../../frameworks/base/include/utils/List.h"
23+#include <utils/List.h>
24+
25+class PropertyManager;
2326
24-#include "PropertyCollection.h"
27+#include "PropertyManager.h"
28+#include "IPropertyProvider.h"
2529
26-class Controller {
30+class Controller : public IPropertyProvider {
2731 private:
28- const char *mName;
29- const char *mPropertyPrefix;
30- PropertyCollection *mProperties;
31- bool mEnabled;
32+ /*
33+ * Name of this controller - WIFI/VPN/USBNET/BTNET/BTDUN/LOOP/etc
34+ */
35+ char *mName;
36+
37+ /*
38+ * Name of the system ethernet interface which this controller is
39+ * bound to.
40+ */
41+ char *mBoundInterface;
42+
43+protected:
44+ PropertyManager *mPropMngr;
3245
3346 public:
34- Controller(const char *name, const char *prefix);
35- virtual ~Controller() {}
47+ Controller(const char *name, PropertyManager *propMngr);
48+ virtual ~Controller();
3649
3750 virtual int start();
3851 virtual int stop();
3952
40- virtual const PropertyCollection &getProperties();
41- virtual int setProperty(const char *name, char *value);
42- virtual const char *getProperty(const char *name, char *buffer, size_t maxsize);
43-
4453 const char *getName() { return mName; }
45- const char *getPropertyPrefix() { return mPropertyPrefix; }
54+ const char *getBoundInterface() { return mBoundInterface; }
55+
56+ /* IPropertyProvider methods */
57+ virtual int set(const char *name, const char *value);
58+ virtual const char *get(const char *name, char *buffer, size_t maxsize);
4659
4760 protected:
4861 int loadKernelModule(char *modpath, const char *args);
4962 bool isKernelModuleLoaded(const char *modtag);
5063 int unloadKernelModule(const char *modtag);
51-
52- int registerProperty(const char *name);
53- int unregisterProperty(const char *name);
64+ int bindInterface(const char *ifname);
65+ int unbindInterface(const char *ifname);
5466
5567 private:
5668 void *loadFile(char *filename, unsigned int *_size);
57-
58- virtual int enable() = 0;
59- virtual int disable() = 0;
60-
6169 };
6270
6371 typedef android::List<Controller *> ControllerCollection;
--- a/nexus/ErrorCode.h
+++ b/nexus/ErrorCode.h
@@ -26,8 +26,9 @@ public:
2626 static const int WifiScanResult = 125;
2727 static const int WifiNetworkList = 126;
2828
29- static const int VariableRead = 127;
30- static const int VariableWrite = 128;
29+ static const int PropertyRead = 127;
30+ static const int PropertySet = 128;
31+ static const int PropertyList = 129;
3132
3233 // 200 series - Requested action has been successfully completed
3334 static const int CommandOkay = 200;
--- /dev/null
+++ b/nexus/IPropertyProvider.h
@@ -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+#ifndef _IPROPERTY_PROVIDER_H
18+#define _IPROPERTY_PROVIDER_H
19+
20+#include <unistd.h>
21+#include <sys/types.h>
22+
23+#include <utils/List.h>
24+
25+class IPropertyProvider {
26+public:
27+ virtual int set(const char *name, const char *value) = 0;
28+ virtual const char *get(const char *name, char *buffer, size_t max) = 0;
29+};
30+
31+typedef android::List<IPropertyProvider *> IPropertyProviderCollection;
32+
33+#endif
--- /dev/null
+++ b/nexus/InterfaceConfig.cpp
@@ -0,0 +1,169 @@
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 "InterfaceConfig"
21+#include <cutils/log.h>
22+
23+#include "InterfaceConfig.h"
24+#include "NetworkManager.h"
25+
26+const char *InterfaceConfig::PropertyNames[] = { "dhcp", "ip",
27+ "netmask",
28+ "gateway", "dns1", "dns2",
29+ "dns3", '\0' };
30+
31+InterfaceConfig::InterfaceConfig(const char *prop_prefix) {
32+ mPropPrefix = strdup(prop_prefix);
33+ mUseDhcp = true;
34+ registerProperties();
35+}
36+
37+InterfaceConfig::~InterfaceConfig() {
38+ unregisterProperties();
39+ free(mPropPrefix);
40+}
41+
42+InterfaceConfig::InterfaceConfig(const char *prop_prefix,
43+ const char *ip, const char *nm,
44+ const char *gw, const char *dns1, const char *dns2,
45+ const char *dns3) {
46+ mPropPrefix = strdup(prop_prefix);
47+ mUseDhcp = false;
48+
49+ if (!inet_aton(ip, &mIp))
50+ LOGW("Unable to parse ip (%s)", ip);
51+ if (!inet_aton(nm, &mNetmask))
52+ LOGW("Unable to parse netmask (%s)", nm);
53+ if (!inet_aton(gw, &mGateway))
54+ LOGW("Unable to parse gateway (%s)", gw);
55+ if (!inet_aton(dns1, &mDns1))
56+ LOGW("Unable to parse dns1 (%s)", dns1);
57+ if (!inet_aton(dns2, &mDns2))
58+ LOGW("Unable to parse dns2 (%s)", dns2);
59+ if (!inet_aton(dns3, &mDns3))
60+ LOGW("Unable to parse dns3 (%s)", dns3);
61+ registerProperties();
62+}
63+
64+InterfaceConfig::InterfaceConfig(const char *prop_prefix,
65+ const struct in_addr *ip,
66+ const struct in_addr *nm, const struct in_addr *gw,
67+ const struct in_addr *dns1, const struct in_addr *dns2,
68+ const struct in_addr *dns3) {
69+ mPropPrefix = strdup(prop_prefix);
70+ mUseDhcp = false;
71+
72+ memcpy(&mIp, ip, sizeof(struct in_addr));
73+ memcpy(&mNetmask, nm, sizeof(struct in_addr));
74+ memcpy(&mGateway, gw, sizeof(struct in_addr));
75+ memcpy(&mDns1, dns1, sizeof(struct in_addr));
76+ memcpy(&mDns2, dns2, sizeof(struct in_addr));
77+ memcpy(&mDns3, dns3, sizeof(struct in_addr));
78+ registerProperties();
79+}
80+
81+int InterfaceConfig::registerProperties() {
82+ for (const char **p = InterfaceConfig::PropertyNames; *p != '\0'; p++) {
83+ char *tmp;
84+ asprintf(&tmp, "%s.if.%s", mPropPrefix, *p);
85+
86+ if (NetworkManager::Instance()->getPropMngr()->registerProperty(tmp,
87+ this)) {
88+ free(tmp);
89+ return -1;
90+ }
91+ free(tmp);
92+ }
93+ return 0;
94+}
95+
96+int InterfaceConfig::unregisterProperties() {
97+ for (const char **p = InterfaceConfig::PropertyNames; *p != '\0'; p++) {
98+ char *tmp;
99+ asprintf(&tmp, "%s.if.%s", mPropPrefix, *p);
100+
101+ if (NetworkManager::Instance()->getPropMngr()->unregisterProperty(tmp))
102+ LOGW("Unable to remove property '%s' (%s)", tmp, strerror(errno));
103+ free(tmp);
104+ }
105+ return 0;
106+}
107+
108+int InterfaceConfig::set(const char *name, const char *value) {
109+ const char *n;
110+
111+ for (n = &name[strlen(name)]; *n != '.'; n--);
112+ n++;
113+
114+ if (!strcasecmp(n, "name")) {
115+ errno = EROFS;
116+ return -1;
117+ } else if (!strcasecmp(n, "ip") && !inet_aton(value, &mIp))
118+ goto out_inval;
119+ else if (!strcasecmp(n, "dhcp"))
120+ mUseDhcp = (atoi(value) == 0 ? false : true);
121+ else if (!strcasecmp(n, "netmask") && !inet_aton(value, &mNetmask))
122+ goto out_inval;
123+ else if (!strcasecmp(n, "gateway") && !inet_aton(value, &mGateway))
124+ goto out_inval;
125+ else if (!strcasecmp(n, "dns1") && !inet_aton(value, &mDns1))
126+ goto out_inval;
127+ else if (!strcasecmp(n, "dns2") && !inet_aton(value, &mDns2))
128+ goto out_inval;
129+ else if (!strcasecmp(n, "dns3") && !inet_aton(value, &mDns3))
130+ goto out_inval;
131+ else {
132+ errno = ENOENT;
133+ return -1;
134+ }
135+
136+ return 0;
137+
138+out_inval:
139+ errno = EINVAL;
140+ return -1;
141+}
142+
143+const char *InterfaceConfig::get(const char *name, char *buffer, size_t max) {
144+ const char *n;
145+
146+ for (n = &name[strlen(name)]; *n != '.'; n--);
147+ n++;
148+
149+ if (!strcasecmp(n, "ip"))
150+ strncpy(buffer, inet_ntoa(mIp), max);
151+ else if (!strcasecmp(n, "dhcp"))
152+ snprintf(buffer, max, "%d", mUseDhcp);
153+ else if (!strcasecmp(n, "netmask"))
154+ strncpy(buffer, inet_ntoa(mNetmask), max);
155+ else if (!strcasecmp(n, "gateway"))
156+ strncpy(buffer, inet_ntoa(mGateway), max);
157+ else if (!strcasecmp(n, "dns1"))
158+ strncpy(buffer, inet_ntoa(mDns1), max);
159+ else if (!strcasecmp(n, "dns2"))
160+ strncpy(buffer, inet_ntoa(mDns2), max);
161+ else if (!strcasecmp(n, "dns3"))
162+ strncpy(buffer, inet_ntoa(mDns3), max);
163+ else {
164+ strncpy(buffer, "(internal error)", max);
165+ errno = ENOENT;
166+ return NULL;
167+ }
168+ return buffer;
169+}
--- /dev/null
+++ b/nexus/InterfaceConfig.h
@@ -0,0 +1,75 @@
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 _INTERFACE_CONFIG_H
18+#define _INTERFACE_CONFIG_H
19+
20+#include <unistd.h>
21+#include <sys/types.h>
22+#include <netinet/in.h>
23+#include <arpa/inet.h>
24+
25+#include "IPropertyProvider.h"
26+
27+class PropertyManager;
28+
29+class InterfaceConfig : public IPropertyProvider {
30+public:
31+ static const char *PropertyNames[];
32+
33+private:
34+ char *mPropPrefix;
35+ bool mUseDhcp;
36+ struct in_addr mIp;
37+ struct in_addr mNetmask;
38+ struct in_addr mGateway;
39+ struct in_addr mDns1;
40+ struct in_addr mDns2;
41+ struct in_addr mDns3;
42+
43+public:
44+ InterfaceConfig(const char *prop_prefix);
45+ InterfaceConfig(const char *prop_prefix,
46+ const char *ip, const char *nm,
47+ const char *gw, const char *dns1, const char *dns2,
48+ const char *dns3);
49+
50+ InterfaceConfig(const char *prop_prefix,
51+ const struct in_addr *ip,
52+ const struct in_addr *nm, const struct in_addr *gw,
53+ const struct in_addr *dns1, const struct in_addr *dns2,
54+ const struct in_addr *dns3);
55+
56+ virtual ~InterfaceConfig();
57+
58+ int set(const char *name, const char *value);
59+ const char *get(const char *name, char *buffer, size_t maxsize);
60+
61+ bool getUseDhcp() const { return mUseDhcp; }
62+ const struct in_addr &getIp() const { return mIp; }
63+ const struct in_addr &getNetmask() const { return mNetmask; }
64+ const struct in_addr &getGateway() const { return mGateway; }
65+ const struct in_addr &getDns1() const { return mDns1; }
66+ const struct in_addr &getDns2() const { return mDns2; }
67+ const struct in_addr &getDns3() const { return mDns3; }
68+
69+private:
70+ int registerProperties();
71+ int unregisterProperties();
72+};
73+
74+
75+#endif
--- a/nexus/LoopController.cpp
+++ b/nexus/LoopController.cpp
@@ -13,20 +13,21 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <errno.h>
1718
1819 #include "LoopController.h"
20+#include "PropertyManager.h"
1921
20-LoopController::LoopController() :
21- Controller("LOOP", "loop") {
22+LoopController::LoopController(PropertyManager *propmngr) :
23+ Controller("LOOP", propmngr) {
2224 }
2325
24-int LoopController::enable() {
25- errno = ENOSYS;
26- return -1;
26+int LoopController::set(const char *name, const char *value) {
27+ return Controller::set(name, value);
2728 }
2829
29-int LoopController::disable() {
30- errno = ENOSYS;
31- return -1;
30+const char *LoopController::get(const char *name, char *buffer, size_t maxsize) {
31+ return Controller::get(name, buffer, maxsize);
3232 }
33+
--- a/nexus/LoopController.h
+++ b/nexus/LoopController.h
@@ -13,19 +13,20 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _LOOP_CONTROLLER_H
1718 #define _LOOP_CONTROLLER_H
1819
1920 #include "Controller.h"
2021
22+
2123 class LoopController : public Controller {
2224 public:
23- LoopController();
25+ LoopController(PropertyManager *propmngr);
2426 virtual ~LoopController() {}
2527
26-private:
27- int enable();
28- int disable();
28+ int set(const char *name, const char *value);
29+ const char *get(const char *name, char *buffer, size_t maxsize);
2930 };
3031
3132 #endif
--- a/nexus/NetworkManager.cpp
+++ b/nexus/NetworkManager.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdio.h>
1718 #include <errno.h>
1819
@@ -21,18 +22,23 @@
2122 #include <cutils/log.h>
2223
2324 #include "NetworkManager.h"
25+#include "InterfaceConfig.h"
2426
2527 NetworkManager *NetworkManager::sInstance = NULL;
2628
2729 NetworkManager *NetworkManager::Instance() {
2830 if (!sInstance)
29- sInstance = new NetworkManager();
31+ sInstance = new NetworkManager(new PropertyManager());
3032 return sInstance;
3133 }
3234
33-NetworkManager::NetworkManager() {
35+NetworkManager::NetworkManager(PropertyManager *propMngr) {
3436 mBroadcaster = NULL;
3537 mControllers = new ControllerCollection();
38+ mPropMngr = propMngr;
39+}
40+
41+NetworkManager::~NetworkManager() {
3642 }
3743
3844 int NetworkManager::run() {
@@ -54,7 +60,7 @@ int NetworkManager::startControllers() {
5460 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
5561 int irc = (*i)->start();
5662 LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc);
57- if (irc && !rc)
63+ if (irc && !rc)
5864 rc = irc;
5965 }
6066 return rc;
@@ -67,7 +73,7 @@ int NetworkManager::stopControllers() {
6773 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
6874 int irc = (*i)->stop();
6975 LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc);
70- if (irc && !rc)
76+ if (irc && !rc)
7177 rc = irc;
7278 }
7379 return rc;
@@ -83,79 +89,23 @@ Controller *NetworkManager::findController(const char *name) {
8389 return NULL;
8490 }
8591
86-int NetworkManager::setProperty(const char *name, char *value) {
87- char *tmp = strdup(name);
88- char *next = tmp;
89- char *prefix;
90- char *rest;
91- ControllerCollection::iterator it;
92+int NetworkManager::onInterfaceStart(Controller *c, const InterfaceConfig *cfg) {
93+ LOGD("Interface %s started by controller %s", c->getBoundInterface(), c->getName());
9294
93- if (!(prefix = strsep(&next, ".")))
94- goto out_inval;
95+ // Look up the interface
9596
96- rest = next;
97-
98- if (!strncasecmp(prefix, "netman", 6)) {
99- errno = ENOSYS;
97+ if (0) { // already started?
98+ errno = EADDRINUSE;
10099 return -1;
101100 }
102101
103- for (it = mControllers->begin(); it != mControllers->end(); ++it) {
104- if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
105- return (*it)->setProperty(rest, value);
106- }
107- }
108-
109- errno = ENOENT;
110- return -1;
111-
112-out_inval:
113- errno = EINVAL;
114- return -1;
115-}
116-
117-const char *NetworkManager::getProperty(const char *name, char *buffer,
118- size_t maxsize) {
119- char *tmp = strdup(name);
120- char *next = tmp;
121- char *prefix;
122- char *rest;
123- ControllerCollection::iterator it;
124-
125- if (!(prefix = strsep(&next, ".")))
126- goto out_inval;
127-
128- rest = next;
129-
130- if (!strncasecmp(prefix, "netman", 6)) {
131- errno = ENOSYS;
132- return NULL;
133- }
134-
135- for (it = mControllers->begin(); it != mControllers->end(); ++it) {
136- if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
137- return (*it)->getProperty(rest, buffer, maxsize);
138- }
102+ if (cfg->getUseDhcp()) {
103+ } else {
139104 }
140-
141- errno = ENOENT;
142- return NULL;
143-
144-out_inval:
145- errno = EINVAL;
146- return NULL;
147-}
148-
149-const PropertyCollection &NetworkManager::getProperties() {
150- return *mProperties;
151-}
152-
153-int NetworkManager::onInterfaceCreated(Controller *c, char *name) {
154- LOGD("Interface %s created by controller %s", name, c->getName());
155105 return 0;
156106 }
157107
158-int NetworkManager::onInterfaceDestroyed(Controller *c, char *name) {
159- LOGD("Interface %s destroyed by controller %s", name, c->getName());
108+int NetworkManager::onInterfaceStop(Controller *c, const char *name) {
109+ LOGD("Interface %s stopped by controller %s", name, c->getName());
160110 return 0;
161111 }
--- a/nexus/NetworkManager.h
+++ b/nexus/NetworkManager.h
@@ -13,13 +13,17 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _NETWORKMANAGER_H
1718 #define _NETWORKMANAGER_H
1819
1920 #include <sysutils/SocketListener.h>
2021
2122 #include "Controller.h"
22-#include "PropertyCollection.h"
23+
24+#include "PropertyManager.h"
25+
26+class InterfaceConfig;
2327
2428 class NetworkManager {
2529 private:
@@ -28,10 +32,10 @@ private:
2832 private:
2933 ControllerCollection *mControllers;
3034 SocketListener *mBroadcaster;
31- PropertyCollection *mProperties;
35+ PropertyManager *mPropMngr;
3236
3337 public:
34- virtual ~NetworkManager() {}
38+ virtual ~NetworkManager();
3539
3640 int run();
3741
@@ -39,27 +43,28 @@ public:
3943
4044 Controller *findController(const char *name);
4145
42- const PropertyCollection &getProperties();
43- int setProperty(const char *name, char *value);
44- const char *getProperty(const char *name, char *buffer, size_t maxsize);
45-
4646 void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
4747 SocketListener *getBroadcaster() { return mBroadcaster; }
48+ PropertyManager *getPropMngr() { return mPropMngr; }
4849
4950 static NetworkManager *Instance();
5051
5152 private:
5253 int startControllers();
5354 int stopControllers();
54- int registerProperty(const char *name);
55- int unregisterProperty(const char *name);
5655
57- NetworkManager();
56+ NetworkManager(PropertyManager *propMngr);
5857
5958 public:
60-// XXX: Extract these into an interface
61- int onInterfaceCreated(Controller *c, char *name);
62- int onInterfaceDestroyed(Controller *c, char *name);
59+ /*
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);
6364
65+ /*
66+ * Called from a controller when an interface should be shut down
67+ */
68+ int onInterfaceStop(Controller *c, const char *name);
6469 };
6570 #endif
--- a/nexus/NexusCommand.cpp
+++ b/nexus/NexusCommand.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include "NexusCommand.h"
1718
1819 NexusCommand::NexusCommand(const char *cmd) :
--- a/nexus/NexusCommand.h
+++ b/nexus/NexusCommand.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _NEXUS_COMMAND_H
1718 #define _NEXUS_COMMAND_H
1819
--- a/nexus/OpenVpnController.cpp
+++ b/nexus/OpenVpnController.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <errno.h>
1718 #include <netinet/in.h>
1819 #include <arpa/inet.h>
@@ -24,13 +25,13 @@
2425 #include <sysutils/ServiceManager.h>
2526
2627 #include "OpenVpnController.h"
28+#include "PropertyManager.h"
2729
2830 #define DAEMON_PROP_NAME "vpn.openvpn.status"
29-
3031 #define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf"
3132
32-OpenVpnController::OpenVpnController() :
33- VpnController() {
33+OpenVpnController::OpenVpnController(PropertyManager *propmngr) :
34+ VpnController(propmngr) {
3435 mServiceManager = new ServiceManager();
3536 }
3637
@@ -49,8 +50,8 @@ int OpenVpnController::stop() {
4950 int OpenVpnController::enable() {
5051 char svc[PROPERTY_VALUE_MAX];
5152 char tmp[64];
52-
53- if (!getProperty("vpn.gateway", tmp, sizeof(tmp))) {
53+
54+ if (!mPropMngr->get("vpn.gateway", tmp, sizeof(tmp))) {
5455 LOGE("Error reading property 'vpn.gateway' (%s)", strerror(errno));
5556 return -1;
5657 }
--- a/nexus/OpenVpnController.h
+++ b/nexus/OpenVpnController.h
@@ -17,6 +17,7 @@
1717 #ifndef _OPEN_VPN_CONTROLLER_H
1818 #define _OPEN_VPN_CONTROLLER_H
1919
20+#include "PropertyManager.h"
2021 #include "VpnController.h"
2122
2223 class ServiceManager;
@@ -26,7 +27,7 @@ private:
2627 ServiceManager *mServiceManager;
2728
2829 public:
29- OpenVpnController();
30+ OpenVpnController(PropertyManager *propmngr);
3031 virtual ~OpenVpnController();
3132
3233 int start();
--- a/nexus/PropertyCollection.h
+++ b/nexus/Property.h
@@ -14,12 +14,8 @@
1414 * limitations under the License.
1515 */
1616
17-#ifndef _PROPERTY_COLLECTION_H
18-#define _PROPERTY_COLLECTION_H
19-
20-#include "../../../frameworks/base/include/utils/List.h"
21-
22-typedef android::List<const char *> PropertyCollection;
23-
24-#endif
25-
17+class Property {
18+public:
19+ static const int NameMaxSize = 128;
20+ static const int ValueMaxSize = 255;
21+};
--- /dev/null
+++ b/nexus/PropertyManager.cpp
@@ -0,0 +1,122 @@
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 "PropertyManager"
18+
19+#include <cutils/log.h>
20+
21+#include "PropertyManager.h"
22+
23+PropertyManager::PropertyManager() {
24+ mPropertyPairs = new PropertyPairCollection();
25+ pthread_mutex_init(&mLock, NULL);
26+}
27+
28+PropertyManager::~PropertyManager() {
29+ delete mPropertyPairs;
30+}
31+
32+int PropertyManager::registerProperty(const char *name, IPropertyProvider *pp) {
33+ PropertyPairCollection::iterator it;
34+
35+// LOGD("registerProperty(%s)", name);
36+ pthread_mutex_lock(&mLock);
37+ for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
38+ if (!strcmp(name, (*it)->getName())) {
39+ errno = EADDRINUSE;
40+ LOGE("Failed to register property (%s)", strerror(errno));
41+ pthread_mutex_unlock(&mLock);
42+ return -1;
43+ }
44+ }
45+ mPropertyPairs->push_back(new PropertyPair(name, pp));
46+ pthread_mutex_unlock(&mLock);
47+ return 0;
48+}
49+
50+int PropertyManager::unregisterProperty(const char *name) {
51+ PropertyPairCollection::iterator it;
52+
53+// LOGD("unregisterProperty(%s)", name);
54+ pthread_mutex_lock(&mLock);
55+ for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
56+ if (!strcmp(name, (*it)->getName())) {
57+ delete ((*it));
58+ mPropertyPairs->erase(it);
59+ pthread_mutex_unlock(&mLock);
60+ return 0;
61+ }
62+ }
63+ pthread_mutex_unlock(&mLock);
64+ errno = ENOENT;
65+ return -1;
66+}
67+
68+/*
69+ * IPropertyManager methods
70+ */
71+
72+int PropertyManager::set(const char *name, const char *value) {
73+ PropertyPairCollection::iterator it;
74+
75+ pthread_mutex_lock(&mLock);
76+ for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
77+ if (!strcmp(name, (*it)->getName())) {
78+ pthread_mutex_unlock(&mLock);
79+ return (*it)->getProvider()->set(name, value);
80+ }
81+ }
82+ pthread_mutex_unlock(&mLock);
83+ errno = ENOENT;
84+ return -1;
85+}
86+
87+const char *PropertyManager::get(const char *name, char *buffer, size_t max) {
88+ PropertyPairCollection::iterator it;
89+
90+ memset(buffer, 0, max);
91+ pthread_mutex_lock(&mLock);
92+ for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
93+ if (!strcmp(name, (*it)->getName())) {
94+ pthread_mutex_unlock(&mLock);
95+ return (*it)->getProvider()->get(name, buffer, max);
96+ }
97+ }
98+ pthread_mutex_unlock(&mLock);
99+ errno = ENOENT;
100+ return NULL;
101+}
102+
103+android::List<char *> *PropertyManager::createPropertyList() {
104+ android::List<char *> *c = new android::List<char *>();
105+
106+ PropertyPairCollection::iterator it;
107+
108+ pthread_mutex_lock(&mLock);
109+ for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it)
110+ c->push_back(strdup((*it)->getName()));
111+ pthread_mutex_unlock(&mLock);
112+ return c;
113+}
114+
115+PropertyPair::PropertyPair(const char *name, IPropertyProvider *pp) {
116+ mName = strdup(name);
117+ mPp = pp;
118+}
119+
120+PropertyPair::~PropertyPair() {
121+ free(mName);
122+}
--- /dev/null
+++ b/nexus/PropertyManager.h
@@ -0,0 +1,57 @@
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 _PROPERTY_MANAGER_H
18+#define _PROPERTY_MANAGER_H
19+
20+#include <errno.h>
21+#include <pthread.h>
22+
23+#include <utils/List.h>
24+
25+#include "IPropertyProvider.h"
26+
27+class PropertyPair {
28+private:
29+ char *mName;
30+ IPropertyProvider *mPp;
31+
32+public:
33+ PropertyPair(const char *name, IPropertyProvider *pp);
34+ virtual ~PropertyPair();
35+
36+ const char *getName() { return mName; }
37+ IPropertyProvider *getProvider() { return mPp; }
38+};
39+
40+typedef android::List<PropertyPair *> PropertyPairCollection;
41+
42+class PropertyManager {
43+ PropertyPairCollection *mPropertyPairs;
44+ pthread_mutex_t mLock;
45+
46+public:
47+ PropertyManager();
48+ virtual ~PropertyManager();
49+ int registerProperty(const char *name, IPropertyProvider *pp);
50+ int unregisterProperty(const char *name);
51+ android::List<char *> *createPropertyList();
52+
53+ int set(const char *name, const char *value);
54+ const char *get(const char *name, char *buffer, size_t max);
55+};
56+
57+#endif
--- a/nexus/ScanResult.cpp
+++ b/nexus/ScanResult.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdlib.h>
1718 #include <ctype.h>
1819
@@ -42,7 +43,7 @@ ScanResult::ScanResult(char *rawResult) {
4243 mFreq = atoi(tmp);
4344 ++q;
4445
45- // LEVEL
46+ // LEVEL
4647 for (p = q; *q != '\t'; ++q);
4748 strncpy(tmp, p, (q - p));
4849 tmp[q-p] = '\0';
@@ -60,7 +61,7 @@ ScanResult::ScanResult(char *rawResult) {
6061 // haven't had time to dig into it ...
6162 if (*q == '\t')
6263 q++;
63-
64+
6465 for (p = q; *q != '\t'; ++q) {
6566 if (*q == '\0')
6667 break;
--- a/nexus/ScanResult.h
+++ b/nexus/ScanResult.h
@@ -13,12 +13,13 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _SCAN_RESULT_H
1718 #define _SCAN_RESULT_H
1819
1920 #include <sys/types.h>
2021
21-#include "../../../frameworks/base/include/utils/List.h"
22+#include <utils/List.h>
2223
2324 class ScanResult {
2425 char *mBssid;
--- a/nexus/Supplicant.cpp
+++ b/nexus/Supplicant.cpp
@@ -32,8 +32,10 @@
3232 #include "SupplicantState.h"
3333 #include "SupplicantEvent.h"
3434 #include "ScanResult.h"
35+#include "PropertyManager.h"
3536 #include "NetworkManager.h"
3637 #include "ErrorCode.h"
38+#include "WifiController.h"
3739
3840 #include "libwpa_client/wpa_ctrl.h"
3941
@@ -43,21 +45,29 @@
4345 #define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf"
4446 #define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf"
4547
46-Supplicant::Supplicant() {
48+Supplicant::Supplicant(WifiController *wc, PropertyManager *propmngr) {
49+ mController = wc;
50+ mPropMngr = propmngr;
51+ mInterfaceName = NULL;
4752 mCtrl = NULL;
4853 mMonitor = NULL;
4954 mListener = NULL;
50-
55+
5156 mState = SupplicantState::UNKNOWN;
5257
5358 mServiceManager = new ServiceManager();
5459
5560 mLatestScanResults = new ScanResultCollection();
5661 pthread_mutex_init(&mLatestScanResultsLock, NULL);
62+
63+ mNetworks = new WifiNetworkCollection();
64+ pthread_mutex_init(&mNetworksLock, NULL);
5765 }
5866
5967 Supplicant::~Supplicant() {
6068 delete mServiceManager;
69+ if (mInterfaceName)
70+ free(mInterfaceName);
6171 }
6272
6373 int Supplicant::start() {
@@ -65,7 +75,7 @@ int Supplicant::start() {
6575 if (setupConfig()) {
6676 LOGW("Unable to setup supplicant.conf");
6777 }
68-
78+
6979 if (mServiceManager->start(SUPPLICANT_SERVICE_NAME)) {
7080 LOGE("Error starting supplicant (%s)", strerror(errno));
7181 return -1;
@@ -76,11 +86,20 @@ int Supplicant::start() {
7686 LOGE("Error connecting to supplicant (%s)\n", strerror(errno));
7787 return -1;
7888 }
89+
90+ if (retrieveInterfaceName()) {
91+ LOGE("Error retrieving interface name (%s)\n", strerror(errno));
92+ return -1;
93+ }
94+
95+ mPropMngr->registerProperty("wifi.supplicant.state", this);
7996 return 0;
8097 }
8198
8299 int Supplicant::stop() {
83100
101+ mPropMngr->unregisterProperty("wifi.supplicant.state");
102+
84103 if (mListener->stopListener()) {
85104 LOGW("Unable to stop supplicant listener (%s)", strerror(errno));
86105 return -1;
@@ -106,13 +125,55 @@ bool Supplicant::isStarted() {
106125 return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME);
107126 }
108127
109-int Supplicant::connectToSupplicant() {
110- if (!isStarted()) {
111- LOGE("Supplicant not running, cannot connect");
128+int Supplicant::refreshNetworkList() {
129+ char *reply;
130+ size_t len = 4096;
131+
132+ if (!(reply = (char *) malloc(len))) {
133+ errno = ENOMEM;
134+ return -1;
135+ }
136+
137+ if (sendCommand("LIST_NETWORKS", reply, &len)) {
138+ free(reply);
112139 return -1;
113140 }
114141
115- mCtrl = wpa_ctrl_open("tiwlan0");
142+ char *linep;
143+ char *linep_next = NULL;
144+
145+ if (!strtok_r(reply, "\n", &linep_next)) {
146+ LOGW("Malformatted network list\n");
147+ } else {
148+ pthread_mutex_lock(&mNetworksLock);
149+ if (!mNetworks->empty()) {
150+ WifiNetworkCollection::iterator i;
151+
152+ for (i = mNetworks->begin(); i !=mNetworks->end(); ++i)
153+ delete *i;
154+ mNetworks->clear();
155+ }
156+
157+ while((linep = strtok_r(NULL, "\n", &linep_next))) {
158+ WifiNetwork *wn = new WifiNetwork(mController, this, linep);
159+ mNetworks->push_back(wn);
160+ if (wn->refresh())
161+ LOGW("Unable to refresh network id %d", wn->getNetworkId());
162+ }
163+
164+ LOGD("Loaded %d networks\n", mNetworks->size());
165+ pthread_mutex_unlock(&mNetworksLock);
166+ }
167+
168+ free(reply);
169+ return 0;
170+}
171+
172+int Supplicant::connectToSupplicant() {
173+ if (!isStarted())
174+ LOGW("Supplicant service not running");
175+
176+ mCtrl = wpa_ctrl_open("tiwlan0"); // XXX:
116177 if (mCtrl == NULL) {
117178 LOGE("Unable to open connection to supplicant on \"%s\": %s",
118179 "tiwlan0", strerror(errno));
@@ -132,7 +193,7 @@ int Supplicant::connectToSupplicant() {
132193 }
133194
134195 mListener = new SupplicantListener(this, mMonitor);
135-
196+
136197 if (mListener->startListener()) {
137198 LOGE("Error - unable to start supplicant listener");
138199 stop();
@@ -151,20 +212,17 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len)
151212 // LOGD("sendCommand(): -> '%s'", cmd);
152213
153214 int rc;
215+ memset(reply, 0, *reply_len);
154216 if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) {
155217 errno = ETIMEDOUT;
156218 return -1;
157219 } else if (rc < 0 || !strncmp(reply, "FAIL", 4)) {
158- LOGW("sendCommand(): <- '%s'", reply);
220+ strcpy(reply, "FAIL");
159221 errno = EIO;
160222 return -1;
161223 }
162224
163- if (!strncmp(cmd, "PING", 4) ||
164- !strncmp(cmd, "SCAN_RESULTS", 12))
165- reply[*reply_len] = '\0';
166-
167-// LOGD("sendCommand(): <- '%s'", reply);
225+// LOGD("sendCommand(): <- '%s'", reply);
168226 return 0;
169227 }
170228
@@ -187,6 +245,22 @@ int Supplicant::triggerScan(bool active) {
187245 return 0;
188246 }
189247
248+int 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+
255+const 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+
190264 int Supplicant::onConnectedEvent(SupplicantEvent *evt) {
191265 LOGD("onConnectedEvent(%s)", evt->getEvent());
192266 return 0;
@@ -237,12 +311,12 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
237311 char *reply;
238312
239313 if (!(reply = (char *) malloc(4096))) {
240- errno = -ENOMEM;
314+ errno = ENOMEM;
241315 return -1;
242316 }
243317
244318 size_t len = 4096;
245-
319+
246320 if (sendCommand("SCAN_RESULTS", reply, &len)) {
247321 LOGW("onScanResultsEvent(%s): Error getting scan results (%s)",
248322 evt->getEvent(), strerror(errno));
@@ -272,7 +346,7 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
272346
273347 while((linep = strtok_r(NULL, "\n", &linep_next)))
274348 mLatestScanResults->push_back(new ScanResult(linep));
275-
349+
276350 char tmp[128];
277351 sprintf(tmp, "Scan results ready (%d)", mLatestScanResults->size());
278352 NetworkManager::Instance()->getBroadcaster()->
@@ -323,43 +397,78 @@ ScanResultCollection *Supplicant::createLatestScanResults() {
323397 ScanResultCollection::iterator i;
324398
325399 pthread_mutex_lock(&mLatestScanResultsLock);
326- for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i) {
400+ for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i)
327401 d->push_back((*i)->clone());
328- }
329402
330403 pthread_mutex_unlock(&mLatestScanResultsLock);
331404 return d;
332405 }
333406
334-WifiNetworkCollection *Supplicant::createNetworkList() {
335- WifiNetworkCollection *d = new WifiNetworkCollection();
336- return d;
337-}
338-
339-int Supplicant::addNetwork() {
340- char reply[32];
407+WifiNetwork *Supplicant::createNetwork() {
408+ char reply[255];
341409 size_t len = sizeof(reply) -1;
342410
343- memset(reply, 0, sizeof(reply));
344411 if (sendCommand("ADD_NETWORK", reply, &len))
345- return -1;
412+ return NULL;
413+
414+ if (reply[strlen(reply) -1] == '\n')
415+ reply[strlen(reply) -1] = '\0';
346416
347- return atoi(reply);
417+ WifiNetwork *wn = new WifiNetwork(mController, this, atoi(reply));
418+ pthread_mutex_lock(&mNetworksLock);
419+ mNetworks->push_back(wn);
420+ pthread_mutex_unlock(&mNetworksLock);
421+ return wn;
348422 }
349423
350-int Supplicant::removeNetwork(int networkId) {
424+int Supplicant::removeNetwork(WifiNetwork *wn) {
351425 char req[64];
352426
353- sprintf(req, "REMOVE_NETWORK %d", networkId);
427+ sprintf(req, "REMOVE_NETWORK %d", wn->getNetworkId());
354428 char reply[32];
355429 size_t len = sizeof(reply) -1;
356- memset(reply, 0, sizeof(reply));
357-
430+
358431 if (sendCommand(req, reply, &len))
359432 return -1;
433+
434+ pthread_mutex_lock(&mNetworksLock);
435+ WifiNetworkCollection::iterator it;
436+ for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
437+ if ((*it) == wn) {
438+ mNetworks->erase(it);
439+ break;
440+ }
441+ }
442+ pthread_mutex_unlock(&mNetworksLock);
360443 return 0;
361444 }
362445
446+WifiNetwork *Supplicant::lookupNetwork(int networkId) {
447+ pthread_mutex_lock(&mNetworksLock);
448+ WifiNetworkCollection::iterator it;
449+ for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
450+ if ((*it)->getNetworkId() == networkId) {
451+ pthread_mutex_unlock(&mNetworksLock);
452+ return *it;
453+ }
454+ }
455+ pthread_mutex_unlock(&mNetworksLock);
456+ errno = ENOENT;
457+ return NULL;
458+}
459+
460+WifiNetworkCollection *Supplicant::createNetworkList() {
461+ WifiNetworkCollection *d = new WifiNetworkCollection();
462+ WifiNetworkCollection::iterator i;
463+
464+ pthread_mutex_lock(&mNetworksLock);
465+ for (i = mNetworks->begin(); i != mNetworks->end(); ++i)
466+ d->push_back((*i)->clone());
467+
468+ pthread_mutex_unlock(&mNetworksLock);
469+ return d;
470+}
471+
363472 int Supplicant::setupConfig() {
364473 char buf[2048];
365474 int srcfd, destfd;
@@ -407,3 +516,60 @@ int Supplicant::setupConfig() {
407516 }
408517 return 0;
409518 }
519+
520+int Supplicant::setNetworkVar(int networkId, const char *var, const char *val) {
521+ char reply[255];
522+ size_t len = sizeof(reply) -1;
523+
524+ char *tmp;
525+ asprintf(&tmp, "SET_NETWORK %d %s \"%s\"", networkId, var, val);
526+ if (sendCommand(tmp, reply, &len)) {
527+ free(tmp);
528+ return -1;
529+ }
530+ free(tmp);
531+ return 0;
532+}
533+
534+const char *Supplicant::getNetworkVar(int networkId, const char *var,
535+ char *buffer, size_t max) {
536+ size_t len = max - 1;
537+ char *tmp;
538+
539+ asprintf(&tmp, "GET_NETWORK %d %s", networkId, var);
540+ if (sendCommand(tmp, buffer, &len)) {
541+ free(tmp);
542+ return NULL;
543+ }
544+ free(tmp);
545+ return buffer;
546+}
547+
548+int Supplicant::enableNetwork(int networkId, bool enabled) {
549+ char req[64];
550+
551+ if (enabled)
552+ sprintf(req, "ENABLE_NETWORK %d", networkId);
553+ else
554+ sprintf(req, "DISABLE_NETWORK %d", networkId);
555+
556+ char reply[16];
557+ size_t len = sizeof(reply) -1;
558+
559+ if (sendCommand(req, reply, &len))
560+ return -1;
561+ return 0;
562+}
563+
564+
565+int Supplicant::retrieveInterfaceName() {
566+ char reply[255];
567+ size_t len = sizeof(reply) -1;
568+
569+ if (sendCommand("INTERFACES", reply, &len))
570+ return -1;
571+
572+ reply[strlen(reply)-1] = '\0';
573+ mInterfaceName = strdup(reply);
574+ return 0;
575+}
--- a/nexus/Supplicant.h
+++ b/nexus/Supplicant.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _SUPPLICANT_H
1718 #define _SUPPLICANT_H
1819
@@ -20,25 +21,35 @@ struct wpa_ctrl;
2021 class SupplicantListener;
2122 class SupplicantEvent;
2223 class ServiceManager;
24+class PropertyManager;
25+class Controller;
26+class WifiController;
2327
2428 #include <pthread.h>
2529
2630 #include "ScanResult.h"
2731 #include "WifiNetwork.h"
32+#include "IPropertyProvider.h"
2833
29-class Supplicant {
34+class Supplicant : public IPropertyProvider {
3035 private:
3136 struct wpa_ctrl *mCtrl;
3237 struct wpa_ctrl *mMonitor;
3338 SupplicantListener *mListener;
3439 int mState;
3540 ServiceManager *mServiceManager;
41+ PropertyManager *mPropMngr;
42+ WifiController *mController;
43+ char *mInterfaceName;
3644
3745 ScanResultCollection *mLatestScanResults;
3846 pthread_mutex_t mLatestScanResultsLock;
39-
47+
48+ WifiNetworkCollection *mNetworks;
49+ pthread_mutex_t mNetworksLock;
50+
4051 public:
41- Supplicant();
52+ Supplicant(WifiController *wc, PropertyManager *propmngr);
4253 virtual ~Supplicant();
4354
4455 int start();
@@ -48,12 +59,23 @@ public:
4859 int triggerScan(bool active);
4960 ScanResultCollection *createLatestScanResults();
5061
51- int addNetwork();
52- int removeNetwork(int networkId);
62+ WifiNetwork *createNetwork();
63+ WifiNetwork *lookupNetwork(int networkId);
64+ int removeNetwork(WifiNetwork *net);
5365 WifiNetworkCollection *createNetworkList();
66+ int refreshNetworkList();
67+
68+ int setNetworkVar(int networkId, const char *var, const char *value);
69+ const char *getNetworkVar(int networkid, const char *var, char *buffer,
70+ size_t max);
71+ int enableNetwork(int networkId, bool enabled);
5472
5573 int getState() { return mState; }
74+ Controller *getController() { return (Controller *) mController; }
75+ const char *getInterfaceName() { return mInterfaceName; }
5676
77+ int set(const char *name, const char *value);
78+ const char *get(const char *name, char *buffer, size_t max);
5779
5880 // XXX: Extract these into an interface
5981 // handlers for SupplicantListener
@@ -76,6 +98,7 @@ private:
7698 int connectToSupplicant();
7799 int sendCommand(const char *cmd, char *reply, size_t *reply_len);
78100 int setupConfig();
101+ int retrieveInterfaceName();
79102 };
80103
81104 #endif
--- a/nexus/SupplicantEvent.cpp
+++ b/nexus/SupplicantEvent.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdlib.h>
1718
1819 #define LOG_TAG "SupplicantEvent"
--- a/nexus/SupplicantEvent.h
+++ b/nexus/SupplicantEvent.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _SUPPLICANT_EVENT_H
1718 #define _SUPPLICANT_EVENT_H
1819
--- a/nexus/SupplicantListener.cpp
+++ b/nexus/SupplicantListener.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <errno.h>
1718 #include <sys/types.h>
1819 #include <pthread.h>
@@ -85,7 +86,7 @@ bool SupplicantListener::onDataAvailable(SocketClient *cli) {
8586 }
8687
8788 delete evt;
88-
89+
8990 if (rc) {
9091 LOGW("Handler %d (%s) error: %s", evt->getType(), evt->getEvent(), strerror(errno));
9192 return false;
--- a/nexus/SupplicantListener.h
+++ b/nexus/SupplicantListener.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _SUPPLICANTLISTENER_H__
1718 #define _SUPPLICANTLISTENER_H__
1819
--- /dev/null
+++ b/nexus/SupplicantState.cpp
@@ -0,0 +1,49 @@
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 <stdio.h>
18+
19+#define LOG_TAG "SupplicantState"
20+#include <cutils/log.h>
21+
22+#include "SupplicantState.h"
23+
24+char *SupplicantState::toString(int val, char *buffer, int max) {
25+ if (val == SupplicantState::UNKNOWN)
26+ strncpy(buffer, "Unknown", max);
27+ else if (val == SupplicantState::DISCONNECTED)
28+ strncpy(buffer, "Disconnected", max);
29+ else if (val == SupplicantState::INACTIVE)
30+ strncpy(buffer, "Inactive", max);
31+ else if (val == SupplicantState::SCANNING)
32+ strncpy(buffer, "Scanning", max);
33+ else if (val == SupplicantState::ASSOCIATING)
34+ strncpy(buffer, "Associating", max);
35+ else if (val == SupplicantState::ASSOCIATED)
36+ strncpy(buffer, "Associated", max);
37+ else if (val == SupplicantState::FOURWAY_HANDSHAKE)
38+ strncpy(buffer, "Fourway Handshake", max);
39+ else if (val == SupplicantState::GROUP_HANDSHAKE)
40+ strncpy(buffer, "Group Handshake", max);
41+ else if (val == SupplicantState::COMPLETED)
42+ strncpy(buffer, "Completed", max);
43+ else if (val == SupplicantState::IDLE)
44+ strncpy(buffer, "Idle", max);
45+ else
46+ strncpy(buffer, "(internal error)", max);
47+
48+ return buffer;
49+}
--- a/nexus/SupplicantState.h
+++ b/nexus/SupplicantState.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _SUPPLICANT_STATE_H
1718 #define _SUPPLICANT_STATE_H
1819
@@ -28,6 +29,8 @@ public:
2829 static const int GROUP_HANDSHAKE = 6;
2930 static const int COMPLETED = 7;
3031 static const int IDLE = 8;
32+
33+ static char *toString(int val, char *buffer, int max);
3134 };
3235
3336 #endif
--- a/nexus/TiwlanWifiController.cpp
+++ b/nexus/TiwlanWifiController.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdlib.h>
1718 #include <fcntl.h>
1819 #include <errno.h>
@@ -22,14 +23,15 @@
2223 #define LOG_TAG "TiwlanWifiController"
2324 #include <cutils/log.h>
2425
26+#include "PropertyManager.h"
2527 #include "TiwlanWifiController.h"
2628
2729 #define DRIVER_PROP_NAME "wlan.driver.status"
2830
2931 extern "C" int sched_yield(void);
3032
31-TiwlanWifiController::TiwlanWifiController(char *modpath, char *modname, char *modargs) :
32- WifiController(modpath, modname, modargs) {
33+TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) :
34+ WifiController(propmngr, modpath, modname, modargs) {
3335 }
3436
3537 int TiwlanWifiController::powerUp() {
--- a/nexus/TiwlanWifiController.h
+++ b/nexus/TiwlanWifiController.h
@@ -13,14 +13,16 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _TIWLAN_WIFI_CONTROLLER_H
1718 #define _TIWLAN_WIFI_CONTROLLER_H
1819
20+#include "PropertyManager.h"
1921 #include "WifiController.h"
2022
2123 class TiwlanWifiController : public WifiController {
2224 public:
23- TiwlanWifiController(char *modpath, char *modname, char *modargs);
25+ TiwlanWifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs);
2426 virtual ~TiwlanWifiController() {}
2527
2628 virtual int powerUp();
--- a/nexus/VpnController.cpp
+++ b/nexus/VpnController.cpp
@@ -16,55 +16,61 @@
1616
1717 #include <stdio.h>
1818 #include <string.h>
19+#include <stdlib.h>
1920 #include <errno.h>
2021 #include <sys/socket.h>
2122 #include <netinet/in.h>
2223 #include <arpa/inet.h>
2324
25+#include "PropertyManager.h"
2426 #include "VpnController.h"
2527
26-VpnController::VpnController() :
27- Controller("VPN", "vpn") {
28- registerProperty("gateway");
28+VpnController::VpnController(PropertyManager *propmngr) :
29+ Controller("VPN", propmngr) {
30+ mEnabled = false;
31+ propmngr->registerProperty("vpn.enabled", this);
32+ propmngr->registerProperty("vpn.gateway", this);
2933 }
3034
3135 int VpnController::start() {
32- errno = ENOSYS;
33- return -1;
36+ return 0;
3437 }
3538
3639 int VpnController::stop() {
37- errno = ENOSYS;
38- return -1;
40+ return 0;
3941 }
4042
41-int VpnController::enable() {
42- errno = ENOSYS;
43- return -1;
44-}
43+int VpnController::set(const char *name, const char *value) {
44+ if (!strcmp(name, "vpn.enabled")) {
45+ int en = atoi(value);
46+ int rc;
4547
46-int VpnController::disable() {
47- errno = ENOSYS;
48- return -1;
49-}
48+ if (en == mEnabled)
49+ return 0;
50+ rc = (en ? enable() : disable());
5051
51-int VpnController::setProperty(const char *name, char *value) {
52- if (!strcmp(name, "gateway")) {
52+ if (!rc)
53+ mEnabled = en;
54+ return rc;
55+ } if (!strcmp(name, "vpn.gateway")) {
5356 if (!inet_aton(value, &mVpnGateway)) {
5457 errno = EINVAL;
5558 return -1;
5659 }
5760 return 0;
58- }
61+ }
5962
60- return Controller::setProperty(name, value);
63+ return Controller::set(name, value);
6164 }
6265
63-const char *VpnController::getProperty(const char *name, char *buffer, size_t maxsize) {
64- if (!strcmp(name, "gateway")) {
66+const char *VpnController::get(const char *name, char *buffer, size_t maxsize) {
67+ if (!strcmp(name, "vpn.enabled")) {
68+ snprintf(buffer, maxsize, "%d", mEnabled);
69+ return buffer;
70+ } if (!strcmp(name, "vpn.gateway")) {
6571 snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway));
6672 return buffer;
6773 }
6874
69- return Controller::getProperty(name, buffer, maxsize);
75+ return Controller::get(name, buffer, maxsize);
7076 }
--- a/nexus/VpnController.h
+++ b/nexus/VpnController.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _VPN_CONTROLLER_H
1718 #define _VPN_CONTROLLER_H
1819
@@ -21,25 +22,25 @@
2122 #include "Controller.h"
2223
2324 class VpnController : public Controller {
25+ bool mEnabled;
2426 /*
2527 * Gateway of the VPN server to connect to
2628 */
2729 struct in_addr mVpnGateway;
2830
2931 public:
30- VpnController();
32+ VpnController(PropertyManager *propmngr);
3133 virtual ~VpnController() {}
3234
3335 virtual int start();
3436 virtual int stop();
3537
36- virtual int setProperty(const char *name, char *value);
37- virtual const char *getProperty(const char *name, char *buffer,
38- size_t maxlen);
38+ virtual int set(const char *name, const char *value);
39+ virtual const char *get(const char *name, char *buffer, size_t maxlen);
3940
40-private:
41- virtual int enable();
42- virtual int disable();
41+protected:
42+ virtual int enable() = 0;
43+ virtual int disable() = 0;
4344
4445 };
4546
--- a/nexus/WifiController.cpp
+++ b/nexus/WifiController.cpp
@@ -26,18 +26,21 @@
2626 #include "WifiScanner.h"
2727 #include "NetworkManager.h"
2828 #include "ErrorCode.h"
29+#include "WifiNetwork.h"
2930
30-WifiController::WifiController(char *modpath, char *modname, char *modargs) :
31- Controller("WIFI", "wifi") {
31+WifiController::WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs) :
32+ Controller("WIFI", propmngr) {
3233 strncpy(mModulePath, modpath, sizeof(mModulePath));
3334 strncpy(mModuleName, modname, sizeof(mModuleName));
3435 strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
3536
36- mSupplicant = new Supplicant();
37+ mSupplicant = new Supplicant(this, propmngr);
3738 mScanner = new WifiScanner(mSupplicant, 10);
3839 mCurrentScanMode = 0;
3940
40- registerProperty("scanmode");
41+ mEnabled = false;
42+
43+ propmngr->registerProperty("wifi.enabled", this);
4144 }
4245
4346 int WifiController::start() {
@@ -82,6 +85,17 @@ int WifiController::enable() {
8285 }
8386 }
8487
88+ if (Controller::bindInterface(mSupplicant->getInterfaceName())) {
89+ LOGE("Error binding interface (%s)", strerror(errno));
90+ goto out_unloadmodule;
91+ }
92+
93+ if (mSupplicant->refreshNetworkList())
94+ LOGW("Error getting list of networks (%s)", strerror(errno));
95+
96+ mPropMngr->registerProperty("wifi.scanmode", this);
97+ mPropMngr->registerProperty("wifi.interface", this);
98+
8599 return 0;
86100
87101 out_unloadmodule:
@@ -106,13 +120,14 @@ void WifiController::sendStatusBroadcast(const char *msg) {
106120
107121 int WifiController::disable() {
108122
123+ mPropMngr->unregisterProperty("wifi.scanmode");
109124 if (mSupplicant->isStarted()) {
110125 sendStatusBroadcast("STOPPING_SUPPLICANT");
111126 if (mSupplicant->stop()) {
112127 LOGE("Supplicant stop failed (%s)", strerror(errno));
113128 return -1;
114129 }
115- } else
130+ } else
116131 LOGW("disable(): Supplicant not running?");
117132
118133 if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
@@ -155,36 +170,60 @@ int WifiController::setScanMode(uint32_t mode) {
155170 return rc;
156171 }
157172
158-int WifiController::addNetwork() {
159- return mSupplicant->addNetwork();
173+WifiNetwork *WifiController::createNetwork() {
174+ WifiNetwork *wn = mSupplicant->createNetwork();
175+ return wn;
160176 }
161177
162178 int WifiController::removeNetwork(int networkId) {
163- return mSupplicant->removeNetwork(networkId);
179+ WifiNetwork *wn = mSupplicant->lookupNetwork(networkId);
180+
181+ if (!wn)
182+ return -1;
183+ return mSupplicant->removeNetwork(wn);
164184 }
165185
166186 ScanResultCollection *WifiController::createScanResults() {
167187 return mSupplicant->createLatestScanResults();
168188 }
169189
170-// XXX: This should be a const list
171190 WifiNetworkCollection *WifiController::createNetworkList() {
172191 return mSupplicant->createNetworkList();
173192 }
174193
175-int WifiController::setProperty(const char *name, char *value) {
176- if (!strcmp(name, "scanmode"))
194+int WifiController::set(const char *name, const char *value) {
195+ int rc;
196+
197+ if (!strcmp(name, "wifi.enabled")) {
198+ int en = atoi(value);
199+
200+ if (en == mEnabled)
201+ return 0;
202+ rc = (en ? enable() : disable());
203+ if (!rc)
204+ mEnabled = en;
205+ } else if (!strcmp(name, "wifi.interface")) {
206+ errno = EROFS;
207+ return -1;
208+ } else if (!strcmp(name, "wifi.scanmode"))
177209 return setScanMode((uint32_t) strtoul(value, NULL, 0));
178-
179- return Controller::setProperty(name, value);
210+ else
211+ return Controller::set(name, value);
212+ return rc;
180213 }
181214
182-const char *WifiController::getProperty(const char *name, char *buffer, size_t maxsize) {
183- if (!strcmp(name, "scanmode")) {
215+const char *WifiController::get(const char *name, char *buffer, size_t maxsize) {
216+
217+ if (!strcmp(name, "wifi.enabled"))
218+ snprintf(buffer, maxsize, "%d", mEnabled);
219+ else if (!strcmp(name, "wifi.interface")) {
220+ snprintf(buffer, maxsize, "%s",
221+ (getBoundInterface() ? getBoundInterface() : "none"));
222+ } else if (!strcmp(name, "wifi.scanmode"))
184223 snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode);
185- return buffer;
186- }
224+ else
225+ return Controller::get(name, buffer, maxsize);
187226
188- return Controller::getProperty(name, buffer, maxsize);
227+ return buffer;
189228 }
190229
--- a/nexus/WifiController.h
+++ b/nexus/WifiController.h
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #ifndef _WIFI_CONTROLLER_H
1718 #define _WIFI_CONTROLLER_H
1819
@@ -46,21 +47,21 @@ private:
4647 char mModuleArgs[255];
4748 uint32_t mCurrentScanMode;
4849 WifiScanner *mScanner;
50+ bool mEnabled;
4951
5052 public:
51- WifiController(char *modpath, char *modname, char *modargs);
53+ WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs);
5254 virtual ~WifiController() {}
5355
5456 int start();
5557 int stop();
5658
57- int addNetwork();
59+ WifiNetwork *createNetwork();
5860 int removeNetwork(int networkId);
5961 WifiNetworkCollection *createNetworkList();
6062
61- virtual int setProperty(const char *name, char *value);
62- virtual const char *getProperty(const char *name, char *buffer,
63- size_t maxlen);
63+ virtual int set(const char *name, const char *value);
64+ virtual const char *get(const char *name, char *buffer, size_t maxlen);
6465
6566 ScanResultCollection *createScanResults();
6667
@@ -71,6 +72,7 @@ public:
7172 Supplicant *getSupplicant() { return mSupplicant; }
7273
7374 protected:
75+ // Move this crap into a 'driver'
7476 virtual int powerUp() = 0;
7577 virtual int powerDown() = 0;
7678 virtual int loadFirmware();
@@ -78,13 +80,11 @@ protected:
7880 virtual bool isFirmwareLoaded() = 0;
7981 virtual bool isPoweredUp() = 0;
8082
81- void sendStatusBroadcast(const char *msg);
82-
8383 private:
84+ void sendStatusBroadcast(const char *msg);
8485 int setScanMode(uint32_t mode);
8586 int enable();
8687 int disable();
87-
8888 };
8989
9090 #endif
--- a/nexus/WifiNetwork.cpp
+++ b/nexus/WifiNetwork.cpp
@@ -16,14 +16,93 @@
1616
1717 #include <errno.h>
1818 #include <string.h>
19+#include <stdlib.h>
1920 #include <sys/types.h>
2021
22+#define LOG_TAG "WifiNetwork"
23+#include <cutils/log.h>
24+
25+#include "NetworkManager.h"
2126 #include "WifiNetwork.h"
2227 #include "Supplicant.h"
28+#include "WifiController.h"
29+#include "InterfaceConfig.h"
30+
31+const char *WifiNetwork::PropertyNames[] = { "ssid", "bssid", "psk", "wepkey.1",
32+ "wepkey.2", "wepkey.3", "wepkey.4",
33+ "defkeyidx", "pri", "hiddenssid",
34+ "AllowedKeyManagement",
35+ "AllowedProtocols",
36+ "AllowedAuthAlgorithms",
37+ "AllowedPairwiseCiphers",
38+ "AllowedGroupCiphers",
39+ "enabled", '\0' };
40+WifiNetwork::WifiNetwork() {
41+ // This is private to restrict copy constructors
42+}
43+
44+WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, const char *data) {
45+ mController = c;
46+ mSuppl = suppl;
47+
48+ char *tmp = strdup(data);
49+ char *next = tmp;
50+ char *id;
51+ char *ssid;
52+ char *bssid;
53+ char *flags;
54+
55+ if (!(id = strsep(&next, "\t")))
56+ LOGE("Failed to extract network id");
57+ if (!(ssid = strsep(&next, "\t")))
58+ LOGE("Failed to extract ssid");
59+ if (!(bssid = strsep(&next, "\t")))
60+ LOGE("Failed to extract bssid");
61+ if (!(flags = strsep(&next, "\t")))
62+ LOGE("Failed to extract flags");
63+
64+ // LOGD("id '%s', ssid '%s', bssid '%s', flags '%s'", id, ssid, bssid,
65+ // flags ? flags :"null");
66+
67+ if (id)
68+ mNetid = atoi(id);
69+ if (ssid)
70+ mSsid = strdup(ssid);
71+ if (bssid)
72+ mBssid = strdup(bssid);
73+
74+ mPsk = NULL;
75+ memset(mWepKeys, 0, sizeof(mWepKeys));
76+ mDefaultKeyIndex = -1;
77+ mPriority = -1;
78+ mHiddenSsid = NULL;
79+ mAllowedKeyManagement = 0;
80+ mAllowedProtocols = 0;
81+ mAllowedAuthAlgorithms = 0;
82+ mAllowedPairwiseCiphers = 0;
83+ mAllowedGroupCiphers = 0;
84+ mEnabled = true;
85+
86+ if (flags && flags[0] != '\0') {
87+ if (!strcmp(flags, "[DISABLED]"))
88+ mEnabled = false;
89+ else
90+ LOGW("Unsupported flags '%s'", flags);
91+ }
2392
24-WifiNetwork::WifiNetwork(Supplicant *suppl) {
93+ char *tmp2;
94+ asprintf(&tmp2, "wifi.net.%d", mNetid);
95+ mIfaceCfg = new InterfaceConfig(tmp2);
96+ free(tmp2);
97+
98+ registerProperties();
99+ free(tmp);
100+}
101+
102+WifiNetwork::WifiNetwork(WifiController *c, Supplicant *suppl, int networkId) {
103+ mController = c;
25104 mSuppl = suppl;
26- mNetid = -1;
105+ mNetid = networkId;
27106 mSsid = NULL;
28107 mBssid = NULL;
29108 mPsk = NULL;
@@ -36,9 +115,45 @@ WifiNetwork::WifiNetwork(Supplicant *suppl) {
36115 mAllowedAuthAlgorithms = 0;
37116 mAllowedPairwiseCiphers = 0;
38117 mAllowedGroupCiphers = 0;
118+ mEnabled = false;
119+
120+ char *tmp2;
121+ asprintf(&tmp2, "wifi.net.%d", mNetid);
122+ mIfaceCfg = new InterfaceConfig(tmp2);
123+ free(tmp2);
124+
125+ registerProperties();
126+}
127+
128+WifiNetwork *WifiNetwork::clone() {
129+ WifiNetwork *r = new WifiNetwork();
130+
131+ r->mSuppl = mSuppl;
132+ r->mNetid = mNetid;
133+
134+ if (mSsid)
135+ r->mSsid = strdup(mSsid);
136+ if (mBssid)
137+ r->mBssid = strdup(mBssid);
138+ if (mPsk)
139+ r->mPsk = strdup(mPsk);
140+
141+ r->mController = mController;
142+ memcpy(r->mWepKeys, mWepKeys, sizeof(mWepKeys));
143+ r->mDefaultKeyIndex = mDefaultKeyIndex;
144+ r->mPriority = mPriority;
145+ if (mHiddenSsid)
146+ r->mHiddenSsid = strdup(mHiddenSsid);
147+ r->mAllowedKeyManagement = mAllowedKeyManagement;
148+ r->mAllowedProtocols = mAllowedProtocols;
149+ r->mAllowedAuthAlgorithms = mAllowedAuthAlgorithms;
150+ r->mAllowedPairwiseCiphers = mAllowedPairwiseCiphers;
151+ r->mAllowedGroupCiphers = mAllowedGroupCiphers;
152+ return r;
39153 }
40154
41155 WifiNetwork::~WifiNetwork() {
156+ unregisterProperties();
42157 if (mSsid)
43158 free(mSsid);
44159 if (mBssid)
@@ -49,61 +164,423 @@ WifiNetwork::~WifiNetwork() {
49164 if (mWepKeys[i])
50165 free(mWepKeys[i]);
51166 }
167+
52168 if (mHiddenSsid)
53169 free(mHiddenSsid);
170+ if (mIfaceCfg)
171+ delete(mIfaceCfg);
54172 }
55173
56-int WifiNetwork::setSsid(char *ssid) {
57- errno = ENOSYS;
174+int WifiNetwork::refresh() {
175+ char buffer[255];
176+ size_t len;
177+
178+ len = sizeof(buffer);
179+ if (mSuppl->getNetworkVar(mNetid, "psk", buffer, len))
180+ mPsk = strdup(buffer);
181+
182+ for (int i = 0; i < 4; i++) {
183+ char *name;
184+
185+ asprintf(&name, "wep_key%d", i);
186+ len = sizeof(buffer);
187+ if (mSuppl->getNetworkVar(mNetid, name, buffer, len))
188+ mWepKeys[i] = strdup(buffer);
189+ free(name);
190+ }
191+
192+ len = sizeof(buffer);
193+ if (mSuppl->getNetworkVar(mNetid, "wep_tx_keyidx", buffer, len))
194+ mDefaultKeyIndex = atoi(buffer);
195+
196+ len = sizeof(buffer);
197+ if (mSuppl->getNetworkVar(mNetid, "priority", buffer, len))
198+ mPriority = atoi(buffer);
199+
200+ len = sizeof(buffer);
201+ if (mSuppl->getNetworkVar(mNetid, "scan_ssid", buffer, len))
202+ mHiddenSsid = strdup(buffer);
203+
204+ len = sizeof(buffer);
205+ if (mSuppl->getNetworkVar(mNetid, "key_mgmt", buffer, len)) {
206+ // TODO
207+ }
208+
209+ len = sizeof(buffer);
210+ if (mSuppl->getNetworkVar(mNetid, "proto", buffer, len)) {
211+ // TODO
212+ }
213+
214+ len = sizeof(buffer);
215+ if (mSuppl->getNetworkVar(mNetid, "auth_alg", buffer, len)) {
216+ // TODO
217+ }
218+
219+ len = sizeof(buffer);
220+ if (mSuppl->getNetworkVar(mNetid, "pairwise", buffer, len)) {
221+ // TODO
222+ }
223+
224+ len = sizeof(buffer);
225+ if (mSuppl->getNetworkVar(mNetid, "group", buffer, len)) {
226+ // TODO
227+ }
228+
229+ return 0;
230+out_err:
231+ LOGE("Refresh failed (%s)",strerror(errno));
58232 return -1;
59233 }
60234
61-int WifiNetwork::setBssid(char *bssid) {
62- errno = ENOSYS;
235+int WifiNetwork::set(const char *name, const char *value) {
236+ char *n_tmp = strdup(name + strlen("wifi.net."));
237+ char *n_next = n_tmp;
238+ char *n_local;
239+ char *n_rest;
240+ int rc = 0;
241+
242+ if (!strsep(&n_next, ".")) // skip net id
243+ goto out_inval;
244+
245+ if (!(n_local = strsep(&n_next, ".")))
246+ goto out_inval;
247+
248+ n_rest = n_next;
249+
250+// LOGD("set(): var '%s'(%s / %s) = %s", name, n_local, n_rest, value);
251+ if (!strcasecmp(n_local, "enabled"))
252+ rc = setEnabled(atoi(value));
253+ else if (!strcmp(n_local, "ssid"))
254+ rc = setSsid(value);
255+ else if (!strcasecmp(n_local, "bssid"))
256+ rc = setBssid(value);
257+ else if (!strcasecmp(n_local, "psk"))
258+ rc = setPsk(value);
259+ else if (!strcasecmp(n_local, "wepkey"))
260+ rc = setWepKey(atoi(n_rest) -1, value);
261+ else if (!strcasecmp(n_local, "defkeyidx"))
262+ rc = setDefaultKeyIndex(atoi(value));
263+ else if (!strcasecmp(n_local, "pri"))
264+ rc = setPriority(atoi(value));
265+ else if (!strcasecmp(n_local, "hiddenssid"))
266+ rc = setHiddenSsid(value);
267+ else if (!strcasecmp(n_local, "AllowedKeyManagement")) {
268+ uint32_t mask = 0;
269+ bool none = false;
270+ char *v_tmp = strdup(value);
271+ char *v_next = v_tmp;
272+ char *v_token;
273+
274+ while((v_token = strsep(&v_next, " "))) {
275+ if (!strcasecmp(v_token, "NONE")) {
276+ mask = 0;
277+ none = true;
278+ } else if (!none) {
279+ if (!strcasecmp(v_token, "WPA_PSK"))
280+ mask |= KeyManagementMask::WPA_PSK;
281+ else if (!strcasecmp(v_token, "WPA_EAP"))
282+ mask |= KeyManagementMask::WPA_EAP;
283+ else if (!strcasecmp(v_token, "IEEE8021X"))
284+ mask |= KeyManagementMask::IEEE8021X;
285+ else {
286+ errno = EINVAL;
287+ rc = -1;
288+ free(v_tmp);
289+ goto out;
290+ }
291+ } else {
292+ errno = EINVAL;
293+ rc = -1;
294+ free(v_tmp);
295+ goto out;
296+ }
297+ }
298+ free(v_tmp);
299+ } else if (!strcasecmp(n_local, "AllowedProtocols")) {
300+ // TODO
301+ } else if (!strcasecmp(n_local, "AllowedPairwiseCiphers")) {
302+ // TODO
303+ } else if (!strcasecmp(n_local, "AllowedAuthAlgorithms")) {
304+ // TODO
305+ } else if (!strcasecmp(n_local, "AllowedGroupCiphers")) {
306+ // TODO
307+ } else {
308+ errno = ENOENT;
309+ free(n_tmp);
310+ return -1;
311+ }
312+
313+out:
314+ free(n_tmp);
315+ return rc;
316+
317+out_inval:
318+ errno = EINVAL;
319+ free(n_tmp);
63320 return -1;
64321 }
65322
66-int WifiNetwork::setPsk(char *psk) {
67- errno = ENOSYS;
68- return -1;
323+const char *WifiNetwork::get(const char *name, char *buffer, size_t maxsize) {
324+ char *n_tmp = strdup(name + strlen("wifi.net."));
325+ char *n_next = n_tmp;
326+ char *n_local;
327+ char fc[64];
328+ char rc[128];
329+
330+ if (!strsep(&n_next, ".")) // skip net id
331+ goto out_inval;
332+
333+ if (!(n_local = strsep(&n_next, ".")))
334+ goto out_inval;
335+
336+
337+ strncpy(fc, n_local, sizeof(fc));
338+ rc[0] = '\0';
339+ if (n_next)
340+ strncpy(rc, n_next, sizeof(rc));
341+
342+ free(n_tmp);
343+
344+ if (!strcasecmp(fc, "enabled"))
345+ snprintf(buffer, maxsize, "%d", getEnabled());
346+ else if (!strcasecmp(fc, "ssid")) {
347+ strncpy(buffer,
348+ getSsid() ? getSsid() : "none",
349+ maxsize);
350+ } else if (!strcasecmp(fc, "bssid")) {
351+ strncpy(buffer,
352+ getBssid() ? getBssid() : "none",
353+ maxsize);
354+ } else if (!strcasecmp(fc, "psk")) {
355+ strncpy(buffer,
356+ getPsk() ? getPsk() : "none",
357+ maxsize);
358+ } else if (!strcasecmp(fc, "wepkey")) {
359+ strncpy(buffer,
360+ getWepKey(atoi(rc)-1) ? getWepKey(atoi(rc)-1) : "none",
361+ maxsize);
362+ } else if (!strcasecmp(fc, "defkeyidx"))
363+ snprintf(buffer, maxsize, "%d", getDefaultKeyIndex());
364+ else if (!strcasecmp(fc, "pri"))
365+ snprintf(buffer, maxsize, "%d", getPriority());
366+ else if (!strcasecmp(fc, "hiddenssid")) {
367+ strncpy(buffer,
368+ getHiddenSsid() ? getHiddenSsid() : "none",
369+ maxsize);
370+ } else {
371+ strncpy(buffer, "(internal error)", maxsize);
372+ errno = ENOENT;
373+ return NULL;
374+ }
375+
376+ return buffer;
377+
378+out_inval:
379+ errno = EINVAL;
380+ free(n_tmp);
381+ return NULL;
69382 }
70383
71-int WifiNetwork::setWepKey(int idx, char *key) {
72- errno = ENOSYS;
73- return -1;
384+int WifiNetwork::setSsid(const char *ssid) {
385+ if (mSuppl->setNetworkVar(mNetid, "ssid", ssid))
386+ return -1;
387+ if (mSsid)
388+ free(mSsid);
389+ mSsid = strdup(ssid);
390+ return 0;
391+}
392+
393+int WifiNetwork::setBssid(const char *bssid) {
394+ if (mSuppl->setNetworkVar(mNetid, "bssid", bssid))
395+ return -1;
396+ if (mBssid)
397+ free(mBssid);
398+ mBssid = strdup(bssid);
399+ return 0;
400+}
401+
402+int WifiNetwork::setPsk(const char *psk) {
403+ if (mSuppl->setNetworkVar(mNetid, "psk", psk))
404+ return -1;
405+
406+ if (mPsk)
407+ free(mPsk);
408+ mPsk = strdup(psk);
409+ return 0;
410+}
411+
412+int WifiNetwork::setWepKey(int idx, const char *key) {
413+ char *name;
414+
415+ asprintf(&name, "wep_key%d", idx);
416+ int rc = mSuppl->setNetworkVar(mNetid, name, key);
417+ free(name);
418+
419+ if (rc)
420+ return -1;
421+
422+ if (mWepKeys[idx])
423+ free(mWepKeys[idx]);
424+ mWepKeys[idx] = strdup(key);
425+ return 0;
74426 }
75427
76428 int WifiNetwork::setDefaultKeyIndex(int idx) {
77- errno = ENOSYS;
78- return -1;
429+ char val[16];
430+ sprintf(val, "%d", idx);
431+ if (mSuppl->setNetworkVar(mNetid, "wep_tx_keyidx", val))
432+ return -1;
433+
434+ mDefaultKeyIndex = idx;
435+ return 0;
79436 }
80437
81-int WifiNetwork::setPriority(int idx) {
82- errno = ENOSYS;
83- return -1;
438+int WifiNetwork::setPriority(int priority) {
439+ char val[16];
440+ sprintf(val, "%d", priority);
441+ if (mSuppl->setNetworkVar(mNetid, "priority", val))
442+ return -1;
443+
444+ mPriority = priority;
445+ return 0;
84446 }
85447
86-int WifiNetwork::setHiddenSsid(char *ssid) {
87- errno = ENOSYS;
88- return -1;
448+int WifiNetwork::setHiddenSsid(const char *ssid) {
449+ if (mSuppl->setNetworkVar(mNetid, "scan_ssid", ssid))
450+ return -1;
451+
452+ if (mHiddenSsid)
453+ free(mHiddenSsid);
454+ mHiddenSsid = strdup(ssid);
455+ return 0;
89456 }
90457
91458 int WifiNetwork::setAllowedKeyManagement(uint32_t mask) {
92- errno = ENOSYS;
93- return -1;
459+ char accum[255];
460+
461+ if (mask == KeyManagementMask::NONE)
462+ strcpy(accum, "NONE");
463+ else {
464+ if (mask & KeyManagementMask::WPA_PSK)
465+ strcat(accum, "WPA_PSK ");
466+ if (mask & KeyManagementMask::WPA_EAP)
467+ strcat(accum, "WPA_EAP ");
468+ if (mask & KeyManagementMask::IEEE8021X)
469+ strcat(accum, "IEEE8021X ");
470+ }
471+
472+ if (mSuppl->setNetworkVar(mNetid, "key_mgmt", accum))
473+ return -1;
474+ mAllowedKeyManagement = mask;
475+ return 0;
94476 }
95477
96478 int WifiNetwork::setAllowedProtocols(uint32_t mask) {
97- errno = ENOSYS;
98- return -1;
479+ char accum[255];
480+
481+ accum[0] = '\0';
482+
483+ if (mask & SecurityProtocolMask::WPA)
484+ strcpy(accum, "WPA ");
485+
486+ if (mask & SecurityProtocolMask::RSN)
487+ strcat(accum, "RSN");
488+
489+ if (mSuppl->setNetworkVar(mNetid, "proto", accum))
490+ return -1;
491+ mAllowedProtocols = mask;
492+ return 0;
493+}
494+
495+int WifiNetwork::setAllowedAuthAlgorithms(uint32_t mask) {
496+ char accum[255];
497+
498+ accum[0] = '\0';
499+
500+ if (mask & AuthenticationAlgorithmMask::OPEN)
501+ strcpy(accum, "OPEN ");
502+
503+ if (mask & AuthenticationAlgorithmMask::SHARED)
504+ strcat(accum, "SHARED ");
505+
506+ if (mask & AuthenticationAlgorithmMask::LEAP)
507+ strcat(accum, "LEAP ");
508+
509+ if (mSuppl->setNetworkVar(mNetid, "auth_alg", accum))
510+ return -1;
511+
512+ mAllowedAuthAlgorithms = mask;
513+ return 0;
99514 }
100515
101516 int WifiNetwork::setAllowedPairwiseCiphers(uint32_t mask) {
102- errno = ENOSYS;
103- return -1;
517+ char accum[255];
518+
519+ if (mask == PairwiseCiphersMask::NONE)
520+ strcpy(accum, "NONE");
521+ else {
522+ if (mask & PairwiseCiphersMask::TKIP)
523+ strcat(accum, "TKIP ");
524+ if (mask & PairwiseCiphersMask::CCMP)
525+ strcat(accum, "CCMP ");
526+ }
527+
528+ if (mSuppl->setNetworkVar(mNetid, "pairwise", accum))
529+ return -1;
530+
531+ mAllowedPairwiseCiphers = mask;
532+ return 0;
104533 }
105534
106535 int WifiNetwork::setAllowedGroupCiphers(uint32_t mask) {
107- errno = ENOSYS;
108- return -1;
536+ char accum[255];
537+
538+ if (mask & GroupCiphersMask::WEP40)
539+ strcat(accum, "WEP40 ");
540+ if (mask & GroupCiphersMask::WEP104)
541+ strcat(accum, "WEP104 ");
542+ if (mask & GroupCiphersMask::TKIP)
543+ strcat(accum, "TKIP ");
544+ if (mask & GroupCiphersMask::CCMP)
545+ strcat(accum, "CCMP ");
546+
547+ if (mSuppl->setNetworkVar(mNetid, "group", accum))
548+ return -1;
549+ mAllowedGroupCiphers = mask;
550+ return 0;
551+}
552+
553+int WifiNetwork::setEnabled(bool enabled) {
554+ if (mSuppl->enableNetwork(mNetid, enabled))
555+ return -1;
556+
557+ mEnabled = enabled;
558+ return 0;
559+}
560+
561+int WifiNetwork::registerProperties() {
562+ for (const char **p = WifiNetwork::PropertyNames; *p != '\0'; p++) {
563+ char *tmp;
564+ asprintf(&tmp, "wifi.net.%d.%s", mNetid, *p);
565+
566+ if (NetworkManager::Instance()->getPropMngr()->registerProperty(tmp,
567+ this)) {
568+ free(tmp);
569+ return -1;
570+ }
571+ free(tmp);
572+ }
573+ return 0;
574+}
575+
576+int WifiNetwork::unregisterProperties() {
577+ for (const char **p = WifiNetwork::PropertyNames; *p != '\0'; p++) {
578+ char *tmp;
579+ asprintf(&tmp, "wifi.net.%d.%s", mNetid, *p);
580+
581+ if (NetworkManager::Instance()->getPropMngr()->unregisterProperty(tmp))
582+ LOGW("Unable to remove property '%s' (%s)", tmp, strerror(errno));
583+ free(tmp);
584+ }
585+ return 0;
109586 }
--- a/nexus/WifiNetwork.h
+++ b/nexus/WifiNetwork.h
@@ -19,7 +19,7 @@
1919
2020 #include <sys/types.h>
2121
22-#include "../../../frameworks/base/include/utils/List.h"
22+#include <utils/List.h>
2323
2424 class KeyManagementMask {
2525 public:
@@ -43,14 +43,14 @@ public:
4343 static const uint32_t LEAP = 0x04;
4444 };
4545
46-class PairwiseCipherMask {
46+class PairwiseCiphersMask {
4747 public:
4848 static const uint32_t NONE = 0x00;
4949 static const uint32_t TKIP = 0x01;
5050 static const uint32_t CCMP = 0x02;
5151 };
5252
53-class GroupCipherMask {
53+class GroupCiphersMask {
5454 public:
5555 static const uint32_t WEP40 = 0x01;
5656 static const uint32_t WEP104 = 0x02;
@@ -59,9 +59,20 @@ public:
5959 };
6060
6161 class Supplicant;
62+class InterfaceConfig;
63+class Controller;
64+class WifiController;
6265
63-class WifiNetwork {
66+#include "IPropertyProvider.h"
67+
68+class WifiNetwork : public IPropertyProvider{
69+public:
70+ static const char *PropertyNames[];
71+
72+private:
6473 Supplicant *mSuppl;
74+ InterfaceConfig *mIfaceCfg;
75+ WifiController *mController;
6576
6677 /*
6778 * Unique network id - normally provided by supplicant
@@ -138,10 +149,24 @@ class WifiNetwork {
138149 */
139150 uint32_t mAllowedGroupCiphers;
140151
152+ /*
153+ * Set if this Network is enabled
154+ */
155+ bool mEnabled;
156+
157+private:
158+ WifiNetwork();
159+ int registerProperties();
160+ int unregisterProperties();
161+
141162 public:
142- WifiNetwork(Supplicant *suppl);
163+ WifiNetwork(WifiController *c, Supplicant *suppl, int networkId);
164+ WifiNetwork(WifiController *c, Supplicant *suppl, const char *data);
165+
143166 virtual ~WifiNetwork();
144167
168+ WifiNetwork *clone();
169+
145170 int getNetworkId() { return mNetid; }
146171 const char *getSsid() { return mSsid; }
147172 const char *getBssid() { return mBssid; }
@@ -155,18 +180,30 @@ public:
155180 uint32_t getAllowedAuthAlgorithms() { return mAllowedAuthAlgorithms; }
156181 uint32_t getAllowedPairwiseCiphers() { return mAllowedPairwiseCiphers; }
157182 uint32_t getAllowedGroupCiphers() { return mAllowedGroupCiphers; }
183+ bool getEnabled() { return mEnabled; }
184+ Controller *getController() { return (Controller *) mController; }
158185
159- int setSsid(char *ssid);
160- int setBssid(char *bssid);
161- int setPsk(char *psk);
162- int setWepKey(int idx, char *key);
186+ int set(const char *name, const char *value);
187+ const char *get(const char *name, char *buffer, size_t maxsize);
188+
189+// InterfaceConfig *getIfaceCfg() { return mIfaceCfg; }
190+
191+ int setEnabled(bool enabled);
192+ int setSsid(const char *ssid);
193+ int setBssid(const char *bssid);
194+ int setPsk(const char *psk);
195+ int setWepKey(int idx, const char *key);
163196 int setDefaultKeyIndex(int idx);
164197 int setPriority(int pri);
165- int setHiddenSsid(char *ssid);
198+ int setHiddenSsid(const char *ssid);
166199 int setAllowedKeyManagement(uint32_t mask);
167200 int setAllowedProtocols(uint32_t mask);
201+ int setAllowedAuthAlgorithms(uint32_t mask);
168202 int setAllowedPairwiseCiphers(uint32_t mask);
169203 int setAllowedGroupCiphers(uint32_t mask);
204+
205+ // XXX:Should this really be exposed?.. meh
206+ int refresh();
170207 };
171208
172209 typedef android::List<WifiNetwork *> WifiNetworkCollection;
--- a/nexus/WifiScanner.cpp
+++ b/nexus/WifiScanner.cpp
@@ -1,3 +1,19 @@
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+
117 #include <stdlib.h>
218 #include <sys/socket.h>
319 #include <sys/select.h>
--- a/nexus/WifiScanner.h
+++ b/nexus/WifiScanner.h
@@ -1,3 +1,19 @@
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+
117 #ifndef _WIFISCANNER_H
218 #define _WIFISCANNER_H
319
--- a/nexus/main.cpp
+++ b/nexus/main.cpp
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdlib.h>
1718 #include <errno.h>
1819
@@ -39,10 +40,10 @@ int main() {
3940
4041 nm->setBroadcaster((SocketListener *) cl);
4142
42- nm->attachController(new LoopController());
43- nm->attachController(new TiwlanWifiController("/system/lib/modules/wlan.ko", "wlan", ""));
43+ nm->attachController(new LoopController(nm->getPropMngr()));
44+ nm->attachController(new TiwlanWifiController(nm->getPropMngr(), "/system/lib/modules/wlan.ko", "wlan", ""));
4445 // nm->attachController(new AndroidL2TPVpnController());
45- nm->attachController(new OpenVpnController());
46+ nm->attachController(new OpenVpnController(nm->getPropMngr()));
4647
4748
4849 if (NetworkManager::Instance()->run()) {
@@ -56,7 +57,7 @@ int main() {
5657 }
5758
5859 // XXX: we'll use the main thread for the NetworkManager eventually
59-
60+
6061 while(1) {
6162 sleep(1000);
6263 }
--- a/nexus/nexctl.c
+++ b/nexus/nexctl.c
@@ -13,6 +13,7 @@
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617 #include <stdio.h>
1718 #include <stdlib.h>
1819 #include <unistd.h>