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

Commit 82a2116e authored by San Mehat's avatar San Mehat
Browse files

nexus: Initial support for manipulating wifi networks + change wifi scan notification msgs



Signed-off-by: default avatarSan Mehat <san@google.com>
parent df6c1b91
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ LOCAL_SRC_FILES:= \
                  VpnController.cpp        \
                  ScanResult.cpp           \
                  WifiScanner.cpp          \
                  WifiNetwork.cpp          \

LOCAL_MODULE:= nexus

+134 −4
Original line number Diff line number Diff line
@@ -33,6 +33,11 @@ CommandListener::CommandListener() :
    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 VpnDisableCmd());
@@ -70,13 +75,46 @@ int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) {
    return 0;
}

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

    if ((networkId = wc->addNetwork()) < 0)
        cli->sendMsg(ErrorCode::OperationFailed, "Failed to add network", true);
    else {
        char tmp[128];
        sprintf(tmp, "Added network id %d.", networkId);
        cli->sendMsg(ErrorCode::CommandOkay, tmp, false);
    }
    return 0;
}

CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() :
                 NexusCommand("wifi_remove_network") {
} 
               
int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *data) {
    NetworkManager *nm = NetworkManager::Instance();
    WifiController *wc = (WifiController *) nm->findController("WIFI");

    if (wc->removeNetwork(atoi(data)))
        cli->sendMsg(ErrorCode::OperationFailed, "Failed to remove network", true);
    else {
        cli->sendMsg(ErrorCode::CommandOkay, "Network removed.", false);
    }
    return 0;
}

CommandListener::WifiScanCmd::WifiScanCmd() :
                 NexusCommand("wifi_scan") {
} 

int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) {
    LOGD("WifiScanCmd(%s)", data);

    WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");

    if (wc->setScanMode(atoi(data)))
@@ -93,7 +131,6 @@ CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :

int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) {
    NetworkManager *nm = NetworkManager::Instance();

    WifiController *wc = (WifiController *) nm->findController("WIFI");

    ScanResultCollection *src = wc->createScanResults();
@@ -104,7 +141,7 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
        sprintf(buffer, "%s:%u:%d:%s:%s",
                (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(),
                (*it)->getFlags(), (*it)->getSsid());
        cli->sendMsg(125, buffer, false);
        cli->sendMsg(ErrorCode::WifiScanResult, buffer, false);
        delete (*it);
        it = src->erase(it);
    }
@@ -114,6 +151,99 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
    return 0;
}

CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() :
                 NexusCommand("wifi_list_networks") {
} 

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

    WifiNetworkCollection *src = wc->createNetworkList();
    WifiNetworkCollection::iterator it;
    char buffer[256];
    
    for(it = src->begin(); it != src->end(); ++it) {
        sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid());
        cli->sendMsg(ErrorCode::WifiNetworkList, buffer, false);
        delete (*it);
        it = src->erase(it);
    }

    delete src;
    cli->sendMsg(ErrorCode::CommandOkay, "Network listing complete.", false);
    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
 * ------------ */
+35 −0
Original line number Diff line number Diff line
@@ -53,6 +53,41 @@ private:
        int runCommand(SocketClient *c, char *data);
    };

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

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

    class WifiListNetworksCmd : public NexusCommand {
    public:
        WifiListNetworksCmd();
        virtual ~WifiListNetworksCmd() {}
        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();
+4 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ public:
    // before proceeding with a new command.
    static const int ActionInitiated = 100;

    static const int WifiScanResult = 125;
    static const int WifiNetworkList = 126;

    // 200 series - Requested action has been successfully completed
    static const int CommandOkay = 200;

@@ -33,6 +36,7 @@ public:
    // 500 series - The command was not accepted and the requested
    // action did not take place.
    static const int CommandSyntaxError = 500;
    static const int CommandParameterError = 501;

    // 600 series - Unsolicited broadcasts
    static const int UnsolicitedInformational = 600;
+32 −3
Original line number Diff line number Diff line
@@ -229,7 +229,7 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len)
        return -1;
    }

//    LOGD("sendCommand(): -> '%s'", cmd);
    LOGD("sendCommand(): -> '%s'", cmd);

    int rc;
    if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2)  {
@@ -245,7 +245,7 @@ int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len)
        !strncmp(cmd, "SCAN_RESULTS", 12)) 
        reply[*reply_len] = '\0';

//    LOGD("sendCommand(): <- '%s'", reply);
    LOGD("sendCommand(): <- '%s'", reply);
    return 0;
}

@@ -355,7 +355,7 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
            mLatestScanResults->push_back(new ScanResult(linep));
    
        char tmp[128];
        sprintf(tmp, "%d scan results ready", mLatestScanResults->size());
        sprintf(tmp, "Scan results ready (%d)", mLatestScanResults->size());
        NetworkManager::Instance()->getBroadcaster()->
                                    sendBroadcast(ErrorCode::UnsolicitedInformational, tmp, false);
        pthread_mutex_unlock(&mLatestScanResultsLock);
@@ -412,6 +412,35 @@ ScanResultCollection *Supplicant::createLatestScanResults() {
    return d;
}

WifiNetworkCollection *Supplicant::createNetworkList() {
    WifiNetworkCollection *d = new WifiNetworkCollection();
    return d;
}

int Supplicant::addNetwork() {
    char reply[32];
    size_t len = sizeof(reply) -1;

    memset(reply, 0, sizeof(reply));
    if (sendCommand("ADD_NETWORK", reply, &len))
        return -1;

    return atoi(reply);
}

int Supplicant::removeNetwork(int networkId) {
    char req[64];

    sprintf(req, "REMOVE_NETWORK %d", networkId);
    char reply[32];
    size_t len = sizeof(reply) -1;
    memset(reply, 0, sizeof(reply));
    
    if (sendCommand(req, reply, &len))
        return -1;
    return 0;
}

int Supplicant::setupConfig() {
    char buf[2048];
    int srcfd, destfd;
Loading