Search in sources :

Example 1 with ModemAddedEvent

use of org.eclipse.kura.net.modem.ModemAddedEvent in project kura by eclipse.

the class NetworkServiceImpl method handleEvent.

@Override
public void handleEvent(Event event) {
    s_logger.debug("handleEvent() :: topic: {}", event.getTopic());
    String topic = event.getTopic();
    if (topic.equals(UsbDeviceAddedEvent.USB_EVENT_DEVICE_ADDED_TOPIC)) {
        // validate mandatory properties
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY) == null) {
            return;
        }
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY) == null) {
            return;
        }
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY) == null) {
            return;
        }
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY) == null) {
            return;
        }
        // do we care?
        final SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
        if (modemInfo != null) {
            // Found one - see if we have some info for it.
            // Also check if we are getting more devices than expected.
            // This can happen if all the modem resources cannot be removed from the OS or from Kura.
            // In this case we did not receive an UsbDeviceRemovedEvent and we did not post
            // an ModemRemovedEvent. Should we do it here?
            List<? extends UsbModemDriver> drivers = modemInfo.getDeviceDrivers();
            for (UsbModemDriver driver : drivers) {
                try {
                    driver.install();
                } catch (Exception e) {
                    s_logger.error("handleEvent() :: Failed to install modem device driver {} - {}", driver.getName(), e);
                }
            }
            UsbModemDevice usbModem = this.m_usbModems.get(event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY));
            boolean createNewUsbModemDevice = false;
            if (usbModem == null) {
                createNewUsbModemDevice = true;
            } else if (modemInfo.getNumTtyDevs() > 0 && modemInfo.getNumBlockDevs() > 0) {
                if (usbModem.getTtyDevs().size() >= modemInfo.getNumTtyDevs() && usbModem.getBlockDevs().size() >= modemInfo.getNumBlockDevs()) {
                    createNewUsbModemDevice = true;
                }
            } else if (modemInfo.getNumTtyDevs() > 0 && usbModem.getTtyDevs().size() >= modemInfo.getNumTtyDevs() || modemInfo.getNumBlockDevs() > 0 && usbModem.getBlockDevs().size() >= modemInfo.getNumBlockDevs()) {
                createNewUsbModemDevice = true;
            }
            if (createNewUsbModemDevice) {
                if (usbModem == null) {
                    s_logger.debug("handleEvent() :: Modem not found. Create one");
                } else {
                    s_logger.debug("handleEvent() :: Found modem with too many resources: {}. Create a new one", usbModem);
                }
                usbModem = new UsbModemDevice((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_BUS_NUMBER_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_DEVICE_PATH_PROPERTY));
            }
            String resource = (String) event.getProperty(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY);
            s_logger.debug("handleEvent() :: Adding resource: {} for: {}", resource, usbModem.getUsbPort());
            if (resource.contains("tty")) {
                usbModem.addTtyDev(resource);
            } else {
                usbModem.addBlockDev(resource);
            }
            this.m_usbModems.put(usbModem.getUsbPort(), usbModem);
            // At this point, we should have some modems - display them
            s_logger.info("handleEvent() :: Modified modem (Added resource): {}", usbModem);
            s_logger.debug("handleEvent() :: usbModem.getTtyDevs().size()={}, modemInfo.getNumTtyDevs()={}", usbModem.getTtyDevs().size(), modemInfo.getNumTtyDevs());
            s_logger.debug("handleEvent() :: usbModem.getBlockDevs().size()={}, modemInfo.getNumBlockDevs()={}", usbModem.getBlockDevs().size(), modemInfo.getNumBlockDevs());
            // Check for correct number of resources
            if (usbModem.getTtyDevs().size() == modemInfo.getNumTtyDevs() && usbModem.getBlockDevs().size() == modemInfo.getNumBlockDevs()) {
                s_logger.info("handleEvent() :: posting ModemAddedEvent -- USB_EVENT_DEVICE_ADDED_TOPIC: {}", usbModem);
                this.m_eventAdmin.postEvent(new ModemAddedEvent(usbModem));
                this.m_addedModems.add(usbModem.getUsbPort());
                if (OS_VERSION != null && TARGET_NAME != null && OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Mini_Gateway.getTargetName()) || OS_VERSION.equals(KuraConstants.Reliagate_10_11.getImageName() + "_" + KuraConstants.Reliagate_10_11.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Reliagate_10_11.getTargetName())) {
                    if (this.m_serialModem != null) {
                        if (SupportedUsbModemInfo.Telit_HE910_D.getVendorId().equals(usbModem.getVendorId()) && SupportedUsbModemInfo.Telit_HE910_D.getProductId().equals(usbModem.getProductId())) {
                            s_logger.info("handleEvent() :: Removing {} from addedModems", this.m_serialModem.getProductName());
                            this.m_addedModems.remove(this.m_serialModem.getProductName());
                        }
                    }
                }
            }
        }
    /*
             * System.out.println("ADDED Device: " + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_VENDOR_ID_PROPERTY)
             * + ":" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_ID_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_RESOURCE_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_USB_PORT_PROPERTY));
             */
    } else if (topic.equals(UsbDeviceRemovedEvent.USB_EVENT_DEVICE_REMOVED_TOPIC)) {
        // validate mandatory properties
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY) == null) {
            return;
        }
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY) == null) {
            return;
        }
        if (event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY) == null) {
            return;
        }
        // do we care?
        SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem((String) event.getProperty(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY), (String) event.getProperty(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
        if (modemInfo != null) {
            // found one - remove if it exists
            UsbModemDevice usbModem = this.m_usbModems.remove(event.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY));
            if (usbModem != null) {
                s_logger.info("handleEvent() :: Removing modem: {}", usbModem);
                this.m_addedModems.remove(usbModem.getUsbPort());
                Map<String, String> properties = new HashMap<String, String>();
                properties.put(UsbDeviceEvent.USB_EVENT_BUS_NUMBER_PROPERTY, usbModem.getUsbBusNumber());
                properties.put(UsbDeviceEvent.USB_EVENT_DEVICE_PATH_PROPERTY, usbModem.getUsbDevicePath());
                properties.put(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY, usbModem.getUsbPort());
                properties.put(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY, usbModem.getVendorId());
                properties.put(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY, usbModem.getProductId());
                properties.put(UsbDeviceEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY, usbModem.getManufacturerName());
                properties.put(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY, usbModem.getProductName());
                this.m_eventAdmin.postEvent(new ModemRemovedEvent(properties));
            }
        }
    /*
             * System.out.println("REMOVED Device: " +
             * event.getProperty(UsbDeviceAddedEvent.USB_EVENT_VENDOR_ID_PROPERTY) + ":" +
             * event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_ID_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_RESOURCE_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_PRODUCT_NAME_PROPERTY));
             * System.out.println("\t" + event.getProperty(UsbDeviceAddedEvent.USB_EVENT_USB_PORT_PROPERTY));
             */
    } else if (topic.equals(SerialModemAddedEvent.SERIAL_MODEM_EVENT_ADDED_TOPIC)) {
        SerialModemAddedEvent serialModemAddedEvent = (SerialModemAddedEvent) event;
        SupportedSerialModemInfo serialModemInfo = serialModemAddedEvent.getSupportedSerialModemInfo();
        if (serialModemInfo != null) {
            if (OS_VERSION != null && TARGET_NAME != null && OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Mini_Gateway.getTargetName()) || OS_VERSION.equals(KuraConstants.Reliagate_10_11.getImageName() + "_" + KuraConstants.Reliagate_10_11.getImageVersion()) && TARGET_NAME.equals(KuraConstants.Reliagate_10_11.getTargetName())) {
                if (this.m_usbModems.isEmpty()) {
                    this.m_serialModem = new SerialModemDevice(serialModemInfo.getModemName(), serialModemInfo.getManufacturerName(), serialModemInfo.getDriver().getComm().getSerialPorts());
                    if (this.m_serialModem != null) {
                        s_logger.debug("handleEvent() :: posting ModemAddedEvent for serial modem: {}", this.m_serialModem.getProductName());
                        this.m_eventAdmin.postEvent(new ModemAddedEvent(this.m_serialModem));
                        this.m_addedModems.add(this.m_serialModem.getProductName());
                    }
                } else {
                    s_logger.info("handleEvent() :: Ignoring {} modem since it has already been detected as a USB device", serialModemInfo.getModemName());
                }
            } else {
                this.m_serialModem = new SerialModemDevice(serialModemInfo.getModemName(), serialModemInfo.getManufacturerName(), serialModemInfo.getDriver().getComm().getSerialPorts());
                if (this.m_serialModem != null) {
                    s_logger.debug("handleEvent() :: posting ModemAddedEvent for serial modem: {}", this.m_serialModem.getProductName());
                    this.m_eventAdmin.postEvent(new ModemAddedEvent(this.m_serialModem));
                    this.m_addedModems.add(this.m_serialModem.getProductName());
                }
            }
        }
    } else {
        s_logger.error("handleEvent() :: Unexpected event topic: {}", topic);
    }
}
Also used : SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) SerialModemAddedEvent(org.eclipse.kura.linux.net.modem.SerialModemAddedEvent) ModemAddedEvent(org.eclipse.kura.net.modem.ModemAddedEvent) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) UsbModemDriver(org.eclipse.kura.linux.net.modem.UsbModemDriver) ModemRemovedEvent(org.eclipse.kura.net.modem.ModemRemovedEvent) Map(java.util.Map) HashMap(java.util.HashMap) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo) SerialModemAddedEvent(org.eclipse.kura.linux.net.modem.SerialModemAddedEvent)

Example 2 with ModemAddedEvent

use of org.eclipse.kura.net.modem.ModemAddedEvent in project kura by eclipse.

the class ModemMonitorServiceImpl method handleEvent.

@Override
public void handleEvent(Event event) {
    s_logger.debug("handleEvent - topic: {}", event.getTopic());
    String topic = event.getTopic();
    if (topic.equals(NetworkConfigurationChangeEvent.NETWORK_EVENT_CONFIG_CHANGE_TOPIC)) {
        NetworkConfigurationChangeEvent netConfigChangedEvent = (NetworkConfigurationChangeEvent) event;
        String[] propNames = netConfigChangedEvent.getPropertyNames();
        if (propNames != null && propNames.length > 0) {
            Map<String, Object> props = new HashMap<String, Object>();
            for (String propName : propNames) {
                Object prop = netConfigChangedEvent.getProperty(propName);
                if (prop != null) {
                    props.put(propName, prop);
                }
            }
            try {
                final NetworkConfiguration newNetworkConfig = new NetworkConfiguration(props);
                ExecutorService ex = Executors.newSingleThreadExecutor();
                ex.submit(new Runnable() {

                    @Override
                    public void run() {
                        processNetworkConfigurationChangeEvent(newNetworkConfig);
                    }
                });
            } catch (Exception e) {
                s_logger.error("Failed to handle the NetworkConfigurationChangeEvent - {}", e);
            }
        }
    } else if (topic.equals(ModemAddedEvent.MODEM_EVENT_ADDED_TOPIC)) {
        ModemAddedEvent modemAddedEvent = (ModemAddedEvent) event;
        final ModemDevice modemDevice = modemAddedEvent.getModemDevice();
        if (this.m_serviceActivated) {
            ExecutorService ex = Executors.newSingleThreadExecutor();
            ex.submit(new Runnable() {

                @Override
                public void run() {
                    trackModem(modemDevice);
                }
            });
        }
    } else if (topic.equals(ModemRemovedEvent.MODEM_EVENT_REMOVED_TOPIC)) {
        ModemRemovedEvent modemRemovedEvent = (ModemRemovedEvent) event;
        String usbPort = (String) modemRemovedEvent.getProperty(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY);
        this.m_modems.remove(usbPort);
    }
}
Also used : NetworkConfigurationChangeEvent(org.eclipse.kura.net.admin.event.NetworkConfigurationChangeEvent) HashMap(java.util.HashMap) ModemAddedEvent(org.eclipse.kura.net.modem.ModemAddedEvent) ModemDevice(org.eclipse.kura.net.modem.ModemDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) KuraException(org.eclipse.kura.KuraException) ModemRemovedEvent(org.eclipse.kura.net.modem.ModemRemovedEvent) ExecutorService(java.util.concurrent.ExecutorService) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration)

Example 3 with ModemAddedEvent

use of org.eclipse.kura.net.modem.ModemAddedEvent in project kura by eclipse.

the class NetworkServiceImpl method activate.

// ----------------------------------------------------------------
// 
// Activation APIs
// 
// ----------------------------------------------------------------
protected void activate(ComponentContext componentContext) {
    // save the bundle context
    this.m_ctx = componentContext;
    s_stopThread = new AtomicBoolean();
    this.m_usbModems = new HashMap<String, UsbModemDevice>();
    this.m_addedModems = new ArrayList<String>();
    Dictionary<String, String[]> d = new Hashtable<String, String[]>();
    d.put(EventConstants.EVENT_TOPIC, EVENT_TOPICS);
    this.m_ctx.getBundleContext().registerService(EventHandler.class.getName(), this, d);
    // Add serial modem if any
    SupportedSerialModemsInfo.getModem();
    // Add tty devices
    List<UsbTtyDevice> ttyDevices = this.m_usbService.getUsbTtyDevices();
    if (ttyDevices != null && !ttyDevices.isEmpty()) {
        s_logger.debug("activate() :: Total tty devices reported by UsbService: {}", ttyDevices.size());
        for (UsbTtyDevice device : ttyDevices) {
            if (SupportedUsbModemsInfo.isSupported(device.getVendorId(), device.getProductId(), device.getProductName())) {
                UsbModemDevice usbModem = null;
                // found one - see if we have some info for it
                if (this.m_usbModems.get(device.getUsbPort()) == null) {
                    usbModem = new UsbModemDevice(device);
                } else {
                    usbModem = this.m_usbModems.get(device.getUsbPort());
                }
                usbModem.addTtyDev(device.getDeviceNode());
                s_logger.debug("activate() :: Adding tty resource: {} for {}", device.getDeviceNode(), device.getUsbPort());
                this.m_usbModems.put(device.getUsbPort(), usbModem);
            }
        }
    }
    // Add block devices
    List<UsbBlockDevice> blockDevices = this.m_usbService.getUsbBlockDevices();
    if (blockDevices != null && !blockDevices.isEmpty()) {
        s_logger.debug("activate() :: Total block devices reported by UsbService: {}", blockDevices.size());
        for (UsbBlockDevice device : blockDevices) {
            if (SupportedUsbModemsInfo.isSupported(device.getVendorId(), device.getProductId(), device.getProductName())) {
                UsbModemDevice usbModem = null;
                // found one - see if we have some info for it
                if (this.m_usbModems.get(device.getUsbPort()) == null) {
                    usbModem = new UsbModemDevice(device);
                } else {
                    usbModem = this.m_usbModems.get(device.getUsbPort());
                }
                usbModem.addBlockDev(device.getDeviceNode());
                s_logger.debug("activate() :: Adding block resource: {} for {}", device.getDeviceNode(), device.getUsbPort());
                this.m_usbModems.put(device.getUsbPort(), usbModem);
            }
        }
    }
    // At this point, we should have some modems - display them
    Iterator<Entry<String, UsbModemDevice>> it = this.m_usbModems.entrySet().iterator();
    while (it.hasNext()) {
        final UsbModemDevice usbModem = it.next().getValue();
        final SupportedUsbModemInfo modemInfo = SupportedUsbModemsInfo.getModem(usbModem.getVendorId(), usbModem.getProductId(), usbModem.getProductName());
        s_logger.debug("activate() :: Found modem: {}", usbModem);
        // Check for correct number of resources
        if (modemInfo != null) {
            s_logger.debug("activate() :: usbModem.getTtyDevs().size()={}, modemInfo.getNumTtyDevs()={}", usbModem.getTtyDevs().size(), modemInfo.getNumTtyDevs());
            s_logger.debug("activate() :: usbModem.getBlockDevs().size()={}, modemInfo.getNumBlockDevs()={}", usbModem.getBlockDevs().size(), modemInfo.getNumBlockDevs());
            // s_logger.info("Product name: {}", usbModem.getProductName());
            if (usbModem.getTtyDevs().size() == modemInfo.getNumTtyDevs() && usbModem.getBlockDevs().size() == modemInfo.getNumBlockDevs()) {
                s_logger.info("activate () :: posting ModemAddedEvent ... {}", usbModem);
                this.m_eventAdmin.postEvent(new ModemAddedEvent(usbModem));
                this.m_addedModems.add(usbModem.getUsbPort());
            } else {
                s_logger.warn("activate() :: modem doesn't have correct number of resources, will try to toggle it ...");
                this.m_executor = Executors.newSingleThreadScheduledExecutor();
                s_logger.info("activate() :: scheduling {} thread in {} minutes ..", TOOGLE_MODEM_THREAD_NAME, TOOGLE_MODEM_THREAD_EXECUTION_DELAY);
                s_stopThread.set(false);
                s_task = this.m_executor.schedule(new Runnable() {

                    @Override
                    public void run() {
                        Thread.currentThread().setName(TOOGLE_MODEM_THREAD_NAME);
                        try {
                            toggleModem(modemInfo);
                        } catch (InterruptedException interruptedException) {
                            Thread.interrupted();
                            s_logger.debug("activate() :: modem monitor interrupted - {}", interruptedException);
                        } catch (Throwable t) {
                            s_logger.error("activate() :: Exception while monitoring cellular connection {}", t);
                        }
                    }
                }, TOOGLE_MODEM_THREAD_EXECUTION_DELAY, TimeUnit.MINUTES);
            }
        }
    }
}
Also used : UsbBlockDevice(org.eclipse.kura.usb.UsbBlockDevice) Hashtable(java.util.Hashtable) SerialModemAddedEvent(org.eclipse.kura.linux.net.modem.SerialModemAddedEvent) ModemAddedEvent(org.eclipse.kura.net.modem.ModemAddedEvent) EventHandler(org.osgi.service.event.EventHandler) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Entry(java.util.Map.Entry) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo) UsbTtyDevice(org.eclipse.kura.usb.UsbTtyDevice)

Aggregations

ModemAddedEvent (org.eclipse.kura.net.modem.ModemAddedEvent)3 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)3 HashMap (java.util.HashMap)2 KuraException (org.eclipse.kura.KuraException)2 SerialModemAddedEvent (org.eclipse.kura.linux.net.modem.SerialModemAddedEvent)2 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)2 ModemRemovedEvent (org.eclipse.kura.net.modem.ModemRemovedEvent)2 SerialModemDevice (org.eclipse.kura.net.modem.SerialModemDevice)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)1 SupportedSerialModemInfo (org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo)1 UsbModemDriver (org.eclipse.kura.linux.net.modem.UsbModemDriver)1 NetworkConfigurationChangeEvent (org.eclipse.kura.net.admin.event.NetworkConfigurationChangeEvent)1 ModemDevice (org.eclipse.kura.net.modem.ModemDevice)1