4 #include <linux/types.h>
5 #include <linux/hiddev.h>
12 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16 unsigned char data[64];
24 static int is_printable(const unsigned char c)
26 return c >= 0x20 && c < 0x80;
29 static int datalen(const unsigned char *data)
33 for (i = 0, len = 0; i < 64; i++)
40 static void print_hex(const unsigned char *data, int len)
44 for (i = 0; i < len; i++)
45 printf("0x%02x ", data[i]);
50 static void print_ascii(const unsigned char *data, int len)
54 for (i = 0; i < len; i++)
55 printf("%c", is_printable(data[i]) ? data[i] : '.');
60 int send_msg(const struct msg *msg, int fd, int usage_code)
64 if (msg->direction != OUT) {
65 printf("Message direction is not OUT\n");
71 print_hex(msg->data + 1, datalen(msg->data) - 1);
72 print_ascii(msg->data + 1, datalen(msg->data) - 1);
74 ret = hiddev_write(msg->data, fd, usage_code);
81 int read_and_verify(struct msg *msg, int fd)
83 unsigned char buf[64];
87 ret = hiddev_read(buf + offset, sizeof(buf) - offset, fd);
95 memcpy(msg->data, buf, sizeof(buf));
96 printf("Got data %d: ", datalen(buf));
97 // print_hex(buf, datalen(buf));
98 print_ascii(buf, datalen(buf));
103 int read_msgs(int fd)
107 read_and_verify(&msg, fd);
108 if (datalen(msg.data) <= 36)
115 #define SET_FIRST_BYTE(byte) \
117 memset(&msg.data, 0, sizeof(msg.data)); \
118 msg.data[4] = (byte); \
122 #define SET_BYTE(idx, byte) \
124 msg.data[(idx) + 4] = (byte); \
128 #define SET_BYTES(byte) msg.data[j++] = (byte)
130 int send_pattern(int fd, int uc, unsigned char byte1, unsigned char byte2)
137 SET_FIRST_BYTE(0x01);
139 send_msg(&msg, fd, uc);
144 send_msg(&msg, fd, uc);
149 send_msg(&msg, fd, uc);
152 SET_FIRST_BYTE(0x02);
155 send_msg(&msg, fd, uc);
160 send_msg(&msg, fd, uc);
166 int communicate(int fd, int uc)
173 SET_FIRST_BYTE(0x01);
175 send_msg(&msg, fd, uc);
179 send_msg(&msg, fd, uc);
183 for (i = 0; i < 6; i++) {
185 send_msg(&msg, fd, uc);
190 send_msg(&msg, fd, uc);
195 send_msg(&msg, fd, uc);
198 send_pattern(fd, uc, 'R', 'A');
199 send_pattern(fd, uc, 'R', 'C');
200 send_pattern(fd, uc, 'R', 'D');
201 send_pattern(fd, uc, 'R', 'G');
202 send_pattern(fd, uc, 'R', 'I');
203 send_pattern(fd, uc, 'R', 'M');
204 send_pattern(fd, uc, 'R', 'P');
205 send_pattern(fd, uc, 'R', 'R');
206 send_pattern(fd, uc, 'R', 'S');
207 send_pattern(fd, uc, 'R', 'T');
208 send_pattern(fd, uc, 'R', 'U');
209 send_pattern(fd, uc, 'R', 'V');
210 send_pattern(fd, uc, 'R', 'W');
211 send_pattern(fd, uc, 'R', 'X');
212 send_pattern(fd, uc, 'W', 'K');
215 SET_FIRST_BYTE(0x08);
224 send_msg(&msg, fd, uc);
228 SET_FIRST_BYTE(0x02);
231 send_msg(&msg, fd, uc);
236 send_msg(&msg, fd, uc);
241 send_msg(&msg, fd, uc);
246 send_msg(&msg, fd, uc);
251 send_msg(&msg, fd, uc);
254 send_pattern(fd, uc, 'R', 'Z');
257 SET_FIRST_BYTE(0x01);
259 send_msg(&msg, fd, uc);
264 send_msg(&msg, fd, uc);
269 send_msg(&msg, fd, uc);
274 send_msg(&msg, fd, uc);
278 send_msg(&msg, fd, uc);
283 send_msg(&msg, fd, uc);
287 send_msg(&msg, fd, uc);
291 send_msg(&msg, fd, uc);
296 send_msg(&msg, fd, uc);
297 read_and_verify(&in, fd);
298 } while (datalen(in.data) > 45);
303 int main(int argc, char *argv[])
307 fd = hiddev_open(argv[1], &usage_code);
311 communicate(fd, usage_code);