Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/meshcore/commands/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,22 @@ async def change_contact_path(self, contact, path, path_hash_mode=None) -> Event
async def change_contact_flags(self, contact, flags) -> Event:
return await self.update_contact(contact, flags=flags)

async def set_autoadd_config(self, flag : int) -> Event:
async def set_autoadd_config(self, flag : int, max_hops: Optional[int] = None) -> Event:
"""Set which adverts the device adds as contacts by itself.

Args:
flag: The auto-add flags.
max_hops: The most hops an advert may have come to be added. The
firmware caps it at 64. Left as it is when None.
"""
data = b"\x3A" + flag.to_bytes(1, "little", signed=False)
if max_hops is not None:
data += max_hops.to_bytes(1, "little", signed=False)
return await self.send(data, [EventType.OK, EventType.ERROR])

async def get_autoadd_config(self) -> Event:
"""Read the auto-add flags ("config") and, from companion v1.14.0,
the hop limit ("max_hops")."""
data = b"\x3B"
return await self.send(data, [EventType.AUTOADD_CONFIG, EventType.ERROR])

Expand Down
20 changes: 14 additions & 6 deletions src/meshcore/commands/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,23 @@ async def sign(self, data: bytes, chunk_size: int = 120, timeout: Optional[float

return await self.sign_finish(timeout=timeout, data_size=len(data))

async def has_connection(self) -> Event:
"""N09: Check if the device has an active connection (CMD 28).
async def has_connection(self, dst: DestinationType) -> Event:
"""N09: Check whether the device holds a connection to *dst* (CMD 28).

A connection is the keep-alive the device keeps with a server it has
logged in to (a room server, for instance). The firmware handles the
command only with the server's 32-byte public key after it.

Args:
dst: The server, as a contact, a public key or its hex.

Returns:
Event with a 1-byte response indicating connection status,
or ERROR.
OK if the device holds a connection to *dst*, ERROR
(ERR_CODE_NOT_FOUND) if not.
"""
logger.debug("Checking device connection status")
return await self.send(b"\x1c", [EventType.OK, EventType.ERROR])
dst_bytes = _validate_destination(dst, prefix_length=32)
logger.debug(f"Checking connection to {dst_bytes.hex()}")
return await self.send(b"\x1c" + dst_bytes, [EventType.OK, EventType.ERROR])

async def get_tuning(self) -> Event:
"""N03/N09: Request current tuning parameters (CMD_GET_TUNING_PARAMS = 43).
Expand Down
57 changes: 54 additions & 3 deletions tests/unit/test_protocol_surface_gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def mock_send(data, expected_events, timeout=None):

@pytest.mark.asyncio
async def test_has_connection_wrapper():
"""has_connection sends CMD 0x1c."""
"""has_connection sends CMD 0x1c and the server's 32-byte key (#119: B1)."""
from meshcore.commands.device import DeviceCommands

cmd = DeviceCommands.__new__(DeviceCommands)
Expand All @@ -229,10 +229,61 @@ async def mock_send(data, expected_events, timeout=None):

cmd.send = mock_send

await cmd.has_connection()
key = bytes(range(32))
await cmd.has_connection(key.hex())

assert captured_data is not None
assert captured_data == b"\x1c"
assert captured_data == b"\x1c" + key


@pytest.mark.asyncio
async def test_has_connection_takes_a_contact():
from meshcore.commands.device import DeviceCommands

cmd = DeviceCommands.__new__(DeviceCommands)
sent = []

async def mock_send(data, expected_events, timeout=None):
sent.append(bytes(data))
return Event(EventType.ERROR, {"error_code": 2})

cmd.send = mock_send
key = bytes(range(32))

result = await cmd.has_connection({"public_key": key.hex(), "adv_name": "Room"})

assert sent == [b"\x1c" + key]
assert result.type == EventType.ERROR


# ---------------------------------------------------------------------------
# Command wrapper: set_autoadd_config's hop limit (#119: B2)
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
@pytest.mark.parametrize(
"kwargs, frame",
[
({}, b"\x3a\x0e"),
({"max_hops": 5}, b"\x3a\x0e\x05"),
({"max_hops": 0}, b"\x3a\x0e\x00"),
],
)
async def test_set_autoadd_config_hop_limit(kwargs, frame):
from meshcore.commands.contact import ContactCommands

cmd = ContactCommands.__new__(ContactCommands)
sent = []

async def mock_send(data, expected_events, timeout=None):
sent.append(bytes(data))
return Event(EventType.OK, {})

cmd.send = mock_send

await cmd.set_autoadd_config(0x0E, **kwargs)

assert sent == [frame]


# ---------------------------------------------------------------------------
Expand Down