diff --git a/DFReader.py b/DFReader.py index c9b7ff90e..23941a8f7 100644 --- a/DFReader.py +++ b/DFReader.py @@ -293,12 +293,101 @@ def __str__(self): ret = ret[:-2] return ret + '}' + def dump_verbose_bitmask(self, f, metadata, c, val): + try: + + if metadata is None: + return + + # only integers contain bitmasks, so use that to avoid + # looking up the field metadata + i = self.fmt.colhash[c] + t = self.fmt.msg_types[i] + if t != int: + return + + # see if we can find this field in the metadata: + field_metadata = None + for field in metadata["fields"].field: + if field.get("name") != c: + continue + field_metadata = field + if field_metadata is None: + raise ValueError("No field metadata for {c}") + + # work out how many bits to show: + t = field_metadata.get("type") + bit_count = None + if t == "uint8_t": + bit_count = 8 + elif t == "uint16_t": + bit_count = 16 + elif t == "uint32_t": + bit_count = 32 + + if bit_count is None: + return + + # we show bit values at least up to the highest bit set: + try: + bitmask = field_metadata["bitmask"] + except Exception: + return + + highest = -1 + + for i in range(bit_count): + if val & (1< highest: + highest = bit_offset + + for i in range(bit_offset): + bit_value = 1 << i + done = False + for bit in bitmask.bit: + if bit["value"] != bit_value: + continue + if val & bit_value: + bang = "" + else: + bang = "!" + bit_name = bit.get('name') + bit_desc = None + try: + bit_desc = bit["description"] + except KeyError: + pass + if bit_desc is None: + f.write(f" {bang}{bit_name}\n") + else: + f.write(f" {bang}{bit_name} ({bit_desc})\n") + done = True + break + if not done: + f.write(f" {bang}UNKNOWN_BIT{i}\n") + except Exception as e: + # print(e) + pass + def dump_verbose(self, f): is_py3 = sys.version_info >= (3,0) timestamp = "%s.%03u" % ( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self._timestamp)), int(self._timestamp*1000.0)%1000) f.write("%s: %s\n" % (timestamp, self.fmt.name)) + metadata = None + try: + metadata_tree = self._parent.metadata.metadata_tree() + metadata = metadata_tree[self.fmt.name] + except Exception: + pass + for c in self.fmt.columns: # Get the value val = self.__getattr__(c) @@ -325,6 +414,9 @@ def dump_verbose(self, f): # Append the unit f.write(" %s\n" % (unit)) + # if this is a bitmask then print out all bits set: + self.dump_verbose_bitmask(f, metadata, c, val) + def get_msgbuf(self): '''create a binary message buffer for a message''' values = []