annotate testugen2.c @ 35:fed32b382de2

Tidy up, hide details behind macros to make it more obvious what we talk to do do things. Convert constants to my preferred format.
author darius
date Tue, 23 Oct 2007 10:54:01 +0930
parents d5a265299a4b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
1 #include <sys/types.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
2 #include <sys/ioctl.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
3 #include <dev/usb/usb.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
4 #include <sys/errno.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
5 #include <sys/uio.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
6 #include <unistd.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
7 #include <fcntl.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
8 #include <stdio.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
9 #include <string.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
10 #include <stdlib.h>
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
11
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
12 /* Return codes for OWFirst()/OWNext() */
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
13 #define OW_BADWIRE -3
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
14 #define OW_BADCRC -2
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
15 #define OW_NOPRESENCE -1
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
16 #define OW_NOMODULES 0
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
17 #define OW_FOUND 1
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
18
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
19 void docmd(int fd, uint8_t *buffer, int rlen, int wlen);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
20
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
21 int
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
22 main(int argc, char **argv) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
23 int endptfd, i;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
24 char *endptname = "/dev/ugen0.2";
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
25 uint8_t buffer[9];
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
26
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
27 if (argc < 2) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
28 fprintf(stderr,
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
29 "Bad usage:\n"
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
30 "\t%s cmd\n", argv[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
31 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
32 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
33
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
34 if ((endptfd = open(endptname, O_RDWR)) == -1) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
35 fprintf(stderr, "Unable to open %s: %s\n", endptname, strerror(errno));
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
36 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
37 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
38
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
39 i = 1;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
40 if (ioctl(endptfd, USB_SET_SHORT_XFER, &i) == -1) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
41 fprintf(stderr, "Unable to set short xfer on end point 1: %s\n", strerror(errno));
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
42 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
43 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
44
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
45 bzero(buffer, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
46
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
47 if (!strcmp(argv[1], "reset")) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
48 buffer[0] = 0x00;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
49 docmd(endptfd, buffer, 1, 1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
50 printf("OWTouchReset() reported %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
51 } else if (!strcmp(argv[1], "first")) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
52 buffer[0] = 0x01;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
53 docmd(endptfd, buffer, 1, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
54 printf("OWFirst() = %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
55 if (buffer[0] == OW_FOUND)
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
56 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
57 buffer[1], buffer[2], buffer[3], buffer[4],
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
58 buffer[5], buffer[6], buffer[7], buffer[8]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
59 } else if (!strcmp(argv[1], "next")) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
60 if (scanf(argv[2], "%hh:%hh:%hh:%hh:%hh:%hh:%hh:%hh", &buffer[1], &buffer[2], &buffer[3],
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
61 &buffer[4], &buffer[5], &buffer[6], &buffer[7], &buffer[8]) != 8) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
62 fprintf(stderr, "Unable to parse ROM ID\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
63 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
64 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
65 buffer[0] = 0x02;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
66 fprintf(stderr, "About to OWNext()\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
67 docmd(endptfd, buffer, 9, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
68 fprintf(stderr, "OWNext() = %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
69 if (buffer[0] == OW_FOUND)
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
70 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
71 buffer[1], buffer[2], buffer[3], buffer[4],
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
72 buffer[5], buffer[6], buffer[7], buffer[8]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
73
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
74 } else if (!strcmp(argv[1], "scan")) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
75 buffer[0] = 0x00;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
76 fprintf(stderr, "About to reset\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
77 docmd(endptfd, buffer, 1, 1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
78 printf("OWReset() reported %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
79 if (buffer[0] != 0)
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
80 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
81
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
82 bzero(buffer, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
83 buffer[0] = 0x01;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
84 fprintf(stderr, "About to OWFirst()\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
85 docmd(endptfd, buffer, 1, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
86 printf("OWFirst() = %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
87
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
88 do {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
89 switch (buffer[0]) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
90 case OW_BADWIRE:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
91 printf("Bad wiring, either bus is held low, or a presence pulse was detected but no module found\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
92 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
93
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
94 case OW_NOPRESENCE:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
95 printf("No presence pulse detected\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
96 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
97
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
98 case OW_BADCRC:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
99 printf("Bad CRC\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
100 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
101
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
102 case OW_NOMODULES:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
103 case OW_FOUND:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
104 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
105
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
106 default:
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
107 printf("Unknown return code %d\n", buffer[0]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
108 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
109 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
110
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
111 if (buffer[0] != OW_FOUND) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
112 printf("No more modules\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
113 break;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
114 } else {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
115 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
116 buffer[1], buffer[2], buffer[3], buffer[4],
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
117 buffer[5], buffer[6], buffer[7], buffer[8]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
118 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
119
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
120 buffer[0] = 0x02;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
121 fprintf(stderr, "About to OWNext()\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
122 docmd(endptfd, buffer, 9, 9);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
123 } while(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
124 } else {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
125 printf("Unknown command: %s\n", argv[1]);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
126 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
127 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
128
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
129
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
130 exit(0);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
131 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
132
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
133 void
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
134 docmd(int fd, uint8_t *buffer, int wlen, int rlen) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
135 char blah[20];
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
136 int i;
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
137
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
138 fprintf(stderr, "About to write\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
139 gets(blah);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
140
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
141 if ((i = write(fd, buffer, wlen)) == -1) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
142 fprintf(stderr, "Unable to write end point: %s\n", strerror(errno));
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
143 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
144 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
145 if (i != wlen)
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
146 printf("Warning, only wrote %d of %d bytes\n", i, wlen);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
147
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
148 fprintf(stderr, "About to read\n");
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
149 gets(blah);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
150
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
151 if ((i = read(fd, buffer, rlen)) == -1) {
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
152 fprintf(stderr, "Unable to read from end point: %s\n", strerror(errno));
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
153 exit(1);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
154 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
155 if (i != rlen)
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
156 printf("Warning, only read %d of %d bytes\n", i, wlen);
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
157 }
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
158
d5a265299a4b Test programs to talk to the hardware from a PC
darius
parents:
diff changeset
159