diff --git a/freestyle_hid/_session.py b/freestyle_hid/_session.py index b204284..24be626 100644 --- a/freestyle_hid/_session.py +++ b/freestyle_hid/_session.py @@ -279,7 +279,7 @@ def _write_hid(self, packet: bytes, hid_report: int = 0) -> None: logging.debug(f"Sending packet: {usb_packet!r}") self._handle.write(usb_packet) - def send_command(self, message_type: int, command: bytes, encrypted: bool = False): + def send_command(self, message_type: int, command: bytes): """Send a raw command to the device. Args: @@ -299,20 +299,25 @@ def send_command(self, message_type: int, command: bytes, encrypted: bool = Fals self._write_hid(message) - def read_response(self, encrypted: bool = False) -> tuple[int, bytes]: + def read_response(self) -> tuple[int, bytes]: """Read the response from the device and extracts it.""" usb_packet = self._handle.read() - logging.debug(f"Read packet: {usb_packet!r}") + logging.debug( + f"Read {'possibly encrypted ' if self._encrypted_protocol else ''}packet: {usb_packet!r}" + ) assert usb_packet message_type = usb_packet[0] - if ( + encrypted = ( self._encrypted_protocol and message_type not in _ALWAYS_UNENCRYPTED_MESSAGES - ): + ) + + if encrypted: usb_packet = self.decrypt_message(usb_packet) + logging.debug(f"Decoded packet: {usb_packet!r}") message_length = usb_packet[1] message_end_idx = 2 + message_length @@ -327,7 +332,7 @@ def read_response(self, encrypted: bool = False) -> tuple[int, bytes]: # unencrypted, so we need to inspect them before we decide what the # message content is. if _is_keepalive_response(message): - return self.read_response(encrypted=encrypted) + return self.read_response() if _is_unknown_message_error(message): raise CommandError("Invalid command") diff --git a/freestyle_hid/models.py b/freestyle_hid/models.py new file mode 100644 index 0000000..bf5c238 --- /dev/null +++ b/freestyle_hid/models.py @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: © 2023 The freestyle-hid Authors +# SPDX-License-Identifier: Apache-2.0 + +FREESTYLE_LIBRE = 0x3650 +FREESTYLE_PRECISION_NEO = 0x3850 +FREESTYLE_LIBRE_2 = 0x3950 diff --git a/freestyle_hid/tools/hid_console.py b/freestyle_hid/tools/hid_console.py index a9a133c..44cff6e 100755 --- a/freestyle_hid/tools/hid_console.py +++ b/freestyle_hid/tools/hid_console.py @@ -12,6 +12,7 @@ import click_log import freestyle_hid +from freestyle_hid import models logger = logging.getLogger() click_log.basic_config(logger) @@ -56,8 +57,10 @@ ) @click.argument( "device-path", - type=click.Path(exists=True, dir_okay=False, writable=True, allow_dash=False), - callback=lambda ctx, param, value: pathlib.Path(value) if value else None, + type=click.Path(exists=True, dir_okay=False, writable=True, allow_dash=True), + callback=lambda ctx, param, value: pathlib.Path(value) + if value and value != "-" + else None, required=False, ) @click.argument( @@ -80,6 +83,11 @@ def main( "One of --product-id or DEVICE_PATH need to be provided." ) + if product_id == models.FREESTYLE_LIBRE_2: + # No matter if the user requested it or not, in this case we know + # the protocol needs to be encrypted. + encrypted_protocol = True + session = freestyle_hid.Session( product_id, device_path,