hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
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
/*
 *  echo_ir_test.c  --  Infrared ray test application
 *
 *  Copyright (c) 2018 Rockchip Electronics Co. Ltd.
 *  Author: chad.ma <chad.ma@rock-chips.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <signal.h>
#include <unistd.h>
 
#define LOG_TAG "IR_test"
#include "common.h"
 
#define IR_INPUT_EVENT "/dev/input/event0"
 
#define IR_TIMEOUT_DOWN    60
#define IR_TIMEOUT_UP      5
 
 
/* TV Remote Controler
 * here accord kernel ir drivers keymap,./driver/media/rc/keymaps/rc-360.c
 * just defien 10 keys.
*/
#define IR_VALID_NUM              10
 
typedef struct IR_CONTRALOR_st {
    char *key_name;
    int key_value;
    int key_press_flag;
} IR_st;
 
 
IR_st gIR_test[IR_VALID_NUM] = {
    {"NUM_0",    KEY_0,    0},
    {"NUM_1",    KEY_1,    0},
    {"NUM_2",    KEY_2,    0},
    {"NUM_3",    KEY_3,    0},
    {"NUM_4",    KEY_4,    0},
    {"NUM_5",    KEY_5,    0},
    {"NUM_6",    KEY_6,    0},
    {"NUM_7",    KEY_7,    0},
    {"NUM_8",    KEY_8,    0},
    {"NUM_9",    KEY_9,    0},
 
};
static int isAllIRKeyPressed(IR_st *key_array)
{
    int i = 0;
 
    if (key_array == NULL)
        return 0;
 
    for (i = 0; i < IR_VALID_NUM; i++) {
        if (key_array[i].key_press_flag)
            continue;
        else
            break;
    }
 
    if (i == IR_VALID_NUM) {
        log_info("########### All IR key had pressed!!! ######### \n");
        return 1;
    } else
        return 0;
}
 
static void dumpKeyPressInfo(IR_st *key_array)
{
    int i = 0;
 
    if (key_array == NULL)
        return;
 
    for (i = 0; i < IR_VALID_NUM; i++) {
        log_info("KEYNAME : %s: \t KEY_press: %d \n", key_array[i].key_name,
                 key_array[i].key_press_flag);
    }
}
 
static char *assemble_info(char *key_info_buf, int buf_size, int key_index)
{
    int error_code = 0;
    char msg[32] = {0};
 
    memset(msg, sizeof(msg), 0);
 
    if (key_info_buf == NULL)
        return NULL;
 
    memset(key_info_buf, buf_size, 0);
 
    snprintf(msg, sizeof(msg), "ir-key name:%s", gIR_test[key_index].key_name);
    snprintf(key_info_buf, buf_size, "x<%s>,<%s>,<%d>", msg, RESULT_KEY_PRESS, error_code);
 
    return key_info_buf;
}
static int save_scan_result(char *result_buf)
{
    int fd = -1;
    int ret = 0;
    char *bin_name = "echo_ir_test";
    char result_filename[COMMAND_VALUESIZE] = {0};
 
    snprintf(result_filename, sizeof(result_filename),
             "%s/%s_result",
             TEST_RESULT_SAVE_PATH,
             bin_name);
 
    fd = open(result_filename, O_CREAT | O_WRONLY   | O_TRUNC);
 
    if (fd < 0) {
        log_err("open %s fail, errno = %d\n", result_filename, errno);
        ret = errno;
    }
 
    assert(strlen(result_buf) <= COMMAND_VALUESIZE);
    int w_len = write(fd, result_buf, strlen(result_buf));
 
    if (w_len <= 0) {
        log_err("Write %s fail, errno = %d\n", result_filename, errno);
        ret = errno;
    }
 
    log_info("\t -------IR_test_result: %s -------\n", result_buf);
    log_info("########## write to %s len %d ##########\n", result_filename, w_len);
 
    close(fd);
 
    return ret;
}
 
static char result[COMMAND_VALUESIZE] = RESULT_FAIL;
 
static int ir_wait_event(int maxfd, fd_set *readfds, int time)
{
    int ret;
    struct timeval timeout;
 
    FD_ZERO(readfds);
    FD_SET(maxfd, readfds);
    timeout.tv_sec = time;
    timeout.tv_usec = 0;
    ret = select(maxfd + 1, readfds, NULL, NULL, &timeout);
 
    switch (ret) {
    case -1:
        return -1;
 
    case 0:
        log_err("select timeout(%ds)\n", time);
        return 1;
 
    default:
        if (FD_ISSET(maxfd, readfds)) {
            FD_CLR (maxfd, readfds);
            return 0;
        }
 
        break;
    }
 
    return -1;
}
 
static int ir_event_read(int fd, struct input_event *buf)
{
    int read_len = 0;
 
    read_len = read(fd, buf, sizeof(*buf));
 
    if (read_len < 0) {
        if ((errno != EINTR) && (errno != EAGAIN))
            return 0;
 
        return -1;
    }
 
    if (buf->type)
        return 1;
 
    return 0;
}
 
//* 信号处理函数,在结束进程前,为按键测试返回一个结果;
static int ir_result_send(int sign_no)
{
    int err_code = 0;
    printf("====================function : %s start =================\n", __func__);
 
    if (!memcmp(result, RESULT_VERIFY, strlen(RESULT_VERIFY)))
        err_code = IR_QUERY_FAIL;
 
    send_msg_to_server("key_test", result, err_code);
 
    printf("====================function : %s finished =================\n", __func__);
    exit(0);
}
 
#define MAX_INPUT_COUNT (4)
int main(int argc, char **argv)
{
    int fd;
    int ret = 0;
    int err_code = 0;
    int time = IR_TIMEOUT_DOWN;
    fd_set rdfds;
    struct input_event key_event;
    int modifier;
    char buf[COMMAND_VALUESIZE] = "ir_test";
    int idx = 0;
    int fds[MAX_INPUT_COUNT] = {0};
    char path[64];
    int fdcount = 0;
    int max_fd = 0;
    int i, k;
    struct timeval sel_timeout_tv;
 
    log_info("key test process start...\n");
    //* 注册信号处理函数
    signal(SIGTERM, (__sighandler_t)ir_result_send);
 
    for (i = 0; i < MAX_INPUT_COUNT; i++) {
        sprintf(path, "/dev/input/event%d", i);
        fd = open(path, O_RDONLY | O_NOCTTY);
 
        if (fd < 0) {
            log_err("open fail:%s\n", strerror(errno));
            continue;
        }
 
        fds[fdcount++] = fd;
    }
 
    if (fdcount < 1) {
        err_code = IR_OPEN_FAIL;
        goto EXIT;
    }
 
    for (i = 0 ; i < fdcount; i++)
        if (max_fd < fds[i])
            max_fd = fds[i];
 
    while (1) {
        FD_ZERO(&rdfds);
        sel_timeout_tv.tv_sec = time;
        sel_timeout_tv.tv_usec = 0;
 
        for (i = 0 ; i < fdcount; i++)
            FD_SET(fds[i], &rdfds);
 
        if (isAllIRKeyPressed(gIR_test))
            break;
 
        ret = select(max_fd + 1, &rdfds, NULL, NULL, &sel_timeout_tv);
        if (ret > 0) {
            k = 0;
 
            while (k < fdcount) {
                int fd = fds[k++];
 
                if (FD_ISSET(fd, &rdfds)) {
                    ret = ir_event_read(fd, &key_event);
 
                    if (ret > 0) {
                        if (key_event.value) {
                            log_info("key(%d) is down\n", key_event.code);
                            time = IR_TIMEOUT_UP;
                        } else {
                            log_info("key(%d) is up\n", key_event.code);
                            time = IR_TIMEOUT_DOWN;
 
                            for (idx = 0; idx < IR_VALID_NUM; idx ++) {
                                if (key_event.code == gIR_test[idx].key_value) {
                                    if (gIR_test[idx].key_press_flag == 0)
                                        gIR_test[idx].key_press_flag = 1;
 
                                    assemble_info(buf, COMMAND_VALUESIZE, idx);
                                    save_scan_result(buf);
                                } else
                                    continue;
                            }
 
                            if (isAllIRKeyPressed(gIR_test)) {
                                //int i =0;
                                log_info(" ======== key test is over ========= \n");
                                strcpy(result, RESULT_VERIFY);
                                break;
                            }
                        }
                    }
                }
            }
        } else if (ret == 0) {
            log_err("wait key event fail, errno=%d\n", errno);
 
            if (idx != IR_VALID_NUM) {
                int i = 0;
 
                for (i  = 0; i < IR_VALID_NUM; ++i)  {
                    if (gIR_test[i].key_press_flag == 0) {
                        strcat(buf, gIR_test[i].key_name);
                        strcat(buf, " ");
                    }
                }
 
                strcat(buf, " NOT PRESS!");
            }
 
            err_code = IR_EVENT_TIMEOUT;
            goto EXIT;
        }
    }
 
    //snprintf(buf, sizeof(buf), "key_code:%d", key_event.code);
    dumpKeyPressInfo(gIR_test);
EXIT:
    sleep(1);
    memset(buf, 0 , sizeof(buf));
 
    if (!err_code)
        strcpy(result, RESULT_VERIFY);
 
    send_msg_to_server(buf, result, err_code);
 
    return err_code;
}