1 /*
2 * Copyright (C) 2014 Satoshi Noguchi
3 * Copyright (C) 2014 Synaptics Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <dirent.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <string>
29 #include <sstream>
30 #include <stdlib.h>
31 #include <signal.h>
32
33 #include "hiddevice.h"
34 #include "f54test.h"
35 #include "display.h"
36
37 #define F54TEST_GETOPTS "hd:r:cnt:"
38
39 static bool stopRequested;
40
printHelp(const char * prog_name)41 void printHelp(const char *prog_name)
42 {
43 fprintf(stdout, "Usage: %s [OPTIONS]\n", prog_name);
44 fprintf(stdout, "\t-h, --help\tPrint this message\n");
45 fprintf(stdout, "\t-d, --device\thidraw device file associated with the device being tested.\n");
46 fprintf(stdout, "\t-r, --report_type\tReport type.\n");
47 fprintf(stdout, "\t-c, --continuous\tContinuous mode.\n");
48 fprintf(stdout, "\t-n, --no_reset\tDo not reset after the report.\n");
49 fprintf(stdout, "\t-t, --device-type\t\t\tFilter by device type [touchpad or touchscreen].\n");
50 }
51
RunF54Test(RMIDevice & rmidevice,f54_report_types reportType,bool continuousMode,bool noReset)52 int RunF54Test(RMIDevice & rmidevice, f54_report_types reportType, bool continuousMode, bool noReset)
53 {
54 int rc;
55 Display * display;
56
57 if (continuousMode)
58 {
59 display = new AnsiConsole();
60 }
61 else
62 {
63 display = new Display();
64 }
65
66 display->Clear();
67
68 F54Test f54Test(rmidevice, *display);
69
70 rc = f54Test.Prepare(reportType);
71 if (rc)
72 return rc;
73
74 stopRequested = false;
75
76 do {
77 rc = f54Test.Run();
78 }
79 while (continuousMode && !stopRequested);
80
81 if (!noReset)
82 rmidevice.Reset();
83
84 delete display;
85
86 return rc;
87 }
88
SignalHandler(int p_signame)89 void SignalHandler(int p_signame)
90 {
91 stopRequested = true;
92 }
93
main(int argc,char ** argv)94 int main(int argc, char **argv)
95 {
96 int rc = 0;
97 int opt;
98 int index;
99 char *deviceName = NULL;
100 static struct option long_options[] = {
101 {"help", 0, NULL, 'h'},
102 {"device", 1, NULL, 'd'},
103 {"report_type", 1, NULL, 'r'},
104 {"continuous", 0, NULL, 'c'},
105 {"no_reset", 0, NULL, 'n'},
106 {"device-type", 1, NULL, 't'},
107 {0, 0, 0, 0},
108 };
109 f54_report_types reportType = F54_16BIT_IMAGE;
110 bool continuousMode = false;
111 bool noReset = false;
112 HIDDevice device;
113 enum RMIDeviceType deviceType = RMI_DEVICE_TYPE_ANY;
114
115 while ((opt = getopt_long(argc, argv, F54TEST_GETOPTS, long_options, &index)) != -1) {
116 switch (opt) {
117 case 'h':
118 printHelp(argv[0]);
119 return 0;
120 case 'd':
121 deviceName = optarg;
122 break;
123 case 'r':
124 reportType = (f54_report_types)strtol(optarg, NULL, 0);
125 break;
126 case 'c':
127 continuousMode = true;
128 break;
129 case 'n':
130 noReset = true;
131 break;
132 case 't':
133 if (!strcasecmp(optarg, "touchpad"))
134 deviceType = RMI_DEVICE_TYPE_TOUCHPAD;
135 else if (!strcasecmp(optarg, "touchscreen"))
136 deviceType = RMI_DEVICE_TYPE_TOUCHSCREEN;
137 break;
138 default:
139 break;
140
141 }
142 }
143
144 if (continuousMode)
145 {
146 signal(SIGHUP, SignalHandler);
147 signal(SIGINT, SignalHandler);
148 signal(SIGTERM, SignalHandler);
149 }
150
151 if (deviceName) {
152 rc = device.Open(deviceName);
153 if (rc) {
154 fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno,
155 strerror(errno));
156 return 1;
157 }
158 } else {
159 if (!device.FindDevice(deviceType))
160 return 1;
161 }
162
163 return RunF54Test(device, reportType, continuousMode, noReset);
164 }
165