]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/java/net/tinyos/comm/TOSSerial.java
Previously, the serial forwarder would only release the COM port on exit. Now it...
[tinyos-2.x.git] / support / sdk / java / net / tinyos / comm / TOSSerial.java
index ee292c526c518403e4b221241f010a15bdbb2bc7..5fbac78b7589cdb6398545f2dae8857e3f1fcf45 100644 (file)
  */
 
 //@author Cory Sharp <cssharp@eecs.berkeley.edu>
-
 package net.tinyos.comm;
 
 import java.io.*;
 import java.util.*;
 import java.util.regex.*;
 
-public class TOSSerial extends NativeSerial implements SerialPort
-{
-  class EventDispatcher extends Thread
-  {
+public class TOSSerial extends NativeSerial implements SerialPort {
+
+  class EventDispatcher extends Thread {
     boolean m_run;
 
-    public EventDispatcher()
-    {
+    public EventDispatcher() {
       m_run = true;
     }
 
-    void dispatch_event( int event )
-    {
-      if( didEventOccur(event) )
-      {
-       SerialPortEvent ev = new SerialPortEvent( TOSSerial.this, event );
-        synchronized(m_listeners) {
+    public void open() {
+      synchronized (this) {
+        this.notify();
+        m_run = true;
+      }
+    }
+
+    public void close() {
+      m_run = false;
+      cancelWait();
+    }
+
+    private void dispatch_event(int event) {
+      if (didEventOccur(event)) {
+        SerialPortEvent ev = new SerialPortEvent(TOSSerial.this, event);
+        synchronized (m_listeners) {
           Iterator i = m_listeners.iterator();
-          while( i.hasNext() )
-            ((SerialPortListener)i.next()).serialEvent( ev );
+          while (i.hasNext())
+            ((SerialPortListener) i.next()).serialEvent(ev);
         }
       }
     }
 
-    public void run()
-    {
-      while( m_run )
-      {
-       if( waitForEvent() )
-       {
-         if( m_run )
-         {
-           dispatch_event( SerialPortEvent.BREAK_INTERRUPT );
-           dispatch_event( SerialPortEvent.CARRIER_DETECT );
-           dispatch_event( SerialPortEvent.CTS );
-           dispatch_event( SerialPortEvent.DATA_AVAILABLE );
-           dispatch_event( SerialPortEvent.DSR );
-           dispatch_event( SerialPortEvent.FRAMING_ERROR );
-           dispatch_event( SerialPortEvent.OVERRUN_ERROR );
-           dispatch_event( SerialPortEvent.OUTPUT_EMPTY );
-           dispatch_event( SerialPortEvent.PARITY_ERROR );
-           dispatch_event( SerialPortEvent.RING_INDICATOR );
-         }
-       }
+    public void run() {
+      while (true) {
+
+        synchronized (this) {
+          if (!m_run) {
+            try {
+              this.wait();
+            } catch (InterruptedException e) {
+              e.printStackTrace();
+            }
+          }
+        }
+        
+        if (waitForEvent()) {
+          dispatch_event(SerialPortEvent.BREAK_INTERRUPT);
+          dispatch_event(SerialPortEvent.CARRIER_DETECT);
+          dispatch_event(SerialPortEvent.CTS);
+          dispatch_event(SerialPortEvent.DATA_AVAILABLE);
+          dispatch_event(SerialPortEvent.DSR);
+          dispatch_event(SerialPortEvent.FRAMING_ERROR);
+          dispatch_event(SerialPortEvent.OVERRUN_ERROR);
+          dispatch_event(SerialPortEvent.OUTPUT_EMPTY);
+          dispatch_event(SerialPortEvent.PARITY_ERROR);
+          dispatch_event(SerialPortEvent.RING_INDICATOR);
+        }
       }
     }
 
-    public void close()
-    {
-      m_run = false;
-      cancelWait();
-    }
   }
 
-
-  class SerialInputStream extends InputStream
-  {
+  class SerialInputStream extends InputStream {
     ByteQueue bq = new ByteQueue(128);
 
-    protected void gather()
-    {
+    protected void gather() {
       int navail = TOSSerial.this.available();
-      if( navail > 0 )
-      {
+      if (navail > 0) {
         byte buffer[] = new byte[navail];
-        bq.push_back( buffer, 0, TOSSerial.this.read( buffer, 0, navail ) );
+        bq.push_back(buffer, 0, TOSSerial.this.read(buffer, 0, navail));
       }
     }
 
-    public int read()
-    {
+    public int read() {
       gather();
       return bq.pop_front();
     }
 
-    public int read( byte[] b )
-    {
+    public int read(byte[] b) {
       gather();
       return bq.pop_front(b);
     }
 
-    public int read( byte[] b, int off, int len )
-    {
+    public int read(byte[] b, int off, int len) {
       gather();
-      return bq.pop_front(b,off,len);
+      return bq.pop_front(b, off, len);
     }
 
-    public int available()
-    {
+    public int available() {
       gather();
       return bq.available();
     }
   }
 
-
-  class SerialOutputStream extends OutputStream
-  {
-    public void write( int b )
-    {
+  class SerialOutputStream extends OutputStream {
+    public void write(int b) {
       TOSSerial.this.write(b);
     }
 
-    public void write( byte[] b )
-    {
-      TOSSerial.this.write(b,0,b.length);
+    public void write(byte[] b) {
+      TOSSerial.this.write(b, 0, b.length);
     }
 
-    public void write( byte[] b, int off, int len )
-    {
+    public void write(byte[] b, int off, int len) {
       int nwritten = 0;
-      while( nwritten < len )
-       nwritten += TOSSerial.this.write( b, nwritten, len-nwritten );
+      while (nwritten < len)
+        nwritten += TOSSerial.this.write(b, nwritten, len - nwritten);
     }
   }
 
+  private SerialInputStream m_in;
+
+  private SerialOutputStream m_out;
 
-  SerialInputStream m_in;
-  SerialOutputStream m_out;
-  Vector m_listeners = new Vector();
-  EventDispatcher m_dispatch;
+  private Vector m_listeners = new Vector();
 
-  static String map_portname( String mapstr, String portname )
-  {
+  private EventDispatcher m_dispatch;
+
+  static String map_portname(String mapstr, String portname) {
     // mapstr is of the form "from1=to1:from2=to2"
 
     // If "from", "to", and "portname" all end port numbers, then the ports in
     // "from" and "to" are used as a bias for the port in "portname", appended
-    // to the "to" string (without its original terminating digits).  If more
+    // to the "to" string (without its original terminating digits). If more
     // than one port mapping matches, the one with the smallest non-negative
     // port number wins.
 
     // For instance, if
-    //   mapstr="com1=COM1:com10=\\.\COM10"
+    // mapstr="com1=COM1:com10=\\.\COM10"
     // then
-    //   com1 => COM1
-    //   com3 => COM3
-    //   com10 => \\.\COM10
-    //   com12 => \\.\COM12
+    // com1 => COM1
+    // com3 => COM3
+    // com10 => \\.\COM10
+    // com12 => \\.\COM12
     // or if
-    //   mapstr="com1=/dev/ttyS0:usb1=/dev/ttyS100"
+    // mapstr="com1=/dev/ttyS0:usb1=/dev/ttyS100"
     // then
-    //   com1 => /dev/ttyS0
-    //   com3 => /dev/ttyS2
-    //   usb1 => /dev/ttyS100
-    //   usb3 => /dev/ttyS102
+    // com1 => /dev/ttyS0
+    // com3 => /dev/ttyS2
+    // usb1 => /dev/ttyS100
+    // usb3 => /dev/ttyS102
 
     String maps[] = mapstr.split(":");
     Pattern pkv = Pattern.compile("(.*?)=(.*?)");
@@ -182,111 +177,122 @@ public class TOSSerial extends NativeSerial implements SerialPort
     int match_distance = -1;
     String str_port_to = null;
 
-    for( int i=0; i<maps.length; i++ )
-    {
-      Matcher mkv = pkv.matcher( maps[i] );
-      if( mkv.matches() )
-      {
-       Matcher mfrom = pnum.matcher( mkv.group(1) );
-       Matcher mto = pnum.matcher( mkv.group(2) );
-       if( mfrom.matches() && mto.matches() && mport.matches()
-           && mfrom.group(1).equalsIgnoreCase( mport.group(1) )
-         )
-       {
-         int nfrom = Integer.parseInt( mfrom.group(2) );
-         int nto = Integer.parseInt( mto.group(2) );
-         int nport_from = Integer.parseInt( mport.group(2) );
-         int nport_to = nport_from - nfrom + nto;
-         int ndist = nport_from - nfrom;
-
-         if( (ndist >= 0) && ((ndist < match_distance) || (match_distance == -1)) )
-         {
-           match_distance = ndist;
-           str_port_to = mto.group(1) + nport_to;
-         }
-       }
-       else if( mkv.group(1).equalsIgnoreCase( portname ) )
-       {
-         match_distance = 0;
-         str_port_to = mkv.group(2);
-       }
+    for (int i = 0; i < maps.length; i++) {
+      Matcher mkv = pkv.matcher(maps[i]);
+      if (mkv.matches()) {
+        Matcher mfrom = pnum.matcher(mkv.group(1));
+        Matcher mto = pnum.matcher(mkv.group(2));
+        if (mfrom.matches() && mto.matches() && mport.matches()
+            && mfrom.group(1).equalsIgnoreCase(mport.group(1))) {
+          int nfrom = Integer.parseInt(mfrom.group(2));
+          int nto = Integer.parseInt(mto.group(2));
+          int nport_from = Integer.parseInt(mport.group(2));
+          int nport_to = nport_from - nfrom + nto;
+          int ndist = nport_from - nfrom;
+
+          if ((ndist >= 0)
+              && ((ndist < match_distance) || (match_distance == -1))) {
+            match_distance = ndist;
+            str_port_to = mto.group(1) + nport_to;
+          }
+        } else if (mkv.group(1).equalsIgnoreCase(portname)) {
+          match_distance = 0;
+          str_port_to = mkv.group(2);
+        }
       }
     }
 
     return (str_port_to == null) ? portname : str_port_to;
   }
 
-  public TOSSerial( String portname )
-  {
-    super( map_portname( NativeSerial.getTOSCommMap(), portname ) );
+  public TOSSerial(String portname) {
+    super(map_portname(NativeSerial.getTOSCommMap(), portname));
     m_in = new SerialInputStream();
     m_out = new SerialOutputStream();
     m_dispatch = new EventDispatcher();
     m_dispatch.start();
   }
+  
+  /**
+   * Open the serial port connection 
+   */
+  public boolean open() {
+    if (m_dispatch != null) {
+      m_dispatch.open();
+    }
+    return super.open();
+  }
+  
+  /**
+   * Close the serial port connection
+   */
+  public void close() {
+    if(m_dispatch != null) {
+      m_dispatch.close();
+    }
+    super.close();
+  }
 
-  public void addListener( SerialPortListener l )
-  {
-    synchronized(m_listeners) {
-      if( !m_listeners.contains(l) )
+  public void addListener(SerialPortListener l) {
+    synchronized (m_listeners) {
+      if (!m_listeners.contains(l))
         m_listeners.add(l);
     }
   }
 
-  public void removeListener( SerialPortListener l )
-  {
-    synchronized(m_listeners) {
+  public void removeListener(SerialPortListener l) {
+    synchronized (m_listeners) {
       m_listeners.remove(l);
     }
   }
 
-  public InputStream getInputStream()
-  {
+  public InputStream getInputStream() {
     return m_in;
   }
 
-  public OutputStream getOutputStream()
-  {
+  public OutputStream getOutputStream() {
     return m_out;
   }
 
-  public void close()
-  {
-    if( m_dispatch != null )
+  /**
+   * Finalize the serial port connection, do not expect to open it 
+   * again
+   */
+  public void finalize() {
+    // Be careful what you call here. The object may never have been
+    // created, so the underlying C++ object may not exist, and there's
+    // insufficient guarding to avoid a core dump. If you call other
+    // methods than super.close() or super.finalize(), be sure to
+    // add an if (swigCptr != 0) guard in NativeSerial.java.
+    if (m_dispatch != null) {
       m_dispatch.close();
+    }
 
-    try { if( m_dispatch != null ) m_dispatch.join(); }
-    catch( InterruptedException e ) { }
+    /*
+    try {
+      if (m_dispatch != null) {
+        m_dispatch.join();
+      }
+    } catch (InterruptedException e) {
+    }
+    */
 
     super.close();
 
-    try
-    {
-      if( m_in != null )
-       m_in.close();
+    try {
+      if (m_in != null) {
+        m_in.close();
+      }
 
-      if( m_out != null )
-       m_out.close();
-    }
-    catch( IOException e )
-    {
+      if (m_out != null) {
+        m_out.close();
+      }
+    } catch (IOException e) {
     }
 
     m_dispatch = null;
     m_in = null;
     m_out = null;
-  }
-
-  protected void finalize()
-  {
-    // Be careful what you call here. The object may never have been
-    // created, so the underlying C++ object may not exist, and there's
-    // insufficient guarding to avoid a core dump. If you call other
-    // methods than super.close() or super.finalize(), be sure to
-    // add an if (swigCptr != 0) guard in NativeSerial.java.
-    System.out.println("Java TOSSerial finalize");
-    close();
     super.finalize();
   }
 }
-