1 /*
2 * rotary_test.c -- rotary test application
3 *
4 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5 * Author: Bin Yang <yangbin@rock-chips.com>
6 * Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <linux/input.h>
26 #include <signal.h>
27 #include <unistd.h>
28
29 #define LOG_TAG "rotary_test"
30 #include "common.h"
31
32 #define ROTARY_INPUT_EVENT "/dev/input/event1"
33
34 #define ROTARY_TIMEOUT_ROTATE 60 //60s��ʱ
35
36 static char result[COMMAND_VALUESIZE] = RESULT_FAIL;
37
rotary_wait_event(int maxfd,fd_set * readfds,int time)38 static int rotary_wait_event(int maxfd, fd_set *readfds, int time)
39 {
40 int ret;
41 struct timeval timeout;
42
43 FD_ZERO(readfds);
44 FD_SET(maxfd, readfds);
45 timeout.tv_sec = time;
46 timeout.tv_usec = 0;
47 ret = select(maxfd + 1, readfds, NULL, NULL, &timeout);
48 switch (ret) {
49 case -1:
50 return -1;
51 case 0:
52 log_err("select timeout(%ds)\n", time);
53 return 1;
54 default:
55 if (FD_ISSET(maxfd, readfds)) {
56 FD_CLR (maxfd, readfds);
57 return 0;
58 }
59 break;
60 }
61
62 return -1;
63 }
64
rotary_event_read(int fd,struct input_event * buf)65 static int rotary_event_read(int fd, struct input_event *buf)
66 {
67 int read_len = 0;
68
69 read_len = read(fd, buf, sizeof(*buf));
70 if (read_len < 0) {
71 if ((errno != EINTR) && (errno != EAGAIN))
72 return 0;
73 return -1;
74 }
75
76 if (buf->type)
77 return 1;
78
79 return 0;
80 }
81
82 //* �źŴ��������ڽ�������ǰ��Ϊrotary���Է���һ�������
rotary_result_send(int sign_no)83 static int rotary_result_send(int sign_no)
84 {
85 int err_code =0;
86 printf("====================function : %s start =================\n",__func__);
87 if(!memcmp(result,RESULT_FAIL,strlen(RESULT_FAIL))){
88 err_code = ROTARY_QUERY_FAIL;
89 }
90 send_msg_to_server("rotary_test", result, err_code);
91
92 printf("====================function : %s finished =================\n",__func__);
93 exit(0);
94 }
95
main(int argc,char ** argv)96 int main(int argc, char **argv)
97 {
98 int fd;
99 int ret = 0;
100 int err_code = 0;
101 int time = ROTARY_TIMEOUT_ROTATE;
102 fd_set rdfds;
103 struct input_event rotary_event;
104 unsigned short clockwise=0,anticlockwise=0; //��־��ת����
105 char buf[COMMAND_VALUESIZE] = {0};
106 //char result[COMMAND_VALUESIZE] = RESULT_VERIFY;
107
108 log_info("rotary test process start...\n");
109
110 //* ע���źŴ�����
111 signal(SIGTERM,(__sighandler_t)rotary_result_send);
112
113 fd = open(ROTARY_INPUT_EVENT, O_RDONLY | O_NOCTTY);
114 if (fd < 0) {
115 log_err("open fail:%s\n", strerror(errno));
116 err_code = ROTARY_OPEN_FAIL;
117 goto EXIT;
118 }
119
120 while (1&&!(clockwise&&anticlockwise)) {
121 if (rotary_wait_event(fd, &rdfds, time) == 0) {
122 ret = rotary_event_read(fd, &rotary_event);
123 if (ret > 0) {
124 if (1==rotary_event.value) {
125 log_info("rotary(%d) is clockwise\n", rotary_event.code);
126 time = ROTARY_TIMEOUT_ROTATE;
127 clockwise = 0x1;
128 } else if(-1 == rotary_event.value){
129 log_info("rotary(%d) is anticlockwise\n", rotary_event.code);
130 time = ROTARY_TIMEOUT_ROTATE;
131 anticlockwise = 0x1;
132 }
133 }
134 } else {
135 log_err("wait rotary event fail, errno=%d\n", errno);
136 err_code = ROTARY_EVENT_TIMEOUT;
137 goto EXIT;
138 }
139 }
140 snprintf(buf, sizeof(buf), "rotary_code:%d", rotary_event.code);
141
142 EXIT:
143 if (!err_code)
144 strcpy(result, RESULT_VERIFY);
145 send_msg_to_server(buf, result, err_code);
146
147 return err_code;
148 }
149