]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/examples/libusb20/aux.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / share / examples / libusb20 / aux.c
1 /* ----------------------------------------------------------------------------
2  * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
3  * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
4  * can do whatever you want with this stuff. If we meet some day, and you think
5  * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
6  * ----------------------------------------------------------------------------
7  *
8  * $FreeBSD$
9  */
10
11 /*
12  * Helper functions common to all examples
13  */
14
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18
19 #include <libusb20.h>
20 #include <libusb20_desc.h>
21
22 #include "aux.h"
23
24 /*
25  * Return a textual description for error "r".
26  */
27 const char *
28 usb_error(enum libusb20_error r)
29 {
30   const char *msg = "UNKNOWN";
31
32   switch (r)
33     {
34     case LIBUSB20_SUCCESS:
35       msg = "success";
36       break;
37
38     case LIBUSB20_ERROR_IO:
39       msg = "IO error";
40       break;
41
42     case LIBUSB20_ERROR_INVALID_PARAM:
43       msg = "Invalid parameter";
44       break;
45
46     case LIBUSB20_ERROR_ACCESS:
47       msg = "Access denied";
48       break;
49
50     case LIBUSB20_ERROR_NO_DEVICE:
51       msg = "No such device";
52       break;
53
54     case LIBUSB20_ERROR_NOT_FOUND:
55       msg = "Entity not found";
56       break;
57
58     case LIBUSB20_ERROR_BUSY:
59       msg = "Resource busy";
60       break;
61
62     case LIBUSB20_ERROR_TIMEOUT:
63       msg = "Operation timed out";
64       break;
65
66     case LIBUSB20_ERROR_OVERFLOW:
67       msg = "Overflow";
68       break;
69
70     case LIBUSB20_ERROR_PIPE:
71       msg = "Pipe error";
72       break;
73
74     case LIBUSB20_ERROR_INTERRUPTED:
75       msg = "System call interrupted";
76       break;
77
78     case LIBUSB20_ERROR_NO_MEM:
79       msg = "Insufficient memory";
80       break;
81
82     case LIBUSB20_ERROR_NOT_SUPPORTED:
83       msg = "Operation not supported";
84       break;
85
86     case LIBUSB20_ERROR_OTHER:
87       msg = "Other error";
88       break;
89     }
90
91   return msg;
92 }
93
94 /*
95  * Print "len" bytes from "buf" in hex, followed by an ASCII
96  * representation (somewhat resembling the output of hd(1)).
97  */
98 void
99 print_formatted(uint8_t *buf, uint32_t len)
100 {
101   int i, j;
102
103   for (j = 0; j < len; j += 16)
104     {
105       printf("%02x: ", j);
106
107       for (i = 0; i < 16 && i + j < len; i++)
108         printf("%02x ", buf[i + j]);
109       printf("  ");
110       for (i = 0; i < 16 && i + j < len; i++)
111         {
112           uint8_t c = buf[i + j];
113           if(c >= ' ' && c <= '~')
114             printf("%c", (char)c);
115           else
116             putchar('.');
117         }
118       putchar('\n');
119     }
120 }