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

Commit cbc92d01 authored by San Mehat's avatar San Mehat Committed by The Android Open Source Project
Browse files

am 3c5a6f0b: nexus: Refactor some of the create/remove network path and add...

am 3c5a6f0b: nexus: Refactor some of the create/remove network path and add code for        retrieving network lists from supplicant nexus: Rework properties nexus: Implement wifi network enable/disable and add some error checking nexus: Add some TODOs nexus: Whitespace cleanup nexus: Add bindings between controllers and network interfaces nexus: Add properties for InterfaceConfig nexus: Fix a few conversion bugs in InterfaceConfig

Merge commit '3c5a6f0b'

* commit '3c5a6f0b':
  nexus: Refactor some of the create/remove network path and add code for
parents 9b8c28ab 3c5a6f0b
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -23,10 +23,12 @@ LOCAL_SRC_FILES:= \
                  WifiNetwork.cpp          \
                  OpenVpnController.cpp    \
                  InterfaceConfig.cpp      \
                  PropertyManager.cpp      \
                  SupplicantState.cpp 

LOCAL_MODULE:= nexus

LOCAL_C_INCLUDES := $(KERNEL_HEADERS)
LOCAL_C_INCLUDES := $(KERNEL_HEADERS) -I../../../frameworks/base/include/

LOCAL_CFLAGS := 

+69 −36
Original line number Diff line number Diff line
/*
 * Copyright (C) ErrorCode::CommandOkay8 The Android Open Source Project
 * 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.
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -26,6 +27,7 @@

#include "CommandListener.h"
#include "Controller.h"
#include "Property.h"
#include "NetworkManager.h"
#include "WifiController.h"
#include "VpnController.h"
@@ -35,31 +37,32 @@ CommandListener::CommandListener() :
                 FrameworkListener("nexus") {
    registerCmd(new WifiScanResultsCmd());
    registerCmd(new WifiListNetworksCmd());
    registerCmd(new WifiAddNetworkCmd());
    registerCmd(new WifiCreateNetworkCmd());
    registerCmd(new WifiRemoveNetworkCmd());

    registerCmd(new GetCmd());
    registerCmd(new SetCmd());
    registerCmd(new ListCmd());
}

/* -------------
 * Wifi Commands
 * ------------ */

CommandListener::WifiAddNetworkCmd::WifiAddNetworkCmd() :
                 NexusCommand("wifi_add_network") {
CommandListener::WifiCreateNetworkCmd::WifiCreateNetworkCmd() :
                 NexusCommand("wifi_create_network") {
}

int CommandListener::WifiAddNetworkCmd::runCommand(SocketClient *cli, char *data) {
int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli, char *data) {
    NetworkManager *nm = NetworkManager::Instance();
    WifiController *wc = (WifiController *) nm->findController("WIFI");
    int networkId;
    WifiNetwork *wn;

    if ((networkId = wc->addNetwork()) < 0)
        cli->sendMsg(ErrorCode::OperationFailed, "Failed to add network", true);
    if (!(wn = wc->createNetwork()))
        cli->sendMsg(ErrorCode::OperationFailed, "Failed to create network", true);
    else {
        char tmp[128];
        sprintf(tmp, "Added network id %d.", networkId);
        sprintf(tmp, "Created network id %d.", wn->getNetworkId());
        cli->sendMsg(ErrorCode::CommandOkay, tmp, false);
    }
    return 0;
@@ -103,7 +106,7 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
    }

    delete src;
    cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete", false);
    cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete.", false);
    return 0;
}

@@ -123,7 +126,6 @@ int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *da
        sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid());
        cli->sendMsg(ErrorCode::WifiNetworkList, buffer, false);
        delete (*it);
        it = src->erase(it);
    }

    delete src;
@@ -143,31 +145,28 @@ CommandListener::GetCmd::GetCmd() :
}

int CommandListener::GetCmd::runCommand(SocketClient *cli, char *data) {
    char *bword;
    char *last;
    char propname[32];
    char *next = data;
    char *propname;

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

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

    char pb[255];
    char pb[Property::NameMaxSize + 6];
    snprintf(pb, sizeof(pb), "%s:", propname);

    if (!NetworkManager::Instance()->getProperty(propname,
    if (!NetworkManager::Instance()->getPropMngr()->get(propname,
                                                        &pb[strlen(pb)],
                                                        sizeof(pb) - strlen(pb))) {
        goto out_inval;
    }

    cli->sendMsg(ErrorCode::VariableRead, pb, false);
    cli->sendMsg(ErrorCode::PropertyRead, pb, false);

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

@@ -178,8 +177,8 @@ CommandListener::SetCmd::SetCmd() :
int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {
    char *bword;
    char *last;
    char propname[32];
    char propval[250];
    char propname[Property::NameMaxSize];
    char propval[Property::ValueMaxSize];

    if (!(bword = strtok_r(data, ":", &last)))
        goto out_inval;
@@ -191,7 +190,7 @@ int CommandListener::SetCmd::runCommand(SocketClient *cli, char *data) {

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

    if (NetworkManager::Instance()->setProperty(propname, propval))
    if (NetworkManager::Instance()->getPropMngr()->set(propname, propval))
        goto out_inval;

    cli->sendMsg(ErrorCode::CommandOkay, "Property set.", false);
@@ -208,5 +207,39 @@ CommandListener::ListCmd::ListCmd() :
}

int CommandListener::ListCmd::runCommand(SocketClient *cli, char *data) {
    android::List<char *> *pc;

    if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList())) {
        errno = ENODATA;
        cli->sendMsg(ErrorCode::CommandParameterError, "Failed to list properties.", true);
        return 0;
    }

    android::List<char *>::iterator it;

    for (it = pc->begin(); it != pc->end(); ++it) {
        char p_v[Property::ValueMaxSize];

        if (!NetworkManager::Instance()->getPropMngr()->get((*it),
                                                            p_v,
                                                            sizeof(p_v))) {
            LOGW("Failed to get %s (%s)", (*it), strerror(errno));
        }

        char *buf;
        if (asprintf(&buf, "%s:%s", (*it), p_v) < 0) {
            LOGE("Failed to allocate memory");
            free((*it));
            continue;
        }
        cli->sendMsg(ErrorCode::PropertyList, buf, false);
        free(buf);

        free((*it));
    }

    delete pc;

    cli->sendMsg(ErrorCode::CommandOkay, "Properties list complete.", false);
    return 0;
}
+4 −3
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _COMMANDLISTENER_H__
#define _COMMANDLISTENER_H__

@@ -40,10 +41,10 @@ private:
        int runCommand(SocketClient *c, char *data);
    };

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

+27 −58
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -33,13 +34,17 @@
extern "C" int init_module(void *, unsigned int, const char *);
extern "C" int delete_module(const char *, unsigned int);

Controller::Controller(const char *name, const char *prefix) {
    mName = name;
    mPropertyPrefix = prefix;
    mProperties = new PropertyCollection();
Controller::Controller(const char *name, PropertyManager *propMngr) {
    mPropMngr = propMngr;
    mName = strdup(name);
    mBoundInterface = NULL;
}

    mEnabled = false;
    registerProperty("enable");
Controller::~Controller() {
    if (mBoundInterface)
        free(mBoundInterface);
    if (mName)
        free(mName);
}

int Controller::start() {
@@ -50,65 +55,16 @@ 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;
    }

int Controller::set(const char *name, const char *value) {
    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;
    }

const char *Controller::get(const char *name, char *buffer, size_t maxsize) {
    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;
@@ -203,3 +159,16 @@ bail:
	close(fd);
	return buffer;
}

int Controller::bindInterface(const char *ifname) {
    mBoundInterface = strdup(ifname);
    LOGD("Controller %s bound to %s", mName, ifname);
    return 0;
}

int Controller::unbindInterface(const char *ifname) {
    free(mBoundInterface);
    mBoundInterface = NULL;
    LOGD("Controller %s unbound from %s", mName, ifname);
    return 0;
}
+29 −21
Original line number Diff line number Diff line
@@ -13,51 +13,59 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _CONTROLLER_H
#define _CONTROLLER_H

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

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

class PropertyManager;

#include "PropertyCollection.h"
#include "PropertyManager.h"
#include "IPropertyProvider.h"

class Controller {
class Controller : public IPropertyProvider {
private:
    const char *mName;
    const char *mPropertyPrefix;
    PropertyCollection *mProperties;
    bool mEnabled;
    /*
     * Name of this controller - WIFI/VPN/USBNET/BTNET/BTDUN/LOOP/etc
     */
    char *mName;

    /*
     * Name of the system ethernet interface which this controller is
     * bound to.
     */
    char *mBoundInterface;

protected:
    PropertyManager *mPropMngr;
    
public:
    Controller(const char *name, const char *prefix);
    virtual ~Controller() {}
    Controller(const char *name, PropertyManager *propMngr);
    virtual ~Controller();

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

    virtual const PropertyCollection &getProperties();
    virtual int setProperty(const char *name, char *value);
    virtual const char *getProperty(const char *name, char *buffer, size_t maxsize);

    const char *getName() { return mName; }
    const char *getPropertyPrefix() { return mPropertyPrefix; }
    const char *getBoundInterface() { return mBoundInterface; }

    /* IPropertyProvider methods */
    virtual int set(const char *name, const char *value);
    virtual const char *get(const char *name, char *buffer, size_t maxsize);

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);
    int bindInterface(const char *ifname);
    int unbindInterface(const char *ifname);

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

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

};

typedef android::List<Controller *> ControllerCollection;
Loading