Skip to main content
summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorDoug Schaefer2017-05-15 13:33:58 -0400
committerDoug Schaefer2017-05-15 16:06:39 -0400
commit4b2e1e5b41cf92bf022759fa851be1a348dd598f (patch)
treee18ca7649bdefcd74d043365427c799d61b37842 /native
parent47b1581eb77df22f93562282669762c74c1bce5b (diff)
downloadorg.eclipse.cdt-4b2e1e5b41cf92bf022759fa851be1a348dd598f.tar.gz
org.eclipse.cdt-4b2e1e5b41cf92bf022759fa851be1a348dd598f.tar.xz
org.eclipse.cdt-4b2e1e5b41cf92bf022759fa851be1a348dd598f.zip
Add a registry of open Serial Ports.
This will be used by components that need to pause an open serial port to run some other functionality over the port. For example, Arduino needs to pause the port to run the bootloader. This removes the need for Serial ports to use o.e.remote. Change-Id: Idb14598541ccf4e87c702cf2e5442335c64a6c65
Diffstat (limited to 'native')
-rw-r--r--native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF2
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java61
2 files changed, 62 insertions, 1 deletions
diff --git a/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
index 5ad3d5cebd..299809f4c7 100644
--- a/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
+++ b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Serial Port
Bundle-SymbolicName: org.eclipse.cdt.native.serial
-Bundle-Version: 1.0.0.qualifier
+Bundle-Version: 1.1.0.qualifier
Bundle-Vendor: Eclipse CDT
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.cdt.serial
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
index fb1d39c49b..24c9002cfb 100644
--- a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
@@ -15,8 +15,13 @@ import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.ref.WeakReference;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.cdt.serial.internal.Messages;
@@ -38,6 +43,8 @@ public class SerialPort {
private static final String PORT_OPEN = Messages.getString("SerialPort.PortIsOpen"); //$NON-NLS-1$
+ private static final Map<String, LinkedList<WeakReference<SerialPort>>> openPorts = new HashMap<>();
+
static {
try {
System.loadLibrary("serial"); //$NON-NLS-1$
@@ -251,6 +258,36 @@ public class SerialPort {
}
/**
+ * Return an the SerialPort with the given name or null if it hasn't been allocated yet. This
+ * would be used by components that need to pause and resume a serial port.
+ *
+ * @param portName
+ * @return
+ * @since 1.1
+ */
+ public static SerialPort get(String portName) {
+ synchronized (openPorts) {
+ LinkedList<WeakReference<SerialPort>> list = openPorts.get(portName);
+ if (list == null) {
+ return null;
+ }
+
+ Iterator<WeakReference<SerialPort>> i = list.iterator();
+ while (i.hasNext()) {
+ WeakReference<SerialPort> ref = i.next();
+ SerialPort port = ref.get();
+ if (port == null) {
+ i.remove();
+ } else {
+ return port;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
* Return the name for this serial port.
*
* @return serial port name
@@ -270,6 +307,15 @@ public class SerialPort {
public void open() throws IOException {
handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
isOpen = true;
+
+ synchronized (openPorts) {
+ LinkedList<WeakReference<SerialPort>> list = openPorts.get(portName);
+ if (list == null) {
+ list = new LinkedList<>();
+ openPorts.put(portName, list);
+ }
+ list.addFirst(new WeakReference<>(this));
+ }
}
public synchronized void close() throws IOException {
@@ -277,6 +323,21 @@ public class SerialPort {
isOpen = false;
close0(handle);
handle = 0;
+
+ synchronized (openPorts) {
+ LinkedList<WeakReference<SerialPort>> list = openPorts.get(portName);
+ if (list != null) {
+ Iterator<WeakReference<SerialPort>> i = list.iterator();
+ while (i.hasNext()) {
+ WeakReference<SerialPort> ref = i.next();
+ SerialPort port = ref.get();
+ if (port == null || port.equals(this)) {
+ i.remove();
+ }
+ }
+ }
+ }
+
try {
// Sleep for a second since some serial ports take a while to actually close
Thread.sleep(500);

Back to the top