blob: 37ebef233cb72a0fd8066d9754e15fc06f96ad62 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
2 *
3 * @file amt.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 2019/02/02
14 * @version 1.1
15 * @pre
16 * @post
17 *
18 * @par
19 * Change History :
20 * ---------------------------------------------------------------------------
21 * date version author description
22 * ---------------------------------------------------------------------------
23 * 2015/04/28 1.0 lu.xieji Create file
24 * 2019/02/02 1.1 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
25 * ---------------------------------------------------------------------------
26 *
27 *
28 */
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <pthread.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/ioctl.h>
37#include <sys/msg.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include "linux/rpmsg_zx29.h"
41#include <sys/socket.h>
42#include <netdb.h>
43#include <netinet/in.h>
44#include <linux/netlink.h>
45#include <arpa/inet.h>
46#include <netinet/in.h>
47#include <signal.h>
48#include <errno.h>
49#include "port_com.h"
50#include "amt.h"
51#include "os_type_def.h"
52#include "softap_api.h"
53#include "other_msg.h"
54#include "message.h"
55#include "amt_agent_devicetest.h"
56#include "kwatch_msg.h"
57#include "amtnv.h"
58#include "libcpnv.h"
59#include <sys/prctl.h>
60#include "sys/wait.h"
61#include "ref_nv_def.h"
62#include "nv_api.h"
63
64
65
66
67/**
68 * Íⲿº¯ÊýÒýÓÃ
69 */
70extern int Amt_Wifi_Init(void);
71extern int Amt_Wifi_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);
72extern int Amt_Gps_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);
73extern int Amt_DeviceTest_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);
74extern int Amt_Process_Gps_Rsp(MSG_BUF *msg,unsigned int msg_id);
75extern int Amt_FuncTest_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);
76extern int wifi_ioctl_handle(int cmd);
77
78
79/**
80 * È«¾Ö±äÁ¿ÉùÃ÷
81 */
82static int g_amt_fd_cp = -1;
83static int g_amt_fd_usb = -1;
84static int g_amt_fd_usb_hotplug = -1;
85static int g_amt_fd_socket_client = -1;
86static int g_amt_fd_socket_server = -1;
87static fd_set g_amt_fdsread;
88static int g_amt_fd_max = 0;
89static volatile int *g_amt_fd_current = NULL;
90static int g_amt_iMsgHandle = 0;
91unsigned int g_amt_at_mode = 0;
92
93
94
95/**
96 * @brief AMTÏûÏ¢·¢Ë͸øPC
97 * @param[in] fd ÎļþÃèÊö·û
98 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
99 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
100 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
101 * @note
102 * @see
103 */
104static int Amt_SendData(int fd, unsigned char* buf, unsigned int buf_len)
105{
106 int write_len = PortSend(fd, (unsigned char*)&buf_len, sizeof(unsigned int), WAIT_ALL);
107
108 //if (write_len > 0)
109 {
110 write_len = PortSend(fd, buf, buf_len, WAIT_ALL);
111
112 if (write_len <= 0)
113 {
114 AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
115 }
116 }
117 /*
118 else
119 {
120 AmtPrintf(AMT_ERROR "%s: PortSend 'length' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
121 }
122 */
123
124 return write_len;
125}
126
127/**
128 * @brief AMTÏûÏ¢·¢ËÍ
129 * @param[in] fd ÎļþÃèÊö·û
130 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
131 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
132 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
133 * @note
134 * @see
135 */
136static int Amt_SendDataToAmtagent(int fd, unsigned char* buf, unsigned int buf_len)
137{
138 int write_len = PortSend(fd, buf, buf_len, NO_WAIT);
139
140 if (write_len <= 0)
141 {
142 AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
143 }
144 else
145 {
146 AmtPrintf(AMT_INFO "%s: PortSend 'data' to device(fd = %d): write_len(%d),buf_len(%d) \n", __FUNCTION__, fd, write_len,buf_len);
147 }
148 return write_len;
149}
150
151
152/**
153 * @brief AMTÏûÏ¢½ÓÊÕ
154 * @param[in] fd ÎļþÃèÊö·û
155 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
156 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
157 * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È
158 * @note
159 * @see
160 */
161static int Amt_ReceiveData(int fd, unsigned char* buf, unsigned int buf_len)
162{
163 int read_len = PortRecv(fd, buf, sizeof(unsigned int), WAIT_ALL);
164
165 if (read_len > 0)
166 {
167 unsigned int packet_len = *((unsigned int *)buf);
168 if (packet_len > 0 && packet_len <= buf_len)
169 {
170 read_len = PortRecv(fd, buf, packet_len, WAIT_ALL);
171
172 if (read_len <= 0)
173 {
174 AmtPrintf(AMT_ERROR "%s: PortRecv 'data' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
175 }
176 }
177 else
178 {
179 AmtPrintf(AMT_ERROR "%s: fd = %d, packet_len(%d) is wrong.\n", __FUNCTION__, fd, packet_len);
180 read_len = -1;
181 assert(0);
182 }
183 }
184 else
185 {
186 AmtPrintf(AMT_ERROR "%s: PortRecv 'length' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
187 }
188
189 return read_len;
190}
191
192#if 0
193static void str_to_int( int *data, char *str )
194{
195 int Lc = 0;
196 int Len = 0;
197 char *Begb = NULL;
198 char *Endb = NULL;
199 char *ptr = NULL;
200 char s1[100] = "";
201 char s2[50] = "";
202 memset(s1, 0, sizeof(s1));
203 strcpy(s1, str);
204 strcat(s1, ",");
205 Begb = s1; //ÆðʼµØÖ·
206 Endb = strchr(s1,','); //µÚÒ»¸ö¶ººÅλÖÃ
207 if (Endb == NULL)
208 return;
209 Lc = 0;
210 do
211 {
212 Len = 0;
213 Len = Endb - Begb; //³¤¶È
214 memset(s2, 0, sizeof(s2)); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
215 strncpy(s2, Begb, Len); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
216 data[Lc] = atoi(s2); //string to int
217 ptr = Endb + 1; //Ö¸ÏòϸöÊýµÄÆðʼµØÖ·
218 strcpy(s1, ptr); //È¥µôÒÑת»¯µÄ×Ö·û´®
219 Begb = s1; //ÆðʼµØÖ·¸üÐÂ
220 Endb = strchr(s1,','); //ϸö¶ººÅλÖÃ
221 Lc++;
222 }while(Endb!=NULL);
223}
224#endif
225
226static void str_to_double( double *data, char *str )
227{
228 int Lc = 0;
229 int Len = 0;
230 char *Begb = NULL;
231 char *Endb = NULL;
232 char *ptr = NULL;
233 char s1[100] = "";
234 char s2[50] = "";
235 memset(s1, 0, sizeof(s1));
236 strncpy(s1, str,sizeof(s1) - 1);
237 strcat(s1, ",");
238 Begb = s1; //ÆðʼµØÖ·
239 Endb = strchr(s1,','); //µÚÒ»¸ö¶ººÅλÖÃ
240 if (Endb == NULL)
241 return;
242 Lc = 0;
243 do
244 {
245 Len = 0;
246 Len = Endb - Begb; //³¤¶È
247 memset(s2, 0, sizeof(s2)); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
248 strncpy(s2, Begb, sizeof(s2) - 1); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
249 data[Lc] = atof(s2); //string to int
250 ptr = Endb + 1; //Ö¸ÏòϸöÊýµÄÆðʼµØÖ·
251 strncpy(s1, ptr, sizeof(s1) - 1); //È¥µôÒÑת»¯µÄ×Ö·û´®
252 Begb = s1; //ÆðʼµØÖ·¸üÐÂ
253 Endb = strchr(s1,','); //ϸö¶ººÅλÖÃ
254 Lc++;
255 }while(Endb!=NULL);
256}
257
258/**
259 * @brief ¶ÁÈ¡cp²àµÄ·´À¡ÏûÏ¢Ï̺߳¯Êý
260 * @param[in] args Ï̺߳¯Êý²ÎÊý
261 * @return N/A
262 * @note
263 * @see
264 */
265static void* ReadFromCPThread(void* args)
266{
267 // Read from AP-CP channel
268 char *receive_buffer = NULL;
269
270 UNUSED(args);
271
272 receive_buffer = malloc(MAX_PACKET_LENGTH);
273 if (receive_buffer == NULL)
274 {
275 return NULL;
276 }
277
278 prctl(PR_SET_NAME, "AmtReadFromCP");
279
280 while (1)
281 {
282 int read_len = Amt_ReceiveData(g_amt_fd_cp, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
283
284 if (read_len > 0)
285 {
286 AmtPrintf(AMT_INFO "%s: Receive cp data, read_len = %d!.\n", __FUNCTION__, read_len);
287
288 if (g_amt_fd_current && *g_amt_fd_current >= 0)
289 {
290 if(g_amt_at_mode != 1)
291 {
292 Amt_SendData(*g_amt_fd_current, (unsigned char *)receive_buffer, read_len);
293 }
294 }
295 else
296 {
297 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
298 }
299
300
301 unsigned int status = cpnv_FsGcWait(FS_NVROFS);
302 if(status != CPNV_OK)
303 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
304 }
305 }
306
307 free(receive_buffer);
308 return NULL;
309}
310
311
312/**
313 * @brief ¶ÁÈ¡ÆäËûappµÄÏûÏ¢Ï̺߳¯Êý
314 * @param[in] args Ï̺߳¯Êý²ÎÊý
315 * @return N/A
316 * @note
317 * @see
318 */
319static void* RecvMsgFromAppThread(void* args)
320{
321 int iRet ;
322 MSG_BUF stMsg = {0};
323 LONG msgSize = sizeof(MSG_BUF) - sizeof(LONG);
324
325 UNUSED(args);
326
327 prctl(PR_SET_NAME, "AmtRecvAppMsg");
328 while(1)
329 {
330 iRet = 0;
331 memset(&stMsg, 0x00, sizeof(MSG_BUF));
332
333 iRet = msgrcv(g_amt_iMsgHandle, &stMsg, msgSize, 0, 0);
334 if(iRet >= 0)
335 {
336 AmtPrintf(AMT_INFO "%s: msgcmd = 0x%x,datalen=%d.\n", __FUNCTION__,stMsg.usMsgCmd,stMsg.usDataLen);
337
338 switch (stMsg.usMsgCmd)
339 {
340 case MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP:
341 {
342 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP.\n", __FUNCTION__);
343 int result = AMT_SUCCESS_RET;
344 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_BATTERY_VOLTAGE, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);
345 break;
346 }
347
348 case MSG_CMD_AMT_KEY_TEST_START_RSP:
349 {
350 int result = AMT_ERROR_RET;
351 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
352 result = *pAucDataBuf;
353 if (result == 0)
354 {
355 result = AMT_SUCCESS_RET;
356 }
357 else
358 {
359 result = AMT_ERROR_RET;
360 AmtPrintf(AMT_INFO "%s: KEY_TEST_START fail.", __FUNCTION__);
361 }
362
363 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_START, result, NULL, 0);
364 break;
365 }
366
367 case MSG_CMD_AMT_KEY_TEST_STOP_RSP:
368 {
369 int result = AMT_SUCCESS_RET;
370 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_STOP, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);
371 break;
372 }
373
374 case MSG_CMD_AMT_LCD_TEST_START_RSP:
375 {
376 int result = AMT_ERROR_RET;
377 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
378 result = *pAucDataBuf;
379 if (result == 0)
380 {
381 result = AMT_SUCCESS_RET;
382 }
383 else
384 {
385 result = AMT_ERROR_RET;
386 AmtPrintf(AMT_INFO "%s: LCD_TEST_START fail.", __FUNCTION__);
387 }
388
389 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_START, result, NULL, 0);
390 break;
391 }
392
393 case MSG_CMD_AMT_LCD_TEST_STOP_RSP:
394 {
395 int result = AMT_ERROR_RET;
396 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
397 result = *pAucDataBuf;
398 AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP result=%d\n", __FUNCTION__,result);
399 if (result == 0)
400 {
401 result = AMT_SUCCESS_RET;
402 }
403 else
404 {
405 result = AMT_ERROR_RET;
406 AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP fail.", __FUNCTION__);
407 }
408
409 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_STOP, result, NULL, 0);
410 break;
411 }
412
413 case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_START_RSP:
414 {
415 int result = AMT_ERROR_RET;
416 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
417 result = *pAucDataBuf;
418 if (result == 1)
419 {
420 result = AMT_SUCCESS_RET;
421 }
422 else
423 {
424 result = AMT_ERROR_RET;
425 AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_START fail.", __FUNCTION__);
426 }
427
428 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_START, result, NULL, 0);
429 break;
430 }
431
432 case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_STOP_RSP:
433 {
434 int result = AMT_ERROR_RET;
435 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
436 result = *pAucDataBuf;
437 if (result == 0)
438 {
439 result = AMT_SUCCESS_RET;
440 }
441 else
442 {
443 result = AMT_ERROR_RET;
444 AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_STOP fail.", __FUNCTION__);
445 }
446
447 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_STOP, result, NULL, 0);
448 break;
449 }
450
451 case MSG_CMD_AMT_VIBRATOR_TEST_START_RSP:
452 {
453 int result = AMT_ERROR_RET;
454 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
455 result = *pAucDataBuf;
456 if (result == 1)
457 {
458 result = AMT_SUCCESS_RET;
459 }
460 else
461 {
462 result = AMT_ERROR_RET;
463 AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_START fail.", __FUNCTION__);
464 }
465
466 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_START, result, NULL, 0);
467 break;
468 }
469
470 case MSG_CMD_AMT_VIBRATOR_TEST_STOP_RSP:
471 {
472 int result = AMT_ERROR_RET;
473 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
474 result = *pAucDataBuf;
475 if (result == 0)
476 {
477 result = AMT_SUCCESS_RET;
478 }
479 else
480 {
481 result = AMT_ERROR_RET;
482 AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_STOP fail.", __FUNCTION__);
483 }
484
485 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_STOP, result, NULL, 0);
486 break;
487 }
488
489 case MSG_CMD_AMT_CAMERA_TEST_START_RSP:
490 {
491 int result = AMT_ERROR_RET;
492 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
493 result = *pAucDataBuf;
494 if (result == 1)
495 {
496 result = AMT_SUCCESS_RET;
497 }
498 else
499 {
500 result = AMT_ERROR_RET;
501 AmtPrintf(AMT_INFO "%s: CAMERA_TEST_START fail.", __FUNCTION__);
502 }
503
504 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_START, result, NULL, 0);
505 break;
506 }
507
508 case MSG_CMD_AMT_CAMERA_TEST_STOP_RSP:
509 {
510 int result = AMT_ERROR_RET;
511 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
512 result = *pAucDataBuf;
513 if (result == 0)
514 {
515 result = AMT_SUCCESS_RET;
516 }
517 else
518 {
519 result = AMT_ERROR_RET;
520 AmtPrintf(AMT_INFO "%s: CAMERA_TEST_STOP fail.", __FUNCTION__);
521 }
522
523 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_STOP, result, NULL, 0);
524 break;
525 }
526
527 case MSG_CMD_AMT_SPEAKER_TEST_RSP:
528 {
529 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_SPEAKER_TEST_RSP.\n", __FUNCTION__);
530 int result = AMT_ERROR_RET;
531 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
532 result = *pAucDataBuf;
533 if (result == 1)
534 {
535 result = AMT_SUCCESS_RET;
536 }
537 else
538 {
539 result = AMT_ERROR_RET;
540 AmtPrintf(AMT_INFO "%s: SPEAKER_TEST fail.", __FUNCTION__);
541 }
542
543 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER, result, NULL, 0);
544 break;
545 }
546
547 case MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP:
548 {
549 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP.\n", __FUNCTION__);
550 int result = AMT_SUCCESS_RET;
551 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER_STOP, result, NULL, 0);
552 break;
553 }
554
555 case MSG_CMD_AMT_RECEIVER_TEST_RSP:
556 {
557 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_RECEIVER_TEST_RSP.\n", __FUNCTION__);
558 int result = AMT_ERROR_RET;
559 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
560 result = *pAucDataBuf;
561 if (result == 1)
562 {
563 result = AMT_SUCCESS_RET;
564 }
565 else
566 {
567 result = AMT_ERROR_RET;
568 AmtPrintf(AMT_INFO "%s: RECEIVER_TEST fail.", __FUNCTION__);
569 }
570
571 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER, result, NULL, 0);
572 break;
573 }
574
575 case MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP:
576 {
577 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP.\n", __FUNCTION__);
578 int result = AMT_SUCCESS_RET;
579 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER_STOP, result, NULL, 0);
580 break;
581 }
582
583 case MSG_CMD_AMT_TP_TEST_START_RSP:
584 {
585 int result = AMT_ERROR_RET;
586 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
587 result = *pAucDataBuf;
588 if (result == 1)
589 {
590 result = AMT_SUCCESS_RET;
591 }
592 else
593 {
594 result = AMT_ERROR_RET;
595 AmtPrintf(AMT_INFO "%s: TP_TEST_START fail.", __FUNCTION__);
596 }
597
598 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_START, result, NULL, 0);
599 break;
600 }
601
602 case MSG_CMD_AMT_TP_TEST_STOP_RSP:
603 {
604 int result = AMT_ERROR_RET;
605 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
606 result = *pAucDataBuf;
607 if (result == 0)
608 {
609 result = AMT_SUCCESS_RET;
610 }
611 else
612 {
613 result = AMT_ERROR_RET;
614 AmtPrintf(AMT_INFO "%s: TP_TEST_STOP fail.", __FUNCTION__);
615 }
616
617 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_STOP, result, NULL, 0);
618 break;
619 }
620
621 case MSG_CMD_AMT_GSENSOR_TEST_START_RSP:
622 {
623 break;
624 }
625
626 case MSG_CMD_AMT_GSENSOR_TEST_STOP_RSP:
627 {
628 break;
629 }
630
631 case MSG_CMD_AMT_WIFI_TEST_START_RSP:
632 {
633 int result = AMT_ERROR_RET;
634 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
635 result = *pAucDataBuf;
636 if (result == 1)
637 {
638 result = AMT_SUCCESS_RET;
639 }
640 else
641 {
642 result = AMT_ERROR_RET;
643 AmtPrintf(AMT_INFO "%s: Wifi test fail!", __FUNCTION__);
644 }
645
646 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_WIFI, result, NULL, 0);
647 break;
648 }
649
650 case MSG_CMD_AMT_FLASHLIGHT_START_RSP:
651 {
652 int result = AMT_ERROR_RET;
653 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
654 result = *pAucDataBuf;
655 if (result == 1)
656 {
657 result = AMT_SUCCESS_RET;
658 }
659 else
660 {
661 result = AMT_ERROR_RET;
662 AmtPrintf(AMT_INFO "%s: flashlight test fail!", __FUNCTION__);
663 }
664
665 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_FLASHLIGHT_START, result, NULL, 0);
666 break;
667 }
668 //gps²âÊÔÏûÏ¢»ØÓ¦
669 case KWATCH_MSG_GPS_RSP:
670 {
671 Amt_Process_Gps_Rsp(&stMsg,FID_GPS_MODULE_TEST);
672 break;
673 }
674 default:
675 break;
676 }
677 }
678 }
679 return NULL;
680}
681
682
683/**
684 * @brief ¼ÓÔØ°®¿ÆÎ¢WIFI¹Ì¼þ
685 * @param[in] args Ï̺߳¯Êý²ÎÊý
686 * @return N/A
687 * @note
688 * @see
689 */
690static void* LoadWifiFirmwareThread(void* args)
691{
692 int ret = -1;
693 UNUSED(args);
694
695 prctl(PR_SET_NAME, "LoadWifiFirmwareThread");
696
697 #if (defined(__AIC_8800DW_CHIP__))
698 ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ
699 if(ret < 0)
700 {
701 AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);
702 }
703 else
704 {
705 AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);
706 }
707 #endif
708
709 return NULL;
710}
711
712
713/**
714 * @brief AMTÏûÏ¢´ò°ü·¢¸øCP
715 * @param[in] fd ÎļþÃèÊö·û
716 * @param[in] msg_id FID
717 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
718 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
719 * @return ³É¹¦·µ»Ø0
720 * @note
721 * @see
722 */
723static int Amt_SendMessageToAmtagent(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
724{
725 // STX TID FID Data Check ETX
726 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
727 unsigned char *pRsp = NULL;
728 unsigned char *pSTX = NULL;
729 unsigned char *pETX = NULL;
730 unsigned char *pTID = NULL;
731 unsigned char *pFID = NULL;
732 unsigned char *pCheck = NULL;
733 unsigned char *pData = NULL;
734 unsigned char CheckSum = 0;
735 unsigned int i = 0;
736
737 pRsp = malloc(CmdCount);
738 if(pRsp == NULL)
739 return -1;
740 memset(pRsp, 0, CmdCount);
741
742 pSTX = &pRsp[0];
743 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
744 pTID = &pRsp[1];
745 pFID = &pRsp[1 + 1];
746 pCheck = &pRsp[1 + 1 + 1 + buf_len];
747 pData = &pRsp[1 + 1 + 1];
748
749 *pSTX = 0x02;
750 *pETX = 0x02;
751 *pTID = (msg_id & 0xFF00) >> 8;
752 *pFID = (msg_id & 0xFF);
753
754 if (buf != NULL && buf_len > 0)
755 {
756 memcpy(pData, buf, buf_len);
757 }
758
759 CheckSum = 0;
760
761 for (i = 0; i < (1 + 1 + buf_len); i++)
762 CheckSum ^= pRsp[1 + i];
763
764 *pCheck = CheckSum;
765
766 Amt_SendDataToAmtagent(fd, pRsp, CmdCount);
767
768 free(pRsp);
769 return 0;
770}
771
772
773/**
774 * @brief AMTÏûÏ¢´ò°ü·¢¸øPC
775 * @param[in] fd ÎļþÃèÊö·û
776 * @param[in] msg_id FID
777 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
778 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
779 * @return ³É¹¦·µ»Ø0
780 * @note
781 * @see
782 */
783static int Amt_SendMessage(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
784{
785 // STX TID FID Data Check ETX
786 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
787 unsigned char *pRsp = NULL;
788 unsigned char *pSTX = NULL;
789 unsigned char *pETX = NULL;
790 unsigned char *pTID = NULL;
791 unsigned char *pFID = NULL;
792 unsigned char *pCheck = NULL;
793 unsigned char *pData = NULL;
794 unsigned char CheckSum = 0;
795 unsigned int i = 0;
796
797 pRsp = malloc(CmdCount);
798 if(pRsp == NULL)
799 return -1;
800 memset(pRsp, 0, CmdCount);
801
802 pSTX = &pRsp[0];
803 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
804 pTID = &pRsp[1];
805 pFID = &pRsp[1 + 1];
806 pCheck = &pRsp[1 + 1 + 1 + buf_len];
807 pData = &pRsp[1 + 1 + 1];
808
809 *pSTX = 0x02;
810 *pETX = 0x02;
811 *pTID = (msg_id & 0xFF00) >> 8;
812 *pFID = (msg_id & 0xFF);
813
814 if (buf != NULL && buf_len > 0)
815 {
816 memcpy(pData, buf, buf_len);
817 }
818
819 CheckSum = 0;
820
821 for (i = 0; i < (1 + 1 + buf_len); i++)
822 CheckSum ^= pRsp[1 + i];
823
824 *pCheck = CheckSum;
825
826 Amt_SendData(fd, pRsp, CmdCount);
827
828 free(pRsp);
829 return 0;
830}
831
832
833/**
834 * @brief AMTÏûÏ¢´¦Àíº¯Êý
835 * @param[in] msg_id FID
836 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
837 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
838 * @return ³É¹¦·µ»Ø0
839 * @note
840 * @see
841 */
842static int Amt_ProcessMessage(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
843{
844 if (msg_id >= FID_WIFI_CMD_NORTN && msg_id <= FID_WIFI_CMD_END) // wifi test
845 {
846 Amt_Wifi_ProcessMsg(msg_id, buf, buf_len);
847 }
848 else if (msg_id == FID_GET_CHIP_PLATFORM)
849 {
850 //
851 if(is_amt_mode())
852 {
853 AmtPrintf(AMT_ERROR "AMT MODE!\n");
854 }
855 else
856 {
857 AmtPrintf(AMT_ERROR "Normal MODE!\n");
858 }
859 unsigned char chipType = 0;
860 AmtPrintf(AMT_INFO "%s: Get chip platform msg_id = %#04x, buf_len = %d.\n",
861 __FUNCTION__, msg_id, buf_len);
862 chipType = 1; /*1:7520V3 2:7100*/
863
864 if (Amt_CreateResponse(msg_id, &chipType, sizeof(unsigned char)) == -1)
865 {
866 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
867 }
868 else
869 {
870 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
871 }
872 }
873 else if (msg_id >= FID_GPS_MODULE_TEST && msg_id <= FID_GPS_CMD_END) // gps test
874 {
875 Amt_Gps_ProcessMsg(msg_id, buf, buf_len);
876 }
877 else if (msg_id >= MSG_DEVICETEST_START && msg_id <= MSG_DEVICETEST_END)
878 {
879 Amt_DeviceTest_ProcessMsg(msg_id, buf, buf_len);
880 }
881 else if (msg_id == FID_AMT_END)
882 {
883 unsigned int status = CPNV_OK;//cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
884 /*
885 if(status != CPNV_OK)
886 {
887 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
888 return -1;
889 }*/
890
891 status = cpnv_FsGcWait(FS_NVROFS);
892 if(status == CPNV_OK)
893 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait ok.\n", __FUNCTION__);
894 else
895 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
896
897 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
898 {
899 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
900 }
901 else
902 {
903 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
904 }
905 }
906 else if (msg_id == FID_AMT_EXIT)
907 {
908 unsigned int status = 0;
909 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
910 {
911 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
912 }
913 else
914 {
915 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
916 }
917 }
918 else if (msg_id == FID_EX_COMMON_SETBOOTMODE)
919 {
920 // set normal mode
921 unsigned char bootmode[] = {0xFF, 0xFF};
922 unsigned int dwRet = CPNV_OK;
923
924 //struct timespec ts;
925 //clock_gettime(CLOCK_MONOTONIC, &ts);
926 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
927 unsigned char mode = 4;
928 if(buf_len == sizeof(mode))
929 {
930 memcpy(&mode, buf, sizeof(mode));
931 }
932 AmtPrintf(AMT_INFO "%s: boot mode=%d\n", __FUNCTION__,mode);
933 switch(mode)
934 {
935 case 0: //user mode
936 {
937 nv_set_item(NV_RO, "usb_modetype", "user", 1);
938 nv_commit(NV_RO);
939 bootmode[0] =0x54;
940 bootmode[1] =0x00;
941 break;
942 }
943 case 1://debug mode
944 {
945 nv_set_item(NV_RO, "usb_modetype", "debug", 1);
946 nv_commit(NV_RO);
947 bootmode[0] =0x54;
948 bootmode[1] =0x01;
949 break;
950 }
951 case 2://factory mode
952 {
953 nv_set_item(NV_RO, "usb_modetype", "factory", 1);
954 nv_commit(NV_RO);
955 bootmode[0] =0x54;
956 bootmode[1] =0x02;
957 break;
958 }
959 case 3://amt mode
960 {
961 //nv_set_item(NV_RO, "usb_modetype", "amt", 1);
962 //nv_commit(NV_RO);
963 bootmode[0] =0x54;
964 bootmode[1] =0x4D;
965 break;
966 }
967 default:
968 {
969 break;
970 }
971 }
972
973 dwRet = amt_set_bootmode(bootmode);
974 if (dwRet == CPNV_OK)
975 {
976 AmtPrintf(AMT_INFO "%s: set boot mode: sucess.\n", __FUNCTION__);
977 }
978 else
979 {
980 AmtPrintf(AMT_ERROR "%s: set boot mode: failed.\n", __FUNCTION__);
981 }
982 //clock_gettime(CLOCK_MONOTONIC, &ts);
983 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
984
985 if (Amt_CreateResponse(msg_id, (unsigned char*)&dwRet, sizeof(unsigned int)) == -1)
986 {
987 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
988 }
989 else
990 {
991 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
992 }
993 }
994 else if(msg_id == FID_CHECK_SOFTDOG_CIPHER_TEXT)
995 {
996 UINT8 softdog_cipher_texts[OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_SIZE] = {0};
997 if(!amt_read_nv_item(ABSOFTDOG_CIPHER_TEXT_NVPARAM,softdog_cipher_texts,sizeof(softdog_cipher_texts)))
998 {
999 if (Amt_CreateResponse(msg_id, softdog_cipher_texts, sizeof(softdog_cipher_texts)) == -1)
1000 {
1001 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1002 }
1003 else
1004 {
1005 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1006 }
1007 }
1008 else
1009 {
1010 AmtPrintf(AMT_ERROR "%s: read softdog cipher text in address:0x%x failure.\n", __FUNCTION__,OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_ADDRESS);
1011 }
1012 }
1013 else if(msg_id == FID_SET_BAT_DET_FLAG)
1014 {
1015 unsigned int retCode = CPNV_ERROR;
1016 int bat_value = -1;
1017 memcpy(&bat_value, buf, sizeof(int));
1018 retCode =amt_set_batdet_flag(bat_value);
1019
1020 if (retCode == CPNV_OK)
1021 {
1022 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag success.\n", __FUNCTION__);
1023 }
1024 else
1025 {
1026 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag fail.\n", __FUNCTION__);
1027 }
1028 if (Amt_CreateResponse(msg_id, (unsigned char*)&retCode, sizeof(retCode)) == -1)
1029 {
1030 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1031 }
1032 else
1033 {
1034 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1035 }
1036 }
1037 else if(msg_id == FID_GET_BAT_DET_FLAG)
1038 {
1039 unsigned int retCode = CPNV_ERROR;
1040 int bat_value = 0;
1041 retCode =amt_get_batdet_flag(&bat_value);
1042
1043 if (retCode == CPNV_OK)
1044 {
1045 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag success.bat_value=%d\n", __FUNCTION__,bat_value);
1046 if (Amt_CreateResponse(msg_id, (unsigned char*)&bat_value, sizeof(bat_value)) == -1)
1047 {
1048 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1049 }
1050 else
1051 {
1052 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1053 }
1054 }
1055 else
1056 {
1057 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag fail.\n", __FUNCTION__);
1058 }
1059 }
1060 else if(msg_id == FID_RINISOFTVERSION)
1061 {
1062 unsigned int retCode = CPNV_ERROR;
1063 char TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN+1]={0};
1064
1065 retCode = cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_INT_BASE_ADDR, (unsigned char*)TmpSoftVersion, ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN);
1066 if (retCode == CPNV_OK)
1067 {
1068 TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN] = '\0';
1069 AmtPrintf(AMT_INFO "%s: inner version=%s\n", __FUNCTION__,TmpSoftVersion);
1070 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpSoftVersion, sizeof(TmpSoftVersion)) == -1)
1071 {
1072 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1073 }
1074 else
1075 {
1076 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1077 }
1078 }
1079 else
1080 {
1081 AmtPrintf(AMT_INFO "%s: get inner version fail!\n", __FUNCTION__);
1082 }
1083 }
1084 else if(msg_id == FID_ROUTSOFTVERSION)
1085 {
1086 unsigned int retCode = CPNV_ERROR;
1087 char TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN+1]={0};
1088
1089 retCode =cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_EXT_BASE_ADDR, (unsigned char *)TmpExtcgmr, ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN);
1090 if (retCode == CPNV_OK)
1091 {
1092 TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN] = '\0';
1093 AmtPrintf(AMT_INFO "%s: outer version=%s\n", __FUNCTION__,TmpExtcgmr);
1094 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpExtcgmr, sizeof(TmpExtcgmr)) == -1)
1095 {
1096 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1097 }
1098 else
1099 {
1100 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1101 }
1102 }
1103 else
1104 {
1105 AmtPrintf(AMT_INFO "%s: get outer version fail!\n", __FUNCTION__);
1106 }
1107 }
1108 else if((msg_id >= FID_FUN_TEST_START)&&(msg_id <= FID_FUN_TEST_END))
1109 {
1110 Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);
1111 }
1112 else // CP message
1113 {
1114 AmtPrintf(AMT_INFO "%s: receive old cp msg.\n", __FUNCTION__);
1115 // Send message to CP
1116 if (Amt_SendMessageToAmtagent(g_amt_fd_cp, msg_id, buf, buf_len) == -1)
1117 {
1118 AmtPrintf(AMT_ERROR "%s: Failed to send data to cp, msg_id = %#04x, buf_len = %d.\n",
1119 __FUNCTION__, msg_id, buf_len);
1120 }
1121 else
1122 {
1123 AmtPrintf(AMT_INFO "%s: Send data to cp, msg_id = %#04x, buf_len = %d.\n",
1124 __FUNCTION__, msg_id, buf_len);
1125 }
1126 }
1127
1128 return 0;
1129}
1130
1131
1132/**
1133 * @brief AMTÏûÏ¢½â°ü
1134 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1135 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1136 * @return N/A
1137 * @note
1138 * @see
1139 */
1140static void Amt_ComposeAndProcess(unsigned char *buf, unsigned int buf_len)
1141{
1142 unsigned char *pSTX = NULL;
1143 unsigned char *pETX = NULL;
1144 unsigned char *pTID = NULL;
1145 unsigned char *pFID = NULL;
1146 unsigned char *pCheck = NULL;
1147 unsigned char *pData = NULL;
1148 unsigned char CheckSum = 0;
1149 unsigned int msg_id;
1150 unsigned int i = 0;
1151 pTID = pTID;
1152 pCheck = pCheck;
1153 pData = pData;
1154
1155 pSTX = &buf[0];
1156 pETX = &buf[buf_len - 1];
1157 pTID = &buf[1];
1158 pFID = &buf[1 + 1];
1159 pCheck = &buf[buf_len - 2];
1160 pData = &buf[1 + 1 + 1];
1161
1162 if (!((0x02 == *pSTX) && (0x02 == *pETX)))
1163 {
1164 return;
1165 }
1166
1167 CheckSum = 0;
1168
1169 for (i = 0; i < (buf_len - 2); i++)
1170 CheckSum ^= buf[1 + i];
1171
1172 if (CheckSum != 0)
1173 {
1174 return;
1175 }
1176
1177 // TIDÓëFID×éºÏʹÓÃÒÑÀ©Õ¹¹¦ÄܺÅ
1178 msg_id = (*pTID << 8) | *pFID;
1179
1180 AmtPrintf(AMT_INFO "%s: msg_id = %#04x, buf_len = %d.\n", __FUNCTION__, msg_id, buf_len);
1181 Amt_ProcessMessage(msg_id, pFID + 1, (buf_len - 1 - 1 - 1 - 1 - 1));
1182}
1183
1184
1185/**
1186 * @brief AMTÏûÏ¢·´À¡
1187 * @param[in] msg_id FID
1188 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1189 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1190 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
1191 * @note
1192 * @see
1193 */
1194int Amt_CreateResponse(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1195{
1196 if (g_amt_fd_current && *g_amt_fd_current >= 0)
1197 {
1198 return Amt_SendMessage(*g_amt_fd_current, msg_id, buf, buf_len);
1199 }
1200 else
1201 {
1202 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
1203 return -1;
1204 }
1205}
1206
1207int wfsystem(const char * cmd)
1208{
1209 pid_t ret = 0 ;
1210 ret = soft_system(cmd);
1211 AmtPrintf (AMT_ERROR "[%s]ret = %d, WIFEXITED(ret)=%d, WEXITSTATUS(ret) =%d",cmd, ret , WIFEXITED (ret), WEXITSTATUS (ret));
1212 if (! (-1 != ret && WIFEXITED (ret) && 0 == WEXITSTATUS (ret))) {
1213 return -1;
1214 }
1215 return 0;
1216}
1217
1218int check_wlan (void)
1219{
1220 int find_wlan = -1;
1221 int i = 0, sum = 0;
1222
1223 while (-1 == find_wlan) {
1224 AmtPrintf (AMT_ERROR "finding wlan i=%d, %d s...", i , sum);
1225 find_wlan = wfsystem ("ifconfig wlan0");
1226 if (-1==find_wlan) {
1227 if (sum >= 60)
1228 return -1;
1229 sum += 2 * i;
1230 sleep (2 * i++);
1231 }
1232 }
1233 return 0;
1234}
1235int Amt_ExecuteCmd(char *pcmd, char *pbuffer, int len)
1236{
1237 FILE *pPipe;
1238 static int wifi_init = 0;
1239 AmtPrintf(AMT_INFO "%s: execute \"%s\"!\n", __FUNCTION__, pcmd);
1240 if(0 == wifi_init)
1241 {
1242 if(0 == strcmp(pcmd, "ifconfig wlan0 up")){
1243 check_wlan();
1244 wifi_init = 1;
1245 }
1246 }
1247 if ((pPipe = popen(pcmd, "rb")) == NULL)
1248 {
1249 AmtPrintf(AMT_ERROR "popen \"%s\" failure.\n", pcmd);
1250 return -1;
1251 }
1252
1253 int read_len = fread(pbuffer, 1, len, pPipe);
1254 //AmtPrintf(AMT_INFO "fread, read_len = %d, pbuffer = %s.\n", read_len, pbuffer);
1255
1256 pclose(pPipe);
1257 return read_len;
1258}
1259
1260static int init_cp_channel(void)
1261{
1262 int fd = open(AMT_CP_CHANNEL, O_RDWR);
1263
1264 if (fd < 0)
1265 {
1266 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CP_CHANNEL);
1267 return -1;
1268 }
1269
1270 if(ioctl(fd, RPMSG_CREATE_CHANNEL, (8*1024))!= 0) // ´´½¨Í¨µÀ´óС8K
1271 {
1272 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");
1273 }
1274 if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0) // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ
1275 {
1276 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");
1277 }
1278 if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0) // ×èÈûµÄ·½Ê½¶Á
1279 {
1280 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");
1281 }
1282 return fd;
1283}
1284
1285static int init_usb_device(void)
1286{
1287 int fd = open(AMT_USB_DEV, O_RDWR);
1288
1289 if (fd < 0)
1290 {
1291 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_USB_DEV);
1292 return -1;
1293 }
1294
1295 PortSet(fd);
1296 return fd;
1297}
1298
1299static int init_hotplug_nl(void)
1300{
1301 struct sockaddr_nl snl;
1302 bzero(&snl, sizeof(snl));
1303 snl.nl_family = AF_NETLINK;
1304 snl.nl_pid = getpid();
1305 snl.nl_groups = 1;
1306
1307 int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1308 if (s == -1)
1309 {
1310 AmtPrintf(AMT_ERROR "Can't create hotplug socket!\n");
1311 return -1;
1312 }
1313
1314 if (bind(s, (struct sockaddr *)&snl, sizeof(snl)) < 0)
1315 {
1316 AmtPrintf(AMT_ERROR "Can't bind hotplug socket!\n");
1317 close(s);
1318 return -1;
1319 }
1320
1321 return s;
1322}
1323
1324static int init_socket(int port)
1325{
1326 struct sockaddr_in seraddr;
1327 int sockfd = -1;
1328
1329 AmtPrintf(AMT_INFO "port = %d.\n", port);
1330
1331 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1332 if (sockfd == -1)
1333 {
1334 AmtPrintf(AMT_ERROR "Can't create socket!\n");
1335 return -1;
1336 }
1337
1338 memset(&seraddr,0,sizeof(seraddr));
1339 seraddr.sin_family = AF_INET;
1340 seraddr.sin_addr.s_addr = 0;
1341 seraddr.sin_port = htons(port);
1342
1343 if (bind(sockfd, (struct sockaddr*)(&seraddr), sizeof(seraddr)) < 0)
1344 {
1345 AmtPrintf(AMT_ERROR "Can't bind port %d!\n", port);
1346 close(sockfd);
1347 return -1;
1348 }
1349
1350 if (listen(sockfd, 1) == -1)
1351 {
1352 AmtPrintf(AMT_ERROR "Socket listen failed!\n");
1353 close(sockfd);
1354 return -1;
1355 }
1356
1357 return sockfd;
1358}
1359
1360static void set_fd(int fd)
1361{
1362 FD_SET(fd, &g_amt_fdsread);
1363 if (fd >= g_amt_fd_max)
1364 {
1365 g_amt_fd_max = fd + 1;
1366 }
1367}
1368
1369static void clr_fd(int fd)
1370{
1371 FD_CLR(fd, &g_amt_fdsread);
1372}
1373
1374static int CreateSendPacket(unsigned short fid, unsigned char *data_buf, int data_len, unsigned char*packet_buf)
1375{
1376 int bRet = 0;
1377 BYTE *pSTX = NULL;
1378 BYTE *pETX = NULL;
1379 //FIDµÄ¸ß×Ö½Ú
1380 BYTE *pHFID = NULL;
1381 //FIDµÄµÍ×Ö½Ú
1382 BYTE *pLFID = NULL;
1383 BYTE *pCheck = NULL;
1384 BYTE *pData = NULL;
1385 BYTE CheckSum = 0;
1386 int i = 0;
1387
1388 // STX FID Data Check ETX
1389 int CmdCount = 1 + 2 + data_len + 1 + 1;
1390
1391 if ((data_len != 0) && (NULL == data_buf))
1392 {
1393 return bRet;
1394 }
1395
1396 pSTX = &packet_buf[0];
1397 pETX = &packet_buf[1 + 2 + data_len + 1];
1398 pHFID = &packet_buf[1];
1399 pLFID = &packet_buf[1+1];
1400 pCheck = &packet_buf[1 + 2 + data_len];
1401 pData = &packet_buf[1 + 2];
1402
1403 *pSTX = 0x02;
1404 *pETX = 0x02;
1405 *pHFID = (fid>>8)&0x00FF;
1406 *pLFID = fid&0x00FF;
1407 memcpy(pData, data_buf, data_len);
1408 CheckSum = 0;
1409 //CheckSumÊÇFIDºÍDATA×ֶεÄУÑé
1410 for (i = 0; i < (2 + data_len); i++)
1411 CheckSum ^= packet_buf[1 + i];
1412 *pCheck = CheckSum;
1413 bRet = 1;
1414 return bRet;
1415}
1416
1417static void send_enter_amt_mode_packet()
1418{
1419 u32 enter_amt_req[5] = {0};
1420 enter_amt_req[0] = 0x0100;
1421 enter_amt_req[1] = 1;
1422 enter_amt_req[2] = 1;
1423 enter_amt_req[3] = 0;
1424 enter_amt_req[4] = 0;
1425
1426 unsigned char enter_amt_packet[25] = {0};
1427 unsigned short fid = 0x005f;
1428 CreateSendPacket(fid,(unsigned char*)enter_amt_req,sizeof(enter_amt_req),enter_amt_packet);
1429 Amt_ComposeAndProcess(enter_amt_packet, sizeof(enter_amt_packet));
1430}
1431
1432static void send_exit_amt_mode_packet()
1433{
1434 u32 exit_amt_req[5] = {0};
1435 exit_amt_req[0] = 0x0100;
1436 exit_amt_req[1] = 0;
1437 exit_amt_req[2] = 1;
1438 exit_amt_req[3] = 0;
1439 exit_amt_req[4] = 0;
1440
1441 unsigned char exit_amt_packet[25] = {0};
1442 unsigned short fid = 0x005f;
1443 CreateSendPacket(fid,(unsigned char*)exit_amt_req,sizeof(exit_amt_req),exit_amt_packet);
1444 Amt_ComposeAndProcess(exit_amt_packet, sizeof(exit_amt_packet));
1445
1446}
1447
1448
1449static void send_tx_start_packet(unsigned int band,unsigned int channel,short int_power,short dec_power)
1450{
1451 typedef struct
1452 {
1453 UINT32 MsgId;
1454 UINT32 wBandNum; // Ƶ¶Î
1455 UINT32 dwEarfcn;
1456 UINT32 wFreq; // ÖÐÐÄÆµµã£¬100kHzΪµ¥Î»
1457 UINT32 wBandWidth; // ´ø¿í£¬100kHzΪµ¥Î»
1458 UINT32 wAntennaPort; // ÌìÏß¶Ë¿Ú £¿£¿£¿
1459 UINT32 wModulation; // µ÷ÖÆ·½Ê½£¬0£ºQPSK£¬1£º16QAM£¬2£º64QAM
1460 UINT32 wRBNum; // RB¸öÊý
1461 UINT32 wRBStartPosition; // RBÆðÊ¼Æ«ÒÆ
1462 UINT32 wSingleWave; // ÊÇ·ñΪµ¥Ôز¨
1463 SINT16 swRfIntPower; //¹¦ÂÊÕûÊý
1464 SINT16 swRfDecPower; //¹¦ÂÊСÊý,[-2 -1 0 1 2 ]´ú±í[-0.5 -0.25 0 0.25 0.5]
1465 }T_zAMT_LTEA_SetTxInit_Req;
1466
1467 T_zAMT_LTEA_SetTxInit_Req stTxInitReq = {0};
1468 stTxInitReq.MsgId = 0x0700;
1469 stTxInitReq.wBandNum = band;
1470 stTxInitReq.dwEarfcn = channel;
1471 stTxInitReq.wFreq = 0;
1472 stTxInitReq.wBandWidth = 9;
1473 stTxInitReq.wAntennaPort = 0;
1474 stTxInitReq.wModulation = 0;
1475 stTxInitReq.wRBNum = 50;
1476 stTxInitReq.wRBStartPosition = 0;
1477 stTxInitReq.wSingleWave = 0;
1478 stTxInitReq.swRfIntPower = int_power;
1479 stTxInitReq.swRfDecPower = dec_power;
1480
1481 unsigned char tx_start_packet[sizeof(stTxInitReq)+5] = {0};
1482 unsigned short fid = 0x005f;
1483 CreateSendPacket(fid,(unsigned char*)&stTxInitReq,sizeof(stTxInitReq),tx_start_packet);
1484 Amt_ComposeAndProcess(tx_start_packet, sizeof(tx_start_packet));
1485
1486}
1487
1488static void send_tx_stop_packet()
1489{
1490 u32 tx_close_req[1] = {0};
1491 tx_close_req[0] = 0x0201;
1492 unsigned char tx_stop_packet[1*4+5] = {0};
1493 unsigned short fid = 0x005f;
1494 CreateSendPacket(fid,(unsigned char*)tx_close_req,sizeof(tx_close_req),tx_stop_packet);
1495 Amt_ComposeAndProcess(tx_stop_packet, sizeof(tx_stop_packet));
1496
1497}
1498
1499static void send_switch_to_user_mode_packet()
1500{
1501 unsigned char bootmode[] = {0x54, 0x00};
1502 unsigned char atmode[] = {0xFF, 0xFF};
1503 nv_set_item(NV_RO, "usb_modetype", "user", 1);
1504 nv_commit(NV_RO);
1505 amt_set_amt_atmode(bootmode,atmode);
1506}
1507
1508static void send_ok(int fd)
1509{
1510 char rep_ok[] = "OK\r\n";
1511 PortSend(fd, (unsigned char*)rep_ok, strlen(rep_ok), WAIT_ALL);
1512}
1513
1514/**
1515 * @brief ATÃüÁî½ÓÊÕ
1516 * @param[in] fd ÎļþÃèÊö·û
1517 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1518 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1519 * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È
1520 * @note
1521 * @see
1522 */
1523static int At_ReceiveData(int fd, char* buf, unsigned int buf_len)
1524{
1525 int read_len = 0;
1526 int i = 0;
1527 read_len = PortRecv(fd, buf, 100, NO_WAIT);
1528 AmtPrintf(AMT_INFO "%s: read_len=%d.\n", __FUNCTION__, read_len);
1529 if (read_len > 0)
1530 {
1531 buf[read_len] = '\0';
1532 AmtPrintf(AMT_INFO "%s: receive [%s]\n", __FUNCTION__, buf);
1533 //ת»»ÎªÐ¡Ð´
1534 for(i = 0; i < read_len; i++)
1535 {
1536 char at = tolower(buf[i]);
1537 buf[i] = at;
1538 }
1539 AmtPrintf(AMT_INFO "%s: after tolower [%s]\n", __FUNCTION__, buf);
1540 //at+txstart=band,channel,power
1541 if(strstr(buf,"at+txstart") != NULL)
1542 {
1543 AmtPrintf(AMT_INFO "%s: receive at+txstart\n", __FUNCTION__);
1544 double data[3] = {0.0};
1545 unsigned int band = 0;
1546 unsigned int channel = 0;
1547 short int_power = 0;
1548 short idec_power = 0;
1549 double dec_power = 0.0;
1550 //»ñÈ¡²ÎÊý
1551 buf = buf + strlen("at+txstart=");
1552 if(buf != NULL)
1553 {
1554 str_to_double(data,buf);
1555 band = data[0];
1556 channel = data[1];
1557 int_power = data[2];
1558 dec_power = data[2] - int_power;
1559 idec_power = dec_power*4;
1560 AmtPrintf(AMT_INFO "%s: band=%d,channel=%d,int_power=%d,dec_power=%f,idec_power=%d\n", __FUNCTION__, band,channel,int_power,dec_power,idec_power);
1561 if(band == 0)
1562 {
1563 AmtPrintf(AMT_ERROR "%s: invalid band!\n", __FUNCTION__);
1564 return read_len;
1565
1566 }
1567 if(channel == 0 )
1568 {
1569 AmtPrintf(AMT_ERROR "%s: invalid channel!\n", __FUNCTION__);
1570 return read_len;
1571 }
1572 }
1573 //ת»¯Îª½øÈëamtģʽºÍtxinitÁ½ÌõAMTÖ¸Áî
1574 send_enter_amt_mode_packet();
1575 sleep(1);
1576 send_tx_start_packet(band,channel,int_power,idec_power);
1577 send_ok(fd); //Ö±½Ó¸øATÃüÁî·¢ËÍÕ߻ظ´ok
1578
1579 }
1580 else if(strstr(buf,"at+txstop") != NULL)
1581 {
1582 //ת»¯Îª¹Ø±Õ·¢Éä»úºÍÍ˳öamtģʽÁ½ÌõAMTÖ¸Áî
1583 send_tx_stop_packet();
1584 sleep(1);
1585 send_exit_amt_mode_packet();
1586 send_ok(fd);
1587 }
1588 else if(strstr(buf,"at+zversiontype=3") != NULL)
1589 {
1590 send_switch_to_user_mode_packet();
1591 send_ok(fd);
1592 }
1593 else
1594 {
1595 AmtPrintf(AMT_ERROR "%s: unknown at %s.\n", __FUNCTION__, buf);
1596 }
1597 }
1598 else
1599 {
1600 AmtPrintf(AMT_ERROR "%s: PortRecv from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
1601 }
1602
1603 return read_len;
1604}
1605
1606/**
1607 * Main Function
1608 */
1609int main (int argc, char *argv[])
1610{
1611 extern char *optarg;
1612 extern int optind;
1613 int c;
1614 int socket_port = -1;
1615
1616 /********************** Get arguments ********************/
1617 while ((c = getopt(argc,argv,"p:r:")) != EOF)
1618 {
1619 switch (c)
1620 {
1621 case 'p':
1622 socket_port = atoi(optarg);
1623 break;
1624 case '?':
1625 AmtPrintf(AMT_INFO "Usage: zte_amt [options]\n");
1626 AmtPrintf(AMT_INFO "-p ipport - Set IP port number to listen on\n");
1627 exit(1);
1628 }
1629 }
1630
1631 AmtPrintf(AMT_INFO "start AMT!\n");
1632
1633 FD_ZERO(&g_amt_fdsread);
1634
1635 /********************** Init AP-CP channel ********************/
1636 g_amt_fd_cp = init_cp_channel();
1637 if (g_amt_fd_cp < 0)
1638 {
1639 return -1;
1640 }
1641 else
1642 {
1643 AmtPrintf(AMT_INFO "g_amt_fd_cp = %d.\n", g_amt_fd_cp);
1644 }
1645
1646 /********************** Init USB device ***********************/
1647 g_amt_fd_usb = init_usb_device();
1648 if (g_amt_fd_usb >= 0)
1649 {
1650 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
1651 set_fd(g_amt_fd_usb);
1652
1653 //Init hotplug netlink
1654 g_amt_fd_usb_hotplug = init_hotplug_nl();
1655 if (g_amt_fd_usb_hotplug >= 0)
1656 {
1657 AmtPrintf(AMT_INFO "g_amt_fd_usb_hotplug = %d.\n", g_amt_fd_usb_hotplug);
1658 set_fd(g_amt_fd_usb_hotplug);
1659 }
1660 }
1661
1662 /*********************** Create socket ***********************/
1663 if (socket_port > 0)
1664 {
1665 g_amt_fd_socket_server = init_socket(socket_port);
1666 if (g_amt_fd_socket_server >= 0)
1667 {
1668 AmtPrintf(AMT_INFO "g_amt_fd_socket_server = %d.\n", g_amt_fd_socket_server);
1669 set_fd(g_amt_fd_socket_server);
1670 }
1671 }
1672
1673 // Create CP read thread
1674 pthread_t cp_read_thread;
1675 if (pthread_create(&cp_read_thread, NULL, ReadFromCPThread, NULL) != 0)
1676 {
1677 AmtPrintf(AMT_ERROR "Failed to create CP read thread!\n");
1678 return -1;
1679 }
1680
1681 // Wifi init
1682 Amt_Wifi_Init();
1683 g_amt_iMsgHandle = msgget(MODULE_ID_AMT, IPC_CREAT|0600);
1684 if(-1 == g_amt_iMsgHandle)
1685 {
1686 AmtPrintf(AMT_ERROR "%s: can not create msg queue for AMT!\n", __FUNCTION__);
1687 return -1;
1688 }
1689
1690 pthread_t msg_recv_thread;
1691 if (pthread_create(&msg_recv_thread, NULL, RecvMsgFromAppThread, NULL) != 0)
1692 {
1693 AmtPrintf(AMT_ERROR "Failed to create RecvMsgFromAppThread thread!\n");
1694 return -1;
1695 }
1696 // malloc receive buffer
1697 char *receive_buffer = malloc(MAX_PACKET_LENGTH);
1698 if (receive_buffer == NULL)
1699 {
1700 return -1;
1701 }
1702
1703 int ret = -1;
1704 int read_len = 0;
1705 fd_set tmpfds;
1706
1707 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
1708 if(ret != CPNV_OK)
1709 {
1710 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
1711 return -1;
1712 }
1713 else
1714 {
1715 AmtPrintf(AMT_INFO"%s: cpnv_ChangeFsPartitionAttr RW nvrofs ok!\n", __FUNCTION__);
1716 }
1717
1718 //¼ÓÔØaic8800¹Ì¼þ
1719 // Create load wifi firmware thread
1720 pthread_t load_wifi_firmware_thread;
1721 if (pthread_create(&load_wifi_firmware_thread, NULL, LoadWifiFirmwareThread, NULL) != 0)
1722 {
1723 AmtPrintf(AMT_ERROR "Failed to create load wifi firmware thread!\n");
1724 free(receive_buffer);
1725 return -1;
1726 }
1727
1728 #if 0
1729 #if (defined(__AIC_8800DW_CHIP__))
1730 ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ
1731 if(ret < 0)
1732 {
1733 AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);
1734 }
1735 else
1736 {
1737 AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);
1738 }
1739 #endif
1740 #endif
1741
1742 g_amt_at_mode = is_amt_atmode();
1743 AmtPrintf(AMT_INFO"%s: g_amt_at_mode=%d\n", __FUNCTION__,g_amt_at_mode);
1744
1745 while (1)
1746 {
1747 tmpfds = g_amt_fdsread;
1748 ret = select(g_amt_fd_max, &tmpfds, NULL, NULL, NULL);
1749 if (ret <= 0)
1750 {
1751 AmtPrintf(AMT_ERROR "select error: %s!\n", strerror(errno));
1752 continue;
1753 }
1754
1755 if (g_amt_fd_usb_hotplug >= 0 && FD_ISSET(g_amt_fd_usb_hotplug, &tmpfds))
1756 {
1757 read_len = PortRecv(g_amt_fd_usb_hotplug, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH - 1, NO_WAIT);
1758 if(read_len >= 0)
1759 receive_buffer[read_len] = '\0';
1760
1761 // Ç¿ÖÆÔö¼Ó½áÊø·û, read_len < MAX_PACKET_LENGTH
1762 receive_buffer[MAX_PACKET_LENGTH - 1] = '\0';
1763
1764 if (strstr(receive_buffer, AMT_DETECT_USB_HOTREMOVE))
1765 {
1766 AmtPrintf(AMT_INFO "USB remove!\n");
1767 system("poweroff");
1768 break;
1769 }
1770
1771 if (strstr(receive_buffer, AMT_DETECT_USB_OFFLINE))
1772 {
1773 AmtPrintf(AMT_INFO "USB offline!\n");
1774
1775 if (g_amt_fd_usb >= 0)
1776 {
1777 close(g_amt_fd_usb);
1778 clr_fd(g_amt_fd_usb);
1779 g_amt_fd_usb = -1;
1780 }
1781 }
1782
1783 if (strstr(receive_buffer, AMT_DETECT_USB_ONLINE))
1784 {
1785 AmtPrintf(AMT_INFO "USB online!\n");
1786
1787 if (g_amt_fd_usb >= 0)
1788 {
1789 AmtPrintf(AMT_INFO "amt already open usb,do nothing\n");
1790 //close(g_amt_fd_usb);
1791 //clr_fd(g_amt_fd_usb);
1792 //g_amt_fd_usb = -1;
1793 }
1794 else
1795 {
1796 g_amt_fd_usb = init_usb_device();
1797 if (g_amt_fd_usb >= 0)
1798 {
1799 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
1800 set_fd(g_amt_fd_usb);
1801 }
1802 }
1803 }
1804 }
1805
1806 if (g_amt_fd_socket_server >= 0 && FD_ISSET(g_amt_fd_socket_server, &tmpfds))
1807 {
1808 struct sockaddr_in cliaddr;
1809 int addrlen = sizeof(cliaddr);
1810
1811 if (g_amt_fd_socket_client >= 0)
1812 {
1813 close(g_amt_fd_socket_client);
1814 clr_fd(g_amt_fd_socket_client);
1815 g_amt_fd_socket_client = -1;
1816 }
1817
1818 if ((g_amt_fd_socket_client = accept(g_amt_fd_socket_server, (struct sockaddr*)&cliaddr, (socklen_t *)&addrlen)) == -1)
1819 {
1820 AmtPrintf(AMT_ERROR "Accept failed!\n");
1821 }
1822 else
1823 {
1824 unsigned long ip = ntohl(cliaddr.sin_addr.s_addr);
1825 AmtPrintf(AMT_INFO "Connect from %lu.%lu.%lu.%lu\n", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, (ip>>0)&0xff);
1826 set_fd(g_amt_fd_socket_client);
1827 }
1828 }
1829
1830 if (g_amt_fd_usb >= 0 && FD_ISSET(g_amt_fd_usb, &tmpfds))
1831 {
1832 g_amt_fd_current = &g_amt_fd_usb;
1833 }
1834 else if (g_amt_fd_socket_client >= 0 && FD_ISSET(g_amt_fd_socket_client, &tmpfds))
1835 {
1836 g_amt_fd_current = &g_amt_fd_socket_client;
1837 }
1838 else
1839 {
1840 continue;
1841 }
1842
1843 //¶ÁNVÅжÏÊÇ·ñÊǽâÎöATÃüÁʽ
1844 if(g_amt_at_mode == 1)
1845 {
1846 //½âÎö²¢´¦ÀíATÃüÁî
1847 At_ReceiveData(*g_amt_fd_current, receive_buffer, MAX_PACKET_LENGTH);
1848 continue;
1849 }
1850
1851 read_len = Amt_ReceiveData(*g_amt_fd_current, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
1852
1853 if (read_len > 0)
1854 {
1855 Amt_ComposeAndProcess((unsigned char *)receive_buffer, read_len);
1856 }
1857 else if (read_len == 0)
1858 {
1859 AmtPrintf(AMT_ERROR "%s: read_len = 0, close fd(%d)\n", __FUNCTION__, *g_amt_fd_current);
1860 close(*g_amt_fd_current);
1861 clr_fd(*g_amt_fd_current);
1862 *g_amt_fd_current = -1;
1863 g_amt_fd_current = NULL;
1864 }
1865
1866
1867 //struct timespec ts;
1868 //clock_gettime(CLOCK_MONOTONIC, &ts);
1869 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
1870 //unsigned int status = cpnv_FsGcWait(FS_NVROFS);
1871 //if(status != CPNV_OK)
1872 // AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
1873 //else
1874 //{
1875 // clock_gettime(CLOCK_MONOTONIC, &ts);
1876 // AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
1877 //}
1878
1879 }
1880
1881 free(receive_buffer);
1882 pthread_join(cp_read_thread, NULL);
1883#if 1
1884 cpnv_FsGcWait(FS_NVROFS);
1885 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait return.\n", __FUNCTION__);
1886
1887 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
1888 if(ret != CPNV_OK)
1889 {
1890 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RO nvrofs failed!\n", __FUNCTION__);
1891 return -1;
1892 }
1893#endif
1894 close(g_amt_fd_cp);
1895
1896 if (g_amt_fd_usb >= 0)
1897 {
1898 close(g_amt_fd_usb);
1899 }
1900
1901 //if (g_amt_fd_usb_hotplug >= 0)
1902 {
1903 close(g_amt_fd_usb_hotplug);
1904 }
1905
1906 if (g_amt_fd_socket_client >= 0)
1907 {
1908 close(g_amt_fd_socket_client);
1909 }
1910
1911 if (g_amt_fd_socket_server >= 0)
1912 {
1913 close(g_amt_fd_socket_server);
1914 }
1915
1916 AmtPrintf(AMT_INFO "AMT exit!\n");
1917 return 0;
1918}
1919