Satoshi Noguchi | c534360 | 2014-09-29 02:49:22 -0700 | [diff] [blame^] | 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:c" |
| 38 | |
| 39 | static bool stopRequested; |
| 40 | |
| 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 | } |
| 49 | |
| 50 | int RunF54Test(const char * deviceFile, f54_report_types reportType, bool continuousMode) |
| 51 | { |
| 52 | int rc; |
| 53 | HIDDevice rmidevice; |
| 54 | Display * display; |
| 55 | |
| 56 | if (continuousMode) |
| 57 | { |
| 58 | display = new AnsiConsole(); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | display = new Display(); |
| 63 | } |
| 64 | |
| 65 | display->Clear(); |
| 66 | |
| 67 | rc = rmidevice.Open(deviceFile); |
| 68 | if (rc) |
| 69 | return rc; |
| 70 | |
| 71 | F54Test f54Test(rmidevice, *display); |
| 72 | |
| 73 | rc = f54Test.Prepare(reportType); |
| 74 | if (rc) |
| 75 | return rc; |
| 76 | |
| 77 | stopRequested = false; |
| 78 | |
| 79 | do { |
| 80 | rc = f54Test.Run(); |
| 81 | } |
| 82 | while (continuousMode && !stopRequested); |
| 83 | |
| 84 | rmidevice.Reset(); |
| 85 | |
| 86 | rmidevice.Close(); |
| 87 | |
| 88 | delete display; |
| 89 | |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | void SignalHandler(int p_signame) |
| 94 | { |
| 95 | stopRequested = true; |
| 96 | } |
| 97 | |
| 98 | int main(int argc, char **argv) |
| 99 | { |
| 100 | int rc; |
| 101 | int opt; |
| 102 | int index; |
| 103 | char *deviceName = NULL; |
| 104 | static struct option long_options[] = { |
| 105 | {"help", 0, NULL, 'h'}, |
| 106 | {"device", 1, NULL, 'd'}, |
| 107 | {"report_type", 1, NULL, 'r'}, |
| 108 | {"continuous", 0, NULL, 'c'}, |
| 109 | {0, 0, 0, 0}, |
| 110 | }; |
| 111 | struct dirent * devDirEntry; |
| 112 | DIR * devDir; |
| 113 | f54_report_types reportType = F54_16BIT_IMAGE; |
| 114 | bool continuousMode = false; |
| 115 | |
| 116 | while ((opt = getopt_long(argc, argv, F54TEST_GETOPTS, long_options, &index)) != -1) { |
| 117 | switch (opt) { |
| 118 | case 'h': |
| 119 | printHelp(argv[0]); |
| 120 | return 0; |
| 121 | case 'd': |
| 122 | deviceName = optarg; |
| 123 | break; |
| 124 | case 'r': |
| 125 | reportType = (f54_report_types)strtol(optarg, NULL, 0); |
| 126 | break; |
| 127 | case 'c': |
| 128 | continuousMode = true; |
| 129 | break; |
| 130 | default: |
| 131 | break; |
| 132 | |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (continuousMode) |
| 137 | { |
| 138 | signal(SIGHUP, SignalHandler); |
| 139 | signal(SIGINT, SignalHandler); |
| 140 | signal(SIGTERM, SignalHandler); |
| 141 | } |
| 142 | |
| 143 | if (deviceName) { |
| 144 | rc = RunF54Test(deviceName, reportType, continuousMode); |
| 145 | if (rc) |
| 146 | return rc; |
| 147 | |
| 148 | return rc; |
| 149 | } else { |
| 150 | char rawDevice[PATH_MAX]; |
| 151 | char deviceFile[PATH_MAX]; |
| 152 | bool found = false; |
| 153 | |
| 154 | devDir = opendir("/dev"); |
| 155 | if (!devDir) |
| 156 | return -1; |
| 157 | |
| 158 | while ((devDirEntry = readdir(devDir)) != NULL) { |
| 159 | if (strstr(devDirEntry->d_name, "hidraw")) { |
| 160 | strncpy(rawDevice, devDirEntry->d_name, PATH_MAX); |
| 161 | snprintf(deviceFile, PATH_MAX, "/dev/%s", devDirEntry->d_name); |
| 162 | rc = RunF54Test(deviceFile, reportType, continuousMode); |
| 163 | if (rc != 0) { |
| 164 | continue; |
| 165 | } else { |
| 166 | found = true; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | closedir(devDir); |
| 172 | |
| 173 | if (!found) |
| 174 | return rc; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |