Advertisement
Guest User

Untitled

a guest
Jul 31st, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. /*
  2. Copyright (c) 2012, SgE
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7.     * Redistributions of source code must retain the above copyright
  8.       notice, this list of conditions and the following disclaimer.
  9.     * Redistributions in binary form must reproduce the above copyright
  10.       notice, this list of conditions and the following disclaimer in the
  11.       documentation and/or other materials provided with the distribution.
  12.     * Neither the name of the SgE nor the
  13.       names of its contributors may be used to endorse or promote products
  14.       derived from this software without specific prior written permission.
  15.  
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL SgE BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. //--------------------------------------------------------------------------------
  28.  
  29. MP709 control
  30. use ./mp709 on or ./mp709 off
  31.  
  32. to compile use command gcc mp709.c -o mp709_sge_mips -lusb-1.0 -lpthread
  33. */
  34.  
  35. #include <libusb-1.0/libusb.h>
  36.  
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <stdlib.h>
  40.  
  41. #define DEV_VID 0x16C0
  42. #define DEV_PID 0x05DF
  43. #define DEV_CONFIG 1
  44. #define DEV_INTF 0
  45.  
  46. unsigned char COMMAND_1[8] = {0xE7,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  47. unsigned char COMMAND_2[8] = {0xE7,0x19,0x00,0x00,0x00,0x00,0x00,0x00};
  48.  
  49. int main(int argc, char * argv[])
  50. {
  51.     if (argc != 2 || argc == 2 && (strcasecmp(argv[1], "off") != 0) && (strcasecmp(argv[1], "on") != 0))
  52.         goto usage;
  53.  
  54.     libusb_device_handle *handle;
  55.     int ret;
  56.     unsigned char buf[8];
  57.  
  58.     ret = libusb_init(NULL);
  59.  
  60.     if (ret < 0) {
  61.         printf("failed to initialise libusb!\n");
  62.         goto exit;
  63.     }
  64.  
  65.     libusb_set_debug(NULL, 3);
  66.     handle = libusb_open_device_with_vid_pid(NULL, DEV_VID, DEV_PID);
  67.  
  68.     if (handle == NULL) {
  69.         printf("no connected mp709 device found!\n");
  70.         libusb_exit(NULL);
  71.         goto exit;
  72.     }
  73.  
  74.     if (libusb_kernel_driver_active(handle,DEV_INTF))
  75.         libusb_detach_kernel_driver(handle, DEV_INTF);
  76.  
  77.     if ((ret = libusb_set_configuration(handle, DEV_CONFIG)) < 0) {
  78.         printf("mp709 device configuration failed!\n");
  79.  
  80.         goto done;
  81.     }
  82.  
  83.     if (libusb_claim_interface(handle,  DEV_INTF) < 0) {
  84.         printf("mp709 device error!\n");
  85.  
  86.         goto finish;
  87.     }
  88.  
  89.     if (strcasecmp(argv[1], "on") == 0) {
  90.         ret = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT, 0x9, 0x300, 0, COMMAND_1, 8, 1000);
  91.  
  92.         if (ret > 0)
  93.             printf("Relay ON\n");
  94.     }
  95.  
  96.     if (strcasecmp(argv[1], "off") == 0) {
  97.         ret = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT, 0x9, 0x300, 0, COMMAND_2, 8, 1000);
  98.  
  99.         if (ret > 0)
  100.             printf("Relay OFF\n");
  101.     }
  102.  
  103. finish:
  104.     libusb_attach_kernel_driver(handle, DEV_INTF);
  105.  
  106. done:
  107.     libusb_close(handle);
  108.     libusb_exit(NULL);
  109.  
  110.     if (ret > 0)
  111.         exit(0);
  112.     else
  113.     {
  114.         printf("mp709 device communication error!");
  115.         goto exit;
  116.     }
  117.  
  118. usage:
  119.     printf("Invalid parameters!\n");
  120.     printf("Usage: mp709 <on|off>\n");
  121. exit:
  122.     exit(1);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement