Send unsolicited time response to first signal strength query after loadvm

Change-Id: I9c6260493d42564ee3aa34799313368bb2de7d4d
diff --git a/telephony/android_modem.c b/telephony/android_modem.c
index 0df1186..009b5af 100644
--- a/telephony/android_modem.c
+++ b/telephony/android_modem.c
@@ -12,6 +12,8 @@
 #include "android/android.h"
 #include "android_modem.h"
 #include "android/config.h"
+#include "android/config/config.h"
+#include "android/snapshot.h"
 #include "android/utils/debug.h"
 #include "android/utils/timezone.h"
 #include "android/utils/system.h"
@@ -1793,7 +1795,9 @@
     return amodem_end_line( modem );
 }
 
-/* retrieve the current time and zone in a format suitable
+/* Add a(n unsolicited) time response.
+ *
+ * retrieve the current time and zone in a format suitable
  * for %CTZV: unsolicited message
  *  "yy/mm/dd,hh:mm:ss(+/-)tz"
  *   mm is 0-based
@@ -1803,8 +1807,8 @@
  * separator, so use a column (:) instead, the Java parsing code won't see a difference
  *
  */
-static const char*
-handleEndOfInit( const char*  cmd, AModem  modem )
+static void
+amodem_addTimeUpdate( AModem  modem )
 {
     time_t       now = time(NULL);
     struct tm    utc, local;
@@ -1854,13 +1858,22 @@
     * and deal with this case (since it normally relied on the operator's country code
     * which is hard to simulate on a general-purpose computer
     */
-    return amodem_printf( modem, "%%CTZV: %02d/%02d/%02d:%02d:%02d:%02d%c%d:%d:%s",
-             (utc.tm_year + 1900) % 100, utc.tm_mon + 1, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
+    amodem_add_line( modem, "%%CTZV: %02d/%02d/%02d:%02d:%02d:%02d%c%d:%d:%s\r\n",
+             (utc.tm_year + 1900) % 100, utc.tm_mon + 1, utc.tm_mday,
+             utc.tm_hour, utc.tm_min, utc.tm_sec,
              (tzdiff >= 0) ? '+' : '-', (tzdiff >= 0 ? tzdiff : -tzdiff) / 15,
              (local.tm_isdst > 0),
              tzname );
 }
 
+static const char*
+handleEndOfInit( const char*  cmd, AModem  modem )
+{
+    amodem_begin_line( modem );
+    amodem_addTimeUpdate( modem );
+    return amodem_end_line( modem );
+}
+
 
 static const char*
 handleListPDPContexts( const char*  cmd, AModem  modem )
@@ -2114,6 +2127,32 @@
     return NULL;
 }
 
+#if CONFIG_ANDROID_SNAPSHOTS
+int android_snapshot_update_time = 1;
+int android_snapshot_update_time_request = 0;
+#endif
+
+static const char*
+handleSignalStrength( const char*  cmd, AModem  modem )
+{
+   /* XXX: TODO: implement variable signal strength and error rates */
+    amodem_begin_line( modem );
+#if CONFIG_ANDROID_SNAPSHOTS
+    /* Sneak time updates into the SignalStrength request, because it's periodic.
+     * Ideally, we'd be able to prod the guest into asking immediately on restore
+     * from snapshot, but that'd require a driver.
+     */
+    if ( android_snapshot_update_time && android_snapshot_update_time_request ) {
+      amodem_addTimeUpdate( modem );
+      android_snapshot_update_time_request = 0;
+    }
+#endif
+    // rssi = 0 (<-113dBm) 1 (<-111) 2-30 (<-109--53) 31 (>=-51) 99 (?!)
+    // ber (bit error rate) - always 99 (unknown), apparently.
+    amodem_add_line( modem, "+CSQ: %i,99\r\n", 27 );
+    return amodem_end_line( modem );
+}
+
 static const char*
 handleHangup( const char*  cmd, AModem  modem )
 {
@@ -2282,7 +2321,7 @@
     { "!+CHLD=", NULL, handleHangup },
 
     /* see requestSignalStrength() */
-    { "+CSQ", "+CSQ: 7,99", NULL },  /* XXX: TODO: implement variable signal strength and error rates */
+    { "+CSQ", NULL, handleSignalStrength },
 
     /* see requestRegistrationState() */
     { "!+CREG", NULL, handleNetworkRegistration },