11# SPDX-License-Identifier: MIT
2- # Copyright (C) 2020 The Pybricks Authors
2+ # Copyright (C) 2020,2023 The Pybricks Authors
33
44"""
55:class:`RFCOMMServer` can be used to communicate with other Bluetooth RFCOMM
1010implementation details.
1111"""
1212
13- from bluetooth import BluetoothSocket , RFCOMM
13+ from socket import socket , AF_BLUETOOTH , BTPROTO_RFCOMM , SOCK_STREAM
1414from socketserver import ThreadingMixIn
1515
16- BDADDR_ANY = ""
17-
18-
19- def str2ba (string , ba ):
20- """Convert string to Bluetooth address"""
21- for i , v in enumerate (string .split (":" )):
22- ba .b [5 - i ] = int (v , 16 )
23-
24-
25- def ba2str (ba ):
26- """Convert Bluetooth address to string"""
27- string = []
28- for b in ba .b :
29- string .append ("{:02X}" .format (b ))
30- string .reverse ()
31- return ":" .join (string ).upper ()
32-
3316
3417class RFCOMMServer :
35- """Object that simplifies setting up an RFCOMM socket server.
18+ """
19+ Object that simplifies setting up an RFCOMM socket server.
3620
3721 This is based on the ``socketserver.SocketServer`` class in the Python
3822 standard library.
@@ -44,10 +28,10 @@ def __init__(self, server_address, RequestHandlerClass):
4428 self .server_address = server_address
4529 self .RequestHandlerClass = RequestHandlerClass
4630
47- self .socket = BluetoothSocket ( RFCOMM )
31+ self .socket = socket ( AF_BLUETOOTH , SOCK_STREAM , BTPROTO_RFCOMM )
4832
4933 try :
50- self .socket .bind (( server_address [ 0 ], server_address [ 1 ]) )
34+ self .socket .bind (server_address )
5135 # self.server_address = self.socket.getsockname()
5236 self .socket .listen (self .request_queue_size )
5337 except Exception :
@@ -83,50 +67,21 @@ def server_close(self):
8367 self .socket .close ()
8468
8569
86- class StreamRequestHandler :
87- """Class that handles incoming requests.
88-
89- This is based on ``socketserver.StreamRequestHandler`` from the Python
90- standard library.
91- """
92-
93- def __init__ (self , request , client_address , server ):
94- self .request = request
95- self .client_address = client_address
96- self .server = server
97- self .setup ()
98- try :
99- self .handle ()
100- finally :
101- self .finish ()
102-
103- def setup (self ):
104- self .wfile = self .request
105- self .rfile = self .request
106-
107- def handle (self ):
108- pass
109-
110- def finish (self ):
111- pass
112-
113-
11470class ThreadingRFCOMMServer (ThreadingMixIn , RFCOMMServer ):
115- """Version of :class:`RFCOMMServer` that handles connections in a new
116- thread.
11771 """
118-
119- pass
72+ Version of :class:`RFCOMMServer` that handles connections in a new thread.
73+ """
74+ daemon_threads = True
12075
12176
12277class RFCOMMClient :
12378 def __init__ (self , client_address , RequestHandlerClass ):
12479 self .client_address = client_address
12580 self .RequestHandlerClass = RequestHandlerClass
126- self .socket = BluetoothSocket ( RFCOMM )
81+ self .socket = socket ( AF_BLUETOOTH , SOCK_STREAM , BTPROTO_RFCOMM )
12782
12883 def handle_request (self ):
129- self .socket .connect (( self .client_address [ 0 ], self . client_address [ 1 ]) )
84+ self .socket .connect (self .client_address )
13085 try :
13186 self .process_request (self .socket , self .client_address )
13287 except Exception :
@@ -145,4 +100,7 @@ def client_close(self):
145100
146101
147102class ThreadingRFCOMMClient (ThreadingMixIn , RFCOMMClient ):
148- pass
103+ """
104+ Version of :class:`RFCOMMClient` that handles connections in a new thread.
105+ """
106+ daemon_threads = True
0 commit comments