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