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])
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(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:`.RemoteATCommandResponseWifiPacket`. Raises: InvalidPacketException: if the bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + frame id + source addr. (8 bytes) + command (2 bytes) + receive 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.REMOTE_AT_COMMAND_RESPONSE_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(operating_mode.name + " is not supported.") XBeeAPIPacket._check_api_packet(raw, min_length=RemoteATCommandResponseWifiPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REMOTE_AT_COMMAND_RESPONSE_WIFI.code: raise InvalidPacketException("This packet is not a remote AT command response Wi-Fi packet.") return RemoteATCommandResponseWifiPacket(raw[4], IPv4Address(bytes(raw[9:13])), raw[13:15].decode("utf8"), ATCommandStatus.get(raw[15]), raw[16:-1])
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.SendDataResponsePacket` Raises: InvalidPacketException: if the bytearray length is less than 10. (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 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.SEND_DATA_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(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=SendDataResponsePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.SEND_DATA_RESPONSE.code: raise InvalidPacketException("This packet is not a send data response packet.") return SendDataResponsePacket(raw[4], DeviceCloudStatus.get(raw[5]))
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"))
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.") 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]))
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.") if operating_mode == OperatingMode.ESCAPED_API_MODE: raw = XBeePacket._unescape_data(raw) 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]))
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.UserDataRelayPacket`. Raises: InvalidPacketException: if the bytearray length is less than 7. (start delim. + length (2 bytes) + frame type + frame id + relay interface + 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 not :attr:`.ApiFrameType.USER_DATA_RELAY_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=UserDataRelayPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.USER_DATA_RELAY_REQUEST.code: raise InvalidPacketException( message="This packet is not a user data relay packet.") return UserDataRelayPacket(raw[4], XBeeLocalInterface.get([5]), data=raw[6:-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(operating_mode.name + " is not supported.") 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])
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])
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(operating_mode.name + " is not supported.") XBeeAPIPacket._check_api_packet( raw, min_length=DeviceResponsePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.DEVICE_RESPONSE.code: raise InvalidPacketException( "This packet is not a device response packet.") return DeviceResponsePacket(raw[4], raw[5], raw[7:-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 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(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=RX16Packet.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.RX_16.code: raise InvalidPacketException("This packet is not an RX 16 Packet") return RX16Packet(XBee16BitAddress(raw[4:6]), raw[6], raw[7], raw[8:-1])
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], raw[14:-1])
def wait_for_frame(self, operating_mode): """ Reads the next packet. Starts to read when finds the start delimiter. The last byte read is the checksum. If there is something in the COM buffer after the start delimiter, this method discards it. If the method can't read a complete and correct packet, it will return `None`. Args: operating_mode (:class:`.OperatingMode`): The operating mode in which the packet should be read. Returns: Bytearray: The read packet as bytearray if a packet is read, `None` otherwise. """ self._is_reading = True try: xbee_packet = bytearray(1) # Add packet delimiter. xbee_packet[0] = self.read_byte() while xbee_packet[0] != SpecialByte.HEADER_BYTE.value: # May be set to false by self.quit_reading() as a stop reading # request. if not self._is_reading: return None xbee_packet[0] = self.read_byte() # Add packet length. packet_length_byte = bytearray() for _ in range(2): packet_length_byte += self.__read_next_byte(operating_mode) xbee_packet += packet_length_byte # Length needs to be un-escaped in API escaped mode to obtain its # integer equivalent. if operating_mode == OperatingMode.ESCAPED_API_MODE: length = utils.length_to_int( XBeeAPIPacket.unescape_data(packet_length_byte)) else: length = utils.length_to_int(packet_length_byte) # Add packet payload. for _ in range(length): xbee_packet += self.__read_next_byte(operating_mode) # Add packet checksum. xbee_packet += self.__read_next_byte(operating_mode) # Return the packet unescaped. if operating_mode == OperatingMode.ESCAPED_API_MODE: return XBeeAPIPacket.unescape_data(xbee_packet) return xbee_packet except digi.xbee.exception.TimeoutException: return None
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.CreateSourceRoutePacket`. Raises: InvalidPacketException: If the bytearray length is less than 18. (start delim. + length (2 bytes) + frame type + frame id + 64-bit addr. + 16-bit addr. + Route command options + num of addrs + hops 16-bit addrs + checksum = 18 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.CREATE_SOURCE_ROUTE`. InvalidPacketException: If the number of hops does not match with the number of 16-bit addresses. 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=CreateSourceRoutePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.CREATE_SOURCE_ROUTE.code: raise InvalidPacketException( "This packet is not a Create Source Route packet.") hops = [ XBee16BitAddress(raw[i:i + 2]) for i in range(17, len(raw) - 1, 2) ] if raw[16] != len(hops): raise InvalidPacketException("Specified number of hops does not" "match with the length of addresses.") return CreateSourceRoutePacket(raw[4], XBee64BitAddress(raw[5:13]), XBee16BitAddress(raw[13:15]), raw[15], hops, op_mode=operating_mode)
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.RouteRecordIndicatorPacket`. Raises: InvalidPacketException: If the bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + 64bit addr. + 16bit addr. + Receive options + num of addrs + checksum = 17 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.ROUTE_RECORD_INDICATOR`. InvalidPacketException: If the number of hops does not match with the number of 16-bit addresses. 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=RouteRecordIndicatorPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.ROUTE_RECORD_INDICATOR.code: raise InvalidPacketException( "This packet is not a Route Record Indicator packet.") hops = [ XBee16BitAddress(raw[i:i + 2]) for i in range(16, len(raw) - 1, 2) ] if raw[15] != len(hops): raise InvalidPacketException("Specified number of hops does not" "match with the length of addresses.") return RouteRecordIndicatorPacket(XBee64BitAddress(raw[4:12]), XBee16BitAddress(raw[12:14]), raw[14], hops, op_mode=operating_mode)
def __try_read_packet(self, operating_mode=OperatingMode.API_MODE): """ Reads the next packet. Starts to read when finds the start delimiter. The last byte read is the checksum. If there is something in the COM buffer after the start delimiter, this method discards it. If the method can't read a complete and correct packet, it will return ``None``. Args: operating_mode (:class:`.OperatingMode`): the operating mode in which the packet should be read. Returns: Bytearray: the read packet as bytearray if a packet is read, ``None`` otherwise. """ try: xbee_packet = bytearray(1) # Add packet delimiter. xbee_packet[0] = self.__serial_port.read_byte() while xbee_packet[0] != SpecialByte.HEADER_BYTE.value: xbee_packet[0] = self.__serial_port.read_byte() # Add packet length. packet_length_byte = bytearray() for _ in range(0, 2): packet_length_byte += self.__read_next_byte(operating_mode) xbee_packet += packet_length_byte # Length needs to be un-escaped in API escaped mode to obtain its integer equivalent. if operating_mode == OperatingMode.ESCAPED_API_MODE: length = utils.length_to_int( XBeeAPIPacket.unescape_data(packet_length_byte)) else: length = utils.length_to_int(packet_length_byte) # Add packet payload. for _ in range(0, length): xbee_packet += self.__read_next_byte(operating_mode) # Add packet checksum. for _ in range(0, 1): xbee_packet += self.__read_next_byte(operating_mode) # Return the packet unescaped. if operating_mode == OperatingMode.ESCAPED_API_MODE: return XBeeAPIPacket.unescape_data(xbee_packet) else: return xbee_packet except TimeoutException: return None
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)
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)
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.OTAFirmwareUpdateStatusPacket`. Raises: InvalidPacketException: if the bytearray length is less than 17. (start delim. + length (2 bytes) + frame type + source 64bit addr. (8 bytes) + updater 16bit addr. (2 bytes) + receive options + bootloader message type + block number + source 64bit addr. (8 bytes) + checksum = 27 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.OTA_FIRMWARE_UPDATE_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=OTAFirmwareUpdateStatusPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.OTA_FIRMWARE_UPDATE_STATUS.code: raise InvalidPacketException( "This packet is not an OTA Firmware Update Status packet.") return OTAFirmwareUpdateStatusPacket(XBee64BitAddress(raw[4:12]), XBee16BitAddress(raw[12:14]), raw[14], EmberBootloaderMessageType.get( raw[15]), raw[16], XBee64BitAddress(raw[17:25]), op_mode=operating_mode)
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 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.REMOTE_AT_COMMAND_REQUEST_WIFI`. 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=RemoteATCommandWifiPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REMOTE_AT_COMMAND_REQUEST_WIFI.code: raise InvalidPacketException( message= "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"), parameter=raw[16:-1] if len(raw) > RemoteATCommandWifiPacket.__MIN_PACKET_LENGTH else None, op_mode=operating_mode)
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 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.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], op_mode=operating_mode)
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)
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)
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): """ Override method. Returns: :class:`.RemoteFSResponsePacket` Raises: InvalidPacketException: If the bytearray length is less than 8 + the minimum length of the command. (start delim. + length (2 bytes) + frame type + frame id + 64bit addr. + receive options + fs cmd id + status + checksum + cmd data = 17 bytes + cmd data). 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.REMOTE_FILE_SYSTEM_RESPONSE`. 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=RemoteFSResponsePacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.REMOTE_FILE_SYSTEM_RESPONSE.code: raise InvalidPacketException( message="This packet is not a Remote File System response packet.") return RemoteFSResponsePacket(raw[4], XBee64BitAddress(raw[5:13]), raw[14:-1], raw[13])
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 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.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:`.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)
def create_packet(raw, operating_mode): """ Override method. Returns: :class:`.FSRequestPacket` Raises: InvalidPacketException: If the bytearray length is less than 7 + the minimum length of the command. (start delim. + length (2 bytes) + frame type + frame id + fs cmd id + checksum + cmd data = 7 bytes + cmd data). 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.FILE_SYSTEM_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=FSRequestPacket.__MIN_PACKET_LENGTH) if raw[3] != ApiFrameType.FILE_SYSTEM_REQUEST.code: raise InvalidPacketException( message="This packet is not a File System request packet.") return FSRequestPacket(raw[4], raw[5:-1])
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