summaryrefslogtreecommitdiff
path: root/evdevtest/juice_evtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'evdevtest/juice_evtest.c')
-rw-r--r--evdevtest/juice_evtest.c458
1 files changed, 458 insertions, 0 deletions
diff --git a/evdevtest/juice_evtest.c b/evdevtest/juice_evtest.c
new file mode 100644
index 0000000..f421a61
--- /dev/null
+++ b/evdevtest/juice_evtest.c
@@ -0,0 +1,458 @@
+#include <linux/input.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <getopt.h>
+#include <errno.h>
+#include <time.h>
+
+#define DELAY 10
+#define CLOCK_REALTIME_ALARM 8
+#define CLOCK_BOOTTIME_ALARM 9
+
+static int suspend_resume_test(int seconds)
+{
+// struct timespec now, target, latest;
+ int ret;
+ int pm_state_fd;
+ int len, n;
+ char buf[32];
+ timer_t timerid;
+ struct itimerspec its;
+ struct timespec t1, t2;
+
+ if (seconds < 6) {
+ printf("Please input suspend time more than 4 seconds!\n");
+ return -1;
+ }
+
+ ret = timer_create(CLOCK_REALTIME_ALARM, NULL, &timerid);
+ if (-1 == ret) {
+// printf("Create alarm timer failed!\n");
+ return -1;
+ }
+
+ if(clock_gettime(CLOCK_REALTIME_ALARM, &its.it_value))
+ return -1;
+
+ its.it_value.tv_sec += seconds;
+
+ ret = timer_settime(timerid, TIMER_ABSTIME, &its, NULL);
+ if (-1 == ret) {
+// perror("Set alarm timer failed!\n");
+ return -1;
+ }
+
+ ret = clock_gettime(CLOCK_REALTIME, &t1);
+ if (-1 == ret) {
+// perror("Get the suspend start time failed\n");
+ return -1;
+ }
+
+#if 0
+ fd = open("/dev/alarm", O_RDWR);
+ if (fd < 0) {
+ printf("Open /dev/alarm failed!\n");
+ return -1;
+ }
+
+ ret = ioctl(fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_RTC_WAKEUP), &now);
+ if (ret < 0) {
+ printf("Get time failed!\n");
+ close(fd);
+ return -1;
+ }
+
+ target = now;
+ target.tv_sec += seconds;
+
+ ret = ioctl(fd, ANDROID_ALARM_SET(ANDROID_ALARM_RTC_WAKEUP), &target);
+ if (ret < 0) {
+ printf("Set time failed:");
+ if (errno == EBUSY)
+ printf("Android using this alarm ANDROID_ALARM_RTC_WAKEUP!\n");
+ close(fd);
+ return -1;
+ }
+#endif
+
+ /*start suspend the system now*/
+ pm_state_fd = open("/sys/power/state", O_WRONLY);
+ if (pm_state_fd < 0) {
+// printf("Open /sys/power/state failed!\n");
+ return -1;
+ }
+ sprintf(buf, "%s\n", "mem");
+ len = strlen(buf);
+ n = write(pm_state_fd, buf, len);
+ if (n < 0 || n != len) {
+// printf("Write /sys/power/state failed!\n");
+ close(pm_state_fd);
+ return -1;
+ }
+#if 0
+ /*Get the time after the system resume*/
+ ret = ioctl(fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_RTC_WAKEUP), &latest);
+ if (ret < 0) {
+ printf("Get time again failed!\n");
+ close(fd);
+ close(pm_state_fd);
+ return -1;
+ }
+
+ /*judge if the system suspend/resume works well */
+ if (latest.tv_sec - now.tv_sec >= seconds) {
+ close(fd);
+ close(pm_state_fd);
+ return 0;
+ } else {
+ close(fd);
+ close(pm_state_fd);
+ return 1;
+ }
+#endif
+ close(pm_state_fd);
+ /*Maybe need sleep here for several seconds to wait system to suspend itself!!!!*/
+ /*
+ *
+ *
+ */
+ ret = clock_gettime(CLOCK_REALTIME, &t2);
+ if (-1 == ret) {
+// printf("Get the suspend start time failed\n");
+ return -1;
+ }
+
+ if (t2.tv_sec - t1.tv_sec >= seconds)
+ return 0;
+ else
+ return 1;
+}
+
+int main (int argc, char **argv)
+{
+ struct option lopt[] = {
+ { "path", required_argument, NULL, 'p' },
+ { "help", no_argument, NULL, 'h' },
+ { NULL, 0, NULL, 0 },
+ };
+
+ char parameter1=0;
+ int parameter2=0;
+ long long parameter3=0;
+
+ char * shopt = "p:h";
+ char *evdevice = NULL;
+ long enable_lock=1, disable_lock=0, lockvalue = -1;
+ int fd, fd_evdev_run;
+ int c, opti = 0;
+ int ret, len, n;
+ char buf[32];
+ unsigned int clk;
+ struct input_event ev[64];
+ struct timespec tp1, tp2;
+
+ if(getuid()) {
+ perror("Need run this test as root!\n");
+ return -1;
+ }
+
+ while ((c = getopt_long(argc, argv, shopt, lopt, &opti)) != -1 ) {
+ switch (c) {
+ case 'h':
+ fprintf(stdout, "Userage:\n" "juice_evtest -p /dev/input/eventX \n");
+ break;
+ case 'p':
+ evdevice = strdup(optarg);
+ break;
+ default:
+ perror("Warning, unknown option!\n");
+ break;
+ }
+ }
+
+ if (!evdevice) {
+ perror("You need to input the event input device!\n");
+ return -1;
+ }
+
+ /*Open the evdev input device*/
+ if ((fd = open(evdevice, O_RDONLY)) < 0) {
+ perror("Open input device failed, please check it!\n");
+ return -1;
+ }
+
+
+ printf("===========================\n");
+ printf("Starting evdev ioctl cmd EVIOCGSUSPENDBLOCK/EVIOCCSUSPENDBLOCK test:\n");
+ /* Read lock command */
+ if (ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue))
+ printf("[EVDEV EVIOCGSUSPENDBLOCK READ TEST1] FAIL\n");
+ else
+ printf("[EVDEV EVIOCGSUSPENDBLOCK READ TEST1] PASS\n");
+
+ if (0 == lockvalue) /*default lock value*/
+ printf("[EVDEV TEST2] PASS\n");
+ else
+ printf("[EVDEV TEST2] FAIL\n");
+
+ /*Set user_wake_lock*/
+ if (ioctl(fd, EVIOCSSUSPENDBLOCK, enable_lock))
+ printf("[EVDEV EVIOCSSUSPENDBLOCK SET TEST3] FAIL\n");
+ else
+ printf("[EVDEV EVIOCSSUSPENDBLOCK SET TEST3] PASS\n");
+
+ /*Verfify the lock value equals what we set before*/
+ if (ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue))
+ perror("[EVDEV READ user_wake_lock value] FAIL\n");
+
+ if (enable_lock == lockvalue)
+ printf("[EVDEV TEST4] PASS\n");
+ else {
+ printf("[EVDEV TEST4] FAIL\n");
+ goto para_test;
+ }
+
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ perror("Please check input kernel test module!\n");
+ goto para_test;
+ } else {
+ sprintf(buf, "%d\n", 1);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+
+ close(fd_evdev_run);
+ }
+ /*Judge the system can not be suspend*/
+ ret = suspend_resume_test(DELAY);
+ if (1 == ret)
+ printf("[EVDEV CMD DISABLE SUSPEND TEST5] PASS\n");
+ else
+ printf("[EVDEV CMD DISABLE SUSPEND TEST5] FAIL\n");
+
+ /*Disable evdev usr_wake_lock*/
+ ioctl(fd, EVIOCSSUSPENDBLOCK, disable_lock);
+ ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue);
+ if ( disable_lock == lockvalue)
+ printf("[EVDEV TEST6] PASS\n");
+ else
+ printf("[EVDEV TEST6] FAIL\n");
+
+ /*Judge the system can be suspend resume*/
+ ret = suspend_resume_test(DELAY);
+ if (0 == ret)
+ printf("[EVDEV ENABLE SUSPEND/RESUME TEST7] PASS\n");
+ else
+ printf("[EVDEV ENABLE SUSPEND/RESUME TEST7] FAIL\n");
+
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ goto para_test;
+ } else {
+ sprintf(buf, "%d\n", 0);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+
+ close(fd_evdev_run);
+ }
+
+
+para_test:
+ /*The following is the test cases for verifying different parameters*/
+ /* Null parameters test!*/
+ printf("Starting test EVIOCGSUSPENDBLOCK/EVIOCSSUSPENDBLOCK with different parameters:\n");
+ ret = ioctl(fd, EVIOCGSUSPENDBLOCK);
+ if (ret == -1)
+ printf("[EVDEV TEST8] PASS\n");
+ else
+ printf("[EVDEV TEST8] FAIL\n");
+
+ ret = ioctl(fd, EVIOCSSUSPENDBLOCK);
+ if (!ret)
+ printf("[EVDEV TEST9] PASS\n");
+ else
+ printf("[EVDEV TEST9] FAIL\n");
+
+ /* byte parameters test*/
+ ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter1);
+ if (!ret)
+ printf("[EVDEV TEST10] PASS\n");
+ else
+ printf("[EVDEV TEST10] FAIL\n");
+
+ ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter1);
+ if (!ret)
+ printf("[EVDEV TEST11] PASS\n");
+ else
+ printf("[EVDEV TEST11] FAIL\n");
+
+ /* int parameters test*/
+ ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter2);
+ if (!ret)
+ printf("[EVDEV TEST12] PASS\n");
+ else
+ printf("[EVDEV TEST12] FAIL\n");
+
+ ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter2);
+ if (!ret)
+ printf("[EVDEV TEST13] PASS\n");
+ else
+ printf("[EVDEV TEST13] FAIL\n");
+
+ /* 64bit parameters test*/
+ ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter3);
+ if (!ret)
+ printf("[EVDEV TEST14] PASS\n");
+ else
+ printf("[EVDEV TEST14] FAIL\n");
+
+ ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter3);
+ if (!ret)
+ printf("[EVDEV TEST15] PASS\n");
+ else
+ printf("[EVDEV TEST15] FAIL\n");
+
+
+/*EVIOCSCLOCKID test*/
+ printf("Starting EVIOCSCLOCKID test:\n");
+ clk = CLOCK_REALTIME_ALARM;
+ ret = ioctl(fd, EVIOCSCLOCKID, &clk);
+ if (ret < 0)
+ printf("[EVDEV TEST16] PASS\n");
+ else
+ printf("[EVDEV TEST16] FAIL\n");
+ clk = CLOCK_BOOTTIME;
+ ret = ioctl(fd, EVIOCSCLOCKID, &clk);
+ if (ret < 0)
+ printf("[EVDEV TEST17] PASS\n");
+ else
+ printf("[EVDEV TEST17] FAIL\n");
+
+ ret = ioctl(fd, EVIOCSCLOCKID);
+ if (ret < 0)
+ printf("[EVDEV TEST18] PASS\n");
+ else
+ printf("[EVDEV TEST18] FAIL\n");
+
+/*EVIOCSCLOCKID & CLOCK_REALTIME test*/
+ clk = CLOCK_REALTIME;
+ ret = ioctl(fd, EVIOCSCLOCKID, &clk);
+ if (ret < 0)
+ printf("[EVDEV TEST19] FAIL\n");
+ else
+ printf("[EVDEV TEST19] PASS\n");
+
+ /*Start generate event here!*/
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ goto finish;
+ } else {
+ sprintf(buf, "%d\n", 1);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+
+ close(fd_evdev_run);
+ }
+
+ ret = clock_gettime(CLOCK_REALTIME, &tp1);
+ if (ret < 0)
+ perror("Clock MONOTONIC gettime1 FAILED\n");
+
+ /*Get event here!*/
+ ret = read(fd, ev, sizeof(struct input_event));
+ if (ret < (int) sizeof(struct input_event))
+ perror("evtest: error reading\n");
+
+ ret = clock_gettime(CLOCK_REALTIME, &tp2);
+ if (ret < 0)
+ perror("Clock MONOTONIC gettime2 FAILED\n");
+
+ if (tp1.tv_sec <= ev[0].time.tv_sec && ev[0].time.tv_sec <= tp2.tv_sec)
+ printf("[EVDEV TEST20] PASS\n");
+ else
+ printf("[EVDEV TEST20] FAIL\n");
+
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ goto finish;
+ } else {
+ sprintf(buf, "%d\n", 0);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+ close(fd_evdev_run);
+ }
+
+/*EVIOCSCLOCKID & CLOCK_MONOTONIC test*/
+ close(fd);
+ if ((fd = open(evdevice, O_RDONLY)) < 0) {
+ perror("Test is not finished but exit!\n");
+ return -1;
+ }
+
+ clk = CLOCK_MONOTONIC;
+ ret = ioctl(fd, EVIOCSCLOCKID, &clk);
+ if (ret < 0)
+ printf("[EVDEV TEST21] FAIL\n");
+ else
+ printf("[EVDEV TEST21] PASS\n");
+
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ goto finish;
+ } else {
+ sprintf(buf, "%d\n", 1);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+ close(fd_evdev_run);
+ }
+
+ ret = clock_gettime(CLOCK_MONOTONIC, &tp1);
+ if (ret < 0)
+ perror("Clock MONOTONIC gettime1 FAIL\n");
+
+ /*Get event here!*/
+ ret = read(fd, ev, sizeof(struct input_event));
+ if (ret < (int) sizeof(struct input_event))
+ perror("evtest: error reading\n");
+
+ ret = clock_gettime(CLOCK_MONOTONIC, &tp2);
+ if (ret < 0)
+ perror("Clock MONOTONIC gettime2 FAILED\n");
+
+ if (tp1.tv_sec <= ev[0].time.tv_sec && ev[0].time.tv_sec<= tp2.tv_sec)
+ printf("[EVDEV TEST22] PASS\n");
+ else
+ printf("[EVDEV TEST22] FAIL\n");
+
+ /*Stop generate input dev event here!*/
+ if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
+ perror("[EVDEV OPEN SYS RUN] FAIL\n");
+ goto finish;
+ } else {
+ sprintf(buf, "%d\n", 0);
+ len = strlen(buf);
+ n = write(fd_evdev_run, buf, len);
+ if (n < 0 || n != len)
+ perror("Write /sys/juice_input/run failed!\n");
+ close(fd_evdev_run);
+ }
+
+finish:
+ close(fd);
+ printf("===========================\n");
+ return -1;
+}