1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or
5f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation; either version 2 of
7f08c3bdfSopenharmony_ci * the License, or (at your option) any later version.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12f08c3bdfSopenharmony_ci * GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci * along with this program; if not, write the Free Software Foundation,
16f08c3bdfSopenharmony_ci * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci /*
20f08c3bdfSopenharmony_ci  *  Create a virtual device (mouse), send events to /dev/uinput
21f08c3bdfSopenharmony_ci  *  and check that the events are well received in /dev/input/eventX
22f08c3bdfSopenharmony_ci  */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include <linux/input.h>
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "input_helper.h"
27f08c3bdfSopenharmony_ci#include "test.h"
28f08c3bdfSopenharmony_ci#include "safe_macros.h"
29f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define NB_TEST 20
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void setup(void);
34f08c3bdfSopenharmony_cistatic void send_events(void);
35f08c3bdfSopenharmony_cistatic int verify_data(struct input_event *iev, int nb);
36f08c3bdfSopenharmony_cistatic int check_events(void);
37f08c3bdfSopenharmony_cistatic void cleanup(void);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic int fd;
40f08c3bdfSopenharmony_cistatic int fd2;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cichar *TCID = "input01";
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ciint main(int ac, char **av)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	int lc;
47f08c3bdfSopenharmony_ci	int pid;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	setup();
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); ++lc) {
54f08c3bdfSopenharmony_ci		pid = tst_fork();
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci		switch (pid) {
57f08c3bdfSopenharmony_ci		case 0:
58f08c3bdfSopenharmony_ci			send_events();
59f08c3bdfSopenharmony_ci			exit(0);
60f08c3bdfSopenharmony_ci		case -1:
61f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
62f08c3bdfSopenharmony_ci		default:
63f08c3bdfSopenharmony_ci			if (check_events())
64f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Wrong data read from eventX");
65f08c3bdfSopenharmony_ci			else
66f08c3bdfSopenharmony_ci				tst_resm(TPASS, "Data received from eventX");
67f08c3bdfSopenharmony_ci		break;
68f08c3bdfSopenharmony_ci		}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci		SAFE_WAITPID(NULL, pid, NULL, 0);
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	cleanup();
74f08c3bdfSopenharmony_ci	tst_exit();
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic void setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	tst_require_root();
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	fd = open_uinput();
82f08c3bdfSopenharmony_ci	setup_mouse_events(fd);
83f08c3bdfSopenharmony_ci	create_device(fd);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	fd2 = open_device();
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic void send_events(void)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	int nb;
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	for (nb = 0; nb < NB_TEST; ++nb) {
93f08c3bdfSopenharmony_ci		send_rel_move(fd, 10, 1);
94f08c3bdfSopenharmony_ci		usleep(1000);
95f08c3bdfSopenharmony_ci	}
96f08c3bdfSopenharmony_ci}
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_cistatic int check_events(void)
99f08c3bdfSopenharmony_ci{
100f08c3bdfSopenharmony_ci	int nb, rd;
101f08c3bdfSopenharmony_ci	unsigned int i;
102f08c3bdfSopenharmony_ci	struct input_event iev[64];
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	nb = 0;
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	while (nb < NB_TEST * 3) {
107f08c3bdfSopenharmony_ci		rd = read(fd2, iev, sizeof(iev));
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci		if (rd < 0)
110f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup, "read()");
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci		if (rd == 0 || rd % sizeof(struct input_event)) {
113f08c3bdfSopenharmony_ci			tst_resm(TINFO, "read() returned unexpected %i", rd);
114f08c3bdfSopenharmony_ci			return 1;
115f08c3bdfSopenharmony_ci		}
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci		for (i = 0; i < rd / sizeof(struct input_event); i++) {
118f08c3bdfSopenharmony_ci			if (verify_data(&iev[i], nb++))
119f08c3bdfSopenharmony_ci				return 1;
120f08c3bdfSopenharmony_ci		}
121f08c3bdfSopenharmony_ci	}
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	return 0;
124f08c3bdfSopenharmony_ci}
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_cistatic int verify_data(struct input_event *iev, int nb)
127f08c3bdfSopenharmony_ci{
128f08c3bdfSopenharmony_ci	if (nb % 3 == 0) {
129f08c3bdfSopenharmony_ci		if (iev->type != EV_REL) {
130f08c3bdfSopenharmony_ci			tst_resm(TINFO,
131f08c3bdfSopenharmony_ci			         "%i: Unexpected event type %i expected %i",
132f08c3bdfSopenharmony_ci			         nb, iev->type, EV_REL);
133f08c3bdfSopenharmony_ci			return 1;
134f08c3bdfSopenharmony_ci		}
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci		if (iev->code != REL_X)
137f08c3bdfSopenharmony_ci			return 1;
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci		if (iev->value != 10)
140f08c3bdfSopenharmony_ci			return 1;
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ci		return 0;
143f08c3bdfSopenharmony_ci	}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	if (nb % 3 == 1) {
146f08c3bdfSopenharmony_ci		if (iev->type != EV_REL) {
147f08c3bdfSopenharmony_ci			tst_resm(TINFO,
148f08c3bdfSopenharmony_ci			         "%i: Unexpected event type %i expected %i",
149f08c3bdfSopenharmony_ci			         nb, iev->type, EV_REL);
150f08c3bdfSopenharmony_ci			return 1;
151f08c3bdfSopenharmony_ci		}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci		if (iev->code != REL_Y)
154f08c3bdfSopenharmony_ci			return 1;
155f08c3bdfSopenharmony_ci
156f08c3bdfSopenharmony_ci		if (iev->value != 1)
157f08c3bdfSopenharmony_ci			return 1;
158f08c3bdfSopenharmony_ci
159f08c3bdfSopenharmony_ci		return 0;
160f08c3bdfSopenharmony_ci	}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci	if (nb % 3 == 2) {
163f08c3bdfSopenharmony_ci		if (iev->type != EV_SYN) {
164f08c3bdfSopenharmony_ci			tst_resm(TINFO,
165f08c3bdfSopenharmony_ci			         "%i: Unexpected event type %i expected %i",
166f08c3bdfSopenharmony_ci			         nb, iev->type, EV_SYN);
167f08c3bdfSopenharmony_ci			return 1;
168f08c3bdfSopenharmony_ci		}
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci		if (iev->code != 0)
171f08c3bdfSopenharmony_ci			return 1;
172f08c3bdfSopenharmony_ci
173f08c3bdfSopenharmony_ci		if (iev->value != 0)
174f08c3bdfSopenharmony_ci			return 1;
175f08c3bdfSopenharmony_ci
176f08c3bdfSopenharmony_ci		return 0;
177f08c3bdfSopenharmony_ci	}
178f08c3bdfSopenharmony_ci	return 1;
179f08c3bdfSopenharmony_ci}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_cistatic void cleanup(void)
182f08c3bdfSopenharmony_ci{
183f08c3bdfSopenharmony_ci	if (fd2 > 0 && close(fd2))
184f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close(fd2)");
185f08c3bdfSopenharmony_ci
186f08c3bdfSopenharmony_ci	destroy_device(fd);
187f08c3bdfSopenharmony_ci}
188