Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new channels from an add URL with the new --ch-add-url option #708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion meshtastic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ def onConnected(interface):

# handle changing channels

if args.ch_add_url:
closeNow = True
interface.getNode(args.dest, **getNode_kwargs).setURL(args.ch_add_url, addOnly=True)

if args.ch_add:
channelIndex = mt_config.channel_index
if channelIndex is not None:
Expand Down Expand Up @@ -1443,7 +1447,7 @@ def addConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
"--set-ham", help="Set licensed Ham ID and turn off encryption", action="store"
)

group.add_argument("--seturl", help="Set a channel URL", action="store")
group.add_argument("--seturl", help="Set all channels with a URL", action="store")

return parser

Expand All @@ -1461,6 +1465,13 @@ def addChannelConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPa
default=None,
)

group.add_argument(
"--ch-add-url",
help="Add secondary channels from a supplied URL",
metavar="URL",
default=None,
)

group.add_argument(
"--ch-del", help="Delete the ch-index channel", action="store_true"
)
Expand Down
58 changes: 42 additions & 16 deletions meshtastic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Timeout,
camel_to_snake,
fromPSK,
genPSK256,
our_exit,
pskToString,
stripnl,
Expand Down Expand Up @@ -337,14 +338,19 @@ def getURL(self, includeAll: bool = True):
s = s.replace("=", "").replace("+", "-").replace("/", "_")
return f"https://meshtastic.org/e/#{s}"

def setURL(self, url):
def setURL(self, url, addOnly: bool = False):
"""Set mesh network URL"""
if self.localConfig is None:
our_exit("Warning: No Config has been read")

# URLs are of the form https://meshtastic.org/d/#{base64_channel_set}
# Split on '/#' to find the base64 encoded channel settings
splitURL = url.split("/#")
if addOnly:
splitURL = url.split("/?add=true#")
else:
splitURL = url.split("/#")
if len(splitURL) == 1:
our_exit(f"Warning: Invalid URL '{url}'")
b64 = splitURL[-1]

# We normally strip padding to make for a shorter URL, but the python parser doesn't like
Expand All @@ -361,20 +367,40 @@ def setURL(self, url):
if len(channelSet.settings) == 0:
our_exit("Warning: There were no settings.")

i = 0
for chs in channelSet.settings:
ch = channel_pb2.Channel()
ch.role = (
channel_pb2.Channel.Role.PRIMARY
if i == 0
else channel_pb2.Channel.Role.SECONDARY
)
ch.index = i
ch.settings.CopyFrom(chs)
self.channels[ch.index] = ch
logging.debug(f"Channel i:{i} ch:{ch}")
self.writeChannel(ch.index)
i = i + 1
if addOnly:
# Add new channels with names not already present
# Don't change existing channels
for ch in channelSet.settings:
channelExists = self.getChannelByName(ch.name)
if channelExists or ch.name == "":
print("Ignoring existing channel from add URL")
next
else:
newChannel = self.getDisabledChannel()
if not newChannel:
our_exit("Warning: No free channels were found")
chs = channel_pb2.ChannelSettings()
chs.name = ch.name
chs.psk = ch.psk
newChannel.settings.CopyFrom(chs)
newChannel.role = channel_pb2.Channel.Role.SECONDARY
print(f"Adding new channel '{ch.name}' to device")
self.writeChannel(newChannel.index)
else:
i = 0
for chs in channelSet.settings:
ch = channel_pb2.Channel()
ch.role = (
channel_pb2.Channel.Role.PRIMARY
if i == 0
else channel_pb2.Channel.Role.SECONDARY
)
ch.index = i
ch.settings.CopyFrom(chs)
self.channels[ch.index] = ch
logging.debug(f"Channel i:{i} ch:{ch}")
self.writeChannel(ch.index)
i = i + 1

p = admin_pb2.AdminMessage()
p.set_config.lora.CopyFrom(channelSet.lora_config)
Expand Down