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;
}
}
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");
}
Aggregations