[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit

Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/app/zte_amt/amt.c b/ap/app/zte_amt/amt.c
new file mode 100755
index 0000000..63aa40d
--- /dev/null
+++ b/ap/app/zte_amt/amt.c
@@ -0,0 +1,1839 @@
+/**

+ * 

+ * @file      amt.c

+ * @brief     

+ *            This file is part of FTM.

+ *            AMT´úÀíÓ¦Óòã

+ *            

+ * @details   

+ * @author    Tools Team.

+ * @email     

+ * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.

+ * @warning   

+ * @date      2019/02/02

+ * @version   1.1

+ * @pre       

+ * @post      

+ *            

+ * @par       

+ * Change History :

+ * ---------------------------------------------------------------------------

+ * date        version  author         description

+ * ---------------------------------------------------------------------------

+ * 2015/04/28  1.0      lu.xieji       Create file

+ * 2019/02/02  1.1      jiang.fenglin  ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen

+ * ---------------------------------------------------------------------------

+ * 

+ * 

+ */

+

+#include <stdlib.h>

+#include <stdio.h>

+#include <string.h>

+#include <pthread.h>

+#include <sys/types.h>

+#include <sys/stat.h>

+#include <sys/ioctl.h>

+#include <sys/msg.h>

+#include <fcntl.h>

+#include <unistd.h>

+#include "linux/rpmsg_zx29.h"

+#include <sys/socket.h>

+#include <netdb.h>

+#include <netinet/in.h>

+#include <linux/netlink.h>

+#include <arpa/inet.h>

+#include <netinet/in.h>

+#include <signal.h>

+#include <errno.h>

+#include "port_com.h"

+#include "amt.h"

+#include "os_type_def.h"

+#include "softap_api.h"

+#include "other_msg.h"

+#include "message.h"

+#include "amt_agent_devicetest.h"

+#include "kwatch_msg.h"

+#include "amtnv.h"

+#include "libcpnv.h"

+#include <sys/prctl.h>

+#include "ref_nv_def.h"

+#include "nv_api.h"

+

+

+

+

+/**

+ * Íⲿº¯ÊýÒýÓÃ

+ */

+extern int Amt_Wifi_Init(void);

+extern int Amt_Wifi_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);

+extern int Amt_Gps_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);

+extern int Amt_DeviceTest_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);

+extern int Amt_Process_Gps_Rsp(MSG_BUF *msg,unsigned int msg_id);

+extern int Amt_FuncTest_ProcessMsg(unsigned int msg_id, unsigned char *msg_buf, unsigned int msg_len);

+extern int wifi_ioctl_handle(int cmd);

+

+

+/**

+ * È«¾Ö±äÁ¿ÉùÃ÷

+ */

+static int g_amt_fd_cp = -1;

+static int g_amt_fd_usb = -1;

+static int g_amt_fd_usb_hotplug = -1;

+static int g_amt_fd_socket_client = -1;

+static int g_amt_fd_socket_server = -1;

+static fd_set g_amt_fdsread;

+static int g_amt_fd_max = 0;

+static volatile int *g_amt_fd_current = NULL;

+static int g_amt_iMsgHandle = 0;

+unsigned int g_amt_at_mode = 0;

+

+

+

+/**

+ * @brief AMTÏûÏ¢·¢Ë͸øPC

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È

+ * @note

+ * @see 

+ */

+static int Amt_SendData(int fd, unsigned char* buf, unsigned int buf_len)

+{

+    int write_len = PortSend(fd, (unsigned char*)&buf_len, sizeof(unsigned int), WAIT_ALL);

+

+    //if (write_len > 0)

+    {

+        write_len = PortSend(fd, buf, buf_len, WAIT_ALL);

+

+        if (write_len <= 0)

+        {

+            AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);

+        }

+    }

+	/*

+    else

+    {

+        AmtPrintf(AMT_ERROR "%s: PortSend 'length' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);

+    }

+    */

+

+    return write_len;

+}

+

+/**

+ * @brief AMTÏûÏ¢·¢ËÍ

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ·µ»Ø·¢Ë͵ÄÊý¾Ý³¤¶È

+ * @note

+ * @see 

+ */

+static int Amt_SendDataToAmtagent(int fd, unsigned char* buf, unsigned int buf_len)

+{

+	int  write_len = PortSend(fd, buf, buf_len, NO_WAIT);

+

+        if (write_len <= 0)

+        {

+            AmtPrintf(AMT_ERROR "%s: PortSend 'data' to device(fd = %d): write_len(%d) is wrong.\n", __FUNCTION__, fd, write_len);

+        }

+	else

+	{

+            AmtPrintf(AMT_INFO "%s: PortSend 'data' to device(fd = %d): write_len(%d),buf_len(%d) \n", __FUNCTION__, fd, write_len,buf_len);

+	}

+        return write_len;

+}

+

+

+/**

+ * @brief AMTÏûÏ¢½ÓÊÕ

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È

+ * @note

+ * @see 

+ */

+static int Amt_ReceiveData(int fd, unsigned char* buf, unsigned int buf_len)

+{

+    int read_len = PortRecv(fd, buf, sizeof(unsigned int), WAIT_ALL);

+

+    if (read_len > 0)

+    {

+        unsigned int packet_len = *((unsigned int *)buf);

+        if (packet_len > 0 && packet_len <= buf_len)

+        {

+            read_len = PortRecv(fd, buf, packet_len, WAIT_ALL);

+

+            if (read_len <= 0)

+            {

+                AmtPrintf(AMT_ERROR "%s: PortRecv 'data' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);

+            }

+        }

+        else

+        {

+            AmtPrintf(AMT_ERROR "%s: fd = %d, packet_len(%d) is wrong.\n", __FUNCTION__, fd, packet_len);

+            read_len = -1;

+        }

+    }

+    else

+    {

+        AmtPrintf(AMT_ERROR "%s: PortRecv 'length' from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);

+    }

+

+    return read_len;

+}

+

+#if 0

+static void str_to_int( int *data, char *str )

+{

+    int Lc = 0;

+    int	Len = 0;

+    char *Begb = NULL;

+    char *Endb = NULL;

+    char *ptr = NULL;

+    char s1[100] = "";

+    char s2[50] = "";	

+    memset(s1, 0, sizeof(s1));

+    strcpy(s1, str);

+    strcat(s1, ",");

+    Begb = s1;						//ÆðʼµØÖ·

+    Endb = strchr(s1,',');			//µÚÒ»¸ö¶ººÅλÖÃ

+	if (Endb == NULL)

+		return;

+    Lc = 0;	

+    do

+    {

+        Len = 0;

+        Len = Endb - Begb;			//³¤¶È

+        memset(s2, 0, sizeof(s2));	//½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®

+        strncpy(s2, Begb, Len);		//½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®

+        data[Lc] = atoi(s2);			//string to int

+        ptr = Endb + 1;				//Ö¸ÏòϸöÊýµÄÆðʼµØÖ·

+        strcpy(s1, ptr);			//È¥µôÒÑת»¯µÄ×Ö·û´®

+        Begb = s1;					//ÆðʼµØÖ·¸üÐÂ

+        Endb = strchr(s1,',');		//ϸö¶ººÅλÖÃ

+        Lc++;

+    }while(Endb!=NULL); 

+}

+#endif

+

+static void str_to_double( double *data, char *str )

+{

+    int Lc = 0;

+    int	Len = 0;

+    char *Begb = NULL;

+    char *Endb = NULL;

+    char *ptr = NULL;

+    char s1[100] = "";

+    char s2[50] = "";	

+    memset(s1, 0, sizeof(s1));

+    strncpy(s1, str,sizeof(s1) - 1);

+    strcat(s1, ",");

+    Begb = s1;						//ÆðʼµØÖ·

+    Endb = strchr(s1,',');			//µÚÒ»¸ö¶ººÅλÖÃ

+	if (Endb == NULL)

+		return;

+    Lc = 0;	

+    do

+    {

+        Len = 0;

+        Len = Endb - Begb;			//³¤¶È

+        memset(s2, 0, sizeof(s2));	//½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®

+        strncpy(s2, Begb, sizeof(s2) - 1);		//½ØÈ¡Á½¸ö¶ººÅ¼äµÄ×Ö·û´®

+        data[Lc] = atof(s2);			//string to int

+        ptr = Endb + 1;				//Ö¸ÏòϸöÊýµÄÆðʼµØÖ·

+        strncpy(s1, ptr, sizeof(s1) - 1);			//È¥µôÒÑת»¯µÄ×Ö·û´®

+        Begb = s1;					//ÆðʼµØÖ·¸üÐÂ

+        Endb = strchr(s1,',');		//ϸö¶ººÅλÖÃ

+        Lc++;

+    }while(Endb!=NULL); 

+}

+

+/**

+ * @brief ¶ÁÈ¡cp²àµÄ·´À¡ÏûÏ¢Ï̺߳¯Êý

+ * @param[in] args Ï̺߳¯Êý²ÎÊý

+ * @return N/A

+ * @note

+ * @see 

+ */

+static void* ReadFromCPThread(void* args)

+{

+    // Read from AP-CP channel

+    char *receive_buffer = NULL;

+

+	UNUSED(args);

+	

+	receive_buffer = malloc(MAX_PACKET_LENGTH);

+    if (receive_buffer == NULL)

+    {

+        return NULL;

+    }

+

+	prctl(PR_SET_NAME, "AmtReadFromCP");

+

+    while (1)

+    {

+        int read_len = Amt_ReceiveData(g_amt_fd_cp, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);

+

+        if (read_len > 0)

+        {

+            AmtPrintf(AMT_INFO "%s: Receive cp data, read_len = %d!.\n", __FUNCTION__, read_len);

+

+            if (g_amt_fd_current && *g_amt_fd_current >= 0)

+            {

+                if(g_amt_at_mode != 1)

+                {

+                    Amt_SendData(*g_amt_fd_current, (unsigned char *)receive_buffer, read_len);

+                }

+            }

+            else

+            {

+                AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);

+            }

+

+            

+            unsigned int status = cpnv_FsGcWait(FS_NVROFS);

+            if(status != CPNV_OK)

+                AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);

+        }

+    }

+

+    free(receive_buffer);

+    return NULL;

+}

+

+

+/**

+ * @brief ¶ÁÈ¡ÆäËûappµÄÏûÏ¢Ï̺߳¯Êý

+ * @param[in] args Ï̺߳¯Êý²ÎÊý

+ * @return N/A

+ * @note

+ * @see 

+ */

+static void* RecvMsgFromAppThread(void* args)

+{

+    int iRet ;

+    MSG_BUF stMsg = {0};

+    LONG msgSize =  sizeof(MSG_BUF) - sizeof(LONG);

+

+	UNUSED(args);

+

+    prctl(PR_SET_NAME, "AmtRecvAppMsg");

+    while(1)

+    {

+        iRet = 0;

+        memset(&stMsg, 0x00, sizeof(MSG_BUF));

+

+        iRet = msgrcv(g_amt_iMsgHandle, &stMsg, msgSize, 0, 0);

+        if(iRet >= 0)

+        {

+            AmtPrintf(AMT_INFO "%s: msgcmd = 0x%x,datalen=%d.\n", __FUNCTION__,stMsg.usMsgCmd,stMsg.usDataLen);

+			

+            switch (stMsg.usMsgCmd)

+            {

+            case MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP:

+            {

+                AmtPrintf(AMT_INFO "%s: recv  MSG_CMD_AMT_BATTERY_VOLTAGE_TEST_RSP.\n", __FUNCTION__);

+                int result = AMT_SUCCESS_RET;

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_BATTERY_VOLTAGE, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);

+                break;

+            }

+            

+            case MSG_CMD_AMT_KEY_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: KEY_TEST_START fail.", __FUNCTION__);

+                }

+

+                 Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_START, result, NULL, 0);

+                 break;

+            }

+            

+            case MSG_CMD_AMT_KEY_TEST_STOP_RSP:

+            {

+                int result = AMT_SUCCESS_RET;

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_KEYBOARD_STOP, result, (unsigned char *)stMsg.aucDataBuf, stMsg.usDataLen);

+                break;

+            }

+            

+            case MSG_CMD_AMT_LCD_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: LCD_TEST_START fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_START, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_LCD_TEST_STOP_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+				AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP result=%d\n", __FUNCTION__,result);

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: LCD_TEST_STOP fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCD_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_START fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_START, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_LCD_BACKLIGHT_TEST_STOP_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: LCD_BACKLIGHT_TEST_STOP fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_LCDBACKLIGHT_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_VIBRATOR_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_START fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_START, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_VIBRATOR_TEST_STOP_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: VIBRATOR_TEST_STOP fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_VIBRATOR_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_CAMERA_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: CAMERA_TEST_START fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_START, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_CAMERA_TEST_STOP_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: CAMERA_TEST_STOP fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_CAMERA_BACK_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_SPEAKER_TEST_RSP:

+            {

+                AmtPrintf(AMT_INFO "%s: recv  MSG_CMD_AMT_SPEAKER_TEST_RSP.\n", __FUNCTION__);

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: SPEAKER_TEST fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP:

+            {

+                AmtPrintf(AMT_INFO "%s: recv  MSG_CMD_AMT_SPEAKER_TEST_STOP_RSP.\n", __FUNCTION__);

+                int result = AMT_SUCCESS_RET;

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_SPEAKER_STOP, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_RECEIVER_TEST_RSP:

+            {

+                AmtPrintf(AMT_INFO "%s: recv  MSG_CMD_AMT_RECEIVER_TEST_RSP.\n", __FUNCTION__);

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: RECEIVER_TEST fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP:

+            {

+                AmtPrintf(AMT_INFO "%s: recv  MSG_CMD_AMT_RECEIVER_TEST_STOP_RSP.\n", __FUNCTION__);

+                int result = AMT_SUCCESS_RET;

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_AUDIO_MIC_RECEIVER_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_TP_TEST_START_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: TP_TEST_START fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_START, result, NULL, 0);

+                break;

+            }

+            

+            case MSG_CMD_AMT_TP_TEST_STOP_RSP:

+            {

+                int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+                result =  *pAucDataBuf;

+                if (result ==  0)

+                {

+                    result = AMT_SUCCESS_RET;

+                }

+                else

+                {

+                    result = AMT_ERROR_RET;

+                    AmtPrintf(AMT_INFO "%s: TP_TEST_STOP fail.", __FUNCTION__);

+                }

+

+                Amt_DeviceTest_SendMsg(MSG_DEVICETEST_TP_STOP, result, NULL, 0);

+                break;

+            }

+

+            case MSG_CMD_AMT_GSENSOR_TEST_START_RSP:

+            {

+                break;

+            }

+            

+            case MSG_CMD_AMT_GSENSOR_TEST_STOP_RSP:

+            {

+                break;

+            }

+			

+	        case MSG_CMD_AMT_WIFI_TEST_START_RSP:

+			{

+				int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+				result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                	result = AMT_SUCCESS_RET;

+                }

+				else

+				{

+					result = AMT_ERROR_RET;

+					AmtPrintf(AMT_INFO "%s: Wifi test fail!", __FUNCTION__);

+				}

+

+				Amt_DeviceTest_SendMsg(MSG_DEVICETEST_WIFI, result, NULL, 0);

+				break;

+			}

+

+			case MSG_CMD_AMT_FLASHLIGHT_START_RSP:

+			{

+				int result = AMT_ERROR_RET;

+				int* pAucDataBuf = (int*)stMsg.aucDataBuf;

+				result =  *pAucDataBuf;

+                if (result ==  1)

+                {

+                	result = AMT_SUCCESS_RET;

+                }

+				else

+				{

+					result = AMT_ERROR_RET;

+					AmtPrintf(AMT_INFO "%s: flashlight test fail!", __FUNCTION__);

+				}

+

+				Amt_DeviceTest_SendMsg(MSG_DEVICETEST_FLASHLIGHT_START, result, NULL, 0);

+				break;

+			}

+			//gps²âÊÔÏûÏ¢»ØÓ¦

+            case KWATCH_MSG_GPS_RSP:

+            {

+                Amt_Process_Gps_Rsp(&stMsg,FID_GPS_MODULE_TEST);

+                break;

+            }

+            default:

+                break;

+        }

+      }

+    }

+	return NULL;

+}

+

+

+/**

+ * @brief AMTÏûÏ¢´ò°ü·¢¸øCP

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] msg_id FID

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ³É¹¦·µ»Ø0

+ * @note

+ * @see 

+ */

+static int Amt_SendMessageToAmtagent(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)

+{

+    // STX  TID  FID   Data  Check  ETX

+    unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1   + 1;

+    unsigned char *pRsp = NULL;

+    unsigned char *pSTX = NULL;

+    unsigned char *pETX = NULL;

+    unsigned char *pTID = NULL;

+    unsigned char *pFID = NULL;

+    unsigned char *pCheck = NULL;

+    unsigned char *pData = NULL;

+    unsigned char CheckSum = 0;

+    unsigned int i = 0;

+

+    pRsp = malloc(CmdCount);

+	if(pRsp == NULL)

+		return -1;

+    memset(pRsp, 0, CmdCount);

+

+    pSTX = &pRsp[0];

+    pETX = &pRsp[1 + 1 + 1 + buf_len + 1];

+    pTID = &pRsp[1];

+    pFID = &pRsp[1 + 1];

+    pCheck = &pRsp[1 + 1 + 1 + buf_len];

+    pData = &pRsp[1 + 1 + 1];

+	

+    *pSTX = 0x02;

+    *pETX = 0x02;

+    *pTID = (msg_id & 0xFF00) >> 8;

+    *pFID = (msg_id & 0xFF);

+

+    if (buf != NULL && buf_len > 0)

+    {

+        memcpy(pData, buf, buf_len);

+    }

+

+    CheckSum = 0;

+

+    for (i = 0; i < (1 + 1 + buf_len); i++)

+        CheckSum ^= pRsp[1 + i];

+

+    *pCheck = CheckSum;

+

+    Amt_SendDataToAmtagent(fd, pRsp, CmdCount);

+

+    free(pRsp);

+    return 0;

+}

+

+

+/**

+ * @brief AMTÏûÏ¢´ò°ü·¢¸øPC

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] msg_id FID

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ³É¹¦·µ»Ø0

+ * @note

+ * @see 

+ */

+static int Amt_SendMessage(int fd, unsigned int msg_id, unsigned char* buf, unsigned int buf_len)

+{

+    // STX  TID  FID   Data  Check  ETX

+    unsigned int CmdCount = 1 + 1 + 1 + buf_len + 1   + 1;

+    unsigned char *pRsp = NULL;

+    unsigned char *pSTX = NULL;

+    unsigned char *pETX = NULL;

+    unsigned char *pTID = NULL;

+    unsigned char *pFID = NULL;

+    unsigned char *pCheck = NULL;

+    unsigned char *pData = NULL;

+    unsigned char CheckSum = 0;

+    unsigned int i = 0;

+

+    pRsp = malloc(CmdCount);

+	if(pRsp == NULL)

+		return -1;

+    memset(pRsp, 0, CmdCount);

+

+    pSTX = &pRsp[0];

+    pETX = &pRsp[1 + 1 + 1 + buf_len + 1];

+    pTID = &pRsp[1];

+    pFID = &pRsp[1 + 1];

+    pCheck = &pRsp[1 + 1 + 1 + buf_len];

+    pData = &pRsp[1 + 1 + 1];

+

+    *pSTX = 0x02;

+    *pETX = 0x02;

+    *pTID = (msg_id & 0xFF00) >> 8;

+    *pFID = (msg_id & 0xFF);

+

+    if (buf != NULL && buf_len > 0)

+    {

+        memcpy(pData, buf, buf_len);

+    }

+

+    CheckSum = 0;

+

+    for (i = 0; i < (1 + 1 + buf_len); i++)

+        CheckSum ^= pRsp[1 + i];

+

+    *pCheck = CheckSum;

+

+    Amt_SendData(fd, pRsp, CmdCount);

+

+    free(pRsp);

+    return 0;

+}

+

+

+/**

+ * @brief AMTÏûÏ¢´¦Àíº¯Êý

+ * @param[in] msg_id FID

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ³É¹¦·µ»Ø0

+ * @note

+ * @see 

+ */

+static int Amt_ProcessMessage(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)

+{

+    if (msg_id >= FID_WIFI_CMD_NORTN && msg_id <= FID_WIFI_CMD_END) // wifi test

+    {

+        Amt_Wifi_ProcessMsg(msg_id, buf, buf_len);

+    }

+    else if (msg_id == FID_GET_CHIP_PLATFORM)

+    {

+         //

+	    if(is_amt_mode())

+	    {

+	    	AmtPrintf(AMT_ERROR "AMT MODE!\n");

+	    }

+	    else

+	    {

+	    	AmtPrintf(AMT_ERROR "Normal MODE!\n");

+	    }

+        unsigned char chipType = 0;

+        AmtPrintf(AMT_INFO "%s: Get chip platform msg_id = %#04x, buf_len = %d.\n",

+                  __FUNCTION__, msg_id, buf_len);

+        chipType = 1; /*1:7520V3 2:7100*/

+

+        if (Amt_CreateResponse(msg_id, &chipType, sizeof(unsigned char)) == -1)

+        {

+            AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        }

+        else

+        {

+            AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+        }

+    }

+    else if (msg_id >= FID_GPS_MODULE_TEST && msg_id <= FID_GPS_CMD_END) // gps test

+    {

+        Amt_Gps_ProcessMsg(msg_id, buf, buf_len);

+    }

+    else if (msg_id >= MSG_DEVICETEST_START && msg_id <= MSG_DEVICETEST_END)

+    {

+        Amt_DeviceTest_ProcessMsg(msg_id, buf, buf_len);

+    }

+	else if (msg_id == FID_AMT_END)

+    {

+		unsigned int status = CPNV_OK;//cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);

+		/*

+	       if(status != CPNV_OK)

+		{

+			AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);

+			return -1;

+		}*/

+		

+        status = cpnv_FsGcWait(FS_NVROFS);

+		if(status == CPNV_OK)

+			AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait ok.\n", __FUNCTION__);

+		else

+			AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);

+

+		if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)

+        {

+            AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        }

+        else

+        {

+            AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+        }

+    }

+	else if (msg_id == FID_AMT_EXIT)

+	{

+	    unsigned int status = 0;

+		if (Amt_CreateResponse(msg_id, (unsigned char*)&status, sizeof(unsigned int)) == -1)

+        {

+            AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        }

+        else

+        {

+            AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+        }

+	}

+    else if (msg_id == FID_EX_COMMON_SETBOOTMODE)

+    {

+        // set normal mode

+        unsigned char bootmode[] = {0xFF, 0xFF};        

+		unsigned int dwRet = CPNV_OK;

+

+        //struct timespec ts;

+		//clock_gettime(CLOCK_MONOTONIC, &ts);

+		//AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);

+        unsigned char mode = 4;

+		if(buf_len == sizeof(mode))

+		{

+		    memcpy(&mode, buf, sizeof(mode));

+		}

+		AmtPrintf(AMT_INFO "%s: boot mode=%d\n", __FUNCTION__,mode);

+		switch(mode)

+		{

+		     case 0: //user mode

+             {   

+                 nv_set_item(NV_RO, "usb_modetype", "user", 1);

+				 nv_commit(NV_RO);

+				 bootmode[0] =0x54;

+                 bootmode[1] =0x00;

+                 break;

+             }

+             case 1://debug mode

+             {

+                 nv_set_item(NV_RO, "usb_modetype", "debug", 1);

+				 nv_commit(NV_RO);

+				 bootmode[0] =0x54;

+                 bootmode[1] =0x01;

+                 break;

+             }

+             case 2://factory mode

+             {

+                 nv_set_item(NV_RO, "usb_modetype", "factory", 1);

+				 nv_commit(NV_RO);

+				 bootmode[0] =0x54;

+                 bootmode[1] =0x02;

+                 break;

+             }

+             case 3://amt mode

+             {

+			 	 //nv_set_item(NV_RO, "usb_modetype", "amt", 1);

+			 	 //nv_commit(NV_RO);

+				 bootmode[0] =0x54;

+                 bootmode[1] =0x4D;

+                 break;

+             }

+             default:

+             {

+                 break;

+             }

+		}

+

+        dwRet = amt_set_bootmode(bootmode);

+        if (dwRet == CPNV_OK)

+        {

+            AmtPrintf(AMT_INFO "%s: set boot mode: sucess.\n", __FUNCTION__);

+        }

+        else

+        {

+            AmtPrintf(AMT_ERROR "%s: set boot mode: failed.\n", __FUNCTION__);

+        }

+		//clock_gettime(CLOCK_MONOTONIC, &ts);

+		//AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);

+      

+        if (Amt_CreateResponse(msg_id, (unsigned char*)&dwRet, sizeof(unsigned int)) == -1)

+        {

+            AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        }

+        else

+        {

+            AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+        }

+    }

+	else if(msg_id == FID_CHECK_SOFTDOG_CIPHER_TEXT)

+    {

+        UINT8 softdog_cipher_texts[OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_SIZE] = {0};

+    	if(!amt_read_nv_item(ABSOFTDOG_CIPHER_TEXT_NVPARAM,softdog_cipher_texts,sizeof(softdog_cipher_texts)))

+    	{

+        	if (Amt_CreateResponse(msg_id, softdog_cipher_texts, sizeof(softdog_cipher_texts)) == -1)

+            {

+           		AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        	}

+       		else

+        	{

+            	AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+       		}

+    	}

+	    else

+	    {

+			AmtPrintf(AMT_ERROR "%s: read softdog cipher text in address:0x%x failure.\n", __FUNCTION__,OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_ADDRESS);

+		}

+    }

+	else if(msg_id == FID_SET_BAT_DET_FLAG)

+    {

+        unsigned int  retCode = CPNV_ERROR;

+        int bat_value = -1;

+        memcpy(&bat_value, buf, sizeof(int));

+		retCode =amt_set_batdet_flag(bat_value);   

+    

+        if (retCode == CPNV_OK)

+        {

+        	AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag success.\n", __FUNCTION__);

+		}

+    	else

+    	{

+    		AmtPrintf(AMT_INFO "%s: amt_set_batdet_flag fail.\n", __FUNCTION__);  

+    	}

+		if (Amt_CreateResponse(msg_id, (unsigned char*)&retCode, sizeof(retCode)) == -1)

+        {

+        	AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+        }

+       	else

+        {

+        	AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+       	}

+    }

+	else if(msg_id == FID_GET_BAT_DET_FLAG)

+    {

+        unsigned int  retCode = CPNV_ERROR;

+        int bat_value = 0;

+        retCode =amt_get_batdet_flag(&bat_value);   

+    

+        if (retCode == CPNV_OK)

+        {

+        	AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag success.bat_value=%d\n", __FUNCTION__,bat_value);

+			if (Amt_CreateResponse(msg_id, (unsigned char*)&bat_value, sizeof(bat_value)) == -1)

+	        {

+	        	AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+	        }

+	       	else

+	        {

+	        	AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+	       	}

+		}

+    	else

+    	{

+    		AmtPrintf(AMT_INFO "%s: amt_get_batdet_flag fail.\n", __FUNCTION__);  

+    	}

+	}

+	else if(msg_id == FID_RINISOFTVERSION)

+    {

+        unsigned int retCode = CPNV_ERROR;

+	    char TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN+1]={0};

+

+	    retCode = cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_INT_BASE_ADDR, (unsigned char*)TmpSoftVersion, ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN);

+	    if (retCode == CPNV_OK)

+        {

+            TmpSoftVersion[ZPS_REF_MSINFO_MAX_SOFTVERSION_INT_LEN] = '\0';

+            AmtPrintf(AMT_INFO "%s: inner version=%s\n", __FUNCTION__,TmpSoftVersion);

+			if (Amt_CreateResponse(msg_id, (unsigned char*)TmpSoftVersion, sizeof(TmpSoftVersion)) == -1)

+	        {

+	        	AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+	        }

+	       	else

+	        {

+	        	AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+	       	}

+		}

+    	else

+    	{

+    	    AmtPrintf(AMT_INFO "%s: get inner version fail!\n", __FUNCTION__);  

+    	}	

+    }

+	else if(msg_id == FID_ROUTSOFTVERSION)

+    {

+        unsigned int retCode = CPNV_ERROR;

+        char TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN+1]={0};

+

+	    retCode =cpnv_NvItemRead(ZPS_REF_MSINFO_SOFTVERSION_EXT_BASE_ADDR, (unsigned char *)TmpExtcgmr, ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN);

+	    if (retCode == CPNV_OK)

+        {

+            TmpExtcgmr[ZPS_REF_MSINFO_MAX_SOFTVERSION_EXT_LEN] = '\0';

+            AmtPrintf(AMT_INFO "%s: outer version=%s\n", __FUNCTION__,TmpExtcgmr);

+			if (Amt_CreateResponse(msg_id, (unsigned char*)TmpExtcgmr, sizeof(TmpExtcgmr)) == -1)

+	        {

+	        	AmtPrintf(AMT_ERROR "%s: Send data failure.\n", __FUNCTION__);

+	        }

+	       	else

+	        {

+	        	AmtPrintf(AMT_INFO "%s: Send data success.\n", __FUNCTION__);

+	       	}

+		}

+    	else

+    	{

+            AmtPrintf(AMT_INFO "%s: get outer version fail!\n", __FUNCTION__);  

+    	}		

+    }

+	else if((msg_id >= FID_FUN_TEST_START)&&(msg_id <= FID_FUN_TEST_END))

+	{

+       Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);

+	}

+    else // CP message

+    {

+        AmtPrintf(AMT_INFO "%s: receive old cp msg.\n", __FUNCTION__);

+        // Send message to CP

+       if (Amt_SendMessageToAmtagent(g_amt_fd_cp, msg_id, buf, buf_len) == -1)

+        {

+            AmtPrintf(AMT_ERROR "%s: Failed to send data to cp, msg_id = %#04x, buf_len = %d.\n",

+                      __FUNCTION__, msg_id, buf_len);

+        }

+        else

+        {

+            AmtPrintf(AMT_INFO "%s: Send data to cp, msg_id = %#04x, buf_len = %d.\n",

+                      __FUNCTION__, msg_id, buf_len);

+        }

+    }

+

+    return 0;

+}

+

+

+/**

+ * @brief AMTÏûÏ¢½â°ü

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return N/A

+ * @note

+ * @see 

+ */

+static void Amt_ComposeAndProcess(unsigned char *buf, unsigned int buf_len)

+{

+    unsigned char *pSTX = NULL;

+    unsigned char *pETX = NULL;

+    unsigned char *pTID = NULL;

+    unsigned char *pFID = NULL;

+    unsigned char *pCheck = NULL;

+    unsigned char *pData = NULL;

+    unsigned char CheckSum = 0;

+    unsigned int msg_id;

+    unsigned int i = 0;

+    pTID = pTID;

+    pCheck = pCheck;

+    pData = pData;

+

+    pSTX = &buf[0];

+    pETX = &buf[buf_len - 1];

+    pTID = &buf[1];

+    pFID = &buf[1 + 1];

+    pCheck = &buf[buf_len - 2];

+    pData = &buf[1 + 1 + 1];

+

+    if (!((0x02 == *pSTX) && (0x02 == *pETX)))

+    {

+        return;

+    }

+

+    CheckSum = 0;

+

+    for (i = 0; i < (buf_len - 2); i++)

+        CheckSum ^= buf[1 + i];

+

+    if (CheckSum != 0)

+    {

+        return;

+    }

+

+    // TIDÓëFID×éºÏʹÓÃÒÑÀ©Õ¹¹¦ÄܺÅ

+    msg_id = (*pTID << 8) | *pFID;

+

+    AmtPrintf(AMT_INFO "%s: msg_id = %#04x, buf_len = %d.\n", __FUNCTION__, msg_id, buf_len);

+    Amt_ProcessMessage(msg_id, pFID + 1, (buf_len - 1 - 1 - 1 - 1 - 1));

+}

+

+

+/**

+ * @brief AMTÏûÏ¢·´À¡

+ * @param[in] msg_id FID

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ³É¹¦·µ»Ø0, ʧ°Ü·µ»Ø-1

+ * @note

+ * @see 

+ */

+int Amt_CreateResponse(unsigned int msg_id, unsigned char* buf, unsigned int buf_len)

+{

+    if (g_amt_fd_current && *g_amt_fd_current >= 0)

+    {

+        return Amt_SendMessage(*g_amt_fd_current, msg_id, buf, buf_len);

+    }

+    else

+    {

+        AmtPrintf(AMT_ERROR "%s: Current fd is wrong.\n", __FUNCTION__);

+        return -1;

+    }

+}

+

+int Amt_ExecuteCmd(char *pcmd, char *pbuffer, int len)

+{

+    FILE *pPipe;

+

+    AmtPrintf(AMT_INFO "%s: execute \"%s\"!\n", __FUNCTION__, pcmd);

+

+    if ((pPipe = popen(pcmd, "rb")) == NULL)

+    {

+        AmtPrintf(AMT_ERROR "popen \"%s\" failure.\n", pcmd);

+        return -1;

+    }

+

+    int read_len = fread(pbuffer, 1, len, pPipe);

+    //AmtPrintf(AMT_INFO "fread, read_len = %d, pbuffer = %s.\n", read_len, pbuffer);

+

+    pclose(pPipe);

+    return read_len;

+}

+

+static int init_cp_channel(void)

+{

+    int fd = open(AMT_CP_CHANNEL, O_RDWR);

+

+    if (fd < 0)

+    {

+        AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_CP_CHANNEL);

+        return -1;

+    }

+

+    if(ioctl(fd, RPMSG_CREATE_CHANNEL, (8*1024))!= 0)   // ´´½¨Í¨µÀ´óС8K

+    {

+        AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CREATE_CHANNEL fail!\n");

+    }

+    if(ioctl(fd, RPMSG_SET_INT_FLAG, NULL)!= 0)       // ·¢ËÍÏûϢʱ£¬´¥·¢ÖжÏ

+    {

+        AmtPrintf(AMT_ERROR "ioctrl:RPMSG_SET_INT_FLAG fail!\n");

+    }

+    if(ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL)!= 0)      // ×èÈûµÄ·½Ê½¶Á

+    {

+        AmtPrintf(AMT_ERROR "ioctrl:RPMSG_CLEAR_POLL_FLAG fail!\n");

+	}

+    return fd;

+}

+

+static int init_usb_device(void)

+{

+    int fd = open(AMT_USB_DEV, O_RDWR);

+

+    if (fd < 0)

+    {

+        AmtPrintf(AMT_ERROR "Failed to open \"%s\"!\n", AMT_USB_DEV);

+        return -1;

+    }

+

+    PortSet(fd);

+    return fd;

+}

+

+static int init_hotplug_nl(void)

+{

+    struct sockaddr_nl snl;

+    bzero(&snl, sizeof(snl));

+    snl.nl_family = AF_NETLINK;

+    snl.nl_pid = getpid();

+    snl.nl_groups = 1;

+

+    int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);

+    if (s == -1)

+    {

+        AmtPrintf(AMT_ERROR "Can't create hotplug socket!\n");

+        return -1;

+    }

+

+    if (bind(s, (struct sockaddr *)&snl, sizeof(snl)) < 0)

+    {

+        AmtPrintf(AMT_ERROR "Can't bind hotplug socket!\n");

+		close(s);

+        return -1;

+    }

+

+    return s;

+}

+

+static int init_socket(int port)

+{

+    struct sockaddr_in seraddr;

+    int sockfd = -1;

+

+    AmtPrintf(AMT_INFO "port = %d.\n", port);

+

+    sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

+    if (sockfd == -1)

+    {

+        AmtPrintf(AMT_ERROR "Can't create socket!\n");

+        return -1;

+    }

+

+    memset(&seraddr,0,sizeof(seraddr));

+    seraddr.sin_family = AF_INET;

+    seraddr.sin_addr.s_addr = 0;

+    seraddr.sin_port = htons(port);

+

+    if (bind(sockfd, (struct sockaddr*)(&seraddr), sizeof(seraddr)) < 0)

+    {

+        AmtPrintf(AMT_ERROR "Can't bind port %d!\n", port);

+		close(sockfd);

+        return -1;

+    }

+

+    if (listen(sockfd, 1) == -1)

+    {

+        AmtPrintf(AMT_ERROR "Socket listen failed!\n");

+		close(sockfd);

+        return -1;

+    }

+

+    return sockfd;

+}

+

+static void set_fd(int fd)

+{

+    FD_SET(fd, &g_amt_fdsread);

+    if (fd >= g_amt_fd_max)

+    {

+        g_amt_fd_max = fd + 1;

+    }

+}

+

+static void clr_fd(int fd)

+{

+    FD_CLR(fd, &g_amt_fdsread);

+}

+

+static int CreateSendPacket(unsigned     short fid, unsigned char *data_buf, int data_len, unsigned char*packet_buf)

+{

+    int bRet = 0;

+    BYTE *pSTX = NULL;

+    BYTE *pETX = NULL;

+	//FIDµÄ¸ß×Ö½Ú

+    BYTE *pHFID = NULL;

+	//FIDµÄµÍ×Ö½Ú

+    BYTE *pLFID = NULL;

+    BYTE *pCheck = NULL;

+    BYTE *pData = NULL;

+    BYTE CheckSum = 0;

+	int i = 0;

+

+    //            STX FID  Data Check  ETX

+    int CmdCount = 1 + 2 + data_len + 1   + 1;

+

+    if ((data_len != 0) && (NULL == data_buf))

+    {

+        return bRet;

+    }

+

+    pSTX = &packet_buf[0];

+    pETX = &packet_buf[1 + 2 + data_len + 1];

+    pHFID = &packet_buf[1];

+    pLFID = &packet_buf[1+1];

+    pCheck = &packet_buf[1 + 2 + data_len];

+    pData = &packet_buf[1 + 2];

+

+    *pSTX = 0x02;

+    *pETX = 0x02;

+    *pHFID = (fid>>8)&0x00FF;

+    *pLFID = fid&0x00FF;

+    memcpy(pData, data_buf, data_len);

+    CheckSum = 0;

+	//CheckSumÊÇFIDºÍDATA×ֶεÄУÑé

+    for (i = 0; i < (2 + data_len); i++)

+        CheckSum ^= packet_buf[1 + i];

+    *pCheck = CheckSum;

+    bRet = 1;

+    return bRet;

+}

+

+static void send_enter_amt_mode_packet()

+{

+    u32 enter_amt_req[5] = {0};

+	enter_amt_req[0] = 0x0100;

+	enter_amt_req[1] = 1;

+	enter_amt_req[2] = 1;

+	enter_amt_req[3] = 0;

+	enter_amt_req[4] = 0;

+

+	unsigned char enter_amt_packet[25] = {0};

+	unsigned short fid = 0x005f;

+	CreateSendPacket(fid,(unsigned char*)enter_amt_req,sizeof(enter_amt_req),enter_amt_packet);

+	Amt_ComposeAndProcess(enter_amt_packet, sizeof(enter_amt_packet));

+}

+

+static void send_exit_amt_mode_packet()

+{

+	u32 exit_amt_req[5] = {0};

+	exit_amt_req[0] = 0x0100;

+	exit_amt_req[1] = 0;

+	exit_amt_req[2] = 1;

+	exit_amt_req[3] = 0;

+	exit_amt_req[4] = 0;

+

+	unsigned char exit_amt_packet[25] = {0};

+	unsigned short fid = 0x005f;

+	CreateSendPacket(fid,(unsigned char*)exit_amt_req,sizeof(exit_amt_req),exit_amt_packet);

+	Amt_ComposeAndProcess(exit_amt_packet, sizeof(exit_amt_packet));

+

+}

+

+

+static void send_tx_start_packet(unsigned int band,unsigned int channel,short int_power,short dec_power)

+{

+    typedef struct

+    {

+        UINT32  MsgId;

+    	UINT32  wBandNum;       // Ƶ¶Î

+    	UINT32  dwEarfcn;   

+    	UINT32  wFreq;          // ÖÐÐÄÆµµã£¬100kHzΪµ¥Î»

+    	UINT32  wBandWidth;     // ´ø¿í£¬100kHzΪµ¥Î»

+    	UINT32  wAntennaPort;   // ÌìÏß¶Ë¿Ú  £¿£¿£¿

+    	UINT32  wModulation;    // µ÷ÖÆ·½Ê½£¬0£ºQPSK£¬1£º16QAM£¬2£º64QAM

+    	UINT32  wRBNum;         // RB¸öÊý

+    	UINT32  wRBStartPosition;  // RBÆðÊ¼Æ«ÒÆ

+    	UINT32  wSingleWave;    // ÊÇ·ñΪµ¥Ôز¨

+    	SINT16 swRfIntPower; 	//¹¦ÂÊÕûÊý

+    	SINT16 swRfDecPower;	//¹¦ÂÊСÊý,[-2 -1 0 1 2 ]´ú±í[-0.5 -0.25 0 0.25 0.5]

+	}T_zAMT_LTEA_SetTxInit_Req; 

+

+	T_zAMT_LTEA_SetTxInit_Req stTxInitReq = {0};

+	stTxInitReq.MsgId = 0x0700;

+	stTxInitReq.wBandNum = band;

+	stTxInitReq.dwEarfcn = channel;

+	stTxInitReq.wFreq = 0;

+	stTxInitReq.wBandWidth = 9;

+	stTxInitReq.wAntennaPort = 0;

+	stTxInitReq.wModulation = 0;

+	stTxInitReq.wRBNum = 50;

+	stTxInitReq.wRBStartPosition = 0;

+	stTxInitReq.wSingleWave = 0;

+	stTxInitReq.swRfIntPower = int_power;

+	stTxInitReq.swRfDecPower = dec_power;

+

+	unsigned char tx_start_packet[sizeof(stTxInitReq)+5] = {0};

+	unsigned short fid = 0x005f;

+	CreateSendPacket(fid,(unsigned char*)&stTxInitReq,sizeof(stTxInitReq),tx_start_packet);

+	Amt_ComposeAndProcess(tx_start_packet, sizeof(tx_start_packet));

+	

+}

+

+static void send_tx_stop_packet()

+{

+    u32 tx_close_req[1] = {0};

+	tx_close_req[0] = 0x0201;

+	unsigned char tx_stop_packet[1*4+5] = {0};

+	unsigned short fid = 0x005f;

+	CreateSendPacket(fid,(unsigned char*)tx_close_req,sizeof(tx_close_req),tx_stop_packet);

+	Amt_ComposeAndProcess(tx_stop_packet, sizeof(tx_stop_packet));

+

+}

+

+static void send_switch_to_user_mode_packet()

+{

+    unsigned char bootmode[] = {0x54, 0x00};

+	unsigned char atmode[] = {0xFF, 0xFF};

+	nv_set_item(NV_RO, "usb_modetype", "user", 1);

+	nv_commit(NV_RO);

+	amt_set_amt_atmode(bootmode,atmode);

+}

+

+static void send_ok(int fd)

+{

+    char rep_ok[] = "OK\r\n";

+	PortSend(fd, (unsigned char*)rep_ok, strlen(rep_ok), WAIT_ALL);

+}

+

+/**

+ * @brief ATÃüÁî½ÓÊÕ

+ * @param[in] fd ÎļþÃèÊö·û

+ * @param[in] buf ½ÓÊÕÊý¾Ýbuffer

+ * @param[in] buf_len ½ÓÊÕÊý¾Ýbuffer³¤¶È

+ * @return ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý³¤¶È

+ * @note

+ * @see 

+ */

+static int At_ReceiveData(int fd, char* buf, unsigned int buf_len)

+{

+    int read_len = 0;

+	int i = 0;

+	read_len = PortRecv(fd, buf, 100, NO_WAIT);

+    AmtPrintf(AMT_INFO "%s: read_len=%d.\n", __FUNCTION__, read_len);

+    if (read_len > 0)

+    {

+        buf[read_len] = '\0';

+        AmtPrintf(AMT_INFO "%s: receive [%s]\n", __FUNCTION__, buf);

+		//ת»»ÎªÐ¡Ð´

+		for(i = 0; i < read_len; i++)

+		{

+		    char at = tolower(buf[i]);

+			buf[i] = at;

+		}

+		AmtPrintf(AMT_INFO "%s: after tolower [%s]\n", __FUNCTION__, buf);

+        //at+txstart=band,channel,power

+		if(strstr(buf,"at+txstart") != NULL)

+		{

+		    AmtPrintf(AMT_INFO "%s: receive at+txstart\n", __FUNCTION__);

+			double data[3] = {0.0};

+			unsigned int band = 0;

+			unsigned int channel = 0;

+			short int_power = 0;

+			short idec_power = 0;

+			double dec_power = 0.0;

+			//»ñÈ¡²ÎÊý

+			buf = buf + strlen("at+txstart=");

+			if(buf != NULL)

+			{

+			    str_to_double(data,buf);

+				band = data[0];

+				channel = data[1];

+				int_power = data[2];

+				dec_power = data[2] - int_power;

+				idec_power = dec_power*4;

+				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);

+				if(band == 0)

+				{

+				   AmtPrintf(AMT_ERROR "%s: invalid band!\n", __FUNCTION__);

+				   return read_len;

+				   	

+				}

+				if(channel == 0 )

+				{

+				   AmtPrintf(AMT_ERROR "%s: invalid channel!\n", __FUNCTION__);

+				   return read_len;

+				}

+			}

+			//ת»¯Îª½øÈëamtģʽºÍtxinitÁ½ÌõAMTÖ¸Áî

+			send_enter_amt_mode_packet();

+			sleep(1);

+			send_tx_start_packet(band,channel,int_power,idec_power);

+			send_ok(fd); //Ö±½Ó¸øATÃüÁî·¢ËÍÕ߻ظ´ok

+			

+		}

+		else if(strstr(buf,"at+txstop") != NULL)

+		{

+		    //ת»¯Îª¹Ø±Õ·¢Éä»úºÍÍ˳öamtģʽÁ½ÌõAMTÖ¸Áî

+			send_tx_stop_packet();

+		    sleep(1);

+			send_exit_amt_mode_packet();

+			send_ok(fd);

+		}

+		else if(strstr(buf,"at+zversiontype=3") != NULL)

+		{

+			send_switch_to_user_mode_packet();

+			send_ok(fd);

+		}

+		else

+		{

+		    AmtPrintf(AMT_ERROR "%s: unknown at %s.\n", __FUNCTION__, buf);

+		}

+    }

+    else

+    {

+        AmtPrintf(AMT_ERROR "%s: PortRecv from device(fd = %d): read_len(%d) is wrong.\n", __FUNCTION__, fd, read_len);

+    }

+

+    return read_len;

+}

+

+/**

+ * Main Function

+ */

+int main (int argc, char *argv[])

+{

+    extern char *optarg;

+    extern int optind;

+    int c;

+	int socket_port = -1;

+

+    /********************** Get arguments ********************/

+    while ((c = getopt(argc,argv,"p:r:")) != EOF)

+    {

+        switch (c)

+        {

+            case 'p':

+                socket_port = atoi(optarg);

+                break;

+            case '?':

+                AmtPrintf(AMT_INFO "Usage: zte_amt [options]\n");

+                AmtPrintf(AMT_INFO "-p ipport - Set IP port number to listen on\n");

+                exit(1);

+        }

+    }

+

+    AmtPrintf(AMT_INFO "start AMT!\n");

+

+    FD_ZERO(&g_amt_fdsread);

+

+    /********************** Init AP-CP channel ********************/

+    g_amt_fd_cp = init_cp_channel();

+    if (g_amt_fd_cp < 0)

+    {

+        return -1;

+    }

+    else

+    {

+        AmtPrintf(AMT_INFO "g_amt_fd_cp = %d.\n", g_amt_fd_cp);

+    }

+

+    /********************** Init USB device ***********************/

+    g_amt_fd_usb = init_usb_device();

+    if (g_amt_fd_usb >= 0)

+    {

+        AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);

+        set_fd(g_amt_fd_usb);

+

+        //Init hotplug netlink

+        g_amt_fd_usb_hotplug = init_hotplug_nl();

+        if (g_amt_fd_usb_hotplug >= 0)

+        {

+            AmtPrintf(AMT_INFO "g_amt_fd_usb_hotplug = %d.\n", g_amt_fd_usb_hotplug);

+            set_fd(g_amt_fd_usb_hotplug);

+        }

+    }

+

+    /*********************** Create socket ***********************/

+    if (socket_port > 0)

+    {

+        g_amt_fd_socket_server = init_socket(socket_port);

+        if (g_amt_fd_socket_server >= 0)

+        {

+            AmtPrintf(AMT_INFO "g_amt_fd_socket_server = %d.\n", g_amt_fd_socket_server);

+            set_fd(g_amt_fd_socket_server);

+        }

+    }

+

+    // Create CP read thread

+    pthread_t cp_read_thread;

+    if (pthread_create(&cp_read_thread, NULL, ReadFromCPThread, NULL) != 0)

+    {

+        AmtPrintf(AMT_ERROR "Failed to create CP read thread!\n");

+        return -1;

+    }

+

+    // Wifi init

+    Amt_Wifi_Init();

+    g_amt_iMsgHandle = msgget(MODULE_ID_AMT, IPC_CREAT|0600);

+    if(-1 == g_amt_iMsgHandle) 

+    {

+   	    AmtPrintf(AMT_ERROR "%s: can not create msg queue for AMT!\n", __FUNCTION__);

+	    return -1;

+    }

+

+    pthread_t msg_recv_thread;

+    if (pthread_create(&msg_recv_thread, NULL, RecvMsgFromAppThread, NULL) != 0)

+    {

+   	 AmtPrintf(AMT_ERROR "Failed to create RecvMsgFromAppThread thread!\n");

+         return -1;

+    }

+    // malloc receive buffer

+    char *receive_buffer = malloc(MAX_PACKET_LENGTH);

+    if (receive_buffer == NULL)

+    {

+        return -1;

+    }

+

+    int ret = -1;

+    int read_len = 0;

+    fd_set tmpfds;

+

+	ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);

+	if(ret != CPNV_OK)

+	{

+		AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RW nvrofs failed!\n", __FUNCTION__);

+		return -1;

+	}

+	else

+	{

+	    AmtPrintf(AMT_INFO"%s: cpnv_ChangeFsPartitionAttr RW nvrofs ok!\n", __FUNCTION__);

+	}

+

+	//¼ÓÔØaic8800¹Ì¼þ

+	#if (defined(__AIC_8800DW_CHIP__))

+	ret = wifi_ioctl_handle(3); //¼ÓÔØ²âÊԹ̼þ

+	if(ret < 0)

+	{

+		AmtPrintf(AMT_ERROR "%s: load AIC_8800DW firmware fail! ret=%d.\n", __FUNCTION__, ret);

+	}

+    else

+	{

+		AmtPrintf(AMT_INFO "%s: load AIC_8800DW firmware success! ret=%d.\n", __FUNCTION__, ret);

+	}

+	#endif

+	

+	g_amt_at_mode = is_amt_atmode();

+	AmtPrintf(AMT_INFO"%s: g_amt_at_mode=%d\n", __FUNCTION__,g_amt_at_mode);

+

+    while (1)

+    {

+        tmpfds = g_amt_fdsread;

+        ret = select(g_amt_fd_max, &tmpfds, NULL, NULL, NULL);

+        if (ret <= 0)

+        {

+            AmtPrintf(AMT_ERROR "select error: %s!\n", strerror(errno));

+            continue;

+        }

+

+        if (g_amt_fd_usb_hotplug >= 0 && FD_ISSET(g_amt_fd_usb_hotplug, &tmpfds))

+        {

+            read_len = PortRecv(g_amt_fd_usb_hotplug, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH - 1, NO_WAIT);

+			if(read_len >= 0)

+				receive_buffer[read_len] = '\0';

+

+			// Ç¿ÖÆÔö¼Ó½áÊø·û, read_len < MAX_PACKET_LENGTH

+			receive_buffer[MAX_PACKET_LENGTH - 1] = '\0';

+

+            if (strstr(receive_buffer, AMT_DETECT_USB_HOTREMOVE))

+            {

+                AmtPrintf(AMT_INFO "USB remove!\n");

+                system("poweroff");

+                break;

+            }

+

+            if (strstr(receive_buffer, AMT_DETECT_USB_OFFLINE))

+            {

+                AmtPrintf(AMT_INFO "USB offline!\n");

+

+                if (g_amt_fd_usb >= 0)

+                {

+                    close(g_amt_fd_usb);

+                    clr_fd(g_amt_fd_usb);

+                    g_amt_fd_usb = -1;

+                }

+            }

+

+            if (strstr(receive_buffer, AMT_DETECT_USB_ONLINE))

+            {

+                AmtPrintf(AMT_INFO "USB online!\n");

+

+                if (g_amt_fd_usb >= 0)

+                {

+                    close(g_amt_fd_usb);

+                    clr_fd(g_amt_fd_usb);

+                    g_amt_fd_usb = -1;

+                }

+

+                g_amt_fd_usb = init_usb_device();

+                if (g_amt_fd_usb >= 0)

+                {

+                    AmtPrintf(AMT_INFO "g_amt_fd_usb = %d.\n", g_amt_fd_usb);

+                    set_fd(g_amt_fd_usb);

+                }

+            }

+        }

+

+        if (g_amt_fd_socket_server >= 0 && FD_ISSET(g_amt_fd_socket_server, &tmpfds))

+        {

+            struct sockaddr_in cliaddr;

+            int addrlen = sizeof(cliaddr);

+

+            if (g_amt_fd_socket_client >= 0)

+            {

+                close(g_amt_fd_socket_client);

+                clr_fd(g_amt_fd_socket_client);

+                g_amt_fd_socket_client = -1;

+            }

+

+            if ((g_amt_fd_socket_client = accept(g_amt_fd_socket_server, (struct sockaddr*)&cliaddr, (socklen_t *)&addrlen)) == -1)

+            {

+                AmtPrintf(AMT_ERROR "Accept failed!\n");

+            }

+            else

+            {

+                unsigned long ip = ntohl(cliaddr.sin_addr.s_addr);

+                AmtPrintf(AMT_INFO "Connect from %lu.%lu.%lu.%lu\n", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, (ip>>0)&0xff);

+                set_fd(g_amt_fd_socket_client);

+            }

+        }

+

+        if (g_amt_fd_usb >= 0 && FD_ISSET(g_amt_fd_usb, &tmpfds))

+        {

+            g_amt_fd_current = &g_amt_fd_usb;

+        }

+        else if (g_amt_fd_socket_client >= 0 && FD_ISSET(g_amt_fd_socket_client, &tmpfds))

+        {

+            g_amt_fd_current = &g_amt_fd_socket_client;

+        }

+        else

+        {

+            continue;

+        }

+

+		//¶ÁNVÅжÏÊÇ·ñÊǽâÎöATÃüÁʽ

+		if(g_amt_at_mode == 1)

+		{

+		   //½âÎö²¢´¦ÀíATÃüÁî

+		   At_ReceiveData(*g_amt_fd_current, receive_buffer, MAX_PACKET_LENGTH);

+		   continue;

+		}

+

+        read_len = Amt_ReceiveData(*g_amt_fd_current, (unsigned char *)receive_buffer, MAX_PACKET_LENGTH);

+

+        if (read_len > 0)

+        {

+            Amt_ComposeAndProcess((unsigned char *)receive_buffer, read_len);

+        }

+        else if (read_len == 0)

+        {

+            AmtPrintf(AMT_ERROR "%s: read_len = 0, close fd(%d)\n", __FUNCTION__, *g_amt_fd_current);

+            close(*g_amt_fd_current);

+            clr_fd(*g_amt_fd_current);

+            *g_amt_fd_current = -1;

+            g_amt_fd_current = NULL;

+        }

+

+

+        //struct timespec ts;

+		//clock_gettime(CLOCK_MONOTONIC, &ts);

+		//AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait start.\n", ts.tv_sec, ts.tv_nsec / 1000000,__FUNCTION__);

+        //unsigned int status = cpnv_FsGcWait(FS_NVROFS);

+		//if(status != CPNV_OK)

+		//	AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait fail, err = %d.\n", __FUNCTION__, status);

+        //else

+        //{

+		//	clock_gettime(CLOCK_MONOTONIC, &ts);

+		//	AmtPrintf(AMT_INFO "[%8d.%03d] %s: cpnv_FsGcWait ok.\n", ts.tv_sec, ts.tv_nsec / 1000000, __FUNCTION__);

+		//}

+		

+    }

+

+    free(receive_buffer);

+    pthread_join(cp_read_thread, NULL);

+#if 1

+    cpnv_FsGcWait(FS_NVROFS);

+	AmtPrintf(AMT_INFO "%s: cpnv_FsGcWait return.\n", __FUNCTION__);

+

+	ret = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);

+	if(ret != CPNV_OK)

+	{

+		AmtPrintf(AMT_ERROR "%s: cpnv_ChangeFsPartitionAttr RO nvrofs failed!\n", __FUNCTION__);

+		return -1;

+	}

+#endif

+    close(g_amt_fd_cp);

+

+    if (g_amt_fd_usb >= 0)

+    {

+        close(g_amt_fd_usb);

+    }

+

+    //if (g_amt_fd_usb_hotplug >= 0)

+    {

+        close(g_amt_fd_usb_hotplug);

+    }

+

+    if (g_amt_fd_socket_client >= 0)

+    {

+        close(g_amt_fd_socket_client);

+    }

+

+    if (g_amt_fd_socket_server >= 0)

+    {

+        close(g_amt_fd_socket_server);

+    }

+

+    AmtPrintf(AMT_INFO "AMT exit!\n");

+    return 0;

+}

+