Skip to content

Commit

Permalink
Fix RegBitField decode/encode (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
andywongarista authored Nov 24, 2021
1 parent ef4f2c6 commit cf2ebe9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sonic_platform_base/sonic_xcvr/fields/xcvr_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ def read_before_write(self):
return True

def decode(self, raw_data, **decoded_deps):
return bool((raw_data[0] >> self.bitpos) & 1)
return bool((raw_data[0] >> self.bitpos % 8) & 1)

def encode(self, val, raw_state=None):
assert not self.ro and raw_state is not None
curr_state = raw_state[0]
if val:
curr_state |= (1 << self.bitpos)
curr_state |= (1 << self.bitpos % 8)
else:
curr_state &= ~(1 << self.bitpos)
curr_state &= ~(1 << self.bitpos % 8)
return bytearray([curr_state])


Expand Down Expand Up @@ -249,7 +249,7 @@ def decode(self, raw_data, **decoded_deps):
for field in self.fields:
offset = field.get_offset()
if not field.get_deps():
result[field.name] = field.decode(raw_data[offset - start: offset + field.get_size() - start],
result[field.name] = field.decode(raw_data[offset - start: offset + field.get_size() - start],
**decoded_deps)

# Now decode any fields that have dependant fields in the same RegGroupField scope
Expand Down
8 changes: 8 additions & 0 deletions tests/sonic_xcvr/test_xcvr_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def __init__(self, codes):
self.NUM_REG = NumberRegField("NumReg", 100, format=">Q", size=8, ro=False)
self.SCALE_NUM_REG = NumberRegField("ScaleNumReg", 120, format=">i", size=4, scale=100, ro=False)
self.FIXED_NUM_REG = FixedNumberRegField("FixedNumReg", 130, 8, format=">f", size=4, ro=False)
self.NUM_REG_WITH_BIT = NumberRegField("NumRegWithBit", 140,
RegBitField("NumRegBit", bitpos=20, ro=False),
format="<I", size=4, ro=False
)
self.STRING_REG = StringRegField("StringReg", 12, size=15)
self.HEX_REG = HexRegField("HexReg", 30, size=3)
self.REG_GROUP = RegGroupField("RegGroup",
Expand Down Expand Up @@ -168,6 +172,10 @@ def test_encode_decode(self):
assert field.decode(field.encode(True, bytearray(b'\x00')))
assert not field.decode(field.encode(False, bytearray(b'\x00')))

field = mem_map.get_field("NumRegBit")
assert field.decode(field.encode(True, bytearray(b'\x00')))
assert not field.decode(field.encode(False, bytearray(b'\xFF')))

class TestRegField(object):
def test_offset(self):
field = mem_map.get_field("Field0")
Expand Down

0 comments on commit cf2ebe9

Please sign in to comment.