Esempio n. 1
0
def format_eeprom_contents(tag_name, data_len, payload):
    if data_len < 3:
        return "Data too short"
    address = 256 * payload[0] + payload[1]
    r = "address=0x%04x " % address
    if address in eeprom.eeprom_address_to_key:
        setting = eeprom.eeprom_address_to_key[address]
        r += "(%s) " % setting
        fmt = eeprom.eeprom[setting]["format"]
        order, quantity, elem_type = eeprom.parse_unpack_format(fmt)
        data = struct.unpack(fmt, payload[2:])
        if len(data) == 1:
            data = data[0]
            if elem_type == "s":
                data = data.rstrip("\x00")
        r += repr(data)
    else:
        r += "0x  " + " ".join(map(byte_hex, payload[2:]))
    return r
Esempio n. 2
0
def format_eeprom_contents_inner(tag_name, address, eeprom_data, epoch):

    r = 'address=0x%04x ' % address
    eeprom_data_len = len(eeprom_data)
    # Try to format the results appropriately if possible.
    if address in eeprom.eeprom_address_to_setting_name:
        # The start address matches a known address in EEPROM
        setting = eeprom.eeprom_address_to_setting_name[address]
        setting_info = eeprom.eeprom[setting]
        # Check that the size of the EEPROM data received matches the expected length for the setting
        if 'size' in setting_info:
            sz = setting_info['size']
            if eeprom_data_len > sz:
                # Multiple settings received. Split into two parts.
                r1 = format_eeprom_contents_inner(tag_name, address, eeprom_data[0:sz], epoch)
                r2 = format_eeprom_contents_inner(tag_name, address + sz, eeprom_data[sz:], epoch)
                r = []
                if isinstance(r1, six.string_types):
                    r.append(r1) # Should expect in this case to always get a string
                else:
                    r.extend(r1)  # Assume a list
                if isinstance(r2, six.string_types):
                    r.append(r2)
                else:
                    r.extend(r2)
                return r

            elif eeprom_data_len < sz:
                # Not the entire setting so cannot format correctly
                setting = setting_info = None
    else:
        # Unknown start address
        # TODO: check for the next start address
        next_address = eeprom.find_next_address(address)
        if next_address is not None: #  and next_address in eeprom.eeprom_address_to_setting_name:
            skip_bytes = next_address - address
            next_setting_name = eeprom.eeprom_address_to_setting_name[next_address]
            if 'size' in eeprom.eeprom[next_setting_name] and eeprom.eeprom[next_setting_name]['size'] <= eeprom_data_len - skip_bytes:
                r1 = format_eeprom_contents_inner(tag_name, address, eeprom_data[0:skip_bytes], epoch)
                r2 = format_eeprom_contents_inner(tag_name, next_address, eeprom_data[skip_bytes:], epoch)
                r = []
                if isinstance(r1, six.string_types):
                    r.append(r1) # Should expect in this case to always get a string
                else:
                    r.extend(r1)  # Assume a list
                if isinstance(r2, six.string_types):
                    r.append(r2)
                else:
                    r.extend(r2)
                return r

        setting = setting_info = None

    if setting:
        # Correct data for this setting
        r += '(%s) ' % setting
        order, quantity, elem_type = eeprom.parse_unpack_format(setting_info['format'])
        data = struct.unpack(setting_info['format'], eeprom_data)
        if len(data) == 1:
            data = data[0]
            if elem_type == 's':
                data = data.rstrip('\x00')
        r += repr(data)
    else:
        r += '(unknown %d bytes)  0x  %s' % (eeprom_data_len, ' '.join(map(byte_hex, eeprom_data)))
    return r
Esempio n. 3
0
for k in aw_cmds:
    s = getattr(args, k)
    if s is None:
        continue

    cmd = { }
    # If the argparse type conversion was employed then convert
    # back to a string
    s = str(s)

    m = re.match('^eeprom_(.*)', k)
    if m:
        cmd['name'] = 'write_eeprom'
        eeprom_setting = m.group(1)
        # Parse struct format string into order, quantity, type
        pfmt = eeprom.parse_unpack_format( \
            eeprom.eeprom[eeprom_setting]['format'])
        if pfmt[2] in ('c', 's', 'p'):
            # String data
            data = s
        else:
            # Convert into numeric data
            data = awn.safe_eval(s)

        # Pack data into suitable bytearrays matching the EEPROM layout
        if pfmt[1] > 1 and pfmt[2] not in ('s', 'p'):
            # Multiple values required
            eeprom_data = struct.pack(eeprom.eeprom[eeprom_setting]['format'],
                                      *data)
        else:
            eeprom_data = struct.pack(eeprom.eeprom[eeprom_setting]['format'],
                                           data)