set-led.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Name: set-led.c
  2. * Project: hid-custom-rq example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-10
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
  9. */
  10. /*
  11. General Description:
  12. This is the host-side driver for the custom-class example device. It searches
  13. the USB for the LEDControl device and sends the requests understood by this
  14. device.
  15. This program must be linked with libusb on Unix and libusb-win32 on Windows.
  16. See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/
  17. respectively.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <usb.h> /* this is libusb */
  23. #include "opendevice.h" /* common code moved to separate module */
  24. #include "../firmware/requests.h" /* custom request numbers */
  25. #include "../firmware/usbconfig.h" /* device's VID/PID and names */
  26. static void usage(char *name)
  27. {
  28. fprintf(stderr, "usage:\n");
  29. fprintf(stderr, " %s on ....... turn on LED\n", name);
  30. fprintf(stderr, " %s off ...... turn off LED\n", name);
  31. fprintf(stderr, " %s status ... ask current status of LED\n", name);
  32. #if ENABLE_TEST
  33. fprintf(stderr, " %s test ..... run driver reliability test\n", name);
  34. #endif /* ENABLE_TEST */
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. usb_dev_handle *handle = NULL;
  39. const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
  40. char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
  41. char buffer[4];
  42. int cnt, vid, pid, isOn;
  43. usb_init();
  44. if(argc < 2){ /* we need at least one argument */
  45. usage(argv[0]);
  46. exit(1);
  47. }
  48. /* compute VID/PID from usbconfig.h so that there is a central source of information */
  49. vid = rawVid[1] * 256 + rawVid[0];
  50. pid = rawPid[1] * 256 + rawPid[0];
  51. /* The following function is in opendevice.c: */
  52. if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
  53. fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
  54. exit(1);
  55. }
  56. /* Since we use only control endpoint 0, we don't need to choose a
  57. * configuration and interface. Reading device descriptor and setting a
  58. * configuration and interface is done through endpoint 0 after all.
  59. * However, newer versions of Linux require that we claim an interface
  60. * even for endpoint 0. Enable the following code if your operating system
  61. * needs it: */
  62. #if 0
  63. int retries = 1, usbConfiguration = 1, usbInterface = 0;
  64. if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
  65. fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
  66. }
  67. /* now try to claim the interface and detach the kernel HID driver on
  68. * Linux and other operating systems which support the call. */
  69. while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
  70. #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
  71. if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
  72. fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
  73. }
  74. #endif
  75. }
  76. #endif
  77. if(strcasecmp(argv[1], "status") == 0){
  78. cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000);
  79. if(cnt < 1){
  80. if(cnt < 0){
  81. fprintf(stderr, "USB error: %s\n", usb_strerror());
  82. }else{
  83. fprintf(stderr, "only %d bytes received.\n", cnt);
  84. }
  85. }else{
  86. printf("LED is %s\n", buffer[0] ? "on" : "off");
  87. }
  88. }else if((isOn = (strcasecmp(argv[1], "on") == 0)) || strcasecmp(argv[1], "off") == 0){
  89. cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, isOn, 0, buffer, 0, 5000);
  90. if(cnt < 0){
  91. fprintf(stderr, "USB error: %s\n", usb_strerror());
  92. }
  93. #if ENABLE_TEST
  94. }else if(strcasecmp(argv[1], "test") == 0){
  95. int i;
  96. srandomdev();
  97. for(i = 0; i < 50000; i++){
  98. int value = random() & 0xffff, index = random() & 0xffff;
  99. int rxValue, rxIndex;
  100. if((i+1) % 100 == 0){
  101. fprintf(stderr, "\r%05d", i+1);
  102. fflush(stderr);
  103. }
  104. cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_ECHO, value, index, buffer, sizeof(buffer), 5000);
  105. if(cnt < 0){
  106. fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
  107. break;
  108. }else if(cnt != 4){
  109. fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of 4\n", i, cnt);
  110. break;
  111. }
  112. rxValue = ((int)buffer[0] & 0xff) | (((int)buffer[1] & 0xff) << 8);
  113. rxIndex = ((int)buffer[2] & 0xff) | (((int)buffer[3] & 0xff) << 8);
  114. if(rxValue != value || rxIndex != index){
  115. fprintf(stderr, "\ndata error in iteration %d:\n", i);
  116. fprintf(stderr, "rxValue = 0x%04x value = 0x%04x\n", rxValue, value);
  117. fprintf(stderr, "rxIndex = 0x%04x index = 0x%04x\n", rxIndex, index);
  118. }
  119. }
  120. fprintf(stderr, "\nTest completed.\n");
  121. #endif /* ENABLE_TEST */
  122. }else{
  123. usage(argv[0]);
  124. exit(1);
  125. }
  126. usb_close(handle);
  127. return 0;
  128. }