Send unsolicited time response to first signal strength query after loadvm

Change-Id: I9c6260493d42564ee3aa34799313368bb2de7d4d
diff --git a/android/cmdline-options.h b/android/cmdline-options.h
index b65b29a..3ee5f45 100644
--- a/android/cmdline-options.h
+++ b/android/cmdline-options.h
@@ -84,6 +84,7 @@
 OPT_FLAG ( no_snapshot_save, "do not auto-save to snapshot on exit: abandon changed state" )
 OPT_FLAG ( no_snapshot_load, "do not auto-start from snapshot: perform a full boot" )
 OPT_FLAG ( snapshot_list,  "show a list of available snapshots" )
+OPT_FLAG ( no_snapshot_update_time, "do not do try to correct snapshot time on restore" )
 #endif
 OPT_FLAG ( wipe_data, "reset the use data image (copy it from initdata)" )
 CFG_PARAM( avd, "<name>", "use a specific android virtual device" )
diff --git a/android/help.c b/android/help.c
index 93ae8b1..f226cf6 100644
--- a/android/help.c
+++ b/android/help.c
@@ -721,6 +721,17 @@
 }
 
 static void
+help_no_snapshot_update_time(stralloc_t*  out)
+{
+    PRINTF(
+    "  Prevent the emulator from sending an unsolicited time update\n"
+    "  in response to the first signal strength query after loadvm,\n"
+    "  to avoid a sudden time jump that might upset testing. (Signal\n"
+    "  strength is queried approximately every 15 seconds)\n\n"
+    );
+}
+
+static void
 help_snapshot_list(stralloc_t*  out)
 {
     PRINTF(
diff --git a/android/main-ui.c b/android/main-ui.c
index b0cd538..5f3b8e9 100644
--- a/android/main-ui.c
+++ b/android/main-ui.c
@@ -1647,13 +1647,13 @@
         } else if (opts->no_snapshot_load || opts->no_snapshot_save) {
             D("ignoring redundant option(s) '-no-snapshot-load' and/or '-no-snapshot-save' implied by '-no-snapshot'");
         }
+        // TODO: Convey -no-snapshot-time-update to core subprocess (?)
     } else if (opts->snapshot || opts->snapstorage) {
         dwarning("option '-no-snapstorage' overrides '-snapshot' and '-snapstorage', "
                  "continuing with full boot, state snapshots are disabled");
     } else if (opts->no_snapshot) {
         D("ignoring redundant option '-no-snapshot' implied by '-no-snapstorage'");
     }
-
     if (opts->snapshot_list) {
         snapshot_print_and_exit(opts->snapstorage);
     }
diff --git a/android/main.c b/android/main.c
index 03bd472..0eebffb 100644
--- a/android/main.c
+++ b/android/main.c
@@ -1425,6 +1425,9 @@
         } else if (opts->no_snapshot_load || opts->no_snapshot_save) {
             D("ignoring redundant option(s) '-no-snapshot-load' and/or '-no-snapshot-save' implied by '-no-snapshot'");
         }
+        if (opts->no_snapshot_update_time) {
+            android_snapshot_update_time = 0;
+        }
     } else if (opts->snapshot || opts->snapstorage) {
         dwarning("option '-no-snapstorage' overrides '-snapshot' and '-snapstorage', "
                  "continuing with full boot, state snapshots are disabled");
diff --git a/android/snapshot.h b/android/snapshot.h
index be03818..c0d5965 100644
--- a/android/snapshot.h
+++ b/android/snapshot.h
@@ -26,6 +26,10 @@
  */
 void snapshot_print_and_exit( const char *snapstorage );
 
+
+extern int android_snapshot_update_time;
+extern int android_snapshot_update_time_request;
+
 #endif
 
 #endif /* SNAPSHOTS_H_ */
diff --git a/savevm.c b/savevm.c
index 0e908f3..0905963 100644
--- a/savevm.c
+++ b/savevm.c
@@ -92,6 +92,7 @@
 #include "migration.h"
 #include "qemu_socket.h"
 #include "qemu_file.h"
+#include "android/snapshot.h"
 
 /* point to the block driver where the snapshots are managed */
 static BlockDriverState *bs_snapshots;
@@ -1283,6 +1284,7 @@
 void do_loadvm(Monitor *mon, const char *name)
 {
     OutputChannel *oc = output_channel_alloc(mon, monitor_output_channel_cb);
+    android_snapshot_update_time_request = 1;
     do_loadvm_oc(oc, name);
     output_channel_free(oc);
 }
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 },