Path: blob/master/tools/usb/ffs-aio-example/multibuff/host_app/test.c
26295 views
/*1* This is free and unencumbered software released into the public domain.2*3* Anyone is free to copy, modify, publish, use, compile, sell, or4* distribute this software, either in source code form or as a compiled5* binary, for any purpose, commercial or non-commercial, and by any6* means.7*8* In jurisdictions that recognize copyright laws, the author or authors9* of this software dedicate any and all copyright interest in the10* software to the public domain. We make this dedication for the benefit11* of the public at large and to the detriment of our heirs and12* successors. We intend this dedication to be an overt act of13* relinquishment in perpetuity of all present and future rights to this14* software under copyright law.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.19* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR20* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,21* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR22* OTHER DEALINGS IN THE SOFTWARE.23*24* For more information, please refer to <http://unlicense.org/>25*/2627#include <libusb.h>28#include <stdio.h>29#include <string.h>30#include <unistd.h>3132#define VENDOR 0x1d6b33#define PRODUCT 0x01053435#define BUF_LEN 81923637/*38* struct test_state - describes test program state39* @list: list of devices returned by libusb_get_device_list function40* @found: pointer to struct describing tested device41* @ctx: context, set to NULL42* @handle: handle of tested device43* @attached: indicates that device was attached to kernel, and has to be44* reattached at the end of test program45*/4647struct test_state {48libusb_device *found;49libusb_context *ctx;50libusb_device_handle *handle;51int attached;52};5354/*55* test_init - initialize test program56*/5758int test_init(struct test_state *state)59{60int i, ret;61ssize_t cnt;62libusb_device **list;6364state->found = NULL;65state->ctx = NULL;66state->handle = NULL;67state->attached = 0;6869ret = libusb_init(&state->ctx);70if (ret) {71printf("cannot init libusb: %s\n", libusb_error_name(ret));72return 1;73}7475cnt = libusb_get_device_list(state->ctx, &list);76if (cnt <= 0) {77printf("no devices found\n");78goto error1;79}8081for (i = 0; i < cnt; ++i) {82libusb_device *dev = list[i];83struct libusb_device_descriptor desc;84ret = libusb_get_device_descriptor(dev, &desc);85if (ret) {86printf("unable to get device descriptor: %s\n",87libusb_error_name(ret));88goto error2;89}90if (desc.idVendor == VENDOR && desc.idProduct == PRODUCT) {91state->found = dev;92break;93}94}9596if (!state->found) {97printf("no devices found\n");98goto error2;99}100101ret = libusb_open(state->found, &state->handle);102if (ret) {103printf("cannot open device: %s\n", libusb_error_name(ret));104goto error2;105}106107if (libusb_claim_interface(state->handle, 0)) {108ret = libusb_detach_kernel_driver(state->handle, 0);109if (ret) {110printf("unable to detach kernel driver: %s\n",111libusb_error_name(ret));112goto error3;113}114state->attached = 1;115ret = libusb_claim_interface(state->handle, 0);116if (ret) {117printf("cannot claim interface: %s\n",118libusb_error_name(ret));119goto error4;120}121}122123return 0;124125error4:126if (state->attached == 1)127libusb_attach_kernel_driver(state->handle, 0);128129error3:130libusb_close(state->handle);131132error2:133libusb_free_device_list(list, 1);134135error1:136libusb_exit(state->ctx);137return 1;138}139140/*141* test_exit - cleanup test program142*/143144void test_exit(struct test_state *state)145{146libusb_release_interface(state->handle, 0);147if (state->attached == 1)148libusb_attach_kernel_driver(state->handle, 0);149libusb_close(state->handle);150libusb_exit(state->ctx);151}152153int main(void)154{155struct test_state state;156struct libusb_config_descriptor *conf;157struct libusb_interface_descriptor const *iface;158unsigned char addr;159160if (test_init(&state))161return 1;162163libusb_get_config_descriptor(state.found, 0, &conf);164iface = &conf->interface[0].altsetting[0];165addr = iface->endpoint[0].bEndpointAddress;166167while (1) {168static unsigned char buffer[BUF_LEN];169int bytes;170libusb_bulk_transfer(state.handle, addr, buffer, BUF_LEN,171&bytes, 500);172}173test_exit(&state);174}175176177