Search in sources :

Example 1 with IScanTool

use of org.eclipse.kura.linux.net.util.IScanTool in project kura by eclipse.

the class WifiMonitorServiceImpl method getSignalLevel.

@Override
public int getSignalLevel(String interfaceName, String ssid) throws KuraException {
    int rssi = 0;
    InterfaceState wifiState = this.m_interfaceStatuses.get(interfaceName);
    if (wifiState != null && ssid != null) {
        if (wifiState.isUp()) {
            s_logger.trace("getSignalLevel() :: using 'iw dev wlan0 link' command ...");
            // IwLinkTool iwLinkTool = new IwLinkTool(interfaceName);
            Collection<String> supportedWifiOptions = WifiOptions.getSupportedOptions(interfaceName);
            LinkTool linkTool = null;
            if (supportedWifiOptions != null && supportedWifiOptions.size() > 0) {
                if (supportedWifiOptions.contains(WifiOptions.WIFI_MANAGED_DRIVER_NL80211)) {
                    linkTool = new IwLinkTool(interfaceName);
                } else if (supportedWifiOptions.contains(WifiOptions.WIFI_MANAGED_DRIVER_WEXT)) {
                    linkTool = new iwconfigLinkTool(interfaceName);
                }
            }
            if (linkTool != null && linkTool.get()) {
                if (linkTool.isLinkDetected()) {
                    rssi = linkTool.getSignal();
                    s_logger.debug("getSignalLevel() :: rssi={} (using 'iw dev wlan0 link')", rssi);
                }
            }
        }
        if (rssi == 0) {
            s_logger.trace("getSignalLevel() :: using 'iw dev wlan0 scan' command ...");
            IScanTool scanTool = ScanTool.get(interfaceName);
            if (scanTool != null) {
                List<WifiAccessPoint> wifiAccessPoints = scanTool.scan();
                for (WifiAccessPoint wap : wifiAccessPoints) {
                    if (ssid.equals(wap.getSSID())) {
                        if (wap.getStrength() > 0) {
                            rssi = 0 - wap.getStrength();
                            s_logger.debug("getSignalLevel() :: rssi={} (using 'iw dev wlan0 scan')", rssi);
                        }
                        break;
                    }
                }
            }
        }
    }
    return rssi;
}
Also used : WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) IScanTool(org.eclipse.kura.linux.net.util.IScanTool) IwLinkTool(org.eclipse.kura.linux.net.util.IwLinkTool) org.eclipse.kura.linux.net.util.iwconfigLinkTool(org.eclipse.kura.linux.net.util.iwconfigLinkTool) IwLinkTool(org.eclipse.kura.linux.net.util.IwLinkTool) org.eclipse.kura.linux.net.util.iwconfigLinkTool(org.eclipse.kura.linux.net.util.iwconfigLinkTool) LinkTool(org.eclipse.kura.linux.net.util.LinkTool) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint)

Example 2 with IScanTool

use of org.eclipse.kura.linux.net.util.IScanTool in project kura by eclipse.

the class WifiMonitorServiceImpl method isAccessPointAvailable.

private boolean isAccessPointAvailable(String interfaceName, String ssid) throws KuraException {
    boolean available = false;
    if (ssid != null) {
        IScanTool scanTool = ScanTool.get(interfaceName);
        if (scanTool != null) {
            List<WifiAccessPoint> wifiAccessPoints = scanTool.scan();
            for (WifiAccessPoint wap : wifiAccessPoints) {
                if (ssid.equals(wap.getSSID())) {
                    s_logger.trace("isAccessPointAvailable() :: SSID={} is available :: strength={}", ssid, wap.getStrength());
                    available = Math.abs(wap.getStrength()) > 0;
                    break;
                }
            }
        }
    }
    return available;
}
Also used : WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) IScanTool(org.eclipse.kura.linux.net.util.IScanTool)

Example 3 with IScanTool

use of org.eclipse.kura.linux.net.util.IScanTool in project kura by eclipse.

the class NetworkServiceImpl method getWifiAccessPoints.

@Override
public List<WifiAccessPoint> getWifiAccessPoints(String wifiInterfaceName) throws KuraException {
    List<WifiAccessPoint> wifAccessPoints = null;
    IScanTool scanTool = ScanTool.get(wifiInterfaceName);
    if (scanTool != null) {
        wifAccessPoints = scanTool.scan();
    }
    return wifAccessPoints;
}
Also used : WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) IScanTool(org.eclipse.kura.linux.net.util.IScanTool)

Example 4 with IScanTool

use of org.eclipse.kura.linux.net.util.IScanTool 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

IScanTool (org.eclipse.kura.linux.net.util.IScanTool)4 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)4 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 KuraException (org.eclipse.kura.KuraException)1 IwLinkTool (org.eclipse.kura.linux.net.util.IwLinkTool)1 LinkTool (org.eclipse.kura.linux.net.util.LinkTool)1 org.eclipse.kura.linux.net.util.iwconfigLinkTool (org.eclipse.kura.linux.net.util.iwconfigLinkTool)1 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)1 WpaSupplicantConfigWriter (org.eclipse.kura.net.admin.visitor.linux.WpaSupplicantConfigWriter)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