Snap for 5743241 from 2c7c01cfc60cd8f8998fc10b4d2567f541df5a18 to qt-c2f2-release

Change-Id: I43d6e6670461b4ffd25064bdfbeee7c8978e5aa5
diff --git a/host/msm/daemon/chre_daemon.cc b/host/msm/daemon/chre_daemon.cc
index dbaab2e..2dd253d 100644
--- a/host/msm/daemon/chre_daemon.cc
+++ b/host/msm/daemon/chre_daemon.cc
@@ -293,6 +293,22 @@
   return timeOffset;
 }
 
+static int startChreThread() {
+  int ret;
+  // Retry a few times in case the SLPI is not ready yet.
+  constexpr size_t kMaxNumRetries = 5;
+  size_t numRetries = 0;
+  useconds_t retryDelayUs = 50000; // 50 ms initially
+  while (((ret = chre_slpi_start_thread()) != CHRE_FASTRPC_SUCCESS)
+         && numRetries < kMaxNumRetries) {
+    usleep(retryDelayUs);
+    numRetries++;
+    retryDelayUs *= 2;
+  }
+
+  return ret;
+}
+
 static void sendTimeSyncMessage() {
   bool timeSyncSuccess = true;
   int64_t timeOffset = getTimeOffset(&timeSyncSuccess);
@@ -305,7 +321,8 @@
         static_cast<int>(builder.GetSize()));
 
     if (success != 0) {
-      LOGE("Failed to deliver timestamp message from host to CHRE: %d", success);
+      LOGE("Failed to deliver time sync message from host to CHRE: %d",
+           success);
     }
   }
 }
@@ -962,9 +979,7 @@
     LOGE("Couldn't initialize LPMA enable thread");
 #endif  // CHRE_DAEMON_LPMA_ENABLED
   } else {
-    // Send time offset message before nanoapps start
-    sendTimeSyncMessage();
-    if ((ret = chre_slpi_start_thread()) != CHRE_FASTRPC_SUCCESS) {
+    if ((ret = startChreThread()) != CHRE_FASTRPC_SUCCESS) {
       LOGE("Failed to start CHRE: %d", ret);
     } else {
       if (!start_thread(&monitor_thread, chre_monitor_thread, NULL)) {
@@ -974,6 +989,8 @@
         LOGE("Couldn't start CHRE->Host message thread");
       } else {
         LOGI("CHRE started");
+        // Send a time sync message before any of the nanoapps start.
+        sendTimeSyncMessage();
         loadPreloadedNanoapps();
 
         // TODO: take 2nd argument as command-line parameter