lejos.nxt.comm
Class NXTConnection

java.lang.Object
  extended by lejos.nxt.comm.NXTConnection
All Implemented Interfaces:
Connection, InputConnection, OutputConnection, StreamConnection
Direct Known Subclasses:
BTConnection, RS485Connection, USBConnection

public abstract class NXTConnection
extends Object
implements StreamConnection

Generic lejos nxt connection class. Provide access to standard read/write methods. This code supports both asynchronous (used for Bluetooth connections) and synchronous (used for USB and RS485) I/O operations for the actual reading and writing of the low level buffers. NOTE: The code in this class makes a number of assumptions: 1. The input and output buffers have been sized to match the underlying device, and the packet header size. In particular the synchronous code assumes that the entire output buffer can be written using a single atomic write operation. 2. Although the code can handle any size packet header the default is assumed to be 2 bytes. If this is not the case (for example USB), then the setIOMode function must be over-ridden. 3. This class allows the use of a "soft" EOF implementation which uses a zero length packet as an EOF marker. This only operates when in packet mode and can be overridden. Currently this is used for USB and RS485 devices. It is not used for Bluetooth connections. 4. Some devices (like USB), have an inherent packet structure. The current PC assumes that when in packet mode an entire packet will fit within a single USB packet. This limits the maximum packet size which can be used over USB connections to 63 bytes. This code does not currently enforce this limit.

Author:
andy

Field Summary
static int LCP
           
static int PACKET
           
static int RAW
           
 
Constructor Summary
NXTConnection()
           
 
Method Summary
 int available()
           
 int available(int what)
          Indicate the number of bytes available to be read.
 void close()
          Close the connection.
 String getAddress()
           
 DataInputStream openDataInputStream()
          Return the DataInputStream for this connect
 DataOutputStream openDataOutputStream()
          Return the DataOutputStream for this connection.
 InputStream openInputStream()
          Return the InputStream for this connection.
 OutputStream openOutputStream()
          Return the OutputStream for this connection
 int read(byte[] data, int len)
          Perform a blocking read on the connection
 int read(byte[] data, int outLen, boolean wait)
          Attempt to read data from the connection.
 int readPacket(byte[] buf, int len)
          Read a packet from the stream.
 int sendPacket(byte[] buf, int bufLen)
          Send a data packet.
 void setIOMode(int mode)
          Set operating mode.
 int write(byte[] data, int len)
          Perform a blocking write on the connection
 int write(byte[] data, int len, boolean wait)
          Attempt to write bytes to the Bluetooth connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LCP

public static final int LCP
See Also:
Constant Field Values

PACKET

public static final int PACKET
See Also:
Constant Field Values

RAW

public static final int RAW
See Also:
Constant Field Values
Constructor Detail

NXTConnection

public NXTConnection()
Method Detail

getAddress

public String getAddress()

write

public int write(byte[] data,
                 int len,
                 boolean wait)
Attempt to write bytes to the Bluetooth connection. Optionally wait if it is not possible to write at the moment. Supports both packet and stream write operations. If in packet mode a set of header bytes indicating the size of the packet will be sent ahead of the data. NOTE: If in packet mode and writing large packets (> 254 bytes), then the blocking mode (wait = true), should be used to ensure that the packet is sent correctly.

Parameters:
data - The data to be written.
len - The number of bytes to write.
wait - True if the call should block until all of the data has been sent.
Returns:
> 0 number of bytes written. 0 Request would have blocked (and wait was false). -1 An error occurred -2 Data has been lost (See notes above).

read

public int read(byte[] data,
                int outLen,
                boolean wait)
Attempt to read data from the connection. Optionally wait for data to become available. Supports both packet and stream mode operations. When in packet mode the packet length bytes are automatically processed. The read will return just a single packet. If the packet is larger then the requested length then the rest of the packet will be returned in the following reads. If wait is true then in packet mode the call will wait until either the entire packet can be read or outLen bytes are available. In stream mode the call will return if at least 1 byte has been read.

Parameters:
data - Location to return the data. If null the data is discarded.
outLen - Max number of bytes to read.
wait - Should the call block waiting for data.
Returns:
> 0 number of bytes read. 0 no bytes available (and wait was false). -1 EOF/Connection closed. -2 data lost (see notes). -3 Some other error

available

public int available(int what)
Indicate the number of bytes available to be read. Supports both packet mode and stream connections.

Parameters:
what - 0 (all modes) return the number of bytes that can be read without blocking. 1 (packet mode) return the number of bytes still to be read from the current packet. 2 (packet mode) return the length of the current packet.
Returns:
number of bytes available

available

public int available()

setIOMode

public void setIOMode(int mode)
Set operating mode. Controls the packet/stream mode of this channel. For packet mode it defines the header size to be used.

Parameters:
mode - I/O mode to be used for this connection. NXTConnection.RAW, .LCP, or .PACKET

read

public int read(byte[] data,
                int len)
Perform a blocking read on the connection

Parameters:
data - byte array to store the results.
len - max number of bytes to read
Returns:
actual number of bytes read, return < 0 for error

write

public int write(byte[] data,
                 int len)
Perform a blocking write on the connection

Parameters:
data - byte array to be written.
len - number of bytes to write
Returns:
actual number of bytes written, return < 0 for error

openInputStream

public InputStream openInputStream()
Return the InputStream for this connection.

Specified by:
openInputStream in interface InputConnection
Returns:
the input stream

openOutputStream

public OutputStream openOutputStream()
Return the OutputStream for this connection

Specified by:
openOutputStream in interface OutputConnection
Returns:
the output stream

openDataInputStream

public DataInputStream openDataInputStream()
Return the DataInputStream for this connect

Specified by:
openDataInputStream in interface InputConnection
Returns:
the data input stream

openDataOutputStream

public DataOutputStream openDataOutputStream()
Return the DataOutputStream for this connection.

Specified by:
openDataOutputStream in interface OutputConnection
Returns:
the data output stream

close

public void close()
Close the connection. Flush any pending output. Inform the remote side that the connection is now closed. Free resources.

Specified by:
close in interface Connection

readPacket

public int readPacket(byte[] buf,
                      int len)
Read a packet from the stream. Do not block and for small packets (< bufSz), do not return a partial packet.

Parameters:
buf - Buffer to read data into.
len - Number of bytes to read.
Returns:
> 0 number of bytes read. other values see read.

sendPacket

public int sendPacket(byte[] buf,
                      int bufLen)
Send a data packet. Must be in data mode.

Parameters:
buf - the data to send
bufLen - the number of bytes to send
Returns:
number of bytes written