Skip to content

Commit

Permalink
Introduce Enum for notification types
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHag committed May 28, 2024
1 parent 378d6c8 commit 2a2895c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 10 additions & 0 deletions lib/logitech_receiver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,13 @@ class Alert(IntEnum):
SHOW_WINDOW = 0x02
ATTENTION = 0x04
ALL = 0xFF


class Notification(IntEnum):
NO_OPERATION = 0x00
CONNECT_DISCONNECT = 0x40
DJ_PAIRING = 0x41
CONNECTED = 0x42
RAW_INPUT = 0x49
PAIRING_LOCK = 0x4A
POWER = 0x4B
20 changes: 11 additions & 9 deletions lib/logitech_receiver/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from . import settings_templates
from .common import Alert
from .common import BatteryStatus
from .common import Notification

logger = logging.getLogger(__name__)

Expand All @@ -58,7 +59,7 @@ def _process_receiver_notification(receiver, n):
# supposedly only 0x4x notifications arrive for the receiver
assert n.sub_id & 0x40 == 0x40

if n.sub_id == 0x4A: # pairing lock notification
if n.sub_id == Notification.PAIRING_LOCK:
receiver.pairing.lock_open = bool(n.address & 0x01)
reason = _("pairing lock is open") if receiver.pairing.lock_open else _("pairing lock is closed")
if logger.isEnabledFor(logging.INFO):
Expand Down Expand Up @@ -147,7 +148,8 @@ def _process_device_notification(device, n):
# incoming packets with SubId >= 0x80 are supposedly replies from HID++ 1.0 requests, should never get here
assert n.sub_id & 0x80 == 0

if n.sub_id == 00: # no-op feature notification, dispose of it quickly
if n.sub_id == Notification.NO_OPERATION:
# dispose it
return False

# Allow the device object to handle the notification using custom per-device state.
Expand Down Expand Up @@ -188,19 +190,19 @@ def _process_dj_notification(device, n):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("%s (%s) DJ %s", device, device.protocol, n)

if n.sub_id == 0x40:
if n.sub_id == Notification.CONNECT_DISCONNECT:
# do all DJ paired notifications also show up as HID++ 1.0 notifications?
if logger.isEnabledFor(logging.INFO):
logger.info("%s: ignoring DJ unpaired: %s", device, n)
return True

if n.sub_id == 0x41:
if n.sub_id == Notification.DJ_PAIRING:
# do all DJ paired notifications also show up as HID++ 1.0 notifications?
if logger.isEnabledFor(logging.INFO):
logger.info("%s: ignoring DJ paired: %s", device, n)
return True

if n.sub_id == 0x42:
if n.sub_id == Notification.CONNECTED:
connected = not n.address & 0x01
if logger.isEnabledFor(logging.INFO):
logger.info("%s: DJ connection: %s %s", device, connected, n)
Expand All @@ -224,7 +226,7 @@ def _process_hidpp10_custom_notification(device, n):


def _process_hidpp10_notification(device, n):
if n.sub_id == 0x40: # device unpairing
if n.sub_id == Notification.CONNECT_DISCONNECT: # device unpairing
if n.address == 0x02:
# device un-paired
device.wpid = None
Expand All @@ -236,7 +238,7 @@ def _process_hidpp10_notification(device, n):
logger.warning("%s: disconnection with unknown type %02X: %s", device, n.address, n)
return True

if n.sub_id == 0x41: # device connection (and disconnection)
if n.sub_id == Notification.DJ_PAIRING: # device connection (and disconnection)
flags = ord(n.data[:1]) & 0xF0
if n.address == 0x02: # very old 27 MHz protocol
wpid = "00" + common.strhex(n.data[2:3])
Expand Down Expand Up @@ -267,13 +269,13 @@ def _process_hidpp10_notification(device, n):
device.changed(active=link_established)
return True

if n.sub_id == 0x49:
if n.sub_id == Notification.RAW_INPUT:
# raw input event? just ignore it
# if n.address == 0x01, no idea what it is, but they keep on coming
# if n.address == 0x03, appears to be an actual input event, because they only come when input happents
return True

if n.sub_id == 0x4B: # power notification
if n.sub_id == Notification.POWER:
if n.address == 0x01:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("%s: device powered on", device)
Expand Down
3 changes: 2 additions & 1 deletion lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from . import hidpp10
from . import hidpp10_constants
from .common import Alert
from .common import Notification
from .device import Device

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -230,7 +231,7 @@ def register_new_device(self, number, notification=None):
raise IndexError(f"{self}: device number {int(number)} already registered")

assert notification is None or notification.devnumber == number
assert notification is None or notification.sub_id == 0x41
assert notification is None or notification.sub_id == Notification.DJ_PAIRING

try:
time.sleep(0.05) # let receiver settle
Expand Down

0 comments on commit 2a2895c

Please sign in to comment.