/// <summary>
        /// Creates a new <see cref="UserDataRelayOutputPacket"/> object from the given payload.
        /// </summary>
        /// <param name="payload">The API frame payload. It must start with the frame type corresponding
        /// to a User Data Relay Output packet (<c>0xAD</c>). The byte array must be in
        /// <see cref="OperatingMode.API"/> mode.</param>
        /// <returns>Parsed User Data Relay Output packet.</returns>
        /// <exception cref="ArgumentException">If <c>payload[0] != APIFrameType.USER_DATA_RELAY_OUTPUT.GetValue()</c>
        /// or if <c>payload.length <![CDATA[<]]> <see cref="MIN_API_PAYLOAD_LENGTH"/></c>.</exception>
        /// <exception cref="ArgumentNullException">If <c><paramref name="payload"/> == null</c>.</exception>
        public static UserDataRelayOutputPacket CreatePacket(byte[] payload)
        {
            if (payload == null)
            {
                throw new ArgumentNullException("User Data Relay Output packet payload cannot be null.");
            }
            // 1 (Frame type) + 1 (Dest. interface)
            if (payload.Length < MIN_API_PAYLOAD_LENGTH)
            {
                throw new ArgumentException("Incomplete User Data Relay Output packet.");
            }
            if ((payload[0] & 0xFF) != APIFrameType.USER_DATA_RELAY_OUTPUT.GetValue())
            {
                throw new ArgumentException("Payload is not a User Data Relay Ouptut packet.");
            }

            // payload[0] is the frame type.
            int index = 1;

            // Source interface.
            XBeeLocalInterface srcInterface = XBeeLocalInterface.UNKNOWN.Get(payload[index]);

            index = index + 1;

            // Data.
            byte[] data = null;
            if (index < payload.Length)
            {
                data = new byte[payload.Length - index];
                Array.Copy(payload, index, data, 0, data.Length);
            }

            return(new UserDataRelayOutputPacket(srcInterface, data));
        }
        /// <summary>
        /// Instantiates a new <see cref="UserDataRelayMessage"/> object with the given parameters.
        /// </summary>
        /// <param name="sourceInterface">Source interface.</param>
        /// <param name="data">Data.</param>
        /// <exception cref="ArgumentException">If the source interface is unknown.</exception>
        public UserDataRelayMessage(XBeeLocalInterface sourceInterface, byte[] data)
        {
            if (sourceInterface == XBeeLocalInterface.UNKNOWN)
            {
                throw new ArgumentException("Source interface cannot be unknown.");
            }

            SourceInterface = sourceInterface;
            Data            = data;
        }
        /// <summary>
        /// Gets the <see cref="XBeeLocalInterface"/> associated with the specified ID <paramref name="value"/>.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="value">ID value to retrieve <see cref="XBeeLocalInterface"/>.</param>
        /// <returns>The <see cref="XBeeLocalInterface"/> for the specified ID <paramref name="value"/>,
        /// <see cref="XBeeLocalInterface.UNKNOWN"/> if it does not exist.</returns>
        /// <seealso cref="XBeeLocalInterface.UNKNOWN"/>
        public static XBeeLocalInterface Get(this XBeeLocalInterface source, byte value)
        {
            var values = Enum.GetValues(typeof(XBeeLocalInterface)).OfType <XBeeLocalInterface>();

            if (values.Cast <byte>().Contains(value))
            {
                return((XBeeLocalInterface)value);
            }

            return(XBeeLocalInterface.UNKNOWN);
        }
        /// <summary>
        /// Class constructor. Instantiates a new <see cref="UserDataRelayOutputPacket"/> object with the
        /// given parameters.
        /// </summary>
        /// <param name="sourceInterface">The source interface.</param>
        /// <param name="data">The data that is received from the source interface.</param>
        /// <exception cref="ArgumentException">If
        /// <c><paramref name="sourceInterface"/> == <see cref="XBeeLocalInterface.UNKNOWN"/></c>.</exception>
        /// <seealso cref="XBeeLocalInterface"/>
        public UserDataRelayOutputPacket(XBeeLocalInterface sourceInterface, byte[] data)
            : base(APIFrameType.USER_DATA_RELAY_OUTPUT)
        {
            if (sourceInterface == XBeeLocalInterface.UNKNOWN)
            {
                throw new ArgumentException("Source interface cannot be unknown.");
            }

            SourceInterface = sourceInterface;
            Data            = data;
            logger          = LogManager.GetLogger <UserDataRelayOutputPacket>();
        }
Пример #5
0
        /// <summary>
        /// Class constructor. Instantiates a new <see cref="UserDataRelayPacket"/> object with the
        /// given parameters.
        /// </summary>
        /// <param name="frameID">The Frame ID.</param>
        /// <param name="destinationInterface">The destination interface.</param>
        /// <param name="data">The data that is sent to the destination interface.</param>
        /// <exception cref="ArgumentException">>If <c><paramref name="frameID"/> <![CDATA[<]]> 0</c>
        /// or if <c><paramref name="frameID"/> <![CDATA[>]]> 255</c>
        /// or if <c><paramref name="destinationInterface"/> == <see cref="XBeeLocalInterface.UNKNOWN"/></c>.</exception>
        /// <seealso cref="XBeeLocalInterface"/>
        public UserDataRelayPacket(byte frameID, XBeeLocalInterface destinationInterface, byte[] data)
            : base(APIFrameType.USER_DATA_RELAY)
        {
            if (destinationInterface == XBeeLocalInterface.UNKNOWN)
            {
                throw new ArgumentException("Destination interface cannot be unknown.");
            }

            FrameID = frameID;
            DestinationInterface = destinationInterface;
            Data   = data;
            logger = LogManager.GetLogger <UserDataRelayPacket>();
        }
Пример #6
0
 /// <summary>
 /// Sends the provided data to the given XBee local interface.
 /// </summary>
 /// <param name="destinationInterface">Destination XBee local interface.</param>
 /// <param name="data">Data to send.</param>
 /// <exception cref="ArgumentException">If the destination interface is unknown.</exception>
 /// <exception cref="XBeeException">If there is any XBee related error sending the User
 /// Data Relay.</exception>
 /// <seealso cref="XBeeLocalInterface"/>
 /// <seealso cref="SendBluetoothData(byte[])"/>
 /// <seealso cref="SendMicroPythonData(byte[])"/>
 public new void SendUserDataRelay(XBeeLocalInterface destinationInterface, byte[] data)
 {
     base.SendUserDataRelay(destinationInterface, data);
 }
 /// <summary>
 /// Returns the <see cref="XBeeLocalInterface"/> in string format.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The <see cref="XBeeLocalInterface"/> in string format.</returns>
 public static string ToDisplayString(this XBeeLocalInterface source)
 {
     return(HexUtils.ByteToHexString((byte)source) + ": " + lookupTable[source]);
 }
 /// <summary>
 /// Gets the XBee local interface description.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The XBee local interface description.</returns>
 public static string GetDescription(this XBeeLocalInterface source)
 {
     return(lookupTable[source]);
 }
 /// <summary>
 /// Gets the XBee local interface value.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The XBee local interface value.</returns>
 public static byte GetValue(this XBeeLocalInterface source)
 {
     return((byte)source);
 }