make comportable with SDCC 3.9.0 (sdcclib was replaced with sdar utility
[fx2lib.git] / examples / usbmon_c / test.c
blob09f2d787f5eaa919acf51fdc1324db1d320220b1
1 /**
2 * Copyright (C) 2009 Ubixum, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 **/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
23 #include <libusb-1.0/libusb.h>
26 int main(int argc __attribute__((unused)),
27 char **argv __attribute__((unused)))
29 int transferred, rv, i, ret = 1;;
30 libusb_device_handle *hndl;
31 libusb_context *ctx;
32 unsigned char buf[512];
35 libusb_init(&ctx);
37 hndl = libusb_open_device_with_vid_pid(ctx, 0x4b4, 0x8613);
39 if (!hndl) {
40 fprintf(stderr, "failed to open device\n");
41 goto out;
44 libusb_claim_interface(hndl, 0);
46 libusb_set_interface_alt_setting(hndl, 0, 0);
48 for(;;) {
49 for (i = 0; i < (int)sizeof(buf); i++)
50 buf[i] = i;
52 printf("OUT transfer to device\n");
53 transferred = 0;
54 rv = libusb_bulk_transfer(hndl, 0x02, bufl, 32, &transferred, 500);
55 if(rv) {
56 fprintf(stderr, "OUT Transfer failed: %d (%d transferred)\n", rv, transferred);
57 // goto out;
60 rv = libusb_bulk_transfer(hndl, 0x02, buf, 0, &transferred, 500);
61 if(rv) {
62 fprintf(stderr, "OUT0 Transfer failed: %d (%d transferred)\n", rv, transferred);
63 // goto out;
66 printf("IN transfer to device\n");
68 memset(buf, 0, sizeof(buf));
69 transferred = 0;
71 rv=libusb_bulk_transfer(hndl, 0x86, (unsigned char*)buf ,sizeof(buf), &transferred, 500);
72 if(rv) {
73 fprintf(stderr, "IN Transfer failed: %d (%d transferred)\n", rv, transferred);
74 // goto out;
75 } else {
76 printf("received %d bytes:\n", transferred);
78 for (i = 0; i < transferred; i++)
79 printf ("%02x ", buf[i]);
80 printf("\n");
82 sleep(1);
84 ret = 0;
85 out:
86 libusb_close(hndl);
87 return ret;