Skip to content

Commit

Permalink
Merge pull request #560 from geeksville/pr-fixchannel
Browse files Browse the repository at this point in the history
(high-pri fix) Update python client to use the 'modern' meshtastic protocol init flow
  • Loading branch information
ianmcorvidae authored Apr 29, 2024
2 parents 2f9307f + 3886bc1 commit e5999f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
16 changes: 14 additions & 2 deletions meshtastic/mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self, debugOut=None, noProto: bool=False) -> None:
self.mask: Optional[int] = None # used in gpio read and gpio watch
self.queueStatus: Optional[mesh_pb2.QueueStatus] = None
self.queue: collections.OrderedDict = collections.OrderedDict()
self._localChannels = None

def close(self):
"""Shutdown this interface"""
Expand Down Expand Up @@ -705,6 +706,7 @@ def _startConfig(self):
self.myInfo = None
self.nodes = {} # nodes keyed by ID
self.nodesByNum = {} # nodes keyed by nodenum
self._localChannels = [] # empty until we start getting channels pushed from the device (during config)

startConfig = mesh_pb2.ToRadio()
self.configId = random.randint(0, 0xFFFFFFFF)
Expand Down Expand Up @@ -786,7 +788,12 @@ def _handleConfigComplete(self) -> None:
Done with initial config messages, now send regular MeshPackets
to ask for settings and channels
"""
self.localNode.requestChannels()
# This is no longer necessary because the current protocol statemachine has already proactively sent us the locally visible channels
# self.localNode.requestChannels()
self.localNode.setChannels(self._localChannels)

# the following should only be called after we have settings and channels
self._connected() # Tell everyone else we are ready to go

def _handleQueueStatusFromRadio(self, queueStatus) -> None:
self.queueStatus = queueStatus
Expand Down Expand Up @@ -859,7 +866,8 @@ def _handleFromRadio(self, fromRadioBytes):
# stream API fromRadio.config_complete_id
logging.debug(f"Config complete ID {self.configId}")
self._handleConfigComplete()

elif fromRadio.HasField("channel"):
self._handleChannel(fromRadio.channel)
elif fromRadio.HasField("packet"):
self._handlePacketFromRadio(fromRadio.packet)

Expand Down Expand Up @@ -986,6 +994,10 @@ def _getOrCreateByNum(self, nodeNum):
self.nodesByNum[nodeNum] = n
return n

def _handleChannel(self, channel):
"""During initial config the local node will proactively send all N (8) channels it knows"""
self._localChannels.append(channel)

def _handlePacketFromRadio(self, meshPacket, hack=False):
"""Handle a MeshPacket that just arrived from the radio
Expand Down
10 changes: 6 additions & 4 deletions meshtastic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def showInfo(self):
print(f"Module preferences: {prefs}\n")
self.showChannels()

def setChannels(self, channels):
"""Set the channels for this node"""
self.channels = channels
self._fixupChannels()

def requestChannels(self):
"""Send regular MeshPackets to ask channels."""
logging.debug(f"requestChannels for nodeNum:{self.nodeNum}")
Expand Down Expand Up @@ -654,7 +659,7 @@ def _fixupChannels(self):
"""Fixup indexes and add disabled channels as needed"""

# Add extra disabled channels as needed
# TODO: These 2 lines seem to not do anything.
# This is needed because the protobufs will have index **missing** if the channel number is zero
for index, ch in enumerate(self.channels):
ch.index = index # fixup indexes

Expand Down Expand Up @@ -726,9 +731,6 @@ def onResponseRequestChannel(self, p):

self.channels = self.partialChannels
self._fixupChannels()

# FIXME, the following should only be called after we have settings and channels
self.iface._connected() # Tell everyone else we are ready to go
else:
self._requestChannel(index + 1)

Expand Down

0 comments on commit e5999f5

Please sign in to comment.