Search in sources :

Example 1 with UsbTtyDevice

use of org.eclipse.kura.usb.UsbTtyDevice in project kura by eclipse.

the class ExampleComponent method activate.

protected void activate(ComponentContext componentContext) {
    s_logger.debug("Activating ExampleComponent");
    List<UsbTtyDevice> ttyDevices = this.m_usbService.getUsbTtyDevices();
    if (ttyDevices != null && !ttyDevices.isEmpty()) {
        for (UsbTtyDevice device : ttyDevices) {
            System.out.println("Device: " + device.getVendorId() + ":" + device.getProductId());
            System.out.println("\t" + device.getDeviceNode());
            System.out.println("\t" + device.getManufacturerName());
            System.out.println("\t" + device.getProductName());
            System.out.println("\t" + device.getUsbPort());
        }
    }
    /*
         * m_worker = new ScheduledThreadPoolExecutor(1);
         *
         * m_worker.schedule(new Runnable() {
         *
         * @Override
         * public void run() {
         * try {
         * System.out.println("m_networkService.getState(): " + m_networkService.getState());
         *
         * List<String> interfaceNames = m_networkService.getAllNetworkInterfaceNames();
         * if(interfaceNames != null && interfaceNames.size() > 0) {
         * for(String interfaceName : interfaceNames) {
         * System.out.println("Interface Name: " + interfaceName + " with State: " +
         * m_networkService.getState(interfaceName));
         * }
         * }
         *
         * List<NetInterface<? extends NetInterfaceAddress>> activeNetworkInterfaces =
         * m_networkService.getActiveNetworkInterfaces();
         * if(activeNetworkInterfaces != null && activeNetworkInterfaces.size() > 0) {
         * for(NetInterface<? extends NetInterfaceAddress> activeNetworkInterface : activeNetworkInterfaces) {
         * System.out.println("ActiveNetworkInterface: " + activeNetworkInterface);
         * }
         * }
         *
         * List<NetInterface<? extends NetInterfaceAddress>> networkInterfaces =
         * m_networkService.getNetworkInterfaces();
         * if(networkInterfaces != null && networkInterfaces.size() > 0) {
         * for(NetInterface<? extends NetInterfaceAddress> networkInterface : networkInterfaces) {
         * System.out.println("NetworkInterface: " + networkInterface);
         * }
         * }
         *
         * List<WifiAccessPoint> wifiAccessPoints = m_networkService.getAllWifiAccessPoints();
         * if(wifiAccessPoints != null && wifiAccessPoints.size() > 0) {
         * for(WifiAccessPoint wifiAccessPoint : wifiAccessPoints) {
         * System.out.println("WifiAccessPoint: " + wifiAccessPoint);
         * }
         * }
         *
         * List<WifiAccessPoint> wlan0wifiAccessPoints = m_networkService.getAllWifiAccessPoints();
         * if(wlan0wifiAccessPoints != null && wlan0wifiAccessPoints.size() > 0) {
         * for(WifiAccessPoint wifiAccessPoint : wlan0wifiAccessPoints) {
         * System.out.println("wlan0 WifiAccessPoint: " + wifiAccessPoint);
         * }
         * }
         * } catch(Exception e) {
         * e.printStackTrace();
         * }
         * }
         *
         * }, 0, TimeUnit.SECONDS);
         */
    doGpsUpdate();
/*
         * // install event listener for serial ports and specific topics of interest
         * Dictionary props = new Hashtable<String, String>();
         * props.put(EventConstants.EVENT_TOPIC, "CLOCK_SERVICE_EVENT");
         * BundleContext bc = componentContext.getBundleContext();
         * bc.registerService(EventHandler.class.getName(), this, props);
         *
         * try {
         * if(m_clockService.getLastSync() != null) {
         * clockIsSynced = true;
         * }
         * } catch (KuraException e) {
         * // TODO Auto-generated catch block
         * e.printStackTrace();
         * }
         *
         * try {
         * List<ComponentConfiguration> configs = m_configurationService.getComponentConfigurations();
         * for(ComponentConfiguration config : configs) {
         * System.out.println(config.getPid());
         * }
         * } catch (KuraException e) {
         * e.printStackTrace();
         * }
         */
// doGpsUpdate();
/*
         * m_systemPropsWorker = new ScheduledThreadPoolExecutor(1);
         * m_systemPropsHandle = m_systemPropsWorker.scheduleAtFixedRate(new Runnable() {
         *
         * @Override
         * public void run() {
         * try {
         *
         * String[] values = {"Zero", "One", "Two", "Three", "Four"};
         *
         * for(int i=0; i<5; i++) {
         * Map<String, Object> map = new Hashtable<String, Object>();
         * System.out.println("SETTING TO " + values[i]);
         * map.put("0", values[i]);
         * m_systemPropertiesService.setValues(map);
         * if(m_systemPropertiesService.getValue("0").equals(values[i])) {
         * System.out.println("SUCCESS... " + m_systemPropertiesService.getValue("0"));
         * } else {
         * System.out.println("FAILURE!!! " + m_systemPropertiesService.getValue("0"));
         * }
         * }
         * } catch (Exception e) {
         * e.printStackTrace();
         * }
         *
         * }
         * }, 10, (Integer) 10, TimeUnit.SECONDS);
         */
/*
         * // get the mqtt client for this application
         * try {
         * s_logger.info("Getting CloudApplicationClient for {}...", APP_ID);
         * m_cloudClient = m_cloudService.getCloudApplicationClient(APP_ID);
         * m_cloudClient.addCloudCallbackHandler(this);
         *
         * // initialize a COM port
         * Properties props = new Properties();
         * props.setProperty("port", "/dev/ttyUSB0");
         * props.setProperty("baudRate", "9600");
         * props.setProperty("stopBits", "1");
         * props.setProperty("parity", "0");
         * props.setProperty("bitsPerWord", "8");
         * try {
         * initSerialCom(props);
         * } catch (ProtocolException e) {
         * // TODO Auto-generated catch block
         * e.printStackTrace();
         * }
         * m_serialSb = new StringBuilder();
         * if(conn!=null){
         * m_serialThread = new Thread(new Runnable() {
         *
         * @Override
         * public void run() {
         * while(conn!=null){
         * doSerial();
         * }
         * }
         * });
         * m_serialThread.start();
         * }
         * counter = 0;
         * doUpdate();
         * doGpsUpdate();
         * } catch (KuraException e) {
         * s_logger.error("Cannot activate", e);
         * throw new ComponentException(e);
         * }
         */
}
Also used : UsbTtyDevice(org.eclipse.kura.usb.UsbTtyDevice)

Example 2 with UsbTtyDevice

use of org.eclipse.kura.usb.UsbTtyDevice in project kura by eclipse.

the class LinuxUdevNative method callback.

/*
     * WARNING
     *
     * The callback does not fire for devices open by a process
     * when the device is unplugged from the USB.
     * Note that `udevadm monitor' correctly reports removal of these devices so there
     * must be something wrong with our native code using libudev.
     *
     * On top of that information for detached (UdevEventType.DETACHED) devices is completely unreliable:
     * missing manufacturer and product names, wrong VID&PID (often the one of the
     * USB hub the device was attached to), wrong USB path (often the path of the USB hub
     * the device was attached to).
     * The only reliable information seems to be the device node name, e.g. ttyACM0.
     *
     * Information for devices being attached (UdevEventType.ATTACHED) seems to work more reliably.
     * The callback might still fire with wrong/incomplete information but EVENTUALLY
     * we get all the devices with the right information.
     *
     */
private void callback(String type, UsbDevice usbDevice) {
    s_logger.debug("TYPE: {}", usbDevice.getClass());
    s_logger.debug("\tmanfufacturer name: {}", usbDevice.getManufacturerName());
    s_logger.debug("\tproduct name: {}", usbDevice.getProductName());
    s_logger.debug("\tvendor ID: {}", usbDevice.getVendorId());
    s_logger.debug("\tproduct ID: {}", usbDevice.getProductId());
    s_logger.debug("\tUSB Bus Number: {}", usbDevice.getUsbBusNumber());
    if (usbDevice instanceof UsbBlockDevice) {
        String name = ((UsbBlockDevice) usbDevice).getDeviceNode();
        if (name != null) {
            if (type.compareTo(UdevEventType.ATTACHED.name()) == 0) {
                /*
                     * FIXME: does an already existing device, with the same name,
                     * need to be removed first?
                     */
                m_blockDevices.put(name, (UsbBlockDevice) usbDevice);
                this.m_linuxUdevListener.attached(usbDevice);
            } else if (type.compareTo(UdevEventType.DETACHED.name()) == 0) {
                /*
                     * Due to the above limitations,
                     * the best we can do is to remove the device from the
                     * map of already known devices by its name.
                     */
                UsbBlockDevice removedDevice = m_blockDevices.remove(name);
                if (removedDevice != null) {
                    this.m_linuxUdevListener.detached(removedDevice);
                }
            } else {
                s_logger.debug("Unknown udev event: {}", type);
            }
        }
    } else if (usbDevice instanceof UsbNetDevice) {
        String name = ((UsbNetDevice) usbDevice).getInterfaceName();
        if (name != null) {
            if (type.compareTo(UdevEventType.ATTACHED.name()) == 0) {
                m_netDevices.put(name, (UsbNetDevice) usbDevice);
                this.m_linuxUdevListener.attached(usbDevice);
            } else if (type.compareTo(UdevEventType.DETACHED.name()) == 0) {
                UsbNetDevice removedDevice = m_netDevices.remove(name);
                if (removedDevice != null) {
                    this.m_linuxUdevListener.detached(removedDevice);
                }
            } else {
                s_logger.debug("Unknown udev event: {}", type);
            }
        }
    } else if (usbDevice instanceof UsbTtyDevice) {
        String name = ((UsbTtyDevice) usbDevice).getDeviceNode();
        if (name != null) {
            if (type.compareTo(UdevEventType.ATTACHED.name()) == 0) {
                m_ttyDevices.put(name, (UsbTtyDevice) usbDevice);
                this.m_linuxUdevListener.attached(usbDevice);
            } else if (type.compareTo(UdevEventType.DETACHED.name()) == 0) {
                UsbTtyDevice removedDevice = m_ttyDevices.remove(name);
                if (removedDevice != null) {
                    this.m_linuxUdevListener.detached(removedDevice);
                }
            } else {
                s_logger.debug("Unknown udev event: {}", type);
            }
        }
    }
}
Also used : UsbBlockDevice(org.eclipse.kura.usb.UsbBlockDevice) UsbNetDevice(org.eclipse.kura.usb.UsbNetDevice) UsbTtyDevice(org.eclipse.kura.usb.UsbTtyDevice)

Example 3 with UsbTtyDevice

use of org.eclipse.kura.usb.UsbTtyDevice in project kura by eclipse.

the class UsbServiceImpl method attached.

@Override
public synchronized void attached(UsbDevice device) {
    s_logger.debug("firing UsbDeviceAddedEvent for: {}", device);
    Map<String, String> map = new HashMap<String, String>();
    map.put(UsbDeviceEvent.USB_EVENT_USB_PORT_PROPERTY, device.getUsbPort());
    map.put(UsbDeviceEvent.USB_EVENT_VENDOR_ID_PROPERTY, device.getVendorId());
    map.put(UsbDeviceEvent.USB_EVENT_PRODUCT_ID_PROPERTY, device.getProductId());
    map.put(UsbDeviceEvent.USB_EVENT_MANUFACTURER_NAME_PROPERTY, device.getManufacturerName());
    map.put(UsbDeviceEvent.USB_EVENT_PRODUCT_NAME_PROPERTY, device.getProductName());
    map.put(UsbDeviceEvent.USB_EVENT_BUS_NUMBER_PROPERTY, device.getUsbBusNumber());
    map.put(UsbDeviceEvent.USB_EVENT_DEVICE_PATH_PROPERTY, device.getUsbDevicePath());
    if (device instanceof UsbBlockDevice) {
        map.put(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY, ((UsbBlockDevice) device).getDeviceNode());
    } else if (device instanceof UsbNetDevice) {
        map.put(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY, ((UsbNetDevice) device).getInterfaceName());
    } else if (device instanceof UsbTtyDevice) {
        map.put(UsbDeviceEvent.USB_EVENT_RESOURCE_PROPERTY, ((UsbTtyDevice) device).getDeviceNode());
    }
    this.m_eventAdmin.postEvent(new UsbDeviceAddedEvent(map));
}
Also used : UsbBlockDevice(org.eclipse.kura.usb.UsbBlockDevice) HashMap(java.util.HashMap) UsbNetDevice(org.eclipse.kura.usb.UsbNetDevice) UsbDeviceAddedEvent(org.eclipse.kura.usb.UsbDeviceAddedEvent) UsbTtyDevice(org.eclipse.kura.usb.UsbTtyDevice)

Example 4 with UsbTtyDevice

use of org.eclipse.kura.usb.UsbTtyDevice in project kura by eclipse.

the class PositionServiceImpl method getSerialConnectionProperties.

private Properties getSerialConnectionProperties(Map<String, Object> props) {
    Properties prop = new Properties();
    if (props != null) {
        String portName = null;
        int baudRate = -1;
        int bitsPerWord = -1;
        int stopBits = -1;
        int parity = -1;
        if (props.get("enabled") != null) {
            this.m_configEnabled = (Boolean) props.get("enabled");
            if (!this.m_configEnabled) {
                return null;
            }
        } else {
            this.m_configEnabled = false;
            return null;
        }
        portName = (String) props.get("port");
        if (portName != null && !portName.contains("/dev/") && !portName.contains("COM")) {
            List<UsbTtyDevice> utds = this.m_usbService.getUsbTtyDevices();
            for (UsbTtyDevice utd : utds) {
                if (utd.getUsbPort().equals(portName)) {
                    portName = utd.getDeviceNode();
                    break;
                }
            }
        }
        if (props.get("baudRate") != null) {
            baudRate = (Integer) props.get("baudRate");
        }
        if (props.get("bitsPerWord") != null) {
            bitsPerWord = (Integer) props.get("bitsPerWord");
        }
        if (props.get("stopBits") != null) {
            stopBits = (Integer) props.get("stopBits");
        }
        if (props.get("parity") != null) {
            parity = (Integer) props.get("parity");
        }
        if (portName == null) {
            return null;
        }
        prop.setProperty("port", portName);
        prop.setProperty("baudRate", Integer.toString(baudRate));
        prop.setProperty("stopBits", Integer.toString(stopBits));
        prop.setProperty("parity", Integer.toString(parity));
        prop.setProperty("bitsPerWord", Integer.toString(bitsPerWord));
        s_logger.debug("port name: {}", portName);
        s_logger.debug("baud rate {}", baudRate);
        s_logger.debug("stop bits {}", stopBits);
        s_logger.debug("parity {}", parity);
        s_logger.debug("bits per word {}", bitsPerWord);
        return prop;
    } else {
        return null;
    }
}
Also used : Properties(java.util.Properties) UsbTtyDevice(org.eclipse.kura.usb.UsbTtyDevice)

Example 5 with UsbTtyDevice

use of org.eclipse.kura.usb.UsbTtyDevice 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

UsbTtyDevice (org.eclipse.kura.usb.UsbTtyDevice)7 UsbBlockDevice (org.eclipse.kura.usb.UsbBlockDevice)4 UsbNetDevice (org.eclipse.kura.usb.UsbNetDevice)3 HashMap (java.util.HashMap)2 File (java.io.File)1 Hashtable (java.util.Hashtable)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SerialModemAddedEvent (org.eclipse.kura.linux.net.modem.SerialModemAddedEvent)1 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)1 ModemAddedEvent (org.eclipse.kura.net.modem.ModemAddedEvent)1 UsbDeviceAddedEvent (org.eclipse.kura.usb.UsbDeviceAddedEvent)1 UsbDeviceRemovedEvent (org.eclipse.kura.usb.UsbDeviceRemovedEvent)1 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)1 EventHandler (org.osgi.service.event.EventHandler)1