Search in sources :

Example 6 with NetworkUpdateResult

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

the class WifiConfigStore method addOrUpdateNetworkNative.

private 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 = mNetworkIds.get(configKey(config));
        if (savedNetId != null) {
            netId = savedNetId;
        } else {
            newNetwork = true;
            netId = mWifiNative.addNetwork();
            if (netId < 0) {
                loge("Failed to add a network!");
                return new NetworkUpdateResult(INVALID_NETWORK_ID);
            }
        }
    }
    boolean updateFailed = true;
    setVariables: {
        if (config.SSID != null && !mWifiNative.setNetworkVariable(netId, WifiConfiguration.ssidVarName, config.SSID)) {
            loge("failed to set SSID: " + config.SSID);
            break setVariables;
        }
        if (config.BSSID != null && !mWifiNative.setNetworkVariable(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 && !mWifiNative.setNetworkVariable(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 && !mWifiNative.setNetworkVariable(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 && !mWifiNative.setNetworkVariable(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 && !mWifiNative.setNetworkVariable(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 && !mWifiNative.setNetworkVariable(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("*") && !mWifiNative.setNetworkVariable(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 (!mWifiNative.setNetworkVariable(netId, WifiConfiguration.wepKeyVarNames[i], config.wepKeys[i])) {
                        loge("failed to set wep_key" + i + ": " + config.wepKeys[i]);
                        break setVariables;
                    }
                    hasSetKey = true;
                }
            }
        }
        if (hasSetKey) {
            if (!mWifiNative.setNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName, Integer.toString(config.wepTxKeyIndex))) {
                loge("failed to set wep_tx_keyidx: " + config.wepTxKeyIndex);
                break setVariables;
            }
        }
        if (!mWifiNative.setNetworkVariable(netId, WifiConfiguration.priorityVarName, Integer.toString(config.priority))) {
            loge(config.SSID + ": failed to set priority: " + config.priority);
            break setVariables;
        }
        if (config.hiddenSSID && !mWifiNative.setNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName, Integer.toString(config.hiddenSSID ? 1 : 0))) {
            loge(config.SSID + ": failed to set hiddenSSID: " + config.hiddenSSID);
            break setVariables;
        }
        if (config.enterpriseConfig != null && config.enterpriseConfig.getEapMethod() != WifiEnterpriseConfig.Eap.NONE) {
            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
            if (enterpriseConfig.needsKeyStore()) {
                /**
                     * Keyguard settings may eventually be controlled by device policy.
                     * We check here if keystore is unlocked before installing
                     * credentials.
                     * TODO: Figure a way to store these credentials for wifi alone
                     * TODO: Do we need a dialog here ?
                     */
                if (mKeyStore.state() != KeyStore.State.UNLOCKED) {
                    loge(config.SSID + ": key store is locked");
                    break setVariables;
                }
                try {
                    /* config passed may include only fields being updated.
                         * In order to generate the key id, fetch uninitialized
                         * fields from the currently tracked configuration
                         */
                    WifiConfiguration currentConfig = mConfiguredNetworks.get(netId);
                    String keyId = config.getKeyIdForCredentials(currentConfig);
                    if (!enterpriseConfig.installKeys(mKeyStore, keyId)) {
                        loge(config.SSID + ": failed to install keys");
                        break setVariables;
                    }
                } catch (IllegalStateException e) {
                    loge(config.SSID + " invalid config for key installation");
                    break setVariables;
                }
            }
            HashMap<String, String> enterpriseFields = enterpriseConfig.getFields();
            for (String key : enterpriseFields.keySet()) {
                String value = enterpriseFields.get(key);
                if (!mWifiNative.setNetworkVariable(netId, key, value)) {
                    enterpriseConfig.removeKeys(mKeyStore);
                    loge(config.SSID + ": failed to set " + key + ": " + value);
                    break setVariables;
                }
            }
        }
        updateFailed = false;
    }
    if (updateFailed) {
        if (newNetwork) {
            mWifiNative.removeNetwork(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 mConfiguredNetworks.
         * 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 currentConfig = mConfiguredNetworks.get(netId);
    if (currentConfig == null) {
        currentConfig = new WifiConfiguration();
        currentConfig.ipAssignment = IpAssignment.DHCP;
        currentConfig.proxySettings = ProxySettings.NONE;
        currentConfig.networkId = netId;
    }
    readNetworkVariables(currentConfig);
    mConfiguredNetworks.put(netId, currentConfig);
    mNetworkIds.put(configKey(currentConfig), netId);
    NetworkUpdateResult result = writeIpAndProxyConfigurationsOnChange(currentConfig, config);
    result.setIsNewNetwork(newNetwork);
    result.setNetworkId(netId);
    return result;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NetworkUpdateResult(android.net.wifi.NetworkUpdateResult)

Example 7 with NetworkUpdateResult

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

the class WifiConfigStore method writeIpAndProxyConfigurationsOnChange.

/* Compare current and new configuration and write to file on change */
private static NetworkUpdateResult writeIpAndProxyConfigurationsOnChange(WifiConfiguration currentConfig, WifiConfiguration newConfig) {
    boolean ipChanged = false;
    boolean proxyChanged = false;
    LinkProperties linkProperties = new LinkProperties();
    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) {
        addIpSettingsFromConfig(linkProperties, currentConfig);
    } else {
        currentConfig.ipAssignment = newConfig.ipAssignment;
        addIpSettingsFromConfig(linkProperties, 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();
    }
    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 8 with NetworkUpdateResult

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

the class WifiConfigStore method addOrUpdateNetwork.

/**
     * Add/update a network. Note that there is no saveConfig operation.
     * This function is retained for compatibility with the public
     * API. The more powerful saveNetwork() is used by the
     * state machine
     *
     * @param config wifi configuration to add/update
     */
static int addOrUpdateNetwork(WifiConfiguration config) {
    NetworkUpdateResult result = addOrUpdateNetworkNative(config);
    sendConfiguredNetworksChangedBroadcast();
    return result.getNetworkId();
}
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