[Tinyos-2-commits] CVS: tinyos-2.x/support/sdk/java/net/tinyos/comm NativeSerial.java, 1.2, 1.3 SerialPort.java, 1.4, 1.5 TOSSerial.java, 1.4, 1.5

dmm rincon at users.sourceforge.net
Fri May 18 11:27:06 PDT 2007


Update of /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/comm
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv10048/net/tinyos/comm

Modified Files:
	NativeSerial.java SerialPort.java TOSSerial.java 
Log Message:
Previously, the serial forwarder would only release the COM port on exit.  Now it provides the ability to temporarily release or reconnect to the COM port when you close/open your source.  The SF GUI demonstrates this ability when you connect to your mote, stop the server and verify you can reprogram your mote at that time, restart the sf server and verify you can continue communicating with that mote... all without closing down your serial forwarder.

Index: NativeSerial.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/comm/NativeSerial.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** NativeSerial.java	12 Jul 2006 16:59:55 -0000	1.2
--- NativeSerial.java	18 May 2007 18:27:03 -0000	1.3
***************
*** 1,2 ****
--- 1,5 ----
+ 
+ package net.tinyos.comm;
+ 
  /* ----------------------------------------------------------------------------
   * This file was automatically generated by SWIG (http://www.swig.org).
***************
*** 7,22 ****
   * ----------------------------------------------------------------------------- */
  
! package net.tinyos.comm;
! 
  
  public class NativeSerial {
    private long swigCPtr;
!   protected boolean swigCMemOwn;
  
!   protected NativeSerial(long cPtr, boolean cMemoryOwn) {
      swigCMemOwn = cMemoryOwn;
      swigCPtr = cPtr;
    }
  
    protected NativeSerial() {
      this(0, false);
--- 10,72 ----
   * ----------------------------------------------------------------------------- */
  
! /**
!  * Updated to include the open() method, which allows us to keep this
!  * object while being temporarily disconnected from the serial port
!  */
  
  public class NativeSerial {
+   
+   /** The handle to the serial port we're connected to */
    private long swigCPtr;
!   
!   /** True if we have an open serial port connection */
!   private boolean swigCMemOwn;
!   
!   /** Name of the port */
!   private String myPortname = "";
!   
  
!   /**
!    * Constructor
!    * @param portname
!    */
!   public NativeSerial(String portname) {
!     this(TOSCommJNI.new_NativeSerial(portname), true);
!   }
!   
!   /**
!    * Constructor
!    * @param cPtr
!    * @param cMemoryOwn
!    */
!   private NativeSerial(long cPtr, boolean cMemoryOwn) {
      swigCMemOwn = cMemoryOwn;
      swigCPtr = cPtr;
    }
  
+ 
+   /**
+    * Reconnect to this serial port 
+    * @return true if the connection is made
+    */
+   public boolean open() {
+     if(!swigCMemOwn && !myPortname.matches("")) {
+       swigCPtr = TOSCommJNI.new_NativeSerial(myPortname);
+       swigCMemOwn = true;
+       return true;
+     }
+     
+     return false;
+   }
+ 
+   public void close() {
+     // We can come here with swigCptr == 0 from finalize if the C++
+     // constructor throws an exception. Ideally, we should guard all
+     // methods in the C++ code, but this is simpler.
+     if (swigCPtr != 0) {
+       TOSCommJNI.NativeSerial_close(swigCPtr);
+     }
+   }
+   
    protected NativeSerial() {
      this(0, false);
***************
*** 68,72 ****
  
    public boolean waitForEvent() {
!     return TOSCommJNI.NativeSerial_waitForEvent(swigCPtr);
    }
  
--- 118,126 ----
  
    public boolean waitForEvent() {
!     try {
!       return TOSCommJNI.NativeSerial_waitForEvent(swigCPtr);
!     } catch (Exception e) {
!       return false;
!     }
    }
  
***************
*** 115,132 ****
    }
  
-   public NativeSerial(String portname) {
-     this(TOSCommJNI.new_NativeSerial(portname), true);
-   }
  
-   public void close() {
-     // We can come here with swigCptr == 0 from finalize if the C++
-     // constructor throws an exception. Ideally, we should guard all
-     // methods in the C++ code, but this is simpler.
-     if (swigCPtr != 0)
-       TOSCommJNI.NativeSerial_close(swigCPtr);
-   }
  
    public int available() {
!     return TOSCommJNI.NativeSerial_available(swigCPtr);
    }
  
--- 169,180 ----
    }
  
  
  
    public int available() {
!     try {
!       return TOSCommJNI.NativeSerial_available(swigCPtr);
!     } catch (Exception e) {
!       return 0;
!     }
    }
  

Index: SerialPort.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/comm/SerialPort.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** SerialPort.java	12 Dec 2006 18:22:59 -0000	1.4
--- SerialPort.java	18 May 2007 18:27:03 -0000	1.5
***************
*** 1,4 ****
--- 1,6 ----
  //$Id$
  
+ package net.tinyos.comm;
+ 
  /* "Copyright (c) 2000-2003 The Regents of the University of California.  
   * All rights reserved.
***************
*** 23,27 ****
  //@author Cory Sharp <cssharp at eecs.berkeley.edu>
  
- package net.tinyos.comm;
  
  import java.io.*;
--- 25,28 ----
***************
*** 44,49 ****
    public OutputStream getOutputStream() throws IOException;
  
    public void close();
! 
    public void setSerialPortParams( 
      int baudrate, int dataBits, int stopBits, boolean parity )
--- 45,52 ----
    public OutputStream getOutputStream() throws IOException;
  
+   public boolean open();
    public void close();
!   public void finalize();
!   
    public void setSerialPortParams( 
      int baudrate, int dataBits, int stopBits, boolean parity )

Index: TOSSerial.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/comm/TOSSerial.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** TOSSerial.java	12 Dec 2006 18:22:59 -0000	1.4
--- TOSSerial.java	18 May 2007 18:27:03 -0000	1.5
***************
*** 22,26 ****
  
  //@author Cory Sharp <cssharp at eecs.berkeley.edu>
- 
  package net.tinyos.comm;
  
--- 22,25 ----
***************
*** 29,121 ****
  import java.util.regex.*;
  
! public class TOSSerial extends NativeSerial implements SerialPort
! {
!   class EventDispatcher extends Thread
!   {
      boolean m_run;
  
!     public EventDispatcher()
!     {
        m_run = true;
      }
  
!     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 );
          }
        }
      }
  
!     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 close()
-     {
-       m_run = false;
-       cancelWait();
-     }
    }
  
! 
!   class SerialInputStream extends InputStream
!   {
      ByteQueue bq = new ByteQueue(128);
  
!     protected void gather()
!     {
        int navail = TOSSerial.this.available();
!       if( navail > 0 )
!       {
          byte buffer[] = new byte[navail];
!         bq.push_back( buffer, 0, TOSSerial.this.read( buffer, 0, navail ) );
        }
      }
  
!     public int read()
!     {
        gather();
        return bq.pop_front();
      }
  
!     public int read( byte[] b )
!     {
        gather();
        return bq.pop_front(b);
      }
  
!     public int read( byte[] b, int off, int len )
!     {
        gather();
!       return bq.pop_front(b,off,len);
      }
  
!     public int available()
!     {
        gather();
        return bq.available();
--- 28,120 ----
  import java.util.regex.*;
  
! public class TOSSerial extends NativeSerial implements SerialPort {
! 
!   class EventDispatcher extends Thread {
      boolean m_run;
  
!     public EventDispatcher() {
        m_run = true;
      }
  
!     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);
          }
        }
      }
  
!     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);
!         }
        }
      }
  
    }
  
!   class SerialInputStream extends InputStream {
      ByteQueue bq = new ByteQueue(128);
  
!     protected void gather() {
        int navail = TOSSerial.this.available();
!       if (navail > 0) {
          byte buffer[] = new byte[navail];
!         bq.push_back(buffer, 0, TOSSerial.this.read(buffer, 0, navail));
        }
      }
  
!     public int read() {
        gather();
        return bq.pop_front();
      }
  
!     public int read(byte[] b) {
        gather();
        return bq.pop_front(b);
      }
  
!     public int read(byte[] b, int off, int len) {
        gather();
!       return bq.pop_front(b, off, len);
      }
  
!     public int available() {
        gather();
        return bq.available();
***************
*** 123,177 ****
    }
  
! 
!   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, int off, int len )
!     {
        int nwritten = 0;
!       while( nwritten < len )
! 	nwritten += TOSSerial.this.write( b, nwritten, len-nwritten );
      }
    }
  
  
!   SerialInputStream m_in;
!   SerialOutputStream m_out;
!   Vector m_listeners = new Vector();
!   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
      // than one port mapping matches, the one with the smallest non-negative
      // port number wins.
  
      // For instance, if
!     //   mapstr="com1=COM1:com10=\\.\COM10"
      // then
!     //   com1 => COM1
!     //   com3 => COM3
!     //   com10 => \\.\COM10
!     //   com12 => \\.\COM12
      // or if
!     //   mapstr="com1=/dev/ttyS0:usb1=/dev/ttyS100"
      // then
!     //   com1 => /dev/ttyS0
!     //   com3 => /dev/ttyS2
!     //   usb1 => /dev/ttyS100
!     //   usb3 => /dev/ttyS102
  
      String maps[] = mapstr.split(":");
--- 122,172 ----
    }
  
!   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, int off, int len) {
        int nwritten = 0;
!       while (nwritten < len)
!         nwritten += TOSSerial.this.write(b, nwritten, len - nwritten);
      }
    }
  
+   private SerialInputStream m_in;
  
!   private SerialOutputStream m_out;
  
!   private Vector m_listeners = new Vector();
! 
!   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
      // than one port mapping matches, the one with the smallest non-negative
      // port number wins.
  
      // For instance, if
!     // mapstr="com1=COM1:com10=\\.\COM10"
      // then
!     // com1 => COM1
!     // com3 => COM3
!     // com10 => \\.\COM10
!     // com12 => \\.\COM12
      // or if
!     // mapstr="com1=/dev/ttyS0:usb1=/dev/ttyS100"
      // then
!     // com1 => /dev/ttyS0
!     // com3 => /dev/ttyS2
!     // usb1 => /dev/ttyS100
!     // usb3 => /dev/ttyS102
  
      String maps[] = mapstr.split(":");
***************
*** 183,214 ****
      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);
! 	}
        }
      }
--- 178,203 ----
      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);
!         }
        }
      }
***************
*** 217,223 ****
    }
  
!   public TOSSerial( String portname )
!   {
!     super( map_portname( NativeSerial.getTOSCommMap(), portname ) );
      m_in = new SerialInputStream();
      m_out = new SerialOutputStream();
--- 206,211 ----
    }
  
!   public TOSSerial(String portname) {
!     super(map_portname(NativeSerial.getTOSCommMap(), portname));
      m_in = new SerialInputStream();
      m_out = new SerialOutputStream();
***************
*** 225,274 ****
      m_dispatch.start();
    }
  
!   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) {
        m_listeners.remove(l);
      }
    }
  
!   public InputStream getInputStream()
!   {
      return m_in;
    }
  
!   public OutputStream getOutputStream()
!   {
      return m_out;
    }
  
!   public void close()
!   {
!     if( m_dispatch != null )
        m_dispatch.close();
  
!     try { if( m_dispatch != null ) m_dispatch.join(); }
!     catch( InterruptedException e ) { }
  
      super.close();
  
!     try
!     {
!       if( m_in != null )
! 	m_in.close();
  
!       if( m_out != null )
! 	m_out.close();
!     }
!     catch( IOException e )
!     {
      }
  
--- 213,292 ----
      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))
          m_listeners.add(l);
      }
    }
  
!   public void removeListener(SerialPortListener l) {
!     synchronized (m_listeners) {
        m_listeners.remove(l);
      }
    }
  
!   public InputStream getInputStream() {
      return m_in;
    }
  
!   public OutputStream getOutputStream() {
      return m_out;
    }
  
!   /**
!    * 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) {
!     }
!     */
  
      super.close();
  
!     try {
!       if (m_in != null) {
!         m_in.close();
!       }
  
!       if (m_out != null) {
!         m_out.close();
!       }
!     } catch (IOException e) {
      }
  
***************
*** 276,292 ****
      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();
    }
  }
- 
--- 294,298 ----



More information about the Tinyos-2-commits mailing list