test_usb_mock_classes.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 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. void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw, bool is_read, int offset, int num_sectors, uint32_t tag)
  14. {
  15. cbw->dCBWSignature = 0x43425355; //Fixed value
  16. cbw->dCBWTag = tag; //Random value that is echoed back
  17. cbw->dCBWDataTransferLength = num_sectors * MOCK_MSC_SCSI_SECTOR_SIZE;
  18. cbw->bmCBWFlags = (is_read) ? (1 << 7) : 0; //If this is a read, set the direction flag
  19. cbw->bCBWLUN = MOCK_MSC_SCSI_LUN;
  20. cbw->bCBWCBLength = 10; //The length of the SCSI command
  21. //Initialize SCSI CMD as READ10 or WRITE 10
  22. cbw->CBWCB.opcode = (is_read) ? 0x28 : 0x2A; //SCSI CMD READ10 or WRITE10
  23. cbw->CBWCB.flags = 0;
  24. cbw->CBWCB.lba_3 = (offset >> 24);
  25. cbw->CBWCB.lba_2 = (offset >> 16);
  26. cbw->CBWCB.lba_1 = (offset >> 8);
  27. cbw->CBWCB.lba_0 = (offset >> 0);
  28. cbw->CBWCB.group = 0;
  29. cbw->CBWCB.len_1 = (num_sectors >> 8);
  30. cbw->CBWCB.len_0 = (num_sectors >> 0);
  31. cbw->CBWCB.control = 0;
  32. }
  33. bool mock_msc_scsi_check_csw(mock_msc_bulk_csw_t *csw, uint32_t tag_expect)
  34. {
  35. bool no_issues = true;
  36. if (csw->dCSWSignature != 0x53425355) {
  37. no_issues = false;
  38. printf("Warning: csw signature corrupt (0x%X)\n", csw->dCSWSignature);
  39. }
  40. if (csw->dCSWTag != tag_expect) {
  41. no_issues = false;
  42. printf("Warning: csw tag unexpected! Expected %d got %d\n", tag_expect, csw->dCSWTag);
  43. }
  44. if (csw->dCSWDataResidue) {
  45. no_issues = false;
  46. printf("Warning: csw indicates data residue of %d bytes!\n", csw->dCSWDataResidue);
  47. }
  48. if (csw->bCSWStatus) {
  49. no_issues = false;
  50. printf("Warning: csw indicates non-good status %d!\n", csw->bCSWStatus);
  51. }
  52. return no_issues;
  53. }
  54. // ---------------------------------------------------- HID Mouse ------------------------------------------------------
  55. void mock_hid_process_report(mock_hid_mouse_report_t *report, int iter)
  56. {
  57. static int x_pos = 0;
  58. static int y_pos = 0;
  59. //Update X position
  60. if (report->x_movement & 0x80) { //Positive movement
  61. x_pos += report->x_movement & 0x7F;
  62. } else { //Negative movement
  63. x_pos -= report->x_movement & 0x7F;
  64. }
  65. //Update Y position
  66. if (report->y_movement & 0x80) { //Positive movement
  67. y_pos += report->y_movement & 0x7F;
  68. } else { //Negative movement
  69. y_pos -= report->y_movement & 0x7F;
  70. }
  71. printf("\rX:%d\tY:%d\tIter: %d\n", x_pos, y_pos, iter);
  72. }