mirror of
https://github.com/markqvist/Reticulum.git
synced 2024-11-22 05:30:17 +00:00
Compare commits
5 Commits
1c56385473
...
9a8d46ab21
Author | SHA1 | Date | |
---|---|---|---|
|
9a8d46ab21 | ||
|
8adab7ee7d | ||
|
b5bde99322 | ||
|
560c8e164c | ||
|
312489e4dc |
@ -28,6 +28,9 @@ import time
|
||||
import math
|
||||
import RNS
|
||||
|
||||
from able import BluetoothDispatcher, GATT_SUCCESS
|
||||
from able.adapter import require_bluetooth_enabled
|
||||
|
||||
class KISS():
|
||||
FEND = 0xC0
|
||||
FESC = 0xDB
|
||||
@ -54,6 +57,7 @@ class KISS():
|
||||
CMD_STAT_SNR = 0x24
|
||||
CMD_STAT_CHTM = 0x25
|
||||
CMD_STAT_PHYPRM = 0x26
|
||||
CMD_STAT_BAT = 0x27
|
||||
CMD_BLINK = 0x30
|
||||
CMD_RANDOM = 0x40
|
||||
CMD_FB_EXT = 0x41
|
||||
@ -89,6 +93,10 @@ class KISS():
|
||||
return data
|
||||
|
||||
class AndroidBluetoothManager():
|
||||
DEVICE_TYPE_CLASSIC = 1
|
||||
DEVICE_TYPE_LE = 2
|
||||
DEVICE_TYPE_DUAL = 3
|
||||
|
||||
def __init__(self, owner, target_device_name = None, target_device_address = None):
|
||||
from jnius import autoclass
|
||||
self.owner = owner
|
||||
@ -237,6 +245,11 @@ class RNodeInterface(Interface):
|
||||
Q_SNR_MAX = 6
|
||||
Q_SNR_STEP = 2
|
||||
|
||||
BATTERY_STATE_UNKNOWN = 0x00
|
||||
BATTERY_STATE_DISCHARGING = 0x01
|
||||
BATTERY_STATE_CHARGING = 0x02
|
||||
BATTERY_STATE_CHARGED = 0x03
|
||||
|
||||
@classmethod
|
||||
def bluetooth_control(device_serial = None, port = None, enable_bluetooth = False, disable_bluetooth = False, pairing_mode = False):
|
||||
if (port != None or device_serial != None) and (enable_bluetooth or disable_bluetooth or pairing_mode):
|
||||
@ -321,7 +334,8 @@ class RNodeInterface(Interface):
|
||||
self, owner, name, port, frequency = None, bandwidth = None, txpower = None,
|
||||
sf = None, cr = None, flow_control = False, id_interval = None,
|
||||
allow_bluetooth = False, target_device_name = None,
|
||||
target_device_address = None, id_callsign = None, st_alock = None, lt_alock = None):
|
||||
target_device_address = None, id_callsign = None, st_alock = None, lt_alock = None,
|
||||
ble_addr = None, ble_name = None, force_ble=False):
|
||||
import importlib
|
||||
if RNS.vendor.platformutils.is_android():
|
||||
self.on_android = True
|
||||
@ -368,9 +382,19 @@ class RNodeInterface(Interface):
|
||||
self.stopbits = 1
|
||||
self.timeout = 150
|
||||
self.online = False
|
||||
self.detached = False
|
||||
self.hw_errors = []
|
||||
self.allow_bluetooth = allow_bluetooth
|
||||
|
||||
self.use_ble = False
|
||||
self.ble_name = ble_name
|
||||
self.ble_addr = ble_addr
|
||||
self.ble = None
|
||||
self.ble_rx_lock = threading.Lock()
|
||||
self.ble_tx_lock = threading.Lock()
|
||||
self.ble_rx_queue= b""
|
||||
self.ble_tx_queue= b""
|
||||
|
||||
self.frequency = frequency
|
||||
self.bandwidth = bandwidth
|
||||
self.txpower = txpower
|
||||
@ -414,6 +438,8 @@ class RNodeInterface(Interface):
|
||||
self.r_symbol_rate = None
|
||||
self.r_preamble_symbols = None
|
||||
self.r_premable_time_ms = None
|
||||
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
||||
self.r_battery_percent = 0
|
||||
|
||||
self.packet_queue = []
|
||||
self.flow_control = flow_control
|
||||
@ -423,6 +449,9 @@ class RNodeInterface(Interface):
|
||||
self.port_io_timeout = RNodeInterface.PORT_IO_TIMEOUT
|
||||
self.last_imagedata = None
|
||||
|
||||
if force_ble or self.ble_addr != None or self.ble_name != None:
|
||||
self.use_ble = True
|
||||
|
||||
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)
|
||||
@ -515,6 +544,7 @@ class RNodeInterface(Interface):
|
||||
raise IOError("No ports available for writing")
|
||||
|
||||
def open_port(self):
|
||||
if not self.use_ble:
|
||||
if self.port != None:
|
||||
RNS.log("Opening serial port "+self.port+"...")
|
||||
# Get device parameters
|
||||
@ -582,6 +612,15 @@ class RNodeInterface(Interface):
|
||||
if self.bt_manager != None:
|
||||
self.bt_manager.connect_any_device()
|
||||
|
||||
else:
|
||||
if self.ble == None:
|
||||
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
||||
self.serial = self.ble
|
||||
|
||||
open_time = time.time()
|
||||
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def configure_device(self):
|
||||
sleep(2.0)
|
||||
@ -590,7 +629,17 @@ class RNodeInterface(Interface):
|
||||
thread.start()
|
||||
|
||||
self.detect()
|
||||
if not self.use_ble:
|
||||
sleep(0.5)
|
||||
else:
|
||||
ble_detect_timeout = 5
|
||||
detect_time = time.time()
|
||||
while not self.detected and time.time() < detect_time + ble_detect_timeout:
|
||||
time.sleep(0.1)
|
||||
if self.detected:
|
||||
detect_time = RNS.prettytime(time.time()-detect_time)
|
||||
else:
|
||||
RNS.log(f"RNode detect timed out over {self.port}", RNS.LOG_ERROR)
|
||||
|
||||
if not self.detected:
|
||||
raise IOError("Could not detect device")
|
||||
@ -654,6 +703,9 @@ class RNodeInterface(Interface):
|
||||
self.setRadioState(KISS.RADIO_STATE_ON)
|
||||
time.sleep(0.15)
|
||||
|
||||
if self.use_ble:
|
||||
time.sleep(1)
|
||||
|
||||
def detect(self):
|
||||
kiss_command = bytes([KISS.FEND, KISS.CMD_DETECT, KISS.DETECT_REQ, KISS.FEND, KISS.CMD_FW_VERSION, 0x00, KISS.FEND, KISS.CMD_PLATFORM, 0x00, KISS.FEND, KISS.CMD_MCU, 0x00, KISS.FEND])
|
||||
written = self.write_mux(kiss_command)
|
||||
@ -1140,6 +1192,25 @@ class RNodeInterface(Interface):
|
||||
RNS.log(str(self)+" Radio reporting symbol time is "+str(round(self.r_symbol_time_ms,2))+"ms (at "+str(self.r_symbol_rate)+" baud)", RNS.LOG_DEBUG)
|
||||
RNS.log(str(self)+" Radio reporting preamble is "+str(self.r_preamble_symbols)+" symbols ("+str(self.r_premable_time_ms)+"ms)", RNS.LOG_DEBUG)
|
||||
RNS.log(str(self)+" Radio reporting CSMA slot time is "+str(self.r_csma_slot_time_ms)+"ms", RNS.LOG_DEBUG)
|
||||
elif (command == KISS.CMD_STAT_BAT):
|
||||
if (byte == KISS.FESC):
|
||||
escape = True
|
||||
else:
|
||||
if (escape):
|
||||
if (byte == KISS.TFEND):
|
||||
byte = KISS.FEND
|
||||
if (byte == KISS.TFESC):
|
||||
byte = KISS.FESC
|
||||
escape = False
|
||||
command_buffer = command_buffer+bytes([byte])
|
||||
if (len(command_buffer) == 2):
|
||||
bat_percent = command_buffer[1]
|
||||
if bat_percent > 100:
|
||||
bat_percent = 100
|
||||
if bat_percent < 0:
|
||||
bat_percent = 0
|
||||
self.r_battery_state = command_buffer[0]
|
||||
self.r_battery_percent = bat_percent
|
||||
elif (command == KISS.CMD_RANDOM):
|
||||
self.r_random = byte
|
||||
elif (command == KISS.CMD_PLATFORM):
|
||||
@ -1212,6 +1283,7 @@ class RNodeInterface(Interface):
|
||||
if self.bt_manager != None:
|
||||
self.bt_manager.close()
|
||||
|
||||
if not self.detached:
|
||||
self.reconnect_port()
|
||||
|
||||
def reconnect_port(self):
|
||||
@ -1247,13 +1319,230 @@ class RNodeInterface(Interface):
|
||||
RNS.log("Reconnected serial port for "+str(self))
|
||||
|
||||
def detach(self):
|
||||
self.detached = True
|
||||
self.disable_external_framebuffer()
|
||||
self.setRadioState(KISS.RADIO_STATE_OFF)
|
||||
self.leave()
|
||||
|
||||
if self.use_ble:
|
||||
self.ble.close()
|
||||
|
||||
def should_ingress_limit(self):
|
||||
return False
|
||||
|
||||
def get_battery_state(self):
|
||||
return self.r_battery_state
|
||||
|
||||
def get_battery_state_string(self):
|
||||
if self.r_battery_state == RNodeInterface.BATTERY_STATE_CHARGED:
|
||||
return "charged"
|
||||
elif self.r_battery_state == RNodeInterface.BATTERY_STATE_CHARGING:
|
||||
return "charging"
|
||||
elif self.r_battery_state == RNodeInterface.BATTERY_STATE_DISCHARGING:
|
||||
return "discharging"
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
def get_battery_percent(self):
|
||||
return self.r_battery_percent
|
||||
|
||||
def ble_receive(self, data):
|
||||
with self.ble_rx_lock:
|
||||
self.ble_rx_queue += data
|
||||
|
||||
def ble_waiting(self):
|
||||
return len(self.ble_tx_queue) > 0
|
||||
|
||||
def get_ble_waiting(self, n):
|
||||
with self.ble_tx_lock:
|
||||
data = self.ble_tx_queue[:n]
|
||||
self.ble_tx_queue = self.ble_tx_queue[n:]
|
||||
return data
|
||||
|
||||
def __str__(self):
|
||||
return "RNodeInterface["+str(self.name)+"]"
|
||||
|
||||
class BLEConnection(BluetoothDispatcher):
|
||||
UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
|
||||
UART_RX_CHAR_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
|
||||
UART_TX_CHAR_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
|
||||
MAX_GATT_ATTR_LEN = 512
|
||||
|
||||
SCAN_TIMEOUT = 2.0
|
||||
CONNECT_TIMEOUT = 7.0
|
||||
|
||||
@property
|
||||
def is_open(self):
|
||||
return self.connected
|
||||
|
||||
@property
|
||||
def in_waiting(self):
|
||||
return len(self.owner.ble_rx_queue) > 0
|
||||
|
||||
def write(self, data_bytes):
|
||||
with self.owner.ble_tx_lock:
|
||||
self.owner.ble_tx_queue += data_bytes
|
||||
return len(data_bytes)
|
||||
|
||||
def read(self):
|
||||
with self.owner.ble_rx_lock:
|
||||
data = self.owner.ble_rx_queue
|
||||
self.owner.ble_rx_queue = b""
|
||||
return data
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
if self.connected:
|
||||
RNS.log(f"Disconnecting BLE device from {self.owner}", RNS.LOG_DEBUG)
|
||||
RNS.log("Waiting for BLE write buffer to empty...")
|
||||
while self.owner.ble_waiting():
|
||||
time.sleep(0.1)
|
||||
RNS.log("Writing concluded")
|
||||
|
||||
self.rx_char = None
|
||||
self.tx_char = None
|
||||
|
||||
RNS.log("Waiting for write thread to finish...")
|
||||
while self.write_thread != None:
|
||||
time.sleep(0.1)
|
||||
|
||||
RNS.log("Writing finished, closing GATT connection")
|
||||
self.close_gatt()
|
||||
|
||||
with self.owner.ble_rx_lock:
|
||||
self.owner.ble_rx_queue = b""
|
||||
|
||||
with self.owner.ble_tx_lock:
|
||||
self.owner.ble_tx_queue = b""
|
||||
|
||||
except Exception as e:
|
||||
RNS.log("An error occurred while closing BLE connection for {self.owner}: {e}", RNS.LOG_ERROR)
|
||||
RNS.trace_exception(e)
|
||||
|
||||
def __init__(self, owner=None, target_name=None, target_bt_addr=None):
|
||||
super(BLEConnection, self).__init__()
|
||||
self.owner = owner
|
||||
self.target_name = target_name
|
||||
self.target_bt_addr = target_bt_addr
|
||||
self.scan_timeout = BLEConnection.SCAN_TIMEOUT
|
||||
self.connect_timeout = BLEConnection.CONNECT_TIMEOUT
|
||||
self.ble_device = None
|
||||
self.connected = False
|
||||
self.running = False
|
||||
self.should_run = False
|
||||
self.connect_job_running = False
|
||||
self.write_thread = None
|
||||
self.mtu = 20
|
||||
|
||||
self.bt_manager = AndroidBluetoothManager(owner=self)
|
||||
|
||||
self.should_run = True
|
||||
self.connection_thread = threading.Thread(target=self.connection_job, daemon=True).start()
|
||||
|
||||
def write_loop(self):
|
||||
try:
|
||||
while self.connected and self.rx_char != None:
|
||||
if self.owner.ble_waiting():
|
||||
data = self.owner.get_ble_waiting(self.mtu)
|
||||
self.write_characteristic(self.rx_char, data)
|
||||
else:
|
||||
time.sleep(0.1)
|
||||
|
||||
except Exception as e:
|
||||
RNS.log("An error occurred in {self} write loop: {e}", RNS.LOG_ERROR)
|
||||
RNS.trace_exception(e)
|
||||
|
||||
self.write_thread = None
|
||||
|
||||
def connection_job(self):
|
||||
while self.should_run:
|
||||
if self.bt_manager.bt_enabled():
|
||||
if self.ble_device == None:
|
||||
self.ble_device = self.find_target_device()
|
||||
|
||||
if self.ble_device != None:
|
||||
if not self.connected:
|
||||
self.connect_device()
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
def connect_device(self):
|
||||
if self.ble_device != None and self.bt_manager.bt_enabled():
|
||||
RNS.log(f"Trying to connect BLE device {self.ble_device.getName()} / {self.ble_device.getAddress()} for {self.owner}...", RNS.LOG_DEBUG)
|
||||
self.connect_by_device_address(self.ble_device.getAddress())
|
||||
end = time.time() + BLEConnection.CONNECT_TIMEOUT
|
||||
while time.time() < end and not self.connected:
|
||||
time.sleep(0.25)
|
||||
|
||||
if self.connected:
|
||||
self.owner.port = f"ble://{self.ble_device.getAddress()}"
|
||||
self.write_thread = threading.Thread(target=self.write_loop, daemon=True)
|
||||
self.write_thread.start()
|
||||
else:
|
||||
RNS.log(f"BLE device connection timed out for {self.owner}", RNS.LOG_DEBUG)
|
||||
self.close_gatt()
|
||||
|
||||
self.connect_job_running = False
|
||||
|
||||
def device_disconnected(self):
|
||||
RNS.log(f"BLE device for {self.owner} disconnected", RNS.LOG_NOTICE)
|
||||
self.connected = False
|
||||
self.ble_device = None
|
||||
self.close_gatt()
|
||||
|
||||
def find_target_device(self):
|
||||
found_device = None
|
||||
potential_devices = self.bt_manager.get_paired_devices()
|
||||
|
||||
if self.target_bt_addr != None:
|
||||
for device in potential_devices:
|
||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||
if str(device.getAddress()).replace(":", "").lower() == str(self.target_bt_addr).replace(":", "").lower():
|
||||
found_device = device
|
||||
break
|
||||
|
||||
if not found_device and self.target_name != None:
|
||||
for device in potential_devices:
|
||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||
if device.getName().lower() == self.target_name.lower():
|
||||
found_device = device
|
||||
break
|
||||
|
||||
if not found_device:
|
||||
for device in potential_devices:
|
||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||
if device.getName().startswith("RNode "):
|
||||
found_device = device
|
||||
break
|
||||
|
||||
RNS.log("Found device "+str(found_device))
|
||||
|
||||
return found_device
|
||||
|
||||
def on_connection_state_change(self, status, state):
|
||||
if status == GATT_SUCCESS and state:
|
||||
self.discover_services()
|
||||
else:
|
||||
self.device_disconnected()
|
||||
|
||||
def on_services(self, status, services):
|
||||
self.request_mtu(BLEConnection.MAX_GATT_ATTR_LEN)
|
||||
self.rx_char = services.search(BLEConnection.UART_RX_CHAR_UUID)
|
||||
|
||||
if self.rx_char is not None:
|
||||
self.tx_char = services.search(BLEConnection.UART_TX_CHAR_UUID)
|
||||
|
||||
if self.tx_char is not None:
|
||||
if self.enable_notifications(self.tx_char):
|
||||
RNS.log("Enabled notifications for BLE TX characteristic")
|
||||
self.connected = True
|
||||
|
||||
def on_mtu_changed(self, mtu, status):
|
||||
if status == GATT_SUCCESS:
|
||||
self.mtu = min(mtu-5, BLEConnection.MAX_GATT_ATTR_LEN)
|
||||
RNS.log(f"BLE MTU updated to {self.mtu} for {self.owner}", RNS.LOG_DEBUG)
|
||||
|
||||
def on_characteristic_changed(self, characteristic):
|
||||
if characteristic.getUuid().toString() == BLEConnection.UART_TX_CHAR_UUID:
|
||||
recvd = bytes(characteristic.getValue())
|
||||
self.owner.ble_receive(recvd)
|
@ -54,6 +54,7 @@ class KISS():
|
||||
CMD_STAT_SNR = 0x24
|
||||
CMD_STAT_CHTM = 0x25
|
||||
CMD_STAT_PHYPRM = 0x26
|
||||
CMD_STAT_BAT = 0x27
|
||||
CMD_BLINK = 0x30
|
||||
CMD_RANDOM = 0x40
|
||||
CMD_FB_EXT = 0x41
|
||||
@ -107,7 +108,12 @@ class RNodeInterface(Interface):
|
||||
Q_SNR_MAX = 6
|
||||
Q_SNR_STEP = 2
|
||||
|
||||
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, st_alock = None, lt_alock = None):
|
||||
BATTERY_STATE_UNKNOWN = 0x00
|
||||
BATTERY_STATE_DISCHARGING = 0x01
|
||||
BATTERY_STATE_CHARGING = 0x02
|
||||
BATTERY_STATE_CHARGED = 0x03
|
||||
|
||||
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, st_alock = None, lt_alock = None, ble_addr = None, ble_name = None, force_ble=False):
|
||||
if RNS.vendor.platformutils.is_android():
|
||||
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
||||
|
||||
@ -136,6 +142,15 @@ class RNodeInterface(Interface):
|
||||
self.detached = False
|
||||
self.reconnecting= False
|
||||
|
||||
self.use_ble = False
|
||||
self.ble_name = ble_name
|
||||
self.ble_addr = ble_addr
|
||||
self.ble = None
|
||||
self.ble_rx_lock = threading.Lock()
|
||||
self.ble_tx_lock = threading.Lock()
|
||||
self.ble_rx_queue= b""
|
||||
self.ble_tx_queue= b""
|
||||
|
||||
self.frequency = frequency
|
||||
self.bandwidth = bandwidth
|
||||
self.txpower = txpower
|
||||
@ -179,12 +194,17 @@ class RNodeInterface(Interface):
|
||||
self.r_symbol_rate = None
|
||||
self.r_preamble_symbols = None
|
||||
self.r_premable_time_ms = None
|
||||
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
||||
self.r_battery_percent = 0
|
||||
|
||||
self.packet_queue = []
|
||||
self.flow_control = flow_control
|
||||
self.interface_ready = False
|
||||
self.announce_rate_target = None
|
||||
|
||||
if force_ble or self.ble_addr != None or self.ble_name != None:
|
||||
self.use_ble = True
|
||||
|
||||
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)
|
||||
@ -248,6 +268,7 @@ class RNodeInterface(Interface):
|
||||
|
||||
|
||||
def open_port(self):
|
||||
if not self.use_ble:
|
||||
RNS.log("Opening serial port "+self.port+"...")
|
||||
self.serial = self.pyserial.Serial(
|
||||
port = self.port,
|
||||
@ -263,6 +284,16 @@ class RNodeInterface(Interface):
|
||||
dsrdtr = False,
|
||||
)
|
||||
|
||||
else:
|
||||
RNS.log(f"Opening BLE connection for {self}...")
|
||||
if self.ble == None:
|
||||
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
||||
self.serial = self.ble
|
||||
|
||||
open_time = time.time()
|
||||
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def configure_device(self):
|
||||
self.r_frequency = None
|
||||
@ -279,7 +310,17 @@ class RNodeInterface(Interface):
|
||||
thread.start()
|
||||
|
||||
self.detect()
|
||||
if not self.use_ble:
|
||||
sleep(0.2)
|
||||
else:
|
||||
ble_detect_timeout = 5
|
||||
detect_time = time.time()
|
||||
while not self.detected and time.time() < detect_time + ble_detect_timeout:
|
||||
time.sleep(0.1)
|
||||
if self.detected:
|
||||
detect_time = RNS.prettytime(time.time()-detect_time)
|
||||
else:
|
||||
RNS.log(f"RNode detect timed out over {self.port}", RNS.LOG_ERROR)
|
||||
|
||||
if not self.detected:
|
||||
RNS.log("Could not detect device for "+str(self), RNS.LOG_ERROR)
|
||||
@ -313,6 +354,9 @@ class RNodeInterface(Interface):
|
||||
self.setLTALock()
|
||||
self.setRadioState(KISS.RADIO_STATE_ON)
|
||||
|
||||
if self.use_ble:
|
||||
time.sleep(2)
|
||||
|
||||
def detect(self):
|
||||
kiss_command = bytes([KISS.FEND, KISS.CMD_DETECT, KISS.DETECT_REQ, KISS.FEND, KISS.CMD_FW_VERSION, 0x00, KISS.FEND, KISS.CMD_PLATFORM, 0x00, KISS.FEND, KISS.CMD_MCU, 0x00, KISS.FEND])
|
||||
written = self.serial.write(kiss_command)
|
||||
@ -765,6 +809,25 @@ class RNodeInterface(Interface):
|
||||
RNS.log(str(self)+" Radio reporting symbol time is "+str(round(self.r_symbol_time_ms,2))+"ms (at "+str(self.r_symbol_rate)+" baud)", RNS.LOG_DEBUG)
|
||||
RNS.log(str(self)+" Radio reporting preamble is "+str(self.r_preamble_symbols)+" symbols ("+str(self.r_premable_time_ms)+"ms)", RNS.LOG_DEBUG)
|
||||
RNS.log(str(self)+" Radio reporting CSMA slot time is "+str(self.r_csma_slot_time_ms)+"ms", RNS.LOG_DEBUG)
|
||||
elif (command == KISS.CMD_STAT_BAT):
|
||||
if (byte == KISS.FESC):
|
||||
escape = True
|
||||
else:
|
||||
if (escape):
|
||||
if (byte == KISS.TFEND):
|
||||
byte = KISS.FEND
|
||||
if (byte == KISS.TFESC):
|
||||
byte = KISS.FESC
|
||||
escape = False
|
||||
command_buffer = command_buffer+bytes([byte])
|
||||
if (len(command_buffer) == 2):
|
||||
bat_percent = command_buffer[1]
|
||||
if bat_percent > 100:
|
||||
bat_percent = 100
|
||||
if bat_percent < 0:
|
||||
bat_percent = 0
|
||||
self.r_battery_state = command_buffer[0]
|
||||
self.r_battery_percent = bat_percent
|
||||
elif (command == KISS.CMD_RANDOM):
|
||||
self.r_random = byte
|
||||
elif (command == KISS.CMD_PLATFORM):
|
||||
@ -853,8 +916,194 @@ class RNodeInterface(Interface):
|
||||
self.setRadioState(KISS.RADIO_STATE_OFF)
|
||||
self.leave()
|
||||
|
||||
if self.use_ble:
|
||||
self.ble.close()
|
||||
|
||||
def should_ingress_limit(self):
|
||||
return False
|
||||
|
||||
def get_battery_state(self):
|
||||
return self.r_battery_state
|
||||
|
||||
def get_battery_state_string(self):
|
||||
if self.r_battery_state == RNodeInterface.BATTERY_STATE_CHARGED:
|
||||
return "charged"
|
||||
elif self.r_battery_state == RNodeInterface.BATTERY_STATE_CHARGING:
|
||||
return "charging"
|
||||
elif self.r_battery_state == RNodeInterface.BATTERY_STATE_DISCHARGING:
|
||||
return "discharging"
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
def get_battery_percent(self):
|
||||
return self.r_battery_percent
|
||||
|
||||
def ble_receive(self, data):
|
||||
with self.ble_rx_lock:
|
||||
self.ble_rx_queue += data
|
||||
|
||||
def ble_waiting(self):
|
||||
return len(self.ble_tx_queue) > 0
|
||||
|
||||
def get_ble_waiting(self, n):
|
||||
with self.ble_tx_lock:
|
||||
data = self.ble_tx_queue[:n]
|
||||
self.ble_tx_queue = self.ble_tx_queue[n:]
|
||||
return data
|
||||
|
||||
def __str__(self):
|
||||
return "RNodeInterface["+str(self.name)+"]"
|
||||
|
||||
class BLEConnection():
|
||||
UART_SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
UART_RX_CHAR_UUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
UART_TX_CHAR_UUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
bleak = None
|
||||
|
||||
SCAN_TIMEOUT = 2.0
|
||||
CONNECT_TIMEOUT = 5.0
|
||||
|
||||
@property
|
||||
def is_open(self):
|
||||
return self.connected
|
||||
|
||||
@property
|
||||
def in_waiting(self):
|
||||
buflen = len(self.owner.ble_rx_queue)
|
||||
return buflen > 0
|
||||
|
||||
def write(self, data_bytes):
|
||||
with self.owner.ble_tx_lock:
|
||||
self.owner.ble_tx_queue += data_bytes
|
||||
return len(data_bytes)
|
||||
|
||||
def read(self, n):
|
||||
with self.owner.ble_rx_lock:
|
||||
data = self.owner.ble_rx_queue[:n]
|
||||
self.owner.ble_rx_queue = self.owner.ble_rx_queue[n:]
|
||||
return data
|
||||
|
||||
def close(self):
|
||||
if self.connected and self.ble_device:
|
||||
RNS.log(f"Disconnecting BLE device from {self.owner}", RNS.LOG_DEBUG)
|
||||
self.must_disconnect = True
|
||||
|
||||
while self.connect_job_running:
|
||||
time.sleep(0.1)
|
||||
|
||||
def __init__(self, owner=None, target_name=None, target_bt_addr=None):
|
||||
self.owner = owner
|
||||
self.target_name = target_name
|
||||
self.target_bt_addr = target_bt_addr
|
||||
self.scan_timeout = BLEConnection.SCAN_TIMEOUT
|
||||
self.ble_device = None
|
||||
self.connected = False
|
||||
self.running = False
|
||||
self.should_run = False
|
||||
self.must_disconnect = False
|
||||
self.connect_job_running = False
|
||||
|
||||
import importlib
|
||||
if BLEConnection.bleak == None:
|
||||
if importlib.util.find_spec("bleak") != None:
|
||||
import bleak
|
||||
BLEConnection.bleak = bleak
|
||||
|
||||
import asyncio
|
||||
BLEConnection.asyncio = asyncio
|
||||
else:
|
||||
RNS.log("Using the RNode interface over BLE requires a the \"bleak\" module to be installed.", RNS.LOG_CRITICAL)
|
||||
RNS.log("You can install one with the command: python3 -m pip install bleak", RNS.LOG_CRITICAL)
|
||||
RNS.panic()
|
||||
|
||||
self.should_run = True
|
||||
self.connection_thread = threading.Thread(target=self.connection_job, daemon=True).start()
|
||||
|
||||
def connection_job(self):
|
||||
while (self.should_run):
|
||||
if self.ble_device == None:
|
||||
self.ble_device = self.find_target_device()
|
||||
|
||||
if type(self.ble_device) == self.bleak.backends.device.BLEDevice:
|
||||
if not self.connected:
|
||||
self.connect_device()
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
def connect_device(self):
|
||||
if self.ble_device != None and type(self.ble_device) == self.bleak.backends.device.BLEDevice:
|
||||
RNS.log(f"Connecting BLE device {self.ble_device} for {self.owner}...", RNS.LOG_DEBUG)
|
||||
|
||||
async def connect_job():
|
||||
self.connect_job_running = True
|
||||
async with self.bleak.BleakClient(self.ble_device, disconnected_callback=self.device_disconnected) as ble_client:
|
||||
def handle_rx(device, data):
|
||||
if self.owner != None:
|
||||
self.owner.ble_receive(data)
|
||||
|
||||
self.connected = True
|
||||
self.ble_device = ble_client
|
||||
self.owner.port = str(f"ble://{ble_client.address}")
|
||||
|
||||
loop = self.asyncio.get_running_loop()
|
||||
uart_service = ble_client.services.get_service(BLEConnection.UART_SERVICE_UUID)
|
||||
rx_characteristic = uart_service.get_characteristic(BLEConnection.UART_RX_CHAR_UUID)
|
||||
await ble_client.start_notify(BLEConnection.UART_TX_CHAR_UUID, handle_rx)
|
||||
|
||||
while self.connected:
|
||||
if self.owner != None and self.owner.ble_waiting():
|
||||
outbound_data = self.owner.get_ble_waiting(rx_characteristic.max_write_without_response_size)
|
||||
await ble_client.write_gatt_char(rx_characteristic, outbound_data, response=False)
|
||||
elif self.must_disconnect:
|
||||
await ble_client.disconnect()
|
||||
else:
|
||||
await self.asyncio.sleep(0.1)
|
||||
|
||||
|
||||
try:
|
||||
self.asyncio.run(connect_job())
|
||||
except Exception as e:
|
||||
RNS.log(f"Could not connect BLE device {self.ble_device} for {self.owner}. Possibly missing authentication.", RNS.LOG_ERROR)
|
||||
self.connect_job_running = False
|
||||
|
||||
def device_disconnected(self, device):
|
||||
RNS.log(f"BLE device for {self.owner} disconnected", RNS.LOG_NOTICE)
|
||||
self.connected = False
|
||||
self.ble_device = None
|
||||
|
||||
def find_target_device(self):
|
||||
RNS.log(f"Searching for attachable BLE device for {self.owner}...", RNS.LOG_EXTREME)
|
||||
def device_filter(device: self.bleak.backends.device.BLEDevice, adv: self.bleak.backends.scanner.AdvertisementData):
|
||||
if BLEConnection.UART_SERVICE_UUID.lower() in adv.service_uuids:
|
||||
if self.device_bonded(device):
|
||||
if self.target_bt_addr == None and self.target_name == None:
|
||||
if device.name.startswith("RNode "):
|
||||
return True
|
||||
|
||||
if self.target_bt_addr == None or (device.address != None and device.address == self.target_bt_addr):
|
||||
if self.target_name == None or (device.name != None and device.name == self.target_name):
|
||||
return True
|
||||
|
||||
else:
|
||||
if self.target_bt_addr != None and device.address == self.target_bt_addr:
|
||||
RNS.log(f"Can't connect to target device {self.target_bt_addr} over BLE, device is not bonded", RNS.LOG_ERROR)
|
||||
|
||||
elif self.target_name != None and device.name == self.target_name:
|
||||
RNS.log(f"Can't connect to target device {self.target_name} over BLE, device is not bonded", RNS.LOG_ERROR)
|
||||
|
||||
return False
|
||||
|
||||
device = self.asyncio.run(self.bleak.BleakScanner.find_device_by_filter(device_filter, timeout=self.scan_timeout))
|
||||
return device
|
||||
|
||||
def device_bonded(self, device):
|
||||
try:
|
||||
if hasattr(device, "details"):
|
||||
if "props" in device.details and "Bonded" in device.details["props"]:
|
||||
if device.details["props"]["Bonded"] == True:
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
RNS.log(f"Error while determining device bond status for {device}, the contained exception was: {e}", RNS.LOG_ERROR)
|
||||
|
||||
return False
|
||||
|
@ -916,11 +916,28 @@ class Reticulum:
|
||||
st_alock = float(c["airtime_limit_short"]) if "airtime_limit_short" in c else None
|
||||
lt_alock = float(c["airtime_limit_long"]) if "airtime_limit_long" in c else None
|
||||
|
||||
force_ble = False
|
||||
ble_name = None
|
||||
ble_addr = None
|
||||
|
||||
port = c["port"] if "port" in c else None
|
||||
|
||||
if port == None:
|
||||
raise ValueError("No port specified for RNode interface")
|
||||
|
||||
if port != None:
|
||||
ble_uri_scheme = "ble://"
|
||||
if port.lower().startswith(ble_uri_scheme):
|
||||
force_ble = True
|
||||
ble_string = port[len(ble_uri_scheme):]
|
||||
port = None
|
||||
if len(ble_string) == 0:
|
||||
pass
|
||||
elif len(ble_string.split(":")) == 6 and len(ble_string) == 17:
|
||||
ble_addr = ble_string
|
||||
else:
|
||||
ble_name = ble_string
|
||||
|
||||
interface = RNodeInterface.RNodeInterface(
|
||||
RNS.Transport,
|
||||
name,
|
||||
@ -934,7 +951,10 @@ class Reticulum:
|
||||
id_interval = id_interval,
|
||||
id_callsign = id_callsign,
|
||||
st_alock = st_alock,
|
||||
lt_alock = lt_alock
|
||||
lt_alock = lt_alock,
|
||||
ble_addr = ble_addr,
|
||||
ble_name = ble_name,
|
||||
force_ble = force_ble,
|
||||
)
|
||||
|
||||
if "outgoing" in c and c.as_bool("outgoing") == False:
|
||||
@ -1299,6 +1319,13 @@ class Reticulum:
|
||||
if hasattr(interface, "r_channel_load_long"):
|
||||
ifstats["channel_load_long"] = interface.r_channel_load_long
|
||||
|
||||
if hasattr(interface, "r_battery_state"):
|
||||
if interface.r_battery_state != 0x00:
|
||||
ifstats["battery_state"] = interface.r_battery_state
|
||||
|
||||
if hasattr(interface, "r_battery_percent"):
|
||||
ifstats["battery_percent"] = interface.r_battery_percent
|
||||
|
||||
if hasattr(interface, "bitrate"):
|
||||
if interface.bitrate != None:
|
||||
ifstats["bitrate"] = interface.bitrate
|
||||
|
@ -292,6 +292,13 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
|
||||
if "bitrate" in ifstat and ifstat["bitrate"] != None:
|
||||
print(" Rate : {ss}".format(ss=speed_str(ifstat["bitrate"])))
|
||||
|
||||
if "battery_percent" in ifstat and ifstat["battery_percent"] != None:
|
||||
try:
|
||||
bpi = int(ifstat["battery_percent"])
|
||||
print(" Battery : {bp}%".format(bp=bpi))
|
||||
except:
|
||||
pass
|
||||
|
||||
if "airtime_short" in ifstat and "airtime_long" in ifstat:
|
||||
print(" Airtime : {ats}% (15s), {atl}% (1h)".format(ats=str(ifstat["airtime_short"]),atl=str(ifstat["airtime_long"])))
|
||||
|
||||
|
@ -1 +1 @@
|
||||
__version__ = "0.8.0"
|
||||
__version__ = "0.8.1"
|
||||
|
Loading…
Reference in New Issue
Block a user