[gnome-keysign: 37/65] bluetooth: let the system assign an available port



commit a835d62881766589971812dec42f32c6a620e5cf
Author: RyuzakiKK <aasonykk gmail com>
Date:   Sun Aug 20 21:21:00 2017 +0200

    bluetooth: let the system assign an available port

 keysign/bluetoothoffer.py | 19 ++++++++++---------
 keysign/discover.py       |  5 ++++-
 2 files changed, 14 insertions(+), 10 deletions(-)
---
diff --git a/keysign/bluetoothoffer.py b/keysign/bluetoothoffer.py
index 94cae44..2b5ec88 100644
--- a/keysign/bluetoothoffer.py
+++ b/keysign/bluetoothoffer.py
@@ -44,12 +44,6 @@ class BluetoothOffer:
         self.stopped = False
         message = "Back"
         success = False
-        if self.server_socket is None:
-            self.server_socket = BluetoothSocket(RFCOMM)
-            self.server_socket.bind(("", self.port))
-            # Number of unaccepted connections that the system will allow before refusing new connections
-            backlog = 1
-            self.server_socket.listen(backlog)
         try:
             while not self.stopped and not success:
                 # server_socket.accept() is not stoppable. So with select we can call accept()
@@ -84,9 +78,16 @@ class BluetoothOffer:
             else:
                 log.error("An unexpected error occurred %s", e.get_dbus_name())
             self.code = None
-            return "", ""
-        log.info("BT Code: %s", code)
-        bt_data = "BT={0}".format(code)
+            return None, None
+        if self.server_socket is None:
+            self.server_socket = BluetoothSocket(RFCOMM)
+            self.server_socket.bind(("", 0))
+            # Number of unaccepted connections that the system will allow before refusing new connections
+            backlog = 1
+            self.server_socket.listen(backlog)
+        port = self.server_socket.getsockname()[1]
+        log.info("BT Code: %s %s", code, port)
+        bt_data = "BT={0};PT={1}".format(code, port)
         return code, bt_data
 
     def stop(self):
diff --git a/keysign/discover.py b/keysign/discover.py
index fd86df4..78e5068 100644
--- a/keysign/discover.py
+++ b/keysign/discover.py
@@ -17,6 +17,9 @@ class Discover:
     def __init__(self, userdata, discovery):
         # if the userdata is a qr code we extract the bluetooth code
         self.bt_code = parse_barcode(userdata).get("BT", [None])[0]
+        self.bt_port = parse_barcode(userdata).get("PT", [None])[0]
+        if self.bt_port:
+            self.bt_port = int(self.bt_port)
         self.mac = parse_barcode(userdata).get("MAC", [None])[0]
         self.userdata = userdata
         if discovery:
@@ -39,7 +42,7 @@ class Discover:
         if self.bt_code and not self.stopped:
             # We try Bluetooth, if we have it
             log.info("Trying to connect to %s with Bluetooth", self.bt_code)
-            self.bt = BluetoothReceive()
+            self.bt = BluetoothReceive(self.bt_port)
             msg_tuple = yield self.bt.find_key(self.bt_code, self.mac)
             key_data, success, message = msg_tuple
             if key_data:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]