Search in sources :

Example 1 with WifiInterface

use of org.eclipse.kura.net.wifi.WifiInterface 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;
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) HashMap(java.util.HashMap) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) UsbDevice(org.eclipse.kura.usb.UsbDevice) WifiInterface(org.eclipse.kura.net.wifi.WifiInterface) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) WifiMode(org.eclipse.kura.net.wifi.WifiMode) ModemTechnologyType(org.eclipse.kura.net.modem.ModemTechnologyType) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) ModemInterface(org.eclipse.kura.net.modem.ModemInterface) Capability(org.eclipse.kura.net.wifi.WifiInterface.Capability) EnumSet(java.util.EnumSet) ModemPowerMode(org.eclipse.kura.net.modem.ModemPowerMode) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddress(org.eclipse.kura.net.wifi.WifiInterfaceAddress) IPAddress(org.eclipse.kura.net.IPAddress) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Aggregations

EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 IPAddress (org.eclipse.kura.net.IPAddress)1 NetConfig (org.eclipse.kura.net.NetConfig)1 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)1 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)1 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)1 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)1 DhcpServerConfig4 (org.eclipse.kura.net.dhcp.DhcpServerConfig4)1 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)1 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)1 ModemInterface (org.eclipse.kura.net.modem.ModemInterface)1 ModemInterfaceAddress (org.eclipse.kura.net.modem.ModemInterfaceAddress)1 ModemPowerMode (org.eclipse.kura.net.modem.ModemPowerMode)1 ModemTechnologyType (org.eclipse.kura.net.modem.ModemTechnologyType)1 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)1 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)1 WifiInterface (org.eclipse.kura.net.wifi.WifiInterface)1 Capability (org.eclipse.kura.net.wifi.WifiInterface.Capability)1 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)1