com.motorola.oem.i2c
Class I2CDatagram

java.lang.Object
  extended by com.motorola.oem.i2c.I2CDatagram
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, javax.microedition.io.Datagram

public class I2CDatagram
extends java.lang.Object
implements javax.microedition.io.Datagram

 Holder of data to be sent or received from I2CdatagramConnection.
 
I2CDatagram consists of two parts:

Datagram header:
    • Device address - destination address (in hexadecimal).
    • Device address type - 7 bits address or 10 bits address.
    • Register address - memory location in the destination device (in hexadecimal) to write to or read from.
    • Register address length - 0,1,2,3 or 4 byte address.
    • Start type - start byte or start bit.
• receive start bit enabled - True or False. • receive stop bit enabled - True or False. Datagram Data:
    • Buffer - data buffer.
    • Length - length of the data.
    • Offset - current location in the buffer.
Applications will be able to access I2CDatagram fields by using the class methods.

I2CDatagram implements DataInput/DataOutput and therefore it provides a simple way to read and write
data in and out of the datagram buffer.
The function reset() may be called to reset the read/write point back to the beginning of the buffer.


Field Summary
static int ADDRESS_MODE_10_BITS
          Specifies 10 bits address mode.
static int ADDRESS_MODE_7_BITS
          Specifies 7 bits address mode.
static int START_TYPE_BIT
          Specifies end point with bit start type.
static int START_TYPE_BYTE
          Specifies end point with byte start type.
 
Method Summary
 void enableReceiveStartBit(boolean flag)
          Enable or disable the start bit in receive.
 void enableReceiveStopBit(boolean flag)
          Enable or disable the stop bit in receive.
 java.lang.String getAddress()
          Gets the address from the datagram header (device address and register address).
 byte[] getData()
          Gets the data buffer.
 int getDeviceAddressType()
          Gets the device's address type from the datagram header.
 int getLength()
          Gets the length of the data buffer.
 int getOffset()
          Gets the offset of the data buffer.
 int getRegisterAddressLen()
          Gets the register's address length from the datagram header.
 int getStartType()
          Gets the start type from the datagram header.
 int read()
           
 boolean readBoolean()
          See the general contract of the readBoolean method of DataInput.
 byte readByte()
          See the general contract of the readByte method of DataInput.
 char readChar()
          See the general contract of the readChar method of DataInput.
 double readDouble()
          See the general contract of the readDouble method of DataInput.
 float readFloat()
          See the general contract of the readFloat method of DataInput.
 void readFully(byte[] b)
          See the general contract of the readFully method of DataInput.
 void readFully(byte[] b, int off, int len)
          See the general contract of the readFully method of DataInput.
 int readInt()
          See the general contract of the readInt method of DataInput.
 long readLong()
          See the general contract of the readLong method of DataInput.
 short readShort()
          See the general contract of the readShort method of DataInput.
 int readUnsignedByte()
          See the general contract of the readUnsignedByte method of DataInput.
 int readUnsignedShort()
          See the general contract of the readUnsignedShort method of DataInput.
 java.lang.String readUTF()
          See the general contract of the readUTF method of DataInput.
 void reset()
          Zero the read/write pointer, offset and length parameters of the data buffer.
 void setAddress(javax.microedition.io.Datagram reference)
          Sets address in the datagram header (device address and register address), copying the address from another datagram.
 void setAddress(java.lang.String addr)
          Sets the address in the datagram header (device address and register address).
 void setData(byte[] buf, int off, int len)
          Sets the buffer, offset and length.
 void setDeviceAddressType(int deviceAddrType)
          Sets the device's address type in the datagram header.
 void setLength(int len)
          Sets the data buffer's length.
 void setRegisterAddressLen(int registerAddrLen)
          Sets the register's address's length, in bytes, in the datagram header.
 void setStartType(int startType)
          Sets the start type in the datagram header.
 int skipBytes(int n)
          See the general contract of the skipBytes method of DataInput.
 void write(byte[] b)
          Writes to the output stream all the bytes in array b.
 void write(byte[] b, int off, int len)
          Writes len bytes from array b, in order, to the output stream.
 void write(int b)
          Writes to the output stream the eight low-order bits of the argument b.
 void writeBoolean(boolean v)
          Writes a boolean value to this output stream.
 void writeByte(int v)
          Writes to the output stream the eight low- order bits of the argument v.
 void writeChar(int v)
          Writes a char value, wich is comprised of two bytes, to the output stream.
 void writeChars(java.lang.String s)
          Writes every character in the string s, to the output stream, in order, two bytes per character.
 void writeDouble(double d)
          See the general contract of the writeDouble method of DataOutput.
 void writeFloat(float f)
          See the general contract of the writeFloat method of DataOutput.
 void writeInt(int v)
          Writes an int value, which is comprised of four bytes, to the output stream.
 void writeLong(long v)
          Writes an long value, which is comprised of four bytes, to the output stream.
 void writeShort(int v)
          Writes two bytes to the output stream to represent the value of the argument.
 void writeUTF(java.lang.String str)
          Writes two bytes of length information to the output stream, followed by the Java modified UTF representation of every character in the string s.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ADDRESS_MODE_7_BITS

public static final int ADDRESS_MODE_7_BITS
Specifies 7 bits address mode.

See Also:
Constant Field Values

ADDRESS_MODE_10_BITS

public static final int ADDRESS_MODE_10_BITS
Specifies 10 bits address mode.

See Also:
Constant Field Values

START_TYPE_BYTE

public static final int START_TYPE_BYTE
Specifies end point with byte start type.

See Also:
Constant Field Values

START_TYPE_BIT

public static final int START_TYPE_BIT
Specifies end point with bit start type.

See Also:
Constant Field Values
Method Detail

enableReceiveStartBit

public void enableReceiveStartBit(boolean flag)
Enable or disable the start bit in receive.

Parameters:
flag - - TRUE - Enable the start bit in receive method.
FALSE - Disable the start bit in receive method.

enableReceiveStopBit

public void enableReceiveStopBit(boolean flag)
Enable or disable the stop bit in receive.

Parameters:
flag - - TRUE - Enable the stop bit in receive method.
FALSE - Disable the stop bit in receive method.

getAddress

public java.lang.String getAddress()
Gets the address from the datagram header (device address and register address).

Specified by:
getAddress in interface javax.microedition.io.Datagram
Returns:
The device address and register address scheme "i2c://[device address]:[register address]"
If both addresses are not yet set, return null. Otherwise, the unset address will be empty.

For example:
If the device address is 0x34, and the register address was not set, the return scheme will be "i2c://0x34:"
See Also:
setAddress(java.lang.String)

getData

public byte[] getData()
Gets the data buffer.

Specified by:
getData in interface javax.microedition.io.Datagram
Returns:
The data buffer.
See Also:
setData(byte[], int, int)

getOffset

public int getOffset()
Gets the offset of the data buffer.

Specified by:
getOffset in interface javax.microedition.io.Datagram
Returns:
The offset in the data buffer.

getLength

public int getLength()
Gets the length of the data buffer.

Specified by:
getLength in interface javax.microedition.io.Datagram
Returns:
The length of the data buffer.

setAddress

public void setAddress(java.lang.String addr)
                throws java.io.IOException
Sets the address in the datagram header (device address and register address).
The parameter addr describes the target of the I2C datagram, which takes the following scheme:
     "I2C://"[device address]:[register address]
 
The device address and register address must be in a Hexadecimal representation. Any of the addresses may be omitted. If the register address is omitted, it will not be used while referring the target.
(Only device address will be used).

Example 1 - device address is set to 0x01, while register address is set to 0x88:
     I2C://0x01:0x88
 
Example 2 - device address is set to 0x01, while register address is absent:
(Existing register address will be erased if exists)
     I2C://0x01:
 
Note: if the device address of an I2Cdatagram is not specified, the user will not be able to send or receive any data.
The register address is required in order to recive data in combined format manner.
(see I2C Spec section 9)
Register address recomended to be used in order to send or recive devices spesefic command.

Specified by:
setAddress in interface javax.microedition.io.Datagram
Parameters:
addr - The string's scheme.
Throws:
java.io.IOException - I/O error.
java.lang.IllegalArgumentException - Either the addresses are invalid or the receive scheme is incorrect.
See Also:
getAddress()

setAddress

public void setAddress(javax.microedition.io.Datagram reference)
Sets address in the datagram header (device address and register address), copying the address from another datagram.

Specified by:
setAddress in interface javax.microedition.io.Datagram
Parameters:
reference - The datagram who's addresses will be copied as the new target address for this datagram.
Throws:
java.lang.IllegalArgumentException - Address is invalid.
See Also:
getAddress()

setData

public void setData(byte[] buf,
                    int off,
                    int len)
Sets the buffer, offset and length.

Specified by:
setData in interface javax.microedition.io.Datagram
Parameters:
buf - data buffer
off - data buffer's offset
len - data buffer's length
Throws:
java.lang.IllegalArgumentException - Length or offset exceeds the buffer.
See Also:
getData()

setLength

public void setLength(int len)
Sets the data buffer's length.

Specified by:
setLength in interface javax.microedition.io.Datagram
Parameters:
len - The new length of the data buffer.
Throws:
java.lang.IllegalArgumentException - Length is either negative or exceeding the buffer.
See Also:
getLength()

reset

public void reset()
Zero the read/write pointer, offset and length parameters of the data buffer.

Specified by:
reset in interface javax.microedition.io.Datagram

getDeviceAddressType

public int getDeviceAddressType()
Gets the device's address type from the datagram header.

Returns:
Device’s address type.
The available types are:
ADDRESS_MODE_7_BITS,
ADDRESS_MODE_10_BITS.

setDeviceAddressType

public void setDeviceAddressType(int deviceAddrType)
Sets the device's address type in the datagram header.

Parameters:
deviceAddrType - The device's address Type.
The available types are:
ADDRESS_MODE_7_BITS,
ADDRESS_MODE_10_BITS.
Throws:
java.lang.IllegalArgumentException - Parameter is out of range.

setStartType

public void setStartType(int startType)
Sets the start type in the datagram header.

Parameters:
startType - The start type.
The available types are:
START_TYPE_BYTE,
START_TYPE_BIT.
Throws:
java.lang.IllegalArgumentException - Parameter is out of range.

getStartType

public int getStartType()
Gets the start type from the datagram header.

Returns:
Type.The types can be:
START_TYPE_BYTE,
START_TYPE_BIT.

setRegisterAddressLen

public void setRegisterAddressLen(int registerAddrLen)
Sets the register's address's length, in bytes, in the datagram header.

Parameters:
registerAddrLen - Register's address length.
Legal range for register address length is 0 to 4 bytes. If the register address len is set to 0 the register address will not be used(existing register address will be erased if exist).
Throws:
java.lang.IllegalArgumentException - Parameters is out of range.

getRegisterAddressLen

public int getRegisterAddressLen()
Gets the register's address length from the datagram header.

Returns:
Register’s address length.

read

public final int read()
               throws java.io.IOException
Throws:
java.io.IOException

readFully

public final void readFully(byte[] b)
                     throws java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readFully

public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

skipBytes

public final int skipBytes(int n)
                    throws java.io.IOException
See the general contract of the skipBytes method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

readDouble

public final double readDouble()
                        throws java.io.IOException
See the general contract of the readDouble method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readDouble in interface java.io.DataInput
Returns:
the double value read.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readFloat

public final float readFloat()
                      throws java.io.IOException
See the general contract of the readFloat method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFloat in interface java.io.DataInput
Returns:
the float value read.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

writeDouble

public final void writeDouble(double d)
                       throws java.io.IOException
See the general contract of the writeDouble method of DataOutput.

Bytes for this operation are write to the contained output stream.

Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException - if an I/O error occurs.

writeFloat

public final void writeFloat(float f)
                      throws java.io.IOException
See the general contract of the writeFloat method of DataOutput.

Bytes for this operation are write to the contained output stream.

Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException - if an I/O error occurs.

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException
See the general contract of the readBoolean method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
the boolean value read.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException

readByte

public final byte readByte()
                    throws java.io.IOException
See the general contract of the readByte method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readByte in interface java.io.DataInput
Returns:
the next byte of this input stream as a signed 8-bit byte.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.IOException
See the general contract of the readUnsignedByte method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the next byte of this input stream, interpreted as an unsigned 8-bit number.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readShort

public final short readShort()
                      throws java.io.IOException
See the general contract of the readShort method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readShort in interface java.io.DataInput
Returns:
the next two bytes of this input stream, interpreted as a signed 16-bit number.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readUnsignedShort

public final int readUnsignedShort()
                            throws java.io.IOException
See the general contract of the readUnsignedShort method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readChar

public final char readChar()
                    throws java.io.IOException
See the general contract of the readChar method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readChar in interface java.io.DataInput
Returns:
the next two bytes of this input stream as a Unicode character.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - if an I/O error occurs.

readInt

public final int readInt()
                  throws java.io.IOException
See the general contract of the readInt method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readInt in interface java.io.DataInput
Returns:
the next four bytes of this input stream, interpreted as an int.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.

readLong

public final long readLong()
                    throws java.io.IOException
See the general contract of the readLong method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readLong in interface java.io.DataInput
Returns:
the next eight bytes of this input stream, interpreted as a long.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException
See the general contract of the readUTF method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a Unicode string.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
See Also:
DataInputStream.readUTF(java.io.DataInput)

write

public final void write(int b)
                 throws java.io.IOException
Writes to the output stream the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the byte to be written.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Writes to the output stream all the bytes in array b. If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are written. Otherwise, the byte b[0] is written first, then b[1], and so on; the last byte written is b[b.length-1].

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the data.
Throws:
java.io.IOException - if an I/O error occurs.

write

public final void write(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
Writes len bytes from array b, in order, to the output stream. If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are written. Otherwise, the byte b[off] is written first, then b[off+1], and so on; the last byte written is b[off+len-1].

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs.

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Writes a boolean value to this output stream. If the argument v is true, the value (byte)1 is written; if v is false, the value (byte)0 is written. The byte written by this method may be read by the readBoolean method of interface DataInput, which will then return a boolean equal to v.

Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
v - the boolean to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeByte

public void writeByte(int v)
               throws java.io.IOException
Writes to the output stream the eight low- order bits of the argument v. The 24 high-order bits of v are ignored. (This means that writeByte does exactly the same thing as write for an integer argument.) The byte written by this method may be read by the readByte method of interface DataInput, which will then return a byte equal to (byte)v.

Specified by:
writeByte in interface java.io.DataOutput
Parameters:
v - the byte value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeShort

public void writeShort(int v)
                throws java.io.IOException
Writes two bytes to the output stream to represent the value of the argument. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
  

The bytes written by this method may be read by the readShort method of interface DataInput , which will then return a short equal to (short)v.

Specified by:
writeShort in interface java.io.DataOutput
Parameters:
v - the short value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeChar

public void writeChar(int v)
               throws java.io.IOException
Writes a char value, wich is comprised of two bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the readChar method of interface DataInput , which will then return a char equal to (char)v.

Specified by:
writeChar in interface java.io.DataOutput
Parameters:
v - the char value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeInt

public void writeInt(int v)
              throws java.io.IOException
Writes an int value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 24))
 (byte)(0xff & (v >> 16))
 (byte)(0xff & (v >>    8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the readInt method of interface DataInput , which will then return an int equal to v.

Specified by:
writeInt in interface java.io.DataOutput
Parameters:
v - the int value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeLong

public void writeLong(long v)
               throws java.io.IOException
Writes an long value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 48))
 (byte)(0xff & (v >> 40))
 (byte)(0xff & (v >> 32))
 (byte)(0xff & (v >> 24))
 (byte)(0xff & (v >> 16))
 (byte)(0xff & (v >>  8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the readLong method of interface DataInput , which will then return a long equal to v.

Specified by:
writeLong in interface java.io.DataOutput
Parameters:
v - the long value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Writes every character in the string s, to the output stream, in order, two bytes per character. If s is null, a NullPointerException is thrown. If s.length is zero, then no characters are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length-1]. For each character, two bytes are actually written, high-order byte first, in exactly the manner of the writeChar method.

Specified by:
writeChars in interface java.io.DataOutput
Parameters:
s - the string value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeUTF

public void writeUTF(java.lang.String str)
              throws java.io.IOException
Writes two bytes of length information to the output stream, followed by the Java modified UTF representation of every character in the string s. If s is null, a NullPointerException is thrown. Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character.

If a character c is in the range \u0001 through \u007f, it is represented by one byte:

(byte)c 

If a character c is \u0000 or is in the range \u0080 through \u07ff, then it is represented by two bytes, to be written in the order shown:


 (byte)(0xc0 | (0x1f & (c >> 6)))
 (byte)(0x80 | (0x3f & c))
  

If a character c is in the range \u0800 through uffff, then it is represented by three bytes, to be written in the order shown:


 (byte)(0xe0 | (0x0f & (c >> 12)))
 (byte)(0x80 | (0x3f & (c >>  6)))
 (byte)(0x80 | (0x3f & c))
  

First, the total number of bytes needed to represent all the characters of s is calculated. If this number is larger than 65535, then a UTFDataFormatError is thrown. Otherwise, this length is written to the output stream in exactly the manner of the writeShort method; after this, the one-, two-, or three-byte representation of each character in the string s is written.

The bytes written by this method may be read by the readUTF method of interface DataInput , which will then return a String equal to s.

Specified by:
writeUTF in interface java.io.DataOutput
Parameters:
str - the string value to be written.
Throws:
java.io.IOException - if an I/O error occurs.