From cb9b60b0300aa11321a2f514d1be240f6a3fb24c Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Fri, 28 Jun 2024 17:01:45 -0700 Subject: [PATCH] update check_connection() per nRF24/RF24Mesh#250 --- circuitpython_nrf24l01/rf24_mesh.py | 18 +++++++++++++----- docs/network_docs/mesh_api.rst | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/circuitpython_nrf24l01/rf24_mesh.py b/circuitpython_nrf24l01/rf24_mesh.py index cb92b9f..53e8c00 100644 --- a/circuitpython_nrf24l01/rf24_mesh.py +++ b/circuitpython_nrf24l01/rf24_mesh.py @@ -40,6 +40,7 @@ NETWORK_DEFAULT_ADDR, NETWORK_MULTICAST_ADDR, NETWORK_POLL, + NETWORK_PING, MESH_ADDR_RELEASE, MESH_ADDR_LOOKUP, MESH_ID_LOOKUP, @@ -162,13 +163,20 @@ def _lookup_2_master(self, number: int, lookup_type: int) -> int: return struct.unpack(" bool: + def check_connection(self, attempts: int = 3, ping_master: bool = False) -> bool: """Check for network connectivity (not for use on master node).""" + if not self._id: + return True + if self._addr == NETWORK_DEFAULT_ADDR: + return False for _ in range(attempts): - result = self.lookup_address(self._id) - if result in (-2, 0): - return False - if result == self.node_address: + if ping_master: + result = self.lookup_address(self._id) + if result == -2: + return False + if result == self._addr: + return True + elif self.write(self._parent, NETWORK_PING, b""): return True return False diff --git a/docs/network_docs/mesh_api.rst b/docs/network_docs/mesh_api.rst index 6005962..f19a66e 100644 --- a/docs/network_docs/mesh_api.rst +++ b/docs/network_docs/mesh_api.rst @@ -177,6 +177,31 @@ Advanced API .. automethod:: circuitpython_nrf24l01.rf24_mesh.RF24Mesh.check_connection + :param attempts: The number of attempts to test for active connection to the mesh network. + :param ping_master: If this parameter is set to `True`, then this function will verify the + connectivity by using `lookup_address()` to transact with the master node. Setting this + parameter to `False` will simply ping the node's parent. + + .. warning:: + Setting this parameter to `True` can result in performance cost when used in + a large mesh network. The disadvantages in such a situation are: + + - increased load on master node + - increased network congestion + - unreliable connectivity information when a parent or grandparent of the current + node briefly loses connection. + + :returns: + - `True` if connected to the mesh network (or current node is the master node). + - `False` if not connected to the mesh network or mesh network is unresponsive. + + .. versionchanged:: 2.2.0 + Added ``attempts`` and ``ping_master`` parameters; changed return value for master nodes + + Previously, this function would return `False` when called from a master node. + This was changed to return `True` to help avoid erroneous user code calling + `renew_address()` on a master node. + .. automethod:: circuitpython_nrf24l01.rf24_mesh.RF24Mesh.release_address :param address: The address to release.