Skip to content

Commit

Permalink
DFReader.py: add bitmask data to verbose print output
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Apr 14, 2024
1 parent 2f6e31e commit a4a1d70
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions DFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<<i):
highest = i

# we show bit values at least up until the highest
# bit we have a description for:
for bit in bitmask.bit:
bit_offset = int(math.log(bit["value"], 2))
if bit_offset > 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)
Expand All @@ -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 = []
Expand Down

0 comments on commit a4a1d70

Please sign in to comment.