/// <summary>
        /// Initializes a new instance of class <see cref="XBeeAPIPacket"/> with the
        /// specified <paramref name="frameType"/>.
        /// </summary>
        /// <param name="frameType">XBee packet frame type.</param>
        protected XBeeAPIPacket(APIFrameType frameType)
            : base()
        {
            FrameType      = frameType;
            FrameTypeValue = frameType.GetValue();

            logger = LogManager.GetLogger <XBeeAPIPacket>();
        }
Example #2
0
        /// <summary>
        /// Returns whether the given XBee packet is a User Data Relay packet or not.
        /// </summary>
        /// <param name="xbeePacket">The XBee packet to check if is a User Data Relay packet.</param>
        /// <returns><c>true</c> if the XBee packet is an User Data Relay packet, <c>false</c>
        /// otherwise.</returns>
        /// <seealso cref="XBeePacket"/>
        /// <seealso cref="UserDataRelayOutputPacket"/>
        private bool IsUserDataRelayPacket(XBeePacket xbeePacket)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            return(packetType == APIFrameType.USER_DATA_RELAY_OUTPUT);
        }
Example #3
0
        /// <summary>
        /// Returns whether or not the given XBee packet is an explicit data packet.
        /// </summary>
        /// <param name="xbeePacket">The XBee packet to check if is an explicit data packet.</param>
        /// <returns><c>true</c> if the XBee packet is an explicit data packet, <c>false</c>
        /// otherwise.</returns>
        /// <seealso cref="XBeePacket"/>
        /// <seealso cref="ExplicitRxIndicatorPacket"/>
        private bool IsExplicitDataPacket(XBeePacket xbeePacket)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            return(packetType == APIFrameType.EXPLICIT_RX_INDICATOR);
        }
        /// <summary>
        /// Initializes a new instance of class <see cref="XBeeAPIPacket"/> with the specified <paramref name="frameType"/>.
        /// </summary>
        /// <param name="frameType">XBee packet frame type.</param>
        /// <exception cref="ArgumentNullException">if <paramref name="frameType"/> is null.</exception>
        protected XBeeAPIPacket(APIFrameType frameType)
            : base()
        {
            Contract.Requires<ArgumentNullException>(frameType != APIFrameType.UNKNOWN, "Frame type cannot be unknown.");

            this.FrameType = frameType;
            FrameTypeValue = frameType.GetValue();

            this.logger = LogManager.GetLogger<XBeeAPIPacket>();
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of class <see cref="XBeeAPIPacket"/> with the specified <paramref name="frameType"/>.
        /// </summary>
        /// <param name="frameType">XBee packet frame type.</param>
        /// <exception cref="ArgumentNullException">if <paramref name="frameType"/> is null.</exception>
        protected XBeeAPIPacket(APIFrameType frameType)
            : base()
        {
            Contract.Requires <ArgumentNullException>(frameType != APIFrameType.UNKNOWN, "Frame type cannot be unknown.");

            this.FrameType = frameType;
            FrameTypeValue = frameType.GetValue();

            this.logger = LogManager.GetLogger <XBeeAPIPacket>();
        }
Example #6
0
        /// <summary>
        /// Returns whether or not the given XBee packet is an IP data packet.
        /// </summary>
        /// <param name="xbeePacket">The XBee packet to check if is an IP data packet.</param>
        /// <returns><c>true</c> if the XBee packet is an IP data packet, <c>false</c>
        /// otherwise.</returns>
        private bool IsIPDataPacket(XBeePacket xbeePacket)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            return(packetType == APIFrameType.RX_IPV4);
        }
Example #7
0
        /// <summary>
        /// Gets the <see cref="APIFrameType"/> associated with the specified ID <paramref name="value"/>.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="value">ID value to retrieve <see cref="APIFrameType"/>.</param>
        /// <returns>The <see cref="APIFrameType"/> for the specified ID <paramref name="value"/>,
        /// <see cref="APIFrameType.UNKNOWN"/> if it does not exist.</returns>
        /// <seealso cref="APIFrameType.UNKNOWN"/>
        public static APIFrameType Get(this APIFrameType source, byte value)
        {
            var values = Enum.GetValues(typeof(APIFrameType)).OfType <APIFrameType>();

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

            return(APIFrameType.UNKNOWN);
        }
Example #8
0
        /**
         * Returns whether or not the given XBee packet is a data packet.
         *
         * @param xbeePacket The XBee packet to check if is data packet.
         *
         * @return {@code true} if the XBee packet is a data packet, {@code false}
         *         otherwise.
         *
         * @see com.digi.xbee.api.packet.XBeePacket
         */
        private bool IsDataPacket(XBeePacket xbeePacket)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            switch (packetType)
            {
            case APIFrameType.RECEIVE_PACKET:
            case APIFrameType.RX_16:
            case APIFrameType.RX_64:
                return(true);

            default:
                return(false);
            }
        }
        /**
         * Returns a byte array with the remote device data to be parsed.
         *
         * @param packet The API packet that contains the data.
         *
         * @return A byte array with the data to be parsed.
         */
        private byte[] GetRemoteDeviceData(XBeeAPIPacket packet)
        {
            byte[] data = null;

            logger.TraceFormat("{0}Received packet: {1}.", xbeeDevice.ToString(), packet);

            APIFrameType frameType = packet.FrameType;

            switch (frameType)
            {
            case APIFrameType.AT_COMMAND_RESPONSE:
                ATCommandResponsePacket atResponse = (ATCommandResponsePacket)packet;
                // Check the frame ID.
                if (atResponse.FrameID != frameID)
                {
                    return(null);
                }
                // Check the command.
                if (!atResponse.Command.Equals(ND_COMMAND))
                {
                    return(null);
                }
                // Check if the 'end' command is received (empty response with OK status).
                if (atResponse.CommandValue == null || atResponse.CommandValue.Length == 0)
                {
                    discovering = atResponse.Status != ATCommandStatus.OK;
                    return(null);
                }

                logger.DebugFormat("{0}Received self response: {1}.", xbeeDevice.ToString(), packet);

                data = atResponse.CommandValue;
                break;

            default:
                break;
            }

            return(data);
        }
Example #10
0
        /// <summary>
        /// Returns whether or not the IP address of the XBee packet matches the provided one.
        /// </summary>
        /// <param name="xbeePacket">The XBee packet to compare its IP address with the provided
        /// one.</param>
        /// <param name="ipAddress">The IP address to be compared with the XBee packet's one.</param>
        /// <returns><c>true</c> if the IP address of the XBee packet (if it has) matches the
        /// provided one. <c>false</c> otherwise.</returns>
        private bool IPAddressesMatch(XBeePacket xbeePacket, IPAddress ipAddress)
        {
            if (xbeePacket == null || ipAddress == null || !(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = (xbeePacket as XBeeAPIPacket).FrameType;

            switch (packetType)
            {
            case APIFrameType.RX_IPV4:
                if ((xbeePacket as RXIPv4Packet).SourceAddress.Equals(ipAddress))
                {
                    return(true);
                }
                break;

            default:
                return(false);
            }
            return(false);
        }
Example #11
0
        /**
         * Returns whether or not the source address of the provided XBee packet
         * matches the address of the given remote XBee device.
         *
         * @param xbeePacket The XBee packet to compare its address with the
         *                   remote XBee device.
         * @param remoteXBeeDevice The remote XBee device to compare its address
         *                         with the XBee packet.
         *
         * @return {@code true} if the source address of the provided packet (if
         *         it has) matches the address of the remote XBee device.
         *
         * @see com.digi.xbee.api.RemoteXBeeDevice
         * @see com.digi.xbee.api.packet.XBeePacket
         */
        private bool AddressesMatch(XBeePacket xbeePacket, RemoteXBeeDevice remoteXBeeDevice)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            switch (packetType)
            {
            case APIFrameType.RECEIVE_PACKET:
                if (remoteXBeeDevice.Get64BitAddress() != null && ((ReceivePacket)xbeePacket).get64bitSourceAddress().Equals(remoteXBeeDevice.Get64BitAddress()))
                {
                    return(true);
                }
                if (remoteXBeeDevice.Get16BitAddress() != null && ((ReceivePacket)xbeePacket).get16bitSourceAddress().Equals(remoteXBeeDevice.Get16BitAddress()))
                {
                    return(true);
                }
                break;

            case APIFrameType.REMOTE_AT_COMMAND_RESPONSE:
                if (remoteXBeeDevice.Get64BitAddress() != null && ((RemoteATCommandResponsePacket)xbeePacket).get64bitSourceAddress().Equals(remoteXBeeDevice.Get64BitAddress()))
                {
                    return(true);
                }
                if (remoteXBeeDevice.Get16BitAddress() != null && ((RemoteATCommandResponsePacket)xbeePacket).get16bitSourceAddress().Equals(remoteXBeeDevice.Get16BitAddress()))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_16:
                if (((RX16Packet)xbeePacket).Get16bitSourceAddress().Equals(remoteXBeeDevice.Get16BitAddress()))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_64:
                if (((RX64Packet)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.Get64BitAddress()))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_IO_16:
                if (((RX16IOPacket)xbeePacket).get16bitSourceAddress().Equals(remoteXBeeDevice.Get16BitAddress()))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_IO_64:
                if (((RX64IOPacket)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.Get64BitAddress()))
                {
                    return(true);
                }
                break;

            default:
                return(false);
            }
            return(false);
        }
Example #12
0
 /// <summary>
 /// Returns the <see cref="APIFrameType"/> in string format.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The <see cref="APIFrameType"/> in string format.</returns>
 public static string ToDisplayString(this APIFrameType source)
 {
     return(string.Format("({0}) {1}", HexUtils.ByteArrayToHexString(ByteUtils.IntToByteArray((byte)source)), GetName(source)));
 }
Example #13
0
 /// <summary>
 /// Gets the API frame type name.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The API frame type name.</returns>
 public static string GetName(this APIFrameType source)
 {
     return(lookupTable.ContainsKey(source) ? lookupTable[source] : source.ToString());
 }
Example #14
0
 /// <summary>
 /// Gets the API frame type value.
 /// </summary>
 /// <param name="source"></param>
 /// <returns>The API frame type value.</returns>
 public static byte GetValue(this APIFrameType source)
 {
     return((byte)source);
 }
        /**
         * Parses the given API payload to get the right API packet, depending
         * on its API type ({@code payload[0]}).
         *
         * @param payload The payload of the API frame.
         *
         * @return The corresponding API packet or {@code UnknownXBeePacket} if
         *         the frame API type is unknown.
         *
         * @throws InvalidPacketException if the payload is invalid for the
         *                                specified frame type.
         *
         * @see APIFrameType
         * @see XBeePacket
         */
        private XBeePacket ParsePayload(byte[] payload)         /*throws InvalidPacketException*/
        {
            // Get the API frame type.
            APIFrameType apiType = APIFrameType.GENERIC.Get(payload[0]);

            if (apiType == APIFrameType.UNKNOWN)
            {
                // Create unknown packet.
                return(UnknownXBeePacket.CreatePacket(payload));
            }

            // Parse API payload depending on API ID.
            XBeePacket packet = null;

            switch (apiType)
            {
            case APIFrameType.TX_64:
                packet = TX64Packet.CreatePacket(payload);
                break;

            case APIFrameType.TX_16:
                packet = TX16Packet.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND:
                packet = ATCommandPacket.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND_QUEUE:
                packet = ATCommandQueuePacket.CreatePacket(payload);
                break;

            case APIFrameType.TRANSMIT_REQUEST:
                packet = TransmitPacket.createPacket(payload);
                break;

            case APIFrameType.REMOTE_AT_COMMAND_REQUEST:
                packet = RemoteATCommandPacket.createPacket(payload);
                break;

            case APIFrameType.RX_64:
                packet = RX64Packet.CreatePacket(payload);
                break;

            case APIFrameType.RX_16:
                packet = RX16Packet.CreatePacket(payload);
                break;

            case APIFrameType.RX_IO_64:
                packet = RX64IOPacket.CreatePacket(payload);
                break;

            case APIFrameType.RX_IO_16:
                packet = RX16IOPacket.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND_RESPONSE:
                packet = ATCommandResponsePacket.createPacket(payload);
                break;

            case APIFrameType.TX_STATUS:
                packet = TXStatusPacket.createPacket(payload);
                break;

            case APIFrameType.MODEM_STATUS:
                packet = ModemStatusPacket.CreatePacket(payload);
                break;

            case APIFrameType.TRANSMIT_STATUS:
                packet = TransmitStatusPacket.createPacket(payload);
                break;

            case APIFrameType.RECEIVE_PACKET:
                packet = ReceivePacket.createPacket(payload);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                packet = IODataSampleRxIndicatorPacket.CreatePacket(payload);
                break;

            case APIFrameType.REMOTE_AT_COMMAND_RESPONSE:
                packet = RemoteATCommandResponsePacket.createPacket(payload);
                break;

            case APIFrameType.GENERIC:
            default:
                packet = GenericXBeePacket.CreatePacket(payload);
                break;
            }
            return(packet);
        }
Example #16
0
        /// <summary>
        /// Dispatches the received XBee packet to the corresponding event handler(s).
        /// </summary>
        /// <param name="packet">The received XBee packet to be dispatched to the corresponding event
        /// handlers.</param>
        /// <seealso cref="XBeeAPIPacket"/>
        /// <seealso cref="XBeePacket"/>
        private void PacketReceived(XBeePacket packet)
        {
            // Add the packet to the packets queue.
            XBeePacketsQueue.AddPacket(packet);
            // Notify that a packet has been received to the corresponding event handlers.
            NotifyPacketReceived(packet);

            // Check if the packet is an API packet.
            if (!(packet is XBeeAPIPacket))
            {
                return;
            }

            // Get the API packet type.
            XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;
            APIFrameType  apiType   = apiPacket.FrameType;

            try
            {
                // Obtain the remote device from the packet.
                RemoteXBeeDevice remoteDevice = GetRemoteXBeeDeviceFromPacket(apiPacket);
                byte[]           data         = null;

                switch (apiType)
                {
                case APIFrameType.RECEIVE_PACKET:
                    ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                    data = receivePacket.RFData;
                    NotifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.RX_64:
                    RX64Packet rx64Packet = (RX64Packet)apiPacket;
                    data = rx64Packet.RFData;
                    NotifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.RX_16:
                    RX16Packet rx16Packet = (RX16Packet)apiPacket;
                    data = rx16Packet.RFData;
                    NotifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                    IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, ioSamplePacket.IOSample);
                    break;

                case APIFrameType.RX_IO_64:
                    RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, rx64IOPacket.IoSample);
                    break;

                case APIFrameType.RX_IO_16:
                    RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, rx16IOPacket.IoSample);
                    break;

                case APIFrameType.MODEM_STATUS:
                    ModemStatusPacket modemStatusPacket = (ModemStatusPacket)apiPacket;
                    NotifyModemStatusReceived(modemStatusPacket.Status);
                    break;

                case APIFrameType.EXPLICIT_RX_INDICATOR:
                    ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)apiPacket;
                    byte   sourceEndpoint = explicitDataPacket.SourceEndpoint;
                    byte   destEndpoint   = explicitDataPacket.DestEndpoint;
                    byte[] clusterID      = explicitDataPacket.ClusterID;
                    byte[] profileID      = explicitDataPacket.ProfileID;
                    data = explicitDataPacket.RFData;
                    // If this is an explicit packet for data transmissions in the Digi profile,
                    // notify also the data event handler and add a Receive packet to the queue.
                    if (sourceEndpoint == ExplicitRxIndicatorPacket.DATA_ENDPOINT &&
                        destEndpoint == ExplicitRxIndicatorPacket.DATA_ENDPOINT &&
                        clusterID.SequenceEqual(ExplicitRxIndicatorPacket.DATA_CLUSTER) &&
                        profileID.SequenceEqual(ExplicitRxIndicatorPacket.DIGI_PROFILE))
                    {
                        NotifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                        XBeePacketsQueue.AddPacket(new ReceivePacket(explicitDataPacket.SourceAddress64,
                                                                     explicitDataPacket.SourceAddress16,
                                                                     explicitDataPacket.ReceiveOptions,
                                                                     explicitDataPacket.RFData));
                    }
                    NotifyExplicitDataReceived(new ExplicitXBeeMessage(remoteDevice, sourceEndpoint, destEndpoint, clusterID,
                                                                       profileID, data, explicitDataPacket.IsBroadcast));
                    break;

                case APIFrameType.USER_DATA_RELAY_OUTPUT:
                    UserDataRelayOutputPacket relayPacket = (UserDataRelayOutputPacket)apiPacket;
                    NotifyUserDataRelayReceived(new UserDataRelayMessage(relayPacket.SourceInterface, relayPacket.Data));
                    break;

                case APIFrameType.RX_IPV4:
                    RXIPv4Packet rxIPv4Packet = (RXIPv4Packet)apiPacket;
                    NotifyIPDataReceived(new IPMessage(
                                             rxIPv4Packet.SourceAddress,
                                             rxIPv4Packet.SourcePort,
                                             rxIPv4Packet.DestPort,
                                             rxIPv4Packet.Protocol,
                                             rxIPv4Packet.Data));
                    break;

                case APIFrameType.RX_SMS:
                    RXSMSPacket rxSMSPacket = (RXSMSPacket)apiPacket;
                    NotifySMSReceived(new SMSMessage(rxSMSPacket.PhoneNumber, rxSMSPacket.Data));
                    break;

                default:
                    break;
                }
            }
            catch (XBeeException e)
            {
                logger.Error(e.Message, e);
            }
        }
Example #17
0
        /// <summary>
        /// Returns the remote XBee device from where the given package was sent from.
        /// </summary>
        /// <remarks>This is for internal use only.
        ///
        /// If the package does not contain information about the source, this method returns <c>null</c>
        /// (for example, <see cref="ModemStatusPacket"/>).
        ///
        /// First the device that sent the provided package is looked in the network of the local
        /// XBee device. If the remote device is not in the network, it is automatically added only
        /// if the packet contains information about the origin of the package.</remarks>
        /// <param name="apiPacket">The packet sent from the remote device.</param>
        /// <returns>The remote XBee device that sends the given packet. It may be <c>null</c> if the
        /// packet is not a known frame (<see cref="APIFrameType"/>) or if it does not contain information
        /// of the source device.</returns>
        /// <exception cref="ArgumentNullException">If <c><paramref name="apiPacket"/> == null</c>.</exception>
        /// <exception cref="XBeeException">If any error occurred while adding the device to the
        /// network.</exception>
        public RemoteXBeeDevice GetRemoteXBeeDeviceFromPacket(XBeeAPIPacket apiPacket)
        {
            if (apiPacket == null)
            {
                throw new ArgumentNullException("XBee API packet cannot be null.");
            }

            APIFrameType apiType = apiPacket.FrameType;

            RemoteXBeeDevice remoteDevice = null;
            XBee64BitAddress addr64       = null;
            XBee16BitAddress addr16       = null;

            XBeeNetwork network = xbeeDevice.GetNetwork();

            switch (apiType)
            {
            case APIFrameType.RECEIVE_PACKET:
                ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                addr64 = receivePacket.SourceAddress64;
                addr16 = receivePacket.SourceAddress16;
                if (!addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS))
                {
                    remoteDevice = network.GetDevice(addr64);
                }
                else if (!addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS))
                {
                    remoteDevice = network.GetDevice(addr16);
                }
                break;

            case APIFrameType.RX_64:
                RX64Packet rx64Packet = (RX64Packet)apiPacket;
                addr64       = rx64Packet.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_16:
                RX16Packet rx16Packet = (RX16Packet)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16Packet.SourceAddress16;
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                addr64       = ioSamplePacket.SourceAddress64;
                addr16       = ioSamplePacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_64:
                RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                addr64       = rx64IOPacket.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_16:
                RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16IOPacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.EXPLICIT_RX_INDICATOR:
                ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)apiPacket;
                addr64       = explicitDataPacket.SourceAddress64;
                addr16       = explicitDataPacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr64);
                break;

            default:
                // Rest of the types are considered not to contain information
                // about the origin of the packet.
                return(remoteDevice);
            }

            // If the origin is not in the network, add it.
            if (remoteDevice == null)
            {
                remoteDevice = CreateRemoteXBeeDevice(addr64, addr16, null);
                if (!addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS) || !addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS))
                {
                    network.AddRemoteDevice(remoteDevice);
                }
            }

            return(remoteDevice);
        }
Example #18
0
        /// <summary>
        /// Returns whether or not the source address of the provided XBee packet matches the address
        /// of the given remote XBee device.
        /// </summary>
        /// <param name="xbeePacket">The XBee packet to compare its address with the remote XBee
        /// device.</param>
        /// <param name="remoteXBeeDevice">The remote XBee device to compare its address with the XBee
        /// packet.</param>
        /// <returns><c>true</c> if the source address of the provided packet (if it has) matches the
        /// address of the remote XBee device.</returns>
        /// <seealso cref="RemoteXBeeDevice"/>
        /// <seealso cref="XBeePacket"/>
        private bool AddressesMatch(XBeePacket xbeePacket, RemoteXBeeDevice remoteXBeeDevice)
        {
            if (!(xbeePacket is XBeeAPIPacket))
            {
                return(false);
            }
            APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).FrameType;

            switch (packetType)
            {
            case APIFrameType.RECEIVE_PACKET:
                if (remoteXBeeDevice.XBee64BitAddr != null &&
                    ((ReceivePacket)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.XBee64BitAddr))
                {
                    return(true);
                }
                if (remoteXBeeDevice.XBee16BitAddr != null &&
                    !remoteXBeeDevice.XBee16BitAddr.Equals(XBee16BitAddress.UNKNOWN_ADDRESS) &&
                    ((ReceivePacket)xbeePacket).SourceAddress16.Equals(remoteXBeeDevice.XBee16BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.REMOTE_AT_COMMAND_RESPONSE:
                if (remoteXBeeDevice.XBee64BitAddr != null &&
                    ((RemoteATCommandResponsePacket)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.XBee64BitAddr))
                {
                    return(true);
                }
                if (remoteXBeeDevice.XBee16BitAddr != null &&
                    !remoteXBeeDevice.XBee16BitAddr.Equals(XBee16BitAddress.UNKNOWN_ADDRESS) &&
                    ((RemoteATCommandResponsePacket)xbeePacket).SourceAddress16.Equals(remoteXBeeDevice.XBee16BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_16:
                if (((RX16Packet)xbeePacket).SourceAddress16.Equals(remoteXBeeDevice.XBee16BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_64:
                if (((RX64Packet)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.XBee64BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_IO_16:
                if (((RX16IOPacket)xbeePacket).SourceAddress16.Equals(remoteXBeeDevice.XBee16BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.RX_IO_64:
                if (((RX64IOPacket)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.XBee64BitAddr))
                {
                    return(true);
                }
                break;

            case APIFrameType.EXPLICIT_RX_INDICATOR:
                if (((ExplicitRxIndicatorPacket)xbeePacket).SourceAddress64.Equals(remoteXBeeDevice.XBee64BitAddr))
                {
                    return(true);
                }
                break;

            default:
                return(false);
            }
            return(false);
        }
Example #19
0
        /**
         * Dispatches the received XBee packet to the corresponding listener(s).
         *
         * @param packet The received XBee packet to be dispatched to the
         *               corresponding listeners.
         *
         * @see com.digi.xbee.api.packet.XBeeAPIPacket
         * @see com.digi.xbee.api.packet.XBeePacket
         */
        private void PacketReceived(XBeePacket packet)
        {
            // Add the packet to the packets queue.
            xbeePacketsQueue.AddPacket(packet);
            // Notify that a packet has been received to the corresponding listeners.
            NotifyPacketReceived(packet);

            // Check if the packet is an API packet.
            if (!(packet is XBeeAPIPacket))
            {
                return;
            }

            // Get the API packet type.
            XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;
            APIFrameType  apiType   = apiPacket.FrameType;

            if (apiType == APIFrameType.UNKNOWN)
            {
                return;
            }

            try
            {
                // Obtain the remote device from the packet.
                RemoteXBeeDevice remoteDevice = GetRemoteXBeeDeviceFromPacket(apiPacket);
                byte[]           data         = null;

                switch (apiType)
                {
                case APIFrameType.RECEIVE_PACKET:
                    ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                    data = receivePacket.RFData;
                    notifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.RX_64:
                    RX64Packet rx64Packet = (RX64Packet)apiPacket;
                    data = rx64Packet.RFData;
                    notifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.RX_16:
                    RX16Packet rx16Packet = (RX16Packet)apiPacket;
                    data = rx16Packet.RFData;
                    notifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.IsBroadcast));
                    break;

                case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                    IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, ioSamplePacket.IOSample);
                    break;

                case APIFrameType.RX_IO_64:
                    RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, rx64IOPacket.getIOSample());
                    break;

                case APIFrameType.RX_IO_16:
                    RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                    NotifyIOSampleReceived(remoteDevice, rx16IOPacket.IOSample);
                    break;

                case APIFrameType.MODEM_STATUS:
                    ModemStatusPacket modemStatusPacket = (ModemStatusPacket)apiPacket;
                    NotifyModemStatusReceived(modemStatusPacket.Status);
                    break;

                default:
                    break;
                }
            }
            catch (XBeeException e)
            {
                logger.Error(e.Message, e);
            }
        }
Example #20
0
        /**
         * Returns the remote XBee device from where the given package was sent
         * from.
         *
         * <p><b>This is for internal use only.</b></p>
         *
         * <p>If the package does not contain information about the source, this
         * method returns {@code null} (for example, {@code ModemStatusPacket}).</p>
         *
         * <p>First the device that sent the provided package is looked in the
         * network of the local XBee device. If the remote device is not in the
         * network, it is automatically added only if the packet contains
         * information about the origin of the package.</p>
         *
         * @param packet The packet sent from the remote device.
         *
         * @return The remote XBee device that sends the given packet. It may be
         *         {@code null} if the packet is not a known frame (see
         *         {@link APIFrameType}) or if it does not contain information of
         *         the source device.
         *
         * @throws ArgumentNullException if {@code packet == null}
         * @throws XBeeException if any error occur while adding the device to the
         *                       network.
         */
        public RemoteXBeeDevice GetRemoteXBeeDeviceFromPacket(XBeeAPIPacket packet)         /*throws XBeeException*/
        {
            if (packet == null)
            {
                throw new ArgumentNullException("XBee API packet cannot be null.");
            }

            XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;
            APIFrameType  apiType   = apiPacket.FrameType;

            if (apiType == APIFrameType.UNKNOWN)
            {
                return(null);
            }

            RemoteXBeeDevice remoteDevice = null;
            XBee64BitAddress addr64       = null;
            XBee16BitAddress addr16       = null;

            XBeeNetwork network = xbeeDevice.GetNetwork();

            switch (apiType)
            {
            case APIFrameType.RECEIVE_PACKET:
                ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                addr64       = receivePacket.get64bitSourceAddress();
                addr16       = receivePacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_64:
                RX64Packet rx64Packet = (RX64Packet)apiPacket;
                addr64       = rx64Packet.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_16:
                RX16Packet rx16Packet = (RX16Packet)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16Packet.Get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                addr64       = ioSamplePacket.get64bitSourceAddress();
                addr16       = ioSamplePacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_64:
                RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                addr64       = rx64IOPacket.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_16:
                RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16IOPacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr16);
                break;

            default:
                // Rest of the types are considered not to contain information
                // about the origin of the packet.
                return(remoteDevice);
            }

            // If the origin is not in the network, add it.
            if (remoteDevice == null)
            {
                remoteDevice = createRemoteXBeeDevice(addr64, addr16, null);
                network.addRemoteDevice(remoteDevice);
            }

            return(remoteDevice);
        }
        /// <summary>
        /// Parses the given API payload to get the right API packet, depending on its API
        /// type (<c><paramref name="payload"/>[0]</c>).
        /// </summary>
        /// <param name="payload">The payload of the API frame.</param>
        /// <returns>The corresponding API packet or <see cref="UnknownXBeePacket"/> if the frame API
        /// type is unknown.</returns>
        /// <exception cref="InvalidPacketException">If the payload is invalid for the specified
        /// frame type.</exception>
        /// <seealso cref="APIFrameType"/>
        /// <seealso cref="XBeePacket"/>
        private XBeePacket ParsePayload(byte[] payload)
        {
            // Get the API frame type.
            APIFrameType apiType = APIFrameType.UNKNOWN.Get(payload[0]);

            // Parse API payload depending on API ID.
            XBeePacket packet = null;

            switch (apiType)
            {
            case APIFrameType.TX_64:
                packet = TX64Packet.CreatePacket(payload);
                break;

            case APIFrameType.TX_16:
                packet = TX16Packet.CreatePacket(payload);
                break;

            case APIFrameType.BLE_UNLOCK:
                packet = BluetoothUnlockPacket.CreatePacket(payload);
                break;

            case APIFrameType.USER_DATA_RELAY:
                packet = UserDataRelayPacket.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND:
                packet = ATCommandPacket.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND_QUEUE:
                packet = ATCommandQueuePacket.CreatePacket(payload);
                break;

            case APIFrameType.TRANSMIT_REQUEST:
                packet = TransmitPacket.CreatePacket(payload);
                break;

            case APIFrameType.EXPLICIT_ADDRESSING_COMMAND_FRAME:
                packet = ExplicitAddressingPacket.CreatePacket(payload);
                break;

            case APIFrameType.REMOTE_AT_COMMAND_REQUEST:
                packet = RemoteATCommandPacket.CreatePacket(payload);
                break;

            case APIFrameType.TX_SMS:
                packet = TXSMSPacket.CreatePacket(payload);
                break;

            case APIFrameType.TX_IPV4:
                packet = TXIPv4Packet.CreatePacket(payload);
                break;

            case APIFrameType.TX_REQUEST_TLS_PROFILE:
                packet = TXTLSProfilePacket.CreatePacket(payload);
                break;

            case APIFrameType.RX_64:
                packet = RX64Packet.CreatePacket(payload);
                break;

            case APIFrameType.RX_16:
                packet = RX16Packet.CreatePacket(payload);
                break;

            case APIFrameType.RX_IO_64:
                packet = RX64IOPacket.CreatePacket(payload);
                break;

            case APIFrameType.RX_IO_16:
                packet = RX16IOPacket.CreatePacket(payload);
                break;

            case APIFrameType.AT_COMMAND_RESPONSE:
                packet = ATCommandResponsePacket.CreatePacket(payload);
                break;

            case APIFrameType.TX_STATUS:
                packet = TXStatusPacket.CreatePacket(payload);
                break;

            case APIFrameType.MODEM_STATUS:
                packet = ModemStatusPacket.CreatePacket(payload);
                break;

            case APIFrameType.TRANSMIT_STATUS:
                packet = TransmitStatusPacket.CreatePacket(payload);
                break;

            case APIFrameType.RECEIVE_PACKET:
                packet = ReceivePacket.CreatePacket(payload);
                break;

            case APIFrameType.EXPLICIT_RX_INDICATOR:
                packet = ExplicitRxIndicatorPacket.CreatePacket(payload);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                packet = IODataSampleRxIndicatorPacket.CreatePacket(payload);
                break;

            case APIFrameType.REMOTE_AT_COMMAND_RESPONSE:
                packet = RemoteATCommandResponsePacket.CreatePacket(payload);
                break;

            case APIFrameType.RX_SMS:
                packet = RXSMSPacket.CreatePacket(payload);
                break;

            case APIFrameType.BLE_UNLOCK_RESPONSE:
                packet = BluetoothUnlockResponsePacket.CreatePacket(payload);
                break;

            case APIFrameType.USER_DATA_RELAY_OUTPUT:
                packet = UserDataRelayOutputPacket.CreatePacket(payload);
                break;

            case APIFrameType.RX_IPV4:
                packet = RXIPv4Packet.CreatePacket(payload);
                break;

            case APIFrameType.UNKNOWN:
            default:
                packet = UnknownXBeePacket.CreatePacket(payload);
                break;
            }
            return(packet);
        }