Merge changes 1341,1342 into donut

* changes:
  libsysutils: Add multiple client support and fix some bugs
  nexus: Implement wifi scanner and fix a lot of bugs
This commit is contained in:
Android (Google) Code Review 2009-05-11 09:01:15 -07:00
commit c73d9e43a0
36 changed files with 764 additions and 423 deletions

View file

@ -0,0 +1,21 @@
#ifndef _FRAMEWORK_CLIENT_H
#define _FRAMEWORK_CLIENT_H
#include "../../../frameworks/base/include/utils/List.h"
#include <pthread.h>
class FrameworkClient {
int mSocket;
pthread_mutex_t mWriteMutex;
public:
FrameworkClient(int sock);
virtual ~FrameworkClient() {}
int sendMsg(char *msg);
int sendMsg(char *msg, char *data);
};
typedef android::List<FrameworkClient *> FrameworkClientCollection;
#endif

View file

@ -18,6 +18,7 @@
#include "../../../frameworks/base/include/utils/List.h" #include "../../../frameworks/base/include/utils/List.h"
class SocketClient;
class FrameworkCommand { class FrameworkCommand {
private: private:
@ -28,7 +29,7 @@ public:
FrameworkCommand(const char *cmd); FrameworkCommand(const char *cmd);
virtual ~FrameworkCommand() { } virtual ~FrameworkCommand() { }
virtual int runCommand(char *data); virtual int runCommand(SocketClient *c, char *data) = 0;
const char *getCommand() { return mCommand; } const char *getCommand() { return mCommand; }
}; };

View file

@ -19,6 +19,8 @@
#include "SocketListener.h" #include "SocketListener.h"
#include "FrameworkCommand.h" #include "FrameworkCommand.h"
class SocketClient;
class FrameworkListener : public SocketListener { class FrameworkListener : public SocketListener {
private: private:
FrameworkCommandCollection *mCommands; FrameworkCommandCollection *mCommands;
@ -29,9 +31,9 @@ public:
protected: protected:
void registerCmd(FrameworkCommand *cmd); void registerCmd(FrameworkCommand *cmd);
virtual bool onDataAvailable(int socket); virtual bool onDataAvailable(SocketClient *c);
private: private:
void dispatchCommand(char *cmd); void dispatchCommand(SocketClient *c, char *cmd);
}; };
#endif #endif

View file

@ -1,40 +0,0 @@
/*
* 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.
*/
#ifndef _FRAMEWORKMANAGER_H
#define _FRAMEWORKMANAGER_H
#include <pthread.h>
class FrameworkListener;
class FrameworkManager {
int mDoorbell; // Socket used to accept connections from framework
int mFwSock; // Socket used to communicate with framework
const char *mSocketName;
FrameworkListener *mListener;
pthread_mutex_t mWriteMutex;
public:
FrameworkManager(FrameworkListener *Listener);
virtual ~FrameworkManager() {}
int run();
int sendMsg(char *msg);
int sendMsg(char *msg, char *data);
};
#endif

View file

@ -27,6 +27,6 @@ public:
NetlinkListener(int socket); NetlinkListener(int socket);
virtual ~NetlinkListener() {} virtual ~NetlinkListener() {}
protected: protected:
virtual bool onDataAvailable(int socket); virtual bool onDataAvailable(SocketClient *cli);
}; };
#endif #endif

View file

@ -0,0 +1,23 @@
#ifndef _SOCKET_CLIENT_H
#define _SOCKET_CLIENT_H
#include "../../../frameworks/base/include/utils/List.h"
#include <pthread.h>
class SocketClient {
int mSocket;
pthread_mutex_t mWriteMutex;
public:
SocketClient(int sock);
virtual ~SocketClient() {}
int getSocket() { return mSocket; }
int sendMsg(char *msg);
int sendMsg(char *msg, char *data);
};
typedef android::List<SocketClient *> SocketClientCollection;
#endif

View file

@ -16,20 +16,35 @@
#ifndef _SOCKETLISTENER_H #ifndef _SOCKETLISTENER_H
#define _SOCKETLISTENER_H #define _SOCKETLISTENER_H
#include <pthread.h>
#include <sysutils/SocketClient.h>
class SocketListener { class SocketListener {
int mSock; int mSock;
int mCsock; const char *mSocketName;
int mAcceptClients; SocketClientCollection *mClients;
const char *mSocketName; pthread_mutex_t mClientsLock;
bool mListen;
int mCtrlPipe[2];
pthread_t mThread;
public: public:
SocketListener(const char *socketName, bool acceptClients); SocketListener(const char *socketNames, bool listen);
SocketListener(int socketFd, bool acceptClients); SocketListener(int socketFd, bool listen);
virtual ~SocketListener() {} virtual ~SocketListener() {}
virtual int run(); int startListener();
int stopListener();
void sendBroadcast(char *msg);
void sendBroadcast(char *msg, char *data);
protected: protected:
virtual bool onDataAvailable(int socket); virtual bool onDataAvailable(SocketClient *c) = 0;
private:
static void *threadStart(void *obj);
void runListener();
}; };
#endif #endif

View file

@ -3,12 +3,12 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \ LOCAL_SRC_FILES:= \
src/FrameworkManager.cpp \
src/SocketListener.cpp \ src/SocketListener.cpp \
src/FrameworkListener.cpp \ src/FrameworkListener.cpp \
src/NetlinkListener.cpp \ src/NetlinkListener.cpp \
src/NetlinkEvent.cpp \ src/NetlinkEvent.cpp \
src/FrameworkCommand.cpp \ src/FrameworkCommand.cpp \
src/SocketClient.cpp \
LOCAL_MODULE:= libsysutils LOCAL_MODULE:= libsysutils

View file

@ -0,0 +1,41 @@
#include <alloca.h>
#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
#define LOG_TAG "FrameworkClient"
#include <cutils/log.h>
#include <sysutils/FrameworkClient.h>
FrameworkClient::FrameworkClient(int socket) {
mSocket = socket;
pthread_mutex_init(&mWriteMutex, NULL);
}
int FrameworkClient::sendMsg(char *msg) {
LOGD("FrameworkClient::sendMsg(%s)", msg);
if (mSocket < 0) {
errno = EHOSTUNREACH;
return -1;
}
pthread_mutex_lock(&mWriteMutex);
if (write(mSocket, msg, strlen(msg) +1) < 0) {
LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
}
pthread_mutex_unlock(&mWriteMutex);
return 0;
}
int FrameworkClient::sendMsg(char *msg, char *data) {
char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
if (!buffer) {
errno = -ENOMEM;
return -1;
}
strcpy(buffer, msg);
strcat(buffer, data);
return sendMsg(buffer);
}

View file

@ -25,7 +25,7 @@ FrameworkCommand::FrameworkCommand(const char *cmd) {
mCommand = cmd; mCommand = cmd;
} }
int FrameworkCommand::runCommand(char *data) { int FrameworkCommand::runCommand(SocketClient *c, char *data) {
LOGW("Command %s has no run handler!", getCommand()); LOGW("Command %s has no run handler!", getCommand());
errno = ENOSYS; errno = ENOSYS;
return -1; return -1;

View file

@ -22,17 +22,18 @@
#include <sysutils/FrameworkListener.h> #include <sysutils/FrameworkListener.h>
#include <sysutils/FrameworkCommand.h> #include <sysutils/FrameworkCommand.h>
#include <sysutils/SocketClient.h>
FrameworkListener::FrameworkListener(const char *socketName) : FrameworkListener::FrameworkListener(const char *socketName) :
SocketListener(socketName, true) { SocketListener(socketName, true) {
mCommands = new FrameworkCommandCollection(); mCommands = new FrameworkCommandCollection();
} }
bool FrameworkListener::onDataAvailable(int socket) { bool FrameworkListener::onDataAvailable(SocketClient *c) {
char buffer[101]; char buffer[101];
int len; int len;
if ((len = read(socket, buffer, sizeof(buffer) -1)) < 0) { if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
LOGE("read() failed (%s)", strerror(errno)); LOGE("read() failed (%s)", strerror(errno));
return errno; return errno;
} else if (!len) { } else if (!len) {
@ -47,7 +48,7 @@ bool FrameworkListener::onDataAvailable(int socket) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (buffer[i] == '\0') { if (buffer[i] == '\0') {
dispatchCommand(buffer + start); dispatchCommand(c, buffer + start);
start = i + 1; start = i + 1;
} }
} }
@ -58,14 +59,22 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
mCommands->push_back(cmd); mCommands->push_back(cmd);
} }
void FrameworkListener::dispatchCommand(char *cmd) { void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
char *cm, *last;
if (!(cm = strtok_r(cmd, ":", &last))) {
cli->sendMsg("BAD_MSG");
return;
}
FrameworkCommandCollection::iterator i; FrameworkCommandCollection::iterator i;
for (i = mCommands->begin(); i != mCommands->end(); ++i) { for (i = mCommands->begin(); i != mCommands->end(); ++i) {
FrameworkCommand *c = *i; FrameworkCommand *c = *i;
if (!strncmp(cmd, c->getCommand(), strlen(c->getCommand()))) { if (!strcmp(cm, c->getCommand())) {
if (c->runCommand(cmd)) { if (c->runCommand(cli, cmd)) {
LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno)); LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
} }
return; return;
@ -73,5 +82,6 @@ void FrameworkListener::dispatchCommand(char *cmd) {
} }
LOGE("No cmd handlers defined for '%s'", cmd); LOGE("No cmd handlers defined for '%s'", cmd);
cli->sendMsg("UNKNOWN_CMD");
return;
} }

View file

@ -1,83 +0,0 @@
/*
* 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.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <cutils/config_utils.h>
#include <cutils/cpu_info.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
#define LOG_TAG "FrameworkManager"
#include <cutils/log.h>
#include <sysutils/FrameworkManager.h>
#include <sysutils/FrameworkListener.h>
FrameworkManager::FrameworkManager(FrameworkListener *Listener) {
mDoorbell = -1;
mFwSock = -1;
mListener = Listener;
pthread_mutex_init(&mWriteMutex, NULL);
}
int FrameworkManager::run() {
if (mListener->run()) {
LOGE("Error running listener (%s)", strerror(errno));
return -1;
}
return 0;
}
/* ========
* Privates
* ========
*/
int FrameworkManager::sendMsg(char *msg) {
LOGD("FrameworkManager::sendMsg(%s)", msg);
if (mFwSock < 0) {
errno = EHOSTUNREACH;
return -1;
}
pthread_mutex_lock(&mWriteMutex);
if (write(mFwSock, msg, strlen(msg) +1) < 0) {
LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
}
pthread_mutex_unlock(&mWriteMutex);
return 0;
}
int FrameworkManager::sendMsg(char *msg, char *data) {
char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
if (!buffer) {
errno = -ENOMEM;
return -1;
}
strcpy(buffer, msg);
strcat(buffer, data);
return sendMsg(buffer);
}

View file

@ -29,8 +29,9 @@ NetlinkListener::NetlinkListener(int socket) :
SocketListener(socket, false) { SocketListener(socket, false) {
} }
bool NetlinkListener::onDataAvailable(int socket) bool NetlinkListener::onDataAvailable(SocketClient *cli)
{ {
int socket = cli->getSocket();
LOGD("NetlinkListener::onDataAvailable()"); LOGD("NetlinkListener::onDataAvailable()");
int count; int count;

View file

@ -0,0 +1,41 @@
#include <alloca.h>
#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
#define LOG_TAG "SocketClient"
#include <cutils/log.h>
#include <sysutils/SocketClient.h>
SocketClient::SocketClient(int socket) {
mSocket = socket;
pthread_mutex_init(&mWriteMutex, NULL);
}
int SocketClient::sendMsg(char *msg) {
LOGD("SocketClient::sendMsg(%s)", msg);
if (mSocket < 0) {
errno = EHOSTUNREACH;
return -1;
}
pthread_mutex_lock(&mWriteMutex);
if (write(mSocket, msg, strlen(msg) +1) < 0) {
LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
}
pthread_mutex_unlock(&mWriteMutex);
return 0;
}
int SocketClient::sendMsg(char *msg, char *data) {
char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
if (!buffer) {
errno = -ENOMEM;
return -1;
}
strcpy(buffer, msg);
strcat(buffer, data);
return sendMsg(buffer);
}

View file

@ -24,26 +24,28 @@
#define LOG_TAG "SocketListener" #define LOG_TAG "SocketListener"
#include <cutils/log.h> #include <cutils/log.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <sysutils/SocketListener.h> #include <sysutils/SocketListener.h>
#include <sysutils/SocketClient.h>
SocketListener::SocketListener(const char *socketName, bool acceptClients) { SocketListener::SocketListener(const char *socketName, bool listen) {
mAcceptClients = acceptClients; mListen = listen;
mCsock = -1;
mSocketName = socketName; mSocketName = socketName;
mSock = -1; mSock = -1;
pthread_mutex_init(&mClientsLock, NULL);
mClients = new SocketClientCollection();
} }
SocketListener::SocketListener(int socketFd, bool acceptClients) { SocketListener::SocketListener(int socketFd, bool listen) {
mAcceptClients = acceptClients; mListen = listen;
mCsock = -1;
mSocketName = NULL; mSocketName = NULL;
mSock = socketFd; mSock = socketFd;
pthread_mutex_init(&mClientsLock, NULL);
mClients = new SocketClientCollection();
} }
int SocketListener::run() { int SocketListener::startListener() {
if (!mSocketName && mSock == -1) { if (!mSocketName && mSock == -1) {
errno = EINVAL; errno = EINVAL;
@ -56,72 +58,155 @@ int SocketListener::run() {
} }
} }
if (mAcceptClients) { if (mListen && listen(mSock, 4) < 0) {
if (listen(mSock, 4) < 0) { LOGE("Unable to listen on socket (%s)", strerror(errno));
LOGE("Unable to listen on socket (%s)", strerror(errno)); return -1;
return -1; } else if (!mListen) {
} mClients->push_back(new SocketClient(mSock));
LOGD("Created phantom client");
} }
if (pipe(mCtrlPipe))
return -1;
if (pthread_create(&mThread, NULL, SocketListener::threadStart, this))
return -1;
return 0;
}
int SocketListener::stopListener() {
char c = 0;
if (write(mCtrlPipe[1], &c, 1) != 1) {
LOGE("Error writing to control pipe (%s)", strerror(errno));
return -1;
}
LOGD("Signaled listener thread - waiting for it to die");
void *ret;
if (pthread_join(mThread, &ret)) {
LOGE("Error joining to listener thread (%s)", strerror(errno));
return -1;
}
LOGD("Listener stopped");
return 0;
}
void *SocketListener::threadStart(void *obj) {
SocketListener *me = reinterpret_cast<SocketListener *>(obj);
me->runListener();
LOGD("Listener thread shutting down");
pthread_exit(NULL);
return NULL;
}
void SocketListener::runListener() {
while(1) { while(1) {
SocketClientCollection::iterator it;
fd_set read_fds; fd_set read_fds;
struct timeval to; struct timeval to;
int max = 0;
int rc = 0; int rc = 0;
to.tv_sec = 60 * 60; to.tv_sec = 60 * 60;
to.tv_usec = 0; to.tv_usec = 0;
int max = 0;
FD_ZERO(&read_fds); FD_ZERO(&read_fds);
if ((mAcceptClients == false) || if (mListen) {
(mAcceptClients == true && mCsock == -1)) {
FD_SET(mSock, &read_fds);
max = mSock; max = mSock;
} else if (mCsock != -1) { FD_SET(mSock, &read_fds);
FD_SET(mCsock, &read_fds);
max = mCsock;
} }
FD_SET(mCtrlPipe[0], &read_fds);
if (mCtrlPipe[0] > max)
max = mCtrlPipe[0];
pthread_mutex_lock(&mClientsLock);
for (it = mClients->begin(); it != mClients->end(); ++it) {
FD_SET((*it)->getSocket(), &read_fds);
if ((*it)->getSocket() > max)
max = (*it)->getSocket();
}
pthread_mutex_unlock(&mClientsLock);
if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) { if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) {
LOGE("select failed (%s)", strerror(errno)); LOGE("select failed (%s)", strerror(errno));
return -errno; sleep(1);
} else if (!rc)
continue; continue;
else if (FD_ISSET(mSock, &read_fds)) { } else if (!rc) {
/* LOGD("select timeout");
* If we're accepting client connections then continue;
* accept and gobble the event. Otherwise }
* pass it on to the handlers.
*/
if (mAcceptClients) {
struct sockaddr addr;
socklen_t alen = sizeof(addr);
if ((mCsock = accept(mSock, &addr, &alen)) < 0) { if (FD_ISSET(mCtrlPipe[0], &read_fds)) {
LOGE("accept failed (%s)", strerror(errno)); LOGD("Control message received");
return -errno; break;
}
if (mListen && FD_ISSET(mSock, &read_fds)) {
struct sockaddr addr;
socklen_t alen = sizeof(addr);
int c;
if ((c = accept(mSock, &addr, &alen)) < 0) {
LOGE("accept failed (%s)", strerror(errno));
sleep(1);
continue;
}
LOGD("SocketListener client connection accepted");
pthread_mutex_lock(&mClientsLock);
mClients->push_back(new SocketClient(c));
pthread_mutex_unlock(&mClientsLock);
}
do {
pthread_mutex_lock(&mClientsLock);
for (it = mClients->begin(); it != mClients->end(); ++it) {
int fd = (*it)->getSocket();
if (FD_ISSET(fd, &read_fds)) {
pthread_mutex_unlock(&mClientsLock);
if (!onDataAvailable(*it)) {
LOGD("SocketListener closing client socket");
close(fd);
pthread_mutex_lock(&mClientsLock);
delete *it;
it = mClients->erase(it);
pthread_mutex_unlock(&mClientsLock);
}
FD_CLR(fd, &read_fds);
continue;
} }
LOGD("SocketListener client connection accepted");
} else if (!onDataAvailable(mSock)) {
LOGW("SocketListener closing listening socket (Will shut down)");
close(mSock);
return -ESHUTDOWN;
}
} else if ((FD_ISSET(mCsock, &read_fds)) &&
!onDataAvailable(mCsock)) {
/*
* Once mCsock == -1, we'll start
* accepting connections on mSock again.
*/
LOGD("SocketListener closing client socket");
close(mCsock);
mCsock = -1;
} }
pthread_mutex_unlock(&mClientsLock);
} while (0);
} }
return 0;
} }
bool SocketListener::onDataAvailable(int socket) { void SocketListener::sendBroadcast(char *msg) {
return false; pthread_mutex_lock(&mClientsLock);
SocketClientCollection::iterator i;
for (i = mClients->begin(); i != mClients->end(); ++i) {
if ((*i)->sendMsg(msg)) {
LOGW("Error sending broadcast (%s)", strerror(errno));
}
}
pthread_mutex_unlock(&mClientsLock);
} }
void SocketListener::sendBroadcast(char *msg, char *data) {
pthread_mutex_lock(&mClientsLock);
SocketClientCollection::iterator i;
for (i = mClients->begin(); i != mClients->end(); ++i) {
if ((*i)->sendMsg(msg, data)) {
LOGW("Error sending broadcast (%s)", strerror(errno));
}
}
pthread_mutex_unlock(&mClientsLock);
}

View file

@ -19,6 +19,7 @@ LOCAL_SRC_FILES:= \
SupplicantListener.cpp \ SupplicantListener.cpp \
VpnController.cpp \ VpnController.cpp \
ScanResult.cpp \ ScanResult.cpp \
WifiScanner.cpp \
LOCAL_MODULE:= nexus LOCAL_MODULE:= nexus

View file

@ -19,60 +19,64 @@
#define LOG_TAG "CommandListener" #define LOG_TAG "CommandListener"
#include <cutils/log.h> #include <cutils/log.h>
#include <sysutils/SocketClient.h>
#include "CommandListener.h" #include "CommandListener.h"
#include "Controller.h" #include "Controller.h"
#include "NetworkManager.h" #include "NetworkManager.h"
#include "WifiController.h" #include "WifiController.h"
CommandListener::CommandListener(NetworkManager *netman) : CommandListener::CommandListener() :
FrameworkListener("nexus") { FrameworkListener("nexus") {
mNetman = netman; registerCmd(new WifiEnableCmd());
registerCmd(new WifiDisableCmd());
registerCmd(new WifiScanCmd());
registerCmd(new WifiScanResultsCmd());
registerCmd(new WifiEnableCmd(netman)); registerCmd(new VpnEnableCmd());
registerCmd(new WifiDisableCmd(netman)); registerCmd(new VpnDisableCmd());
registerCmd(new WifiScanCmd(netman));
registerCmd(new VpnEnableCmd(netman));
registerCmd(new VpnDisableCmd(netman));
} }
/* ------------- /* -------------
* Wifi Commands * Wifi Commands
* ------------ */ * ------------ */
CommandListener::WifiEnableCmd::WifiEnableCmd(NetworkManager *netman) : CommandListener::WifiEnableCmd::WifiEnableCmd() :
NexusCommand("wifi_enable", netman) { NexusCommand("wifi_enable") {
} }
int CommandListener::WifiEnableCmd::runCommand(char *data) { int CommandListener::WifiEnableCmd::runCommand(SocketClient *cli, char *data) {
Controller *c = mNetman->findController("WIFI"); Controller *c = NetworkManager::Instance()->findController("WIFI");
char buffer[32]; char buffer[32];
sprintf(buffer, "WIFI_ENABLE:%d", (c->enable() ? errno : 0)); sprintf(buffer, "WIFI_ENABLE:%d", (c->enable() ? errno : 0));
mNetman->getFrameworkManager()->sendMsg(buffer);
cli->sendMsg(buffer);
return 0; return 0;
} }
CommandListener::WifiDisableCmd::WifiDisableCmd(NetworkManager *netman) : CommandListener::WifiDisableCmd::WifiDisableCmd() :
NexusCommand("wifi_disable", netman) { NexusCommand("wifi_disable") {
} }
int CommandListener::WifiDisableCmd::runCommand(char *data) { int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) {
Controller *c = mNetman->findController("WIFI"); Controller *c = NetworkManager::Instance()->findController("WIFI");
char buffer[32]; char buffer[32];
sprintf(buffer, "WIFI_DISABLE:%d", (c->disable() ? errno : 0)); sprintf(buffer, "WIFI_DISABLE:%d", (c->disable() ? errno : 0));
mNetman->getFrameworkManager()->sendMsg(buffer); cli->sendMsg(buffer);
return 0; return 0;
} }
CommandListener::WifiScanCmd::WifiScanCmd(NetworkManager *netman) : CommandListener::WifiScanCmd::WifiScanCmd() :
NexusCommand("wifi_scan", netman) { NexusCommand("wifi_scan") {
} }
int CommandListener::WifiScanCmd::runCommand(char *data) { int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) {
LOGD("WifiScanCmd(%s)", data); LOGD("WifiScanCmd(%s)", data);
WifiController *wc = (WifiController *) mNetman->findController("WIFI");
WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
char buffer[32]; char buffer[32];
int mode = 0; int mode = 0;
char *bword, *last; char *bword, *last;
@ -90,35 +94,62 @@ int CommandListener::WifiScanCmd::runCommand(char *data) {
mode = atoi(bword); mode = atoi(bword);
sprintf(buffer, "WIFI_SCAN:%d", (wc->setScanMode(mode) ? errno : 0)); sprintf(buffer, "WIFI_SCAN:%d", (wc->setScanMode(mode) ? errno : 0));
mNetman->getFrameworkManager()->sendMsg(buffer); cli->sendMsg(buffer);
return 0;
}
CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
NexusCommand("wifi_scan_results") {
}
int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) {
NetworkManager *nm = NetworkManager::Instance();
WifiController *wc = (WifiController *) nm->findController("WIFI");
ScanResultCollection *src = wc->createScanResults();
ScanResultCollection::iterator it;
char buffer[256];
for(it = src->begin(); it != src->end(); ++it) {
sprintf(buffer, "WIFI_SCAN_RESULT:%s:%u:%d:%s:%s",
(*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(),
(*it)->getFlags(), (*it)->getSsid());
cli->sendMsg(buffer);
delete (*it);
it = src->erase(it);
}
delete src;
cli->sendMsg("WIFI_SCAN_RESULT:0");
return 0; return 0;
} }
/* ------------ /* ------------
* Vpn Commands * Vpn Commands
* ------------ */ * ------------ */
CommandListener::VpnEnableCmd::VpnEnableCmd(NetworkManager *netman) : CommandListener::VpnEnableCmd::VpnEnableCmd() :
NexusCommand("vpn_enable", netman) { NexusCommand("vpn_enable") {
} }
int CommandListener::VpnEnableCmd::runCommand(char *data) { int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) {
Controller *c = mNetman->findController("VPN"); Controller *c = NetworkManager::Instance()->findController("VPN");
char buffer[32]; char buffer[32];
sprintf(buffer, "VPN_ENABLE:%d", (c->enable() ? errno : 0)); sprintf(buffer, "VPN_ENABLE:%d", (c->enable() ? errno : 0));
mNetman->getFrameworkManager()->sendMsg(buffer); cli->sendMsg(buffer);
return 0; return 0;
} }
CommandListener::VpnDisableCmd::VpnDisableCmd(NetworkManager *netman) : CommandListener::VpnDisableCmd::VpnDisableCmd() :
NexusCommand("vpn_disable", netman) { NexusCommand("vpn_disable") {
} }
int CommandListener::VpnDisableCmd::runCommand(char *data) { int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) {
Controller *c = mNetman->findController("VPN"); Controller *c = NetworkManager::Instance()->findController("VPN");
char buffer[32]; char buffer[32];
sprintf(buffer, "VPN_DISABLE:%d", (c->disable() ? errno : 0)); sprintf(buffer, "VPN_DISABLE:%d", (c->disable() ? errno : 0));
mNetman->getFrameworkManager()->sendMsg(buffer); cli->sendMsg(buffer);
return 0; return 0;
} }

View file

@ -19,50 +19,52 @@
#include <sysutils/FrameworkListener.h> #include <sysutils/FrameworkListener.h>
#include "NexusCommand.h" #include "NexusCommand.h"
class NetworkManager;
class CommandListener : public FrameworkListener { class CommandListener : public FrameworkListener {
protected:
NetworkManager *mNetman;
public: public:
CommandListener(NetworkManager *netman); CommandListener();
virtual ~CommandListener() {} virtual ~CommandListener() {}
private: private:
class WifiEnableCmd : public NexusCommand { class WifiEnableCmd : public NexusCommand {
public: public:
WifiEnableCmd(NetworkManager *); WifiEnableCmd();
virtual ~WifiEnableCmd() {} virtual ~WifiEnableCmd() {}
int runCommand(char *data); int runCommand(SocketClient *c, char *data);
}; };
class WifiDisableCmd : public NexusCommand { class WifiDisableCmd : public NexusCommand {
public: public:
WifiDisableCmd(NetworkManager *); WifiDisableCmd();
virtual ~WifiDisableCmd() {} virtual ~WifiDisableCmd() {}
int runCommand(char *data); int runCommand(SocketClient *c, char *data);
}; };
class WifiScanCmd : public NexusCommand { class WifiScanCmd : public NexusCommand {
public: public:
WifiScanCmd(NetworkManager *); WifiScanCmd();
virtual ~WifiScanCmd() {} virtual ~WifiScanCmd() {}
int runCommand(char *data); int runCommand(SocketClient *c, char *data);
};
class WifiScanResultsCmd : public NexusCommand {
public:
WifiScanResultsCmd();
virtual ~WifiScanResultsCmd() {}
int runCommand(SocketClient *c, char *data);
}; };
class VpnEnableCmd : public NexusCommand { class VpnEnableCmd : public NexusCommand {
public: public:
VpnEnableCmd(NetworkManager *); VpnEnableCmd();
virtual ~VpnEnableCmd() {} virtual ~VpnEnableCmd() {}
int runCommand(char *data); int runCommand(SocketClient *c, char *data);
}; };
class VpnDisableCmd : public NexusCommand { class VpnDisableCmd : public NexusCommand {
public: public:
VpnDisableCmd(NetworkManager *); VpnDisableCmd();
virtual ~VpnDisableCmd() {} virtual ~VpnDisableCmd() {}
int runCommand(char *data); int runCommand(SocketClient *c, char *data);
}; };
}; };

View file

@ -21,36 +21,30 @@
#include <cutils/log.h> #include <cutils/log.h>
#include "NetworkManager.h" #include "NetworkManager.h"
#include "CommandListener.h"
#include "LoopController.h"
#include "VpnController.h"
#include "TiwlanWifiController.h" NetworkManager *NetworkManager::sInstance = NULL;
NetworkManager *NetworkManager::Instance() {
if (!sInstance)
sInstance = new NetworkManager();
return sInstance;
}
NetworkManager::NetworkManager() { NetworkManager::NetworkManager() {
mListener = new CommandListener(this); mBroadcaster = NULL;
mFm = new FrameworkManager(mListener);
mControllers = new ControllerCollection(); mControllers = new ControllerCollection();
} }
int NetworkManager::run() { int NetworkManager::run() {
LOGD("NetworkManager::start()");
// XXX: Factory needed
addController(new LoopController());
addController(new TiwlanWifiController("/system/lib/modules/wlan.ko", "wlan", ""));
addController(new VpnController());
//addController(new GenericController("rmnet0"));
if (startControllers()) { if (startControllers()) {
LOGW("Unable to start all controllers (%s)", strerror(errno)); LOGW("Unable to start all controllers (%s)", strerror(errno));
} }
mFm->run();
return 0; return 0;
} }
void NetworkManager::addController(Controller *c) { int NetworkManager::attachController(Controller *c) {
mControllers->push_back(c); mControllers->push_back(c);
return 0;
} }
int NetworkManager::startControllers() { int NetworkManager::startControllers() {

View file

@ -16,31 +16,36 @@
#ifndef _NETWORKMANAGER_H #ifndef _NETWORKMANAGER_H
#define _NETWORKMANAGER_H #define _NETWORKMANAGER_H
#include "Controller.h" #include <sysutils/SocketListener.h>
#include <sysutils/FrameworkManager.h> #include "Controller.h"
class NetworkManager { class NetworkManager {
private: private:
FrameworkListener *mListener; static NetworkManager *sInstance;
FrameworkManager *mFm;
private:
ControllerCollection *mControllers; ControllerCollection *mControllers;
SocketListener *mBroadcaster;
public: public:
NetworkManager();
virtual ~NetworkManager() {} virtual ~NetworkManager() {}
int run(); int run();
int attachController(Controller *controller);
Controller *findController(const char *name);
void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
SocketListener *getBroadcaster() { return mBroadcaster; }
static NetworkManager *Instance();
private: private:
void addController(Controller *c);
int startControllers(); int startControllers();
int stopControllers(); int stopControllers();
NetworkManager();
public:
Controller *findController(const char *name);
ControllerCollection *getControllers() { return mControllers; }
FrameworkManager *getFrameworkManager() { return mFm; }
public: public:
// XXX: Extract these into an interface // XXX: Extract these into an interface

View file

@ -15,7 +15,6 @@
*/ */
#include "NexusCommand.h" #include "NexusCommand.h"
NexusCommand::NexusCommand(const char *cmd, NetworkManager *netman) : NexusCommand::NexusCommand(const char *cmd) :
FrameworkCommand(cmd) { FrameworkCommand(cmd) {
mNetman = netman;
} }

View file

@ -18,14 +18,9 @@
#include <sysutils/FrameworkCommand.h> #include <sysutils/FrameworkCommand.h>
class NetworkManager;
class NexusCommand : public FrameworkCommand { class NexusCommand : public FrameworkCommand {
protected:
NetworkManager *mNetman;
public: public:
NexusCommand(const char *cmd, NetworkManager *netman); NexusCommand(const char *cmd);
virtual ~NexusCommand() {} virtual ~NexusCommand() {}
}; };

View file

@ -38,6 +38,7 @@ public:
const char *getBssid() { return mBssid; } const char *getBssid() { return mBssid; }
uint32_t getFreq() { return mFreq; } uint32_t getFreq() { return mFreq; }
int getLevel() { return mLevel; }
const char *getFlags() { return mFlags; } const char *getFlags() { return mFlags; }
const char *getSsid() { return mSsid; } const char *getSsid() { return mSsid; }
}; };

View file

@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <stdlib.h>
#include <errno.h> #include <errno.h>
#define LOG_TAG "Supplicant" #define LOG_TAG "Supplicant"
@ -31,6 +33,7 @@
#include "SupplicantState.h" #include "SupplicantState.h"
#include "SupplicantEvent.h" #include "SupplicantEvent.h"
#include "ScanResult.h" #include "ScanResult.h"
#include "NetworkManager.h"
#include "libwpa_client/wpa_ctrl.h" #include "libwpa_client/wpa_ctrl.h"
@ -52,7 +55,6 @@ Supplicant::Supplicant() {
} }
int Supplicant::start() { int Supplicant::start() {
LOGD("start():");
// XXX: Validate supplicant config file // XXX: Validate supplicant config file
char status[PROPERTY_VALUE_MAX] = {'\0'}; char status[PROPERTY_VALUE_MAX] = {'\0'};
@ -63,47 +65,47 @@ int Supplicant::start() {
#endif #endif
if (property_get(SUPP_PROP_NAME, status, NULL) && if (property_get(SUPP_PROP_NAME, status, NULL) &&
strcmp(status, "running") == 0) { !strcmp(status, "running")) {
return 0; LOGD("Supplicant already started");
} else {
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
pi = __system_property_find(SUPP_PROP_NAME);
if (pi != NULL)
serial = pi->serial;
#endif
LOGD("Starting Supplicant");
property_set("ctl.start", SUPPLICANT_NAME);
sched_yield();
while (count--) {
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
if (!pi)
pi = __system_property_find(SUPP_PROP_NAME);
if (pi) {
__system_property_read(pi, NULL, status);
if (strcmp(status, "running") == 0)
break;
else if (pi->serial != serial &&
strcmp(status, "stopped") == 0) {
errno = EIO;
return -1;
}
}
#else
if (property_get(SUPP_PROP_NAME, status, NULL)) {
if (strcmp(status, "running") == 0)
break;
}
#endif
usleep(100000);
}
if (!count) {
errno = ETIMEDOUT;
return -1;
}
} }
wpa_ctrl_cleanup(); wpa_ctrl_cleanup();
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
pi = __system_property_find(SUPP_PROP_NAME);
if (pi != NULL)
serial = pi->serial;
#endif
property_set("ctl.start", SUPPLICANT_NAME);
sched_yield();
while (count--) {
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
if (!pi)
pi = __system_property_find(SUPP_PROP_NAME);
if (pi) {
__system_property_read(pi, NULL, status);
if (strcmp(status, "running") == 0)
return 0;
else if (pi->serial != serial &&
strcmp(status, "stopped") == 0) {
errno = EIO;
return -1;
}
}
#else
if (property_get(SUPP_PROP_NAME, status, NULL)) {
if (strcmp(status, "running") == 0)
break;
}
#endif
usleep(100000);
}
if (!count) {
errno = ETIMEDOUT;
return -1;
}
if (connectToSupplicant()) { if (connectToSupplicant()) {
LOGE("Error connecting to supplicant (%s)\n", strerror(errno)); LOGE("Error connecting to supplicant (%s)\n", strerror(errno));
return -1; return -1;
@ -112,7 +114,6 @@ int Supplicant::start() {
} }
int Supplicant::stop() { int Supplicant::stop() {
LOGD("stop()");
char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 50; int count = 50;
@ -124,9 +125,11 @@ int Supplicant::stop() {
if (property_get(SUPP_PROP_NAME, supp_status, NULL) if (property_get(SUPP_PROP_NAME, supp_status, NULL)
&& strcmp(supp_status, "stopped") == 0) { && strcmp(supp_status, "stopped") == 0) {
LOGD("Supplicant already stopped");
return 0; return 0;
} }
LOGD("Stopping Supplicant");
property_set("ctl.stop", SUPPLICANT_NAME); property_set("ctl.stop", SUPPLICANT_NAME);
sched_yield(); sched_yield();
@ -153,24 +156,29 @@ int Supplicant::stop() {
return -1; return -1;
} }
LOGD("Stopped OK"); LOGD("Supplicant shutdown");
return 0; return 0;
} }
bool Supplicant::isStarted() { bool Supplicant::isStarted() {
char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
if (!property_get(SUPP_PROP_NAME, supp_status, NULL) ||
!strcmp(supp_status, "running")) { int rc = property_get(SUPP_PROP_NAME, supp_status, NULL);
return false;
} LOGD("rc = %d, property = '%s'", rc, supp_status);
return true;
if (!strcmp(supp_status, "running"))
return true;
return false;
} }
int Supplicant::connectToSupplicant() { int Supplicant::connectToSupplicant() {
char ifname[256]; char ifname[256];
char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
LOGD("connectToSupplicant()");
if (!property_get(SUPP_PROP_NAME, supp_status, NULL) if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
|| strcmp(supp_status, "running") != 0) { || strcmp(supp_status, "running") != 0) {
LOGE("Supplicant not running, cannot connect"); LOGE("Supplicant not running, cannot connect");
@ -213,13 +221,14 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len)
return -1; return -1;
} }
LOGD("sendCommand(): -> '%s'", cmd); // LOGD("sendCommand(): -> '%s'", cmd);
int rc; int rc;
if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) { if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2) {
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} else if (rc < 0 || !strncmp(reply, "FAIL", 4)) { } else if (rc < 0 || !strncmp(reply, "FAIL", 4)) {
LOGW("sendCommand(): <- '%s'", reply);
errno = EIO; errno = EIO;
return -1; return -1;
} }
@ -228,7 +237,7 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len)
!strncmp(cmd, "SCAN_RESULTS", 12)) !strncmp(cmd, "SCAN_RESULTS", 12))
reply[*reply_len] = '\0'; reply[*reply_len] = '\0';
LOGD("sendCommand(): <- '%s'", reply); // LOGD("sendCommand(): <- '%s'", reply);
return 0; return 0;
} }
@ -332,12 +341,16 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
if (!strtok_r(reply, "\n", &linep_next)) { if (!strtok_r(reply, "\n", &linep_next)) {
free(reply); free(reply);
return 0;; pthread_mutex_unlock(&mLatestScanResultsLock);
return 0;
} }
while((linep = strtok_r(NULL, "\n", &linep_next))) while((linep = strtok_r(NULL, "\n", &linep_next)))
mLatestScanResults->push_back(new ScanResult(linep)); mLatestScanResults->push_back(new ScanResult(linep));
char tmp[32];
sprintf(tmp, "WIFI_SCAN_RESULTS_READY:%d", mLatestScanResults->size());
NetworkManager::Instance()->getBroadcaster()->sendBroadcast(tmp);
pthread_mutex_unlock(&mLatestScanResultsLock); pthread_mutex_unlock(&mLatestScanResultsLock);
free(reply); free(reply);
} else { } else {
@ -347,8 +360,24 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
} }
int Supplicant::onStateChangeEvent(SupplicantEvent *evt) { int Supplicant::onStateChangeEvent(SupplicantEvent *evt) {
LOGD("onStateChangeEvent(%s)", evt->getEvent()); char *bword, *last;
// XXX: Update mState char *tmp = strdup(evt->getEvent());
if (!(bword = strtok_r(tmp, " ", &last))) {
LOGE("Malformatted state update (%s)", evt->getEvent());
free(tmp);
return 0;
}
if (!(bword = strtok_r(NULL, " ", &last))) {
LOGE("Malformatted state update (%s)", evt->getEvent());
free(tmp);
return 0;
}
mState = atoi(&bword[strlen("state=")]);
LOGD("State changed to %d", mState);
free(tmp);
return 0; return 0;
} }
@ -363,7 +392,7 @@ int Supplicant::onDriverStateEvent(SupplicantEvent *evt) {
} }
// XXX: Use a cursor + smartptr instead // XXX: Use a cursor + smartptr instead
const ScanResultCollection *Supplicant::getLatestScanResults() { ScanResultCollection *Supplicant::createLatestScanResults() {
ScanResultCollection *d = new ScanResultCollection(); ScanResultCollection *d = new ScanResultCollection();
ScanResultCollection::iterator i; ScanResultCollection::iterator i;

View file

@ -46,7 +46,7 @@ public:
int getState() { return mState; } int getState() { return mState; }
const ScanResultCollection *getLatestScanResults(); ScanResultCollection *createLatestScanResults();
// XXX: Extract these into an interface // XXX: Extract these into an interface
public: public:

View file

@ -30,31 +30,9 @@ SupplicantListener::SupplicantListener(Supplicant *supplicant, struct wpa_ctrl *
SocketListener(wpa_ctrl_get_fd(monitor), false) { SocketListener(wpa_ctrl_get_fd(monitor), false) {
mSupplicant = supplicant; mSupplicant = supplicant;
mMonitor = monitor; mMonitor = monitor;
mThread = NULL;
} }
int SupplicantListener::startListener() { bool SupplicantListener::onDataAvailable(SocketClient *cli) {
LOGD("startListener()");
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
return pthread_create(&mThread, &attr, &SupplicantListener::threadStart, this);
}
int SupplicantListener::stopListener() {
errno = -ENOSYS;
return -1;
}
void *SupplicantListener::threadStart(void *obj) {
LOGD("threadStart(): Worker thread started");
reinterpret_cast<SupplicantListener *>(obj)->run();
LOGD("threadStart(): Worker thread exited");
return NULL;
}
bool SupplicantListener::onDataAvailable(int socket) {
char buf[255]; char buf[255];
size_t buflen = sizeof(buf); size_t buflen = sizeof(buf);
int rc; int rc;
@ -62,7 +40,7 @@ bool SupplicantListener::onDataAvailable(int socket) {
if ((rc = wpa_ctrl_recv(mMonitor, buf, &nread))) { if ((rc = wpa_ctrl_recv(mMonitor, buf, &nread))) {
LOGE("wpa_ctrl_recv failed (%s)", strerror(errno)); LOGE("wpa_ctrl_recv failed (%s)", strerror(errno));
return -errno; return false;
} }
buf[nread] = '\0'; buf[nread] = '\0';
@ -108,7 +86,9 @@ bool SupplicantListener::onDataAvailable(int socket) {
delete evt; delete evt;
if (rc) if (rc) {
LOGW("Handler %d (%s) error: %s", evt->getType(), evt->getEvent(), strerror(errno));
return false; return false;
}
return true; return true;
} }

View file

@ -16,33 +16,27 @@
#ifndef _SUPPLICANTLISTENER_H__ #ifndef _SUPPLICANTLISTENER_H__
#define _SUPPLICANTLISTENER_H__ #define _SUPPLICANTLISTENER_H__
#include <pthread.h>
#include <sysutils/SocketListener.h> #include <sysutils/SocketListener.h>
struct wpa_ctrl; struct wpa_ctrl;
class Supplicant; class Supplicant;
class SocketClient;
class SupplicantListener: public SocketListener { class SupplicantListener: public SocketListener {
private: private:
struct wpa_ctrl *mMonitor; struct wpa_ctrl *mMonitor;
Supplicant *mSupplicant; Supplicant *mSupplicant;
pthread_t mThread;
public: public:
SupplicantListener(Supplicant *supplicant, struct wpa_ctrl *monitor); SupplicantListener(Supplicant *supplicant, struct wpa_ctrl *monitor);
virtual ~SupplicantListener() {} virtual ~SupplicantListener() {}
int startListener();
int stopListener();
struct wpa_ctrl *getMonitor() { return mMonitor; } struct wpa_ctrl *getMonitor() { return mMonitor; }
Supplicant *getSupplicant() { return mSupplicant; } Supplicant *getSupplicant() { return mSupplicant; }
protected: protected:
virtual bool onDataAvailable(int socket); virtual bool onDataAvailable(SocketClient *c);
private:
static void *threadStart(void *obj);
}; };
#endif #endif

View file

@ -18,16 +18,16 @@
class SupplicantState { class SupplicantState {
public: public:
static const int UNKNOWN = 0; static const int UNKNOWN = -1;
static const int DISCONNECTED = 1; static const int DISCONNECTED = 0;
static const int INACTIVE = 2; static const int INACTIVE = 1;
static const int SCANNING = 3; static const int SCANNING = 2;
static const int ASSOCIATING = 4; static const int ASSOCIATING = 3;
static const int ASSOCIATED = 5; static const int ASSOCIATED = 4;
static const int FOURWAY_HANDSHAKE = 6; static const int FOURWAY_HANDSHAKE = 5;
static const int GROUP_HANDSHAKE = 7; static const int GROUP_HANDSHAKE = 6;
static const int COMPLETED = 8; static const int COMPLETED = 7;
static const int IDLE = 9; static const int IDLE = 8;
}; };
#endif #endif

View file

@ -65,3 +65,17 @@ int TiwlanWifiController::loadFirmware() {
property_set(DRIVER_PROP_NAME, "timeout"); property_set(DRIVER_PROP_NAME, "timeout");
return -1; return -1;
} }
bool TiwlanWifiController::isFirmwareLoaded() {
char driver_status[PROPERTY_VALUE_MAX];
if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
if (!strcmp(driver_status, "ok"))
return true;
else {
LOGD("Driver status '%s'", driver_status);
return false;
}
}
LOGW("Unable to get property '%s'", DRIVER_PROP_NAME);
return false;
}

View file

@ -27,5 +27,6 @@ public:
virtual int powerDown(); virtual int powerDown();
virtual bool isPoweredUp(); virtual bool isPoweredUp();
virtual int loadFirmware(); virtual int loadFirmware();
virtual bool isFirmwareLoaded();
}; };
#endif #endif

View file

@ -21,6 +21,8 @@
#include "Supplicant.h" #include "Supplicant.h"
#include "WifiController.h" #include "WifiController.h"
#include "WifiScanner.h"
#include "NetworkManager.h"
WifiController::WifiController(char *modpath, char *modname, char *modargs) : WifiController::WifiController(char *modpath, char *modname, char *modargs) :
Controller("WIFI") { Controller("WIFI") {
@ -29,6 +31,7 @@ WifiController::WifiController(char *modpath, char *modname, char *modargs) :
strncpy(mModuleArgs, modargs, sizeof(mModuleArgs)); strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
mSupplicant = new Supplicant(); mSupplicant = new Supplicant();
mScanner = new WifiScanner(mSupplicant, 10);
mCurrentScanMode = 0; mCurrentScanMode = 0;
} }
@ -42,26 +45,36 @@ int WifiController::stop() {
} }
int WifiController::enable() { int WifiController::enable() {
if (!isPoweredUp() && powerUp()) { if (!isPoweredUp()) {
LOGE("Powerup failed (%s)", strerror(errno)); sendStatusBroadcast("POWERING_UP");
return -1; if (powerUp()) {
LOGE("Powerup failed (%s)", strerror(errno));
return -1;
}
} }
if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) { if (mModuleName[0] != '\0' && !isKernelModuleLoaded(mModuleName)) {
sendStatusBroadcast("LOADING_DRIVER");
if (loadKernelModule(mModulePath, mModuleArgs)) { if (loadKernelModule(mModulePath, mModuleArgs)) {
LOGE("Kernel module load failed (%s)", strerror(errno)); LOGE("Kernel module load failed (%s)", strerror(errno));
goto out_powerdown; goto out_powerdown;
} }
} }
if (loadFirmware()) { if (!isFirmwareLoaded()) {
LOGE("Firmware load failed (%s)", strerror(errno)); sendStatusBroadcast("LOADING_FIRMWARE");
goto out_powerdown; if (loadFirmware()) {
LOGE("Firmware load failed (%s)", strerror(errno));
goto out_powerdown;
}
} }
if (!mSupplicant->isStarted() && mSupplicant->start()) { if (!mSupplicant->isStarted()) {
LOGE("Supplicant start failed (%s)", strerror(errno)); sendStatusBroadcast("STARTING_SUPPLICANT");
goto out_unloadmodule; if (mSupplicant->start()) {
LOGE("Supplicant start failed (%s)", strerror(errno));
goto out_unloadmodule;
}
} }
return 0; return 0;
@ -80,24 +93,38 @@ out_powerdown:
return -1; return -1;
} }
int WifiController::disable() { void WifiController::sendStatusBroadcast(const char *msg) {
LOGD("disable()"); char tmp[255];
if (mSupplicant->isStarted() && mSupplicant->stop()) { sprintf(tmp, "WIFI_STATUS:%s", msg);
LOGE("Supplicant stop failed (%s)", strerror(errno)); NetworkManager::Instance()->getBroadcaster()->sendBroadcast(tmp);
return -1; }
}
int WifiController::disable() {
if (mSupplicant->isStarted()) {
sendStatusBroadcast("STOPPING_SUPPLICANT");
if (mSupplicant->stop()) {
LOGE("Supplicant stop failed (%s)", strerror(errno));
return -1;
}
} else
LOGW("disable(): Supplicant not running?");
if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) { if (mModuleName[0] != '\0' && isKernelModuleLoaded(mModuleName)) {
sendStatusBroadcast("UNLOADING_DRIVER");
if (unloadKernelModule(mModuleName)) { if (unloadKernelModule(mModuleName)) {
LOGE("Unable to unload module (%s)", strerror(errno)); LOGE("Unable to unload module (%s)", strerror(errno));
return -1; return -1;
} }
} }
if (isPoweredUp() && powerDown()) { if (isPoweredUp()) {
LOGE("Powerdown failed (%s)", strerror(errno)); sendStatusBroadcast("POWERING_DOWN");
return -1; if (powerDown()) {
LOGE("Powerdown failed (%s)", strerror(errno));
return -1;
}
} }
return 0; return 0;
} }
@ -106,7 +133,7 @@ int WifiController::loadFirmware() {
return 0; return 0;
} }
int WifiController::setScanMode(int mode) { int WifiController::setScanMode(uint32_t mode) {
int rc = 0; int rc = 0;
if (mCurrentScanMode == mode) if (mCurrentScanMode == mode)
@ -114,21 +141,15 @@ int WifiController::setScanMode(int mode) {
if (!(mode & SCAN_ENABLE_MASK)) { if (!(mode & SCAN_ENABLE_MASK)) {
if (mCurrentScanMode & SCAN_REPEAT_MASK) if (mCurrentScanMode & SCAN_REPEAT_MASK)
stopPeriodicScan(); mScanner->stopPeriodicScan();
} else if (mode & SCAN_REPEAT_MASK) } else if (mode & SCAN_REPEAT_MASK)
rc = startPeriodicScan(); rc = mScanner->startPeriodicScan(mode & SCAN_ACTIVE_MASK);
else else
rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK); rc = mSupplicant->triggerScan(mode & SCAN_ACTIVE_MASK);
return rc; return rc;
} }
int WifiController::startPeriodicScan() { ScanResultCollection *WifiController::createScanResults() {
errno = -ENOSYS; return mSupplicant->createLatestScanResults();
return -1;
}
int WifiController::stopPeriodicScan() {
errno = -ENOSYS;
return -1;
} }

View file

@ -22,6 +22,9 @@
class NetInterface; class NetInterface;
class Supplicant; class Supplicant;
class WifiScanner;
#include "ScanResult.h"
class WifiController : public Controller { class WifiController : public Controller {
public: public:
@ -40,8 +43,8 @@ private:
char mModulePath[255]; char mModulePath[255];
char mModuleName[64]; char mModuleName[64];
char mModuleArgs[255]; char mModuleArgs[255];
int mCurrentScanMode; uint32_t mCurrentScanMode;
WifiScanner *mScanner;
public: public:
WifiController(char *modpath, char *modname, char *modargs); WifiController(char *modpath, char *modname, char *modargs);
@ -53,6 +56,8 @@ public:
int enable(); int enable();
int disable(); int disable();
ScanResultCollection *createScanResults();
int getType(); int getType();
char *getModulePath() { return mModulePath; } char *getModulePath() { return mModulePath; }
@ -62,17 +67,17 @@ public:
Supplicant *getSupplicant() { return mSupplicant; } Supplicant *getSupplicant() { return mSupplicant; }
int getScanMode() { return mCurrentScanMode; } int getScanMode() { return mCurrentScanMode; }
int setScanMode(int mode); int setScanMode(uint32_t mode);
protected: protected:
virtual int powerUp() = 0; virtual int powerUp() = 0;
virtual int powerDown() = 0; virtual int powerDown() = 0;
virtual int loadFirmware(); virtual int loadFirmware();
virtual bool isFirmwareLoaded() = 0;
virtual bool isPoweredUp() = 0; virtual bool isPoweredUp() = 0;
private: void sendStatusBroadcast(const char *msg);
int startPeriodicScan();
int stopPeriodicScan();
}; };
#endif #endif

96
nexus/WifiScanner.cpp Normal file
View file

@ -0,0 +1,96 @@
#include <errno.h>
#include <pthread.h>
#define LOG_TAG "WifiScanner"
#include <cutils/log.h>
#include "WifiScanner.h"
#include "Supplicant.h"
extern "C" int pthread_cancel(pthread_t thread);
WifiScanner::WifiScanner(Supplicant *suppl, int period) {
mSuppl = suppl;
mPeriod = period;
mActive = false;
mWorkerRunning = false;
mAbortRequest = false;
pthread_mutex_init(&mAbortRequestLock, NULL);
pthread_mutex_init(&mWorkerLock, NULL);
}
int WifiScanner::startPeriodicScan(bool active) {
mActive = active;
pthread_mutex_lock(&mWorkerLock);
if (mWorkerRunning) {
errno = EBUSY;
return -1;
}
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&mWorker, &attr, WifiScanner::threadStart, this))
return -1;
return 0;
}
void *WifiScanner::threadStart(void *obj) {
WifiScanner *me = reinterpret_cast<WifiScanner *>(obj);
me->run();
pthread_exit(NULL);
return NULL;
}
void WifiScanner::threadCleanup(void *obj) {
WifiScanner *me = reinterpret_cast<WifiScanner *>(obj);
me->mWorkerRunning = false;
pthread_mutex_unlock(&me->mWorkerLock);
if (me->mAbortRequest) {
me->mAbortRequest = false;
pthread_mutex_unlock(&me->mAbortRequestLock);
}
}
int WifiScanner::stopPeriodicScan() {
pthread_mutex_lock(&mAbortRequestLock);
pthread_mutex_lock(&mWorkerLock);
if (mWorkerRunning)
mAbortRequest = true;
pthread_mutex_unlock(&mWorkerLock);
pthread_mutex_unlock(&mAbortRequestLock);
return 0;
}
void WifiScanner::run() {
LOGD("Thread started");
mWorkerRunning = true;
pthread_cleanup_push(WifiScanner::threadCleanup, this);
pthread_mutex_unlock(&mWorkerLock);
while(1) {
LOGD("Triggering periodic scan");
if (mSuppl->triggerScan(mActive)) {
LOGW("Error triggering scan (%s)", strerror(errno));
}
sleep(mPeriod);
pthread_mutex_lock(&mAbortRequestLock);
if (mAbortRequest) {
LOGD("Abort request!");
goto out;
}
pthread_mutex_unlock(&mAbortRequestLock);
}
out:
pthread_cleanup_pop(1);
pthread_mutex_unlock(&mWorkerLock);
}

36
nexus/WifiScanner.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef _WIFISCANNER_H
#define _WIFISCANNER_H
#include <pthread.h>
class Supplicant;
class WifiScanner {
pthread_t mWorker;
pthread_mutex_t mWorkerLock;
bool mWorkerRunning;
bool mAbortRequest;
pthread_mutex_t mAbortRequestLock;
Supplicant *mSuppl;
int mPeriod;
bool mActive;
public:
WifiScanner(Supplicant *suppl, int period);
virtual ~WifiScanner() {}
int getPeriod() { return mPeriod; }
int startPeriodicScan(bool active);
int stopPeriodicScan();
private:
static void *threadStart(void *obj);
static void threadCleanup(void *obj);
void run();
};
#endif

View file

@ -20,22 +20,44 @@
#include "cutils/log.h" #include "cutils/log.h"
#include "NetworkManager.h" #include "NetworkManager.h"
#include "CommandListener.h"
#include "LoopController.h"
#include "VpnController.h"
#include "TiwlanWifiController.h"
int main() { int main() {
NetworkManager *nm;
LOGI("Nexus version 0.1 firing up"); LOGI("Nexus version 0.1 firing up");
if (!(nm = new NetworkManager())) { CommandListener *cl = new CommandListener();
NetworkManager *nm;
if (!(nm = NetworkManager::Instance())) {
LOGE("Unable to create NetworkManager"); LOGE("Unable to create NetworkManager");
exit (-1); exit (-1);
}; };
if (nm->run()) { nm->setBroadcaster((SocketListener *) cl);
nm->attachController(new LoopController());
nm->attachController(new TiwlanWifiController("/system/lib/modules/wlan.ko", "wlan", ""));
nm->attachController(new VpnController());
if (NetworkManager::Instance()->run()) {
LOGE("Unable to Run NetworkManager (%s)", strerror(errno)); LOGE("Unable to Run NetworkManager (%s)", strerror(errno));
exit (1); exit (1);
} }
if (cl->startListener()) {
LOGE("Unable to start CommandListener (%s)", strerror(errno));
exit (1);
}
while(1) {
sleep(1000);
}
LOGI("Nexus exiting"); LOGI("Nexus exiting");
exit(0); exit(0);
} }

View file

@ -65,14 +65,13 @@ int main(int argc, char **argv) {
buffer[strlen(buffer) -1] = 0; buffer[strlen(buffer) -1] = 0;
printf("sending '%s'\n", buffer);
if (write(sock, buffer, strlen(buffer) +1) < 0) { if (write(sock, buffer, strlen(buffer) +1) < 0) {
fprintf(stderr, "Error writing data (%s)\n", strerror(errno)); fprintf(stderr, "Error writing data (%s)\n", strerror(errno));
exit(2); exit(2);
} }
wait: wait:
to.tv_sec = 5; to.tv_sec = 10;
to.tv_usec = 0; to.tv_usec = 0;
FD_ZERO(&read_fds); FD_ZERO(&read_fds);
FD_SET(sock, &read_fds); FD_SET(sock, &read_fds);
@ -88,12 +87,11 @@ wait:
printf("{response timeout}\n"); printf("{response timeout}\n");
continue; continue;
} else if (FD_ISSET(sock, &read_fds)) { } else if (FD_ISSET(sock, &read_fds)) {
printf("got data!\n"); if ((rc = read(sock, buffer, sizeof(buffer)-1)) <= 0) {
if ((rc = read(sock, buffer, sizeof(buffer)-1)) < 0) {
fprintf(stderr, "Error reading response (%s)\n", strerror(errno)); fprintf(stderr, "Error reading response (%s)\n", strerror(errno));
exit(2); exit(2);
} }
printf(" |%s|\n", buffer); printf(" %s\n", buffer);
goto wait; goto wait;
} }
} }