[Feature][ZXW-65]merged P49 base code

Change-Id: I3e09c0c3d47483bc645f02310380ecb7fc6f4041
diff --git a/ap/app/zte_amt/amt.c b/ap/app/zte_amt/amt.c
index 6178997..5280171 100755
--- a/ap/app/zte_amt/amt.c
+++ b/ap/app/zte_amt/amt.c
@@ -88,6 +88,10 @@
 static volatile int *g_amt_fd_current = NULL;

 static int g_amt_iMsgHandle = 0;

 unsigned int g_amt_at_mode = 0;

+#ifdef USE_CAP_SUPPORT

+static int g_amt_fd_cap = -1;

+#endif

+

 

 

 

@@ -136,15 +140,15 @@
 {

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

-        }

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

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

+    return write_len;

 }

 

 

@@ -253,6 +257,57 @@
     }while(Endb!=NULL); 

 }

 

+#ifdef USE_CAP_SUPPORT

+/**

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

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

+ * @return N/A

+ * @note

+ * @see 

+ */

+static void* ReadFromCAPThread(void* args)

+{

+    // Read from AP-CAP channel

+    char *receive_buffer = NULL;

+

+	UNUSED(args);

+	

+	receive_buffer = malloc(MAX_PACKET_LENGTH);

+    if (receive_buffer == NULL)

+    {

+        return NULL;

+    }

+

+	prctl(PR_SET_NAME, "AmtReadFromCAP");

+

+    while (1)

+    {

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

+

+        if (read_len > 0)

+        {

+            AmtPrintf(AMT_INFO "%s: Receive cap 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__);

+            }

+        }

+    }

+

+    free(receive_buffer);

+    return NULL;

+}

+#endif

+

+

 /**

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

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

@@ -797,6 +852,105 @@
     return 0;

 }

 

+#ifdef USE_CAP_SUPPORT

+/**

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

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

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

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

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

+ * @note

+ * @see 

+ */

+static int Amt_SendDataToCAP(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 != sizeof(unsigned int))

+    {

+        AmtPrintf(AMT_ERROR "%s: Failed to send data_len to fd(%d)! write_len = %d.\n",

+                  __FUNCTION__, fd, write_len);

+        write_len = 0;

+    }

+    else

+    {

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

+        {

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

+

+            if (write_len != (int)buf_len)

+            {

+                AmtPrintf(AMT_ERROR "%s: Failed to send data to fd(%d)! (write_len = %d) != (buf_len = %d).\n",

+                          __FUNCTION__, fd, write_len, buf_len);

+            }

+        }

+    }

+

+    return write_len;

+}

+

+

+/**

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

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

+ * @param[in] msg_id FID

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

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

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

+ * @note

+ * @see 

+ */

+static int Amt_SendMessageToCAP(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_SendDataToCAP(fd, pRsp, CmdCount);

+

+    free(pRsp);

+    return 0;

+}

+#endif

+

+

 

 /**

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

@@ -1075,8 +1229,25 @@
     }

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

 	{

-       Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);

+		Amt_FuncTest_ProcessMsg(msg_id, buf, buf_len);

 	}

+	#ifdef USE_CAP_SUPPORT

+	else if((msg_id >= FID_CAP_TEST_START)&&(msg_id <= FID_CAP_TEST_END))

+	{

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

+        // Send message to CAP

+        if (Amt_SendMessageToCAP(g_amt_fd_cap, msg_id, buf, buf_len) == -1)

+        {

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

+                      __FUNCTION__, msg_id, buf_len);

+        }

+        else

+        {

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

+                      __FUNCTION__, msg_id, buf_len);

+        }

+	}

+	#endif

     else // CP message

     {

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

@@ -1291,6 +1462,34 @@
     return sockfd;

 }

 

+#ifdef USE_CAP_SUPPORT

+static int init_cap_channel(void)

+{

+    int fd = open(AMT_CAP_DEV, O_RDWR);

+

+    if (fd < 0)

+    {

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

+        return -1;

+    }

+

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

+    {

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

+}

+#endif

+

+

 static void set_fd(int fd)

 {

     FD_SET(fd, &g_amt_fdsread);

@@ -1603,6 +1802,27 @@
             set_fd(g_amt_fd_socket_server);

         }

     }

+	

+    #ifdef USE_CAP_SUPPORT

+	/********************** Init AP-CAP channel ********************/

+	g_amt_fd_cap = init_cap_channel();

+    if (g_amt_fd_cap < 0)

+    {

+        return -1;

+    }

+    else

+    {

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

+    }

+

+	 // Create CAP read thread

+    pthread_t cap_read_thread;

+    if (pthread_create(&cap_read_thread, NULL, ReadFromCAPThread, NULL) != 0)

+    {

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

+        return -1;

+    }

+	#endif

 

     // Create CP read thread

     pthread_t cp_read_thread;

@@ -1833,6 +2053,10 @@
         close(g_amt_fd_socket_server);

     }

 

+	#ifdef USE_CAP_SUPPORT

+	close(g_amt_fd_cap);

+	#endif

+

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

     return 0;

 }

diff --git a/ap/app/zte_amt/amt.h b/ap/app/zte_amt/amt.h
index 5dce06d..10e7c23 100755
--- a/ap/app/zte_amt/amt.h
+++ b/ap/app/zte_amt/amt.h
@@ -38,6 +38,9 @@
 

 #define AMT_USB_DEV    "/dev/ttyGS0"

 #define AMT_CP_CHANNEL "/dev/rpm9"

+#ifdef USE_CAP_SUPPORT

+#define AMT_CAP_DEV    "/dev/rpmsg15"

+#endif

 

 

 #define AMT_DETECT_USB_OFFLINE   "offline@/devices/platform/zx29_hsotg.0/gadget/tty/ttyGS0"

@@ -85,6 +88,12 @@
 #define FID_OPEN_UART           (FID_FUN_TEST_START + 6)

 #define FID_CLOSE_UART          (FID_FUN_TEST_START + 7)

 

+#ifdef USE_CAP_SUPPORT

+//ÐèÒª·¢Ë͵½capºË´¦ÀíµÄÏûÏ¢·¶Î§

+#define FID_CAP_TEST_START             0x2000

+#define FID_CAP_TEST_END               0x2400

+#endif

+

 

 

 #define min(X, Y)				\