diff --git a/drivers/include/usbdev_mock.h b/drivers/include/usbdev_mock.h index 324dea8c83..d502a02a4d 100644 --- a/drivers/include/usbdev_mock.h +++ b/drivers/include/usbdev_mock.h @@ -40,10 +40,11 @@ typedef enum { * @brief usbdev mock device endpoint */ typedef struct { - usbdev_ep_t ep; /**< Generic endpoint struct */ - usbdev_mock_ep_state_t state; /**< Endpoint state */ - size_t available; /**< Bytes available in the buffer */ - uint8_t *buf_start; /**< Start location of the buffer */ + usbdev_ep_t ep; /**< Generic endpoint struct */ + usbdev_mock_ep_state_t state; /**< Endpoint state */ + size_t available; /**< Bytes available in the buffer */ + uint8_t *target_buf; /**< Buffer as passed by @ref usbdev_ep_xmit */ + uint8_t *buf; /**< Internal mock buffer pointer */ } usbdev_mock_ep_t; /** diff --git a/drivers/usbdev_mock/usbdev_mock.c b/drivers/usbdev_mock/usbdev_mock.c index a3c9a9d3bf..944cef9381 100644 --- a/drivers/usbdev_mock/usbdev_mock.c +++ b/drivers/usbdev_mock/usbdev_mock.c @@ -75,16 +75,15 @@ usbdev_ep_t *_new_ep(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, res = &testdev->out[0]; res->ep.num = 0; - res->ep.buf = _out_buf; + res->buf = _out_buf; } else { res = &testdev->in[0]; res->ep.num = 0; - res->ep.buf = _in_buf; + res->buf = _in_buf; } } if (res) { - res->buf_start = res->ep.buf; res->state = EP_STATE_READY; res->available = 0; res->ep.len = buf_len; @@ -190,26 +189,30 @@ void _ep_esr(usbdev_ep_t *ep) ep->num, ep->dir == USB_EP_DIR_OUT ? "out" : "in"); usbdev_mock_ep_t *mock_ep = (usbdev_mock_ep_t *)ep; + if (mock_ep->ep.dir == USB_EP_DIR_OUT) { + memcpy(mock_ep->target_buf, mock_ep->buf, mock_ep->available); + } if (mock_ep->state == EP_STATE_DATA_AVAILABLE) { dev->ep_esr_cb(dev, mock_ep); mock_ep->state = EP_STATE_READY; } } -int _ready(usbdev_ep_t *ep, size_t len) +int _xmit(usbdev_ep_t *ep, uint8_t *buf, size_t len) { DEBUG("[mock]: Readying EP %u, dir %s, len %u\n", ep->num, ep->dir == USB_EP_DIR_OUT ? "out" : "in", (unsigned)len); if (ep->num == 0) { usbdev_mock_t *usbdev_mock = _ep2dev(ep); usbdev_mock_ep_t *mock_ep = (usbdev_mock_ep_t *)ep; - - mock_ep->available = len; - mock_ep->ep.buf += len; + mock_ep->target_buf = buf; + if (ep->dir == USB_EP_DIR_IN) { + memcpy(mock_ep->buf + mock_ep->available, mock_ep->target_buf, len); + mock_ep->available = len; + } mock_ep->state = EP_STATE_DATA_AVAILABLE; usbdev_mock->ready_cb(usbdev_mock, (usbdev_mock_ep_t *)ep, len); - } return 0; } @@ -224,5 +227,5 @@ static const usbdev_driver_t testdriver = { .ep_get = _ep_get, .ep_set = _ep_set, .ep_esr = _ep_esr, - .ready = _ready, + .xmit = _xmit, }; diff --git a/tests/usbus/main.c b/tests/usbus/main.c index a2ba8b4f8f..3ca6bf32dc 100644 --- a/tests/usbus/main.c +++ b/tests/usbus/main.c @@ -146,7 +146,7 @@ static void _test_sequence(usbdev_mock_t *dev) case TESTPHASE_RESET: next_phase = TESTPHASE_REQ_DEV_INIT; DEBUG("[test]: Requesting device descriptor\n"); - _build_conf_req(dev->out[0].ep.buf, 8); /* initial config request */ + _build_conf_req(dev->out[0].buf, 8); /* initial config request */ dev->req_len = 8; dev->out[0].state = EP_STATE_DATA_AVAILABLE; dev->out[0].available = 8; @@ -157,7 +157,7 @@ static void _test_sequence(usbdev_mock_t *dev) DEBUG("[test]: validating device descriptor\n"); TEST_ASSERT_EQUAL_INT(dev->in[0].available, 8); _validate_device_desc_init( - (usb_descriptor_device_t *)dev->in[0].buf_start); + (usb_descriptor_device_t *)dev->in[0].buf); /* Reset device */ DEBUG("[test]: Signalling second USB reset condition\n"); dev->usbdev.cb(&dev->usbdev, USBDEV_EVENT_RESET); @@ -166,7 +166,7 @@ static void _test_sequence(usbdev_mock_t *dev) case TESTPHASE_RESET2: next_phase = TESTPHASE_SET_ADDRESS; DEBUG("[test]: Set USB address\n"); - _build_set_addr(dev->out[0].ep.buf); + _build_set_addr(dev->out[0].buf); dev->req_len = 0; dev->out[0].state = EP_STATE_DATA_AVAILABLE; dev->out[0].available = 8; @@ -175,7 +175,7 @@ static void _test_sequence(usbdev_mock_t *dev) case TESTPHASE_SET_ADDRESS: next_phase = TESTPHASE_REQ_DEV_FULL; DEBUG("[test]: Requesting full device descriptor\n"); - _build_conf_req(dev->out[0].ep.buf, + _build_conf_req(dev->out[0].buf, sizeof(usb_descriptor_device_t)); dev->req_len = sizeof(usb_descriptor_device_t); dev->out[0].state = EP_STATE_DATA_AVAILABLE; @@ -187,7 +187,7 @@ static void _test_sequence(usbdev_mock_t *dev) sizeof(usb_descriptor_device_t)); DEBUG("[test]: Validating full descriptor\n"); _validate_device_desc_init( - (usb_descriptor_device_t *)dev->in[0].buf_start); + (usb_descriptor_device_t *)dev->in[0].buf); next_phase = TESTPHASE_FINAL; dev->usbdev.cb(&dev->usbdev, USBDEV_EVENT_ESR); @@ -261,8 +261,6 @@ static void _handle_data(usbdev_mock_t *dev, usbdev_mock_ep_t *ep, size_t len) if (ep->available == dev->req_len) { DEBUG("[data]: Full data received from stack\n"); req_phase = TEST_REQ_PHASE_OUTACK; - /* Reset buffer ptr to the start */ - ep->ep.buf = ep->buf_start; } else { DEBUG("[data]: Expecting more data from stack: %u/%u\n",