From 53fe3d669ff2174d84dd524c9efeb1715271c65d Mon Sep 17 00:00:00 2001 From: Matt Webb Date: Mon, 22 Sep 2014 12:20:31 +0100 Subject: [PATCH] fixing some misunderstandings about endianness, and adding some test code to unwrap binary payloads in commands so I can see if things match up --- app/commands.py | 12 +-- app/image_encoding.py | 2 +- app/payload.py | 201 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+), 7 deletions(-) create mode 100644 app/payload.py diff --git a/app/commands.py b/app/commands.py index b2b53e7..fc3cbb0 100644 --- a/app/commands.py +++ b/app/commands.py @@ -67,10 +67,10 @@ def set_delivery_and_print_payload(file_id, png_fn): # - UC05 controls c, reserved byte >c, - # command_name >H (short), file_id >L (long), + # device type c, reserved byte c, + # command_name BBHLL", 1, 0, 1, 1, 0) + command_header = struct.pack("BL", 0, len(printer_control) + len(printer_data)) + header_region = struct.pack("LB", len(header_region) + len(encoded_image) + 1, 0) + payload = struct.pack("L", len(payload)) + payload + entire_payload = command_header + struct.pack("%ds" % len(x), str(x)) + output = struct.pack("<%ds" % len(x), str(x)) # return (number of pixels, output) return len(pixels), output diff --git a/app/payload.py b/app/payload.py new file mode 100644 index 0000000..951909c --- /dev/null +++ b/app/payload.py @@ -0,0 +1,201 @@ +import base64 +import struct + +class BERGCloudConst(object): + BC_EVENT_PRODUCT_ANNOUNCE = 0xA000 + BC_COMMAND_SET_BERGCLOUD_ID = 0xB000 + + BC_COMMAND_START_BINARY = 0xC000 + BC_COMMAND_START_PACKED = 0xC100 + BC_COMMAND_ID_MASK = 0x00FF + BC_COMMAND_FORMAT_MASK = 0xFF00 + + BC_COMMAND_DISPLAY_IMAGE = 0xD000 + BC_COMMAND_DISPLAY_TEXT = 0xD001 + + BC_EVENT_START_BINARY = 0xE000 + BC_EVENT_START_PACKED = 0xE100 + BC_EVENT_ID_MASK = 0x00FF + BC_EVENT_FORMAT_MASK = 0xFF00 + + BC_COMMAND_FIRMWARE_ARDUINO = 0xF010 + BC_COMMAND_FIRMWARE_MBED = 0xF020 + + +class DeviceCommandPayload(object): + COMMANDS = { + 'set_delivery_and_print': 0x0001, + 'set_delivery': 0x0002, + 'set_delivery_and_print_no_face': 0x0011, + 'set_delivery_no_face': 0x0012, + 'set_personality': 0x0102, + 'set_personality_with_message': 0x0101, + 'set_quip': 0x0202, + 'firmware_update': 0xf000 + } + + @classmethod + def from_base64(cls, base64_payload): + data = base64.b64decode(base64_payload) + + # encode_command_header and add_length_header_to_payload + # iare used for all Barringer commands, + # in lib/berg_cloud/barringer/commands.rb + offset = 0 + device_type, reserved_byte, command_name_id, file_id, reserved_long, payload_length \ + = struct.unpack_from(" encode_files have a + # similar format: a list of files, each as + # - length+1 (LLLLL", data, offset) + return_hash['name'] = "product_announce" + return_hash['product_id'] = "%08x%08x%08x%08x" % (id0, id1, id2, id3) + return_hash['product_version'] = version + + elif eventCode == DeviceEventPayload.EVENT_HEARTBEAT: + if eventPayloadLength != DeviceEventPayload.EVENT_HEARTBEAT_SIZE: + return_hash['error'] = 'Invalid heartbeat payload length of %d. Should be %d' % (eventPayloadLength, DeviceEventPayload.EVENT_HEARTBEAT_SIZE) + return return_hash + + uptime = struct.unpack_from("