/**
		 * 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;
		}
예제 #2
0
        /*throws XBeeException*/
        /**
         * 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)
        {
            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;
        }