| // Copyright 2022 The Android Open Source Project |
| // |
| // This software is licensed under the terms of the GNU General Public |
| // License version 2, as published by the Free Software Foundation, and |
| // may be copied, distributed, and modified under those terms. |
| // |
| // This program is distributed in the hope that it will be useful, |
| // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| // GNU General Public License for more details. |
| #include <cassert> |
| #include <string> |
| #include "android/avd/info.h" |
| #include "android/base/system/System.h" |
| #include "android/cmdline-definitions.h" |
| #include "android/emulation/QemuMiscPipe.h" |
| #include "android/emulation/control/BootCompletionHandler.h" |
| #include "android/emulation/control/globals_agent.h" |
| #include "android/utils/debug.h" |
| #include "host-common/hw-config.h" |
| |
| using android::base::System; |
| using android::emulation::control::BootCompletionHandler; |
| using std::chrono::milliseconds; |
| |
| AndroidOptions* sAndroid_cmdLineOptions = nullptr; |
| |
| AvdInfo* sAndroid_avdInfo = nullptr; |
| AndroidHwConfig s_hwConfig = {0}; |
| AvdInfoParams sAndroid_avdInfoParams = {0}; |
| std::string sCmdlLine; |
| LanguageSettings s_languageSettings = {0}; |
| AUserConfig* s_userConfig = nullptr; |
| bool sKeyCodeForwarding = false; |
| |
| // /* this indicates that guest has mounted data partition */ |
| int s_guest_data_partition_mounted = 0; |
| // /* this indicates that guest has boot completed */ |
| bool s_guest_boot_completed = 0; |
| bool s_arm_snapshot_save_completed = 0; |
| bool s_host_emulator_is_headless = 0; |
| // /* are we using the emulator in the android mode or plain qemu? */ |
| bool s_android_qemu_mode = true; |
| // /* are we using android-emu libraries for a minimal configuration? */ |
| // Min config mode and fuchsia mode are equivalent, at least for now. |
| bool s_min_config_qemu_mode = false; |
| // /* is android-emu running Fuchsia? */ |
| int s_android_snapshot_update_timer = 0; |
| |
| static const QAndroidGlobalVarsAgent globalVarsAgent = { |
| .avdParams = []() { return &sAndroid_avdInfoParams; }, |
| .avdInfo = |
| []() { |
| // Do not access the info before it is injected! |
| assert(sAndroid_avdInfo != nullptr); |
| return sAndroid_avdInfo; |
| }, |
| .hw = []() { return &s_hwConfig; }, |
| // /* this indicates that guest has mounted data partition */ |
| .guest_data_partition_mounted = |
| []() { return s_guest_data_partition_mounted; }, |
| |
| // /* this indicates that guest has boot completed */ |
| .guest_boot_completed = []() { return s_guest_boot_completed; }, |
| |
| .arm_snapshot_save_completed = |
| []() { return s_arm_snapshot_save_completed; }, |
| |
| .host_emulator_is_headless = |
| []() { return s_host_emulator_is_headless; }, |
| |
| // /* are we using the emulator in the android mode or plain qemu? */ |
| .android_qemu_mode = []() { return s_android_qemu_mode; }, |
| |
| // /* are we using android-emu libraries for a minimal configuration? */ |
| .min_config_qemu_mode = []() { return s_min_config_qemu_mode; }, |
| |
| // /* is android-emu running Fuchsia? */ |
| .is_fuchsia = []() { return s_min_config_qemu_mode; }, |
| |
| .android_snapshot_update_timer = |
| []() { return s_android_snapshot_update_timer; }, |
| .language = []() { return &s_languageSettings; }, |
| .use_keycode_forwarding = []() { return sKeyCodeForwarding; }, |
| .userConfig = []() { return s_userConfig; }, |
| .android_cmdLineOptions = []() { return sAndroid_cmdLineOptions; }, |
| .inject_cmdLineOptions = |
| [](AndroidOptions* opts) { sAndroid_cmdLineOptions = opts; }, |
| .has_cmdLineOptions = |
| []() { |
| return globalVarsAgent.android_cmdLineOptions() != nullptr; |
| }, |
| .android_cmdLine = []() { return (const char*)sCmdlLine.c_str(); }, |
| .inject_android_cmdLine = |
| [](const char* cmdline) { sCmdlLine = cmdline; }, |
| .inject_language = |
| [](char* language, char* country, char* locale) { |
| s_languageSettings.language = language; |
| s_languageSettings.country = country; |
| s_languageSettings.locale = locale; |
| s_languageSettings.changing_language_country_locale = |
| language || country || locale; |
| }, |
| .inject_userConfig = [](AUserConfig* config) { s_userConfig = config; }, |
| .set_keycode_forwarding = |
| [](bool enabled) { sKeyCodeForwarding = enabled; }, |
| .inject_AvdInfo = [](AvdInfo* avd) { sAndroid_avdInfo = avd; }, |
| |
| // /* this indicates that guest has mounted data partition */ |
| .set_guest_data_partition_mounted = |
| [](int guest_data_partition_mounted) { |
| s_guest_data_partition_mounted = |
| guest_data_partition_mounted; |
| }, |
| |
| // /* this indicates that guest has boot completed */ |
| .set_guest_boot_completed = |
| [](bool guest_boot_completed) { |
| milliseconds bootTimeInMs = milliseconds(0); |
| |
| if (guest_boot_completed) { |
| bootTimeInMs = milliseconds(System::get() |
| ->getProcessTimes() |
| .wallClockMs) - |
| milliseconds(get_uptime_since_reset()); |
| } |
| |
| s_guest_boot_completed = guest_boot_completed; |
| BootCompletionHandler::get()->setBootTime(bootTimeInMs); |
| BootCompletionHandler::get()->signalBootChange( |
| s_guest_boot_completed); |
| }, |
| .set_arm_snapshot_save_completed = |
| [](bool arm_snapshot_save_completed) { |
| s_arm_snapshot_save_completed = arm_snapshot_save_completed; |
| }, |
| |
| .set_host_emulator_is_headless = |
| [](bool host_emulator_is_headless) { |
| s_host_emulator_is_headless = host_emulator_is_headless; |
| }, |
| |
| // /* are we using the emulator in the android mode or plain qemu? */ |
| .set_android_qemu_mode = |
| [](bool android_qemu_mode) { |
| s_android_qemu_mode = android_qemu_mode; |
| }, |
| |
| // /* are we using android-emu libraries for a minimal configuration? */ |
| .set_min_config_qemu_mode = |
| [](bool min_config_qemu_mode) { |
| s_min_config_qemu_mode = min_config_qemu_mode; |
| }, |
| |
| // /* is android-emu running Fuchsia? */ |
| .set_is_fuchsia = |
| [](bool is_fuchsia) { s_min_config_qemu_mode = is_fuchsia; }, |
| |
| .set_android_snapshot_update_timer = |
| [](int android_snapshot_update_timer) { |
| s_android_snapshot_update_timer = |
| android_snapshot_update_timer; |
| } |
| |
| }; |
| |
| extern "C" const QAndroidGlobalVarsAgent* const gQAndroidGlobalVarsAgent = |
| &globalVarsAgent; |