Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "host/libs/config/cuttlefish_config.h" |
| 18 | |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 20 | #include <climits> |
| 21 | #include <cstdlib> |
| 22 | #include <cstring> |
| 23 | #include <fstream> |
| 24 | #include <iomanip> |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 25 | #include <iterator> |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 29 | #include <glog/logging.h> |
| 30 | #include <json/json.h> |
| 31 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 32 | #include "common/libs/utils/environment.h" |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 33 | #include "common/libs/utils/files.h" |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 34 | #include "host/libs/vm_manager/qemu_manager.h" |
| 35 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 36 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 39 | int InstanceFromEnvironment() { |
| 40 | static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 41 | static constexpr int kDefaultInstance = 1; |
| 42 | |
| 43 | // CUTTLEFISH_INSTANCE environment variable |
| 44 | const char* instance_str = std::getenv(kInstanceEnvironmentVariable); |
| 45 | if (!instance_str) { |
| 46 | // Try to get it from the user instead |
| 47 | instance_str = std::getenv("USER"); |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 48 | |
| 49 | if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix, |
| 50 | sizeof(vsoc::kVsocUserPrefix) - 1)) { |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 51 | // No user or we don't recognize this user |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 52 | LOG(WARNING) << "No user or non-vsoc user, returning default config"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 53 | return kDefaultInstance; |
| 54 | } |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 55 | instance_str += sizeof(vsoc::kVsocUserPrefix) - 1; |
| 56 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 57 | // Set the environment variable so that child processes see it |
| 58 | setenv(kInstanceEnvironmentVariable, instance_str, 0); |
| 59 | } |
| 60 | |
| 61 | int instance = std::atoi(instance_str); |
| 62 | if (instance <= 0) { |
| 63 | instance = kDefaultInstance; |
| 64 | } |
| 65 | |
| 66 | return instance; |
| 67 | } |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 68 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 69 | const char* kSerialNumber = "serial_number"; |
| 70 | const char* kInstanceDir = "instance_dir"; |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 71 | const char* kVmManager = "vm_manager"; |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 72 | const char* kHardwareName = "hardware_name"; |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 73 | const char* kDeviceTitle = "device_title"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 74 | |
| 75 | const char* kCpus = "cpus"; |
| 76 | const char* kMemoryMb = "memory_mb"; |
| 77 | const char* kDpi = "dpi"; |
| 78 | const char* kXRes = "x_res"; |
| 79 | const char* kYRes = "y_res"; |
Cody Schuffelen | 5bc697c | 2019-01-28 21:05:29 -0800 | [diff] [blame] | 80 | const char* kNumScreenBuffers = "num_screen_buffers"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 81 | const char* kRefreshRateHz = "refresh_rate_hz"; |
| 82 | |
| 83 | const char* kKernelImagePath = "kernel_image_path"; |
Cody Schuffelen | e13a78a | 2019-01-29 14:51:26 -0800 | [diff] [blame] | 84 | const char* kUseUnpackedKernel = "use_unpacked_kernel"; |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 85 | const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path"; |
| 86 | const char* kDecompressKernel = "decompress_kernel"; |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 87 | const char* kGdbFlag = "gdb_flag"; |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 88 | const char* kKernelCmdline = "kernel_cmdline"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 89 | const char* kRamdiskImagePath = "ramdisk_image_path"; |
| 90 | |
| 91 | const char* kSystemImagePath = "system_image_path"; |
| 92 | const char* kCacheImagePath = "cache_image_path"; |
| 93 | const char* kDataImagePath = "data_image_path"; |
| 94 | const char* kVendorImagePath = "vendor_image_path"; |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 95 | const char* kMetadataImagePath = "metadata_image_path"; |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 96 | const char* kProductImagePath = "product_image_path"; |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame^] | 97 | const char* kSuperImagePath = "super_image_path"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 98 | const char* kUsbV1SocketName = "usb_v1_socket_name"; |
| 99 | const char* kVhciPort = "vhci_port"; |
| 100 | const char* kUsbIpSocketName = "usb_ip_socket_name"; |
| 101 | const char* kKernelLogSocketName = "kernel_log_socket_name"; |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 102 | const char* kDeprecatedBootCompleted = "deprecated_boot_completed"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 103 | const char* kConsolePath = "console_path"; |
| 104 | const char* kLogcatPath = "logcat_path"; |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 105 | const char* kLauncherLogPath = "launcher_log_path"; |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 106 | const char* kLauncherMonitorPath = "launcher_monitor_socket"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 107 | const char* kDtbPath = "dtb_path"; |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 108 | const char* kGsiFstabPath = "gsi.fstab_path"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 109 | |
| 110 | const char* kMempath = "mempath"; |
| 111 | const char* kIvshmemQemuSocketPath = "ivshmem_qemu_socket_path"; |
| 112 | const char* kIvshmemClientSocketPath = "ivshmem_client_socket_path"; |
| 113 | const char* kIvshmemVectorCount = "ivshmem_vector_count"; |
| 114 | |
| 115 | const char* kMobileBridgeName = "mobile_bridge_name"; |
| 116 | const char* kMobileTapName = "mobile_tap_name"; |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 117 | const char* kWifiTapName = "wifi_tap_name"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 118 | const char* kWifiGuestMacAddr = "wifi_guest_mac_addr"; |
| 119 | const char* kWifiHostMacAddr = "wifi_host_mac_addr"; |
| 120 | const char* kEntropySource = "entropy_source"; |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 121 | const char* kVsockGuestCid = "vsock_guest_cid"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 122 | |
| 123 | const char* kUuid = "uuid"; |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 124 | const char* kCuttlefishEnvPath = "cuttlefish_env_path"; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 125 | |
| 126 | const char* kAdbMode = "adb_mode"; |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 127 | const char* kAdbIPAndPort = "adb_ip_and_port"; |
Greg Hartman | f1f28c5 | 2018-09-20 17:19:19 -0700 | [diff] [blame] | 128 | const char* kSetupWizardMode = "setupwizard_mode"; |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 129 | |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 130 | const char* kQemuBinary = "qemu_binary"; |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 131 | const char* kCrosvmBinary = "crosvm_binary"; |
Cody Schuffelen | 5bc697c | 2019-01-28 21:05:29 -0800 | [diff] [blame] | 132 | const char* kIvServerBinary = "ivserver_binary"; |
Cody Schuffelen | 55676ca | 2019-01-28 22:00:05 -0800 | [diff] [blame] | 133 | const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary"; |
Cody Schuffelen | 17b3472 | 2019-01-28 22:57:14 -0800 | [diff] [blame] | 134 | |
| 135 | const char* kEnableVncServer = "enable_vnc_server"; |
| 136 | const char* kVncServerBinary = "vnc_server_binary"; |
| 137 | const char* kVncServerPort = "vnc_server_port"; |
| 138 | |
| 139 | const char* kEnableStreamAudio = "enable_stream_audio"; |
| 140 | const char* kStreamAudioBinary = "stream_audio_binary"; |
| 141 | const char* kStreamAudioPort = "stream_audio_port"; |
Cody Schuffelen | d12e0c9 | 2019-01-29 15:44:45 -0800 | [diff] [blame] | 142 | |
| 143 | const char* kRestartSubprocesses = "restart_subprocesses"; |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 144 | const char* kRunAdbConnector = "run_adb_connector"; |
| 145 | const char* kAdbConnectorBinary = "adb_connector_binary"; |
| 146 | const char* kVirtualUsbManagerBinary = "virtual_usb_manager_binary"; |
| 147 | const char* kSocketForwardProxyBinary = "socket_forward_proxy_binary"; |
| 148 | const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary"; |
Cody Schuffelen | 5024973 | 2019-01-29 16:42:55 -0800 | [diff] [blame] | 149 | |
| 150 | const char* kRunAsDaemon = "run_as_daemon"; |
Cody Schuffelen | 3a64095 | 2019-01-29 17:14:41 -0800 | [diff] [blame] | 151 | const char* kRunE2eTest = "run_e2e_test"; |
| 152 | const char* kE2eTestBinary = "e2e_test_binary"; |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 153 | |
| 154 | const char* kDataPolicy = "data_policy"; |
| 155 | const char* kBlankDataImageMb = "blank_data_image_mb"; |
| 156 | const char* kBlankDataImageFmt = "blank_data_image_fmt"; |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 157 | |
| 158 | const char* kLogcatMode = "logcat_mode"; |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 159 | const char* kLogcatVsockPort = "logcat_vsock_port"; |
| 160 | const char* kFramesVsockPort = "frames_vsock_port"; |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 161 | const char* kLogcatReceiverBinary = "logcat_receiver_binary"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 162 | } // namespace |
| 163 | |
| 164 | namespace vsoc { |
| 165 | |
| 166 | std::string CuttlefishConfig::instance_dir() const { |
| 167 | return (*dictionary_)[kInstanceDir].asString(); |
| 168 | } |
| 169 | void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) { |
| 170 | (*dictionary_)[kInstanceDir] = instance_dir; |
| 171 | } |
| 172 | |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 173 | std::string CuttlefishConfig::vm_manager() const { |
| 174 | return (*dictionary_)[kVmManager].asString(); |
| 175 | } |
| 176 | void CuttlefishConfig::set_vm_manager(const std::string& name) { |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 177 | (*dictionary_)[kVmManager] = name; |
Jorge E. Moreira | 8832283 | 2018-07-22 16:41:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 180 | std::string CuttlefishConfig::hardware_name() const { |
| 181 | return (*dictionary_)[kHardwareName].asString(); |
| 182 | } |
| 183 | void CuttlefishConfig::set_hardware_name(const std::string& name) { |
| 184 | (*dictionary_)[kHardwareName] = name; |
| 185 | } |
| 186 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 187 | std::string CuttlefishConfig::serial_number() const { |
| 188 | return (*dictionary_)[kSerialNumber].asString(); |
| 189 | } |
| 190 | void CuttlefishConfig::set_serial_number(const std::string& serial_number) { |
| 191 | (*dictionary_)[kSerialNumber] = serial_number; |
| 192 | } |
| 193 | |
| 194 | int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); } |
| 195 | void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; } |
| 196 | |
| 197 | int CuttlefishConfig::memory_mb() const { |
| 198 | return (*dictionary_)[kMemoryMb].asInt(); |
| 199 | } |
| 200 | void CuttlefishConfig::set_memory_mb(int memory_mb) { |
| 201 | (*dictionary_)[kMemoryMb] = memory_mb; |
| 202 | } |
| 203 | |
| 204 | int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); } |
| 205 | void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; } |
| 206 | |
| 207 | int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); } |
| 208 | void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; } |
| 209 | |
| 210 | int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); } |
| 211 | void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; } |
| 212 | |
Cody Schuffelen | 5bc697c | 2019-01-28 21:05:29 -0800 | [diff] [blame] | 213 | int CuttlefishConfig::num_screen_buffers() const { |
| 214 | return (*dictionary_)[kNumScreenBuffers].asInt(); |
| 215 | } |
| 216 | void CuttlefishConfig::set_num_screen_buffers(int num_screen_buffers) { |
| 217 | (*dictionary_)[kNumScreenBuffers] = num_screen_buffers; |
| 218 | } |
| 219 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 220 | int CuttlefishConfig::refresh_rate_hz() const { |
| 221 | return (*dictionary_)[kRefreshRateHz].asInt(); |
| 222 | } |
| 223 | void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) { |
| 224 | (*dictionary_)[kRefreshRateHz] = refresh_rate_hz; |
| 225 | } |
| 226 | |
| 227 | std::string CuttlefishConfig::kernel_image_path() const { |
| 228 | return (*dictionary_)[kKernelImagePath].asString(); |
| 229 | } |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 230 | |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 231 | void CuttlefishConfig::SetPath(const std::string& key, |
| 232 | const std::string& path) { |
| 233 | if (!path.empty()) { |
| 234 | (*dictionary_)[key] = cvd::AbsolutePath(path); |
| 235 | } |
| 236 | } |
| 237 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 238 | void CuttlefishConfig::set_kernel_image_path( |
| 239 | const std::string& kernel_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 240 | SetPath(kKernelImagePath, kernel_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Cody Schuffelen | e13a78a | 2019-01-29 14:51:26 -0800 | [diff] [blame] | 243 | bool CuttlefishConfig::use_unpacked_kernel() const { |
| 244 | return (*dictionary_)[kUseUnpackedKernel].asBool(); |
| 245 | } |
| 246 | |
| 247 | void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) { |
| 248 | (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel; |
| 249 | } |
| 250 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 251 | bool CuttlefishConfig::decompress_kernel() const { |
| 252 | return (*dictionary_)[kDecompressKernel].asBool(); |
| 253 | } |
| 254 | void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) { |
| 255 | (*dictionary_)[kDecompressKernel] = decompress_kernel; |
| 256 | } |
| 257 | |
| 258 | std::string CuttlefishConfig::decompressed_kernel_image_path() const { |
| 259 | return (*dictionary_)[kDecompressedKernelImagePath].asString(); |
| 260 | } |
| 261 | void CuttlefishConfig::set_decompressed_kernel_image_path( |
| 262 | const std::string& path) { |
| 263 | SetPath(kDecompressedKernelImagePath, path); |
| 264 | } |
| 265 | |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 266 | std::string CuttlefishConfig::gdb_flag() const { |
| 267 | return (*dictionary_)[kGdbFlag].asString(); |
| 268 | } |
| 269 | |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 270 | void CuttlefishConfig::set_gdb_flag(const std::string& device) { |
Matthias Maennich | ac14e1e | 2019-02-06 16:25:42 +0000 | [diff] [blame] | 271 | (*dictionary_)[kGdbFlag] = device; |
Greg Hartman | 91f8142 | 2018-07-09 16:04:49 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 274 | std::set<std::string> CuttlefishConfig::kernel_cmdline() const { |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 275 | std::set<std::string> args_set; |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 276 | auto args_json_obj = (*dictionary_)[kKernelCmdline]; |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 277 | std::transform(args_json_obj.begin(), args_json_obj.end(), |
| 278 | std::inserter(args_set, args_set.begin()), |
| 279 | [](const Json::Value& it) { return it.asString(); }); |
| 280 | return args_set; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 281 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 282 | void CuttlefishConfig::set_kernel_cmdline( |
| 283 | const std::set<std::string>& kernel_cmdline) { |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 284 | Json::Value args_json_obj(Json::arrayValue); |
Chih-Hung Hsieh | 90c1dcc | 2018-12-12 13:43:34 -0800 | [diff] [blame] | 285 | for (const auto& arg : kernel_cmdline) { |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 286 | args_json_obj.append(arg); |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 287 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 288 | (*dictionary_)[kKernelCmdline] = args_json_obj; |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 289 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 290 | void CuttlefishConfig::add_kernel_cmdline( |
| 291 | const std::set<std::string>& extra_args) { |
| 292 | std::set<std::string> cmdline = kernel_cmdline(); |
Chih-Hung Hsieh | 90c1dcc | 2018-12-12 13:43:34 -0800 | [diff] [blame] | 293 | for (const auto& arg : extra_args) { |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 294 | if (cmdline.count(arg)) { |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 295 | LOG(ERROR) << "Kernel argument " << arg << " is duplicated"; |
| 296 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 297 | cmdline.insert(arg); |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 298 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 299 | set_kernel_cmdline(cmdline); |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 300 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 301 | void CuttlefishConfig::add_kernel_cmdline(const std::string& kernel_cmdline) { |
| 302 | std::stringstream args_stream(kernel_cmdline); |
| 303 | std::set<std::string> kernel_cmdline_set; |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 304 | using is_iter = std::istream_iterator<std::string>; |
| 305 | std::copy(is_iter(args_stream), is_iter(), |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 306 | std::inserter(kernel_cmdline_set, kernel_cmdline_set.begin())); |
| 307 | add_kernel_cmdline(kernel_cmdline_set); |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 308 | } |
Jorge E. Moreira | b2427ba | 2018-08-23 17:11:55 -0700 | [diff] [blame] | 309 | std::string CuttlefishConfig::kernel_cmdline_as_string() const { |
| 310 | auto args_set = kernel_cmdline(); |
Cody Schuffelen | efadb8d | 2018-08-10 16:10:35 -0700 | [diff] [blame] | 311 | std::stringstream output; |
| 312 | std::copy(args_set.begin(), args_set.end(), |
| 313 | std::ostream_iterator<std::string>(output, " ")); |
| 314 | return output.str(); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | std::string CuttlefishConfig::ramdisk_image_path() const { |
| 318 | return (*dictionary_)[kRamdiskImagePath].asString(); |
| 319 | } |
| 320 | void CuttlefishConfig::set_ramdisk_image_path( |
| 321 | const std::string& ramdisk_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 322 | SetPath(kRamdiskImagePath, ramdisk_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | std::string CuttlefishConfig::system_image_path() const { |
| 326 | return (*dictionary_)[kSystemImagePath].asString(); |
| 327 | } |
| 328 | void CuttlefishConfig::set_system_image_path( |
| 329 | const std::string& system_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 330 | SetPath(kSystemImagePath, system_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | std::string CuttlefishConfig::cache_image_path() const { |
| 334 | return (*dictionary_)[kCacheImagePath].asString(); |
| 335 | } |
| 336 | void CuttlefishConfig::set_cache_image_path( |
| 337 | const std::string& cache_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 338 | SetPath(kCacheImagePath, cache_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | std::string CuttlefishConfig::data_image_path() const { |
| 342 | return (*dictionary_)[kDataImagePath].asString(); |
| 343 | } |
| 344 | void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 345 | SetPath(kDataImagePath, data_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | std::string CuttlefishConfig::vendor_image_path() const { |
| 349 | return (*dictionary_)[kVendorImagePath].asString(); |
| 350 | } |
| 351 | void CuttlefishConfig::set_vendor_image_path( |
| 352 | const std::string& vendor_image_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 353 | SetPath(kVendorImagePath, vendor_image_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 356 | std::string CuttlefishConfig::metadata_image_path() const { |
| 357 | return (*dictionary_)[kMetadataImagePath].asString(); |
| 358 | } |
| 359 | void CuttlefishConfig::set_metadata_image_path( |
| 360 | const std::string& metadata_image_path) { |
| 361 | SetPath(kMetadataImagePath, metadata_image_path); |
| 362 | } |
| 363 | |
Alistair Strachan | 054af06 | 2018-11-27 12:41:19 -0800 | [diff] [blame^] | 364 | std::string CuttlefishConfig::super_image_path() const { |
| 365 | return (*dictionary_)[kSuperImagePath].asString(); |
| 366 | } |
| 367 | void CuttlefishConfig::set_super_image_path( |
| 368 | const std::string& super_image_path) { |
| 369 | SetPath(kSuperImagePath, super_image_path); |
| 370 | } |
| 371 | |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 372 | std::string CuttlefishConfig::product_image_path() const { |
| 373 | return (*dictionary_)[kProductImagePath].asString(); |
| 374 | } |
| 375 | void CuttlefishConfig::set_product_image_path( |
| 376 | const std::string& product_image_path) { |
| 377 | SetPath(kProductImagePath, product_image_path); |
| 378 | } |
| 379 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 380 | std::string CuttlefishConfig::dtb_path() const { |
| 381 | return (*dictionary_)[kDtbPath].asString(); |
| 382 | } |
| 383 | void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 384 | SetPath(kDtbPath, dtb_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 387 | std::string CuttlefishConfig::gsi_fstab_path() const { |
| 388 | return (*dictionary_)[kGsiFstabPath].asString(); |
| 389 | } |
| 390 | void CuttlefishConfig::set_gsi_fstab_path(const std::string& path){ |
| 391 | SetPath(kGsiFstabPath, path); |
| 392 | } |
| 393 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 394 | std::string CuttlefishConfig::mempath() const { |
| 395 | return (*dictionary_)[kMempath].asString(); |
| 396 | } |
| 397 | void CuttlefishConfig::set_mempath(const std::string& mempath) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 398 | SetPath(kMempath, mempath); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | std::string CuttlefishConfig::ivshmem_qemu_socket_path() const { |
| 402 | return (*dictionary_)[kIvshmemQemuSocketPath].asString(); |
| 403 | } |
| 404 | void CuttlefishConfig::set_ivshmem_qemu_socket_path( |
| 405 | const std::string& ivshmem_qemu_socket_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 406 | SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | std::string CuttlefishConfig::ivshmem_client_socket_path() const { |
| 410 | return (*dictionary_)[kIvshmemClientSocketPath].asString(); |
| 411 | } |
| 412 | void CuttlefishConfig::set_ivshmem_client_socket_path( |
| 413 | const std::string& ivshmem_client_socket_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 414 | SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | int CuttlefishConfig::ivshmem_vector_count() const { |
| 418 | return (*dictionary_)[kIvshmemVectorCount].asInt(); |
| 419 | } |
| 420 | void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) { |
| 421 | (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count; |
| 422 | } |
| 423 | |
| 424 | std::string CuttlefishConfig::usb_v1_socket_name() const { |
| 425 | return (*dictionary_)[kUsbV1SocketName].asString(); |
| 426 | } |
| 427 | void CuttlefishConfig::set_usb_v1_socket_name( |
| 428 | const std::string& usb_v1_socket_name) { |
| 429 | (*dictionary_)[kUsbV1SocketName] = usb_v1_socket_name; |
| 430 | } |
| 431 | |
| 432 | int CuttlefishConfig::vhci_port() const { |
| 433 | return (*dictionary_)[kVhciPort].asInt(); |
| 434 | } |
| 435 | void CuttlefishConfig::set_vhci_port(int vhci_port) { |
| 436 | (*dictionary_)[kVhciPort] = vhci_port; |
| 437 | } |
| 438 | |
| 439 | std::string CuttlefishConfig::usb_ip_socket_name() const { |
| 440 | return (*dictionary_)[kUsbIpSocketName].asString(); |
| 441 | } |
| 442 | void CuttlefishConfig::set_usb_ip_socket_name( |
| 443 | const std::string& usb_ip_socket_name) { |
| 444 | (*dictionary_)[kUsbIpSocketName] = usb_ip_socket_name; |
| 445 | } |
| 446 | |
| 447 | std::string CuttlefishConfig::kernel_log_socket_name() const { |
| 448 | return (*dictionary_)[kKernelLogSocketName].asString(); |
| 449 | } |
| 450 | void CuttlefishConfig::set_kernel_log_socket_name( |
| 451 | const std::string& kernel_log_socket_name) { |
| 452 | (*dictionary_)[kKernelLogSocketName] = kernel_log_socket_name; |
| 453 | } |
| 454 | |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 455 | bool CuttlefishConfig::deprecated_boot_completed() const { |
| 456 | return (*dictionary_)[kDeprecatedBootCompleted].asBool(); |
| 457 | } |
| 458 | void CuttlefishConfig::set_deprecated_boot_completed( |
| 459 | bool deprecated_boot_completed) { |
| 460 | (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed; |
| 461 | } |
| 462 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 463 | std::string CuttlefishConfig::console_path() const { |
| 464 | return (*dictionary_)[kConsolePath].asString(); |
| 465 | } |
| 466 | void CuttlefishConfig::set_console_path(const std::string& console_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 467 | SetPath(kConsolePath, console_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | std::string CuttlefishConfig::logcat_path() const { |
| 471 | return (*dictionary_)[kLogcatPath].asString(); |
| 472 | } |
| 473 | void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 474 | SetPath(kLogcatPath, logcat_path); |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 477 | std::string CuttlefishConfig::launcher_monitor_socket_path() const { |
| 478 | return (*dictionary_)[kLauncherMonitorPath].asString(); |
| 479 | } |
| 480 | void CuttlefishConfig::set_launcher_monitor_socket_path( |
Jorge E. Moreira | be7049a | 2018-07-26 18:12:13 -0700 | [diff] [blame] | 481 | const std::string& launcher_monitor_path) { |
| 482 | SetPath(kLauncherMonitorPath, launcher_monitor_path); |
Jorge E. Moreira | 50a0775 | 2018-07-18 18:49:04 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Jorge E. Moreira | 66f6ec8 | 2018-07-16 16:43:15 -0700 | [diff] [blame] | 485 | std::string CuttlefishConfig::launcher_log_path() const { |
| 486 | return (*dictionary_)[kLauncherLogPath].asString(); |
| 487 | } |
| 488 | void CuttlefishConfig::set_launcher_log_path( |
| 489 | const std::string& launcher_log_path) { |
| 490 | (*dictionary_)[kLauncherLogPath] = launcher_log_path; |
| 491 | } |
| 492 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 493 | std::string CuttlefishConfig::mobile_bridge_name() const { |
| 494 | return (*dictionary_)[kMobileBridgeName].asString(); |
| 495 | } |
| 496 | void CuttlefishConfig::set_mobile_bridge_name( |
| 497 | const std::string& mobile_bridge_name) { |
| 498 | (*dictionary_)[kMobileBridgeName] = mobile_bridge_name; |
| 499 | } |
| 500 | |
| 501 | std::string CuttlefishConfig::wifi_guest_mac_addr() const { |
| 502 | return (*dictionary_)[kWifiGuestMacAddr].asString(); |
| 503 | } |
| 504 | void CuttlefishConfig::set_wifi_guest_mac_addr( |
| 505 | const std::string& wifi_guest_mac_addr) { |
| 506 | (*dictionary_)[kWifiGuestMacAddr] = wifi_guest_mac_addr; |
| 507 | } |
| 508 | |
| 509 | std::string CuttlefishConfig::wifi_host_mac_addr() const { |
| 510 | return (*dictionary_)[kWifiHostMacAddr].asString(); |
| 511 | } |
| 512 | void CuttlefishConfig::set_wifi_host_mac_addr( |
| 513 | const std::string& wifi_host_mac_addr) { |
| 514 | (*dictionary_)[kWifiHostMacAddr] = wifi_host_mac_addr; |
| 515 | } |
| 516 | |
| 517 | std::string CuttlefishConfig::mobile_tap_name() const { |
| 518 | return (*dictionary_)[kMobileTapName].asString(); |
| 519 | } |
| 520 | void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) { |
| 521 | (*dictionary_)[kMobileTapName] = mobile_tap_name; |
| 522 | } |
| 523 | |
Cody Schuffelen | 975175a | 2018-06-14 18:20:02 -0700 | [diff] [blame] | 524 | std::string CuttlefishConfig::wifi_tap_name() const { |
| 525 | return (*dictionary_)[kWifiTapName].asString(); |
| 526 | } |
| 527 | void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) { |
| 528 | (*dictionary_)[kWifiTapName] = wifi_tap_name; |
| 529 | } |
| 530 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 531 | std::string CuttlefishConfig::entropy_source() const { |
| 532 | return (*dictionary_)[kEntropySource].asString(); |
| 533 | } |
| 534 | void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) { |
| 535 | (*dictionary_)[kEntropySource] = entropy_source; |
| 536 | } |
| 537 | |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 538 | int CuttlefishConfig::vsock_guest_cid() const { |
| 539 | return (*dictionary_)[kVsockGuestCid].asInt(); |
| 540 | } |
| 541 | |
| 542 | void CuttlefishConfig::set_vsock_guest_cid(int vsock_guest_cid) { |
| 543 | (*dictionary_)[kVsockGuestCid] = vsock_guest_cid; |
| 544 | } |
| 545 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 546 | std::string CuttlefishConfig::uuid() const { |
| 547 | return (*dictionary_)[kUuid].asString(); |
| 548 | } |
| 549 | void CuttlefishConfig::set_uuid(const std::string& uuid) { |
| 550 | (*dictionary_)[kUuid] = uuid; |
| 551 | } |
| 552 | |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 553 | void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 554 | SetPath(kCuttlefishEnvPath, path); |
Greg Hartman | 6abdbb9 | 2018-05-24 09:49:00 -0700 | [diff] [blame] | 555 | } |
| 556 | std::string CuttlefishConfig::cuttlefish_env_path() const { |
| 557 | return (*dictionary_)[kCuttlefishEnvPath].asString(); |
| 558 | } |
| 559 | |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 560 | std::set<std::string> CuttlefishConfig::adb_mode() const { |
| 561 | std::set<std::string> args_set; |
| 562 | for (auto& mode : (*dictionary_)[kAdbMode]) { |
| 563 | args_set.insert(mode.asString()); |
| 564 | } |
| 565 | return args_set; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 568 | void CuttlefishConfig::set_adb_mode(const std::set<std::string>& mode) { |
| 569 | Json::Value mode_json_obj(Json::arrayValue); |
| 570 | for (const auto& arg : mode) { |
| 571 | mode_json_obj.append(arg); |
| 572 | } |
| 573 | (*dictionary_)[kAdbMode] = mode_json_obj; |
Ryan Haining | 4a1abea | 2018-07-10 16:20:19 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 576 | std::string CuttlefishConfig::adb_ip_and_port() const { |
| 577 | return (*dictionary_)[kAdbIPAndPort].asString(); |
| 578 | } |
| 579 | |
| 580 | void CuttlefishConfig::set_adb_ip_and_port(const std::string& ip_port) { |
| 581 | (*dictionary_)[kAdbIPAndPort] = ip_port; |
| 582 | } |
| 583 | |
| 584 | std::string CuttlefishConfig::adb_device_name() const { |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 585 | // TODO(schuffelen): Deal with duplication between here and launch.cc |
| 586 | bool tunnelMode = adb_mode().count("tunnel") > 0; |
| 587 | bool vsockTunnel = adb_mode().count("vsock_tunnel") > 0; |
| 588 | bool vsockHalfProxy = adb_mode().count("vsock_half_proxy") > 0; |
| 589 | bool nativeVsock = adb_mode().count("native_vsock") > 0; |
| 590 | if (tunnelMode || vsockTunnel || vsockHalfProxy || nativeVsock) { |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 591 | return adb_ip_and_port(); |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 592 | } else if (adb_mode().count("usb") > 0) { |
Ryan Haining | 7338d35 | 2018-10-24 17:41:52 -0700 | [diff] [blame] | 593 | return serial_number(); |
| 594 | } |
| 595 | LOG(ERROR) << "no adb_mode found, returning bad device name"; |
| 596 | return "NO_ADB_MODE_SET_NO_VALID_DEVICE_NAME"; |
| 597 | } |
| 598 | |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 599 | std::string CuttlefishConfig::device_title() const { |
| 600 | return (*dictionary_)[kDeviceTitle].asString(); |
| 601 | } |
| 602 | |
| 603 | void CuttlefishConfig::set_device_title(const std::string& title) { |
| 604 | (*dictionary_)[kDeviceTitle] = title; |
| 605 | } |
| 606 | |
Greg Hartman | f1f28c5 | 2018-09-20 17:19:19 -0700 | [diff] [blame] | 607 | std::string CuttlefishConfig::setupwizard_mode() const { |
| 608 | return (*dictionary_)[kSetupWizardMode].asString(); |
| 609 | } |
| 610 | |
| 611 | void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) { |
| 612 | (*dictionary_)[kSetupWizardMode] = mode; |
| 613 | } |
| 614 | |
Cody Schuffelen | 5898003 | 2018-10-11 17:11:13 -0700 | [diff] [blame] | 615 | std::string CuttlefishConfig::qemu_binary() const { |
| 616 | return (*dictionary_)[kQemuBinary].asString(); |
| 617 | } |
| 618 | |
| 619 | void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) { |
| 620 | (*dictionary_)[kQemuBinary] = qemu_binary; |
| 621 | } |
| 622 | |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 623 | std::string CuttlefishConfig::crosvm_binary() const { |
| 624 | return (*dictionary_)[kCrosvmBinary].asString(); |
| 625 | } |
| 626 | |
| 627 | void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) { |
| 628 | (*dictionary_)[kCrosvmBinary] = crosvm_binary; |
| 629 | } |
| 630 | |
Cody Schuffelen | 5bc697c | 2019-01-28 21:05:29 -0800 | [diff] [blame] | 631 | std::string CuttlefishConfig::ivserver_binary() const { |
| 632 | return (*dictionary_)[kIvServerBinary].asString(); |
| 633 | } |
| 634 | |
| 635 | void CuttlefishConfig::set_ivserver_binary(const std::string& ivserver_binary) { |
| 636 | (*dictionary_)[kIvServerBinary] = ivserver_binary; |
| 637 | } |
| 638 | |
Cody Schuffelen | 55676ca | 2019-01-28 22:00:05 -0800 | [diff] [blame] | 639 | std::string CuttlefishConfig::kernel_log_monitor_binary() const { |
| 640 | return (*dictionary_)[kKernelLogMonitorBinary].asString(); |
| 641 | } |
| 642 | |
| 643 | void CuttlefishConfig::set_kernel_log_monitor_binary( |
| 644 | const std::string& kernel_log_monitor_binary) { |
| 645 | (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary; |
| 646 | } |
| 647 | |
Cody Schuffelen | 17b3472 | 2019-01-28 22:57:14 -0800 | [diff] [blame] | 648 | bool CuttlefishConfig::enable_vnc_server() const { |
| 649 | return (*dictionary_)[kEnableVncServer].asBool(); |
| 650 | } |
| 651 | |
| 652 | void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) { |
| 653 | (*dictionary_)[kEnableVncServer] = enable_vnc_server; |
| 654 | } |
| 655 | |
| 656 | std::string CuttlefishConfig::vnc_server_binary() const { |
| 657 | return (*dictionary_)[kVncServerBinary].asString(); |
| 658 | } |
| 659 | |
| 660 | void CuttlefishConfig::set_vnc_server_binary( |
| 661 | const std::string& vnc_server_binary) { |
| 662 | (*dictionary_)[kVncServerBinary] = vnc_server_binary; |
| 663 | } |
| 664 | |
| 665 | int CuttlefishConfig::vnc_server_port() const { |
| 666 | return (*dictionary_)[kVncServerPort].asInt(); |
| 667 | } |
| 668 | |
| 669 | void CuttlefishConfig::set_vnc_server_port(int vnc_server_port) { |
| 670 | (*dictionary_)[kVncServerPort] = vnc_server_port; |
| 671 | } |
| 672 | |
| 673 | bool CuttlefishConfig::enable_stream_audio() const { |
| 674 | return (*dictionary_)[kEnableStreamAudio].asBool(); |
| 675 | } |
| 676 | |
| 677 | void CuttlefishConfig::set_enable_stream_audio(bool enable_stream_audio) { |
| 678 | (*dictionary_)[kEnableStreamAudio] = enable_stream_audio; |
| 679 | } |
| 680 | |
| 681 | std::string CuttlefishConfig::stream_audio_binary() const { |
| 682 | return (*dictionary_)[kStreamAudioBinary].asString(); |
| 683 | } |
| 684 | |
| 685 | void CuttlefishConfig::set_stream_audio_binary( |
| 686 | const std::string& stream_audio_binary) { |
| 687 | (*dictionary_)[kStreamAudioBinary] = stream_audio_binary; |
| 688 | } |
| 689 | |
| 690 | int CuttlefishConfig::stream_audio_port() const { |
| 691 | return (*dictionary_)[kStreamAudioPort].asInt(); |
| 692 | } |
| 693 | |
| 694 | void CuttlefishConfig::set_stream_audio_port(int stream_audio_port) { |
| 695 | (*dictionary_)[kStreamAudioPort] = stream_audio_port; |
| 696 | } |
| 697 | |
Cody Schuffelen | d12e0c9 | 2019-01-29 15:44:45 -0800 | [diff] [blame] | 698 | bool CuttlefishConfig::restart_subprocesses() const { |
| 699 | return (*dictionary_)[kRestartSubprocesses].asBool(); |
| 700 | } |
| 701 | |
| 702 | void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) { |
| 703 | (*dictionary_)[kRestartSubprocesses] = restart_subprocesses; |
| 704 | } |
| 705 | |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 706 | bool CuttlefishConfig::run_adb_connector() const { |
Jorge E. Moreira | b86922b | 2019-02-04 10:52:58 -0800 | [diff] [blame] | 707 | return (*dictionary_)[kRunAdbConnector].asBool(); |
Cody Schuffelen | 1f08d97 | 2019-01-29 16:21:12 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) { |
| 711 | (*dictionary_)[kRunAdbConnector] = run_adb_connector; |
| 712 | } |
| 713 | |
| 714 | std::string CuttlefishConfig::adb_connector_binary() const { |
| 715 | return (*dictionary_)[kAdbConnectorBinary].asString(); |
| 716 | } |
| 717 | |
| 718 | void CuttlefishConfig::set_adb_connector_binary( |
| 719 | const std::string& adb_connector_binary) { |
| 720 | (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary; |
| 721 | } |
| 722 | |
| 723 | std::string CuttlefishConfig::virtual_usb_manager_binary() const { |
| 724 | return (*dictionary_)[kVirtualUsbManagerBinary].asString(); |
| 725 | } |
| 726 | |
| 727 | void CuttlefishConfig::set_virtual_usb_manager_binary( |
| 728 | const std::string& virtual_usb_manager_binary) { |
| 729 | (*dictionary_)[kVirtualUsbManagerBinary] = virtual_usb_manager_binary; |
| 730 | } |
| 731 | |
| 732 | std::string CuttlefishConfig::socket_forward_proxy_binary() const { |
| 733 | return (*dictionary_)[kSocketForwardProxyBinary].asString(); |
| 734 | } |
| 735 | |
| 736 | void CuttlefishConfig::set_socket_forward_proxy_binary( |
| 737 | const std::string& socket_forward_proxy_binary) { |
| 738 | (*dictionary_)[kSocketForwardProxyBinary] = socket_forward_proxy_binary; |
| 739 | } |
| 740 | |
| 741 | std::string CuttlefishConfig::socket_vsock_proxy_binary() const { |
| 742 | return (*dictionary_)[kSocketVsockProxyBinary].asString(); |
| 743 | } |
| 744 | |
| 745 | void CuttlefishConfig::set_socket_vsock_proxy_binary( |
| 746 | const std::string& socket_vsock_proxy_binary) { |
| 747 | (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary; |
| 748 | } |
| 749 | |
Cody Schuffelen | 5024973 | 2019-01-29 16:42:55 -0800 | [diff] [blame] | 750 | bool CuttlefishConfig::run_as_daemon() const { |
| 751 | return (*dictionary_)[kRunAsDaemon].asBool(); |
| 752 | } |
| 753 | |
| 754 | void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) { |
| 755 | (*dictionary_)[kRunAsDaemon] = run_as_daemon; |
| 756 | } |
| 757 | |
Cody Schuffelen | 3a64095 | 2019-01-29 17:14:41 -0800 | [diff] [blame] | 758 | bool CuttlefishConfig::run_e2e_test() const { |
| 759 | return (*dictionary_)[kRunE2eTest].asBool(); |
| 760 | } |
| 761 | |
| 762 | void CuttlefishConfig::set_run_e2e_test(bool run_e2e_test) { |
| 763 | (*dictionary_)[kRunE2eTest] = run_e2e_test; |
| 764 | } |
| 765 | |
| 766 | std::string CuttlefishConfig::e2e_test_binary() const { |
| 767 | return (*dictionary_)[kE2eTestBinary].asString(); |
| 768 | } |
| 769 | |
| 770 | void CuttlefishConfig::set_e2e_test_binary(const std::string& e2e_test_binary) { |
| 771 | (*dictionary_)[kE2eTestBinary] = e2e_test_binary; |
| 772 | } |
| 773 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 774 | std::string CuttlefishConfig::data_policy() const { |
| 775 | return (*dictionary_)[kDataPolicy].asString(); |
| 776 | } |
| 777 | |
| 778 | void CuttlefishConfig::set_data_policy(const std::string& data_policy) { |
| 779 | (*dictionary_)[kDataPolicy] = data_policy; |
| 780 | } |
| 781 | |
| 782 | int CuttlefishConfig::blank_data_image_mb() const { |
Cody Schuffelen | e3269ed | 2019-02-11 18:38:42 -0800 | [diff] [blame] | 783 | return (*dictionary_)[kBlankDataImageMb].asInt(); |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) { |
| 787 | (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb; |
| 788 | } |
| 789 | |
| 790 | std::string CuttlefishConfig::blank_data_image_fmt() const { |
| 791 | return (*dictionary_)[kBlankDataImageFmt].asString(); |
| 792 | } |
| 793 | |
| 794 | void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) { |
| 795 | (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt; |
| 796 | } |
| 797 | |
Jorge E. Moreira | f4b202b | 2019-02-05 17:51:06 -0800 | [diff] [blame] | 798 | |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 799 | void CuttlefishConfig::set_logcat_mode(const std::string& mode) { |
| 800 | (*dictionary_)[kLogcatMode] = mode; |
| 801 | } |
| 802 | |
| 803 | std::string CuttlefishConfig::logcat_mode() const { |
| 804 | return (*dictionary_)[kLogcatMode].asString(); |
| 805 | } |
| 806 | |
| 807 | void CuttlefishConfig::set_logcat_vsock_port(int port) { |
| 808 | (*dictionary_)[kLogcatVsockPort] = port; |
| 809 | } |
| 810 | |
| 811 | int CuttlefishConfig::logcat_vsock_port() const { |
| 812 | return (*dictionary_)[kLogcatVsockPort].asInt(); |
| 813 | } |
| 814 | |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 815 | void CuttlefishConfig::set_frames_vsock_port(int port) { |
| 816 | (*dictionary_)[kFramesVsockPort] = port; |
| 817 | } |
| 818 | |
| 819 | int CuttlefishConfig::frames_vsock_port() const { |
| 820 | return (*dictionary_)[kFramesVsockPort].asInt(); |
| 821 | } |
| 822 | |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 823 | void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) { |
| 824 | SetPath(kLogcatReceiverBinary, binary); |
| 825 | } |
| 826 | |
| 827 | std::string CuttlefishConfig::logcat_receiver_binary() const { |
| 828 | return (*dictionary_)[kLogcatReceiverBinary].asString(); |
| 829 | } |
| 830 | |
Jorge E. Moreira | f4b202b | 2019-02-05 17:51:06 -0800 | [diff] [blame] | 831 | bool CuttlefishConfig::enable_ivserver() const { |
Jorge E. Moreira | fd1a6b0 | 2019-02-27 16:25:00 -0800 | [diff] [blame] | 832 | return hardware_name() == "cutf_ivsh"; |
Jorge E. Moreira | f4b202b | 2019-02-05 17:51:06 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 835 | std::string CuttlefishConfig::touch_socket_path() const { |
| 836 | return PerInstancePath("touch.sock"); |
| 837 | } |
| 838 | |
| 839 | std::string CuttlefishConfig::keyboard_socket_path() const { |
| 840 | return PerInstancePath("keyboard.sock"); |
| 841 | } |
| 842 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 843 | // Creates the (initially empty) config object and populates it with values from |
Cody Schuffelen | f219a16 | 2018-10-11 19:01:57 -0700 | [diff] [blame] | 844 | // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present. |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 845 | // Returns nullptr if there was an error loading from file |
| 846 | /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() { |
Jorge E. Moreira | a00584e | 2018-10-25 15:07:52 -0700 | [diff] [blame] | 847 | auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName, |
| 848 | vsoc::GetGlobalConfigFileLink()); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 849 | auto ret = new CuttlefishConfig(); |
Jorge E. Moreira | a00584e | 2018-10-25 15:07:52 -0700 | [diff] [blame] | 850 | if (ret) { |
| 851 | auto loaded = ret->LoadFromFile(config_file_path.c_str()); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 852 | if (!loaded) { |
Cody Schuffelen | f219a16 | 2018-10-11 19:01:57 -0700 | [diff] [blame] | 853 | delete ret; |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 854 | return nullptr; |
| 855 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 856 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 857 | return ret; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 860 | /*static*/ CuttlefishConfig* CuttlefishConfig::Get() { |
| 861 | static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl()); |
| 862 | return config.get(); |
| 863 | } |
| 864 | |
| 865 | CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {} |
| 866 | // Can't use '= default' on the header because the compiler complains of |
| 867 | // Json::Value being an incomplete type |
| 868 | CuttlefishConfig::~CuttlefishConfig() {} |
| 869 | |
| 870 | bool CuttlefishConfig::LoadFromFile(const char* file) { |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 871 | auto real_file_path = cvd::AbsolutePath(file); |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 872 | if (real_file_path.empty()) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 873 | LOG(ERROR) << "Could not get real path for file " << file; |
| 874 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 875 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 876 | Json::Reader reader; |
| 877 | std::ifstream ifs(real_file_path); |
| 878 | if (!reader.parse(ifs, *dictionary_)) { |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 879 | LOG(ERROR) << "Could not read config file " << file << ": " |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 880 | << reader.getFormattedErrorMessages(); |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 881 | return false; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 882 | } |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 883 | return true; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 884 | } |
| 885 | bool CuttlefishConfig::SaveToFile(const std::string& file) const { |
| 886 | std::ofstream ofs(file); |
| 887 | if (!ofs.is_open()) { |
| 888 | LOG(ERROR) << "Unable to write to file " << file; |
| 889 | return false; |
| 890 | } |
| 891 | ofs << *dictionary_; |
| 892 | return !ofs.fail(); |
| 893 | } |
| 894 | |
| 895 | std::string CuttlefishConfig::PerInstancePath(const char* file_name) const { |
| 896 | return (instance_dir() + "/") + file_name; |
| 897 | } |
| 898 | |
| 899 | std::string CuttlefishConfig::instance_name() const { |
| 900 | return GetPerInstanceDefault("cvd-"); |
| 901 | } |
| 902 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 903 | int GetInstance() { |
| 904 | static int instance_id = InstanceFromEnvironment(); |
| 905 | return instance_id; |
| 906 | } |
| 907 | |
Jorge E. Moreira | 015c000 | 2018-07-19 11:01:16 -0700 | [diff] [blame] | 908 | std::string GetGlobalConfigFileLink() { |
| 909 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json"; |
| 910 | } |
| 911 | |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 912 | std::string GetDomain() { |
| 913 | return CuttlefishConfig::Get()->ivshmem_client_socket_path(); |
| 914 | } |
| 915 | |
| 916 | std::string GetPerInstanceDefault(const char* prefix) { |
| 917 | std::ostringstream stream; |
| 918 | stream << prefix << std::setfill('0') << std::setw(2) << GetInstance(); |
| 919 | return stream.str(); |
| 920 | } |
| 921 | int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; } |
| 922 | |
| 923 | std::string GetDefaultPerInstanceDir() { |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 924 | std::ostringstream stream; |
Jorge E. Moreira | d966837 | 2019-01-28 22:40:42 -0800 | [diff] [blame] | 925 | stream << std::getenv("HOME") << "/cuttlefish_runtime"; |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 926 | return stream.str(); |
| 927 | } |
| 928 | |
Jorge E. Moreira | 077d300 | 2018-07-20 11:43:05 -0700 | [diff] [blame] | 929 | std::string GetDefaultMempath() { |
| 930 | return GetPerInstanceDefault("/var/run/shm/cvd-"); |
| 931 | } |
| 932 | |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 933 | int GetDefaultPerInstanceVsockCid() { |
| 934 | constexpr int kFirstGuestCid = 3; |
| 935 | return vsoc::HostSupportsVsock() ? GetPerInstanceDefault(kFirstGuestCid) : 0; |
| 936 | } |
| 937 | |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 938 | std::string DefaultHostArtifactsPath(const std::string& file_name) { |
| 939 | return (cvd::StringFromEnv("ANDROID_HOST_OUT", |
| 940 | cvd::StringFromEnv("HOME", ".")) + |
| 941 | "/") + |
| 942 | file_name; |
| 943 | } |
| 944 | |
| 945 | std::string DefaultGuestImagePath(const std::string& file_name) { |
| 946 | return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT", |
Ryan Haining | 6d821ec | 2018-07-27 11:39:09 -0700 | [diff] [blame] | 947 | cvd::StringFromEnv("HOME", ".")) + |
Jorge E. Moreira | a8142f9 | 2018-06-13 17:33:55 -0700 | [diff] [blame] | 948 | "/") + |
| 949 | file_name; |
| 950 | } |
| 951 | |
| 952 | bool HostSupportsQemuCli() { |
| 953 | static bool supported = |
| 954 | std::system( |
| 955 | "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0; |
| 956 | return supported; |
| 957 | } |
Cody Schuffelen | d946b5f | 2018-12-12 11:54:48 -0800 | [diff] [blame] | 958 | |
| 959 | bool HostSupportsVsock() { |
| 960 | static bool supported = |
| 961 | std::system( |
| 962 | "/usr/lib/cuttlefish-common/bin/capability_query.py vsock") == 0; |
| 963 | return supported; |
| 964 | } |
Jorge E. Moreira | 577383b | 2018-05-24 14:17:51 -0700 | [diff] [blame] | 965 | } // namespace vsoc |