use of org.eclipse.kura.net.NetConfig 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.NetConfig 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.NetConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method getReconfiguredWifiInterfaces.
private Collection<String> getReconfiguredWifiInterfaces() throws KuraException {
Set<String> reconfiguredInterfaces = new HashSet<String>();
for (String interfaceName : this.m_networkService.getAllNetworkInterfaceNames()) {
// skip non-wifi interfaces
if (LinuxNetworkUtil.getType(interfaceName) != NetInterfaceType.WIFI) {
continue;
}
// ignore "mon" interface
if (interfaceName.startsWith("mon")) {
continue;
}
// Get the old wifi config
WifiInterfaceConfigImpl currentConfig = null;
if (this.m_currentNetworkConfiguration != null) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_currentNetworkConfiguration.getNetInterfaceConfig(interfaceName);
if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
currentConfig = (WifiInterfaceConfigImpl) netInterfaceConfig;
}
}
// Get the new wifi config
WifiInterfaceConfigImpl newConfig = null;
if (this.m_newNetConfiguration != null) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> newNetInterfaceConfig = this.m_newNetConfiguration.getNetInterfaceConfig(interfaceName);
if (newNetInterfaceConfig instanceof WifiInterfaceConfigImpl) {
newConfig = (WifiInterfaceConfigImpl) newNetInterfaceConfig;
}
}
if (newConfig != null && currentConfig != null) {
List<WifiInterfaceAddressConfig> currentInterfaceAddressConfigs = currentConfig.getNetInterfaceAddresses();
List<WifiInterfaceAddressConfig> newInterfaceAddressConfigs = newConfig.getNetInterfaceAddresses();
if (currentInterfaceAddressConfigs == null && newInterfaceAddressConfigs == null) {
// no config changed - continue
continue;
}
if (currentInterfaceAddressConfigs == null || newInterfaceAddressConfigs == null) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// TODO: compare interfaceAddressConfigs
// FIXME - assuming one InterfaceAddressConfig for now
WifiInterfaceAddressConfig currentInterfaceAddressConfig = currentInterfaceAddressConfigs.get(0);
WifiInterfaceAddressConfig newInterfaceAddressConfig = newInterfaceAddressConfigs.get(0);
if (currentInterfaceAddressConfig.getConfigs() == null && newInterfaceAddressConfig.getConfigs() == null) {
continue;
}
if (currentInterfaceAddressConfig.getConfigs() == null || newInterfaceAddressConfig.getConfigs() == null) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// Remove other WifiConfigs that don't match the selected mode, for comparison purposes
//
List<NetConfig> currentNetConfigs = new ArrayList<NetConfig>(currentInterfaceAddressConfig.getConfigs());
List<NetConfig> newNetConfigs = new ArrayList<NetConfig>(newInterfaceAddressConfig.getConfigs());
WifiMode newWifiMode = newInterfaceAddressConfig.getMode();
WifiMode currentWifiMode = currentInterfaceAddressConfig.getMode();
if (newWifiMode != currentWifiMode) {
reconfiguredInterfaces.add(interfaceName);
continue;
}
// Modes don't match. We need to compare configs deeply
internalWifiConfigCompare(reconfiguredInterfaces, interfaceName, currentNetConfigs, newNetConfigs);
} else if (newConfig != null) {
// only newConfig - oldConfig is null
s_logger.debug("oldConfig was null, adding newConfig");
reconfiguredInterfaces.add(interfaceName);
} else if (currentConfig != null) {
s_logger.debug("Configuration for {} has changed", interfaceName);
reconfiguredInterfaces.add(interfaceName);
s_logger.debug("Removing {} from list of enabled interfaces because it is not configured", interfaceName);
this.m_disabledInterfaces.add(interfaceName);
} else {
s_logger.debug("old and new wifi config are null...");
}
}
updateInterfacesLists(reconfiguredInterfaces);
return reconfiguredInterfaces;
}
use of org.eclipse.kura.net.NetConfig 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.NetConfig 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