Search in sources :

Example 1 with NetworkUpdateResult

use of android.net.wifi.NetworkUpdateResult in project android_frameworks_base by ParanoidAndroid.

the class WifiConfigStore method writeIpAndProxyConfigurationsOnChange.

/* Compare current and new configuration and write to file on change */
private NetworkUpdateResult writeIpAndProxyConfigurationsOnChange(WifiConfiguration currentConfig, WifiConfiguration newConfig) {
    boolean ipChanged = false;
    boolean proxyChanged = false;
    LinkProperties linkProperties = null;
    switch(newConfig.ipAssignment) {
        case STATIC:
            Collection<LinkAddress> currentLinkAddresses = currentConfig.linkProperties.getLinkAddresses();
            Collection<LinkAddress> newLinkAddresses = newConfig.linkProperties.getLinkAddresses();
            Collection<InetAddress> currentDnses = currentConfig.linkProperties.getDnses();
            Collection<InetAddress> newDnses = newConfig.linkProperties.getDnses();
            Collection<RouteInfo> currentRoutes = currentConfig.linkProperties.getRoutes();
            Collection<RouteInfo> newRoutes = newConfig.linkProperties.getRoutes();
            boolean linkAddressesDiffer = (currentLinkAddresses.size() != newLinkAddresses.size()) || !currentLinkAddresses.containsAll(newLinkAddresses);
            boolean dnsesDiffer = (currentDnses.size() != newDnses.size()) || !currentDnses.containsAll(newDnses);
            boolean routesDiffer = (currentRoutes.size() != newRoutes.size()) || !currentRoutes.containsAll(newRoutes);
            if ((currentConfig.ipAssignment != newConfig.ipAssignment) || linkAddressesDiffer || dnsesDiffer || routesDiffer) {
                ipChanged = true;
            }
            break;
        case DHCP:
            if (currentConfig.ipAssignment != newConfig.ipAssignment) {
                ipChanged = true;
            }
            break;
        case UNASSIGNED:
            /* Ignore */
            break;
        default:
            loge("Ignore invalid ip assignment during write");
            break;
    }
    switch(newConfig.proxySettings) {
        case STATIC:
            ProxyProperties newHttpProxy = newConfig.linkProperties.getHttpProxy();
            ProxyProperties currentHttpProxy = currentConfig.linkProperties.getHttpProxy();
            if (newHttpProxy != null) {
                proxyChanged = !newHttpProxy.equals(currentHttpProxy);
            } else {
                proxyChanged = (currentHttpProxy != null);
            }
            break;
        case NONE:
            if (currentConfig.proxySettings != newConfig.proxySettings) {
                proxyChanged = true;
            }
            break;
        case UNASSIGNED:
            /* Ignore */
            break;
        default:
            loge("Ignore invalid proxy configuration during write");
            break;
    }
    if (!ipChanged) {
        linkProperties = copyIpSettingsFromConfig(currentConfig);
    } else {
        currentConfig.ipAssignment = newConfig.ipAssignment;
        linkProperties = copyIpSettingsFromConfig(newConfig);
        log("IP config changed SSID = " + currentConfig.SSID + " linkProperties: " + linkProperties.toString());
    }
    if (!proxyChanged) {
        linkProperties.setHttpProxy(currentConfig.linkProperties.getHttpProxy());
    } else {
        currentConfig.proxySettings = newConfig.proxySettings;
        linkProperties.setHttpProxy(newConfig.linkProperties.getHttpProxy());
        log("proxy changed SSID = " + currentConfig.SSID);
        if (linkProperties.getHttpProxy() != null) {
            log(" proxyProperties: " + linkProperties.getHttpProxy().toString());
        }
    }
    if (ipChanged || proxyChanged) {
        currentConfig.linkProperties = linkProperties;
        writeIpAndProxyConfigurations();
        sendConfiguredNetworksChangedBroadcast(currentConfig, WifiManager.CHANGE_REASON_CONFIG_CHANGE);
    }
    return new NetworkUpdateResult(ipChanged, proxyChanged);
}
Also used : LinkAddress(android.net.LinkAddress) ProxyProperties(android.net.ProxyProperties) NetworkUpdateResult(android.net.wifi.NetworkUpdateResult) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Example 2 with NetworkUpdateResult

use of android.net.wifi.NetworkUpdateResult in project android_frameworks_base by ParanoidAndroid.

the class WifiConfigStore method saveNetwork.

/**
     * Add/update the specified configuration and save config
     *
     * @param config WifiConfiguration to be saved
     * @return network update result
     */
NetworkUpdateResult saveNetwork(WifiConfiguration config) {
    // A new network cannot have null SSID
    if (config == null || (config.networkId == INVALID_NETWORK_ID && config.SSID == null)) {
        return new NetworkUpdateResult(INVALID_NETWORK_ID);
    }
    boolean newNetwork = (config.networkId == INVALID_NETWORK_ID);
    NetworkUpdateResult result = addOrUpdateNetworkNative(config);
    int netId = result.getNetworkId();
    /* enable a new network */
    if (newNetwork && netId != INVALID_NETWORK_ID) {
        mWifiNative.enableNetwork(netId, false);
        mConfiguredNetworks.get(netId).status = Status.ENABLED;
    }
    mWifiNative.saveConfig();
    sendConfiguredNetworksChangedBroadcast(config, result.isNewNetwork() ? WifiManager.CHANGE_REASON_ADDED : WifiManager.CHANGE_REASON_CONFIG_CHANGE);
    return result;
}
Also used : NetworkUpdateResult(android.net.wifi.NetworkUpdateResult)

Example 3 with NetworkUpdateResult

use of android.net.wifi.NetworkUpdateResult in project XobotOS by xamarin.

the class WifiConfigStore method selectNetwork.

/**
     * Selects the specified network config for connection. This involves
     * addition/update of the specified config, updating the priority of
     * all the networks and enabling the given network while disabling others.
     *
     * Selecting a network will leave the other networks disabled and
     * a call to enableAllNetworks() needs to be issued upon a connection
     * or a failure event from supplicant
     *
     * @param config The configuration details in WifiConfiguration
     * @return the networkId now associated with the specified configuration
     */
static int selectNetwork(WifiConfiguration config) {
    if (config != null) {
        NetworkUpdateResult result = addOrUpdateNetworkNative(config);
        int netId = result.getNetworkId();
        if (netId != INVALID_NETWORK_ID) {
            selectNetwork(netId);
        } else {
            loge("Failed to update network " + config);
        }
        return netId;
    }
    return INVALID_NETWORK_ID;
}
Also used : NetworkUpdateResult(android.net.wifi.NetworkUpdateResult)

Example 4 with NetworkUpdateResult

use of android.net.wifi.NetworkUpdateResult in project XobotOS by xamarin.

the class WifiConfigStore method saveNetwork.

/**
     * Add/update the specified configuration and save config
     *
     * @param config WifiConfiguration to be saved
     */
static NetworkUpdateResult saveNetwork(WifiConfiguration config) {
    boolean newNetwork = (config.networkId == INVALID_NETWORK_ID);
    NetworkUpdateResult result = addOrUpdateNetworkNative(config);
    int netId = result.getNetworkId();
    /* enable a new network */
    if (newNetwork && netId != INVALID_NETWORK_ID) {
        WifiNative.enableNetworkCommand(netId, false);
        synchronized (sConfiguredNetworks) {
            sConfiguredNetworks.get(netId).status = Status.ENABLED;
        }
    }
    WifiNative.saveConfigCommand();
    sendConfiguredNetworksChangedBroadcast();
    return result;
}
Also used : NetworkUpdateResult(android.net.wifi.NetworkUpdateResult)

Example 5 with NetworkUpdateResult

use of android.net.wifi.NetworkUpdateResult in project XobotOS by xamarin.

the class WifiConfigStore method addOrUpdateNetworkNative.

private static NetworkUpdateResult addOrUpdateNetworkNative(WifiConfiguration config) {
    /*
         * If the supplied networkId is INVALID_NETWORK_ID, we create a new empty
         * network configuration. Otherwise, the networkId should
         * refer to an existing configuration.
         */
    int netId = config.networkId;
    boolean newNetwork = false;
    // networkId of INVALID_NETWORK_ID means we want to create a new network
    if (netId == INVALID_NETWORK_ID) {
        Integer savedNetId = sNetworkIds.get(configKey(config));
        if (savedNetId != null) {
            netId = savedNetId;
        } else {
            newNetwork = true;
            netId = WifiNative.addNetworkCommand();
            if (netId < 0) {
                loge("Failed to add a network!");
                return new NetworkUpdateResult(INVALID_NETWORK_ID);
            }
        }
    }
    boolean updateFailed = true;
    setVariables: {
        if (config.SSID != null && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.ssidVarName, config.SSID)) {
            loge("failed to set SSID: " + config.SSID);
            break setVariables;
        }
        if (config.BSSID != null && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.bssidVarName, config.BSSID)) {
            loge("failed to set BSSID: " + config.BSSID);
            break setVariables;
        }
        String allowedKeyManagementString = makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
        if (config.allowedKeyManagement.cardinality() != 0 && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.KeyMgmt.varName, allowedKeyManagementString)) {
            loge("failed to set key_mgmt: " + allowedKeyManagementString);
            break setVariables;
        }
        String allowedProtocolsString = makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
        if (config.allowedProtocols.cardinality() != 0 && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.Protocol.varName, allowedProtocolsString)) {
            loge("failed to set proto: " + allowedProtocolsString);
            break setVariables;
        }
        String allowedAuthAlgorithmsString = makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
        if (config.allowedAuthAlgorithms.cardinality() != 0 && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.AuthAlgorithm.varName, allowedAuthAlgorithmsString)) {
            loge("failed to set auth_alg: " + allowedAuthAlgorithmsString);
            break setVariables;
        }
        String allowedPairwiseCiphersString = makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
        if (config.allowedPairwiseCiphers.cardinality() != 0 && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.PairwiseCipher.varName, allowedPairwiseCiphersString)) {
            loge("failed to set pairwise: " + allowedPairwiseCiphersString);
            break setVariables;
        }
        String allowedGroupCiphersString = makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
        if (config.allowedGroupCiphers.cardinality() != 0 && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.GroupCipher.varName, allowedGroupCiphersString)) {
            loge("failed to set group: " + allowedGroupCiphersString);
            break setVariables;
        }
        // by preventing "*" as a key.
        if (config.preSharedKey != null && !config.preSharedKey.equals("*") && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.pskVarName, config.preSharedKey)) {
            loge("failed to set psk");
            break setVariables;
        }
        boolean hasSetKey = false;
        if (config.wepKeys != null) {
            for (int i = 0; i < config.wepKeys.length; i++) {
                // by preventing "*" as a key.
                if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
                    if (!WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.wepKeyVarNames[i], config.wepKeys[i])) {
                        loge("failed to set wep_key" + i + ": " + config.wepKeys[i]);
                        break setVariables;
                    }
                    hasSetKey = true;
                }
            }
        }
        if (hasSetKey) {
            if (!WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.wepTxKeyIdxVarName, Integer.toString(config.wepTxKeyIndex))) {
                loge("failed to set wep_tx_keyidx: " + config.wepTxKeyIndex);
                break setVariables;
            }
        }
        if (!WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.priorityVarName, Integer.toString(config.priority))) {
            loge(config.SSID + ": failed to set priority: " + config.priority);
            break setVariables;
        }
        if (config.hiddenSSID && !WifiNative.setNetworkVariableCommand(netId, WifiConfiguration.hiddenSSIDVarName, Integer.toString(config.hiddenSSID ? 1 : 0))) {
            loge(config.SSID + ": failed to set hiddenSSID: " + config.hiddenSSID);
            break setVariables;
        }
        for (WifiConfiguration.EnterpriseField field : config.enterpriseFields) {
            String varName = field.varName();
            String value = field.value();
            if (value != null) {
                if (field != config.eap) {
                    value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
                }
                if (!WifiNative.setNetworkVariableCommand(netId, varName, value)) {
                    loge(config.SSID + ": failed to set " + varName + ": " + value);
                    break setVariables;
                }
            }
        }
        updateFailed = false;
    }
    if (updateFailed) {
        if (newNetwork) {
            WifiNative.removeNetworkCommand(netId);
            loge("Failed to set a network variable, removed network: " + netId);
        }
        return new NetworkUpdateResult(INVALID_NETWORK_ID);
    }
    /* An update of the network variables requires reading them
         * back from the supplicant to update sConfiguredNetworks.
         * This is because some of the variables (SSID, wep keys &
         * passphrases) reflect different values when read back than
         * when written. For example, wep key is stored as * irrespective
         * of the value sent to the supplicant
         */
    WifiConfiguration sConfig;
    synchronized (sConfiguredNetworks) {
        sConfig = sConfiguredNetworks.get(netId);
    }
    if (sConfig == null) {
        sConfig = new WifiConfiguration();
        sConfig.networkId = netId;
    }
    readNetworkVariables(sConfig);
    synchronized (sConfiguredNetworks) {
        sConfiguredNetworks.put(netId, sConfig);
        sNetworkIds.put(configKey(sConfig), netId);
    }
    NetworkUpdateResult result = writeIpAndProxyConfigurationsOnChange(sConfig, config);
    result.setNetworkId(netId);
    return result;
}
Also used : NetworkUpdateResult(android.net.wifi.NetworkUpdateResult)

Aggregations

NetworkUpdateResult (android.net.wifi.NetworkUpdateResult)8 LinkAddress (android.net.LinkAddress)2 LinkProperties (android.net.LinkProperties)2 ProxyProperties (android.net.ProxyProperties)2 RouteInfo (android.net.RouteInfo)2 InetAddress (java.net.InetAddress)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1