Search in sources :

Example 1 with ModemMonitorListener

use of org.eclipse.kura.net.modem.ModemMonitorListener 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)

Example 2 with ModemMonitorListener

use of org.eclipse.kura.net.modem.ModemMonitorListener in project kura by eclipse.

the class ModemMonitorServiceImpl method activate.

protected void activate(ComponentContext componentContext) {
    // save the bundle context
    this.m_ctx = componentContext;
    this.m_pppState = PppState.NOT_CONNECTED;
    this.m_resetTimerStart = 0L;
    Dictionary<String, String[]> d = new Hashtable<String, String[]>();
    d.put(EventConstants.EVENT_TOPIC, EVENT_TOPICS);
    this.m_ctx.getBundleContext().registerService(EventHandler.class.getName(), this, d);
    this.m_modems = new HashMap<String, CellularModem>();
    this.m_interfaceStatuses = new HashMap<String, InterfaceState>();
    this.m_listeners = new ArrayList<ModemMonitorListener>();
    stopThread = new AtomicBoolean();
    // track currently installed modems
    try {
        this.m_networkConfig = this.m_netConfigService.getNetworkConfiguration();
        for (NetInterface<? extends NetInterfaceAddress> netInterface : this.m_networkService.getNetworkInterfaces()) {
            if (netInterface instanceof ModemInterface) {
                ModemDevice modemDevice = ((ModemInterface<?>) netInterface).getModemDevice();
                trackModem(modemDevice);
            }
        }
    } catch (Exception e) {
        s_logger.error("Error getting installed modems", e);
    }
    stopThread.set(false);
    this.m_executor = Executors.newSingleThreadExecutor();
    task = this.m_executor.submit(new Runnable() {

        @Override
        public void run() {
            while (!stopThread.get()) {
                Thread.currentThread().setName("ModemMonitor");
                try {
                    monitor();
                    monitorWait();
                } catch (InterruptedException interruptedException) {
                    Thread.interrupted();
                    s_logger.debug("modem monitor interrupted - {}", interruptedException);
                } catch (Throwable t) {
                    s_logger.error("activate() :: Exception while monitoring cellular connection {}", t);
                }
            }
        }
    });
    this.m_serviceActivated = true;
    s_logger.debug("ModemMonitor activated and ready to receive events");
}
Also used : ModemMonitorListener(org.eclipse.kura.net.modem.ModemMonitorListener) Hashtable(java.util.Hashtable) EventHandler(org.osgi.service.event.EventHandler) ModemDevice(org.eclipse.kura.net.modem.ModemDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) KuraException(org.eclipse.kura.KuraException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HspaCellularModem(org.eclipse.kura.net.admin.modem.HspaCellularModem) CellularModem(org.eclipse.kura.net.modem.CellularModem) EvdoCellularModem(org.eclipse.kura.net.admin.modem.EvdoCellularModem) ModemInterface(org.eclipse.kura.net.modem.ModemInterface)

Aggregations

KuraException (org.eclipse.kura.KuraException)2 EvdoCellularModem (org.eclipse.kura.net.admin.modem.EvdoCellularModem)2 HspaCellularModem (org.eclipse.kura.net.admin.modem.HspaCellularModem)2 CellularModem (org.eclipse.kura.net.modem.CellularModem)2 ModemMonitorListener (org.eclipse.kura.net.modem.ModemMonitorListener)2 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ConnectionInfoImpl (org.eclipse.kura.linux.net.ConnectionInfoImpl)1 ConnectionInfo (org.eclipse.kura.net.ConnectionInfo)1 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)1 IModemLinkService (org.eclipse.kura.net.admin.modem.IModemLinkService)1 PppState (org.eclipse.kura.net.admin.modem.PppState)1 ModemDevice (org.eclipse.kura.net.modem.ModemDevice)1 ModemInterface (org.eclipse.kura.net.modem.ModemInterface)1 ModemTechnologyType (org.eclipse.kura.net.modem.ModemTechnologyType)1 SerialModemDevice (org.eclipse.kura.net.modem.SerialModemDevice)1 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)1 EventHandler (org.osgi.service.event.EventHandler)1