Search in sources :

Example 1 with ProcessStats

use of org.eclipse.kura.core.linux.util.ProcessStats in project kura by eclipse.

the class WpaSupplicantScan method scan.

public void scan() throws KuraException {
    String line = null;
    ProcessStats processStats = null;
    BufferedReader br = null;
    String sScanCommand = formSupplicantScanCommand(this.m_iface);
    // scan for wireless networks
    try {
        processStats = LinuxProcessUtil.startWithStats(sScanCommand);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    try {
        br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
        line = br.readLine();
        if (line == null || !line.equals("OK")) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, sScanCommand + " command failed");
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        try {
            br.close();
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    // get scan results
    String sScanResultsCommand = formSupplicantScanResultsCommand(this.m_iface);
    try {
        processStats = LinuxProcessUtil.startWithStats(sScanResultsCommand);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    try {
        br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
        String[] aScanInfo = null;
        while ((line = br.readLine()) != null) {
            aScanInfo = line.split("\\s+");
            if (aScanInfo.length > 0) {
                String macAddress = aScanInfo[0];
                int frequency = Integer.parseInt(aScanInfo[1]);
                int signalLevel = Integer.parseInt(aScanInfo[2]);
                int securityCode = 0;
                String sSecurity = aScanInfo[3].substring(aScanInfo[3].indexOf("[") + 1, aScanInfo[3].lastIndexOf(']'));
                StringTokenizer st = new StringTokenizer(sSecurity, "][");
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if (token.startsWith("WEP")) {
                        securityCode |= 1;
                    } else if (token.startsWith("WPA2")) {
                        securityCode |= 4;
                    } else if (token.startsWith("WPA")) {
                        securityCode |= 2;
                    }
                }
                WifiSecurity wifiSecurity = null;
                switch(securityCode) {
                    case 1:
                        wifiSecurity = WifiSecurity.SECURITY_WEP;
                        break;
                    case 2:
                        wifiSecurity = WifiSecurity.SECURITY_WPA;
                        break;
                    case 4:
                        wifiSecurity = WifiSecurity.SECURITY_WPA2;
                        break;
                    case 6:
                        wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                        break;
                    default:
                        wifiSecurity = WifiSecurity.NONE;
                }
                String ssid = aScanInfo[4];
                WifiHotspotInfo wifiHotspotInfo = new WifiHotspotInfo(ssid, macAddress, signalLevel, frequencyMhz2Channel(frequency), frequency, wifiSecurity);
                this.m_listWifiHotspotInfo.add(wifiHotspotInfo);
            }
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        try {
            br.close();
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
}
Also used : ProcessStats(org.eclipse.kura.core.linux.util.ProcessStats) StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) KuraException(org.eclipse.kura.KuraException) WifiHotspotInfo(org.eclipse.kura.net.wifi.WifiHotspotInfo)

Example 2 with ProcessStats

use of org.eclipse.kura.core.linux.util.ProcessStats in project kura by eclipse.

the class PppLinux method getPid.

private static int getPid(String iface, String port) throws KuraException {
    int pid = -1;
    synchronized (s_lock) {
        String[] pgrepCmd = { "pgrep", "-f", "" };
        pgrepCmd[2] = formConnectCommand(iface, port);
        BufferedReader br = null;
        try {
            ProcessStats processStats = LinuxProcessUtil.startWithStats(pgrepCmd);
            br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
            String line = br.readLine();
            if (line != null && line.length() > 0) {
                pid = Integer.parseInt(line);
                s_logger.trace("getPid() :: pppd pid={} for {}", pid, iface);
            }
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                s_logger.warn("Error closing input stream", e);
            }
        }
    }
    return pid;
}
Also used : ProcessStats(org.eclipse.kura.core.linux.util.ProcessStats) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException)

Example 3 with ProcessStats

use of org.eclipse.kura.core.linux.util.ProcessStats in project kura by eclipse.

the class SerialModemDriver method unlockSerialPort.

private void unlockSerialPort() throws Exception {
    String dataPort = this.m_serialModemComm.getDataPort();
    dataPort = dataPort.substring(dataPort.lastIndexOf("/") + 1);
    File fLockFile = new File("/var/lock/LCK.." + dataPort);
    if (fLockFile.exists()) {
        s_logger.warn("lock exists for the {} device", dataPort);
        BufferedReader br = new BufferedReader(new FileReader(fLockFile));
        int lockedPid = Integer.parseInt(br.readLine().trim());
        br.close();
        ProcessStats processStats = LinuxProcessUtil.startWithStats("pgrep pppd");
        br = new BufferedReader(new InputStreamReader(processStats.getInputStream()));
        String spid = null;
        int pidToKill = -1;
        while ((spid = br.readLine()) != null) {
            int pid = Integer.parseInt(spid);
            if (pid == lockedPid) {
                pidToKill = pid;
                break;
            }
        }
        br.close();
        if (pidToKill > 0) {
            s_logger.info("killing pppd that locks the {} device", dataPort);
            int stat = LinuxProcessUtil.start("kill " + pidToKill, true);
            if (stat == 0) {
                s_logger.info("deleting " + fLockFile.getName());
                fLockFile.delete();
            }
        }
    }
}
Also used : ProcessStats(org.eclipse.kura.core.linux.util.ProcessStats) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 ProcessStats (org.eclipse.kura.core.linux.util.ProcessStats)3 KuraException (org.eclipse.kura.KuraException)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1 WifiHotspotInfo (org.eclipse.kura.net.wifi.WifiHotspotInfo)1 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)1