/**
         * 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);
        }