-
Notifications
You must be signed in to change notification settings - Fork 36
Add HID classes #47
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
Open
electretmike
wants to merge
7
commits into
greatscottgadgets:main
Choose a base branch
from
electretmike:hid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−0
Open
Add HID classes #47
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2ccdd4
Add HID classes
2bc1a5d
Add HID class codes
72ac292
Rename ReportDescriptor to DescriptorReference
6c5da66
Correct some wording
martinling 5e22c61
Correct some wording
martinling 398771f
Correct some wording
martinling df62547
Update usb_protocol/types/descriptors/hid.py
martinling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| 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 DescriptorReference(self): | ||
| """ Context manager that allows addition of a descriptor reference. | ||
|
|
||
| It can be used with a `with` statement; and yields an HIDDescriptorReferenceEmitter | ||
| that can be populated: | ||
|
|
||
| with hiddescriptor.DescriptorReference() as r: | ||
| r.bDescriptorType = 0x22 | ||
| r.wDescriptorLength = 0x10 | ||
| r.wDescriptorLength = 0x10 | ||
|
|
||
| This adds the relevant descriptor reference, automatically. | ||
| """ | ||
|
|
||
| descriptor = HIDDescriptorReferenceEmitter() | ||
| 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 | ||
|
|
||
|
|
||
| HIDDescriptorReferenceEmitter = emitter_for_format(HIDDescriptorReference) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # | ||
| # 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 really a stand-alone descriptor, but rather a reference to a descriptor | ||
| # that can be retrieved seperately. It is part of the HIDDescriptor above. The HID | ||
| # descriptor can contain multiple descriptor references. To support this, a separate | ||
| # descriptor format is used. | ||
| HIDDescriptorReference = DescriptorFormat( | ||
| "bDescriptorType" / DescriptorField("HID Descriptor Type", default=HidClassSpecificDescriptorTypes.CS_REPORT), | ||
| "wDescriptorLength" / DescriptorField("HID Descriptor Length") | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.