-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HID classes #47
base: main
Are you sure you want to change the base?
Add HID classes #47
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import unittest | ||
|
||
from contextlib import contextmanager | ||
|
||
from .. import emitter_for_format | ||
from ..descriptor import ComplexDescriptorEmitter | ||
from ...types.descriptors.hid import * | ||
|
||
class HIDDescriptorEmitter(ComplexDescriptorEmitter): | ||
|
||
DESCRIPTOR_FORMAT = HIDDescriptor | ||
|
||
@contextmanager | ||
def ReportDescriptor(self): | ||
""" Context manager that allows addition of a subordinate report descriptor. | ||
|
||
It can be used with a `with` statement; and yields an HIDReportDescriptorEmitter | ||
that can be populated: | ||
|
||
with hiddescriptor.ReportDescriptor() as r: | ||
r.wDescriptorLength = 0x10 | ||
|
||
This adds the relevant descriptor, automatically. | ||
""" | ||
|
||
descriptor = HIDReportDescriptorEmitter() | ||
yield descriptor | ||
|
||
self.add_subordinate_descriptor(descriptor) | ||
|
||
def _pre_emit(self): | ||
# Figure out the total length of our descriptor, including subordinates. | ||
subordinate_length = sum(len(sub) for sub in self._subordinates) | ||
self.bLength = subordinate_length + self.DESCRIPTOR_FORMAT.sizeof() | ||
self.bNumDescriptors = len(self._subordinates) | ||
pass | ||
|
||
|
||
HIDReportDescriptorEmitter = emitter_for_format(HIDReportDescriptor) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# This file is part of usb-protocol. | ||
# | ||
""" Structures describing Human Interface Device Class descriptors. """ | ||
|
||
import unittest | ||
from enum import IntEnum | ||
|
||
from ..descriptor import \ | ||
DescriptorField, DescriptorNumber, DescriptorFormat | ||
|
||
class HidInterfaceClassCodes(IntEnum): | ||
HID = 0x03 | ||
|
||
class HidInterfaceSubclassCodes(IntEnum): | ||
NO_SUBCLASS = 0 | ||
BOOT = 1 | ||
|
||
class HidInterfaceProtocols(IntEnum): | ||
NONE = 0 | ||
KEYBOARD = 1 | ||
MOUSE = 2 | ||
|
||
class HidClassSpecificDescriptorTypes(IntEnum): | ||
CS_UNDEFINED = 0x20 | ||
CS_HID = 0x21 | ||
CS_REPORT = 0x22 | ||
CS_PHYSICAL = 0x23 | ||
|
||
|
||
HIDDescriptor = DescriptorFormat( | ||
"bLength" / DescriptorField("Descriptor Length"), | ||
"bDescriptorType" / DescriptorNumber(HidClassSpecificDescriptorTypes.CS_HID), | ||
"bcdHID" / DescriptorField("HID Protocol Version", default=1.11), | ||
"bCountryCode" / DescriptorField("Hardware target country", default=0), | ||
"bNumDescriptors" / DescriptorField("Number of HID class descriptors to follow", default=0), | ||
) | ||
|
||
# This is not reallyy a stand-alone descriptor, but it is part of the HIDDescriptor above. | ||
# That descriptor can contain multiple ReportDescriptors. To support this, a seperate | ||
# descriptor format is used. | ||
HIDReportDescriptor = DescriptorFormat( | ||
"bDescriptorType" / DescriptorField("HID Descriptor Type", default=HidClassSpecificDescriptorTypes.CS_REPORT), | ||
"wDescriptorLength" / DescriptorField("HID Descriptor Length") | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type should be called something other than
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, I think
ReportDescriptor
should be changed toDescriptorReference
throughout these lines.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, applied it