summaryrefslogtreecommitdiff
path: root/evdevtest/juice_evtest.c
blob: 652f2da09b8d126a8cbf8b5e399583efdd400306 (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
#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>
#include <signal.h>

#define debug 0
#define JUICE_DEBUG() if (debug) \
		do \
			printf("##Juice debug in %s and line ##\n", __FUNCTION__, __LINE__); \
		while(0)

#define JUICE_ERROR(X) \
		do \
			printf("##Juice error: %s in %s at line %d ##\n", X, __FUNCTION__, __LINE__); \
		while(0)

#define DELAY 10
#define CLOCK_REALTIME_ALARM        8
#define CLOCK_BOOTTIME_ALARM        9

void sigalarm(int signumber)
{
	/*We just want to test the suspend and resuem
	 * nothing need to be done here!
	 */
}

static int suspend_resume_test(const int seconds)
{
	int ret;
	timer_t timerid;
	struct itimerspec its;	
	struct timespec t1, t2;
	struct sigevent se;
	struct sigaction act;
	sigset_t sigmask;
	int signum = SIGRTMAX;

	/* Set up signal handler */
	sigfillset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = sigalarm;
	sigaction(signum, &act, NULL);

	/* Set up timer */
	memset(&se, 0, sizeof(se));
	se.sigev_notify = SIGEV_SIGNAL;
	se.sigev_signo = signum;
	se.sigev_value.sival_int = 0;

	if (seconds < 6) {
		JUICE_ERROR("Input suspend time less than 6 seconds");
		return -1;
	}

	ret = timer_create(CLOCK_REALTIME_ALARM, &se, &timerid);
	if (ret < 0) {
		JUICE_ERROR("Create alarm timer failed");
		return -1;
	}
	
	memset(&its, 0, sizeof(struct itimerspec));
	ret = clock_gettime(CLOCK_REALTIME_ALARM, &its.it_value);
	if (ret < 0) {
		JUICE_ERROR("Get time failed");
		return -1;
	}

	its.it_value.tv_sec += seconds;

	ret = timer_settime(timerid, TIMER_ABSTIME, &its, NULL);
	if (ret < 0) {
		JUICE_ERROR("Set alarm timer failed");
		return -1;
	}

	/*record the suspend start time*/
	ret = clock_gettime(CLOCK_REALTIME, &t1);
	if (ret < 0) {
		JUICE_ERROR("Get start time of suspend failed");
		return -1;
	}

	/*start suspend the system now*/
	ret = system("echo mem > /sys/power/state");
	if (ret < 0) {
		JUICE_ERROR("echo mem to power state failed");
		return -1;
	}

	/*record the system resume time*/
	ret = clock_gettime(CLOCK_REALTIME, &t2);
	if (ret < 0) {
		JUICE_ERROR("Get the suspend end time failed");
		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, c, opti = 0, ret;
	unsigned int clk;
	struct input_event ev[64];
	struct timespec tp1, tp2;

	if(getuid()) {
		JUICE_ERROR("Not run this test as root");
		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:
				JUICE_ERROR("Warning, unknown option!");
				break;
		}
	}

	if (!evdevice) {
		JUICE_ERROR("No input device");
		return -1;
	}

	/*Open the evdev input device*/
	if ((fd = open(evdevice, O_RDONLY)) < 0) {
		JUICE_ERROR("Open input device failed");
		return -1;
	}


	printf("===========================\n");
	printf("Starting input 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*/
	ioctl(fd, EVIOCGSUSPENDBLOCK, &lockvalue);
	if (enable_lock == lockvalue)
		printf("[EVDEV_TEST4]: test passed\n");
	else {
		printf("[EVDEV_TEST4]: test failed\n");
		goto para_test;
	}

	/*Start generating input event*/
	ret = system("echo 1 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Enable generate input event failed");
		goto para_test;
	}

	/*Judge the system can not 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");

	/*Stop generateing input event*/
	ret = system("echo 0 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Disable generate input event failed");
		goto para_test;
	}



	/*The followings are the test cases for verifying different parameters*/
para_test:
	/* Null parameters test!*/
	printf("Starting test EVIOCGSUSPENDBLOCK/EVIOCSSUSPENDBLOCK with different parameters:\n");
	ret = ioctl(fd, EVIOCGSUSPENDBLOCK, NULL);
	if (ret < 0)
		printf("[EVDEV_TEST8]: test passed\n");
	else
		printf("[EVDEV_TEST8]: test failed\n");

	ret = ioctl(fd, EVIOCSSUSPENDBLOCK, NULL);
	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");



/*The followings are testcases for ioctl cmd EVIOCSCLOCKID*/
	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, NULL);
	if (ret < 0)
		printf("[EVDEV_TEST18]: test passed\n");
	else
		printf("[EVDEV_TEST18]: test failed\n");


	/*EVIOCSCLOCKID with CLOCK_REALTIME test start*/
	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*/
	ret = system("echo 1 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Enable generating input event failed");
		goto finish;
	}

	ret = clock_gettime(CLOCK_REALTIME, &tp1);
	if (ret < 0)
		JUICE_ERROR("Get time with clock id REALTIME failed");

	/*Get event here!*/
	ret = read(fd, ev, sizeof(struct input_event));
	if (ret < (int) sizeof(struct input_event)) 
		JUICE_ERROR("Reading input event failed");

	ret = clock_gettime(CLOCK_REALTIME, &tp2);
	if (ret < 0)
		JUICE_ERROR("Get time with clock id REALTIME failed");

	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");

	/*Stop generate input event*/
	ret = system("echo 0 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Disable generating input event failed");
		goto finish;
	}
	/*EVIOCSCLOCKID with CLOCK_REALTIME test end*/


	/*EVIOCSCLOCKID with CLOCK_MONOTONIC test start*/
	close(fd);
	if ((fd = open(evdevice, O_RDONLY)) < 0) {
		JUICE_ERROR("Open input device failed");
		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");

	ret = system("echo 1 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Enable generating input event failed");
		goto finish;
	}

	ret = clock_gettime(CLOCK_MONOTONIC, &tp1);
	if (ret < 0)
		JUICE_ERROR("Get time with clock id MONOTONIC failed");

	/*Get event here!*/
	ret = read(fd, ev, sizeof(struct input_event));
	if (ret < (int) sizeof(struct input_event))
		JUICE_ERROR("Reading input event failed");

	ret = clock_gettime(CLOCK_MONOTONIC, &tp2);
	if (ret < 0)
		JUICE_ERROR("Get time with clock id MONOTONIC failed");

	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 event*/
	ret = system("echo 0 > /sys/juice_input/run");
	if (ret < 0) {
		JUICE_ERROR("Disable generating input event failed");
		goto finish;
	}
	/*EVIOCSCLOCKID with CLOCK_MONOTONIC test end*/

finish:
	close(fd);
	printf("===========================\n");
	return 0;
}