Search in sources :

Example 16 with WifiMode

use of org.eclipse.kura.net.wifi.WifiMode in project kura by eclipse.

the class WpaSupplicantConfigWriter method writeConfig.

private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    s_logger.debug("Writing wpa_supplicant config for {}", interfaceName);
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    if (netInterfaceAddressConfigs.size() > 0) {
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
                WifiMode wifiMode = ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).getMode();
                WifiConfig infraConfig = null;
                WifiConfig adhocConfig = null;
                WifiConfig wpaSupplicantConfig = null;
                // Get the wifi configs
                if (netConfigs != null) {
                    for (NetConfig netConfig : netConfigs) {
                        if (netConfig instanceof WifiConfig) {
                            if (((WifiConfig) netConfig).getMode() == WifiMode.ADHOC) {
                                adhocConfig = (WifiConfig) netConfig;
                            } else if (((WifiConfig) netConfig).getMode() == WifiMode.INFRA) {
                                infraConfig = (WifiConfig) netConfig;
                            }
                        } else if (netConfig instanceof NetConfigIP4) {
                            netInterfaceStatus = ((NetConfigIP4) netConfig).getStatus();
                        }
                    }
                }
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
                    s_logger.info("Network interface status for " + interfaceName + " is disabled - not overwriting wpaconfig file");
                    return;
                }
                // Choose which config to write
                if (wifiMode == WifiMode.INFRA) {
                    if (infraConfig != null) {
                        StringBuilder key = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.wifi.infra.pingAccessPoint");
                        try {
                            KuranetConfig.setProperty(key.toString(), Boolean.toString(infraConfig.pingAccessPoint()));
                        } catch (IOException e) {
                            s_logger.warn("Error setting KuranetConfig property", e);
                        }
                        key = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.wifi.infra.ignoreSSID");
                        try {
                            KuranetConfig.setProperty(key.toString(), Boolean.toString(infraConfig.ignoreSSID()));
                        } catch (IOException e) {
                            s_logger.warn("Error setting KuranetConfig property", e);
                        }
                        wpaSupplicantConfig = infraConfig;
                    } else {
                        s_logger.debug("Not updating wpa_supplicant config - wifi mode is " + wifiMode + " but the infra config is null");
                    }
                } else if (wifiMode == WifiMode.ADHOC) {
                    if (adhocConfig != null) {
                        wpaSupplicantConfig = adhocConfig;
                    } else {
                        s_logger.debug("Not updating wpa_supplicant config - wifi mode is " + wifiMode + " but the adhoc config is null");
                    }
                } else if (wifiMode == WifiMode.MASTER) {
                    if (infraConfig != null && adhocConfig != null) {
                        // Choose the infra config if both are present?
                        wpaSupplicantConfig = infraConfig;
                    } else if (infraConfig != null) {
                        wpaSupplicantConfig = infraConfig;
                    } else if (adhocConfig != null) {
                        wpaSupplicantConfig = adhocConfig;
                    } else {
                        s_logger.debug("Not updating wpa_supplicant config - wifi mode is " + wifiMode + " and the infra and adhoc configs are null");
                    }
                }
                // Write the config
                try {
                    if (wpaSupplicantConfig != null) {
                        s_logger.debug("Writing wifiConfig: {}", wpaSupplicantConfig);
                        generateWpaSupplicantConf(wpaSupplicantConfig, interfaceName, WPA_TMP_CONFIG_FILE);
                        moveWpaSupplicantConf(interfaceName, WPA_TMP_CONFIG_FILE);
                    }
                } catch (Exception e) {
                    s_logger.error("Failed to configure WPA Supplicant");
                    throw KuraException.internalError(e);
                }
            }
        }
    }
}
Also used : NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 17 with WifiMode

use of org.eclipse.kura.net.wifi.WifiMode in project kura by eclipse.

the class NetworkAdminServiceImpl method enableInterface.

@Override
public void enableInterface(String interfaceName, boolean dhcp) throws KuraException {
    try {
        NetInterfaceType type = LinuxNetworkUtil.getType(interfaceName);
        NetInterfaceStatus status = NetInterfaceStatus.netIPv4StatusUnknown;
        WifiMode wifiMode = WifiMode.UNKNOWN;
        WifiConfig wifiConfig = null;
        WifiInterfaceState wifiInterfaceState = null;
        if (type == NetInterfaceType.WIFI) {
            List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> wifiNetInterfaceConfigs = getWifiInterfaceConfigs();
            List<? extends NetInterfaceAddressConfig> wifiNetInterfaceAddressConfigs = getWifiNetInterfaceAddressConfigs(interfaceName, wifiNetInterfaceConfigs);
            WifiInterfaceAddressConfig wifiInterfaceAddressConfig = getWifiAddressConfig(wifiNetInterfaceAddressConfigs);
            wifiMode = wifiInterfaceAddressConfig.getMode();
            wifiInterfaceState = new WifiInterfaceState(interfaceName, wifiMode);
            for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
                if (netConfig instanceof NetConfigIP4) {
                    status = ((NetConfigIP4) netConfig).getStatus();
                    s_logger.debug("Interface status is set to {}", status);
                } else if (netConfig instanceof WifiConfig && ((WifiConfig) netConfig).getMode() == wifiMode) {
                    wifiConfig = (WifiConfig) netConfig;
                }
            }
        }
        if (!LinuxNetworkUtil.hasAddress(interfaceName) || ((type == NetInterfaceType.WIFI) && (wifiInterfaceState != null) && !wifiInterfaceState.isLinkUp())) {
            s_logger.info("bringing interface {} up", interfaceName);
            if (type == NetInterfaceType.WIFI) {
                enableWifiInterface(interfaceName, status, wifiMode, wifiConfig);
            }
            if (dhcp) {
                renewDhcpLease(interfaceName);
            } else {
                LinuxNetworkUtil.enableInterface(interfaceName);
            }
            // if it isn't up - at least make sure the Ethernet controller is powered on
            if (!LinuxNetworkUtil.hasAddress(interfaceName)) {
                LinuxNetworkUtil.bringUpDeletingAddress(interfaceName);
            }
        } else {
            s_logger.info("not bringing interface {} up because it is already up", interfaceName);
            if (dhcp) {
                renewDhcpLease(interfaceName);
            }
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceState(org.eclipse.kura.net.admin.monitor.WifiInterfaceState) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 18 with WifiMode

use of org.eclipse.kura.net.wifi.WifiMode in project kura by eclipse.

the class NetworkAdminServiceImpl method getWifiHotspots.

@Override
public Map<String, WifiHotspotInfo> getWifiHotspots(String ifaceName) throws KuraException {
    Map<String, WifiHotspotInfo> mWifiHotspotInfo = new HashMap<String, WifiHotspotInfo>();
    WifiMode wifiMode = WifiMode.UNKNOWN;
    List<? extends NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = getNetworkInterfaceConfigs();
    for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
        if (netInterfaceConfig.getName().equals(ifaceName)) {
            List<? extends NetInterfaceAddressConfig> netInterfaceAddresses = netInterfaceConfig.getNetInterfaceAddresses();
            if (netInterfaceAddresses != null) {
                for (NetInterfaceAddressConfig netInterfaceAddress : netInterfaceAddresses) {
                    if (netInterfaceAddress instanceof WifiInterfaceAddressConfig) {
                        wifiMode = ((WifiInterfaceAddressConfig) netInterfaceAddress).getMode();
                    }
                }
            }
            break;
        }
    }
    try {
        if (wifiMode == WifiMode.MASTER) {
            reloadKernelModule(ifaceName, WifiMode.INFRA);
            WpaSupplicantConfigWriter wpaSupplicantConfigWriter = WpaSupplicantConfigWriter.getInstance();
            wpaSupplicantConfigWriter.generateTempWpaSupplicantConf();
            s_logger.debug("getWifiHotspots() :: Starting temporary instance of wpa_supplicant");
            StringBuilder key = new StringBuilder("net.interface." + ifaceName + ".config.wifi.infra.driver");
            String driver = KuranetConfig.getProperty(key.toString());
            WpaSupplicantManager.startTemp(ifaceName, WifiMode.INFRA, driver);
            wifiModeWait(ifaceName, WifiMode.INFRA, 10);
        }
        s_logger.info("getWifiHotspots() :: scanning for available access points ...");
        IScanTool scanTool = ScanTool.get(ifaceName);
        if (scanTool != null) {
            List<WifiAccessPoint> wifiAccessPoints = scanTool.scan();
            for (WifiAccessPoint wap : wifiAccessPoints) {
                if (wap.getSSID() == null || wap.getSSID().length() == 0) {
                    s_logger.debug("Skipping hidden SSID");
                    continue;
                }
                // if (!wap.getSSID().matches(SSID_REGEXP)){
                // s_logger.debug("Skipping undesired SSID");
                // continue;
                // }
                s_logger.trace("getWifiHotspots() :: SSID={}", wap.getSSID());
                s_logger.trace("getWifiHotspots() :: Signal={}", wap.getStrength());
                s_logger.trace("getWifiHotspots() :: Frequency={}", wap.getFrequency());
                byte[] baMacAddress = wap.getHardwareAddress();
                StringBuffer sbMacAddress = new StringBuffer();
                for (int i = 0; i < baMacAddress.length; i++) {
                    sbMacAddress.append(String.format("%02x", baMacAddress[i] & 0x0ff).toUpperCase());
                    if (i < baMacAddress.length - 1) {
                        sbMacAddress.append(':');
                    }
                }
                WifiSecurity wifiSecurity = WifiSecurity.NONE;
                EnumSet<WifiSecurity> esWpaSecurity = wap.getWpaSecurity();
                if (esWpaSecurity != null && !esWpaSecurity.isEmpty()) {
                    wifiSecurity = WifiSecurity.SECURITY_WPA;
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        s_logger.trace("getWifiHotspots() :: WPA Security={}", itWpaSecurity.next());
                    }
                }
                EnumSet<WifiSecurity> esRsnSecurity = wap.getRsnSecurity();
                if (esRsnSecurity != null && !esRsnSecurity.isEmpty()) {
                    if (wifiSecurity == WifiSecurity.SECURITY_WPA) {
                        wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                    } else {
                        wifiSecurity = WifiSecurity.SECURITY_WPA2;
                    }
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        s_logger.trace("getWifiHotspots() :: RSN Security={}", itRsnSecurity.next());
                    }
                }
                if (wifiSecurity == WifiSecurity.NONE) {
                    List<String> capabilities = wap.getCapabilities();
                    if (capabilities != null && !capabilities.isEmpty()) {
                        for (String capab : capabilities) {
                            if (capab.equals("Privacy")) {
                                wifiSecurity = WifiSecurity.SECURITY_WEP;
                                break;
                            }
                        }
                    }
                }
                int frequency = (int) wap.getFrequency();
                int channel = frequencyMhz2Channel(frequency);
                EnumSet<WifiSecurity> pairCiphers = EnumSet.noneOf(WifiSecurity.class);
                EnumSet<WifiSecurity> groupCiphers = EnumSet.noneOf(WifiSecurity.class);
                if (wifiSecurity == WifiSecurity.SECURITY_WPA_WPA2) {
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        WifiSecurity securityEntry = itWpaSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        WifiSecurity securityEntry = itRsnSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            if (!pairCiphers.contains(securityEntry)) {
                                pairCiphers.add(securityEntry);
                            }
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            if (!groupCiphers.contains(securityEntry)) {
                                groupCiphers.add(securityEntry);
                            }
                        }
                    }
                } else if (wifiSecurity == WifiSecurity.SECURITY_WPA) {
                    Iterator<WifiSecurity> itWpaSecurity = esWpaSecurity.iterator();
                    while (itWpaSecurity.hasNext()) {
                        WifiSecurity securityEntry = itWpaSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                } else if (wifiSecurity == WifiSecurity.SECURITY_WPA2) {
                    Iterator<WifiSecurity> itRsnSecurity = esRsnSecurity.iterator();
                    while (itRsnSecurity.hasNext()) {
                        WifiSecurity securityEntry = itRsnSecurity.next();
                        if (securityEntry == WifiSecurity.PAIR_CCMP || securityEntry == WifiSecurity.PAIR_TKIP) {
                            pairCiphers.add(securityEntry);
                        } else if (securityEntry == WifiSecurity.GROUP_CCMP || securityEntry == WifiSecurity.GROUP_TKIP) {
                            groupCiphers.add(securityEntry);
                        }
                    }
                }
                WifiHotspotInfo wifiHotspotInfo = new WifiHotspotInfo(wap.getSSID(), sbMacAddress.toString(), 0 - wap.getStrength(), channel, frequency, wifiSecurity, pairCiphers, groupCiphers);
                mWifiHotspotInfo.put(wap.getSSID(), wifiHotspotInfo);
            }
        }
        if (wifiMode == WifiMode.MASTER) {
            if (WpaSupplicantManager.isTempRunning()) {
                s_logger.debug("getWifiHotspots() :: stoping temporary instance of wpa_supplicant");
                WpaSupplicantManager.stop(ifaceName);
            }
            reloadKernelModule(ifaceName, WifiMode.MASTER);
        }
    } catch (Throwable t) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, t, "scan operation has failed");
    }
    return mWifiHotspotInfo;
}
Also used : HashMap(java.util.HashMap) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) IScanTool(org.eclipse.kura.linux.net.util.IScanTool) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) Iterator(java.util.Iterator) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WpaSupplicantConfigWriter(org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 19 with WifiMode

use of org.eclipse.kura.net.wifi.WifiMode in project kura by eclipse.

the class iwlistScanTool method parse.

private List<WifiAccessPoint> parse() throws Exception {
    List<WifiAccessPoint> wifiAccessPoints = new ArrayList<WifiAccessPoint>();
    // get the output
    BufferedReader br = new BufferedReader(new InputStreamReader(this.m_process.getInputStream()));
    String line = null;
    String ssid = null;
    List<Long> bitrate = null;
    long frequency = -1;
    byte[] hardwareAddress = null;
    WifiMode mode = null;
    EnumSet<WifiSecurity> rsnSecurity = null;
    int strength = -1;
    EnumSet<WifiSecurity> wpaSecurity = null;
    while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("Cell")) {
            // new AP
            if (ssid != null) {
                WifiAccessPointImpl wifiAccessPoint = new WifiAccessPointImpl(ssid);
                wifiAccessPoint.setBitrate(bitrate);
                wifiAccessPoint.setFrequency(frequency);
                wifiAccessPoint.setHardwareAddress(hardwareAddress);
                wifiAccessPoint.setMode(mode);
                wifiAccessPoint.setRsnSecurity(rsnSecurity);
                wifiAccessPoint.setStrength(strength);
                wifiAccessPoint.setWpaSecurity(wpaSecurity);
                wifiAccessPoints.add(wifiAccessPoint);
            }
            // reset
            ssid = null;
            bitrate = null;
            frequency = -1;
            hardwareAddress = null;
            mode = null;
            rsnSecurity = null;
            strength = -1;
            wpaSecurity = null;
            // parse out the MAC
            StringTokenizer st = new StringTokenizer(line, " ");
            // eat Cell
            st.nextToken();
            // eat Cell #
            st.nextToken();
            // eat '-'
            st.nextToken();
            // eat 'Address:'
            st.nextToken();
            String macAddressString = st.nextToken();
            if (macAddressString != null) {
                hardwareAddress = NetworkUtil.macToBytes(macAddressString);
            }
        } else if (line.startsWith("ESSID:")) {
            ssid = line.substring("ESSID:".length() + 1, line.length() - 1);
        } else if (line.startsWith("Quality=")) {
            StringTokenizer st = new StringTokenizer(line, " ");
            // eat 'Quality='
            st.nextToken();
            // eat 'Signal'
            st.nextToken();
            String signalLevel = st.nextToken();
            if (signalLevel != null) {
                signalLevel = signalLevel.substring(signalLevel.indexOf('=') + 1);
                if (signalLevel.contains("/")) {
                    // Could also be of format 39/100
                    final String[] parts = signalLevel.split("/");
                    strength = (int) Float.parseFloat(parts[0]);
                    strength = SignalStrengthConversion.getRssi(strength);
                } else {
                    strength = (int) Float.parseFloat(signalLevel);
                }
                strength = Math.abs(strength);
            }
        } else if (line.startsWith("Mode:")) {
            line = line.substring("Mode:".length());
            if (line.equals("Master")) {
                mode = WifiMode.MASTER;
            }
        } else if (line.startsWith("Frequency:")) {
            line = line.substring("Frequency:".length(), line.indexOf(' '));
            frequency = (long) (Float.parseFloat(line) * 1000);
        } else if (line.startsWith("Bit Rates:")) {
            if (bitrate == null) {
                bitrate = new ArrayList<Long>();
            }
            line = line.substring("Bit Rates:".length());
            String[] bitRates = line.split(";");
            for (String rate : bitRates) {
                if (rate != null) {
                    rate = rate.trim();
                    if (rate.length() > 0) {
                        rate = rate.substring(0, rate.indexOf(' '));
                        bitrate.add((long) (Float.parseFloat(rate) * 1000000));
                    }
                }
            }
        } else if (line.contains("IE: IEEE 802.11i/WPA2")) {
            rsnSecurity = EnumSet.noneOf(WifiSecurity.class);
            boolean foundGroup = false;
            boolean foundPairwise = false;
            boolean foundAuthSuites = false;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.contains("Group Cipher")) {
                    foundGroup = true;
                    if (line.contains("CCMP")) {
                        rsnSecurity.add(WifiSecurity.GROUP_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        rsnSecurity.add(WifiSecurity.GROUP_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        rsnSecurity.add(WifiSecurity.GROUP_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        rsnSecurity.add(WifiSecurity.GROUP_WEP40);
                    }
                } else if (line.contains("Pairwise Ciphers")) {
                    foundPairwise = true;
                    if (line.contains("CCMP")) {
                        rsnSecurity.add(WifiSecurity.PAIR_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        rsnSecurity.add(WifiSecurity.PAIR_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        rsnSecurity.add(WifiSecurity.PAIR_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        rsnSecurity.add(WifiSecurity.PAIR_WEP40);
                    }
                } else if (line.contains("Authentication Suites")) {
                    foundAuthSuites = true;
                    if (line.contains("802_1X")) {
                        rsnSecurity.add(WifiSecurity.KEY_MGMT_802_1X);
                    }
                    if (line.contains("PSK")) {
                        rsnSecurity.add(WifiSecurity.KEY_MGMT_PSK);
                    }
                } else {
                    s_logger.debug("Ignoring line in RSN: {}", line);
                }
                if (foundGroup && foundPairwise && foundAuthSuites) {
                    break;
                }
            }
        } else if (line.contains("IE: WPA Version")) {
            wpaSecurity = EnumSet.noneOf(WifiSecurity.class);
            boolean foundGroup = false;
            boolean foundPairwise = false;
            boolean foundAuthSuites = false;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.contains("Group Cipher")) {
                    foundGroup = true;
                    if (line.contains("CCMP")) {
                        wpaSecurity.add(WifiSecurity.GROUP_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        wpaSecurity.add(WifiSecurity.GROUP_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        wpaSecurity.add(WifiSecurity.GROUP_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        wpaSecurity.add(WifiSecurity.GROUP_WEP40);
                    }
                } else if (line.contains("Pairwise Ciphers")) {
                    foundPairwise = true;
                    if (line.contains("CCMP")) {
                        wpaSecurity.add(WifiSecurity.PAIR_CCMP);
                    }
                    if (line.contains("TKIP")) {
                        wpaSecurity.add(WifiSecurity.PAIR_TKIP);
                    }
                    if (line.contains("WEP104")) {
                        wpaSecurity.add(WifiSecurity.PAIR_WEP104);
                    }
                    if (line.contains("WEP40")) {
                        wpaSecurity.add(WifiSecurity.PAIR_WEP40);
                    }
                } else if (line.contains("Authentication Suites")) {
                    foundAuthSuites = true;
                    if (line.contains("802_1X")) {
                        wpaSecurity.add(WifiSecurity.KEY_MGMT_802_1X);
                    }
                    if (line.contains("PSK")) {
                        wpaSecurity.add(WifiSecurity.KEY_MGMT_PSK);
                    }
                } else {
                    s_logger.debug("Ignoring line in WPA: {}", line);
                }
                if (foundGroup && foundPairwise && foundAuthSuites) {
                    break;
                }
            }
        }
    }
    // store the last one
    if (ssid != null) {
        WifiAccessPointImpl wifiAccessPoint = new WifiAccessPointImpl(ssid);
        wifiAccessPoint.setBitrate(bitrate);
        wifiAccessPoint.setFrequency(frequency);
        wifiAccessPoint.setHardwareAddress(hardwareAddress);
        wifiAccessPoint.setMode(mode);
        wifiAccessPoint.setRsnSecurity(rsnSecurity);
        wifiAccessPoint.setStrength(strength);
        wifiAccessPoint.setWpaSecurity(wpaSecurity);
        wifiAccessPoints.add(wifiAccessPoint);
    }
    br.close();
    return wifiAccessPoints;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) StringTokenizer(java.util.StringTokenizer) WifiAccessPointImpl(org.eclipse.kura.core.net.WifiAccessPointImpl) WifiMode(org.eclipse.kura.net.wifi.WifiMode) BufferedReader(java.io.BufferedReader) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity)

Example 20 with WifiMode

use of org.eclipse.kura.net.wifi.WifiMode in project kura by eclipse.

the class LinuxNetworkUtil method getWifiMode.

/*
     * Returns WifiMode.UNKNOWN if the interface is not found or on error
     */
public static WifiMode getWifiMode(String ifaceName) throws KuraException {
    // ignore logical interfaces like "1-1.2"
    if (Character.isDigit(ifaceName.charAt(0))) {
        return WifiMode.UNKNOWN;
    }
    WifiMode mode = WifiMode.UNKNOWN;
    SafeProcess procIw = null;
    SafeProcess procIwConfig = null;
    BufferedReader br1 = null;
    BufferedReader br2 = null;
    String line = null;
    try {
        if (toolExists("iw")) {
            procIw = ProcessUtil.exec("iw dev " + ifaceName + " info");
            if (procIw.waitFor() != 0) {
                s_logger.warn("error executing command --- iw --- exit value = {}; will try iwconfig ...", procIw.exitValue());
            // fallback to iwconfig
            }
            br1 = new BufferedReader(new InputStreamReader(procIw.getInputStream()));
            while ((line = br1.readLine()) != null) {
                int index = line.indexOf("type ");
                if (index > -1) {
                    s_logger.debug("line: {}", line);
                    String sMode = line.substring(index + "type ".length());
                    if ("AP".equals(sMode)) {
                        mode = WifiMode.MASTER;
                    } else if ("managed".equals(sMode)) {
                        mode = WifiMode.INFRA;
                    }
                    break;
                }
            }
        }
        if (mode.equals(WifiMode.UNKNOWN)) {
            if (toolExists("iwconfig")) {
                procIwConfig = ProcessUtil.exec("iwconfig " + ifaceName);
                if (procIwConfig.waitFor() != 0) {
                    s_logger.error("error executing command --- iwconfig --- exit value = {}", procIwConfig.exitValue());
                    // FIXME: throw exception
                    return mode;
                }
                // get the output
                br2 = new BufferedReader(new InputStreamReader(procIwConfig.getInputStream()));
                while ((line = br2.readLine()) != null) {
                    int index = line.indexOf("Mode:");
                    if (index > -1) {
                        s_logger.debug("line: {}", line);
                        StringTokenizer st = new StringTokenizer(line.substring(index));
                        String modeStr = st.nextToken().substring(5);
                        if ("Managed".equals(modeStr)) {
                            mode = WifiMode.INFRA;
                        } else if ("Master".equals(modeStr)) {
                            mode = WifiMode.MASTER;
                        } else if ("Ad-Hoc".equals(modeStr)) {
                            mode = WifiMode.ADHOC;
                        }
                        break;
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } catch (InterruptedException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        if (br1 != null) {
            try {
                br1.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
        if (br2 != null) {
            try {
                br2.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
        if (procIw != null) {
            ProcessUtil.destroy(procIw);
        }
        if (procIwConfig != null) {
            ProcessUtil.destroy(procIwConfig);
        }
    }
    return mode;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Aggregations

WifiMode (org.eclipse.kura.net.wifi.WifiMode)21 NetConfig (org.eclipse.kura.net.NetConfig)13 KuraException (org.eclipse.kura.KuraException)12 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)12 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)11 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)10 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 UnknownHostException (java.net.UnknownHostException)5 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)5 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)5 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)4 IPAddress (org.eclipse.kura.net.IPAddress)4 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)4 WifiAccessPointImpl (org.eclipse.kura.core.net.WifiAccessPointImpl)3 WifiInterfaceConfigImpl (org.eclipse.kura.core.net.WifiInterfaceConfigImpl)3 DhcpServerConfigIP4 (org.eclipse.kura.net.dhcp.DhcpServerConfigIP4)3 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)3 BufferedReader (java.io.BufferedReader)2