Search in sources :

Example 1 with ModemPppConfigGenerator

use of org.eclipse.kura.net.admin.modem.ModemPppConfigGenerator in project kura by eclipse.

the class PppConfigWriter method writeConfig.

private void writeConfig(ModemInterfaceConfigImpl modemInterfaceConfig) throws KuraException {
    String oldInterfaceName = modemInterfaceConfig.getName();
    String newInterfaceName = modemInterfaceConfig.getName();
    // Get the configs
    ModemConfig modemConfig = null;
    NetConfigIP4 netConfigIP4 = null;
    for (ModemInterfaceAddressConfig modemInterfaceAddressConfig : modemInterfaceConfig.getNetInterfaceAddresses()) {
        for (NetConfig netConfig : modemInterfaceAddressConfig.getConfigs()) {
            if (netConfig instanceof ModemConfig) {
                modemConfig = (ModemConfig) netConfig;
            } else if (netConfig instanceof NetConfigIP4) {
                netConfigIP4 = (NetConfigIP4) netConfig;
            }
        }
    }
    // Use the ppp number for the interface name, if configured
    int pppNum = -1;
    if (modemConfig != null) {
        pppNum = modemConfig.getPppNumber();
        if (pppNum >= 0) {
            newInterfaceName = "ppp" + pppNum;
            modemInterfaceConfig.setName(newInterfaceName);
        }
    }
    // Save the status and priority
    IfcfgConfigWriter.writeKuraExtendedConfig(modemInterfaceConfig);
    Class<? extends ModemPppConfigGenerator> configClass = null;
    UsbDevice usbDevice = modemInterfaceConfig.getUsbDevice();
    int baudRate = -1;
    if (usbDevice != null) {
        SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem(usbDevice);
        UsbModemFactoryInfo usbFactoryInfo = SupportedUsbModemsFactoryInfo.getModem(modemInfo);
        if (usbFactoryInfo != null) {
            configClass = usbFactoryInfo.getConfigGeneratorClass();
        }
        baudRate = 921600;
    } else {
        SupportedSerialModemInfo serialModemInfo = SupportedSerialModemsInfo.getModem();
        SerialModemFactoryInfo serialFactoryInfo = SupportedSerialModemsFactoryInfo.getModem(serialModemInfo);
        configClass = serialFactoryInfo.getConfigGeneratorClass();
        baudRate = serialModemInfo.getDriver().getComm().getBaudRate();
    }
    String pppPeerFilename = formPeerFilename(usbDevice);
    String pppLogfile = formPppLogFilename(usbDevice);
    String chatFilename = formChatFilename(usbDevice);
    String disconnectFilename = formDisconnectFilename(usbDevice);
    // Cleanup values associated with the old name if the interface name has changed
    if (!oldInterfaceName.equals(newInterfaceName)) {
        try {
            // Remove the old ppp peers symlink
            s_logger.debug("Removing old symlinks to {}", pppPeerFilename);
            removeSymbolicLinks(pppPeerFilename, OS_PEERS_DIRECTORY);
            // Remove the old modem identifier
            StringBuilder key = new StringBuilder("net.interface.").append(oldInterfaceName).append(".modem.identifier");
            s_logger.debug("Removing modem identifier for {}", oldInterfaceName);
            KuranetConfig.deleteProperty(key.toString());
            // Remove custom dns servers
            key = new StringBuilder("net.interface.").append(oldInterfaceName).append(".config.dnsServers");
            s_logger.debug("Removing dns servers for {}", oldInterfaceName);
            KuranetConfig.deleteProperty(key.toString());
            // Remove gpsEnabled
            key = new StringBuilder().append("net.interface.").append(oldInterfaceName).append(".config.gpsEnabled");
            s_logger.debug("Removing gpsEnabled for {}", oldInterfaceName);
            KuranetConfig.deleteProperty(key.toString());
            // Remove status
            IfcfgConfigWriter.removeKuraExtendedConfig(oldInterfaceName);
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    if (configClass != null) {
        try {
            ModemPppConfigGenerator scriptGenerator = configClass.newInstance();
            if (modemConfig != null) {
                s_logger.debug("Writing connect scripts for " + modemInterfaceConfig.getName() + " using " + configClass.toString());
                s_logger.debug("Writing {}", pppPeerFilename);
                PppPeer pppPeer = scriptGenerator.getPppPeer(getDeviceId(usbDevice), modemConfig, pppLogfile, chatFilename, disconnectFilename);
                pppPeer.setBaudRate(baudRate);
                pppPeer.write(pppPeerFilename);
                s_logger.debug("Writing {}", chatFilename);
                ModemXchangeScript connectScript = scriptGenerator.getConnectScript(modemConfig);
                connectScript.writeScript(chatFilename);
                s_logger.debug("Writing {}", disconnectFilename);
                ModemXchangeScript disconnectScript = scriptGenerator.getDisconnectScript(modemConfig);
                disconnectScript.writeScript(disconnectFilename);
                if (pppNum >= 0) {
                    s_logger.debug("Linking peer file using ppp number: {}", pppNum);
                    String symlinkFilename = formPeerLinkAbsoluteName(pppNum);
                    LinuxFileUtil.createSymbolicLink(pppPeerFilename, symlinkFilename);
                } else {
                    s_logger.error("Can't create symbolic link to " + pppPeerFilename + ", invalid ppp number: " + pppNum);
                }
                String modemIdentifier = modemInterfaceConfig.getModemIdentifier();
                if (modemIdentifier != null) {
                    StringBuilder key = new StringBuilder("net.interface.").append(modemInterfaceConfig.getName()).append(".modem.identifier");
                    s_logger.debug("Storing modem identifier " + modemIdentifier + " using key: " + key);
                    KuranetConfig.setProperty(key.toString(), modemIdentifier);
                }
                // Custom dns servers
                if (netConfigIP4 != null) {
                    StringBuilder key = new StringBuilder("net.interface.").append(modemInterfaceConfig.getName()).append(".config.dnsServers");
                    List<IP4Address> dnsServers = netConfigIP4.getDnsServers();
                    if (dnsServers != null && !dnsServers.isEmpty()) {
                        StringBuilder serversSB = new StringBuilder();
                        Iterator<IP4Address> it = dnsServers.iterator();
                        serversSB.append(it.next().getHostAddress());
                        while (it.hasNext()) {
                            serversSB.append(DNS_DELIM).append(it.next().getHostAddress());
                        }
                        s_logger.debug("Storing DNS servers " + serversSB + " using key: " + key);
                        KuranetConfig.setProperty(key.toString(), serversSB.toString());
                    } else {
                        KuranetConfig.deleteProperty(key.toString());
                    }
                }
                StringBuilder key = new StringBuilder().append("net.interface.").append(newInterfaceName).append(".config.gpsEnabled");
                s_logger.debug("Setting gpsEnabled for {}", newInterfaceName);
                KuranetConfig.setProperty(key.toString(), Boolean.toString(modemConfig.isGpsEnabled()));
                key = new StringBuilder().append("net.interface.").append(newInterfaceName).append(".config.resetTimeout");
                s_logger.debug("Setting modem resetTimeout for {}", newInterfaceName);
                KuranetConfig.setProperty(key.toString(), Integer.toString(modemConfig.getResetTimeout()));
            } else {
                s_logger.error("Error writing connect scripts - modemConfig is null");
            }
        } catch (Exception e) {
            s_logger.error("Could not write modem config", e);
        }
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) UsbDevice(org.eclipse.kura.usb.UsbDevice) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) UsbModemFactoryInfo(org.eclipse.kura.net.admin.modem.SupportedUsbModemsFactoryInfo.UsbModemFactoryInfo) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) PppPeer(org.eclipse.kura.net.admin.modem.PppPeer) SerialModemFactoryInfo(org.eclipse.kura.net.admin.modem.SupportedSerialModemsFactoryInfo.SerialModemFactoryInfo) ModemXchangeScript(org.eclipse.kura.net.admin.visitor.linux.util.ModemXchangeScript) KuraException(org.eclipse.kura.KuraException) ModemInterfaceAddressConfig(org.eclipse.kura.net.modem.ModemInterfaceAddressConfig) NetConfig(org.eclipse.kura.net.NetConfig) ModemPppConfigGenerator(org.eclipse.kura.net.admin.modem.ModemPppConfigGenerator) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)

Aggregations

IOException (java.io.IOException)1 KuraException (org.eclipse.kura.KuraException)1 SupportedSerialModemInfo (org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo)1 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)1 IP4Address (org.eclipse.kura.net.IP4Address)1 NetConfig (org.eclipse.kura.net.NetConfig)1 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)1 ModemPppConfigGenerator (org.eclipse.kura.net.admin.modem.ModemPppConfigGenerator)1 PppPeer (org.eclipse.kura.net.admin.modem.PppPeer)1 SerialModemFactoryInfo (org.eclipse.kura.net.admin.modem.SupportedSerialModemsFactoryInfo.SerialModemFactoryInfo)1 UsbModemFactoryInfo (org.eclipse.kura.net.admin.modem.SupportedUsbModemsFactoryInfo.UsbModemFactoryInfo)1 ModemXchangeScript (org.eclipse.kura.net.admin.visitor.linux.util.ModemXchangeScript)1 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)1 ModemInterfaceAddressConfig (org.eclipse.kura.net.modem.ModemInterfaceAddressConfig)1 UsbDevice (org.eclipse.kura.usb.UsbDevice)1