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
29 #include <glog/logging.h>
30 #include <json/json.h>
31
32 #include "common/libs/utils/environment.h"
33 #include "common/libs/utils/files.h"
34 #include "host/libs/vm_manager/qemu_manager.h"
35
36
37 namespace {
38
InstanceFromEnvironment()39 int InstanceFromEnvironment() {
40 static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE";
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");
48
49 if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix,
50 sizeof(vsoc::kVsocUserPrefix) - 1)) {
51 // No user or we don't recognize this user
52 LOG(WARNING) << "No user or non-vsoc user, returning default config";
53 return kDefaultInstance;
54 }
55 instance_str += sizeof(vsoc::kVsocUserPrefix) - 1;
56
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 }
68
69 const char* kSerialNumber = "serial_number";
70 const char* kInstanceDir = "instance_dir";
71 const char* kVmManager = "vm_manager";
72 const char* kHardwareName = "hardware_name";
73 const char* kDeviceTitle = "device_title";
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";
80 const char* kNumScreenBuffers = "num_screen_buffers";
81 const char* kRefreshRateHz = "refresh_rate_hz";
82
83 const char* kKernelImagePath = "kernel_image_path";
84 const char* kUseUnpackedKernel = "use_unpacked_kernel";
85 const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path";
86 const char* kDecompressKernel = "decompress_kernel";
87 const char* kGdbFlag = "gdb_flag";
88 const char* kKernelCmdline = "kernel_cmdline";
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";
95 const char* kMetadataImagePath = "metadata_image_path";
96 const char* kProductImagePath = "product_image_path";
97 const char* kSuperImagePath = "super_image_path";
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";
102 const char* kDeprecatedBootCompleted = "deprecated_boot_completed";
103 const char* kConsolePath = "console_path";
104 const char* kLogcatPath = "logcat_path";
105 const char* kLauncherLogPath = "launcher_log_path";
106 const char* kLauncherMonitorPath = "launcher_monitor_socket";
107 const char* kDtbPath = "dtb_path";
108 const char* kGsiFstabPath = "gsi.fstab_path";
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";
117 const char* kWifiTapName = "wifi_tap_name";
118 const char* kWifiGuestMacAddr = "wifi_guest_mac_addr";
119 const char* kWifiHostMacAddr = "wifi_host_mac_addr";
120 const char* kEntropySource = "entropy_source";
121 const char* kVsockGuestCid = "vsock_guest_cid";
122
123 const char* kUuid = "uuid";
124 const char* kCuttlefishEnvPath = "cuttlefish_env_path";
125
126 const char* kAdbMode = "adb_mode";
127 const char* kAdbIPAndPort = "adb_ip_and_port";
128 const char* kSetupWizardMode = "setupwizard_mode";
129
130 const char* kQemuBinary = "qemu_binary";
131 const char* kCrosvmBinary = "crosvm_binary";
132 const char* kIvServerBinary = "ivserver_binary";
133 const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary";
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";
142
143 const char* kRestartSubprocesses = "restart_subprocesses";
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";
149
150 const char* kRunAsDaemon = "run_as_daemon";
151 const char* kRunE2eTest = "run_e2e_test";
152 const char* kE2eTestBinary = "e2e_test_binary";
153
154 const char* kDataPolicy = "data_policy";
155 const char* kBlankDataImageMb = "blank_data_image_mb";
156 const char* kBlankDataImageFmt = "blank_data_image_fmt";
157
158 const char* kLogcatMode = "logcat_mode";
159 const char* kLogcatVsockPort = "logcat_vsock_port";
160 const char* kFramesVsockPort = "frames_vsock_port";
161 const char* kLogcatReceiverBinary = "logcat_receiver_binary";
162 } // namespace
163
164 namespace vsoc {
165
instance_dir() const166 std::string CuttlefishConfig::instance_dir() const {
167 return (*dictionary_)[kInstanceDir].asString();
168 }
set_instance_dir(const std::string & instance_dir)169 void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) {
170 (*dictionary_)[kInstanceDir] = instance_dir;
171 }
172
vm_manager() const173 std::string CuttlefishConfig::vm_manager() const {
174 return (*dictionary_)[kVmManager].asString();
175 }
set_vm_manager(const std::string & name)176 void CuttlefishConfig::set_vm_manager(const std::string& name) {
177 (*dictionary_)[kVmManager] = name;
178 }
179
hardware_name() const180 std::string CuttlefishConfig::hardware_name() const {
181 return (*dictionary_)[kHardwareName].asString();
182 }
set_hardware_name(const std::string & name)183 void CuttlefishConfig::set_hardware_name(const std::string& name) {
184 (*dictionary_)[kHardwareName] = name;
185 }
186
serial_number() const187 std::string CuttlefishConfig::serial_number() const {
188 return (*dictionary_)[kSerialNumber].asString();
189 }
set_serial_number(const std::string & serial_number)190 void CuttlefishConfig::set_serial_number(const std::string& serial_number) {
191 (*dictionary_)[kSerialNumber] = serial_number;
192 }
193
cpus() const194 int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); }
set_cpus(int cpus)195 void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; }
196
memory_mb() const197 int CuttlefishConfig::memory_mb() const {
198 return (*dictionary_)[kMemoryMb].asInt();
199 }
set_memory_mb(int memory_mb)200 void CuttlefishConfig::set_memory_mb(int memory_mb) {
201 (*dictionary_)[kMemoryMb] = memory_mb;
202 }
203
dpi() const204 int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); }
set_dpi(int dpi)205 void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; }
206
x_res() const207 int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); }
set_x_res(int x_res)208 void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; }
209
y_res() const210 int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
set_y_res(int y_res)211 void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
212
num_screen_buffers() const213 int CuttlefishConfig::num_screen_buffers() const {
214 return (*dictionary_)[kNumScreenBuffers].asInt();
215 }
set_num_screen_buffers(int num_screen_buffers)216 void CuttlefishConfig::set_num_screen_buffers(int num_screen_buffers) {
217 (*dictionary_)[kNumScreenBuffers] = num_screen_buffers;
218 }
219
refresh_rate_hz() const220 int CuttlefishConfig::refresh_rate_hz() const {
221 return (*dictionary_)[kRefreshRateHz].asInt();
222 }
set_refresh_rate_hz(int refresh_rate_hz)223 void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) {
224 (*dictionary_)[kRefreshRateHz] = refresh_rate_hz;
225 }
226
kernel_image_path() const227 std::string CuttlefishConfig::kernel_image_path() const {
228 return (*dictionary_)[kKernelImagePath].asString();
229 }
230
SetPath(const std::string & key,const std::string & path)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
set_kernel_image_path(const std::string & kernel_image_path)238 void CuttlefishConfig::set_kernel_image_path(
239 const std::string& kernel_image_path) {
240 SetPath(kKernelImagePath, kernel_image_path);
241 }
242
use_unpacked_kernel() const243 bool CuttlefishConfig::use_unpacked_kernel() const {
244 return (*dictionary_)[kUseUnpackedKernel].asBool();
245 }
246
set_use_unpacked_kernel(bool use_unpacked_kernel)247 void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) {
248 (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel;
249 }
250
decompress_kernel() const251 bool CuttlefishConfig::decompress_kernel() const {
252 return (*dictionary_)[kDecompressKernel].asBool();
253 }
set_decompress_kernel(bool decompress_kernel)254 void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) {
255 (*dictionary_)[kDecompressKernel] = decompress_kernel;
256 }
257
decompressed_kernel_image_path() const258 std::string CuttlefishConfig::decompressed_kernel_image_path() const {
259 return (*dictionary_)[kDecompressedKernelImagePath].asString();
260 }
set_decompressed_kernel_image_path(const std::string & path)261 void CuttlefishConfig::set_decompressed_kernel_image_path(
262 const std::string& path) {
263 SetPath(kDecompressedKernelImagePath, path);
264 }
265
gdb_flag() const266 std::string CuttlefishConfig::gdb_flag() const {
267 return (*dictionary_)[kGdbFlag].asString();
268 }
269
set_gdb_flag(const std::string & device)270 void CuttlefishConfig::set_gdb_flag(const std::string& device) {
271 (*dictionary_)[kGdbFlag] = device;
272 }
273
kernel_cmdline() const274 std::set<std::string> CuttlefishConfig::kernel_cmdline() const {
275 std::set<std::string> args_set;
276 auto args_json_obj = (*dictionary_)[kKernelCmdline];
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;
281 }
set_kernel_cmdline(const std::set<std::string> & kernel_cmdline)282 void CuttlefishConfig::set_kernel_cmdline(
283 const std::set<std::string>& kernel_cmdline) {
284 Json::Value args_json_obj(Json::arrayValue);
285 for (const auto& arg : kernel_cmdline) {
286 args_json_obj.append(arg);
287 }
288 (*dictionary_)[kKernelCmdline] = args_json_obj;
289 }
add_kernel_cmdline(const std::set<std::string> & extra_args)290 void CuttlefishConfig::add_kernel_cmdline(
291 const std::set<std::string>& extra_args) {
292 std::set<std::string> cmdline = kernel_cmdline();
293 for (const auto& arg : extra_args) {
294 if (cmdline.count(arg)) {
295 LOG(ERROR) << "Kernel argument " << arg << " is duplicated";
296 }
297 cmdline.insert(arg);
298 }
299 set_kernel_cmdline(cmdline);
300 }
add_kernel_cmdline(const std::string & kernel_cmdline)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;
304 using is_iter = std::istream_iterator<std::string>;
305 std::copy(is_iter(args_stream), is_iter(),
306 std::inserter(kernel_cmdline_set, kernel_cmdline_set.begin()));
307 add_kernel_cmdline(kernel_cmdline_set);
308 }
kernel_cmdline_as_string() const309 std::string CuttlefishConfig::kernel_cmdline_as_string() const {
310 auto args_set = kernel_cmdline();
311 std::stringstream output;
312 std::copy(args_set.begin(), args_set.end(),
313 std::ostream_iterator<std::string>(output, " "));
314 return output.str();
315 }
316
ramdisk_image_path() const317 std::string CuttlefishConfig::ramdisk_image_path() const {
318 return (*dictionary_)[kRamdiskImagePath].asString();
319 }
set_ramdisk_image_path(const std::string & ramdisk_image_path)320 void CuttlefishConfig::set_ramdisk_image_path(
321 const std::string& ramdisk_image_path) {
322 SetPath(kRamdiskImagePath, ramdisk_image_path);
323 }
324
system_image_path() const325 std::string CuttlefishConfig::system_image_path() const {
326 return (*dictionary_)[kSystemImagePath].asString();
327 }
set_system_image_path(const std::string & system_image_path)328 void CuttlefishConfig::set_system_image_path(
329 const std::string& system_image_path) {
330 SetPath(kSystemImagePath, system_image_path);
331 }
332
cache_image_path() const333 std::string CuttlefishConfig::cache_image_path() const {
334 return (*dictionary_)[kCacheImagePath].asString();
335 }
set_cache_image_path(const std::string & cache_image_path)336 void CuttlefishConfig::set_cache_image_path(
337 const std::string& cache_image_path) {
338 SetPath(kCacheImagePath, cache_image_path);
339 }
340
data_image_path() const341 std::string CuttlefishConfig::data_image_path() const {
342 return (*dictionary_)[kDataImagePath].asString();
343 }
set_data_image_path(const std::string & data_image_path)344 void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) {
345 SetPath(kDataImagePath, data_image_path);
346 }
347
vendor_image_path() const348 std::string CuttlefishConfig::vendor_image_path() const {
349 return (*dictionary_)[kVendorImagePath].asString();
350 }
set_vendor_image_path(const std::string & vendor_image_path)351 void CuttlefishConfig::set_vendor_image_path(
352 const std::string& vendor_image_path) {
353 SetPath(kVendorImagePath, vendor_image_path);
354 }
355
metadata_image_path() const356 std::string CuttlefishConfig::metadata_image_path() const {
357 return (*dictionary_)[kMetadataImagePath].asString();
358 }
set_metadata_image_path(const std::string & metadata_image_path)359 void CuttlefishConfig::set_metadata_image_path(
360 const std::string& metadata_image_path) {
361 SetPath(kMetadataImagePath, metadata_image_path);
362 }
363
super_image_path() const364 std::string CuttlefishConfig::super_image_path() const {
365 return (*dictionary_)[kSuperImagePath].asString();
366 }
set_super_image_path(const std::string & super_image_path)367 void CuttlefishConfig::set_super_image_path(
368 const std::string& super_image_path) {
369 SetPath(kSuperImagePath, super_image_path);
370 }
371
product_image_path() const372 std::string CuttlefishConfig::product_image_path() const {
373 return (*dictionary_)[kProductImagePath].asString();
374 }
set_product_image_path(const std::string & product_image_path)375 void CuttlefishConfig::set_product_image_path(
376 const std::string& product_image_path) {
377 SetPath(kProductImagePath, product_image_path);
378 }
379
dtb_path() const380 std::string CuttlefishConfig::dtb_path() const {
381 return (*dictionary_)[kDtbPath].asString();
382 }
set_dtb_path(const std::string & dtb_path)383 void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) {
384 SetPath(kDtbPath, dtb_path);
385 }
386
gsi_fstab_path() const387 std::string CuttlefishConfig::gsi_fstab_path() const {
388 return (*dictionary_)[kGsiFstabPath].asString();
389 }
set_gsi_fstab_path(const std::string & path)390 void CuttlefishConfig::set_gsi_fstab_path(const std::string& path){
391 SetPath(kGsiFstabPath, path);
392 }
393
mempath() const394 std::string CuttlefishConfig::mempath() const {
395 return (*dictionary_)[kMempath].asString();
396 }
set_mempath(const std::string & mempath)397 void CuttlefishConfig::set_mempath(const std::string& mempath) {
398 SetPath(kMempath, mempath);
399 }
400
ivshmem_qemu_socket_path() const401 std::string CuttlefishConfig::ivshmem_qemu_socket_path() const {
402 return (*dictionary_)[kIvshmemQemuSocketPath].asString();
403 }
set_ivshmem_qemu_socket_path(const std::string & ivshmem_qemu_socket_path)404 void CuttlefishConfig::set_ivshmem_qemu_socket_path(
405 const std::string& ivshmem_qemu_socket_path) {
406 SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path);
407 }
408
ivshmem_client_socket_path() const409 std::string CuttlefishConfig::ivshmem_client_socket_path() const {
410 return (*dictionary_)[kIvshmemClientSocketPath].asString();
411 }
set_ivshmem_client_socket_path(const std::string & ivshmem_client_socket_path)412 void CuttlefishConfig::set_ivshmem_client_socket_path(
413 const std::string& ivshmem_client_socket_path) {
414 SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path);
415 }
416
ivshmem_vector_count() const417 int CuttlefishConfig::ivshmem_vector_count() const {
418 return (*dictionary_)[kIvshmemVectorCount].asInt();
419 }
set_ivshmem_vector_count(int ivshmem_vector_count)420 void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) {
421 (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count;
422 }
423
usb_v1_socket_name() const424 std::string CuttlefishConfig::usb_v1_socket_name() const {
425 return (*dictionary_)[kUsbV1SocketName].asString();
426 }
set_usb_v1_socket_name(const std::string & usb_v1_socket_name)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
vhci_port() const432 int CuttlefishConfig::vhci_port() const {
433 return (*dictionary_)[kVhciPort].asInt();
434 }
set_vhci_port(int vhci_port)435 void CuttlefishConfig::set_vhci_port(int vhci_port) {
436 (*dictionary_)[kVhciPort] = vhci_port;
437 }
438
usb_ip_socket_name() const439 std::string CuttlefishConfig::usb_ip_socket_name() const {
440 return (*dictionary_)[kUsbIpSocketName].asString();
441 }
set_usb_ip_socket_name(const std::string & usb_ip_socket_name)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
kernel_log_socket_name() const447 std::string CuttlefishConfig::kernel_log_socket_name() const {
448 return (*dictionary_)[kKernelLogSocketName].asString();
449 }
set_kernel_log_socket_name(const std::string & kernel_log_socket_name)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
deprecated_boot_completed() const455 bool CuttlefishConfig::deprecated_boot_completed() const {
456 return (*dictionary_)[kDeprecatedBootCompleted].asBool();
457 }
set_deprecated_boot_completed(bool deprecated_boot_completed)458 void CuttlefishConfig::set_deprecated_boot_completed(
459 bool deprecated_boot_completed) {
460 (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed;
461 }
462
console_path() const463 std::string CuttlefishConfig::console_path() const {
464 return (*dictionary_)[kConsolePath].asString();
465 }
set_console_path(const std::string & console_path)466 void CuttlefishConfig::set_console_path(const std::string& console_path) {
467 SetPath(kConsolePath, console_path);
468 }
469
logcat_path() const470 std::string CuttlefishConfig::logcat_path() const {
471 return (*dictionary_)[kLogcatPath].asString();
472 }
set_logcat_path(const std::string & logcat_path)473 void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) {
474 SetPath(kLogcatPath, logcat_path);
475 }
476
launcher_monitor_socket_path() const477 std::string CuttlefishConfig::launcher_monitor_socket_path() const {
478 return (*dictionary_)[kLauncherMonitorPath].asString();
479 }
set_launcher_monitor_socket_path(const std::string & launcher_monitor_path)480 void CuttlefishConfig::set_launcher_monitor_socket_path(
481 const std::string& launcher_monitor_path) {
482 SetPath(kLauncherMonitorPath, launcher_monitor_path);
483 }
484
launcher_log_path() const485 std::string CuttlefishConfig::launcher_log_path() const {
486 return (*dictionary_)[kLauncherLogPath].asString();
487 }
set_launcher_log_path(const std::string & launcher_log_path)488 void CuttlefishConfig::set_launcher_log_path(
489 const std::string& launcher_log_path) {
490 (*dictionary_)[kLauncherLogPath] = launcher_log_path;
491 }
492
mobile_bridge_name() const493 std::string CuttlefishConfig::mobile_bridge_name() const {
494 return (*dictionary_)[kMobileBridgeName].asString();
495 }
set_mobile_bridge_name(const std::string & mobile_bridge_name)496 void CuttlefishConfig::set_mobile_bridge_name(
497 const std::string& mobile_bridge_name) {
498 (*dictionary_)[kMobileBridgeName] = mobile_bridge_name;
499 }
500
wifi_guest_mac_addr() const501 std::string CuttlefishConfig::wifi_guest_mac_addr() const {
502 return (*dictionary_)[kWifiGuestMacAddr].asString();
503 }
set_wifi_guest_mac_addr(const std::string & wifi_guest_mac_addr)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
wifi_host_mac_addr() const509 std::string CuttlefishConfig::wifi_host_mac_addr() const {
510 return (*dictionary_)[kWifiHostMacAddr].asString();
511 }
set_wifi_host_mac_addr(const std::string & wifi_host_mac_addr)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
mobile_tap_name() const517 std::string CuttlefishConfig::mobile_tap_name() const {
518 return (*dictionary_)[kMobileTapName].asString();
519 }
set_mobile_tap_name(const std::string & mobile_tap_name)520 void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) {
521 (*dictionary_)[kMobileTapName] = mobile_tap_name;
522 }
523
wifi_tap_name() const524 std::string CuttlefishConfig::wifi_tap_name() const {
525 return (*dictionary_)[kWifiTapName].asString();
526 }
set_wifi_tap_name(const std::string & wifi_tap_name)527 void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) {
528 (*dictionary_)[kWifiTapName] = wifi_tap_name;
529 }
530
entropy_source() const531 std::string CuttlefishConfig::entropy_source() const {
532 return (*dictionary_)[kEntropySource].asString();
533 }
set_entropy_source(const std::string & entropy_source)534 void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) {
535 (*dictionary_)[kEntropySource] = entropy_source;
536 }
537
vsock_guest_cid() const538 int CuttlefishConfig::vsock_guest_cid() const {
539 return (*dictionary_)[kVsockGuestCid].asInt();
540 }
541
set_vsock_guest_cid(int vsock_guest_cid)542 void CuttlefishConfig::set_vsock_guest_cid(int vsock_guest_cid) {
543 (*dictionary_)[kVsockGuestCid] = vsock_guest_cid;
544 }
545
uuid() const546 std::string CuttlefishConfig::uuid() const {
547 return (*dictionary_)[kUuid].asString();
548 }
set_uuid(const std::string & uuid)549 void CuttlefishConfig::set_uuid(const std::string& uuid) {
550 (*dictionary_)[kUuid] = uuid;
551 }
552
set_cuttlefish_env_path(const std::string & path)553 void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) {
554 SetPath(kCuttlefishEnvPath, path);
555 }
cuttlefish_env_path() const556 std::string CuttlefishConfig::cuttlefish_env_path() const {
557 return (*dictionary_)[kCuttlefishEnvPath].asString();
558 }
559
adb_mode() const560 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;
566 }
567
set_adb_mode(const std::set<std::string> & mode)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;
574 }
575
adb_ip_and_port() const576 std::string CuttlefishConfig::adb_ip_and_port() const {
577 return (*dictionary_)[kAdbIPAndPort].asString();
578 }
579
set_adb_ip_and_port(const std::string & ip_port)580 void CuttlefishConfig::set_adb_ip_and_port(const std::string& ip_port) {
581 (*dictionary_)[kAdbIPAndPort] = ip_port;
582 }
583
adb_device_name() const584 std::string CuttlefishConfig::adb_device_name() const {
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_tunnel") > 0;
589 bool nativeVsock = adb_mode().count("native_vsock") > 0;
590 if (tunnelMode || vsockTunnel || vsockHalfProxy || nativeVsock) {
591 return adb_ip_and_port();
592 } else if (adb_mode().count("usb") > 0) {
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
device_title() const599 std::string CuttlefishConfig::device_title() const {
600 return (*dictionary_)[kDeviceTitle].asString();
601 }
602
set_device_title(const std::string & title)603 void CuttlefishConfig::set_device_title(const std::string& title) {
604 (*dictionary_)[kDeviceTitle] = title;
605 }
606
setupwizard_mode() const607 std::string CuttlefishConfig::setupwizard_mode() const {
608 return (*dictionary_)[kSetupWizardMode].asString();
609 }
610
set_setupwizard_mode(const std::string & mode)611 void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) {
612 (*dictionary_)[kSetupWizardMode] = mode;
613 }
614
qemu_binary() const615 std::string CuttlefishConfig::qemu_binary() const {
616 return (*dictionary_)[kQemuBinary].asString();
617 }
618
set_qemu_binary(const std::string & qemu_binary)619 void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) {
620 (*dictionary_)[kQemuBinary] = qemu_binary;
621 }
622
crosvm_binary() const623 std::string CuttlefishConfig::crosvm_binary() const {
624 return (*dictionary_)[kCrosvmBinary].asString();
625 }
626
set_crosvm_binary(const std::string & crosvm_binary)627 void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) {
628 (*dictionary_)[kCrosvmBinary] = crosvm_binary;
629 }
630
ivserver_binary() const631 std::string CuttlefishConfig::ivserver_binary() const {
632 return (*dictionary_)[kIvServerBinary].asString();
633 }
634
set_ivserver_binary(const std::string & ivserver_binary)635 void CuttlefishConfig::set_ivserver_binary(const std::string& ivserver_binary) {
636 (*dictionary_)[kIvServerBinary] = ivserver_binary;
637 }
638
kernel_log_monitor_binary() const639 std::string CuttlefishConfig::kernel_log_monitor_binary() const {
640 return (*dictionary_)[kKernelLogMonitorBinary].asString();
641 }
642
set_kernel_log_monitor_binary(const std::string & kernel_log_monitor_binary)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
enable_vnc_server() const648 bool CuttlefishConfig::enable_vnc_server() const {
649 return (*dictionary_)[kEnableVncServer].asBool();
650 }
651
set_enable_vnc_server(bool enable_vnc_server)652 void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) {
653 (*dictionary_)[kEnableVncServer] = enable_vnc_server;
654 }
655
vnc_server_binary() const656 std::string CuttlefishConfig::vnc_server_binary() const {
657 return (*dictionary_)[kVncServerBinary].asString();
658 }
659
set_vnc_server_binary(const std::string & vnc_server_binary)660 void CuttlefishConfig::set_vnc_server_binary(
661 const std::string& vnc_server_binary) {
662 (*dictionary_)[kVncServerBinary] = vnc_server_binary;
663 }
664
vnc_server_port() const665 int CuttlefishConfig::vnc_server_port() const {
666 return (*dictionary_)[kVncServerPort].asInt();
667 }
668
set_vnc_server_port(int vnc_server_port)669 void CuttlefishConfig::set_vnc_server_port(int vnc_server_port) {
670 (*dictionary_)[kVncServerPort] = vnc_server_port;
671 }
672
enable_stream_audio() const673 bool CuttlefishConfig::enable_stream_audio() const {
674 return (*dictionary_)[kEnableStreamAudio].asBool();
675 }
676
set_enable_stream_audio(bool enable_stream_audio)677 void CuttlefishConfig::set_enable_stream_audio(bool enable_stream_audio) {
678 (*dictionary_)[kEnableStreamAudio] = enable_stream_audio;
679 }
680
stream_audio_binary() const681 std::string CuttlefishConfig::stream_audio_binary() const {
682 return (*dictionary_)[kStreamAudioBinary].asString();
683 }
684
set_stream_audio_binary(const std::string & stream_audio_binary)685 void CuttlefishConfig::set_stream_audio_binary(
686 const std::string& stream_audio_binary) {
687 (*dictionary_)[kStreamAudioBinary] = stream_audio_binary;
688 }
689
stream_audio_port() const690 int CuttlefishConfig::stream_audio_port() const {
691 return (*dictionary_)[kStreamAudioPort].asInt();
692 }
693
set_stream_audio_port(int stream_audio_port)694 void CuttlefishConfig::set_stream_audio_port(int stream_audio_port) {
695 (*dictionary_)[kStreamAudioPort] = stream_audio_port;
696 }
697
restart_subprocesses() const698 bool CuttlefishConfig::restart_subprocesses() const {
699 return (*dictionary_)[kRestartSubprocesses].asBool();
700 }
701
set_restart_subprocesses(bool restart_subprocesses)702 void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) {
703 (*dictionary_)[kRestartSubprocesses] = restart_subprocesses;
704 }
705
run_adb_connector() const706 bool CuttlefishConfig::run_adb_connector() const {
707 return (*dictionary_)[kRunAdbConnector].asBool();
708 }
709
set_run_adb_connector(bool run_adb_connector)710 void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) {
711 (*dictionary_)[kRunAdbConnector] = run_adb_connector;
712 }
713
adb_connector_binary() const714 std::string CuttlefishConfig::adb_connector_binary() const {
715 return (*dictionary_)[kAdbConnectorBinary].asString();
716 }
717
set_adb_connector_binary(const std::string & adb_connector_binary)718 void CuttlefishConfig::set_adb_connector_binary(
719 const std::string& adb_connector_binary) {
720 (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary;
721 }
722
virtual_usb_manager_binary() const723 std::string CuttlefishConfig::virtual_usb_manager_binary() const {
724 return (*dictionary_)[kVirtualUsbManagerBinary].asString();
725 }
726
set_virtual_usb_manager_binary(const std::string & virtual_usb_manager_binary)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
socket_forward_proxy_binary() const732 std::string CuttlefishConfig::socket_forward_proxy_binary() const {
733 return (*dictionary_)[kSocketForwardProxyBinary].asString();
734 }
735
set_socket_forward_proxy_binary(const std::string & socket_forward_proxy_binary)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
socket_vsock_proxy_binary() const741 std::string CuttlefishConfig::socket_vsock_proxy_binary() const {
742 return (*dictionary_)[kSocketVsockProxyBinary].asString();
743 }
744
set_socket_vsock_proxy_binary(const std::string & socket_vsock_proxy_binary)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
run_as_daemon() const750 bool CuttlefishConfig::run_as_daemon() const {
751 return (*dictionary_)[kRunAsDaemon].asBool();
752 }
753
set_run_as_daemon(bool run_as_daemon)754 void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) {
755 (*dictionary_)[kRunAsDaemon] = run_as_daemon;
756 }
757
run_e2e_test() const758 bool CuttlefishConfig::run_e2e_test() const {
759 return (*dictionary_)[kRunE2eTest].asBool();
760 }
761
set_run_e2e_test(bool run_e2e_test)762 void CuttlefishConfig::set_run_e2e_test(bool run_e2e_test) {
763 (*dictionary_)[kRunE2eTest] = run_e2e_test;
764 }
765
e2e_test_binary() const766 std::string CuttlefishConfig::e2e_test_binary() const {
767 return (*dictionary_)[kE2eTestBinary].asString();
768 }
769
set_e2e_test_binary(const std::string & e2e_test_binary)770 void CuttlefishConfig::set_e2e_test_binary(const std::string& e2e_test_binary) {
771 (*dictionary_)[kE2eTestBinary] = e2e_test_binary;
772 }
773
data_policy() const774 std::string CuttlefishConfig::data_policy() const {
775 return (*dictionary_)[kDataPolicy].asString();
776 }
777
set_data_policy(const std::string & data_policy)778 void CuttlefishConfig::set_data_policy(const std::string& data_policy) {
779 (*dictionary_)[kDataPolicy] = data_policy;
780 }
781
blank_data_image_mb() const782 int CuttlefishConfig::blank_data_image_mb() const {
783 return (*dictionary_)[kBlankDataImageMb].asInt();
784 }
785
set_blank_data_image_mb(int blank_data_image_mb)786 void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) {
787 (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb;
788 }
789
blank_data_image_fmt() const790 std::string CuttlefishConfig::blank_data_image_fmt() const {
791 return (*dictionary_)[kBlankDataImageFmt].asString();
792 }
793
set_blank_data_image_fmt(const std::string & blank_data_image_fmt)794 void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) {
795 (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt;
796 }
797
798
set_logcat_mode(const std::string & mode)799 void CuttlefishConfig::set_logcat_mode(const std::string& mode) {
800 (*dictionary_)[kLogcatMode] = mode;
801 }
802
logcat_mode() const803 std::string CuttlefishConfig::logcat_mode() const {
804 return (*dictionary_)[kLogcatMode].asString();
805 }
806
set_logcat_vsock_port(int port)807 void CuttlefishConfig::set_logcat_vsock_port(int port) {
808 (*dictionary_)[kLogcatVsockPort] = port;
809 }
810
logcat_vsock_port() const811 int CuttlefishConfig::logcat_vsock_port() const {
812 return (*dictionary_)[kLogcatVsockPort].asInt();
813 }
814
set_frames_vsock_port(int port)815 void CuttlefishConfig::set_frames_vsock_port(int port) {
816 (*dictionary_)[kFramesVsockPort] = port;
817 }
818
frames_vsock_port() const819 int CuttlefishConfig::frames_vsock_port() const {
820 return (*dictionary_)[kFramesVsockPort].asInt();
821 }
822
set_logcat_receiver_binary(const std::string & binary)823 void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) {
824 SetPath(kLogcatReceiverBinary, binary);
825 }
826
logcat_receiver_binary() const827 std::string CuttlefishConfig::logcat_receiver_binary() const {
828 return (*dictionary_)[kLogcatReceiverBinary].asString();
829 }
830
enable_ivserver() const831 bool CuttlefishConfig::enable_ivserver() const {
832 return hardware_name() == "cutf_ivsh";
833 }
834
touch_socket_path() const835 std::string CuttlefishConfig::touch_socket_path() const {
836 return PerInstancePath("touch.sock");
837 }
838
keyboard_socket_path() const839 std::string CuttlefishConfig::keyboard_socket_path() const {
840 return PerInstancePath("keyboard.sock");
841 }
842
843 // Creates the (initially empty) config object and populates it with values from
844 // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
845 // Returns nullptr if there was an error loading from file
BuildConfigImpl()846 /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() {
847 auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName,
848 vsoc::GetGlobalConfigFileLink());
849 auto ret = new CuttlefishConfig();
850 if (ret) {
851 auto loaded = ret->LoadFromFile(config_file_path.c_str());
852 if (!loaded) {
853 delete ret;
854 return nullptr;
855 }
856 }
857 return ret;
858 }
859
Get()860 /*static*/ CuttlefishConfig* CuttlefishConfig::Get() {
861 static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl());
862 return config.get();
863 }
864
CuttlefishConfig()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
~CuttlefishConfig()868 CuttlefishConfig::~CuttlefishConfig() {}
869
LoadFromFile(const char * file)870 bool CuttlefishConfig::LoadFromFile(const char* file) {
871 auto real_file_path = cvd::AbsolutePath(file);
872 if (real_file_path.empty()) {
873 LOG(ERROR) << "Could not get real path for file " << file;
874 return false;
875 }
876 Json::Reader reader;
877 std::ifstream ifs(real_file_path);
878 if (!reader.parse(ifs, *dictionary_)) {
879 LOG(ERROR) << "Could not read config file " << file << ": "
880 << reader.getFormattedErrorMessages();
881 return false;
882 }
883 return true;
884 }
SaveToFile(const std::string & file) const885 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
PerInstancePath(const char * file_name) const895 std::string CuttlefishConfig::PerInstancePath(const char* file_name) const {
896 return (instance_dir() + "/") + file_name;
897 }
898
instance_name() const899 std::string CuttlefishConfig::instance_name() const {
900 return GetPerInstanceDefault("cvd-");
901 }
902
GetInstance()903 int GetInstance() {
904 static int instance_id = InstanceFromEnvironment();
905 return instance_id;
906 }
907
GetGlobalConfigFileLink()908 std::string GetGlobalConfigFileLink() {
909 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
910 }
911
GetDomain()912 std::string GetDomain() {
913 return CuttlefishConfig::Get()->ivshmem_client_socket_path();
914 }
915
GetPerInstanceDefault(const char * prefix)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 }
GetPerInstanceDefault(int base)921 int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; }
922
GetDefaultPerInstanceDir()923 std::string GetDefaultPerInstanceDir() {
924 std::ostringstream stream;
925 stream << std::getenv("HOME") << "/cuttlefish_runtime";
926 return stream.str();
927 }
928
GetDefaultMempath()929 std::string GetDefaultMempath() {
930 return GetPerInstanceDefault("/var/run/shm/cvd-");
931 }
932
GetDefaultPerInstanceVsockCid()933 int GetDefaultPerInstanceVsockCid() {
934 constexpr int kFirstGuestCid = 3;
935 return vsoc::HostSupportsVsock() ? GetPerInstanceDefault(kFirstGuestCid) : 0;
936 }
937
DefaultHostArtifactsPath(const std::string & file_name)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
DefaultGuestImagePath(const std::string & file_name)945 std::string DefaultGuestImagePath(const std::string& file_name) {
946 return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT",
947 cvd::StringFromEnv("HOME", ".")) +
948 "/") +
949 file_name;
950 }
951
HostSupportsQemuCli()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 }
958
HostSupportsVsock()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 }
965 } // namespace vsoc
966