xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_touchpad_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <linux/input.h>
13 #include <signal.h>
14 
15 #define LOG_TAG "echo_touchpad_test"
16 #include "common.h"
17 
18 #define TOUCHPAD_NODE "sys/devices/11050000.i2c/i2c-0/0-0020/cy8ctouchread"
19 
char_to_value(char * ch)20 int char_to_value(char *ch)
21 {
22 	if ((*ch >= 'A') && (*ch <= 'F'))
23 		return 10 + *ch - 'A';
24 	else if ((*ch >= 'a') && (*ch <= 'f'))
25 		return 10 + *ch - 'a';
26 	else if ((*ch >= '0') && (*ch <= '9'))
27 		return *ch - '0';
28 	else
29 		return 0;
30 }
31 
main()32 int main()
33 {
34 	int delay_t = 0,err_code = 0;
35 	struct timeval t1, t2;
36 	char buf[COMMAND_VALUESIZE] = "touchpad_test";
37     char result[COMMAND_VALUESIZE] = RESULT_VERIFY;
38 	int fd = 0, ret = 0, key = 0;
39 	char buff[40], *start = NULL;
40 	int flag_pre = 0, flag_play = 0, flag_nxt = 0;
41 
42 	fd = open(TOUCHPAD_NODE, O_RDONLY);
43 	if (fd <= 0) {
44 		log_err("open touchpad node file failed(%s).\n",
45 				strerror(errno));
46 		err_code = -ENODEV;
47 		goto OUT;
48 	}
49 
50 	gettimeofday(&t1, NULL);
51     while(1) {
52 		lseek(fd, 0, SEEK_SET);
53 		ret = read(fd, buff, sizeof(buff));
54 		if (ret < 0) {
55 			log_err("read touchpad node file failed(%s).\n",
56 					strerror(errno));
57 			err_code = -ENODEV;
58 			break;
59 		}
60 		start = strstr(buff, "0x");
61 		key = char_to_value(start + 2)*16 + char_to_value(start + 3);
62 
63 		if ((key >= 0x40) && (key <= 0x44)) {
64 			log_info("--> <pause/play> key pressed!\n");
65 			flag_play = 1;
66 		} else if ((key >= 0x59) && (key <= 0x5d)) {
67 			log_info("--> <previous> key pressed!\n");
68 			flag_pre = 1;
69 		} else if ((key >= 0x28) && (key <= 0x2c)) {
70 			log_info("--> <next> key pressed!\n");
71 			flag_nxt = 1;
72 		}
73 
74 		if (flag_nxt && flag_play && flag_pre)
75 			break;
76 
77         gettimeofday(&t2, NULL);
78 		delay_t = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
79 		if (delay_t > MANUAL_TEST_TIMEOUT) {
80 			log_warn("timeout(60s)\n");
81 			err_code = -ETIME;
82 			break;
83 		}
84 
85 		usleep(2000);
86     }
87 
88 OUT:
89     if (err_code)
90 		strcpy(result, RESULT_FAIL);
91     send_msg_to_server(buf, result, err_code);
92 
93     return 0;
94 }
95