From 5fba6b1caedf9e0c16220ef0cc19b76e7bb7f387 Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Thu, 27 Jul 2023 19:19:11 +0800 Subject: [PATCH 1/4] Complete typing in RFCOMM --- bumble/rfcomm.py | 77 +++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/bumble/rfcomm.py b/bumble/rfcomm.py index 0176a78a..579fad9a 100644 --- a/bumble/rfcomm.py +++ b/bumble/rfcomm.py @@ -19,12 +19,15 @@ import logging import asyncio from pyee import EventEmitter -from typing import Optional, Tuple, Callable, Dict, Union +from typing import Optional, Tuple, Callable, Dict, Union, TYPE_CHECKING from . import core, l2cap from .colors import color from .core import BT_BR_EDR_TRANSPORT, InvalidStateError, ProtocolError +if TYPE_CHECKING: + from bumble.device import Device, Connection + # ----------------------------------------------------------------------------- # Logging # ----------------------------------------------------------------------------- @@ -149,9 +152,9 @@ class RFCOMM_Frame: return RFCOMM_FRAME_TYPE_NAMES[self.type] @staticmethod - def parse_mcc(data) -> Tuple[int, int, bytes]: + def parse_mcc(data) -> Tuple[int, bool, bytes]: mcc_type = data[0] >> 2 - c_r = (data[0] >> 1) & 1 + c_r = bool((data[0] >> 1) & 1) length = data[1] if data[1] & 1: length >>= 1 @@ -215,7 +218,7 @@ class RFCOMM_Frame: return frame - def __bytes__(self): + def __bytes__(self) -> bytes: return ( bytes([self.address, self.control]) + self.length @@ -223,7 +226,7 @@ class RFCOMM_Frame: + bytes([self.fcs]) ) - def __str__(self): + def __str__(self) -> str: return ( f'{color(self.type_name(), "yellow")}' f'(c/r={self.c_r},' @@ -253,7 +256,7 @@ class RFCOMM_MCC_PN: max_frame_size: int, max_retransmissions: int, window_size: int, - ): + ) -> None: self.dlci = dlci self.cl = cl self.priority = priority @@ -263,7 +266,7 @@ class RFCOMM_MCC_PN: self.window_size = window_size @staticmethod - def from_bytes(data: bytes): + def from_bytes(data: bytes) -> 'RFCOMM_MCC_PN': return RFCOMM_MCC_PN( dlci=data[0], cl=data[1], @@ -274,7 +277,7 @@ class RFCOMM_MCC_PN: window_size=data[7], ) - def __bytes__(self): + def __bytes__(self) -> bytes: return bytes( [ self.dlci & 0xFF, @@ -288,7 +291,7 @@ class RFCOMM_MCC_PN: ] ) - def __str__(self): + def __str__(self) -> str: return ( f'PN(dlci={self.dlci},' f'cl={self.cl},' @@ -309,7 +312,9 @@ class RFCOMM_MCC_MSC: ic: int dv: int - def __init__(self, dlci: int, fc: int, rtc: int, rtr: int, ic: int, dv: int): + def __init__( + self, dlci: int, fc: int, rtc: int, rtr: int, ic: int, dv: int + ) -> None: self.dlci = dlci self.fc = fc self.rtc = rtc @@ -318,7 +323,7 @@ class RFCOMM_MCC_MSC: self.dv = dv @staticmethod - def from_bytes(data: bytes): + def from_bytes(data: bytes) -> 'RFCOMM_MCC_MSC': return RFCOMM_MCC_MSC( dlci=data[0] >> 2, fc=data[1] >> 1 & 1, @@ -328,7 +333,7 @@ class RFCOMM_MCC_MSC: dv=data[1] >> 7 & 1, ) - def __bytes__(self): + def __bytes__(self) -> bytes: return bytes( [ (self.dlci << 2) | 3, @@ -341,7 +346,7 @@ class RFCOMM_MCC_MSC: ] ) - def __str__(self): + def __str__(self) -> str: return ( f'MSC(dlci={self.dlci},' f'fc={self.fc},' @@ -375,8 +380,12 @@ class DLC(EventEmitter): sink: Optional[Callable[[bytes], None]] def __init__( - self, multiplexer, dlci: int, max_frame_size: int, initial_tx_credits: int - ): + self, + multiplexer: 'Multiplexer', + dlci: int, + max_frame_size: int, + initial_tx_credits: int, + ) -> None: super().__init__() self.multiplexer = multiplexer self.dlci = dlci @@ -413,7 +422,7 @@ class DLC(EventEmitter): handler = getattr(self, f'on_{frame.type_name()}_frame'.lower()) handler(frame) - def on_sabm_frame(self, _frame) -> None: + def on_sabm_frame(self, _frame: RFCOMM_Frame) -> None: if self.state != DLC.CONNECTING: logger.warning( color('!!! received SABM when not in CONNECTING state', 'red') @@ -433,7 +442,7 @@ class DLC(EventEmitter): self.change_state(DLC.CONNECTED) self.emit('open') - def on_ua_frame(self, _frame) -> None: + def on_ua_frame(self, _frame: RFCOMM_Frame) -> None: if self.state != DLC.CONNECTING: logger.warning( color('!!! received SABM when not in CONNECTING state', 'red') @@ -451,11 +460,11 @@ class DLC(EventEmitter): self.change_state(DLC.CONNECTED) self.multiplexer.on_dlc_open_complete(self) - def on_dm_frame(self, frame) -> None: + def on_dm_frame(self, frame: RFCOMM_Frame) -> None: # TODO: handle all states pass - def on_disc_frame(self, _frame) -> None: + def on_disc_frame(self, _frame: RFCOMM_Frame) -> None: # TODO: handle all states self.send_frame(RFCOMM_Frame.ua(c_r=1 - self.c_r, dlci=self.dlci)) @@ -489,10 +498,10 @@ class DLC(EventEmitter): # Check if there's anything to send (including credits) self.process_tx() - def on_ui_frame(self, frame) -> None: + def on_ui_frame(self, frame: RFCOMM_Frame) -> None: pass - def on_mcc_msc(self, c_r, msc) -> None: + def on_mcc_msc(self, c_r: bool, msc: RFCOMM_MCC_MSC) -> None: if c_r: # Command logger.debug(f'<<< MCC MSC Command: {msc}') @@ -592,7 +601,7 @@ class DLC(EventEmitter): # TODO pass - def __str__(self): + def __str__(self) -> str: return f'DLC(dlci={self.dlci},state={self.state_name(self.state)})' @@ -679,14 +688,14 @@ class Multiplexer(EventEmitter): handler = getattr(self, f'on_{frame.type_name()}_frame'.lower()) handler(frame) - def on_sabm_frame(self, _frame) -> None: + def on_sabm_frame(self, _frame: RFCOMM_Frame) -> None: if self.state != Multiplexer.INIT: logger.debug('not in INIT state, ignoring SABM') return self.change_state(Multiplexer.CONNECTED) self.send_frame(RFCOMM_Frame.ua(c_r=1, dlci=0)) - def on_ua_frame(self, _frame) -> None: + def on_ua_frame(self, _frame: RFCOMM_Frame) -> None: if self.state == Multiplexer.CONNECTING: self.change_state(Multiplexer.CONNECTED) if self.connection_result: @@ -698,7 +707,7 @@ class Multiplexer(EventEmitter): self.disconnection_result.set_result(None) self.disconnection_result = None - def on_dm_frame(self, _frame) -> None: + def on_dm_frame(self, _frame: RFCOMM_Frame) -> None: if self.state == Multiplexer.OPENING: self.change_state(Multiplexer.CONNECTED) if self.open_result: @@ -713,7 +722,7 @@ class Multiplexer(EventEmitter): else: logger.warning(f'unexpected state for DM: {self}') - def on_disc_frame(self, _frame) -> None: + def on_disc_frame(self, _frame: RFCOMM_Frame) -> None: self.change_state(Multiplexer.DISCONNECTED) self.send_frame( RFCOMM_Frame.ua(c_r=0 if self.role == Multiplexer.INITIATOR else 1, dlci=0) @@ -729,11 +738,11 @@ class Multiplexer(EventEmitter): mcs = RFCOMM_MCC_MSC.from_bytes(value) self.on_mcc_msc(c_r, mcs) - def on_ui_frame(self, frame) -> None: + def on_ui_frame(self, frame: RFCOMM_Frame) -> None: pass - def on_mcc_pn(self, c_r, pn) -> None: - if c_r == 1: + def on_mcc_pn(self, c_r: bool, pn: RFCOMM_MCC_PN) -> None: + if c_r: # Command logger.debug(f'<<< PN Command: {pn}') @@ -771,7 +780,7 @@ class Multiplexer(EventEmitter): else: logger.warning('ignoring PN response') - def on_mcc_msc(self, c_r, msc) -> None: + def on_mcc_msc(self, c_r: bool, msc: RFCOMM_MCC_MSC) -> None: dlc = self.dlcs.get(msc.dlci) if dlc is None: logger.warning(f'no dlc for DLCI {msc.dlci}') @@ -831,13 +840,13 @@ class Multiplexer(EventEmitter): self.open_result = None return result - def on_dlc_open_complete(self, dlc: DLC): + def on_dlc_open_complete(self, dlc: DLC) -> None: logger.debug(f'DLC [{dlc.dlci}] open complete') self.change_state(Multiplexer.CONNECTED) if self.open_result: self.open_result.set_result(dlc) - def __str__(self): + def __str__(self) -> str: return f'Multiplexer(state={self.state_name(self.state)})' @@ -846,7 +855,7 @@ class Client: multiplexer: Optional[Multiplexer] l2cap_channel: Optional[l2cap.Channel] - def __init__(self, device, connection) -> None: + def __init__(self, device: 'Device', connection: 'Connection') -> None: self.device = device self.connection = connection self.l2cap_channel = None @@ -886,7 +895,7 @@ class Client: class Server(EventEmitter): acceptors: Dict[int, Callable[[DLC], None]] - def __init__(self, device) -> None: + def __init__(self, device: 'Device') -> None: super().__init__() self.device = device self.multiplexer = None From 2527a711dca218ae3556cde7a3f4413474f0dd14 Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Thu, 27 Jul 2023 19:23:11 +0800 Subject: [PATCH 2/4] Refactor RFCOMM states to enum --- bumble/rfcomm.py | 130 ++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 80 deletions(-) diff --git a/bumble/rfcomm.py b/bumble/rfcomm.py index 579fad9a..0ca53d8b 100644 --- a/bumble/rfcomm.py +++ b/bumble/rfcomm.py @@ -17,6 +17,7 @@ # ----------------------------------------------------------------------------- import logging import asyncio +import enum from pyee import EventEmitter from typing import Optional, Tuple, Callable, Dict, Union, TYPE_CHECKING @@ -359,22 +360,13 @@ class RFCOMM_MCC_MSC: # ----------------------------------------------------------------------------- class DLC(EventEmitter): - # States - INIT = 0x00 - CONNECTING = 0x01 - CONNECTED = 0x02 - DISCONNECTING = 0x03 - DISCONNECTED = 0x04 - RESET = 0x05 - - STATE_NAMES = { - INIT: 'INIT', - CONNECTING: 'CONNECTING', - CONNECTED: 'CONNECTED', - DISCONNECTING: 'DISCONNECTING', - DISCONNECTED: 'DISCONNECTED', - RESET: 'RESET', - } + class State(enum.IntEnum): + INIT = 0x00 + CONNECTING = 0x01 + CONNECTED = 0x02 + DISCONNECTING = 0x03 + DISCONNECTED = 0x04 + RESET = 0x05 connection_result: Optional[asyncio.Future] sink: Optional[Callable[[bytes], None]] @@ -393,7 +385,7 @@ class DLC(EventEmitter): self.rx_threshold = self.rx_credits // 2 self.tx_credits = initial_tx_credits self.tx_buffer = b'' - self.state = DLC.INIT + self.state = DLC.State.INIT self.role = multiplexer.role self.c_r = 1 if self.role == Multiplexer.INITIATOR else 0 self.sink = None @@ -405,14 +397,8 @@ class DLC(EventEmitter): max_frame_size, self.multiplexer.l2cap_channel.mtu - max_overhead ) - @staticmethod - def state_name(state: int) -> str: - return DLC.STATE_NAMES[state] - - def change_state(self, new_state: int) -> None: - logger.debug( - f'{self} state change -> {color(self.state_name(new_state), "magenta")}' - ) + def change_state(self, new_state: State) -> None: + logger.debug(f'{self} state change -> {color(new_state.name, "magenta")}') self.state = new_state def send_frame(self, frame: RFCOMM_Frame) -> None: @@ -423,7 +409,7 @@ class DLC(EventEmitter): handler(frame) def on_sabm_frame(self, _frame: RFCOMM_Frame) -> None: - if self.state != DLC.CONNECTING: + if self.state != DLC.State.CONNECTING: logger.warning( color('!!! received SABM when not in CONNECTING state', 'red') ) @@ -439,11 +425,11 @@ class DLC(EventEmitter): logger.debug(f'>>> MCC MSC Command: {msc}') self.send_frame(RFCOMM_Frame.uih(c_r=self.c_r, dlci=0, information=mcc)) - self.change_state(DLC.CONNECTED) + self.change_state(DLC.State.CONNECTED) self.emit('open') def on_ua_frame(self, _frame: RFCOMM_Frame) -> None: - if self.state != DLC.CONNECTING: + if self.state != DLC.State.CONNECTING: logger.warning( color('!!! received SABM when not in CONNECTING state', 'red') ) @@ -457,7 +443,7 @@ class DLC(EventEmitter): logger.debug(f'>>> MCC MSC Command: {msc}') self.send_frame(RFCOMM_Frame.uih(c_r=self.c_r, dlci=0, information=mcc)) - self.change_state(DLC.CONNECTED) + self.change_state(DLC.State.CONNECTED) self.multiplexer.on_dlc_open_complete(self) def on_dm_frame(self, frame: RFCOMM_Frame) -> None: @@ -516,15 +502,15 @@ class DLC(EventEmitter): logger.debug(f'<<< MCC MSC Response: {msc}') def connect(self) -> None: - if self.state != DLC.INIT: + if self.state != DLC.State.INIT: raise InvalidStateError('invalid state') - self.change_state(DLC.CONNECTING) + self.change_state(DLC.State.CONNECTING) self.connection_result = asyncio.get_running_loop().create_future() self.send_frame(RFCOMM_Frame.sabm(c_r=self.c_r, dlci=self.dlci)) def accept(self) -> None: - if self.state != DLC.INIT: + if self.state != DLC.State.INIT: raise InvalidStateError('invalid state') pn = RFCOMM_MCC_PN( @@ -539,7 +525,7 @@ class DLC(EventEmitter): mcc = RFCOMM_Frame.make_mcc(mcc_type=RFCOMM_MCC_PN_TYPE, c_r=0, data=bytes(pn)) logger.debug(f'>>> PN Response: {pn}') self.send_frame(RFCOMM_Frame.uih(c_r=self.c_r, dlci=0, information=mcc)) - self.change_state(DLC.CONNECTING) + self.change_state(DLC.State.CONNECTING) def rx_credits_needed(self) -> int: if self.rx_credits <= self.rx_threshold: @@ -602,7 +588,7 @@ class DLC(EventEmitter): pass def __str__(self) -> str: - return f'DLC(dlci={self.dlci},state={self.state_name(self.state)})' + return f'DLC(dlci={self.dlci},state={self.state.name})' # ----------------------------------------------------------------------------- @@ -611,24 +597,14 @@ class Multiplexer(EventEmitter): INITIATOR = 0x00 RESPONDER = 0x01 - # States - INIT = 0x00 - CONNECTING = 0x01 - CONNECTED = 0x02 - OPENING = 0x03 - DISCONNECTING = 0x04 - DISCONNECTED = 0x05 - RESET = 0x06 - - STATE_NAMES = { - INIT: 'INIT', - CONNECTING: 'CONNECTING', - CONNECTED: 'CONNECTED', - OPENING: 'OPENING', - DISCONNECTING: 'DISCONNECTING', - DISCONNECTED: 'DISCONNECTED', - RESET: 'RESET', - } + class State(enum.IntEnum): + INIT = 0x00 + CONNECTING = 0x01 + CONNECTED = 0x02 + OPENING = 0x03 + DISCONNECTING = 0x04 + DISCONNECTED = 0x05 + RESET = 0x06 connection_result: Optional[asyncio.Future] disconnection_result: Optional[asyncio.Future] @@ -640,7 +616,7 @@ class Multiplexer(EventEmitter): super().__init__() self.role = role self.l2cap_channel = l2cap_channel - self.state = Multiplexer.INIT + self.state = Multiplexer.State.INIT self.dlcs = {} # DLCs, by DLCI self.connection_result = None self.disconnection_result = None @@ -650,14 +626,8 @@ class Multiplexer(EventEmitter): # Become a sink for the L2CAP channel l2cap_channel.sink = self.on_pdu - @staticmethod - def state_name(state: int): - return Multiplexer.STATE_NAMES[state] - - def change_state(self, new_state: int) -> None: - logger.debug( - f'{self} state change -> {color(self.state_name(new_state), "cyan")}' - ) + def change_state(self, new_state: State) -> None: + logger.debug(f'{self} state change -> {color(new_state.name, "cyan")}') self.state = new_state def send_frame(self, frame: RFCOMM_Frame) -> None: @@ -689,27 +659,27 @@ class Multiplexer(EventEmitter): handler(frame) def on_sabm_frame(self, _frame: RFCOMM_Frame) -> None: - if self.state != Multiplexer.INIT: + if self.state != Multiplexer.State.INIT: logger.debug('not in INIT state, ignoring SABM') return - self.change_state(Multiplexer.CONNECTED) + self.change_state(Multiplexer.State.CONNECTED) self.send_frame(RFCOMM_Frame.ua(c_r=1, dlci=0)) def on_ua_frame(self, _frame: RFCOMM_Frame) -> None: - if self.state == Multiplexer.CONNECTING: - self.change_state(Multiplexer.CONNECTED) + if self.state == Multiplexer.State.CONNECTING: + self.change_state(Multiplexer.State.CONNECTED) if self.connection_result: self.connection_result.set_result(0) self.connection_result = None - elif self.state == Multiplexer.DISCONNECTING: - self.change_state(Multiplexer.DISCONNECTED) + elif self.state == Multiplexer.State.DISCONNECTING: + self.change_state(Multiplexer.State.DISCONNECTED) if self.disconnection_result: self.disconnection_result.set_result(None) self.disconnection_result = None def on_dm_frame(self, _frame: RFCOMM_Frame) -> None: - if self.state == Multiplexer.OPENING: - self.change_state(Multiplexer.CONNECTED) + if self.state == Multiplexer.State.OPENING: + self.change_state(Multiplexer.State.CONNECTED) if self.open_result: self.open_result.set_exception( core.ConnectionError( @@ -723,7 +693,7 @@ class Multiplexer(EventEmitter): logger.warning(f'unexpected state for DM: {self}') def on_disc_frame(self, _frame: RFCOMM_Frame) -> None: - self.change_state(Multiplexer.DISCONNECTED) + self.change_state(Multiplexer.State.DISCONNECTED) self.send_frame( RFCOMM_Frame.ua(c_r=0 if self.role == Multiplexer.INITIATOR else 1, dlci=0) ) @@ -773,7 +743,7 @@ class Multiplexer(EventEmitter): else: # Response logger.debug(f'>>> PN Response: {pn}') - if self.state == Multiplexer.OPENING: + if self.state == Multiplexer.State.OPENING: dlc = DLC(self, pn.dlci, pn.max_frame_size, pn.window_size) self.dlcs[pn.dlci] = dlc dlc.connect() @@ -788,20 +758,20 @@ class Multiplexer(EventEmitter): dlc.on_mcc_msc(c_r, msc) async def connect(self) -> None: - if self.state != Multiplexer.INIT: + if self.state != Multiplexer.State.INIT: raise InvalidStateError('invalid state') - self.change_state(Multiplexer.CONNECTING) + self.change_state(Multiplexer.State.CONNECTING) self.connection_result = asyncio.get_running_loop().create_future() self.send_frame(RFCOMM_Frame.sabm(c_r=1, dlci=0)) return await self.connection_result async def disconnect(self) -> None: - if self.state != Multiplexer.CONNECTED: + if self.state != Multiplexer.State.CONNECTED: return self.disconnection_result = asyncio.get_running_loop().create_future() - self.change_state(Multiplexer.DISCONNECTING) + self.change_state(Multiplexer.State.DISCONNECTING) self.send_frame( RFCOMM_Frame.disc( c_r=1 if self.role == Multiplexer.INITIATOR else 0, dlci=0 @@ -810,8 +780,8 @@ class Multiplexer(EventEmitter): await self.disconnection_result async def open_dlc(self, channel: int) -> DLC: - if self.state != Multiplexer.CONNECTED: - if self.state == Multiplexer.OPENING: + if self.state != Multiplexer.State.CONNECTED: + if self.state == Multiplexer.State.OPENING: raise InvalidStateError('open already in progress') raise InvalidStateError('not connected') @@ -828,7 +798,7 @@ class Multiplexer(EventEmitter): mcc = RFCOMM_Frame.make_mcc(mcc_type=RFCOMM_MCC_PN_TYPE, c_r=1, data=bytes(pn)) logger.debug(f'>>> Sending MCC: {pn}') self.open_result = asyncio.get_running_loop().create_future() - self.change_state(Multiplexer.OPENING) + self.change_state(Multiplexer.State.OPENING) self.send_frame( RFCOMM_Frame.uih( c_r=1 if self.role == Multiplexer.INITIATOR else 0, @@ -842,12 +812,12 @@ class Multiplexer(EventEmitter): def on_dlc_open_complete(self, dlc: DLC) -> None: logger.debug(f'DLC [{dlc.dlci}] open complete') - self.change_state(Multiplexer.CONNECTED) + self.change_state(Multiplexer.State.CONNECTED) if self.open_result: self.open_result.set_result(dlc) def __str__(self) -> str: - return f'Multiplexer(state={self.state_name(self.state)})' + return f'Multiplexer(state={self.state.name})' # ----------------------------------------------------------------------------- From e6b566b84852b11389c23e5edab5594634b2b217 Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Mon, 21 Aug 2023 15:16:34 +0800 Subject: [PATCH 3/4] RFCOMM: Refactor role to enum --- bumble/rfcomm.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bumble/rfcomm.py b/bumble/rfcomm.py index 0ca53d8b..132493d1 100644 --- a/bumble/rfcomm.py +++ b/bumble/rfcomm.py @@ -387,7 +387,7 @@ class DLC(EventEmitter): self.tx_buffer = b'' self.state = DLC.State.INIT self.role = multiplexer.role - self.c_r = 1 if self.role == Multiplexer.INITIATOR else 0 + self.c_r = 1 if self.role == Multiplexer.Role.INITIATOR else 0 self.sink = None self.connection_result = None @@ -593,9 +593,9 @@ class DLC(EventEmitter): # ----------------------------------------------------------------------------- class Multiplexer(EventEmitter): - # Roles - INITIATOR = 0x00 - RESPONDER = 0x01 + class Role(enum.IntEnum): + INITIATOR = 0x00 + RESPONDER = 0x01 class State(enum.IntEnum): INIT = 0x00 @@ -612,7 +612,7 @@ class Multiplexer(EventEmitter): acceptor: Optional[Callable[[int], bool]] dlcs: Dict[int, DLC] - def __init__(self, l2cap_channel: l2cap.Channel, role: int) -> None: + def __init__(self, l2cap_channel: l2cap.Channel, role: Role) -> None: super().__init__() self.role = role self.l2cap_channel = l2cap_channel @@ -695,7 +695,9 @@ class Multiplexer(EventEmitter): def on_disc_frame(self, _frame: RFCOMM_Frame) -> None: self.change_state(Multiplexer.State.DISCONNECTED) self.send_frame( - RFCOMM_Frame.ua(c_r=0 if self.role == Multiplexer.INITIATOR else 1, dlci=0) + RFCOMM_Frame.ua( + c_r=0 if self.role == Multiplexer.Role.INITIATOR else 1, dlci=0 + ) ) def on_uih_frame(self, frame: RFCOMM_Frame) -> None: @@ -774,7 +776,7 @@ class Multiplexer(EventEmitter): self.change_state(Multiplexer.State.DISCONNECTING) self.send_frame( RFCOMM_Frame.disc( - c_r=1 if self.role == Multiplexer.INITIATOR else 0, dlci=0 + c_r=1 if self.role == Multiplexer.Role.INITIATOR else 0, dlci=0 ) ) await self.disconnection_result @@ -801,7 +803,7 @@ class Multiplexer(EventEmitter): self.change_state(Multiplexer.State.OPENING) self.send_frame( RFCOMM_Frame.uih( - c_r=1 if self.role == Multiplexer.INITIATOR else 0, + c_r=1 if self.role == Multiplexer.Role.INITIATOR else 0, dlci=0, information=mcc, ) @@ -843,7 +845,7 @@ class Client: assert self.l2cap_channel is not None # Create a mutliplexer to manage DLCs with the server - self.multiplexer = Multiplexer(self.l2cap_channel, Multiplexer.INITIATOR) + self.multiplexer = Multiplexer(self.l2cap_channel, Multiplexer.Role.INITIATOR) # Connect the multiplexer await self.multiplexer.connect() @@ -904,7 +906,7 @@ class Server(EventEmitter): logger.debug(f'$$$ L2CAP channel open: {l2cap_channel}') # Create a new multiplexer for the channel - multiplexer = Multiplexer(l2cap_channel, Multiplexer.RESPONDER) + multiplexer = Multiplexer(l2cap_channel, Multiplexer.Role.RESPONDER) multiplexer.acceptor = self.accept_dlc multiplexer.on('dlc', self.on_dlc) From 73411727390b4f86284af68c26957061eb7a3684 Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Tue, 22 Aug 2023 14:42:44 +0800 Subject: [PATCH 4/4] Use __future__.annotations for typing --- bumble/rfcomm.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bumble/rfcomm.py b/bumble/rfcomm.py index 132493d1..c67ea560 100644 --- a/bumble/rfcomm.py +++ b/bumble/rfcomm.py @@ -15,6 +15,8 @@ # ----------------------------------------------------------------------------- # Imports # ----------------------------------------------------------------------------- +from __future__ import annotations + import logging import asyncio import enum @@ -196,7 +198,7 @@ class RFCOMM_Frame: ) @staticmethod - def from_bytes(data: bytes): + def from_bytes(data: bytes) -> RFCOMM_Frame: # Extract fields dlci = (data[0] >> 2) & 0x3F c_r = (data[0] >> 1) & 0x01 @@ -267,7 +269,7 @@ class RFCOMM_MCC_PN: self.window_size = window_size @staticmethod - def from_bytes(data: bytes) -> 'RFCOMM_MCC_PN': + def from_bytes(data: bytes) -> RFCOMM_MCC_PN: return RFCOMM_MCC_PN( dlci=data[0], cl=data[1], @@ -324,7 +326,7 @@ class RFCOMM_MCC_MSC: self.dv = dv @staticmethod - def from_bytes(data: bytes) -> 'RFCOMM_MCC_MSC': + def from_bytes(data: bytes) -> RFCOMM_MCC_MSC: return RFCOMM_MCC_MSC( dlci=data[0] >> 2, fc=data[1] >> 1 & 1, @@ -373,7 +375,7 @@ class DLC(EventEmitter): def __init__( self, - multiplexer: 'Multiplexer', + multiplexer: Multiplexer, dlci: int, max_frame_size: int, initial_tx_credits: int, @@ -827,7 +829,7 @@ class Client: multiplexer: Optional[Multiplexer] l2cap_channel: Optional[l2cap.Channel] - def __init__(self, device: 'Device', connection: 'Connection') -> None: + def __init__(self, device: Device, connection: Connection) -> None: self.device = device self.connection = connection self.l2cap_channel = None @@ -867,7 +869,7 @@ class Client: class Server(EventEmitter): acceptors: Dict[int, Callable[[DLC], None]] - def __init__(self, device: 'Device') -> None: + def __init__(self, device: Device) -> None: super().__init__() self.device = device self.multiplexer = None