Skip to content

Commit b076077

Browse files
committed
use switch for ctrl_transfer result check
This directly translates the Device.ctrl_transfer() result check logic from its old if-statements to an equivalent switch-statement. The point is to make it clear how each possible result code is handled. Note that XFER_RESULT_FAILED and XFER_RESULT_TIMEOUT both return 0 without generating any exception. (but also, tinyusb may not actually use XFER_RESULT_TIMOUT if its comments are still accurate)
1 parent 05db6ee commit b076077

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

shared-module/usb/core/Device.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,21 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,
375375
}
376376
xfer_result_t result = _xfer_result;
377377
_xfer_result = XFER_RESULT_INVALID;
378-
if (result == XFER_RESULT_STALLED) {
379-
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Pipe error"));
380-
}
381-
if (result == XFER_RESULT_INVALID) {
382-
tuh_edpt_abort_xfer(xfer.daddr, xfer.ep_addr);
383-
mp_raise_usb_core_USBTimeoutError();
384-
}
385-
if (result == XFER_RESULT_SUCCESS) {
386-
return len;
378+
switch (result) {
379+
case XFER_RESULT_SUCCESS:
380+
return len;
381+
case XFER_RESULT_FAILED:
382+
break;
383+
case XFER_RESULT_STALLED:
384+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Pipe error"));
385+
break;
386+
case XFER_RESULT_TIMEOUT:
387+
break;
388+
case XFER_RESULT_INVALID:
389+
tuh_edpt_abort_xfer(xfer.daddr, xfer.ep_addr);
390+
mp_raise_usb_core_USBTimeoutError();
391+
break;
387392
}
388-
389393
return 0;
390394
}
391395

0 commit comments

Comments
 (0)