From 74c911cb75696331d51998f9c0a697e776e724ad Mon Sep 17 00:00:00 2001 From: digitaldisarray Date: Fri, 6 Dec 2024 17:54:08 -0800 Subject: [PATCH 1/3] Add text message port cli option --- meshtastic/__main__.py | 13 +++++++++++-- meshtastic/mesh_interface.py | 7 ++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index f5390d5..a26c497 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -442,7 +442,7 @@ def onConnected(interface): channelIndex = mt_config.channel_index or 0 if checkChannel(interface, channelIndex): print( - f"Sending text message {args.sendtext} to {args.dest} on channelIndex:{channelIndex}" + f"Sending text message {args.sendtext} to {args.dest}:{args.textport} on channelIndex:{channelIndex}" ) interface.sendText( args.sendtext, @@ -450,6 +450,7 @@ def onConnected(interface): wantAck=True, channelIndex=channelIndex, onResponse=interface.getNode(args.dest, False, **getNode_kwargs).onAckNak, + portNum=args.textport ) else: meshtastic.util.our_exit( @@ -1592,10 +1593,18 @@ def addRemoteActionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPar group.add_argument( "--sendtext", - help="Send a text message. Can specify a destination '--dest' and/or channel index '--ch-index'.", + help="Send a text message. Can specify a destination '--dest', port '--textport', and/or channel index '--ch-index'.", metavar="TEXT", ) + group.add_argument( + "--textport", + help="Optional argument for sending text messages to the non default port. Use in combination with --sendtext.", + type=int, + default=portnums_pb2.PortNum.TEXT_MESSAGE_APP, + metavar="PORT" + ) + group.add_argument( "--traceroute", help="Traceroute from connected node to a destination. " diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 648755c..ddc938d 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -354,6 +354,7 @@ class MeshInterface: # pylint: disable=R0902 wantResponse: bool = False, onResponse: Optional[Callable[[dict], Any]] = None, channelIndex: int = 0, + portNum: int = portnums_pb2.PortNum.TEXT_MESSAGE_APP ): """Send a utf8 string to some other node, if the node has a display it will also be shown on the device. @@ -364,12 +365,12 @@ class MeshInterface: # pylint: disable=R0902 Keyword Arguments: destinationId {nodeId or nodeNum} -- where to send this message (default: {BROADCAST_ADDR}) - portNum -- the application portnum (similar to IP port numbers) - of the destination, see portnums.proto for a list wantAck -- True if you want the message sent in a reliable manner (with retries and ack/nak provided for delivery) wantResponse -- True if you want the service on the other side to send an application layer response + portNum -- the application portnum (similar to IP port numbers) + of the destination, see portnums.proto for a list Returns the sent packet. The id field will be populated in this packet and can be used to track future message acks/naks. @@ -378,7 +379,7 @@ class MeshInterface: # pylint: disable=R0902 return self.sendData( text.encode("utf-8"), destinationId, - portNum=portnums_pb2.PortNum.TEXT_MESSAGE_APP, + portNum=portNum, wantAck=wantAck, wantResponse=wantResponse, onResponse=onResponse, From 0fb72b8ad1bb4a2824d0f8c86149667363f85a15 Mon Sep 17 00:00:00 2001 From: digitaldisarray Date: Tue, 31 Dec 2024 15:15:18 -0800 Subject: [PATCH 2/3] Only allow PRIVATE_APP custom port --- meshtastic/__main__.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index a26c497..4b36f8d 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -442,7 +442,7 @@ def onConnected(interface): channelIndex = mt_config.channel_index or 0 if checkChannel(interface, channelIndex): print( - f"Sending text message {args.sendtext} to {args.dest}:{args.textport} on channelIndex:{channelIndex}" + f"Sending text message {args.sendtext} to {args.dest} on channelIndex:{channelIndex} {"using PRIVATE_APP port" if args.private else ""}" ) interface.sendText( args.sendtext, @@ -450,7 +450,7 @@ def onConnected(interface): wantAck=True, channelIndex=channelIndex, onResponse=interface.getNode(args.dest, False, **getNode_kwargs).onAckNak, - portNum=args.textport + portNum=portnums_pb2.PortNum.PRIVATE_APP if args.private else portnums_pb2.PortNum.TEXT_MESSAGE_APP ) else: meshtastic.util.our_exit( @@ -1593,16 +1593,14 @@ def addRemoteActionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPar group.add_argument( "--sendtext", - help="Send a text message. Can specify a destination '--dest', port '--textport', and/or channel index '--ch-index'.", + help="Send a text message. Can specify a destination '--dest', use of PRIVATE_APP port '--private', and/or channel index '--ch-index'.", metavar="TEXT", ) - group.add_argument( - "--textport", - help="Optional argument for sending text messages to the non default port. Use in combination with --sendtext.", - type=int, - default=portnums_pb2.PortNum.TEXT_MESSAGE_APP, - metavar="PORT" + group.add_argument( + "--private", + help="Optional argument for sending text messages to the PRIVATE_APP port. Use in combination with --sendtext.", + action="store_true" ) group.add_argument( From 3954fbd404de3725aa942a0312caf6ad1eb78af6 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Wed, 19 Feb 2025 09:52:16 -0700 Subject: [PATCH 3/3] fix misc lint, test, type complaints from CI --- meshtastic/__main__.py | 3 ++- meshtastic/mesh_interface.py | 2 +- meshtastic/tests/test_main.py | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 4b36f8d..18ea484 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -442,7 +442,8 @@ def onConnected(interface): channelIndex = mt_config.channel_index or 0 if checkChannel(interface, channelIndex): print( - f"Sending text message {args.sendtext} to {args.dest} on channelIndex:{channelIndex} {"using PRIVATE_APP port" if args.private else ""}" + f"Sending text message {args.sendtext} to {args.dest} on channelIndex:{channelIndex}" + f" {'using PRIVATE_APP port' if args.private else ''}" ) interface.sendText( args.sendtext, diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index ddc938d..b05157b 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -354,7 +354,7 @@ class MeshInterface: # pylint: disable=R0902 wantResponse: bool = False, onResponse: Optional[Callable[[dict], Any]] = None, channelIndex: int = 0, - portNum: int = portnums_pb2.PortNum.TEXT_MESSAGE_APP + portNum: portnums_pb2.PortNum.ValueType = portnums_pb2.PortNum.TEXT_MESSAGE_APP ): """Send a utf8 string to some other node, if the node has a display it will also be shown on the device. diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 86d3ad2..7f99c84 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -593,10 +593,10 @@ def test_main_sendtext(capsys): iface = MagicMock(autospec=SerialInterface) def mock_sendText( - text, dest, wantAck=False, wantResponse=False, onResponse=None, channelIndex=0 + text, dest, wantAck=False, wantResponse=False, onResponse=None, channelIndex=0, portNum=0 ): print("inside mocked sendText") - print(f"{text} {dest} {wantAck} {wantResponse} {channelIndex}") + print(f"{text} {dest} {wantAck} {wantResponse} {channelIndex} {portNum}") iface.sendText.side_effect = mock_sendText @@ -620,10 +620,10 @@ def test_main_sendtext_with_channel(capsys): iface = MagicMock(autospec=SerialInterface) def mock_sendText( - text, dest, wantAck=False, wantResponse=False, onResponse=None, channelIndex=0 + text, dest, wantAck=False, wantResponse=False, onResponse=None, channelIndex=0, portNum=0 ): print("inside mocked sendText") - print(f"{text} {dest} {wantAck} {wantResponse} {channelIndex}") + print(f"{text} {dest} {wantAck} {wantResponse} {channelIndex} {portNum}") iface.sendText.side_effect = mock_sendText