Ejemplo n.º 1
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);
        }