Move client to own folder

This commit is contained in:
zvecr 2022-07-17 01:28:54 +01:00
parent a90e2e6a76
commit 70eae6b348
5 changed files with 60 additions and 59 deletions

View File

@ -7,7 +7,7 @@ from milc import cli
from qmk.keyboard import render_layout from qmk.keyboard import render_layout
from qmk.xap.common import get_xap_keycodes from qmk.xap.common import get_xap_keycodes
from .xap_client import XAPClient, XAPEventType, XAPSecureStatus from xap_client import XAPClient, XAPEventType, XAPSecureStatus
KEYCODE_MAP = get_xap_keycodes('latest') KEYCODE_MAP = get_xap_keycodes('latest')

View File

@ -0,0 +1,2 @@
from .types import * # noqa: F403
from .client import * # noqa: F403

View File

@ -0,0 +1,29 @@
"""XAP Client
"""
import hid
from .device import XAPDevice
class XAPClient:
@staticmethod
def list(search=None):
"""Find compatible XAP devices
"""
def _is_xap_usage(x):
return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
def _is_filtered_device(x):
name = '%04x:%04x' % (x['vendor_id'], x['product_id'])
return name.lower().startswith(search.lower())
devices = filter(_is_xap_usage, hid.enumerate())
if search:
devices = filter(_is_filtered_device, devices)
return list(devices)
def connect(self, dev):
"""Connect to a given XAP device
"""
return XAPDevice(dev)

View File

@ -1,5 +1,6 @@
"""Dummy XAP Client """XAP Device
""" """
import hid
import json import json
import random import random
import gzip import gzip
@ -7,9 +8,11 @@ import threading
import functools import functools
from struct import Struct, pack, unpack from struct import Struct, pack, unpack
from collections import namedtuple from collections import namedtuple
from enum import IntFlag, IntEnum
from platform import platform from platform import platform
from .types import XAPSecureStatus, XAPFlags, XAPRouteError
RequestPacket = namedtuple('RequestPacket', 'token length data') RequestPacket = namedtuple('RequestPacket', 'token length data')
RequestStruct = Struct('<HB61s') RequestStruct = Struct('<HB61s')
@ -32,28 +35,6 @@ def _u32toBCD(val): # noqa: N802
return f'{val>>24}.{val>>16 & 0xFF}.{val & 0xFFFF}' return f'{val>>24}.{val>>16 & 0xFF}.{val & 0xFFFF}'
class XAPSecureStatus(IntEnum):
LOCKED = 0x00
UNLOCKING = 0x01
UNLOCKED = 0x02
class XAPFlags(IntFlag):
FAILURE = 0
SUCCESS = 1 << 0
SECURE_FAILURE = 1 << 1
class XAPEventType(IntEnum):
SECURE = 0x01
KEYBOARD = 0x02
USER = 0x03
class XAPRouteError(Exception):
pass
class XAPDevice: class XAPDevice:
def __init__(self, dev): def __init__(self, dev):
"""Constructor opens hid device and starts dependent services """Constructor opens hid device and starts dependent services
@ -191,37 +172,3 @@ class XAPDevice:
def reset(self): def reset(self):
status = int.from_bytes(self.transaction(b'\x01\x07') or bytes(0), 'little') status = int.from_bytes(self.transaction(b'\x01\x07') or bytes(0), 'little')
return status == 1 return status == 1
class XAPClient:
@staticmethod
def _lazy_imports():
# Lazy load to avoid missing dependency issues
global hid
import hid
@staticmethod
def list(search=None):
"""Find compatible XAP devices
"""
XAPClient._lazy_imports()
def _is_xap_usage(x):
return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
def _is_filtered_device(x):
name = '%04x:%04x' % (x['vendor_id'], x['product_id'])
return name.lower().startswith(search.lower())
devices = filter(_is_xap_usage, hid.enumerate())
if search:
devices = filter(_is_filtered_device, devices)
return list(devices)
def connect(self, dev):
"""Connect to a given XAP device
"""
XAPClient._lazy_imports()
return XAPDevice(dev)

View File

@ -0,0 +1,23 @@
from enum import IntFlag, IntEnum
class XAPSecureStatus(IntEnum):
LOCKED = 0x00
UNLOCKING = 0x01
UNLOCKED = 0x02
class XAPFlags(IntFlag):
FAILURE = 0
SUCCESS = 1 << 0
SECURE_FAILURE = 1 << 1
class XAPEventType(IntEnum):
SECURE = 0x01
KEYBOARD = 0x02
USER = 0x03
class XAPRouteError(Exception):
pass