Beispiel #1
0
    def create_packet(raw, operating_mode):
        """
        Override method.
        
        Returns:
            RXIPv4Packet.

        Raises:
            InvalidPacketException: if the bytearray length is less than 15. (start delim + length (2 bytes) + frame
                type + source address (4 bytes) + dest port (2 bytes) + source port (2 bytes) + network protocol +
                status + checksum = 15 bytes)
            InvalidPacketException: if the length field of ``raw`` is different than its real length. (length field:
                bytes 2 and 3)
            InvalidPacketException: if the first byte of ``raw`` is not the header byte. See :class:`.SPECIAL_BYTE`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is not :attr:`ApiFrameType.RX_IPV4`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode != OperatingMode.ESCAPED_API_MODE and operating_mode != OperatingMode.API_MODE:
            raise InvalidOperatingModeException(operating_mode.name + " is not supported.")

        XBeeAPIPacket._check_api_packet(raw, min_length=RXIPv4Packet.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.RX_IPV4.code:
            raise InvalidPacketException("This packet is not an RXIPv4Packet.")

        return RXIPv4Packet(IPv4Address(bytes(raw[4:8])), utils.bytes_to_int(raw[8:10]),
                            utils.bytes_to_int(raw[10:12]), IPProtocol.get(raw[12]),
                            raw[14:-1])
Beispiel #2
0
    def create_socket_info(raw):
        """
        Parses the given bytearray data and returns a `SocketInfo` object.

        Args:
            raw (Bytearray): received data from the `SI` command with a socket
                ID as argument.

        Returns:
            :class:`.SocketInfo`: The socket information, or `None` if the
                provided data is invalid.
        """
        info_array = bytearray.fromhex(
            utils.hex_to_string(raw)).decode("utf8").strip().split(
                SocketInfo.__SEPARATOR)
        if len(info_array) != SocketInfo.__LIST_LENGTH:
            return None
        socket_id = int(info_array[0], 0)
        state = SocketInfoState.get_by_description(info_array[1])
        protocol = IPProtocol.get_by_description(info_array[2])
        local_port = int(info_array[3], 0)
        remote_port = int(info_array[4], 0)
        remote_addr = info_array[5]
        return SocketInfo(socket_id, state, protocol, local_port, remote_port,
                          remote_addr)
Beispiel #3
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            TXIPv4Packet.

        Raises:
            InvalidPacketException: if the bytearray length is less than 16.
                (start delim + length (2 bytes) + frame type + frame id
                + dest address (4 bytes) + dest port (2 bytes)
                + source port (2 bytes) + network protocol + transmit options
                + checksum = 16 bytes)
            InvalidPacketException: if the length field of `raw` is different
                from its real length. (length field: bytes 2 and 3)
            InvalidPacketException: if the first byte of `raw` is not the
                header byte. See :class:`.SPECIAL_BYTE`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is not
                :attr:`ApiFrameType.TX_IPV4`.
            InvalidOperatingModeException: if `operating_mode` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_packet`
           | :meth:`.XBeeAPIPacket._check_api_packet`
        """
        if operating_mode not in (OperatingMode.ESCAPED_API_MODE,
                                  OperatingMode.API_MODE):
            raise InvalidOperatingModeException(op_mode=operating_mode)

        XBeeAPIPacket._check_api_packet(
            raw, min_length=TXIPv4Packet.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.TX_IPV4.code:
            raise InvalidPacketException(
                message="This packet is not an TXIPv4Packet.")

        return TXIPv4Packet(
            raw[4],
            IPv4Address(bytes(raw[5:9])),
            utils.bytes_to_int(raw[9:11]),
            utils.bytes_to_int(raw[11:13]),
            IPProtocol.get(raw[13]),
            raw[14],
            data=raw[15:-1]
            if len(raw) > TXIPv4Packet.__MIN_PACKET_LENGTH else None,
            op_mode=operating_mode)