San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame^] | 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 | #include <errno.h> |
| 17 | |
| 18 | #define LOG_TAG "Supplicant" |
| 19 | #include <cutils/log.h> |
| 20 | #include <cutils/properties.h> |
| 21 | |
| 22 | #undef HAVE_LIBC_SYSTEM_PROPERTIES |
| 23 | |
| 24 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
| 25 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 26 | #include <sys/_system_properties.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "Supplicant.h" |
| 30 | #include "SupplicantListener.h" |
| 31 | #include "SupplicantState.h" |
| 32 | #include "SupplicantEvent.h" |
| 33 | #include "ScanResult.h" |
| 34 | |
| 35 | #include "libwpa_client/wpa_ctrl.h" |
| 36 | |
| 37 | #define IFACE_DIR "/data/system/wpa_supplicant" |
| 38 | #define DRIVER_PROP_NAME "wlan.driver.status" |
| 39 | #define SUPPLICANT_NAME "wpa_supplicant" |
| 40 | #define SUPP_PROP_NAME "init.svc.wpa_supplicant" |
| 41 | |
| 42 | Supplicant::Supplicant() { |
| 43 | mCtrl = NULL; |
| 44 | mMonitor = NULL; |
| 45 | mListener = NULL; |
| 46 | |
| 47 | mState = SupplicantState::UNKNOWN; |
| 48 | |
| 49 | mLatestScanResults = new ScanResultCollection(); |
| 50 | |
| 51 | pthread_mutex_init(&mLatestScanResultsLock, NULL); |
| 52 | } |
| 53 | |
| 54 | int Supplicant::start() { |
| 55 | LOGD("start():"); |
| 56 | // XXX: Validate supplicant config file |
| 57 | |
| 58 | char status[PROPERTY_VALUE_MAX] = {'\0'}; |
| 59 | int count = 200; |
| 60 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
| 61 | const prop_info *pi; |
| 62 | unsigned int serial = 0; |
| 63 | #endif |
| 64 | |
| 65 | if (property_get(SUPP_PROP_NAME, status, NULL) && |
| 66 | strcmp(status, "running") == 0) { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | wpa_ctrl_cleanup(); |
| 71 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
| 72 | pi = __system_property_find(SUPP_PROP_NAME); |
| 73 | if (pi != NULL) |
| 74 | serial = pi->serial; |
| 75 | #endif |
| 76 | |
| 77 | property_set("ctl.start", SUPPLICANT_NAME); |
| 78 | sched_yield(); |
| 79 | while (count--) { |
| 80 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
| 81 | if (!pi) |
| 82 | pi = __system_property_find(SUPP_PROP_NAME); |
| 83 | if (pi) { |
| 84 | __system_property_read(pi, NULL, status); |
| 85 | if (strcmp(status, "running") == 0) |
| 86 | return 0; |
| 87 | else if (pi->serial != serial && |
| 88 | strcmp(status, "stopped") == 0) { |
| 89 | errno = EIO; |
| 90 | return -1; |
| 91 | } |
| 92 | } |
| 93 | #else |
| 94 | if (property_get(SUPP_PROP_NAME, status, NULL)) { |
| 95 | if (strcmp(status, "running") == 0) |
| 96 | break; |
| 97 | } |
| 98 | #endif |
| 99 | usleep(100000); |
| 100 | } |
| 101 | |
| 102 | if (!count) { |
| 103 | errno = ETIMEDOUT; |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | if (connectToSupplicant()) { |
| 108 | LOGE("Error connecting to supplicant (%s)\n", strerror(errno)); |
| 109 | return -1; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int Supplicant::stop() { |
| 115 | LOGD("stop()"); |
| 116 | |
| 117 | char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; |
| 118 | int count = 50; |
| 119 | |
| 120 | if (mListener->stopListener()) { |
| 121 | LOGW("Unable to stop supplicant listener (%s)", strerror(errno)); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (property_get(SUPP_PROP_NAME, supp_status, NULL) |
| 126 | && strcmp(supp_status, "stopped") == 0) { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | property_set("ctl.stop", SUPPLICANT_NAME); |
| 131 | sched_yield(); |
| 132 | |
| 133 | while (count-- > 0) { |
| 134 | if (property_get(SUPP_PROP_NAME, supp_status, NULL)) { |
| 135 | if (strcmp(supp_status, "stopped") == 0) |
| 136 | break; |
| 137 | } |
| 138 | usleep(100000); |
| 139 | } |
| 140 | |
| 141 | if (mCtrl) { |
| 142 | wpa_ctrl_close(mCtrl); |
| 143 | mCtrl = NULL; |
| 144 | } |
| 145 | if (mMonitor) { |
| 146 | wpa_ctrl_close(mMonitor); |
| 147 | mMonitor = NULL; |
| 148 | } |
| 149 | |
| 150 | if (!count) { |
| 151 | LOGD("Timed out waiting for supplicant to stop"); |
| 152 | errno = ETIMEDOUT; |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | LOGD("Stopped OK"); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | bool Supplicant::isStarted() { |
| 162 | char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; |
| 163 | if (!property_get(SUPP_PROP_NAME, supp_status, NULL) || |
| 164 | !strcmp(supp_status, "running")) { |
| 165 | return false; |
| 166 | } |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | int Supplicant::connectToSupplicant() { |
| 171 | char ifname[256]; |
| 172 | char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; |
| 173 | |
| 174 | if (!property_get(SUPP_PROP_NAME, supp_status, NULL) |
| 175 | || strcmp(supp_status, "running") != 0) { |
| 176 | LOGE("Supplicant not running, cannot connect"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | mCtrl = wpa_ctrl_open("tiwlan0"); |
| 181 | if (mCtrl == NULL) { |
| 182 | LOGE("Unable to open connection to supplicant on \"%s\": %s", |
| 183 | "tiwlan0", strerror(errno)); |
| 184 | return -1; |
| 185 | } |
| 186 | mMonitor = wpa_ctrl_open("tiwlan0"); |
| 187 | if (mMonitor == NULL) { |
| 188 | wpa_ctrl_close(mCtrl); |
| 189 | mCtrl = NULL; |
| 190 | return -1; |
| 191 | } |
| 192 | if (wpa_ctrl_attach(mMonitor) != 0) { |
| 193 | wpa_ctrl_close(mMonitor); |
| 194 | wpa_ctrl_close(mCtrl); |
| 195 | mCtrl = mMonitor = NULL; |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | mListener = new SupplicantListener(this, mMonitor); |
| 200 | |
| 201 | if (mListener->startListener()) { |
| 202 | LOGE("Error - unable to start supplicant listener"); |
| 203 | stop(); |
| 204 | return -1; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len) |
| 210 | { |
| 211 | if (!mCtrl) { |
| 212 | errno = ENOTCONN; |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | LOGD("sendCommand(): -> '%s'", cmd); |
| 217 | |
| 218 | int rc; |
| 219 | if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) { |
| 220 | errno = ETIMEDOUT; |
| 221 | return -1; |
| 222 | } else if (rc < 0 || !strncmp(reply, "FAIL", 4)) { |
| 223 | errno = EIO; |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | if (!strncmp(cmd, "PING", 4) || |
| 228 | !strncmp(cmd, "SCAN_RESULTS", 12)) |
| 229 | reply[*reply_len] = '\0'; |
| 230 | |
| 231 | LOGD("sendCommand(): <- '%s'", reply); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | int Supplicant::triggerScan(bool active) { |
| 236 | char reply[255]; |
| 237 | size_t len = sizeof(reply); |
| 238 | |
| 239 | if (sendCommand((active ? "DRIVER SCAN-ACTIVE" : "DRIVER SCAN-PASSIVE"), |
| 240 | reply, &len)) { |
| 241 | LOGW("triggerScan(%d): Error setting scan mode (%s)", active, |
| 242 | strerror(errno)); |
| 243 | return -1; |
| 244 | } |
| 245 | len = sizeof(reply); |
| 246 | |
| 247 | if (sendCommand("SCAN", reply, &len)) { |
| 248 | LOGW("triggerScan(%d): Error initiating scan", active); |
| 249 | return -1; |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int Supplicant::onConnectedEvent(SupplicantEvent *evt) { |
| 255 | LOGD("onConnectedEvent(%s)", evt->getEvent()); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int Supplicant::onDisconnectedEvent(SupplicantEvent *evt) { |
| 260 | LOGD("onDisconnectedEvent(%s)", evt->getEvent()); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | int Supplicant::onTerminatingEvent(SupplicantEvent *evt) { |
| 265 | LOGD("onTerminatingEvent(%s)", evt->getEvent()); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int Supplicant::onPasswordChangedEvent(SupplicantEvent *evt) { |
| 270 | LOGD("onPasswordChangedEvent(%s)", evt->getEvent()); |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int Supplicant::onEapNotificationEvent(SupplicantEvent *evt) { |
| 275 | LOGD("onEapNotificationEvent(%s)", evt->getEvent()); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int Supplicant::onEapStartedEvent(SupplicantEvent *evt) { |
| 280 | LOGD("onEapStartedEvent(%s)", evt->getEvent()); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | int Supplicant::onEapMethodEvent(SupplicantEvent *evt) { |
| 285 | LOGD("onEapMethodEvent(%s)", evt->getEvent()); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int Supplicant::onEapSuccessEvent(SupplicantEvent *evt) { |
| 290 | LOGD("onEapSuccessEvent(%s)", evt->getEvent()); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int Supplicant::onEapFailureEvent(SupplicantEvent *evt) { |
| 295 | LOGD("onEapFailureEvent(%s)", evt->getEvent()); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | int Supplicant::onScanResultsEvent(SupplicantEvent *evt) { |
| 300 | LOGD("onScanResultsEvent(%s)", evt->getEvent()); |
| 301 | |
| 302 | if (!strcmp(evt->getEvent(), "Ready")) { |
| 303 | char *reply; |
| 304 | |
| 305 | if (!(reply = (char *) malloc(4096))) { |
| 306 | errno = -ENOMEM; |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | size_t len = 4096; |
| 311 | |
| 312 | if (sendCommand("SCAN_RESULTS", reply, &len)) { |
| 313 | LOGW("onScanResultsEvent(%s): Error getting scan results (%s)", |
| 314 | evt->getEvent(), strerror(errno)); |
| 315 | free(reply); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | pthread_mutex_lock(&mLatestScanResultsLock); |
| 320 | if (!mLatestScanResults->empty()) { |
| 321 | ScanResultCollection::iterator i; |
| 322 | |
| 323 | for (i = mLatestScanResults->begin(); |
| 324 | i !=mLatestScanResults->end(); ++i) { |
| 325 | delete *i; |
| 326 | } |
| 327 | mLatestScanResults->clear(); |
| 328 | } |
| 329 | |
| 330 | char *linep; |
| 331 | char *linep_next = NULL; |
| 332 | |
| 333 | if (!strtok_r(reply, "\n", &linep_next)) { |
| 334 | free(reply); |
| 335 | return 0;; |
| 336 | } |
| 337 | |
| 338 | while((linep = strtok_r(NULL, "\n", &linep_next))) |
| 339 | mLatestScanResults->push_back(new ScanResult(linep)); |
| 340 | |
| 341 | pthread_mutex_unlock(&mLatestScanResultsLock); |
| 342 | free(reply); |
| 343 | } else { |
| 344 | LOGW("Unknown SCAN_RESULTS event (%s)", evt->getEvent()); |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | int Supplicant::onStateChangeEvent(SupplicantEvent *evt) { |
| 350 | LOGD("onStateChangeEvent(%s)", evt->getEvent()); |
| 351 | // XXX: Update mState |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | int Supplicant::onLinkSpeedEvent(SupplicantEvent *evt) { |
| 356 | LOGD("onLinkSpeedEvent(%s)", evt->getEvent()); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | int Supplicant::onDriverStateEvent(SupplicantEvent *evt) { |
| 361 | LOGD("onDriverStateEvent(%s)", evt->getEvent()); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | // XXX: Use a cursor + smartptr instead |
| 366 | const ScanResultCollection *Supplicant::getLatestScanResults() { |
| 367 | ScanResultCollection *d = new ScanResultCollection(); |
| 368 | ScanResultCollection::iterator i; |
| 369 | |
| 370 | pthread_mutex_lock(&mLatestScanResultsLock); |
| 371 | for (i = mLatestScanResults->begin(); i != mLatestScanResults->end(); ++i) { |
| 372 | d->push_back((*i)->clone()); |
| 373 | } |
| 374 | |
| 375 | pthread_mutex_unlock(&mLatestScanResultsLock); |
| 376 | return d; |
| 377 | }; |