def pack_header(header_data): """ Attempt to pack the data from the header_data dictionary into binary format according to Waggle protocol. :param dictionary header_data: The header data to be serialized :rtype: string :raises KeyError: An exception will be raised if the provided dictionary does not contain required information """ header = str() try: header += _pack_version(header_data["prot_ver"]) # Serialize protocol version header += _pack_flags(header_data["flags"]) # Packet flags header += _bin_pack(header_data["len_body"],HEADER_BYTELENGTHS["len_body"]) # Length of message body header += _bin_pack(header_data["time"],HEADER_BYTELENGTHS["time"]) # Timestamp header += _bin_pack(header_data["msg_mj_type"], HEADER_BYTELENGTHS["msg_mj_type"]) # Message Major Type header += _bin_pack(header_data["msg_mi_type"], HEADER_BYTELENGTHS["msg_mi_type"]) # Message Minor Type header += _bin_pack(header_data["ext_header"], HEADER_BYTELENGTHS["ext_header"]) # Optional extended header header += _bin_pack(header_data["s_uniqid"],HEADER_BYTELENGTHS["s_uniqid"]) # Sender unique ID header += _bin_pack(header_data["r_uniqid"],HEADER_BYTELENGTHS["r_uniqid"]) # Recipient unique ID header += _bin_pack(header_data["snd_session"],HEADER_BYTELENGTHS["snd_session"]) # Send session number header += _bin_pack(header_data["resp_session"],HEADER_BYTELENGTHS["resp_session"]) # Response session number header += _bin_pack(header_data["snd_seq"],HEADER_BYTELENGTHS["snd_seq"]) # Send sequence number header += _bin_pack(header_data["resp_seq"],HEADER_BYTELENGTHS["resp_seq"]) # Response sequence number except KeyError as e: raise KeyError("Header packing failed. The required dictionary entry %s was missing!" % str(e)) #Compute the header CRC and stick it on the end crc16 = mkCrcFun('crc-16') header += _bin_pack(crc16(header),2) return header
def main() -> None: """Main function to run everything.""" functions: Dict[str, Any] = { 'mod_crc': predefinedcrc.mkCrcFun('crc-32c'), 'sha1': hashlib.sha1, 'md5': hashlib.md5, 'blake2b': hashlib.blake2b, 'blake2s': hashlib.blake2s } low: int = 32 high: int = 1024 increment: int = 2 iterations: List[int] = [1000, 5000, 10000, 15000] data: Dict[int, bytes] = gen_data(low=low, high=high,\ increment=increment, additionals=[20]) # print actual size of data objects in the memory print("Actual sizes of generated chunks ...") for each in data.values(): print(f"{len(each)} bytes: {sys.getsizeof(each)}") # measure timings for iteration in iterations: timings_total, timings_avg = calculate_timings(data,\ functions, iterations=iteration) print_timings(timings_total, timings_avg)
def flash_firmware(device: Device, path: str = "firmware/5110") -> None: firmware_file = open(join(path, f"{TARGET_FIRMWARE}.bin"), "rb") firmware = firmware_file.read() firmware_file.close() length = len(firmware) for i in range(0, length, 1008): device.write_firmware(i, firmware[i:i + 1008]) device.check_firmware(0, len(firmware), mkCrcFun("crc-32-mpeg")(firmware)) device.reset(False, True) device.wait_disconnect() print("Flash firmware: OK")
def unpack(packet): """ Turns a packet object into a tuple containing the header data and message body :param string packet: The packet data to be unpacked :rtype: tuple(dictionary, string) :raises IOError: An IOError will be raised if a CRC fails in the packet :raises KeyError: An IndexError will be raised if a packet header is the wrong length """ crc32fun = mkCrcFun('crc-32') if(crc32fun(packet[HEADER_LENGTH:-FOOTER_LENGTH]) != _bin_unpack(packet[-FOOTER_LENGTH:])): raise IOError("Packet body CRC-32 failed.") try: header = _unpack_header(packet[:HEADER_LENGTH]) except Exception as e: raise return (header, packet[HEADER_LENGTH:-FOOTER_LENGTH])
def _unpack_header(packed_header): """ Given a packed header, this method will return a dictionary with the unpacked contents. :param string packed_header: A string representing a waggle header :rtype: Dictionary :raises IndexError: An IndexError will be raised if the packed header is not 40 bytes long :raises IOError: An IOError will be raised if the packet header fails its CRC-16 check """ # Check header length if len(packed_header) != HEADER_LENGTH: raise IndexError("Tried to unpack a waggle header that was not 40 bytes long.") header_IO = StringIO.StringIO(packed_header) #Check the CRC CRC16 = mkCrcFun('CRC-16') header_IO.seek(HEADER_LOCATIONS["crc-16"]) headerCRC = header_IO.read(2) if(CRC16(packed_header[:-2]) != _bin_unpack(headerCRC)): raise IOError("Header CRC-16 check failed") header_IO.seek(0) # The header passed the CRC check. Hooray! Now return a dictionary containing the info. header = { "prot_ver" : _unpack_version(header_IO.read(HEADER_BYTELENGTHS["prot_ver"])), # Load protocol version "flags" : _unpack_flags(header_IO.read(HEADER_BYTELENGTHS["flags"])), # Load flags "len_body" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["len_body"])), # Load message body length "time" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["time"])), # Load time "msg_mj_type" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["msg_mj_type"])), # Load message major type "msg_mi_type" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["msg_mi_type"])), # Load message minor type "ext_header" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["ext_header"])), # Load extended header "s_uniqid" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["s_uniqid"])), # Load sender unique ID "r_uniqid" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["r_uniqid"])), # Load recipient unique ID "snd_session" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["snd_session"])), # Load send session number "resp_session" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["resp_session"])), # Load recipient session number "snd_seq" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["snd_seq"])), # Load send sequence number "resp_seq" : _bin_unpack(header_IO.read(HEADER_BYTELENGTHS["resp_seq"])) # Load recieve sequence number } header_IO.close() return header
import sys from fds_const import commands as FMCC from crcmod.predefined import mkCrcFun crc = mkCrcFun("crc-16") def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be "yes" (the default), "no" or None (meaning an answer is required of the user). The "answer" return value is True for "yes" or False for "no". """ valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} if default is None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " elif default == "no": prompt = " [y/N] " else: raise ValueError("invalid default answer: '%s'" % default) while True: sys.stdout.write(question + prompt) choice = input().lower() if default is not None and choice == "":
HEADER_LENGTH = 40 FOOTER_LENGTH = 4 MAX_SEQ_NUMBER = pow(2,8*HEADER_BYTELENGTHS["snd_seq"]) MAX_PACKET_SIZE = 1024 VERSION = "0.4" #Sequence becomes zero when the node starts again or when the package is #reimported SEQUENCE = 0 #The /etc/waggle folder has waggle specific information S_UNIQUEID_HEX=None #Create the CRC functions crc32fun = mkCrcFun('crc-32') crc16fun = mkCrcFun('crc-16') crc16_position = HEADER_LOCATIONS['crc-16'] def nodeid_hexstr2int(node_id_hex): if type(node_id_hex) is str: node_id_hex = node_id_hex.encode('iso-8859-1') #return int.from_bytes(node_id_hex, byteorder='big') return int(node_id_hex, 16) if os.path.isfile('/etc/waggle/node_id'): with open('/etc/waggle/node_id','r') as file_: S_UNIQUEID_HEX = file_.read().rstrip('\n') if len(S_UNIQUEID_HEX) != 2*HEADER_BYTELENGTHS["s_uniqid"]:
HEADER_LENGTH = 40 FOOTER_LENGTH = 4 MAX_SEQ_NUMBER = pow(2, 8 * HEADER_BYTELENGTHS["snd_seq"]) MAX_PACKET_SIZE = 1024 VERSION = "0.4" #Sequence becomes zero when the node starts again or when the package is #reimported SEQUENCE = 0 #The /etc/waggle folder has waggle specific information S_UNIQUEID_HEX = None #Create the CRC functions crc32fun = mkCrcFun('crc-32') crc16fun = mkCrcFun('crc-16') crc16_position = HEADER_LOCATIONS['crc-16'] def nodeid_hexstr2int(node_id_hex): if type(node_id_hex) is str: node_id_hex = node_id_hex.encode('iso-8859-1') #return int.from_bytes(node_id_hex, byteorder='big') return int(node_id_hex, 16) if os.path.isfile('/etc/waggle/node_id'): with open('/etc/waggle/node_id', 'r') as file_: S_UNIQUEID_HEX = file_.read().rstrip('\n')
#!/usr/bin/env python from __future__ import print_function from bottle import request, response, get, post, run, template from time import time from datetime import datetime import struct from os import environ from sys import argv #from crc16 import crc16xmodem from crcmod.predefined import mkCrcFun crc16xmodem = mkCrcFun('xmodem') # user details GENDERS = {'f': 0, 'm': 2} name = environ.get('HEL_USER', 'EXAMPLE') min_tolerance = int(environ.get('HEL_MIN_TOLERANCE', 89000)) # grams max_tolerance = int(environ.get('HEL_MAX_TOLERANCE', 97000)) # grams age = datetime.now().year - int(environ.get('HEL_BIRTHYEAR', 1970)) # birthyear gender = GENDERS.get(environ.get('HEL_GENDER', '')[:1].lower(), 0x34) height = int(environ.get('HEL_HEIGHT', 1900)) # millimetres name = name[:20].upper().ljust(20) age = 1 if age < 1 else age log_buffer = [] def log(f, *args):
""" import logging import struct from typing import Optional, Any from crcmod import predefined # type: ignore import attr from ventserver.protocols import exceptions from ventserver.sansio import channels from ventserver.sansio import protocols CRC_FUNC = predefined.mkCrcFun('crc-32c') def quad_byte_attr( _: Any, __: 'attr.Attribute[bytes]', value: bytes ) -> None: """Validate the attr input as a byte re. Raises: ValueError: attr init value cannot be represented as a single byte. """ if len(bytes(value)) != 4: raise ValueError( 'Attr must be a bytes representation of unsigned long: {!r}' .format(value) )
def pack(header_data, message_data=""): """ Takes header and message information and yields packets representing that data. :param dictionary header_data: A dictionary containing the header data :param string/FileObject message_data: The data to be packed into a Packet :yields: string :raises KeyError: A KeyError will be raised if the header_data dictionary is not properly formatted """ global SEQUENCE global UNIQUEID global VERSION #Generate the automatic fields auto_header = { "prot_ver" : VERSION, "flags" : (1,1,True), "len_body" : len(message_data), "time" : int(time.time()), "snd_session" : 0, "s_uniqid" : UNIQUEID, "ext_header" : 0, "resp_session" : 0, "r_uniqid" : 0, "snd_seq" : SEQUENCE, "resp_seq" : 0, } #and update them with user-supplied values auto_header.update(header_data) #Create the CRC function crc32fun = mkCrcFun('crc-32') #If it's a string, make it a file object if(type(message_data) is str): message_data = StringIO.StringIO(message_data) #If it's under 1K, send it off as a single packet #Jump to the end of the file message_data.seek(0,2) #See if it is less than 1K if(message_data.tell() < MAX_PACKET_SIZE): try: header = pack_header(auto_header) except KeyError as e: raise #Save the short message to a string message_data.seek(0) msg = message_data.read() message_data.close() #Calculate the CRC, pack it all up, and return the result. SEQUENCE = (SEQUENCE + 1) % MAX_SEQ_NUMBER msg_crc32 = _bin_pack(crc32fun(msg),FOOTER_LENGTH) yield header + msg + msg_crc32 #Multi-packet else: length = message_data.tell() message_data.seek(0) packetNum = 0 # Create smaller packets MAX_PACKET_SIZE bytes at a time, also attach packet number while length > MAX_PACKET_SIZE: try: header = pack_header(auto_header) except KeyError as e: raise msg = _bin_pack(packetNum,4) + message_data.read(MAX_PACKET_SIZE) SEQUENCE = (SEQUENCE + 1) % MAX_SEQ_NUMBER packetNum += 1 msg_crc32 = _bin_pack(crc32fun(msg),FOOTER_LENGTH) yield header + msg + msg_crc32 length -= MAX_PACKET_SIZE # Finish sending the message if length > 0: header = pack_header(auto_header) msg = _bin_pack(packetNum,4) + message_data.read(MAX_PACKET_SIZE) SEQUENCE = (SEQUENCE + 1) % MAX_SEQ_NUMBER msg_crc32 = _bin_pack(crc32fun(msg),FOOTER_LENGTH) yield header + msg + msg_crc32
#!/usr/bin/env python from __future__ import print_function from bottle import request, response, get, post, run, template from time import time from datetime import datetime import struct from os import environ from sys import argv #from crc16 import crc16xmodem from crcmod.predefined import mkCrcFun crc16xmodem = mkCrcFun('xmodem') # user details GENDERS = {'f': 0, 'm': 2} name = environ.get('HEL_USER', 'EXAMPLE') min_tolerance = int(environ.get('HEL_MIN_TOLERANCE', 89000)) # grams max_tolerance = int(environ.get('HEL_MAX_TOLERANCE', 97000)) # grams age = datetime.now().year - int(environ.get('HEL_BIRTHYEAR', 1970)) # birthyear gender = GENDERS.get(environ.get('HEL_GENDER', '')[:1].lower(), 0x34) height = int(environ.get('HEL_HEIGHT', 1900)) # millimetres name = name[:20].upper().ljust(20) age = 1 if age < 1 else age log_buffer = [] def log(f, *args): global log_buffer now = datetime.now().isoformat() o = f % args