Search in sources :

Example 1 with WifiAccessPoint

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

the class iwScanTool method parse.

private List<WifiAccessPoint> parse() throws Exception {
    List<IWAPParser> apInfos = new ArrayList<IWAPParser>();
    IWAPParser currentAP = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(this.m_process.getInputStream()));
    try {
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.startsWith("scan aborted!")) {
                br.close();
                s_logger.warn("parse() :: scan operation was aborted");
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "iw scan operation was aborted");
            }
            if (line.startsWith("BSS")) {
                // new AP - parse out the MAC
                StringTokenizer st = new StringTokenizer(line, " ");
                // eat BSS
                st.nextToken();
                String macAddressString = st.nextToken().substring(0, 16);
                if (macAddressString != null) {
                    // Set this AP parser as the current one
                    currentAP = new IWAPParser(macAddressString);
                }
                // Add it to the list
                apInfos.add(currentAP);
            } else {
                // Must be an AP property line
                String propLine = line.trim();
                if (currentAP != null) {
                    // We're currently parsing an AP
                    try {
                        // Give this line to the AP parser
                        currentAP.parsePropLine(propLine);
                    } catch (Exception e) {
                        currentAP = null;
                        s_logger.error("Failed to parse line: {}; giving up on the current AP", propLine, e);
                    }
                }
            }
        }
    } finally {
        br.close();
    }
    // Generate list of WifiAccessPoint objects
    List<WifiAccessPoint> wifiAccessPoints = new ArrayList<WifiAccessPoint>();
    for (IWAPParser info : apInfos) {
        wifiAccessPoints.add(info.toWifiAccessPoint());
    }
    return wifiAccessPoints;
}
Also used : StringTokenizer(java.util.StringTokenizer) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) KuraException(org.eclipse.kura.KuraException)

Example 2 with WifiAccessPoint

use of org.eclipse.kura.net.wifi.WifiAccessPoint 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 3 with WifiAccessPoint

use of org.eclipse.kura.net.wifi.WifiAccessPoint 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 4 with WifiAccessPoint

use of org.eclipse.kura.net.wifi.WifiAccessPoint 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 5 with WifiAccessPoint

use of org.eclipse.kura.net.wifi.WifiAccessPoint 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

WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)9 ArrayList (java.util.ArrayList)4 KuraException (org.eclipse.kura.KuraException)4 IScanTool (org.eclipse.kura.linux.net.util.IScanTool)4 WifiMode (org.eclipse.kura.net.wifi.WifiMode)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 StringTokenizer (java.util.StringTokenizer)2 SafeProcess (org.eclipse.kura.core.util.SafeProcess)2 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)2 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)2 EnumSet (java.util.EnumSet)1 Iterator (java.util.Iterator)1 WifiAccessPointImpl (org.eclipse.kura.core.net.WifiAccessPointImpl)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 IPAddress (org.eclipse.kura.net.IPAddress)1 NetConfig (org.eclipse.kura.net.NetConfig)1