mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-04-25 00:21:27 +00:00
Prep client gen for header parsing
This commit is contained in:
parent
5c21830da2
commit
7bce3d7b25
@ -1,4 +1,8 @@
|
|||||||
|
|
||||||
|
class XAPRouteError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class XAPRoutes():
|
class XAPRoutes():
|
||||||
{%- for id, route in xap.routes | dictsort %}
|
{%- for id, route in xap.routes | dictsort %}
|
||||||
{%- if route.routes %}
|
{%- if route.routes %}
|
||||||
|
@ -1,8 +1,34 @@
|
|||||||
|
from collections import namedtuple
|
||||||
from enum import IntFlag, IntEnum
|
from enum import IntFlag, IntEnum
|
||||||
|
from struct import Struct
|
||||||
|
|
||||||
|
|
||||||
class XAPRouteError(Exception):
|
class XAPRequest(namedtuple('XAPRequest', 'token length data')):
|
||||||
pass
|
fmt = Struct('<HB61s')
|
||||||
|
|
||||||
|
def __new__(cls, *args):
|
||||||
|
return super().__new__(cls, *args)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_bytes(data):
|
||||||
|
return XAPRequest._make(XAPRequest.fmt.unpack(data))
|
||||||
|
|
||||||
|
def to_bytes(self):
|
||||||
|
return self.fmt.pack(*list(self))
|
||||||
|
|
||||||
|
|
||||||
|
class XAPResponse(namedtuple('XAPResponse', 'token flags length data')):
|
||||||
|
fmt = Struct('<HBB60s')
|
||||||
|
|
||||||
|
def __new__(cls, *args):
|
||||||
|
return super().__new__(cls, *args)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_bytes(data):
|
||||||
|
return XAPResponse._make(XAPResponse.fmt.unpack(data))
|
||||||
|
|
||||||
|
def to_bytes(self):
|
||||||
|
return self.fmt.pack(*list(self))
|
||||||
|
|
||||||
|
|
||||||
class XAPSecureStatus(IntEnum):
|
class XAPSecureStatus(IntEnum):
|
||||||
|
@ -7,20 +7,13 @@ import gzip
|
|||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
import functools
|
import functools
|
||||||
from struct import Struct, pack, unpack
|
from struct import pack, unpack
|
||||||
from collections import namedtuple
|
|
||||||
from platform import platform
|
from platform import platform
|
||||||
|
|
||||||
from .types import XAPSecureStatus, XAPFlags, XAPRouteError
|
from .types import XAPSecureStatus, XAPFlags, XAPRequest, XAPResponse
|
||||||
from .routes import XAPRoutes
|
from .routes import XAPRoutes, XAPRouteError
|
||||||
from .util import u32toBCD
|
from .util import u32toBCD
|
||||||
|
|
||||||
RequestPacket = namedtuple('RequestPacket', 'token length data')
|
|
||||||
RequestStruct = Struct('<HB61s')
|
|
||||||
|
|
||||||
ResponsePacket = namedtuple('ResponsePacket', 'token flags length data')
|
|
||||||
ResponseStruct = Struct('<HBB60s')
|
|
||||||
|
|
||||||
|
|
||||||
def _gen_token():
|
def _gen_token():
|
||||||
"""Generate XAP token - cannot start with 00xx or 'reserved' (FFFE|FFFF)
|
"""Generate XAP token - cannot start with 00xx or 'reserved' (FFFE|FFFF)
|
||||||
@ -58,12 +51,12 @@ class XAPDevice:
|
|||||||
"""Background thread to signal waiting transactions
|
"""Background thread to signal waiting transactions
|
||||||
"""
|
"""
|
||||||
while self.do_read:
|
while self.do_read:
|
||||||
array_alpha = self.dev.read(ResponseStruct.size, 100)
|
data = self.dev.read(XAPResponse.fmt.size, 100)
|
||||||
if array_alpha:
|
if data:
|
||||||
token = int.from_bytes(array_alpha[:2], 'little')
|
r = XAPResponse.from_bytes(data)
|
||||||
event = self.responses.get(token)
|
event = self.responses.get(r.token)
|
||||||
if event:
|
if event:
|
||||||
event._ret = array_alpha
|
event._ret = data
|
||||||
event.set()
|
event.set()
|
||||||
|
|
||||||
def _query_device_info(self):
|
def _query_device_info(self):
|
||||||
@ -90,7 +83,7 @@ class XAPDevice:
|
|||||||
while not hasattr(event, '_ret'):
|
while not hasattr(event, '_ret'):
|
||||||
event.wait(timeout=0.25)
|
event.wait(timeout=0.25)
|
||||||
|
|
||||||
r = ResponsePacket._make(ResponseStruct.unpack(event._ret))
|
r = XAPResponse.from_bytes(event._ret)
|
||||||
return (r.flags, r.data[:r.length])
|
return (r.flags, r.data[:r.length])
|
||||||
|
|
||||||
def _transaction(self, *args):
|
def _transaction(self, *args):
|
||||||
@ -106,8 +99,7 @@ class XAPDevice:
|
|||||||
|
|
||||||
token = _gen_token()
|
token = _gen_token()
|
||||||
|
|
||||||
p = RequestPacket(token, len(data), data)
|
buffer = XAPRequest(token, len(data), data).to_bytes()
|
||||||
buffer = RequestStruct.pack(*list(p))
|
|
||||||
|
|
||||||
event = threading.Event()
|
event = threading.Event()
|
||||||
self.responses[token] = event
|
self.responses[token] = event
|
||||||
@ -122,7 +114,7 @@ class XAPDevice:
|
|||||||
if not hasattr(event, '_ret'):
|
if not hasattr(event, '_ret'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
r = ResponsePacket._make(ResponseStruct.unpack(event._ret))
|
r = XAPResponse.from_bytes(event._ret)
|
||||||
if r.flags & XAPFlags.SUCCESS == 0:
|
if r.flags & XAPFlags.SUCCESS == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -27,6 +27,10 @@
|
|||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
class XAPRouteError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class XAPRoutes():
|
class XAPRoutes():
|
||||||
# XAP
|
# XAP
|
||||||
XAP_VERSION_QUERY = b'\x00\x00'
|
XAP_VERSION_QUERY = b'\x00\x00'
|
||||||
|
@ -26,11 +26,37 @@
|
|||||||
#
|
#
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
from enum import IntFlag, IntEnum
|
from enum import IntFlag, IntEnum
|
||||||
|
from struct import Struct
|
||||||
|
|
||||||
|
|
||||||
class XAPRouteError(Exception):
|
class XAPRequest(namedtuple('XAPRequest', 'token length data')):
|
||||||
pass
|
fmt = Struct('<HB61s')
|
||||||
|
|
||||||
|
def __new__(cls, *args):
|
||||||
|
return super().__new__(cls, *args)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_bytes(data):
|
||||||
|
return XAPRequest._make(XAPRequest.fmt.unpack(data))
|
||||||
|
|
||||||
|
def to_bytes(self):
|
||||||
|
return self.fmt.pack(*list(self))
|
||||||
|
|
||||||
|
|
||||||
|
class XAPResponse(namedtuple('XAPResponse', 'token flags length data')):
|
||||||
|
fmt = Struct('<HBB60s')
|
||||||
|
|
||||||
|
def __new__(cls, *args):
|
||||||
|
return super().__new__(cls, *args)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_bytes(data):
|
||||||
|
return XAPResponse._make(XAPResponse.fmt.unpack(data))
|
||||||
|
|
||||||
|
def to_bytes(self):
|
||||||
|
return self.fmt.pack(*list(self))
|
||||||
|
|
||||||
|
|
||||||
class XAPSecureStatus(IntEnum):
|
class XAPSecureStatus(IntEnum):
|
||||||
|
Loading…
Reference in New Issue
Block a user