From 947a9f7d10bc3c54939cfa72dab27b2e64034318 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 11:33:33 +0200 Subject: Create packet. --- parrot_zik/bluetooth_paired_devices.py | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 parrot_zik/bluetooth_paired_devices.py (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py new file mode 100644 index 0000000..eb1ec09 --- /dev/null +++ b/parrot_zik/bluetooth_paired_devices.py @@ -0,0 +1,107 @@ +import sys +import re +import os + +from parrot_zik.resource_manager import GenericResourceManager + +if sys.platform == "darwin": + from binplist import binplist + import lightblue +else: + import bluetooth + if sys.platform == "win32": + import _winreg + + +def get_parrot_zik_mac(): + p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|' + 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') + if sys.platform == "linux2": + bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) + if bluetooth_on == 1: + out = os.popen("bluez-test-device list").read() + res = p.findall(out) + if len(res) > 0: + return res[0] + else: + raise DeviceNotConnected + else: + raise BluetoothIsNotOn + elif sys.platform == "darwin": + fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb") + plist = binplist.BinaryPlist(file_obj=fd) + parsed_plist = plist.Parse() + try: + for mac in parsed_plist['PairedDevices']: + if p.match(mac.replace("-", ":")): + return mac.replace("-", ":") + else: + raise DeviceNotConnected + except Exception: + pass + + elif sys.platform == "win32": + aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE) + aKey = _winreg.OpenKey( + aReg, 'SYSTEM\CurrentControlSet\Services\ + BTHPORT\Parameters\Devices') + for i in range(10): + try: + asubkey_name = _winreg.EnumKey(aKey, i) + mac = ':'.join(asubkey_name[i:i+2] for i in range(0, 12, 2)) + res = p.findall(mac) + if len(res) > 0: + return res[0] + else: + raise DeviceNotConnected + except EnvironmentError: + pass + + +def connect(): + mac = get_parrot_zik_mac() + if sys.platform == "darwin": + service_matches = lightblue.findservices( + name="Parrot RFcomm service", addr=mac) + else: + uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb", + "8B6814D3-6CE7-4498-9700-9312C1711F63"] + service_matches = [] + for uuid in uuids: + try: + service_matches = bluetooth.find_service(uuid=uuid, address=mac) + except bluetooth.btcommon.BluetoothError: + pass + if service_matches: + break + + if len(service_matches) == 0: + raise ConnectionFailure + first_match = service_matches[0] + + if sys.platform == "darwin": + host = first_match[0] + port = first_match[1] + sock = lightblue.socket() + else: + port = first_match["port"] + host = first_match["host"] + sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) + + sock.connect((host, port)) + + sock.send('\x00\x03\x00') + sock.recv(1024) + return GenericResourceManager(sock) + + +class DeviceNotConnected(Exception): + pass + + +class ConnectionFailure(Exception): + pass + + +class BluetoothIsNotOn(Exception): + pass -- cgit v1.2.1 From b4ff541fd77d4076051e57b748fc5817e0cc1e25 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 12:41:30 +0200 Subject: Make into proper package. --- parrot_zik/bluetooth_paired_devices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index eb1ec09..726731c 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -2,7 +2,7 @@ import sys import re import os -from parrot_zik.resource_manager import GenericResourceManager +from .resource_manager import GenericResourceManager if sys.platform == "darwin": from binplist import binplist -- cgit v1.2.1 From 814921eabbfb0cdc41baeb62e4b25da10c9e0212 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 15:11:43 +0200 Subject: Fix connection refused error. --- parrot_zik/bluetooth_paired_devices.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index 726731c..f06286a 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -88,7 +88,10 @@ def connect(): host = first_match["host"] sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) - sock.connect((host, port)) + try: + sock.connect((host, port)) + except bluetooth.btcommon.BluetoothError: + raise ConnectionFailure sock.send('\x00\x03\x00') sock.recv(1024) -- cgit v1.2.1 From feac48c21e390cbf4e2ecf3b3c7a12d89c1e3436 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 20:39:17 +0200 Subject: Ignore dbus exception. --- parrot_zik/bluetooth_paired_devices.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index f06286a..cec6d8e 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -1,3 +1,4 @@ +import dbus import sys import re import os @@ -17,16 +18,24 @@ def get_parrot_zik_mac(): p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|' 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') if sys.platform == "linux2": - bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) - if bluetooth_on == 1: - out = os.popen("bluez-test-device list").read() - res = p.findall(out) - if len(res) > 0: - return res[0] - else: - raise DeviceNotConnected + try: + bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) + except dbus.exceptions.DBusException: + pass else: - raise BluetoothIsNotOn + if bluetooth_on == 1: + try: + out = os.popen("bluez-test-device list").read() + except dbus.exceptions.DBusException: + pass + else: + res = p.findall(out) + if len(res) > 0: + return res[0] + else: + raise DeviceNotConnected + else: + raise BluetoothIsNotOn elif sys.platform == "darwin": fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb") plist = binplist.BinaryPlist(file_obj=fd) -- cgit v1.2.1 From deedbdca9564168460b24ee9549d084e2749fa1d Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 20:49:11 +0200 Subject: Refactor. --- parrot_zik/bluetooth_paired_devices.py | 101 +++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 44 deletions(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index cec6d8e..e518161 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -14,57 +14,70 @@ else: import _winreg -def get_parrot_zik_mac(): - p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|' - 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') - if sys.platform == "linux2": +p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|' + 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') + +def get_parrot_zik_mac_linux(): + try: + bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) + except dbus.exceptions.DBusException: + pass + else: + if bluetooth_on == 1: try: - bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) + out = os.popen("bluez-test-device list").read() except dbus.exceptions.DBusException: pass else: - if bluetooth_on == 1: - try: - out = os.popen("bluez-test-device list").read() - except dbus.exceptions.DBusException: - pass - else: - res = p.findall(out) - if len(res) > 0: - return res[0] - else: - raise DeviceNotConnected - else: - raise BluetoothIsNotOn - elif sys.platform == "darwin": - fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb") - plist = binplist.BinaryPlist(file_obj=fd) - parsed_plist = plist.Parse() - try: - for mac in parsed_plist['PairedDevices']: - if p.match(mac.replace("-", ":")): - return mac.replace("-", ":") + res = p.findall(out) + if len(res) > 0: + return res[0] else: raise DeviceNotConnected - except Exception: - pass + else: + raise BluetoothIsNotOn - elif sys.platform == "win32": - aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE) - aKey = _winreg.OpenKey( - aReg, 'SYSTEM\CurrentControlSet\Services\ - BTHPORT\Parameters\Devices') - for i in range(10): - try: - asubkey_name = _winreg.EnumKey(aKey, i) - mac = ':'.join(asubkey_name[i:i+2] for i in range(0, 12, 2)) - res = p.findall(mac) - if len(res) > 0: - return res[0] - else: - raise DeviceNotConnected - except EnvironmentError: - pass + +def get_parrot_zik_mac_darwin(): + fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb") + plist = binplist.BinaryPlist(file_obj=fd) + parsed_plist = plist.Parse() + try: + for mac in parsed_plist['PairedDevices']: + if p.match(mac.replace("-", ":")): + return mac.replace("-", ":") + else: + raise DeviceNotConnected + except Exception: + pass + + +def get_parrot_zik_mac_windows(): + aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE) + aKey = _winreg.OpenKey( + aReg, 'SYSTEM\CurrentControlSet\Services\ + BTHPORT\Parameters\Devices') + for i in range(10): + try: + asubkey_name = _winreg.EnumKey(aKey, i) + mac = ':'.join(asubkey_name[i:i+2] for i in range(0, 12, 2)) + res = p.findall(mac) + if len(res) > 0: + return res[0] + else: + raise DeviceNotConnected + except EnvironmentError: + pass + + +if sys.platform in ['linux', 'linux2']: + get_parrot_zik_mac = get_parrot_zik_mac_linux +elif sys.platform == 'darwin': + get_parrot_zik_mac = get_parrot_zik_mac_darwin +elif sys.platform == 'win32': + get_parrot_zik_mac = get_parrot_zik_mac_windows +else: + raise AssertionError('Platform not supported') def connect(): -- cgit v1.2.1 From 0973bb1a2f8e46d7dffe67063f0d1415b3397619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20S=C3=A1nchez?= Date: Fri, 3 Jul 2015 12:07:31 +0200 Subject: Use other method to get bluetooth devices list in linux --- parrot_zik/bluetooth_paired_devices.py | 42 ++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index e518161..8eba2ce 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -1,7 +1,7 @@ import dbus import sys import re -import os +from subprocess import Popen, PIPE, STDOUT from .resource_manager import GenericResourceManager @@ -17,15 +17,28 @@ else: p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|' 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') -def get_parrot_zik_mac_linux(): + +def get_parrot_zik_mac_linux_using_bluez_test(): try: - bluetooth_on = int(os.popen('bluez-test-adapter powered').read()) + pipe = Popen( + ['bluez-test-adapter', 'powered'], + stdout=PIPE, + stdin=PIPE, + stderr=STDOUT + ) + bluetooth_on = int(pipe.communicate()) except dbus.exceptions.DBusException: pass else: if bluetooth_on == 1: try: - out = os.popen("bluez-test-device list").read() + pipe = Popen( + ['bluez-test-device', 'list'], + stdout=PIPE, + stdin=PIPE, + stderr=STDOUT + ) + out = pipe.communicate() except dbus.exceptions.DBusException: pass else: @@ -38,6 +51,27 @@ def get_parrot_zik_mac_linux(): raise BluetoothIsNotOn +def get_parrot_zik_mac_linux_using_bluetoothcmd(): + pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) + res = pipe.communicate("exit") + if len(res) > 0 and res[0]: + match = p.search(res[0]) + if match: + return match.group(0) + + raise DeviceNotConnected + + +def get_parrot_zik_mac_linux(): + try: + get_parrot_zik_mac_linux_using_bluez_test() + except OSError as e: + if e.errno == 2: + # File not found, probably it means that bluez utils are not + # installed + return get_parrot_zik_mac_linux_using_bluetoothcmd() + + def get_parrot_zik_mac_darwin(): fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb") plist = binplist.BinaryPlist(file_obj=fd) -- cgit v1.2.1 From 6eba736eb54ebecef14c4bd2b03df29b82f16f4b Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Fri, 3 Jul 2015 13:20:40 +0200 Subject: Fix mistakes and refactor. --- parrot_zik/bluetooth_paired_devices.py | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index 8eba2ce..0a40481 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -18,31 +18,24 @@ p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') -def get_parrot_zik_mac_linux_using_bluez_test(): +def get_parrot_zik_mac_bluez(): + pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE, + stderr=STDOUT) try: - pipe = Popen( - ['bluez-test-adapter', 'powered'], - stdout=PIPE, - stdin=PIPE, - stderr=STDOUT - ) - bluetooth_on = int(pipe.communicate()) + stdout, stderr = pipe.communicate() except dbus.exceptions.DBusException: pass else: + bluetooth_on = int(stdout) if bluetooth_on == 1: + pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE, + stderr=STDOUT) try: - pipe = Popen( - ['bluez-test-device', 'list'], - stdout=PIPE, - stdin=PIPE, - stderr=STDOUT - ) - out = pipe.communicate() + stdout, stderr = pipe.communicate() except dbus.exceptions.DBusException: pass else: - res = p.findall(out) + res = p.findall(stdout) if len(res) > 0: return res[0] else: @@ -51,7 +44,7 @@ def get_parrot_zik_mac_linux_using_bluez_test(): raise BluetoothIsNotOn -def get_parrot_zik_mac_linux_using_bluetoothcmd(): +def get_parrot_zik_mac_bluetoothcmd(): pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) res = pipe.communicate("exit") if len(res) > 0 and res[0]: @@ -64,12 +57,10 @@ def get_parrot_zik_mac_linux_using_bluetoothcmd(): def get_parrot_zik_mac_linux(): try: - get_parrot_zik_mac_linux_using_bluez_test() + return get_parrot_zik_mac_bluez() except OSError as e: if e.errno == 2: - # File not found, probably it means that bluez utils are not - # installed - return get_parrot_zik_mac_linux_using_bluetoothcmd() + return get_parrot_zik_mac_bluetoothcmd() def get_parrot_zik_mac_darwin(): -- cgit v1.2.1 From 3d455057798db862b5e1fb510d2f4aceaf71515a Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Fri, 3 Jul 2015 13:40:07 +0200 Subject: Refactor. --- parrot_zik/bluetooth_paired_devices.py | 80 ++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'parrot_zik/bluetooth_paired_devices.py') diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py index 0a40481..905819f 100644 --- a/parrot_zik/bluetooth_paired_devices.py +++ b/parrot_zik/bluetooth_paired_devices.py @@ -18,49 +18,63 @@ p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2 'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}') -def get_parrot_zik_mac_bluez(): - pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE, - stderr=STDOUT) - try: - stdout, stderr = pipe.communicate() - except dbus.exceptions.DBusException: - pass - else: - bluetooth_on = int(stdout) - if bluetooth_on == 1: - pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE, - stderr=STDOUT) - try: - stdout, stderr = pipe.communicate() - except dbus.exceptions.DBusException: - pass - else: - res = p.findall(stdout) - if len(res) > 0: - return res[0] - else: - raise DeviceNotConnected +class BluetoothDeviceManager(object): + def is_bluetooth_on(self): + raise NotImplementedError + + def get_mac(self): + raise NotImplementedError + + +class BluezBluetoothDeviceManager(BluetoothDeviceManager): + def is_bluetooth_on(self): + pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE, + stderr=STDOUT) + try: + stdout, stderr = pipe.communicate() + except dbus.exceptions.DBusException: + pass + else: + return bool(stdout.strip()) + + def get_mac(self): + pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE, + stderr=STDOUT) + try: + stdout, stderr = pipe.communicate() + except dbus.exceptions.DBusException: + pass else: - raise BluetoothIsNotOn + res = p.findall(stdout) + if len(res) > 0: + return res[0] + else: + raise DeviceNotConnected -def get_parrot_zik_mac_bluetoothcmd(): - pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) - res = pipe.communicate("exit") - if len(res) > 0 and res[0]: - match = p.search(res[0]) - if match: - return match.group(0) +class BluetoothCmdDeviceManager(BluetoothDeviceManager): + def is_bluetooth_on(self): + return True - raise DeviceNotConnected + def get_mac(self): + pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) + res = pipe.communicate("exit") + if len(res) > 0 and res[0]: + match = p.search(res[0]) + if match: + return match.group(0) + raise DeviceNotConnected def get_parrot_zik_mac_linux(): + bluez_manager = BluezBluetoothDeviceManager() try: - return get_parrot_zik_mac_bluez() + bluez_manager.is_bluetooth_on() + return bluez_manager.get_mac() except OSError as e: if e.errno == 2: - return get_parrot_zik_mac_bluetoothcmd() + bluetoothcmd_manager = BluetoothCmdDeviceManager() + return bluetoothcmd_manager.get_mac() def get_parrot_zik_mac_darwin(): -- cgit v1.2.1