Search in sources :

Example 1 with PppState

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

the class ModemMonitorServiceImpl method processNetworkConfigurationChangeEvent.

private void processNetworkConfigurationChangeEvent(NetworkConfiguration newNetworkConfig) {
    synchronized (s_lock) {
        if (this.m_modems == null || this.m_modems.isEmpty()) {
            return;
        }
        for (Map.Entry<String, CellularModem> modemEntry : this.m_modems.entrySet()) {
            String usbPort = modemEntry.getKey();
            CellularModem modem = modemEntry.getValue();
            try {
                String ifaceName = null;
                if (this.m_networkService != null) {
                    ifaceName = this.m_networkService.getModemPppPort(modem.getModemDevice());
                }
                if (ifaceName != null) {
                    List<NetConfig> oldNetConfigs = modem.getConfiguration();
                    NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = newNetworkConfig.getNetInterfaceConfig(ifaceName);
                    if (netInterfaceConfig == null) {
                        netInterfaceConfig = newNetworkConfig.getNetInterfaceConfig(usbPort);
                    }
                    List<NetConfig> newNetConfigs = null;
                    IModemLinkService pppService = null;
                    int ifaceNo = getInterfaceNumber(oldNetConfigs);
                    if (ifaceNo >= 0) {
                        pppService = PppFactory.obtainPppService(ifaceNo, modem.getDataPort());
                    }
                    if (netInterfaceConfig != null) {
                        newNetConfigs = getNetConfigs(netInterfaceConfig);
                    } else {
                        if (oldNetConfigs != null && pppService != null) {
                            if (!ifaceName.equals(pppService.getIfaceName())) {
                                StringBuilder key = new StringBuilder().append("net.interface.").append(ifaceName).append(".config.ip4.status");
                                String statusString = KuranetConfig.getProperty(key.toString());
                                NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
                                if (statusString != null && !statusString.isEmpty()) {
                                    netInterfaceStatus = NetInterfaceStatus.valueOf(statusString);
                                }
                                newNetConfigs = oldNetConfigs;
                                oldNetConfigs = null;
                                try {
                                    setInterfaceNumber(ifaceName, newNetConfigs);
                                    setNetInterfaceStatus(netInterfaceStatus, newNetConfigs);
                                } catch (NumberFormatException e) {
                                    s_logger.error("failed to set new interface number - {}", e);
                                }
                            }
                        }
                    }
                    if (oldNetConfigs == null || !isConfigsEqual(oldNetConfigs, newNetConfigs)) {
                        s_logger.info("new configuration for cellular modem on usb port {} netinterface {}", usbPort, ifaceName);
                        this.m_networkConfig = newNetworkConfig;
                        if (pppService != null) {
                            PppState pppState = pppService.getPppState();
                            if (pppState == PppState.CONNECTED || pppState == PppState.IN_PROGRESS) {
                                s_logger.info("disconnecting " + pppService.getIfaceName());
                                pppService.disconnect();
                            }
                            PppFactory.releasePppService(pppService.getIfaceName());
                        }
                        if (modem.isGpsEnabled()) {
                            if (!disableModemGps(modem)) {
                                s_logger.error("processNetworkConfigurationChangeEvent() :: Failed to disable modem GPS");
                                modem.reset();
                                this.m_resetTimerStart = System.currentTimeMillis();
                            }
                        }
                        modem.setConfiguration(newNetConfigs);
                        if (modem instanceof EvdoCellularModem) {
                            NetInterfaceStatus netIfaceStatus = getNetInterfaceStatus(newNetConfigs);
                            if (netIfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
                                if (!((EvdoCellularModem) modem).isProvisioned()) {
                                    s_logger.info("NetworkConfigurationChangeEvent :: The {} is not provisioned, will try to provision it ...", modem.getModel());
                                    if (task != null && !task.isCancelled()) {
                                        s_logger.info("NetworkConfigurationChangeEvent :: Cancelling monitor task");
                                        stopThread.set(true);
                                        monitorNotity();
                                        task.cancel(true);
                                        task = null;
                                    }
                                    ((EvdoCellularModem) modem).provision();
                                    if (task == null) {
                                        s_logger.info("NetworkConfigurationChangeEvent :: Restarting monitor task");
                                        stopThread.set(false);
                                        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("handleEvent() :: Exception while monitoring cellular connection {}", t);
                                                    }
                                                }
                                            }
                                        });
                                    } else {
                                        monitorNotity();
                                    }
                                } else {
                                    s_logger.info("NetworkConfigurationChangeEvent :: The " + modem.getModel() + " is provisioned");
                                }
                            }
                            if (modem.isGpsSupported()) {
                                if (isGpsEnabledInConfig(newNetConfigs) && !modem.isGpsEnabled()) {
                                    modem.enableGps();
                                    postModemGpsEvent(modem, true);
                                }
                            }
                        }
                    }
                }
            } catch (KuraException e) {
                s_logger.error("NetworkConfigurationChangeEvent :: Failed to process - {}", e);
            }
        }
    }
}
Also used : NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IModemLinkService(org.eclipse.kura.net.admin.modem.IModemLinkService) 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) NetConfig(org.eclipse.kura.net.NetConfig) EvdoCellularModem(org.eclipse.kura.net.admin.modem.EvdoCellularModem) Map(java.util.Map) HashMap(java.util.HashMap) PppState(org.eclipse.kura.net.admin.modem.PppState)

Example 2 with PppState

use of org.eclipse.kura.net.admin.modem.PppState 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)2 Map (java.util.Map)2 KuraException (org.eclipse.kura.KuraException)2 NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)2 EvdoCellularModem (org.eclipse.kura.net.admin.modem.EvdoCellularModem)2 HspaCellularModem (org.eclipse.kura.net.admin.modem.HspaCellularModem)2 IModemLinkService (org.eclipse.kura.net.admin.modem.IModemLinkService)2 PppState (org.eclipse.kura.net.admin.modem.PppState)2 CellularModem (org.eclipse.kura.net.modem.CellularModem)2 ConnectionInfoImpl (org.eclipse.kura.linux.net.ConnectionInfoImpl)1 ConnectionInfo (org.eclipse.kura.net.ConnectionInfo)1 NetConfig (org.eclipse.kura.net.NetConfig)1 ModemMonitorListener (org.eclipse.kura.net.modem.ModemMonitorListener)1 ModemTechnologyType (org.eclipse.kura.net.modem.ModemTechnologyType)1