Merge change 2135 into donut
* changes: nexus: Switch controllers to use abstracted properties and refactor command protocol
This commit is contained in:
commit
4919d556f5
16 changed files with 306 additions and 281 deletions
|
|
@ -33,54 +33,19 @@
|
||||||
|
|
||||||
CommandListener::CommandListener() :
|
CommandListener::CommandListener() :
|
||||||
FrameworkListener("nexus") {
|
FrameworkListener("nexus") {
|
||||||
registerCmd(new WifiEnableCmd());
|
|
||||||
registerCmd(new WifiDisableCmd());
|
|
||||||
registerCmd(new WifiScanCmd());
|
|
||||||
registerCmd(new WifiScanResultsCmd());
|
registerCmd(new WifiScanResultsCmd());
|
||||||
registerCmd(new WifiListNetworksCmd());
|
registerCmd(new WifiListNetworksCmd());
|
||||||
registerCmd(new WifiAddNetworkCmd());
|
registerCmd(new WifiAddNetworkCmd());
|
||||||
registerCmd(new WifiRemoveNetworkCmd());
|
registerCmd(new WifiRemoveNetworkCmd());
|
||||||
registerCmd(new WifiSetVarCmd());
|
|
||||||
registerCmd(new WifiGetVarCmd());
|
|
||||||
|
|
||||||
registerCmd(new VpnEnableCmd());
|
registerCmd(new GetCmd());
|
||||||
registerCmd(new VpnSetVarCmd());
|
registerCmd(new SetCmd());
|
||||||
registerCmd(new VpnGetVarCmd());
|
|
||||||
registerCmd(new VpnDisableCmd());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------
|
/* -------------
|
||||||
* Wifi Commands
|
* Wifi Commands
|
||||||
* ------------ */
|
* ------------ */
|
||||||
|
|
||||||
CommandListener::WifiEnableCmd::WifiEnableCmd() :
|
|
||||||
NexusCommand("wifi_enable") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::WifiEnableCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
Controller *c = NetworkManager::Instance()->findController("WIFI");
|
|
||||||
|
|
||||||
if (c->enable())
|
|
||||||
cli->sendMsg(ErrorCode::OperationFailed, "Failed to enable wifi", true);
|
|
||||||
else
|
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "Wifi Enabled", false);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::WifiDisableCmd::WifiDisableCmd() :
|
|
||||||
NexusCommand("wifi_disable") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
Controller *c = NetworkManager::Instance()->findController("WIFI");
|
|
||||||
|
|
||||||
if (c->disable())
|
|
||||||
cli->sendMsg(ErrorCode::OperationFailed, "Failed to disable wifi", true);
|
|
||||||
else
|
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "Wifi Disabled", false);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::WifiAddNetworkCmd::WifiAddNetworkCmd() :
|
CommandListener::WifiAddNetworkCmd::WifiAddNetworkCmd() :
|
||||||
NexusCommand("wifi_add_network") {
|
NexusCommand("wifi_add_network") {
|
||||||
}
|
}
|
||||||
|
|
@ -116,21 +81,6 @@ int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *d
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandListener::WifiScanCmd::WifiScanCmd() :
|
|
||||||
NexusCommand("wifi_scan") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
|
|
||||||
|
|
||||||
if (wc->setScanMode(atoi(data)))
|
|
||||||
cli->sendMsg(ErrorCode::OperationFailed, "Failed to set scan mode", true);
|
|
||||||
else
|
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "Scan mode set", false);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
|
CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
|
||||||
NexusCommand("wifi_scan_results") {
|
NexusCommand("wifi_scan_results") {
|
||||||
}
|
}
|
||||||
|
|
@ -181,158 +131,39 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandListener::WifiSetVarCmd::WifiSetVarCmd() :
|
|
||||||
NexusCommand("wifi_setvar") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::WifiSetVarCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
|
|
||||||
|
|
||||||
char *bword;
|
|
||||||
char *last;
|
|
||||||
char varname[32];
|
|
||||||
char val[250];
|
|
||||||
int networkId;
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(data, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
networkId = atoi(bword);
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(NULL, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
strncpy(varname, bword, sizeof(varname));
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(NULL, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
strncpy(val, bword, sizeof(val));
|
|
||||||
|
|
||||||
LOGD("Network id %d, varname '%s', value '%s'", networkId, varname, val);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
out_inval:
|
|
||||||
errno = EINVAL;
|
|
||||||
cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set variable.", true);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::WifiGetVarCmd::WifiGetVarCmd() :
|
|
||||||
NexusCommand("wifi_getvar") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::WifiGetVarCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
|
|
||||||
|
|
||||||
char *bword;
|
|
||||||
char *last;
|
|
||||||
char varname[32];
|
|
||||||
int networkId;
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(data, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
networkId = atoi(bword);
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(NULL, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
strncpy(varname, bword, sizeof(varname));
|
|
||||||
|
|
||||||
LOGD("networkId = %d, varname '%s'", networkId, varname);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
out_inval:
|
|
||||||
errno = EINVAL;
|
|
||||||
cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------
|
/* ------------
|
||||||
* Vpn Commands
|
* Vpn Commands
|
||||||
* ------------ */
|
* ------------ */
|
||||||
CommandListener::VpnEnableCmd::VpnEnableCmd() :
|
|
||||||
NexusCommand("vpn_enable") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
Controller *c = NetworkManager::Instance()->findController("VPN");
|
|
||||||
|
|
||||||
if (c->enable())
|
/* ----------------
|
||||||
cli->sendMsg(ErrorCode::OperationFailed, "Failed to enable VPN", true);
|
* Generic Commands
|
||||||
else
|
* ---------------- */
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "VPN enabled", false);
|
CommandListener::GetCmd::GetCmd() :
|
||||||
return 0;
|
NexusCommand("get") {
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::VpnSetVarCmd::VpnSetVarCmd() :
|
|
||||||
NexusCommand("vpn_setvar") {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CommandListener::VpnSetVarCmd::runCommand(SocketClient *cli, char *data) {
|
int CommandListener::GetCmd::runCommand(SocketClient *cli, char *data) {
|
||||||
VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN");
|
|
||||||
|
|
||||||
char *bword;
|
char *bword;
|
||||||
char *last;
|
char *last;
|
||||||
char varname[32];
|
char propname[32];
|
||||||
char val[250];
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(data, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
strncpy(varname, bword, sizeof(varname));
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(NULL, ":", &last)))
|
|
||||||
goto out_inval;
|
|
||||||
|
|
||||||
strncpy(val, bword, sizeof(val));
|
|
||||||
|
|
||||||
if (!strcasecmp(varname, "vpn_gateway")) {
|
|
||||||
if (vc->setVpnGateway(val))
|
|
||||||
goto out_inval;
|
|
||||||
} else {
|
|
||||||
cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "Variable written.", false);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
out_inval:
|
|
||||||
errno = EINVAL;
|
|
||||||
cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set variable.", true);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandListener::VpnGetVarCmd::VpnGetVarCmd() :
|
|
||||||
NexusCommand("vpn_getvar") {
|
|
||||||
}
|
|
||||||
|
|
||||||
int CommandListener::VpnGetVarCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN");
|
|
||||||
|
|
||||||
char *bword;
|
|
||||||
char *last;
|
|
||||||
char varname[32];
|
|
||||||
|
|
||||||
if (!(bword = strtok_r(data, ":", &last)))
|
if (!(bword = strtok_r(data, ":", &last)))
|
||||||
goto out_inval;
|
goto out_inval;
|
||||||
|
|
||||||
strncpy(varname, bword, sizeof(varname));
|
strncpy(propname, bword, sizeof(propname));
|
||||||
|
|
||||||
if (!strcasecmp(varname, "vpn_gateway")) {
|
char pb[255];
|
||||||
char buffer[255];
|
snprintf(pb, sizeof(pb), "%s:", propname);
|
||||||
|
|
||||||
sprintf(buffer, "%s:%s", varname, inet_ntoa(vc->getVpnGateway()));
|
if (!NetworkManager::Instance()->getProperty(propname,
|
||||||
cli->sendMsg(ErrorCode::VariableRead, buffer, false);
|
&pb[strlen(pb)],
|
||||||
} else {
|
sizeof(pb) - strlen(pb))) {
|
||||||
cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true);
|
goto out_inval;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "Variable read.", false);
|
cli->sendMsg(ErrorCode::VariableRead, pb, false);
|
||||||
|
|
||||||
|
cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false);
|
||||||
return 0;
|
return 0;
|
||||||
out_inval:
|
out_inval:
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
|
|
@ -340,16 +171,34 @@ out_inval:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandListener::VpnDisableCmd::VpnDisableCmd() :
|
CommandListener::SetCmd::SetCmd() :
|
||||||
NexusCommand("vpn_disable") {
|
NexusCommand("set") {
|
||||||
}
|
}
|
||||||
|
|
||||||
int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) {
|
|
||||||
Controller *c = NetworkManager::Instance()->findController("VPN");
|
|
||||||
|
|
||||||
if (c->disable())
|
int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {
|
||||||
cli->sendMsg(ErrorCode::OperationFailed, "Failed to disable VPN", true);
|
char *bword;
|
||||||
else
|
char *last;
|
||||||
cli->sendMsg(ErrorCode::CommandOkay, "VPN disabled", false);
|
char propname[32];
|
||||||
|
char propval[250];
|
||||||
|
|
||||||
|
if (!(bword = strtok_r(data, ":", &last)))
|
||||||
|
goto out_inval;
|
||||||
|
|
||||||
|
strncpy(propname, bword, sizeof(propname));
|
||||||
|
|
||||||
|
if (!(bword = strtok_r(NULL, ":", &last)))
|
||||||
|
goto out_inval;
|
||||||
|
|
||||||
|
strncpy(propval, bword, sizeof(propval));
|
||||||
|
|
||||||
|
if (NetworkManager::Instance()->setProperty(propname, propval))
|
||||||
|
goto out_inval;
|
||||||
|
|
||||||
|
cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
out_inval:
|
||||||
|
errno = EINVAL;
|
||||||
|
cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set property.", true);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,19 +25,6 @@ public:
|
||||||
virtual ~CommandListener() {}
|
virtual ~CommandListener() {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class WifiEnableCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
WifiEnableCmd();
|
|
||||||
virtual ~WifiEnableCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
class WifiDisableCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
WifiDisableCmd();
|
|
||||||
virtual ~WifiDisableCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
class WifiScanCmd : public NexusCommand {
|
class WifiScanCmd : public NexusCommand {
|
||||||
public:
|
public:
|
||||||
|
|
@ -74,48 +61,19 @@ private:
|
||||||
int runCommand(SocketClient *c, char *data);
|
int runCommand(SocketClient *c, char *data);
|
||||||
};
|
};
|
||||||
|
|
||||||
class WifiSetVarCmd : public NexusCommand {
|
class SetCmd : public NexusCommand {
|
||||||
public:
|
public:
|
||||||
WifiSetVarCmd();
|
SetCmd();
|
||||||
virtual ~WifiSetVarCmd() {}
|
virtual ~SetCmd() {}
|
||||||
int runCommand(SocketClient *c, char *data);
|
int runCommand(SocketClient *c, char *data);
|
||||||
};
|
};
|
||||||
|
|
||||||
class WifiGetVarCmd : public NexusCommand {
|
class GetCmd : public NexusCommand {
|
||||||
public:
|
public:
|
||||||
WifiGetVarCmd();
|
GetCmd();
|
||||||
virtual ~WifiGetVarCmd() {}
|
virtual ~GetCmd() {}
|
||||||
int runCommand(SocketClient *c, char *data);
|
int runCommand(SocketClient *c, char *data);
|
||||||
};
|
};
|
||||||
|
|
||||||
class VpnEnableCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
VpnEnableCmd();
|
|
||||||
virtual ~VpnEnableCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
class VpnSetVarCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
VpnSetVarCmd();
|
|
||||||
virtual ~VpnSetVarCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
class VpnGetVarCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
VpnGetVarCmd();
|
|
||||||
virtual ~VpnGetVarCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
class VpnDisableCmd : public NexusCommand {
|
|
||||||
public:
|
|
||||||
VpnDisableCmd();
|
|
||||||
virtual ~VpnDisableCmd() {}
|
|
||||||
int runCommand(SocketClient *c, char *data);
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
@ -32,8 +33,13 @@
|
||||||
extern "C" int init_module(void *, unsigned int, const char *);
|
extern "C" int init_module(void *, unsigned int, const char *);
|
||||||
extern "C" int delete_module(const char *, unsigned int);
|
extern "C" int delete_module(const char *, unsigned int);
|
||||||
|
|
||||||
Controller::Controller(const char *name) {
|
Controller::Controller(const char *name, const char *prefix) {
|
||||||
mName = name;
|
mName = name;
|
||||||
|
mPropertyPrefix = prefix;
|
||||||
|
mProperties = new PropertyCollection();
|
||||||
|
|
||||||
|
mEnabled = false;
|
||||||
|
registerProperty("enable");
|
||||||
}
|
}
|
||||||
|
|
||||||
int Controller::start() {
|
int Controller::start() {
|
||||||
|
|
@ -44,12 +50,69 @@ int Controller::stop() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PropertyCollection & Controller::getProperties() {
|
||||||
|
return *mProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Controller::setProperty(const char *name, char *value) {
|
||||||
|
if (!strcmp(name, "enable")) {
|
||||||
|
int en = atoi(value);
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = (en ? enable() : disable());
|
||||||
|
|
||||||
|
if (!rc)
|
||||||
|
mEnabled = en;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *Controller::getProperty(const char *name, char *buffer, size_t maxsize) {
|
||||||
|
if (!strcmp(name, "enable")) {
|
||||||
|
snprintf(buffer, maxsize, "%d", mEnabled);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Controller::registerProperty(const char *name) {
|
||||||
|
PropertyCollection::iterator it;
|
||||||
|
|
||||||
|
for (it = mProperties->begin(); it != mProperties->end(); ++it) {
|
||||||
|
if (!strcmp(name, (*it))) {
|
||||||
|
errno = EADDRINUSE;
|
||||||
|
LOGE("Failed to register property (%s)", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mProperties->push_back(name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Controller::unregisterProperty(const char *name) {
|
||||||
|
PropertyCollection::iterator it;
|
||||||
|
|
||||||
|
for (it = mProperties->begin(); it != mProperties->end(); ++it) {
|
||||||
|
if (!strcmp(name, (*it))) {
|
||||||
|
mProperties->erase(it);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int Controller::loadKernelModule(char *modpath, const char *args) {
|
int Controller::loadKernelModule(char *modpath, const char *args) {
|
||||||
void *module;
|
void *module;
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
|
|
||||||
LOGD("loadKernelModule(%s, %s)", modpath, args);
|
|
||||||
|
|
||||||
module = loadFile(modpath, &size);
|
module = loadFile(modpath, &size);
|
||||||
if (!module) {
|
if (!module) {
|
||||||
errno = -EIO;
|
errno = -EIO;
|
||||||
|
|
@ -65,7 +128,6 @@ int Controller::unloadKernelModule(const char *modtag) {
|
||||||
int rc = -1;
|
int rc = -1;
|
||||||
int retries = 10;
|
int retries = 10;
|
||||||
|
|
||||||
LOGD("unloadKernelModule(%s)", modtag);
|
|
||||||
while (retries--) {
|
while (retries--) {
|
||||||
rc = delete_module(modtag, O_NONBLOCK | O_EXCL);
|
rc = delete_module(modtag, O_NONBLOCK | O_EXCL);
|
||||||
if (rc < 0 && errno == EAGAIN)
|
if (rc < 0 && errno == EAGAIN)
|
||||||
|
|
@ -107,7 +169,6 @@ bool Controller::isKernelModuleLoaded(const char *modtag) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *Controller::loadFile(char *filename, unsigned int *_size)
|
void *Controller::loadFile(char *filename, unsigned int *_size)
|
||||||
{
|
{
|
||||||
int ret, fd;
|
int ret, fd;
|
||||||
|
|
|
||||||
|
|
@ -16,31 +16,48 @@
|
||||||
#ifndef _CONTROLLER_H
|
#ifndef _CONTROLLER_H
|
||||||
#define _CONTROLLER_H
|
#define _CONTROLLER_H
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "../../../frameworks/base/include/utils/List.h"
|
#include "../../../frameworks/base/include/utils/List.h"
|
||||||
|
|
||||||
|
#include "PropertyCollection.h"
|
||||||
|
|
||||||
class Controller {
|
class Controller {
|
||||||
private:
|
private:
|
||||||
const char *mName;
|
const char *mName;
|
||||||
|
const char *mPropertyPrefix;
|
||||||
|
PropertyCollection *mProperties;
|
||||||
|
bool mEnabled;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Controller(const char *name);
|
Controller(const char *name, const char *prefix);
|
||||||
virtual ~Controller() {}
|
virtual ~Controller() {}
|
||||||
|
|
||||||
virtual int start();
|
virtual int start();
|
||||||
virtual int stop();
|
virtual int stop();
|
||||||
|
|
||||||
virtual int enable() = 0;
|
virtual const PropertyCollection &getProperties();
|
||||||
virtual int disable() = 0;
|
virtual int setProperty(const char *name, char *value);
|
||||||
|
virtual const char *getProperty(const char *name, char *buffer, size_t maxsize);
|
||||||
|
|
||||||
virtual const char *getName() { return mName; }
|
const char *getName() { return mName; }
|
||||||
|
const char *getPropertyPrefix() { return mPropertyPrefix; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int loadKernelModule(char *modpath, const char *args);
|
int loadKernelModule(char *modpath, const char *args);
|
||||||
bool isKernelModuleLoaded(const char *modtag);
|
bool isKernelModuleLoaded(const char *modtag);
|
||||||
int unloadKernelModule(const char *modtag);
|
int unloadKernelModule(const char *modtag);
|
||||||
|
|
||||||
|
int registerProperty(const char *name);
|
||||||
|
int unregisterProperty(const char *name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void *loadFile(char *filename, unsigned int *_size);
|
void *loadFile(char *filename, unsigned int *_size);
|
||||||
|
|
||||||
|
virtual int enable() = 0;
|
||||||
|
virtual int disable() = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef android::List<Controller *> ControllerCollection;
|
typedef android::List<Controller *> ControllerCollection;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
#include "LoopController.h"
|
#include "LoopController.h"
|
||||||
|
|
||||||
LoopController::LoopController() :
|
LoopController::LoopController() :
|
||||||
Controller("LOOP") {
|
Controller("LOOP", "loop") {
|
||||||
}
|
}
|
||||||
|
|
||||||
int LoopController::enable() {
|
int LoopController::enable() {
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public:
|
||||||
LoopController();
|
LoopController();
|
||||||
virtual ~LoopController() {}
|
virtual ~LoopController() {}
|
||||||
|
|
||||||
|
private:
|
||||||
int enable();
|
int enable();
|
||||||
int disable();
|
int disable();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,73 @@ Controller *NetworkManager::findController(const char *name) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int NetworkManager::setProperty(const char *name, char *value) {
|
||||||
|
char *tmp = strdup(name);
|
||||||
|
char *next = tmp;
|
||||||
|
char *prefix;
|
||||||
|
char *rest;
|
||||||
|
ControllerCollection::iterator it;
|
||||||
|
|
||||||
|
if (!(prefix = strsep(&next, ".")))
|
||||||
|
goto out_inval;
|
||||||
|
|
||||||
|
rest = next;
|
||||||
|
|
||||||
|
if (!strncasecmp(prefix, "netman", 6)) {
|
||||||
|
errno = ENOSYS;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (it = mControllers->begin(); it != mControllers->end(); ++it) {
|
||||||
|
if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
|
||||||
|
return (*it)->setProperty(rest, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
out_inval:
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *NetworkManager::getProperty(const char *name, char *buffer,
|
||||||
|
size_t maxsize) {
|
||||||
|
char *tmp = strdup(name);
|
||||||
|
char *next = tmp;
|
||||||
|
char *prefix;
|
||||||
|
char *rest;
|
||||||
|
ControllerCollection::iterator it;
|
||||||
|
|
||||||
|
if (!(prefix = strsep(&next, ".")))
|
||||||
|
goto out_inval;
|
||||||
|
|
||||||
|
rest = next;
|
||||||
|
|
||||||
|
if (!strncasecmp(prefix, "netman", 6)) {
|
||||||
|
errno = ENOSYS;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (it = mControllers->begin(); it != mControllers->end(); ++it) {
|
||||||
|
if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
|
||||||
|
return (*it)->getProperty(rest, buffer, maxsize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
out_inval:
|
||||||
|
errno = EINVAL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PropertyCollection &NetworkManager::getProperties() {
|
||||||
|
return *mProperties;
|
||||||
|
}
|
||||||
|
|
||||||
int NetworkManager::onInterfaceCreated(Controller *c, char *name) {
|
int NetworkManager::onInterfaceCreated(Controller *c, char *name) {
|
||||||
LOGD("Interface %s created by controller %s", name, c->getName());
|
LOGD("Interface %s created by controller %s", name, c->getName());
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
#include <sysutils/SocketListener.h>
|
#include <sysutils/SocketListener.h>
|
||||||
|
|
||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
|
#include "PropertyCollection.h"
|
||||||
|
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
private:
|
private:
|
||||||
|
|
@ -27,6 +28,7 @@ private:
|
||||||
private:
|
private:
|
||||||
ControllerCollection *mControllers;
|
ControllerCollection *mControllers;
|
||||||
SocketListener *mBroadcaster;
|
SocketListener *mBroadcaster;
|
||||||
|
PropertyCollection *mProperties;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~NetworkManager() {}
|
virtual ~NetworkManager() {}
|
||||||
|
|
@ -37,6 +39,10 @@ public:
|
||||||
|
|
||||||
Controller *findController(const char *name);
|
Controller *findController(const char *name);
|
||||||
|
|
||||||
|
const PropertyCollection &getProperties();
|
||||||
|
int setProperty(const char *name, char *value);
|
||||||
|
const char *getProperty(const char *name, char *buffer, size_t maxsize);
|
||||||
|
|
||||||
void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
|
void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
|
||||||
SocketListener *getBroadcaster() { return mBroadcaster; }
|
SocketListener *getBroadcaster() { return mBroadcaster; }
|
||||||
|
|
||||||
|
|
@ -45,6 +51,9 @@ public:
|
||||||
private:
|
private:
|
||||||
int startControllers();
|
int startControllers();
|
||||||
int stopControllers();
|
int stopControllers();
|
||||||
|
int registerProperty(const char *name);
|
||||||
|
int unregisterProperty(const char *name);
|
||||||
|
|
||||||
NetworkManager();
|
NetworkManager();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,6 @@ int OpenVpnController::enable() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int OpenVpnController::disable() {
|
int OpenVpnController::disable() {
|
||||||
|
|
||||||
if (mServiceManager->stop("openvpn"))
|
if (mServiceManager->stop("openvpn"))
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ public:
|
||||||
|
|
||||||
int start();
|
int start();
|
||||||
int stop();
|
int stop();
|
||||||
|
|
||||||
|
private:
|
||||||
int enable();
|
int enable();
|
||||||
int disable();
|
int disable();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
25
nexus/PropertyCollection.h
Normal file
25
nexus/PropertyCollection.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* 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 _PROPERTY_COLLECTION_H
|
||||||
|
#define _PROPERTY_COLLECTION_H
|
||||||
|
|
||||||
|
#include "../../../frameworks/base/include/utils/List.h"
|
||||||
|
|
||||||
|
typedef android::List<const char *> PropertyCollection;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
@ -23,7 +24,8 @@
|
||||||
#include "VpnController.h"
|
#include "VpnController.h"
|
||||||
|
|
||||||
VpnController::VpnController() :
|
VpnController::VpnController() :
|
||||||
Controller("VPN") {
|
Controller("VPN", "vpn") {
|
||||||
|
registerProperty("gateway");
|
||||||
}
|
}
|
||||||
|
|
||||||
int VpnController::start() {
|
int VpnController::start() {
|
||||||
|
|
@ -46,15 +48,23 @@ int VpnController::disable() {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int VpnController::setVpnGateway(const char *vpnGw) {
|
int VpnController::setProperty(const char *name, char *value) {
|
||||||
if (!inet_aton(vpnGw, &mVpnGateway)) {
|
if (!strcmp(name, "gateway")) {
|
||||||
errno = EINVAL;
|
if (!inet_aton(value, &mVpnGateway)) {
|
||||||
return -1;
|
errno = EINVAL;
|
||||||
}
|
return -1;
|
||||||
return 0;
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Controller::setProperty(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int VpnController::setVpnGateway(struct in_addr *vpnGw) {
|
const char *VpnController::getProperty(const char *name, char *buffer, size_t maxsize) {
|
||||||
memcpy(&mVpnGateway, vpnGw, sizeof(struct in_addr));
|
if (!strcmp(name, "gateway")) {
|
||||||
return 0;
|
snprintf(buffer, maxsize, "%s", inet_ntoa(mVpnGateway));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Controller::getProperty(name, buffer, maxsize);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,14 @@ public:
|
||||||
virtual int start();
|
virtual int start();
|
||||||
virtual int stop();
|
virtual int stop();
|
||||||
|
|
||||||
|
virtual int setProperty(const char *name, char *value);
|
||||||
|
virtual const char *getProperty(const char *name, char *buffer,
|
||||||
|
size_t maxlen);
|
||||||
|
|
||||||
|
private:
|
||||||
virtual int enable();
|
virtual int enable();
|
||||||
virtual int disable();
|
virtual int disable();
|
||||||
|
|
||||||
struct in_addr &getVpnGateway() { return mVpnGateway; }
|
|
||||||
int setVpnGateway(const char *vpnGw);
|
|
||||||
int setVpnGateway(struct in_addr *vpnGw);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -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 <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
@ -26,7 +28,7 @@
|
||||||
#include "ErrorCode.h"
|
#include "ErrorCode.h"
|
||||||
|
|
||||||
WifiController::WifiController(char *modpath, char *modname, char *modargs) :
|
WifiController::WifiController(char *modpath, char *modname, char *modargs) :
|
||||||
Controller("WIFI") {
|
Controller("WIFI", "wifi") {
|
||||||
strncpy(mModulePath, modpath, sizeof(mModulePath));
|
strncpy(mModulePath, modpath, sizeof(mModulePath));
|
||||||
strncpy(mModuleName, modname, sizeof(mModuleName));
|
strncpy(mModuleName, modname, sizeof(mModuleName));
|
||||||
strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
|
strncpy(mModuleArgs, modargs, sizeof(mModuleArgs));
|
||||||
|
|
@ -34,6 +36,8 @@ WifiController::WifiController(char *modpath, char *modname, char *modargs) :
|
||||||
mSupplicant = new Supplicant();
|
mSupplicant = new Supplicant();
|
||||||
mScanner = new WifiScanner(mSupplicant, 10);
|
mScanner = new WifiScanner(mSupplicant, 10);
|
||||||
mCurrentScanMode = 0;
|
mCurrentScanMode = 0;
|
||||||
|
|
||||||
|
registerProperty("scanmode");
|
||||||
}
|
}
|
||||||
|
|
||||||
int WifiController::start() {
|
int WifiController::start() {
|
||||||
|
|
@ -94,7 +98,7 @@ out_powerdown:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiController::sendStatusBroadcast(char *msg) {
|
void WifiController::sendStatusBroadcast(const char *msg) {
|
||||||
NetworkManager::Instance()->
|
NetworkManager::Instance()->
|
||||||
getBroadcaster()->
|
getBroadcaster()->
|
||||||
sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false);
|
sendBroadcast(ErrorCode::UnsolicitedInformational, msg, false);
|
||||||
|
|
@ -167,3 +171,20 @@ ScanResultCollection *WifiController::createScanResults() {
|
||||||
WifiNetworkCollection *WifiController::createNetworkList() {
|
WifiNetworkCollection *WifiController::createNetworkList() {
|
||||||
return mSupplicant->createNetworkList();
|
return mSupplicant->createNetworkList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int WifiController::setProperty(const char *name, char *value) {
|
||||||
|
if (!strcmp(name, "scanmode"))
|
||||||
|
return setScanMode((uint32_t) strtoul(value, NULL, 0));
|
||||||
|
|
||||||
|
return Controller::setProperty(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *WifiController::getProperty(const char *name, char *buffer, size_t maxsize) {
|
||||||
|
if (!strcmp(name, "scanmode")) {
|
||||||
|
snprintf(buffer, maxsize, "0x%.8x", mCurrentScanMode);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Controller::getProperty(name, buffer, maxsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,15 +54,14 @@ public:
|
||||||
int start();
|
int start();
|
||||||
int stop();
|
int stop();
|
||||||
|
|
||||||
int enable();
|
|
||||||
int disable();
|
|
||||||
|
|
||||||
int addNetwork();
|
int addNetwork();
|
||||||
int removeNetwork(int networkId);
|
int removeNetwork(int networkId);
|
||||||
WifiNetworkCollection *createNetworkList();
|
WifiNetworkCollection *createNetworkList();
|
||||||
|
|
||||||
int getScanMode() { return mCurrentScanMode; }
|
virtual int setProperty(const char *name, char *value);
|
||||||
int setScanMode(uint32_t mode);
|
virtual const char *getProperty(const char *name, char *buffer,
|
||||||
|
size_t maxlen);
|
||||||
|
|
||||||
ScanResultCollection *createScanResults();
|
ScanResultCollection *createScanResults();
|
||||||
|
|
||||||
char *getModulePath() { return mModulePath; }
|
char *getModulePath() { return mModulePath; }
|
||||||
|
|
@ -79,7 +78,13 @@ protected:
|
||||||
virtual bool isFirmwareLoaded() = 0;
|
virtual bool isFirmwareLoaded() = 0;
|
||||||
virtual bool isPoweredUp() = 0;
|
virtual bool isPoweredUp() = 0;
|
||||||
|
|
||||||
void sendStatusBroadcast(char *msg);
|
void sendStatusBroadcast(const char *msg);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int setScanMode(uint32_t mode);
|
||||||
|
int enable();
|
||||||
|
int disable();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ void WifiScanner::run() {
|
||||||
struct timeval to;
|
struct timeval to;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
to.tv_sec = 0;
|
to.tv_usec = 0;
|
||||||
to.tv_sec = mPeriod;
|
to.tv_sec = mPeriod;
|
||||||
|
|
||||||
FD_ZERO(&read_fds);
|
FD_ZERO(&read_fds);
|
||||||
|
|
@ -83,4 +83,5 @@ void WifiScanner::run() {
|
||||||
} else if (FD_ISSET(mCtrlPipe[0], &read_fds))
|
} else if (FD_ISSET(mCtrlPipe[0], &read_fds))
|
||||||
break;
|
break;
|
||||||
} // while
|
} // while
|
||||||
|
LOGD("Stopping wifi scanner");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue