test_usb_mock_classes.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include "usb/usb_types_ch9.h"
  10. #include "test_usb_mock_classes.h"
  11. // ---------------------------------------------------- MSC SCSI -------------------------------------------------------
  12. const char *MSC_CLIENT_TAG = "MSC Client";
  13. const uint8_t mock_msc_scsi_dev_desc[] = {
  14. 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x5F, 0x12, 0x8A, 0xC0, 0x00, 0x01, 0x01, 0x02, 0x03, 0x01,
  15. };
  16. const uint8_t mock_msc_scsi_config_desc[] = {
  17. 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xF0, 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07,
  18. 0x05, 0x01, 0x02, 0x40, 0x00, 0x01, 0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x01,
  19. };
  20. const uint8_t mock_msc_scsi_str_desc_manu[] = {
  21. 0x0c, 0x03, 0x41, 0x00, 0x44, 0x00, 0x41, 0x00, 0x54, 0x00, 0x41, 0x00,
  22. };
  23. const uint8_t mock_msc_scsi_str_desc_prod[] = {
  24. 0x2c, 0x03, 0x41, 0x00, 0x44, 0x00, 0x41, 0x00, 0x54, 0x00, 0x41, 0x00, 0x20, 0x00, 0x55, 0x00, 0x53, 0x00, 0x42,
  25. 0x00, 0x20, 0x00, 0x46, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x73, 0x00, 0x68, 0x00, 0x20, 0x00, 0x44, 0x00, 0x72, 0x00,
  26. 0x69, 0x00, 0x76, 0x00, 0x65, 0x00,
  27. };
  28. const uint8_t mock_msc_scsi_str_desc_ser_num[] = {
  29. 0x22, 0x03, 0x31, 0x00, 0x33, 0x00, 0x43, 0x00, 0x32, 0x00, 0x38, 0x00, 0x31, 0x00, 0x36, 0x00, 0x35, 0x00, 0x38,
  30. 0x00, 0x32, 0x00, 0x31, 0x00, 0x38, 0x00, 0x30, 0x00, 0x30, 0x00, 0x38, 0x00, 0x45, 0x00,
  31. };
  32. const usb_ep_desc_t mock_msc_scsi_bulk_out_ep_desc = {
  33. .bLength = sizeof(usb_ep_desc_t),
  34. .bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
  35. .bEndpointAddress = 0x01, //EP 1 OUT
  36. .bmAttributes = USB_BM_ATTRIBUTES_XFER_BULK,
  37. .wMaxPacketSize = 64, //MPS of 64 bytes
  38. .bInterval = 1,
  39. };
  40. const usb_ep_desc_t mock_msc_scsi_bulk_in_ep_desc = {
  41. .bLength = sizeof(usb_ep_desc_t),
  42. .bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
  43. .bEndpointAddress = 0x82, //EP 2 IN
  44. .bmAttributes = USB_BM_ATTRIBUTES_XFER_BULK,
  45. .wMaxPacketSize = 64, //MPS of 64 bytes
  46. .bInterval = 1,
  47. };
  48. void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw, bool is_read, int offset, int num_sectors, uint32_t tag)
  49. {
  50. cbw->dCBWSignature = 0x43425355; //Fixed value
  51. cbw->dCBWTag = tag; //Random value that is echoed back
  52. cbw->dCBWDataTransferLength = num_sectors * MOCK_MSC_SCSI_SECTOR_SIZE;
  53. cbw->bmCBWFlags = (is_read) ? (1 << 7) : 0; //If this is a read, set the direction flag
  54. cbw->bCBWLUN = MOCK_MSC_SCSI_LUN;
  55. cbw->bCBWCBLength = 10; //The length of the SCSI command
  56. //Initialize SCSI CMD as READ10 or WRITE 10
  57. cbw->CBWCB.opcode = (is_read) ? 0x28 : 0x2A; //SCSI CMD READ10 or WRITE10
  58. cbw->CBWCB.flags = 0;
  59. cbw->CBWCB.lba_3 = (offset >> 24);
  60. cbw->CBWCB.lba_2 = (offset >> 16);
  61. cbw->CBWCB.lba_1 = (offset >> 8);
  62. cbw->CBWCB.lba_0 = (offset >> 0);
  63. cbw->CBWCB.group = 0;
  64. cbw->CBWCB.len_1 = (num_sectors >> 8);
  65. cbw->CBWCB.len_0 = (num_sectors >> 0);
  66. cbw->CBWCB.control = 0;
  67. }
  68. bool mock_msc_scsi_check_csw(mock_msc_bulk_csw_t *csw, uint32_t tag_expect)
  69. {
  70. bool no_issues = true;
  71. if (csw->dCSWSignature != 0x53425355) {
  72. no_issues = false;
  73. printf("Warning: csw signature corrupt (0x%X)\n", csw->dCSWSignature);
  74. }
  75. if (csw->dCSWTag != tag_expect) {
  76. no_issues = false;
  77. printf("Warning: csw tag unexpected! Expected %d got %d\n", tag_expect, csw->dCSWTag);
  78. }
  79. if (csw->dCSWDataResidue) {
  80. no_issues = false;
  81. printf("Warning: csw indicates data residue of %d bytes!\n", csw->dCSWDataResidue);
  82. }
  83. if (csw->bCSWStatus) {
  84. no_issues = false;
  85. printf("Warning: csw indicates non-good status %d!\n", csw->bCSWStatus);
  86. }
  87. return no_issues;
  88. }
  89. // ---------------------------------------------------- HID Mouse ------------------------------------------------------
  90. const usb_ep_desc_t mock_hid_mouse_in_ep_desc = {
  91. .bLength = sizeof(usb_ep_desc_t),
  92. .bDescriptorType = USB_B_DESCRIPTOR_TYPE_ENDPOINT,
  93. .bEndpointAddress = 0x81, //EP 1 IN
  94. .bmAttributes = USB_BM_ATTRIBUTES_XFER_INT,
  95. .wMaxPacketSize = 4, //MPS of 4 bytes
  96. .bInterval = 10, //Interval of 10ms
  97. };
  98. void mock_hid_process_report(mock_hid_mouse_report_t *report, int iter)
  99. {
  100. static int x_pos = 0;
  101. static int y_pos = 0;
  102. //Update X position
  103. if (report->x_movement & 0x80) { //Positive movement
  104. x_pos += report->x_movement & 0x7F;
  105. } else { //Negative movement
  106. x_pos -= report->x_movement & 0x7F;
  107. }
  108. //Update Y position
  109. if (report->y_movement & 0x80) { //Positive movement
  110. y_pos += report->y_movement & 0x7F;
  111. } else { //Negative movement
  112. y_pos -= report->y_movement & 0x7F;
  113. }
  114. printf("\rX:%d\tY:%d\tIter: %d\n", x_pos, y_pos, iter);
  115. }