Review comment Fix

This commit is contained in:
skarnataki
2023-09-27 10:57:51 +00:00
committed by Lucas Abel
parent 16d33199eb
commit 5ce353bcde
3 changed files with 186 additions and 131 deletions

View File

@@ -15,11 +15,13 @@
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
from __future__ import annotations
import logging
import asyncio
from pyee import EventEmitter
from typing import Optional, Tuple, Callable, Dict, Union
from .device import Device, Connection
from . import core, l2cap # type: ignore
from .colors import color # type: ignore
@@ -36,8 +38,8 @@ logger = logging.getLogger(__name__)
# -----------------------------------------------------------------------------
# fmt: off
HID_CTRL_PSM = 0x0011
HID_INTR_PSM = 0x0013
HID_CONTROL_PSM = 0x0011
HID_INTERRUPT_PSM = 0x0013
# HIDP message types
HID_HANDSHAKE = 0x00
@@ -71,11 +73,64 @@ HID_SUSPEND = 0x03
HID_EXIT_SUSPEND = 0x04
HID_VIRTUAL_CABLE_UNPLUG = 0x05
class HIDPacket():
def __init__(self,
report_type: Optional[int] = None,
report_id: Optional[int] = None,
buffer_size: Optional[int] = None,
protocol_mode: Optional[int] = None,
data: Optional[bytes] = None) -> None:
self.report_type = report_type
self.report_id = report_id
self.buffer_size = buffer_size
self.protocol_mode = protocol_mode
self.data = data
def to_bytes_gr(self) -> bytes:
if(self.report_type == HID_OTHER_REPORT):
param = self.report_type
else:
param = 0x08 | self.report_type
header = ((HID_GET_REPORT << 4) | param)
packet_bytes = bytearray()
packet_bytes.append(header)
packet_bytes.append(self.report_id)
packet_bytes.extend([(self.buffer_size & 0xff), ((self.buffer_size >> 8) & 0xff)])
return bytes(packet_bytes)
def to_bytes_sr(self) -> bytes:
header = ((HID_SET_REPORT << 4) | self.report_type)
packet_bytes = bytearray()
packet_bytes.append(header)
packet_bytes.extend(self.data)
return bytes(packet_bytes)
def to_bytes_gp(self) -> bytes:
header = (HID_GET_PROTOCOL << 4)
packet_bytes = bytearray()
packet_bytes.append(header)
return bytes(packet_bytes)
def to_bytes_sp(self) -> bytes:
header = (HID_SET_PROTOCOL << 4 | self.protocol_mode)
packet_bytes = bytearray()
packet_bytes.append(header)
packet_bytes.append(self.protocol_mode)
return bytes(packet_bytes)
def to_bytes_send_data(self) -> bytes:
header = ((HID_DATA << 4) | HID_OUTPUT_REPORT)
packet_bytes = bytearray()
packet_bytes.append(header)
packet_bytes.extend(self.data)
return bytes(packet_bytes)
# -----------------------------------------------------------------------------
class HIDHost(EventEmitter):
l2cap_channel: Optional[l2cap.Channel]
def __init__(self, device, connection) -> None:
def __init__(self, device: Device, connection: Connection) -> None:
super().__init__()
self.device = device
self.connection = connection
@@ -83,14 +138,14 @@ class HIDHost(EventEmitter):
self.l2cap_intr_channel = None
# Register ourselves with the L2CAP channel manager
device.register_l2cap_server(HID_CTRL_PSM, self.on_connection)
device.register_l2cap_server(HID_INTR_PSM, self.on_connection)
device.register_l2cap_server(HID_CONTROL_PSM, self.on_connection)
device.register_l2cap_server(HID_INTERRUPT_PSM, self.on_connection)
async def control_channel_connect(self) -> None:
async def connect_control_channel(self) -> None:
# Create a new L2CAP connection - control channel
try:
self.l2cap_ctrl_channel = await self.device.l2cap_channel_manager.connect(
self.connection, HID_CTRL_PSM
self.connection, HID_CONTROL_PSM
)
except ProtocolError as error:
logger.error(f'L2CAP connection failed: {error}')
@@ -100,11 +155,11 @@ class HIDHost(EventEmitter):
# Become a sink for the L2CAP channel
self.l2cap_ctrl_channel.sink = self.on_ctrl_pdu
async def interrupt_channel_connect(self) -> None:
async def connect_interrupt_channel(self) -> None:
# Create a new L2CAP connection - interrupt channel
try:
self.l2cap_intr_channel = await self.device.l2cap_channel_manager.connect(
self.connection, HID_INTR_PSM
self.connection, HID_INTERRUPT_PSM
)
except ProtocolError as error:
logger.error(f'L2CAP connection failed: {error}')
@@ -114,10 +169,14 @@ class HIDHost(EventEmitter):
# Become a sink for the L2CAP channel
self.l2cap_intr_channel.sink = self.on_intr_pdu
async def interrupt_channel_disconnect(self):
async def disconnect_interrupt_channel(self) -> None:
if self.l2cap_intr_channel is None:
raise InvalidStateError('invalid state')
await self.l2cap_intr_channel.disconnect() # type: ignore
async def control_channel_disconnect(self):
async def disconnect_control_channel(self) -> None:
if self.l2cap_ctrl_channel is None:
raise InvalidStateError('invalid state')
await self.l2cap_ctrl_channel.disconnect() # type: ignore
def on_connection(self, l2cap_channel: l2cap.Channel) -> None:
@@ -125,7 +184,7 @@ class HIDHost(EventEmitter):
l2cap_channel.on('open', lambda: self.on_l2cap_channel_open(l2cap_channel))
def on_l2cap_channel_open(self, l2cap_channel: l2cap.Channel) -> None:
if l2cap_channel.psm == HID_CTRL_PSM:
if l2cap_channel.psm == HID_CONTROL_PSM:
self.l2cap_ctrl_channel = l2cap_channel
self.l2cap_ctrl_channel.sink = self.on_ctrl_pdu
else:
@@ -138,21 +197,21 @@ class HIDHost(EventEmitter):
# Here we will receive all kinds of packets, parse and then call respective callbacks
message_type = pdu[0] >> 4
param = pdu[0] & 0x0f
if (message_type == HID_HANDSHAKE):
if message_type == HID_HANDSHAKE :
logger.debug('<<< HID HANDSHAKE')
self.handle_handshake(param)
self.emit('handshake', )
elif (message_type == HID_DATA):
self.emit('handshake', pdu)
elif message_type == HID_DATA :
logger.debug('<<< HID CONTROL DATA')
self.emit('data', pdu)
elif (message_type == HID_CONTROL):
if (param == HID_SUSPEND):
elif message_type == HID_CONTROL :
if param == HID_SUSPEND :
logger.debug('<<< HID SUSPEND')
self.emit('suspend', pdu)
elif (param == HID_EXIT_SUSPEND):
elif param == HID_EXIT_SUSPEND :
logger.debug('<<< HID EXIT SUSPEND')
self.emit('exit_suspend', pdu)
elif (param == HID_VIRTUAL_CABLE_UNPLUG):
elif param == HID_VIRTUAL_CABLE_UNPLUG :
logger.debug('<<< HID VIRTUAL CABLE UNPLUG')
self.emit('virtual_cable_unplug')
else:
@@ -165,50 +224,41 @@ class HIDHost(EventEmitter):
logger.debug(f'<<< HID INTERRUPT PDU: {pdu.hex()}')
self.emit("data", pdu)
def register_data_cb(self, data_cb):
self.on('data', data_cb)
def get_report(self, report_type: int, report_id: int, buffer_size: int) -> None:
msg = HIDPacket(report_type = report_type , report_id = report_id , buffer_size = buffer_size)
hid_packet = msg.to_bytes_gr()
logger.debug(f'>>> HID CONTROL GET REPORT, PDU: {hid_packet.hex()}')
self.send_pdu_on_ctrl(hid_packet) # type: ignore
def register_handshake_cb(self, handshake_cb):
self.on('handshake', handshake_cb)
def register_virtual_cable_unplug(self, virtual_cable_unplug_cb):
self.on('virtual_cable_unplug', virtual_cable_unplug_cb)
def get_report(self, report_type, report_id, buffer_size):
if(report_type == HID_OTHER_REPORT):
param = report_type
else:
param = 0x08 | report_type
header = ((HID_GET_REPORT << 4) | param)
msg = bytes([header, report_id, (buffer_size & 0xff), ((buffer_size >> 8) & 0xff)])
logger.debug(f'>>> HID CONTROL GET REPORT, PDU: {msg.hex()}')
self.l2cap_ctrl_channel.send_pdu(msg) # type: ignore
def set_report(self, report_type, data):
header = ((HID_SET_REPORT << 4) | report_type)
msg = bytearray([header])
msg.extend(data)
logger.debug(f'>>> HID CONTROL SET REPORT, PDU:{msg.hex()}')
self.l2cap_ctrl_channel.send_pdu(msg) # type: ignore
def set_report(self, report_type: int, data: bytes):
msg = HIDPacket(report_type= report_type,data = data)
hid_packet = msg.to_bytes_sr()
logger.debug(f'>>> HID CONTROL SET REPORT, PDU:{hid_packet.hex()}')
self.send_pdu_on_ctrl(hid_packet) # type: ignore
def get_protocol(self):
header = (HID_GET_PROTOCOL << 4)
msg = bytearray([header])
logger.debug(f'>>> HID CONTROL GET PROTOCOL, PDU: {msg.hex()}')
msg = HIDPacket()
hid_packet = msg.to_bytes_gp()
logger.debug(f'>>> HID CONTROL GET PROTOCOL, PDU: {hid_packet.hex()}')
self.send_pdu_on_ctrl(hid_packet) # type: ignore
def set_protocol(self, protocol_mode: int):
msg = HIDPacket(protocol_mode= protocol_mode)
hid_packet = msg.to_bytes_sp()
logger.debug(f'>>> HID CONTROL SET PROTOCOL, PDU: {hid_packet.hex()}')
self.send_pdu_on_ctrl(hid_packet) # type: ignore
def send_pdu_on_ctrl(self, msg: bytes) -> None:
self.l2cap_ctrl_channel.send_pdu(msg) # type: ignore
def set_protocol(self, protocol_mode):
header = (HID_SET_PROTOCOL << 4 | protocol_mode)
msg = bytearray([header])
logger.debug(f'>>> HID CONTROL SET PROTOCOL, PDU: {msg.hex()}')
self.l2cap_ctrl_channel.send_pdu(msg) # type: ignore
def send_pdu_on_intr(self, msg: bytes) -> None:
self.l2cap_intr_channel.send_pdu(msg) # type: ignore
def send_data(self, data):
header = ((HID_DATA << 4) | HID_OUTPUT_REPORT)
msg = bytearray([header])
msg.extend(data)
logger.debug(f'>>> HID INTERRUPT SEND DATA, PDU: {msg.hex()}')
self.l2cap_intr_channel.send_pdu(msg) # type: ignore
msg = HIDPacket(data= data)
hid_packet = msg.to_bytes_send_data()
logger.debug(f'>>> HID INTERRUPT SEND DATA, PDU: {hid_packet.hex()}')
self.send_pdu_on_intr(hid_packet) # type: ignore
def suspend(self):
header = (HID_CONTROL << 4 | HID_SUSPEND)
@@ -228,19 +278,18 @@ class HIDHost(EventEmitter):
logger.debug(f'>>> HID CONTROL VIRTUAL CABLE UNPLUG, PDU: {msg.hex()}')
self.l2cap_ctrl_channel.send_pdu(msg) # type: ignore
def handle_handshake(self, param):
if (param == HANDSHAKE_SUCCESSFUL):
def handle_handshake(self, param: int):
if param == HANDSHAKE_SUCCESSFUL :
logger.debug(f'<<< HID HANDSHAKE: SUCCESSFUL')
elif (param == HANDSHAKE_NOT_READY):
elif param == HANDSHAKE_NOT_READY :
logger.warning(f'<<< HID HANDSHAKE: NOT_READY')
elif (param == HANDSHAKE_ERR_INVALID_REPORT_ID):
elif param == HANDSHAKE_ERR_INVALID_REPORT_ID :
logger.warning(f'<<< HID HANDSHAKE: ERR_INVALID_REPORT_ID')
elif (param == HANDSHAKE_ERR_UNSUPPORTED_REQUEST):
elif param == HANDSHAKE_ERR_UNSUPPORTED_REQUEST :
logger.warning(f'<<< HID HANDSHAKE: ERR_UNSUPPORTED_REQUEST')
elif (param == HANDSHAKE_ERR_UNKNOWN):
elif param == HANDSHAKE_ERR_UNKNOWN :
logger.warning(f'<<< HID HANDSHAKE: ERR_UNKNOWN')
elif (param == HANDSHAKE_ERR_FATAL):
elif param == HANDSHAKE_ERR_FATAL :
logger.warning(f'<<< HID HANDSHAKE: ERR_FATAL')
else: # 0x5-0xD = Reserved
logger.warning("<<< HID HANDSHAKE: RESERVED VALUE")

View File

@@ -167,7 +167,7 @@ class DataElement:
UUID: lambda x: DataElement(
DataElement.UUID, core.UUID.from_bytes(bytes(reversed(x)))
),
TEXT_STRING: lambda x: DataElement(DataElement.TEXT_STRING, x),
TEXT_STRING: lambda x: DataElement(DataElement.TEXT_STRING, x.decode('latin1')),
BOOLEAN: lambda x: DataElement(DataElement.BOOLEAN, x[0] == 1),
SEQUENCE: lambda x: DataElement(
DataElement.SEQUENCE, DataElement.list_from_bytes(x)
@@ -376,7 +376,9 @@ class DataElement:
raise ValueError('invalid value_size')
elif self.type == DataElement.UUID:
data = bytes(reversed(bytes(self.value)))
elif self.type in (DataElement.TEXT_STRING, DataElement.URL):
elif self.type == DataElement.TEXT_STRING:
data = self.value.encode('latin1')
elif self.type == DataElement.URL:
data = self.value.encode('utf8')
elif self.type == DataElement.BOOLEAN:
data = bytes([1 if self.value else 0])