Exemplo n.º 1
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RX64IOPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 20. (start delim. + length (2 bytes) + frame
                type + 64bit addr. + rssi + receive options + rf data (5 bytes) + checksum = 20 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.RX_IO_64`.
            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=RX64IOPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.RX_IO_64.code:
            raise InvalidPacketException("This packet is not an RX 64 IO packet.")

        return RX64IOPacket(XBee64BitAddress(raw[4:12]), raw[12], raw[13], raw[14:-1])
Exemplo n.º 2
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.TXSMSPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 27. (start delim, length (2 bytes), frame type,
                frame id, transmit options, phone number (20 bytes), checksum)
            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 different than :py:attr:`.ApiFrameType.TX_SMS`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.

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

        _raw = XBeeAPIPacket._unescape_data(
            raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw

        XBeeAPIPacket._check_api_packet(
            raw, min_length=TXSMSPacket.__MIN_PACKET_LENGTH)
        if _raw[3] != ApiFrameType.TX_SMS.code:
            raise InvalidPacketException("This packet is not a TXSMSPacket")

        return TXSMSPacket(_raw[4],
                           _raw[6:25].decode("utf8").replace("\0", ""),
                           _raw[26:-1].decode("utf8"))
Exemplo n.º 3
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.FrameErrorPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 6. (start delim. + length (2 bytes) + frame
                type + frame error + checksum = 6 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.FRAME_ERROR`.
            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=FrameErrorPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.FRAME_ERROR.code:
            raise InvalidPacketException(
                "This packet is not a frame error packet.")

        return FrameErrorPacket(FrameError.get(raw[4]))
Exemplo n.º 4
0
    def create_packet(raw, operating_mode=OperatingMode.API_MODE):
        """
        Override method.

        Returns:
            :class:`.GenericXBeePacket`: the GenericXBeePacket generated.
            
        Raises:
            InvalidPacketException: if the bytearray length is less than 5. (start delim. + length (2 bytes) +
                frame type + checksum = 5 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.GENERIC`.
            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(op_mode=operating_mode)

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

        if raw[3] != ApiFrameType.GENERIC.code:
            raise InvalidPacketException(
                message="Wrong frame type, expected: " +
                ApiFrameType.GENERIC.description + ". Value: " +
                ApiFrameType.GENERIC.code)

        return GenericXBeePacket(raw[4:-1])
Exemplo n.º 5
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.TXStatusPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 7. (start delim. + length (2 bytes) + frame
                type + frame id + transmit status + checksum = 7 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if operating_mode mode is not supported.
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.TX_16`

        .. 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.")

        raw = XBeeAPIPacket._unescape_data(raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw

        XBeeAPIPacket._check_api_packet(raw, min_length=TXStatusPacket.__MIN_PACKET_LENGTH)
        if raw[3] != ApiFrameType.TX_STATUS.code:
            raise InvalidPacketException("This packet is not a TX status packet.")

        return TXStatusPacket(raw[4], TransmitStatus.get(raw[5]))
Exemplo n.º 6
0
    def create_packet(raw, operating_mode):
        """
        Override method.
        
        Returns:
            :class:`.RXSMSPacket`
            
        Raises:
            InvalidPacketException: if the bytearray length is less than 25. (start delim + length (2 bytes) +
                frame type + phone number (20 bytes) + checksum = 25 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 different than :py:attr:`.ApiFrameType.RX_SMS`.
            InvalidOperatingModeException: if ``operating_mode`` is not supported.
            
        .. seealso::
           | :meth:`.XBeePacket.create_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=RXSMSPacket.__MIN_PACKET_LENGTH)

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

        return RXSMSPacket(raw[4:23].decode("utf8").replace("\0", ""), raw[24:-1].decode("utf8"))
Exemplo n.º 7
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.DeviceResponsePacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 8. (start delim. + length (2 bytes) + frame
                type + frame id + request id + reserved + checksum = 8 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.DEVICE_RESPONSE`.
            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(op_mode=operating_mode)

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

        if raw[3] != ApiFrameType.DEVICE_RESPONSE.code:
            raise InvalidPacketException(
                message="This packet is not a device response packet.")

        return DeviceResponsePacket(raw[4], raw[5], response_data=raw[7:-1])
Exemplo n.º 8
0
    def create_packet(raw, operating_mode):
        """
        Override method.
        
        Returns:
            :class:`.RX16Packet`.
            
        Raises:
            InvalidPacketException: if the bytearray length is less than 9. (start delim. + length (2 bytes) + frame
                type + 16bit addr. + rssi + receive options + checksum = 9 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.RX_16`.
            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(op_mode=operating_mode)

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

        if raw[3] != ApiFrameType.RX_16.code:
            raise InvalidPacketException(
                message="This packet is not an RX 16 Packet")

        return RX16Packet(XBee16BitAddress(raw[4:6]),
                          raw[6],
                          raw[7],
                          rf_data=raw[8:-1])
Exemplo n.º 9
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.DeviceRequestPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 9. (start delim. + length (2 bytes) + frame
                type + request id + transport + flags + target length + checksum = 9 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.DEVICE_REQUEST`.
            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.")

        if operating_mode == OperatingMode.ESCAPED_API_MODE:
            raw = XBeePacket.unescape_data(raw)

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

        if raw[3] != ApiFrameType.DEVICE_REQUEST.code:
            raise InvalidPacketException("This packet is not a device request packet.")

        target_length = raw[7]
        return DeviceRequestPacket(raw[4], raw[8:8 + target_length].decode("utf8"), raw[8 + target_length:-1])
Exemplo n.º 10
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.IODataSampleRxIndicatorWifiPacket`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 16. (start delim. + length (2 bytes) + frame
                type + source addr. (4 bytes) + rssi + receive options + rf data (5 bytes) + checksum = 16 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if operating_mode mode is not supported.
            InvalidPacketException: if the frame type is not :attr:`.ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI`.

        .. 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.")

        raw = XBeeAPIPacket._unescape_data(raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw

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

        if raw[3] != ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI.code:
            raise InvalidPacketException("This packet is not an IO data sample RX indicator Wi-Fi packet.")

        return IODataSampleRxIndicatorWifiPacket(IPv4Address(bytes(raw[4:8])), raw[7], raw[8], raw[9:-1])
Exemplo n.º 11
0
    def create_packet(raw, operating_mode):
        """
        Override method.
        
        Returns:
            :class:`.TX64Packet`.
            
        Raises:
            InvalidPacketException: if the bytearray length is less than 15. (start delim. + length (2 bytes) + frame
                type + frame id + 64bit addr. + transmit options + 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different than the checksum field value (last byte).
            InvalidPacketException: if the frame type is different than :attr:`.ApiFrameType.TX_64`.
            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(op_mode=operating_mode)

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

        if raw[3] != ApiFrameType.TX_64.code:
            raise InvalidPacketException(
                message="This packet is not a TX 64 packet.")

        return TX64Packet(raw[4],
                          XBee64BitAddress(raw[5:13]),
                          raw[13],
                          rf_data=raw[14:-1])
Exemplo n.º 12
0
    def _check_api_packet(raw, min_length=5):
        """
        Checks the not escaped  bytearray 'raw' meets conditions.

        Args:
            raw (Bytearray): non-escaped bytearray to be checked.
            min_length (Integer): the minimum length of the packet in bytes.

        Raises:
            InvalidPacketException: if the bytearray length is less than 5.
                (start delim. + length (2 bytes) + frame type + checksum = 5 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).

        .. seealso::
           | :mod:`.factory`
        """
        if len(raw) < min_length:
            raise InvalidPacketException(
                message="Bytearray must have, at least, 5 of complete length "
                "(header, length, frameType, checksum)")

        if raw[0] & 0xFF != SpecialByte.HEADER_BYTE.code:
            raise InvalidPacketException(
                message="Bytearray must start with the header byte "
                "(SpecialByte.HEADER_BYTE.code)")

        # real frame specific data length
        real_length = len(raw[3:-1])
        # length is specified in the length field.
        length_field = utils.length_to_int(raw[1:3])
        if real_length != length_field:
            raise InvalidPacketException(
                message="The real length of this frame does not match the "
                "specified in length field (bytes 2 and 3) (real %d, "
                "length field %d)" % (real_length, length_field))

        checksum = 0xFF - (sum(raw[3:-1]) & 0xFF)
        if checksum != raw[-1]:
            raise InvalidPacketException(
                message="Wrong checksum (expected %02X, received %02X)" %
                (checksum, raw[-1]))
Exemplo n.º 13
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.SendDataRequestPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 10.
                (start delim. + length (2 bytes) + frame type + frame id
                + path length + content type length + transport + options
                + checksum = 10 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is different from
                :attr:`.ApiFrameType.SEND_DATA_REQUEST`.
            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=SendDataRequestPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.SEND_DATA_REQUEST.code:
            raise InvalidPacketException(
                message="This packet is not a send data request packet.")

        path_length = raw[5]
        content_type_length = raw[6 + path_length]
        return SendDataRequestPacket(
            raw[4],
            raw[6:6 + path_length].decode("utf8"),
            raw[6 + path_length + 1:6 + path_length + 1 +
                content_type_length].decode("utf8"),
            SendDataRequestOptions.get(raw[6 + path_length + 2 +
                                           content_type_length]),
            file_data=raw[6 + path_length + 3 + content_type_length:-1],
            op_mode=operating_mode)
Exemplo n.º 14
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.RX64Packet`

        Raises:
            InvalidPacketException: if the bytearray length is less than 15.
                (start delim. + length (2 bytes) + frame type + 64bit addr.
                + rssi + receive options + checksum = 15 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is different from
                :attr:`.ApiFrameType.RX_64`.
            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=RX64Packet.__MIN_PACKET_LENGTH)

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

        return RX64Packet(XBee64BitAddress(raw[4:12]),
                          raw[12],
                          raw[13],
                          rf_data=raw[14:-1] if
                          len(raw) > RX64Packet.__MIN_PACKET_LENGTH else None,
                          op_mode=operating_mode)
Exemplo n.º 15
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.TX16Packet`.

        Raises:
            InvalidPacketException: if the bytearray length is less than 9.
                (start delim. + length (2 bytes) + frame type + frame id
                + 16bit addr. + transmit options + checksum = 9 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is different from
                :attr:`.ApiFrameType.TX_16`.
            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=TX16Packet.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.TX_16.code:
            raise InvalidPacketException(
                message="This packet is not a TX 16 packet.")

        return TX16Packet(raw[4],
                          XBee16BitAddress(raw[5:7]),
                          raw[7],
                          rf_data=raw[8:-1] if
                          len(raw) > TX16Packet.__MIN_PACKET_LENGTH else None,
                          op_mode=operating_mode)
Exemplo n.º 16
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.TXSMSPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 27.
                (start delim, length (2 bytes), frame type, frame id,
                transmit options, phone number (20 bytes), checksum)
            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 different than
                :py:attr:`.ApiFrameType.TX_SMS`.
            InvalidOperatingModeException: if `operating_mode` is not supported.

        .. seealso::
           | :meth:`.XBeePacket.create_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=TXSMSPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.TX_SMS.code:
            raise InvalidPacketException(
                message="This packet is not a TXSMSPacket")

        data = None
        if len(raw) > TXSMSPacket.__MIN_PACKET_LENGTH:
            data = raw[26:-1]
        return TXSMSPacket(raw[4],
                           raw[6:25].decode(encoding="utf8").replace("\0",
                                                                     ""), data)
Exemplo n.º 17
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 _OPERATING_MODE mode is not supported.
            InvalidPacketException: if the frame type is not :attr:`ApiFrameType.RX_IPV4`.

        .. 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.")

        _raw = XBeeAPIPacket._unescape_data(
            raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw

        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])
Exemplo n.º 18
0
    def create_packet(raw, operating_mode):
        """
        Override method.

        Returns:
            :class:`.DeviceResponseStatusPacket`

        Raises:
            InvalidPacketException: if the bytearray length is less than 7.
                (start delim. + length (2 bytes) + frame type + frame id
                + device response status + checksum = 7 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:`.SpecialByte`.
            InvalidPacketException: if the calculated checksum is different
                from the checksum field value (last byte).
            InvalidPacketException: if the frame type is different
                from :attr:`.ApiFrameType.DEVICE_RESPONSE_STATUS`.
            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=DeviceResponseStatusPacket.__MIN_PACKET_LENGTH)

        if raw[3] != ApiFrameType.DEVICE_RESPONSE_STATUS.code:
            raise InvalidPacketException(
                message="This packet is not a device response status packet.")

        return DeviceResponseStatusPacket(raw[4],
                                          DeviceCloudStatus.get(raw[5]),
                                          op_mode=operating_mode)