/** * Parses the given byte array and returns a Generic XBee packet. * * @param packet The byte array to parse. * @param mode The operating mode to parse the packet (API 1 or API 2). * * @return The generated Generic XBee Packet. * * @throws ArgumentException if {@code mode != OperatingMode.API } and * if {@code mode != OperatingMode.API_ESCAPE} * or if {@code packet.Length == 0}. * @throws InvalidPacketException if the given byte array does not represent * a valid frame: invalid checksum, Length, * start delimiter, etc. * @throws ArgumentNullException if {@code packet == null}. * * @see com.digi.xbee.api.models.OperatingMode#API * @see com.digi.xbee.api.models.OperatingMode#API_ESCAPE */ public static XBeePacket ParsePacket(byte[] packet, OperatingMode mode) { Contract.Requires <ArgumentNullException>(packet != null, "Packet byte array cannot be null."); Contract.Requires <ArgumentException>(mode == OperatingMode.API || mode == OperatingMode.API_ESCAPE, "Operating mode must be API or API Escaped."); Contract.Requires <ArgumentException>(packet.Length != 0, "Packet Length should be greater than 0."); Contract.Requires <ArgumentException>(packet.Length == 1 || packet[0] == (byte)SpecialByte.HEADER_BYTE, "Invalid start delimiter."); XBeePacketParser parser = new XBeePacketParser(); XBeePacket xbeePacket = parser.ParsePacket(new MemoryStream(packet, 1, packet.Length - 1), mode); return(xbeePacket); }
/** * Class constructor. Instantiates a new {@code DataReader} object for the * given connection interface using the given XBee operating mode and XBee * device. * * @param connectionInterface Connection interface to read data from. * @param mode XBee operating mode. * @param xbeeDevice Reference to the XBee device containing this * {@code DataReader} object. * * @throws ArgumentNullException if {@code connectionInterface == null} or * {@code mode == null}. * * @see IConnectionInterface * @see com.digi.xbee.api.XBeeDevice * @see com.digi.xbee.api.models.OperatingMode */ public DataReader(IConnectionInterface connectionInterface, OperatingMode mode, XBeeDevice xbeeDevice) { if (connectionInterface == null) throw new ArgumentNullException("Connection interface cannot be null."); if (mode == null) throw new ArgumentNullException("Operating mode cannot be null."); this.connectionInterface = connectionInterface; this.mode = mode; this.xbeeDevice = xbeeDevice; this.logger = LogManager.GetLogger<DataReader>(); parser = new XBeePacketParser(); xbeePacketsQueue = new XBeePacketsQueue(); }
/** * Parses the given byte array and returns a Generic XBee packet. * * @param packet The byte array to parse. * @param mode The operating mode to parse the packet (API 1 or API 2). * * @return The generated Generic XBee Packet. * * @throws ArgumentException if {@code mode != OperatingMode.API } and * if {@code mode != OperatingMode.API_ESCAPE} * or if {@code packet.Length == 0}. * @throws InvalidPacketException if the given byte array does not represent * a valid frame: invalid checksum, Length, * start delimiter, etc. * @throws ArgumentNullException if {@code packet == null}. * * @see com.digi.xbee.api.models.OperatingMode#API * @see com.digi.xbee.api.models.OperatingMode#API_ESCAPE */ public static XBeePacket ParsePacket(byte[] packet, OperatingMode mode) { Contract.Requires<ArgumentNullException>(packet != null, "Packet byte array cannot be null."); Contract.Requires<ArgumentException>(mode == OperatingMode.API || mode == OperatingMode.API_ESCAPE, "Operating mode must be API or API Escaped."); Contract.Requires<ArgumentException>(packet.Length != 0, "Packet Length should be greater than 0."); Contract.Requires<ArgumentException>(packet.Length == 1 || packet[0] == (byte)SpecialByte.HEADER_BYTE, "Invalid start delimiter."); XBeePacketParser parser = new XBeePacketParser(); XBeePacket xbeePacket = parser.ParsePacket(new MemoryStream(packet, 1, packet.Length - 1), mode); return xbeePacket; }