Resend NITZ when the upstream client is not ready

When the upstream client android RILJ is not ready, NITZ time data
must be saved and resend when the connection is established.

b/34856813

Change-Id: I468918ac807674b3ae0cbfbb1757a65ac79b20e4
Signed-off-by: Weilun Du <wdu@google.com>
diff --git a/libril/ril.cpp b/libril/ril.cpp
index 2c788ca..0b8afe4 100644
--- a/libril/ril.cpp
+++ b/libril/ril.cpp
@@ -4068,6 +4068,21 @@
     }
 }
 
+static void resendLastNITZTimeData(RIL_SOCKET_ID socket_id) {
+    if (s_lastNITZTimeData != NULL) {
+        Parcel p;
+        int responseType = (s_callbacks.version >= 13)
+                           ? RESPONSE_UNSOLICITED_ACK_EXP
+                           : RESPONSE_UNSOLICITED;
+        int ret = radio::nitzTimeReceivedInd(
+            p, (int)socket_id, RIL_UNSOL_NITZ_TIME_RECEIVED, responseType, 0,
+            RIL_E_SUCCESS, s_lastNITZTimeData, s_lastNITZTimeDataSize);
+        if (ret == 0) {
+            free(s_lastNITZTimeData);
+            s_lastNITZTimeData = NULL;
+        }
+    }
+}
 
 static void onNewCommandConnect(RIL_SOCKET_ID socket_id) {
     // Inform we are connected and the ril version
@@ -4081,10 +4096,7 @@
 
     // Send last NITZ time data, in case it was missed
     if (s_lastNITZTimeData != NULL) {
-        sendResponseRaw(s_lastNITZTimeData, s_lastNITZTimeDataSize, socket_id);
-
-        free(s_lastNITZTimeData);
-        s_lastNITZTimeData = NULL;
+        resendLastNITZTimeData(socket_id);
     }
 
     // Get version string
@@ -5159,11 +5171,6 @@
     rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
     assert(rwlockRet == 0);
 
-    if (ret != 0) {
-        // Problem with the response. Don't continue;
-        goto error_exit;
-    }
-
     // some things get more payload
     switch(unsolResponse) {
         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
@@ -5193,7 +5200,7 @@
     RLOGI("%s UNSOLICITED: %s length:%d", rilSocketIdToString(soc_id),
             requestToString(unsolResponse), p.dataSize());
 #endif
-    ret = 0;
+
     switch (unsolResponse) {
         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
             rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
@@ -5207,24 +5214,23 @@
     }
 
     if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
-
         // Unfortunately, NITZ time is not poll/update like everything
         // else in the system. So, if the upstream client isn't connected,
         // keep a copy of the last NITZ response (with receive time noted
         // above) around so we can deliver it when it is connected
 
         if (s_lastNITZTimeData != NULL) {
-            free (s_lastNITZTimeData);
+            free(s_lastNITZTimeData);
             s_lastNITZTimeData = NULL;
         }
 
-        s_lastNITZTimeData = calloc(p.dataSize(), 1);
+        s_lastNITZTimeData = calloc(datalen, 1);
         if (s_lastNITZTimeData == NULL) {
-             RLOGE("Memory allocation failed in RIL_onUnsolicitedResponse");
-             goto error_exit;
+            RLOGE("Memory allocation failed in RIL_onUnsolicitedResponse");
+            goto error_exit;
         }
-        s_lastNITZTimeDataSize = p.dataSize();
-        memcpy(s_lastNITZTimeData, p.data(), p.dataSize());
+        s_lastNITZTimeDataSize = datalen;
+        memcpy(s_lastNITZTimeData, data, datalen);
     }
 
     // Normal exit