• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <algorithm>
20 #include <climits>
21 #include <cstdlib>
22 #include <cstring>
23 #include <fstream>
24 #include <iomanip>
25 #include <iterator>
26 #include <sstream>
27 #include <string>
28 #include <time.h>
29 
30 #include <android-base/strings.h>
31 #include <glog/logging.h>
32 #include <json/json.h>
33 
34 #include "common/libs/utils/environment.h"
35 #include "common/libs/utils/files.h"
36 #include "host/libs/vm_manager/qemu_manager.h"
37 
38 
39 namespace {
40 
InstanceFromEnvironment()41 int InstanceFromEnvironment() {
42   static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE";
43   static constexpr int kDefaultInstance = 1;
44 
45   // CUTTLEFISH_INSTANCE environment variable
46   const char* instance_str = std::getenv(kInstanceEnvironmentVariable);
47   if (!instance_str) {
48     // Try to get it from the user instead
49     instance_str = std::getenv("USER");
50 
51     if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix,
52                                       sizeof(vsoc::kVsocUserPrefix) - 1)) {
53       // No user or we don't recognize this user
54       LOG(WARNING) << "No user or non-vsoc user, returning default config";
55       return kDefaultInstance;
56     }
57     instance_str += sizeof(vsoc::kVsocUserPrefix) - 1;
58 
59     // Set the environment variable so that child processes see it
60     setenv(kInstanceEnvironmentVariable, instance_str, 0);
61   }
62 
63   int instance = std::atoi(instance_str);
64   if (instance <= 0) {
65     instance = kDefaultInstance;
66   }
67 
68   return instance;
69 }
70 
71 const char* kInstances = "instances";
72 const char* kAssemblyDir = "assembly_dir";
73 const char* kSerialNumber = "serial_number";
74 const char* kInstanceDir = "instance_dir";
75 const char* kVmManager = "vm_manager";
76 const char* const kGpuMode = "gpu_mode";
77 const char* const kWaylandSocket = "wayland_socket";
78 const char* const kXDisplay = "x_display";
79 const char* kDeviceTitle = "device_title";
80 
81 const char* kCpus = "cpus";
82 const char* kMemoryMb = "memory_mb";
83 const char* kDpi = "dpi";
84 const char* kXRes = "x_res";
85 const char* kYRes = "y_res";
86 const char* kRefreshRateHz = "refresh_rate_hz";
87 
88 const char* kKernelImagePath = "kernel_image_path";
89 const char* kUseUnpackedKernel = "use_unpacked_kernel";
90 const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path";
91 const char* kDecompressKernel = "decompress_kernel";
92 const char* kGdbFlag = "gdb_flag";
93 const char* kRamdiskImagePath = "ramdisk_image_path";
94 const char* kInitramfsPath = "initramfs_path";
95 const char* kFinalRamdiskPath = "final_ramdisk_path";
96 const char* kVendorRamdiskImagePath = "vendor_ramdisk_image_path";
97 
98 const char* kVirtualDiskPaths = "virtual_disk_paths";
99 const char* kDeprecatedBootCompleted = "deprecated_boot_completed";
100 
101 const char* kMobileBridgeName = "mobile_bridge_name";
102 const char* kMobileTapName = "mobile_tap_name";
103 const char* kWifiTapName = "wifi_tap_name";
104 const char* kVsockGuestCid = "vsock_guest_cid";
105 
106 const char* kUuid = "uuid";
107 const char* kCuttlefishEnvPath = "cuttlefish_env_path";
108 
109 const char* kAdbMode = "adb_mode";
110 const char* kHostPort = "host_port";
111 const char* kAdbIPAndPort = "adb_ip_and_port";
112 const char* kSetupWizardMode = "setupwizard_mode";
113 
114 const char* kQemuBinary = "qemu_binary";
115 const char* kCrosvmBinary = "crosvm_binary";
116 const char* kConsoleForwarderBinary = "console_forwarder_binary";
117 const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary";
118 
119 const char* kEnableVncServer = "enable_vnc_server";
120 const char* kVncServerBinary = "vnc_server_binary";
121 const char* kVncServerPort = "vnc_server_port";
122 
123 const char* kEnableWebRTC = "enable_webrtc";
124 const char* kWebRTCBinary = "webrtc_binary";
125 const char* kWebRTCAssetsDir = "webrtc_assets_dir";
126 const char* kWebRTCPublicIP = "webrtc_public_ip";
127 const char* kWebRTCEnableADBWebSocket = "webrtc_enable_adb_websocket";
128 
129 const char* kEnableVehicleHalServer = "enable_vehicle_hal_server";
130 const char* kVehicleHalServerBinary = "vehicle_hal_server_binary";
131 const char* kVehicleHalServerPort = "vehicle_hal_server_port";
132 
133 const char* kAudioControlServerPort = "audiocontrol_server_port";
134 
135 const char* kRestartSubprocesses = "restart_subprocesses";
136 const char* kRunAdbConnector = "run_adb_connector";
137 const char* kAdbConnectorBinary = "adb_connector_binary";
138 const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary";
139 
140 const char* kRunAsDaemon = "run_as_daemon";
141 
142 const char* kDataPolicy = "data_policy";
143 const char* kBlankDataImageMb = "blank_data_image_mb";
144 const char* kBlankDataImageFmt = "blank_data_image_fmt";
145 
146 const char* kLogcatMode = "logcat_mode";
147 const char* kLogcatReceiverBinary = "logcat_receiver_binary";
148 const char* kConfigServerBinary = "config_server_binary";
149 
150 const char* kRunTombstoneReceiver = "enable_tombstone_logger";
151 const char* kTombstoneReceiverBinary = "tombstone_receiver_binary";
152 
153 const char* kWebRTCCertsDir = "webrtc_certs_dir";
154 
155 const char* kBootloader = "bootloader";
156 const char* kUseBootloader = "use_bootloader";
157 
158 const char* kBootSlot = "boot_slot";
159 
160 const char* kLoopMaxPart = "loop_max_part";
161 const char* kGuestEnforceSecurity = "guest_enforce_security";
162 const char* kGuestAuditSecurity = "guest_audit_security";
163 const char* kGuestForceNormalBoot = "guest_force_normal_boot";
164 const char* kBootImageKernelCmdline = "boot_image_kernel_cmdline";
165 const char* kExtraKernelCmdline = "extra_kernel_cmdline";
166 
167 const char* kWifiMacAddress = "wifi_mac_address";
168 }  // namespace
169 
170 namespace vsoc {
171 
172 const char* const kGpuModeGuestSwiftshader = "guest_swiftshader";
173 const char* const kGpuModeDrmVirgl = "drm_virgl";
174 const char* const kGpuModeGfxStream = "gfxstream";
175 
DefaultEnvironmentPath(const char * environment_key,const char * default_value,const char * subpath)176 std::string DefaultEnvironmentPath(const char* environment_key,
177                                    const char* default_value,
178                                    const char* subpath) {
179   return cvd::StringFromEnv(environment_key, default_value) + "/" + subpath;
180 }
181 
Dictionary()182 Json::Value* CuttlefishConfig::MutableInstanceSpecific::Dictionary() {
183   return &(*config_->dictionary_)[kInstances][id_];
184 }
185 
Dictionary() const186 const Json::Value* CuttlefishConfig::InstanceSpecific::Dictionary() const {
187   return &(*config_->dictionary_)[kInstances][id_];
188 }
189 
assembly_dir() const190 std::string CuttlefishConfig::assembly_dir() const {
191   return (*dictionary_)[kAssemblyDir].asString();
192 }
set_assembly_dir(const std::string & assembly_dir)193 void CuttlefishConfig::set_assembly_dir(const std::string& assembly_dir) {
194   (*dictionary_)[kAssemblyDir] = assembly_dir;
195 }
196 
instance_dir() const197 std::string CuttlefishConfig::InstanceSpecific::instance_dir() const {
198   return (*Dictionary())[kInstanceDir].asString();
199 }
set_instance_dir(const std::string & instance_dir)200 void CuttlefishConfig::MutableInstanceSpecific::set_instance_dir(
201     const std::string& instance_dir) {
202   (*Dictionary())[kInstanceDir] = instance_dir;
203 }
204 
instance_internal_dir() const205 std::string CuttlefishConfig::InstanceSpecific::instance_internal_dir() const {
206   return PerInstancePath(kInternalDirName);
207 }
208 
vm_manager() const209 std::string CuttlefishConfig::vm_manager() const {
210   return (*dictionary_)[kVmManager].asString();
211 }
set_vm_manager(const std::string & name)212 void CuttlefishConfig::set_vm_manager(const std::string& name) {
213   (*dictionary_)[kVmManager] = name;
214 }
215 
gpu_mode() const216 std::string CuttlefishConfig::gpu_mode() const {
217   return (*dictionary_)[kGpuMode].asString();
218 }
set_gpu_mode(const std::string & name)219 void CuttlefishConfig::set_gpu_mode(const std::string& name) {
220   (*dictionary_)[kGpuMode] = name;
221 }
222 
wayland_socket() const223 std::string CuttlefishConfig::wayland_socket() const {
224   // Don't use SetPath here: the path is already fully formed.
225   return (*dictionary_)[kWaylandSocket].asString();
226 }
set_wayland_socket(const std::string & path)227 void CuttlefishConfig::set_wayland_socket(const std::string& path) {
228   (*dictionary_)[kWaylandSocket] = path;
229 }
230 
x_display() const231 std::string CuttlefishConfig::x_display() const {
232   return (*dictionary_)[kXDisplay].asString();
233 }
set_x_display(const std::string & address)234 void CuttlefishConfig::set_x_display(const std::string& address) {
235   (*dictionary_)[kXDisplay] = address;
236 }
237 
serial_number() const238 std::string CuttlefishConfig::InstanceSpecific::serial_number() const {
239   return (*Dictionary())[kSerialNumber].asString();
240 }
set_serial_number(const std::string & serial_number)241 void CuttlefishConfig::MutableInstanceSpecific::set_serial_number(
242     const std::string& serial_number) {
243   (*Dictionary())[kSerialNumber] = serial_number;
244 }
245 
cpus() const246 int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); }
set_cpus(int cpus)247 void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; }
248 
memory_mb() const249 int CuttlefishConfig::memory_mb() const {
250   return (*dictionary_)[kMemoryMb].asInt();
251 }
set_memory_mb(int memory_mb)252 void CuttlefishConfig::set_memory_mb(int memory_mb) {
253   (*dictionary_)[kMemoryMb] = memory_mb;
254 }
255 
dpi() const256 int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); }
set_dpi(int dpi)257 void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; }
258 
x_res() const259 int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); }
set_x_res(int x_res)260 void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; }
261 
y_res() const262 int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
set_y_res(int y_res)263 void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
264 
refresh_rate_hz() const265 int CuttlefishConfig::refresh_rate_hz() const {
266   return (*dictionary_)[kRefreshRateHz].asInt();
267 }
set_refresh_rate_hz(int refresh_rate_hz)268 void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) {
269   (*dictionary_)[kRefreshRateHz] = refresh_rate_hz;
270 }
271 
kernel_image_path() const272 std::string CuttlefishConfig::kernel_image_path() const {
273   return (*dictionary_)[kKernelImagePath].asString();
274 }
275 
SetPath(const std::string & key,const std::string & path)276 void CuttlefishConfig::SetPath(const std::string& key,
277                                const std::string& path) {
278   if (!path.empty()) {
279     (*dictionary_)[key] = cvd::AbsolutePath(path);
280   }
281 }
282 
set_kernel_image_path(const std::string & kernel_image_path)283 void CuttlefishConfig::set_kernel_image_path(
284     const std::string& kernel_image_path) {
285   SetPath(kKernelImagePath, kernel_image_path);
286 }
287 
use_unpacked_kernel() const288 bool CuttlefishConfig::use_unpacked_kernel() const {
289   return (*dictionary_)[kUseUnpackedKernel].asBool();
290 }
291 
set_use_unpacked_kernel(bool use_unpacked_kernel)292 void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) {
293   (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel;
294 }
295 
decompress_kernel() const296 bool CuttlefishConfig::decompress_kernel() const {
297   return (*dictionary_)[kDecompressKernel].asBool();
298 }
set_decompress_kernel(bool decompress_kernel)299 void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) {
300   (*dictionary_)[kDecompressKernel] = decompress_kernel;
301 }
302 
decompressed_kernel_image_path() const303 std::string CuttlefishConfig::decompressed_kernel_image_path() const {
304   return (*dictionary_)[kDecompressedKernelImagePath].asString();
305 }
set_decompressed_kernel_image_path(const std::string & path)306 void CuttlefishConfig::set_decompressed_kernel_image_path(
307     const std::string& path) {
308   SetPath(kDecompressedKernelImagePath, path);
309 }
310 
gdb_flag() const311 std::string CuttlefishConfig::gdb_flag() const {
312   return (*dictionary_)[kGdbFlag].asString();
313 }
314 
set_gdb_flag(const std::string & device)315 void CuttlefishConfig::set_gdb_flag(const std::string& device) {
316   (*dictionary_)[kGdbFlag] = device;
317 }
318 
ramdisk_image_path() const319 std::string CuttlefishConfig::ramdisk_image_path() const {
320   return (*dictionary_)[kRamdiskImagePath].asString();
321 }
set_ramdisk_image_path(const std::string & ramdisk_image_path)322 void CuttlefishConfig::set_ramdisk_image_path(
323     const std::string& ramdisk_image_path) {
324   SetPath(kRamdiskImagePath, ramdisk_image_path);
325 }
326 
initramfs_path() const327 std::string CuttlefishConfig::initramfs_path() const {
328   return (*dictionary_)[kInitramfsPath].asString();
329 }
set_initramfs_path(const std::string & initramfs_path)330 void CuttlefishConfig::set_initramfs_path(const std::string& initramfs_path) {
331   SetPath(kInitramfsPath, initramfs_path);
332 }
333 
final_ramdisk_path() const334 std::string CuttlefishConfig::final_ramdisk_path() const {
335   return (*dictionary_)[kFinalRamdiskPath].asString();
336 }
set_final_ramdisk_path(const std::string & final_ramdisk_path)337 void CuttlefishConfig::set_final_ramdisk_path(
338     const std::string& final_ramdisk_path) {
339   SetPath(kFinalRamdiskPath, final_ramdisk_path);
340 }
341 
vendor_ramdisk_image_path() const342 std::string CuttlefishConfig::vendor_ramdisk_image_path() const {
343   return (*dictionary_)[kVendorRamdiskImagePath].asString();
344 }
set_vendor_ramdisk_image_path(const std::string & vendor_ramdisk_image_path)345 void CuttlefishConfig::set_vendor_ramdisk_image_path(
346     const std::string& vendor_ramdisk_image_path) {
347   SetPath(kVendorRamdiskImagePath, vendor_ramdisk_image_path);
348 }
349 
virtual_disk_paths() const350 std::vector<std::string> CuttlefishConfig::InstanceSpecific::virtual_disk_paths() const {
351   std::vector<std::string> virtual_disks;
352   auto virtual_disks_json_obj = (*Dictionary())[kVirtualDiskPaths];
353   for (const auto& disk : virtual_disks_json_obj) {
354     virtual_disks.push_back(disk.asString());
355   }
356   return virtual_disks;
357 }
set_virtual_disk_paths(const std::vector<std::string> & virtual_disk_paths)358 void CuttlefishConfig::MutableInstanceSpecific::set_virtual_disk_paths(
359     const std::vector<std::string>& virtual_disk_paths) {
360   Json::Value virtual_disks_json_obj(Json::arrayValue);
361   for (const auto& arg : virtual_disk_paths) {
362     virtual_disks_json_obj.append(arg);
363   }
364   (*Dictionary())[kVirtualDiskPaths] = virtual_disks_json_obj;
365 }
366 
kernel_log_pipe_name() const367 std::string CuttlefishConfig::InstanceSpecific::kernel_log_pipe_name() const {
368   return cvd::AbsolutePath(PerInstanceInternalPath("kernel-log-pipe"));
369 }
370 
console_pipe_name() const371 std::string CuttlefishConfig::InstanceSpecific::console_pipe_name() const {
372   return cvd::AbsolutePath(PerInstanceInternalPath("console-pipe"));
373 }
374 
deprecated_boot_completed() const375 bool CuttlefishConfig::deprecated_boot_completed() const {
376   return (*dictionary_)[kDeprecatedBootCompleted].asBool();
377 }
set_deprecated_boot_completed(bool deprecated_boot_completed)378 void CuttlefishConfig::set_deprecated_boot_completed(
379     bool deprecated_boot_completed) {
380   (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed;
381 }
382 
access_kregistry_path() const383 std::string CuttlefishConfig::InstanceSpecific::access_kregistry_path() const {
384   return cvd::AbsolutePath(PerInstancePath("access-kregistry"));
385 }
386 
console_path() const387 std::string CuttlefishConfig::InstanceSpecific::console_path() const {
388   return cvd::AbsolutePath(PerInstancePath("console"));
389 }
390 
logcat_path() const391 std::string CuttlefishConfig::InstanceSpecific::logcat_path() const {
392   return cvd::AbsolutePath(PerInstancePath("logcat"));
393 }
394 
launcher_monitor_socket_path() const395 std::string CuttlefishConfig::InstanceSpecific::launcher_monitor_socket_path()
396     const {
397   return cvd::AbsolutePath(PerInstancePath("launcher_monitor.sock"));
398 }
399 
launcher_log_path() const400 std::string CuttlefishConfig::InstanceSpecific::launcher_log_path() const {
401   return cvd::AbsolutePath(PerInstancePath("launcher.log"));
402 }
403 
sdcard_path() const404 std::string CuttlefishConfig::InstanceSpecific::sdcard_path() const {
405   return cvd::AbsolutePath(PerInstancePath("sdcard.img"));
406 }
407 
mobile_bridge_name() const408 std::string CuttlefishConfig::InstanceSpecific::mobile_bridge_name() const {
409   return (*Dictionary())[kMobileBridgeName].asString();
410 }
set_mobile_bridge_name(const std::string & mobile_bridge_name)411 void CuttlefishConfig::MutableInstanceSpecific::set_mobile_bridge_name(
412     const std::string& mobile_bridge_name) {
413   (*Dictionary())[kMobileBridgeName] = mobile_bridge_name;
414 }
415 
mobile_tap_name() const416 std::string CuttlefishConfig::InstanceSpecific::mobile_tap_name() const {
417   return (*Dictionary())[kMobileTapName].asString();
418 }
set_mobile_tap_name(const std::string & mobile_tap_name)419 void CuttlefishConfig::MutableInstanceSpecific::set_mobile_tap_name(
420     const std::string& mobile_tap_name) {
421   (*Dictionary())[kMobileTapName] = mobile_tap_name;
422 }
423 
wifi_tap_name() const424 std::string CuttlefishConfig::InstanceSpecific::wifi_tap_name() const {
425   return (*Dictionary())[kWifiTapName].asString();
426 }
set_wifi_tap_name(const std::string & wifi_tap_name)427 void CuttlefishConfig::MutableInstanceSpecific::set_wifi_tap_name(
428     const std::string& wifi_tap_name) {
429   (*Dictionary())[kWifiTapName] = wifi_tap_name;
430 }
431 
vsock_guest_cid() const432 int CuttlefishConfig::InstanceSpecific::vsock_guest_cid() const {
433   return (*Dictionary())[kVsockGuestCid].asInt();
434 }
435 
set_vsock_guest_cid(int vsock_guest_cid)436 void CuttlefishConfig::MutableInstanceSpecific::set_vsock_guest_cid(
437     int vsock_guest_cid) {
438   (*Dictionary())[kVsockGuestCid] = vsock_guest_cid;
439 }
440 
uuid() const441 std::string CuttlefishConfig::InstanceSpecific::uuid() const {
442   return (*Dictionary())[kUuid].asString();
443 }
set_uuid(const std::string & uuid)444 void CuttlefishConfig::MutableInstanceSpecific::set_uuid(const std::string& uuid) {
445   (*Dictionary())[kUuid] = uuid;
446 }
447 
set_cuttlefish_env_path(const std::string & path)448 void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) {
449   SetPath(kCuttlefishEnvPath, path);
450 }
cuttlefish_env_path() const451 std::string CuttlefishConfig::cuttlefish_env_path() const {
452   return (*dictionary_)[kCuttlefishEnvPath].asString();
453 }
454 
stringToAdbMode(std::string mode)455 static AdbMode stringToAdbMode(std::string mode) {
456   std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
457   if (mode == "vsock_tunnel") {
458     return AdbMode::VsockTunnel;
459   } else if (mode == "vsock_half_tunnel") {
460     return AdbMode::VsockHalfTunnel;
461   } else if (mode == "native_vsock") {
462     return AdbMode::NativeVsock;
463   } else {
464     return AdbMode::Unknown;
465   }
466 }
467 
adb_mode() const468 std::set<AdbMode> CuttlefishConfig::adb_mode() const {
469   std::set<AdbMode> args_set;
470   for (auto& mode : (*dictionary_)[kAdbMode]) {
471     args_set.insert(stringToAdbMode(mode.asString()));
472   }
473   return args_set;
474 }
475 
set_adb_mode(const std::set<std::string> & mode)476 void CuttlefishConfig::set_adb_mode(const std::set<std::string>& mode) {
477   Json::Value mode_json_obj(Json::arrayValue);
478   for (const auto& arg : mode) {
479     mode_json_obj.append(arg);
480   }
481   (*dictionary_)[kAdbMode] = mode_json_obj;
482 }
483 
host_port() const484 int CuttlefishConfig::InstanceSpecific::host_port() const {
485   return (*Dictionary())[kHostPort].asInt();
486 }
487 
set_host_port(int host_port)488 void CuttlefishConfig::MutableInstanceSpecific::set_host_port(int host_port) {
489   (*Dictionary())[kHostPort] = host_port;
490 }
491 
adb_ip_and_port() const492 std::string CuttlefishConfig::InstanceSpecific::adb_ip_and_port() const {
493   return (*Dictionary())[kAdbIPAndPort].asString();
494 }
495 
set_adb_ip_and_port(const std::string & ip_port)496 void CuttlefishConfig::MutableInstanceSpecific::set_adb_ip_and_port(
497     const std::string& ip_port) {
498   (*Dictionary())[kAdbIPAndPort] = ip_port;
499 }
500 
adb_device_name() const501 std::string CuttlefishConfig::InstanceSpecific::adb_device_name() const {
502   if (adb_ip_and_port() != "") {
503     return adb_ip_and_port();
504   }
505   LOG(ERROR) << "no adb_mode found, returning bad device name";
506   return "NO_ADB_MODE_SET_NO_VALID_DEVICE_NAME";
507 }
508 
device_title() const509 std::string CuttlefishConfig::InstanceSpecific::device_title() const {
510   return (*Dictionary())[kDeviceTitle].asString();
511 }
512 
set_device_title(const std::string & title)513 void CuttlefishConfig::MutableInstanceSpecific::set_device_title(
514     const std::string& title) {
515   (*Dictionary())[kDeviceTitle] = title;
516 }
517 
setupwizard_mode() const518 std::string CuttlefishConfig::setupwizard_mode() const {
519   return (*dictionary_)[kSetupWizardMode].asString();
520 }
521 
set_setupwizard_mode(const std::string & mode)522 void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) {
523   (*dictionary_)[kSetupWizardMode] = mode;
524 }
525 
qemu_binary() const526 std::string CuttlefishConfig::qemu_binary() const {
527   return (*dictionary_)[kQemuBinary].asString();
528 }
529 
set_qemu_binary(const std::string & qemu_binary)530 void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) {
531   (*dictionary_)[kQemuBinary] = qemu_binary;
532 }
533 
crosvm_binary() const534 std::string CuttlefishConfig::crosvm_binary() const {
535   return (*dictionary_)[kCrosvmBinary].asString();
536 }
537 
set_crosvm_binary(const std::string & crosvm_binary)538 void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) {
539   (*dictionary_)[kCrosvmBinary] = crosvm_binary;
540 }
541 
console_forwarder_binary() const542 std::string CuttlefishConfig::console_forwarder_binary() const {
543   return (*dictionary_)[kConsoleForwarderBinary].asString();
544 }
545 
set_console_forwarder_binary(const std::string & binary)546 void CuttlefishConfig::set_console_forwarder_binary(
547     const std::string& binary) {
548   (*dictionary_)[kConsoleForwarderBinary] = binary;
549 }
550 
kernel_log_monitor_binary() const551 std::string CuttlefishConfig::kernel_log_monitor_binary() const {
552   return (*dictionary_)[kKernelLogMonitorBinary].asString();
553 }
554 
set_kernel_log_monitor_binary(const std::string & kernel_log_monitor_binary)555 void CuttlefishConfig::set_kernel_log_monitor_binary(
556     const std::string& kernel_log_monitor_binary) {
557   (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary;
558 }
559 
enable_vnc_server() const560 bool CuttlefishConfig::enable_vnc_server() const {
561   return (*dictionary_)[kEnableVncServer].asBool();
562 }
563 
set_enable_vnc_server(bool enable_vnc_server)564 void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) {
565   (*dictionary_)[kEnableVncServer] = enable_vnc_server;
566 }
567 
vnc_server_binary() const568 std::string CuttlefishConfig::vnc_server_binary() const {
569   return (*dictionary_)[kVncServerBinary].asString();
570 }
571 
set_vnc_server_binary(const std::string & vnc_server_binary)572 void CuttlefishConfig::set_vnc_server_binary(
573     const std::string& vnc_server_binary) {
574   (*dictionary_)[kVncServerBinary] = vnc_server_binary;
575 }
576 
vnc_server_port() const577 int CuttlefishConfig::InstanceSpecific::vnc_server_port() const {
578   return (*Dictionary())[kVncServerPort].asInt();
579 }
580 
set_vnc_server_port(int vnc_server_port)581 void CuttlefishConfig::MutableInstanceSpecific::set_vnc_server_port(int vnc_server_port) {
582   (*Dictionary())[kVncServerPort] = vnc_server_port;
583 }
584 
vehicle_hal_server_port() const585 int CuttlefishConfig::InstanceSpecific::vehicle_hal_server_port() const {
586   return (*Dictionary())[kVehicleHalServerPort].asInt();
587 }
588 
set_vehicle_hal_server_port(int vehicle_hal_server_port)589 void CuttlefishConfig::MutableInstanceSpecific::set_vehicle_hal_server_port(int vehicle_hal_server_port) {
590   (*Dictionary())[kVehicleHalServerPort] = vehicle_hal_server_port;
591 }
592 
audiocontrol_server_port() const593 int CuttlefishConfig::InstanceSpecific::audiocontrol_server_port() const {
594   return (*Dictionary())[kAudioControlServerPort].asInt();
595 }
596 
set_audiocontrol_server_port(int audiocontrol_server_port)597 void CuttlefishConfig::MutableInstanceSpecific::set_audiocontrol_server_port(int audiocontrol_server_port) {
598   (*Dictionary())[kAudioControlServerPort] = audiocontrol_server_port;
599 }
600 
set_enable_webrtc(bool enable_webrtc)601 void CuttlefishConfig::set_enable_webrtc(bool enable_webrtc) {
602   (*dictionary_)[kEnableWebRTC] = enable_webrtc;
603 }
604 
enable_webrtc() const605 bool CuttlefishConfig::enable_webrtc() const {
606   return (*dictionary_)[kEnableWebRTC].asBool();
607 }
608 
set_webrtc_binary(const std::string & webrtc_binary)609 void CuttlefishConfig::set_webrtc_binary(const std::string& webrtc_binary) {
610   (*dictionary_)[kWebRTCBinary] = webrtc_binary;
611 }
612 
webrtc_binary() const613 std::string CuttlefishConfig::webrtc_binary() const {
614   return (*dictionary_)[kWebRTCBinary].asString();
615 }
616 
set_enable_vehicle_hal_grpc_server(bool enable_vehicle_hal_grpc_server)617 void CuttlefishConfig::set_enable_vehicle_hal_grpc_server(bool enable_vehicle_hal_grpc_server) {
618   (*dictionary_)[kEnableVehicleHalServer] = enable_vehicle_hal_grpc_server;
619 }
620 
enable_vehicle_hal_grpc_server() const621 bool CuttlefishConfig::enable_vehicle_hal_grpc_server() const {
622   return (*dictionary_)[kEnableVehicleHalServer].asBool();
623 }
624 
set_vehicle_hal_grpc_server_binary(const std::string & vehicle_hal_server_binary)625 void CuttlefishConfig::set_vehicle_hal_grpc_server_binary(const std::string& vehicle_hal_server_binary) {
626   (*dictionary_)[kVehicleHalServerBinary] = vehicle_hal_server_binary;
627 }
628 
vehicle_hal_grpc_server_binary() const629 std::string CuttlefishConfig::vehicle_hal_grpc_server_binary() const {
630   return (*dictionary_)[kVehicleHalServerBinary].asString();
631 }
632 
set_webrtc_assets_dir(const std::string & webrtc_assets_dir)633 void CuttlefishConfig::set_webrtc_assets_dir(const std::string& webrtc_assets_dir) {
634   (*dictionary_)[kWebRTCAssetsDir] = webrtc_assets_dir;
635 }
636 
webrtc_assets_dir() const637 std::string CuttlefishConfig::webrtc_assets_dir() const {
638   return (*dictionary_)[kWebRTCAssetsDir].asString();
639 }
640 
set_webrtc_public_ip(const std::string & webrtc_public_ip)641 void CuttlefishConfig::set_webrtc_public_ip(
642         const std::string& webrtc_public_ip) {
643   (*dictionary_)[kWebRTCPublicIP] = webrtc_public_ip;
644 }
645 
webrtc_public_ip() const646 std::string CuttlefishConfig::webrtc_public_ip() const {
647   return (*dictionary_)[kWebRTCPublicIP].asString();
648 }
649 
set_webrtc_enable_adb_websocket(bool enable)650 void CuttlefishConfig::set_webrtc_enable_adb_websocket(bool enable) {
651     (*dictionary_)[kWebRTCEnableADBWebSocket] = enable;
652 }
653 
webrtc_enable_adb_websocket() const654 bool CuttlefishConfig::webrtc_enable_adb_websocket() const {
655     return (*dictionary_)[kWebRTCEnableADBWebSocket].asBool();
656 }
657 
restart_subprocesses() const658 bool CuttlefishConfig::restart_subprocesses() const {
659   return (*dictionary_)[kRestartSubprocesses].asBool();
660 }
661 
set_restart_subprocesses(bool restart_subprocesses)662 void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) {
663   (*dictionary_)[kRestartSubprocesses] = restart_subprocesses;
664 }
665 
run_adb_connector() const666 bool CuttlefishConfig::run_adb_connector() const {
667   return (*dictionary_)[kRunAdbConnector].asBool();
668 }
669 
set_run_adb_connector(bool run_adb_connector)670 void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) {
671   (*dictionary_)[kRunAdbConnector] = run_adb_connector;
672 }
673 
adb_connector_binary() const674 std::string CuttlefishConfig::adb_connector_binary() const {
675   return (*dictionary_)[kAdbConnectorBinary].asString();
676 }
677 
set_adb_connector_binary(const std::string & adb_connector_binary)678 void CuttlefishConfig::set_adb_connector_binary(
679     const std::string& adb_connector_binary) {
680   (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary;
681 }
682 
socket_vsock_proxy_binary() const683 std::string CuttlefishConfig::socket_vsock_proxy_binary() const {
684   return (*dictionary_)[kSocketVsockProxyBinary].asString();
685 }
686 
set_socket_vsock_proxy_binary(const std::string & socket_vsock_proxy_binary)687 void CuttlefishConfig::set_socket_vsock_proxy_binary(
688     const std::string& socket_vsock_proxy_binary) {
689   (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary;
690 }
691 
run_as_daemon() const692 bool CuttlefishConfig::run_as_daemon() const {
693   return (*dictionary_)[kRunAsDaemon].asBool();
694 }
695 
set_run_as_daemon(bool run_as_daemon)696 void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) {
697   (*dictionary_)[kRunAsDaemon] = run_as_daemon;
698 }
data_policy() const699 std::string CuttlefishConfig::data_policy() const {
700   return (*dictionary_)[kDataPolicy].asString();
701 }
702 
set_data_policy(const std::string & data_policy)703 void CuttlefishConfig::set_data_policy(const std::string& data_policy) {
704   (*dictionary_)[kDataPolicy] = data_policy;
705 }
706 
blank_data_image_mb() const707 int CuttlefishConfig::blank_data_image_mb() const {
708   return (*dictionary_)[kBlankDataImageMb].asInt();
709 }
710 
set_blank_data_image_mb(int blank_data_image_mb)711 void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) {
712   (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb;
713 }
714 
blank_data_image_fmt() const715 std::string CuttlefishConfig::blank_data_image_fmt() const {
716   return (*dictionary_)[kBlankDataImageFmt].asString();
717 }
718 
set_blank_data_image_fmt(const std::string & blank_data_image_fmt)719 void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) {
720   (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt;
721 }
722 
723 
set_logcat_mode(const std::string & mode)724 void CuttlefishConfig::set_logcat_mode(const std::string& mode) {
725   (*dictionary_)[kLogcatMode] = mode;
726 }
727 
logcat_mode() const728 std::string CuttlefishConfig::logcat_mode() const {
729   return (*dictionary_)[kLogcatMode].asString();
730 }
731 
set_logcat_receiver_binary(const std::string & binary)732 void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) {
733   SetPath(kLogcatReceiverBinary, binary);
734 }
735 
logcat_receiver_binary() const736 std::string CuttlefishConfig::logcat_receiver_binary() const {
737   return (*dictionary_)[kLogcatReceiverBinary].asString();
738 }
739 
set_config_server_binary(const std::string & binary)740 void CuttlefishConfig::set_config_server_binary(const std::string& binary) {
741   SetPath(kConfigServerBinary, binary);
742 }
743 
config_server_binary() const744 std::string CuttlefishConfig::config_server_binary() const {
745   return (*dictionary_)[kConfigServerBinary].asString();
746 }
747 
enable_tombstone_receiver() const748 bool CuttlefishConfig::enable_tombstone_receiver() const {
749   return (*dictionary_)[kRunTombstoneReceiver].asBool();
750 }
751 
set_enable_tombstone_receiver(bool enable_tombstone_receiver)752 void CuttlefishConfig::set_enable_tombstone_receiver(bool enable_tombstone_receiver) {
753   (*dictionary_)[kRunTombstoneReceiver] = enable_tombstone_receiver;
754 }
755 
tombstone_receiver_binary() const756 std::string CuttlefishConfig::tombstone_receiver_binary() const {
757   return (*dictionary_)[kTombstoneReceiverBinary].asString();
758 }
759 
set_tombstone_receiver_binary(const std::string & e2e_test_binary)760 void CuttlefishConfig::set_tombstone_receiver_binary(const std::string& e2e_test_binary) {
761   (*dictionary_)[kTombstoneReceiverBinary] = e2e_test_binary;
762 }
763 
use_bootloader() const764 bool CuttlefishConfig::use_bootloader() const {
765   return (*dictionary_)[kUseBootloader].asBool();
766 }
767 
set_use_bootloader(bool use_bootloader)768 void CuttlefishConfig::set_use_bootloader(bool use_bootloader) {
769   (*dictionary_)[kUseBootloader] = use_bootloader;
770 }
771 
bootloader() const772 std::string CuttlefishConfig::bootloader() const {
773   return (*dictionary_)[kBootloader].asString();
774 }
775 
set_bootloader(const std::string & bootloader)776 void CuttlefishConfig::set_bootloader(const std::string& bootloader) {
777   SetPath(kBootloader, bootloader);
778 }
779 
set_boot_slot(const std::string & boot_slot)780 void CuttlefishConfig::set_boot_slot(const std::string& boot_slot) {
781   (*dictionary_)[kBootSlot] = boot_slot;
782 }
783 
boot_slot() const784 std::string CuttlefishConfig::boot_slot() const {
785   return (*dictionary_)[kBootSlot].asString();
786 }
787 
set_webrtc_certs_dir(const std::string & certs_dir)788 void CuttlefishConfig::set_webrtc_certs_dir(const std::string& certs_dir) {
789   (*dictionary_)[kWebRTCCertsDir] = certs_dir;
790 }
791 
webrtc_certs_dir() const792 std::string CuttlefishConfig::webrtc_certs_dir() const {
793   return (*dictionary_)[kWebRTCCertsDir].asString();
794 }
795 
touch_socket_path() const796 std::string CuttlefishConfig::InstanceSpecific::touch_socket_path() const {
797   return PerInstanceInternalPath("touch.sock");
798 }
799 
keyboard_socket_path() const800 std::string CuttlefishConfig::InstanceSpecific::keyboard_socket_path() const {
801   return PerInstanceInternalPath("keyboard.sock");
802 }
803 
frames_socket_path() const804 std::string CuttlefishConfig::InstanceSpecific::frames_socket_path() const {
805   return PerInstanceInternalPath("frames.sock");
806 }
807 
set_loop_max_part(int loop_max_part)808 void CuttlefishConfig::set_loop_max_part(int loop_max_part) {
809   (*dictionary_)[kLoopMaxPart] = loop_max_part;
810 }
loop_max_part() const811 int CuttlefishConfig::loop_max_part() const {
812   return (*dictionary_)[kLoopMaxPart].asInt();
813 }
814 
set_guest_enforce_security(bool guest_enforce_security)815 void CuttlefishConfig::set_guest_enforce_security(bool guest_enforce_security) {
816   (*dictionary_)[kGuestEnforceSecurity] = guest_enforce_security;
817 }
guest_enforce_security() const818 bool CuttlefishConfig::guest_enforce_security() const {
819   return (*dictionary_)[kGuestEnforceSecurity].asBool();
820 }
821 
set_guest_audit_security(bool guest_audit_security)822 void CuttlefishConfig::set_guest_audit_security(bool guest_audit_security) {
823   (*dictionary_)[kGuestAuditSecurity] = guest_audit_security;
824 }
guest_audit_security() const825 bool CuttlefishConfig::guest_audit_security() const {
826   return (*dictionary_)[kGuestAuditSecurity].asBool();
827 }
828 
set_guest_force_normal_boot(bool guest_force_normal_boot)829 void CuttlefishConfig::set_guest_force_normal_boot(bool guest_force_normal_boot) {
830   (*dictionary_)[kGuestForceNormalBoot] = guest_force_normal_boot;
831 }
guest_force_normal_boot() const832 bool CuttlefishConfig::guest_force_normal_boot() const {
833   return (*dictionary_)[kGuestForceNormalBoot].asBool();
834 }
835 
set_wifi_mac_address(const std::array<unsigned char,6> & mac_address)836 void CuttlefishConfig::MutableInstanceSpecific::set_wifi_mac_address(
837     const std::array<unsigned char, 6>& mac_address) {
838   Json::Value mac_address_obj(Json::arrayValue);
839   for (const auto& num : mac_address) {
840     mac_address_obj.append(num);
841   }
842   (*Dictionary())[kWifiMacAddress] = mac_address_obj;
843 }
844 
wifi_mac_address() const845 std::array<unsigned char, 6> CuttlefishConfig::InstanceSpecific::wifi_mac_address() const {
846   std::array<unsigned char, 6> mac_address{0, 0, 0, 0, 0, 0};
847   auto mac_address_obj = (*Dictionary())[kWifiMacAddress];
848   if (mac_address_obj.size() != 6) {
849     LOG(ERROR) << kWifiMacAddress << " entry had wrong size";
850     return {};
851   }
852   for (int i = 0; i < 6; i++) {
853     mac_address[i] = mac_address_obj[i].asInt();
854   }
855   return mac_address;
856 }
857 
set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline)858 void CuttlefishConfig::set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline) {
859   Json::Value args_json_obj(Json::arrayValue);
860   for (const auto& arg : android::base::Split(boot_image_kernel_cmdline, " ")) {
861     args_json_obj.append(arg);
862   }
863   (*dictionary_)[kBootImageKernelCmdline] = args_json_obj;
864 }
boot_image_kernel_cmdline() const865 std::vector<std::string> CuttlefishConfig::boot_image_kernel_cmdline() const {
866   std::vector<std::string> cmdline;
867   for (const Json::Value& arg : (*dictionary_)[kBootImageKernelCmdline]) {
868     cmdline.push_back(arg.asString());
869   }
870   return cmdline;
871 }
872 
set_extra_kernel_cmdline(std::string extra_cmdline)873 void CuttlefishConfig::set_extra_kernel_cmdline(std::string extra_cmdline) {
874   Json::Value args_json_obj(Json::arrayValue);
875   for (const auto& arg : android::base::Split(extra_cmdline, " ")) {
876     args_json_obj.append(arg);
877   }
878   (*dictionary_)[kExtraKernelCmdline] = args_json_obj;
879 }
extra_kernel_cmdline() const880 std::vector<std::string> CuttlefishConfig::extra_kernel_cmdline() const {
881   std::vector<std::string> cmdline;
882   for (const Json::Value& arg : (*dictionary_)[kExtraKernelCmdline]) {
883     cmdline.push_back(arg.asString());
884   }
885   return cmdline;
886 }
887 
888 // Creates the (initially empty) config object and populates it with values from
889 // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
890 // Returns nullptr if there was an error loading from file
BuildConfigImpl()891 /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() {
892   auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName,
893                                              vsoc::GetGlobalConfigFileLink());
894   auto ret = new CuttlefishConfig();
895   if (ret) {
896     auto loaded = ret->LoadFromFile(config_file_path.c_str());
897     if (!loaded) {
898       delete ret;
899       return nullptr;
900     }
901   }
902   return ret;
903 }
904 
Get()905 /*static*/ const CuttlefishConfig* CuttlefishConfig::Get() {
906   static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl());
907   return config.get();
908 }
909 
CuttlefishConfig()910 CuttlefishConfig::CuttlefishConfig() : dictionary_(new Json::Value()) {}
911 // Can't use '= default' on the header because the compiler complains of
912 // Json::Value being an incomplete type
913 CuttlefishConfig::~CuttlefishConfig() = default;
914 
915 CuttlefishConfig::CuttlefishConfig(CuttlefishConfig&&) = default;
916 CuttlefishConfig& CuttlefishConfig::operator=(CuttlefishConfig&&) = default;
917 
LoadFromFile(const char * file)918 bool CuttlefishConfig::LoadFromFile(const char* file) {
919   auto real_file_path = cvd::AbsolutePath(file);
920   if (real_file_path.empty()) {
921     LOG(ERROR) << "Could not get real path for file " << file;
922     return false;
923   }
924   Json::Reader reader;
925   std::ifstream ifs(real_file_path);
926   if (!reader.parse(ifs, *dictionary_)) {
927     LOG(ERROR) << "Could not read config file " << file << ": "
928                << reader.getFormattedErrorMessages();
929     return false;
930   }
931   return true;
932 }
SaveToFile(const std::string & file) const933 bool CuttlefishConfig::SaveToFile(const std::string& file) const {
934   std::ofstream ofs(file);
935   if (!ofs.is_open()) {
936     LOG(ERROR) << "Unable to write to file " << file;
937     return false;
938   }
939   ofs << *dictionary_;
940   return !ofs.fail();
941 }
942 
AssemblyPath(const std::string & file_name) const943 std::string CuttlefishConfig::AssemblyPath(
944     const std::string& file_name) const {
945   return cvd::AbsolutePath(assembly_dir() + "/" + file_name);
946 }
947 
composite_disk_path() const948 std::string CuttlefishConfig::composite_disk_path() const {
949   return AssemblyPath("composite.img");
950 }
951 
PerInstancePath(const char * file_name) const952 std::string CuttlefishConfig::InstanceSpecific::PerInstancePath(
953     const char* file_name) const {
954   return (instance_dir() + "/") + file_name;
955 }
956 
PerInstanceInternalPath(const char * file_name) const957 std::string CuttlefishConfig::InstanceSpecific::PerInstanceInternalPath(
958     const char* file_name) const {
959   if (file_name[0] == '\0') {
960     // Don't append a / if file_name is empty.
961     return PerInstancePath(kInternalDirName);
962   }
963   auto relative_path = (std::string(kInternalDirName) + "/") + file_name;
964   return PerInstancePath(relative_path.c_str());
965 }
966 
instance_name() const967 std::string CuttlefishConfig::InstanceSpecific::instance_name() const {
968   return ForCurrentInstance("cvd-");
969 }
970 
ForInstance(int num)971 CuttlefishConfig::MutableInstanceSpecific CuttlefishConfig::ForInstance(int num) {
972   return MutableInstanceSpecific(this, std::to_string(num));
973 }
974 
ForInstance(int num) const975 CuttlefishConfig::InstanceSpecific CuttlefishConfig::ForInstance(int num) const {
976   return InstanceSpecific(this, std::to_string(num));
977 }
978 
ForDefaultInstance() const979 CuttlefishConfig::InstanceSpecific CuttlefishConfig::ForDefaultInstance() const {
980   return InstanceSpecific(this, std::to_string(GetInstance()));
981 }
982 
Instances() const983 std::vector<CuttlefishConfig::InstanceSpecific> CuttlefishConfig::Instances() const {
984   const auto& json = (*dictionary_)[kInstances];
985   std::vector<CuttlefishConfig::InstanceSpecific> instances;
986   for (const auto& name : json.getMemberNames()) {
987     instances.push_back(CuttlefishConfig::InstanceSpecific(this, name));
988   }
989   return instances;
990 }
991 
GetInstance()992 int GetInstance() {
993   static int instance_id = InstanceFromEnvironment();
994   return instance_id;
995 }
996 
GetGlobalConfigFileLink()997 std::string GetGlobalConfigFileLink() {
998   return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
999 }
1000 
ForCurrentInstance(const char * prefix)1001 std::string ForCurrentInstance(const char* prefix) {
1002   std::ostringstream stream;
1003   stream << prefix << std::setfill('0') << std::setw(2) << GetInstance();
1004   return stream.str();
1005 }
ForCurrentInstance(int base)1006 int ForCurrentInstance(int base) { return base + GetInstance() - 1; }
1007 
RandomSerialNumber(const std::string & prefix)1008 std::string RandomSerialNumber(const std::string& prefix) {
1009   const char hex_characters[] = "0123456789ABCDEF";
1010   std::srand(time(0));
1011   char str[10];
1012   for(int i=0; i<10; i++){
1013     str[i] = hex_characters[rand() % strlen(hex_characters)];
1014   }
1015   return prefix + str;
1016 }
1017 
GetDefaultPerInstanceDir()1018 std::string GetDefaultPerInstanceDir() {
1019   std::ostringstream stream;
1020   stream << std::getenv("HOME") << "/cuttlefish_runtime";
1021   return stream.str();
1022 }
1023 
GetDefaultPerInstanceVsockCid()1024 int GetDefaultPerInstanceVsockCid() {
1025   constexpr int kFirstGuestCid = 3;
1026   return vsoc::HostSupportsVsock() ? ForCurrentInstance(kFirstGuestCid) : 0;
1027 }
1028 
DefaultHostArtifactsPath(const std::string & file_name)1029 std::string DefaultHostArtifactsPath(const std::string& file_name) {
1030   return (cvd::StringFromEnv("ANDROID_HOST_OUT",
1031                              cvd::StringFromEnv("HOME", ".")) +
1032           "/") +
1033          file_name;
1034 }
1035 
DefaultGuestImagePath(const std::string & file_name)1036 std::string DefaultGuestImagePath(const std::string& file_name) {
1037   return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT",
1038                              cvd::StringFromEnv("HOME", ".")) +
1039           "/") +
1040          file_name;
1041 }
1042 
HostSupportsQemuCli()1043 bool HostSupportsQemuCli() {
1044   static bool supported =
1045       std::system(
1046           "/usr/lib/cuttlefish-common/bin/capability_query.py qemu_cli") == 0;
1047   return supported;
1048 }
1049 
HostSupportsVsock()1050 bool HostSupportsVsock() {
1051   static bool supported =
1052       std::system(
1053           "/usr/lib/cuttlefish-common/bin/capability_query.py vsock") == 0;
1054   return supported;
1055 }
1056 }  // namespace vsoc
1057