scsi2sd-debug.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include <getopt.h>
  18. #include <inttypes.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. // htonl/ntohl includes.
  25. #ifdef _WIN32
  26. #include <winsock2.h>
  27. #else
  28. #include <arpa/inet.h>
  29. #endif
  30. #include "hidapi.h"
  31. #define MIN(a,b) (a < b ? a : b)
  32. FILE* logfile = NULL;
  33. static void readConfig(hid_device* handle)
  34. {
  35. // First byte is the report ID (0)
  36. unsigned char buf[65];
  37. memset(buf, 0, sizeof(buf));
  38. int result = hid_read(handle, buf, sizeof(buf));
  39. if (result < 0)
  40. {
  41. fprintf(stderr, "USB HID Read Failure: %ls\n", hid_error(handle));
  42. }
  43. int i;
  44. for (i = 0; i < 32; ++i)
  45. {
  46. fprintf(logfile, "%02x ", buf[i]);
  47. }
  48. fprintf(logfile, "\n");
  49. fflush(logfile);
  50. }
  51. static void usage()
  52. {
  53. printf("Usage: scsi2sd-debug outputfile\n");
  54. printf("\n");
  55. printf("outputfile\tPath to the output log file.\n\n");
  56. printf("\n\n");
  57. exit(1);
  58. }
  59. int main(int argc, char* argv[])
  60. {
  61. printf("SCSI2SD Debug Utility.\n");
  62. printf("Copyright (C) 2014 Michael McMaster <michael@codesrc.com>\n\n");
  63. if (argc != 2)
  64. {
  65. usage();
  66. exit(1);
  67. }
  68. logfile = fopen(argv[1], "w");
  69. if (!logfile)
  70. {
  71. fprintf(stderr, "Could not write to file %s.\n", argv[1]);
  72. exit(1);
  73. }
  74. uint16_t vendorId = 0x04B4; // Cypress
  75. uint16_t productId = 0x1337; // SCSI2SD
  76. printf(
  77. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  78. vendorId,
  79. productId);
  80. // Enumerate and print the HID devices on the system
  81. struct hid_device_info *dev = hid_enumerate(vendorId, productId);
  82. if (!dev)
  83. {
  84. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  85. exit(1);
  86. }
  87. // We need the SECOND interface for debug data
  88. while (dev && dev->interface_number != 1)
  89. {
  90. dev = dev->next;
  91. }
  92. if (!dev)
  93. {
  94. fprintf(stderr, "ERROR: SCSI2SD Debug firmware not enabled.\n");
  95. exit(1);
  96. }
  97. printf("USB Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
  98. dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
  99. printf("\n");
  100. printf(" Manufacturer: %ls\n", dev->manufacturer_string);
  101. printf(" Product: %ls\n", dev->product_string);
  102. printf("\n");
  103. hid_device* handle = hid_open_path(dev->path);
  104. if (!handle)
  105. {
  106. fprintf(
  107. stderr,
  108. "ERROR: Could not open device %s. Check permissions.\n", dev->path
  109. );
  110. exit(1);
  111. }
  112. while (1)
  113. {
  114. readConfig(handle);
  115. }
  116. return 0;
  117. }