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
23 changes: 18 additions & 5 deletions meshtastic/mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

logger = logging.getLogger(__name__)

WANT_CONFIG_RETRY_SECS = 3.0

def _timeago(delta_secs: int) -> str:
"""Convert a number of seconds in the past into a short, friendly string
e.g. "now", "30 sec ago", "1 hour ago"
Expand Down Expand Up @@ -1130,12 +1132,23 @@ def getRingtone(self):

def _waitConnected(self, timeout=30.0):
"""Block until the initial node db download is complete, or timeout
and raise an exception"""
and raise an exception

A radio that is restarting when we connect can drop our want_config, and
then never sends its config. While nothing of the config has arrived,
ask again every WANT_CONFIG_RETRY_SECS."""
if not self.noProto:
if not self.isConnected.wait(timeout): # timeout after x seconds
raise MeshInterface.MeshInterfaceError(
"Timed out waiting for connection completion"
)
deadline = time.monotonic() + timeout
while not self.isConnected.wait(
max(0.0, min(WANT_CONFIG_RETRY_SECS, deadline - time.monotonic()))
):
if time.monotonic() >= deadline:
raise MeshInterface.MeshInterfaceError(
"Timed out waiting for connection completion"
)
if self.myInfo is None:
logger.info("No reply to want_config, sending it again")
self._startConfig()

# If we failed while connecting, raise the connection to the client
if self.failure:
Expand Down
32 changes: 32 additions & 0 deletions meshtastic/tests/test_mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,38 @@ def test_waitConnected_isConnected_timeout(capsys):
assert out == ""


@pytest.mark.unit
def test_waitConnected_resends_want_config_until_the_radio_answers():
"""A want_config a restarting radio dropped is sent again"""
iface = MeshInterface()
sent = []

def answer_second_request():
sent.append(1)
if len(sent) == 2:
iface.myInfo = mesh_pb2.MyNodeInfo(my_node_num=1)
iface.isConnected.set()

with patch("meshtastic.mesh_interface.WANT_CONFIG_RETRY_SECS", 0.01), patch.object(
iface, "_startConfig", side_effect=answer_second_request
):
iface._waitConnected(5)
assert len(sent) == 2


@pytest.mark.unit
def test_waitConnected_does_not_restart_a_config_download_under_way():
"""Once the radio has started answering, a slow download is left alone"""
iface = MeshInterface()
iface.myInfo = mesh_pb2.MyNodeInfo(my_node_num=1)
with patch("meshtastic.mesh_interface.WANT_CONFIG_RETRY_SECS", 0.01), patch.object(
iface, "_startConfig"
) as start:
with pytest.raises(MeshInterface.MeshInterfaceError):
iface._waitConnected(0.05)
start.assert_not_called()


@pytest.mark.unit
def test_timeago():
"""Test that the _timeago function returns sane values"""
Expand Down
Loading