Skip to content

Commit

Permalink
backend: add a 'clear_halt' method for non-control endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinevg committed Aug 26, 2024
1 parent e0e491a commit 47912e6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
12 changes: 12 additions & 0 deletions facedancer/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ def stall_endpoint(self, endpoint_number:int, direction: USBDirection=USBDirecti
raise NotImplementedError


def clear_halt(self, endpoint_number:int, direction: USBDirection):
""" Clears a halt condition on the provided non-control endpoint.
Args:
endpoint_number : The endpoint number
direction : The endpoint direction; or OUT if not provided.
"""
# FIXME do nothing as only the moondancer backend supports this for now
# raise NotImplementedError
pass


def service_irqs(self):
"""
Core routine of the Facedancer execution/event loop. Continuously monitors the
Expand Down
15 changes: 15 additions & 0 deletions facedancer/backends/moondancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,21 @@ def stall_endpoint(self, endpoint_number:int, direction: USBDirection=USBDirecti
log.debug(f" moondancer.api.stall_endpoint_out({endpoint_number})")


def clear_halt(self, endpoint_number: int, direction: USBDirection):
""" Clears a halt condition on the provided non-control endpoint.
Args:
endpoint_number : The endpoint number
direction : The endpoint direction; or OUT if not provided.
"""

endpoint_address = (endpoint_number | 0x80) if direction else endpoint_number
log.debug(f"Clearing halt EP{endpoint_number} {USBDirection(direction).name} (0x{endpoint_address:x})")

self.api.clear_feature_endpoint_halt(endpoint_number, direction)
log.debug(f" moondancer.api.clear_feature_endpoint_halt({endpoint_number}, {direction})")


def service_irqs(self):
"""
Core routine of the Facedancer execution/event loop. Continuously monitors the
Expand Down
10 changes: 9 additions & 1 deletion facedancer/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,15 @@ def stall(self, *, endpoint_number: int = 0, direction: USBDirection = USBDirect
self.backend.stall_endpoint(endpoint_number, direction)


# TODO: add a clear_stall() method here for non-control endpoints
def clear_halt(self, endpoint_number: int, direction: USBDirection):
""" Clears a halt condition on the provided non-control endpoint.
Args:
endpoint_number : The endpoint number
direction : The endpoint direction; or OUT if not provided.
"""
self.backend.clear_halt(endpoint_number, direction)


def send(self, endpoint_number: int, data: bytes, *, blocking: bool = False):
""" Queues sending data on the IN endpoint with the provided number.
Expand Down
13 changes: 9 additions & 4 deletions facedancer/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,16 @@ def _proxy_in_transfer(self, endpoint):
if ep_num is None:
return

# Read the target data from the target device.
endpoint_address = ep_num | 0x80

# Quick hack to improve responsiveness on interrupt endpoints.
try:
# Quick hack to improve responsiveness on interrupt endpoints.
if endpoint.interval:
data = self.proxied_device.read(ep_num, endpoint.max_packet_size, timeout=endpoint.interval)
else:
data = self.proxied_device.read(ep_num, endpoint.max_packet_size)

except usb1.USBErrorPipe:
self.proxied_device.clear_halt(ep_num, USBDirection.IN)
return
except USBErrorTimeout:
return

Expand Down Expand Up @@ -448,6 +448,11 @@ def write(cls, endpoint_number, data, timeout=1000):
return cls.device_handle.bulkWrite(endpoint_number, data, timeout)


@classmethod
def clear_halt(cls, endpoint_number, direction):
endpoint_address = direction.to_endpoint_address(endpoint_number)
return cls.device_handle.clearHalt(endpoint_address)


if __name__ == "__main__":
from . import FacedancerUSBApp
Expand Down

0 comments on commit 47912e6

Please sign in to comment.