2009-05-07 02:16:52 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2009-05-08 02:37:10 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2009-05-11 00:27:07 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
2009-05-07 02:16:52 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "Supplicant"
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2009-05-11 00:27:07 +08:00
|
|
|
#include "private/android_filesystem_config.h"
|
|
|
|
|
2009-05-15 06:00:06 +08:00
|
|
|
#include <sysutils/ServiceManager.h>
|
2009-05-07 02:16:52 +08:00
|
|
|
|
|
|
|
#include "Supplicant.h"
|
|
|
|
#include "SupplicantListener.h"
|
2009-05-08 02:37:10 +08:00
|
|
|
#include "NetworkManager.h"
|
2009-05-23 06:36:13 +08:00
|
|
|
#include "WifiController.h"
|
2009-06-16 05:10:44 +08:00
|
|
|
#include "SupplicantStatus.h"
|
2009-05-07 02:16:52 +08:00
|
|
|
|
|
|
|
#include "libwpa_client/wpa_ctrl.h"
|
|
|
|
|
|
|
|
#define IFACE_DIR "/data/system/wpa_supplicant"
|
|
|
|
#define DRIVER_PROP_NAME "wlan.driver.status"
|
2009-05-15 06:00:06 +08:00
|
|
|
#define SUPPLICANT_SERVICE_NAME "wpa_supplicant"
|
2009-05-11 00:27:07 +08:00
|
|
|
#define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf"
|
|
|
|
#define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf"
|
|
|
|
|
2009-06-16 05:10:44 +08:00
|
|
|
Supplicant::Supplicant(WifiController *wc, ISupplicantEventHandler *handlers) {
|
|
|
|
mHandlers = handlers;
|
2009-05-23 06:36:13 +08:00
|
|
|
mController = wc;
|
|
|
|
mInterfaceName = NULL;
|
2009-05-07 02:16:52 +08:00
|
|
|
mCtrl = NULL;
|
|
|
|
mMonitor = NULL;
|
|
|
|
mListener = NULL;
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-05-15 06:00:06 +08:00
|
|
|
mServiceManager = new ServiceManager();
|
2009-05-07 02:16:52 +08:00
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
mNetworks = new WifiNetworkCollection();
|
|
|
|
pthread_mutex_init(&mNetworksLock, NULL);
|
2009-05-07 02:16:52 +08:00
|
|
|
}
|
|
|
|
|
2009-05-15 06:00:06 +08:00
|
|
|
Supplicant::~Supplicant() {
|
|
|
|
delete mServiceManager;
|
2009-05-23 06:36:13 +08:00
|
|
|
if (mInterfaceName)
|
|
|
|
free(mInterfaceName);
|
2009-05-15 06:00:06 +08:00
|
|
|
}
|
|
|
|
|
2009-05-07 02:16:52 +08:00
|
|
|
int Supplicant::start() {
|
2009-05-11 00:27:07 +08:00
|
|
|
|
|
|
|
if (setupConfig()) {
|
|
|
|
LOGW("Unable to setup supplicant.conf");
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-05-15 06:00:06 +08:00
|
|
|
if (mServiceManager->start(SUPPLICANT_SERVICE_NAME)) {
|
|
|
|
LOGE("Error starting supplicant (%s)", strerror(errno));
|
|
|
|
return -1;
|
2009-05-07 02:16:52 +08:00
|
|
|
}
|
|
|
|
|
2009-05-08 02:37:10 +08:00
|
|
|
wpa_ctrl_cleanup();
|
2009-05-07 02:16:52 +08:00
|
|
|
if (connectToSupplicant()) {
|
|
|
|
LOGE("Error connecting to supplicant (%s)\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
|
|
|
|
if (retrieveInterfaceName()) {
|
|
|
|
LOGE("Error retrieving interface name (%s)\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-07 02:16:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::stop() {
|
|
|
|
|
|
|
|
if (mListener->stopListener()) {
|
|
|
|
LOGW("Unable to stop supplicant listener (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-15 06:00:06 +08:00
|
|
|
if (mServiceManager->stop(SUPPLICANT_SERVICE_NAME)) {
|
|
|
|
LOGW("Error stopping supplicant (%s)", strerror(errno));
|
2009-05-07 02:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCtrl) {
|
|
|
|
wpa_ctrl_close(mCtrl);
|
|
|
|
mCtrl = NULL;
|
|
|
|
}
|
|
|
|
if (mMonitor) {
|
|
|
|
wpa_ctrl_close(mMonitor);
|
|
|
|
mMonitor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Supplicant::isStarted() {
|
2009-05-15 06:00:06 +08:00
|
|
|
return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME);
|
2009-05-07 02:16:52 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len) {
|
|
|
|
|
|
|
|
if (!mCtrl) {
|
|
|
|
errno = ENOTCONN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LOGD("sendCommand(): -> '%s'", cmd);
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
memset(reply, 0, *reply_len);
|
|
|
|
if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
} else if (rc < 0 || !strncmp(reply, "FAIL", 4)) {
|
|
|
|
strcpy(reply, "FAIL");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LOGD("sendCommand(): <- '%s'", reply);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-16 05:10:44 +08:00
|
|
|
SupplicantStatus *Supplicant::getStatus() {
|
|
|
|
char *reply;
|
|
|
|
size_t len = 4096;
|
|
|
|
|
|
|
|
if (!(reply = (char *) malloc(len))) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendCommand("STATUS", reply, &len)) {
|
|
|
|
free(reply);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SupplicantStatus *ss = SupplicantStatus::createStatus(reply, len);
|
|
|
|
|
|
|
|
free (reply);
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves the list of networks from Supplicant
|
|
|
|
* and merge them into our current list
|
|
|
|
*/
|
2009-05-23 06:36:13 +08:00
|
|
|
int Supplicant::refreshNetworkList() {
|
|
|
|
char *reply;
|
|
|
|
size_t len = 4096;
|
|
|
|
|
|
|
|
if (!(reply = (char *) malloc(len))) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendCommand("LIST_NETWORKS", reply, &len)) {
|
|
|
|
free(reply);
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
char *linep;
|
|
|
|
char *linep_next = NULL;
|
|
|
|
|
|
|
|
if (!strtok_r(reply, "\n", &linep_next)) {
|
|
|
|
LOGW("Malformatted network list\n");
|
2009-06-16 05:10:44 +08:00
|
|
|
free(reply);
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
PropertyManager *pm = NetworkManager::Instance()->getPropMngr();
|
2009-06-16 05:10:44 +08:00
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
|
|
|
|
|
|
|
int num_added = 0;
|
|
|
|
int num_refreshed = 0;
|
|
|
|
int num_removed = 0;
|
|
|
|
while((linep = strtok_r(NULL, "\n", &linep_next))) {
|
|
|
|
// TODO: Move the decode into a static method so we
|
|
|
|
// don't create new_wn when we don't have to.
|
|
|
|
WifiNetwork *new_wn = new WifiNetwork(mController, this, linep);
|
|
|
|
WifiNetwork *merge_wn;
|
|
|
|
|
|
|
|
if ((merge_wn = this->lookupNetwork_UNLOCKED(new_wn->getNetworkId()))) {
|
|
|
|
num_refreshed++;
|
|
|
|
if (merge_wn->refresh()) {
|
|
|
|
LOGW("Error refreshing network %d (%s)",
|
|
|
|
merge_wn->getNetworkId(), strerror(errno));
|
|
|
|
}
|
|
|
|
delete new_wn;
|
|
|
|
} else {
|
|
|
|
num_added++;
|
2009-06-24 12:10:57 +08:00
|
|
|
char new_ns[20];
|
|
|
|
snprintf(new_ns, sizeof(new_ns), "wifi.net.%d", new_wn->getNetworkId());
|
|
|
|
new_wn->attachProperties(pm, new_ns);
|
2009-06-16 05:10:44 +08:00
|
|
|
mNetworks->push_back(new_wn);
|
|
|
|
if (new_wn->refresh()) {
|
|
|
|
LOGW("Unable to refresh network id %d (%s)",
|
|
|
|
new_wn->getNetworkId(), strerror(errno));
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
}
|
2009-06-16 05:10:44 +08:00
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-06-16 05:10:44 +08:00
|
|
|
if (!mNetworks->empty()) {
|
|
|
|
// TODO: Add support for detecting removed networks
|
|
|
|
WifiNetworkCollection::iterator i;
|
|
|
|
|
|
|
|
for (i = mNetworks->begin(); i != mNetworks->end(); ++i) {
|
|
|
|
if (0) {
|
|
|
|
num_removed++;
|
2009-06-24 12:10:57 +08:00
|
|
|
char del_ns[20];
|
|
|
|
snprintf(del_ns, sizeof(del_ns), "wifi.net.%d", (*i)->getNetworkId());
|
|
|
|
(*i)->detachProperties(pm, del_ns);
|
2009-06-16 05:10:44 +08:00
|
|
|
delete (*i);
|
|
|
|
i = mNetworks->erase(i);
|
|
|
|
}
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
}
|
|
|
|
|
2009-06-16 05:10:44 +08:00
|
|
|
|
|
|
|
LOGD("Networks added %d, refreshed %d, removed %d\n",
|
|
|
|
num_added, num_refreshed, num_removed);
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
free(reply);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::connectToSupplicant() {
|
|
|
|
if (!isStarted())
|
|
|
|
LOGW("Supplicant service not running");
|
|
|
|
|
|
|
|
mCtrl = wpa_ctrl_open("tiwlan0"); // XXX:
|
2009-05-07 02:16:52 +08:00
|
|
|
if (mCtrl == NULL) {
|
|
|
|
LOGE("Unable to open connection to supplicant on \"%s\": %s",
|
|
|
|
"tiwlan0", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mMonitor = wpa_ctrl_open("tiwlan0");
|
|
|
|
if (mMonitor == NULL) {
|
|
|
|
wpa_ctrl_close(mCtrl);
|
|
|
|
mCtrl = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (wpa_ctrl_attach(mMonitor) != 0) {
|
|
|
|
wpa_ctrl_close(mMonitor);
|
|
|
|
wpa_ctrl_close(mCtrl);
|
|
|
|
mCtrl = mMonitor = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-16 05:10:44 +08:00
|
|
|
mListener = new SupplicantListener(mHandlers, mMonitor);
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-05-07 02:16:52 +08:00
|
|
|
if (mListener->startListener()) {
|
|
|
|
LOGE("Error - unable to start supplicant listener");
|
|
|
|
stop();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
int Supplicant::setScanMode(bool active) {
|
|
|
|
char reply[255];
|
|
|
|
size_t len = sizeof(reply);
|
|
|
|
|
|
|
|
if (sendCommand((active ? "DRIVER SCAN-ACTIVE" : "DRIVER SCAN-PASSIVE"),
|
|
|
|
reply, &len)) {
|
|
|
|
LOGW("triggerScan(%d): Error setting scan mode (%s)", active,
|
|
|
|
strerror(errno));
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2009-06-24 12:10:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-07 02:16:52 +08:00
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
int Supplicant::triggerScan() {
|
|
|
|
char reply[255];
|
|
|
|
size_t len = sizeof(reply);
|
2009-05-07 02:16:52 +08:00
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
if (sendCommand("SCAN", reply, &len)) {
|
|
|
|
LOGW("triggerScan(): Error initiating scan");
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
2009-06-24 12:10:57 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::getRssi(int *buffer) {
|
|
|
|
char reply[64];
|
|
|
|
size_t len = sizeof(reply);
|
|
|
|
|
|
|
|
if (sendCommand("DRIVER RSSI", reply, &len)) {
|
|
|
|
LOGW("Failed to get RSSI (%s)", strerror(errno));
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
char *next = reply;
|
|
|
|
char *s;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (!(s = strsep(&next, " "))) {
|
|
|
|
LOGE("Error parsing RSSI");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*buffer = atoi(s);
|
2009-05-07 02:16:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
int Supplicant::getLinkSpeed() {
|
|
|
|
char reply[64];
|
2009-05-07 02:16:52 +08:00
|
|
|
size_t len = sizeof(reply);
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
if (sendCommand("DRIVER LINKSPEED", reply, &len)) {
|
|
|
|
LOGW("Failed to get LINKSPEED (%s)", strerror(errno));
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
char *next = reply;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (!(s = strsep(&next, " "))) {
|
|
|
|
LOGE("Error parsing LINKSPEED");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(s = strsep(&next, " "))) {
|
|
|
|
LOGE("Error parsing LINKSPEED");
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return atoi(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::stopDriver() {
|
|
|
|
char reply[64];
|
|
|
|
size_t len = sizeof(reply);
|
|
|
|
|
|
|
|
LOGD("stopDriver()");
|
|
|
|
|
|
|
|
if (sendCommand("DRIVER STOP", reply, &len)) {
|
|
|
|
LOGW("Failed to stop driver (%s)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::startDriver() {
|
|
|
|
char reply[64];
|
|
|
|
size_t len = sizeof(reply);
|
|
|
|
|
|
|
|
LOGD("startDriver()");
|
|
|
|
if (sendCommand("DRIVER START", reply, &len)) {
|
|
|
|
LOGW("Failed to start driver (%s)", strerror(errno));
|
2009-05-07 02:16:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
WifiNetwork *Supplicant::createNetwork() {
|
|
|
|
char reply[255];
|
2009-05-13 08:26:28 +08:00
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
|
|
|
if (sendCommand("ADD_NETWORK", reply, &len))
|
2009-05-23 06:36:13 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (reply[strlen(reply) -1] == '\n')
|
|
|
|
reply[strlen(reply) -1] = '\0';
|
2009-05-13 08:26:28 +08:00
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
WifiNetwork *wn = new WifiNetwork(mController, this, atoi(reply));
|
2009-06-24 12:10:57 +08:00
|
|
|
PropertyManager *pm = NetworkManager::Instance()->getPropMngr();
|
2009-05-23 06:36:13 +08:00
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
2009-06-24 12:10:57 +08:00
|
|
|
char new_ns[20];
|
|
|
|
snprintf(new_ns, sizeof(new_ns), "wifi.net.%d", wn->getNetworkId());
|
|
|
|
wn->attachProperties(pm, new_ns);
|
2009-05-23 06:36:13 +08:00
|
|
|
mNetworks->push_back(wn);
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
|
|
|
return wn;
|
2009-05-13 08:26:28 +08:00
|
|
|
}
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
int Supplicant::removeNetwork(WifiNetwork *wn) {
|
2009-05-13 08:26:28 +08:00
|
|
|
char req[64];
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
sprintf(req, "REMOVE_NETWORK %d", wn->getNetworkId());
|
2009-05-13 08:26:28 +08:00
|
|
|
char reply[32];
|
|
|
|
size_t len = sizeof(reply) -1;
|
2009-05-23 06:36:13 +08:00
|
|
|
|
2009-05-13 08:26:28 +08:00
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
2009-05-23 06:36:13 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
|
|
|
WifiNetworkCollection::iterator it;
|
|
|
|
for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
|
|
|
|
if ((*it) == wn) {
|
|
|
|
mNetworks->erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
2009-05-13 08:26:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
WifiNetwork *Supplicant::lookupNetwork(int networkId) {
|
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
2009-06-16 05:10:44 +08:00
|
|
|
WifiNetwork *wn = lookupNetwork_UNLOCKED(networkId);
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
|
|
|
return wn;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiNetwork *Supplicant::lookupNetwork_UNLOCKED(int networkId) {
|
2009-05-23 06:36:13 +08:00
|
|
|
WifiNetworkCollection::iterator it;
|
|
|
|
for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
|
|
|
|
if ((*it)->getNetworkId() == networkId) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errno = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiNetworkCollection *Supplicant::createNetworkList() {
|
|
|
|
WifiNetworkCollection *d = new WifiNetworkCollection();
|
|
|
|
WifiNetworkCollection::iterator i;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
|
|
|
for (i = mNetworks->begin(); i != mNetworks->end(); ++i)
|
|
|
|
d->push_back((*i)->clone());
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2009-05-11 00:27:07 +08:00
|
|
|
int Supplicant::setupConfig() {
|
|
|
|
char buf[2048];
|
|
|
|
int srcfd, destfd;
|
|
|
|
int nread;
|
|
|
|
|
|
|
|
if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (errno != ENOENT) {
|
|
|
|
LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
|
|
|
|
if (srcfd < 0) {
|
|
|
|
LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
|
|
|
|
if (destfd < 0) {
|
|
|
|
close(srcfd);
|
|
|
|
LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
|
|
|
|
if (nread < 0) {
|
|
|
|
LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
|
|
|
|
close(srcfd);
|
|
|
|
close(destfd);
|
|
|
|
unlink(SUPP_CONFIG_FILE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
write(destfd, buf, nread);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(destfd);
|
|
|
|
close(srcfd);
|
|
|
|
|
|
|
|
if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
|
|
|
|
LOGE("Error changing group ownership of %s to %d: %s",
|
|
|
|
SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
|
|
|
|
unlink(SUPP_CONFIG_FILE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
|
|
|
|
int Supplicant::setNetworkVar(int networkId, const char *var, const char *val) {
|
|
|
|
char reply[255];
|
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
LOGD("netid %d, var '%s' = '%s'", networkId, var, val);
|
2009-05-23 06:36:13 +08:00
|
|
|
char *tmp;
|
2009-06-24 12:10:57 +08:00
|
|
|
asprintf(&tmp, "SET_NETWORK %d %s %s", networkId, var, val);
|
2009-05-23 06:36:13 +08:00
|
|
|
if (sendCommand(tmp, reply, &len)) {
|
|
|
|
free(tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(tmp);
|
2009-06-16 05:10:44 +08:00
|
|
|
|
|
|
|
len = sizeof(reply) -1;
|
|
|
|
if (sendCommand("SAVE_CONFIG", reply, &len)) {
|
|
|
|
LOGE("Error saving config after %s = %s", var, val);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-23 06:36:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Supplicant::getNetworkVar(int networkId, const char *var,
|
|
|
|
char *buffer, size_t max) {
|
|
|
|
size_t len = max - 1;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
asprintf(&tmp, "GET_NETWORK %d %s", networkId, var);
|
|
|
|
if (sendCommand(tmp, buffer, &len)) {
|
|
|
|
free(tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::enableNetwork(int networkId, bool enabled) {
|
|
|
|
char req[64];
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
sprintf(req, "ENABLE_NETWORK %d", networkId);
|
|
|
|
else
|
|
|
|
sprintf(req, "DISABLE_NETWORK %d", networkId);
|
|
|
|
|
|
|
|
char reply[16];
|
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 12:10:57 +08:00
|
|
|
int Supplicant::enablePacketFilter() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <=3; i++) {
|
|
|
|
snprintf(req, sizeof(req), "DRIVER RXFILTER-ADD %d", i);
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("DRIVER RXFILTER-START", reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::disablePacketFilter() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("DRIVER RXFILTER-STOP", reply, &len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 3; i >=0; i--) {
|
|
|
|
snprintf(req, sizeof(req), "DRIVER RXFILTER-REMOVE %d", i);
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::enableBluetoothCoexistenceScan() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("DRIVER BTCOEXSCAN-START", reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::disableBluetoothCoexistenceScan() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("DRIVER BTCOEXSCAN-STOP", reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::setBluetoothCoexistenceMode(int mode) {
|
|
|
|
char req[64];
|
|
|
|
|
|
|
|
sprintf(req, "DRIVER BTCOEXMODE %d", mode);
|
|
|
|
|
|
|
|
char reply[16];
|
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::setApScanMode(int mode) {
|
|
|
|
char req[64];
|
|
|
|
|
|
|
|
// LOGD("setApScanMode(%d)", mode);
|
|
|
|
sprintf(req, "AP_SCAN %d", mode);
|
|
|
|
|
|
|
|
char reply[16];
|
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
|
|
|
if (sendCommand(req, reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-23 06:36:13 +08:00
|
|
|
|
|
|
|
int Supplicant::retrieveInterfaceName() {
|
|
|
|
char reply[255];
|
|
|
|
size_t len = sizeof(reply) -1;
|
|
|
|
|
|
|
|
if (sendCommand("INTERFACES", reply, &len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
reply[strlen(reply)-1] = '\0';
|
|
|
|
mInterfaceName = strdup(reply);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-24 12:10:57 +08:00
|
|
|
|
|
|
|
int Supplicant::reconnect() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("RECONNECT", reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::disconnect() {
|
|
|
|
char req[128];
|
|
|
|
char reply[16];
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = sizeof(reply);
|
|
|
|
if (sendCommand("DISCONNECT", reply, &len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Supplicant::getNetworkCount() {
|
|
|
|
pthread_mutex_lock(&mNetworksLock);
|
|
|
|
int cnt = mNetworks->size();
|
|
|
|
pthread_mutex_unlock(&mNetworksLock);
|
|
|
|
return cnt;
|
|
|
|
}
|