Merge remote-tracking branch 'upstream/main'

This commit is contained in:
jacob.eva 2025-02-20 12:43:45 +00:00
commit 39eccf8461
No known key found for this signature in database
GPG Key ID: 0B92E083BBCCAA1E
33 changed files with 8409 additions and 238 deletions

View File

@ -12,10 +12,14 @@ Before creating a bug report on this issue tracker, you **must** read the [Contr
- The issue tracker is used by developers of this project. **Do not use it to ask general questions, or for support requests**. - The issue tracker is used by developers of this project. **Do not use it to ask general questions, or for support requests**.
- Ideas and feature requests can be made on the [Discussions](https://github.com/markqvist/Reticulum/discussions). **Only** feature requests accepted by maintainers and developers are tracked and included on the issue tracker. **Do not post feature requests here**. - Ideas and feature requests can be made on the [Discussions](https://github.com/markqvist/Reticulum/discussions). **Only** feature requests accepted by maintainers and developers are tracked and included on the issue tracker. **Do not post feature requests here**.
- After reading the [Contribution Guidelines](https://github.com/markqvist/Reticulum/blob/master/Contributing.md), delete this section from your bug report. - After reading the [Contribution Guidelines](https://github.com/markqvist/Reticulum/blob/master/Contributing.md), **delete this section only** (*"Read the Contribution Guidelines"*) from your bug report, **and fill in all the other sections**.
**Describe the Bug** **Describe the Bug**
A clear and concise description of what the bug is. First of all: Is this really a bug? Is it reproducible?
If this is a request for help because something is not working as you expected, stop right here, and go to the [discussions](https://github.com/markqvist/Reticulum/discussions) instead, where you can post your questions and get help from other users.
If this really is a bug or issue with the software, remove this section of the template, and provide **a clear and concise description of what the bug is**.
**To Reproduce** **To Reproduce**
Describe in detail how to reproduce the bug. Describe in detail how to reproduce the bug.
@ -24,7 +28,7 @@ Describe in detail how to reproduce the bug.
A clear and concise description of what you expected to happen. A clear and concise description of what you expected to happen.
**Logs & Screenshots** **Logs & Screenshots**
Please include any relevant log output. If applicable, also add screenshots to help explain your problem. Please include any relevant log output. If applicable, also add screenshots to help explain your problem. In most cases, without any relevant log output, we will not be able to determine the cause of the bug, or reproduce it.
**System Information** **System Information**
- OS and version - OS and version

View File

@ -195,24 +195,33 @@ To install Sideband via `pip`, follow these instructions:
```bash ```bash
# Install Sideband and dependencies on macOS using pip: # Install Sideband and dependencies on macOS using pip:
pip3 install sbapp --user --break-system-packages pip3 install sbapp
# Optionally install RNS command line utilities:
pip3 install rns
# Run Sideband from the terminal: # Run Sideband from the terminal:
#################################
sideband
# or
python3 -m sbapp.main python3 -m sbapp.main
# Enable debug logging: # Enable debug logging:
#################################
sideband -v
# or
python3 -m sbapp.main -v python3 -m sbapp.main -v
# Start Sideband in daemon mode: # Start Sideband in daemon mode:
#################################
sideband -d
# or
python3 -m sbapp.main -d python3 -m sbapp.main -d
# If you add your pip install location to # If Python and pip was installed correctly,
# the PATH environment variable, you can # you can simply use the "sideband" command
# also run Sideband simply using: # directly. Otherwise, you will manually
sideband # need to add the pip binaries directory to
# your PATH environment variable, or start
# Sideband via the "python3 -m sbapp.main"
# syntax.
``` ```
@ -302,11 +311,11 @@ You can help support the continued development of open, free and private communi
- <s>Adding a Linux desktop integration</s> - <s>Adding a Linux desktop integration</s>
- <s>Adding prebuilt Windows binaries to the releases</s> - <s>Adding prebuilt Windows binaries to the releases</s>
- <s>Adding prebuilt macOS binaries to the releases</s> - <s>Adding prebuilt macOS binaries to the releases</s>
- <s>A debug log viewer</s>
- Adding a Nomad Net page browser - Adding a Nomad Net page browser
- LXMF sneakernet functionality - LXMF sneakernet functionality
- Network visualisation and test tools - Network visualisation and test tools
- Better message sorting mechanism - Better message sorting mechanism
- A debug log viewer
# License # License
Unless otherwise noted, this work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa]. Unless otherwise noted, this work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa].

View File

@ -0,0 +1,41 @@
# This is an LXMd telemetry plugin that
# queries a running LXMF Propagation Node
# for status and statistics.
import RNS
class LXMdTelemetryPlugin(SidebandTelemetryPlugin):
plugin_name = "lxmd_telemetry"
def start(self):
# Do any initialisation work here
RNS.log("LXMd telemetry plugin starting...")
# And finally call start on superclass
super().start()
def stop(self):
# Do any teardown work here
pass
# And finally call stop on superclass
super().stop()
def update_telemetry(self, telemeter):
if telemeter != None:
if not "lxmf_propagation" in telemeter.sensors:
# Create lxmd status sensor if it is not already
# enabled in the running telemeter instance
telemeter.enable("lxmf_propagation")
# Set the identity file used to communicate with
# the running LXMd instance.
telemeter.sensors["lxmf_propagation"].set_identity("~/.lxmd/identity")
# You can also get LXMF Propagation Node stats
# from an LXMd instance running inside nomadnet
# telemeter.sensors["lxmf_propagation"].set_identity("~/.nomadnetwork/storage/identity")
# Finally, tell Sideband what class in this
# file is the actual plugin class.
plugin_class = LXMdTelemetryPlugin

View File

@ -59,7 +59,8 @@ class BasicTelemetryPlugin(SidebandTelemetryPlugin):
# Create fuel sensor # Create fuel sensor
telemeter.synthesize("fuel") telemeter.synthesize("fuel")
telemeter.sensors["fuel"].update_entry(capacity=75, level=61) telemeter.sensors["fuel"].update_entry(capacity=75, level=61, type_label="Main")
telemeter.sensors["fuel"].update_entry(capacity=15, level=15, type_label="Reserve")
# Finally, tell Sideband what class in this # Finally, tell Sideband what class in this
# file is the actual plugin class. # file is the actual plugin class.

View File

@ -310,7 +310,8 @@ class ViewCommandPlugin(SidebandCommandPlugin):
else: else:
image_field = self.sources[source].get_image_field(quality_preset) image_field = self.sources[source].get_image_field(quality_preset)
image_timestamp = self.timestamp_str(self.sources[source].last_update) image_timestamp = self.timestamp_str(self.sources[source].last_update)
message = f"Source [b]{source}[/b] at [b]{image_timestamp}[/b]" message = "#!md\n" # Tell sideband to format this message
message += f"Source [b]{source}[/b] at [b]{image_timestamp}[/b]"
if image_field != None: if image_field != None:
self.image_response(message, image_field, requestor) self.image_response(message, image_field, requestor)

View File

@ -1,129 +0,0 @@
import os
import math
import RNS
import RNS.vendor.umsgpack as mp
def simulate(link_speed=9600, audio_slot_ms=70, codec_rate=1200, method="msgpack"):
# Simulated on-air link speed
LINK_SPEED = link_speed
# Packing method, can be "msgpack" or "protobuf"
PACKING_METHOD = method
# The target audio slot time
TARGET_MS = audio_slot_ms
# Packets needed per second for half-duplex audio
PACKETS_PER_SECOND = 1000/TARGET_MS
# Effective audio encoder bitrate
CODEC_RATE = codec_rate
# Maximum number of supported audio modes
MAX_ENUM = 127
# Per-packet overhead on a established link is 19
# bytes, 3 for header and context, 16 for link ID
RNS_OVERHEAD = 19
# Physical-layer overhead. For RNode, this is 1
# byte per RNS packet.
PHY_OVERHEAD = 1
# Total transport overhead
TRANSPORT_OVERHEAD = PHY_OVERHEAD+RNS_OVERHEAD
# Calculate parameters
AUDIO_LEN = int(math.ceil(CODEC_RATE/(1000/TARGET_MS)/8))
PER_BYTE_LATENCY_MS = 1000/(LINK_SPEED/8)
# Pack the message with msgpack to get real-
# world packed message size
if PACKING_METHOD == "msgpack":
# Calculate msgpack overhead
PL_LEN = len(mp.packb([MAX_ENUM, os.urandom(AUDIO_LEN)]))
PACKING_OVERHEAD = PL_LEN-AUDIO_LEN
elif PACKING_METHOD == "protobuf":
# For protobuf, assume the 8 bytes of stated overhead
PACKING_OVERHEAD = 8
PL_LEN = AUDIO_LEN+PACKING_OVERHEAD
else:
print("Unsupported packing method")
exit(1)
# Calculate required encrypted token blocks
BLOCKSIZE = 16
REQUIRED_BLOCKS = math.ceil((PL_LEN+1)/BLOCKSIZE)
ENCRYPTED_PAYLOAD_LEN = REQUIRED_BLOCKS*BLOCKSIZE
BLOCK_HEADROOM = (REQUIRED_BLOCKS*BLOCKSIZE) - PL_LEN - 1
# The complete on-air packet length
PACKET_LEN = PHY_OVERHEAD+RNS_OVERHEAD+ENCRYPTED_PAYLOAD_LEN
PACKET_LATENCY = round(PACKET_LEN*PER_BYTE_LATENCY_MS, 1)
# TODO: This should include any additional
# airtime consumption such as preamble and TX-tail.
PACKET_AIRTIME = PACKET_LEN*PER_BYTE_LATENCY_MS
AIRTIME_PCT = (PACKET_AIRTIME/TARGET_MS) * 100
# Maximum amount of concurrent full-duplex
# calls that can coexist on the same channel
CONCURRENT_CALLS = math.floor(100/AIRTIME_PCT)
# Calculate latencies
TRANSPORT_LATENCY = round((PHY_OVERHEAD+RNS_OVERHEAD)*PER_BYTE_LATENCY_MS, 1)
PAYLOAD_LATENCY = round(ENCRYPTED_PAYLOAD_LEN*PER_BYTE_LATENCY_MS, 1)
RAW_DATA_LATENCY = round(AUDIO_LEN*PER_BYTE_LATENCY_MS, 1)
PACKING_LATENCY = round(PACKING_OVERHEAD*PER_BYTE_LATENCY_MS, 1)
DATA_LATENCY = round(ENCRYPTED_PAYLOAD_LEN*PER_BYTE_LATENCY_MS, 1)
ENCRYPTION_LATENCY = round((ENCRYPTED_PAYLOAD_LEN-PL_LEN)*PER_BYTE_LATENCY_MS, 1)
if ENCRYPTED_PAYLOAD_LEN-PL_LEN == 1:
E_OPT_STR = "(optimal)"
else:
E_OPT_STR = "(sub-optimal)"
TOTAL_LATENCY = round(TARGET_MS+PACKET_LATENCY, 1)
print( "\n===== Simulation Parameters ===\n")
print(f" Packing method : {method}")
print(f" Sampling delay : {TARGET_MS}ms")
print(f" Codec bitrate : {CODEC_RATE} bps")
print(f" Audio data : {AUDIO_LEN} bytes")
print(f" Packing overhead : {PACKING_OVERHEAD} bytes")
print(f" Payload length : {PL_LEN} bytes")
print(f" AES blocks needed : {REQUIRED_BLOCKS}")
print(f" Encrypted payload : {ENCRYPTED_PAYLOAD_LEN} bytes")
print(f" Transport overhead : {TRANSPORT_OVERHEAD} bytes ({RNS_OVERHEAD} from RNS, {PHY_OVERHEAD} from PHY)")
print(f" On-air length : {PACKET_LEN} bytes")
print(f" Packet airtime : {PACKET_AIRTIME}ms")
print( "\n===== Results for "+RNS.prettyspeed(LINK_SPEED)+" Link Speed ===\n")
print(f" Final latency : {TOTAL_LATENCY}ms")
print(f" Recording latency : contributes {TARGET_MS}ms")
print(f" Packet transport : contributes {PACKET_LATENCY}ms")
print(f" Payload : contributes {PAYLOAD_LATENCY}ms")
print(f" Audio data : contributes {RAW_DATA_LATENCY}ms")
print(f" Packing format : contributes {PACKING_LATENCY}ms")
print(f" Encryption : contributes {ENCRYPTION_LATENCY}ms {E_OPT_STR}")
print(f" RNS+PHY overhead : contributes {TRANSPORT_LATENCY}ms")
print(f"")
print(f" Half-duplex airtime : {round(AIRTIME_PCT, 2)}% of link capacity")
print(f" Concurrent calls : {int(CONCURRENT_CALLS)}\n")
print(f" Full-duplex airtime : {round(AIRTIME_PCT*2, 2)}% of link capacity")
print(f" Concurrent calls : {int(CONCURRENT_CALLS/2)}")
if BLOCK_HEADROOM != 0:
print("")
print(f" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(f" Unaligned AES block! Each packet could fit")
print(f" {BLOCK_HEADROOM} bytes of additional audio data")
print(f" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print( "\n= With mspack =================")
simulate(method="msgpack")
#print("\n\n= With protobuf ===============")
#simulate(method="protobuf")

View File

@ -10,9 +10,9 @@ source.exclude_patterns = app_storage/*,venv/*,Makefile,./Makefil*,requirements,
version.regex = __version__ = ['"](.*)['"] version.regex = __version__ = ['"](.*)['"]
version.filename = %(source.dir)s/main.py version.filename = %(source.dir)s/main.py
android.numeric_version = 20241213 android.numeric_version = 20250126
requirements = kivy==2.3.0,libbz2,pillow==10.2.0,qrcode==7.3.1,usb4a,usbserial4a,able_recipe,libwebp,libogg,libopus,opusfile,numpy,cryptography,ffpyplayer,codec2,pycodec2,sh,pynacl,typing-extensions requirements = kivy==2.3.0,libbz2,pillow==10.2.0,qrcode==7.3.1,usb4a,usbserial4a,able_recipe,libwebp,libogg,libopus,opusfile,numpy,cryptography,ffpyplayer,codec2,pycodec2,sh,pynacl,typing-extensions,mistune>=3.0.2,beautifulsoup4
#android.gradle_dependencies = com.android.support:support-compat:28.0.0 #android.gradle_dependencies = com.android.support:support-compat:28.0.0
#android.enable_androidx = True #android.enable_androidx = True

View File

@ -1,6 +1,6 @@
__debug_build__ = False __debug_build__ = False
__disable_shaders__ = False __disable_shaders__ = False
__version__ = "1.2.0" __version__ = "1.4.0"
__variant__ = "" __variant__ = ""
import sys import sys
@ -19,6 +19,7 @@ import RNS
import LXMF import LXMF
import time import time
import os import os
import re
import pathlib import pathlib
import base64 import base64
import threading import threading
@ -196,7 +197,7 @@ if args.daemon:
NewConv = DaemonElement; Telemetry = DaemonElement; ObjectDetails = DaemonElement; Announces = DaemonElement; NewConv = DaemonElement; Telemetry = DaemonElement; ObjectDetails = DaemonElement; Announces = DaemonElement;
Messages = DaemonElement; ts_format = DaemonElement; messages_screen_kv = DaemonElement; plyer = DaemonElement; multilingual_markup = DaemonElement; Messages = DaemonElement; ts_format = DaemonElement; messages_screen_kv = DaemonElement; plyer = DaemonElement; multilingual_markup = DaemonElement;
ContentNavigationDrawer = DaemonElement; DrawerList = DaemonElement; IconListItem = DaemonElement; escape_markup = DaemonElement; ContentNavigationDrawer = DaemonElement; DrawerList = DaemonElement; IconListItem = DaemonElement; escape_markup = DaemonElement;
SoundLoader = DaemonElement; BoxLayout = DaemonElement; SoundLoader = DaemonElement; BoxLayout = DaemonElement; mdconv = DaemonElement;
else: else:
apply_ui_scale() apply_ui_scale()
@ -274,9 +275,7 @@ else:
import sbapp.pyogg as pyogg import sbapp.pyogg as pyogg
from sbapp.pydub import AudioSegment from sbapp.pydub import AudioSegment
class toast: from kivymd.toast import toast
def __init__(self, *kwargs):
pass
from kivy.config import Config from kivy.config import Config
Config.set('input', 'mouse', 'mouse,disable_multitouch') Config.set('input', 'mouse', 'mouse,disable_multitouch')
@ -385,7 +384,8 @@ class SidebandApp(MDApp):
self.connectivity_updater = None self.connectivity_updater = None
self.last_map_update = 0 self.last_map_update = 0
self.last_telemetry_received = 0 self.last_telemetry_received = 0
self.reposository_url = None self.repository_url = None
self.rnode_flasher_url = None
################################################# #################################################
@ -1524,6 +1524,58 @@ class SidebandApp(MDApp):
### Messages (conversation) screen ### Messages (conversation) screen
###################################### ######################################
def md_to_bbcode(self, text):
if not hasattr(self, "mdconv"):
if RNS.vendor.platformutils.is_android(): from md import mdconv
else: from .md import mdconv
self.mdconv = mdconv
converted = self.mdconv(text)
while converted.endswith("\n"):
converted = converted[:-1]
return converted
def process_bb_markup(self, text):
st = time.time()
ms = int(sp(14))
h1s = int(sp(20))
h2s = int(sp(18))
h3s = int(sp(16))
if not hasattr(self, "pres"):
self.presz = re.compile(r"\[(?:size=\d*?)\]", re.IGNORECASE | re.MULTILINE )
self.pres = []
res = [ [r"\[(?:code|icode).*?\]", f"[font=mono][size={ms}]"],
[r"\[\/(?:code|icode).*?\]", "[/size][/font]"],
[r"\[(?:heading)\]", f"[b][size={h1s}]"],
[r"\[(?:heading=1)*?\]", f"[b][size={h1s}]"],
[r"\[(?:heading=2)*?\]", f"[b][size={h2s}]"],
[r"\[(?:heading=3)*?\]", f"[b][size={h3s}]"],
[r"\[(?:heading=).*?\]", f"[b][size={h3s}]"], # Match all remaining lower-level headings
[r"\[\/(?:heading).*?\]", "[/size][/b]"],
[r"\[(?:list).*?\]", ""],
[r"\[\/(?:list).*?\]", ""],
[r"\n\[(?:\*).*?\]", "\n - "],
[r"\[(?:url).*?\]", ""], # Strip URLs for now
[r"\[\/(?:url).*?\]", ""],
[r"\[(?:img).*?\].*\[\/(?:img).*?\]", ""] # Strip images for now
]
for r in res:
self.pres.append([re.compile(r[0], re.IGNORECASE | re.MULTILINE ), r[1]])
size_matches = self.presz.findall(text)
for sm in size_matches:
text = text.replace(sm, f"{sm[:-1]}sp]")
for pr in self.pres:
text = pr[0].sub(pr[1], text)
return text
def conversation_from_announce_action(self, context_dest): def conversation_from_announce_action(self, context_dest):
if self.sideband.has_conversation(context_dest): if self.sideband.has_conversation(context_dest):
pass pass
@ -1766,6 +1818,11 @@ class SidebandApp(MDApp):
if self.root.ids.screen_manager.current == "messages_screen": if self.root.ids.screen_manager.current == "messages_screen":
self.object_details_action(self.messages_view, from_conv=True) self.object_details_action(self.messages_view, from_conv=True)
def outbound_mode_reset(self, sender=None):
self.outbound_mode_paper = False
self.outbound_mode_propagation = False
self.outbound_mode_command = False
def message_propagation_action(self, sender): def message_propagation_action(self, sender):
if self.outbound_mode_command: if self.outbound_mode_command:
self.outbound_mode_paper = False self.outbound_mode_paper = False
@ -1795,18 +1852,10 @@ class SidebandApp(MDApp):
tf = open(path, "rb") tf = open(path, "rb")
tf.close() tf.close()
self.attach_path = path self.attach_path = path
if self.outbound_mode_command:
self.outbound_mode_reset()
if RNS.vendor.platformutils.is_android():
toast("Attached \""+str(fbn)+"\"") toast("Attached \""+str(fbn)+"\"")
else:
ok_button = MDRectangleFlatButton(text="OK",font_size=dp(18))
ate_dialog = MDDialog(
title="File Attached",
text="The file \""+str(fbn)+"\" was attached, and will be included with the next message sent.",
buttons=[ ok_button ],
)
ok_button.bind(on_release=ate_dialog.dismiss)
ate_dialog.open()
except Exception as e: except Exception as e:
RNS.log(f"Error while attaching \"{fbn}\": "+str(e), RNS.LOG_ERROR) RNS.log(f"Error while attaching \"{fbn}\": "+str(e), RNS.LOG_ERROR)
@ -2047,6 +2096,8 @@ class SidebandApp(MDApp):
self.sideband.ui_stopped_recording() self.sideband.ui_stopped_recording()
if self.message_process_audio(): if self.message_process_audio():
if self.outbound_mode_command:
self.outbound_mode_reset()
self.message_send_action() self.message_send_action()
Clock.schedule_once(cb_s, 0.35) Clock.schedule_once(cb_s, 0.35)
@ -2194,6 +2245,8 @@ class SidebandApp(MDApp):
else: else:
self.message_process_audio() self.message_process_audio()
if self.outbound_mode_command:
self.outbound_mode_reset()
self.update_message_widgets() self.update_message_widgets()
toast("Added recorded audio to message") toast("Added recorded audio to message")
@ -2320,18 +2373,7 @@ class SidebandApp(MDApp):
self.attach_type = None self.attach_type = None
self.update_message_widgets() self.update_message_widgets()
if RNS.vendor.platformutils.get_platform() == "android":
toast("Attachment removed") toast("Attachment removed")
else:
ok_button = MDRectangleFlatButton(text="OK",font_size=dp(18))
ate_dialog = MDDialog(
title="Attachment Removed",
text="The attached resource was removed from the message",
buttons=[ ok_button ],
)
ok_button.bind(on_release=ate_dialog.dismiss)
ate_dialog.open()
def shared_attachment_action(self, attachment_data): def shared_attachment_action(self, attachment_data):
if not self.root.ids.screen_manager.current == "messages_screen": if not self.root.ids.screen_manager.current == "messages_screen":
@ -2769,8 +2811,8 @@ class SidebandApp(MDApp):
str_comps = " - [b]Reticulum[/b] (MIT License)\n - [b]LXMF[/b] (MIT License)\n - [b]KivyMD[/b] (MIT License)" str_comps = " - [b]Reticulum[/b] (MIT License)\n - [b]LXMF[/b] (MIT License)\n - [b]KivyMD[/b] (MIT License)"
str_comps += "\n - [b]Kivy[/b] (MIT License)\n - [b]Codec2[/b] (LGPL License)\n - [b]PyCodec2[/b] (BSD-3 License)" str_comps += "\n - [b]Kivy[/b] (MIT License)\n - [b]Codec2[/b] (LGPL License)\n - [b]PyCodec2[/b] (BSD-3 License)"
str_comps += "\n - [b]PyDub[/b] (MIT License)\n - [b]PyOgg[/b] (Public Domain)" str_comps += "\n - [b]PyDub[/b] (MIT License)\n - [b]PyOgg[/b] (Public Domain) \n - [b]FFmpeg[/b] (GPL3 License)"
str_comps += "\n - [b]GeoidHeight[/b] (LGPL License)\n - [b]Python[/b] (PSF License)" str_comps += "\n - [b]GeoidHeight[/b] (LGPL License)\n - [b]Paho MQTT[/b] (EPL2 License)\n - [b]Python[/b] (PSF License)"
str_comps += "\n\nGo to [u][ref=link]https://unsigned.io/donate[/ref][/u] to support the upstream Sideband project.\n\nThe openCom Companion app is Copyright (c) 2024 Liberated Embedded Systems.\n\nPermission is granted to freely share and distribute binary copies of openCom Companion v"+__version__+" "+__variant__+", so long as no payment or compensation is charged for said distribution or sharing.\n\nIf you were charged or paid anything for this copy of openCom Companion, please report it to [b]contact@liberatedsystems.co.uk[/b].\n\nTHIS IS EXPERIMENTAL SOFTWARE - OPENCOM COMPANION COMES WITH ABSOLUTELY NO WARRANTY - USE AT YOUR OWN RISK AND RESPONSIBILITY" str_comps += "\n\nGo to [u][ref=link]https://unsigned.io/donate[/ref][/u] to support the upstream Sideband project.\n\nThe openCom Companion app is Copyright (c) 2024 Liberated Embedded Systems.\n\nPermission is granted to freely share and distribute binary copies of openCom Companion v"+__version__+" "+__variant__+", so long as no payment or compensation is charged for said distribution or sharing.\n\nIf you were charged or paid anything for this copy of openCom Companion, please report it to [b]contact@liberatedsystems.co.uk[/b].\n\nTHIS IS EXPERIMENTAL SOFTWARE - OPENCOM COMPANION COMES WITH ABSOLUTELY NO WARRANTY - USE AT YOUR OWN RISK AND RESPONSIBILITY"
info = "This is "+self.root.ids.app_version_info.text+", on RNS v"+RNS.__version__+" and LXMF v"+LXMF.__version__+".\n\nHumbly built using the following open components:\n\n"+str_comps info = "This is "+self.root.ids.app_version_info.text+", on RNS v"+RNS.__version__+" and LXMF v"+LXMF.__version__+".\n\nHumbly built using the following open components:\n\n"+str_comps
self.information_screen.ids.information_info.text = info self.information_screen.ids.information_info.text = info
@ -3052,6 +3094,10 @@ class SidebandApp(MDApp):
self.sideband.config["trusted_markup_only"] = self.settings_screen.ids.settings_trusted_markup_only.active self.sideband.config["trusted_markup_only"] = self.settings_screen.ids.settings_trusted_markup_only.active
self.sideband.save_configuration() self.sideband.save_configuration()
def save_compose_in_markdown(sender=None, event=None):
self.sideband.config["compose_in_markdown"] = self.settings_screen.ids.settings_compose_in_markdown.active
self.sideband.save_configuration()
def save_advanced_stats(sender=None, event=None): def save_advanced_stats(sender=None, event=None):
self.sideband.config["advanced_stats"] = self.settings_screen.ids.settings_advanced_statistics.active self.sideband.config["advanced_stats"] = self.settings_screen.ids.settings_advanced_statistics.active
self.sideband.save_configuration() self.sideband.save_configuration()
@ -3230,6 +3276,9 @@ class SidebandApp(MDApp):
self.settings_screen.ids.settings_trusted_markup_only.active = self.sideband.config["trusted_markup_only"] self.settings_screen.ids.settings_trusted_markup_only.active = self.sideband.config["trusted_markup_only"]
self.settings_screen.ids.settings_trusted_markup_only.bind(active=save_trusted_markup_only) self.settings_screen.ids.settings_trusted_markup_only.bind(active=save_trusted_markup_only)
self.settings_screen.ids.settings_compose_in_markdown.active = self.sideband.config["compose_in_markdown"]
self.settings_screen.ids.settings_compose_in_markdown.bind(active=save_compose_in_markdown)
self.settings_screen.ids.settings_ignore_invalid_stamps.active = self.sideband.config["lxmf_ignore_invalid_stamps"] self.settings_screen.ids.settings_ignore_invalid_stamps.active = self.sideband.config["lxmf_ignore_invalid_stamps"]
self.settings_screen.ids.settings_ignore_invalid_stamps.bind(active=save_lxmf_ignore_invalid_stamps) self.settings_screen.ids.settings_ignore_invalid_stamps.bind(active=save_lxmf_ignore_invalid_stamps)
@ -3705,9 +3754,9 @@ class SidebandApp(MDApp):
self.root.ids.screen_manager.transition = self.slide_transition self.root.ids.screen_manager.transition = self.slide_transition
def repository_link_action(self, sender=None, event=None): def repository_link_action(self, sender=None, event=None):
if self.reposository_url != None: if self.repository_url != None:
def lj(): def lj():
webbrowser.open(self.reposository_url) webbrowser.open(self.repository_url)
threading.Thread(target=lj, daemon=True).start() threading.Thread(target=lj, daemon=True).start()
def repository_update_info(self, sender=None): def repository_update_info(self, sender=None):
@ -3749,27 +3798,38 @@ class SidebandApp(MDApp):
ips = getIP() ips = getIP()
if ips == None or len(ips) == 0: if ips == None or len(ips) == 0:
info += "The repository server is running, but the local device IP address could not be determined.\n\nYou can access the repository by pointing a browser to: https://DEVICE_IP:4444/" info += "The repository server is running, but the local device IP address could not be determined.\n\nYou can access the repository by pointing a browser to: https://DEVICE_IP:4444/"
self.reposository_url = None self.repository_url = None
else: else:
ipstr = "" ipstr = ""
self.repository_url = None
for ip in ips: for ip in ips:
ipstr += "https://"+str(ip)+":4444/\n" ipurl = "https://" + str(ip) + ":4444/"
self.reposository_url = ipstr ipstr += "[u][ref=link]"+ipurl+"[/ref][u]\n"
if self.repository_url == None:
self.repository_url = ipurl
self.rnode_flasher_url = ipurl+"mirrors/rnode-flasher/index.html"
ms = "" if len(ips) == 1 else "es" ms = "" if len(ips) == 1 else "es"
info += "The repository server is running at the following address"+ms+":\n [u][ref=link]"+ipstr+"[/ref][u]" info += "The repository server is running at the following address" + ms +":\n\n"+ipstr
self.repository_screen.ids.repository_info.bind(on_ref_press=self.repository_link_action) self.repository_screen.ids.repository_info.bind(on_ref_press=self.repository_link_action)
def cb(dt): def cb(dt):
self.repository_screen.ids.repository_enable_button.disabled = True self.repository_screen.ids.repository_enable_button.disabled = True
self.repository_screen.ids.repository_disable_button.disabled = False self.repository_screen.ids.repository_disable_button.disabled = False
if hasattr(self, "wants_flasher_launch") and self.wants_flasher_launch == True:
self.wants_flasher_launch = False
if self.rnode_flasher_url != None:
def lj():
webbrowser.open(self.rnode_flasher_url)
threading.Thread(target=lj, daemon=True).start()
Clock.schedule_once(cb, 0.1) Clock.schedule_once(cb, 0.1)
else: else:
self.repository_screen.ids.repository_enable_button.disabled = False self.repository_screen.ids.repository_enable_button.disabled = False
self.repository_screen.ids.repository_disable_button.disabled = True self.repository_screen.ids.repository_disable_button.disabled = True
info += "\n" info += ""
self.repository_screen.ids.repository_info.text = info self.repository_screen.ids.repository_info.text = info
def repository_start_action(self, sender=None): def repository_start_action(self, sender=None):
@ -3777,7 +3837,7 @@ class SidebandApp(MDApp):
Clock.schedule_once(self.repository_update_info, 1.0) Clock.schedule_once(self.repository_update_info, 1.0)
def repository_stop_action(self, sender=None): def repository_stop_action(self, sender=None):
self.reposository_url = None self.repository_url = None
self.sideband.stop_webshare() self.sideband.stop_webshare()
Clock.schedule_once(self.repository_update_info, 0.75) Clock.schedule_once(self.repository_update_info, 0.75)
@ -4686,7 +4746,11 @@ class SidebandApp(MDApp):
valid = False valid = False
try: try:
<<<<<<< HEAD
valid_vals = [7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 203.125, 250, 406.25, 500, 812.5, 1625] valid_vals = [7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 203.125, 250, 406.25, 500, 812.5, 1625]
=======
valid_vals = [7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250, 500, 203.125, 406.25, 812.5, 1625]
>>>>>>> upstream/main
val = float(self.hardware_rnode_screen.ids.hardware_rnode_bandwidth.text) val = float(self.hardware_rnode_screen.ids.hardware_rnode_bandwidth.text)
if not val in valid_vals: if not val in valid_vals:
raise ValueError("Invalid bandwidth") raise ValueError("Invalid bandwidth")
@ -5373,7 +5437,11 @@ class SidebandApp(MDApp):
self.bind_clipboard_actions(self.keys_screen.ids) self.bind_clipboard_actions(self.keys_screen.ids)
self.keys_screen.ids.keys_scrollview.effect_cls = ScrollEffect self.keys_screen.ids.keys_scrollview.effect_cls = ScrollEffect
<<<<<<< HEAD
info = "Your primary encryption keys are stored in a Reticulum Identity within the openCom Companion app. If you want to backup this Identity for later use on this or another device, you can export it as a plain text blob, with the key data encoded in Base32 format. This will allow you to restore your address in openCom Companion or other LXMF clients at a later point.\n\n[b]Warning![/b] Anyone that gets access to the key data will be able to control your LXMF address, impersonate you, and read your messages. In is [b]extremely important[/b] that you keep the Identity data secure if you export it.\n\nBefore displaying or exporting your Identity data, make sure that no machine or person in your vicinity is able to see, copy or record your device screen or similar." info = "Your primary encryption keys are stored in a Reticulum Identity within the openCom Companion app. If you want to backup this Identity for later use on this or another device, you can export it as a plain text blob, with the key data encoded in Base32 format. This will allow you to restore your address in openCom Companion or other LXMF clients at a later point.\n\n[b]Warning![/b] Anyone that gets access to the key data will be able to control your LXMF address, impersonate you, and read your messages. In is [b]extremely important[/b] that you keep the Identity data secure if you export it.\n\nBefore displaying or exporting your Identity data, make sure that no machine or person in your vicinity is able to see, copy or record your device screen or similar."
=======
info = "Your primary encryption keys are stored in a Reticulum Identity within the Sideband app. If you want to backup this Identity for later use on this or another device, you can export it as a plain text blob, with the key data encoded in Base32 format. This will allow you to restore your address in Sideband or other LXMF clients at a later point.\n\n[b]Warning![/b] Anyone that gets access to the key data will be able to control your LXMF address, impersonate you, and read your messages. It is [b]extremely important[/b] that you keep the Identity data secure if you export it.\n\nBefore displaying or exporting your Identity data, make sure that no machine or person in your vicinity is able to see, copy or record your device screen or similar."
>>>>>>> upstream/main
if not RNS.vendor.platformutils.get_platform() == "android": if not RNS.vendor.platformutils.get_platform() == "android":
self.widget_hide(self.keys_screen.ids.keys_share) self.widget_hide(self.keys_screen.ids.keys_share)
@ -6276,7 +6344,22 @@ class SidebandApp(MDApp):
self.map_action() self.map_action()
self.map_show(location) self.map_show(location)
def map_display_telemetry(self, sender=None): def map_display_telemetry(self, sender=None, event=None):
alt_event = False
if sender != None:
if hasattr(sender, "last_touch"):
if hasattr(sender.last_touch, "button"):
if sender.last_touch.button == "right":
alt_event = True
if alt_event:
try:
if hasattr(sender, "source_dest"):
self.sideband.request_latest_telemetry(from_addr=sender.source_dest)
toast("Telemetry request sent")
except Exception as e:
RNS.log(f"Could not request telemetry update: {e}", RNS.LOG_ERROR)
else:
self.object_details_action(sender) self.object_details_action(sender)
def map_display_own_telemetry(self, sender=None): def map_display_own_telemetry(self, sender=None):

1
sbapp/md/__init__.py Normal file
View File

@ -0,0 +1 @@
from .md import mdconv

110
sbapp/md/md.py Normal file
View File

@ -0,0 +1,110 @@
import mistune
from mistune.core import BaseRenderer
from mistune.plugins.formatting import strikethrough, mark, superscript, subscript, insert
from mistune.plugins.table import table, table_in_list
from mistune.plugins.footnotes import footnotes
from mistune.plugins.task_lists import task_lists
from mistune.plugins.spoiler import spoiler
from mistune.util import escape as escape_text, safe_entity
def mdconv(markdown_text, domain=None, debug=False):
parser = mistune.create_markdown(renderer=BBRenderer(), plugins=[strikethrough, mark, superscript, subscript, insert, footnotes, task_lists, spoiler])
return parser(markdown_text)
class BBRenderer(BaseRenderer):
NAME = "bbcode"
def __init__(self, escape=False):
super(BBRenderer, self).__init__()
self._escape = escape
def render_token(self, token, state):
func = self._get_method(token["type"])
attrs = token.get("attrs")
if "raw" in token: text = token["raw"]
elif "children" in token: text = self.render_tokens(token["children"], state)
else:
if attrs: return func(**attrs)
else: return func()
if attrs: return func(text, **attrs)
else: return func(text)
# Simple renderers
def emphasis(self, text): return f"[i]{text}[/i]"
def strong(self, text): return f"[b]{text}[/b]"
def codespan(self, text): return f"[icode]{text}[/icode]"
def linebreak(self): return "\n"
def softbreak(self): return "\n"
def list_item(self, text): return f"{text}\n"
def task_list_item(self, text, checked=False): e = "󰱒" if checked else "󰄱"; return f"{e} {text}\n"
def strikethrough(self, text): return f"[s]{text}[/s]"
def insert(self, text): return f"[u]{text}[/u]"
def inline_spoiler(self, text): return f"[ISPOILER]{text}[/ISPOILER]"
def block_spoiler(self, text): return f"[SPOILER]\n{text}\n[/SPOILER]"
def block_error(self, text): return f"[color=red][icode]{text}[/icode][/color]\n"
def block_html(self, html): return ""
def link(self, text, url, title=None): return f"[u]{text}[/u] ({url})"
def footnote_ref(self, key, index): return f"[sup][u]{index}[/u][/sup]"
def footnotes(self, text): return f"[b]Footnotes[/b]\n{text}"
def footnote_item(self, text, key, index): return f"[ANAME=footnote-{index}]{index}[/ANAME]. {text}"
def superscript(self, text: str) -> str: return f"[sup]{text}[/sup]"
def subscript(self, text): return f"[sub]{text}[/sub]"
def block_quote(self, text: str) -> str: return f"| [i]{text}[/i]"
def paragraph(self, text): return f"{text}\n\n"
def blank_line(self): return ""
def block_text(self, text): return text
# Renderers needing some logic
def text(self, text):
if self._escape: return escape_text(text)
else: return text
def inline_html(self, html: str) -> str:
if self._escape: return escape_text(html)
else: return html
def heading(self, text, level, **attrs):
if 1 <= level <= 3: return f"[HEADING={level}]{text}[/HEADING]\n"
else: return f"[HEADING=3]{text}[/HEADING]\n"
def block_code(self, code: str, **attrs) -> str:
special_cases = {"plaintext": None, "text": None, "txt": None}
if "info" in attrs:
lang_info = safe_entity(attrs["info"].strip())
lang = lang_info.split(None, 1)[0].lower()
bbcode_lang = special_cases.get(lang, lang)
if bbcode_lang: return f"[CODE={bbcode_lang}]{escape_text(code)}[/CODE]\n"
else: return f"[CODE]{escape_text(code)}[/CODE]\n"
else: return f"[CODE]{escape_text(code)}[/CODE]\n"
def list(self, text, ordered, **attrs):
depth = 0; sln = ""; tli = ""
if "depth" in attrs: depth = attrs["depth"]
if depth != 0: sln = "\n"
if depth == 0: tli = "\n"
def remove_empty_lines(text):
lines = text.split("\n")
non_empty_lines = [line for line in lines if line.strip() != ""]
nli = ""; dlm = "\n"+" "*depth
if depth != 0: nli = dlm
return nli+dlm.join(non_empty_lines)
text = remove_empty_lines(text)
return sln+text+"\n"+tli
# TODO: Implement various table types and other special formatting
def table(self, children, **attrs): return children
def table_head(self, children, **attrs): return children
def table_body(self, children, **attrs): return children
def table_row(self, children, **attrs): return children
def table_cell(self, text, align=None, head=False, **attrs): return f"{text}\n"
def def_list(self, text): return f"{text}\n"
def def_list_head(self, text): return f"{text}\n"
def def_list_item(self, text): return f"{text}\n"
def abbr(self, text, title): return text
def mark(self, text): return text
def image(self, text, url, title=None): return ""
def thematic_break(self): return "-------------\n"

5
sbapp/pmqtt/__init__.py Normal file
View File

@ -0,0 +1,5 @@
__version__ = "2.1.1.dev0"
class MQTTException(Exception):
pass

4856
sbapp/pmqtt/client.py Normal file

File diff suppressed because it is too large Load Diff

113
sbapp/pmqtt/enums.py Normal file
View File

@ -0,0 +1,113 @@
import enum
class MQTTErrorCode(enum.IntEnum):
MQTT_ERR_AGAIN = -1
MQTT_ERR_SUCCESS = 0
MQTT_ERR_NOMEM = 1
MQTT_ERR_PROTOCOL = 2
MQTT_ERR_INVAL = 3
MQTT_ERR_NO_CONN = 4
MQTT_ERR_CONN_REFUSED = 5
MQTT_ERR_NOT_FOUND = 6
MQTT_ERR_CONN_LOST = 7
MQTT_ERR_TLS = 8
MQTT_ERR_PAYLOAD_SIZE = 9
MQTT_ERR_NOT_SUPPORTED = 10
MQTT_ERR_AUTH = 11
MQTT_ERR_ACL_DENIED = 12
MQTT_ERR_UNKNOWN = 13
MQTT_ERR_ERRNO = 14
MQTT_ERR_QUEUE_SIZE = 15
MQTT_ERR_KEEPALIVE = 16
class MQTTProtocolVersion(enum.IntEnum):
MQTTv31 = 3
MQTTv311 = 4
MQTTv5 = 5
class CallbackAPIVersion(enum.Enum):
"""Defined the arguments passed to all user-callback.
See each callbacks for details: `on_connect`, `on_connect_fail`, `on_disconnect`, `on_message`, `on_publish`,
`on_subscribe`, `on_unsubscribe`, `on_log`, `on_socket_open`, `on_socket_close`,
`on_socket_register_write`, `on_socket_unregister_write`
"""
VERSION1 = 1
"""The version used with paho-mqtt 1.x before introducing CallbackAPIVersion.
This version had different arguments depending if MQTTv5 or MQTTv3 was used. `Properties` & `ReasonCode` were missing
on some callback (apply only to MQTTv5).
This version is deprecated and will be removed in version 3.0.
"""
VERSION2 = 2
""" This version fix some of the shortcoming of previous version.
Callback have the same signature if using MQTTv5 or MQTTv3. `ReasonCode` are used in MQTTv3.
"""
class MessageType(enum.IntEnum):
CONNECT = 0x10
CONNACK = 0x20
PUBLISH = 0x30
PUBACK = 0x40
PUBREC = 0x50
PUBREL = 0x60
PUBCOMP = 0x70
SUBSCRIBE = 0x80
SUBACK = 0x90
UNSUBSCRIBE = 0xA0
UNSUBACK = 0xB0
PINGREQ = 0xC0
PINGRESP = 0xD0
DISCONNECT = 0xE0
AUTH = 0xF0
class LogLevel(enum.IntEnum):
MQTT_LOG_INFO = 0x01
MQTT_LOG_NOTICE = 0x02
MQTT_LOG_WARNING = 0x04
MQTT_LOG_ERR = 0x08
MQTT_LOG_DEBUG = 0x10
class ConnackCode(enum.IntEnum):
CONNACK_ACCEPTED = 0
CONNACK_REFUSED_PROTOCOL_VERSION = 1
CONNACK_REFUSED_IDENTIFIER_REJECTED = 2
CONNACK_REFUSED_SERVER_UNAVAILABLE = 3
CONNACK_REFUSED_BAD_USERNAME_PASSWORD = 4
CONNACK_REFUSED_NOT_AUTHORIZED = 5
class _ConnectionState(enum.Enum):
MQTT_CS_NEW = enum.auto()
MQTT_CS_CONNECT_ASYNC = enum.auto()
MQTT_CS_CONNECTING = enum.auto()
MQTT_CS_CONNECTED = enum.auto()
MQTT_CS_CONNECTION_LOST = enum.auto()
MQTT_CS_DISCONNECTING = enum.auto()
MQTT_CS_DISCONNECTED = enum.auto()
class MessageState(enum.IntEnum):
MQTT_MS_INVALID = 0
MQTT_MS_PUBLISH = 1
MQTT_MS_WAIT_FOR_PUBACK = 2
MQTT_MS_WAIT_FOR_PUBREC = 3
MQTT_MS_RESEND_PUBREL = 4
MQTT_MS_WAIT_FOR_PUBREL = 5
MQTT_MS_RESEND_PUBCOMP = 6
MQTT_MS_WAIT_FOR_PUBCOMP = 7
MQTT_MS_SEND_PUBREC = 8
MQTT_MS_QUEUED = 9
class PahoClientMode(enum.IntEnum):
MQTT_CLIENT = 0
MQTT_BRIDGE = 1

78
sbapp/pmqtt/matcher.py Normal file
View File

@ -0,0 +1,78 @@
class MQTTMatcher:
"""Intended to manage topic filters including wildcards.
Internally, MQTTMatcher use a prefix tree (trie) to store
values associated with filters, and has an iter_match()
method to iterate efficiently over all filters that match
some topic name."""
class Node:
__slots__ = '_children', '_content'
def __init__(self):
self._children = {}
self._content = None
def __init__(self):
self._root = self.Node()
def __setitem__(self, key, value):
"""Add a topic filter :key to the prefix tree
and associate it to :value"""
node = self._root
for sym in key.split('/'):
node = node._children.setdefault(sym, self.Node())
node._content = value
def __getitem__(self, key):
"""Retrieve the value associated with some topic filter :key"""
try:
node = self._root
for sym in key.split('/'):
node = node._children[sym]
if node._content is None:
raise KeyError(key)
return node._content
except KeyError as ke:
raise KeyError(key) from ke
def __delitem__(self, key):
"""Delete the value associated with some topic filter :key"""
lst = []
try:
parent, node = None, self._root
for k in key.split('/'):
parent, node = node, node._children[k]
lst.append((parent, k, node))
# TODO
node._content = None
except KeyError as ke:
raise KeyError(key) from ke
else: # cleanup
for parent, k, node in reversed(lst):
if node._children or node._content is not None:
break
del parent._children[k]
def iter_match(self, topic):
"""Return an iterator on all values associated with filters
that match the :topic"""
lst = topic.split('/')
normal = not topic.startswith('$')
def rec(node, i=0):
if i == len(lst):
if node._content is not None:
yield node._content
else:
part = lst[i]
if part in node._children:
for content in rec(node._children[part], i + 1):
yield content
if '+' in node._children and (normal or i > 0):
for content in rec(node._children['+'], i + 1):
yield content
if '#' in node._children and (normal or i > 0):
content = node._children['#']._content
if content is not None:
yield content
return rec(self._root)

View File

@ -0,0 +1,43 @@
"""
*******************************************************************
Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v20.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Ian Craggs - initial implementation and/or documentation
*******************************************************************
"""
class PacketTypes:
"""
Packet types class. Includes the AUTH packet for MQTT v5.0.
Holds constants for each packet type such as PacketTypes.PUBLISH
and packet name strings: PacketTypes.Names[PacketTypes.PUBLISH].
"""
indexes = range(1, 16)
# Packet types
CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, \
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, \
PINGREQ, PINGRESP, DISCONNECT, AUTH = indexes
# Dummy packet type for properties use - will delay only applies to will
WILLMESSAGE = 99
Names = ( "reserved", \
"Connect", "Connack", "Publish", "Puback", "Pubrec", "Pubrel", \
"Pubcomp", "Subscribe", "Suback", "Unsubscribe", "Unsuback", \
"Pingreq", "Pingresp", "Disconnect", "Auth")

421
sbapp/pmqtt/properties.py Normal file
View File

@ -0,0 +1,421 @@
# *******************************************************************
# Copyright (c) 2017, 2019 IBM Corp.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v20.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Ian Craggs - initial implementation and/or documentation
# *******************************************************************
import struct
from .packettypes import PacketTypes
class MQTTException(Exception):
pass
class MalformedPacket(MQTTException):
pass
def writeInt16(length):
# serialize a 16 bit integer to network format
return bytearray(struct.pack("!H", length))
def readInt16(buf):
# deserialize a 16 bit integer from network format
return struct.unpack("!H", buf[:2])[0]
def writeInt32(length):
# serialize a 32 bit integer to network format
return bytearray(struct.pack("!L", length))
def readInt32(buf):
# deserialize a 32 bit integer from network format
return struct.unpack("!L", buf[:4])[0]
def writeUTF(data):
# data could be a string, or bytes. If string, encode into bytes with utf-8
if not isinstance(data, bytes):
data = bytes(data, "utf-8")
return writeInt16(len(data)) + data
def readUTF(buffer, maxlen):
if maxlen >= 2:
length = readInt16(buffer)
else:
raise MalformedPacket("Not enough data to read string length")
maxlen -= 2
if length > maxlen:
raise MalformedPacket("Length delimited string too long")
buf = buffer[2:2+length].decode("utf-8")
# look for chars which are invalid for MQTT
for c in buf: # look for D800-DFFF in the UTF string
ord_c = ord(c)
if ord_c >= 0xD800 and ord_c <= 0xDFFF:
raise MalformedPacket("[MQTT-1.5.4-1] D800-DFFF found in UTF-8 data")
if ord_c == 0x00: # look for null in the UTF string
raise MalformedPacket("[MQTT-1.5.4-2] Null found in UTF-8 data")
if ord_c == 0xFEFF:
raise MalformedPacket("[MQTT-1.5.4-3] U+FEFF in UTF-8 data")
return buf, length+2
def writeBytes(buffer):
return writeInt16(len(buffer)) + buffer
def readBytes(buffer):
length = readInt16(buffer)
return buffer[2:2+length], length+2
class VariableByteIntegers: # Variable Byte Integer
"""
MQTT variable byte integer helper class. Used
in several places in MQTT v5.0 properties.
"""
@staticmethod
def encode(x):
"""
Convert an integer 0 <= x <= 268435455 into multi-byte format.
Returns the buffer converted from the integer.
"""
if not 0 <= x <= 268435455:
raise ValueError(f"Value {x!r} must be in range 0-268435455")
buffer = b''
while 1:
digit = x % 128
x //= 128
if x > 0:
digit |= 0x80
buffer += bytes([digit])
if x == 0:
break
return buffer
@staticmethod
def decode(buffer):
"""
Get the value of a multi-byte integer from a buffer
Return the value, and the number of bytes used.
[MQTT-1.5.5-1] the encoded value MUST use the minimum number of bytes necessary to represent the value
"""
multiplier = 1
value = 0
bytes = 0
while 1:
bytes += 1
digit = buffer[0]
buffer = buffer[1:]
value += (digit & 127) * multiplier
if digit & 128 == 0:
break
multiplier *= 128
return (value, bytes)
class Properties:
"""MQTT v5.0 properties class.
See Properties.names for a list of accepted property names along with their numeric values.
See Properties.properties for the data type of each property.
Example of use::
publish_properties = Properties(PacketTypes.PUBLISH)
publish_properties.UserProperty = ("a", "2")
publish_properties.UserProperty = ("c", "3")
First the object is created with packet type as argument, no properties will be present at
this point. Then properties are added as attributes, the name of which is the string property
name without the spaces.
"""
def __init__(self, packetType):
self.packetType = packetType
self.types = ["Byte", "Two Byte Integer", "Four Byte Integer", "Variable Byte Integer",
"Binary Data", "UTF-8 Encoded String", "UTF-8 String Pair"]
self.names = {
"Payload Format Indicator": 1,
"Message Expiry Interval": 2,
"Content Type": 3,
"Response Topic": 8,
"Correlation Data": 9,
"Subscription Identifier": 11,
"Session Expiry Interval": 17,
"Assigned Client Identifier": 18,
"Server Keep Alive": 19,
"Authentication Method": 21,
"Authentication Data": 22,
"Request Problem Information": 23,
"Will Delay Interval": 24,
"Request Response Information": 25,
"Response Information": 26,
"Server Reference": 28,
"Reason String": 31,
"Receive Maximum": 33,
"Topic Alias Maximum": 34,
"Topic Alias": 35,
"Maximum QoS": 36,
"Retain Available": 37,
"User Property": 38,
"Maximum Packet Size": 39,
"Wildcard Subscription Available": 40,
"Subscription Identifier Available": 41,
"Shared Subscription Available": 42
}
self.properties = {
# id: type, packets
# payload format indicator
1: (self.types.index("Byte"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]),
2: (self.types.index("Four Byte Integer"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]),
3: (self.types.index("UTF-8 Encoded String"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]),
8: (self.types.index("UTF-8 Encoded String"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]),
9: (self.types.index("Binary Data"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]),
11: (self.types.index("Variable Byte Integer"),
[PacketTypes.PUBLISH, PacketTypes.SUBSCRIBE]),
17: (self.types.index("Four Byte Integer"),
[PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.DISCONNECT]),
18: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK]),
19: (self.types.index("Two Byte Integer"), [PacketTypes.CONNACK]),
21: (self.types.index("UTF-8 Encoded String"),
[PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.AUTH]),
22: (self.types.index("Binary Data"),
[PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.AUTH]),
23: (self.types.index("Byte"),
[PacketTypes.CONNECT]),
24: (self.types.index("Four Byte Integer"), [PacketTypes.WILLMESSAGE]),
25: (self.types.index("Byte"), [PacketTypes.CONNECT]),
26: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK]),
28: (self.types.index("UTF-8 Encoded String"),
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]),
31: (self.types.index("UTF-8 Encoded String"),
[PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC,
PacketTypes.PUBREL, PacketTypes.PUBCOMP, PacketTypes.SUBACK,
PacketTypes.UNSUBACK, PacketTypes.DISCONNECT, PacketTypes.AUTH]),
33: (self.types.index("Two Byte Integer"),
[PacketTypes.CONNECT, PacketTypes.CONNACK]),
34: (self.types.index("Two Byte Integer"),
[PacketTypes.CONNECT, PacketTypes.CONNACK]),
35: (self.types.index("Two Byte Integer"), [PacketTypes.PUBLISH]),
36: (self.types.index("Byte"), [PacketTypes.CONNACK]),
37: (self.types.index("Byte"), [PacketTypes.CONNACK]),
38: (self.types.index("UTF-8 String Pair"),
[PacketTypes.CONNECT, PacketTypes.CONNACK,
PacketTypes.PUBLISH, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP,
PacketTypes.SUBSCRIBE, PacketTypes.SUBACK,
PacketTypes.UNSUBSCRIBE, PacketTypes.UNSUBACK,
PacketTypes.DISCONNECT, PacketTypes.AUTH, PacketTypes.WILLMESSAGE]),
39: (self.types.index("Four Byte Integer"),
[PacketTypes.CONNECT, PacketTypes.CONNACK]),
40: (self.types.index("Byte"), [PacketTypes.CONNACK]),
41: (self.types.index("Byte"), [PacketTypes.CONNACK]),
42: (self.types.index("Byte"), [PacketTypes.CONNACK]),
}
def allowsMultiple(self, compressedName):
return self.getIdentFromName(compressedName) in [11, 38]
def getIdentFromName(self, compressedName):
# return the identifier corresponding to the property name
result = -1
for name in self.names.keys():
if compressedName == name.replace(' ', ''):
result = self.names[name]
break
return result
def __setattr__(self, name, value):
name = name.replace(' ', '')
privateVars = ["packetType", "types", "names", "properties"]
if name in privateVars:
object.__setattr__(self, name, value)
else:
# the name could have spaces in, or not. Remove spaces before assignment
if name not in [aname.replace(' ', '') for aname in self.names.keys()]:
raise MQTTException(
f"Property name must be one of {self.names.keys()}")
# check that this attribute applies to the packet type
if self.packetType not in self.properties[self.getIdentFromName(name)][1]:
raise MQTTException(f"Property {name} does not apply to packet type {PacketTypes.Names[self.packetType]}")
# Check for forbidden values
if not isinstance(value, list):
if name in ["ReceiveMaximum", "TopicAlias"] \
and (value < 1 or value > 65535):
raise MQTTException(f"{name} property value must be in the range 1-65535")
elif name in ["TopicAliasMaximum"] \
and (value < 0 or value > 65535):
raise MQTTException(f"{name} property value must be in the range 0-65535")
elif name in ["MaximumPacketSize", "SubscriptionIdentifier"] \
and (value < 1 or value > 268435455):
raise MQTTException(f"{name} property value must be in the range 1-268435455")
elif name in ["RequestResponseInformation", "RequestProblemInformation", "PayloadFormatIndicator"] \
and (value != 0 and value != 1):
raise MQTTException(
f"{name} property value must be 0 or 1")
if self.allowsMultiple(name):
if not isinstance(value, list):
value = [value]
if hasattr(self, name):
value = object.__getattribute__(self, name) + value
object.__setattr__(self, name, value)
def __str__(self):
buffer = "["
first = True
for name in self.names.keys():
compressedName = name.replace(' ', '')
if hasattr(self, compressedName):
if not first:
buffer += ", "
buffer += f"{compressedName} : {getattr(self, compressedName)}"
first = False
buffer += "]"
return buffer
def json(self):
data = {}
for name in self.names.keys():
compressedName = name.replace(' ', '')
if hasattr(self, compressedName):
val = getattr(self, compressedName)
if compressedName == 'CorrelationData' and isinstance(val, bytes):
data[compressedName] = val.hex()
else:
data[compressedName] = val
return data
def isEmpty(self):
rc = True
for name in self.names.keys():
compressedName = name.replace(' ', '')
if hasattr(self, compressedName):
rc = False
break
return rc
def clear(self):
for name in self.names.keys():
compressedName = name.replace(' ', '')
if hasattr(self, compressedName):
delattr(self, compressedName)
def writeProperty(self, identifier, type, value):
buffer = b""
buffer += VariableByteIntegers.encode(identifier) # identifier
if type == self.types.index("Byte"): # value
buffer += bytes([value])
elif type == self.types.index("Two Byte Integer"):
buffer += writeInt16(value)
elif type == self.types.index("Four Byte Integer"):
buffer += writeInt32(value)
elif type == self.types.index("Variable Byte Integer"):
buffer += VariableByteIntegers.encode(value)
elif type == self.types.index("Binary Data"):
buffer += writeBytes(value)
elif type == self.types.index("UTF-8 Encoded String"):
buffer += writeUTF(value)
elif type == self.types.index("UTF-8 String Pair"):
buffer += writeUTF(value[0]) + writeUTF(value[1])
return buffer
def pack(self):
# serialize properties into buffer for sending over network
buffer = b""
for name in self.names.keys():
compressedName = name.replace(' ', '')
if hasattr(self, compressedName):
identifier = self.getIdentFromName(compressedName)
attr_type = self.properties[identifier][0]
if self.allowsMultiple(compressedName):
for prop in getattr(self, compressedName):
buffer += self.writeProperty(identifier,
attr_type, prop)
else:
buffer += self.writeProperty(identifier, attr_type,
getattr(self, compressedName))
return VariableByteIntegers.encode(len(buffer)) + buffer
def readProperty(self, buffer, type, propslen):
if type == self.types.index("Byte"):
value = buffer[0]
valuelen = 1
elif type == self.types.index("Two Byte Integer"):
value = readInt16(buffer)
valuelen = 2
elif type == self.types.index("Four Byte Integer"):
value = readInt32(buffer)
valuelen = 4
elif type == self.types.index("Variable Byte Integer"):
value, valuelen = VariableByteIntegers.decode(buffer)
elif type == self.types.index("Binary Data"):
value, valuelen = readBytes(buffer)
elif type == self.types.index("UTF-8 Encoded String"):
value, valuelen = readUTF(buffer, propslen)
elif type == self.types.index("UTF-8 String Pair"):
value, valuelen = readUTF(buffer, propslen)
buffer = buffer[valuelen:] # strip the bytes used by the value
value1, valuelen1 = readUTF(buffer, propslen - valuelen)
value = (value, value1)
valuelen += valuelen1
return value, valuelen
def getNameFromIdent(self, identifier):
rc = None
for name in self.names:
if self.names[name] == identifier:
rc = name
return rc
def unpack(self, buffer):
self.clear()
# deserialize properties into attributes from buffer received from network
propslen, VBIlen = VariableByteIntegers.decode(buffer)
buffer = buffer[VBIlen:] # strip the bytes used by the VBI
propslenleft = propslen
while propslenleft > 0: # properties length is 0 if there are none
identifier, VBIlen2 = VariableByteIntegers.decode(
buffer) # property identifier
buffer = buffer[VBIlen2:] # strip the bytes used by the VBI
propslenleft -= VBIlen2
attr_type = self.properties[identifier][0]
value, valuelen = self.readProperty(
buffer, attr_type, propslenleft)
buffer = buffer[valuelen:] # strip the bytes used by the value
propslenleft -= valuelen
propname = self.getNameFromIdent(identifier)
compressedName = propname.replace(' ', '')
if not self.allowsMultiple(compressedName) and hasattr(self, compressedName):
raise MQTTException(
f"Property '{property}' must not exist more than once")
setattr(self, propname, value)
return self, propslen + VBIlen

306
sbapp/pmqtt/publish.py Normal file
View File

@ -0,0 +1,306 @@
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v20.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Roger Light - initial API and implementation
"""
This module provides some helper functions to allow straightforward publishing
of messages in a one-shot manner. In other words, they are useful for the
situation where you have a single/multiple messages you want to publish to a
broker, then disconnect and nothing else is required.
"""
from __future__ import annotations
import collections
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, List, Tuple, Union
from .enums import CallbackAPIVersion, MQTTProtocolVersion
from .properties import Properties
from .reasoncodes import ReasonCode
from .. import mqtt
from . import client as paho
if TYPE_CHECKING:
try:
from typing import NotRequired, Required, TypedDict # type: ignore
except ImportError:
from typing_extensions import NotRequired, Required, TypedDict
try:
from typing import Literal
except ImportError:
from typing_extensions import Literal # type: ignore
class AuthParameter(TypedDict, total=False):
username: Required[str]
password: NotRequired[str]
class TLSParameter(TypedDict, total=False):
ca_certs: Required[str]
certfile: NotRequired[str]
keyfile: NotRequired[str]
tls_version: NotRequired[int]
ciphers: NotRequired[str]
insecure: NotRequired[bool]
class MessageDict(TypedDict, total=False):
topic: Required[str]
payload: NotRequired[paho.PayloadType]
qos: NotRequired[int]
retain: NotRequired[bool]
MessageTuple = Tuple[str, paho.PayloadType, int, bool]
MessagesList = List[Union[MessageDict, MessageTuple]]
def _do_publish(client: paho.Client):
"""Internal function"""
message = client._userdata.popleft()
if isinstance(message, dict):
client.publish(**message)
elif isinstance(message, (tuple, list)):
client.publish(*message)
else:
raise TypeError('message must be a dict, tuple, or list')
def _on_connect(client: paho.Client, userdata: MessagesList, flags, reason_code, properties):
"""Internal v5 callback"""
if reason_code == 0:
if len(userdata) > 0:
_do_publish(client)
else:
raise mqtt.MQTTException(paho.connack_string(reason_code))
def _on_publish(
client: paho.Client, userdata: collections.deque[MessagesList], mid: int, reason_codes: ReasonCode, properties: Properties,
) -> None:
"""Internal callback"""
#pylint: disable=unused-argument
if len(userdata) == 0:
client.disconnect()
else:
_do_publish(client)
def multiple(
msgs: MessagesList,
hostname: str = "localhost",
port: int = 1883,
client_id: str = "",
keepalive: int = 60,
will: MessageDict | None = None,
auth: AuthParameter | None = None,
tls: TLSParameter | None = None,
protocol: MQTTProtocolVersion = paho.MQTTv311,
transport: Literal["tcp", "websockets"] = "tcp",
proxy_args: Any | None = None,
) -> None:
"""Publish multiple messages to a broker, then disconnect cleanly.
This function creates an MQTT client, connects to a broker and publishes a
list of messages. Once the messages have been delivered, it disconnects
cleanly from the broker.
:param msgs: a list of messages to publish. Each message is either a dict or a
tuple.
If a dict, only the topic must be present. Default values will be
used for any missing arguments. The dict must be of the form:
msg = {'topic':"<topic>", 'payload':"<payload>", 'qos':<qos>,
'retain':<retain>}
topic must be present and may not be empty.
If payload is "", None or not present then a zero length payload
will be published.
If qos is not present, the default of 0 is used.
If retain is not present, the default of False is used.
If a tuple, then it must be of the form:
("<topic>", "<payload>", qos, retain)
:param str hostname: the address of the broker to connect to.
Defaults to localhost.
:param int port: the port to connect to the broker on. Defaults to 1883.
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
generate a client id automatically.
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
seconds.
:param will: a dict containing will parameters for the client: will = {'topic':
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
Topic is required, all other parameters are optional and will
default to None, 0 and False respectively.
Defaults to None, which indicates no will should be used.
:param auth: a dict containing authentication parameters for the client:
auth = {'username':"<username>", 'password':"<password>"}
Username is required, password is optional and will default to None
if not provided.
Defaults to None, which indicates no authentication is to be used.
:param tls: a dict containing TLS configuration parameters for the client:
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
'ciphers':"<ciphers">, 'insecure':"<bool>"}
ca_certs is required, all other parameters are optional and will
default to None if not provided, which results in the client using
the default behaviour - see the paho.mqtt.client documentation.
Alternatively, tls input can be an SSLContext object, which will be
processed using the tls_set_context method.
Defaults to None, which indicates that TLS should not be used.
:param str transport: set to "tcp" to use the default setting of transport which is
raw TCP. Set to "websockets" to use WebSockets as the transport.
:param proxy_args: a dictionary that will be given to the client.
"""
if not isinstance(msgs, Iterable):
raise TypeError('msgs must be an iterable')
if len(msgs) == 0:
raise ValueError('msgs is empty')
client = paho.Client(
CallbackAPIVersion.VERSION2,
client_id=client_id,
userdata=collections.deque(msgs),
protocol=protocol,
transport=transport,
)
client.enable_logger()
client.on_publish = _on_publish
client.on_connect = _on_connect # type: ignore
if proxy_args is not None:
client.proxy_set(**proxy_args)
if auth:
username = auth.get('username')
if username:
password = auth.get('password')
client.username_pw_set(username, password)
else:
raise KeyError("The 'username' key was not found, this is "
"required for auth")
if will is not None:
client.will_set(**will)
if tls is not None:
if isinstance(tls, dict):
insecure = tls.pop('insecure', False)
# mypy don't get that tls no longer contains the key insecure
client.tls_set(**tls) # type: ignore[misc]
if insecure:
# Must be set *after* the `client.tls_set()` call since it sets
# up the SSL context that `client.tls_insecure_set` alters.
client.tls_insecure_set(insecure)
else:
# Assume input is SSLContext object
client.tls_set_context(tls)
client.connect(hostname, port, keepalive)
client.loop_forever()
def single(
topic: str,
payload: paho.PayloadType = None,
qos: int = 0,
retain: bool = False,
hostname: str = "localhost",
port: int = 1883,
client_id: str = "",
keepalive: int = 60,
will: MessageDict | None = None,
auth: AuthParameter | None = None,
tls: TLSParameter | None = None,
protocol: MQTTProtocolVersion = paho.MQTTv311,
transport: Literal["tcp", "websockets"] = "tcp",
proxy_args: Any | None = None,
) -> None:
"""Publish a single message to a broker, then disconnect cleanly.
This function creates an MQTT client, connects to a broker and publishes a
single message. Once the message has been delivered, it disconnects cleanly
from the broker.
:param str topic: the only required argument must be the topic string to which the
payload will be published.
:param payload: the payload to be published. If "" or None, a zero length payload
will be published.
:param int qos: the qos to use when publishing, default to 0.
:param bool retain: set the message to be retained (True) or not (False).
:param str hostname: the address of the broker to connect to.
Defaults to localhost.
:param int port: the port to connect to the broker on. Defaults to 1883.
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
generate a client id automatically.
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
seconds.
:param will: a dict containing will parameters for the client: will = {'topic':
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
Topic is required, all other parameters are optional and will
default to None, 0 and False respectively.
Defaults to None, which indicates no will should be used.
:param auth: a dict containing authentication parameters for the client:
Username is required, password is optional and will default to None
auth = {'username':"<username>", 'password':"<password>"}
if not provided.
Defaults to None, which indicates no authentication is to be used.
:param tls: a dict containing TLS configuration parameters for the client:
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
'ciphers':"<ciphers">, 'insecure':"<bool>"}
ca_certs is required, all other parameters are optional and will
default to None if not provided, which results in the client using
the default behaviour - see the paho.mqtt.client documentation.
Defaults to None, which indicates that TLS should not be used.
Alternatively, tls input can be an SSLContext object, which will be
processed using the tls_set_context method.
:param transport: set to "tcp" to use the default setting of transport which is
raw TCP. Set to "websockets" to use WebSockets as the transport.
:param proxy_args: a dictionary that will be given to the client.
"""
msg: MessageDict = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain}
multiple([msg], hostname, port, client_id, keepalive, will, auth, tls,
protocol, transport, proxy_args)

0
sbapp/pmqtt/py.typed Normal file
View File

223
sbapp/pmqtt/reasoncodes.py Normal file
View File

@ -0,0 +1,223 @@
# *******************************************************************
# Copyright (c) 2017, 2019 IBM Corp.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v20.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Ian Craggs - initial implementation and/or documentation
# *******************************************************************
import functools
import warnings
from typing import Any
from .packettypes import PacketTypes
@functools.total_ordering
class ReasonCode:
"""MQTT version 5.0 reason codes class.
See ReasonCode.names for a list of possible numeric values along with their
names and the packets to which they apply.
"""
def __init__(self, packetType: int, aName: str ="Success", identifier: int =-1):
"""
packetType: the type of the packet, such as PacketTypes.CONNECT that
this reason code will be used with. Some reason codes have different
names for the same identifier when used a different packet type.
aName: the String name of the reason code to be created. Ignored
if the identifier is set.
identifier: an integer value of the reason code to be created.
"""
self.packetType = packetType
self.names = {
0: {"Success": [PacketTypes.CONNACK, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP,
PacketTypes.UNSUBACK, PacketTypes.AUTH],
"Normal disconnection": [PacketTypes.DISCONNECT],
"Granted QoS 0": [PacketTypes.SUBACK]},
1: {"Granted QoS 1": [PacketTypes.SUBACK]},
2: {"Granted QoS 2": [PacketTypes.SUBACK]},
4: {"Disconnect with will message": [PacketTypes.DISCONNECT]},
16: {"No matching subscribers":
[PacketTypes.PUBACK, PacketTypes.PUBREC]},
17: {"No subscription found": [PacketTypes.UNSUBACK]},
24: {"Continue authentication": [PacketTypes.AUTH]},
25: {"Re-authenticate": [PacketTypes.AUTH]},
128: {"Unspecified error": [PacketTypes.CONNACK, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK,
PacketTypes.DISCONNECT], },
129: {"Malformed packet":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
130: {"Protocol error":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
131: {"Implementation specific error": [PacketTypes.CONNACK,
PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK,
PacketTypes.UNSUBACK, PacketTypes.DISCONNECT], },
132: {"Unsupported protocol version": [PacketTypes.CONNACK]},
133: {"Client identifier not valid": [PacketTypes.CONNACK]},
134: {"Bad user name or password": [PacketTypes.CONNACK]},
135: {"Not authorized": [PacketTypes.CONNACK, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK,
PacketTypes.DISCONNECT], },
136: {"Server unavailable": [PacketTypes.CONNACK]},
137: {"Server busy": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
138: {"Banned": [PacketTypes.CONNACK]},
139: {"Server shutting down": [PacketTypes.DISCONNECT]},
140: {"Bad authentication method":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
141: {"Keep alive timeout": [PacketTypes.DISCONNECT]},
142: {"Session taken over": [PacketTypes.DISCONNECT]},
143: {"Topic filter invalid":
[PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT]},
144: {"Topic name invalid":
[PacketTypes.CONNACK, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
145: {"Packet identifier in use":
[PacketTypes.PUBACK, PacketTypes.PUBREC,
PacketTypes.SUBACK, PacketTypes.UNSUBACK]},
146: {"Packet identifier not found":
[PacketTypes.PUBREL, PacketTypes.PUBCOMP]},
147: {"Receive maximum exceeded": [PacketTypes.DISCONNECT]},
148: {"Topic alias invalid": [PacketTypes.DISCONNECT]},
149: {"Packet too large": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
150: {"Message rate too high": [PacketTypes.DISCONNECT]},
151: {"Quota exceeded": [PacketTypes.CONNACK, PacketTypes.PUBACK,
PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.DISCONNECT], },
152: {"Administrative action": [PacketTypes.DISCONNECT]},
153: {"Payload format invalid":
[PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]},
154: {"Retain not supported":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
155: {"QoS not supported":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
156: {"Use another server":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
157: {"Server moved":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
158: {"Shared subscription not supported":
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
159: {"Connection rate exceeded":
[PacketTypes.CONNACK, PacketTypes.DISCONNECT]},
160: {"Maximum connect time":
[PacketTypes.DISCONNECT]},
161: {"Subscription identifiers not supported":
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
162: {"Wildcard subscription not supported":
[PacketTypes.SUBACK, PacketTypes.DISCONNECT]},
}
if identifier == -1:
if packetType == PacketTypes.DISCONNECT and aName == "Success":
aName = "Normal disconnection"
self.set(aName)
else:
self.value = identifier
self.getName() # check it's good
def __getName__(self, packetType, identifier):
"""
Get the reason code string name for a specific identifier.
The name can vary by packet type for the same identifier, which
is why the packet type is also required.
Used when displaying the reason code.
"""
if identifier not in self.names:
raise KeyError(identifier)
names = self.names[identifier]
namelist = [name for name in names.keys() if packetType in names[name]]
if len(namelist) != 1:
raise ValueError(f"Expected exactly one name, found {namelist!r}")
return namelist[0]
def getId(self, name):
"""
Get the numeric id corresponding to a reason code name.
Used when setting the reason code for a packetType
check that only valid codes for the packet are set.
"""
for code in self.names.keys():
if name in self.names[code].keys():
if self.packetType in self.names[code][name]:
return code
raise KeyError(f"Reason code name not found: {name}")
def set(self, name):
self.value = self.getId(name)
def unpack(self, buffer):
c = buffer[0]
name = self.__getName__(self.packetType, c)
self.value = self.getId(name)
return 1
def getName(self):
"""Returns the reason code name corresponding to the numeric value which is set.
"""
return self.__getName__(self.packetType, self.value)
def __eq__(self, other):
if isinstance(other, int):
return self.value == other
if isinstance(other, str):
return other == str(self)
if isinstance(other, ReasonCode):
return self.value == other.value
return False
def __lt__(self, other):
if isinstance(other, int):
return self.value < other
if isinstance(other, ReasonCode):
return self.value < other.value
return NotImplemented
def __repr__(self):
try:
packet_name = PacketTypes.Names[self.packetType]
except IndexError:
packet_name = "Unknown"
return f"ReasonCode({packet_name}, {self.getName()!r})"
def __str__(self):
return self.getName()
def json(self):
return self.getName()
def pack(self):
return bytearray([self.value])
@property
def is_failure(self) -> bool:
return self.value >= 0x80
class _CompatibilityIsInstance(type):
def __instancecheck__(self, other: Any) -> bool:
return isinstance(other, ReasonCode)
class ReasonCodes(ReasonCode, metaclass=_CompatibilityIsInstance):
def __init__(self, *args, **kwargs):
warnings.warn("ReasonCodes is deprecated, use ReasonCode (singular) instead",
category=DeprecationWarning,
stacklevel=2,
)
super().__init__(*args, **kwargs)

281
sbapp/pmqtt/subscribe.py Normal file
View File

@ -0,0 +1,281 @@
# Copyright (c) 2016 Roger Light <roger@atchoo.org>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v20.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Roger Light - initial API and implementation
"""
This module provides some helper functions to allow straightforward subscribing
to topics and retrieving messages. The two functions are simple(), which
returns one or messages matching a set of topics, and callback() which allows
you to pass a callback for processing of messages.
"""
from .. import mqtt
from . import client as paho
def _on_connect(client, userdata, flags, reason_code, properties):
"""Internal callback"""
if reason_code != 0:
raise mqtt.MQTTException(paho.connack_string(reason_code))
if isinstance(userdata['topics'], list):
for topic in userdata['topics']:
client.subscribe(topic, userdata['qos'])
else:
client.subscribe(userdata['topics'], userdata['qos'])
def _on_message_callback(client, userdata, message):
"""Internal callback"""
userdata['callback'](client, userdata['userdata'], message)
def _on_message_simple(client, userdata, message):
"""Internal callback"""
if userdata['msg_count'] == 0:
return
# Don't process stale retained messages if 'retained' was false
if message.retain and not userdata['retained']:
return
userdata['msg_count'] = userdata['msg_count'] - 1
if userdata['messages'] is None and userdata['msg_count'] == 0:
userdata['messages'] = message
client.disconnect()
return
userdata['messages'].append(message)
if userdata['msg_count'] == 0:
client.disconnect()
def callback(callback, topics, qos=0, userdata=None, hostname="localhost",
port=1883, client_id="", keepalive=60, will=None, auth=None,
tls=None, protocol=paho.MQTTv311, transport="tcp",
clean_session=True, proxy_args=None):
"""Subscribe to a list of topics and process them in a callback function.
This function creates an MQTT client, connects to a broker and subscribes
to a list of topics. Incoming messages are processed by the user provided
callback. This is a blocking function and will never return.
:param callback: function with the same signature as `on_message` for
processing the messages received.
:param topics: either a string containing a single topic to subscribe to, or a
list of topics to subscribe to.
:param int qos: the qos to use when subscribing. This is applied to all topics.
:param userdata: passed to the callback
:param str hostname: the address of the broker to connect to.
Defaults to localhost.
:param int port: the port to connect to the broker on. Defaults to 1883.
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
generate a client id automatically.
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
seconds.
:param will: a dict containing will parameters for the client: will = {'topic':
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
Topic is required, all other parameters are optional and will
default to None, 0 and False respectively.
Defaults to None, which indicates no will should be used.
:param auth: a dict containing authentication parameters for the client:
auth = {'username':"<username>", 'password':"<password>"}
Username is required, password is optional and will default to None
if not provided.
Defaults to None, which indicates no authentication is to be used.
:param tls: a dict containing TLS configuration parameters for the client:
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
'ciphers':"<ciphers">, 'insecure':"<bool>"}
ca_certs is required, all other parameters are optional and will
default to None if not provided, which results in the client using
the default behaviour - see the paho.mqtt.client documentation.
Alternatively, tls input can be an SSLContext object, which will be
processed using the tls_set_context method.
Defaults to None, which indicates that TLS should not be used.
:param str transport: set to "tcp" to use the default setting of transport which is
raw TCP. Set to "websockets" to use WebSockets as the transport.
:param clean_session: a boolean that determines the client type. If True,
the broker will remove all information about this client
when it disconnects. If False, the client is a persistent
client and subscription information and queued messages
will be retained when the client disconnects.
Defaults to True.
:param proxy_args: a dictionary that will be given to the client.
"""
if qos < 0 or qos > 2:
raise ValueError('qos must be in the range 0-2')
callback_userdata = {
'callback':callback,
'topics':topics,
'qos':qos,
'userdata':userdata}
client = paho.Client(
paho.CallbackAPIVersion.VERSION2,
client_id=client_id,
userdata=callback_userdata,
protocol=protocol,
transport=transport,
clean_session=clean_session,
)
client.enable_logger()
client.on_message = _on_message_callback
client.on_connect = _on_connect
if proxy_args is not None:
client.proxy_set(**proxy_args)
if auth:
username = auth.get('username')
if username:
password = auth.get('password')
client.username_pw_set(username, password)
else:
raise KeyError("The 'username' key was not found, this is "
"required for auth")
if will is not None:
client.will_set(**will)
if tls is not None:
if isinstance(tls, dict):
insecure = tls.pop('insecure', False)
client.tls_set(**tls)
if insecure:
# Must be set *after* the `client.tls_set()` call since it sets
# up the SSL context that `client.tls_insecure_set` alters.
client.tls_insecure_set(insecure)
else:
# Assume input is SSLContext object
client.tls_set_context(tls)
client.connect(hostname, port, keepalive)
client.loop_forever()
def simple(topics, qos=0, msg_count=1, retained=True, hostname="localhost",
port=1883, client_id="", keepalive=60, will=None, auth=None,
tls=None, protocol=paho.MQTTv311, transport="tcp",
clean_session=True, proxy_args=None):
"""Subscribe to a list of topics and return msg_count messages.
This function creates an MQTT client, connects to a broker and subscribes
to a list of topics. Once "msg_count" messages have been received, it
disconnects cleanly from the broker and returns the messages.
:param topics: either a string containing a single topic to subscribe to, or a
list of topics to subscribe to.
:param int qos: the qos to use when subscribing. This is applied to all topics.
:param int msg_count: the number of messages to retrieve from the broker.
if msg_count == 1 then a single MQTTMessage will be returned.
if msg_count > 1 then a list of MQTTMessages will be returned.
:param bool retained: If set to True, retained messages will be processed the same as
non-retained messages. If set to False, retained messages will
be ignored. This means that with retained=False and msg_count=1,
the function will return the first message received that does
not have the retained flag set.
:param str hostname: the address of the broker to connect to.
Defaults to localhost.
:param int port: the port to connect to the broker on. Defaults to 1883.
:param str client_id: the MQTT client id to use. If "" or None, the Paho library will
generate a client id automatically.
:param int keepalive: the keepalive timeout value for the client. Defaults to 60
seconds.
:param will: a dict containing will parameters for the client: will = {'topic':
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
Topic is required, all other parameters are optional and will
default to None, 0 and False respectively.
Defaults to None, which indicates no will should be used.
:param auth: a dict containing authentication parameters for the client:
auth = {'username':"<username>", 'password':"<password>"}
Username is required, password is optional and will default to None
if not provided.
Defaults to None, which indicates no authentication is to be used.
:param tls: a dict containing TLS configuration parameters for the client:
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
'ciphers':"<ciphers">, 'insecure':"<bool>"}
ca_certs is required, all other parameters are optional and will
default to None if not provided, which results in the client using
the default behaviour - see the paho.mqtt.client documentation.
Alternatively, tls input can be an SSLContext object, which will be
processed using the tls_set_context method.
Defaults to None, which indicates that TLS should not be used.
:param protocol: the MQTT protocol version to use. Defaults to MQTTv311.
:param transport: set to "tcp" to use the default setting of transport which is
raw TCP. Set to "websockets" to use WebSockets as the transport.
:param clean_session: a boolean that determines the client type. If True,
the broker will remove all information about this client
when it disconnects. If False, the client is a persistent
client and subscription information and queued messages
will be retained when the client disconnects.
Defaults to True. If protocol is MQTTv50, clean_session
is ignored.
:param proxy_args: a dictionary that will be given to the client.
"""
if msg_count < 1:
raise ValueError('msg_count must be > 0')
# Set ourselves up to return a single message if msg_count == 1, or a list
# if > 1.
if msg_count == 1:
messages = None
else:
messages = []
# Ignore clean_session if protocol is MQTTv50, otherwise Client will raise
if protocol == paho.MQTTv5:
clean_session = None
userdata = {'retained':retained, 'msg_count':msg_count, 'messages':messages}
callback(_on_message_simple, topics, qos, userdata, hostname, port,
client_id, keepalive, will, auth, tls, protocol, transport,
clean_session, proxy_args)
return userdata['messages']

View File

@ -0,0 +1,113 @@
"""
*******************************************************************
Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v20.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Ian Craggs - initial implementation and/or documentation
*******************************************************************
"""
class MQTTException(Exception):
pass
class SubscribeOptions:
"""The MQTT v5.0 subscribe options class.
The options are:
qos: As in MQTT v3.1.1.
noLocal: True or False. If set to True, the subscriber will not receive its own publications.
retainAsPublished: True or False. If set to True, the retain flag on received publications will be as set
by the publisher.
retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
Controls when the broker should send retained messages:
- RETAIN_SEND_ON_SUBSCRIBE: on any successful subscribe request
- RETAIN_SEND_IF_NEW_SUB: only if the subscribe request is new
- RETAIN_DO_NOT_SEND: never send retained messages
"""
# retain handling options
RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range(
0, 3)
def __init__(
self,
qos: int = 0,
noLocal: bool = False,
retainAsPublished: bool = False,
retainHandling: int = RETAIN_SEND_ON_SUBSCRIBE,
):
"""
qos: 0, 1 or 2. 0 is the default.
noLocal: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
retainAsPublished: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
RETAIN_SEND_ON_SUBSCRIBE is the default and corresponds to MQTT v3.1.1 behavior.
"""
object.__setattr__(self, "names",
["QoS", "noLocal", "retainAsPublished", "retainHandling"])
self.QoS = qos # bits 0,1
self.noLocal = noLocal # bit 2
self.retainAsPublished = retainAsPublished # bit 3
self.retainHandling = retainHandling # bits 4 and 5: 0, 1 or 2
if self.retainHandling not in (0, 1, 2):
raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
if self.QoS not in (0, 1, 2):
raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
def __setattr__(self, name, value):
if name not in self.names:
raise MQTTException(
f"{name} Attribute name must be one of {self.names}")
object.__setattr__(self, name, value)
def pack(self):
if self.retainHandling not in (0, 1, 2):
raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
if self.QoS not in (0, 1, 2):
raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
noLocal = 1 if self.noLocal else 0
retainAsPublished = 1 if self.retainAsPublished else 0
data = [(self.retainHandling << 4) | (retainAsPublished << 3) |
(noLocal << 2) | self.QoS]
return bytes(data)
def unpack(self, buffer):
b0 = buffer[0]
self.retainHandling = ((b0 >> 4) & 0x03)
self.retainAsPublished = True if ((b0 >> 3) & 0x01) == 1 else False
self.noLocal = True if ((b0 >> 2) & 0x01) == 1 else False
self.QoS = (b0 & 0x03)
if self.retainHandling not in (0, 1, 2):
raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
if self.QoS not in (0, 1, 2):
raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
return 1
def __repr__(self):
return str(self)
def __str__(self):
return "{QoS="+str(self.QoS)+", noLocal="+str(self.noLocal) +\
", retainAsPublished="+str(self.retainAsPublished) +\
", retainHandling="+str(self.retainHandling)+"}"
def json(self):
data = {
"QoS": self.QoS,
"noLocal": self.noLocal,
"retainAsPublished": self.retainAsPublished,
"retainHandling": self.retainHandling,
}
return data

View File

@ -47,7 +47,11 @@ def get_key(key_path, force_reload=False):
return LOADED_KEY return LOADED_KEY
elif os.path.isfile(KEY_PATH): elif os.path.isfile(KEY_PATH):
with open(KEY_PATH, "rb") as f: with open(KEY_PATH, "rb") as f:
if cryptography_major_version > 3:
key = load_pem_private_key(f.read(), KEY_PASSPHRASE) key = load_pem_private_key(f.read(), KEY_PASSPHRASE)
else:
from cryptography.hazmat.backends import default_backend
key = load_pem_private_key(f.read(), KEY_PASSPHRASE, backend=default_backend())
else: else:
if cryptography_major_version > 3: if cryptography_major_version > 3:
key = ec.generate_private_key(curve=ec.SECP256R1()) key = ec.generate_private_key(curve=ec.SECP256R1())
@ -87,6 +91,7 @@ def gen_cert(cert_path, key):
cb = cb.not_valid_before(datetime.datetime.now(datetime.timezone.utc)+datetime.timedelta(days=-14)) cb = cb.not_valid_before(datetime.datetime.now(datetime.timezone.utc)+datetime.timedelta(days=-14))
cb = cb.not_valid_after(datetime.datetime.now(datetime.timezone.utc)+datetime.timedelta(days=3652)) cb = cb.not_valid_after(datetime.datetime.now(datetime.timezone.utc)+datetime.timedelta(days=3652))
cb = cb.add_extension(x509.SubjectAlternativeName([x509.DNSName("localhost")]), critical=False) cb = cb.add_extension(x509.SubjectAlternativeName([x509.DNSName("localhost")]), critical=False)
if cryptography_major_version > 3: if cryptography_major_version > 3:
cert = cb.sign(key, hashes.SHA256()) cert = cb.sign(key, hashes.SHA256())
else: else:

View File

@ -7,6 +7,7 @@ import struct
import sqlite3 import sqlite3
import random import random
import shlex import shlex
import re
import RNS.vendor.umsgpack as msgpack import RNS.vendor.umsgpack as msgpack
import RNS.Interfaces.Interface as Interface import RNS.Interfaces.Interface as Interface
@ -20,6 +21,7 @@ from collections import deque
from .res import sideband_fb_data from .res import sideband_fb_data
from .sense import Telemeter, Commands from .sense import Telemeter, Commands
from .plugins import SidebandCommandPlugin, SidebandServicePlugin, SidebandTelemetryPlugin from .plugins import SidebandCommandPlugin, SidebandServicePlugin, SidebandTelemetryPlugin
from .mqtt import MQTT
if RNS.vendor.platformutils.get_platform() == "android": if RNS.vendor.platformutils.get_platform() == "android":
import plyer import plyer
@ -64,11 +66,16 @@ class PropagationNodeDetector():
# age = 0 # age = 0
pass pass
link_stats = {"rssi": self.owner_app.sideband.reticulum.get_packet_rssi(announce_packet_hash), if self.owner_app != None:
"snr": self.owner_app.sideband.reticulum.get_packet_snr(announce_packet_hash), stat_endpoint = self.owner_app.sideband
"q": self.owner_app.sideband.reticulum.get_packet_q(announce_packet_hash)} else:
stat_endpoint = self.owner
RNS.log("Detected active propagation node "+RNS.prettyhexrep(destination_hash)+" emission "+str(age)+" seconds ago, "+str(hops)+" hops away") link_stats = {"rssi": stat_endpoint.reticulum.get_packet_rssi(announce_packet_hash),
"snr": stat_endpoint.reticulum.get_packet_snr(announce_packet_hash),
"q": stat_endpoint.reticulum.get_packet_q(announce_packet_hash)}
RNS.log("Detected active propagation node "+RNS.prettyhexrep(destination_hash)+" emission "+str(age)+" seconds ago, "+str(hops)+" hops away", RNS.LOG_EXTREME)
self.owner.log_announce(destination_hash, app_data, dest_type=PropagationNodeDetector.aspect_filter, link_stats=link_stats) self.owner.log_announce(destination_hash, app_data, dest_type=PropagationNodeDetector.aspect_filter, link_stats=link_stats)
if self.owner.config["lxmf_propagation_node"] == None: if self.owner.config["lxmf_propagation_node"] == None:
@ -84,10 +91,10 @@ class PropagationNodeDetector():
pass pass
else: else:
RNS.log(f"Received malformed propagation node announce from {RNS.prettyhexrep(destination_hash)} with data: {app_data}", RNS.LOG_DEBUG) RNS.log(f"Received malformed propagation node announce from {RNS.prettyhexrep(destination_hash)} with data: {app_data}", RNS.LOG_EXTREME)
else: else:
RNS.log(f"Received malformed propagation node announce from {RNS.prettyhexrep(destination_hash)} with data: {app_data}", RNS.LOG_DEBUG) RNS.log(f"Received malformed propagation node announce from {RNS.prettyhexrep(destination_hash)} with data: {app_data}", RNS.LOG_EXTREME)
except Exception as e: except Exception as e:
RNS.log("Error while processing received propagation node announce: "+str(e)) RNS.log("Error while processing received propagation node announce: "+str(e))
@ -106,8 +113,10 @@ class SidebandCore():
SERVICE_JOB_INTERVAL = 1 SERVICE_JOB_INTERVAL = 1
PERIODIC_JOBS_INTERVAL = 60 PERIODIC_JOBS_INTERVAL = 60
PERIODIC_SYNC_RETRY = 360 PERIODIC_SYNC_RETRY = 360
TELEMETRY_KEEP = 60*60*24*7
TELEMETRY_INTERVAL = 60 TELEMETRY_INTERVAL = 60
SERVICE_TELEMETRY_INTERVAL = 300 SERVICE_TELEMETRY_INTERVAL = 300
TELEMETRY_CLEAN_INTERVAL = 3600
IF_CHANGE_ANNOUNCE_MIN_INTERVAL = 3.5 # In seconds IF_CHANGE_ANNOUNCE_MIN_INTERVAL = 3.5 # In seconds
AUTO_ANNOUNCE_RANDOM_MIN = 90 # In minutes AUTO_ANNOUNCE_RANDOM_MIN = 90 # In minutes
@ -122,6 +131,7 @@ class SidebandCore():
# Add the announce to the directory announce # Add the announce to the directory announce
# stream logger # stream logger
if self.reticulum != None:
link_stats = {"rssi": self.reticulum.get_packet_rssi(announce_packet_hash), link_stats = {"rssi": self.reticulum.get_packet_rssi(announce_packet_hash),
"snr": self.reticulum.get_packet_snr(announce_packet_hash), "snr": self.reticulum.get_packet_snr(announce_packet_hash),
"q": self.reticulum.get_packet_q(announce_packet_hash)} "q": self.reticulum.get_packet_q(announce_packet_hash)}
@ -166,6 +176,8 @@ class SidebandCore():
self.pending_telemetry_send_try = 0 self.pending_telemetry_send_try = 0
self.pending_telemetry_send_maxtries = 2 self.pending_telemetry_send_maxtries = 2
self.telemetry_send_blocked_until = 0 self.telemetry_send_blocked_until = 0
self.telemetry_clean_interval = self.TELEMETRY_CLEAN_INTERVAL
self.last_telemetry_clean = 0
self.pending_telemetry_request = False self.pending_telemetry_request = False
self.telemetry_request_max_history = 7*24*60*60 self.telemetry_request_max_history = 7*24*60*60
self.live_tracked_objects = {} self.live_tracked_objects = {}
@ -181,6 +193,7 @@ class SidebandCore():
self.allow_service_dispatch = True self.allow_service_dispatch = True
self.version_str = "" self.version_str = ""
self.config_template = rns_config self.config_template = rns_config
self.default_config_template = rns_config
if config_path == None: if config_path == None:
self.app_dir = plyer.storagepath.get_home_dir()+"/.config/occ" self.app_dir = plyer.storagepath.get_home_dir()+"/.config/occ"
@ -251,6 +264,8 @@ class SidebandCore():
self.webshare_ssl_key_path = self.app_dir+"/app_storage/ssl_key.pem" self.webshare_ssl_key_path = self.app_dir+"/app_storage/ssl_key.pem"
self.webshare_ssl_cert_path = self.app_dir+"/app_storage/ssl_cert.pem" self.webshare_ssl_cert_path = self.app_dir+"/app_storage/ssl_cert.pem"
self.mqtt = None
self.first_run = True self.first_run = True
self.saving_configuration = False self.saving_configuration = False
self.last_lxmf_announce = 0 self.last_lxmf_announce = 0
@ -449,6 +464,7 @@ class SidebandCore():
self.config["eink_mode"] = True self.config["eink_mode"] = True
self.config["lxm_limit_1mb"] = True self.config["lxm_limit_1mb"] = True
self.config["trusted_markup_only"] = False self.config["trusted_markup_only"] = False
self.config["compose_in_markdown"] = False
# Connectivity # Connectivity
self.config["connect_transport"] = False self.config["connect_transport"] = False
@ -644,6 +660,8 @@ class SidebandCore():
self.config["hq_ptt"] = False self.config["hq_ptt"] = False
if not "trusted_markup_only" in self.config: if not "trusted_markup_only" in self.config:
self.config["trusted_markup_only"] = False self.config["trusted_markup_only"] = False
if not "compose_in_markdown" in self.config:
self.config["compose_in_markdown"] = False
if not "input_language" in self.config: if not "input_language" in self.config:
self.config["input_language"] = None self.config["input_language"] = None
@ -781,6 +799,18 @@ class SidebandCore():
self.config["telemetry_request_interval"] = 43200 self.config["telemetry_request_interval"] = 43200
if not "telemetry_collector_enabled" in self.config: if not "telemetry_collector_enabled" in self.config:
self.config["telemetry_collector_enabled"] = False self.config["telemetry_collector_enabled"] = False
if not "telemetry_to_mqtt" in self.config:
self.config["telemetry_to_mqtt"] = False
if not "telemetry_mqtt_host" in self.config:
self.config["telemetry_mqtt_host"] = None
if not "telemetry_mqtt_port" in self.config:
self.config["telemetry_mqtt_port"] = None
if not "telemetry_mqtt_user" in self.config:
self.config["telemetry_mqtt_user"] = None
if not "telemetry_mqtt_pass" in self.config:
self.config["telemetry_mqtt_pass"] = None
if not "telemetry_mqtt_validate_ssl" in self.config:
self.config["telemetry_mqtt_validate_ssl"] = False
if not "telemetry_icon" in self.config: if not "telemetry_icon" in self.config:
self.config["telemetry_icon"] = SidebandCore.DEFAULT_APPEARANCE[0] self.config["telemetry_icon"] = SidebandCore.DEFAULT_APPEARANCE[0]
@ -832,6 +862,8 @@ class SidebandCore():
self.config["telemetry_s_acceleration"] = False self.config["telemetry_s_acceleration"] = False
if not "telemetry_s_proximity" in self.config: if not "telemetry_s_proximity" in self.config:
self.config["telemetry_s_proximity"] = False self.config["telemetry_s_proximity"] = False
if not "telemetry_s_rns_transport" in self.config:
self.config["telemetry_s_rns_transport"] = False
if not "telemetry_s_fixed_location" in self.config: if not "telemetry_s_fixed_location" in self.config:
self.config["telemetry_s_fixed_location"] = False self.config["telemetry_s_fixed_location"] = False
if not "telemetry_s_fixed_latlon" in self.config: if not "telemetry_s_fixed_latlon" in self.config:
@ -1042,7 +1074,7 @@ class SidebandCore():
app_data = b"" app_data = b""
if type(app_data) != bytes: if type(app_data) != bytes:
app_data = msgpack.packb([app_data, stamp_cost]) app_data = msgpack.packb([app_data, stamp_cost])
RNS.log("Received "+str(dest_type)+" announce for "+RNS.prettyhexrep(dest)+" with data: "+str(app_data), RNS.LOG_DEBUG) RNS.log("Received "+str(dest_type)+" announce for "+RNS.prettyhexrep(dest), RNS.LOG_DEBUG)
self._db_save_announce(dest, app_data, dest_type, link_stats) self._db_save_announce(dest, app_data, dest_type, link_stats)
self.setstate("app.flags.new_announces", True) self.setstate("app.flags.new_announces", True)
@ -1929,6 +1961,10 @@ class SidebandCore():
image=args["image"], image=args["image"],
audio=args["audio"]) audio=args["audio"])
connection.send(send_result) connection.send(send_result)
elif "cancel_message" in call:
args = call["cancel_message"]
cancel_result = self.cancel_message(args["message_id"])
connection.send(cancel_result)
elif "send_command" in call: elif "send_command" in call:
args = call["send_command"] args = call["send_command"]
send_result = self.send_command( send_result = self.send_command(
@ -2319,6 +2355,11 @@ class SidebandCore():
self.setstate("app.flags.last_telemetry", time.time()) self.setstate("app.flags.last_telemetry", time.time())
if self.config["telemetry_to_mqtt"] == True:
def mqtt_job():
self.mqtt_handle_telemetry(context_dest, telemetry)
threading.Thread(target=mqtt_job, daemon=True).start()
return telemetry return telemetry
except Exception as e: except Exception as e:
@ -2747,7 +2788,7 @@ class SidebandCore():
db.commit() db.commit()
def _db_clean_messages(self): def _db_clean_messages(self):
RNS.log("Purging stale messages... "+str(self.db_path)) RNS.log("Purging stale messages... ", RNS.LOG_DEBUG)
with self.db_lock: with self.db_lock:
db = self.__db_connect() db = self.__db_connect()
dbc = db.cursor() dbc = db.cursor()
@ -2756,6 +2797,20 @@ class SidebandCore():
dbc.execute(query, {"outbound_state": LXMF.LXMessage.OUTBOUND, "sending_state": LXMF.LXMessage.SENDING}) dbc.execute(query, {"outbound_state": LXMF.LXMessage.OUTBOUND, "sending_state": LXMF.LXMessage.SENDING})
db.commit() db.commit()
def _db_clean_telemetry(self):
RNS.log("Cleaning telemetry... ", RNS.LOG_DEBUG)
clean_time = time.time()-self.TELEMETRY_KEEP
with self.db_lock:
db = self.__db_connect()
dbc = db.cursor()
query = f"delete from telemetry where (ts < {clean_time});"
dbc.execute(query, {"outbound_state": LXMF.LXMessage.OUTBOUND, "sending_state": LXMF.LXMessage.SENDING})
db.commit()
self.last_telemetry_clean = time.time()
def _db_message_set_state(self, lxm_hash, state, is_retry=False, ratchet_id=None, originator_stamp=None): def _db_message_set_state(self, lxm_hash, state, is_retry=False, ratchet_id=None, originator_stamp=None):
msg_extras = None msg_extras = None
if ratchet_id != None: if ratchet_id != None:
@ -3135,6 +3190,14 @@ class SidebandCore():
self.update_telemeter_config() self.update_telemeter_config()
self.setstate("app.flags.last_telemetry", time.time()) self.setstate("app.flags.last_telemetry", time.time())
def mqtt_handle_telemetry(self, context_dest, telemetry):
if self.mqtt == None:
self.mqtt = MQTT()
self.mqtt.set_server(self.config["telemetry_mqtt_host"], self.config["telemetry_mqtt_port"])
self.mqtt.set_auth(self.config["telemetry_mqtt_user"], self.config["telemetry_mqtt_pass"])
self.mqtt.handle(context_dest, telemetry)
def update_telemetry(self): def update_telemetry(self):
try: try:
try: try:
@ -3200,7 +3263,9 @@ class SidebandCore():
else: else:
self.telemeter = Telemeter(android_context=self.service_context, service=True, location_provider=self.owner_service) self.telemeter = Telemeter(android_context=self.service_context, service=True, location_provider=self.owner_service)
sensors = ["location", "information", "battery", "pressure", "temperature", "humidity", "magnetic_field", "ambient_light", "gravity", "angular_velocity", "acceleration", "proximity"] sensors = ["location", "information", "battery", "pressure", "temperature", "humidity", "magnetic_field",
"ambient_light", "gravity", "angular_velocity", "acceleration", "proximity", "rns_transport"]
for sensor in sensors: for sensor in sensors:
if self.config["telemetry_s_"+sensor]: if self.config["telemetry_s_"+sensor]:
self.telemeter.enable(sensor) self.telemeter.enable(sensor)
@ -3248,6 +3313,10 @@ class SidebandCore():
if self.config["telemetry_enabled"] == True: if self.config["telemetry_enabled"] == True:
self.update_telemeter_config() self.update_telemeter_config()
if self.telemeter != None: if self.telemeter != None:
if self.config["telemetry_to_mqtt"]:
def mqtt_job():
self.mqtt_handle_telemetry(self.lxmf_destination.hash, self.telemeter.packed())
threading.Thread(target=mqtt_job, daemon=True).start()
return self.telemeter.read_all() return self.telemeter.read_all()
else: else:
return {} return {}
@ -3541,6 +3610,9 @@ class SidebandCore():
self.setpersistent("lxmf.syncretrying", False) self.setpersistent("lxmf.syncretrying", False)
if self.config["telemetry_enabled"]: if self.config["telemetry_enabled"]:
if time.time()-self.last_telemetry_clean > self.telemetry_clean_interval:
self._db_clean_telemetry()
if self.config["telemetry_send_to_collector"]: if self.config["telemetry_send_to_collector"]:
if self.config["telemetry_collector"] != None and self.config["telemetry_collector"] != self.lxmf_destination.hash: if self.config["telemetry_collector"] != None and self.config["telemetry_collector"] != self.lxmf_destination.hash:
try: try:
@ -4354,6 +4426,21 @@ class SidebandCore():
RNS.log("An error occurred while getting message transfer stamp cost: "+str(e), RNS.LOG_ERROR) RNS.log("An error occurred while getting message transfer stamp cost: "+str(e), RNS.LOG_ERROR)
return None return None
def _service_cancel_message(self, message_id):
if not RNS.vendor.platformutils.is_android():
return False
else:
if self.is_client:
try:
return self.service_rpc_request({"cancel_message": {"message_id": message_id }})
except Exception as e:
RNS.log("Error while cancelling message over RPC: "+str(e), RNS.LOG_DEBUG)
RNS.trace_exception(e)
return False
else:
return False
def _service_send_message(self, content, destination_hash, propagation, skip_fields=False, no_display=False, attachment = None, image = None, audio = None): def _service_send_message(self, content, destination_hash, propagation, skip_fields=False, no_display=False, attachment = None, image = None, audio = None):
if not RNS.vendor.platformutils.is_android(): if not RNS.vendor.platformutils.is_android():
return False return False
@ -4397,6 +4484,26 @@ class SidebandCore():
else: else:
return False return False
def cancel_message(self, message_id):
if self.allow_service_dispatch and self.is_client:
try:
return self._service_cancel_message(message_id)
except Exception as e:
RNS.log("Error while cancelling message: "+str(e), RNS.LOG_ERROR)
RNS.trace_exception(e)
return False
else:
try:
self.message_router.cancel_outbound(message_id)
return True
except Exception as e:
RNS.log("Error while cancelling message: "+str(e), RNS.LOG_ERROR)
RNS.trace_exception(e)
return False
def send_message(self, content, destination_hash, propagation, skip_fields=False, no_display=False, attachment = None, image = None, audio = None): def send_message(self, content, destination_hash, propagation, skip_fields=False, no_display=False, attachment = None, image = None, audio = None):
if self.allow_service_dispatch and self.is_client: if self.allow_service_dispatch and self.is_client:
try: try:
@ -4436,6 +4543,14 @@ class SidebandCore():
fields[LXMF.FIELD_IMAGE] = image fields[LXMF.FIELD_IMAGE] = image
if audio != None: if audio != None:
fields[LXMF.FIELD_AUDIO] = audio fields[LXMF.FIELD_AUDIO] = audio
md_sig = "#!md\n"
if content.startswith(md_sig):
content = content[len(md_sig):]
fields[LXMF.FIELD_RENDERER] = LXMF.RENDERER_MARKDOWN
elif self.config["compose_in_markdown"]:
fields[LXMF.FIELD_RENDERER] = LXMF.RENDERER_MARKDOWN
elif self.has_bb_markup(content):
fields[LXMF.FIELD_RENDERER] = LXMF.RENDERER_BBCODE
lxm = LXMF.LXMessage(dest, source, content, title="", desired_method=desired_method, fields = fields, include_ticket=self.is_trusted(destination_hash)) lxm = LXMF.LXMessage(dest, source, content, title="", desired_method=desired_method, fields = fields, include_ticket=self.is_trusted(destination_hash))
@ -4574,6 +4689,19 @@ class SidebandCore():
self.setstate("lxm_uri_ingest.result", response) self.setstate("lxm_uri_ingest.result", response)
def strip_bb_markup(self, text):
if not hasattr(self, "smr") or self.smr == None:
self.smr = re.compile(r"\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]",re.IGNORECASE | re.MULTILINE )
return self.smr.sub("", text)
def has_bb_markup(self, text):
if not hasattr(self, "smr") or self.smr == None:
self.smr = re.compile(r"\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]",re.IGNORECASE | re.MULTILINE )
if self.smr.match(text):
return True
else:
return False
def lxm_ingest(self, message, originator = False): def lxm_ingest(self, message, originator = False):
should_notify = False should_notify = False
is_trusted = False is_trusted = False
@ -4656,7 +4784,7 @@ class SidebandCore():
if should_notify: if should_notify:
nlen = 128 nlen = 128
text = message.content.decode("utf-8") text = message.content.decode("utf-8")
notification_content = text[:nlen] notification_content = self.strip_bb_markup(text[:nlen])
if len(text) > nlen: if len(text) > nlen:
notification_content += " [...]" notification_content += " [...]"
@ -4763,6 +4891,7 @@ class SidebandCore():
def start(self): def start(self):
self._db_clean_messages() self._db_clean_messages()
self._db_clean_telemetry()
self.__start_jobs_immediate() self.__start_jobs_immediate()
thread = threading.Thread(target=self.__start_jobs_deferred) thread = threading.Thread(target=self.__start_jobs_deferred)
@ -4988,6 +5117,10 @@ class SidebandCore():
RNS.log("Error while handling commands: "+str(e), RNS.LOG_ERROR) RNS.log("Error while handling commands: "+str(e), RNS.LOG_ERROR)
def create_telemetry_collector_response(self, to_addr, timebase, is_authorized_telemetry_request=False): def create_telemetry_collector_response(self, to_addr, timebase, is_authorized_telemetry_request=False):
if self.getstate(f"telemetry.{RNS.hexrep(to_addr, delimit=False)}.update_sending") == True:
RNS.log("Not sending new telemetry collector response, since an earlier transfer is already in progress", RNS.LOG_DEBUG)
return "in_progress"
added_sources = {} added_sources = {}
sources = self.list_telemetry(after=timebase) sources = self.list_telemetry(after=timebase)
only_latest = self.config["telemetry_requests_only_send_latest"] only_latest = self.config["telemetry_requests_only_send_latest"]
@ -5099,6 +5232,6 @@ rns_config = """# This template is used to generate a
# No additional interfaces are currently # No additional interfaces are currently
# defined, but you can use this section # defined, but you can use this section
# to do so. # to add additional custom interfaces.
[interfaces] [interfaces]
""" """

129
sbapp/sideband/mqtt.py Normal file
View File

@ -0,0 +1,129 @@
import RNS
import time
import threading
from collections import deque
from .sense import Telemeter, Commands
if RNS.vendor.platformutils.get_platform() == "android":
import pmqtt.client as mqtt
else:
from sbapp.pmqtt import client as mqtt
class MQTT():
QUEUE_MAXLEN = 65536
SCHEDULER_SLEEP = 1
def __init__(self):
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self.host = None
self.port = None
self.run = False
self.is_connected = False
self.queue_lock = threading.Lock()
self.waiting_msgs = deque(maxlen=MQTT.QUEUE_MAXLEN)
self.waiting_telemetry = set()
self.client.on_connect_fail = self.connect_failed
self.client.on_disconnect = self.disconnected
self.start()
def start(self):
self.run = True
threading.Thread(target=self.jobs, daemon=True).start()
RNS.log("Started MQTT scheduler", RNS.LOG_DEBUG)
def stop(self):
RNS.log("Stopping MQTT scheduler", RNS.LOG_DEBUG)
self.run = False
def jobs(self):
while self.run:
try:
if len(self.waiting_msgs) > 0:
RNS.log(f"Processing {len(self.waiting_msgs)} MQTT messages", RNS.LOG_DEBUG)
if self.process_queue():
RNS.log("All MQTT messages processed", RNS.LOG_DEBUG)
except Exception as e:
RNS.log("An error occurred while running MQTT scheduler jobs: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
time.sleep(MQTT.SCHEDULER_SLEEP)
RNS.log("Stopped MQTT scheduler", RNS.LOG_DEBUG)
def connect_failed(self, client, userdata):
RNS.log(f"Connection to MQTT server failed", RNS.LOG_DEBUG)
self.is_connected = False
def disconnected(self, client, userdata, disconnect_flags, reason_code, properties):
RNS.log(f"Disconnected from MQTT server, reason code: {reason_code}", RNS.LOG_EXTREME)
self.is_connected = False
def set_server(self, host, port):
try:
port = int(port)
except:
port = None
self.host = host
self.port = port or 1883
def set_auth(self, username, password):
self.client.username_pw_set(username, password)
def connect(self):
RNS.log(f"Connecting MQTT server {self.host}:{self.port}", RNS.LOG_DEBUG) # TODO: Remove debug
cs = self.client.connect(self.host, self.port)
self.client.loop_start()
def disconnect(self):
RNS.log("Disconnecting from MQTT server", RNS.LOG_EXTREME) # TODO: Remove debug
self.client.disconnect()
self.client.loop_stop()
self.is_connected = False
def post_message(self, topic, data):
mqtt_msg = self.client.publish(topic, data, qos=1)
self.waiting_telemetry.add(mqtt_msg)
def process_queue(self):
with self.queue_lock:
try:
self.connect()
except Exception as e:
RNS.log(f"An error occurred while connecting to MQTT server: {e}", RNS.LOG_ERROR)
return False
try:
while len(self.waiting_msgs) > 0:
topic, data = self.waiting_msgs.pop()
self.post_message(topic, data)
except Exception as e:
RNS.log(f"An error occurred while publishing MQTT messages: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
try:
for msg in self.waiting_telemetry:
msg.wait_for_publish()
self.waiting_telemetry.clear()
except Exception as e:
RNS.log(f"An error occurred while publishing MQTT messages: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
self.disconnect()
return True
def handle(self, context_dest, telemetry):
remote_telemeter = Telemeter.from_packed(telemetry)
read_telemetry = remote_telemeter.read_all()
telemetry_timestamp = read_telemetry["time"]["utc"]
root_path = f"lxmf/telemetry/{RNS.hexrep(context_dest, delimit=False)}"
for sensor in remote_telemeter.sensors:
s = remote_telemeter.sensors[sensor]
topics = s.render_mqtt()
if topics != None:
for topic in topics:
topic_path = f"{root_path}/{topic}"
data = topics[topic]
self.waiting_msgs.append((topic_path, data))

File diff suppressed because it is too large Load Diff

View File

@ -20,11 +20,13 @@ color_delivered = "Blue"
color_paper = "Indigo" color_paper = "Indigo"
color_propagated = "Indigo" color_propagated = "Indigo"
color_failed = "Red" color_failed = "Red"
color_cancelled = "Red"
color_unknown = "Gray" color_unknown = "Gray"
intensity_msgs_dark = "800" intensity_msgs_dark = "800"
intensity_msgs_light = "500" intensity_msgs_light = "500"
intensity_play_dark = "600" intensity_play_dark = "600"
intensity_play_light = "300" intensity_play_light = "300"
intensity_cancelled = "900"
intensity_msgs_dark_alt = "800" intensity_msgs_dark_alt = "800"
@ -38,6 +40,7 @@ color_paper_alt = "DeepPurple"
color_playing_alt = "Amber" color_playing_alt = "Amber"
color_failed_alt = "Red" color_failed_alt = "Red"
color_unknown_alt = "Gray" color_unknown_alt = "Gray"
color_cancelled_alt = "Red"
class ContentNavigationDrawer(Screen): class ContentNavigationDrawer(Screen):
pass pass
@ -120,10 +123,12 @@ def sig_icon_for_q(q):
return "󰣸" return "󰣸"
elif q > 50: elif q > 50:
return "󰣶" return "󰣶"
elif q > 30: elif q > 20:
return "󰣴" return "󰣴"
elif q > 10: elif q > 5:
return "󰣾" return "󰣾"
else:
return "󰣽"
persistent_fonts = ["nf", "term"] persistent_fonts = ["nf", "term"]
nf_mapped = "nf" nf_mapped = "nf"

View File

@ -1655,6 +1655,22 @@ MDScreen:
disabled: False disabled: False
active: False active: False
MDBoxLayout:
orientation: "horizontal"
size_hint_y: None
padding: [0,0,dp(24),dp(0)]
height: dp(48)
MDLabel:
text: "Compose messages in markdown"
font_style: "H6"
MDSwitch:
id: settings_compose_in_markdown
pos_hint: {"center_y": 0.3}
disabled: False
active: False
MDBoxLayout: MDBoxLayout:
orientation: "horizontal" orientation: "horizontal"
size_hint_y: None size_hint_y: None

View File

@ -34,14 +34,14 @@ if RNS.vendor.platformutils.get_platform() == "android":
import plyer import plyer
from sideband.sense import Telemeter, Commands from sideband.sense import Telemeter, Commands
from ui.helpers import ts_format, file_ts_format, mdc from ui.helpers import ts_format, file_ts_format, mdc
from ui.helpers import color_playing, color_received, color_delivered, color_propagated, color_paper, color_failed, color_unknown, intensity_msgs_dark, intensity_msgs_light, intensity_play_dark, intensity_play_light from ui.helpers import color_playing, color_received, color_delivered, color_propagated, color_paper, color_failed, color_unknown, intensity_msgs_dark, intensity_msgs_light, intensity_play_dark, intensity_play_light, color_cancelled, intensity_cancelled
from ui.helpers import color_received_alt, color_received_alt_light, color_delivered_alt, color_propagated_alt, color_paper_alt, color_failed_alt, color_unknown_alt, color_playing_alt, intensity_msgs_dark_alt, intensity_msgs_light_alt, intensity_delivered_alt_dark from ui.helpers import color_received_alt, color_received_alt_light, color_delivered_alt, color_propagated_alt, color_paper_alt, color_failed_alt, color_unknown_alt, color_playing_alt, intensity_msgs_dark_alt, intensity_msgs_light_alt, intensity_delivered_alt_dark, color_cancelled_alt
else: else:
import sbapp.plyer as plyer import sbapp.plyer as plyer
from sbapp.sideband.sense import Telemeter, Commands from sbapp.sideband.sense import Telemeter, Commands
from .helpers import ts_format, file_ts_format, mdc from .helpers import ts_format, file_ts_format, mdc
from .helpers import color_playing, color_received, color_delivered, color_propagated, color_paper, color_failed, color_unknown, intensity_msgs_dark, intensity_msgs_light, intensity_play_dark, intensity_play_light from .helpers import color_playing, color_received, color_delivered, color_propagated, color_paper, color_failed, color_unknown, intensity_msgs_dark, intensity_msgs_light, intensity_play_dark, intensity_play_light, color_cancelled, intensity_cancelled
from .helpers import color_received_alt, color_received_alt_light, color_delivered_alt, color_propagated_alt, color_paper_alt, color_failed_alt, color_unknown_alt, color_playing_alt, intensity_msgs_dark_alt, intensity_msgs_light_alt, intensity_delivered_alt_dark from .helpers import color_received_alt, color_received_alt_light, color_delivered_alt, color_propagated_alt, color_paper_alt, color_failed_alt, color_unknown_alt, color_playing_alt, intensity_msgs_dark_alt, intensity_msgs_light_alt, intensity_delivered_alt_dark, color_cancelled_alt
if RNS.vendor.platformutils.is_darwin(): if RNS.vendor.platformutils.is_darwin():
from PIL import Image as PilImage from PIL import Image as PilImage
@ -110,8 +110,6 @@ class Messages():
msg = self.app.sideband.message(lxm_hash) msg = self.app.sideband.message(lxm_hash)
if msg: if msg:
close_button = MDRectangleFlatButton(text="Close", font_size=dp(18)) close_button = MDRectangleFlatButton(text="Close", font_size=dp(18))
# d_items = [ ]
# d_items.append(DialogItem(IconLeftWidget(icon="postage-stamp"), text="[size="+str(ss)+"]Stamp[/size]"))
d_text = "" d_text = ""
@ -213,6 +211,7 @@ class Messages():
c_paper = color_paper_alt c_paper = color_paper_alt
c_unknown = color_unknown_alt c_unknown = color_unknown_alt
c_failed = color_failed_alt c_failed = color_failed_alt
c_cancelled = color_cancelled_alt
else: else:
c_delivered = color_delivered c_delivered = color_delivered
c_received = color_received c_received = color_received
@ -221,6 +220,7 @@ class Messages():
c_paper = color_paper c_paper = color_paper
c_unknown = color_unknown c_unknown = color_unknown
c_failed = color_failed c_failed = color_failed
c_cancelled = color_cancelled
for new_message in self.app.sideband.list_messages(self.context_dest, after=self.latest_message_timestamp,limit=limit): for new_message in self.app.sideband.list_messages(self.context_dest, after=self.latest_message_timestamp,limit=limit):
self.new_messages.append(new_message) self.new_messages.append(new_message)
@ -301,6 +301,17 @@ class Messages():
delivery_syms += " 📦" delivery_syms += " 📦"
delivery_syms = multilingual_markup(delivery_syms.encode("utf-8")).decode("utf-8") delivery_syms = multilingual_markup(delivery_syms.encode("utf-8")).decode("utf-8")
if msg["state"] > LXMF.LXMessage.SENT:
if hasattr(w, "dmenu"):
if hasattr(w.dmenu, "items"):
remove_item = None
for item in w.dmenu.items:
if item["text"] == "Cancel message":
remove_item = item
break
if remove_item != None:
w.dmenu.items.remove(remove_item)
if msg["state"] == LXMF.LXMessage.OUTBOUND or msg["state"] == LXMF.LXMessage.SENDING or msg["state"] == LXMF.LXMessage.SENT: if msg["state"] == LXMF.LXMessage.OUTBOUND or msg["state"] == LXMF.LXMessage.SENDING or msg["state"] == LXMF.LXMessage.SENT:
w.md_bg_color = msg_color = mdc(c_unknown, intensity_msgs) w.md_bg_color = msg_color = mdc(c_unknown, intensity_msgs)
txstr = time.strftime(ts_format, time.localtime(msg["sent"])) txstr = time.strftime(ts_format, time.localtime(msg["sent"]))
@ -334,6 +345,12 @@ class Messages():
w.heading += f"\n[b]Audio Message[/b] ({alstr})" w.heading += f"\n[b]Audio Message[/b] ({alstr})"
m["state"] = msg["state"] m["state"] = msg["state"]
att_heading_str = ""
if hasattr(w, "has_attachment") and w.has_attachment:
att_heading_str = "\n[b]Attachments[/b] "
for attachment in w.attachments_field:
att_heading_str += str(attachment[0])+", "
att_heading_str = att_heading_str[:-2]
if msg["state"] == LXMF.LXMessage.DELIVERED: if msg["state"] == LXMF.LXMessage.DELIVERED:
w.md_bg_color = msg_color = mdc(c_delivered, intensity_delivered) w.md_bg_color = msg_color = mdc(c_delivered, intensity_delivered)
@ -369,7 +386,7 @@ class Messages():
m["state"] = msg["state"] m["state"] = msg["state"]
if msg["state"] == LXMF.LXMessage.FAILED: if msg["state"] == LXMF.LXMessage.FAILED:
w.md_bg_color = msg_color = mdc(c_failed, intensity_msgs) w.md_bg_color = msg_color = mdc(c_failed, intensity_cancelled)
txstr = time.strftime(ts_format, time.localtime(msg["sent"])) txstr = time.strftime(ts_format, time.localtime(msg["sent"]))
titlestr = "" titlestr = ""
if msg["title"]: if msg["title"]:
@ -381,6 +398,34 @@ class Messages():
w.heading += f"\n[b]Audio Message[/b] ({alstr})" w.heading += f"\n[b]Audio Message[/b] ({alstr})"
w.dmenu.items.append(w.dmenu.retry_item) w.dmenu.items.append(w.dmenu.retry_item)
if msg["state"] == LXMF.LXMessage.CANCELLED:
w.md_bg_color = msg_color = mdc(c_cancelled, intensity_cancelled)
txstr = time.strftime(ts_format, time.localtime(msg["sent"]))
titlestr = ""
if msg["title"]:
titlestr = "[b]Title[/b] "+msg["title"].decode("utf-8")+"\n"
w.heading = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Cancelled"
m["state"] = msg["state"]
if w.has_audio:
alstr = RNS.prettysize(w.audio_size)
w.heading += f"\n[b]Audio Message[/b] ({alstr})"
w.dmenu.items.append(w.dmenu.retry_item)
if msg["state"] == LXMF.LXMessage.REJECTED:
w.md_bg_color = msg_color = mdc(c_cancelled, intensity_cancelled)
txstr = time.strftime(ts_format, time.localtime(msg["sent"]))
titlestr = ""
if msg["title"]:
titlestr = "[b]Title[/b] "+msg["title"].decode("utf-8")+"\n"
w.heading = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Rejected"
m["state"] = msg["state"]
if w.has_audio:
alstr = RNS.prettysize(w.audio_size)
w.heading += f"\n[b]Audio Message[/b] ({alstr})"
w.dmenu.items.append(w.dmenu.retry_item)
w.heading += att_heading_str
def hide_widget(self, wid, dohide=True): def hide_widget(self, wid, dohide=True):
if hasattr(wid, 'saved_attrs'): if hasattr(wid, 'saved_attrs'):
@ -427,6 +472,7 @@ class Messages():
c_paper = color_paper_alt c_paper = color_paper_alt
c_unknown = color_unknown_alt c_unknown = color_unknown_alt
c_failed = color_failed_alt c_failed = color_failed_alt
c_cancelled = color_cancelled_alt
else: else:
c_delivered = color_delivered c_delivered = color_delivered
c_received = color_received c_received = color_received
@ -435,6 +481,7 @@ class Messages():
c_paper = color_paper c_paper = color_paper
c_unknown = color_unknown c_unknown = color_unknown
c_failed = color_failed c_failed = color_failed
c_cancelled = color_cancelled
self.ids.message_text.font_name = self.app.input_font self.ids.message_text.font_name = self.app.input_font
@ -443,13 +490,27 @@ class Messages():
for m in self.new_messages: for m in self.new_messages:
if not m["hash"] in self.added_item_hashes: if not m["hash"] in self.added_item_hashes:
renderer = None
message_source = m["content"]
if "lxm" in m and m["lxm"] and m["lxm"].fields != None and LXMF.FIELD_RENDERER in m["lxm"].fields:
renderer = m["lxm"].fields[LXMF.FIELD_RENDERER]
try: try:
if self.app.sideband.config["trusted_markup_only"] and not self.is_trusted: if self.app.sideband.config["trusted_markup_only"] and not self.is_trusted:
message_input = str( escape_markup(m["content"].decode("utf-8")) ).encode("utf-8") message_input = str( escape_markup(m["content"].decode("utf-8")) ).encode("utf-8")
else: else:
message_input = m["content"] message_input = m["content"]
if renderer == LXMF.RENDERER_MARKDOWN:
message_input = self.app.md_to_bbcode(message_input.decode("utf-8")).encode("utf-8")
message_input = self.app.process_bb_markup(message_input.decode("utf-8")).encode("utf-8")
elif renderer == LXMF.RENDERER_BBCODE:
message_input = self.app.process_bb_markup(message_input.decode("utf-8")).encode("utf-8")
else:
message_input = str(escape_markup(m["content"].decode("utf-8"))).encode("utf-8")
except Exception as e: except Exception as e:
RNS.log(f"Message content could not be decoded: {e}", RNS.LOG_DEBUG) RNS.log(f"Message content could not be decoded: {e}", RNS.LOG_DEBUG)
RNS.trace_exception(e)
message_input = b"" message_input = b""
if message_input.strip() == b"": if message_input.strip() == b"":
@ -602,9 +663,17 @@ class Messages():
heading_str = titlestr+"[b]Created[/b] "+txstr+"\n[b]State[/b] Paper Message" heading_str = titlestr+"[b]Created[/b] "+txstr+"\n[b]State[/b] Paper Message"
elif m["state"] == LXMF.LXMessage.FAILED: elif m["state"] == LXMF.LXMessage.FAILED:
msg_color = mdc(c_failed, intensity_msgs) msg_color = mdc(c_failed, intensity_cancelled)
heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Failed" heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Failed"
elif m["state"] == LXMF.LXMessage.CANCELLED:
msg_color = mdc(c_cancelled, intensity_cancelled)
heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Cancelled"
elif m["state"] == LXMF.LXMessage.REJECTED:
msg_color = mdc(c_cancelled, intensity_cancelled)
heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Rejected"
elif m["state"] == LXMF.LXMessage.OUTBOUND or m["state"] == LXMF.LXMessage.SENDING: elif m["state"] == LXMF.LXMessage.OUTBOUND or m["state"] == LXMF.LXMessage.SENDING:
msg_color = mdc(c_unknown, intensity_msgs) msg_color = mdc(c_unknown, intensity_msgs)
heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Sending " heading_str = titlestr+"[b]Sent[/b] "+txstr+"\n[b]State[/b] Sending "
@ -618,9 +687,6 @@ class Messages():
heading_str = titlestr heading_str = titlestr
if phy_stats_str != "" and self.app.sideband.config["advanced_stats"]: if phy_stats_str != "" and self.app.sideband.config["advanced_stats"]:
heading_str += phy_stats_str+"\n" heading_str += phy_stats_str+"\n"
# TODO: Remove
# if stamp_valid:
# txstr += f" [b]Stamp[/b] value is {stamp_value} "
heading_str += "[b]Sent[/b] "+txstr+delivery_syms heading_str += "[b]Sent[/b] "+txstr+delivery_syms
heading_str += "\n[b]Received[/b] "+rxstr heading_str += "\n[b]Received[/b] "+rxstr
@ -658,6 +724,9 @@ class Messages():
if has_attachment: if has_attachment:
item.attachments_field = attachments_field item.attachments_field = attachments_field
item.has_attachment = True
else:
item.has_attachment = False
if has_audio: if has_audio:
def play_audio(sender): def play_audio(sender):
@ -798,6 +867,13 @@ class Messages():
return x return x
def gen_cancel(mhash, item):
def x():
self.app.sideband.cancel_message(mhash)
item.dmenu.dismiss()
return x
def gen_save_image(item): def gen_save_image(item):
if RNS.vendor.platformutils.is_android(): if RNS.vendor.platformutils.is_android():
def x(): def x():
@ -1080,7 +1156,7 @@ class Messages():
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
"text": "Copy message text", "text": "Copy message text",
"height": dp(40), "height": dp(40),
"on_release": gen_copy(message_input.decode("utf-8"), item) "on_release": gen_copy(message_source.decode("utf-8"), item)
}, },
{ {
"text": "Delete", "text": "Delete",
@ -1114,7 +1190,7 @@ class Messages():
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
"text": "Copy message text", "text": "Copy message text",
"height": dp(40), "height": dp(40),
"on_release": gen_copy(message_input.decode("utf-8"), item) "on_release": gen_copy(message_source.decode("utf-8"), item)
}, },
{ {
"text": "Delete", "text": "Delete",
@ -1132,7 +1208,7 @@ class Messages():
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
"text": "Copy", "text": "Copy",
"height": dp(40), "height": dp(40),
"on_release": gen_copy(message_input.decode("utf-8"), item) "on_release": gen_copy(message_source.decode("utf-8"), item)
}, },
{ {
"text": "Delete", "text": "Delete",
@ -1149,7 +1225,7 @@ class Messages():
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
"text": "Copy", "text": "Copy",
"height": dp(40), "height": dp(40),
"on_release": gen_copy(message_input.decode("utf-8"), item) "on_release": gen_copy(message_source.decode("utf-8"), item)
}, },
{ {
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
@ -1172,7 +1248,7 @@ class Messages():
"viewclass": "OneLineListItem", "viewclass": "OneLineListItem",
"text": "Copy", "text": "Copy",
"height": dp(40), "height": dp(40),
"on_release": gen_copy(message_input.decode("utf-8"), item) "on_release": gen_copy(message_source.decode("utf-8"), item)
}, },
{ {
"text": "Delete", "text": "Delete",
@ -1197,6 +1273,14 @@ class Messages():
"on_release": gen_save_attachment(item) "on_release": gen_save_attachment(item)
} }
dm_items.append(extra_item) dm_items.append(extra_item)
if m["source"] == self.app.sideband.lxmf_destination.hash and m["state"] <= LXMF.LXMessage.SENT:
extra_item = {
"viewclass": "OneLineListItem",
"text": "Cancel message",
"height": dp(40),
"on_release": gen_cancel(m["hash"], item)
}
dm_items.append(extra_item)
item.dmenu = MDDropdownMenu( item.dmenu = MDDropdownMenu(
caller=item.ids.msg_submenu, caller=item.ids.msg_submenu,

View File

@ -767,6 +767,16 @@ class RVDetails(MDRecycleView):
threading.Thread(target=lj, daemon=True).start() threading.Thread(target=lj, daemon=True).start()
release_function = select release_function = select
elif name == "Reticulum Transport":
te = "enabled" if s["values"]["transport_enabled"] else "disabled"
formatted_values = f"Reticulum Transport [b]{te}[/b]"
elif name == "LXMF Propagation":
tp = str(s["values"]["total_peers"])
ap = str(s["values"]["active_peers"])
formatted_values = f"Peered with [b]{tp}[/b] LXMF Propagation Nodes, [b]{ap}[/b] available"
else: else:
formatted_values = f"{name}" formatted_values = f"{name}"
for vn in s["values"]: for vn in s["values"]:

View File

@ -44,6 +44,30 @@ class Telemetry():
else: else:
self.screen.ids.telemetry_collector.text = RNS.hexrep(self.app.sideband.config["telemetry_collector"], delimit=False) self.screen.ids.telemetry_collector.text = RNS.hexrep(self.app.sideband.config["telemetry_collector"], delimit=False)
self.screen.ids.telemetry_mqtt_host.bind(focus=self.telemetry_save)
if self.app.sideband.config["telemetry_mqtt_host"] == None:
self.screen.ids.telemetry_mqtt_host.text = ""
else:
self.screen.ids.telemetry_mqtt_host.text = self.app.sideband.config["telemetry_mqtt_host"]
self.screen.ids.telemetry_mqtt_port.bind(focus=self.telemetry_save)
if self.app.sideband.config["telemetry_mqtt_port"] == None:
self.screen.ids.telemetry_mqtt_port.text = ""
else:
self.screen.ids.telemetry_mqtt_port.text = self.app.sideband.config["telemetry_mqtt_port"]
self.screen.ids.telemetry_mqtt_user.bind(focus=self.telemetry_save)
if self.app.sideband.config["telemetry_mqtt_user"] == None:
self.screen.ids.telemetry_mqtt_user.text = ""
else:
self.screen.ids.telemetry_mqtt_user.text = self.app.sideband.config["telemetry_mqtt_user"]
self.screen.ids.telemetry_mqtt_pass.bind(focus=self.telemetry_save)
if self.app.sideband.config["telemetry_mqtt_pass"] == None:
self.screen.ids.telemetry_mqtt_pass.text = ""
else:
self.screen.ids.telemetry_mqtt_pass.text = self.app.sideband.config["telemetry_mqtt_pass"]
self.screen.ids.telemetry_icon_preview.icon_color = self.app.sideband.config["telemetry_fg"] self.screen.ids.telemetry_icon_preview.icon_color = self.app.sideband.config["telemetry_fg"]
self.screen.ids.telemetry_icon_preview.md_bg_color = self.app.sideband.config["telemetry_bg"] self.screen.ids.telemetry_icon_preview.md_bg_color = self.app.sideband.config["telemetry_bg"]
self.screen.ids.telemetry_icon_preview.icon = self.app.sideband.config["telemetry_icon"] self.screen.ids.telemetry_icon_preview.icon = self.app.sideband.config["telemetry_icon"]
@ -84,6 +108,9 @@ class Telemetry():
self.screen.ids.telemetry_allow_requests_from_anyone.active = self.app.sideband.config["telemetry_allow_requests_from_anyone"] self.screen.ids.telemetry_allow_requests_from_anyone.active = self.app.sideband.config["telemetry_allow_requests_from_anyone"]
self.screen.ids.telemetry_allow_requests_from_anyone.bind(active=self.telemetry_save) self.screen.ids.telemetry_allow_requests_from_anyone.bind(active=self.telemetry_save)
self.screen.ids.telemetry_to_mqtt.active = self.app.sideband.config["telemetry_to_mqtt"]
self.screen.ids.telemetry_to_mqtt.bind(active=self.telemetry_save)
self.screen.ids.telemetry_scrollview.effect_cls = ScrollEffect self.screen.ids.telemetry_scrollview.effect_cls = ScrollEffect
info = "\nopenCom Companion allows you to securely share telemetry, such as location and sensor data, with people, custom programs, " info = "\nopenCom Companion allows you to securely share telemetry, such as location and sensor data, with people, custom programs, "
@ -259,6 +286,11 @@ class Telemetry():
self.app.sideband.config["telemetry_allow_requests_from_trusted"] = self.screen.ids.telemetry_allow_requests_from_trusted.active self.app.sideband.config["telemetry_allow_requests_from_trusted"] = self.screen.ids.telemetry_allow_requests_from_trusted.active
self.app.sideband.config["telemetry_allow_requests_from_anyone"] = self.screen.ids.telemetry_allow_requests_from_anyone.active self.app.sideband.config["telemetry_allow_requests_from_anyone"] = self.screen.ids.telemetry_allow_requests_from_anyone.active
self.app.sideband.config["telemetry_collector_enabled"] = self.screen.ids.telemetry_collector_enabled.active self.app.sideband.config["telemetry_collector_enabled"] = self.screen.ids.telemetry_collector_enabled.active
self.app.sideband.config["telemetry_to_mqtt"] = self.screen.ids.telemetry_to_mqtt.active
self.app.sideband.config["telemetry_mqtt_host"] = self.screen.ids.telemetry_mqtt_host.text
self.app.sideband.config["telemetry_mqtt_port"] = self.screen.ids.telemetry_mqtt_port.text
self.app.sideband.config["telemetry_mqtt_user"] = self.screen.ids.telemetry_mqtt_user.text
self.app.sideband.config["telemetry_mqtt_pass"] = self.screen.ids.telemetry_mqtt_pass.text
self.app.sideband.save_configuration() self.app.sideband.save_configuration()
if run_telemetry_update: if run_telemetry_update:
@ -366,6 +398,8 @@ class Telemetry():
self.sensors_screen.ids.telemetry_s_accelerometer.bind(active=self.sensors_save) self.sensors_screen.ids.telemetry_s_accelerometer.bind(active=self.sensors_save)
self.sensors_screen.ids.telemetry_s_proximity.active = self.app.sideband.config["telemetry_s_proximity"] self.sensors_screen.ids.telemetry_s_proximity.active = self.app.sideband.config["telemetry_s_proximity"]
self.sensors_screen.ids.telemetry_s_proximity.bind(active=self.sensors_save) self.sensors_screen.ids.telemetry_s_proximity.bind(active=self.sensors_save)
self.sensors_screen.ids.telemetry_s_rns_transport.active = self.app.sideband.config["telemetry_s_rns_transport"]
self.sensors_screen.ids.telemetry_s_rns_transport.bind(active=self.sensors_save)
self.sensors_screen.ids.telemetry_s_information.active = self.app.sideband.config["telemetry_s_information"] self.sensors_screen.ids.telemetry_s_information.active = self.app.sideband.config["telemetry_s_information"]
self.sensors_screen.ids.telemetry_s_information.bind(active=self.sensors_save) self.sensors_screen.ids.telemetry_s_information.bind(active=self.sensors_save)
self.sensors_screen.ids.telemetry_s_information_text.text = str(self.app.sideband.config["telemetry_s_information_text"]) self.sensors_screen.ids.telemetry_s_information_text.text = str(self.app.sideband.config["telemetry_s_information_text"])
@ -434,6 +468,7 @@ class Telemetry():
self.app.sideband.config["telemetry_s_angular_velocity"] = self.sensors_screen.ids.telemetry_s_gyroscope.active self.app.sideband.config["telemetry_s_angular_velocity"] = self.sensors_screen.ids.telemetry_s_gyroscope.active
self.app.sideband.config["telemetry_s_acceleration"] = self.sensors_screen.ids.telemetry_s_accelerometer.active self.app.sideband.config["telemetry_s_acceleration"] = self.sensors_screen.ids.telemetry_s_accelerometer.active
self.app.sideband.config["telemetry_s_proximity"] = self.sensors_screen.ids.telemetry_s_proximity.active self.app.sideband.config["telemetry_s_proximity"] = self.sensors_screen.ids.telemetry_s_proximity.active
self.app.sideband.config["telemetry_s_rns_transport"] = self.sensors_screen.ids.telemetry_s_rns_transport.active
if self.app.sideband.config["telemetry_s_information"] != self.sensors_screen.ids.telemetry_s_information.active: if self.app.sideband.config["telemetry_s_information"] != self.sensors_screen.ids.telemetry_s_information.active:
run_telemetry_update = True run_telemetry_update = True
@ -880,6 +915,90 @@ MDScreen:
on_release: root.delegate.telemetry_bg_color(self) on_release: root.delegate.telemetry_bg_color(self)
disabled: False disabled: False
MDLabel:
text: "MQTT Configuration"
font_style: "H6"
MDLabel:
id: telemetry_info6
markup: True
text: "\\nFor integration with other systems, you can configure Sideband to send all known telemetry data to an MQTT server in real-time as it is received or generated.\\n"
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
MDBoxLayout:
orientation: "horizontal"
size_hint_y: None
padding: [0,0,dp(24),dp(0)]
height: dp(48)
MDLabel:
text: "Send telemetry to MQTT"
font_style: "H6"
MDSwitch:
id: telemetry_to_mqtt
pos_hint: {"center_y": 0.3}
active: False
MDBoxLayout:
orientation: "vertical"
spacing: dp(24)
size_hint_y: None
padding: [dp(0),dp(0),dp(0),dp(0)]
#height: dp(232)
height: self.minimum_height
MDTextField:
id: telemetry_mqtt_host
hint_text: "Server Hostname"
text: ""
font_size: dp(24)
MDBoxLayout:
orientation: "vertical"
spacing: dp(24)
size_hint_y: None
padding: [dp(0),dp(0),dp(0),dp(0)]
#height: dp(232)
height: self.minimum_height
MDTextField:
id: telemetry_mqtt_port
hint_text: "Server Port"
text: ""
font_size: dp(24)
MDBoxLayout:
orientation: "vertical"
spacing: dp(24)
size_hint_y: None
padding: [dp(0),dp(0),dp(0),dp(0)]
#height: dp(232)
height: self.minimum_height
MDTextField:
id: telemetry_mqtt_user
hint_text: "Username"
text: ""
font_size: dp(24)
MDBoxLayout:
orientation: "vertical"
spacing: dp(24)
size_hint_y: None
padding: [dp(0),dp(0),dp(0),dp(60)]
#height: dp(232)
height: self.minimum_height
MDTextField:
id: telemetry_mqtt_pass
password: True
hint_text: "Password"
text: ""
font_size: dp(24)
MDLabel: MDLabel:
text: "Advanced Configuration" text: "Advanced Configuration"
font_style: "H6" font_style: "H6"
@ -1211,6 +1330,21 @@ MDScreen:
pos_hint: {"center_y": 0.3} pos_hint: {"center_y": 0.3}
active: False active: False
MDBoxLayout:
orientation: "horizontal"
size_hint_y: None
padding: [0,0,dp(24),dp(0)]
height: dp(48)
MDLabel:
text: "Reticulum Transport Stats"
font_style: "H6"
MDSwitch:
id: telemetry_s_rns_transport
pos_hint: {"center_y": 0.3}
active: False
MDBoxLayout: MDBoxLayout:
orientation: "horizontal" orientation: "horizontal"
size_hint_y: None size_hint_y: None

View File

@ -64,6 +64,7 @@ class Utilities():
) )
def dl_yes(s): def dl_yes(s):
dialog.dismiss() dialog.dismiss()
self.app.wants_flasher_launch = True
self.app.sideband.start_webshare() self.app.sideband.start_webshare()
def cb(dt): def cb(dt):
self.app.repository_action() self.app.repository_action()
@ -135,13 +136,24 @@ class Utilities():
def update_advanced(self, sender=None): def update_advanced(self, sender=None):
if RNS.vendor.platformutils.is_android(): if RNS.vendor.platformutils.is_android():
ct = self.app.sideband.config["config_template"] ct = self.app.sideband.config["config_template"]
if ct == None:
ct = self.app.sideband.default_config_template
self.advanced_screen.ids.config_template.text = f"[font=RobotoMono-Regular][size={int(dp(12))}]{ct}[/size][/font]" self.advanced_screen.ids.config_template.text = f"[font=RobotoMono-Regular][size={int(dp(12))}]{ct}[/size][/font]"
else: else:
self.advanced_screen.ids.config_template.text = f"[font=RobotoMono-Regular][size={int(dp(12))}]On this platform, Reticulum configuration is managed by the system. You can change the configuration by editing the file located at:\n\n{self.app.sideband.reticulum.configpath}[/size][/font]" self.advanced_screen.ids.config_template.text = f"[font=RobotoMono-Regular][size={int(dp(12))}]On this platform, Reticulum configuration is managed by the system. You can change the configuration by editing the file located at:\n\n{self.app.sideband.reticulum.configpath}[/size][/font]"
def reset_config(self, sender=None):
if RNS.vendor.platformutils.is_android():
self.app.sideband.config["config_template"] = None
self.app.sideband.save_configuration()
self.update_advanced()
def copy_config(self, sender=None): def copy_config(self, sender=None):
if RNS.vendor.platformutils.is_android(): if RNS.vendor.platformutils.is_android():
Clipboard.copy(self.app.sideband.config_template) if self.app.sideband.config["config_template"]:
Clipboard.copy(self.app.sideband.config["config_template"])
else:
Clipboard.copy(self.app.sideband.default_config_template)
def paste_config(self, sender=None): def paste_config(self, sender=None):
if RNS.vendor.platformutils.is_android(): if RNS.vendor.platformutils.is_android():
@ -409,7 +421,7 @@ MDScreen:
padding: [dp(0), dp(14), dp(0), dp(24)] padding: [dp(0), dp(14), dp(0), dp(24)]
MDRectangleFlatIconButton: MDRectangleFlatIconButton:
id: telemetry_button id: conf_copy_button
icon: "content-copy" icon: "content-copy"
text: "Copy Configuration" text: "Copy Configuration"
padding: [dp(0), dp(14), dp(0), dp(14)] padding: [dp(0), dp(14), dp(0), dp(14)]
@ -420,7 +432,7 @@ MDScreen:
disabled: False disabled: False
MDRectangleFlatIconButton: MDRectangleFlatIconButton:
id: coordinates_button id: conf_paste_button
icon: "download" icon: "download"
text: "Paste Configuration" text: "Paste Configuration"
padding: [dp(0), dp(14), dp(0), dp(14)] padding: [dp(0), dp(14), dp(0), dp(14)]
@ -430,6 +442,23 @@ MDScreen:
on_release: root.delegate.paste_config(self) on_release: root.delegate.paste_config(self)
disabled: False disabled: False
MDBoxLayout:
orientation: "horizontal"
spacing: dp(24)
size_hint_y: None
height: self.minimum_height
padding: [dp(0), dp(0), dp(0), dp(24)]
MDRectangleFlatIconButton:
id: conf_reset_button
icon: "cog-counterclockwise"
text: "Reset Configuration"
padding: [dp(0), dp(14), dp(0), dp(14)]
icon_size: dp(24)
font_size: dp(16)
size_hint: [1.0, None]
on_release: root.delegate.reset_config(self)
disabled: False
MDLabel: MDLabel:
id: config_template id: config_template

View File

@ -114,8 +114,8 @@ setuptools.setup(
] ]
}, },
install_requires=[ install_requires=[
"rns>=0.8.8", "rns>=0.9.2",
"lxmf>=0.5.8", "lxmf>=0.6.0",
"kivy>=2.3.0", "kivy>=2.3.0",
"pillow>=10.2.0", "pillow>=10.2.0",
"qrcode", "qrcode",
@ -123,6 +123,8 @@ setuptools.setup(
"ffpyplayer", "ffpyplayer",
"sh", "sh",
"numpy<=1.26.4", "numpy<=1.26.4",
"mistune>=3.0.2",
"beautifulsoup4",
"pycodec2;sys.platform!='Windows' and sys.platform!='win32' and sys.platform!='darwin'", "pycodec2;sys.platform!='Windows' and sys.platform!='win32' and sys.platform!='darwin'",
"pyaudio;sys.platform=='linux'", "pyaudio;sys.platform=='linux'",
"pyobjus;sys.platform=='darwin'", "pyobjus;sys.platform=='darwin'",

View File

@ -7,7 +7,7 @@ a = Analysis(
pathex=[], pathex=[],
binaries=[], binaries=[],
datas=[], datas=[],
hiddenimports=[], hiddenimports=["mistune", "bs4"],
hookspath=[], hookspath=[],
hooksconfig={}, hooksconfig={},
runtime_hooks=[], runtime_hooks=[],