From 1bbcc452ae13de5401d1d50153f0480a828333ba Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Fri, 23 Aug 2024 15:34:25 -0700 Subject: [PATCH] precalculate bit-shifts and don't generate too-large random numbers for packet ID generation --- meshtastic/mesh_interface.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 6ee292e..fa2781a 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -803,9 +803,9 @@ class MeshInterface: # pylint: disable=R0902 ) else: nextPacketId = (self.currentPacketId + 1) & 0xFFFFFFFF - nextPacketId = nextPacketId & (0xFFFFFFFF >> 22) # mask upper 22 bits - randomPart = (random.randint(0, 0x7FFFFFFF) << 10) & 0xFFFFFFFF # generate number with 10 zeros at end - self.currentPacketId = nextPacketId | randomPart # combine + nextPacketId = nextPacketId & 0x3FF # == (0xFFFFFFFF >> 22), masks upper 22 bits + randomPart = (random.randint(0, 0x3FFFFF) << 10) & 0xFFFFFFFF # generate number with 10 zeros at end + self.currentPacketId = nextPacketId | randomPart # combine return self.currentPacketId def _disconnected(self):