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