summaryrefslogtreecommitdiff
path: root/evdevtest/juice_evtest.c
blob: 0d4dac2a45e07cd532368dc41a25b9b026ede0ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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]: test failed\n");
	else 
		printf("[EVDEV_EVIOCGSUSPENDBLOCK_READ_TEST1]: test passed\n");

	if (0 == lockvalue)  /*default lock value*/
		printf("[EVDEV_TEST2]: test passed\n");
	else 
		printf("[EVDEV_TEST2]: test failed\n");

	/*Set user_wake_lock*/
	if (ioctl(fd, EVIOCSSUSPENDBLOCK, enable_lock)) 
		printf("[EVDEV_EVIOCSSUSPENDBLOCK_SET_TEST3]: test failed\n");
	else 
		printf("[EVDEV_EVIOCSSUSPENDBLOCK_SET_TEST3]: test passed\n");
	
	/*Verfify the lock value equals what we set before*/
	if (ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue)) 
		perror("[EVDEV_READ_user_wake_lock_value]: test failed\n");
	
	if (enable_lock == lockvalue)
		printf("[EVDEV_TEST4]: test passed\n");
	else {
		printf("[EVDEV_TEST4]: test failed\n");
		goto para_test;
	}

	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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]: test passed\n");
	else 
		printf("[EVDEV_CMD_DISABLE_SUSPEND_TEST5]: test failed\n");
	
	/*Disable evdev usr_wake_lock*/
	ioctl(fd, EVIOCSSUSPENDBLOCK, disable_lock);
	ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue);
	if ( disable_lock == lockvalue)
		printf("[EVDEV_TEST6]: test passed\n");
	else 
		printf("[EVDEV_TEST6]: test failed\n");

	/*Judge the system can be suspend resume*/
	ret = suspend_resume_test(DELAY);
	if (0 == ret)
		printf("[EVDEV_ENABLE_SUSPEND_RESUME_TEST7]: test passed\n");
	else 
		printf("[EVDEV_ENABLE_SUSPEND_RESUME_TEST7]: test failed\n");

	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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]: test passed\n");
	else 
		printf("[EVDEV_TEST8]: test failed\n");
	
	ret = ioctl(fd, EVIOCSSUSPENDBLOCK);
	if (!ret) 
		printf("[EVDEV_TEST9]: test passed\n");
	else 
		printf("[EVDEV_TEST9]: test failed\n");

	/* byte parameters test*/
	ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter1);
	if (!ret) 
		printf("[EVDEV_TEST10]: test passed\n");
	else 
		printf("[EVDEV_TEST10]: test failed\n");

	ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter1);
	if (!ret) 
		printf("[EVDEV_TEST11]: test passed\n");
	else 
		printf("[EVDEV_TEST11]: test failed\n");

	/* int parameters test*/
	ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter2);
	if (!ret) 
		printf("[EVDEV_TEST12]: test passed\n");
	else 
		printf("[EVDEV_TEST12]: test failed\n");
	
	ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter2);
	if (!ret) 
		printf("[EVDEV_TEST13]: test passed\n");
	else 
		printf("[EVDEV_TEST13]: test failed\n");

	/* 64bit parameters test*/
	ret = ioctl(fd, EVIOCGSUSPENDBLOCK, &parameter3);
	if (!ret) 
		printf("[EVDEV_TEST14]: test passed\n");
	else 
		printf("[EVDEV_TEST14]: test failed\n");

	ret = ioctl(fd, EVIOCSSUSPENDBLOCK, parameter3);
	if (!ret) 
		printf("[EVDEV_TEST15]: test passed\n");
	else 
		printf("[EVDEV_TEST15]: test failed\n");


/*EVIOCSCLOCKID test*/
	printf("Starting EVIOCSCLOCKID test:\n");
	clk = CLOCK_REALTIME_ALARM;
	ret = ioctl(fd, EVIOCSCLOCKID, &clk);
	if (ret < 0)
		printf("[EVDEV_TEST16]: test passed\n");
	else 
		printf("[EVDEV_TEST16]: test failed\n");
	clk = CLOCK_BOOTTIME;
	ret = ioctl(fd, EVIOCSCLOCKID, &clk);
	if (ret < 0)
		printf("[EVDEV_TEST17]: test passed\n");
	else 
		printf("[EVDEV_TEST17]: test failed\n");

	ret = ioctl(fd, EVIOCSCLOCKID);
	if (ret < 0)
		printf("[EVDEV_TEST18]: test passed\n");
	else 
		printf("[EVDEV_TEST18]: test failed\n");

/*EVIOCSCLOCKID & CLOCK_REALTIME test*/
	clk = CLOCK_REALTIME;
	ret = ioctl(fd, EVIOCSCLOCKID, &clk);
	if (ret < 0)
		printf("[EVDEV_TEST19]: test failed\n");
	else 
		printf("[EVDEV_TEST19]: test passed\n");

	/*Start generate event here!*/
	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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]: test passed\n");
	else 
		printf("[EVDEV_TEST20]: test failed\n");

	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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]: test failed\n");
	else 
		printf("[EVDEV_TEST21]: test passed\n");

	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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]: test passed\n");
	else 
		printf("[EVDEV_TEST22]: test failed\n");

	/*Stop generate input dev event here!*/
	if ((fd_evdev_run = open("/sys/juice_input/run", O_WRONLY)) < 0) {
		perror("[EVDEV_OPEN_SYS_RUN]: test failed\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;
}