Merge pull request #689 from dadav/fix/bt-tether-lock
Add lock to bt-tether
This commit is contained in:
commit
4f153e3899
@ -2,6 +2,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
import dbus
|
import dbus
|
||||||
|
|
||||||
@ -426,6 +427,7 @@ class BTTether(plugins.Plugin):
|
|||||||
self.ready = False
|
self.ready = False
|
||||||
self.options = dict()
|
self.options = dict()
|
||||||
self.devices = dict()
|
self.devices = dict()
|
||||||
|
self.lock = Lock()
|
||||||
|
|
||||||
def on_loaded(self):
|
def on_loaded(self):
|
||||||
# new config
|
# new config
|
||||||
@ -479,102 +481,103 @@ class BTTether(plugins.Plugin):
|
|||||||
if not self.ready:
|
if not self.ready:
|
||||||
return
|
return
|
||||||
|
|
||||||
devices_to_try = list()
|
with self.lock:
|
||||||
connected_priorities = list()
|
devices_to_try = list()
|
||||||
any_device_connected = False # if this is true, last status on screen should be C
|
connected_priorities = list()
|
||||||
|
any_device_connected = False # if this is true, last status on screen should be C
|
||||||
|
|
||||||
for _, device in self.devices.items():
|
for _, device in self.devices.items():
|
||||||
if device.connected():
|
if device.connected():
|
||||||
connected_priorities.append(device.priority)
|
connected_priorities.append(device.priority)
|
||||||
any_device_connected = True
|
any_device_connected = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not device.max_tries or (device.max_tries > device.tries):
|
if not device.max_tries or (device.max_tries > device.tries):
|
||||||
if not device.status.newer_then_minutes(device.interval):
|
if not device.status.newer_then_minutes(device.interval):
|
||||||
devices_to_try.append(device)
|
devices_to_try.append(device)
|
||||||
device.status.update()
|
device.status.update()
|
||||||
device.tries += 1
|
device.tries += 1
|
||||||
|
|
||||||
sorted_devices = sorted(devices_to_try, key=lambda x: x.search_order)
|
sorted_devices = sorted(devices_to_try, key=lambda x: x.search_order)
|
||||||
|
|
||||||
for device in sorted_devices:
|
for device in sorted_devices:
|
||||||
bt = BTNap(device.mac)
|
bt = BTNap(device.mac)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logging.debug('BT-TETHER: Search %d secs for %s ...', device.scantime, device.name)
|
logging.debug('BT-TETHER: Search %d secs for %s ...', device.scantime, device.name)
|
||||||
dev_remote = bt.wait_for_device(timeout=device.scantime)
|
dev_remote = bt.wait_for_device(timeout=device.scantime)
|
||||||
if dev_remote is None:
|
if dev_remote is None:
|
||||||
logging.debug('BT-TETHER: Could not find %s, try again in %d minutes.', device.name, device.interval)
|
logging.debug('BT-TETHER: Could not find %s, try again in %d minutes.', device.name, device.interval)
|
||||||
|
ui.set('bluetooth', 'NF')
|
||||||
|
continue
|
||||||
|
except Exception as bt_ex:
|
||||||
|
logging.error(bt_ex)
|
||||||
ui.set('bluetooth', 'NF')
|
ui.set('bluetooth', 'NF')
|
||||||
continue
|
continue
|
||||||
except Exception as bt_ex:
|
|
||||||
logging.error(bt_ex)
|
|
||||||
ui.set('bluetooth', 'NF')
|
|
||||||
continue
|
|
||||||
|
|
||||||
paired = bt.is_paired()
|
paired = bt.is_paired()
|
||||||
if not paired:
|
if not paired:
|
||||||
if BTNap.pair(dev_remote):
|
if BTNap.pair(dev_remote):
|
||||||
logging.debug('BT-TETHER: Paired with %s.', device.name)
|
logging.debug('BT-TETHER: Paired with %s.', device.name)
|
||||||
|
else:
|
||||||
|
logging.debug('BT-TETHER: Pairing with %s failed ...', device.name)
|
||||||
|
ui.set('bluetooth', 'PE')
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
logging.debug('BT-TETHER: Pairing with %s failed ...', device.name)
|
logging.debug('BT-TETHER: Already paired.')
|
||||||
ui.set('bluetooth', 'PE')
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
logging.debug('BT-TETHER: Already paired.')
|
|
||||||
|
|
||||||
|
|
||||||
logging.debug('BT-TETHER: Try to create nap connection with %s ...', device.name)
|
logging.debug('BT-TETHER: Try to create nap connection with %s ...', device.name)
|
||||||
device.network, success = BTNap.nap(dev_remote)
|
device.network, success = BTNap.nap(dev_remote)
|
||||||
interface = None
|
interface = None
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
try:
|
try:
|
||||||
interface = device.interface()
|
interface = device.interface()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if interface is None:
|
||||||
|
ui.set('bluetooth', 'BE')
|
||||||
|
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
logging.debug('BT-TETHER: Created interface (%s)', interface)
|
||||||
|
ui.set('bluetooth', 'C')
|
||||||
|
any_device_connected = True
|
||||||
|
device.tries = 0 # reset tries
|
||||||
|
else:
|
||||||
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
||||||
|
ui.set('bluetooth', 'NF')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if interface is None:
|
addr = f"{device.ip}/{device.netmask}"
|
||||||
ui.set('bluetooth', 'BE')
|
if device.gateway:
|
||||||
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
gateway = device.gateway
|
||||||
|
else:
|
||||||
|
gateway = ".".join(device.ip.split('.')[:-1] + ['1'])
|
||||||
|
|
||||||
|
wrapped_interface = IfaceWrapper(interface)
|
||||||
|
logging.debug('BT-TETHER: Add ip to %s', interface)
|
||||||
|
if not wrapped_interface.set_addr(addr):
|
||||||
|
ui.set('bluetooth', 'AE')
|
||||||
|
logging.debug("BT-TETHER: Could not add ip to %s", interface)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
logging.debug('BT-TETHER: Created interface (%s)', interface)
|
if device.share_internet:
|
||||||
|
if not connected_priorities or device.priority > max(connected_priorities):
|
||||||
|
logging.debug('BT-TETHER: Set default route to %s via %s', gateway, interface)
|
||||||
|
IfaceWrapper.set_route(gateway, interface)
|
||||||
|
connected_priorities.append(device.priority)
|
||||||
|
|
||||||
|
logging.debug('BT-TETHER: Change resolv.conf if necessary ...')
|
||||||
|
with open('/etc/resolv.conf', 'r+') as resolv:
|
||||||
|
nameserver = resolv.read()
|
||||||
|
if 'nameserver 9.9.9.9' not in nameserver:
|
||||||
|
logging.debug('BT-TETHER: Added nameserver')
|
||||||
|
resolv.seek(0)
|
||||||
|
resolv.write(nameserver + 'nameserver 9.9.9.9\n')
|
||||||
|
|
||||||
|
if any_device_connected:
|
||||||
ui.set('bluetooth', 'C')
|
ui.set('bluetooth', 'C')
|
||||||
any_device_connected = True
|
|
||||||
device.tries = 0 # reset tries
|
|
||||||
else:
|
|
||||||
logging.debug('BT-TETHER: Could not establish nap connection with %s', device.name)
|
|
||||||
ui.set('bluetooth', 'NF')
|
|
||||||
continue
|
|
||||||
|
|
||||||
addr = f"{device.ip}/{device.netmask}"
|
|
||||||
if device.gateway:
|
|
||||||
gateway = device.gateway
|
|
||||||
else:
|
|
||||||
gateway = ".".join(device.ip.split('.')[:-1] + ['1'])
|
|
||||||
|
|
||||||
wrapped_interface = IfaceWrapper(interface)
|
|
||||||
logging.debug('BT-TETHER: Add ip to %s', interface)
|
|
||||||
if not wrapped_interface.set_addr(addr):
|
|
||||||
ui.set('bluetooth', 'AE')
|
|
||||||
logging.debug("BT-TETHER: Could not add ip to %s", interface)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if device.share_internet:
|
|
||||||
if not connected_priorities or device.priority > max(connected_priorities):
|
|
||||||
logging.debug('BT-TETHER: Set default route to %s via %s', gateway, interface)
|
|
||||||
IfaceWrapper.set_route(gateway, interface)
|
|
||||||
connected_priorities.append(device.priority)
|
|
||||||
|
|
||||||
logging.debug('BT-TETHER: Change resolv.conf if necessary ...')
|
|
||||||
with open('/etc/resolv.conf', 'r+') as resolv:
|
|
||||||
nameserver = resolv.read()
|
|
||||||
if 'nameserver 9.9.9.9' not in nameserver:
|
|
||||||
logging.debug('BT-TETHER: Added nameserver')
|
|
||||||
resolv.seek(0)
|
|
||||||
resolv.write(nameserver + 'nameserver 9.9.9.9\n')
|
|
||||||
|
|
||||||
if any_device_connected:
|
|
||||||
ui.set('bluetooth', 'C')
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user