use of org.eclipse.kura.net.NetInterfaceStatus 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);
}
}
}
}
use of org.eclipse.kura.net.NetInterfaceStatus 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.NetInterfaceStatus in project kura by eclipse.
the class ModemMonitorServiceImpl method trackModem.
private void trackModem(ModemDevice modemDevice) {
Class<? extends CellularModemFactory> modemFactoryClass = null;
if (modemDevice instanceof UsbModemDevice) {
SupportedUsbModemInfo supportedUsbModemInfo = SupportedUsbModemsInfo.getModem((UsbModemDevice) modemDevice);
UsbModemFactoryInfo usbModemFactoryInfo = SupportedUsbModemsFactoryInfo.getModem(supportedUsbModemInfo);
modemFactoryClass = usbModemFactoryInfo.getModemFactoryClass();
} else if (modemDevice instanceof SerialModemDevice) {
SupportedSerialModemInfo supportedSerialModemInfo = SupportedSerialModemsInfo.getModem();
SerialModemFactoryInfo serialModemFactoryInfo = SupportedSerialModemsFactoryInfo.getModem(supportedSerialModemInfo);
modemFactoryClass = serialModemFactoryInfo.getModemFactoryClass();
}
if (modemFactoryClass != null) {
CellularModemFactory modemFactoryService = null;
try {
try {
Method getInstanceMethod = modemFactoryClass.getDeclaredMethod("getInstance", (Class<?>[]) null);
getInstanceMethod.setAccessible(true);
modemFactoryService = (CellularModemFactory) getInstanceMethod.invoke(null, (Object[]) null);
} catch (Exception e) {
s_logger.error("Error calling getInstance() method on " + modemFactoryClass.getName() + e);
}
// if unsuccessful in calling getInstance()
if (modemFactoryService == null) {
modemFactoryService = modemFactoryClass.newInstance();
}
String platform = null;
if (this.m_systemService != null) {
platform = this.m_systemService.getPlatform();
}
CellularModem modem = modemFactoryService.obtainCellularModemService(modemDevice, platform);
try {
HashMap<String, String> modemInfoMap = new HashMap<String, String>();
modemInfoMap.put(ModemReadyEvent.IMEI, modem.getSerialNumber());
modemInfoMap.put(ModemReadyEvent.IMSI, modem.getMobileSubscriberIdentity());
modemInfoMap.put(ModemReadyEvent.ICCID, modem.getIntegratedCirquitCardId());
modemInfoMap.put(ModemReadyEvent.RSSI, Integer.toString(modem.getSignalStrength()));
s_logger.info("posting ModemReadyEvent on topic {}", ModemReadyEvent.MODEM_EVENT_READY_TOPIC);
this.m_eventAdmin.postEvent(new ModemReadyEvent(modemInfoMap));
} catch (Exception e) {
s_logger.error("Failed to post the ModemReadyEvent - {}", e);
}
String ifaceName = this.m_networkService.getModemPppPort(modemDevice);
List<NetConfig> netConfigs = null;
if (ifaceName != null) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_networkConfig.getNetInterfaceConfig(ifaceName);
if (netInterfaceConfig == null) {
this.m_networkConfig = this.m_netConfigService.getNetworkConfiguration();
netInterfaceConfig = this.m_networkConfig.getNetInterfaceConfig(ifaceName);
}
if (netInterfaceConfig != null) {
netConfigs = getNetConfigs(netInterfaceConfig);
if (netConfigs != null && netConfigs.size() > 0) {
modem.setConfiguration(netConfigs);
}
}
}
if (modemDevice instanceof UsbModemDevice) {
this.m_modems.put(((UsbModemDevice) modemDevice).getUsbPort(), modem);
} else if (modemDevice instanceof SerialModemDevice) {
this.m_modems.put(modemDevice.getProductName(), modem);
}
if (modem instanceof EvdoCellularModem) {
NetInterfaceStatus netIfaceStatus = getNetInterfaceStatus(netConfigs);
if (netIfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
if (modem.isGpsEnabled()) {
if (!disableModemGps(modem)) {
s_logger.error("trackModem() :: Failed to disable modem GPS, resetting modem ...");
modem.reset();
this.m_resetTimerStart = System.currentTimeMillis();
}
}
if (!((EvdoCellularModem) modem).isProvisioned()) {
s_logger.info("trackModem() :: The {} is not provisioned, will try to provision it ...", modem.getModel());
if (task != null && !task.isCancelled()) {
s_logger.info("trackModem() :: Cancelling monitor task");
stopThread.set(true);
monitorNotity();
task.cancel(true);
task = null;
}
((EvdoCellularModem) modem).provision();
if (task == null) {
s_logger.info("trackModem() :: 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("trackModem() :: Exception while monitoring cellular connection {}", t);
}
}
}
});
} else {
monitorNotity();
}
} else {
s_logger.info("trackModem() :: The {} is provisioned", modem.getModel());
}
}
if (modem.isGpsSupported()) {
if (isGpsEnabledInConfig(netConfigs) && !modem.isGpsEnabled()) {
modem.enableGps();
postModemGpsEvent(modem, true);
}
}
}
} catch (Exception e) {
s_logger.error("trackModem() :: {}", e);
}
}
}
use of org.eclipse.kura.net.NetInterfaceStatus in project kura by eclipse.
the class WifiMonitorServiceImpl method isWifiEnabled.
private boolean isWifiEnabled(WifiInterfaceConfigImpl wifiInterfaceConfig) {
WifiMode wifiMode = WifiMode.UNKNOWN;
NetInterfaceStatus status = NetInterfaceStatus.netIPv4StatusUnknown;
if (wifiInterfaceConfig != null) {
for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceConfig.getNetInterfaceAddresses()) {
wifiMode = wifiInterfaceAddressConfig.getMode();
for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof NetConfigIP4) {
status = ((NetConfigIP4) netConfig).getStatus();
}
}
}
} else {
s_logger.debug("wifiInterfaceConfig is null");
}
boolean statusEnabled = status.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN) || status.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN);
boolean wifiEnabled = wifiMode.equals(WifiMode.INFRA) || wifiMode.equals(WifiMode.MASTER);
s_logger.debug("statusEnabled: " + statusEnabled);
s_logger.debug("wifiEnabled: " + wifiEnabled);
return statusEnabled && wifiEnabled;
}
use of org.eclipse.kura.net.NetInterfaceStatus in project kura by eclipse.
the class WifiMonitorServiceImpl method enableInterface.
private void enableInterface(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
s_logger.debug("enableInterface: {}", netInterfaceConfig);
WifiInterfaceConfigImpl wifiInterfaceConfig = null;
if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
wifiInterfaceConfig = (WifiInterfaceConfigImpl) netInterfaceConfig;
} else {
return;
}
String interfaceName = wifiInterfaceConfig.getName();
WifiMode wifiMode = WifiMode.UNKNOWN;
NetInterfaceStatus status = NetInterfaceStatus.netIPv4StatusUnknown;
boolean isDhcpClient = false;
boolean enableDhcpServer = false;
if (wifiInterfaceConfig != null) {
for (WifiInterfaceAddressConfig wifiInterfaceAddressConfig : wifiInterfaceConfig.getNetInterfaceAddresses()) {
wifiMode = wifiInterfaceAddressConfig.getMode();
for (NetConfig netConfig : wifiInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof NetConfigIP4) {
status = ((NetConfigIP4) netConfig).getStatus();
isDhcpClient = ((NetConfigIP4) netConfig).isDhcp();
} else if (netConfig instanceof DhcpServerConfig4) {
enableDhcpServer = ((DhcpServerConfig4) netConfig).isEnabled();
}
}
}
}
if (status.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN) || status.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN)) {
if (wifiMode.equals(WifiMode.INFRA) || wifiMode.equals(WifiMode.MASTER)) {
this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
if (enableDhcpServer) {
this.m_netAdminService.manageDhcpServer(interfaceName, true);
}
}
}
}
Aggregations