Search in sources :

Example 1 with WpaSupplicantConfigWriter

use of org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter in project kura by eclipse.

the class NetworkAdminServiceImpl method verifyWifiCredentials.

@Override
public boolean verifyWifiCredentials(String ifaceName, WifiConfig wifiConfig, int tout) {
    boolean ret = false;
    boolean restartSupplicant = false;
    WpaSupplicantConfigWriter wpaSupplicantConfigWriter = WpaSupplicantConfigWriter.getInstance();
    try {
        wpaSupplicantConfigWriter.generateTempWpaSupplicantConf(wifiConfig, ifaceName);
        if (WpaSupplicantManager.isRunning(ifaceName)) {
            s_logger.debug("verifyWifiCredentials() :: stoping wpa_supplicant");
            WpaSupplicantManager.stop(ifaceName);
            restartSupplicant = true;
        }
        s_logger.debug("verifyWifiCredentials() :: Restarting temporary instance of wpa_supplicant");
        WpaSupplicantManager.startTemp(ifaceName, WifiMode.INFRA, wifiConfig.getDriver());
        wifiModeWait(ifaceName, WifiMode.INFRA, 10);
        ret = isWifiConnectionCompleted(ifaceName, tout);
        if (WpaSupplicantManager.isTempRunning()) {
            s_logger.debug("verifyWifiCredentials() :: stopping temporary instance of wpa_supplicant");
            WpaSupplicantManager.stop(ifaceName);
        }
    } catch (KuraException e) {
        s_logger.warn("Exception while managing the temporary instance of the Wpa supplicant.", e);
    }
    if (restartSupplicant) {
        try {
            s_logger.debug("verifyWifiCredentials() :: Restarting wpa_supplicant");
            WpaSupplicant wpaSupplicant = WpaSupplicant.getWpaSupplicant(ifaceName);
            if (wpaSupplicant != null) {
                WpaSupplicantManager.start(ifaceName, wpaSupplicant.getMode(), wpaSupplicant.getDriver());
                if (isWifiConnectionCompleted(ifaceName, tout)) {
                    renewDhcpLease(ifaceName);
                }
            }
        } catch (KuraException e) {
            s_logger.warn("Exception while trying to restart the Wpa supplicant.", e);
        }
    }
    return ret;
}
Also used : WpaSupplicantConfigWriter(org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter) KuraException(org.eclipse.kura.KuraException) WpaSupplicant(org.eclipse.kura.linux.net.wifi.WpaSupplicant)

Example 2 with WpaSupplicantConfigWriter

use of org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter 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)

Aggregations

KuraException (org.eclipse.kura.KuraException)2 WpaSupplicantConfigWriter (org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 IScanTool (org.eclipse.kura.linux.net.util.IScanTool)1 WpaSupplicant (org.eclipse.kura.linux.net.wifi.WpaSupplicant)1 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)1 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)1 WifiHotspotInfo (org.eclipse.kura.net.wifi.WifiHotspotInfo)1 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)1 WifiMode (org.eclipse.kura.net.wifi.WifiMode)1 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)1