-
Notifications
You must be signed in to change notification settings - Fork 122
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
New mouse peripheral and tests #94
Draft
kauwua
wants to merge
2
commits into
greatscottgadgets:main
Choose a base branch
from
HydraDancer:hydradancer_peripherals
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.
+1,282
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,24 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# This file is part of FaceDancer. | ||
# | ||
""" USB mouse device example, makes the mouse go crazy on screen """ | ||
|
||
import asyncio | ||
|
||
from facedancer import main | ||
from facedancer.devices.mouse import USBMouseDevice | ||
|
||
device = USBMouseDevice() | ||
|
||
|
||
async def crazy_mouse(): | ||
"""Makes the mouse oscillate""" | ||
while 1: | ||
device.set_x(-10) | ||
await asyncio.sleep(0.1) | ||
device.set_x(10) | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
main(device, crazy_mouse()) |
This file contains 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,168 @@ | ||
""" | ||
Create a basic mouse device with three buttons and two axis | ||
""" | ||
|
||
from . import default_main | ||
from .. import * | ||
from ..classes.hid.descriptor import * | ||
from ..classes.hid.usage import * | ||
|
||
|
||
@use_inner_classes_automatically | ||
class USBMouseDevice(USBDevice): | ||
"""Simple USB mouse device.""" | ||
|
||
name: str = "USB Mouse Device" | ||
product_string: str = "Non-suspicious Mouse" | ||
|
||
# Local mouse state | ||
_x: int = 0 | ||
_y: int = 0 | ||
_wheel: int = 0 | ||
_trigger: bool = False | ||
_secondary: bool = False | ||
_tertiary: bool = False | ||
|
||
class MouseConfiguration(USBConfiguration): | ||
"""Primary configuration : act as a mouse""" | ||
|
||
max_power: int = 100 | ||
self_powered: bool = False | ||
supports_remote_wakeup: bool = True | ||
|
||
class MouseInterface(USBInterface): | ||
"""Core HID interface for our mouse""" | ||
|
||
name: str = "Generic USB mouse interface" | ||
class_number: int = 3 # Human Interface Device class number | ||
|
||
class MouseEventEndpoint(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 1 | ||
direction: USBDirection = USBDirection.IN | ||
transfer_type: USBTransferType = USBTransferType.INTERRUPT | ||
interval: int = 10 | ||
|
||
class MouseHIDDescriptor(USBClassDescriptor): | ||
"""Container for the mouse HID report descriptors""" | ||
|
||
number: int = USBDescriptorTypeNumber.HID | ||
|
||
# raw descriptor fields | ||
bLength: bytes = b"\x09" | ||
bHIDDescriptorType: bytes = b"\x21" # HID descriptor type | ||
bcdHID: bytes = b"\x11\x01" # HID 1.11 | ||
bCountryCode: bytes = b"\x00" | ||
bNumDescriptors: bytes = b"\x01" | ||
bDescriptorType: bytes = b"\x22" # Report descriptor type | ||
wDescriptorLength: bytes = ( | ||
b"\x3e\x00" # 62 -- TODO should be computed automatically | ||
) | ||
|
||
raw: bytes = ( | ||
bLength | ||
+ bHIDDescriptorType | ||
+ bcdHID | ||
+ bCountryCode | ||
+ bNumDescriptors | ||
+ bDescriptorType | ||
+ wDescriptorLength | ||
) | ||
|
||
class MouseReportDescriptor(HIDReportDescriptor): | ||
"""Defines the mouse report descriptor : | ||
* X/Y axis | ||
* three buttons (trigger/primary, secondary, tertiary) | ||
""" | ||
|
||
fields: tuple = ( | ||
USAGE_PAGE(HIDUsagePage.GENERIC_DESKTOP), | ||
USAGE(HIDGenericDesktopUsage.MOUSE), | ||
COLLECTION(HIDCollection.APPLICATION), | ||
USAGE(HIDGenericDesktopUsage.POINTER), | ||
COLLECTION(HIDCollection.PHYSICAL), | ||
USAGE_PAGE(HIDUsagePage.BUTTONS), | ||
USAGE_MINIMUM(0x01), # see HID 1.11 | ||
USAGE_MAXIMUM(0x03), | ||
LOGICAL_MINIMUM(0x0), | ||
LOGICAL_MAXIMUM(0x01), | ||
REPORT_SIZE(1), | ||
REPORT_COUNT(3), | ||
INPUT(variable=True, relative=False), | ||
REPORT_SIZE(5), | ||
REPORT_COUNT(1), | ||
INPUT(variable=True, constant=True), | ||
USAGE_PAGE(HIDUsagePage.GENERIC_DESKTOP), | ||
USAGE(HIDGenericDesktopUsage.X), | ||
USAGE(HIDGenericDesktopUsage.Y), | ||
LOGICAL_MINIMUM(0x81), # -127 | ||
LOGICAL_MAXIMUM(0x7F), # 127 | ||
REPORT_SIZE(8), | ||
REPORT_COUNT(2), | ||
INPUT(variable=True, relative=True), | ||
USAGE(HIDGenericDesktopUsage.WHEEL), | ||
LOGICAL_MINIMUM(0x81), # -127 | ||
LOGICAL_MAXIMUM(0x7F), # 127 | ||
REPORT_SIZE(8), | ||
REPORT_COUNT(1), | ||
INPUT(variable=True, relative=True), | ||
END_COLLECTION(), | ||
END_COLLECTION(), | ||
) | ||
|
||
@class_request_handler(number=USBStandardRequests.GET_INTERFACE) | ||
@to_this_interface | ||
def handle_get_interface_request(self, request): | ||
# Silently stall GET_INTERFACE class requests. | ||
request.stall() | ||
|
||
def set_x(self, x: int): | ||
"""Set X axis translation""" | ||
self._x = x | ||
|
||
def set_y(self, y: int): | ||
"""Set Y axis translation""" | ||
self._y = y | ||
|
||
def set_wheel(self, rotation: int): | ||
"""Set rotation""" | ||
self._wheel = rotation | ||
|
||
def set_trigger(self, down: bool): | ||
"""Set down to True to trigger primary button""" | ||
self._trigger = down | ||
|
||
def set_secondary(self, down: bool): | ||
"""Set down to True to trigger secondary button""" | ||
self._secondary = down | ||
|
||
def set_tertiary(self, down: bool): | ||
"""Set down to True to trigger tertiary button""" | ||
self._tertiary = down | ||
|
||
def _get_buttons_state(self): | ||
"""Create buttons report from current state""" | ||
state = 0x00 | ||
|
||
if self._trigger: | ||
state |= 1 << 0 | ||
if self._secondary: | ||
state |= 1 << 1 | ||
if self._tertiary: | ||
state |= 1 << 2 | ||
|
||
return state | ||
|
||
def handle_data_requested(self, endpoint: USBEndpoint): | ||
"""Provide data once per host request.""" | ||
endpoint.send( | ||
self._get_buttons_state().to_bytes(1, "little") | ||
+ self._x.to_bytes(1, "little", signed=True) | ||
+ self._y.to_bytes(1, "little", signed=True) | ||
+ self._wheel.to_bytes(1, "little", signed=True) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
default_main(USBMouseDevice) |
This file contains 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,104 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# | ||
""" | ||
Create a basic mouse device with three buttons and two axis | ||
""" | ||
|
||
from facedancer.devices import default_main | ||
from facedancer import * | ||
|
||
|
||
@use_inner_classes_automatically | ||
class USBLoopback(USBDevice): | ||
"""Loopback on EP1""" | ||
|
||
name: str = "USB EP1 Loopback" | ||
product_string: str = "Loopback device" | ||
max_packet_size_ep0: int = 64 | ||
device_speed : DeviceSpeed = DeviceSpeed.FULL | ||
|
||
EP_MAX_SIZE = 64 | ||
buffer = [None, None, None, None, None] | ||
|
||
class USBLoopbackConfiguration(USBConfiguration): | ||
"""Primary configuration : act as a mouse""" | ||
|
||
max_power: int = 100 | ||
self_powered: bool = False | ||
supports_remote_wakeup: bool = True | ||
ep_in_ready: bool = False | ||
|
||
class USBLoopbackInterface(USBInterface): | ||
"""Core interface""" | ||
|
||
name: str = "Loopback device" | ||
class_number: int = 0xff # Vendor class | ||
|
||
class USBLoopbackOUT1(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 1 | ||
direction: USBDirection = USBDirection.OUT | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
class USBLoopbackIN1(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 1 | ||
direction: USBDirection = USBDirection.IN | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
class USBLoopbackOUT2(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 2 | ||
direction: USBDirection = USBDirection.OUT | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
class USBLoopbackIN2(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 2 | ||
direction: USBDirection = USBDirection.IN | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
class USBLoopbackOUT3(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 3 | ||
direction: USBDirection = USBDirection.OUT | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
class USBLoopbackIN3(USBEndpoint): | ||
"""Interrupt IN endpoint for guaranteed max latency""" | ||
|
||
number: int = 3 | ||
direction: USBDirection = USBDirection.IN | ||
transfer_type: USBTransferType = USBTransferType.BULK | ||
max_packet_size: int = 64 | ||
|
||
@class_request_handler(number=USBStandardRequests.GET_INTERFACE) | ||
@to_this_interface | ||
def handle_get_interface_request(self, request): | ||
# Silently stall GET_INTERFACE class requests. | ||
request.stall() | ||
|
||
def handle_data_received(self, ep, data): | ||
print(f"received {len(data)} bytes on {ep}") | ||
self.buffer[ep.number] = data | ||
|
||
def handle_data_requested(self, ep): | ||
"""Provide data once per host request.""" | ||
if self.buffer[ep.number] is not None: | ||
self.send(ep.number, self.buffer[ep.number]) | ||
self.buffer[ep.number] = None | ||
|
||
|
||
if __name__ == "__main__": | ||
default_main(USBLoopback) |
Oops, something went wrong.
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.
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.
This comment should probably be changed to fit the device :)