blob: 128d67e795dcb9ef7a2ce81d11ccb3e6db12cec4 [file] [log] [blame]
Andrew Duggan4e811252014-04-03 15:17:57 -07001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <errno.h>
6#include <string.h>
7#include <unistd.h>
8#include <sys/ioctl.h>
9#include <sys/select.h>
10
11#include <linux/types.h>
12#include <linux/input.h>
13#include <linux/hidraw.h>
14#include <signal.h>
15#include <stdlib.h>
16
17#include "hiddevice.h"
18
19static int report_attn = 0;
20static HIDDevice * g_device = NULL;
21
22void print_help()
23{
24 fprintf(stdout, "Commands:\n");
25 fprintf(stdout, "s [0,1,2]: Set RMIMode\n");
26 fprintf(stdout, "r address size: read size bytes from address\n");
27 fprintf(stdout, "w address { values }: write bytes to address\n");
28 fprintf(stdout, "a: Wait for attention\n");
29 fprintf(stdout, "q: quit\n");
30}
31
32int find_token(char * input, char * result, char ** endpp)
33{
34 int i = 0;
35 char * start = input;
36 char * end;
37
38 while (input[i] == ' ') {
39 ++start;
40 ++i;
41 }
42
43 while (input[i] != '\0') {
44 ++i;
45 if (input[i] == ' ') {
46 break;
47 }
48 }
49 end = &input[i];
50
51 if (start == end) {
52 return 0;
53 }
54
55 *endpp = end;
56 strncpy(result, start, end - start);
57 result[end - start + 1] = '\0';
58
59 return 1;
60}
61
62int process(HIDDevice * device, char * input)
63{
64 unsigned char report[256];
65 char token[256];
66 char * start;
67 char * end;
68 int rc;
69
70 memset(token, 0, 256);
71
72 if (input[0] == 's') {
73 start = input + 2;
74 find_token(start, token, &end);
75 int mode = strtol(token, NULL, 0);
76 if (mode >= 0 && mode <= 2) {
77 if (device->SetMode(mode)) {
78 fprintf(stderr, "Set RMI Mode to: %d\n", mode);
79 } else {
80 fprintf(stderr, "Set RMI Mode FAILED!\n");
81 return -1;
82 }
83 }
84 } else if (input[0] == 'r') {
85 start = input + 2;
86 find_token(start, token, &end);
87 start = end + 1;
88 int addr = strtol(token, NULL, 0);
89 find_token(start, token, &end);
90 start = end + 1;
91 int len = strtol(token, NULL, 0);
92 fprintf(stdout, "Address = 0x%02x Length = %d\n", addr, len);
93
94 memset(report, 0, sizeof(report));
95 rc = device->Read(addr, report, len);
96 if (rc < 0)
97 fprintf(stderr, "Failed to read report: %d\n", rc);
98
99 fprintf(stdout, "Data:\n");
100 for (int i = 0; i < len; ++i) {
101 fprintf(stdout, "0x%02X ", report[i]);
102 if (i % 8 == 7) {
103 fprintf(stdout, "\n");
104 }
105 }
106 fprintf(stdout, "\n");
107 } else if (input[0] == 'w') {
108 int index = 0;
109 start = input + 2;
110 find_token(start, token, &end);
111 start = end + 1;
112 int addr = strtol(token, NULL, 0);
113 int len = 0;
114
115 while (find_token(start, token, &end)) {
116 start = end + 1;
117 report[index++] = (unsigned char)strtol(token, NULL, 0);
118 ++len;
119 }
120
121 memset(report, 0, sizeof(report));
122
123 if (device->Write(addr, report, len) < 0) {
124 fprintf(stderr, "Failed to Write Report\n");
125 return -1;
126 }
127 } else if (input[0] == 'a') {
128 report_attn = 1;
129 while(report_attn) {
130 int bytes = 256;
131 device->GetAttentionReport(NULL, NULL, report, &bytes);
132 fprintf(stdout, "Data:\n");
133 for (int i = 0; i < bytes; ++i) {
134 fprintf(stdout, "0x%02X ", report[i]);
135 if (i % 8 == 7) {
136 fprintf(stdout, "\n");
137 }
138 }
139 fprintf(stdout, "\n\n");
140 }
141 } else if (input[0] == 'q') {
142 return 1;
143 } else {
144 print_help();
145 }
146 return 0;
147}
148
149static void cleanup(int status)
150{
151 if (report_attn) {
152 report_attn = 0;
153 if (g_device)
154 g_device->Cancel();
155 } else {
156 exit(0);
157 }
158}
159
160int main(int argc, char ** argv)
161{
162 int rc;
163 struct sigaction sig_cleanup_action;
164
165 memset(&sig_cleanup_action, 0, sizeof(struct sigaction));
166 sig_cleanup_action.sa_handler = cleanup;
167 sig_cleanup_action.sa_flags = SA_RESTART;
168 sigaction(SIGINT, &sig_cleanup_action, NULL);
169
170 HIDDevice device;
171 rc = device.Open(argv[1]);
172 if (rc) {
173 fprintf(stderr, "%s: failed to initialize rmi device (%d)\n", argv[0], rc);
174 return 1;
175 }
176
177 device.ScanPDT();
178 device.QueryBasicProperties();
179 device.PrintProperties();
180
181 g_device = &device;
182
183 fprintf(stdout, "\n");
184 for (;;) {
185 fprintf(stdout, "\n");
186 print_help();
187 char buf[256];
188
189 if (fgets(buf, 256, stdin)) {
190 if (process(&device, buf) == 1)
191 break;
192 }
193 }
194
195 device.Close();
196
197 return 0;
198}