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