From b7a0c79de57179c1f22e4fdaa98c188dacb6aea9 Mon Sep 17 00:00:00 2001 From: ukBaz Date: Mon, 14 Feb 2022 11:33:59 +0000 Subject: [PATCH 1/4] Experiment to see issue #1 --- btsocket/btmgmt_callback.py | 2 +- btsocket/btmgmt_socket.py | 31 +++++++++++++++++++++---------- btsocket/btmgmt_sync.py | 10 +++++----- examples/sync_list_commands.py | 3 +++ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/btsocket/btmgmt_callback.py b/btsocket/btmgmt_callback.py index 5401054..b87d3b3 100644 --- a/btsocket/btmgmt_callback.py +++ b/btsocket/btmgmt_callback.py @@ -15,7 +15,7 @@ class Mgmt: def __init__(self): # Setup read and write sockets - self.rsock, self.wsock = btmgmt_socket.btmgmt_socket() + self.rsock, self.wsock = btmgmt_socket.open() self.loop = asyncio.get_event_loop() # Store for event callbacks self._event_callbacks = dict() diff --git a/btsocket/btmgmt_socket.py b/btsocket/btmgmt_socket.py index 5e1cfe8..747336a 100644 --- a/btsocket/btmgmt_socket.py +++ b/btsocket/btmgmt_socket.py @@ -1,7 +1,9 @@ import asyncio import ctypes +import os import socket +import btsocket.btmgmt_socket AF_BLUETOOTH = 31 PF_BLUETOOTH = AF_BLUETOOTH @@ -29,7 +31,7 @@ class SocketAddr(ctypes.Structure): ] -def btmgmt_socket(): +def open(): """ Because of the following issue with Python the Bluetooth User socket on linux needs to be done with lower level calls. @@ -53,6 +55,7 @@ def btmgmt_socket(): # fd = libc_socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, # BTPROTO_HCI) fd = libc_socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) + print('libc fd', fd) if fd < 0: raise BluetoothSocketError("Unable to open PF_BLUETOOTH socket") @@ -64,40 +67,48 @@ def btmgmt_socket(): if r < 0: raise BluetoothSocketError("Unable to bind %s", r) - ins = outs = socket.fromfd(fd, AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) - return ins, outs + sock_fd = socket.fromfd(fd, AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) + return sock_fd + + +def close(socket_fd): + fd = socket_fd.detach() + print('Python fd', fd) + socket_fd.close() + os.close(fd - 1) + os.close(fd) + print(socket_fd._closed) def test_asyncio_usage(): - rsock, wsock = btmgmt_socket() + sock = open() loop = asyncio.get_event_loop() def reader(): - data = rsock.recv(100) + data = sock.recv(100) print("Received:", data) # We are done: unregister the file descriptor - loop.remove_reader(rsock) + loop.remove_reader(sock) # Stop the event loop loop.stop() # Register the file descriptor for read event - loop.add_reader(rsock, reader) + loop.add_reader(sock, reader) # Write a command to the socket # Read Management Version Information Command # b'\x01\x00\xff\xff\x00\x00' - loop.call_soon(wsock.send, b'\x01\x00\xff\xff\x00\x00') + loop.call_soon(sock.send, b'\x01\x00\xff\xff\x00\x00') try: # Run the event loop loop.run_forever() finally: # We are done. Close sockets and the event loop. - rsock.close() - wsock.close() + btsocket.btmgmt_socket.close(sock) loop.close() diff --git a/btsocket/btmgmt_sync.py b/btsocket/btmgmt_sync.py index 09c845e..2fe0218 100644 --- a/btsocket/btmgmt_sync.py +++ b/btsocket/btmgmt_sync.py @@ -19,18 +19,18 @@ def send(*args): logger.debug('Sending btmgmt frames %s', pkt_objs) cmd_pkt = _as_packet(pkt_objs) # print('cmd pkt', [f'{octets:x}' for octets in cmd_pkt]) - rsock, wsock = btmgmt_socket.btmgmt_socket() + sock = btmgmt_socket.open() logger.debug('Sending bytes: %s', cmd_pkt) - wsock.send(cmd_pkt) + sock.send(cmd_pkt) while not response_recvd: - raw_data = rsock.recv(100) + raw_data = sock.recv(100) logger.debug('Received: %s', raw_data) data = btmgmt_protocol.reader(raw_data) logger.debug('Received btmgmt frames: %s', data) if data.cmd_response_frame: response_recvd = True - rsock.close() - wsock.close() + + btmgmt_socket.close(sock) if data.event_frame.status != btmgmt_protocol.ErrorCodes.Success: raise NameError(f'btmgmt Error: ' f'{data.event_frame.command_opcode} ' diff --git a/examples/sync_list_commands.py b/examples/sync_list_commands.py index b3501c3..7d92a74 100644 --- a/examples/sync_list_commands.py +++ b/examples/sync_list_commands.py @@ -17,3 +17,6 @@ f'{required_params}') except ValueError: pass + +from time import sleep +sleep(10) From 9c7ec3330464fd8de0d3efa38d912b83a156d608 Mon Sep 17 00:00:00 2001 From: ukBaz Date: Mon, 14 Feb 2022 19:32:47 +0000 Subject: [PATCH 2/4] Change how socket is created from: socket.fromfd to: socket.socket --- btsocket/btmgmt_socket.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/btsocket/btmgmt_socket.py b/btsocket/btmgmt_socket.py index 747336a..dd3296c 100644 --- a/btsocket/btmgmt_socket.py +++ b/btsocket/btmgmt_socket.py @@ -1,9 +1,7 @@ import asyncio import ctypes -import os import socket -import btsocket.btmgmt_socket AF_BLUETOOTH = 31 PF_BLUETOOTH = AF_BLUETOOTH @@ -55,7 +53,7 @@ def open(): # fd = libc_socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, # BTPROTO_HCI) fd = libc_socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) - print('libc fd', fd) + if fd < 0: raise BluetoothSocketError("Unable to open PF_BLUETOOTH socket") @@ -67,17 +65,14 @@ def open(): if r < 0: raise BluetoothSocketError("Unable to bind %s", r) - sock_fd = socket.fromfd(fd, AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) + sock_fd = socket.socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI, fileno=fd) return sock_fd def close(socket_fd): + """Close the open socket""" fd = socket_fd.detach() - print('Python fd', fd) - socket_fd.close() - os.close(fd - 1) - os.close(fd) - print(socket_fd._closed) + socket.close(fd) def test_asyncio_usage(): @@ -108,7 +103,7 @@ def reader(): loop.run_forever() finally: # We are done. Close sockets and the event loop. - btsocket.btmgmt_socket.close(sock) + close(sock) loop.close() From b5d3bdd9e66bd64e00ca1b2235a3115716b95f34 Mon Sep 17 00:00:00 2001 From: ukBaz Date: Mon, 14 Feb 2022 19:40:06 +0000 Subject: [PATCH 3/4] Remove "test sleep" used for socket testing from example --- examples/sync_list_commands.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/sync_list_commands.py b/examples/sync_list_commands.py index 7d92a74..b3501c3 100644 --- a/examples/sync_list_commands.py +++ b/examples/sync_list_commands.py @@ -17,6 +17,3 @@ f'{required_params}') except ValueError: pass - -from time import sleep -sleep(10) From fa58e74d752fade5dc4add771393731095fe8ed2 Mon Sep 17 00:00:00 2001 From: ukBaz Date: Tue, 15 Feb 2022 06:22:59 +0000 Subject: [PATCH 4/4] Update btmgmt_callback.py to use new single socket API --- btsocket/btmgmt_callback.py | 17 ++++++++--------- btsocket/btmgmt_socket.py | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/btsocket/btmgmt_callback.py b/btsocket/btmgmt_callback.py index b87d3b3..5efb552 100644 --- a/btsocket/btmgmt_callback.py +++ b/btsocket/btmgmt_callback.py @@ -15,7 +15,7 @@ class Mgmt: def __init__(self): # Setup read and write sockets - self.rsock, self.wsock = btmgmt_socket.open() + self.sock = btmgmt_socket.open() self.loop = asyncio.get_event_loop() # Store for event callbacks self._event_callbacks = dict() @@ -42,7 +42,7 @@ def reader(self): to events. """ logger.debug('Reader callback') - data = self.rsock.recv(100) + data = self.sock.recv(100) pkt = btmgmt_protocol.reader(data) logger.info('pkt: [%s]', pkt) if pkt.header.event_code in self._event_callbacks: @@ -59,7 +59,7 @@ def writer(self): if len(self.cmd_queue) > 0: this_cmd = self.cmd_queue.popleft() logger.info('sending pkt [%s]', tools.format_pkt(this_cmd)) - self.wsock.send(this_cmd) + self.sock.send(this_cmd) if not self.running and len(self.cmd_queue) == 0: self.loop.stop() # Do one more read to get the response from the last command @@ -95,24 +95,23 @@ def stop(self): """ self.running = False - self.loop.remove_writer(self.wsock) - self.loop.remove_reader(self.rsock) + self.loop.remove_writer(self.sock) + self.loop.remove_reader(self.sock) self.loop.stop() def close(self): """ Stop the event loop and close sockets etc. """ - self.rsock.close() - # self.wsock.close() + btmgmt_socket.close(self.sock) # Stop the event loop self.loop.close() def start(self): self.running = True # Setup reader and writer for socket streams - self.loop.add_reader(self.rsock, self.reader) - self.loop.add_writer(self.wsock, self.writer) + self.loop.add_reader(self.sock, self.reader) + self.loop.add_writer(self.sock, self.writer) logger.debug('Starting event loop...') try: # Run the event loop diff --git a/btsocket/btmgmt_socket.py b/btsocket/btmgmt_socket.py index dd3296c..66584fd 100644 --- a/btsocket/btmgmt_socket.py +++ b/btsocket/btmgmt_socket.py @@ -69,9 +69,9 @@ def open(): return sock_fd -def close(socket_fd): +def close(bt_socket): """Close the open socket""" - fd = socket_fd.detach() + fd = bt_socket.detach() socket.close(fd)