blob: 438c6c1d8697cfe03dc47e2d2f8b59f122ca41ca [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
xf.libe704612024-05-28 19:09:12 -070095static unsigned int rat = 0;
96static unsigned int ant_num = 0;
97static void send_ok(int fd);
98static void send_error(int fd);
99static void send_query_result(int fd,char* cmd, char* param);
lh9ed821d2023-04-07 01:36:19 -0700100
101
102/**
103 * @brief AMTÏûÏ¢·¢Ë͸øPC
104 * @param[in] fd ÎļþÃèÊö·û
105 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
106 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
107 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
108 * @note
109 * @see
110 */
111static int Amt_SendData(int fd, unsigned char* buf, unsigned int buf_len)
112{
113 int write_len = PortSend(fd, (unsigned char*)&buf_len, sizeof(unsigned int), WAIT_ALL);
114
115 //if (write_len > 0)
116 {
117 write_len = PortSend(fd, buf, buf_len, WAIT_ALL);
118
119 if (write_len <= 0)
120 {
121 AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
122 }
123 }
124 /*
125 else
126 {
127 AmtPrintf(AMT_ERROR "%s: PortSend 'length' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
128 }
129 */
130
131 return write_len;
132}
133
134/**
135 * @brief AMTÏûÏ¢·¢ËÍ
136 * @param[in] fd ÎļþÃèÊö·û
137 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
138 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
139 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
140 * @note
141 * @see
142 */
143static int Amt_SendDataToAmtagent(int fd, unsigned char* buf, unsigned int buf_len)
144{
145 int write_len = PortSend(fd, buf, buf_len, NO_WAIT);
146
lh758261d2023-07-13 05:52:04 -0700147 if (write_len <= 0)
148 {
149 AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);
150 }
lh9ed821d2023-04-07 01:36:19 -0700151 else
152 {
lh758261d2023-07-13 05:52:04 -0700153 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 -0700154 }
lh758261d2023-07-13 05:52:04 -0700155 return write_len;
lh9ed821d2023-04-07 01:36:19 -0700156}
157
158
159/**
160 * @brief AMTÏûÏ¢½ÓÊÕ
161 * @param[in] fd ÎļþÃèÊö·û
162 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
163 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
164 * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È
165 * @note
166 * @see
167 */
168static int Amt_ReceiveData(int fd, unsigned char* buf, unsigned int buf_len)
169{
170 int read_len = PortRecv(fd, buf, sizeof(unsigned int), WAIT_ALL);
171
172 if (read_len > 0)
173 {
174 unsigned int packet_len = *((unsigned int *)buf);
175 if (packet_len > 0 && packet_len <= buf_len)
176 {
177 read_len = PortRecv(fd, buf, packet_len, WAIT_ALL);
178
179 if (read_len <= 0)
180 {
181 AmtPrintf(AMT_ERROR "%s: PortRecv 'data' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
182 }
183 }
184 else
185 {
186 AmtPrintf(AMT_ERROR "%s: fd = %d, packet_len(%d) is wrong.\n", __FUNCTION__, fd, packet_len);
187 read_len = -1;
188 }
189 }
190 else
191 {
192 AmtPrintf(AMT_ERROR "%s: PortRecv 'length' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
193 }
194
195 return read_len;
196}
197
198#if 0
199static void str_to_int( int *data, char *str )
200{
201 int Lc = 0;
202 int Len = 0;
203 char *Begb = NULL;
204 char *Endb = NULL;
205 char *ptr = NULL;
206 char s1[100] = "";
207 char s2[50] = "";
208 memset(s1, 0, sizeof(s1));
209 strcpy(s1, str);
210 strcat(s1, ",");
211 Begb = s1; //ÆðʼµØÖ·
212 Endb = strchr(s1,','); //µÚÒ»¸ö¶ººÅλÖÃ
213 if (Endb == NULL)
214 return;
215 Lc = 0;
216 do
217 {
218 Len = 0;
219 Len = Endb - Begb; //³¤¶È
220 memset(s2, 0, sizeof(s2)); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
221 strncpy(s2, Begb, Len); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
222 data[Lc] = atoi(s2); //string to int
223 ptr = Endb + 1; //Ö¸ÏòϸöÊýµÄÆðʼµØÖ·
224 strcpy(s1, ptr); //È¥µôÒÑת»¯µÄ×Ö·û´®
225 Begb = s1; //ÆðʼµØÖ·¸üÐÂ
226 Endb = strchr(s1,','); //ϸö¶ººÅλÖÃ
227 Lc++;
228 }while(Endb!=NULL);
229}
230#endif
231
232static void str_to_double( double *data, char *str )
233{
234 int Lc = 0;
235 int Len = 0;
236 char *Begb = NULL;
237 char *Endb = NULL;
238 char *ptr = NULL;
239 char s1[100] = "";
240 char s2[50] = "";
241 memset(s1, 0, sizeof(s1));
xf.lice873192023-11-08 17:10:35 -0800242 memcpy(s1, str,sizeof(s1) - 1);
lh9ed821d2023-04-07 01:36:19 -0700243 strcat(s1, ",");
244 Begb = s1; //ÆðʼµØÖ·
245 Endb = strchr(s1,','); //µÚÒ»¸ö¶ººÅλÖÃ
246 if (Endb == NULL)
247 return;
248 Lc = 0;
249 do
250 {
251 Len = 0;
252 Len = Endb - Begb; //³¤¶È
253 memset(s2, 0, sizeof(s2)); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
xf.lice873192023-11-08 17:10:35 -0800254 memcpy(s2, Begb, sizeof(s2) - 1); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
lh9ed821d2023-04-07 01:36:19 -0700255 data[Lc] = atof(s2); //string to int
256 ptr = Endb + 1; //Ö¸ÏòϸöÊýµÄÆðʼµØÖ·
xf.lice873192023-11-08 17:10:35 -0800257 memcpy(s1, ptr, 99); //È¥µôÒÑת»¯µÄ×Ö·û´®
lh9ed821d2023-04-07 01:36:19 -0700258 Begb = s1; //ÆðʼµØÖ·¸üÐÂ
259 Endb = strchr(s1,','); //ϸö¶ººÅλÖÃ
260 Lc++;
261 }while(Endb!=NULL);
262}
263
lh758261d2023-07-13 05:52:04 -0700264#ifdef USE_CAP_SUPPORT
265/**
266 * @brief ¶ÁÈ¡cap²àµÄ·´À¡ÏûÏ¢Ï̺߳¯Êý
267 * @param[in] args Ï̺߳¯Êý²ÎÊý
268 * @return N/A
269 * @note
270 * @see
271 */
272static void* ReadFromCAPThread(void* args)
273{
274 // Read from AP-CAP channel
275 char *receive_buffer = NULL;
276
277 UNUSED(args);
278
279 receive_buffer = malloc(MAX_PACKET_LENGTH);
280 if (receive_buffer == NULL)
281 {
282 return NULL;
283 }
284
285 prctl(PR_SET_NAME, "AmtReadFromCAP");
286
287 while (1)
288 {
289 int read_len = Amt_ReceiveData(g_amt_fd_cap, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
290
291 if (read_len > 0)
292 {
293 AmtPrintf(AMT_INFO "%s: Receive cap data, read_len = %d!.\n", __FUNCTION__, read_len);
294
295 if (g_amt_fd_current && *g_amt_fd_current >= 0)
296 {
297 if(g_amt_at_mode != 1)
298 {
299 Amt_SendData(*g_amt_fd_current, (unsigned char *)receive_buffer, read_len);
300 }
301 }
302 else
303 {
304 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
305 }
306 }
307 }
308
309 free(receive_buffer);
310 return NULL;
311}
312#endif
313
xf.libe704612024-05-28 19:09:12 -0700314/**
315 * @brief ½âÎöAMTÏûϢת»¯ÎªAT×Ö·û´®·¢Ë͸øPC
316 * @param[in] fd ÎļþÃèÊö·û
317 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
318 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
319 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
320 * @note
321 * @see
322 */
323static int At_SendData(int fd, unsigned char* buf, unsigned int buf_len)
324{
325 unsigned char *pSTX = NULL;
326 unsigned char *pETX = NULL;
327 unsigned char *pTID = NULL;
328 unsigned char *pFID = NULL;
329 unsigned char *pCheck = NULL;
330 unsigned char *pData = NULL;
331 unsigned char CheckSum = 0;
332 unsigned int msg_id;
333 unsigned int i = 0;
334 unsigned int phy_msg_id;
335 unsigned int phy_ret;
336 pTID = pTID;
337 pCheck = pCheck;
338 pData = pData;
339
340 pSTX = &buf[0];
341 pETX = &buf[buf_len - 1];
342 pTID = &buf[1];
343 pFID = &buf[1 + 1];
344 pCheck = &buf[buf_len - 2];
345 pData = &buf[1 + 1 + 1];
346
347 if (!((0x02 == *pSTX) && (0x02 == *pETX)))
348 {
349 return -1;
350 }
351
352 CheckSum = 0;
353
354 for (i = 0; i < (buf_len - 2); i++)
355 CheckSum ^= buf[1 + i];
356
357 if (CheckSum != 0)
358 {
359 return -1;
360 }
361
362 // TIDÓëFID×éºÏʹÓÃÒÑÀ©Õ¹¹¦ÄܺÅ
363 msg_id = (*pTID << 8) | *pFID;
364
365 AmtPrintf(AMT_INFO "%s: msg_id = %#04x, buf_len = %d.\n", __FUNCTION__, msg_id, buf_len);
366
367 switch(msg_id)
368 {
369 //LTEÎïÀí²ãAMTÏûÏ¢
370 case FID_LTEA_PHY_MSG:
371 {
372 memcpy(&phy_msg_id, pData, 4);
373 memcpy(&phy_ret, pData + 4, 4);
374 AmtPrintf(AMT_INFO "%s: lte_phy_msg_id = %#04x, lte_phy_ret = %d.\n", __FUNCTION__, phy_msg_id,phy_ret);
375 switch(phy_msg_id)
376 {
377 case 0x505://ZAMT_LTEA_GET_NST_RSRP
378 {
379 typedef struct
380 {
381 UINT32 dwMsgId; //x0505
382 UINT32 dwRet; //=0,±êʶ³É¹¦; ÆäËûÖµ±êʶʧ°Ü
383 UINT16 wAntRsrp[4]; // ×î¶à4¸öÌìÏß½ÓÊÕRSRPÖµ
384 UINT16 wAntRssi[2]; // Ö÷¸¨ÌìÏß½ÓÊÕRSSIÖµµÄÕûÊý²¿·Ö
385 UINT16 wAntRssiDec[2]; // Ö÷¸¨ÌìÏß½ÓÊÕRSSIÖµµÄСÊý²¿·Ö
386 }T_zAMT_LTEA_GetRsrp_Rsp;
387
388 T_zAMT_LTEA_GetRsrp_Rsp stGetRsrp_Rsp = {0};
389
390 if(phy_ret == 0)
391 {
392 memcpy(&stGetRsrp_Rsp, pData,sizeof(stGetRsrp_Rsp));
393 AmtPrintf(AMT_INFO "%s: RSRP[0] = %d, RSRP[1] = %d,RSSI[0] = %d, RSSI[1] = %d,RSSIDec[0] = %d, RSSIDec[1] = %d\n", \
394 __FUNCTION__, stGetRsrp_Rsp.wAntRsrp[0],stGetRsrp_Rsp.wAntRsrp[1],\
395 stGetRsrp_Rsp.wAntRssi[0],stGetRsrp_Rsp.wAntRssi[1],\
396 stGetRsrp_Rsp.wAntRssiDec[0],stGetRsrp_Rsp.wAntRssiDec[1]);\
397 char strRSSI[100] = {0};
398 UINT16 wAnt0RSSI = stGetRsrp_Rsp.wAntRssi[0];
399 UINT16 wAnt0RSSIDec = stGetRsrp_Rsp.wAntRssiDec[0];
400 double dAnt0RSSI = (double)(wAnt0RSSI + (wAnt0RSSIDec/128.0) - 65535);
401 UINT16 wAnt1RSSI = stGetRsrp_Rsp.wAntRssi[1];
402 UINT16 wAnt1RSSIDec = stGetRsrp_Rsp.wAntRssiDec[1];
403 double dAnt1RSSI = (double)(wAnt1RSSI + (wAnt1RSSIDec/128.0) - 65535);
404
405 switch(ant_num)
406 {
407 //»ñÈ¡Ö÷·rssi
408 case 10:
409 {
410 sprintf(strRSSI,"%.2f",dAnt0RSSI);
411 send_query_result(fd, "RSSI",strRSSI);
412 break;
413 }
414 //»ñÈ¡¸¨Â·rssi
415 case 11:
416 {
417 sprintf(strRSSI,"%.2f",dAnt1RSSI);
418 send_query_result(fd, "RSSI",strRSSI);
419 break;
420 }
421 //»ñÈ¡Ö÷¸¨Â·rssi
422 default:
423 {
424 sprintf(strRSSI,"%.2f,%.2f",dAnt0RSSI,dAnt1RSSI);
425 send_query_result(fd, "RSSI",strRSSI);
426 break;
427 }
428 }
429 }
430 else
431 {
432 send_error(fd);
433 }
434
435 break;
436 }
437 case 0x500://ZAMT_LTEA_SET_NST_INIT
438 {
439 if(phy_ret == 0)
440 {
441 send_ok(fd);
442 }
443 else
444 {
445 send_error(fd);
446 }
447 break;
448 }
449 default:
450 {
451 AmtPrintf(AMT_INFO "%s: lte_phy_msg_id = %#04x\n", __FUNCTION__, phy_msg_id);
452 break;
453 }
454 }
455
456 break;
457 }
458 default:
459 {
460 AmtPrintf(AMT_INFO "%s: unknown msg_id = %#04x\n", __FUNCTION__, msg_id);
461 break;
462 }
463 }
464
465 return 1;
466
467}
lh758261d2023-07-13 05:52:04 -0700468
lh9ed821d2023-04-07 01:36:19 -0700469/**
470 * @brief ¶ÁÈ¡cp²àµÄ·´À¡ÏûÏ¢Ï̺߳¯Êý
471 * @param[in] args Ï̺߳¯Êý²ÎÊý
472 * @return N/A
473 * @note
474 * @see
475 */
476static void* ReadFromCPThread(void* args)
477{
478 // Read from AP-CP channel
479 char *receive_buffer = NULL;
480
481 UNUSED(args);
482
483 receive_buffer = malloc(MAX_PACKET_LENGTH);
484 if (receive_buffer == NULL)
485 {
486 return NULL;
487 }
488
489 prctl(PR_SET_NAME, "AmtReadFromCP");
490
491 while (1)
492 {
493 int read_len = Amt_ReceiveData(g_amt_fd_cp, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
494
495 if (read_len > 0)
496 {
497 AmtPrintf(AMT_INFO "%s: Receive cp data, read_len = %d!.\n", __FUNCTION__, read_len);
498
499 if (g_amt_fd_current && *g_amt_fd_current >= 0)
500 {
501 if(g_amt_at_mode != 1)
502 {
503 Amt_SendData(*g_amt_fd_current, (unsigned char *)receive_buffer, read_len);
504 }
xf.libe704612024-05-28 19:09:12 -0700505 else
506 {
507
508 At_SendData(*g_amt_fd_current, (unsigned char *)receive_buffer, read_len);
509 }
lh9ed821d2023-04-07 01:36:19 -0700510 }
511 else
512 {
513 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
514 }
515
xf.libe704612024-05-28 19:09:12 -0700516#if 0
lh9ed821d2023-04-07 01:36:19 -0700517 unsigned int status = cpnv_FsGcWait(FS_NVROFS);
518 if(status != CPNV_OK)
519 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
xf.libe704612024-05-28 19:09:12 -0700520#endif
521 }
lh9ed821d2023-04-07 01:36:19 -0700522 }
523
524 free(receive_buffer);
525 return NULL;
526}
527
528
529/**
530 * @brief ¶ÁÈ¡ÆäËûappµÄÏûÏ¢Ï̺߳¯Êý
531 * @param[in] args Ï̺߳¯Êý²ÎÊý
532 * @return N/A
533 * @note
534 * @see
535 */
536static void* RecvMsgFromAppThread(void* args)
537{
538 int iRet ;
539 MSG_BUF stMsg = {0};
540 LONG msgSize = sizeof(MSG_BUF) - sizeof(LONG);
541
542 UNUSED(args);
543
544 prctl(PR_SET_NAME, "AmtRecvAppMsg");
545 while(1)
546 {
547 iRet = 0;
548 memset(&stMsg, 0x00, sizeof(MSG_BUF));
549
550 iRet = msgrcv(g_amt_iMsgHandle, &stMsg, msgSize, 0, 0);
551 if(iRet >= 0)
552 {
553 AmtPrintf(AMT_INFO "%s: msgcmd = 0x%x,datalen=%d.\n", __FUNCTION__,stMsg.usMsgCmd,stMsg.usDataLen);
554
555 switch (stMsg.usMsgCmd)
556 {
557 case MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP:
558 {
559 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP.\n", __FUNCTION__);
560 int result = AMT_SUCCESS_RET;
561 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_BATTERY_VOLTAGE, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);
562 break;
563 }
564
565 case MSG_CMD_AMT_KEY_TEST_START_RSP:
566 {
567 int result = AMT_ERROR_RET;
568 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
569 result = *pAucDataBuf;
570 if (result == 0)
571 {
572 result = AMT_SUCCESS_RET;
573 }
574 else
575 {
576 result = AMT_ERROR_RET;
577 AmtPrintf(AMT_INFO "%s: KEY_TEST_START fail.", __FUNCTION__);
578 }
579
580 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_START, result, NULL, 0);
581 break;
582 }
583
584 case MSG_CMD_AMT_KEY_TEST_STOP_RSP:
585 {
586 int result = AMT_SUCCESS_RET;
587 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_STOP, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);
588 break;
589 }
590
591 case MSG_CMD_AMT_LCD_TEST_START_RSP:
592 {
593 int result = AMT_ERROR_RET;
594 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
595 result = *pAucDataBuf;
596 if (result == 0)
597 {
598 result = AMT_SUCCESS_RET;
599 }
600 else
601 {
602 result = AMT_ERROR_RET;
603 AmtPrintf(AMT_INFO "%s: LCD_TEST_START fail.", __FUNCTION__);
604 }
605
606 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_START, result, NULL, 0);
607 break;
608 }
609
610 case MSG_CMD_AMT_LCD_TEST_STOP_RSP:
611 {
612 int result = AMT_ERROR_RET;
613 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
614 result = *pAucDataBuf;
615 AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP result=%d\n", __FUNCTION__,result);
616 if (result == 0)
617 {
618 result = AMT_SUCCESS_RET;
619 }
620 else
621 {
622 result = AMT_ERROR_RET;
623 AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP fail.", __FUNCTION__);
624 }
625
626 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_STOP, result, NULL, 0);
627 break;
628 }
629
630 case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_START_RSP:
631 {
632 int result = AMT_ERROR_RET;
633 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
634 result = *pAucDataBuf;
635 if (result == 1)
636 {
637 result = AMT_SUCCESS_RET;
638 }
639 else
640 {
641 result = AMT_ERROR_RET;
642 AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_START fail.", __FUNCTION__);
643 }
644
645 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_START, result, NULL, 0);
646 break;
647 }
648
649 case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_STOP_RSP:
650 {
651 int result = AMT_ERROR_RET;
652 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
653 result = *pAucDataBuf;
654 if (result == 0)
655 {
656 result = AMT_SUCCESS_RET;
657 }
658 else
659 {
660 result = AMT_ERROR_RET;
661 AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_STOP fail.", __FUNCTION__);
662 }
663
664 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_STOP, result, NULL, 0);
665 break;
666 }
667
668 case MSG_CMD_AMT_VIBRATOR_TEST_START_RSP:
669 {
670 int result = AMT_ERROR_RET;
671 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
672 result = *pAucDataBuf;
673 if (result == 1)
674 {
675 result = AMT_SUCCESS_RET;
676 }
677 else
678 {
679 result = AMT_ERROR_RET;
680 AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_START fail.", __FUNCTION__);
681 }
682
683 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_START, result, NULL, 0);
684 break;
685 }
686
687 case MSG_CMD_AMT_VIBRATOR_TEST_STOP_RSP:
688 {
689 int result = AMT_ERROR_RET;
690 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
691 result = *pAucDataBuf;
692 if (result == 0)
693 {
694 result = AMT_SUCCESS_RET;
695 }
696 else
697 {
698 result = AMT_ERROR_RET;
699 AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_STOP fail.", __FUNCTION__);
700 }
701
702 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_STOP, result, NULL, 0);
703 break;
704 }
705
706 case MSG_CMD_AMT_CAMERA_TEST_START_RSP:
707 {
708 int result = AMT_ERROR_RET;
709 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
710 result = *pAucDataBuf;
711 if (result == 1)
712 {
713 result = AMT_SUCCESS_RET;
714 }
715 else
716 {
717 result = AMT_ERROR_RET;
718 AmtPrintf(AMT_INFO "%s: CAMERA_TEST_START fail.", __FUNCTION__);
719 }
720
721 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_START, result, NULL, 0);
722 break;
723 }
724
725 case MSG_CMD_AMT_CAMERA_TEST_STOP_RSP:
726 {
727 int result = AMT_ERROR_RET;
728 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
729 result = *pAucDataBuf;
730 if (result == 0)
731 {
732 result = AMT_SUCCESS_RET;
733 }
734 else
735 {
736 result = AMT_ERROR_RET;
737 AmtPrintf(AMT_INFO "%s: CAMERA_TEST_STOP fail.", __FUNCTION__);
738 }
739
740 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_STOP, result, NULL, 0);
741 break;
742 }
743
744 case MSG_CMD_AMT_SPEAKER_TEST_RSP:
745 {
746 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_SPEAKER_TEST_RSP.\n", __FUNCTION__);
747 int result = AMT_ERROR_RET;
748 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
749 result = *pAucDataBuf;
750 if (result == 1)
751 {
752 result = AMT_SUCCESS_RET;
753 }
754 else
755 {
756 result = AMT_ERROR_RET;
757 AmtPrintf(AMT_INFO "%s: SPEAKER_TEST fail.", __FUNCTION__);
758 }
759
760 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER, result, NULL, 0);
761 break;
762 }
763
764 case MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP:
765 {
766 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP.\n", __FUNCTION__);
767 int result = AMT_SUCCESS_RET;
768 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER_STOP, result, NULL, 0);
769 break;
770 }
771
772 case MSG_CMD_AMT_RECEIVER_TEST_RSP:
773 {
774 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_RECEIVER_TEST_RSP.\n", __FUNCTION__);
775 int result = AMT_ERROR_RET;
776 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
777 result = *pAucDataBuf;
778 if (result == 1)
779 {
780 result = AMT_SUCCESS_RET;
781 }
782 else
783 {
784 result = AMT_ERROR_RET;
785 AmtPrintf(AMT_INFO "%s: RECEIVER_TEST fail.", __FUNCTION__);
786 }
787
788 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER, result, NULL, 0);
789 break;
790 }
791
792 case MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP:
793 {
794 AmtPrintf(AMT_INFO "%s: recv MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP.\n", __FUNCTION__);
795 int result = AMT_SUCCESS_RET;
796 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER_STOP, result, NULL, 0);
797 break;
798 }
799
800 case MSG_CMD_AMT_TP_TEST_START_RSP:
801 {
802 int result = AMT_ERROR_RET;
803 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
804 result = *pAucDataBuf;
805 if (result == 1)
806 {
807 result = AMT_SUCCESS_RET;
808 }
809 else
810 {
811 result = AMT_ERROR_RET;
812 AmtPrintf(AMT_INFO "%s: TP_TEST_START fail.", __FUNCTION__);
813 }
814
815 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_START, result, NULL, 0);
816 break;
817 }
818
819 case MSG_CMD_AMT_TP_TEST_STOP_RSP:
820 {
821 int result = AMT_ERROR_RET;
822 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
823 result = *pAucDataBuf;
824 if (result == 0)
825 {
826 result = AMT_SUCCESS_RET;
827 }
828 else
829 {
830 result = AMT_ERROR_RET;
831 AmtPrintf(AMT_INFO "%s: TP_TEST_STOP fail.", __FUNCTION__);
832 }
833
834 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_STOP, result, NULL, 0);
835 break;
836 }
837
838 case MSG_CMD_AMT_GSENSOR_TEST_START_RSP:
839 {
840 break;
841 }
842
843 case MSG_CMD_AMT_GSENSOR_TEST_STOP_RSP:
844 {
845 break;
846 }
847
848 case MSG_CMD_AMT_WIFI_TEST_START_RSP:
849 {
850 int result = AMT_ERROR_RET;
851 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
852 result = *pAucDataBuf;
853 if (result == 1)
854 {
855 result = AMT_SUCCESS_RET;
856 }
857 else
858 {
859 result = AMT_ERROR_RET;
860 AmtPrintf(AMT_INFO "%s: Wifi test fail!", __FUNCTION__);
861 }
862
863 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_WIFI, result, NULL, 0);
864 break;
865 }
866
867 case MSG_CMD_AMT_FLASHLIGHT_START_RSP:
868 {
869 int result = AMT_ERROR_RET;
870 int* pAucDataBuf = (int*)stMsg.aucDataBuf;
871 result = *pAucDataBuf;
872 if (result == 1)
873 {
874 result = AMT_SUCCESS_RET;
875 }
876 else
877 {
878 result = AMT_ERROR_RET;
879 AmtPrintf(AMT_INFO "%s: flashlight test fail!", __FUNCTION__);
880 }
881
882 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_FLASHLIGHT_START, result, NULL, 0);
883 break;
884 }
885 //gps²âÊÔÏûÏ¢»ØÓ¦
886 case KWATCH_MSG_GPS_RSP:
887 {
888 Amt_Process_Gps_Rsp(&stMsg,FID_GPS_MODULE_TEST);
889 break;
890 }
891 default:
892 break;
893 }
894 }
895 }
896 return NULL;
897}
898
899
900/**
xf.lice873192023-11-08 17:10:35 -0800901 * @brief ¼ÓÔØ°®¿ÆÎ¢WIFI¹Ì¼þ
902 * @param[in] args Ï̺߳¯Êý²ÎÊý
903 * @return N/A
904 * @note
905 * @see
906 */
907static void* LoadWifiFirmwareThread(void* args)
908{
909 int ret = -1;
910 UNUSED(args);
911
912 prctl(PR_SET_NAME, "LoadWifiFirmwareThread");
913
914 #if (defined(__AIC_8800DW_CHIP__))
915 ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ
916 if(ret < 0)
917 {
918 AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);
919 }
920 else
921 {
922 AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);
923 }
924 #endif
925
926 return NULL;
927}
928
929
930/**
lh9ed821d2023-04-07 01:36:19 -0700931 * @brief AMTÏûÏ¢´ò°ü·¢¸øCP
932 * @param[in] fd ÎļþÃèÊö·û
933 * @param[in] msg_id FID
934 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
935 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
936 * @return ³É¹¦·µ»Ø0
937 * @note
938 * @see
939 */
940static int Amt_SendMessageToAmtagent(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
941{
942 // STX TID FID Data Check ETX
943 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
944 unsigned char *pRsp = NULL;
945 unsigned char *pSTX = NULL;
946 unsigned char *pETX = NULL;
947 unsigned char *pTID = NULL;
948 unsigned char *pFID = NULL;
949 unsigned char *pCheck = NULL;
950 unsigned char *pData = NULL;
951 unsigned char CheckSum = 0;
952 unsigned int i = 0;
953
954 pRsp = malloc(CmdCount);
955 if(pRsp == NULL)
956 return -1;
957 memset(pRsp, 0, CmdCount);
958
959 pSTX = &pRsp[0];
960 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
961 pTID = &pRsp[1];
962 pFID = &pRsp[1 + 1];
963 pCheck = &pRsp[1 + 1 + 1 + buf_len];
964 pData = &pRsp[1 + 1 + 1];
965
966 *pSTX = 0x02;
967 *pETX = 0x02;
968 *pTID = (msg_id & 0xFF00) >> 8;
969 *pFID = (msg_id & 0xFF);
970
971 if (buf != NULL && buf_len > 0)
972 {
973 memcpy(pData, buf, buf_len);
974 }
975
976 CheckSum = 0;
977
978 for (i = 0; i < (1 + 1 + buf_len); i++)
979 CheckSum ^= pRsp[1 + i];
980
981 *pCheck = CheckSum;
982
983 Amt_SendDataToAmtagent(fd, pRsp, CmdCount);
984
985 free(pRsp);
986 return 0;
987}
988
989
990/**
991 * @brief AMTÏûÏ¢´ò°ü·¢¸øPC
992 * @param[in] fd ÎļþÃèÊö·û
993 * @param[in] msg_id FID
994 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
995 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
996 * @return ³É¹¦·µ»Ø0
997 * @note
998 * @see
999 */
1000static int Amt_SendMessage(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1001{
1002 // STX TID FID Data Check ETX
1003 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
1004 unsigned char *pRsp = NULL;
1005 unsigned char *pSTX = NULL;
1006 unsigned char *pETX = NULL;
1007 unsigned char *pTID = NULL;
1008 unsigned char *pFID = NULL;
1009 unsigned char *pCheck = NULL;
1010 unsigned char *pData = NULL;
1011 unsigned char CheckSum = 0;
1012 unsigned int i = 0;
1013
1014 pRsp = malloc(CmdCount);
1015 if(pRsp == NULL)
1016 return -1;
1017 memset(pRsp, 0, CmdCount);
1018
1019 pSTX = &pRsp[0];
1020 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
1021 pTID = &pRsp[1];
1022 pFID = &pRsp[1 + 1];
1023 pCheck = &pRsp[1 + 1 + 1 + buf_len];
1024 pData = &pRsp[1 + 1 + 1];
1025
1026 *pSTX = 0x02;
1027 *pETX = 0x02;
1028 *pTID = (msg_id & 0xFF00) >> 8;
1029 *pFID = (msg_id & 0xFF);
1030
1031 if (buf != NULL && buf_len > 0)
1032 {
1033 memcpy(pData, buf, buf_len);
1034 }
1035
1036 CheckSum = 0;
1037
1038 for (i = 0; i < (1 + 1 + buf_len); i++)
1039 CheckSum ^= pRsp[1 + i];
1040
1041 *pCheck = CheckSum;
1042
1043 Amt_SendData(fd, pRsp, CmdCount);
1044
1045 free(pRsp);
1046 return 0;
1047}
1048
lh758261d2023-07-13 05:52:04 -07001049#ifdef USE_CAP_SUPPORT
1050/**
1051 * @brief AMTÏûÏ¢·¢ËÍ
1052 * @param[in] fd ÎļþÃèÊö·û
1053 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1054 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1055 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
1056 * @note
1057 * @see
1058 */
1059static int Amt_SendDataToCAP(int fd, unsigned char* buf, unsigned int buf_len)
1060{
1061 int write_len = PortSend(fd, (unsigned char*)&buf_len, sizeof(unsigned int), WAIT_ALL);
1062
1063 if (write_len != sizeof(unsigned int))
1064 {
1065 AmtPrintf(AMT_ERROR "%s: Failed to send data_len to fd(%d)! write_len = %d.\n",
1066 __FUNCTION__, fd, write_len);
1067 write_len = 0;
1068 }
1069 else
1070 {
1071 if (buf != NULL && buf_len > 0)
1072 {
1073 write_len = PortSend(fd, buf, buf_len, WAIT_ALL);
1074
1075 if (write_len != (int)buf_len)
1076 {
1077 AmtPrintf(AMT_ERROR "%s: Failed to send data to fd(%d)! (write_len = %d) != (buf_len = %d).\n",
1078 __FUNCTION__, fd, write_len, buf_len);
1079 }
1080 }
1081 }
1082
1083 return write_len;
1084}
1085
1086
1087/**
1088 * @brief AMTÏûÏ¢´ò°ü·¢¸øCAP
1089 * @param[in] fd ÎļþÃèÊö·û
1090 * @param[in] msg_id FID
1091 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1092 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1093 * @return ³É¹¦·µ»Ø0
1094 * @note
1095 * @see
1096 */
1097static int Amt_SendMessageToCAP(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1098{
1099 // STX TID FID Data Check ETX
1100 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
1101 unsigned char *pRsp = NULL;
1102 unsigned char *pSTX = NULL;
1103 unsigned char *pETX = NULL;
1104 unsigned char *pTID = NULL;
1105 unsigned char *pFID = NULL;
1106 unsigned char *pCheck = NULL;
1107 unsigned char *pData = NULL;
1108 unsigned char CheckSum = 0;
1109 unsigned int i = 0;
1110
1111 pRsp = malloc(CmdCount);
1112 if(pRsp == NULL)
1113 return -1;
1114 memset(pRsp, 0, CmdCount);
1115
1116 pSTX = &pRsp[0];
1117 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
1118 pTID = &pRsp[1];
1119 pFID = &pRsp[1 + 1];
1120 pCheck = &pRsp[1 + 1 + 1 + buf_len];
1121 pData = &pRsp[1 + 1 + 1];
1122
1123 *pSTX = 0x02;
1124 *pETX = 0x02;
1125 *pTID = (msg_id & 0xFF00) >> 8;
1126 *pFID = (msg_id & 0xFF);
1127
1128 if (buf != NULL && buf_len > 0)
1129 {
1130 memcpy(pData, buf, buf_len);
1131 }
1132
1133 CheckSum = 0;
1134
1135 for (i = 0; i < (1 + 1 + buf_len); i++)
1136 CheckSum ^= pRsp[1 + i];
1137
1138 *pCheck = CheckSum;
1139
1140 Amt_SendDataToCAP(fd, pRsp, CmdCount);
1141
1142 free(pRsp);
1143 return 0;
1144}
1145#endif
1146
1147
lh9ed821d2023-04-07 01:36:19 -07001148
1149/**
1150 * @brief AMTÏûÏ¢´¦Àíº¯Êý
1151 * @param[in] msg_id FID
1152 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1153 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1154 * @return ³É¹¦·µ»Ø0
1155 * @note
1156 * @see
1157 */
1158static int Amt_ProcessMessage(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1159{
1160 if (msg_id >= FID_WIFI_CMD_NORTN && msg_id <= FID_WIFI_CMD_END) // wifi test
1161 {
1162 Amt_Wifi_ProcessMsg(msg_id, buf, buf_len);
1163 }
1164 else if (msg_id == FID_GET_CHIP_PLATFORM)
1165 {
1166 //
1167 if(is_amt_mode())
1168 {
1169 AmtPrintf(AMT_ERROR "AMT MODE!\n");
1170 }
1171 else
1172 {
1173 AmtPrintf(AMT_ERROR "Normal MODE!\n");
1174 }
1175 unsigned char chipType = 0;
1176 AmtPrintf(AMT_INFO "%s: Get chip platform msg_id = %#04x, buf_len = %d.\n",
1177 __FUNCTION__, msg_id, buf_len);
1178 chipType = 1; /*1:7520V3 2:7100*/
1179
1180 if (Amt_CreateResponse(msg_id, &chipType, sizeof(unsigned char)) == -1)
1181 {
1182 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1183 }
1184 else
1185 {
1186 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1187 }
1188 }
1189 else if (msg_id >= FID_GPS_MODULE_TEST && msg_id <= FID_GPS_CMD_END) // gps test
1190 {
1191 Amt_Gps_ProcessMsg(msg_id, buf, buf_len);
1192 }
1193 else if (msg_id >= MSG_DEVICETEST_START && msg_id <= MSG_DEVICETEST_END)
1194 {
1195 Amt_DeviceTest_ProcessMsg(msg_id, buf, buf_len);
1196 }
1197 else if (msg_id == FID_AMT_END)
1198 {
1199 unsigned int status = CPNV_OK;//cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
1200 /*
1201 if(status != CPNV_OK)
1202 {
1203 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
1204 return -1;
1205 }*/
1206
1207 status = cpnv_FsGcWait(FS_NVROFS);
1208 if(status == CPNV_OK)
1209 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait ok.\n", __FUNCTION__);
1210 else
1211 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
1212
1213 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
1214 {
1215 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1216 }
1217 else
1218 {
1219 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1220 }
1221 }
1222 else if (msg_id == FID_AMT_EXIT)
1223 {
1224 unsigned int status = 0;
1225 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
1226 {
1227 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1228 }
1229 else
1230 {
1231 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1232 }
1233 }
1234 else if (msg_id == FID_EX_COMMON_SETBOOTMODE)
1235 {
1236 // set normal mode
1237 unsigned char bootmode[] = {0xFF, 0xFF};
1238 unsigned int dwRet = CPNV_OK;
1239
1240 //struct timespec ts;
1241 //clock_gettime(CLOCK_MONOTONIC, &ts);
1242 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
1243 unsigned char mode = 4;
1244 if(buf_len == sizeof(mode))
1245 {
1246 memcpy(&mode, buf, sizeof(mode));
1247 }
1248 AmtPrintf(AMT_INFO "%s: boot mode=%d\n", __FUNCTION__,mode);
1249 switch(mode)
1250 {
1251 case 0: //user mode
1252 {
1253 nv_set_item(NV_RO, "usb_modetype", "user", 1);
1254 nv_commit(NV_RO);
1255 bootmode[0] =0x54;
1256 bootmode[1] =0x00;
1257 break;
1258 }
1259 case 1://debug mode
1260 {
1261 nv_set_item(NV_RO, "usb_modetype", "debug", 1);
1262 nv_commit(NV_RO);
1263 bootmode[0] =0x54;
1264 bootmode[1] =0x01;
1265 break;
1266 }
1267 case 2://factory mode
1268 {
1269 nv_set_item(NV_RO, "usb_modetype", "factory", 1);
1270 nv_commit(NV_RO);
1271 bootmode[0] =0x54;
1272 bootmode[1] =0x02;
1273 break;
1274 }
1275 case 3://amt mode
1276 {
1277 //nv_set_item(NV_RO, "usb_modetype", "amt", 1);
1278 //nv_commit(NV_RO);
1279 bootmode[0] =0x54;
1280 bootmode[1] =0x4D;
1281 break;
1282 }
1283 default:
1284 {
1285 break;
1286 }
1287 }
1288
1289 dwRet = amt_set_bootmode(bootmode);
1290 if (dwRet == CPNV_OK)
1291 {
1292 AmtPrintf(AMT_INFO "%s: set boot mode: sucess.\n", __FUNCTION__);
1293 }
1294 else
1295 {
1296 AmtPrintf(AMT_ERROR "%s: set boot mode: failed.\n", __FUNCTION__);
1297 }
1298 //clock_gettime(CLOCK_MONOTONIC, &ts);
1299 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
1300
1301 if (Amt_CreateResponse(msg_id, (unsigned char*)&dwRet, sizeof(unsigned int)) == -1)
1302 {
1303 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1304 }
1305 else
1306 {
1307 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1308 }
1309 }
1310 else if(msg_id == FID_CHECK_SOFTDOG_CIPHER_TEXT)
1311 {
1312 UINT8 softdog_cipher_texts[OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_SIZE] = {0};
1313 if(!amt_read_nv_item(ABSOFTDOG_CIPHER_TEXT_NVPARAM,softdog_cipher_texts,sizeof(softdog_cipher_texts)))
1314 {
1315 if (Amt_CreateResponse(msg_id, softdog_cipher_texts, sizeof(softdog_cipher_texts)) == -1)
1316 {
1317 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1318 }
1319 else
1320 {
1321 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1322 }
1323 }
1324 else
1325 {
1326 AmtPrintf(AMT_ERROR "%s: read softdog cipher text in address:0x%x failure.\n", __FUNCTION__,OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_ADDRESS);
1327 }
1328 }
1329 else if(msg_id == FID_SET_BAT_DET_FLAG)
1330 {
1331 unsigned int retCode = CPNV_ERROR;
1332 int bat_value = -1;
1333 memcpy(&bat_value, buf, sizeof(int));
1334 retCode =amt_set_batdet_flag(bat_value);
1335
1336 if (retCode == CPNV_OK)
1337 {
1338 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag success.\n", __FUNCTION__);
1339 }
1340 else
1341 {
1342 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag fail.\n", __FUNCTION__);
1343 }
1344 if (Amt_CreateResponse(msg_id, (unsigned char*)&retCode, sizeof(retCode)) == -1)
1345 {
1346 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1347 }
1348 else
1349 {
1350 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1351 }
1352 }
1353 else if(msg_id == FID_GET_BAT_DET_FLAG)
1354 {
1355 unsigned int retCode = CPNV_ERROR;
1356 int bat_value = 0;
1357 retCode =amt_get_batdet_flag(&bat_value);
1358
1359 if (retCode == CPNV_OK)
1360 {
1361 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag success.bat_value=%d\n", __FUNCTION__,bat_value);
1362 if (Amt_CreateResponse(msg_id, (unsigned char*)&bat_value, sizeof(bat_value)) == -1)
1363 {
1364 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1365 }
1366 else
1367 {
1368 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1369 }
1370 }
1371 else
1372 {
1373 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag fail.\n", __FUNCTION__);
1374 }
1375 }
1376 else if(msg_id == FID_RINISOFTVERSION)
1377 {
1378 unsigned int retCode = CPNV_ERROR;
1379 char TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN+1]={0};
1380
1381 retCode = cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_INT_BASE_ADDR, (unsigned char*)TmpSoftVersion, ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN);
1382 if (retCode == CPNV_OK)
1383 {
1384 TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN] = '\0';
1385 AmtPrintf(AMT_INFO "%s: inner version=%s\n", __FUNCTION__,TmpSoftVersion);
1386 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpSoftVersion, sizeof(TmpSoftVersion)) == -1)
1387 {
1388 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1389 }
1390 else
1391 {
1392 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1393 }
1394 }
1395 else
1396 {
1397 AmtPrintf(AMT_INFO "%s: get inner version fail!\n", __FUNCTION__);
1398 }
1399 }
1400 else if(msg_id == FID_ROUTSOFTVERSION)
1401 {
1402 unsigned int retCode = CPNV_ERROR;
1403 char TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN+1]={0};
1404
1405 retCode =cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_EXT_BASE_ADDR, (unsigned char *)TmpExtcgmr, ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN);
1406 if (retCode == CPNV_OK)
1407 {
1408 TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN] = '\0';
1409 AmtPrintf(AMT_INFO "%s: outer version=%s\n", __FUNCTION__,TmpExtcgmr);
1410 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpExtcgmr, sizeof(TmpExtcgmr)) == -1)
1411 {
1412 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1413 }
1414 else
1415 {
1416 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1417 }
1418 }
1419 else
1420 {
1421 AmtPrintf(AMT_INFO "%s: get outer version fail!\n", __FUNCTION__);
1422 }
1423 }
1424 else if((msg_id >= FID_FUN_TEST_START)&&(msg_id <= FID_FUN_TEST_END))
1425 {
lh758261d2023-07-13 05:52:04 -07001426 Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);
lh9ed821d2023-04-07 01:36:19 -07001427 }
lh758261d2023-07-13 05:52:04 -07001428 #ifdef USE_CAP_SUPPORT
1429 else if((msg_id >= FID_CAP_TEST_START)&&(msg_id <= FID_CAP_TEST_END))
1430 {
1431 AmtPrintf(AMT_INFO "%s: receive cap msg.\n", __FUNCTION__);
1432 // Send message to CAP
1433 if (Amt_SendMessageToCAP(g_amt_fd_cap, msg_id, buf, buf_len) == -1)
1434 {
1435 AmtPrintf(AMT_ERROR "%s: Failed to send data to cap, msg_id = %#04x, buf_len = %d.\n",
1436 __FUNCTION__, msg_id, buf_len);
1437 }
1438 else
1439 {
1440 AmtPrintf(AMT_INFO "%s: Send data to cap, msg_id = %#04x, buf_len = %d.\n",
1441 __FUNCTION__, msg_id, buf_len);
1442 }
1443 }
1444 #endif
lh9ed821d2023-04-07 01:36:19 -07001445 else // CP message
1446 {
1447 AmtPrintf(AMT_INFO "%s: receive old cp msg.\n", __FUNCTION__);
1448 // Send message to CP
1449 if (Amt_SendMessageToAmtagent(g_amt_fd_cp, msg_id, buf, buf_len) == -1)
1450 {
1451 AmtPrintf(AMT_ERROR "%s: Failed to send data to cp, msg_id = %#04x, buf_len = %d.\n",
1452 __FUNCTION__, msg_id, buf_len);
1453 }
1454 else
1455 {
1456 AmtPrintf(AMT_INFO "%s: Send data to cp, msg_id = %#04x, buf_len = %d.\n",
1457 __FUNCTION__, msg_id, buf_len);
1458 }
1459 }
1460
1461 return 0;
1462}
1463
1464
1465/**
1466 * @brief AMTÏûÏ¢½â°ü
1467 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1468 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1469 * @return N/A
1470 * @note
1471 * @see
1472 */
1473static void Amt_ComposeAndProcess(unsigned char *buf, unsigned int buf_len)
1474{
1475 unsigned char *pSTX = NULL;
1476 unsigned char *pETX = NULL;
1477 unsigned char *pTID = NULL;
1478 unsigned char *pFID = NULL;
1479 unsigned char *pCheck = NULL;
1480 unsigned char *pData = NULL;
1481 unsigned char CheckSum = 0;
1482 unsigned int msg_id;
1483 unsigned int i = 0;
1484 pTID = pTID;
1485 pCheck = pCheck;
1486 pData = pData;
1487
1488 pSTX = &buf[0];
1489 pETX = &buf[buf_len - 1];
1490 pTID = &buf[1];
1491 pFID = &buf[1 + 1];
1492 pCheck = &buf[buf_len - 2];
1493 pData = &buf[1 + 1 + 1];
1494
1495 if (!((0x02 == *pSTX) && (0x02 == *pETX)))
1496 {
1497 return;
1498 }
1499
1500 CheckSum = 0;
1501
1502 for (i = 0; i < (buf_len - 2); i++)
1503 CheckSum ^= buf[1 + i];
1504
1505 if (CheckSum != 0)
1506 {
1507 return;
1508 }
1509
1510 // TIDÓëFID×éºÏʹÓÃÒÑÀ©Õ¹¹¦ÄܺÅ
1511 msg_id = (*pTID << 8) | *pFID;
1512
1513 AmtPrintf(AMT_INFO "%s: msg_id = %#04x, buf_len = %d.\n", __FUNCTION__, msg_id, buf_len);
1514 Amt_ProcessMessage(msg_id, pFID + 1, (buf_len - 1 - 1 - 1 - 1 - 1));
1515}
1516
1517
1518/**
1519 * @brief AMTÏûÏ¢·´À¡
1520 * @param[in] msg_id FID
1521 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1522 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1523 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
1524 * @note
1525 * @see
1526 */
1527int Amt_CreateResponse(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1528{
1529 if (g_amt_fd_current && *g_amt_fd_current >= 0)
1530 {
1531 return Amt_SendMessage(*g_amt_fd_current, msg_id, buf, buf_len);
1532 }
1533 else
1534 {
1535 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
1536 return -1;
1537 }
1538}
1539
xf.li84027492024-04-09 00:17:51 -07001540int wfsystem(const char * cmd)
1541{
1542 pid_t ret = 0 ;
1543 ret = soft_system(cmd);
1544 AmtPrintf (AMT_ERROR "[%s]ret = %d, WIFEXITED(ret)=%d, WEXITSTATUS(ret) =%d",cmd, ret , WIFEXITED (ret), WEXITSTATUS (ret));
1545 if (! (-1 != ret && WIFEXITED (ret) && 0 == WEXITSTATUS (ret))) {
1546 return -1;
1547 }
1548 return 0;
1549}
1550
1551int check_wlan (void)
1552{
1553 int find_wlan = -1;
1554 int i = 0, sum = 0;
1555
1556 while (-1 == find_wlan) {
1557 AmtPrintf (AMT_ERROR "finding wlan i=%d, %d s...", i , sum);
1558 find_wlan = wfsystem ("ifconfig wlan0");
1559 if (-1==find_wlan) {
1560 if (sum >= 60)
1561 return -1;
1562 sum += 2 * i;
1563 sleep (2 * i++);
1564 }
1565 }
1566 return 0;
1567}
1568
lh9ed821d2023-04-07 01:36:19 -07001569int Amt_ExecuteCmd(char *pcmd, char *pbuffer, int len)
1570{
1571 FILE *pPipe;
xf.li84027492024-04-09 00:17:51 -07001572 static int wifi_init = 0;
lh9ed821d2023-04-07 01:36:19 -07001573 AmtPrintf(AMT_INFO "%s: execute \"%s\"!\n", __FUNCTION__, pcmd);
xf.li84027492024-04-09 00:17:51 -07001574 if(0 == wifi_init)
1575 {
1576 if(0 == strcmp(pcmd, "ifconfig wlan0 up")){
1577 check_wlan();
1578 wifi_init = 1;
1579 }
1580 }
1581
xf.li742dd022023-06-08 01:43:32 -07001582 if ((pPipe = popen(pcmd, "r")) == NULL)
lh9ed821d2023-04-07 01:36:19 -07001583 {
1584 AmtPrintf(AMT_ERROR "popen \"%s\" failure.\n", pcmd);
1585 return -1;
1586 }
1587
1588 int read_len = fread(pbuffer, 1, len, pPipe);
1589 //AmtPrintf(AMT_INFO "fread, read_len = %d, pbuffer = %s.\n", read_len, pbuffer);
1590
1591 pclose(pPipe);
1592 return read_len;
1593}
1594
1595static int init_cp_channel(void)
1596{
1597 int fd = open(AMT_CP_CHANNEL, O_RDWR);
1598
1599 if (fd < 0)
1600 {
1601 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CP_CHANNEL);
1602 return -1;
1603 }
1604
1605 if(ioctl(fd, RPMSG_CREATE_CHANNEL, (8*1024))!= 0) // ´´½¨Í¨µÀ´óС8K
1606 {
1607 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");
1608 }
1609 if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0) // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ
1610 {
1611 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");
1612 }
1613 if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0) // ×èÈûµÄ·½Ê½¶Á
1614 {
1615 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");
1616 }
1617 return fd;
1618}
1619
1620static int init_usb_device(void)
1621{
1622 int fd = open(AMT_USB_DEV, O_RDWR);
1623
1624 if (fd < 0)
1625 {
1626 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_USB_DEV);
1627 return -1;
1628 }
1629
1630 PortSet(fd);
1631 return fd;
1632}
1633
1634static int init_hotplug_nl(void)
1635{
1636 struct sockaddr_nl snl;
1637 bzero(&snl, sizeof(snl));
1638 snl.nl_family = AF_NETLINK;
1639 snl.nl_pid = getpid();
1640 snl.nl_groups = 1;
1641
1642 int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1643 if (s == -1)
1644 {
1645 AmtPrintf(AMT_ERROR "Can't create hotplug socket!\n");
1646 return -1;
1647 }
1648
1649 if (bind(s, (struct sockaddr *)&snl, sizeof(snl)) < 0)
1650 {
1651 AmtPrintf(AMT_ERROR "Can't bind hotplug socket!\n");
1652 close(s);
1653 return -1;
1654 }
1655
1656 return s;
1657}
1658
1659static int init_socket(int port)
1660{
1661 struct sockaddr_in seraddr;
1662 int sockfd = -1;
1663
1664 AmtPrintf(AMT_INFO "port = %d.\n", port);
1665
1666 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1667 if (sockfd == -1)
1668 {
1669 AmtPrintf(AMT_ERROR "Can't create socket!\n");
1670 return -1;
1671 }
1672
1673 memset(&seraddr,0,sizeof(seraddr));
1674 seraddr.sin_family = AF_INET;
1675 seraddr.sin_addr.s_addr = 0;
1676 seraddr.sin_port = htons(port);
1677
1678 if (bind(sockfd, (struct sockaddr*)(&seraddr), sizeof(seraddr)) < 0)
1679 {
1680 AmtPrintf(AMT_ERROR "Can't bind port %d!\n", port);
1681 close(sockfd);
1682 return -1;
1683 }
1684
1685 if (listen(sockfd, 1) == -1)
1686 {
1687 AmtPrintf(AMT_ERROR "Socket listen failed!\n");
1688 close(sockfd);
1689 return -1;
1690 }
1691
1692 return sockfd;
1693}
1694
lh758261d2023-07-13 05:52:04 -07001695#ifdef USE_CAP_SUPPORT
1696static int init_cap_channel(void)
1697{
1698 int fd = open(AMT_CAP_DEV, O_RDWR);
1699
1700 if (fd < 0)
1701 {
1702 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CAP_DEV);
1703 return -1;
1704 }
1705
1706 if(ioctl(fd, RPMSG_CREATE_CHANNEL, (4*1024))!= 0) // ´´½¨Í¨µÀ´óС4K
1707 {
1708 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");
1709 }
1710 if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0) // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ
1711 {
1712 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");
1713 }
1714 if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0) // ×èÈûµÄ·½Ê½¶Á
1715 {
1716 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");
1717 }
1718 return fd;
1719}
1720#endif
1721
1722
lh9ed821d2023-04-07 01:36:19 -07001723static void set_fd(int fd)
1724{
1725 FD_SET(fd, &g_amt_fdsread);
1726 if (fd >= g_amt_fd_max)
1727 {
1728 g_amt_fd_max = fd + 1;
1729 }
1730}
1731
1732static void clr_fd(int fd)
1733{
1734 FD_CLR(fd, &g_amt_fdsread);
1735}
1736
1737static int CreateSendPacket(unsigned short fid, unsigned char *data_buf, int data_len, unsigned char*packet_buf)
1738{
1739 int bRet = 0;
1740 BYTE *pSTX = NULL;
1741 BYTE *pETX = NULL;
1742 //FIDµÄ¸ß×Ö½Ú
1743 BYTE *pHFID = NULL;
1744 //FIDµÄµÍ×Ö½Ú
1745 BYTE *pLFID = NULL;
1746 BYTE *pCheck = NULL;
1747 BYTE *pData = NULL;
1748 BYTE CheckSum = 0;
1749 int i = 0;
1750
1751 // STX FID Data Check ETX
1752 int CmdCount = 1 + 2 + data_len + 1 + 1;
1753
1754 if ((data_len != 0) && (NULL == data_buf))
1755 {
1756 return bRet;
1757 }
1758
1759 pSTX = &packet_buf[0];
1760 pETX = &packet_buf[1 + 2 + data_len + 1];
1761 pHFID = &packet_buf[1];
1762 pLFID = &packet_buf[1+1];
1763 pCheck = &packet_buf[1 + 2 + data_len];
1764 pData = &packet_buf[1 + 2];
1765
1766 *pSTX = 0x02;
1767 *pETX = 0x02;
1768 *pHFID = (fid>>8)&0x00FF;
1769 *pLFID = fid&0x00FF;
1770 memcpy(pData, data_buf, data_len);
1771 CheckSum = 0;
1772 //CheckSumÊÇFIDºÍDATA×ֶεÄУÑé
1773 for (i = 0; i < (2 + data_len); i++)
1774 CheckSum ^= packet_buf[1 + i];
1775 *pCheck = CheckSum;
1776 bRet = 1;
1777 return bRet;
1778}
1779
1780static void send_enter_amt_mode_packet()
1781{
1782 u32 enter_amt_req[5] = {0};
1783 enter_amt_req[0] = 0x0100;
1784 enter_amt_req[1] = 1;
1785 enter_amt_req[2] = 1;
1786 enter_amt_req[3] = 0;
1787 enter_amt_req[4] = 0;
1788
1789 unsigned char enter_amt_packet[25] = {0};
1790 unsigned short fid = 0x005f;
1791 CreateSendPacket(fid,(unsigned char*)enter_amt_req,sizeof(enter_amt_req),enter_amt_packet);
1792 Amt_ComposeAndProcess(enter_amt_packet, sizeof(enter_amt_packet));
1793}
1794
1795static void send_exit_amt_mode_packet()
1796{
1797 u32 exit_amt_req[5] = {0};
1798 exit_amt_req[0] = 0x0100;
1799 exit_amt_req[1] = 0;
1800 exit_amt_req[2] = 1;
1801 exit_amt_req[3] = 0;
1802 exit_amt_req[4] = 0;
1803
1804 unsigned char exit_amt_packet[25] = {0};
1805 unsigned short fid = 0x005f;
1806 CreateSendPacket(fid,(unsigned char*)exit_amt_req,sizeof(exit_amt_req),exit_amt_packet);
1807 Amt_ComposeAndProcess(exit_amt_packet, sizeof(exit_amt_packet));
1808
1809}
1810
1811
1812static void send_tx_start_packet(unsigned int band,unsigned int channel,short int_power,short dec_power)
1813{
1814 typedef struct
1815 {
1816 UINT32 MsgId;
1817 UINT32 wBandNum; // Ƶ¶Î
1818 UINT32 dwEarfcn;
1819 UINT32 wFreq; // ÖÐÐÄÆµµã£¬100kHzΪµ¥Î»
1820 UINT32 wBandWidth; // ´ø¿í£¬100kHzΪµ¥Î»
1821 UINT32 wAntennaPort; // ÌìÏß¶Ë¿Ú £¿£¿£¿
1822 UINT32 wModulation; // µ÷ÖÆ·½Ê½£¬0£ºQPSK£¬1£º16QAM£¬2£º64QAM
1823 UINT32 wRBNum; // RB¸öÊý
1824 UINT32 wRBStartPosition; // RBÆðÊ¼Æ«ÒÆ
1825 UINT32 wSingleWave; // ÊÇ·ñΪµ¥Ôز¨
1826 SINT16 swRfIntPower; //¹¦ÂÊÕûÊý
1827 SINT16 swRfDecPower; //¹¦ÂÊСÊý,[-2 -1 0 1 2 ]´ú±í[-0.5 -0.25 0 0.25 0.5]
1828 }T_zAMT_LTEA_SetTxInit_Req;
1829
1830 T_zAMT_LTEA_SetTxInit_Req stTxInitReq = {0};
1831 stTxInitReq.MsgId = 0x0700;
1832 stTxInitReq.wBandNum = band;
1833 stTxInitReq.dwEarfcn = channel;
1834 stTxInitReq.wFreq = 0;
1835 stTxInitReq.wBandWidth = 9;
1836 stTxInitReq.wAntennaPort = 0;
1837 stTxInitReq.wModulation = 0;
1838 stTxInitReq.wRBNum = 50;
1839 stTxInitReq.wRBStartPosition = 0;
1840 stTxInitReq.wSingleWave = 0;
1841 stTxInitReq.swRfIntPower = int_power;
1842 stTxInitReq.swRfDecPower = dec_power;
1843
1844 unsigned char tx_start_packet[sizeof(stTxInitReq)+5] = {0};
1845 unsigned short fid = 0x005f;
1846 CreateSendPacket(fid,(unsigned char*)&stTxInitReq,sizeof(stTxInitReq),tx_start_packet);
1847 Amt_ComposeAndProcess(tx_start_packet, sizeof(tx_start_packet));
1848
1849}
1850
1851static void send_tx_stop_packet()
1852{
1853 u32 tx_close_req[1] = {0};
1854 tx_close_req[0] = 0x0201;
1855 unsigned char tx_stop_packet[1*4+5] = {0};
1856 unsigned short fid = 0x005f;
1857 CreateSendPacket(fid,(unsigned char*)tx_close_req,sizeof(tx_close_req),tx_stop_packet);
1858 Amt_ComposeAndProcess(tx_stop_packet, sizeof(tx_stop_packet));
1859
1860}
1861
xf.libe704612024-05-28 19:09:12 -07001862static double lte_channel_to_freq(unsigned int band,unsigned int channel,unsigned int link)
1863{
1864 double freq = 0;
1865 typedef struct
1866 {
1867 int band;
1868 double dl_f_low;
1869 int dl_n_offset;
1870 double ul_f_low;
1871 int ul_n_offset;
1872 }stLteChannelArrangement;
1873
1874 stLteChannelArrangement info[64] = {
1875
1876 { 1, 2110, 0, 1920, 18000 },
1877 { 2, 1930, 600, 1850, 18600 },
1878 { 3, 1805, 1200, 1710, 19200 },
1879 { 4, 2110, 1950, 1710, 19950 },
1880 { 5, 869, 2400, 824, 20400 },
1881 { 6, 875, 2650, 830, 20650 },
1882 { 7, 2620, 2750, 2500, 20750 },
1883 { 8, 925, 3450, 880, 21450 },
1884 { 9, 1844.9, 3800, 1749.9, 21800 },
1885 { 10, 2110, 4150, 1710, 22150 },
1886 { 11, 1475.9, 4750, 1427.9, 22750 },
1887 { 12, 729, 5010, 699, 23010 },
1888 { 13, 746, 5180, 777, 23180 },
1889 { 14, 758, 5280, 788, 23280 },
1890 { 15, 0, 0, 0, 0 },
1891 { 16, 0, 0, 0, 0 },
1892 { 17, 734, 5730, 704, 23730 },
1893 { 18, 860, 5850, 815, 23850 },
1894 { 19, 875, 6000, 830, 24000 },
1895 { 20, 791, 6150, 832, 24150 },
1896 { 21, 1495.9, 6450, 1447.9, 24450 },
1897 { 22, 3510, 6600, 3410, 24600 },
1898 { 23, 2180, 7500, 2000, 25500 },
1899 { 24, 1525, 7700, 1626.5, 25700 },
1900 { 25, 1930, 8040, 1850, 26040 },
1901 { 26, 859, 8690, 814, 26690 },
1902 { 27, 852, 9040, 807, 27040 },
1903 { 28, 758, 9210, 703, 27210 },
1904 { 29, 0, 0, 0, 0 },
1905 { 30, 2350, 9770, 2305, 27660 },
1906 { 31, 462.5, 9870, 452.5, 27760 },
1907 { 32, 0, 0, 0, 0 },
1908 { 33, 1900, 36000, 1900, 36000 },
1909 { 34, 2010, 36200, 2010, 36200 },
1910 { 35, 1850, 36350, 1850, 36350 },
1911 { 36, 1930, 36950, 1930, 36950 },
1912 { 37, 1910, 37550, 1910, 37550 },
1913 { 38, 2570, 37750, 2570, 37750 },
1914 { 39, 1880, 38250, 1880, 38250 },
1915 { 40, 2300, 38650, 2300, 38650 },
1916 { 41, 2496, 39650, 2496, 39650 },
1917 { 42, 3400, 41590, 3400, 41590 },
1918 { 43, 3600, 43590, 3600, 43590 },
1919 { 44, 703, 45590, 703, 45590 },
1920 { 45, 0, 0, 0, 0 },
1921 { 46, 0, 0, 0, 0 },
1922 { 47, 0, 0, 0, 0 },
1923 { 48, 0, 0, 0, 0 },
1924 { 49, 0, 0, 0, 0 },
1925 { 50, 0, 0, 0, 0 },
1926 { 51, 0, 0, 0, 0 },
1927 { 52, 0, 0, 0, 0 },
1928 { 53, 0, 0, 0, 0 },
1929 { 54, 0, 0, 0, 0 },
1930 { 55, 0, 0, 0, 0 },
1931 { 56, 0, 0, 0, 0 },
1932 { 57, 0, 0, 0, 0 },
1933 { 58, 0, 0, 0, 0 },
1934 { 59, 0, 0, 0, 0 },
1935 { 60, 0, 0, 0, 0 },
1936 { 61, 0, 0, 0, 0 },
1937 { 62, 0, 0, 0, 0 },
1938 { 63, 0, 0, 0, 0 },
1939 { 64, 0, 0, 0, 0 },
1940
1941 };
1942
1943 AmtPrintf(AMT_INFO "%s: beore transform band=%d,channel=%d,link=%d,freq=%f\n", __FUNCTION__, band,channel,link,freq);
1944
1945 if (band > 0 && band <= 64)
1946 {
1947 freq = info[band - 1].ul_f_low + 0.1 * (channel - info[band - 1].ul_n_offset);
1948 if(link)
1949 {
1950 freq = info[band -1].dl_f_low + 0.1 * (channel - info[band - 1].dl_n_offset);
1951 }
1952 }
1953 AmtPrintf(AMT_INFO "%s: after transform freq=%f\n", __FUNCTION__, freq);
1954 return freq;
1955}
1956
1957static void send_lte_nst_init_packet(unsigned int band,unsigned int channel,unsigned int band_width)
1958{
1959 typedef struct
1960 {
1961 u32 MsgId;
1962 unsigned short wBandNum[2]; // Ƶ¶Î
1963 unsigned short wFreq[2]; // ÖÐÐÄÆµµã£¬100kHzΪµ¥Î»
1964 unsigned short wBandWidth[2]; // ´ø¿í
1965 unsigned short wAntennaPort[2]; // ÌìÏß¶Ë¿Ú
1966 unsigned short wModulation[2]; // µ÷ÖÆ·½Ê½£¬0£ºQPSK£¬1£º16QAM£¬2£º64QAM
1967 unsigned short wRbNum[2]; // rb num
1968 unsigned short wRbStartPostion[2]; // rb start
1969 unsigned short wSingleCarrier[2]; //0:·¢µ÷ÖÆÐźŠ1:·¢µ¥Ôز¨
1970 }T_zAMT_LTEA_NST_INIT;
1971
1972 unsigned short freq = 0;
1973 freq = (unsigned short)(lte_channel_to_freq(band,channel,1)*10);
1974
1975
1976 T_zAMT_LTEA_NST_INIT stNSTInitReq = {0};
1977 stNSTInitReq.MsgId = 0x0500;
1978 stNSTInitReq.wBandNum[0] = band;
1979 stNSTInitReq.wFreq[0] = freq;
1980 stNSTInitReq.wBandWidth[0] = band_width;
1981 stNSTInitReq.wAntennaPort[0] = 1;
1982 stNSTInitReq.wModulation[0] = 0;
1983 stNSTInitReq.wRbNum[0] = 50;
1984 stNSTInitReq.wRbStartPostion[0] = 0;
1985 stNSTInitReq.wSingleCarrier[0] = 0;
1986
1987 AmtPrintf(AMT_INFO "%s: stNSTInitReq:MsgId=%d,wBandNum[0]=%d,wFreq[0]=%d,wBandWidth[0]=%d\n",\
1988 __FUNCTION__, stNSTInitReq.MsgId,stNSTInitReq.wBandNum[0],stNSTInitReq.wFreq[0],stNSTInitReq.wBandWidth[0]);
1989
1990 AmtPrintf(AMT_INFO "%s: stNSTInitReq:wAntennaPort[0]=%d,wModulation[0]=%d,wRbNum[0]=%d\n",\
1991 __FUNCTION__, stNSTInitReq.wAntennaPort[0],stNSTInitReq.wModulation[0],stNSTInitReq.wRbNum[0]);
1992
1993 AmtPrintf(AMT_INFO "%s: wRbStartPostion[0]=%d,wSingleCarrier[0]=%d\n",\
1994 __FUNCTION__, stNSTInitReq.wRbStartPostion[0],stNSTInitReq.wSingleCarrier[0]);
1995
1996 unsigned char lte_nst_init_packet[sizeof(stNSTInitReq)+5] = {0};
1997 unsigned short fid = 0x005f;
1998 CreateSendPacket(fid,(unsigned char*)&stNSTInitReq,sizeof(stNSTInitReq),lte_nst_init_packet);
1999 Amt_ComposeAndProcess(lte_nst_init_packet, sizeof(lte_nst_init_packet));
2000
2001}
2002
2003static void send_lte_get_rssi_packet()
2004{
2005 u32 lte_get_rssi_req[1] = {0};
2006 lte_get_rssi_req[0] = 0x0505;
2007 unsigned char lte_get_rssi_packet[1*4+5] = {0};
2008 unsigned short fid = 0x005f;
2009 CreateSendPacket(fid,(unsigned char*)lte_get_rssi_req,sizeof(lte_get_rssi_req),lte_get_rssi_packet);
2010 Amt_ComposeAndProcess(lte_get_rssi_packet, sizeof(lte_get_rssi_packet));
2011}
2012
2013static void send_lte_rx_close_packet()
2014{
2015 u32 lte_rx_close_req[1] = {0};
2016 lte_rx_close_req[0] = 0x0301;
2017 unsigned char lte_rx_close_packet[1*4+5] = {0};
2018 unsigned short fid = 0x005f;
2019 CreateSendPacket(fid,(unsigned char*)lte_rx_close_req,sizeof(lte_rx_close_req),lte_rx_close_packet);
2020 Amt_ComposeAndProcess(lte_rx_close_packet, sizeof(lte_rx_close_packet));
2021}
lh9ed821d2023-04-07 01:36:19 -07002022static void send_switch_to_user_mode_packet()
2023{
2024 unsigned char bootmode[] = {0x54, 0x00};
2025 unsigned char atmode[] = {0xFF, 0xFF};
2026 nv_set_item(NV_RO, "usb_modetype", "user", 1);
2027 nv_commit(NV_RO);
2028 amt_set_amt_atmode(bootmode,atmode);
2029}
2030
2031static void send_ok(int fd)
2032{
2033 char rep_ok[] = "OK\r\n";
2034 PortSend(fd, (unsigned char*)rep_ok, strlen(rep_ok), WAIT_ALL);
2035}
2036
xf.libe704612024-05-28 19:09:12 -07002037static void send_error(int fd)
2038{
2039 char rep_ok[] = "ERROR\r\n";
2040 PortSend(fd, (unsigned char*)rep_ok, strlen(rep_ok), WAIT_ALL);
2041}
2042
2043static void send_query_result(int fd,char* cmd, char* param)
2044{
2045 char * at_str = NULL;
2046 int len = 0;
2047
2048 if(param)
2049 {
2050 len = 32 + strlen(cmd) + strlen(param);
2051 at_str = malloc(len);
2052 }
2053 else
2054 {
2055 len = 32 + strlen(cmd);
2056 at_str = malloc(len);
2057 }
2058 assert(at_str);
2059 memset(at_str, 0, len);
2060 if(!param)
2061 sprintf(at_str,"\r\n+%s\r\n\r\nOK\r\n",cmd);
2062 else
2063 sprintf(at_str,"\r\n+%s: %s\r\n\r\nOK\r\n",cmd,param);
2064
2065 PortSend(fd, (unsigned char*)at_str, strlen(at_str), WAIT_ALL);
2066 free(at_str);
2067}
lh9ed821d2023-04-07 01:36:19 -07002068/**
2069 * @brief ATÃüÁî½ÓÊÕ
2070 * @param[in] fd ÎļþÃèÊö·û
2071 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
2072 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
2073 * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È
2074 * @note
2075 * @see
2076 */
2077static int At_ReceiveData(int fd, char* buf, unsigned int buf_len)
2078{
2079 int read_len = 0;
2080 int i = 0;
2081 read_len = PortRecv(fd, buf, 100, NO_WAIT);
2082 AmtPrintf(AMT_INFO "%s: read_len=%d.\n", __FUNCTION__, read_len);
2083 if (read_len > 0)
2084 {
2085 buf[read_len] = '\0';
2086 AmtPrintf(AMT_INFO "%s: receive [%s]\n", __FUNCTION__, buf);
2087 //ת»»ÎªÐ¡Ð´
2088 for(i = 0; i < read_len; i++)
2089 {
2090 char at = tolower(buf[i]);
2091 buf[i] = at;
2092 }
2093 AmtPrintf(AMT_INFO "%s: after tolower [%s]\n", __FUNCTION__, buf);
2094 //at+txstart=band,channel,power
2095 if(strstr(buf,"at+txstart") != NULL)
2096 {
2097 AmtPrintf(AMT_INFO "%s: receive at+txstart\n", __FUNCTION__);
2098 double data[3] = {0.0};
2099 unsigned int band = 0;
2100 unsigned int channel = 0;
2101 short int_power = 0;
2102 short idec_power = 0;
2103 double dec_power = 0.0;
2104 //»ñÈ¡²ÎÊý
2105 buf = buf + strlen("at+txstart=");
2106 if(buf != NULL)
2107 {
2108 str_to_double(data,buf);
2109 band = data[0];
2110 channel = data[1];
2111 int_power = data[2];
2112 dec_power = data[2] - int_power;
2113 idec_power = dec_power*4;
2114 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);
2115 if(band == 0)
2116 {
2117 AmtPrintf(AMT_ERROR "%s: invalid band!\n", __FUNCTION__);
2118 return read_len;
2119
2120 }
2121 if(channel == 0 )
2122 {
2123 AmtPrintf(AMT_ERROR "%s: invalid channel!\n", __FUNCTION__);
2124 return read_len;
2125 }
2126 }
2127 //ת»¯Îª½øÈëamtģʽºÍtxinitÁ½ÌõAMTÖ¸Áî
2128 send_enter_amt_mode_packet();
2129 sleep(1);
2130 send_tx_start_packet(band,channel,int_power,idec_power);
2131 send_ok(fd); //Ö±½Ó¸øATÃüÁî·¢ËÍÕ߻ظ´ok
2132
2133 }
2134 else if(strstr(buf,"at+txstop") != NULL)
2135 {
2136 //ת»¯Îª¹Ø±Õ·¢Éä»úºÍÍ˳öamtģʽÁ½ÌõAMTÖ¸Áî
2137 send_tx_stop_packet();
2138 sleep(1);
2139 send_exit_amt_mode_packet();
2140 send_ok(fd);
2141 }
2142 else if(strstr(buf,"at+zversiontype=3") != NULL)
2143 {
2144 send_switch_to_user_mode_packet();
2145 send_ok(fd);
2146 }
xf.libe704612024-05-28 19:09:12 -07002147 else if(strstr(buf,"at+rxstart") != NULL)
2148 {
2149 //½âÎöATÃüÁîat+rxstart=rat,band,channel,bandwidth,antnum,expect_level
2150 AmtPrintf(AMT_INFO "%s: receive at+rxstart\n", __FUNCTION__);
2151 int data[6] = {0};
2152 //unsigned int rat = 0;
2153 unsigned int band = 0;
2154 unsigned int channel = 0;
2155 unsigned int band_width = 0;
2156 //unsigned int ant_num = 0;
2157 int expect_level = 0;
2158 int ret = 0;
2159
2160 //»ñÈ¡²ÎÊý
2161 buf = buf + strlen("at+rxstart=");
2162 if(buf != NULL)
2163 {
2164 ret = sscanf(buf, "%d,%d,%d,%d,%d,%d", &data[0], &data[1], &data[2], &data[3],&data[4],&data[5]);
2165 if(ret == 6)
2166 {
2167 rat = data[0];
2168 band = data[1];
2169 channel = data[2];
2170 band_width = data[3];
2171 ant_num = data[4];
2172 expect_level = data[5];
2173 AmtPrintf(AMT_INFO "%s: rat=%d,band=%d,channel=%d,band_width=%d,ant_num=%d,expect_level=%d\n", __FUNCTION__, rat,band,channel,band_width,ant_num,expect_level);
2174 //rat(ÖÆÊ½)ȡֵ·¶Î§[0,2] 0:GSM 1:WCDMA 2:LTE
2175 if((rat < 0) || (rat > 2))
2176 {
2177 AmtPrintf(AMT_ERROR "%s: invalid rat=%d[0,2]!\n", __FUNCTION__,rat);
2178 return read_len;
2179 }
2180 if(band == 0)
2181 {
2182 AmtPrintf(AMT_ERROR "%s: invalid band!\n", __FUNCTION__);
2183 return read_len;
2184
2185 }
2186 if(channel == 0 )
2187 {
2188 AmtPrintf(AMT_ERROR "%s: invalid channel!\n", __FUNCTION__);
2189 return read_len;
2190 }
2191 //ant_num·¶Î§[0,1000]
2192 if((ant_num < 0) || (ant_num > 1000))
2193 {
2194 AmtPrintf(AMT_ERROR "%s: invalid ant_num=%d[0,1000]!\n", __FUNCTION__,ant_num);
2195 return read_len;
2196 }
2197
2198 switch(rat)
2199 {
2200 //LTE
2201 case 2:
2202 {
2203 //LTE´ø¿í·¶Î§
2204 /*
2205 typedef enum TDDLTE_bandW
2206 {
2207 RX1point75M = 0,
2208 RX2point25M,
2209 RX3point5M,
2210 RX5M,
2211 RX5point5M,
2212 RX6M,
2213 RX7M,
2214 RX8M,
2215 RX9M,
2216 RX10M,
2217 RX12M,
2218 RX14M,
2219 RX15M,
2220 RX20M,
2221 RX24M,
2222 RX28M
2223 RX1point4M
2224 } TDDLTE_BandWidth;*/
2225 if((band_width < 0) || (band_width > 16))
2226 {
2227 AmtPrintf(AMT_ERROR "%s: invalid LTE band_width=%d[0,16]!\n", __FUNCTION__,band_width);
2228 return read_len;
2229 }
2230 //ת»¯Îª½øÈëamtģʽºÍnstinitÁ½ÌõAMTÖ¸Áî
2231 send_enter_amt_mode_packet();
2232 sleep(1);
2233 send_lte_nst_init_packet(band,channel,band_width);
2234 break;
2235 }
2236 case 0:
2237 case 1:
2238 default:
2239 {
2240 AmtPrintf(AMT_ERROR "%s: rat=%d not support\n", __FUNCTION__,rat);
2241 send_error(fd);
2242 break;
2243 }
2244 }
2245
2246 }
2247 else
2248 {
2249 AmtPrintf(AMT_INFO "%s: at+rxstart param parse error!\n", __FUNCTION__);
2250 return read_len;
2251 }
2252 }
2253 else
2254 {
2255 AmtPrintf(AMT_INFO "%s: at+rxstart param str is empty!\n", __FUNCTION__);
2256 return read_len;
2257 }
2258 }
2259 else if(strstr(buf,"at+rssi") != NULL)
2260 {
2261 //½âÎöATÃüÁîat+rssi
2262 AmtPrintf(AMT_INFO "%s: receive at+rssi\n", __FUNCTION__);
2263 switch(rat)
2264 {
2265 //LTE
2266 case 2:
2267 {
2268 //ת»¯Îª»ñÈ¡RSSIµÄAMTÖ¸Áî
2269 send_lte_get_rssi_packet();
2270 break;
2271 }
2272 case 0:
2273 case 1:
2274 default:
2275 {
2276 AmtPrintf(AMT_ERROR "%s: rat=%d not support\n", __FUNCTION__,rat);
2277 send_error(fd);
2278 break;
2279 }
2280 }
2281 }
2282 else if(strstr(buf,"at+rxstop") != NULL)
2283 {
2284 //ת»¯Îª¹Ø±Õ½ÓÊÕ»úºÍÍ˳öamtģʽÁ½ÌõAMTÖ¸Áî
2285 send_lte_rx_close_packet();
2286 sleep(1);
2287 send_exit_amt_mode_packet();
2288 send_ok(fd);
2289 }
lh9ed821d2023-04-07 01:36:19 -07002290 else
2291 {
2292 AmtPrintf(AMT_ERROR "%s: unknown at %s.\n", __FUNCTION__, buf);
2293 }
2294 }
2295 else
2296 {
2297 AmtPrintf(AMT_ERROR "%s: PortRecv from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
2298 }
2299
2300 return read_len;
2301}
2302
2303/**
2304 * Main Function
2305 */
2306int main (int argc, char *argv[])
2307{
2308 extern char *optarg;
2309 extern int optind;
2310 int c;
2311 int socket_port = -1;
2312
2313 /********************** Get arguments ********************/
2314 while ((c = getopt(argc,argv,"p:r:")) != EOF)
2315 {
2316 switch (c)
2317 {
2318 case 'p':
2319 socket_port = atoi(optarg);
2320 break;
2321 case '?':
2322 AmtPrintf(AMT_INFO "Usage: zte_amt [options]\n");
2323 AmtPrintf(AMT_INFO "-p ipport - Set IP port number to listen on\n");
2324 exit(1);
2325 }
2326 }
2327
2328 AmtPrintf(AMT_INFO "start AMT!\n");
2329
2330 FD_ZERO(&g_amt_fdsread);
2331
2332 /********************** Init AP-CP channel ********************/
2333 g_amt_fd_cp = init_cp_channel();
2334 if (g_amt_fd_cp < 0)
2335 {
2336 return -1;
2337 }
2338 else
2339 {
2340 AmtPrintf(AMT_INFO "g_amt_fd_cp = %d.\n", g_amt_fd_cp);
2341 }
2342
2343 /********************** Init USB device ***********************/
2344 g_amt_fd_usb = init_usb_device();
2345 if (g_amt_fd_usb >= 0)
2346 {
2347 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
2348 set_fd(g_amt_fd_usb);
2349
2350 //Init hotplug netlink
2351 g_amt_fd_usb_hotplug = init_hotplug_nl();
2352 if (g_amt_fd_usb_hotplug >= 0)
2353 {
2354 AmtPrintf(AMT_INFO "g_amt_fd_usb_hotplug = %d.\n", g_amt_fd_usb_hotplug);
2355 set_fd(g_amt_fd_usb_hotplug);
2356 }
2357 }
xf.lice873192023-11-08 17:10:35 -08002358
lh9ed821d2023-04-07 01:36:19 -07002359 /*********************** Create socket ***********************/
2360 if (socket_port > 0)
2361 {
2362 g_amt_fd_socket_server = init_socket(socket_port);
2363 if (g_amt_fd_socket_server >= 0)
2364 {
2365 AmtPrintf(AMT_INFO "g_amt_fd_socket_server = %d.\n", g_amt_fd_socket_server);
2366 set_fd(g_amt_fd_socket_server);
2367 }
2368 }
lh758261d2023-07-13 05:52:04 -07002369
2370 #ifdef USE_CAP_SUPPORT
2371 /********************** Init AP-CAP channel ********************/
2372 g_amt_fd_cap = init_cap_channel();
2373 if (g_amt_fd_cap < 0)
2374 {
2375 return -1;
2376 }
2377 else
2378 {
2379 AmtPrintf(AMT_INFO "g_amt_fd_cap = %d.\n", g_amt_fd_cap);
2380 }
2381
2382 // Create CAP read thread
2383 pthread_t cap_read_thread;
2384 if (pthread_create(&cap_read_thread, NULL, ReadFromCAPThread, NULL) != 0)
2385 {
2386 AmtPrintf(AMT_ERROR "Failed to create CAP read thread!\n");
2387 return -1;
2388 }
2389 #endif
lh9ed821d2023-04-07 01:36:19 -07002390
2391 // Create CP read thread
2392 pthread_t cp_read_thread;
2393 if (pthread_create(&cp_read_thread, NULL, ReadFromCPThread, NULL) != 0)
2394 {
2395 AmtPrintf(AMT_ERROR "Failed to create CP read thread!\n");
2396 return -1;
2397 }
2398
2399 // Wifi init
2400 Amt_Wifi_Init();
2401 g_amt_iMsgHandle = msgget(MODULE_ID_AMT, IPC_CREAT|0600);
2402 if(-1 == g_amt_iMsgHandle)
2403 {
2404 AmtPrintf(AMT_ERROR "%s: can not create msg queue for AMT!\n", __FUNCTION__);
2405 return -1;
2406 }
2407
2408 pthread_t msg_recv_thread;
2409 if (pthread_create(&msg_recv_thread, NULL, RecvMsgFromAppThread, NULL) != 0)
2410 {
2411 AmtPrintf(AMT_ERROR "Failed to create RecvMsgFromAppThread thread!\n");
2412 return -1;
2413 }
2414 // malloc receive buffer
2415 char *receive_buffer = malloc(MAX_PACKET_LENGTH);
2416 if (receive_buffer == NULL)
2417 {
2418 return -1;
2419 }
2420
2421 int ret = -1;
2422 int read_len = 0;
2423 fd_set tmpfds;
2424
2425 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
2426 if(ret != CPNV_OK)
2427 {
2428 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
xf.lie31de8b2023-12-26 23:38:58 -08002429 free(receive_buffer);
lh9ed821d2023-04-07 01:36:19 -07002430 return -1;
2431 }
2432 else
2433 {
2434 AmtPrintf(AMT_INFO"%s: cpnv_ChangeFsPartitionAttr RW nvrofs ok!\n", __FUNCTION__);
2435 }
2436
2437 //¼ÓÔØaic8800¹Ì¼þ
xf.lice873192023-11-08 17:10:35 -08002438 // Create load wifi firmware thread
2439 pthread_t load_wifi_firmware_thread;
2440 if (pthread_create(&load_wifi_firmware_thread, NULL, LoadWifiFirmwareThread, NULL) != 0)
2441 {
2442 AmtPrintf(AMT_ERROR "Failed to create load wifi firmware thread!\n");
xf.lie31de8b2023-12-26 23:38:58 -08002443 free(receive_buffer);
xf.lice873192023-11-08 17:10:35 -08002444 return -1;
2445 }
2446
2447 #if 0
lh9ed821d2023-04-07 01:36:19 -07002448 #if (defined(__AIC_8800DW_CHIP__))
2449 ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ
2450 if(ret < 0)
2451 {
2452 AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);
2453 }
2454 else
2455 {
2456 AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);
2457 }
2458 #endif
xf.lice873192023-11-08 17:10:35 -08002459 #endif
2460
lh9ed821d2023-04-07 01:36:19 -07002461 g_amt_at_mode = is_amt_atmode();
2462 AmtPrintf(AMT_INFO"%s: g_amt_at_mode=%d\n", __FUNCTION__,g_amt_at_mode);
2463
2464 while (1)
2465 {
2466 tmpfds = g_amt_fdsread;
2467 ret = select(g_amt_fd_max, &tmpfds, NULL, NULL, NULL);
2468 if (ret <= 0)
2469 {
2470 AmtPrintf(AMT_ERROR "select error: %s!\n", strerror(errno));
2471 continue;
2472 }
2473
2474 if (g_amt_fd_usb_hotplug >= 0 && FD_ISSET(g_amt_fd_usb_hotplug, &tmpfds))
2475 {
2476 read_len = PortRecv(g_amt_fd_usb_hotplug, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH - 1, NO_WAIT);
2477 if(read_len >= 0)
2478 receive_buffer[read_len] = '\0';
2479
2480 // Ç¿ÖÆÔö¼Ó½áÊø·û, read_len < MAX_PACKET_LENGTH
2481 receive_buffer[MAX_PACKET_LENGTH - 1] = '\0';
2482
2483 if (strstr(receive_buffer, AMT_DETECT_USB_HOTREMOVE))
2484 {
2485 AmtPrintf(AMT_INFO "USB remove!\n");
2486 system("poweroff");
2487 break;
2488 }
2489
2490 if (strstr(receive_buffer, AMT_DETECT_USB_OFFLINE))
2491 {
2492 AmtPrintf(AMT_INFO "USB offline!\n");
2493
2494 if (g_amt_fd_usb >= 0)
2495 {
2496 close(g_amt_fd_usb);
2497 clr_fd(g_amt_fd_usb);
2498 g_amt_fd_usb = -1;
2499 }
2500 }
2501
2502 if (strstr(receive_buffer, AMT_DETECT_USB_ONLINE))
2503 {
2504 AmtPrintf(AMT_INFO "USB online!\n");
2505
2506 if (g_amt_fd_usb >= 0)
2507 {
xf.lice873192023-11-08 17:10:35 -08002508 AmtPrintf(AMT_INFO "amt already open usb,do nothing\n");
2509 //close(g_amt_fd_usb);
2510 //clr_fd(g_amt_fd_usb);
2511 //g_amt_fd_usb = -1;
lh9ed821d2023-04-07 01:36:19 -07002512 }
xf.lice873192023-11-08 17:10:35 -08002513 else
lh9ed821d2023-04-07 01:36:19 -07002514 {
xf.lice873192023-11-08 17:10:35 -08002515 g_amt_fd_usb = init_usb_device();
2516 if (g_amt_fd_usb >= 0)
2517 {
2518 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
2519 set_fd(g_amt_fd_usb);
2520 }
lh9ed821d2023-04-07 01:36:19 -07002521 }
2522 }
2523 }
2524
2525 if (g_amt_fd_socket_server >= 0 && FD_ISSET(g_amt_fd_socket_server, &tmpfds))
2526 {
2527 struct sockaddr_in cliaddr;
2528 int addrlen = sizeof(cliaddr);
2529
2530 if (g_amt_fd_socket_client >= 0)
2531 {
2532 close(g_amt_fd_socket_client);
2533 clr_fd(g_amt_fd_socket_client);
2534 g_amt_fd_socket_client = -1;
2535 }
2536
2537 if ((g_amt_fd_socket_client = accept(g_amt_fd_socket_server, (struct sockaddr*)&cliaddr, (socklen_t *)&addrlen)) == -1)
2538 {
2539 AmtPrintf(AMT_ERROR "Accept failed!\n");
2540 }
2541 else
2542 {
2543 unsigned long ip = ntohl(cliaddr.sin_addr.s_addr);
2544 AmtPrintf(AMT_INFO "Connect from %lu.%lu.%lu.%lu\n", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, (ip>>0)&0xff);
2545 set_fd(g_amt_fd_socket_client);
2546 }
2547 }
2548
2549 if (g_amt_fd_usb >= 0 && FD_ISSET(g_amt_fd_usb, &tmpfds))
2550 {
2551 g_amt_fd_current = &g_amt_fd_usb;
2552 }
2553 else if (g_amt_fd_socket_client >= 0 && FD_ISSET(g_amt_fd_socket_client, &tmpfds))
2554 {
2555 g_amt_fd_current = &g_amt_fd_socket_client;
2556 }
2557 else
2558 {
2559 continue;
2560 }
2561
2562 //¶ÁNVÅжÏÊÇ·ñÊǽâÎöATÃüÁʽ
2563 if(g_amt_at_mode == 1)
2564 {
2565 //½âÎö²¢´¦ÀíATÃüÁî
2566 At_ReceiveData(*g_amt_fd_current, receive_buffer, MAX_PACKET_LENGTH);
2567 continue;
2568 }
2569
2570 read_len = Amt_ReceiveData(*g_amt_fd_current, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
2571
2572 if (read_len > 0)
2573 {
2574 Amt_ComposeAndProcess((unsigned char *)receive_buffer, read_len);
2575 }
2576 else if (read_len == 0)
2577 {
2578 AmtPrintf(AMT_ERROR "%s: read_len = 0, close fd(%d)\n", __FUNCTION__, *g_amt_fd_current);
2579 close(*g_amt_fd_current);
2580 clr_fd(*g_amt_fd_current);
2581 *g_amt_fd_current = -1;
2582 g_amt_fd_current = NULL;
2583 }
2584
2585
2586 //struct timespec ts;
2587 //clock_gettime(CLOCK_MONOTONIC, &ts);
2588 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
2589 //unsigned int status = cpnv_FsGcWait(FS_NVROFS);
2590 //if(status != CPNV_OK)
2591 // AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
2592 //else
2593 //{
2594 // clock_gettime(CLOCK_MONOTONIC, &ts);
2595 // AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
2596 //}
2597
2598 }
2599
2600 free(receive_buffer);
2601 pthread_join(cp_read_thread, NULL);
2602#if 1
2603 cpnv_FsGcWait(FS_NVROFS);
2604 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait return.\n", __FUNCTION__);
2605
2606 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
2607 if(ret != CPNV_OK)
2608 {
2609 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RO nvrofs failed!\n", __FUNCTION__);
2610 return -1;
2611 }
2612#endif
2613 close(g_amt_fd_cp);
2614
2615 if (g_amt_fd_usb >= 0)
2616 {
2617 close(g_amt_fd_usb);
2618 }
2619
2620 //if (g_amt_fd_usb_hotplug >= 0)
2621 {
2622 close(g_amt_fd_usb_hotplug);
2623 }
2624
2625 if (g_amt_fd_socket_client >= 0)
2626 {
2627 close(g_amt_fd_socket_client);
2628 }
2629
2630 if (g_amt_fd_socket_server >= 0)
2631 {
2632 close(g_amt_fd_socket_server);
2633 }
2634
lh758261d2023-07-13 05:52:04 -07002635 #ifdef USE_CAP_SUPPORT
2636 close(g_amt_fd_cap);
2637 #endif
2638
lh9ed821d2023-04-07 01:36:19 -07002639 AmtPrintf(AMT_INFO "AMT exit!\n");
2640 return 0;
2641}
2642