MCPcopy Create free account
hub / github.com/AstroPrint/AstroBox / decode_packet

Function decode_packet

src/ext/makerbot_driver/Encoder/Packet.py:24–46  ·  view source on GitHub ↗

Decode a packet from a payload.Non-streaming packet decoder. Accepts a byte array containing a single packet, and attempts to parse the packet and return the payload. @param packet byte array containing the input packet @return payload of the packet

(packet)

Source from the content-addressed store, hash-verified

22
23
24def decode_packet(packet):
25 """
26 Decode a packet from a payload.Non-streaming packet decoder.
27 Accepts a byte array containing a single packet, and attempts
28 to parse the packet and return the payload.
29 @param packet byte array containing the input packet
30 @return payload of the packet
31 """
32 assert type(packet) is bytearray
33
34 if len(packet) < 4:
35 raise makerbot_driver.errors.PacketLengthError(len(packet), 4)
36
37 if packet[0] != makerbot_driver.constants.header:
38 raise makerbot_driver.errors.PacketHeaderError(packet[0], makerbot_driver.constants.header)
39
40 if packet[1] != len(packet) - 3:
41 raise makerbot_driver.errors.PacketLengthFieldError(packet[1], len(packet) - 3)
42
43 if packet[len(packet) - 1] != makerbot_driver.Encoder.CalculateCRC(packet[2:(len(packet) - 1)]):
44 raise makerbot_driver.errors.PacketCRCError(packet[len(packet) - 1], makerbot_driver.Encoder.CalculateCRC(packet[2:(len(packet) - 1)]))
45
46 return packet[2:(len(packet) - 1)]
47
48
49def check_response_code(response_code):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected