test_usb_mock_classes.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <stdio.h>
  16. #include "usb/usb_types_ch9.h"
  17. #include "test_usb_mock_classes.h"
  18. // ---------------------------------------------------- MSC SCSI -------------------------------------------------------
  19. void mock_msc_scsi_init_cbw(mock_msc_bulk_cbw_t *cbw, bool is_read, int offset, int num_sectors, uint32_t tag)
  20. {
  21. cbw->dCBWSignature = 0x43425355; //Fixed value
  22. cbw->dCBWTag = tag; //Random value that is echoed back
  23. cbw->dCBWDataTransferLength = num_sectors * MOCK_MSC_SCSI_SECTOR_SIZE;
  24. cbw->bmCBWFlags = (is_read) ? (1 << 7) : 0; //If this is a read, set the direction flag
  25. cbw->bCBWLUN = MOCK_MSC_SCSI_LUN;
  26. cbw->bCBWCBLength = 10; //The length of the SCSI command
  27. //Initialize SCSI CMD as READ10 or WRITE 10
  28. cbw->CBWCB.opcode = (is_read) ? 0x28 : 0x2A; //SCSI CMD READ10 or WRITE10
  29. cbw->CBWCB.flags = 0;
  30. cbw->CBWCB.lba_3 = (offset >> 24);
  31. cbw->CBWCB.lba_2 = (offset >> 16);
  32. cbw->CBWCB.lba_1 = (offset >> 8);
  33. cbw->CBWCB.lba_0 = (offset >> 0);
  34. cbw->CBWCB.group = 0;
  35. cbw->CBWCB.len_1 = (num_sectors >> 8);
  36. cbw->CBWCB.len_0 = (num_sectors >> 0);
  37. cbw->CBWCB.control = 0;
  38. }
  39. bool mock_msc_scsi_check_csw(mock_msc_bulk_csw_t *csw, uint32_t tag_expect)
  40. {
  41. bool no_issues = true;
  42. if (csw->dCSWSignature != 0x53425355) {
  43. no_issues = false;
  44. printf("Warning: csw signature corrupt (0x%X)\n", csw->dCSWSignature);
  45. }
  46. if (csw->dCSWTag != tag_expect) {
  47. no_issues = false;
  48. printf("Warning: csw tag unexpected! Expected %d got %d\n", tag_expect, csw->dCSWTag);
  49. }
  50. if (csw->dCSWDataResidue) {
  51. no_issues = false;
  52. printf("Warning: csw indicates data residue of %d bytes!\n", csw->dCSWDataResidue);
  53. }
  54. if (csw->bCSWStatus) {
  55. no_issues = false;
  56. printf("Warning: csw indicates non-good status %d!\n", csw->bCSWStatus);
  57. }
  58. return no_issues;
  59. }
  60. // ---------------------------------------------------- HID Mouse ------------------------------------------------------
  61. void mock_hid_process_report(mock_hid_mouse_report_t *report, int iter)
  62. {
  63. static int x_pos = 0;
  64. static int y_pos = 0;
  65. //Update X position
  66. if (report->x_movement & 0x80) { //Positive movement
  67. x_pos += report->x_movement & 0x7F;
  68. } else { //Negative movement
  69. x_pos -= report->x_movement & 0x7F;
  70. }
  71. //Update Y position
  72. if (report->y_movement & 0x80) { //Positive movement
  73. y_pos += report->y_movement & 0x7F;
  74. } else { //Negative movement
  75. y_pos -= report->y_movement & 0x7F;
  76. }
  77. printf("\rX:%d\tY:%d\tIter: %d\n", x_pos, y_pos, iter);
  78. }