Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <signal.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h> // for assert()
- #include "stdafx.h"
- #include <libusb.h>
- void print_devs(libusb_device **devs);
- libusb_device* verify_device(libusb_device **devs);
- #include <iostream>
- int test2();
- int do_exit = 0;
- using namespace std;
- BOOL WINAPI ConsoleHandler(
- DWORD dwCtrlType // control signal type
- );
- BOOL WINAPI ConsoleHandler(DWORD CEvent)
- {
- char mesg[128];
- switch(CEvent)
- {
- case CTRL_C_EVENT:
- MessageBox(NULL,
- _T("CTRL+C received!"),_T("CEvent"),MB_OK);
- do_exit = true;
- break;
- case CTRL_BREAK_EVENT:
- MessageBox(NULL,
- _T("CTRL+BREAK received!"),_T("CEvent"),MB_OK);
- break;
- case CTRL_CLOSE_EVENT:
- MessageBox(NULL,
- _T("Program being closed!"),_T("CEvent"),MB_OK);
- break;
- case CTRL_LOGOFF_EVENT:
- MessageBox(NULL,
- _T("User is logging off!"),_T("CEvent"),MB_OK);
- break;
- case CTRL_SHUTDOWN_EVENT:
- MessageBox(NULL,
- _T("User is logging off!"),_T("CEvent"),MB_OK);
- break;
- }
- return TRUE;
- }
- int main() {
- //Handle Control+C
- if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
- {
- printf("Unable to install handler!\n");
- return -1;
- }
- test2();
- return 0;
- }
- // OUT-going transfers (OUT from host PC to USB-device)
- struct libusb_transfer *transfer_command_out = NULL; // find version
- // IN-coming transfers (IN to host PC from USB-device)
- struct libusb_transfer *transfer_response_in = NULL;
- extern "C" void LIBUSB_CALL receive(struct libusb_transfer *transfer);
- extern "C" void LIBUSB_CALL cb_command_out(struct libusb_transfer *transfer);
- extern "C" void sighandler(int signum);
- // Setup Endpoints - The endpoints (2,4) are particular to the USB-connected device
- #define EP_COMMAND (0x03 | LIBUSB_ENDPOINT_OUT) // OUT of PC to USB-device //DEVICE_SPECIFIC
- #define EP_RESPONSE (0x83 | LIBUSB_ENDPOINT_IN ) // IN PC from USB-device //DEVICE_SPECIFIC
- unsigned char buffer[1024] = {0};
- int device_num = 1;
- int test2()
- {
- libusb_device **devs;
- int r;
- libusb_context *ctx = NULL; //a libusb session
- ssize_t cnt; //holding number of devices in list
- r = libusb_init(&ctx); //initialize the library for the session we just declared
- if(r < 0) {
- cout<<"Init Error "<<r<<endl; //there was an error
- return 1;
- }
- libusb_set_debug(ctx, 3);
- cnt = libusb_get_device_list(ctx, &devs);
- if (cnt < 0)
- return (int) cnt;
- libusb_device *dev = verify_device(devs);
- struct libusb_device_descriptor desc;
- r = libusb_get_device_descriptor(dev, &desc);
- if (r < 0) {
- fprintf(stderr, "failed to get device descriptor");
- return -1;
- }
- libusb_config_descriptor *config;
- libusb_get_config_descriptor(dev, 0, &config);
- cout<<"Interfaces: "<<(int)config->bNumInterfaces;
- const libusb_interface *inter;
- const libusb_interface_descriptor *interdesc;
- const libusb_endpoint_descriptor *epdesc;
- /*
- for(int i=0; i<(int)config->bNumInterfaces; i++) {
- inter = &config->interface[i];
- cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";
- for(int j=0; j<inter->num_altsetting; j++) {
- interdesc = &inter->altsetting[j];
- cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
- cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
- for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
- epdesc = &interdesc->endpoint[k];
- cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
- cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";
- }
- }
- }*/
- libusb_free_config_descriptor(config);
- printf("%04x:%04x (bus %d, device %d)\n",
- desc.idVendor, desc.idProduct,
- libusb_get_bus_number(dev), libusb_get_device_address(dev));
- libusb_device_handle *handle = NULL;
- r = libusb_open(dev, &handle);
- printf("libusb_open = %d\n", r);
- r = libusb_kernel_driver_active(handle, device_num);
- printf("libusb_kernel_driver_active = %d\n", r);
- if (r == 1) {
- r = libusb_detach_kernel_driver(handle, device_num);
- printf("libusb_detach_kernel_driver = %d\n", r);
- }
- r = libusb_claim_interface(handle, device_num);
- if (r < 0) {
- fprintf(stderr, "usb_claim_interface error %d\n", r);
- return -1;
- }
- const int length = 1;
- unsigned char buf[length] = {0};
- buf[0] = 'a';
- transfer_response_in = libusb_alloc_transfer(0);
- libusb_fill_bulk_transfer( transfer_response_in, handle, EP_RESPONSE,
- buffer, 1024, // Note: version_string is where input data written.
- receive, NULL, 0); // no user data
- r = libusb_submit_transfer(transfer_response_in);
- printf("return value from libusb_submit_transfer(for transfer_response_in)= %d\n", r);
- // Define transfer of data OUT (OUT to USB-device from host PC)
- transfer_command_out = libusb_alloc_transfer(0);
- libusb_fill_bulk_transfer(transfer_command_out, handle, EP_COMMAND,
- buf, length, // version request command, command length
- cb_command_out, NULL, 0); // callback function, NO callback data
- r = libusb_submit_transfer(transfer_command_out);
- printf("return value from libusb_submit_transfer(for transfer_command_out)=%d\n", r);
- while (!do_exit) {
- struct timeval tv;
- tv.tv_sec = 0; // seconds
- tv.tv_usec = 100; // milliseconds ( .1 sec)
- //libusb_handle_events(ctx);
- libusb_handle_events_timeout(ctx, &tv);
- }
- libusb_release_interface(handle, device_num);
- libusb_close(handle);
- libusb_free_device_list(devs, 0);
- libusb_exit(ctx);
- printf("Exit 0");
- return 0;
- }
- extern "C" void LIBUSB_CALL receive(struct libusb_transfer *transfer)
- { int i;
- fprintf(stderr, "cb_response_in: status =%d, actual_length=%d\n",
- transfer->status, transfer->actual_length);
- for (i=0; i < transfer->actual_length; i++){
- printf("buffer[%d]=%x \n", i, buffer[i] );
- }
- return;
- }
- //This is called after command for version is received.
- // - This is ONLY a mechanism to indicate the that version command has been received by libusb.
- // It does NOT signal that the version_string is ready.
- // It does NOT say when the version will be ready.
- extern "C" void LIBUSB_CALL cb_command_out(struct libusb_transfer *transfer)
- {
- fprintf(stderr, "cb_command_out: status =%d, actual_length=%d\n",
- transfer->status, transfer->actual_length);
- return;
- }
- libusb_device* verify_device(libusb_device **devs)
- {
- libusb_device *dev;
- int i = 0;
- while ((dev = devs[i++]) != NULL) {
- struct libusb_device_descriptor desc;
- int r = libusb_get_device_descriptor(dev, &desc);
- if (r < 0) {
- fprintf(stderr, "failed to get device descriptor");
- return NULL;
- }
- if(desc.idVendor == 0x04D8 && desc.idProduct == 0x00DF)
- {
- puts("Found our Device!");
- return dev;
- }
- }
- return NULL;
- }
- void print_devs(libusb_device **devs)
- {
- libusb_device *dev;
- int i = 0;
- while ((dev = devs[i++]) != NULL) {
- struct libusb_device_descriptor desc;
- int r = libusb_get_device_descriptor(dev, &desc);
- if (r < 0) {
- fprintf(stderr, "failed to get device descriptor");
- return;
- }
- printf("%04x:%04x (bus %d, device %d)\n",
- desc.idVendor, desc.idProduct,
- libusb_get_bus_number(dev), libusb_get_device_address(dev));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement