- Use libusb20_strerror() function instead of custom usb_error() one.

- Rename "aux.[ch]" to "util.[ch]" which is a more common name for
utility functions and allows checkout on some non-FreeBSD systems
where the "aux.*" namespace is reserved.
- Fix some compile warnings while at it.

PR:		usb/183728
MFC after:	2 weeks
This commit is contained in:
hselasky 2013-11-07 07:22:51 +00:00
parent ebbf5b2ae0
commit 4d145bcb8e
6 changed files with 72 additions and 141 deletions

View File

@ -1,13 +1,14 @@
# $FreeBSD$ # $FreeBSD$
TARGETS= bulk control TARGETS= bulk control
CFLAGS+= -Wall
all: $(TARGETS) all: $(TARGETS)
bulk: bulk.o aux.o bulk: bulk.o util.o
$(CC) $(CFLAGS) -o bulk bulk.o aux.o -lusb $(CC) $(CFLAGS) -o bulk bulk.o util.o -lusb
control: control.o aux.o control: control.o util.o
$(CC) $(CFLAGS) -o control control.o aux.o -lusb $(CC) $(CFLAGS) -o control control.o util.o -lusb
clean: clean:
rm -f $(TARGETS) *.o *~ rm -f $(TARGETS) *.o *~

View File

@ -1,120 +0,0 @@
/* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
* <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
* ----------------------------------------------------------------------------
*
* $FreeBSD$
*/
/*
* Helper functions common to all examples
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <libusb20.h>
#include <libusb20_desc.h>
#include "aux.h"
/*
* Return a textual description for error "r".
*/
const char *
usb_error(enum libusb20_error r)
{
const char *msg = "UNKNOWN";
switch (r)
{
case LIBUSB20_SUCCESS:
msg = "success";
break;
case LIBUSB20_ERROR_IO:
msg = "IO error";
break;
case LIBUSB20_ERROR_INVALID_PARAM:
msg = "Invalid parameter";
break;
case LIBUSB20_ERROR_ACCESS:
msg = "Access denied";
break;
case LIBUSB20_ERROR_NO_DEVICE:
msg = "No such device";
break;
case LIBUSB20_ERROR_NOT_FOUND:
msg = "Entity not found";
break;
case LIBUSB20_ERROR_BUSY:
msg = "Resource busy";
break;
case LIBUSB20_ERROR_TIMEOUT:
msg = "Operation timed out";
break;
case LIBUSB20_ERROR_OVERFLOW:
msg = "Overflow";
break;
case LIBUSB20_ERROR_PIPE:
msg = "Pipe error";
break;
case LIBUSB20_ERROR_INTERRUPTED:
msg = "System call interrupted";
break;
case LIBUSB20_ERROR_NO_MEM:
msg = "Insufficient memory";
break;
case LIBUSB20_ERROR_NOT_SUPPORTED:
msg = "Operation not supported";
break;
case LIBUSB20_ERROR_OTHER:
msg = "Other error";
break;
}
return msg;
}
/*
* Print "len" bytes from "buf" in hex, followed by an ASCII
* representation (somewhat resembling the output of hd(1)).
*/
void
print_formatted(uint8_t *buf, uint32_t len)
{
int i, j;
for (j = 0; j < len; j += 16)
{
printf("%02x: ", j);
for (i = 0; i < 16 && i + j < len; i++)
printf("%02x ", buf[i + j]);
printf(" ");
for (i = 0; i < 16 && i + j < len; i++)
{
uint8_t c = buf[i + j];
if(c >= ' ' && c <= '~')
printf("%c", (char)c);
else
putchar('.');
}
putchar('\n');
}
}

View File

@ -41,7 +41,7 @@
#include <libusb20.h> #include <libusb20.h>
#include <libusb20_desc.h> #include <libusb20_desc.h>
#include "aux.h" #include "util.h"
/* /*
* If you want to see the details of the internal datastructures * If you want to see the details of the internal datastructures
@ -74,7 +74,7 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_dev_open(dev, 2)) != 0) if ((rv = libusb20_dev_open(dev, 2)) != 0)
{ {
fprintf(stderr, "libusb20_dev_open: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -84,7 +84,7 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0) if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
{ {
fprintf(stderr, "libusb20_dev_set_config_index: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -97,7 +97,7 @@ doit(struct libusb20_device *dev)
if (xfr_in == NULL || xfr_out == NULL) if (xfr_in == NULL || xfr_out == NULL)
{ {
fprintf(stderr, "libusb20_tr_get_pointer: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -107,12 +107,12 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0) if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
{ {
fprintf(stderr, "libusb20_tr_open: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
return; return;
} }
if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0) if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
{ {
fprintf(stderr, "libusb20_tr_open: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -124,7 +124,7 @@ doit(struct libusb20_device *dev)
if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT)) if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
!= 0) != 0)
{ {
fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", libusb20_strerror(rv));
} }
printf("sent %d bytes\n", rlen); printf("sent %d bytes\n", rlen);
} }
@ -132,7 +132,7 @@ doit(struct libusb20_device *dev)
if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT)) if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
!= 0) != 0)
{ {
fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
} }
printf("received %d bytes\n", rlen); printf("received %d bytes\n", rlen);
if (rlen > 0) if (rlen > 0)

View File

@ -11,8 +11,6 @@
/* /*
* Simple demo program to illustrate the handling of FreeBSD's * Simple demo program to illustrate the handling of FreeBSD's
* libusb20. * libusb20.
*
* XXX
*/ */
/* /*
@ -38,12 +36,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <sysexits.h> #include <sysexits.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <libusb20.h> #include <libusb20.h>
#include <libusb20_desc.h> #include <libusb20_desc.h>
#include <sys/queue.h> #include <sys/queue.h>
#include "util.h"
/* /*
* If you want to see the details of the internal datastructures * If you want to see the details of the internal datastructures
* in the debugger, unifdef the following. * in the debugger, unifdef the following.
@ -86,7 +87,7 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_dev_open(dev, 1)) != 0) if ((rv = libusb20_dev_open(dev, 1)) != 0)
{ {
fprintf(stderr, "libusb20_dev_open: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -96,7 +97,7 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0) if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
{ {
fprintf(stderr, "libusb20_dev_set_config_index: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -126,7 +127,7 @@ doit(struct libusb20_device *dev)
0 /* flags */)) != 0) 0 /* flags */)) != 0)
{ {
fprintf(stderr, fprintf(stderr,
"libusb20_dev_request_sync: %s\n", usb_error(rv)); "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
} }
printf("sent %d bytes\n", actlen); printf("sent %d bytes\n", actlen);
if ((setup.bmRequestType & 0x80) != 0) if ((setup.bmRequestType & 0x80) != 0)
@ -146,7 +147,7 @@ doit(struct libusb20_device *dev)
if (xfr_intr == NULL) if (xfr_intr == NULL)
{ {
fprintf(stderr, "libusb20_tr_get_pointer: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -155,7 +156,7 @@ doit(struct libusb20_device *dev)
*/ */
if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0) if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
{ {
fprintf(stderr, "libusb20_tr_open: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
return; return;
} }
@ -165,7 +166,7 @@ doit(struct libusb20_device *dev)
if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT)) if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
!= 0) != 0)
{ {
fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", usb_error(rv)); fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
} }
printf("received %d bytes\n", rlen); printf("received %d bytes\n", rlen);
if (rlen > 0) if (rlen > 0)

View File

@ -0,0 +1,50 @@
/* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
* <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
* ----------------------------------------------------------------------------
*
* $FreeBSD$
*/
/*
* Helper functions common to all examples
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <libusb20.h>
#include <libusb20_desc.h>
#include "util.h"
/*
* Print "len" bytes from "buf" in hex, followed by an ASCII
* representation (somewhat resembling the output of hd(1)).
*/
void
print_formatted(uint8_t *buf, uint32_t len)
{
int i, j;
for (j = 0; j < len; j += 16)
{
printf("%02x: ", j);
for (i = 0; i < 16 && i + j < len; i++)
printf("%02x ", buf[i + j]);
printf(" ");
for (i = 0; i < 16 && i + j < len; i++)
{
uint8_t c = buf[i + j];
if(c >= ' ' && c <= '~')
printf("%c", (char)c);
else
putchar('.');
}
putchar('\n');
}
}

View File

@ -11,5 +11,4 @@
#include <stdint.h> #include <stdint.h>
#include <libusb20.h> #include <libusb20.h>
const char *usb_error(enum libusb20_error r);
void print_formatted(uint8_t *buf, uint32_t len); void print_formatted(uint8_t *buf, uint32_t len);