Beispiel #1
0
        /**
         * Sends the provided data to the XBee device of the network corresponding
         * to the given 16-bit address.
         *
         * <p>This method blocks until a success or error response arrives or the
         * configured receive timeout expires.</p>
         *
         * <p>The received timeout is configured using the {@code setReceiveTimeout}
         * method and can be consulted with {@code getReceiveTimeout} method.</p>
         *
         * <p>For non-blocking operations use the method
         * {@link #sendData(XBee16BitAddress, byte[])}.</p>
         *
         * @param address The 16-bit address of the XBee that will receive the data.
         * @param data Byte array containing data to be sent.
         *
         * @throws InterfaceNotOpenException if the device is not open.
         * @throws ArgumentNullException if {@code address == null} or
         *                              if {@code data == null}.
         * @throws TimeoutException if there is a timeout sending the data.
         * @throws XBeeException if there is any other XBee related exception.
         *
         * @see com.digi.xbee.api.models.XBee16BitAddress
         * @see XBeeDevice#getReceiveTimeout()
         * @see XBeeDevice#setReceiveTimeout(int)
         * @see #sendData(RemoteXBeeDevice, byte[])
         * @see #sendData(XBee64BitAddress, byte[])
         * @see #sendDataAsync(RemoteXBeeDevice, byte[])
         * @see #sendDataAsync(XBee16BitAddress, byte[])
         * @see #sendDataAsync(XBee64BitAddress, byte[])
         */
        public void SendData(XBee16BitAddress address, byte[] data)        /*throws TimeoutException, XBeeException */
        {
            // Verify the parameters are not null, if they are null, throw an exception.
            if (address == null)
            {
                throw new ArgumentNullException("Address cannot be null");
            }
            if (data == null)
            {
                throw new ArgumentNullException("Data cannot be null");
            }

            // Check connection.
            if (!connectionInterface.SerialPort.IsOpen)
            {
                throw new InterfaceNotOpenException();
            }
            // Check if device is remote.
            if (IsRemote)
            {
                throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");
            }

            logger.InfoFormat(ToString() + "Sending data to {0} >> {1}.", address, HexUtils.PrettyHexString(data));

            XBeePacket xbeePacket = new TX16Packet(GetNextFrameID(), address, (byte)XBeeTransmitOptions.NONE, data);

            SendAndCheckXBeePacket(xbeePacket, false);
        }
        /**
         * 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);
        }
        /// <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);
        }