system/corennnnn
リビジョン | 7c5e61394069f6351cb518fbfd6a89e8c2eda1bf (tree) |
---|---|
日時 | 2009-06-02 06:30:09 |
作者 | Mathias Agopian <mathias@goog...> |
コミッター | Mathias Agopian |
Merge commit 'goog/master' into merge_master
@@ -14,7 +14,6 @@ | ||
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | |
17 | -#include <utils/executablepath.h> | |
18 | 17 | #import <Carbon/Carbon.h> |
19 | 18 | #include <unistd.h> |
20 | 19 |
@@ -26,7 +26,6 @@ | ||
26 | 26 | * SUCH DAMAGE. |
27 | 27 | */ |
28 | 28 | |
29 | -#include <utils/executablepath.h> | |
30 | 29 | #import <Carbon/Carbon.h> |
31 | 30 | #include <unistd.h> |
32 | 31 |
@@ -59,9 +59,11 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) { | ||
59 | 59 | } |
60 | 60 | |
61 | 61 | void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { |
62 | - char *cm, *last; | |
62 | + char *next = cmd; | |
63 | + char *cm; | |
64 | + char *arg; | |
63 | 65 | |
64 | - if (!(cm = strtok_r(cmd, ":", &last))) { | |
66 | + if (!(cm = strsep(&next, ":"))) { | |
65 | 67 | cli->sendMsg(500, "Malformatted message", false); |
66 | 68 | return; |
67 | 69 | } |
@@ -72,8 +74,7 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { | ||
72 | 74 | FrameworkCommand *c = *i; |
73 | 75 | |
74 | 76 | if (!strcmp(cm, c->getCommand())) { |
75 | - cm += strlen(cm) +1; | |
76 | - if (c->runCommand(cli, cm)) { | |
77 | + if (c->runCommand(cli, next)) { | |
77 | 78 | LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno)); |
78 | 79 | } |
79 | 80 | return; |
@@ -67,9 +67,9 @@ bool NetlinkEvent::decode(char *buffer, int size) { | ||
67 | 67 | mAction = NlActionRemove; |
68 | 68 | else if (!strcmp(a, "change")) |
69 | 69 | mAction = NlActionChange; |
70 | - } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM="))) | |
70 | + } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM="))) | |
71 | 71 | mSeq = atoi(s + strlen("SEQNUM=")); |
72 | - else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM="))) | |
72 | + else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM="))) | |
73 | 73 | mSubsystem = strdup(s + strlen("SUBSYSTEM=")); |
74 | 74 | else |
75 | 75 | mParams[param_idx++] = strdup(s); |
@@ -16,7 +16,7 @@ SocketClient::SocketClient(int socket) { | ||
16 | 16 | |
17 | 17 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { |
18 | 18 | char *buf; |
19 | - | |
19 | + | |
20 | 20 | if (addErrno) { |
21 | 21 | buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8); |
22 | 22 | sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno)); |
@@ -42,7 +42,7 @@ int SocketClient::sendMsg(const char *msg) { | ||
42 | 42 | strcat(tmp, "\n"); |
43 | 43 | bp = tmp; |
44 | 44 | } |
45 | - | |
45 | + | |
46 | 46 | int rc = 0; |
47 | 47 | const char *p = bp; |
48 | 48 | int brtw = strlen(bp); |
@@ -125,7 +125,7 @@ void SocketListener::runListener() { | ||
125 | 125 | max = (*it)->getSocket(); |
126 | 126 | } |
127 | 127 | pthread_mutex_unlock(&mClientsLock); |
128 | - | |
128 | + | |
129 | 129 | if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) { |
130 | 130 | LOGE("select failed (%s)", strerror(errno)); |
131 | 131 | sleep(1); |
@@ -22,10 +22,13 @@ LOCAL_SRC_FILES:= \ | ||
22 | 22 | WifiScanner.cpp \ |
23 | 23 | WifiNetwork.cpp \ |
24 | 24 | OpenVpnController.cpp \ |
25 | + InterfaceConfig.cpp \ | |
26 | + PropertyManager.cpp \ | |
27 | + SupplicantState.cpp | |
25 | 28 | |
26 | 29 | LOCAL_MODULE:= nexus |
27 | 30 | |
28 | -LOCAL_C_INCLUDES := $(KERNEL_HEADERS) | |
31 | +LOCAL_C_INCLUDES := $(KERNEL_HEADERS) -I../../../frameworks/base/include/ | |
29 | 32 | |
30 | 33 | LOCAL_CFLAGS := |
31 | 34 |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - * Copyright (C) ErrorCode::CommandOkay8 The Android Open Source Project | |
2 | + * Copyright (C) 2008 The Android Open Source Project | |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdlib.h> |
17 | 18 | #include <sys/socket.h> |
18 | 19 | #include <netinet/in.h> |
@@ -26,6 +27,7 @@ | ||
26 | 27 | |
27 | 28 | #include "CommandListener.h" |
28 | 29 | #include "Controller.h" |
30 | +#include "Property.h" | |
29 | 31 | #include "NetworkManager.h" |
30 | 32 | #include "WifiController.h" |
31 | 33 | #include "VpnController.h" |
@@ -35,31 +37,32 @@ CommandListener::CommandListener() : | ||
35 | 37 | FrameworkListener("nexus") { |
36 | 38 | registerCmd(new WifiScanResultsCmd()); |
37 | 39 | registerCmd(new WifiListNetworksCmd()); |
38 | - registerCmd(new WifiAddNetworkCmd()); | |
40 | + registerCmd(new WifiCreateNetworkCmd()); | |
39 | 41 | registerCmd(new WifiRemoveNetworkCmd()); |
40 | 42 | |
41 | 43 | registerCmd(new GetCmd()); |
42 | 44 | registerCmd(new SetCmd()); |
45 | + registerCmd(new ListCmd()); | |
43 | 46 | } |
44 | - | |
47 | + | |
45 | 48 | /* ------------- |
46 | 49 | * Wifi Commands |
47 | 50 | * ------------ */ |
48 | 51 | |
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) { | |
54 | 57 | NetworkManager *nm = NetworkManager::Instance(); |
55 | 58 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
56 | - int networkId; | |
59 | + WifiNetwork *wn; | |
57 | 60 | |
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); | |
60 | 63 | else { |
61 | 64 | char tmp[128]; |
62 | - sprintf(tmp, "Added network id %d.", networkId); | |
65 | + sprintf(tmp, "Created network id %d.", wn->getNetworkId()); | |
63 | 66 | cli->sendMsg(ErrorCode::CommandOkay, tmp, false); |
64 | 67 | } |
65 | 68 | return 0; |
@@ -67,8 +70,8 @@ int CommandListener::WifiAddNetworkCmd::runCommand(SocketClient *cli, char *data | ||
67 | 70 | |
68 | 71 | CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() : |
69 | 72 | NexusCommand("wifi_remove_network") { |
70 | -} | |
71 | - | |
73 | +} | |
74 | + | |
72 | 75 | int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *data) { |
73 | 76 | NetworkManager *nm = NetworkManager::Instance(); |
74 | 77 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
@@ -83,7 +86,7 @@ int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *d | ||
83 | 86 | |
84 | 87 | CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() : |
85 | 88 | NexusCommand("wifi_scan_results") { |
86 | -} | |
89 | +} | |
87 | 90 | |
88 | 91 | int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) { |
89 | 92 | NetworkManager *nm = NetworkManager::Instance(); |
@@ -92,7 +95,7 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat | ||
92 | 95 | ScanResultCollection *src = wc->createScanResults(); |
93 | 96 | ScanResultCollection::iterator it; |
94 | 97 | char buffer[256]; |
95 | - | |
98 | + | |
96 | 99 | for(it = src->begin(); it != src->end(); ++it) { |
97 | 100 | sprintf(buffer, "%s:%u:%d:%s:%s", |
98 | 101 | (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(), |
@@ -103,13 +106,13 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat | ||
103 | 106 | } |
104 | 107 | |
105 | 108 | delete src; |
106 | - cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete", false); | |
109 | + cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete.", false); | |
107 | 110 | return 0; |
108 | 111 | } |
109 | 112 | |
110 | 113 | CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() : |
111 | 114 | NexusCommand("wifi_list_networks") { |
112 | -} | |
115 | +} | |
113 | 116 | |
114 | 117 | int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *data) { |
115 | 118 | NetworkManager *nm = NetworkManager::Instance(); |
@@ -118,12 +121,11 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da | ||
118 | 121 | WifiNetworkCollection *src = wc->createNetworkList(); |
119 | 122 | WifiNetworkCollection::iterator it; |
120 | 123 | char buffer[256]; |
121 | - | |
124 | + | |
122 | 125 | for(it = src->begin(); it != src->end(); ++it) { |
123 | 126 | sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid()); |
124 | 127 | cli->sendMsg(ErrorCode::WifiNetworkList, buffer, false); |
125 | 128 | delete (*it); |
126 | - it = src->erase(it); | |
127 | 129 | } |
128 | 130 | |
129 | 131 | delete src; |
@@ -140,34 +142,31 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da | ||
140 | 142 | * ---------------- */ |
141 | 143 | CommandListener::GetCmd::GetCmd() : |
142 | 144 | NexusCommand("get") { |
143 | -} | |
145 | +} | |
144 | 146 | |
145 | 147 | 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; | |
149 | 150 | |
150 | - if (!(bword = strtok_r(data, ":", &last))) | |
151 | + if (!(propname = strsep(&next, ":"))) | |
151 | 152 | goto out_inval; |
152 | - | |
153 | - strncpy(propname, bword, sizeof(propname)); | |
154 | 153 | |
155 | - char pb[255]; | |
154 | + char pb[Property::NameMaxSize + 6]; | |
156 | 155 | snprintf(pb, sizeof(pb), "%s:", propname); |
157 | 156 | |
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))) { | |
161 | 160 | goto out_inval; |
162 | 161 | } |
163 | 162 | |
164 | - cli->sendMsg(ErrorCode::VariableRead, pb, false); | |
163 | + cli->sendMsg(ErrorCode::PropertyRead, pb, false); | |
165 | 164 | |
166 | 165 | cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false); |
167 | 166 | return 0; |
168 | 167 | out_inval: |
169 | 168 | errno = EINVAL; |
170 | - cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true); | |
169 | + cli->sendMsg(ErrorCode::CommandParameterError, "Failed to read property.", true); | |
171 | 170 | return 0; |
172 | 171 | } |
173 | 172 |
@@ -178,8 +177,8 @@ CommandListener::SetCmd::SetCmd() : | ||
178 | 177 | int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) { |
179 | 178 | char *bword; |
180 | 179 | char *last; |
181 | - char propname[32]; | |
182 | - char propval[250]; | |
180 | + char propname[Property::NameMaxSize]; | |
181 | + char propval[Property::ValueMaxSize]; | |
183 | 182 | |
184 | 183 | if (!(bword = strtok_r(data, ":", &last))) |
185 | 184 | goto out_inval; |
@@ -191,7 +190,7 @@ int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) { | ||
191 | 190 | |
192 | 191 | strncpy(propval, bword, sizeof(propval)); |
193 | 192 | |
194 | - if (NetworkManager::Instance()->setProperty(propname, propval)) | |
193 | + if (NetworkManager::Instance()->getPropMngr()->set(propname, propval)) | |
195 | 194 | goto out_inval; |
196 | 195 | |
197 | 196 | cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false); |
@@ -202,3 +201,45 @@ out_inval: | ||
202 | 201 | cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set property.", true); |
203 | 202 | return 0; |
204 | 203 | } |
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 | +} |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _COMMANDLISTENER_H__ |
17 | 18 | #define _COMMANDLISTENER_H__ |
18 | 19 |
@@ -40,10 +41,10 @@ private: | ||
40 | 41 | int runCommand(SocketClient *c, char *data); |
41 | 42 | }; |
42 | 43 | |
43 | - class WifiAddNetworkCmd : public NexusCommand { | |
44 | + class WifiCreateNetworkCmd : public NexusCommand { | |
44 | 45 | public: |
45 | - WifiAddNetworkCmd(); | |
46 | - virtual ~WifiAddNetworkCmd() {} | |
46 | + WifiCreateNetworkCmd(); | |
47 | + virtual ~WifiCreateNetworkCmd() {} | |
47 | 48 | int runCommand(SocketClient *c, char *data); |
48 | 49 | }; |
49 | 50 |
@@ -74,6 +75,13 @@ private: | ||
74 | 75 | virtual ~GetCmd() {} |
75 | 76 | int runCommand(SocketClient *c, char *data); |
76 | 77 | }; |
78 | + | |
79 | + class ListCmd : public NexusCommand { | |
80 | + public: | |
81 | + ListCmd(); | |
82 | + virtual ~ListCmd() {} | |
83 | + int runCommand(SocketClient *c, char *data); | |
84 | + }; | |
77 | 85 | }; |
78 | 86 | |
79 | 87 | #endif |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdio.h> |
17 | 18 | #include <stdlib.h> |
18 | 19 | #include <string.h> |
@@ -33,13 +34,17 @@ | ||
33 | 34 | extern "C" int init_module(void *, unsigned int, const char *); |
34 | 35 | extern "C" int delete_module(const char *, unsigned int); |
35 | 36 | |
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 | +} | |
40 | 42 | |
41 | - mEnabled = false; | |
42 | - registerProperty("enable"); | |
43 | +Controller::~Controller() { | |
44 | + if (mBoundInterface) | |
45 | + free(mBoundInterface); | |
46 | + if (mName) | |
47 | + free(mName); | |
43 | 48 | } |
44 | 49 | |
45 | 50 | int Controller::start() { |
@@ -50,65 +55,16 @@ int Controller::stop() { | ||
50 | 55 | return 0; |
51 | 56 | } |
52 | 57 | |
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) { | |
70 | 59 | errno = ENOENT; |
71 | 60 | return -1; |
72 | 61 | } |
73 | 62 | |
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) { | |
80 | 64 | errno = ENOENT; |
81 | 65 | return NULL; |
82 | 66 | } |
83 | 67 | |
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 | - | |
112 | 68 | int Controller::loadKernelModule(char *modpath, const char *args) { |
113 | 69 | void *module; |
114 | 70 | unsigned int size; |
@@ -137,7 +93,7 @@ int Controller::unloadKernelModule(const char *modtag) { | ||
137 | 93 | } |
138 | 94 | |
139 | 95 | if (rc != 0) { |
140 | - LOGW("Unable to unload kernel driver '%s' (%s)", modtag, | |
96 | + LOGW("Unable to unload kernel driver '%s' (%s)", modtag, | |
141 | 97 | strerror(errno)); |
142 | 98 | } |
143 | 99 | return rc; |
@@ -203,3 +159,16 @@ bail: | ||
203 | 159 | close(fd); |
204 | 160 | return buffer; |
205 | 161 | } |
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 | +} |
@@ -13,51 +13,59 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _CONTROLLER_H |
17 | 18 | #define _CONTROLLER_H |
18 | 19 | |
19 | 20 | #include <unistd.h> |
20 | 21 | #include <sys/types.h> |
21 | 22 | |
22 | -#include "../../../frameworks/base/include/utils/List.h" | |
23 | +#include <utils/List.h> | |
24 | + | |
25 | +class PropertyManager; | |
23 | 26 | |
24 | -#include "PropertyCollection.h" | |
27 | +#include "PropertyManager.h" | |
28 | +#include "IPropertyProvider.h" | |
25 | 29 | |
26 | -class Controller { | |
30 | +class Controller : public IPropertyProvider { | |
27 | 31 | 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; | |
32 | 45 | |
33 | 46 | public: |
34 | - Controller(const char *name, const char *prefix); | |
35 | - virtual ~Controller() {} | |
47 | + Controller(const char *name, PropertyManager *propMngr); | |
48 | + virtual ~Controller(); | |
36 | 49 | |
37 | 50 | virtual int start(); |
38 | 51 | virtual int stop(); |
39 | 52 | |
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 | - | |
44 | 53 | 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); | |
46 | 59 | |
47 | 60 | protected: |
48 | 61 | int loadKernelModule(char *modpath, const char *args); |
49 | 62 | bool isKernelModuleLoaded(const char *modtag); |
50 | 63 | 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); | |
54 | 66 | |
55 | 67 | private: |
56 | 68 | void *loadFile(char *filename, unsigned int *_size); |
57 | - | |
58 | - virtual int enable() = 0; | |
59 | - virtual int disable() = 0; | |
60 | - | |
61 | 69 | }; |
62 | 70 | |
63 | 71 | typedef android::List<Controller *> ControllerCollection; |
@@ -26,8 +26,9 @@ public: | ||
26 | 26 | static const int WifiScanResult = 125; |
27 | 27 | static const int WifiNetworkList = 126; |
28 | 28 | |
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; | |
31 | 32 | |
32 | 33 | // 200 series - Requested action has been successfully completed |
33 | 34 | static const int CommandOkay = 200; |
@@ -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 |
@@ -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 | +} |
@@ -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 |
@@ -13,20 +13,21 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <errno.h> |
17 | 18 | |
18 | 19 | #include "LoopController.h" |
20 | +#include "PropertyManager.h" | |
19 | 21 | |
20 | -LoopController::LoopController() : | |
21 | - Controller("LOOP", "loop") { | |
22 | +LoopController::LoopController(PropertyManager *propmngr) : | |
23 | + Controller("LOOP", propmngr) { | |
22 | 24 | } |
23 | 25 | |
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); | |
27 | 28 | } |
28 | 29 | |
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); | |
32 | 32 | } |
33 | + |
@@ -13,19 +13,20 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _LOOP_CONTROLLER_H |
17 | 18 | #define _LOOP_CONTROLLER_H |
18 | 19 | |
19 | 20 | #include "Controller.h" |
20 | 21 | |
22 | + | |
21 | 23 | class LoopController : public Controller { |
22 | 24 | public: |
23 | - LoopController(); | |
25 | + LoopController(PropertyManager *propmngr); | |
24 | 26 | virtual ~LoopController() {} |
25 | 27 | |
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); | |
29 | 30 | }; |
30 | 31 | |
31 | 32 | #endif |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdio.h> |
17 | 18 | #include <errno.h> |
18 | 19 |
@@ -21,18 +22,23 @@ | ||
21 | 22 | #include <cutils/log.h> |
22 | 23 | |
23 | 24 | #include "NetworkManager.h" |
25 | +#include "InterfaceConfig.h" | |
24 | 26 | |
25 | 27 | NetworkManager *NetworkManager::sInstance = NULL; |
26 | 28 | |
27 | 29 | NetworkManager *NetworkManager::Instance() { |
28 | 30 | if (!sInstance) |
29 | - sInstance = new NetworkManager(); | |
31 | + sInstance = new NetworkManager(new PropertyManager()); | |
30 | 32 | return sInstance; |
31 | 33 | } |
32 | 34 | |
33 | -NetworkManager::NetworkManager() { | |
35 | +NetworkManager::NetworkManager(PropertyManager *propMngr) { | |
34 | 36 | mBroadcaster = NULL; |
35 | 37 | mControllers = new ControllerCollection(); |
38 | + mPropMngr = propMngr; | |
39 | +} | |
40 | + | |
41 | +NetworkManager::~NetworkManager() { | |
36 | 42 | } |
37 | 43 | |
38 | 44 | int NetworkManager::run() { |
@@ -54,7 +60,7 @@ int NetworkManager::startControllers() { | ||
54 | 60 | for (i = mControllers->begin(); i != mControllers->end(); ++i) { |
55 | 61 | int irc = (*i)->start(); |
56 | 62 | LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc); |
57 | - if (irc && !rc) | |
63 | + if (irc && !rc) | |
58 | 64 | rc = irc; |
59 | 65 | } |
60 | 66 | return rc; |
@@ -67,7 +73,7 @@ int NetworkManager::stopControllers() { | ||
67 | 73 | for (i = mControllers->begin(); i != mControllers->end(); ++i) { |
68 | 74 | int irc = (*i)->stop(); |
69 | 75 | LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc); |
70 | - if (irc && !rc) | |
76 | + if (irc && !rc) | |
71 | 77 | rc = irc; |
72 | 78 | } |
73 | 79 | return rc; |
@@ -83,79 +89,23 @@ Controller *NetworkManager::findController(const char *name) { | ||
83 | 89 | return NULL; |
84 | 90 | } |
85 | 91 | |
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()); | |
92 | 94 | |
93 | - if (!(prefix = strsep(&next, "."))) | |
94 | - goto out_inval; | |
95 | + // Look up the interface | |
95 | 96 | |
96 | - rest = next; | |
97 | - | |
98 | - if (!strncasecmp(prefix, "netman", 6)) { | |
99 | - errno = ENOSYS; | |
97 | + if (0) { // already started? | |
98 | + errno = EADDRINUSE; | |
100 | 99 | return -1; |
101 | 100 | } |
102 | 101 | |
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 { | |
139 | 104 | } |
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()); | |
155 | 105 | return 0; |
156 | 106 | } |
157 | 107 | |
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()); | |
160 | 110 | return 0; |
161 | 111 | } |
@@ -13,13 +13,17 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _NETWORKMANAGER_H |
17 | 18 | #define _NETWORKMANAGER_H |
18 | 19 | |
19 | 20 | #include <sysutils/SocketListener.h> |
20 | 21 | |
21 | 22 | #include "Controller.h" |
22 | -#include "PropertyCollection.h" | |
23 | + | |
24 | +#include "PropertyManager.h" | |
25 | + | |
26 | +class InterfaceConfig; | |
23 | 27 | |
24 | 28 | class NetworkManager { |
25 | 29 | private: |
@@ -28,10 +32,10 @@ private: | ||
28 | 32 | private: |
29 | 33 | ControllerCollection *mControllers; |
30 | 34 | SocketListener *mBroadcaster; |
31 | - PropertyCollection *mProperties; | |
35 | + PropertyManager *mPropMngr; | |
32 | 36 | |
33 | 37 | public: |
34 | - virtual ~NetworkManager() {} | |
38 | + virtual ~NetworkManager(); | |
35 | 39 | |
36 | 40 | int run(); |
37 | 41 |
@@ -39,27 +43,28 @@ public: | ||
39 | 43 | |
40 | 44 | Controller *findController(const char *name); |
41 | 45 | |
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 | - | |
46 | 46 | void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; } |
47 | 47 | SocketListener *getBroadcaster() { return mBroadcaster; } |
48 | + PropertyManager *getPropMngr() { return mPropMngr; } | |
48 | 49 | |
49 | 50 | static NetworkManager *Instance(); |
50 | 51 | |
51 | 52 | private: |
52 | 53 | int startControllers(); |
53 | 54 | int stopControllers(); |
54 | - int registerProperty(const char *name); | |
55 | - int unregisterProperty(const char *name); | |
56 | 55 | |
57 | - NetworkManager(); | |
56 | + NetworkManager(PropertyManager *propMngr); | |
58 | 57 | |
59 | 58 | 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); | |
63 | 64 | |
65 | + /* | |
66 | + * Called from a controller when an interface should be shut down | |
67 | + */ | |
68 | + int onInterfaceStop(Controller *c, const char *name); | |
64 | 69 | }; |
65 | 70 | #endif |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include "NexusCommand.h" |
17 | 18 | |
18 | 19 | NexusCommand::NexusCommand(const char *cmd) : |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _NEXUS_COMMAND_H |
17 | 18 | #define _NEXUS_COMMAND_H |
18 | 19 |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <errno.h> |
17 | 18 | #include <netinet/in.h> |
18 | 19 | #include <arpa/inet.h> |
@@ -24,13 +25,13 @@ | ||
24 | 25 | #include <sysutils/ServiceManager.h> |
25 | 26 | |
26 | 27 | #include "OpenVpnController.h" |
28 | +#include "PropertyManager.h" | |
27 | 29 | |
28 | 30 | #define DAEMON_PROP_NAME "vpn.openvpn.status" |
29 | - | |
30 | 31 | #define DAEMON_CONFIG_FILE "/data/misc/openvpn/openvpn.conf" |
31 | 32 | |
32 | -OpenVpnController::OpenVpnController() : | |
33 | - VpnController() { | |
33 | +OpenVpnController::OpenVpnController(PropertyManager *propmngr) : | |
34 | + VpnController(propmngr) { | |
34 | 35 | mServiceManager = new ServiceManager(); |
35 | 36 | } |
36 | 37 |
@@ -49,8 +50,8 @@ int OpenVpnController::stop() { | ||
49 | 50 | int OpenVpnController::enable() { |
50 | 51 | char svc[PROPERTY_VALUE_MAX]; |
51 | 52 | char tmp[64]; |
52 | - | |
53 | - if (!getProperty("vpn.gateway", tmp, sizeof(tmp))) { | |
53 | + | |
54 | + if (!mPropMngr->get("vpn.gateway", tmp, sizeof(tmp))) { | |
54 | 55 | LOGE("Error reading property 'vpn.gateway' (%s)", strerror(errno)); |
55 | 56 | return -1; |
56 | 57 | } |
@@ -17,6 +17,7 @@ | ||
17 | 17 | #ifndef _OPEN_VPN_CONTROLLER_H |
18 | 18 | #define _OPEN_VPN_CONTROLLER_H |
19 | 19 | |
20 | +#include "PropertyManager.h" | |
20 | 21 | #include "VpnController.h" |
21 | 22 | |
22 | 23 | class ServiceManager; |
@@ -26,7 +27,7 @@ private: | ||
26 | 27 | ServiceManager *mServiceManager; |
27 | 28 | |
28 | 29 | public: |
29 | - OpenVpnController(); | |
30 | + OpenVpnController(PropertyManager *propmngr); | |
30 | 31 | virtual ~OpenVpnController(); |
31 | 32 | |
32 | 33 | int start(); |
@@ -14,12 +14,8 @@ | ||
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | |
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 | +}; |
@@ -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 | +} |
@@ -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 |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdlib.h> |
17 | 18 | #include <ctype.h> |
18 | 19 |
@@ -42,7 +43,7 @@ ScanResult::ScanResult(char *rawResult) { | ||
42 | 43 | mFreq = atoi(tmp); |
43 | 44 | ++q; |
44 | 45 | |
45 | - // LEVEL | |
46 | + // LEVEL | |
46 | 47 | for (p = q; *q != '\t'; ++q); |
47 | 48 | strncpy(tmp, p, (q - p)); |
48 | 49 | tmp[q-p] = '\0'; |
@@ -60,7 +61,7 @@ ScanResult::ScanResult(char *rawResult) { | ||
60 | 61 | // haven't had time to dig into it ... |
61 | 62 | if (*q == '\t') |
62 | 63 | q++; |
63 | - | |
64 | + | |
64 | 65 | for (p = q; *q != '\t'; ++q) { |
65 | 66 | if (*q == '\0') |
66 | 67 | break; |
@@ -13,12 +13,13 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _SCAN_RESULT_H |
17 | 18 | #define _SCAN_RESULT_H |
18 | 19 | |
19 | 20 | #include <sys/types.h> |
20 | 21 | |
21 | -#include "../../../frameworks/base/include/utils/List.h" | |
22 | +#include <utils/List.h> | |
22 | 23 | |
23 | 24 | class ScanResult { |
24 | 25 | char *mBssid; |
@@ -32,8 +32,10 @@ | ||
32 | 32 | #include "SupplicantState.h" |
33 | 33 | #include "SupplicantEvent.h" |
34 | 34 | #include "ScanResult.h" |
35 | +#include "PropertyManager.h" | |
35 | 36 | #include "NetworkManager.h" |
36 | 37 | #include "ErrorCode.h" |
38 | +#include "WifiController.h" | |
37 | 39 | |
38 | 40 | #include "libwpa_client/wpa_ctrl.h" |
39 | 41 |
@@ -43,21 +45,29 @@ | ||
43 | 45 | #define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf" |
44 | 46 | #define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf" |
45 | 47 | |
46 | -Supplicant::Supplicant() { | |
48 | +Supplicant::Supplicant(WifiController *wc, PropertyManager *propmngr) { | |
49 | + mController = wc; | |
50 | + mPropMngr = propmngr; | |
51 | + mInterfaceName = NULL; | |
47 | 52 | mCtrl = NULL; |
48 | 53 | mMonitor = NULL; |
49 | 54 | mListener = NULL; |
50 | - | |
55 | + | |
51 | 56 | mState = SupplicantState::UNKNOWN; |
52 | 57 | |
53 | 58 | mServiceManager = new ServiceManager(); |
54 | 59 | |
55 | 60 | mLatestScanResults = new ScanResultCollection(); |
56 | 61 | pthread_mutex_init(&mLatestScanResultsLock, NULL); |
62 | + | |
63 | + mNetworks = new WifiNetworkCollection(); | |
64 | + pthread_mutex_init(&mNetworksLock, NULL); | |
57 | 65 | } |
58 | 66 | |
59 | 67 | Supplicant::~Supplicant() { |
60 | 68 | delete mServiceManager; |
69 | + if (mInterfaceName) | |
70 | + free(mInterfaceName); | |
61 | 71 | } |
62 | 72 | |
63 | 73 | int Supplicant::start() { |
@@ -65,7 +75,7 @@ int Supplicant::start() { | ||
65 | 75 | if (setupConfig()) { |
66 | 76 | LOGW("Unable to setup supplicant.conf"); |
67 | 77 | } |
68 | - | |
78 | + | |
69 | 79 | if (mServiceManager->start(SUPPLICANT_SERVICE_NAME)) { |
70 | 80 | LOGE("Error starting supplicant (%s)", strerror(errno)); |
71 | 81 | return -1; |
@@ -76,11 +86,20 @@ int Supplicant::start() { | ||
76 | 86 | LOGE("Error connecting to supplicant (%s)\n", strerror(errno)); |
77 | 87 | return -1; |
78 | 88 | } |
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); | |
79 | 96 | return 0; |
80 | 97 | } |
81 | 98 | |
82 | 99 | int Supplicant::stop() { |
83 | 100 | |
101 | + mPropMngr->unregisterProperty("wifi.supplicant.state"); | |
102 | + | |
84 | 103 | if (mListener->stopListener()) { |
85 | 104 | LOGW("Unable to stop supplicant listener (%s)", strerror(errno)); |
86 | 105 | return -1; |
@@ -106,13 +125,55 @@ bool Supplicant::isStarted() { | ||
106 | 125 | return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME); |
107 | 126 | } |
108 | 127 | |
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); | |
112 | 139 | return -1; |
113 | 140 | } |
114 | 141 | |
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: | |
116 | 177 | if (mCtrl == NULL) { |
117 | 178 | LOGE("Unable to open connection to supplicant on \"%s\": %s", |
118 | 179 | "tiwlan0", strerror(errno)); |
@@ -132,7 +193,7 @@ int Supplicant::connectToSupplicant() { | ||
132 | 193 | } |
133 | 194 | |
134 | 195 | mListener = new SupplicantListener(this, mMonitor); |
135 | - | |
196 | + | |
136 | 197 | if (mListener->startListener()) { |
137 | 198 | LOGE("Error - unable to start supplicant listener"); |
138 | 199 | stop(); |
@@ -151,20 +212,17 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len) | ||
151 | 212 | // LOGD("sendCommand(): -> '%s'", cmd); |
152 | 213 | |
153 | 214 | int rc; |
215 | + memset(reply, 0, *reply_len); | |
154 | 216 | if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) { |
155 | 217 | errno = ETIMEDOUT; |
156 | 218 | return -1; |
157 | 219 | } else if (rc < 0 || !strncmp(reply, "FAIL", 4)) { |
158 | - LOGW("sendCommand(): <- '%s'", reply); | |
220 | + strcpy(reply, "FAIL"); | |
159 | 221 | errno = EIO; |
160 | 222 | return -1; |
161 | 223 | } |
162 | 224 | |
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); | |
168 | 226 | return 0; |
169 | 227 | } |
170 | 228 |
@@ -187,6 +245,22 @@ int Supplicant::triggerScan(bool active) { | ||
187 | 245 | return 0; |
188 | 246 | } |
189 | 247 | |
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 | + | |
190 | 264 | int Supplicant::onConnectedEvent(SupplicantEvent *evt) { |
191 | 265 | LOGD("onConnectedEvent(%s)", evt->getEvent()); |
192 | 266 | return 0; |
@@ -237,12 +311,12 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) { | ||
237 | 311 | char *reply; |
238 | 312 | |
239 | 313 | if (!(reply = (char *) malloc(4096))) { |
240 | - errno = -ENOMEM; | |
314 | + errno = ENOMEM; | |
241 | 315 | return -1; |
242 | 316 | } |
243 | 317 | |
244 | 318 | size_t len = 4096; |
245 | - | |
319 | + | |
246 | 320 | if (sendCommand("SCAN_RESULTS", reply, &len)) { |
247 | 321 | LOGW("onScanResultsEvent(%s): Error getting scan results (%s)", |
248 | 322 | evt->getEvent(), strerror(errno)); |
@@ -272,7 +346,7 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) { | ||
272 | 346 | |
273 | 347 | while((linep = strtok_r(NULL, "\n", &linep_next))) |
274 | 348 | mLatestScanResults->push_back(new ScanResult(linep)); |
275 | - | |
349 | + | |
276 | 350 | char tmp[128]; |
277 | 351 | sprintf(tmp, "Scan results ready (%d)", mLatestScanResults->size()); |
278 | 352 | NetworkManager::Instance()->getBroadcaster()-> |
@@ -323,43 +397,78 @@ ScanResultCollection *Supplicant::createLatestScanResults() { | ||
323 | 397 | ScanResultCollection::iterator i; |
324 | 398 | |
325 | 399 | pthread_mutex_lock(&mLatestScanResultsLock); |
326 | - for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i) { | |
400 | + for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i) | |
327 | 401 | d->push_back((*i)->clone()); |
328 | - } | |
329 | 402 | |
330 | 403 | pthread_mutex_unlock(&mLatestScanResultsLock); |
331 | 404 | return d; |
332 | 405 | } |
333 | 406 | |
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]; | |
341 | 409 | size_t len = sizeof(reply) -1; |
342 | 410 | |
343 | - memset(reply, 0, sizeof(reply)); | |
344 | 411 | 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'; | |
346 | 416 | |
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; | |
348 | 422 | } |
349 | 423 | |
350 | -int Supplicant::removeNetwork(int networkId) { | |
424 | +int Supplicant::removeNetwork(WifiNetwork *wn) { | |
351 | 425 | char req[64]; |
352 | 426 | |
353 | - sprintf(req, "REMOVE_NETWORK %d", networkId); | |
427 | + sprintf(req, "REMOVE_NETWORK %d", wn->getNetworkId()); | |
354 | 428 | char reply[32]; |
355 | 429 | size_t len = sizeof(reply) -1; |
356 | - memset(reply, 0, sizeof(reply)); | |
357 | - | |
430 | + | |
358 | 431 | if (sendCommand(req, reply, &len)) |
359 | 432 | 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); | |
360 | 443 | return 0; |
361 | 444 | } |
362 | 445 | |
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 | + | |
363 | 472 | int Supplicant::setupConfig() { |
364 | 473 | char buf[2048]; |
365 | 474 | int srcfd, destfd; |
@@ -407,3 +516,60 @@ int Supplicant::setupConfig() { | ||
407 | 516 | } |
408 | 517 | return 0; |
409 | 518 | } |
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 | +} |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _SUPPLICANT_H |
17 | 18 | #define _SUPPLICANT_H |
18 | 19 |
@@ -20,25 +21,35 @@ struct wpa_ctrl; | ||
20 | 21 | class SupplicantListener; |
21 | 22 | class SupplicantEvent; |
22 | 23 | class ServiceManager; |
24 | +class PropertyManager; | |
25 | +class Controller; | |
26 | +class WifiController; | |
23 | 27 | |
24 | 28 | #include <pthread.h> |
25 | 29 | |
26 | 30 | #include "ScanResult.h" |
27 | 31 | #include "WifiNetwork.h" |
32 | +#include "IPropertyProvider.h" | |
28 | 33 | |
29 | -class Supplicant { | |
34 | +class Supplicant : public IPropertyProvider { | |
30 | 35 | private: |
31 | 36 | struct wpa_ctrl *mCtrl; |
32 | 37 | struct wpa_ctrl *mMonitor; |
33 | 38 | SupplicantListener *mListener; |
34 | 39 | int mState; |
35 | 40 | ServiceManager *mServiceManager; |
41 | + PropertyManager *mPropMngr; | |
42 | + WifiController *mController; | |
43 | + char *mInterfaceName; | |
36 | 44 | |
37 | 45 | ScanResultCollection *mLatestScanResults; |
38 | 46 | pthread_mutex_t mLatestScanResultsLock; |
39 | - | |
47 | + | |
48 | + WifiNetworkCollection *mNetworks; | |
49 | + pthread_mutex_t mNetworksLock; | |
50 | + | |
40 | 51 | public: |
41 | - Supplicant(); | |
52 | + Supplicant(WifiController *wc, PropertyManager *propmngr); | |
42 | 53 | virtual ~Supplicant(); |
43 | 54 | |
44 | 55 | int start(); |
@@ -48,12 +59,23 @@ public: | ||
48 | 59 | int triggerScan(bool active); |
49 | 60 | ScanResultCollection *createLatestScanResults(); |
50 | 61 | |
51 | - int addNetwork(); | |
52 | - int removeNetwork(int networkId); | |
62 | + WifiNetwork *createNetwork(); | |
63 | + WifiNetwork *lookupNetwork(int networkId); | |
64 | + int removeNetwork(WifiNetwork *net); | |
53 | 65 | 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); | |
54 | 72 | |
55 | 73 | int getState() { return mState; } |
74 | + Controller *getController() { return (Controller *) mController; } | |
75 | + const char *getInterfaceName() { return mInterfaceName; } | |
56 | 76 | |
77 | + int set(const char *name, const char *value); | |
78 | + const char *get(const char *name, char *buffer, size_t max); | |
57 | 79 | |
58 | 80 | // XXX: Extract these into an interface |
59 | 81 | // handlers for SupplicantListener |
@@ -76,6 +98,7 @@ private: | ||
76 | 98 | int connectToSupplicant(); |
77 | 99 | int sendCommand(const char *cmd, char *reply, size_t *reply_len); |
78 | 100 | int setupConfig(); |
101 | + int retrieveInterfaceName(); | |
79 | 102 | }; |
80 | 103 | |
81 | 104 | #endif |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdlib.h> |
17 | 18 | |
18 | 19 | #define LOG_TAG "SupplicantEvent" |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _SUPPLICANT_EVENT_H |
17 | 18 | #define _SUPPLICANT_EVENT_H |
18 | 19 |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <errno.h> |
17 | 18 | #include <sys/types.h> |
18 | 19 | #include <pthread.h> |
@@ -85,7 +86,7 @@ bool SupplicantListener::onDataAvailable(SocketClient *cli) { | ||
85 | 86 | } |
86 | 87 | |
87 | 88 | delete evt; |
88 | - | |
89 | + | |
89 | 90 | if (rc) { |
90 | 91 | LOGW("Handler %d (%s) error: %s", evt->getType(), evt->getEvent(), strerror(errno)); |
91 | 92 | return false; |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _SUPPLICANTLISTENER_H__ |
17 | 18 | #define _SUPPLICANTLISTENER_H__ |
18 | 19 |
@@ -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 | +} |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _SUPPLICANT_STATE_H |
17 | 18 | #define _SUPPLICANT_STATE_H |
18 | 19 |
@@ -28,6 +29,8 @@ public: | ||
28 | 29 | static const int GROUP_HANDSHAKE = 6; |
29 | 30 | static const int COMPLETED = 7; |
30 | 31 | static const int IDLE = 8; |
32 | + | |
33 | + static char *toString(int val, char *buffer, int max); | |
31 | 34 | }; |
32 | 35 | |
33 | 36 | #endif |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdlib.h> |
17 | 18 | #include <fcntl.h> |
18 | 19 | #include <errno.h> |
@@ -22,14 +23,15 @@ | ||
22 | 23 | #define LOG_TAG "TiwlanWifiController" |
23 | 24 | #include <cutils/log.h> |
24 | 25 | |
26 | +#include "PropertyManager.h" | |
25 | 27 | #include "TiwlanWifiController.h" |
26 | 28 | |
27 | 29 | #define DRIVER_PROP_NAME "wlan.driver.status" |
28 | 30 | |
29 | 31 | extern "C" int sched_yield(void); |
30 | 32 | |
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) { | |
33 | 35 | } |
34 | 36 | |
35 | 37 | int TiwlanWifiController::powerUp() { |
@@ -13,14 +13,16 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _TIWLAN_WIFI_CONTROLLER_H |
17 | 18 | #define _TIWLAN_WIFI_CONTROLLER_H |
18 | 19 | |
20 | +#include "PropertyManager.h" | |
19 | 21 | #include "WifiController.h" |
20 | 22 | |
21 | 23 | class TiwlanWifiController : public WifiController { |
22 | 24 | public: |
23 | - TiwlanWifiController(char *modpath, char *modname, char *modargs); | |
25 | + TiwlanWifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs); | |
24 | 26 | virtual ~TiwlanWifiController() {} |
25 | 27 | |
26 | 28 | virtual int powerUp(); |
@@ -16,55 +16,61 @@ | ||
16 | 16 | |
17 | 17 | #include <stdio.h> |
18 | 18 | #include <string.h> |
19 | +#include <stdlib.h> | |
19 | 20 | #include <errno.h> |
20 | 21 | #include <sys/socket.h> |
21 | 22 | #include <netinet/in.h> |
22 | 23 | #include <arpa/inet.h> |
23 | 24 | |
25 | +#include "PropertyManager.h" | |
24 | 26 | #include "VpnController.h" |
25 | 27 | |
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); | |
29 | 33 | } |
30 | 34 | |
31 | 35 | int VpnController::start() { |
32 | - errno = ENOSYS; | |
33 | - return -1; | |
36 | + return 0; | |
34 | 37 | } |
35 | 38 | |
36 | 39 | int VpnController::stop() { |
37 | - errno = ENOSYS; | |
38 | - return -1; | |
40 | + return 0; | |
39 | 41 | } |
40 | 42 | |
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; | |
45 | 47 | |
46 | -int VpnController::disable() { | |
47 | - errno = ENOSYS; | |
48 | - return -1; | |
49 | -} | |
48 | + if (en == mEnabled) | |
49 | + return 0; | |
50 | + rc = (en ? enable() : disable()); | |
50 | 51 | |
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")) { | |
53 | 56 | if (!inet_aton(value, &mVpnGateway)) { |
54 | 57 | errno = EINVAL; |
55 | 58 | return -1; |
56 | 59 | } |
57 | 60 | return 0; |
58 | - } | |
61 | + } | |
59 | 62 | |
60 | - return Controller::setProperty(name, value); | |
63 | + return Controller::set(name, value); | |
61 | 64 | } |
62 | 65 | |
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")) { | |
65 | 71 | snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway)); |
66 | 72 | return buffer; |
67 | 73 | } |
68 | 74 | |
69 | - return Controller::getProperty(name, buffer, maxsize); | |
75 | + return Controller::get(name, buffer, maxsize); | |
70 | 76 | } |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _VPN_CONTROLLER_H |
17 | 18 | #define _VPN_CONTROLLER_H |
18 | 19 |
@@ -21,25 +22,25 @@ | ||
21 | 22 | #include "Controller.h" |
22 | 23 | |
23 | 24 | class VpnController : public Controller { |
25 | + bool mEnabled; | |
24 | 26 | /* |
25 | 27 | * Gateway of the VPN server to connect to |
26 | 28 | */ |
27 | 29 | struct in_addr mVpnGateway; |
28 | 30 | |
29 | 31 | public: |
30 | - VpnController(); | |
32 | + VpnController(PropertyManager *propmngr); | |
31 | 33 | virtual ~VpnController() {} |
32 | 34 | |
33 | 35 | virtual int start(); |
34 | 36 | virtual int stop(); |
35 | 37 | |
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); | |
39 | 40 | |
40 | -private: | |
41 | - virtual int enable(); | |
42 | - virtual int disable(); | |
41 | +protected: | |
42 | + virtual int enable() = 0; | |
43 | + virtual int disable() = 0; | |
43 | 44 | |
44 | 45 | }; |
45 | 46 |
@@ -26,18 +26,21 @@ | ||
26 | 26 | #include "WifiScanner.h" |
27 | 27 | #include "NetworkManager.h" |
28 | 28 | #include "ErrorCode.h" |
29 | +#include "WifiNetwork.h" | |
29 | 30 | |
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) { | |
32 | 33 | strncpy(mModulePath, modpath, sizeof(mModulePath)); |
33 | 34 | strncpy(mModuleName, modname, sizeof(mModuleName)); |
34 | 35 | strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); |
35 | 36 | |
36 | - mSupplicant = new Supplicant(); | |
37 | + mSupplicant = new Supplicant(this, propmngr); | |
37 | 38 | mScanner = new WifiScanner(mSupplicant, 10); |
38 | 39 | mCurrentScanMode = 0; |
39 | 40 | |
40 | - registerProperty("scanmode"); | |
41 | + mEnabled = false; | |
42 | + | |
43 | + propmngr->registerProperty("wifi.enabled", this); | |
41 | 44 | } |
42 | 45 | |
43 | 46 | int WifiController::start() { |
@@ -82,6 +85,17 @@ int WifiController::enable() { | ||
82 | 85 | } |
83 | 86 | } |
84 | 87 | |
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 | + | |
85 | 99 | return 0; |
86 | 100 | |
87 | 101 | out_unloadmodule: |
@@ -106,13 +120,14 @@ void WifiController::sendStatusBroadcast(const char *msg) { | ||
106 | 120 | |
107 | 121 | int WifiController::disable() { |
108 | 122 | |
123 | + mPropMngr->unregisterProperty("wifi.scanmode"); | |
109 | 124 | if (mSupplicant->isStarted()) { |
110 | 125 | sendStatusBroadcast("STOPPING_SUPPLICANT"); |
111 | 126 | if (mSupplicant->stop()) { |
112 | 127 | LOGE("Supplicant stop failed (%s)", strerror(errno)); |
113 | 128 | return -1; |
114 | 129 | } |
115 | - } else | |
130 | + } else | |
116 | 131 | LOGW("disable(): Supplicant not running?"); |
117 | 132 | |
118 | 133 | if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { |
@@ -155,36 +170,60 @@ int WifiController::setScanMode(uint32_t mode) { | ||
155 | 170 | return rc; |
156 | 171 | } |
157 | 172 | |
158 | -int WifiController::addNetwork() { | |
159 | - return mSupplicant->addNetwork(); | |
173 | +WifiNetwork *WifiController::createNetwork() { | |
174 | + WifiNetwork *wn = mSupplicant->createNetwork(); | |
175 | + return wn; | |
160 | 176 | } |
161 | 177 | |
162 | 178 | 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); | |
164 | 184 | } |
165 | 185 | |
166 | 186 | ScanResultCollection *WifiController::createScanResults() { |
167 | 187 | return mSupplicant->createLatestScanResults(); |
168 | 188 | } |
169 | 189 | |
170 | -// XXX: This should be a const list | |
171 | 190 | WifiNetworkCollection *WifiController::createNetworkList() { |
172 | 191 | return mSupplicant->createNetworkList(); |
173 | 192 | } |
174 | 193 | |
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")) | |
177 | 209 | 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; | |
180 | 213 | } |
181 | 214 | |
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")) | |
184 | 223 | snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode); |
185 | - return buffer; | |
186 | - } | |
224 | + else | |
225 | + return Controller::get(name, buffer, maxsize); | |
187 | 226 | |
188 | - return Controller::getProperty(name, buffer, maxsize); | |
227 | + return buffer; | |
189 | 228 | } |
190 | 229 |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #ifndef _WIFI_CONTROLLER_H |
17 | 18 | #define _WIFI_CONTROLLER_H |
18 | 19 |
@@ -46,21 +47,21 @@ private: | ||
46 | 47 | char mModuleArgs[255]; |
47 | 48 | uint32_t mCurrentScanMode; |
48 | 49 | WifiScanner *mScanner; |
50 | + bool mEnabled; | |
49 | 51 | |
50 | 52 | public: |
51 | - WifiController(char *modpath, char *modname, char *modargs); | |
53 | + WifiController(PropertyManager *propmngr, char *modpath, char *modname, char *modargs); | |
52 | 54 | virtual ~WifiController() {} |
53 | 55 | |
54 | 56 | int start(); |
55 | 57 | int stop(); |
56 | 58 | |
57 | - int addNetwork(); | |
59 | + WifiNetwork *createNetwork(); | |
58 | 60 | int removeNetwork(int networkId); |
59 | 61 | WifiNetworkCollection *createNetworkList(); |
60 | 62 | |
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); | |
64 | 65 | |
65 | 66 | ScanResultCollection *createScanResults(); |
66 | 67 |
@@ -71,6 +72,7 @@ public: | ||
71 | 72 | Supplicant *getSupplicant() { return mSupplicant; } |
72 | 73 | |
73 | 74 | protected: |
75 | + // Move this crap into a 'driver' | |
74 | 76 | virtual int powerUp() = 0; |
75 | 77 | virtual int powerDown() = 0; |
76 | 78 | virtual int loadFirmware(); |
@@ -78,13 +80,11 @@ protected: | ||
78 | 80 | virtual bool isFirmwareLoaded() = 0; |
79 | 81 | virtual bool isPoweredUp() = 0; |
80 | 82 | |
81 | - void sendStatusBroadcast(const char *msg); | |
82 | - | |
83 | 83 | private: |
84 | + void sendStatusBroadcast(const char *msg); | |
84 | 85 | int setScanMode(uint32_t mode); |
85 | 86 | int enable(); |
86 | 87 | int disable(); |
87 | - | |
88 | 88 | }; |
89 | 89 | |
90 | 90 | #endif |
@@ -16,14 +16,93 @@ | ||
16 | 16 | |
17 | 17 | #include <errno.h> |
18 | 18 | #include <string.h> |
19 | +#include <stdlib.h> | |
19 | 20 | #include <sys/types.h> |
20 | 21 | |
22 | +#define LOG_TAG "WifiNetwork" | |
23 | +#include <cutils/log.h> | |
24 | + | |
25 | +#include "NetworkManager.h" | |
21 | 26 | #include "WifiNetwork.h" |
22 | 27 | #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 | + } | |
23 | 92 | |
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; | |
25 | 104 | mSuppl = suppl; |
26 | - mNetid = -1; | |
105 | + mNetid = networkId; | |
27 | 106 | mSsid = NULL; |
28 | 107 | mBssid = NULL; |
29 | 108 | mPsk = NULL; |
@@ -36,9 +115,45 @@ WifiNetwork::WifiNetwork(Supplicant *suppl) { | ||
36 | 115 | mAllowedAuthAlgorithms = 0; |
37 | 116 | mAllowedPairwiseCiphers = 0; |
38 | 117 | 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; | |
39 | 153 | } |
40 | 154 | |
41 | 155 | WifiNetwork::~WifiNetwork() { |
156 | + unregisterProperties(); | |
42 | 157 | if (mSsid) |
43 | 158 | free(mSsid); |
44 | 159 | if (mBssid) |
@@ -49,61 +164,423 @@ WifiNetwork::~WifiNetwork() { | ||
49 | 164 | if (mWepKeys[i]) |
50 | 165 | free(mWepKeys[i]); |
51 | 166 | } |
167 | + | |
52 | 168 | if (mHiddenSsid) |
53 | 169 | free(mHiddenSsid); |
170 | + if (mIfaceCfg) | |
171 | + delete(mIfaceCfg); | |
54 | 172 | } |
55 | 173 | |
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)); | |
58 | 232 | return -1; |
59 | 233 | } |
60 | 234 | |
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); | |
63 | 320 | return -1; |
64 | 321 | } |
65 | 322 | |
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; | |
69 | 382 | } |
70 | 383 | |
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; | |
74 | 426 | } |
75 | 427 | |
76 | 428 | 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; | |
79 | 436 | } |
80 | 437 | |
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; | |
84 | 446 | } |
85 | 447 | |
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; | |
89 | 456 | } |
90 | 457 | |
91 | 458 | 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; | |
94 | 476 | } |
95 | 477 | |
96 | 478 | 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; | |
99 | 514 | } |
100 | 515 | |
101 | 516 | 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; | |
104 | 533 | } |
105 | 534 | |
106 | 535 | 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; | |
109 | 586 | } |
@@ -19,7 +19,7 @@ | ||
19 | 19 | |
20 | 20 | #include <sys/types.h> |
21 | 21 | |
22 | -#include "../../../frameworks/base/include/utils/List.h" | |
22 | +#include <utils/List.h> | |
23 | 23 | |
24 | 24 | class KeyManagementMask { |
25 | 25 | public: |
@@ -43,14 +43,14 @@ public: | ||
43 | 43 | static const uint32_t LEAP = 0x04; |
44 | 44 | }; |
45 | 45 | |
46 | -class PairwiseCipherMask { | |
46 | +class PairwiseCiphersMask { | |
47 | 47 | public: |
48 | 48 | static const uint32_t NONE = 0x00; |
49 | 49 | static const uint32_t TKIP = 0x01; |
50 | 50 | static const uint32_t CCMP = 0x02; |
51 | 51 | }; |
52 | 52 | |
53 | -class GroupCipherMask { | |
53 | +class GroupCiphersMask { | |
54 | 54 | public: |
55 | 55 | static const uint32_t WEP40 = 0x01; |
56 | 56 | static const uint32_t WEP104 = 0x02; |
@@ -59,9 +59,20 @@ public: | ||
59 | 59 | }; |
60 | 60 | |
61 | 61 | class Supplicant; |
62 | +class InterfaceConfig; | |
63 | +class Controller; | |
64 | +class WifiController; | |
62 | 65 | |
63 | -class WifiNetwork { | |
66 | +#include "IPropertyProvider.h" | |
67 | + | |
68 | +class WifiNetwork : public IPropertyProvider{ | |
69 | +public: | |
70 | + static const char *PropertyNames[]; | |
71 | + | |
72 | +private: | |
64 | 73 | Supplicant *mSuppl; |
74 | + InterfaceConfig *mIfaceCfg; | |
75 | + WifiController *mController; | |
65 | 76 | |
66 | 77 | /* |
67 | 78 | * Unique network id - normally provided by supplicant |
@@ -138,10 +149,24 @@ class WifiNetwork { | ||
138 | 149 | */ |
139 | 150 | uint32_t mAllowedGroupCiphers; |
140 | 151 | |
152 | + /* | |
153 | + * Set if this Network is enabled | |
154 | + */ | |
155 | + bool mEnabled; | |
156 | + | |
157 | +private: | |
158 | + WifiNetwork(); | |
159 | + int registerProperties(); | |
160 | + int unregisterProperties(); | |
161 | + | |
141 | 162 | public: |
142 | - WifiNetwork(Supplicant *suppl); | |
163 | + WifiNetwork(WifiController *c, Supplicant *suppl, int networkId); | |
164 | + WifiNetwork(WifiController *c, Supplicant *suppl, const char *data); | |
165 | + | |
143 | 166 | virtual ~WifiNetwork(); |
144 | 167 | |
168 | + WifiNetwork *clone(); | |
169 | + | |
145 | 170 | int getNetworkId() { return mNetid; } |
146 | 171 | const char *getSsid() { return mSsid; } |
147 | 172 | const char *getBssid() { return mBssid; } |
@@ -155,18 +180,30 @@ public: | ||
155 | 180 | uint32_t getAllowedAuthAlgorithms() { return mAllowedAuthAlgorithms; } |
156 | 181 | uint32_t getAllowedPairwiseCiphers() { return mAllowedPairwiseCiphers; } |
157 | 182 | uint32_t getAllowedGroupCiphers() { return mAllowedGroupCiphers; } |
183 | + bool getEnabled() { return mEnabled; } | |
184 | + Controller *getController() { return (Controller *) mController; } | |
158 | 185 | |
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); | |
163 | 196 | int setDefaultKeyIndex(int idx); |
164 | 197 | int setPriority(int pri); |
165 | - int setHiddenSsid(char *ssid); | |
198 | + int setHiddenSsid(const char *ssid); | |
166 | 199 | int setAllowedKeyManagement(uint32_t mask); |
167 | 200 | int setAllowedProtocols(uint32_t mask); |
201 | + int setAllowedAuthAlgorithms(uint32_t mask); | |
168 | 202 | int setAllowedPairwiseCiphers(uint32_t mask); |
169 | 203 | int setAllowedGroupCiphers(uint32_t mask); |
204 | + | |
205 | + // XXX:Should this really be exposed?.. meh | |
206 | + int refresh(); | |
170 | 207 | }; |
171 | 208 | |
172 | 209 | typedef android::List<WifiNetwork *> WifiNetworkCollection; |
@@ -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 | + | |
1 | 17 | #include <stdlib.h> |
2 | 18 | #include <sys/socket.h> |
3 | 19 | #include <sys/select.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 | + | |
1 | 17 | #ifndef _WIFISCANNER_H |
2 | 18 | #define _WIFISCANNER_H |
3 | 19 |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdlib.h> |
17 | 18 | #include <errno.h> |
18 | 19 |
@@ -39,10 +40,10 @@ int main() { | ||
39 | 40 | |
40 | 41 | nm->setBroadcaster((SocketListener *) cl); |
41 | 42 | |
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", "")); | |
44 | 45 | // nm->attachController(new AndroidL2TPVpnController()); |
45 | - nm->attachController(new OpenVpnController()); | |
46 | + nm->attachController(new OpenVpnController(nm->getPropMngr())); | |
46 | 47 | |
47 | 48 | |
48 | 49 | if (NetworkManager::Instance()->run()) { |
@@ -56,7 +57,7 @@ int main() { | ||
56 | 57 | } |
57 | 58 | |
58 | 59 | // XXX: we'll use the main thread for the NetworkManager eventually |
59 | - | |
60 | + | |
60 | 61 | while(1) { |
61 | 62 | sleep(1000); |
62 | 63 | } |
@@ -13,6 +13,7 @@ | ||
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | + | |
16 | 17 | #include <stdio.h> |
17 | 18 | #include <stdlib.h> |
18 | 19 | #include <unistd.h> |