@@ -290,6 +290,11 @@ def open_port(self):
290
290
291
291
else :
292
292
RNS .log (f"Opening BLE connection for { self } ..." )
293
+ if self .ble != None and self .ble .running == False :
294
+ self .ble .close ()
295
+ self .ble .cleanup ()
296
+ self .ble = None
297
+
293
298
if self .ble == None :
294
299
self .ble = BLEConnection (owner = self , target_name = self .ble_name , target_bt_addr = self .ble_addr )
295
300
self .serial = self .ble
@@ -298,15 +303,18 @@ def open_port(self):
298
303
while not self .ble .connected and time .time () < open_time + self .ble .CONNECT_TIMEOUT :
299
304
time .sleep (1 )
300
305
301
-
302
- def configure_device (self ):
306
+ def reset_radio_state (self ):
303
307
self .r_frequency = None
304
308
self .r_bandwidth = None
305
309
self .r_txpower = None
306
310
self .r_sf = None
307
311
self .r_cr = None
308
312
self .r_state = None
309
313
self .r_lock = None
314
+ self .detected = False
315
+
316
+ def configure_device (self ):
317
+ self .reset_radio_state ()
310
318
sleep (2.0 )
311
319
312
320
thread = threading .Thread (target = self .readLoop )
@@ -513,7 +521,14 @@ def validate_firmware(self):
513
521
514
522
def validateRadioState (self ):
515
523
RNS .log ("Waiting for radio configuration validation for " + str (self )+ "..." , RNS .LOG_VERBOSE )
516
- sleep (0.25 );
524
+ if self .use_ble :
525
+ sleep (1.00 )
526
+ else :
527
+ sleep (0.25 )
528
+
529
+ if self .use_ble and self .ble != None and self .ble .device_disappeared :
530
+ RNS .log (f"Device disappeared during radio state validation for { self } " , RNS .LOG_ERROR )
531
+ return False
517
532
518
533
self .validcfg = True
519
534
if (self .r_frequency != None and abs (self .frequency - int (self .r_frequency )) > 100 ):
@@ -1010,11 +1025,13 @@ def __init__(self, owner=None, target_name=None, target_bt_addr=None):
1010
1025
self .target_bt_addr = target_bt_addr
1011
1026
self .scan_timeout = BLEConnection .SCAN_TIMEOUT
1012
1027
self .ble_device = None
1028
+ self .last_client = None
1013
1029
self .connected = False
1014
1030
self .running = False
1015
1031
self .should_run = False
1016
1032
self .must_disconnect = False
1017
1033
self .connect_job_running = False
1034
+ self .device_disappeared = False
1018
1035
1019
1036
import importlib
1020
1037
if BLEConnection .bleak == None :
@@ -1032,8 +1049,17 @@ def __init__(self, owner=None, target_name=None, target_bt_addr=None):
1032
1049
self .should_run = True
1033
1050
self .connection_thread = threading .Thread (target = self .connection_job , daemon = True ).start ()
1034
1051
1052
+ def cleanup (self ):
1053
+ try :
1054
+ if self .last_client != None :
1055
+ self .asyncio .run (self .last_client .disconnect ())
1056
+ except Exception as e :
1057
+ RNS .log (f"Error while disconnecting BLE device on cleanup for { self .owner } " , RNS .LOG_ERROR )
1058
+
1059
+ self .should_run = False
1060
+
1035
1061
def connection_job (self ):
1036
- while ( self .should_run ) :
1062
+ while self .should_run :
1037
1063
if self .ble_device == None :
1038
1064
self .ble_device = self .find_target_device ()
1039
1065
@@ -1043,6 +1069,10 @@ def connection_job(self):
1043
1069
1044
1070
time .sleep (1 )
1045
1071
1072
+ self .cleanup ()
1073
+ self .running = False
1074
+ RNS .log (f"BLE connection job for { self .owner } ended" , RNS .LOG_DEBUG )
1075
+
1046
1076
def connect_device (self ):
1047
1077
if self .ble_device != None and type (self .ble_device ) == self .bleak .backends .device .BLEDevice :
1048
1078
RNS .log (f"Connecting BLE device { self .ble_device } for { self .owner } ..." , RNS .LOG_DEBUG )
@@ -1056,6 +1086,7 @@ def handle_rx(device, data):
1056
1086
1057
1087
self .connected = True
1058
1088
self .ble_device = ble_client
1089
+ self .last_client = ble_client
1059
1090
self .owner .port = str (f"ble://{ ble_client .address } " )
1060
1091
1061
1092
loop = self .asyncio .get_running_loop ()
@@ -1077,12 +1108,14 @@ def handle_rx(device, data):
1077
1108
self .asyncio .run (connect_job ())
1078
1109
except Exception as e :
1079
1110
RNS .log (f"Could not connect BLE device { self .ble_device } for { self .owner } . Possibly missing authentication." , RNS .LOG_ERROR )
1111
+
1080
1112
self .connect_job_running = False
1081
1113
1082
1114
def device_disconnected (self , device ):
1083
1115
RNS .log (f"BLE device for { self .owner } disconnected" , RNS .LOG_NOTICE )
1084
1116
self .connected = False
1085
1117
self .ble_device = None
1118
+ self .device_disappeared = True
1086
1119
1087
1120
def find_target_device (self ):
1088
1121
RNS .log (f"Searching for attachable BLE device for { self .owner } ..." , RNS .LOG_EXTREME )
@@ -1106,7 +1139,13 @@ def device_filter(device: self.bleak.backends.device.BLEDevice, adv: self.bleak.
1106
1139
1107
1140
return False
1108
1141
1109
- device = self .asyncio .run (self .bleak .BleakScanner .find_device_by_filter (device_filter , timeout = self .scan_timeout ))
1142
+ device = None
1143
+ try :
1144
+ device = self .asyncio .run (self .bleak .BleakScanner .find_device_by_filter (device_filter , timeout = self .scan_timeout ))
1145
+ except Exception as e :
1146
+ RNS .log (f"Error while finding BLE device for { self .owner } : { e } " , RNS .LOG_ERROR )
1147
+ self .should_run = False
1148
+
1110
1149
return device
1111
1150
1112
1151
def device_bonded (self , device ):
0 commit comments