From 061799e5d58feebceafa75887b422e38ec32cb1d Mon Sep 17 00:00:00 2001 From: James Ross Date: Thu, 24 Oct 2019 23:45:00 -0700 Subject: [PATCH] rfkillMagic: support hotpluggable bt devices by unconditionally starting the event monitor and handling finding and losing devices. --- usr/lib/blueberry/blueberry.py | 5 +++ usr/lib/blueberry/rfkillMagic.py | 53 +++++++++++++++----------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/usr/lib/blueberry/blueberry.py b/usr/lib/blueberry/blueberry.py index 2819a3b..68de8c4 100755 --- a/usr/lib/blueberry/blueberry.py +++ b/usr/lib/blueberry/blueberry.py @@ -232,7 +232,11 @@ class Blueberry(Gtk.Application): return label def get_default_adapter_name(self): + if not self.rfkill.have_adapter: + return None + name = None + try: output = subprocess.check_output(["timeout", "2s", "bt-adapter", "-i"]).decode("utf-8").strip() for line in output.split("\n"): @@ -242,6 +246,7 @@ class Blueberry(Gtk.Application): break except Exception as cause: print ("Could not retrieve the BT adapter name with 'bt-adapter -i': %s" % cause) + return name def update_status(self, path=None, iter=None, data=None): diff --git a/usr/lib/blueberry/rfkillMagic.py b/usr/lib/blueberry/rfkillMagic.py index 6d54308..b1863cd 100644 --- a/usr/lib/blueberry/rfkillMagic.py +++ b/usr/lib/blueberry/rfkillMagic.py @@ -26,13 +26,12 @@ def __init__(self, output_callback, debug): self.monitor_killer = False - self.have_adapter = self.adapter_check() - - if self.have_adapter: - self.start_event_monitor() + self.adapter_check() + self.start_event_monitor() def adapter_check(self): res = subprocess.check_output(RFKILL_CHK).decode('utf-8') + have_adapter = False ''' Assume the output of: @@ -46,21 +45,20 @@ def adapter_check(self): Hard blocked: no ''' - self.debug("adapter_check full output:\n%s" % res) - - if not res: + if res: + self.debug("adapter_check full output:\n%s" % res) + reslines = res.split('\n') + for line in reslines: + if "Bluetooth" in line: + self.adapter_index = int(line[0]) + self.debug("adapter_check found adapter at %d" % self.adapter_index) + have_adapter = True + break + else: self.debug("adapter_check no output (no adapter)") - self.have_adapter = False - return False - - reslines = res.split('\n') - for line in reslines: - if "Bluetooth" in line: - self.adapter_index = int(line[0]) - self.debug("adapter_check found adapter at %d" % self.adapter_index) - return True - return False + self.have_adapter = have_adapter + return have_adapter def start_event_monitor(self): if not self.tproc and not self.monitor_killer: @@ -72,9 +70,6 @@ def event_monitor_thread(self, data): l = self.tproc.stdout.readline().decode('utf-8') # This blocks until it receives a newline. self.update_state(l) - self.tproc = None - thread.exit() - def update_state(self, line): self.debug("update_state line: %s" % line) @@ -95,15 +90,17 @@ def update_state(self, line): 2017-12-08 11:54:16,972431-0800: idx 1 type 1 op 0 soft 0 hard 0 2017-12-08 11:54:16,972474-0800: idx 4 type 2 op 0 soft 0 hard 0 ''' + if not self.have_adapter: + self.adapter_check() - match = re.search(r'idx (?P\d+) type (?P\d+) op (?P\d+) soft (?P\d+) hard (?P\d+)', line) - - if not match: - return - - if int(match.group('idx')) == self.adapter_index: - self.soft_block = int(match.group('soft')) == 1 - self.hard_block = int(match.group('hard')) == 1 + if self.have_adapter: + match = re.search(r'idx (?P\d+) type (?P\d+) op (?P\d+) soft (?P\d+) hard (?P\d+)', line) + if match: + if int(match.group('idx')) == self.adapter_index: + if int(match.group('op')) == 1: + self.adapter_check() + self.soft_block = int(match.group('soft')) == 1 + self.hard_block = int(match.group('hard')) == 1 self.update_ui()