Search in sources :

Example 1 with WifiRadioMode

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

the class GenericNetworkInterface method setWifiAccessPointConfig.

/**
 * Populate a WifiConfigIP4 object using the hostapd config
 *
 * @param wifiConfig
 * @throws KuraException
 */
private static void setWifiAccessPointConfig(WifiConfig wifiConfig) throws KuraException {
    wifiConfig.setMode(WifiMode.MASTER);
    Hostapd hostapd = Hostapd.getHostapd();
    if (hostapd != null) {
        wifiConfig.setSSID(hostapd.getSSID());
        // wifiConfig.setChannel((short)hostapd.getChannel());
        int[] channels = new int[1];
        channels[0] = hostapd.getChannel();
        wifiConfig.setChannels(channels);
        wifiConfig.setPasskey(hostapd.getPassword());
        // TODO: always true? is this needed?
        wifiConfig.setBroadcast(true);
        // security
        wifiConfig.setSecurity(hostapd.getSecurity());
        // hw mode
        WifiRadioMode radioMode = hostapd.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            wifiConfig.setHardwareMode("b");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            wifiConfig.setHardwareMode("g");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20 || radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above || radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            // TODO: specify these 'n' modes separately?
            wifiConfig.setHardwareMode("n");
        }
    }
}
Also used : WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) Hostapd(org.eclipse.kura.linux.net.wifi.Hostapd)

Example 2 with WifiRadioMode

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

the class HostapdConfigWriter method generateHostapdConf.

/*
     * This method generates hostapd configuration file
     */
private void generateHostapdConf(WifiConfig wifiConfig, String interfaceName, String interfaceDriver) throws Exception {
    s_logger.debug("Generating Hostapd Config");
    if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_NONE || wifiConfig.getSecurity() == WifiSecurity.NONE) {
        File outputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), "/src/main/resources/wifi/hostapd.conf_no_security");
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel " + wifiConfig.getChannels()[0] + " must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, outputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WEP) {
        File outputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), "/src/main/resources/wifi/hostapd.conf_wep");
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        String passKey = new String(wifiConfig.getPasskey().getPassword());
        if (passKey != null) {
            if (passKey.length() == 10) {
                // check to make sure it is all hex
                try {
                    Long.parseLong(passKey, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 26) {
                String part1 = passKey.substring(0, 13);
                String part2 = passKey.substring(13);
                try {
                    Long.parseLong(part1, 16);
                    Long.parseLong(part2, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 32) {
                String part1 = passKey.substring(0, 10);
                String part2 = passKey.substring(10, 20);
                String part3 = passKey.substring(20);
                try {
                    Long.parseLong(part1, 16);
                    Long.parseLong(part2, 16);
                    Long.parseLong(part3, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 5 || passKey.length() == 13 || passKey.length() == 16) {
                // 5, 13, or 16 ASCII characters
                passKey = toHex(passKey);
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else {
                throw KuraException.internalError("the WEP key (passwd) must be 10, 26, or 32 HEX characters in length");
            }
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, outputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA || wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2 || wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA_WPA2) {
        File tmpOutputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        /*
             * String resName = null;
             * if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA) {
             * resName = "/src/main/resources/wifi/hostapd.conf_master_wpa_psk";
             * } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2) {
             * resName = "/src/main/resources/wifi/hostapd.conf_master_wpa2_psk";
             * }
             */
        String resName = "/src/main/resources/wifi/hostapd.conf_master_wpa_wpa2_psk";
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), resName);
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "1");
        } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "2");
        } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA_WPA2) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "3");
        } else {
            throw KuraException.internalError("invalid WiFi Security");
        }
        WifiCiphers wifiCiphers = wifiConfig.getPairwiseCiphers();
        if (wifiCiphers == WifiCiphers.TKIP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "TKIP");
        } else if (wifiCiphers == WifiCiphers.CCMP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "CCMP");
        } else if (wifiCiphers == WifiCiphers.CCMP_TKIP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "CCMP TKIP");
        } else {
            throw KuraException.internalError("invalid WiFi Pairwise Ciphers");
        }
        String passKey = new String(wifiConfig.getPasskey().getPassword());
        if (wifiConfig.getPasskey() != null && passKey.trim().length() > 0) {
            if (passKey.length() < 8 || passKey.length() > 63) {
                throw KuraException.internalError("the WPA passphrase (passwd) must be between 8 (inclusive) and 63 (inclusive) characters in length: " + wifiConfig.getPasskey());
            } else {
                fileAsString = fileAsString.replaceFirst("KURA_PASSPHRASE", passKey.trim());
            }
        } else {
            throw KuraException.internalError("the passwd can not be null");
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, tmpOutputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else {
        s_logger.error("Unsupported security type: {}. It must be WifiSecurity.NONE, WifiSecurity.SECURITY_NONE, WifiSecurity.SECURITY_WEP, WifiSecurity.SECURITY_WPA, or WifiSecurity.SECURITY_WPA2", wifiConfig.getSecurity());
        throw KuraException.internalError("unsupported security type: " + wifiConfig.getSecurity());
    }
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) File(java.io.File) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException)

Example 3 with WifiRadioMode

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

the class HostapdConfigReader method getWifiHostConfig.

private static WifiConfig getWifiHostConfig(String ifaceName) throws KuraException {
    try {
        WifiConfig wifiConfig = new WifiConfig();
        wifiConfig.setMode(WifiMode.MASTER);
        File configFile = new File(HostapdManager.getHostapdConfigFileName(ifaceName));
        Properties hostapdProps = new Properties();
        s_logger.debug("parsing hostapd config file: " + configFile.getAbsolutePath());
        if (configFile.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                hostapdProps.load(fis);
            } finally {
                if (null != fis) {
                    fis.close();
                }
            }
            // remove any quotes around the values
            Enumeration<Object> keys = hostapdProps.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                String val = hostapdProps.getProperty(key);
                if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
                    hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
                }
            }
            String iface = hostapdProps.getProperty("interface");
            if (ifaceName != null && ifaceName.equals(iface)) {
                String driver = hostapdProps.getProperty("driver");
                String essid = hostapdProps.getProperty("ssid");
                int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
                int ignoreSSID = Integer.parseInt(hostapdProps.getProperty("ignore_broadcast_ssid"));
                // Determine radio mode
                WifiRadioMode wifiRadioMode = null;
                String hwModeStr = hostapdProps.getProperty("hw_mode");
                if ("a".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
                } else if ("b".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
                } else if ("g".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
                    if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
                        wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
                        String ht_capab = hostapdProps.getProperty("ht_capab");
                        if (ht_capab != null) {
                            if (ht_capab.contains("HT40+")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
                            } else if (ht_capab.contains("HT40-")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
                            }
                        }
                    }
                } else {
                    throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
                }
                // Determine security and pass
                WifiSecurity security = WifiSecurity.SECURITY_NONE;
                String password = "";
                if (hostapdProps.containsKey("wpa")) {
                    if ("1".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA;
                    } else if ("2".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA2;
                    } else if ("3".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA_WPA2;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                    if (hostapdProps.containsKey("wpa_passphrase")) {
                        password = hostapdProps.getProperty("wpa_passphrase");
                    } else if (hostapdProps.containsKey("wpa_psk")) {
                        password = hostapdProps.getProperty("wpa_psk");
                    } else {
                        throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
                    }
                } else if (hostapdProps.containsKey("wep_key0")) {
                    security = WifiSecurity.SECURITY_WEP;
                    password = hostapdProps.getProperty("wep_key0");
                }
                WifiCiphers pairwise = null;
                if (hostapdProps.containsKey("wpa_pairwise")) {
                    if ("TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.TKIP;
                    } else if ("CCMP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP;
                    } else if ("CCMP TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP_TKIP;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                }
                // Populate the config
                wifiConfig.setSSID(essid);
                wifiConfig.setDriver(driver);
                wifiConfig.setChannels(new int[] { channel });
                wifiConfig.setPasskey(password);
                wifiConfig.setSecurity(security);
                wifiConfig.setPairwiseCiphers(pairwise);
                wifiConfig.setRadioMode(wifiRadioMode);
                if (ignoreSSID == 0) {
                    wifiConfig.setIgnoreSSID(false);
                    wifiConfig.setBroadcast(true);
                } else {
                    wifiConfig.setIgnoreSSID(true);
                    wifiConfig.setBroadcast(false);
                }
                // hw mode
                if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211b) {
                    wifiConfig.setHardwareMode("b");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211g) {
                    wifiConfig.setHardwareMode("g");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT20 || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40above || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
                    // TODO: specify these 'n' modes separately?
                    wifiConfig.setHardwareMode("n");
                }
            }
        } else {
            s_logger.warn("getWifiHostConfig() :: {} file doesn't exist, will generate default wifiConfig", configFile.getName());
            wifiConfig.setSSID("kura_gateway");
            wifiConfig.setDriver("nl80211");
            wifiConfig.setChannels(new int[] { 11 });
            wifiConfig.setPasskey("");
            wifiConfig.setSecurity(WifiSecurity.SECURITY_NONE);
            wifiConfig.setPairwiseCiphers(WifiCiphers.CCMP);
            wifiConfig.setRadioMode(WifiRadioMode.RADIO_MODE_80211b);
            wifiConfig.setIgnoreSSID(false);
            wifiConfig.setBroadcast(true);
            wifiConfig.setHardwareMode("b");
        }
        return wifiConfig;
    } catch (Exception e) {
        s_logger.error("Exception getting WiFi configuration", e);
        throw KuraException.internalError(e);
    }
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) KuraException(org.eclipse.kura.KuraException)

Example 4 with WifiRadioMode

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

the class Hostapd method parseHostapdConf.

/*
     * Return a Hostapd instance from a given config file
     */
private static Hostapd parseHostapdConf(String filename) throws KuraException {
    FileInputStream fis = null;
    try {
        Hostapd hostapd = null;
        File configFile = new File(filename);
        Properties hostapdProps = new Properties();
        s_logger.debug("parsing hostapd config file: {}", configFile.getAbsolutePath());
        if (configFile.exists()) {
            fis = new FileInputStream(configFile);
            hostapdProps.load(fis);
            // remove any quotes around the values
            Enumeration<Object> keys = hostapdProps.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                String val = hostapdProps.getProperty(key);
                if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
                    hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
                }
            }
            String iface = hostapdProps.getProperty("interface");
            String driver = hostapdProps.getProperty("driver");
            String essid = hostapdProps.getProperty("ssid");
            int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
            // Determine radio mode
            WifiRadioMode wifiRadioMode = null;
            String hwModeStr = hostapdProps.getProperty("hw_mode");
            if ("a".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
            } else if ("b".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
            } else if ("g".equals(hwModeStr)) {
                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
                if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
                    String ht_capab = hostapdProps.getProperty("ht_capab");
                    if (ht_capab != null) {
                        if (ht_capab.contains("HT40+")) {
                            wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
                        } else if (ht_capab.contains("HT40-")) {
                            wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
                        }
                    }
                }
            } else {
                throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
            }
            // Determine security and pass
            WifiSecurity security = WifiSecurity.SECURITY_NONE;
            String passwd = "";
            if (hostapdProps.containsKey("wpa")) {
                if ("1".equals(hostapdProps.getProperty("wpa"))) {
                    security = WifiSecurity.SECURITY_WPA;
                } else if ("2".equals(hostapdProps.getProperty("wpa"))) {
                    security = WifiSecurity.SECURITY_WPA2;
                } else {
                    throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                }
                if (hostapdProps.containsKey("wpa_passphrase")) {
                    passwd = hostapdProps.getProperty("wpa_passphrase");
                } else if (hostapdProps.containsKey("wpa_psk")) {
                    passwd = hostapdProps.getProperty("wpa_psk");
                } else {
                    throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
                }
            } else if (hostapdProps.containsKey("wep_key0")) {
                security = WifiSecurity.SECURITY_WEP;
                passwd = hostapdProps.getProperty("wep_key0");
            }
            hostapd = new Hostapd(iface, driver, essid, wifiRadioMode, channel, security, passwd);
        } else {
            hostapd = new Hostapd();
        }
        return hostapd;
    } catch (Exception e) {
        e.printStackTrace();
        throw KuraException.internalError(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
    }
}
Also used : WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Example 5 with WifiRadioMode

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

the class NetworkConfiguration method getWifiConfig.

private static WifiConfig getWifiConfig(String netIfConfigPrefix, WifiMode mode, Map<String, Object> properties) throws KuraException {
    String key;
    WifiConfig wifiConfig = new WifiConfig();
    StringBuilder prefix = new StringBuilder(netIfConfigPrefix).append("wifi.").append(mode.toString().toLowerCase());
    // mode
    s_logger.trace("mode is {}", mode.toString());
    wifiConfig.setMode(mode);
    // ssid
    key = prefix + ".ssid";
    String ssid = (String) properties.get(key);
    if (ssid == null) {
        ssid = "";
    }
    s_logger.trace("SSID is {}", ssid);
    wifiConfig.setSSID(ssid);
    // driver
    key = prefix + ".driver";
    String driver = (String) properties.get(key);
    if (driver == null) {
        driver = "";
    }
    s_logger.trace("driver is {}", driver);
    wifiConfig.setDriver(driver);
    // security
    key = prefix + ".securityType";
    WifiSecurity wifiSecurity = WifiSecurity.NONE;
    String securityString = (String) properties.get(key);
    s_logger.trace("securityString is {}", securityString);
    if (securityString != null && !securityString.isEmpty()) {
        try {
            wifiSecurity = WifiSecurity.valueOf(securityString);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi security " + securityString);
        }
    }
    wifiConfig.setSecurity(wifiSecurity);
    // channels
    key = prefix + ".channel";
    String channelsString = (String) properties.get(key);
    s_logger.trace("channelsString is {}", channelsString);
    if (channelsString != null) {
        channelsString = channelsString.trim();
        if (channelsString.length() > 0) {
            StringTokenizer st = new StringTokenizer(channelsString, " ");
            int tokens = st.countTokens();
            if (tokens > 0) {
                int[] channels = new int[tokens];
                for (int i = 0; i < tokens; i++) {
                    String token = st.nextToken();
                    try {
                        channels[i] = Integer.parseInt(token);
                    } catch (Exception e) {
                        s_logger.error("Error parsing channels!", e);
                    }
                }
                wifiConfig.setChannels(channels);
            }
        }
    }
    // passphrase
    key = prefix + ".passphrase";
    Object psswdObj = properties.get(key);
    Password psswd = null;
    if (psswdObj instanceof Password) {
        psswd = (Password) psswdObj;
    } else if (psswdObj instanceof String) {
        char[] tempPsswd = ((String) psswdObj).toCharArray();
        psswd = new Password(tempPsswd);
    }
    String passphrase = new String(psswd.getPassword());
    s_logger.trace("passphrase is {}", passphrase);
    wifiConfig.setPasskey(passphrase);
    // hardware mode
    key = prefix + ".hardwareMode";
    String hwMode = (String) properties.get(key);
    if (hwMode == null) {
        hwMode = "";
    }
    s_logger.trace("hwMode is {}", hwMode);
    wifiConfig.setHardwareMode(hwMode);
    // ignore SSID
    key = prefix + ".ignoreSSID";
    boolean ignoreSSID = false;
    if (properties.get(key) != null) {
        ignoreSSID = (Boolean) properties.get(key);
        s_logger.trace("Ignore SSID is {}", ignoreSSID);
    } else {
        s_logger.trace("Ignore SSID is null");
    }
    wifiConfig.setIgnoreSSID(ignoreSSID);
    key = prefix + ".pairwiseCiphers";
    String pairwiseCiphers = (String) properties.get(key);
    if (pairwiseCiphers != null) {
        wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
    }
    if (mode == WifiMode.INFRA) {
        key = prefix + ".bgscan";
        String bgscan = (String) properties.get(key);
        if (bgscan == null) {
            bgscan = "";
        }
        s_logger.trace("bgscan is {}", bgscan);
        wifiConfig.setBgscan(new WifiBgscan(bgscan));
        /*
             * key = prefix + ".pairwiseCiphers";
             * String pairwiseCiphers = (String)properties.get(key);
             * if (pairwiseCiphers != null) {
             * wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
             * }
             */
        key = prefix + ".groupCiphers";
        String groupCiphers = (String) properties.get(key);
        if (groupCiphers != null) {
            wifiConfig.setGroupCiphers(WifiCiphers.valueOf(groupCiphers));
        }
        // ping access point?
        key = prefix + ".pingAccessPoint";
        boolean pingAccessPoint = false;
        if (properties.get(key) != null) {
            pingAccessPoint = (Boolean) properties.get(key);
            s_logger.trace("Ping Access Point is {}", pingAccessPoint);
        } else {
            s_logger.trace("Ping Access Point is null");
        }
        wifiConfig.setPingAccessPoint(pingAccessPoint);
    }
    // broadcast
    key = prefix + ".broadcast";
    Boolean broadcast = (Boolean) properties.get(key);
    if (broadcast != null) {
        wifiConfig.setBroadcast(broadcast);
    }
    s_logger.trace("hwMode is {}", hwMode);
    // radio mode
    key = prefix + ".radioMode";
    WifiRadioMode radioMode;
    String radioModeString = (String) properties.get(key);
    s_logger.trace("radioModeString is {}", radioModeString);
    if (radioModeString != null && !radioModeString.isEmpty()) {
        try {
            radioMode = WifiRadioMode.valueOf(radioModeString);
            wifiConfig.setRadioMode(radioMode);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi radio mode " + radioModeString);
        }
    }
    if (!wifiConfig.isValid()) {
        return null;
    } else {
        s_logger.trace("Returning wifiConfig: {}", wifiConfig);
        return wifiConfig;
    }
}
Also used : WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException) StringTokenizer(java.util.StringTokenizer) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) KuraException(org.eclipse.kura.KuraException) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiBgscan(org.eclipse.kura.net.wifi.WifiBgscan) Password(org.eclipse.kura.configuration.Password)

Aggregations

WifiRadioMode (org.eclipse.kura.net.wifi.WifiRadioMode)5 KuraException (org.eclipse.kura.KuraException)4 File (java.io.File)3 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 WifiCiphers (org.eclipse.kura.net.wifi.WifiCiphers)2 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)2 UnknownHostException (java.net.UnknownHostException)1 StringTokenizer (java.util.StringTokenizer)1 Password (org.eclipse.kura.configuration.Password)1 Hostapd (org.eclipse.kura.linux.net.wifi.Hostapd)1 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)1 WifiBgscan (org.eclipse.kura.net.wifi.WifiBgscan)1