mirror of
https://github.com/markqvist/Reticulum.git
synced 2026-06-15 17:11:55 -07:00
714 lines
30 KiB
Python
714 lines
30 KiB
Python
# MIT License
|
|
#
|
|
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
# copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
from .Interface import Interface
|
|
from time import sleep
|
|
import sys
|
|
import threading
|
|
import time
|
|
import math
|
|
import RNS
|
|
|
|
class KISS():
|
|
FEND = 0xC0
|
|
FESC = 0xDB
|
|
TFEND = 0xDC
|
|
TFESC = 0xDD
|
|
|
|
CMD_UNKNOWN = 0xFE
|
|
CMD_DATA = 0x00
|
|
CMD_FREQUENCY = 0x01
|
|
CMD_BANDWIDTH = 0x02
|
|
CMD_TXPOWER = 0x03
|
|
CMD_SF = 0x04
|
|
CMD_CR = 0x05
|
|
CMD_RADIO_STATE = 0x06
|
|
CMD_RADIO_LOCK = 0x07
|
|
CMD_DETECT = 0x08
|
|
CMD_LEAVE = 0x0A
|
|
CMD_READY = 0x0F
|
|
CMD_STAT_RX = 0x21
|
|
CMD_STAT_TX = 0x22
|
|
CMD_STAT_RSSI = 0x23
|
|
CMD_STAT_SNR = 0x24
|
|
CMD_BLINK = 0x30
|
|
CMD_RANDOM = 0x40
|
|
CMD_FB_EXT = 0x41
|
|
CMD_FB_READ = 0x42
|
|
CMD_FB_WRITE = 0x43
|
|
CMD_PLATFORM = 0x48
|
|
CMD_MCU = 0x49
|
|
CMD_FW_VERSION = 0x50
|
|
CMD_ROM_READ = 0x51
|
|
CMD_RESET = 0x55
|
|
|
|
DETECT_REQ = 0x73
|
|
DETECT_RESP = 0x46
|
|
|
|
RADIO_STATE_OFF = 0x00
|
|
RADIO_STATE_ON = 0x01
|
|
RADIO_STATE_ASK = 0xFF
|
|
|
|
CMD_ERROR = 0x90
|
|
ERROR_INITRADIO = 0x01
|
|
ERROR_TXFAILED = 0x02
|
|
ERROR_EEPROM_LOCKED = 0x03
|
|
|
|
PLATFORM_AVR = 0x90
|
|
PLATFORM_ESP32 = 0x80
|
|
|
|
@staticmethod
|
|
def escape(data):
|
|
data = data.replace(bytes([0xdb]), bytes([0xdb, 0xdd]))
|
|
data = data.replace(bytes([0xc0]), bytes([0xdb, 0xdc]))
|
|
return data
|
|
|
|
|
|
class RNodeInterface(Interface):
|
|
MAX_CHUNK = 32768
|
|
|
|
FREQ_MIN = 137000000
|
|
FREQ_MAX = 1020000000
|
|
|
|
RSSI_OFFSET = 157
|
|
|
|
CALLSIGN_MAX_LEN = 32
|
|
|
|
REQUIRED_FW_VER_MAJ = 1
|
|
REQUIRED_FW_VER_MIN = 52
|
|
|
|
RECONNECT_WAIT = 5
|
|
|
|
def __init__(self, owner, name, port, frequency = None, bandwidth = None, txpower = None, sf = None, cr = None, flow_control = False, id_interval = None, id_callsign = None):
|
|
if RNS.vendor.platformutils.is_android():
|
|
raise SystemError("Invlaid interface type. The Android-specific RNode interface must be used on Android")
|
|
|
|
import importlib
|
|
if importlib.util.find_spec('serial') != None:
|
|
import serial
|
|
else:
|
|
RNS.log("Using the RNode interface requires a serial communication module to be installed.", RNS.LOG_CRITICAL)
|
|
RNS.log("You can install one with the command: python3 -m pip install pyserial", RNS.LOG_CRITICAL)
|
|
RNS.panic()
|
|
|
|
self.rxb = 0
|
|
self.txb = 0
|
|
|
|
self.HW_MTU = 508
|
|
|
|
self.pyserial = serial
|
|
self.serial = None
|
|
self.owner = owner
|
|
self.name = name
|
|
self.port = port
|
|
self.speed = 115200
|
|
self.databits = 8
|
|
self.stopbits = 1
|
|
self.timeout = 100
|
|
self.online = False
|
|
self.detached = False
|
|
self.reconnecting= False
|
|
|
|
self.frequency = frequency
|
|
self.bandwidth = bandwidth
|
|
self.txpower = txpower
|
|
self.sf = sf
|
|
self.cr = cr
|
|
self.state = KISS.RADIO_STATE_OFF
|
|
self.bitrate = 0
|
|
self.platform = None
|
|
self.display = None
|
|
self.mcu = None
|
|
self.detected = False
|
|
self.firmware_ok = False
|
|
self.maj_version = 0
|
|
self.min_version = 0
|
|
|
|
self.last_id = 0
|
|
self.first_tx = None
|
|
self.reconnect_w = RNodeInterface.RECONNECT_WAIT
|
|
|
|
self.r_frequency = None
|
|
self.r_bandwidth = None
|
|
self.r_txpower = None
|
|
self.r_sf = None
|
|
self.r_cr = None
|
|
self.r_state = None
|
|
self.r_lock = None
|
|
self.r_stat_rx = None
|
|
self.r_stat_tx = None
|
|
self.r_stat_rssi = None
|
|
self.r_random = None
|
|
|
|
self.packet_queue = []
|
|
self.flow_control = flow_control
|
|
self.interface_ready = False
|
|
self.announce_rate_target = None
|
|
|
|
self.validcfg = True
|
|
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
|
|
RNS.log("Invalid frequency configured for "+str(self), RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
|
|
if (self.txpower < 0 or self.txpower > 17):
|
|
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
|
|
if (self.bandwidth < 7800 or self.bandwidth > 500000):
|
|
RNS.log("Invalid bandwidth configured for "+str(self), RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
|
|
if (self.sf < 7 or self.sf > 12):
|
|
RNS.log("Invalid spreading factor configured for "+str(self), RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
|
|
if (self.cr < 5 or self.cr > 8):
|
|
RNS.log("Invalid coding rate configured for "+str(self), RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
|
|
if id_interval != None and id_callsign != None:
|
|
if (len(id_callsign.encode("utf-8")) <= RNodeInterface.CALLSIGN_MAX_LEN):
|
|
self.should_id = True
|
|
self.id_callsign = id_callsign.encode("utf-8")
|
|
self.id_interval = id_interval
|
|
else:
|
|
RNS.log("The encoded ID callsign for "+str(self)+" exceeds the max length of "+str(RNodeInterface.CALLSIGN_MAX_LEN)+" bytes.", RNS.LOG_ERROR)
|
|
self.validcfg = False
|
|
else:
|
|
self.id_interval = None
|
|
self.id_callsign = None
|
|
|
|
if (not self.validcfg):
|
|
raise ValueError("The configuration for "+str(self)+" contains errors, interface is offline")
|
|
|
|
try:
|
|
self.open_port()
|
|
|
|
if self.serial.is_open:
|
|
self.configure_device()
|
|
else:
|
|
raise IOError("Could not open serial port")
|
|
|
|
except Exception as e:
|
|
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
|
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
RNS.log("Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
|
if not self.detached and not self.reconnecting:
|
|
thread = threading.Thread(target=self.reconnect_port)
|
|
thread.daemon = True
|
|
thread.start()
|
|
|
|
|
|
def open_port(self):
|
|
RNS.log("Opening serial port "+self.port+"...")
|
|
self.serial = self.pyserial.Serial(
|
|
port = self.port,
|
|
baudrate = self.speed,
|
|
bytesize = self.databits,
|
|
parity = self.pyserial.PARITY_NONE,
|
|
stopbits = self.stopbits,
|
|
xonxoff = False,
|
|
rtscts = False,
|
|
timeout = 0,
|
|
inter_byte_timeout = None,
|
|
write_timeout = None,
|
|
dsrdtr = False,
|
|
)
|
|
|
|
|
|
def configure_device(self):
|
|
self.r_frequency = None
|
|
self.r_bandwidth = None
|
|
self.r_txpower = None
|
|
self.r_sf = None
|
|
self.r_cr = None
|
|
self.r_state = None
|
|
self.r_lock = None
|
|
sleep(2.0)
|
|
|
|
thread = threading.Thread(target=self.readLoop)
|
|
thread.daemon = True
|
|
thread.start()
|
|
|
|
self.detect()
|
|
sleep(0.2)
|
|
|
|
if not self.detected:
|
|
RNS.log("Could not detect device for "+str(self), RNS.LOG_ERROR)
|
|
self.serial.close()
|
|
else:
|
|
if self.platform == KISS.PLATFORM_ESP32:
|
|
self.display = True
|
|
|
|
RNS.log("Serial port "+self.port+" is now open")
|
|
RNS.log("Configuring RNode interface...", RNS.LOG_VERBOSE)
|
|
self.initRadio()
|
|
if (self.validateRadioState()):
|
|
self.interface_ready = True
|
|
RNS.log(str(self)+" is configured and powered up")
|
|
sleep(0.3)
|
|
self.online = True
|
|
else:
|
|
RNS.log("After configuring "+str(self)+", the reported radio parameters did not match your configuration.", RNS. |