blob: 5280171bf083c06b4818398a6c6988eb191d25da [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));
238 strncpy(s1, str,sizeof(s1) - 1);
239 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)); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
250 strncpy(s2, Begb, sizeof(s2) - 1); //½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®
251 data[Lc] = atof(s2); //string to int
252 ptr = Endb + 1; //Ö¸ÏòϸöÊýµÄÆðʼµØÖ·
253 strncpy(s1, ptr, sizeof(s1) - 1); //È¥µôÒÑת»¯µÄ×Ö·û´®
254 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/**
737 * @brief AMTÏûÏ¢´ò°ü·¢¸øCP
738 * @param[in] fd ÎļþÃèÊö·û
739 * @param[in] msg_id FID
740 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
741 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
742 * @return ³É¹¦·µ»Ø0
743 * @note
744 * @see
745 */
746static int Amt_SendMessageToAmtagent(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
747{
748 // STX TID FID Data Check ETX
749 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
750 unsigned char *pRsp = NULL;
751 unsigned char *pSTX = NULL;
752 unsigned char *pETX = NULL;
753 unsigned char *pTID = NULL;
754 unsigned char *pFID = NULL;
755 unsigned char *pCheck = NULL;
756 unsigned char *pData = NULL;
757 unsigned char CheckSum = 0;
758 unsigned int i = 0;
759
760 pRsp = malloc(CmdCount);
761 if(pRsp == NULL)
762 return -1;
763 memset(pRsp, 0, CmdCount);
764
765 pSTX = &pRsp[0];
766 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
767 pTID = &pRsp[1];
768 pFID = &pRsp[1 + 1];
769 pCheck = &pRsp[1 + 1 + 1 + buf_len];
770 pData = &pRsp[1 + 1 + 1];
771
772 *pSTX = 0x02;
773 *pETX = 0x02;
774 *pTID = (msg_id & 0xFF00) >> 8;
775 *pFID = (msg_id & 0xFF);
776
777 if (buf != NULL && buf_len > 0)
778 {
779 memcpy(pData, buf, buf_len);
780 }
781
782 CheckSum = 0;
783
784 for (i = 0; i < (1 + 1 + buf_len); i++)
785 CheckSum ^= pRsp[1 + i];
786
787 *pCheck = CheckSum;
788
789 Amt_SendDataToAmtagent(fd, pRsp, CmdCount);
790
791 free(pRsp);
792 return 0;
793}
794
795
796/**
797 * @brief AMTÏûÏ¢´ò°ü·¢¸øPC
798 * @param[in] fd ÎļþÃèÊö·û
799 * @param[in] msg_id FID
800 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
801 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
802 * @return ³É¹¦·µ»Ø0
803 * @note
804 * @see
805 */
806static int Amt_SendMessage(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
807{
808 // STX TID FID Data Check ETX
809 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
810 unsigned char *pRsp = NULL;
811 unsigned char *pSTX = NULL;
812 unsigned char *pETX = NULL;
813 unsigned char *pTID = NULL;
814 unsigned char *pFID = NULL;
815 unsigned char *pCheck = NULL;
816 unsigned char *pData = NULL;
817 unsigned char CheckSum = 0;
818 unsigned int i = 0;
819
820 pRsp = malloc(CmdCount);
821 if(pRsp == NULL)
822 return -1;
823 memset(pRsp, 0, CmdCount);
824
825 pSTX = &pRsp[0];
826 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
827 pTID = &pRsp[1];
828 pFID = &pRsp[1 + 1];
829 pCheck = &pRsp[1 + 1 + 1 + buf_len];
830 pData = &pRsp[1 + 1 + 1];
831
832 *pSTX = 0x02;
833 *pETX = 0x02;
834 *pTID = (msg_id & 0xFF00) >> 8;
835 *pFID = (msg_id & 0xFF);
836
837 if (buf != NULL && buf_len > 0)
838 {
839 memcpy(pData, buf, buf_len);
840 }
841
842 CheckSum = 0;
843
844 for (i = 0; i < (1 + 1 + buf_len); i++)
845 CheckSum ^= pRsp[1 + i];
846
847 *pCheck = CheckSum;
848
849 Amt_SendData(fd, pRsp, CmdCount);
850
851 free(pRsp);
852 return 0;
853}
854
lh758261d2023-07-13 05:52:04 -0700855#ifdef USE_CAP_SUPPORT
856/**
857 * @brief AMTÏûÏ¢·¢ËÍ
858 * @param[in] fd ÎļþÃèÊö·û
859 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
860 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
861 * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È
862 * @note
863 * @see
864 */
865static int Amt_SendDataToCAP(int fd, unsigned char* buf, unsigned int buf_len)
866{
867 int write_len = PortSend(fd, (unsigned char*)&buf_len, sizeof(unsigned int), WAIT_ALL);
868
869 if (write_len != sizeof(unsigned int))
870 {
871 AmtPrintf(AMT_ERROR "%s: Failed to send data_len to fd(%d)! write_len = %d.\n",
872 __FUNCTION__, fd, write_len);
873 write_len = 0;
874 }
875 else
876 {
877 if (buf != NULL && buf_len > 0)
878 {
879 write_len = PortSend(fd, buf, buf_len, WAIT_ALL);
880
881 if (write_len != (int)buf_len)
882 {
883 AmtPrintf(AMT_ERROR "%s: Failed to send data to fd(%d)! (write_len = %d) != (buf_len = %d).\n",
884 __FUNCTION__, fd, write_len, buf_len);
885 }
886 }
887 }
888
889 return write_len;
890}
891
892
893/**
894 * @brief AMTÏûÏ¢´ò°ü·¢¸øCAP
895 * @param[in] fd ÎļþÃèÊö·û
896 * @param[in] msg_id FID
897 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
898 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
899 * @return ³É¹¦·µ»Ø0
900 * @note
901 * @see
902 */
903static int Amt_SendMessageToCAP(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
904{
905 // STX TID FID Data Check ETX
906 unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1 + 1;
907 unsigned char *pRsp = NULL;
908 unsigned char *pSTX = NULL;
909 unsigned char *pETX = NULL;
910 unsigned char *pTID = NULL;
911 unsigned char *pFID = NULL;
912 unsigned char *pCheck = NULL;
913 unsigned char *pData = NULL;
914 unsigned char CheckSum = 0;
915 unsigned int i = 0;
916
917 pRsp = malloc(CmdCount);
918 if(pRsp == NULL)
919 return -1;
920 memset(pRsp, 0, CmdCount);
921
922 pSTX = &pRsp[0];
923 pETX = &pRsp[1 + 1 + 1 + buf_len + 1];
924 pTID = &pRsp[1];
925 pFID = &pRsp[1 + 1];
926 pCheck = &pRsp[1 + 1 + 1 + buf_len];
927 pData = &pRsp[1 + 1 + 1];
928
929 *pSTX = 0x02;
930 *pETX = 0x02;
931 *pTID = (msg_id & 0xFF00) >> 8;
932 *pFID = (msg_id & 0xFF);
933
934 if (buf != NULL && buf_len > 0)
935 {
936 memcpy(pData, buf, buf_len);
937 }
938
939 CheckSum = 0;
940
941 for (i = 0; i < (1 + 1 + buf_len); i++)
942 CheckSum ^= pRsp[1 + i];
943
944 *pCheck = CheckSum;
945
946 Amt_SendDataToCAP(fd, pRsp, CmdCount);
947
948 free(pRsp);
949 return 0;
950}
951#endif
952
953
lh9ed821d2023-04-07 01:36:19 -0700954
955/**
956 * @brief AMTÏûÏ¢´¦Àíº¯Êý
957 * @param[in] msg_id FID
958 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
959 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
960 * @return ³É¹¦·µ»Ø0
961 * @note
962 * @see
963 */
964static int Amt_ProcessMessage(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
965{
966 if (msg_id >= FID_WIFI_CMD_NORTN && msg_id <= FID_WIFI_CMD_END) // wifi test
967 {
968 Amt_Wifi_ProcessMsg(msg_id, buf, buf_len);
969 }
970 else if (msg_id == FID_GET_CHIP_PLATFORM)
971 {
972 //
973 if(is_amt_mode())
974 {
975 AmtPrintf(AMT_ERROR "AMT MODE!\n");
976 }
977 else
978 {
979 AmtPrintf(AMT_ERROR "Normal MODE!\n");
980 }
981 unsigned char chipType = 0;
982 AmtPrintf(AMT_INFO "%s: Get chip platform msg_id = %#04x, buf_len = %d.\n",
983 __FUNCTION__, msg_id, buf_len);
984 chipType = 1; /*1:7520V3 2:7100*/
985
986 if (Amt_CreateResponse(msg_id, &chipType, sizeof(unsigned char)) == -1)
987 {
988 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
989 }
990 else
991 {
992 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
993 }
994 }
995 else if (msg_id >= FID_GPS_MODULE_TEST && msg_id <= FID_GPS_CMD_END) // gps test
996 {
997 Amt_Gps_ProcessMsg(msg_id, buf, buf_len);
998 }
999 else if (msg_id >= MSG_DEVICETEST_START && msg_id <= MSG_DEVICETEST_END)
1000 {
1001 Amt_DeviceTest_ProcessMsg(msg_id, buf, buf_len);
1002 }
1003 else if (msg_id == FID_AMT_END)
1004 {
1005 unsigned int status = CPNV_OK;//cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
1006 /*
1007 if(status != CPNV_OK)
1008 {
1009 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
1010 return -1;
1011 }*/
1012
1013 status = cpnv_FsGcWait(FS_NVROFS);
1014 if(status == CPNV_OK)
1015 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait ok.\n", __FUNCTION__);
1016 else
1017 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
1018
1019 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
1020 {
1021 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1022 }
1023 else
1024 {
1025 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1026 }
1027 }
1028 else if (msg_id == FID_AMT_EXIT)
1029 {
1030 unsigned int status = 0;
1031 if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)
1032 {
1033 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1034 }
1035 else
1036 {
1037 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1038 }
1039 }
1040 else if (msg_id == FID_EX_COMMON_SETBOOTMODE)
1041 {
1042 // set normal mode
1043 unsigned char bootmode[] = {0xFF, 0xFF};
1044 unsigned int dwRet = CPNV_OK;
1045
1046 //struct timespec ts;
1047 //clock_gettime(CLOCK_MONOTONIC, &ts);
1048 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
1049 unsigned char mode = 4;
1050 if(buf_len == sizeof(mode))
1051 {
1052 memcpy(&mode, buf, sizeof(mode));
1053 }
1054 AmtPrintf(AMT_INFO "%s: boot mode=%d\n", __FUNCTION__,mode);
1055 switch(mode)
1056 {
1057 case 0: //user mode
1058 {
1059 nv_set_item(NV_RO, "usb_modetype", "user", 1);
1060 nv_commit(NV_RO);
1061 bootmode[0] =0x54;
1062 bootmode[1] =0x00;
1063 break;
1064 }
1065 case 1://debug mode
1066 {
1067 nv_set_item(NV_RO, "usb_modetype", "debug", 1);
1068 nv_commit(NV_RO);
1069 bootmode[0] =0x54;
1070 bootmode[1] =0x01;
1071 break;
1072 }
1073 case 2://factory mode
1074 {
1075 nv_set_item(NV_RO, "usb_modetype", "factory", 1);
1076 nv_commit(NV_RO);
1077 bootmode[0] =0x54;
1078 bootmode[1] =0x02;
1079 break;
1080 }
1081 case 3://amt mode
1082 {
1083 //nv_set_item(NV_RO, "usb_modetype", "amt", 1);
1084 //nv_commit(NV_RO);
1085 bootmode[0] =0x54;
1086 bootmode[1] =0x4D;
1087 break;
1088 }
1089 default:
1090 {
1091 break;
1092 }
1093 }
1094
1095 dwRet = amt_set_bootmode(bootmode);
1096 if (dwRet == CPNV_OK)
1097 {
1098 AmtPrintf(AMT_INFO "%s: set boot mode: sucess.\n", __FUNCTION__);
1099 }
1100 else
1101 {
1102 AmtPrintf(AMT_ERROR "%s: set boot mode: failed.\n", __FUNCTION__);
1103 }
1104 //clock_gettime(CLOCK_MONOTONIC, &ts);
1105 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
1106
1107 if (Amt_CreateResponse(msg_id, (unsigned char*)&dwRet, sizeof(unsigned int)) == -1)
1108 {
1109 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1110 }
1111 else
1112 {
1113 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1114 }
1115 }
1116 else if(msg_id == FID_CHECK_SOFTDOG_CIPHER_TEXT)
1117 {
1118 UINT8 softdog_cipher_texts[OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_SIZE] = {0};
1119 if(!amt_read_nv_item(ABSOFTDOG_CIPHER_TEXT_NVPARAM,softdog_cipher_texts,sizeof(softdog_cipher_texts)))
1120 {
1121 if (Amt_CreateResponse(msg_id, softdog_cipher_texts, sizeof(softdog_cipher_texts)) == -1)
1122 {
1123 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1124 }
1125 else
1126 {
1127 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1128 }
1129 }
1130 else
1131 {
1132 AmtPrintf(AMT_ERROR "%s: read softdog cipher text in address:0x%x failure.\n", __FUNCTION__,OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_ADDRESS);
1133 }
1134 }
1135 else if(msg_id == FID_SET_BAT_DET_FLAG)
1136 {
1137 unsigned int retCode = CPNV_ERROR;
1138 int bat_value = -1;
1139 memcpy(&bat_value, buf, sizeof(int));
1140 retCode =amt_set_batdet_flag(bat_value);
1141
1142 if (retCode == CPNV_OK)
1143 {
1144 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag success.\n", __FUNCTION__);
1145 }
1146 else
1147 {
1148 AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag fail.\n", __FUNCTION__);
1149 }
1150 if (Amt_CreateResponse(msg_id, (unsigned char*)&retCode, sizeof(retCode)) == -1)
1151 {
1152 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1153 }
1154 else
1155 {
1156 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1157 }
1158 }
1159 else if(msg_id == FID_GET_BAT_DET_FLAG)
1160 {
1161 unsigned int retCode = CPNV_ERROR;
1162 int bat_value = 0;
1163 retCode =amt_get_batdet_flag(&bat_value);
1164
1165 if (retCode == CPNV_OK)
1166 {
1167 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag success.bat_value=%d\n", __FUNCTION__,bat_value);
1168 if (Amt_CreateResponse(msg_id, (unsigned char*)&bat_value, sizeof(bat_value)) == -1)
1169 {
1170 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1171 }
1172 else
1173 {
1174 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1175 }
1176 }
1177 else
1178 {
1179 AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag fail.\n", __FUNCTION__);
1180 }
1181 }
1182 else if(msg_id == FID_RINISOFTVERSION)
1183 {
1184 unsigned int retCode = CPNV_ERROR;
1185 char TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN+1]={0};
1186
1187 retCode = cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_INT_BASE_ADDR, (unsigned char*)TmpSoftVersion, ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN);
1188 if (retCode == CPNV_OK)
1189 {
1190 TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN] = '\0';
1191 AmtPrintf(AMT_INFO "%s: inner version=%s\n", __FUNCTION__,TmpSoftVersion);
1192 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpSoftVersion, sizeof(TmpSoftVersion)) == -1)
1193 {
1194 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1195 }
1196 else
1197 {
1198 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1199 }
1200 }
1201 else
1202 {
1203 AmtPrintf(AMT_INFO "%s: get inner version fail!\n", __FUNCTION__);
1204 }
1205 }
1206 else if(msg_id == FID_ROUTSOFTVERSION)
1207 {
1208 unsigned int retCode = CPNV_ERROR;
1209 char TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN+1]={0};
1210
1211 retCode =cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_EXT_BASE_ADDR, (unsigned char *)TmpExtcgmr, ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN);
1212 if (retCode == CPNV_OK)
1213 {
1214 TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN] = '\0';
1215 AmtPrintf(AMT_INFO "%s: outer version=%s\n", __FUNCTION__,TmpExtcgmr);
1216 if (Amt_CreateResponse(msg_id, (unsigned char*)TmpExtcgmr, sizeof(TmpExtcgmr)) == -1)
1217 {
1218 AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);
1219 }
1220 else
1221 {
1222 AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);
1223 }
1224 }
1225 else
1226 {
1227 AmtPrintf(AMT_INFO "%s: get outer version fail!\n", __FUNCTION__);
1228 }
1229 }
1230 else if((msg_id >= FID_FUN_TEST_START)&&(msg_id <= FID_FUN_TEST_END))
1231 {
lh758261d2023-07-13 05:52:04 -07001232 Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);
lh9ed821d2023-04-07 01:36:19 -07001233 }
lh758261d2023-07-13 05:52:04 -07001234 #ifdef USE_CAP_SUPPORT
1235 else if((msg_id >= FID_CAP_TEST_START)&&(msg_id <= FID_CAP_TEST_END))
1236 {
1237 AmtPrintf(AMT_INFO "%s: receive cap msg.\n", __FUNCTION__);
1238 // Send message to CAP
1239 if (Amt_SendMessageToCAP(g_amt_fd_cap, msg_id, buf, buf_len) == -1)
1240 {
1241 AmtPrintf(AMT_ERROR "%s: Failed to send data to cap, msg_id = %#04x, buf_len = %d.\n",
1242 __FUNCTION__, msg_id, buf_len);
1243 }
1244 else
1245 {
1246 AmtPrintf(AMT_INFO "%s: Send data to cap, msg_id = %#04x, buf_len = %d.\n",
1247 __FUNCTION__, msg_id, buf_len);
1248 }
1249 }
1250 #endif
lh9ed821d2023-04-07 01:36:19 -07001251 else // CP message
1252 {
1253 AmtPrintf(AMT_INFO "%s: receive old cp msg.\n", __FUNCTION__);
1254 // Send message to CP
1255 if (Amt_SendMessageToAmtagent(g_amt_fd_cp, msg_id, buf, buf_len) == -1)
1256 {
1257 AmtPrintf(AMT_ERROR "%s: Failed to send data to cp, msg_id = %#04x, buf_len = %d.\n",
1258 __FUNCTION__, msg_id, buf_len);
1259 }
1260 else
1261 {
1262 AmtPrintf(AMT_INFO "%s: Send data to cp, msg_id = %#04x, buf_len = %d.\n",
1263 __FUNCTION__, msg_id, buf_len);
1264 }
1265 }
1266
1267 return 0;
1268}
1269
1270
1271/**
1272 * @brief AMTÏûÏ¢½â°ü
1273 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1274 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1275 * @return N/A
1276 * @note
1277 * @see
1278 */
1279static void Amt_ComposeAndProcess(unsigned char *buf, unsigned int buf_len)
1280{
1281 unsigned char *pSTX = NULL;
1282 unsigned char *pETX = NULL;
1283 unsigned char *pTID = NULL;
1284 unsigned char *pFID = NULL;
1285 unsigned char *pCheck = NULL;
1286 unsigned char *pData = NULL;
1287 unsigned char CheckSum = 0;
1288 unsigned int msg_id;
1289 unsigned int i = 0;
1290 pTID = pTID;
1291 pCheck = pCheck;
1292 pData = pData;
1293
1294 pSTX = &buf[0];
1295 pETX = &buf[buf_len - 1];
1296 pTID = &buf[1];
1297 pFID = &buf[1 + 1];
1298 pCheck = &buf[buf_len - 2];
1299 pData = &buf[1 + 1 + 1];
1300
1301 if (!((0x02 == *pSTX) && (0x02 == *pETX)))
1302 {
1303 return;
1304 }
1305
1306 CheckSum = 0;
1307
1308 for (i = 0; i < (buf_len - 2); i++)
1309 CheckSum ^= buf[1 + i];
1310
1311 if (CheckSum != 0)
1312 {
1313 return;
1314 }
1315
1316 // TIDÓëFID×éºÏʹÓÃÒÑÀ©Õ¹¹¦ÄܺÅ
1317 msg_id = (*pTID << 8) | *pFID;
1318
1319 AmtPrintf(AMT_INFO "%s: msg_id = %#04x, buf_len = %d.\n", __FUNCTION__, msg_id, buf_len);
1320 Amt_ProcessMessage(msg_id, pFID + 1, (buf_len - 1 - 1 - 1 - 1 - 1));
1321}
1322
1323
1324/**
1325 * @brief AMTÏûÏ¢·´À¡
1326 * @param[in] msg_id FID
1327 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1328 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1329 * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1
1330 * @note
1331 * @see
1332 */
1333int Amt_CreateResponse(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)
1334{
1335 if (g_amt_fd_current && *g_amt_fd_current >= 0)
1336 {
1337 return Amt_SendMessage(*g_amt_fd_current, msg_id, buf, buf_len);
1338 }
1339 else
1340 {
1341 AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);
1342 return -1;
1343 }
1344}
1345
1346int Amt_ExecuteCmd(char *pcmd, char *pbuffer, int len)
1347{
1348 FILE *pPipe;
1349
1350 AmtPrintf(AMT_INFO "%s: execute \"%s\"!\n", __FUNCTION__, pcmd);
1351
xf.li742dd022023-06-08 01:43:32 -07001352 if ((pPipe = popen(pcmd, "r")) == NULL)
lh9ed821d2023-04-07 01:36:19 -07001353 {
1354 AmtPrintf(AMT_ERROR "popen \"%s\" failure.\n", pcmd);
1355 return -1;
1356 }
1357
1358 int read_len = fread(pbuffer, 1, len, pPipe);
1359 //AmtPrintf(AMT_INFO "fread, read_len = %d, pbuffer = %s.\n", read_len, pbuffer);
1360
1361 pclose(pPipe);
1362 return read_len;
1363}
1364
1365static int init_cp_channel(void)
1366{
1367 int fd = open(AMT_CP_CHANNEL, O_RDWR);
1368
1369 if (fd < 0)
1370 {
1371 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CP_CHANNEL);
1372 return -1;
1373 }
1374
1375 if(ioctl(fd, RPMSG_CREATE_CHANNEL, (8*1024))!= 0) // ´´½¨Í¨µÀ´óС8K
1376 {
1377 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");
1378 }
1379 if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0) // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ
1380 {
1381 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");
1382 }
1383 if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0) // ×èÈûµÄ·½Ê½¶Á
1384 {
1385 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");
1386 }
1387 return fd;
1388}
1389
1390static int init_usb_device(void)
1391{
1392 int fd = open(AMT_USB_DEV, O_RDWR);
1393
1394 if (fd < 0)
1395 {
1396 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_USB_DEV);
1397 return -1;
1398 }
1399
1400 PortSet(fd);
1401 return fd;
1402}
1403
1404static int init_hotplug_nl(void)
1405{
1406 struct sockaddr_nl snl;
1407 bzero(&snl, sizeof(snl));
1408 snl.nl_family = AF_NETLINK;
1409 snl.nl_pid = getpid();
1410 snl.nl_groups = 1;
1411
1412 int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1413 if (s == -1)
1414 {
1415 AmtPrintf(AMT_ERROR "Can't create hotplug socket!\n");
1416 return -1;
1417 }
1418
1419 if (bind(s, (struct sockaddr *)&snl, sizeof(snl)) < 0)
1420 {
1421 AmtPrintf(AMT_ERROR "Can't bind hotplug socket!\n");
1422 close(s);
1423 return -1;
1424 }
1425
1426 return s;
1427}
1428
1429static int init_socket(int port)
1430{
1431 struct sockaddr_in seraddr;
1432 int sockfd = -1;
1433
1434 AmtPrintf(AMT_INFO "port = %d.\n", port);
1435
1436 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1437 if (sockfd == -1)
1438 {
1439 AmtPrintf(AMT_ERROR "Can't create socket!\n");
1440 return -1;
1441 }
1442
1443 memset(&seraddr,0,sizeof(seraddr));
1444 seraddr.sin_family = AF_INET;
1445 seraddr.sin_addr.s_addr = 0;
1446 seraddr.sin_port = htons(port);
1447
1448 if (bind(sockfd, (struct sockaddr*)(&seraddr), sizeof(seraddr)) < 0)
1449 {
1450 AmtPrintf(AMT_ERROR "Can't bind port %d!\n", port);
1451 close(sockfd);
1452 return -1;
1453 }
1454
1455 if (listen(sockfd, 1) == -1)
1456 {
1457 AmtPrintf(AMT_ERROR "Socket listen failed!\n");
1458 close(sockfd);
1459 return -1;
1460 }
1461
1462 return sockfd;
1463}
1464
lh758261d2023-07-13 05:52:04 -07001465#ifdef USE_CAP_SUPPORT
1466static int init_cap_channel(void)
1467{
1468 int fd = open(AMT_CAP_DEV, O_RDWR);
1469
1470 if (fd < 0)
1471 {
1472 AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CAP_DEV);
1473 return -1;
1474 }
1475
1476 if(ioctl(fd, RPMSG_CREATE_CHANNEL, (4*1024))!= 0) // ´´½¨Í¨µÀ´óС4K
1477 {
1478 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");
1479 }
1480 if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0) // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ
1481 {
1482 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");
1483 }
1484 if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0) // ×èÈûµÄ·½Ê½¶Á
1485 {
1486 AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");
1487 }
1488 return fd;
1489}
1490#endif
1491
1492
lh9ed821d2023-04-07 01:36:19 -07001493static void set_fd(int fd)
1494{
1495 FD_SET(fd, &g_amt_fdsread);
1496 if (fd >= g_amt_fd_max)
1497 {
1498 g_amt_fd_max = fd + 1;
1499 }
1500}
1501
1502static void clr_fd(int fd)
1503{
1504 FD_CLR(fd, &g_amt_fdsread);
1505}
1506
1507static int CreateSendPacket(unsigned short fid, unsigned char *data_buf, int data_len, unsigned char*packet_buf)
1508{
1509 int bRet = 0;
1510 BYTE *pSTX = NULL;
1511 BYTE *pETX = NULL;
1512 //FIDµÄ¸ß×Ö½Ú
1513 BYTE *pHFID = NULL;
1514 //FIDµÄµÍ×Ö½Ú
1515 BYTE *pLFID = NULL;
1516 BYTE *pCheck = NULL;
1517 BYTE *pData = NULL;
1518 BYTE CheckSum = 0;
1519 int i = 0;
1520
1521 // STX FID Data Check ETX
1522 int CmdCount = 1 + 2 + data_len + 1 + 1;
1523
1524 if ((data_len != 0) && (NULL == data_buf))
1525 {
1526 return bRet;
1527 }
1528
1529 pSTX = &packet_buf[0];
1530 pETX = &packet_buf[1 + 2 + data_len + 1];
1531 pHFID = &packet_buf[1];
1532 pLFID = &packet_buf[1+1];
1533 pCheck = &packet_buf[1 + 2 + data_len];
1534 pData = &packet_buf[1 + 2];
1535
1536 *pSTX = 0x02;
1537 *pETX = 0x02;
1538 *pHFID = (fid>>8)&0x00FF;
1539 *pLFID = fid&0x00FF;
1540 memcpy(pData, data_buf, data_len);
1541 CheckSum = 0;
1542 //CheckSumÊÇFIDºÍDATA×ֶεÄУÑé
1543 for (i = 0; i < (2 + data_len); i++)
1544 CheckSum ^= packet_buf[1 + i];
1545 *pCheck = CheckSum;
1546 bRet = 1;
1547 return bRet;
1548}
1549
1550static void send_enter_amt_mode_packet()
1551{
1552 u32 enter_amt_req[5] = {0};
1553 enter_amt_req[0] = 0x0100;
1554 enter_amt_req[1] = 1;
1555 enter_amt_req[2] = 1;
1556 enter_amt_req[3] = 0;
1557 enter_amt_req[4] = 0;
1558
1559 unsigned char enter_amt_packet[25] = {0};
1560 unsigned short fid = 0x005f;
1561 CreateSendPacket(fid,(unsigned char*)enter_amt_req,sizeof(enter_amt_req),enter_amt_packet);
1562 Amt_ComposeAndProcess(enter_amt_packet, sizeof(enter_amt_packet));
1563}
1564
1565static void send_exit_amt_mode_packet()
1566{
1567 u32 exit_amt_req[5] = {0};
1568 exit_amt_req[0] = 0x0100;
1569 exit_amt_req[1] = 0;
1570 exit_amt_req[2] = 1;
1571 exit_amt_req[3] = 0;
1572 exit_amt_req[4] = 0;
1573
1574 unsigned char exit_amt_packet[25] = {0};
1575 unsigned short fid = 0x005f;
1576 CreateSendPacket(fid,(unsigned char*)exit_amt_req,sizeof(exit_amt_req),exit_amt_packet);
1577 Amt_ComposeAndProcess(exit_amt_packet, sizeof(exit_amt_packet));
1578
1579}
1580
1581
1582static void send_tx_start_packet(unsigned int band,unsigned int channel,short int_power,short dec_power)
1583{
1584 typedef struct
1585 {
1586 UINT32 MsgId;
1587 UINT32 wBandNum; // Ƶ¶Î
1588 UINT32 dwEarfcn;
1589 UINT32 wFreq; // ÖÐÐÄÆµµã£¬100kHzΪµ¥Î»
1590 UINT32 wBandWidth; // ´ø¿í£¬100kHzΪµ¥Î»
1591 UINT32 wAntennaPort; // ÌìÏß¶Ë¿Ú £¿£¿£¿
1592 UINT32 wModulation; // µ÷ÖÆ·½Ê½£¬0£ºQPSK£¬1£º16QAM£¬2£º64QAM
1593 UINT32 wRBNum; // RB¸öÊý
1594 UINT32 wRBStartPosition; // RBÆðÊ¼Æ«ÒÆ
1595 UINT32 wSingleWave; // ÊÇ·ñΪµ¥Ôز¨
1596 SINT16 swRfIntPower; //¹¦ÂÊÕûÊý
1597 SINT16 swRfDecPower; //¹¦ÂÊСÊý,[-2 -1 0 1 2 ]´ú±í[-0.5 -0.25 0 0.25 0.5]
1598 }T_zAMT_LTEA_SetTxInit_Req;
1599
1600 T_zAMT_LTEA_SetTxInit_Req stTxInitReq = {0};
1601 stTxInitReq.MsgId = 0x0700;
1602 stTxInitReq.wBandNum = band;
1603 stTxInitReq.dwEarfcn = channel;
1604 stTxInitReq.wFreq = 0;
1605 stTxInitReq.wBandWidth = 9;
1606 stTxInitReq.wAntennaPort = 0;
1607 stTxInitReq.wModulation = 0;
1608 stTxInitReq.wRBNum = 50;
1609 stTxInitReq.wRBStartPosition = 0;
1610 stTxInitReq.wSingleWave = 0;
1611 stTxInitReq.swRfIntPower = int_power;
1612 stTxInitReq.swRfDecPower = dec_power;
1613
1614 unsigned char tx_start_packet[sizeof(stTxInitReq)+5] = {0};
1615 unsigned short fid = 0x005f;
1616 CreateSendPacket(fid,(unsigned char*)&stTxInitReq,sizeof(stTxInitReq),tx_start_packet);
1617 Amt_ComposeAndProcess(tx_start_packet, sizeof(tx_start_packet));
1618
1619}
1620
1621static void send_tx_stop_packet()
1622{
1623 u32 tx_close_req[1] = {0};
1624 tx_close_req[0] = 0x0201;
1625 unsigned char tx_stop_packet[1*4+5] = {0};
1626 unsigned short fid = 0x005f;
1627 CreateSendPacket(fid,(unsigned char*)tx_close_req,sizeof(tx_close_req),tx_stop_packet);
1628 Amt_ComposeAndProcess(tx_stop_packet, sizeof(tx_stop_packet));
1629
1630}
1631
1632static void send_switch_to_user_mode_packet()
1633{
1634 unsigned char bootmode[] = {0x54, 0x00};
1635 unsigned char atmode[] = {0xFF, 0xFF};
1636 nv_set_item(NV_RO, "usb_modetype", "user", 1);
1637 nv_commit(NV_RO);
1638 amt_set_amt_atmode(bootmode,atmode);
1639}
1640
1641static void send_ok(int fd)
1642{
1643 char rep_ok[] = "OK\r\n";
1644 PortSend(fd, (unsigned char*)rep_ok, strlen(rep_ok), WAIT_ALL);
1645}
1646
1647/**
1648 * @brief ATÃüÁî½ÓÊÕ
1649 * @param[in] fd ÎļþÃèÊö·û
1650 * @param[in] buf ½ÓÊÕÊý¾Ýbuffer
1651 * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È
1652 * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È
1653 * @note
1654 * @see
1655 */
1656static int At_ReceiveData(int fd, char* buf, unsigned int buf_len)
1657{
1658 int read_len = 0;
1659 int i = 0;
1660 read_len = PortRecv(fd, buf, 100, NO_WAIT);
1661 AmtPrintf(AMT_INFO "%s: read_len=%d.\n", __FUNCTION__, read_len);
1662 if (read_len > 0)
1663 {
1664 buf[read_len] = '\0';
1665 AmtPrintf(AMT_INFO "%s: receive [%s]\n", __FUNCTION__, buf);
1666 //ת»»ÎªÐ¡Ð´
1667 for(i = 0; i < read_len; i++)
1668 {
1669 char at = tolower(buf[i]);
1670 buf[i] = at;
1671 }
1672 AmtPrintf(AMT_INFO "%s: after tolower [%s]\n", __FUNCTION__, buf);
1673 //at+txstart=band,channel,power
1674 if(strstr(buf,"at+txstart") != NULL)
1675 {
1676 AmtPrintf(AMT_INFO "%s: receive at+txstart\n", __FUNCTION__);
1677 double data[3] = {0.0};
1678 unsigned int band = 0;
1679 unsigned int channel = 0;
1680 short int_power = 0;
1681 short idec_power = 0;
1682 double dec_power = 0.0;
1683 //»ñÈ¡²ÎÊý
1684 buf = buf + strlen("at+txstart=");
1685 if(buf != NULL)
1686 {
1687 str_to_double(data,buf);
1688 band = data[0];
1689 channel = data[1];
1690 int_power = data[2];
1691 dec_power = data[2] - int_power;
1692 idec_power = dec_power*4;
1693 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);
1694 if(band == 0)
1695 {
1696 AmtPrintf(AMT_ERROR "%s: invalid band!\n", __FUNCTION__);
1697 return read_len;
1698
1699 }
1700 if(channel == 0 )
1701 {
1702 AmtPrintf(AMT_ERROR "%s: invalid channel!\n", __FUNCTION__);
1703 return read_len;
1704 }
1705 }
1706 //ת»¯Îª½øÈëamtģʽºÍtxinitÁ½ÌõAMTÖ¸Áî
1707 send_enter_amt_mode_packet();
1708 sleep(1);
1709 send_tx_start_packet(band,channel,int_power,idec_power);
1710 send_ok(fd); //Ö±½Ó¸øATÃüÁî·¢ËÍÕ߻ظ´ok
1711
1712 }
1713 else if(strstr(buf,"at+txstop") != NULL)
1714 {
1715 //ת»¯Îª¹Ø±Õ·¢Éä»úºÍÍ˳öamtģʽÁ½ÌõAMTÖ¸Áî
1716 send_tx_stop_packet();
1717 sleep(1);
1718 send_exit_amt_mode_packet();
1719 send_ok(fd);
1720 }
1721 else if(strstr(buf,"at+zversiontype=3") != NULL)
1722 {
1723 send_switch_to_user_mode_packet();
1724 send_ok(fd);
1725 }
1726 else
1727 {
1728 AmtPrintf(AMT_ERROR "%s: unknown at %s.\n", __FUNCTION__, buf);
1729 }
1730 }
1731 else
1732 {
1733 AmtPrintf(AMT_ERROR "%s: PortRecv from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);
1734 }
1735
1736 return read_len;
1737}
1738
1739/**
1740 * Main Function
1741 */
1742int main (int argc, char *argv[])
1743{
1744 extern char *optarg;
1745 extern int optind;
1746 int c;
1747 int socket_port = -1;
1748
1749 /********************** Get arguments ********************/
1750 while ((c = getopt(argc,argv,"p:r:")) != EOF)
1751 {
1752 switch (c)
1753 {
1754 case 'p':
1755 socket_port = atoi(optarg);
1756 break;
1757 case '?':
1758 AmtPrintf(AMT_INFO "Usage: zte_amt [options]\n");
1759 AmtPrintf(AMT_INFO "-p ipport - Set IP port number to listen on\n");
1760 exit(1);
1761 }
1762 }
1763
1764 AmtPrintf(AMT_INFO "start AMT!\n");
1765
1766 FD_ZERO(&g_amt_fdsread);
1767
1768 /********************** Init AP-CP channel ********************/
1769 g_amt_fd_cp = init_cp_channel();
1770 if (g_amt_fd_cp < 0)
1771 {
1772 return -1;
1773 }
1774 else
1775 {
1776 AmtPrintf(AMT_INFO "g_amt_fd_cp = %d.\n", g_amt_fd_cp);
1777 }
1778
1779 /********************** Init USB device ***********************/
1780 g_amt_fd_usb = init_usb_device();
1781 if (g_amt_fd_usb >= 0)
1782 {
1783 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
1784 set_fd(g_amt_fd_usb);
1785
1786 //Init hotplug netlink
1787 g_amt_fd_usb_hotplug = init_hotplug_nl();
1788 if (g_amt_fd_usb_hotplug >= 0)
1789 {
1790 AmtPrintf(AMT_INFO "g_amt_fd_usb_hotplug = %d.\n", g_amt_fd_usb_hotplug);
1791 set_fd(g_amt_fd_usb_hotplug);
1792 }
1793 }
1794
1795 /*********************** Create socket ***********************/
1796 if (socket_port > 0)
1797 {
1798 g_amt_fd_socket_server = init_socket(socket_port);
1799 if (g_amt_fd_socket_server >= 0)
1800 {
1801 AmtPrintf(AMT_INFO "g_amt_fd_socket_server = %d.\n", g_amt_fd_socket_server);
1802 set_fd(g_amt_fd_socket_server);
1803 }
1804 }
lh758261d2023-07-13 05:52:04 -07001805
1806 #ifdef USE_CAP_SUPPORT
1807 /********************** Init AP-CAP channel ********************/
1808 g_amt_fd_cap = init_cap_channel();
1809 if (g_amt_fd_cap < 0)
1810 {
1811 return -1;
1812 }
1813 else
1814 {
1815 AmtPrintf(AMT_INFO "g_amt_fd_cap = %d.\n", g_amt_fd_cap);
1816 }
1817
1818 // Create CAP read thread
1819 pthread_t cap_read_thread;
1820 if (pthread_create(&cap_read_thread, NULL, ReadFromCAPThread, NULL) != 0)
1821 {
1822 AmtPrintf(AMT_ERROR "Failed to create CAP read thread!\n");
1823 return -1;
1824 }
1825 #endif
lh9ed821d2023-04-07 01:36:19 -07001826
1827 // Create CP read thread
1828 pthread_t cp_read_thread;
1829 if (pthread_create(&cp_read_thread, NULL, ReadFromCPThread, NULL) != 0)
1830 {
1831 AmtPrintf(AMT_ERROR "Failed to create CP read thread!\n");
1832 return -1;
1833 }
1834
1835 // Wifi init
1836 Amt_Wifi_Init();
1837 g_amt_iMsgHandle = msgget(MODULE_ID_AMT, IPC_CREAT|0600);
1838 if(-1 == g_amt_iMsgHandle)
1839 {
1840 AmtPrintf(AMT_ERROR "%s: can not create msg queue for AMT!\n", __FUNCTION__);
1841 return -1;
1842 }
1843
1844 pthread_t msg_recv_thread;
1845 if (pthread_create(&msg_recv_thread, NULL, RecvMsgFromAppThread, NULL) != 0)
1846 {
1847 AmtPrintf(AMT_ERROR "Failed to create RecvMsgFromAppThread thread!\n");
1848 return -1;
1849 }
1850 // malloc receive buffer
1851 char *receive_buffer = malloc(MAX_PACKET_LENGTH);
1852 if (receive_buffer == NULL)
1853 {
1854 return -1;
1855 }
1856
1857 int ret = -1;
1858 int read_len = 0;
1859 fd_set tmpfds;
1860
1861 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
1862 if(ret != CPNV_OK)
1863 {
1864 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);
1865 return -1;
1866 }
1867 else
1868 {
1869 AmtPrintf(AMT_INFO"%s: cpnv_ChangeFsPartitionAttr RW nvrofs ok!\n", __FUNCTION__);
1870 }
1871
1872 //¼ÓÔØaic8800¹Ì¼þ
1873 #if (defined(__AIC_8800DW_CHIP__))
1874 ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ
1875 if(ret < 0)
1876 {
1877 AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);
1878 }
1879 else
1880 {
1881 AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);
1882 }
1883 #endif
1884
1885 g_amt_at_mode = is_amt_atmode();
1886 AmtPrintf(AMT_INFO"%s: g_amt_at_mode=%d\n", __FUNCTION__,g_amt_at_mode);
1887
1888 while (1)
1889 {
1890 tmpfds = g_amt_fdsread;
1891 ret = select(g_amt_fd_max, &tmpfds, NULL, NULL, NULL);
1892 if (ret <= 0)
1893 {
1894 AmtPrintf(AMT_ERROR "select error: %s!\n", strerror(errno));
1895 continue;
1896 }
1897
1898 if (g_amt_fd_usb_hotplug >= 0 && FD_ISSET(g_amt_fd_usb_hotplug, &tmpfds))
1899 {
1900 read_len = PortRecv(g_amt_fd_usb_hotplug, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH - 1, NO_WAIT);
1901 if(read_len >= 0)
1902 receive_buffer[read_len] = '\0';
1903
1904 // Ç¿ÖÆÔö¼Ó½áÊø·û, read_len < MAX_PACKET_LENGTH
1905 receive_buffer[MAX_PACKET_LENGTH - 1] = '\0';
1906
1907 if (strstr(receive_buffer, AMT_DETECT_USB_HOTREMOVE))
1908 {
1909 AmtPrintf(AMT_INFO "USB remove!\n");
1910 system("poweroff");
1911 break;
1912 }
1913
1914 if (strstr(receive_buffer, AMT_DETECT_USB_OFFLINE))
1915 {
1916 AmtPrintf(AMT_INFO "USB offline!\n");
1917
1918 if (g_amt_fd_usb >= 0)
1919 {
1920 close(g_amt_fd_usb);
1921 clr_fd(g_amt_fd_usb);
1922 g_amt_fd_usb = -1;
1923 }
1924 }
1925
1926 if (strstr(receive_buffer, AMT_DETECT_USB_ONLINE))
1927 {
1928 AmtPrintf(AMT_INFO "USB online!\n");
1929
1930 if (g_amt_fd_usb >= 0)
1931 {
1932 close(g_amt_fd_usb);
1933 clr_fd(g_amt_fd_usb);
1934 g_amt_fd_usb = -1;
1935 }
1936
1937 g_amt_fd_usb = init_usb_device();
1938 if (g_amt_fd_usb >= 0)
1939 {
1940 AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);
1941 set_fd(g_amt_fd_usb);
1942 }
1943 }
1944 }
1945
1946 if (g_amt_fd_socket_server >= 0 && FD_ISSET(g_amt_fd_socket_server, &tmpfds))
1947 {
1948 struct sockaddr_in cliaddr;
1949 int addrlen = sizeof(cliaddr);
1950
1951 if (g_amt_fd_socket_client >= 0)
1952 {
1953 close(g_amt_fd_socket_client);
1954 clr_fd(g_amt_fd_socket_client);
1955 g_amt_fd_socket_client = -1;
1956 }
1957
1958 if ((g_amt_fd_socket_client = accept(g_amt_fd_socket_server, (struct sockaddr*)&cliaddr, (socklen_t *)&addrlen)) == -1)
1959 {
1960 AmtPrintf(AMT_ERROR "Accept failed!\n");
1961 }
1962 else
1963 {
1964 unsigned long ip = ntohl(cliaddr.sin_addr.s_addr);
1965 AmtPrintf(AMT_INFO "Connect from %lu.%lu.%lu.%lu\n", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, (ip>>0)&0xff);
1966 set_fd(g_amt_fd_socket_client);
1967 }
1968 }
1969
1970 if (g_amt_fd_usb >= 0 && FD_ISSET(g_amt_fd_usb, &tmpfds))
1971 {
1972 g_amt_fd_current = &g_amt_fd_usb;
1973 }
1974 else if (g_amt_fd_socket_client >= 0 && FD_ISSET(g_amt_fd_socket_client, &tmpfds))
1975 {
1976 g_amt_fd_current = &g_amt_fd_socket_client;
1977 }
1978 else
1979 {
1980 continue;
1981 }
1982
1983 //¶ÁNVÅжÏÊÇ·ñÊǽâÎöATÃüÁʽ
1984 if(g_amt_at_mode == 1)
1985 {
1986 //½âÎö²¢´¦ÀíATÃüÁî
1987 At_ReceiveData(*g_amt_fd_current, receive_buffer, MAX_PACKET_LENGTH);
1988 continue;
1989 }
1990
1991 read_len = Amt_ReceiveData(*g_amt_fd_current, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);
1992
1993 if (read_len > 0)
1994 {
1995 Amt_ComposeAndProcess((unsigned char *)receive_buffer, read_len);
1996 }
1997 else if (read_len == 0)
1998 {
1999 AmtPrintf(AMT_ERROR "%s: read_len = 0, close fd(%d)\n", __FUNCTION__, *g_amt_fd_current);
2000 close(*g_amt_fd_current);
2001 clr_fd(*g_amt_fd_current);
2002 *g_amt_fd_current = -1;
2003 g_amt_fd_current = NULL;
2004 }
2005
2006
2007 //struct timespec ts;
2008 //clock_gettime(CLOCK_MONOTONIC, &ts);
2009 //AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);
2010 //unsigned int status = cpnv_FsGcWait(FS_NVROFS);
2011 //if(status != CPNV_OK)
2012 // AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);
2013 //else
2014 //{
2015 // clock_gettime(CLOCK_MONOTONIC, &ts);
2016 // AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);
2017 //}
2018
2019 }
2020
2021 free(receive_buffer);
2022 pthread_join(cp_read_thread, NULL);
2023#if 1
2024 cpnv_FsGcWait(FS_NVROFS);
2025 AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait return.\n", __FUNCTION__);
2026
2027 ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
2028 if(ret != CPNV_OK)
2029 {
2030 AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RO nvrofs failed!\n", __FUNCTION__);
2031 return -1;
2032 }
2033#endif
2034 close(g_amt_fd_cp);
2035
2036 if (g_amt_fd_usb >= 0)
2037 {
2038 close(g_amt_fd_usb);
2039 }
2040
2041 //if (g_amt_fd_usb_hotplug >= 0)
2042 {
2043 close(g_amt_fd_usb_hotplug);
2044 }
2045
2046 if (g_amt_fd_socket_client >= 0)
2047 {
2048 close(g_amt_fd_socket_client);
2049 }
2050
2051 if (g_amt_fd_socket_server >= 0)
2052 {
2053 close(g_amt_fd_socket_server);
2054 }
2055
lh758261d2023-07-13 05:52:04 -07002056 #ifdef USE_CAP_SUPPORT
2057 close(g_amt_fd_cap);
2058 #endif
2059
lh9ed821d2023-04-07 01:36:19 -07002060 AmtPrintf(AMT_INFO "AMT exit!\n");
2061 return 0;
2062}
2063