Search in sources :

Example 1 with HspaCellularModem

use of org.eclipse.kura.net.admin.modem.HspaCellularModem in project kura by eclipse.

the class ModemMonitorServiceImpl method monitor.

private void monitor() {
    synchronized (s_lock) {
        HashMap<String, InterfaceState> newInterfaceStatuses = new HashMap<String, InterfaceState>();
        if (this.m_modems == null || this.m_modems.isEmpty()) {
            return;
        }
        for (Map.Entry<String, CellularModem> modemEntry : this.m_modems.entrySet()) {
            CellularModem modem = modemEntry.getValue();
            // get signal strength only if somebody needs it
            if (this.m_listeners != null && !this.m_listeners.isEmpty()) {
                for (ModemMonitorListener listener : this.m_listeners) {
                    try {
                        int rssi = modem.getSignalStrength();
                        listener.setCellularSignalLevel(rssi);
                    } catch (KuraException e) {
                        listener.setCellularSignalLevel(0);
                        s_logger.error("monitor() :: Failed to obtain signal strength - {}", e);
                    }
                }
            }
            IModemLinkService pppService = null;
            PppState pppState = null;
            NetInterfaceStatus netInterfaceStatus = getNetInterfaceStatus(modem.getConfiguration());
            try {
                String ifaceName = this.m_networkService.getModemPppPort(modem.getModemDevice());
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN && ifaceName != null) {
                    pppService = PppFactory.obtainPppService(ifaceName, modem.getDataPort());
                    pppState = pppService.getPppState();
                    if (this.m_pppState != pppState) {
                        s_logger.info("monitor() :: previous PppState={}", this.m_pppState);
                        s_logger.info("monitor() :: current PppState={}", pppState);
                    }
                    if (pppState == PppState.NOT_CONNECTED) {
                        boolean checkIfSimCardReady = false;
                        List<ModemTechnologyType> modemTechnologyTypes = modem.getTechnologyTypes();
                        for (ModemTechnologyType modemTechnologyType : modemTechnologyTypes) {
                            if (modemTechnologyType == ModemTechnologyType.GSM_GPRS || modemTechnologyType == ModemTechnologyType.UMTS || modemTechnologyType == ModemTechnologyType.HSDPA || modemTechnologyType == ModemTechnologyType.HSPA) {
                                checkIfSimCardReady = true;
                                break;
                            }
                        }
                        if (checkIfSimCardReady) {
                            if (((HspaCellularModem) modem).isSimCardReady()) {
                                s_logger.info("monitor() :: !!! SIM CARD IS READY !!! connecting ...");
                                pppService.connect();
                                if (this.m_pppState == PppState.NOT_CONNECTED) {
                                    this.m_resetTimerStart = System.currentTimeMillis();
                                }
                            } else {
                                s_logger.warn("monitor() :: ! SIM CARD IS NOT READY !");
                            }
                        } else {
                            s_logger.info("monitor() :: connecting ...");
                            pppService.connect();
                            if (this.m_pppState == PppState.NOT_CONNECTED) {
                                this.m_resetTimerStart = System.currentTimeMillis();
                            }
                        }
                    } else if (pppState == PppState.IN_PROGRESS) {
                        long modemResetTout = getModemResetTimeoutMsec(ifaceName, modem.getConfiguration());
                        if (modemResetTout > 0) {
                            long timeElapsed = System.currentTimeMillis() - this.m_resetTimerStart;
                            if (timeElapsed > modemResetTout) {
                                // reset modem
                                s_logger.info("monitor() :: Modem Reset TIMEOUT !!!");
                                pppService.disconnect();
                                if (modem.isGpsEnabled() && !disableModemGps(modem)) {
                                    s_logger.error("monitor() :: Failed to disable modem GPS");
                                }
                                modem.reset();
                                pppState = PppState.NOT_CONNECTED;
                                this.m_resetTimerStart = System.currentTimeMillis();
                            } else {
                                int timeTillReset = (int) (modemResetTout - timeElapsed) / 1000;
                                s_logger.info("monitor() :: PPP connection in progress. Modem will be reset in {} sec if not connected", timeTillReset);
                            }
                        }
                    } else if (pppState == PppState.CONNECTED) {
                        this.m_resetTimerStart = System.currentTimeMillis();
                    }
                    this.m_pppState = pppState;
                    ConnectionInfo connInfo = new ConnectionInfoImpl(ifaceName);
                    InterfaceState interfaceState = new InterfaceState(ifaceName, LinuxNetworkUtil.hasAddress(ifaceName), pppState == PppState.CONNECTED, connInfo.getIpAddress());
                    newInterfaceStatuses.put(ifaceName, interfaceState);
                }
                if (modem.isGpsSupported()) {
                    if (isGpsEnabledInConfig(modem.getConfiguration())) {
                        if (modem instanceof HspaCellularModem) {
                            if (!modem.isGpsEnabled()) {
                                modem.enableGps();
                            }
                        }
                        postModemGpsEvent(modem, true);
                    }
                }
            } catch (Exception e) {
                s_logger.error("monitor() :: Exception", e);
                if (pppService != null && pppState != null) {
                    try {
                        s_logger.info("monitor() :: Exception :: PPPD disconnect");
                        pppService.disconnect();
                    } catch (KuraException e1) {
                        s_logger.error("monitor() :: Exception while disconnect", e1);
                    }
                    this.m_pppState = pppState;
                }
                if (modem.isGpsEnabled()) {
                    try {
                        if (!disableModemGps(modem)) {
                            s_logger.error("monitor() :: Failed to disable modem GPS");
                        }
                    } catch (KuraException e1) {
                        s_logger.error("monitor() :: Exception disableModemGps", e1);
                    }
                }
                try {
                    s_logger.info("monitor() :: Exception :: modem reset");
                    modem.reset();
                    this.m_resetTimerStart = System.currentTimeMillis();
                } catch (KuraException e1) {
                    s_logger.error("monitor() :: Exception modem.reset", e1);
                }
            }
        }
        // post event for any status changes
        checkStatusChange(this.m_interfaceStatuses, newInterfaceStatuses);
        this.m_interfaceStatuses = newInterfaceStatuses;
    }
}
Also used : ModemMonitorListener(org.eclipse.kura.net.modem.ModemMonitorListener) HashMap(java.util.HashMap) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IModemLinkService(org.eclipse.kura.net.admin.modem.IModemLinkService) HspaCellularModem(org.eclipse.kura.net.admin.modem.HspaCellularModem) KuraException(org.eclipse.kura.KuraException) HspaCellularModem(org.eclipse.kura.net.admin.modem.HspaCellularModem) CellularModem(org.eclipse.kura.net.modem.CellularModem) EvdoCellularModem(org.eclipse.kura.net.admin.modem.EvdoCellularModem) KuraException(org.eclipse.kura.KuraException) ModemTechnologyType(org.eclipse.kura.net.modem.ModemTechnologyType) ConnectionInfo(org.eclipse.kura.net.ConnectionInfo) Map(java.util.Map) HashMap(java.util.HashMap) PppState(org.eclipse.kura.net.admin.modem.PppState) ConnectionInfoImpl(org.eclipse.kura.linux.net.ConnectionInfoImpl)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 KuraException (org.eclipse.kura.KuraException)1 ConnectionInfoImpl (org.eclipse.kura.linux.net.ConnectionInfoImpl)1 ConnectionInfo (org.eclipse.kura.net.ConnectionInfo)1 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)1 EvdoCellularModem (org.eclipse.kura.net.admin.modem.EvdoCellularModem)1 HspaCellularModem (org.eclipse.kura.net.admin.modem.HspaCellularModem)1 IModemLinkService (org.eclipse.kura.net.admin.modem.IModemLinkService)1 PppState (org.eclipse.kura.net.admin.modem.PppState)1 CellularModem (org.eclipse.kura.net.modem.CellularModem)1 ModemMonitorListener (org.eclipse.kura.net.modem.ModemMonitorListener)1 ModemTechnologyType (org.eclipse.kura.net.modem.ModemTechnologyType)1