Merge pull request #279 from dadav/feature/auto-pairing-bt
Add auto-pair and internet-sharing
This commit is contained in:
commit
cfa8a02abc
@ -165,21 +165,35 @@ class BTNap:
|
|||||||
if not bt_dev:
|
if not bt_dev:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
bt_dev.StartDiscovery()
|
||||||
|
except Exception:
|
||||||
|
# can fail with org.bluez.Error.NotReady / org.bluez.Error.Failed
|
||||||
|
# TODO: add loop?
|
||||||
|
pass
|
||||||
|
|
||||||
|
dev_remote = None
|
||||||
|
|
||||||
# could be set to 0, so check if > -1
|
# could be set to 0, so check if > -1
|
||||||
while timeout > -1:
|
while timeout > -1:
|
||||||
try:
|
try:
|
||||||
dev_remote = BTNap.find_device(self._mac, bt_dev)
|
dev_remote = BTNap.find_device(self._mac, bt_dev)
|
||||||
logging.debug('Using remote device (addr: %s): %s',
|
logging.debug('Using remote device (addr: %s): %s',
|
||||||
BTNap.prop_get(dev_remote, 'Address'), dev_remote.object_path )
|
BTNap.prop_get(dev_remote, 'Address'), dev_remote.object_path )
|
||||||
return dev_remote
|
break
|
||||||
except BTError:
|
except BTError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
|
|
||||||
# Device not found :(
|
try:
|
||||||
return None
|
bt_dev.StopDiscovery()
|
||||||
|
except Exception:
|
||||||
|
# can fail with org.bluez.Error.NotReady / org.bluez.Error.Failed / org.bluez.Error.NotAuthorized
|
||||||
|
pass
|
||||||
|
|
||||||
|
return dev_remote
|
||||||
|
|
||||||
|
|
||||||
def connect(self, reconnect=False):
|
def connect(self, reconnect=False):
|
||||||
@ -189,19 +203,18 @@ class BTNap:
|
|||||||
return True if connected; False if failed
|
return True if connected; False if failed
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# power up devices
|
|
||||||
bt_dev = self.power(True)
|
|
||||||
if not bt_dev:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# check if device is close
|
# check if device is close
|
||||||
dev_remote = self.wait_for_device()
|
dev_remote = self.wait_for_device()
|
||||||
|
|
||||||
if not dev_remote:
|
if not dev_remote:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#wait_iter = lambda: time.sleep(3600)
|
try:
|
||||||
# signal.signal(signal.SIGTERM, lambda sig,frm: sys.exit(0))
|
dev_remote.Pair()
|
||||||
|
logging.info('BT-TETHER: Successful paired with device ;)')
|
||||||
|
except Exception:
|
||||||
|
# can fail because of AlreadyExists etc.
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dev_remote.ConnectProfile('nap')
|
dev_remote.ConnectProfile('nap')
|
||||||
@ -347,6 +360,18 @@ class IfaceWrapper:
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_route(addr):
|
||||||
|
process = subprocess.Popen(f"ip route replace default via {addr}", shell=True, stdin=None,
|
||||||
|
stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
|
||||||
|
process.wait()
|
||||||
|
|
||||||
|
if process.returncode > 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def on_loaded():
|
def on_loaded():
|
||||||
"""
|
"""
|
||||||
@ -355,7 +380,7 @@ def on_loaded():
|
|||||||
global READY
|
global READY
|
||||||
global INTERVAL
|
global INTERVAL
|
||||||
|
|
||||||
for opt in ['mac', 'ip', 'netmask', 'interval']:
|
for opt in ['share_internet', 'mac', 'ip', 'netmask', 'interval']:
|
||||||
if opt not in OPTIONS or (opt in OPTIONS and OPTIONS[opt] is None):
|
if opt not in OPTIONS or (opt in OPTIONS and OPTIONS[opt] is None):
|
||||||
logging.error("BT-TET: Pleace specify the %s in your config.yml.", opt)
|
logging.error("BT-TET: Pleace specify the %s in your config.yml.", opt)
|
||||||
return
|
return
|
||||||
@ -396,6 +421,16 @@ def on_ui_update(ui):
|
|||||||
logging.error("Could not set ip of bnep0 to %s", addr)
|
logging.error("Could not set ip of bnep0 to %s", addr)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# change route if sharking
|
||||||
|
if OPTIONS['share_internet']:
|
||||||
|
IfaceWrapper.set_route(".".join(OPTIONS['ip'].split('.')[:-1] + ['1'])) # im not proud about that
|
||||||
|
# fix resolv.conf; dns over https ftw!
|
||||||
|
with open('/etc/resolv.conf', 'r+') as resolv:
|
||||||
|
nameserver = resolv.read()
|
||||||
|
if 'nameserver 9.9.9.9' not in nameserver:
|
||||||
|
resolv.seek(0)
|
||||||
|
resolv.write(nameserver + 'nameserver 9.9.9.9\n')
|
||||||
|
|
||||||
ui.set('bluetooth', 'CON')
|
ui.set('bluetooth', 'CON')
|
||||||
else:
|
else:
|
||||||
ui.set('bluetooth', 'ERR2')
|
ui.set('bluetooth', 'ERR2')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user