[Feature][ZXW-41] merged 0601 version

Change-Id: I667af6bb09d65581d455b73f8984c160b2c67ad8
diff --git a/ap/app/zte_comm/nvserver/nvserver_rpc.c b/ap/app/zte_comm/nvserver/nvserver_rpc.c
index f165cdc..eb1efee 100755
--- a/ap/app/zte_comm/nvserver/nvserver_rpc.c
+++ b/ap/app/zte_comm/nvserver/nvserver_rpc.c
@@ -18,6 +18,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <assert.h>
 #include <dirent.h>
 #include <string.h>
 #include <sys/file.h>
@@ -65,6 +66,10 @@
 *                                È«¾Ö±äÁ¿¶¨Òå                                  *
 *******************************************************************************/
 static int g_rpc_fd = -1;
+static unsigned int g_msgRcvCnt = 0;
+static unsigned int g_msgRcvErrCnt = 0;
+static unsigned int g_msgSndCnt = 0;
+static unsigned int g_msgSndErrCnt = 0;
 
 /*******************************************************************************
 *                                ¾Ö²¿º¯ÊýʵÏÖ                                  *
@@ -100,11 +105,42 @@
         try_cnt++;
         //sleep(1);
         //continue;
-    } while (0 != sc_rpc_recv(g_rpc_fd, rpc_data2, timeout)); // µÈ´ýCAPºË´´½¨Í¨µÀ
+    } while (0 != sc_rpc_recv(g_rpc_fd, rpc_data2, timeout)); //µÈ´ýÍê³É
+    if (rpc_data2->msg_type != RPC_MSG_TYPE_REPLY)
+        assert(0);
 
     memcpy(msgsnd, rpc_data2->data, sizeof(T_NV_MSG_RESULT));
 }
 
+static int nv_rpc_wait_ready(int fd)
+{
+    T_sc_rpc_header *rpc_data;
+    unsigned int timeout = 200; // ms
+    int write_flag = 1;
+
+    rpc_data = malloc(sizeof(T_sc_rpc_header));
+    if (rpc_data == NULL)
+        return -1;
+    while(1)
+    {
+        rpc_data->msg_type = RPC_MSG_TYPE_READY;
+        if (write_flag)
+        {
+            if (write(fd, rpc_data, sizeof(T_sc_rpc_header)) != 0)
+                write_flag = 0; // write ok and write sonly once
+        }
+        if (read(fd, rpc_data, sizeof(T_sc_rpc_header)) == 0)
+        {
+            usleep(timeout * 1000);
+            continue;
+        }
+        break; /* read some data mean channel ok */
+    }
+
+    free(rpc_data);
+    return 0;
+}
+
 /*******************************************************************************
 *                                È«¾Öº¯ÊýʵÏÖ                                  *
 *******************************************************************************/
@@ -125,6 +161,7 @@
     struct msqid_ds msgInfo;
     T_sc_rpc_header *rpc_data;
     T_sc_rpc_header *rpc_data2;
+    long pid_backup;       /*  app-libnvram pid backup */
 
     prctl(PR_SET_NAME, "nvserver", 0, 0, 0);
 
@@ -162,24 +199,47 @@
         return -3;
     }
 
+    if (nv_rpc_wait_ready(g_rpc_fd) < 0)
+    {
+       assert(0);
+    }
+    sc_rpc_clear(g_rpc_fd);
+    printf("apnv and nv-rpc-daemon are ready both.");
+
     // ÏûÏ¢½»»¥
     while (1)
     {
         memset(&rcvBuf, 0, sizeof(rcvBuf));
         memset(&sndBuf, 0, sizeof(sndBuf));
+        if (g_msgRcvCnt != g_msgSndCnt)
+        {
+            printf("[nvserver_rpc]Rcv:%u Snd:%u RcvErr:%u SndErr:%u\n", g_msgRcvCnt, g_msgSndCnt, g_msgRcvErrCnt, g_msgSndErrCnt);
+            assert(0);
+        }
         if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0))
         {
+            g_msgRcvErrCnt++;
             printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno);
             continue;
         }
+        //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid);
+        pid_backup = rcvBuf.pid;
+        g_msgRcvCnt++;  /* msgrcv success count */
 
         analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2);
 
+        //printf("apnv nvserver_rpc msgsnd pid:%d, backup:%d\n", sndBuf.msgType, pid_backup);
         if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0))
         {
+            g_msgSndErrCnt++;
             printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno);
             continue;
         }
+        g_msgSndCnt++;  /* msgsnd success count */
+        if (sndBuf.msgType != pid_backup)
+        {
+            assert(0);
+        }
     }
 
     free(rpc_data);