use of org.eclipse.kura.net.wifi.WifiInterfaceAddress in project kura by eclipse.
the class NetworkServiceImpl method getWifiInterfaceAddresses.
private List<WifiInterfaceAddress> getWifiInterfaceAddresses(String interfaceName, boolean isUp) throws KuraException {
List<WifiInterfaceAddress> wifiInterfaceAddresses = new ArrayList<WifiInterfaceAddress>();
if (isUp) {
ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
WifiInterfaceAddressImpl wifiInterfaceAddress = new WifiInterfaceAddressImpl();
wifiInterfaceAddresses.add(wifiInterfaceAddress);
try {
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
String currentNetmask = ifconfig.getInetMask();
if (currentNetmask != null) {
wifiInterfaceAddress.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
wifiInterfaceAddress.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
wifiInterfaceAddress.setNetmask(IPAddress.parseHostAddress(currentNetmask));
wifiInterfaceAddress.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
wifiInterfaceAddress.setGateway(conInfo.getGateway());
wifiInterfaceAddress.setDnsServers(conInfo.getDnsServers());
WifiMode wifiMode = LinuxNetworkUtil.getWifiMode(interfaceName);
wifiInterfaceAddress.setBitrate(LinuxNetworkUtil.getWifiBitrate(interfaceName));
wifiInterfaceAddress.setMode(wifiMode);
// TODO - should this only be the AP we are connected to in client mode?
if (wifiMode == WifiMode.INFRA) {
String currentSSID = LinuxNetworkUtil.getSSID(interfaceName);
if (currentSSID != null) {
s_logger.debug("Adding access point SSID: {}", currentSSID);
WifiAccessPointImpl wifiAccessPoint = new WifiAccessPointImpl(currentSSID);
// FIXME: fill in other info
wifiAccessPoint.setMode(WifiMode.INFRA);
List<Long> bitrate = new ArrayList<Long>();
bitrate.add(54000000L);
wifiAccessPoint.setBitrate(bitrate);
wifiAccessPoint.setFrequency(12345);
wifiAccessPoint.setHardwareAddress("20AA4B8A6442".getBytes());
wifiAccessPoint.setRsnSecurity(EnumSet.allOf(WifiSecurity.class));
wifiAccessPoint.setStrength(1234);
wifiAccessPoint.setWpaSecurity(EnumSet.allOf(WifiSecurity.class));
wifiInterfaceAddress.setWifiAccessPoint(wifiAccessPoint);
}
}
} else {
return null;
}
} else {
return null;
}
} catch (UnknownHostException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
return wifiInterfaceAddresses;
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddress in project kura by eclipse.
the class GenericNetworkInterface method getCurrentConfig.
protected static NetInterfaceConfig<?> getCurrentConfig(String interfaceName, NetInterfaceType type, NetInterfaceStatus status, boolean dhcpServerEnabled, boolean passDns, Properties kuraProps) throws KuraException {
try {
NetInterfaceConfig<?> netInterfaceConfig = null;
boolean autoConnect = false;
int mtu = -1;
boolean dhcp = false;
IP4Address address = null;
String ipAddress = null;
String prefixString = null;
String netmask = null;
String gateway = null;
boolean interfaceEnabled = false;
if (kuraProps != null) {
String onBoot = kuraProps.getProperty("ONBOOT");
if ("yes".equals(onBoot)) {
autoConnect = true;
// we are enabled - just not sure if for LAN or WAN
if (status == NetInterfaceStatus.netIPv4StatusUnknown) {
interfaceEnabled = true;
}
} else {
autoConnect = false;
}
// override MTU with what is in config if it is present
String stringMtu = kuraProps.getProperty("MTU");
if (stringMtu == null) {
try {
mtu = LinuxNetworkUtil.getCurrentMtu(interfaceName);
} catch (KuraException e) {
// just assume ???
if (interfaceName.equals("lo")) {
mtu = 16436;
} else {
mtu = 1500;
}
}
} else {
mtu = Short.parseShort(stringMtu);
}
// get the bootproto
String bootproto = kuraProps.getProperty("BOOTPROTO");
if (bootproto == null) {
bootproto = "static";
}
// get the defroute
String defroute = kuraProps.getProperty("DEFROUTE");
if (defroute == null) {
defroute = "no";
}
if (interfaceEnabled) {
if (defroute.equals("yes")) {
status = NetInterfaceStatus.netIPv4StatusEnabledWAN;
} else {
status = NetInterfaceStatus.netIPv4StatusEnabledLAN;
}
}
// check for dhcp or static configuration
try {
ipAddress = kuraProps.getProperty("IPADDR");
prefixString = kuraProps.getProperty("PREFIX");
netmask = kuraProps.getProperty("NETMASK");
kuraProps.getProperty("BROADCAST");
try {
gateway = kuraProps.getProperty("GATEWAY");
s_logger.debug("got gateway for {}: {}", interfaceName, gateway);
} catch (Exception e) {
s_logger.warn("missing gateway stanza for " + interfaceName);
}
if (bootproto.equals("dhcp")) {
s_logger.debug("currently set for DHCP");
dhcp = true;
ipAddress = null;
netmask = null;
} else {
s_logger.debug("currently set for static address");
dhcp = false;
}
} catch (Exception e) {
e.printStackTrace();
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
}
if (ipAddress != null && !ipAddress.isEmpty()) {
address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
}
// make sure at least prefix or netmask is present if static
if (!dhcp && prefixString == null && netmask == null) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName + " must contain NETMASK and/or PREFIX");
}
}
ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
LinuxDns dnsService = LinuxDns.getInstance();
// note - we only add the fields we need/care about from a configuration standpoint
if (type == NetInterfaceType.LOOPBACK) {
s_logger.debug("Adding a Loopback interface");
netInterfaceConfig = new LoopbackInterfaceConfigImpl(interfaceName);
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
// loopback autoConnect
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(true);
// should always be true?
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setLoopback(true);
List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null && ifconfig.isUp()) {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
} else {
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
}
((LoopbackInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
// FIXME - hardcoded
NetConfig netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusEnabledLAN, true);
((NetConfigIP4) netConfig).setAddress(address);
((NetConfigIP4) netConfig).setDhcp(dhcp);
((NetConfigIP4) netConfig).setDnsServers(null);
((NetConfigIP4) netConfig).setDomains(null);
((NetConfigIP4) netConfig).setGateway(null);
((NetConfigIP4) netConfig).setNetworkPrefixLength((short) 8);
((NetConfigIP4) netConfig).setSubnetMask((IP4Address) IPAddress.parseHostAddress("255.0.0.0"));
((NetConfigIP4) netConfig).setWinsServers(null);
netConfigs.add(netConfig);
} else if (type == NetInterfaceType.ETHERNET) {
s_logger.debug("Adding an Ethernet interface - {}", interfaceName);
netInterfaceConfig = new EthernetInterfaceConfigImpl(interfaceName);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(autoConnect);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setLoopback(false);
List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
((EthernetInterfaceImpl<?>) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
try {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
} else {
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
} catch (KuraException e) {
s_logger.warn("The interface went down " + interfaceName + " not including current state in status because it is not up");
netInterfaceAddressConfig.setAddress(null);
netInterfaceAddressConfig.setBroadcast(null);
netInterfaceAddressConfig.setNetmask(null);
netInterfaceAddressConfig.setNetworkPrefixLength((short) -1);
netInterfaceAddressConfig.setGateway(null);
netInterfaceAddressConfig.setDnsServers(null);
}
}
}
((EthernetInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
// add the config
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
if (dhcpServerEnabled) {
// add DHCP server configuration to the list
DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
if (dhcpServerConfig != null) {
netConfigs.add(dhcpServerConfig);
}
}
} else if (type == NetInterfaceType.WIFI) {
s_logger.debug("Adding a Wireless interface - {}", interfaceName);
WifiInterfaceConfigImpl wifiInterfaceConfig = new WifiInterfaceConfigImpl(interfaceName);
netInterfaceConfig = wifiInterfaceConfig;
wifiInterfaceConfig.setMTU(mtu);
wifiInterfaceConfig.setAutoConnect(autoConnect);
wifiInterfaceConfig.setLoopback(false);
List<WifiInterfaceAddressConfig> wifiInterfaceAddressConfigs = new ArrayList<WifiInterfaceAddressConfig>();
List<WifiInterfaceAddress> wifiInterfaceAddresses = new ArrayList<WifiInterfaceAddress>();
WifiInterfaceAddressConfigImpl wifiInterfaceAddressConfig = new WifiInterfaceAddressConfigImpl();
wifiInterfaceAddressConfigs.add(wifiInterfaceAddressConfig);
wifiInterfaceAddresses.add(wifiInterfaceAddressConfig);
String currentSSID = LinuxNetworkUtil.getSSID(interfaceName);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
wifiInterfaceConfig.setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
wifiInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
wifiInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
String currentNetmask = ifconfig.getInetMask();
if (currentNetmask != null) {
wifiInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(currentNetmask));
wifiInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
}
wifiInterfaceAddressConfig.setBitrate(LinuxNetworkUtil.getWifiBitrate(interfaceName));
wifiInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
wifiInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, wifiInterfaceAddressConfig.getAddress()));
} else {
wifiInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
WifiAccessPointImpl ap = null;
if (currentSSID != null) {
s_logger.debug("Adding access point SSID: {}", currentSSID);
ap = new WifiAccessPointImpl(currentSSID);
// TODO: fill in other info
ap.setMode(WifiMode.INFRA);
List<Long> bitrate = new ArrayList<Long>();
bitrate.add(54000000L);
ap.setBitrate(bitrate);
ap.setFrequency(12345);
ap.setHardwareAddress("20AA4B8A6442".getBytes());
ap.setRsnSecurity(EnumSet.allOf(WifiSecurity.class));
ap.setStrength(1234);
ap.setWpaSecurity(EnumSet.allOf(WifiSecurity.class));
}
wifiInterfaceAddressConfig.setWifiAccessPoint(ap);
}
}
// mode
WifiMode wifiMode = WifiMode.UNKNOWN;
s_logger.debug("Get WifiMode...");
try {
// get from config file
String mode = kuraProps.getProperty("MODE");
if (mode != null) {
s_logger.debug("Getting wifi mode from {}", kuraFile.getAbsolutePath());
if (mode.equalsIgnoreCase("Managed")) {
wifiMode = WifiMode.INFRA;
} else if (mode.equalsIgnoreCase("Master")) {
wifiMode = WifiMode.MASTER;
} else if (mode.equalsIgnoreCase("Ad-Hoc")) {
wifiMode = WifiMode.ADHOC;
} else {
wifiMode = WifiMode.valueOf(mode);
}
} else {
// get current setting using iwconfig
s_logger.debug("Getting wifi mode from iwconfig");
wifiMode = LinuxNetworkUtil.getWifiMode(interfaceName);
}
} catch (Exception e) {
// leave as unknown
}
s_logger.debug("Current WifiMode: {}", wifiMode);
wifiInterfaceAddressConfig.setMode(wifiMode);
wifiInterfaceConfig.setNetInterfaceAddresses(wifiInterfaceAddressConfigs);
// TODO: fix
wifiInterfaceConfig.setCapabilities(EnumSet.allOf(Capability.class));
// add the configs - one for client (managed) mode, one for access point (master) mode
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
wifiInterfaceAddressConfig.setNetConfigs(netConfigs);
// get the NetConfig
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
// get the wpa_supplicant configuration
WifiConfig wifiClientConfig = new WifiConfig();
setWifiClientConfig(interfaceName, wifiClientConfig, wifiMode);
// get the hostapd configuration
WifiConfig wifiAPConfig = new WifiConfig();
setWifiAccessPointConfig(wifiAPConfig);
// add WiFi configurations to the list
netConfigs.add(wifiClientConfig);
netConfigs.add(wifiAPConfig);
if (dhcpServerEnabled) {
// add DHCP server configuration to the list
DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
if (dhcpServerConfig != null) {
netConfigs.add(dhcpServerConfig);
}
}
} else if (type == NetInterfaceType.MODEM) {
s_logger.debug("Adding a Modem interface");
netInterfaceConfig = new ModemInterfaceConfigImpl(interfaceName);
((ModemInterfaceConfigImpl) netInterfaceConfig).setMTU(mtu);
((ModemInterfaceConfigImpl) netInterfaceConfig).setAutoConnect(autoConnect);
((ModemInterfaceConfigImpl) netInterfaceConfig).setLoopback(false);
((ModemInterfaceConfigImpl) netInterfaceConfig).setPointToPoint(true);
List<ModemInterfaceAddressConfig> modemInterfaceAddressConfigs = new ArrayList<ModemInterfaceAddressConfig>();
List<ModemInterfaceAddress> netInterfaceAddresses = new ArrayList<ModemInterfaceAddress>();
ModemInterfaceAddressConfigImpl netInterfaceAddressConfig = new ModemInterfaceAddressConfigImpl();
modemInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
((ModemInterfaceConfigImpl) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
}
((ModemInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(modemInterfaceAddressConfigs);
// add the config
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
} else {
s_logger.warn("Unsupported Type: " + type);
}
return netInterfaceConfig;
} catch (UnknownHostException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddress in project kura by eclipse.
the class NetworkServiceImpl method getNetworkInterface.
public NetInterface<? extends NetInterfaceAddress> getNetworkInterface(String interfaceName) throws KuraException {
// ignore redpine vlan interface
if (interfaceName.startsWith("rpine")) {
s_logger.debug("Ignoring redpine vlan interface.");
return null;
}
// ignore usb0 for beaglebone
if (interfaceName.startsWith("usb0") && "beaglebone".equals(System.getProperty("target.device"))) {
s_logger.debug("Ignoring usb0 for beaglebone.");
return null;
}
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig == null) {
s_logger.debug("Ignoring {} interface.", interfaceName);
return null;
}
NetInterfaceType type = ifconfig.getType();
boolean isUp = ifconfig.isUp();
if (type == NetInterfaceType.UNKNOWN) {
if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
// If the interface name is in a form such as "1-3.4", assume it is a modem
type = NetInterfaceType.MODEM;
} else if (this.m_serialModem != null && interfaceName.equals(this.m_serialModem.getProductName())) {
type = NetInterfaceType.MODEM;
}
}
if (type == NetInterfaceType.ETHERNET) {
EthernetInterfaceImpl<NetInterfaceAddress> netInterface = new EthernetInterfaceImpl<NetInterfaceAddress>(interfaceName);
Map<String, String> driver = LinuxNetworkUtil.getEthernetDriver(interfaceName);
netInterface.setDriver(driver.get("name"));
netInterface.setDriverVersion(driver.get("version"));
netInterface.setFirmwareVersion(driver.get("firmware"));
netInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
netInterface.setHardwareAddress(ifconfig.getMacAddressBytes());
netInterface.setMTU(ifconfig.getMtu());
netInterface.setSupportsMulticast(ifconfig.isMulticast());
netInterface.setLinkUp(LinuxNetworkUtil.isLinkUp(type, interfaceName));
netInterface.setLoopback(false);
netInterface.setPointToPoint(false);
netInterface.setUp(isUp);
netInterface.setVirtual(isVirtual());
netInterface.setUsbDevice(getUsbDevice(interfaceName));
netInterface.setState(getState(interfaceName, isUp));
netInterface.setNetInterfaceAddresses(getNetInterfaceAddresses(interfaceName, type, isUp));
return netInterface;
} else if (type == NetInterfaceType.LOOPBACK) {
LoopbackInterfaceImpl<NetInterfaceAddress> netInterface = new LoopbackInterfaceImpl<NetInterfaceAddress>(interfaceName);
netInterface.setDriver(getDriver());
netInterface.setDriverVersion(getDriverVersion());
netInterface.setFirmwareVersion(getFirmwareVersion());
netInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
netInterface.setHardwareAddress(new byte[] { 0, 0, 0, 0, 0, 0 });
netInterface.setLoopback(true);
netInterface.setMTU(ifconfig.getMtu());
netInterface.setSupportsMulticast(ifconfig.isMulticast());
netInterface.setPointToPoint(false);
netInterface.setUp(isUp);
netInterface.setVirtual(false);
netInterface.setUsbDevice(null);
netInterface.setState(getState(interfaceName, isUp));
netInterface.setNetInterfaceAddresses(getNetInterfaceAddresses(interfaceName, type, isUp));
return netInterface;
} else if (type == NetInterfaceType.WIFI) {
WifiInterfaceImpl<WifiInterfaceAddress> wifiInterface = new WifiInterfaceImpl<WifiInterfaceAddress>(interfaceName);
Map<String, String> driver = LinuxNetworkUtil.getEthernetDriver(interfaceName);
wifiInterface.setDriver(driver.get("name"));
wifiInterface.setDriverVersion(driver.get("version"));
wifiInterface.setFirmwareVersion(driver.get("firmware"));
wifiInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
wifiInterface.setHardwareAddress(ifconfig.getMacAddressBytes());
wifiInterface.setMTU(ifconfig.getMtu());
wifiInterface.setSupportsMulticast(ifconfig.isMulticast());
// FIXME:MS Add linkUp in the AbstractNetInterface and populate accordingly
// wifiInterface.setLinkUp(LinuxNetworkUtil.isLinkUp(type, interfaceName));
wifiInterface.setLoopback(false);
wifiInterface.setPointToPoint(false);
wifiInterface.setUp(isUp);
wifiInterface.setVirtual(isVirtual());
wifiInterface.setUsbDevice(getUsbDevice(interfaceName));
wifiInterface.setState(getState(interfaceName, isUp));
wifiInterface.setNetInterfaceAddresses(getWifiInterfaceAddresses(interfaceName, isUp));
wifiInterface.setCapabilities(LinuxNetworkUtil.getWifiCapabilities(interfaceName));
return wifiInterface;
} else if (type == NetInterfaceType.MODEM) {
ModemDevice modemDevice = null;
if (interfaceName.startsWith("ppp")) {
// already connected - find the corresponding usb device
modemDevice = this.m_usbModems.get(getModemUsbPort(interfaceName));
if (modemDevice == null && this.m_serialModem != null) {
modemDevice = this.m_serialModem;
}
} else if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
// the interface name is in the form of a usb port i.e. "1-3.4"
modemDevice = this.m_usbModems.get(interfaceName);
} else if (this.m_serialModem != null && interfaceName.equals(this.m_serialModem.getProductName())) {
modemDevice = this.m_serialModem;
}
return modemDevice != null ? getModemInterface(interfaceName, isUp, modemDevice) : null;
} else {
if (interfaceName.startsWith("can")) {
s_logger.trace("Ignoring CAN interface: {}", interfaceName);
} else if (interfaceName.startsWith("ppp")) {
s_logger.debug("Ignoring unconfigured ppp interface: {}", interfaceName);
} else {
s_logger.debug("Unsupported network type - not adding to network devices: {} of type: ", interfaceName, type.toString());
}
return null;
}
}
use of org.eclipse.kura.net.wifi.WifiInterfaceAddress in project kura by eclipse.
the class NetworkConfiguration method recomputeNetworkProperties.
// ---------------------------------------------------------------
//
// Private Methods
//
// ---------------------------------------------------------------
private void recomputeNetworkProperties() {
Map<String, Object> properties = new HashMap<String, Object>();
String netIfPrefix = null;
String netIfReadOnlyPrefix = null;
String netIfConfigPrefix = null;
StringBuilder sbPrefix = null;
StringBuilder sbInterfaces = new StringBuilder();
if (this.m_modifiedInterfaceNames != null && !this.m_modifiedInterfaceNames.isEmpty()) {
StringBuilder sb = new StringBuilder();
String prefix = "";
for (String interfaceName : this.m_modifiedInterfaceNames) {
sb.append(prefix);
prefix = ",";
sb.append(interfaceName);
}
String result = sb.toString();
s_logger.debug("Set modified interface names: {}", result);
properties.put("modified.interface.names", result);
}
Iterator<String> it = this.m_netInterfaceConfigs.keySet().iterator();
while (it.hasNext()) {
NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_netInterfaceConfigs.get(it.next());
// add the interface to the list of interface found in the platform
if (sbInterfaces.length() != 0) {
sbInterfaces.append(",");
}
sbInterfaces.append(netInterfaceConfig.getName());
// build the prefixes for all the properties associated with this interface
sbPrefix = new StringBuilder("net.interface.").append(netInterfaceConfig.getName()).append(".");
netIfReadOnlyPrefix = sbPrefix.toString();
netIfPrefix = sbPrefix.append("config.").toString();
netIfConfigPrefix = sbPrefix.toString();
// add the properties of the interface
properties.put(netIfReadOnlyPrefix + "type", netInterfaceConfig.getType().toString());
properties.put(netIfPrefix + "name", netInterfaceConfig.getName());
if (netInterfaceConfig.getState() != null) {
properties.put(netIfPrefix + "state", netInterfaceConfig.getState().toString());
}
properties.put(netIfPrefix + "autoconnect", netInterfaceConfig.isAutoConnect());
properties.put(netIfPrefix + "mtu", netInterfaceConfig.getMTU());
properties.put(netIfReadOnlyPrefix + "driver", netInterfaceConfig.getDriver());
properties.put(netIfReadOnlyPrefix + "driver.version", netInterfaceConfig.getDriverVersion());
properties.put(netIfReadOnlyPrefix + "firmware.version", netInterfaceConfig.getFirmwareVersion());
properties.put(netIfReadOnlyPrefix + "mac", NetUtil.hardwareAddressToString(netInterfaceConfig.getHardwareAddress()));
properties.put(netIfReadOnlyPrefix + "loopback", netInterfaceConfig.isLoopback());
properties.put(netIfReadOnlyPrefix + "ptp", netInterfaceConfig.isPointToPoint());
properties.put(netIfReadOnlyPrefix + "up", netInterfaceConfig.isUp());
properties.put(netIfReadOnlyPrefix + "virtual", netInterfaceConfig.isVirtual());
// usb
if (netInterfaceConfig.getUsbDevice() != null) {
UsbDevice usbDev = netInterfaceConfig.getUsbDevice();
properties.put(netIfReadOnlyPrefix + "usb.vendor.id", usbDev.getVendorId());
properties.put(netIfReadOnlyPrefix + "usb.vendor.name", usbDev.getManufacturerName());
properties.put(netIfReadOnlyPrefix + "usb.product.id", usbDev.getProductId());
properties.put(netIfReadOnlyPrefix + "usb.product.name", usbDev.getProductName());
properties.put(netIfReadOnlyPrefix + "usb.busNumber", usbDev.getUsbBusNumber());
properties.put(netIfReadOnlyPrefix + "usb.devicePath", usbDev.getUsbDevicePath());
}
// custom readonly props for Ethernet and Wifi
if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
properties.put(netIfReadOnlyPrefix + "eth.link.up", ((EthernetInterfaceConfigImpl) netInterfaceConfig).isLinkUp());
} else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
EnumSet<Capability> capabilities = ((WifiInterfaceConfigImpl) netInterfaceConfig).getCapabilities();
if (capabilities != null && !capabilities.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Capability capability : capabilities) {
sb.append(capability.toString());
sb.append(",");
}
String capabilitiesString = sb.toString();
capabilitiesString = capabilitiesString.substring(0, capabilitiesString.length() - 1);
properties.put(netIfReadOnlyPrefix + "wifi.capabilities", capabilitiesString);
}
}
// add wifi properties
if (netInterfaceConfig.getType() == NetInterfaceType.WIFI) {
// capabilities
StringBuilder sbCapabilities = new StringBuilder();
EnumSet<Capability> capabilities = ((WifiInterface) netInterfaceConfig).getCapabilities();
if (capabilities != null) {
Iterator<Capability> it2 = ((WifiInterface) netInterfaceConfig).getCapabilities().iterator();
while (it2.hasNext()) {
sbCapabilities.append(it2.next().name()).append(" ");
}
properties.put(netIfReadOnlyPrefix + "wifi.capabilities", sbCapabilities.toString());
}
}
// add modem properties
if (netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
String delim;
// revision
StringBuffer revisionIdBuf = new StringBuffer();
String[] revisionId = ((ModemInterface<?>) netInterfaceConfig).getRevisionId();
if (revisionId != null) {
delim = null;
for (String rev : revisionId) {
if (delim != null) {
revisionIdBuf.append(delim);
}
revisionIdBuf.append(rev);
delim = ",";
}
}
// technology types
StringBuffer techTypesBuf = new StringBuffer();
List<ModemTechnologyType> techTypes = ((ModemInterface<?>) netInterfaceConfig).getTechnologyTypes();
if (techTypes != null) {
delim = null;
for (ModemTechnologyType techType : techTypes) {
if (delim != null) {
techTypesBuf.append(delim);
}
techTypesBuf.append(techType.toString());
delim = ",";
}
}
ModemPowerMode powerMode = ModemPowerMode.UNKNOWN;
if (((ModemInterface<?>) netInterfaceConfig).getPowerMode() != null) {
powerMode = ((ModemInterface<?>) netInterfaceConfig).getPowerMode();
}
properties.put(netIfReadOnlyPrefix + "manufacturer", ((ModemInterface<?>) netInterfaceConfig).getManufacturer());
properties.put(netIfReadOnlyPrefix + "model", ((ModemInterface<?>) netInterfaceConfig).getModel());
properties.put(netIfReadOnlyPrefix + "revisionId", revisionIdBuf.toString());
properties.put(netIfReadOnlyPrefix + "serialNum", ((ModemInterface<?>) netInterfaceConfig).getSerialNumber());
properties.put(netIfReadOnlyPrefix + "technologyTypes", techTypesBuf.toString());
properties.put(netIfConfigPrefix + "identifier", ((ModemInterface<?>) netInterfaceConfig).getModemIdentifier());
properties.put(netIfConfigPrefix + "powerMode", powerMode.toString());
properties.put(netIfConfigPrefix + "pppNum", ((ModemInterface<?>) netInterfaceConfig).getPppNum());
properties.put(netIfConfigPrefix + "poweredOn", ((ModemInterface<?>) netInterfaceConfig).isPoweredOn());
}
for (NetInterfaceAddress nia : netInterfaceConfig.getNetInterfaceAddresses()) {
String typePrefix = "ip4.";
if (nia != null) {
if (nia.getAddress() != null) {
properties.put(netIfReadOnlyPrefix + typePrefix + "address", nia.getAddress().getHostAddress());
}
if (nia.getBroadcast() != null) {
properties.put(netIfReadOnlyPrefix + typePrefix + "broadcast", nia.getBroadcast().getHostAddress());
}
if (nia.getGateway() != null) {
properties.put(netIfReadOnlyPrefix + typePrefix + "gateway", nia.getGateway().getHostAddress());
}
if (nia.getNetmask() != null) {
properties.put(netIfReadOnlyPrefix + typePrefix + "netmask", nia.getNetmask().getHostAddress());
}
if (nia.getNetmask() != null) {
properties.put(netIfReadOnlyPrefix + typePrefix + "prefix", Short.valueOf(nia.getNetworkPrefixLength()));
}
if (nia.getDnsServers() != null) {
StringBuilder dnsServers = new StringBuilder();
for (IPAddress dnsServer : nia.getDnsServers()) {
if (dnsServers.length() != 0) {
dnsServers.append(",");
}
dnsServers.append(dnsServer);
}
properties.put(netIfReadOnlyPrefix + typePrefix + "dnsServers", dnsServers.toString());
}
// Wifi interface address
if (nia instanceof WifiInterfaceAddress) {
// access point
WifiAccessPoint wap = ((WifiInterfaceAddress) nia).getWifiAccessPoint();
if (wap != null) {
/*
* TODO: need fields to reflect current state?
* properties.put(sbNetIfPrefix+"wifi.ssid", wap.getSSID());
* properties.put(sbNetIfPrefix+"wifi.mode", wap.getMode());
*/
}
long bitrate = ((WifiInterfaceAddress) nia).getBitrate();
properties.put(netIfReadOnlyPrefix + "wifi.bitrate", Long.valueOf(bitrate));
WifiMode wifiMode;
if (((WifiInterfaceAddress) nia).getMode() != null) {
wifiMode = ((WifiInterfaceAddress) nia).getMode();
} else {
wifiMode = WifiMode.UNKNOWN;
}
properties.put(netIfPrefix + "wifi.mode", wifiMode.toString());
}
// Modem interface address
if (nia instanceof ModemInterfaceAddress) {
if (((ModemInterfaceAddress) nia).getConnectionType() != null) {
properties.put(netIfConfigPrefix + "connection.type", ((ModemInterfaceAddress) nia).getConnectionType().toString());
}
if (((ModemInterfaceAddress) nia).getConnectionStatus() != null) {
properties.put(netIfConfigPrefix + "connection.status", ((ModemInterfaceAddress) nia).getConnectionStatus().toString());
}
}
}
}
// add the properties of the network configurations associated to the interface
List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
s_logger.trace("netInterfaceAddressConfigs.size() for {}: {}", netInterfaceConfig.getName(), netInterfaceAddressConfigs.size());
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
if (netConfigs != null) {
s_logger.trace("netConfigs.size(): {}", netConfigs.size());
for (NetConfig netConfig : netConfigs) {
if (netConfig instanceof WifiConfig) {
s_logger.trace("adding netconfig WifiConfigIP4 for {}", netInterfaceConfig.getName());
addWifiConfigIP4Properties((WifiConfig) netConfig, netIfConfigPrefix, properties);
} else if (netConfig instanceof ModemConfig) {
s_logger.trace("adding netconfig ModemConfig for {}", netInterfaceConfig.getName());
addModemConfigProperties((ModemConfig) netConfig, netIfConfigPrefix, properties);
} else if (netConfig instanceof NetConfigIP4) {
s_logger.trace("adding netconfig NetConfigIP4 for {}", netInterfaceConfig.getName());
addNetConfigIP4Properties((NetConfigIP4) netConfig, netIfConfigPrefix, properties);
/*
* Iterator<String> it2 = properties.keySet().iterator();
* while(it2.hasNext()) {
* String key = it2.next();
* System.out.println("\t\t\t"+key+"="+properties.get(key));
* }
*/
} else if (netConfig instanceof NetConfigIP6) {
s_logger.trace("adding netconfig NetConfigIP6 for {}", netInterfaceConfig.getName());
addNetConfigIP6Properties((NetConfigIP6) netConfig, netIfConfigPrefix, properties);
/*
* Iterator<String> it = properties.keySet().iterator();
* while(it.hasNext()) {
* String key = it.next();
* System.out.println("\t\t\t"+key+"="+properties.get(key));
* }
*/
} else if (netConfig instanceof DhcpServerConfig4) {
s_logger.trace("adding netconfig DhcpServerConfig4 for {}", netInterfaceConfig.getName());
addDhcpServerConfig4((DhcpServerConfig4) netConfig, netIfConfigPrefix, properties);
} else if (netConfig instanceof FirewallAutoNatConfig) {
s_logger.trace("adding netconfig FirewallNatConfig for {}", netInterfaceConfig.getName());
addFirewallNatConfig((FirewallAutoNatConfig) netConfig, netIfConfigPrefix, properties);
}
}
}
}
}
properties.put("net.interfaces", sbInterfaces.toString());
this.m_properties = properties;
}
Aggregations