com.digi.xbee.api.packet

## Class XBeePacket

- java.lang.Object
- - com.digi.xbee.api.packet.XBeePacket

- Direct Known Subclasses:
    XBeeAPIPacket

      public abstract class XBeePacket
      extends Object

  This abstract class represents the basic structure of an XBee packet.

  Derived classes should implement their own payload generation depending on their type.

  Generic actions like checksum compute or packet length calculation is performed here.

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `generateByteArray()` Generates the XBee packet byte array.
    `byte[]` | `generateByteArrayEscaped()` Generates the XBee packet byte array escaping the special bytes.
    `int` | `getChecksum()` Returns the packet checksum.
    `abstract byte[]` | `getPacketData()` Returns the packet data.
    `int` | `getPacketLength()` Returns the packet length.
    `LinkedHashMap<String,String>` | `getParameters()` Returns a map with the XBee packet parameters and their values.
    `static XBeePacket` | `parsePacket(byte[] packet, OperatingMode mode)` Parses the given byte array and returns a Generic XBee packet.
    `static XBeePacket` | `parsePacket(String packet, OperatingMode mode)` Parses the given hexadecimal string and returns a Generic XBee packet.
    `String` | `toPrettyString()` Returns a pretty string representing the packet.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Method Detail

    - #### generateByteArray

          public byte[] generateByteArray()

      Generates the XBee packet byte array.

      Use only while working in API mode 1. If API mode is 2, use `generateByteArrayEscaped()`.

      Returns:
        The XBee packet byte array.
      See Also:
        `generateByteArrayEscaped()`

    - #### generateByteArrayEscaped

          public byte[] generateByteArrayEscaped()

      Generates the XBee packet byte array escaping the special bytes.

      Use only while working in API mode 2. If API mode is 1 use `generateByteArray()`.

      Returns:
        The XBee packet byte array with escaped characters.
      See Also:
        `generateByteArray()`

    - #### getChecksum

          public int getChecksum()

      Returns the packet checksum.

      To calculate: Not including frame delimiters and length, add all bytes keeping only the lowest 8 bits of the result and subtract the result from `0xFF`.

      Returns:
        The packet checksum.

    - #### getPacketData

          public abstract byte[] getPacketData()

      Returns the packet data.
      Returns:
        The packet data.

    - #### getPacketLength

          public int getPacketLength()

      Returns the packet length.
      Returns:
        The packet length.

    - #### getParameters

          public LinkedHashMap<String,String> getParameters()

      Returns a map with the XBee packet parameters and their values.
      Returns:
        A sorted map containing the XBee packet parameters with their values.

    - #### parsePacket

          public static XBeePacket parsePacket(byte[] packet,
                               OperatingMode mode)
                                        throws InvalidPacketException

      Parses the given byte array and returns a Generic XBee packet.
      Parameters:
        `packet` - The byte array to parse.
        `mode` - The operating mode to parse the packet (API 1 or API 2).
      Returns:
        The generated Generic XBee Packet.
      Throws:
        `IllegalArgumentException` - if `mode != OperatingMode.API` and if `mode != OperatingMode.API_ESCAPE` or if `packet.length == 0`.
        `InvalidPacketException` - if the given byte array does not represent a valid frame: invalid checksum, length, start delimiter, etc.
        `NullPointerException` - if `packet == null`.
      See Also:
        `OperatingMode.API`, `OperatingMode.API_ESCAPE`

    - #### parsePacket

          public static XBeePacket parsePacket(String packet,
                               OperatingMode mode)
                                        throws InvalidPacketException

      Parses the given hexadecimal string and returns a Generic XBee packet.

      The string can contain white spaces.

      Parameters:
        `packet` - The hexadecimal string to parse.
        `mode` - The operating mode to parse the packet (API 1 or API 2).
      Returns:
        The generated Generic XBee Packet.
      Throws:
        `IllegalArgumentException` - if `mode != OperatingMode.API` and if `mode != OperatingMode.API_ESCAPE`.
        `InvalidPacketException` - if the given string does not represent a valid frame: invalid checksum, length, start delimiter, etc.
        `NullPointerException` - if `packet == null`.
      See Also:
        `OperatingMode.API`, `OperatingMode.API_ESCAPE`

    - #### toPrettyString

          public String toPrettyString()

      Returns a pretty string representing the packet.
      Returns:
        Pretty String representing the packet.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

© Copyright 2014–2014 Digi International Inc. All rights reserved.