blob: f4626e2f1feb48808bff469aa88629ee63412c93 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 *
3 * @file amt_func_test.c
4 * @brief
5 * This file is part of FTM.
6 * AMT´úÀíÓ¦Óòã
7 *
8 * @details
9 * @author Tools Team.
10 * @email
11 * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
12 * @warning
13 * @date 2020/11/25
14 * @version 1.1
15 * @pre
16 * @post
17 *
18 * @par
19 * Change History :
20 * ---------------------------------------------------------------------------
21 * date version author description
22 * ---------------------------------------------------------------------------
23 * 2020/11/25 1.0 liu.xin Create file
24 * ---------------------------------------------------------------------------
25 *
26 *
27 */
28
29
30#include <sys/msg.h>
31#include <stdarg.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <string.h>
37#include <sys/types.h>
38#include <semaphore.h>
39#include <time.h>
40#include <sys/stat.h>
41#include "softap_api.h"
42#include "kwatch_msg.h"
43#include "amt.h"
44#include "amt_func_test.h"
45#include "port_com.h"
46
47
48
49
50
51static fd_set g_uart_fdsread;
52static int g_uart_fd_max = 0;
53static int g_fd_uart0 = -1;
54static int g_fd_uart1 = -1;
55static int g_fd_uart2 = -1;
56static int g_close_uart = 0;
57
58
59
60
61
62static void set_fd(int fd)
63{
64 FD_SET(fd, &g_uart_fdsread);
65 if (fd >= g_uart_fd_max)
66 {
67 g_uart_fd_max = fd + 1;
68 }
69}
70
71static void clr_fd(int fd)
72{
73 FD_CLR(fd, &g_uart_fdsread);
74}
75
76static int init_uart_device(char*uart_path)
77{
78
79 int fd = open(uart_path, O_RDWR);
80
81 if (fd < 0)
82 {
83 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", uart_path);
84 return -1;
85 }
86
87 uart_set(fd);
88 return fd;
89}
90
91/**
92 * @brief ¶ÁÈ¡uart Êý¾ÝµÄÏ̺߳¯Êý
93 * @param[in] args Ï̺߳¯Êý²ÎÊý
94 * @return N/A
95 * @note
96 * @see
97 */
98static void* ReadUartThread(void* args)
99{
100 int ret = -1;
101 int read_len = 0;
102 char *receive_buffer = malloc(MAX_UART_DATA_LENGTH);
103 UNUSED(args);
104 if (receive_buffer == NULL)
105 {
106 return NULL;
107 }
108 memset(receive_buffer,0,MAX_UART_DATA_LENGTH);
109
110 while (!g_close_uart)
111 {
112 ret = select(g_uart_fd_max, &g_uart_fdsread, NULL, NULL, NULL);
113 if (ret <= 0)
114 {
115 AmtPrintf(AMT_ERROR "select error: %s!\n", strerror(errno));
116 continue;
117 }
118
119 if (g_fd_uart0 >= 0 && FD_ISSET(g_fd_uart0, &g_uart_fdsread))
120 {
121 memset(receive_buffer,0,MAX_UART_DATA_LENGTH);
122 read_len = PortRecv(g_fd_uart0, (unsigned char *)receive_buffer, MAX_UART_DATA_LENGTH - 1, NO_WAIT);
123 if(read_len > 0)
124 {
125 receive_buffer[read_len] = '\0';
126 AmtPrintf(AMT_INFO "ttyS0 receive[%s]\n", receive_buffer);
127 if(!strcmp(receive_buffer,"amt uart test\r\n"))
128 {
129 PortSend(g_fd_uart0, "ttyS0 test OK!", strlen("ttyS0 test OK!"), WAIT_ALL);
130 }
131 else
132 {
133 AmtPrintf(AMT_ERROR "ttyS0 input error!\n");
134 }
135
136 }
137 }
138 if (g_fd_uart1 >= 0 && FD_ISSET(g_fd_uart1, &g_uart_fdsread))
139 {
140 memset(receive_buffer,0,MAX_UART_DATA_LENGTH);
141 read_len = PortRecv(g_fd_uart1, (unsigned char *)receive_buffer, MAX_UART_DATA_LENGTH - 1, NO_WAIT);
142 if(read_len > 0)
143 {
144 receive_buffer[read_len] = '\0';
145 AmtPrintf(AMT_INFO "ttyS1 receive[%s]\n", receive_buffer);
146 if(!strcmp(receive_buffer,"amt uart test\r\n"))
147 {
148 PortSend(g_fd_uart1, "ttyS1 test OK!", strlen("ttyS1 test OK!"), WAIT_ALL);
149 }
150
151 else
152 {
153 AmtPrintf(AMT_ERROR "ttyS1 input error!\n");
154 }
155 }
156 }
157 if (g_fd_uart2 >= 0 && FD_ISSET(g_fd_uart2, &g_uart_fdsread))
158 {
159 memset(receive_buffer,0,MAX_UART_DATA_LENGTH);
160 read_len = PortRecv(g_fd_uart2, (unsigned char *)receive_buffer, MAX_UART_DATA_LENGTH - 1, NO_WAIT);
161 if(read_len > 0)
162 {
163 receive_buffer[read_len] = '\0';
164 AmtPrintf(AMT_INFO "ttyS2 receive[%s]\n", receive_buffer);
165 if(!strcmp(receive_buffer,"amt uart test\r\n"))
166 {
167 PortSend(g_fd_uart2, "ttyS2 test OK!", strlen("ttyS2 test OK!"), WAIT_ALL);
168 }
169 else
170 {
171 AmtPrintf(AMT_ERROR "ttyS2 input error!\n");
172 }
173 }
174 }
175 }
176
177 free(receive_buffer);
178 receive_buffer = NULL;
179 return NULL;
180}
181
182
183
184
185/**
186 * @brief AMTÍâÉè²âÊÔ³õʼ»¯
187 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
188 * @note
189 * @see
190 */
191int Amt_FuncTest_Init(void)
192{
193 return 0;
194}
195
196/**
197 * @brief AMTÍâÉèÏûÏ¢´¦Àíº¯Êý
198 * @param[in] msg_id FID
199 * @param[in] msg_buf ½ÓÊÕÊý¾Ýbuffer
200 * @param[in] msg_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
201 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
202 * @note
203 * @see
204 */
205int Amt_FuncTest_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len)
206{
207 unsigned char result = 1;
208
209 switch(msg_id)
210 {
211 case FID_OPEN_UART:
212 {
213 //´ò¿ª´®¿Ú
214 g_close_uart = 0;
215 FD_ZERO(&g_uart_fdsread);
216 g_fd_uart0 = init_uart_device(UART0_DEV);
217 AmtPrintf(AMT_INFO "g_fd_uart0 = %d.\n", g_fd_uart0);
218 if (g_fd_uart0 >= 0)
219 {
220 set_fd(g_fd_uart0);
221 }
222 g_fd_uart1 = init_uart_device(UART1_DEV);
223 AmtPrintf(AMT_INFO "g_fd_uart1 = %d.\n", g_fd_uart1);
224 if (g_fd_uart1 >= 0)
225 {
226 set_fd(g_fd_uart1);
227 }
228 g_fd_uart2 = init_uart_device(UART2_DEV);
229 AmtPrintf(AMT_INFO "g_fd_uart2 = %d.\n", g_fd_uart2);
230 if (g_fd_uart2 >= 0)
231 {
232 set_fd(g_fd_uart2);
233 }
234 if((g_fd_uart0 == -1)&&(g_fd_uart1 == -1)&&(g_fd_uart2 == -1))
235 {
236 unsigned char ret = 1;
237 if (Amt_CreateResponse(msg_id, (unsigned char*)&ret, sizeof(unsigned char)) == -1)
238 {
239 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
240 }
241 else
242 {
243 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
244 }
245 }
246 else
247 {
248 unsigned char ret = 0;
249 if (Amt_CreateResponse(msg_id, (unsigned char*)&ret, sizeof(unsigned char)) == -1)
250 {
251 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
252 }
253 else
254 {
255 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
256 }
257 //´´½¨Ï̼߳àÌý´®¿Ú
258
259 pthread_t uart_read_thread;
260 if (pthread_create(&uart_read_thread, NULL, ReadUartThread, NULL) != 0)
261 {
262 AmtPrintf(AMT_ERROR "Failed to create uart read thread!\n");
263 return -1;
264 }
265
266 }
267
268 break;
269 }
270 case FID_CLOSE_UART:
271 {
272 AmtPrintf(AMT_INFO "receive FID_CLOSE_UART\n");
273 g_close_uart = 1;
274 if (g_fd_uart0 >= 0)
275 {
276 close(g_fd_uart0);
277 }
278 if (g_fd_uart1 >= 0)
279 {
280 close(g_fd_uart1);
281 }
282 if (g_fd_uart2 >= 0)
283 {
284 close(g_fd_uart2);
285 }
286 if (Amt_CreateResponse(msg_id, NULL, 0) == -1)
287 {
288 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
289 }
290 else
291 {
292 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
293 }
294 break;
295 }
296 default:
297 break;
298
299 }
300
301 return 0;
302}
303
304/**
305 * @brief AMTÍâÉèÏûÏ¢·´À¡¸øPC
306 * @param[in] msg_id FID
307 * @param[in] result ״̬Âë
308 * @param[in] msg_buf ½ÓÊÕÊý¾Ýbuffer
309 * @param[in] msg_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
310 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
311 * @note
312 * @see
313 */
314int Amt_FuncTest_SendMsg(unsigned int msg_id, int result, unsigned char *msg_buf, unsigned int msg_len)
315{
316 return 0;
317}
318
319