def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.UserDataRelayOutputPacket`. Raises: InvalidPacketException: if the bytearray length is less than 6. (start delim. + length (2 bytes) + frame type + relay interface + checksum = 6 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 not :attr:`.ApiFrameType.USER_DATA_RELAY_OUTPUT`. 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=UserDataRelayOutputPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.USER_DATA_RELAY_OUTPUT.code: raise InvalidPacketException( message="This packet is not a user data relay output packet.") return UserDataRelayOutputPacket(XBeeLocalInterface.get(raw[4]), data=raw[5:-1])
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 the frame type is not :attr:`.ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI`. 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=IODataSampleRxIndicatorWifiPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI.code: raise InvalidPacketException( message= "This packet is not an IO data sample RX indicator Wi-Fi packet." ) return IODataSampleRxIndicatorWifiPacket(IPv4Address(bytes(raw[4:8])), raw[7], raw[8], rf_data=raw[9:-1])
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]))
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RemoteATCommandWifiPacket` Raises: InvalidPacketException: if the Bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + frame id + dest. addr. (8 bytes) + transmit options + command (2 bytes) + checksum = 17 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.REMOTE_AT_COMMAND_REQUEST_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=RemoteATCommandWifiPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI.code: raise InvalidPacketException("This packet is not a remote AT command request Wi-Fi packet.") return RemoteATCommandWifiPacket( raw[4], IPv4Address(bytes(raw[9:13])), raw[13], raw[14:16].decode("utf8"), raw[16:-1] )
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)
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(operating_mode + " is not supported.") unesc_raw = XBeeAPIPacket._unescape_data( raw) if operating_mode == OperatingMode.ESCAPED_API_MODE else raw XBeeAPIPacket._check_api_packet( unesc_raw, min_length=GenericXBeePacket.__MIN_PACKET_LENGTH) if unesc_raw[3] != ApiFrameType.GENERIC.code: raise InvalidPacketException("Wrong frame type, expected: " + ApiFrameType.GENERIC.description + ". Value: " + ApiFrameType.GENERIC.code) return GenericXBeePacket(unesc_raw[4:-1])
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RegisterJoiningDevicePacket`. Raises: InvalidPacketException: if the bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + frame id + 64-bit registrant addr. (8 bytes) + 16-bit registrant addr. (2 bytes) + options + checksum = 17 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 not :attr:`.ApiFrameType.REGISTER_JOINING_DEVICE`. 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(operating_mode.name + " is not supported.") XBeeAPIPacket._check_api_packet( raw, min_length=RegisterJoiningDevicePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REGISTER_JOINING_DEVICE.code: raise InvalidPacketException( "This packet is not a Register Joining Device packet.") return RegisterJoiningDevicePacket(raw[4], XBee64BitAddress(raw[5:13]), RegisterKeyOptions.get(raw[15]), raw[16:-1])
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 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.FRAME_ERROR`. 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=FrameErrorPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.FRAME_ERROR.code: raise InvalidPacketException( message="This packet is not a frame error packet.") return FrameErrorPacket(FrameError.get(raw[4]))
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(op_mode=operating_mode) XBeeAPIPacket._check_api_packet( raw, min_length=RXIPv4Packet.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.RX_IPV4.code: raise InvalidPacketException( message="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]), data=raw[14:-1])
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 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.GENERIC`. 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=GenericXBeePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.GENERIC.code: raise InvalidPacketException( message="Wrong frame type, expected: %s. Value %d" % (ApiFrameType.GENERIC.description, ApiFrameType.GENERIC.code)) return GenericXBeePacket(raw[4:-1], op_mode=operating_mode)
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 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.RX_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=RXSMSPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.RX_SMS.code: raise InvalidPacketException( message="This packet is not an RXSMSPacket") return RXSMSPacket(raw[4:23].decode("utf8").replace("\0", ""), raw[24:-1].decode("utf8"))
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(op_mode=operating_mode) XBeeAPIPacket._check_api_packet( raw, min_length=DeviceRequestPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.DEVICE_REQUEST.code: raise InvalidPacketException( message="This packet is not a device request packet.") target_length = raw[7] return DeviceRequestPacket(raw[4], target=raw[8:8 + target_length].decode("utf8"), request_data=raw[8 + target_length:-1])
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RX16IOPacket`. Raises: InvalidPacketException: if the bytearray length is less than 14. (start delim. + length (2 bytes) + frame type + 16bit addr. + rssi + receive options + rf data (5 bytes) + checksum = 14 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_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(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=RX16IOPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.RX_IO_16.code: raise InvalidPacketException( "This packet is not an RX 16 IO packet.") return RX16IOPacket(XBee16BitAddress(raw[4:6]), raw[6], raw[7], raw[8:-1])
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 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.SEND_DATA_REQUEST` .. 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=SendDataRequestPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.SEND_DATA_REQUEST.code: raise InvalidPacketException("This packet is not a send data request packet.") path_length = raw[5] content_type_length = raw[6 + path_length] return SendDataRequestPacket(raw[4], str(raw[6:6 + path_length]), str(raw[6 + path_length + 1:6 + path_length + 1 + content_type_length]), SendDataRequestOptions.get(raw[6 + path_length + 2 + content_type_length]), raw[6 + path_length + 3 + content_type_length:-1])
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 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_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=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] if len(raw) > RX16Packet.__MIN_PACKET_LENGTH else None, op_mode=operating_mode)
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 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_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=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] if len(raw) > TX64Packet.__MIN_PACKET_LENGTH else None, op_mode=operating_mode)
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 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.TX_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.") if operating_mode == OperatingMode.ESCAPED_API_MODE: raw = XBeePacket._unescape_data(raw) XBeeAPIPacket._check_api_packet(raw, min_length=TXIPv4Packet.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.TX_IPV4.code: raise InvalidPacketException("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], raw[15:-1])
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RegisterDeviceStatusPacket`. Raises: InvalidPacketException: if the bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + frame id + status + checksum = 7 bytes). InvalidPacketException: if the length field of 'raw' is different than its real length. (length field: bytes 1 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 not :attr:`.ApiFrameType.REGISTER_JOINING_DEVICE_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(operating_mode.name + " is not supported.") XBeeAPIPacket._check_api_packet( raw, min_length=RegisterDeviceStatusPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REGISTER_JOINING_DEVICE_STATUS.code: raise InvalidPacketException( "This packet is not a Register Device Status packet.") return RegisterDeviceStatusPacket(raw[4], ZigbeeRegisterStatus.get(raw[5]))
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 the frame type is different than :attr:`.ApiFrameType.TX_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(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=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]))
def build_fs_command(cmd_bytearray, direction=FSCmd.REQUEST): """ Creates a file system command from raw data. Args: cmd_bytearray (Bytearray): Raw data of the packet to build. direction (Integer, optional, default=0): If this command is a request (0) or a response (1). Raises: InvalidPacketException: If `cmd_bytearray` is not a bytearray or its length is less than 1 for requests 2 for responses. .. seealso:: | :class:`.FSCmd` """ if not isinstance(cmd_bytearray, bytearray): raise TypeError("Command must be a bytearray") if direction not in (FSCmd.REQUEST, FSCmd.RESPONSE): raise ValueError("Direction must be 0 or 1") min_len = 2 if direction == FSCmd.RESPONSE else 1 if len(cmd_bytearray) < min_len: raise InvalidPacketException( message="Command bytearray must have, at least, %d bytes" % min_len) cmd_type = FSCmdType.get(cmd_bytearray[0]) if cmd_type == FSCmdType.FILE_OPEN: if direction == FSCmd.REQUEST: return OpenFileCmdRequest.create_cmd(cmd_bytearray) return OpenFileCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.FILE_CLOSE: if direction == FSCmd.REQUEST: return CloseFileCmdRequest.create_cmd(cmd_bytearray) return CloseFileCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.FILE_READ: if direction == FSCmd.REQUEST: return ReadFileCmdRequest.create_cmd(cmd_bytearray) return ReadFileCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.FILE_WRITE: if direction == FSCmd.REQUEST: return WriteFileCmdRequest.create_cmd(cmd_bytearray) return WriteFileCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.FILE_HASH: if direction == FSCmd.REQUEST: return HashFileCmdRequest.create_cmd(cmd_bytearray) return HashFileCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.DIR_CREATE: if direction == FSCmd.REQUEST: return CreateDirCmdRequest.create_cmd(cmd_bytearray) return CreateDirCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.DIR_OPEN: if direction == FSCmd.REQUEST: return OpenDirCmdRequest.create_cmd(cmd_bytearray) return OpenDirCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.DIR_CLOSE: if direction == FSCmd.REQUEST: return CloseDirCmdRequest.create_cmd(cmd_bytearray) return CloseDirCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.DIR_READ: if direction == FSCmd.REQUEST: return ReadDirCmdRequest.create_cmd(cmd_bytearray) return ReadDirCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.GET_PATH_ID: if direction == FSCmd.REQUEST: return GetPathIdCmdRequest.create_cmd(cmd_bytearray) return GetPathIdCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.RENAME: if direction == FSCmd.REQUEST: return RenameCmdRequest.create_cmd(cmd_bytearray) return RenameCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.DELETE: if direction == FSCmd.REQUEST: return DeleteCmdRequest.create_cmd(cmd_bytearray) return DeleteCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.STAT: if direction == FSCmd.REQUEST: return VolStatCmdRequest.create_cmd(cmd_bytearray) return VolStatCmdResponse.create_cmd(cmd_bytearray) if cmd_type == FSCmdType.FORMAT: if direction == FSCmd.REQUEST: return VolFormatCmdRequest.create_cmd(cmd_bytearray) return VolFormatCmdResponse.create_cmd(cmd_bytearray) return UnknownFSCmd(cmd_bytearray, direction=direction)
def build_frame(packet_bytearray, operating_mode=OperatingMode.API_MODE): """ Creates a packet from raw data. Args: packet_bytearray (Bytearray): the raw data of the packet to build. operating_mode (:class:`.OperatingMode`): the operating mode in which the raw data has been captured. .. seealso:: | :class:`.OperatingMode` """ if len(packet_bytearray) < 5: raise InvalidPacketException( message="Bytearray must have, at least, 5 bytes (header, length, " "frameType, checksum)") frame_type = ApiFrameType.get(packet_bytearray[3]) if frame_type == ApiFrameType.GENERIC: return GenericXBeePacket.create_packet(packet_bytearray, operating_mode=operating_mode) if frame_type == ApiFrameType.AT_COMMAND: return ATCommPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.AT_COMMAND_QUEUE: return ATCommQueuePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.AT_COMMAND_RESPONSE: return ATCommResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RECEIVE_PACKET: return ReceivePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_64: return RX64Packet.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_16: return RX16Packet.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_AT_COMMAND_REQUEST: return RemoteATCommandPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_AT_COMMAND_RESPONSE: return RemoteATCommandResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.TRANSMIT_REQUEST: return TransmitPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.TRANSMIT_STATUS: return TransmitStatusPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.MODEM_STATUS: return ModemStatusPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.TX_STATUS: return TXStatusPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_IO_16: return RX16IOPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_IO_64: return RX64IOPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR: return IODataSampleRxIndicatorPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.EXPLICIT_ADDRESSING: return ExplicitAddressingPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.EXPLICIT_RX_INDICATOR: return ExplicitRXIndicatorPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.TX_SMS: return TXSMSPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.TX_IPV4: return TXIPv4Packet.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_SMS: return RXSMSPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.USER_DATA_RELAY_OUTPUT: return UserDataRelayOutputPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.RX_IPV4: return RXIPv4Packet.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI: return RemoteATCommandWifiPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SEND_DATA_REQUEST: return SendDataRequestPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.DEVICE_RESPONSE: return DeviceResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.USER_DATA_RELAY_REQUEST: return UserDataRelayPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_AT_COMMAND_RESPONSE_WIFI: return RemoteATCommandResponseWifiPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.IO_DATA_SAMPLE_RX_INDICATOR_WIFI: return IODataSampleRxIndicatorWifiPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SEND_DATA_RESPONSE: return SendDataResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.DEVICE_REQUEST: return DeviceRequestPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.DEVICE_RESPONSE_STATUS: return DeviceResponseStatusPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.FRAME_ERROR: return FrameErrorPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REGISTER_JOINING_DEVICE: return RegisterJoiningDevicePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REGISTER_JOINING_DEVICE_STATUS: return RegisterDeviceStatusPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.ROUTE_RECORD_INDICATOR: return RouteRecordIndicatorPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CREATE: return SocketCreatePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CREATE_RESPONSE: return SocketCreateResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_OPTION_REQUEST: return SocketOptionRequestPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_OPTION_RESPONSE: return SocketOptionResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CONNECT: return SocketConnectPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CONNECT_RESPONSE: return SocketConnectResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CLOSE: return SocketClosePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_CLOSE_RESPONSE: return SocketCloseResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_SEND: return SocketSendPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_SENDTO: return SocketSendToPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_BIND: return SocketBindListenPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_LISTEN_RESPONSE: return SocketListenResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_NEW_IPV4_CLIENT: return SocketNewIPv4ClientPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_RECEIVE: return SocketReceivePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_RECEIVE_FROM: return SocketReceiveFromPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.SOCKET_STATE: return SocketStatePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.DIGIMESH_ROUTE_INFORMATION: return RouteInformationPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.FILE_SYSTEM_REQUEST: return FSRequestPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.FILE_SYSTEM_RESPONSE: return FSResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_FILE_SYSTEM_REQUEST: return RemoteFSRequestPacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.REMOTE_FILE_SYSTEM_RESPONSE: return RemoteFSResponsePacket.create_packet(packet_bytearray, operating_mode) if frame_type == ApiFrameType.OTA_FIRMWARE_UPDATE_STATUS: return OTAFirmwareUpdateStatusPacket.create_packet(packet_bytearray, operating_mode) return UnknownXBeePacket.create_packet(packet_bytearray, operating_mode=operating_mode)
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RouteInformationPacket`. Raises: InvalidPacketException: If the bytearray length is less than 46. (start delim. + length (2 bytes) + frame type + src_event + length + timestamp (4 bytes) + ack timeout count + tx blocked count + reserved + dest addr (8 bytes) + src addr (8 bytes) + responder addr (8 bytes) + successor addr (8 bytes) + checksum = 46 bytes). InvalidPacketException: If the length field of `raw` is different from its real length. (length field: bytes 1 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 not :attr:`.ApiFrameType.DIGIMESH_ROUTE_INFORMATION`. InvalidPacketException: If the internal length byte of the rest of the frame (without the checksum) is different from its real length. 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(operating_mode.name + " is not supported.") XBeeAPIPacket._check_api_packet( raw, min_length=RouteInformationPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.DIGIMESH_ROUTE_INFORMATION.code: raise InvalidPacketException( "This packet is not a Route Information packet.") # 7: frame len starting from this byte (index 5) and without the checksum if raw[5] != len(raw) - 7: raise InvalidPacketException( "Length does not match with the data length") additional_data = [] if len(raw) > RouteInformationPacket.__MIN_PACKET_LENGTH: additional_data = raw[45:] packet = RouteInformationPacket(raw[4], utils.bytes_to_int(raw[6:10]), raw[10], raw[11], XBee64BitAddress(raw[13:21]), XBee64BitAddress(raw[21:29]), XBee64BitAddress(raw[29:37]), XBee64BitAddress(raw[37:45]), additional_data, op_mode=operating_mode) packet._reserved = raw[12] return packet