Moved radio, gps and audio setup entirely into the core

Change-Id: I9d76bb3cd892b7b2b0713a27ff5477195c93ff36
diff --git a/android/main.c b/android/main.c
index 25710bb..42c3a3f 100644
--- a/android/main.c
+++ b/android/main.c
@@ -32,9 +32,6 @@
 #include "math.h"
 
 #include "android/charmap.h"
-#include "modem_driver.h"
-#include "proxy_http.h"
-
 #include "android/utils/debug.h"
 #include "android/resource.h"
 #include "android/config.h"
@@ -106,9 +103,6 @@
 
 unsigned long   android_verbose;
 
-int   qemu_cpu_delay = 0;
-int   qemu_cpu_delay_count;
-
 /***********************************************************************/
 /***********************************************************************/
 /*****                                                             *****/
@@ -1228,73 +1222,6 @@
         }
     }
 
-    /* the purpose of -no-audio is to disable sound output from the emulator,
-     * not to disable Audio emulation. So simply force the 'none' backends */
-    if (opts->no_audio)
-        opts->audio = "none";
-
-    if (opts->audio) {
-        if (opts->audio_in || opts->audio_out) {
-            derror( "you can't use -audio with -audio-in or -audio-out\n" );
-            exit(1);
-        }
-        if ( !audio_check_backend_name( 0, opts->audio ) ) {
-            derror( "'%s' is not a valid audio output backend. see -help-audio-out\n",
-                    opts->audio);
-            exit(1);
-        }
-        opts->audio_out = opts->audio;
-        opts->audio_in  = opts->audio;
-
-        if ( !audio_check_backend_name( 1, opts->audio ) ) {
-            fprintf(stderr,
-                    "emulator: warning: '%s' is not a valid audio input backend. audio record disabled\n",
-                    opts->audio);
-            opts->audio_in = "none";
-        }
-    }
-
-    if (opts->audio_in) {
-        static char  env[64]; /* note: putenv needs a static unique string buffer */
-        if ( !audio_check_backend_name( 1, opts->audio_in ) ) {
-            derror( "'%s' is not a valid audio input backend. see -help-audio-in\n",
-                    opts->audio_in);
-            exit(1);
-        }
-        bufprint( env, env+sizeof(env), "QEMU_AUDIO_IN_DRV=%s", opts->audio_in );
-        putenv( env );
-
-        if (!hw->hw_audioInput) {
-            dwarning( "Emulated hardware doesn't have audio input.");
-        }
-    }
-    if (opts->audio_out) {
-        static char  env[64]; /* note: putenv needs a static unique string buffer */
-        if ( !audio_check_backend_name( 0, opts->audio_out ) ) {
-            derror( "'%s' is not a valid audio output backend. see -help-audio-out\n",
-                    opts->audio_out);
-            exit(1);
-        }
-        bufprint( env, env+sizeof(env), "QEMU_AUDIO_OUT_DRV=%s", opts->audio_out );
-        putenv( env );
-        if (!hw->hw_audioOutput) {
-            dwarning( "Emulated hardware doesn't have audio output");
-        }
-    }
-
-    if (opts->cpu_delay) {
-        char*   end;
-        long    delay = strtol(opts->cpu_delay, &end, 0);
-        if (end == NULL || *end || delay < 0 || delay > 1000 ) {
-            fprintf(stderr, "option -cpu-delay must be an integer between 0 and 1000\n" );
-            exit(1);
-        }
-        if (delay > 0)
-            delay = (1000-delay);
-
-        qemu_cpu_delay = (int) delay;
-    }
-
     if (opts->shared_net_id) {
         char*  end;
         long   shared_net_id = strtol(opts->shared_net_id, &end, 0);
@@ -1392,6 +1319,30 @@
          }
     }
 
+    /* the purpose of -no-audio is to disable sound output from the emulator,
+     * not to disable Audio emulation. So simply force the 'none' backends */
+    if (opts->no_audio)
+        opts->audio = "none";
+
+    if (opts->audio) {
+        args[n++] = "-audio";
+        args[n++] = opts->audio;
+    }
+
+    if (opts->audio_in) {
+        args[n++] = "-audio-in";
+        args[n++] = opts->audio_in;
+    }
+    if (opts->audio_out) {
+        args[n++] = "-audio-out";
+        args[n++] = opts->audio_out;
+    }
+
+    if (opts->cpu_delay) {
+        args[n++] = "-cpu-delay";
+        args[n++] = opts->cpu_delay;
+    }
+
     if (opts->dns_server) {
         args[n++] = "-dns-server";
         args[n++] = opts->dns_server;
@@ -1603,35 +1554,13 @@
         qemud_serial = serial++;
 
         if (opts->radio) {
-            CharDriverState*  cs = qemu_chr_open("radio",opts->radio,NULL);
-            if (cs == NULL) {
-                derror( "unsupported character device specification: %s\n"
-                        "used -help-char-devices for list of available formats\n", opts->radio );
-                exit(1);
-            }
-            android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
-        }
-        else if ( hw->hw_gsmModem != 0 ) {
-            if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
-                derror( "could not initialize qemud 'gsm' channel" );
-                exit(1);
-            }
+            args[n++] = "-radio";
+            args[n++] = opts->radio;
         }
 
         if (opts->gps) {
-            CharDriverState*  cs = qemu_chr_open("gps",opts->gps,NULL);
-            if (cs == NULL) {
-                derror( "unsupported character device specification: %s\n"
-                        "used -help-char-devices for list of available formats\n", opts->gps );
-                exit(1);
-            }
-            android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
-        }
-        else if ( hw->hw_gps != 0 ) {
-            if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
-                derror( "could not initialize qemud 'gps' channel" );
-                exit(1);
-            }
+            args[n++] = "-gps";
+            args[n++] = opts->gps;
         }
     }
 
diff --git a/qemu-options.hx b/qemu-options.hx
index 927daaf..e810c5c 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1636,4 +1636,22 @@
 DEF("dns-server", HAS_ARG, QEMU_OPTION_dns_server, \
     "-dns-server <servers> use this DNS server(s) in the emulated system\n")
 
+DEF("radio", HAS_ARG, QEMU_OPTION_radio, \
+    "-radio <device> redirect radio modem interface to character device\n")
+
+DEF("gps", HAS_ARG, QEMU_OPTION_gps, \
+    "-gps <device> redirect NMEA GPS to character device\n")
+
+DEF("audio", HAS_ARG, QEMU_OPTION_audio, \
+    "-audio <backend> use specific audio backend\n")
+
+DEF("audio-in", HAS_ARG, QEMU_OPTION_audio_in, \
+    "-audio-in <backend> use specific audio input backend\n")
+
+DEF("audio-out", HAS_ARG, QEMU_OPTION_audio_out, \
+    "-audio-out <backend> use specific audio output backend\n")
+
+DEF("cpu-delay", HAS_ARG, QEMU_OPTION_cpu_delay, \
+    "-cpu-delay <cpudelay> throttle CPU emulation\n")
+
 #endif
diff --git a/vl-android.c b/vl-android.c
index 8e38b7f..c66e4e0 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -55,6 +55,7 @@
 #include "android/hw-kmsg.h"
 #include "android/charmap.h"
 #include "android/globals.h"
+#include "android/utils/bufprint.h"
 #include "targphys.h"
 
 #ifdef CONFIG_MEMCHECK
@@ -332,7 +333,7 @@
 uint8_t qemu_uuid[16];
 
 
-extern int   qemu_cpu_delay;
+int   qemu_cpu_delay;
 extern char* audio_input_source;
 
 extern char* android_op_ports;
@@ -354,6 +355,23 @@
 /* -dns-server option value. */
 char* android_op_dns_server = NULL;
 
+char* android_op_radio = NULL;
+
+/* -gps option value. */
+char* android_op_gps = NULL;
+
+/* -audio option value. */
+char* android_op_audio = NULL;
+
+/* -audio-in option value. */
+char* android_op_audio_in = NULL;
+
+/* -audio-out option value. */
+char* android_op_audio_out = NULL;
+
+/* -cpu-delay option value. */
+char* android_op_cpu_delay = NULL;
+
 extern int android_display_width;
 extern int android_display_height;
 extern int android_display_bpp;
@@ -5878,6 +5896,30 @@
                 android_op_dns_server = (char*)optarg;
                 break;
 
+            case QEMU_OPTION_radio:
+                android_op_radio = (char*)optarg;
+                break;
+
+            case QEMU_OPTION_gps:
+                android_op_gps = (char*)optarg;
+                break;
+
+            case QEMU_OPTION_audio:
+                android_op_audio = (char*)optarg;
+                break;
+
+            case QEMU_OPTION_audio_in:
+                android_op_audio_in = (char*)optarg;
+                break;
+
+            case QEMU_OPTION_audio_out:
+                android_op_audio_out = (char*)optarg;
+                break;
+
+            case QEMU_OPTION_cpu_delay:
+                android_op_cpu_delay = (char*)optarg;
+                break;
+
 #ifdef CONFIG_MEMCHECK
             case QEMU_OPTION_android_memcheck:
                 android_op_memcheck = (char*)optarg;
@@ -5932,6 +5974,103 @@
     iniFile_free(hw_ini);
 #endif  // CONFIG_STANDALONE_CORE
 
+    /* Initialize modem */
+    if (android_op_radio) {
+        CharDriverState*  cs = qemu_chr_open("radio", android_op_radio, NULL);
+        if (cs == NULL) {
+            fprintf(stderr, "unsupported character device specification: %s\n"
+                            "used -help-char-devices for list of available formats\n",
+                    android_op_radio);
+            exit(1);
+        }
+        android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
+    } else if (android_hw->hw_gsmModem != 0 ) {
+        if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
+            fprintf(stderr, "could not initialize qemud 'gsm' channel");
+            exit(1);
+        }
+    }
+
+    /* Initialize GPS */
+    if (android_op_gps) {
+        CharDriverState*  cs = qemu_chr_open("gps", android_op_gps, NULL);
+        if (cs == NULL) {
+            fprintf(stderr, "unsupported character device specification: %s\n"
+                            "used -help-char-devices for list of available formats\n",
+                    android_op_gps);
+            exit(1);
+        }
+        android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
+    } else if (android_hw->hw_gps != 0) {
+        if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
+            fprintf(stderr, "could not initialize qemud 'gps' channel" );
+            exit(1);
+        }
+    }
+
+    /* Initialize audio. */
+    if (android_op_audio) {
+        if (android_op_audio_in || android_op_audio_out) {
+            fprintf(stderr, "you can't use -audio with -audio-in or -audio-out\n" );
+            exit(1);
+        }
+        if ( !audio_check_backend_name( 0, android_op_audio ) ) {
+            fprintf(stderr, "'%s' is not a valid audio output backend. see -help-audio-out\n",
+                    android_op_audio);
+            exit(1);
+        }
+        android_op_audio_out = android_op_audio;
+        android_op_audio_in  = android_op_audio;
+
+        if ( !audio_check_backend_name( 1, android_op_audio ) ) {
+            fprintf(stdout,
+                    "emulator: warning: '%s' is not a valid audio input backend. audio record disabled\n",
+                    android_op_audio);
+            android_op_audio_in = "none";
+        }
+    }
+
+    if (android_op_audio_in) {
+        static char  env[64]; /* note: putenv needs a static unique string buffer */
+        if ( !audio_check_backend_name( 1, android_op_audio_in ) ) {
+            fprintf(stderr, "'%s' is not a valid audio input backend. see -help-audio-in\n",
+                    android_op_audio_in);
+            exit(1);
+        }
+        bufprint( env, env+sizeof(env), "QEMU_AUDIO_IN_DRV=%s", android_op_audio_in );
+        putenv( env );
+
+        if (!android_hw->hw_audioInput) {
+            fprintf(stdout, "Emulated hardware doesn't have audio input.\n");
+        }
+    }
+    if (android_op_audio_out) {
+        static char  env[64]; /* note: putenv needs a static unique string buffer */
+        if ( !audio_check_backend_name( 0, android_op_audio_out ) ) {
+            fprintf(stderr, "'%s' is not a valid audio output backend. see -help-audio-out\n",
+                    android_op_audio_out);
+            exit(1);
+        }
+        bufprint( env, env+sizeof(env), "QEMU_AUDIO_OUT_DRV=%s", android_op_audio_out );
+        putenv( env );
+        if (!android_hw->hw_audioOutput) {
+            fprintf(stdout, "Emulated hardware doesn't have audio output\n");
+        }
+    }
+
+    if (android_op_cpu_delay) {
+        char*   end;
+        long    delay = strtol(android_op_cpu_delay, &end, 0);
+        if (end == NULL || *end || delay < 0 || delay > 1000 ) {
+            fprintf(stderr, "option -cpu-delay must be an integer between 0 and 1000\n" );
+            exit(1);
+        }
+        if (delay > 0)
+            delay = (1000-delay);
+
+        qemu_cpu_delay = (int) delay;
+    }
+
     if (android_op_dns_server) {
         char*  x = strchr(android_op_dns_server, ',');
         dns_count = 0;