Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions meshtastic/remote_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ def onGPIOreceive(packet, interface) -> None:
# so, we set it here
gpioValue = 0

# print(f'mask:{interface.mask}')
value = int(gpioValue) & int(interface.mask)
# Keep read replies consistent with the hexadecimal mask/value notation used
# when the CLI writes GPIOs.
mask = int(interface.mask)
value = int(gpioValue) & mask
print(
f'Received RemoteHardware type={hw["type"]}, gpio_value={gpioValue} value={value}'
f'Received Remote_Hardware type={hw["type"]}, '
f"mask=0x{mask:x} value=0x{value:x}"
)
interface.gotResponse = True

Expand Down
29 changes: 25 additions & 4 deletions meshtastic/tests/test_remote_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,35 @@ def test_RemoteHardwareClient():


@pytest.mark.unit
def test_onGPIOreceive(capsys):
"""Test onGPIOreceive"""
@pytest.mark.parametrize(
("gpio_value", "mask", "expected_value"),
[
("8192", 0x2000, "0x2000"),
("12288", 0x2000, "0x2000"),
(None, 0x10, "0x0"),
],
)
def test_onGPIOreceive_formats_masked_values_as_hex(
capsys: pytest.CaptureFixture[str],
gpio_value: str | None,
mask: int,
expected_value: str,
) -> None:
"""GPIO replies use the same hexadecimal notation as write requests."""
iface = MagicMock(autospec=SerialInterface)
packet = {"decoded": {"remotehw": {"type": "foo", "gpioValue": "4096"}}}
iface.mask = mask
remotehw = {"type": "READ_GPIOS_REPLY"}
if gpio_value is not None:
remotehw["gpioValue"] = gpio_value
packet = {"decoded": {"remotehw": remotehw}}
onGPIOreceive(packet, iface)
out, err = capsys.readouterr()
assert re.search(r"Received RemoteHardware", out)
assert out == (
"Received Remote_Hardware type=READ_GPIOS_REPLY, "
f"mask=0x{mask:x} value={expected_value}\n"
)
assert err == ""
assert iface.gotResponse is True


@pytest.mark.unit
Expand Down