1.1.33 Improve channel/device settings support

Allow setting to arbitrary byte arrays by preceding the string with 0x
for things like

meshtastic --setchan psk 0x1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b --info
This commit is contained in:
Kevin Hester
2021-01-29 09:51:56 +08:00
parent 01d1a5ab48
commit 9c1232be79
5 changed files with 60 additions and 30 deletions

8
.vscode/launch.json vendored
View File

@@ -28,6 +28,14 @@
"justMyCode": true,
"args": ["--tunnel", "--debug"]
},
{
"name": "meshtastic set chan",
"type": "python",
"request": "launch",
"module": "meshtastic",
"justMyCode": true,
"args": ["--setchan", "psk", "0x1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b", "--debug"]
},
{
"name": "meshtastic debug",
"type": "python",

View File

@@ -39,6 +39,8 @@ To display a (partial) list of the available commands:
meshtastic -h
```
### Changing device settings
You can also use this tool to set any of the device parameters which are stored in persistent storage. For instance, here's how to set the device
to keep the bluetooth link alive for eight hours (any usage of the bluetooth protcol from your phone will reset this timer)
@@ -69,6 +71,24 @@ meshtastic --set wifi_ap_mode true --setstr wifi_ssid mywifissid --setstr wifi_p
For a full list of preferences which can be set (and their documentation) see [here](https://github.com/meshtastic/Meshtastic-protobufs/blob/master/docs/docs.md#.RadioConfig.UserPreferences).
### Changing channel settings
The channel settings can be changed similiarly. Either by using a standard (sharable) meshtastic URL or you can set partiular channel parameters (for advanced users).
The URL is constructed automatically based off of the current channel settings. So if you want to customize a channel you could do something like:
```
meshtastic --setchan name mychan --setchan channel_num 4 --info
```
This will change some channel params and then show device info (which will include the current channel URL)
You can even set the channel preshared key to a particular AES128 or AES256 sequence.
```
meshtastic --setchan psk 0x1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b1a1a1a1a2b2b2b2b --info
```
## FAQ/common problems
This is a collection of common questions and answers from our friendly forum.

View File

@@ -115,12 +115,12 @@ class Tunnel:
ip = self._nodeNumToIp(node["num"])
logging.info(f"Node { nodeId } has IP address { ip }")
logging.debug("creating TUN device")
logging.debug("creating TUN device with MTU=200")
# FIXME - figure out real max MTU, it should be 240 - the overhead bytes for SubPacket and Data
from pytap2 import TapDevice
self.tun = TapDevice(name="mesh", mtu=200)
self.tun = TapDevice(name="mesh")
self.tun.up()
self.tun.ifconfig(address=myAddr,netmask=netmask)
self.tun.ifconfig(address=myAddr,netmask=netmask,mtu=200)
logging.debug(f"starting TUN reader, our IP address is {myAddr}")
self._rxThread = threading.Thread(target=self.__tunReader, args=(), daemon=True)
self._rxThread.start()
@@ -346,12 +346,12 @@ subnet is used to construct our network number (normally 10.115.x.x)</p></div>
ip = self._nodeNumToIp(node[&#34;num&#34;])
logging.info(f&#34;Node { nodeId } has IP address { ip }&#34;)
logging.debug(&#34;creating TUN device&#34;)
logging.debug(&#34;creating TUN device with MTU=200&#34;)
# FIXME - figure out real max MTU, it should be 240 - the overhead bytes for SubPacket and Data
from pytap2 import TapDevice
self.tun = TapDevice(name=&#34;mesh&#34;, mtu=200)
self.tun = TapDevice(name=&#34;mesh&#34;)
self.tun.up()
self.tun.ifconfig(address=myAddr,netmask=netmask)
self.tun.ifconfig(address=myAddr,netmask=netmask,mtu=200)
logging.debug(f&#34;starting TUN reader, our IP address is {myAddr}&#34;)
self._rxThread = threading.Thread(target=self.__tunReader, args=(), daemon=True)
self._rxThread.start()

View File

@@ -52,25 +52,28 @@ def onConnection(interface, topic=pub.AUTO_TOPIC):
def fromStr(valstr):
"""try to parse as int, float or bool (and fallback to a string as last resort)
Returns: an int, bool, float or str
Returns: an int, bool, float, str or byte array (for strings of hex digits)
Args:
valstr (string): A user provided string
"""
try:
val = int(valstr)
except ValueError:
if(valstr.startswith('0x')):
val = bytes.fromhex(valstr[2:]) # if needed convert to string with asBytes.decode('utf-8')
else:
try:
val = float(valstr)
val = int(valstr)
except ValueError:
trueTerms = {"t", "true", "yes"}
falseTerms = {"f", "false", "no"}
if valstr in trueTerms:
val = True
elif valstr in falseTerms:
val = False
else:
val = valstr # Try to treat the parameter as a string
try:
val = float(valstr)
except ValueError:
trueTerms = {"t", "true", "yes"}
falseTerms = {"f", "false", "no"}
if valstr in trueTerms:
val = True
elif valstr in falseTerms:
val = False
else:
val = valstr # Try to treat the parameter as a string
return val
@@ -207,19 +210,18 @@ def onConnected(interface):
or args.seturl or args.router != None:
closeNow = True
def setPref(attributes, name, val):
def setPref(attributes, name, valStr):
"""Set a preferences value"""
print(f"Setting {name} to {val}")
val = fromStr(valStr)
try:
try:
setattr(attributes, name, val)
except TypeError as ex:
# The setter didn't like our arg type - try again as a byte array (so we can convert strings to bytearray)
if isinstance(val, str):
setattr(attributes, name,
codecs.decode(val, "hex"))
else:
print(f"Incorrect type for {name} {ex}")
# The setter didn't like our arg type guess try again as a string
setattr(attributes, name, valStr)
# succeeded!
print(f"Set {name} to {valStr}")
except Exception as ex:
print(f"Can't set {name} due to {ex}")
@@ -236,7 +238,7 @@ def onConnected(interface):
# Handle the int/float/bool arguments
for pref in (args.set or []):
setPref(
prefs, pref[0], fromStr(pref[1]))
prefs, pref[0], pref[1])
# Handle the string arguments
for pref in (args.setstr or []):
@@ -252,7 +254,7 @@ def onConnected(interface):
# Handle the channel settings
for pref in (args.setchan or []):
setPref(interface.radioConfig.channel_settings,
pref[0], fromStr(pref[1]))
pref[0], pref[1])
# Handle set URL
if args.seturl:

View File

@@ -12,7 +12,7 @@ with open("README.md", "r") as fh:
# This call to setup() does all the work
setup(
name="meshtastic",
version="1.1.32",
version="1.1.33",
description="Python API & client shell for talking to Meshtastic devices",
long_description=long_description,
long_description_content_type="text/markdown",