1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * wlan_test.c -- wlan test application
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5*4882a593Smuzhiyun * Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
8*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
9*4882a593Smuzhiyun * You may obtain a copy of the License at
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
14*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
15*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*4882a593Smuzhiyun * See the License for the specific language governing permissions and
17*4882a593Smuzhiyun * limitations under the License.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <stdio.h>
21*4882a593Smuzhiyun #include <errno.h>
22*4882a593Smuzhiyun #include <string.h>
23*4882a593Smuzhiyun #include <fcntl.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include <sys/types.h>
26*4882a593Smuzhiyun #include <sys/stat.h>
27*4882a593Smuzhiyun #include <unistd.h>
28*4882a593Smuzhiyun #include <sys/time.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define LOG_TAG "echo_auto_test"
32*4882a593Smuzhiyun #include "common.h"
33*4882a593Smuzhiyun
pcba_test_result_rw(char * file_name,char * w_buf,char * r_buf,unsigned char rw)34*4882a593Smuzhiyun static int pcba_test_result_rw(char *file_name, char *w_buf, char *r_buf, unsigned char rw)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun int ret = 0;
37*4882a593Smuzhiyun int fd = -1;
38*4882a593Smuzhiyun char pcbatest_result_filename[COMMAND_VALUESIZE] = {0};
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun snprintf(pcbatest_result_filename, sizeof(pcbatest_result_filename),
41*4882a593Smuzhiyun "%s/%s_result\0", TEST_RESULT_SAVE_PATH, file_name);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (rw) {
44*4882a593Smuzhiyun log_info("=================fucntion: %s================\n",__func__);
45*4882a593Smuzhiyun log_info("write result ** pcbatest_result_filename is :%s\n",pcbatest_result_filename);
46*4882a593Smuzhiyun if(w_buf[0]!='\0'){
47*4882a593Smuzhiyun fd = open(pcbatest_result_filename, O_CREAT | O_WRONLY | O_TRUNC);
48*4882a593Smuzhiyun if (fd < 0)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun log_err("open %s fail, errno = %d\n", pcbatest_result_filename, errno);
51*4882a593Smuzhiyun return -1;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun write(fd, w_buf, COMMAND_VALUESIZE);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun else {
56*4882a593Smuzhiyun log_info("w_buf is NUll, do nothing\n");
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun } else {
59*4882a593Smuzhiyun fd = open(pcbatest_result_filename, O_RDONLY);
60*4882a593Smuzhiyun if (fd < 0) {
61*4882a593Smuzhiyun log_info("can't open %s, errno = %d\n", pcbatest_result_filename, errno);
62*4882a593Smuzhiyun return 1;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun ret = read(fd, r_buf, COMMAND_VALUESIZE);
65*4882a593Smuzhiyun if (ret <= 0) {
66*4882a593Smuzhiyun log_err("read %s fail, errno = %d\n", pcbatest_result_filename, errno);
67*4882a593Smuzhiyun ret = -1;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun log_info("\n**********Read file: %s; Result is %s\t*****\n",pcbatest_result_filename,r_buf);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun close(fd);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return ret;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
run_cmd_to_shell_duplex(char * cmd,char * w_buf,char * r_buf,char * match_str)76*4882a593Smuzhiyun static int run_cmd_to_shell_duplex(char *cmd, char *w_buf, char *r_buf, char *match_str)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun int ret = 0;
79*4882a593Smuzhiyun int read_len = 0;
80*4882a593Smuzhiyun FILE *fp;
81*4882a593Smuzhiyun char buf[COMMAND_VALUESIZE] = {0};
82*4882a593Smuzhiyun char cmd_msg[COMMAND_VALUESIZE] = {0};
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun snprintf(cmd_msg, sizeof(cmd_msg),"%s %s\0", cmd, w_buf);
85*4882a593Smuzhiyun log_info("========cmd_msg is : %s\n",cmd_msg);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun fp = popen(cmd_msg, "r");
88*4882a593Smuzhiyun if (fp == NULL) {
89*4882a593Smuzhiyun log_err("run_cmd_to_shell_duplex dpopen fail, errno=%d\n", errno);
90*4882a593Smuzhiyun return -1;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if(match_str == NULL){
94*4882a593Smuzhiyun read_len = fread(buf, sizeof(char), sizeof(buf), fp);
95*4882a593Smuzhiyun if (read_len <= 0)
96*4882a593Smuzhiyun ret = -1;
97*4882a593Smuzhiyun } else {
98*4882a593Smuzhiyun while (fgets(buf, sizeof(buf), fp)) {
99*4882a593Smuzhiyun if (strstr(buf, match_str)) {
100*4882a593Smuzhiyun log_info("====================================\n");
101*4882a593Smuzhiyun log_info("strstr(buf, match_str) is : %s\n",buf);
102*4882a593Smuzhiyun strcpy(r_buf, buf);
103*4882a593Smuzhiyun break; //* �����
104*4882a593Smuzhiyun } else {
105*4882a593Smuzhiyun puts(buf);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun EXIT:
111*4882a593Smuzhiyun pclose(fp);
112*4882a593Smuzhiyun return ret;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
pcba_test_item_process(char * pcba_test_filename,char * args)115*4882a593Smuzhiyun static int pcba_test_item_process(char *pcba_test_filename, char *args)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun int ret = 0;
118*4882a593Smuzhiyun char buf[COMMAND_VALUESIZE] = {0};
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun chmod(pcba_test_filename, S_IRUSR|S_IWUSR|S_IXUSR);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun ret = run_cmd_to_shell_duplex(pcba_test_filename, args, buf, TESTITEM_SEND_HEAD);
123*4882a593Smuzhiyun if (ret) {
124*4882a593Smuzhiyun log_err("run_cmd_to_shell_duplex fail, ret=%d \n", ret);
125*4882a593Smuzhiyun return TEST_FORK_ERR;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun log_info("pcba_test_result_rw buf is: %s\n", buf);
129*4882a593Smuzhiyun ret = pcba_test_result_rw(pcba_test_filename, buf, NULL, 1);
130*4882a593Smuzhiyun if (ret)
131*4882a593Smuzhiyun return SAVE_RESULE_ERR;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun return ret;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
main(int argc,char * argv[])136*4882a593Smuzhiyun int main(int argc, char *argv[])
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun int i = 0;
139*4882a593Smuzhiyun int ret = 0;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (argc > 3 || argc < 2) {
142*4882a593Smuzhiyun printf("[usage]:\n\techo_auto_test program_name args\n");
143*4882a593Smuzhiyun printf("[example]:\n\techo_auto_test sendmsg \"hello world!\"\n");
144*4882a593Smuzhiyun return -EINVAL;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (argc == 2)
148*4882a593Smuzhiyun ret = pcba_test_item_process(argv[1], "");
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun ret = pcba_test_item_process(argv[1], argv[2]);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun return ret;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155