working networking and serial
[linux-usb-otg] / hid_gadget_test.c
1 /* hid_gadget_test */
2
3 #include <pthread.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 #define BUF_LEN 512
14
15 struct options {
16         const char    *opt;
17         unsigned char val;
18 };
19
20 static struct options kmod[] = {
21         {.opt = "--left-ctrl",          .val = 0x01},
22         {.opt = "--right-ctrl",         .val = 0x10},
23         {.opt = "--left-shift",         .val = 0x02},
24         {.opt = "--right-shift",        .val = 0x20},
25         {.opt = "--left-alt",           .val = 0x04},
26         {.opt = "--right-alt",          .val = 0x40},
27         {.opt = "--left-meta",          .val = 0x08},
28         {.opt = "--right-meta",         .val = 0x80},
29         {.opt = NULL}
30 };
31
32 static struct options kval[] = {
33         {.opt = "--return",     .val = 0x28},
34         {.opt = "--esc",        .val = 0x29},
35         {.opt = "--bckspc",     .val = 0x2a},
36         {.opt = "--tab",        .val = 0x2b},
37         {.opt = "--spacebar",   .val = 0x2c},
38         {.opt = "--caps-lock",  .val = 0x39},
39         {.opt = "--f1",         .val = 0x3a},
40         {.opt = "--f2",         .val = 0x3b},
41         {.opt = "--f3",         .val = 0x3c},
42         {.opt = "--f4",         .val = 0x3d},
43         {.opt = "--f5",         .val = 0x3e},
44         {.opt = "--f6",         .val = 0x3f},
45         {.opt = "--f7",         .val = 0x40},
46         {.opt = "--f8",         .val = 0x41},
47         {.opt = "--f9",         .val = 0x42},
48         {.opt = "--f10",        .val = 0x43},
49         {.opt = "--f11",        .val = 0x44},
50         {.opt = "--f12",        .val = 0x45},
51         {.opt = "--insert",     .val = 0x49},
52         {.opt = "--home",       .val = 0x4a},
53         {.opt = "--pageup",     .val = 0x4b},
54         {.opt = "--del",        .val = 0x4c},
55         {.opt = "--end",        .val = 0x4d},
56         {.opt = "--pagedown",   .val = 0x4e},
57         {.opt = "--right",      .val = 0x4f},
58         {.opt = "--left",       .val = 0x50},
59         {.opt = "--down",       .val = 0x51},
60         {.opt = "--kp-enter",   .val = 0x58},
61         {.opt = "--up",         .val = 0x52},
62         {.opt = "--num-lock",   .val = 0x53},
63         {.opt = NULL}
64 };
65
66 int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold)
67 {
68         char *tok = strtok(buf, " ");
69         int key = 0;
70         int i = 0;
71
72         for (; tok != NULL; tok = strtok(NULL, " ")) {
73
74                 if (strcmp(tok, "--quit") == 0)
75                         return -1;
76
77                 if (strcmp(tok, "--hold") == 0) {
78                         *hold = 1;
79                         continue;
80                 }
81
82                 if (key < 6) {
83                         for (i = 0; kval[i].opt != NULL; i++)
84                                 if (strcmp(tok, kval[i].opt) == 0) {
85                                         report[2 + key++] = kval[i].val;
86                                         break;
87                                 }
88                         if (kval[i].opt != NULL)
89                                 continue;
90                 }
91
92                 if (key < 6)
93                         if (islower(tok[0])) {
94                                 report[2 + key++] = (tok[0] - ('a' - 0x04));
95                                 continue;
96                         }
97
98                 for (i = 0; kmod[i].opt != NULL; i++)
99                         if (strcmp(tok, kmod[i].opt) == 0) {
100                                 report[0] = report[0] | kmod[i].val;
101                                 break;
102                         }
103                 if (kmod[i].opt != NULL)
104                         continue;
105
106                 if (key < 6)
107                         fprintf(stderr, "unknown option: %s\n", tok);
108         }
109         return 8;
110 }
111
112 static struct options mmod[] = {
113         {.opt = "--b1", .val = 0x01},
114         {.opt = "--b2", .val = 0x02},
115         {.opt = "--b3", .val = 0x04},
116         {.opt = NULL}
117 };
118
119 int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold)
120 {
121         char *tok = strtok(buf, " ");
122         int mvt = 0;
123         int i = 0;
124         for (; tok != NULL; tok = strtok(NULL, " ")) {
125
126                 if (strcmp(tok, "--quit") == 0)
127                         return -1;
128
129                 if (strcmp(tok, "--hold") == 0) {
130                         *hold = 1;
131                         continue;
132                 }
133
134                 for (i = 0; mmod[i].opt != NULL; i++)
135                         if (strcmp(tok, mmod[i].opt) == 0) {
136                                 report[0] = report[0] | mmod[i].val;
137                                 break;
138                         }
139                 if (mmod[i].opt != NULL)
140                         continue;
141
142                 if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
143                         errno = 0;
144                         report[1 + mvt++] = (char)strtol(tok, NULL, 0);
145                         if (errno != 0) {
146                                 fprintf(stderr, "Bad value:'%s'\n", tok);
147                                 report[1 + mvt--] = 0;
148                         }
149                         continue;
150                 }
151
152                 fprintf(stderr, "unknown option: %s\n", tok);
153         }
154         return 3;
155 }
156
157 static struct options jmod[] = {
158         {.opt = "--b1",         .val = 0x10},
159         {.opt = "--b2",         .val = 0x20},
160         {.opt = "--b3",         .val = 0x40},
161         {.opt = "--b4",         .val = 0x80},
162         {.opt = "--hat1",       .val = 0x00},
163         {.opt = "--hat2",       .val = 0x01},
164         {.opt = "--hat3",       .val = 0x02},
165         {.opt = "--hat4",       .val = 0x03},
166         {.opt = "--hatneutral", .val = 0x04},
167         {.opt = NULL}
168 };
169
170 int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold)
171 {
172         char *tok = strtok(buf, " ");
173         int mvt = 0;
174         int i = 0;
175
176         *hold = 1;
177
178         /* set default hat position: neutral */
179         report[3] = 0x04;
180
181         for (; tok != NULL; tok = strtok(NULL, " ")) {
182
183                 if (strcmp(tok, "--quit") == 0)
184                         return -1;
185
186                 for (i = 0; jmod[i].opt != NULL; i++)
187                         if (strcmp(tok, jmod[i].opt) == 0) {
188                                 report[3] = (report[3] & 0xF0) | jmod[i].val;
189                                 break;
190                         }
191                 if (jmod[i].opt != NULL)
192                         continue;
193
194                 if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
195                         errno = 0;
196                         report[mvt++] = (char)strtol(tok, NULL, 0);
197                         if (errno != 0) {
198                                 fprintf(stderr, "Bad value:'%s'\n", tok);
199                                 report[mvt--] = 0;
200                         }
201                         continue;
202                 }
203
204                 fprintf(stderr, "unknown option: %s\n", tok);
205         }
206         return 4;
207 }
208
209 void print_options(char c)
210 {
211         int i = 0;
212
213         if (c == 'k') {
214                 printf("        keyboard options:\n"
215                        "                --hold\n");
216                 for (i = 0; kmod[i].opt != NULL; i++)
217                         printf("\t\t%s\n", kmod[i].opt);
218                 printf("\n      keyboard values:\n"
219                        "                [a-z] or\n");
220                 for (i = 0; kval[i].opt != NULL; i++)
221                         printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
222                 printf("\n");
223         } else if (c == 'm') {
224                 printf("        mouse options:\n"
225                        "                --hold\n");
226                 for (i = 0; mmod[i].opt != NULL; i++)
227                         printf("\t\t%s\n", mmod[i].opt);
228                 printf("\n      mouse values:\n"
229                        "                Two signed numbers\n"
230                        "--quit to close\n");
231         } else {
232                 printf("        joystick options:\n");
233                 for (i = 0; jmod[i].opt != NULL; i++)
234                         printf("\t\t%s\n", jmod[i].opt);
235                 printf("\n      joystick values:\n"
236                        "                three signed numbers\n"
237                        "--quit to close\n");
238         }
239 }
240
241 int main(int argc, const char *argv[])
242 {
243         const char *filename = NULL;
244         int fd = 0;
245         char buf[BUF_LEN];
246         int cmd_len;
247         char report[8];
248         int to_send = 8;
249         int hold = 0;
250         fd_set rfds;
251         int retval, i;
252
253         if (argc < 3) {
254                 fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
255                         argv[0]);
256                 return 1;
257         }
258
259         if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
260           return 2;
261
262         filename = argv[1];
263
264         if ((fd = open(filename, O_RDWR, 0666)) == -1) {
265                 perror(filename);
266                 return 3;
267         }
268
269         print_options(argv[2][0]);
270
271         while (42) {
272
273                 FD_ZERO(&rfds);
274                 FD_SET(STDIN_FILENO, &rfds);
275                 FD_SET(fd, &rfds);
276
277                 retval = select(fd + 1, &rfds, NULL, NULL, NULL);
278                 if (retval == -1 && errno == EINTR)
279                         continue;
280                 if (retval < 0) {
281                         perror("select()");
282                         return 4;
283                 }
284
285                 if (FD_ISSET(fd, &rfds)) {
286                         cmd_len = read(fd, buf, BUF_LEN - 1);
287                         printf("recv report:");
288                         for (i = 0; i < cmd_len; i++)
289                                 printf(" %02x", buf[i]);
290                         printf("\n");
291                 }
292
293                 if (FD_ISSET(STDIN_FILENO, &rfds)) {
294                         memset(report, 0x0, sizeof(report));
295                         cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1);
296
297                         if (cmd_len == 0)
298                                 break;
299
300                         buf[cmd_len - 1] = '\0';
301                         hold = 0;
302
303                         memset(report, 0x0, sizeof(report));
304                         if (argv[2][0] == 'k')
305                                 to_send = keyboard_fill_report(report, buf, &hold);
306                         else if (argv[2][0] == 'm')
307                                 to_send = mouse_fill_report(report, buf, &hold);
308                         else
309                                 to_send = joystick_fill_report(report, buf, &hold);
310
311                         if (to_send == -1)
312                                 break;
313
314                         if (write(fd, report, to_send) != to_send) {
315                                 perror(filename);
316                                 return 5;
317                         }
318                         if (!hold) {
319                                 memset(report, 0x0, sizeof(report));
320                                 if (write(fd, report, to_send) != to_send) {
321                                         perror(filename);
322                                         return 6;
323                                 }
324                         }
325                 }
326         }
327
328         close(fd);
329         return 0;
330 }