Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4919d556 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 2135 into donut

* changes:
  nexus: Switch controllers to use abstracted properties and refactor command protocol
parents 47c73898 4876567c
Loading
Loading
Loading
Loading
+33 −184
Original line number Diff line number Diff line
@@ -33,54 +33,19 @@

CommandListener::CommandListener() :
                 FrameworkListener("nexus") {
    registerCmd(new WifiEnableCmd());
    registerCmd(new WifiDisableCmd());
    registerCmd(new WifiScanCmd());
    registerCmd(new WifiScanResultsCmd());
    registerCmd(new WifiListNetworksCmd());
    registerCmd(new WifiAddNetworkCmd());
    registerCmd(new WifiRemoveNetworkCmd());
    registerCmd(new WifiSetVarCmd());
    registerCmd(new WifiGetVarCmd());

    registerCmd(new VpnEnableCmd());
    registerCmd(new VpnSetVarCmd());
    registerCmd(new VpnGetVarCmd());
    registerCmd(new VpnDisableCmd());
    registerCmd(new GetCmd());
    registerCmd(new SetCmd());
}
 
/* -------------
 * 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() :
                 NexusCommand("wifi_add_network") {
} 
@@ -116,21 +81,6 @@ int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *d
    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() :
                 NexusCommand("wifi_scan_results") {
} 
@@ -181,68 +131,39 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da
    return 0;
}

CommandListener::WifiSetVarCmd::WifiSetVarCmd() :
                 NexusCommand("wifi_setvar") {
} 
/* ------------
 * Vpn Commands
 * ------------ */

int CommandListener::WifiSetVarCmd::runCommand(SocketClient *cli, char *data) {
    WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
/* ----------------
 * Generic Commands
 * ---------------- */
CommandListener::GetCmd::GetCmd() :
                 NexusCommand("get") {
} 

int CommandListener::GetCmd::runCommand(SocketClient *cli, char *data) {
    char *bword;
    char *last;
    char varname[32];
    char val[250];
    int networkId;
    char propname[32];

    if (!(bword = strtok_r(data, ":", &last)))
        goto out_inval;
   
    networkId = atoi(bword);
   
    if (!(bword = strtok_r(NULL, ":", &last)))
        goto out_inval;
    strncpy(propname, bword, sizeof(propname));

    strncpy(varname, bword, sizeof(varname));
    char pb[255];
    snprintf(pb, sizeof(pb), "%s:", propname);

    if (!(bword = strtok_r(NULL, ":", &last)))
    if (!NetworkManager::Instance()->getProperty(propname,
                                                 &pb[strlen(pb)],
                                                 sizeof(pb) - strlen(pb))) {
        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);
    cli->sendMsg(ErrorCode::VariableRead, pb, false);

    cli->sendMsg(ErrorCode::CommandOkay, "Property read.", false);
    return 0;
out_inval:
    errno = EINVAL;
@@ -250,106 +171,34 @@ out_inval:
    return 0;
}

/* ------------
 * Vpn Commands
 * ------------ */
CommandListener::VpnEnableCmd::VpnEnableCmd() :
                 NexusCommand("vpn_enable") {
CommandListener::SetCmd::SetCmd() :
                 NexusCommand("set") {
}

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);
    else
        cli->sendMsg(ErrorCode::CommandOkay, "VPN enabled", false);
    return 0;
}

CommandListener::VpnSetVarCmd::VpnSetVarCmd() :
                 NexusCommand("vpn_setvar") {
} 

int CommandListener::VpnSetVarCmd::runCommand(SocketClient *cli, char *data) {
    VpnController *vc = (VpnController *) NetworkManager::Instance()->findController("VPN");

int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {
    char *bword;
    char *last;
    char varname[32];
    char val[250];
    char propname[32];
    char propval[250];

    if (!(bword = strtok_r(data, ":", &last)))
        goto out_inval;

    strncpy(varname, bword, sizeof(varname));
    strncpy(propname, bword, sizeof(propname));

    if (!(bword = strtok_r(NULL, ":", &last)))
        goto out_inval;

    strncpy(val, bword, sizeof(val));
    strncpy(propval, bword, sizeof(propval));

    if (!strcasecmp(varname, "vpn_gateway")) {
        if (vc->setVpnGateway(val))
    if (NetworkManager::Instance()->setProperty(propname, propval))
        goto out_inval;
    } else {
        cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true);
        return 0;
    }

    cli->sendMsg(ErrorCode::CommandOkay, "Variable written.", false);
    cli->sendMsg(ErrorCode::CommandOkay, "Property set.", 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)))
        goto out_inval;
   
    strncpy(varname, bword, sizeof(varname));

    if (!strcasecmp(varname, "vpn_gateway")) {
        char buffer[255];

        sprintf(buffer, "%s:%s", varname, inet_ntoa(vc->getVpnGateway()));
        cli->sendMsg(ErrorCode::VariableRead, buffer, false);
    } else {
        cli->sendMsg(ErrorCode::CommandParameterError, "Variable not found.", true);
        return 0;
    }

    cli->sendMsg(ErrorCode::CommandOkay, "Variable read.", false);
    return 0;
out_inval:
    errno = EINVAL;
    cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true);
    return 0;
}

CommandListener::VpnDisableCmd::VpnDisableCmd() :
                 NexusCommand("vpn_disable") {
} 
               
int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) {
    Controller *c = NetworkManager::Instance()->findController("VPN");

    if (c->disable())
        cli->sendMsg(ErrorCode::OperationFailed, "Failed to disable VPN", true);
    else
        cli->sendMsg(ErrorCode::CommandOkay, "VPN disabled", false);
    cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set property.", true);
    return 0;
}
+6 −48
Original line number Diff line number Diff line
@@ -25,19 +25,6 @@ public:
    virtual ~CommandListener() {}

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 {
    public:
@@ -74,48 +61,19 @@ private:
        int runCommand(SocketClient *c, char *data);
    };

    class WifiSetVarCmd : public NexusCommand {
    public:
        WifiSetVarCmd();
        virtual ~WifiSetVarCmd() {}
        int runCommand(SocketClient *c, char *data);
    };

    class WifiGetVarCmd : public NexusCommand {
    public:
        WifiGetVarCmd();
        virtual ~WifiGetVarCmd() {}
        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 {
    class SetCmd : public NexusCommand {
    public:
        VpnGetVarCmd();
        virtual ~VpnGetVarCmd() {}
        SetCmd();
        virtual ~SetCmd() {}
        int runCommand(SocketClient *c, char *data);
    };

    class VpnDisableCmd : public NexusCommand {
    class GetCmd : public NexusCommand {
    public:
        VpnDisableCmd();
        virtual ~VpnDisableCmd() {}
        GetCmd();
        virtual ~GetCmd() {}
        int runCommand(SocketClient *c, char *data);
    };

};

#endif
+66 −5
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
@@ -32,8 +33,13 @@
extern "C" int init_module(void *, unsigned int, const char *);
extern "C" int delete_module(const char *, unsigned int);

Controller::Controller(const char *name) {
Controller::Controller(const char *name, const char *prefix) {
    mName = name;
    mPropertyPrefix = prefix;
    mProperties = new PropertyCollection();

    mEnabled = false;
    registerProperty("enable");
}

int Controller::start() {
@@ -44,12 +50,69 @@ int Controller::stop() {
    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) {
    void *module;
    unsigned int size;

    LOGD("loadKernelModule(%s, %s)", modpath, args);

    module = loadFile(modpath, &size);
    if (!module) {
        errno = -EIO;
@@ -65,7 +128,6 @@ int Controller::unloadKernelModule(const char *modtag) {
    int rc = -1;
    int retries = 10;

    LOGD("unloadKernelModule(%s)", modtag);
    while (retries--) {
        rc = delete_module(modtag, O_NONBLOCK | O_EXCL);
        if (rc < 0 && errno == EAGAIN)
@@ -107,7 +169,6 @@ bool Controller::isKernelModuleLoaded(const char *modtag) {
    return false;
}


void *Controller::loadFile(char *filename, unsigned int *_size)
{
	int ret, fd;
+21 −4
Original line number Diff line number Diff line
@@ -16,31 +16,48 @@
#ifndef _CONTROLLER_H
#define _CONTROLLER_H

#include <unistd.h>
#include <sys/types.h>

#include "../../../frameworks/base/include/utils/List.h"

#include "PropertyCollection.h"

class Controller {
private:
    const char *mName;
    const char *mPropertyPrefix;
    PropertyCollection *mProperties;
    bool mEnabled;
    
public:
    Controller(const char *name);
    Controller(const char *name, const char *prefix);
    virtual ~Controller() {}

    virtual int start();
    virtual int stop();

    virtual int enable() = 0;
    virtual int disable() = 0;
    virtual const PropertyCollection &getProperties();
    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:
    int loadKernelModule(char *modpath, const char *args);
    bool isKernelModuleLoaded(const char *modtag);
    int unloadKernelModule(const char *modtag);

    int registerProperty(const char *name);
    int unregisterProperty(const char *name);

private:
    void *loadFile(char *filename, unsigned int *_size);

    virtual int enable() = 0;
    virtual int disable() = 0;

};

typedef android::List<Controller *> ControllerCollection;
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
#include "LoopController.h"

LoopController::LoopController() :
                Controller("LOOP") {
                Controller("LOOP", "loop") {
}

int LoopController::enable() {
Loading