Skip to content

Commit

Permalink
core: refactor facedancer core to use 'control_send' for control endp…
Browse files Browse the repository at this point in the history
…oint transfers.
  • Loading branch information
antoinevg committed Aug 28, 2024
1 parent 75fe0c3 commit 50cdd94
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
23 changes: 17 additions & 6 deletions facedancer/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ def clear_halt(self, endpoint_number: int, direction: USBDirection):
self.backend.clear_halt(endpoint_number, direction)


def control_send(self, endpoint_number: int, in_request: USBControlRequest, data: bytes, *, blocking: bool = False):
""" Queues sending data on the provided control endpoint in
response to a IN control request.
Args:
endpoint_number : The endpoint number to send data upon.
in_request : The control request being responded to.
data : The data to send.
blocking : If provided and true, this function will block
until the backend indicates the send is complete.
"""
self.backend.send_on_control_endpoint(endpoint_number, in_request, data, blocking=blocking)


def send(self, endpoint_number: int, data: bytes, *, blocking: bool = False):
""" Queues sending data on the IN endpoint with the provided number.
Expand All @@ -267,13 +282,9 @@ def send(self, endpoint_number: int, data: bytes, *, blocking: bool = False):
until the backend indicates the send is complete.
"""

# EP0 is special, as it conceptually belongs to the whole device, as it's used
# for control requests and configuration. We'll handle it directly, here.
#
# All of our backends currently automatically handle packetization and ZLPs for
# the control endpoint, so we'll skip packetizing it (which would generate spurious ZLPs).
if endpoint_number == 0:
self.backend.send_on_endpoint(0, data, blocking=blocking)
# TODO upgrade to an exception with the release of Facedancer 3.1.0
log.warning("Please use USBDevice::control_send() for control transfers")
elif self.configuration:
endpoint = self.configuration.get_endpoint(endpoint_number, USBDirection.IN)
endpoint.send(data, blocking=blocking)
Expand Down
2 changes: 1 addition & 1 deletion facedancer/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_device(self):
return self.parent.get_device()


def send(self, data: bytes, *, blocking: bool =False):
def send(self, data: bytes, *, blocking: bool = False):
""" Sends data on this endpoint. Valid only for IN endpoints.
Args:
Expand Down
2 changes: 1 addition & 1 deletion facedancer/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _proxy_in_control_request(self, request: USBControlRequest):
self.backend.stall_endpoint(0, USBDirection.IN)
else:
# TODO: support control endpoints other than 0
self.send(0, data)
self.control_send(0, request, data)


def _proxy_out_control_request(self, request: USBControlRequest):
Expand Down
4 changes: 2 additions & 2 deletions facedancer/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def from_raw_bytes(cls, raw_bytes: bytes, *, device = None):

def reply(self, data: bytes):
""" Replies to the given request with a given set of bytes. """
self.device.send(endpoint_number=0, data=data)
self.device.control_send(endpoint_number=0, in_request=self, data=data)


def acknowledge(self, *, blocking: bool = False):
Expand All @@ -260,7 +260,7 @@ def acknowledge(self, *, blocking: bool = False):
Args:
blocking : If true, the relevant control request will complete before returning.
"""
self.device.send(endpoint_number=0, data=b"", blocking=blocking)
self.device.control_send(endpoint_number=0, in_request=self, data=b"", blocking=blocking)


def ack(self, *, blocking: bool = False):
Expand Down

0 comments on commit 50cdd94

Please sign in to comment.