Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions freestyle_hid/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions freestyle_hid/models.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions freestyle_hid/tools/hid_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import click_log

import freestyle_hid
from freestyle_hid import models

logger = logging.getLogger()
click_log.basic_config(logger)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down