blob: af42de85801de278f1b4d72af714b77be71674a2 [file] [log] [blame]
Jorge E. Moreira577383b2018-05-24 14:17:51 -07001/*
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 Schuffelenefadb8d2018-08-10 16:10:35 -070019#include <algorithm>
Jorge E. Moreira577383b2018-05-24 14:17:51 -070020#include <climits>
21#include <cstdlib>
22#include <cstring>
23#include <fstream>
24#include <iomanip>
Cody Schuffelenefadb8d2018-08-10 16:10:35 -070025#include <iterator>
Jorge E. Moreira577383b2018-05-24 14:17:51 -070026#include <sstream>
27#include <string>
28
Jorge E. Moreira577383b2018-05-24 14:17:51 -070029#include <glog/logging.h>
30#include <json/json.h>
31
Jorge E. Moreira2a777f62018-06-13 17:28:10 -070032#include "common/libs/utils/environment.h"
Ryan Haining10e42312018-07-17 12:11:52 -070033#include "common/libs/utils/files.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080034#include "host/libs/vm_manager/qemu_manager.h"
35
Jorge E. Moreira2a777f62018-06-13 17:28:10 -070036
Jorge E. Moreira577383b2018-05-24 14:17:51 -070037namespace {
38
Jorge E. Moreira577383b2018-05-24 14:17:51 -070039int InstanceFromEnvironment() {
40 static constexpr char kInstanceEnvironmentVariable[] = "CUTTLEFISH_INSTANCE";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070041 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 Haining7338d352018-10-24 17:41:52 -070048
49 if (!instance_str || std::strncmp(instance_str, vsoc::kVsocUserPrefix,
50 sizeof(vsoc::kVsocUserPrefix) - 1)) {
Jorge E. Moreira577383b2018-05-24 14:17:51 -070051 // No user or we don't recognize this user
Ryan Haining7338d352018-10-24 17:41:52 -070052 LOG(WARNING) << "No user or non-vsoc user, returning default config";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070053 return kDefaultInstance;
54 }
Ryan Haining7338d352018-10-24 17:41:52 -070055 instance_str += sizeof(vsoc::kVsocUserPrefix) - 1;
56
Jorge E. Moreira577383b2018-05-24 14:17:51 -070057 // 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 Haining10e42312018-07-17 12:11:52 -070068
Jorge E. Moreira577383b2018-05-24 14:17:51 -070069const char* kSerialNumber = "serial_number";
70const char* kInstanceDir = "instance_dir";
Jorge E. Moreira88322832018-07-22 16:41:01 -070071const char* kVmManager = "vm_manager";
Jorge E. Moreiraba626622019-01-28 17:47:50 -080072const char* kHardwareName = "hardware_name";
Ryan Haining6d821ec2018-07-27 11:39:09 -070073const char* kDeviceTitle = "device_title";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070074
75const char* kCpus = "cpus";
76const char* kMemoryMb = "memory_mb";
77const char* kDpi = "dpi";
78const char* kXRes = "x_res";
79const char* kYRes = "y_res";
Cody Schuffelen5bc697c2019-01-28 21:05:29 -080080const char* kNumScreenBuffers = "num_screen_buffers";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070081const char* kRefreshRateHz = "refresh_rate_hz";
82
83const char* kKernelImagePath = "kernel_image_path";
Cody Schuffelene13a78a2019-01-29 14:51:26 -080084const char* kUseUnpackedKernel = "use_unpacked_kernel";
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080085const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path";
86const char* kDecompressKernel = "decompress_kernel";
Greg Hartman91f81422018-07-09 16:04:49 -070087const char* kGdbFlag = "gdb_flag";
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -070088const char* kKernelCmdline = "kernel_cmdline";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070089const char* kRamdiskImagePath = "ramdisk_image_path";
90
91const char* kSystemImagePath = "system_image_path";
92const char* kCacheImagePath = "cache_image_path";
93const char* kDataImagePath = "data_image_path";
94const char* kVendorImagePath = "vendor_image_path";
Paul Trautrimba8f8e92019-03-12 17:55:48 +090095const char* kMetadataImagePath = "metadata_image_path";
Alistair Strachan5782ce42019-03-28 22:05:13 -070096const char* kProductImagePath = "product_image_path";
Alistair Strachan054af062018-11-27 12:41:19 -080097const char* kSuperImagePath = "super_image_path";
Jorge E. Moreira577383b2018-05-24 14:17:51 -070098const char* kUsbV1SocketName = "usb_v1_socket_name";
99const char* kVhciPort = "vhci_port";
100const char* kUsbIpSocketName = "usb_ip_socket_name";
101const char* kKernelLogSocketName = "kernel_log_socket_name";
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700102const char* kDeprecatedBootCompleted = "deprecated_boot_completed";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700103const char* kConsolePath = "console_path";
104const char* kLogcatPath = "logcat_path";
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700105const char* kLauncherLogPath = "launcher_log_path";
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700106const char* kLauncherMonitorPath = "launcher_monitor_socket";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700107const char* kDtbPath = "dtb_path";
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800108const char* kGsiFstabPath = "gsi.fstab_path";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700109
110const char* kMempath = "mempath";
111const char* kIvshmemQemuSocketPath = "ivshmem_qemu_socket_path";
112const char* kIvshmemClientSocketPath = "ivshmem_client_socket_path";
113const char* kIvshmemVectorCount = "ivshmem_vector_count";
114
115const char* kMobileBridgeName = "mobile_bridge_name";
116const char* kMobileTapName = "mobile_tap_name";
Cody Schuffelen975175a2018-06-14 18:20:02 -0700117const char* kWifiTapName = "wifi_tap_name";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700118const char* kWifiGuestMacAddr = "wifi_guest_mac_addr";
119const char* kWifiHostMacAddr = "wifi_host_mac_addr";
120const char* kEntropySource = "entropy_source";
Cody Schuffelend946b5f2018-12-12 11:54:48 -0800121const char* kVsockGuestCid = "vsock_guest_cid";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700122
123const char* kUuid = "uuid";
Greg Hartman6abdbb92018-05-24 09:49:00 -0700124const char* kCuttlefishEnvPath = "cuttlefish_env_path";
Ryan Haining4a1abea2018-07-10 16:20:19 -0700125
126const char* kAdbMode = "adb_mode";
Ryan Haining7338d352018-10-24 17:41:52 -0700127const char* kAdbIPAndPort = "adb_ip_and_port";
Greg Hartmanf1f28c52018-09-20 17:19:19 -0700128const char* kSetupWizardMode = "setupwizard_mode";
Cody Schuffelen58980032018-10-11 17:11:13 -0700129
Cody Schuffelen58980032018-10-11 17:11:13 -0700130const char* kQemuBinary = "qemu_binary";
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800131const char* kCrosvmBinary = "crosvm_binary";
Cody Schuffelen5bc697c2019-01-28 21:05:29 -0800132const char* kIvServerBinary = "ivserver_binary";
Cody Schuffelen55676ca2019-01-28 22:00:05 -0800133const char* kKernelLogMonitorBinary = "kernel_log_monitor_binary";
Cody Schuffelen17b34722019-01-28 22:57:14 -0800134
135const char* kEnableVncServer = "enable_vnc_server";
136const char* kVncServerBinary = "vnc_server_binary";
137const char* kVncServerPort = "vnc_server_port";
138
139const char* kEnableStreamAudio = "enable_stream_audio";
140const char* kStreamAudioBinary = "stream_audio_binary";
141const char* kStreamAudioPort = "stream_audio_port";
Cody Schuffelend12e0c92019-01-29 15:44:45 -0800142
143const char* kRestartSubprocesses = "restart_subprocesses";
Cody Schuffelen1f08d972019-01-29 16:21:12 -0800144const char* kRunAdbConnector = "run_adb_connector";
145const char* kAdbConnectorBinary = "adb_connector_binary";
146const char* kVirtualUsbManagerBinary = "virtual_usb_manager_binary";
147const char* kSocketForwardProxyBinary = "socket_forward_proxy_binary";
148const char* kSocketVsockProxyBinary = "socket_vsock_proxy_binary";
Cody Schuffelen50249732019-01-29 16:42:55 -0800149
150const char* kRunAsDaemon = "run_as_daemon";
Cody Schuffelen3a640952019-01-29 17:14:41 -0800151const char* kRunE2eTest = "run_e2e_test";
152const char* kE2eTestBinary = "e2e_test_binary";
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800153
154const char* kDataPolicy = "data_policy";
155const char* kBlankDataImageMb = "blank_data_image_mb";
156const char* kBlankDataImageFmt = "blank_data_image_fmt";
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800157
158const char* kLogcatMode = "logcat_mode";
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800159const char* kLogcatVsockPort = "logcat_vsock_port";
160const char* kFramesVsockPort = "frames_vsock_port";
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800161const char* kLogcatReceiverBinary = "logcat_receiver_binary";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700162} // namespace
163
164namespace vsoc {
165
166std::string CuttlefishConfig::instance_dir() const {
167 return (*dictionary_)[kInstanceDir].asString();
168}
169void CuttlefishConfig::set_instance_dir(const std::string& instance_dir) {
170 (*dictionary_)[kInstanceDir] = instance_dir;
171}
172
Jorge E. Moreira88322832018-07-22 16:41:01 -0700173std::string CuttlefishConfig::vm_manager() const {
174 return (*dictionary_)[kVmManager].asString();
175}
176void CuttlefishConfig::set_vm_manager(const std::string& name) {
Ryan Haining6d821ec2018-07-27 11:39:09 -0700177 (*dictionary_)[kVmManager] = name;
Jorge E. Moreira88322832018-07-22 16:41:01 -0700178}
179
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800180std::string CuttlefishConfig::hardware_name() const {
181 return (*dictionary_)[kHardwareName].asString();
182}
183void CuttlefishConfig::set_hardware_name(const std::string& name) {
184 (*dictionary_)[kHardwareName] = name;
185}
186
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700187std::string CuttlefishConfig::serial_number() const {
188 return (*dictionary_)[kSerialNumber].asString();
189}
190void CuttlefishConfig::set_serial_number(const std::string& serial_number) {
191 (*dictionary_)[kSerialNumber] = serial_number;
192}
193
194int CuttlefishConfig::cpus() const { return (*dictionary_)[kCpus].asInt(); }
195void CuttlefishConfig::set_cpus(int cpus) { (*dictionary_)[kCpus] = cpus; }
196
197int CuttlefishConfig::memory_mb() const {
198 return (*dictionary_)[kMemoryMb].asInt();
199}
200void CuttlefishConfig::set_memory_mb(int memory_mb) {
201 (*dictionary_)[kMemoryMb] = memory_mb;
202}
203
204int CuttlefishConfig::dpi() const { return (*dictionary_)[kDpi].asInt(); }
205void CuttlefishConfig::set_dpi(int dpi) { (*dictionary_)[kDpi] = dpi; }
206
207int CuttlefishConfig::x_res() const { return (*dictionary_)[kXRes].asInt(); }
208void CuttlefishConfig::set_x_res(int x_res) { (*dictionary_)[kXRes] = x_res; }
209
210int CuttlefishConfig::y_res() const { return (*dictionary_)[kYRes].asInt(); }
211void CuttlefishConfig::set_y_res(int y_res) { (*dictionary_)[kYRes] = y_res; }
212
Cody Schuffelen5bc697c2019-01-28 21:05:29 -0800213int CuttlefishConfig::num_screen_buffers() const {
214 return (*dictionary_)[kNumScreenBuffers].asInt();
215}
216void CuttlefishConfig::set_num_screen_buffers(int num_screen_buffers) {
217 (*dictionary_)[kNumScreenBuffers] = num_screen_buffers;
218}
219
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700220int CuttlefishConfig::refresh_rate_hz() const {
221 return (*dictionary_)[kRefreshRateHz].asInt();
222}
223void CuttlefishConfig::set_refresh_rate_hz(int refresh_rate_hz) {
224 (*dictionary_)[kRefreshRateHz] = refresh_rate_hz;
225}
226
227std::string CuttlefishConfig::kernel_image_path() const {
228 return (*dictionary_)[kKernelImagePath].asString();
229}
Greg Hartman91f81422018-07-09 16:04:49 -0700230
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700231void 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. Moreira577383b2018-05-24 14:17:51 -0700238void CuttlefishConfig::set_kernel_image_path(
239 const std::string& kernel_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700240 SetPath(kKernelImagePath, kernel_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700241}
242
Cody Schuffelene13a78a2019-01-29 14:51:26 -0800243bool CuttlefishConfig::use_unpacked_kernel() const {
244 return (*dictionary_)[kUseUnpackedKernel].asBool();
245}
246
247void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) {
248 (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel;
249}
250
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800251bool CuttlefishConfig::decompress_kernel() const {
252 return (*dictionary_)[kDecompressKernel].asBool();
253}
254void CuttlefishConfig::set_decompress_kernel(bool decompress_kernel) {
255 (*dictionary_)[kDecompressKernel] = decompress_kernel;
256}
257
258std::string CuttlefishConfig::decompressed_kernel_image_path() const {
259 return (*dictionary_)[kDecompressedKernelImagePath].asString();
260}
261void CuttlefishConfig::set_decompressed_kernel_image_path(
262 const std::string& path) {
263 SetPath(kDecompressedKernelImagePath, path);
264}
265
Greg Hartman91f81422018-07-09 16:04:49 -0700266std::string CuttlefishConfig::gdb_flag() const {
267 return (*dictionary_)[kGdbFlag].asString();
268}
269
Ryan Haining6d821ec2018-07-27 11:39:09 -0700270void CuttlefishConfig::set_gdb_flag(const std::string& device) {
Matthias Maennichac14e1e2019-02-06 16:25:42 +0000271 (*dictionary_)[kGdbFlag] = device;
Greg Hartman91f81422018-07-09 16:04:49 -0700272}
273
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700274std::set<std::string> CuttlefishConfig::kernel_cmdline() const {
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700275 std::set<std::string> args_set;
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700276 auto args_json_obj = (*dictionary_)[kKernelCmdline];
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700277 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. Moreira577383b2018-05-24 14:17:51 -0700281}
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700282void CuttlefishConfig::set_kernel_cmdline(
283 const std::set<std::string>& kernel_cmdline) {
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700284 Json::Value args_json_obj(Json::arrayValue);
Chih-Hung Hsieh90c1dcc2018-12-12 13:43:34 -0800285 for (const auto& arg : kernel_cmdline) {
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700286 args_json_obj.append(arg);
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700287 }
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700288 (*dictionary_)[kKernelCmdline] = args_json_obj;
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700289}
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700290void CuttlefishConfig::add_kernel_cmdline(
291 const std::set<std::string>& extra_args) {
292 std::set<std::string> cmdline = kernel_cmdline();
Chih-Hung Hsieh90c1dcc2018-12-12 13:43:34 -0800293 for (const auto& arg : extra_args) {
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700294 if (cmdline.count(arg)) {
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700295 LOG(ERROR) << "Kernel argument " << arg << " is duplicated";
296 }
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700297 cmdline.insert(arg);
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700298 }
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700299 set_kernel_cmdline(cmdline);
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700300}
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700301void 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 Schuffelenefadb8d2018-08-10 16:10:35 -0700304 using is_iter = std::istream_iterator<std::string>;
305 std::copy(is_iter(args_stream), is_iter(),
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700306 std::inserter(kernel_cmdline_set, kernel_cmdline_set.begin()));
307 add_kernel_cmdline(kernel_cmdline_set);
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700308}
Jorge E. Moreirab2427ba2018-08-23 17:11:55 -0700309std::string CuttlefishConfig::kernel_cmdline_as_string() const {
310 auto args_set = kernel_cmdline();
Cody Schuffelenefadb8d2018-08-10 16:10:35 -0700311 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. Moreira577383b2018-05-24 14:17:51 -0700315}
316
317std::string CuttlefishConfig::ramdisk_image_path() const {
318 return (*dictionary_)[kRamdiskImagePath].asString();
319}
320void CuttlefishConfig::set_ramdisk_image_path(
321 const std::string& ramdisk_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700322 SetPath(kRamdiskImagePath, ramdisk_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700323}
324
325std::string CuttlefishConfig::system_image_path() const {
326 return (*dictionary_)[kSystemImagePath].asString();
327}
328void CuttlefishConfig::set_system_image_path(
329 const std::string& system_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700330 SetPath(kSystemImagePath, system_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700331}
332
333std::string CuttlefishConfig::cache_image_path() const {
334 return (*dictionary_)[kCacheImagePath].asString();
335}
336void CuttlefishConfig::set_cache_image_path(
337 const std::string& cache_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700338 SetPath(kCacheImagePath, cache_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700339}
340
341std::string CuttlefishConfig::data_image_path() const {
342 return (*dictionary_)[kDataImagePath].asString();
343}
344void CuttlefishConfig::set_data_image_path(const std::string& data_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700345 SetPath(kDataImagePath, data_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700346}
347
348std::string CuttlefishConfig::vendor_image_path() const {
349 return (*dictionary_)[kVendorImagePath].asString();
350}
351void CuttlefishConfig::set_vendor_image_path(
352 const std::string& vendor_image_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700353 SetPath(kVendorImagePath, vendor_image_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700354}
355
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900356std::string CuttlefishConfig::metadata_image_path() const {
357 return (*dictionary_)[kMetadataImagePath].asString();
358}
359void CuttlefishConfig::set_metadata_image_path(
360 const std::string& metadata_image_path) {
361 SetPath(kMetadataImagePath, metadata_image_path);
362}
363
Alistair Strachan054af062018-11-27 12:41:19 -0800364std::string CuttlefishConfig::super_image_path() const {
365 return (*dictionary_)[kSuperImagePath].asString();
366}
367void CuttlefishConfig::set_super_image_path(
368 const std::string& super_image_path) {
369 SetPath(kSuperImagePath, super_image_path);
370}
371
Alistair Strachan5782ce42019-03-28 22:05:13 -0700372std::string CuttlefishConfig::product_image_path() const {
373 return (*dictionary_)[kProductImagePath].asString();
374}
375void CuttlefishConfig::set_product_image_path(
376 const std::string& product_image_path) {
377 SetPath(kProductImagePath, product_image_path);
378}
379
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700380std::string CuttlefishConfig::dtb_path() const {
381 return (*dictionary_)[kDtbPath].asString();
382}
383void CuttlefishConfig::set_dtb_path(const std::string& dtb_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700384 SetPath(kDtbPath, dtb_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700385}
386
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800387std::string CuttlefishConfig::gsi_fstab_path() const {
388 return (*dictionary_)[kGsiFstabPath].asString();
389}
390void CuttlefishConfig::set_gsi_fstab_path(const std::string& path){
391 SetPath(kGsiFstabPath, path);
392}
393
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700394std::string CuttlefishConfig::mempath() const {
395 return (*dictionary_)[kMempath].asString();
396}
397void CuttlefishConfig::set_mempath(const std::string& mempath) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700398 SetPath(kMempath, mempath);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700399}
400
401std::string CuttlefishConfig::ivshmem_qemu_socket_path() const {
402 return (*dictionary_)[kIvshmemQemuSocketPath].asString();
403}
404void CuttlefishConfig::set_ivshmem_qemu_socket_path(
405 const std::string& ivshmem_qemu_socket_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700406 SetPath(kIvshmemQemuSocketPath, ivshmem_qemu_socket_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700407}
408
409std::string CuttlefishConfig::ivshmem_client_socket_path() const {
410 return (*dictionary_)[kIvshmemClientSocketPath].asString();
411}
412void CuttlefishConfig::set_ivshmem_client_socket_path(
413 const std::string& ivshmem_client_socket_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700414 SetPath(kIvshmemClientSocketPath, ivshmem_client_socket_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700415}
416
417int CuttlefishConfig::ivshmem_vector_count() const {
418 return (*dictionary_)[kIvshmemVectorCount].asInt();
419}
420void CuttlefishConfig::set_ivshmem_vector_count(int ivshmem_vector_count) {
421 (*dictionary_)[kIvshmemVectorCount] = ivshmem_vector_count;
422}
423
424std::string CuttlefishConfig::usb_v1_socket_name() const {
425 return (*dictionary_)[kUsbV1SocketName].asString();
426}
427void CuttlefishConfig::set_usb_v1_socket_name(
428 const std::string& usb_v1_socket_name) {
429 (*dictionary_)[kUsbV1SocketName] = usb_v1_socket_name;
430}
431
432int CuttlefishConfig::vhci_port() const {
433 return (*dictionary_)[kVhciPort].asInt();
434}
435void CuttlefishConfig::set_vhci_port(int vhci_port) {
436 (*dictionary_)[kVhciPort] = vhci_port;
437}
438
439std::string CuttlefishConfig::usb_ip_socket_name() const {
440 return (*dictionary_)[kUsbIpSocketName].asString();
441}
442void CuttlefishConfig::set_usb_ip_socket_name(
443 const std::string& usb_ip_socket_name) {
444 (*dictionary_)[kUsbIpSocketName] = usb_ip_socket_name;
445}
446
447std::string CuttlefishConfig::kernel_log_socket_name() const {
448 return (*dictionary_)[kKernelLogSocketName].asString();
449}
450void 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. Moreira81afca12018-07-26 16:48:49 -0700455bool CuttlefishConfig::deprecated_boot_completed() const {
456 return (*dictionary_)[kDeprecatedBootCompleted].asBool();
457}
458void CuttlefishConfig::set_deprecated_boot_completed(
459 bool deprecated_boot_completed) {
460 (*dictionary_)[kDeprecatedBootCompleted] = deprecated_boot_completed;
461}
462
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700463std::string CuttlefishConfig::console_path() const {
464 return (*dictionary_)[kConsolePath].asString();
465}
466void CuttlefishConfig::set_console_path(const std::string& console_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700467 SetPath(kConsolePath, console_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700468}
469
470std::string CuttlefishConfig::logcat_path() const {
471 return (*dictionary_)[kLogcatPath].asString();
472}
473void CuttlefishConfig::set_logcat_path(const std::string& logcat_path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700474 SetPath(kLogcatPath, logcat_path);
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700475}
476
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700477std::string CuttlefishConfig::launcher_monitor_socket_path() const {
478 return (*dictionary_)[kLauncherMonitorPath].asString();
479}
480void CuttlefishConfig::set_launcher_monitor_socket_path(
Jorge E. Moreirabe7049a2018-07-26 18:12:13 -0700481 const std::string& launcher_monitor_path) {
482 SetPath(kLauncherMonitorPath, launcher_monitor_path);
Jorge E. Moreira50a07752018-07-18 18:49:04 -0700483}
484
Jorge E. Moreira66f6ec82018-07-16 16:43:15 -0700485std::string CuttlefishConfig::launcher_log_path() const {
486 return (*dictionary_)[kLauncherLogPath].asString();
487}
488void CuttlefishConfig::set_launcher_log_path(
489 const std::string& launcher_log_path) {
490 (*dictionary_)[kLauncherLogPath] = launcher_log_path;
491}
492
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700493std::string CuttlefishConfig::mobile_bridge_name() const {
494 return (*dictionary_)[kMobileBridgeName].asString();
495}
496void CuttlefishConfig::set_mobile_bridge_name(
497 const std::string& mobile_bridge_name) {
498 (*dictionary_)[kMobileBridgeName] = mobile_bridge_name;
499}
500
501std::string CuttlefishConfig::wifi_guest_mac_addr() const {
502 return (*dictionary_)[kWifiGuestMacAddr].asString();
503}
504void CuttlefishConfig::set_wifi_guest_mac_addr(
505 const std::string& wifi_guest_mac_addr) {
506 (*dictionary_)[kWifiGuestMacAddr] = wifi_guest_mac_addr;
507}
508
509std::string CuttlefishConfig::wifi_host_mac_addr() const {
510 return (*dictionary_)[kWifiHostMacAddr].asString();
511}
512void CuttlefishConfig::set_wifi_host_mac_addr(
513 const std::string& wifi_host_mac_addr) {
514 (*dictionary_)[kWifiHostMacAddr] = wifi_host_mac_addr;
515}
516
517std::string CuttlefishConfig::mobile_tap_name() const {
518 return (*dictionary_)[kMobileTapName].asString();
519}
520void CuttlefishConfig::set_mobile_tap_name(const std::string& mobile_tap_name) {
521 (*dictionary_)[kMobileTapName] = mobile_tap_name;
522}
523
Cody Schuffelen975175a2018-06-14 18:20:02 -0700524std::string CuttlefishConfig::wifi_tap_name() const {
525 return (*dictionary_)[kWifiTapName].asString();
526}
527void CuttlefishConfig::set_wifi_tap_name(const std::string& wifi_tap_name) {
528 (*dictionary_)[kWifiTapName] = wifi_tap_name;
529}
530
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700531std::string CuttlefishConfig::entropy_source() const {
532 return (*dictionary_)[kEntropySource].asString();
533}
534void CuttlefishConfig::set_entropy_source(const std::string& entropy_source) {
535 (*dictionary_)[kEntropySource] = entropy_source;
536}
537
Cody Schuffelend946b5f2018-12-12 11:54:48 -0800538int CuttlefishConfig::vsock_guest_cid() const {
539 return (*dictionary_)[kVsockGuestCid].asInt();
540}
541
542void CuttlefishConfig::set_vsock_guest_cid(int vsock_guest_cid) {
543 (*dictionary_)[kVsockGuestCid] = vsock_guest_cid;
544}
545
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700546std::string CuttlefishConfig::uuid() const {
547 return (*dictionary_)[kUuid].asString();
548}
549void CuttlefishConfig::set_uuid(const std::string& uuid) {
550 (*dictionary_)[kUuid] = uuid;
551}
552
Greg Hartman6abdbb92018-05-24 09:49:00 -0700553void CuttlefishConfig::set_cuttlefish_env_path(const std::string& path) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700554 SetPath(kCuttlefishEnvPath, path);
Greg Hartman6abdbb92018-05-24 09:49:00 -0700555}
556std::string CuttlefishConfig::cuttlefish_env_path() const {
557 return (*dictionary_)[kCuttlefishEnvPath].asString();
558}
559
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800560std::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 Haining4a1abea2018-07-10 16:20:19 -0700566}
567
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800568void 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 Haining4a1abea2018-07-10 16:20:19 -0700574}
575
Ryan Haining7338d352018-10-24 17:41:52 -0700576std::string CuttlefishConfig::adb_ip_and_port() const {
577 return (*dictionary_)[kAdbIPAndPort].asString();
578}
579
580void CuttlefishConfig::set_adb_ip_and_port(const std::string& ip_port) {
581 (*dictionary_)[kAdbIPAndPort] = ip_port;
582}
583
584std::string CuttlefishConfig::adb_device_name() const {
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800585 // 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 Haining7338d352018-10-24 17:41:52 -0700591 return adb_ip_and_port();
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800592 } else if (adb_mode().count("usb") > 0) {
Ryan Haining7338d352018-10-24 17:41:52 -0700593 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 Haining6d821ec2018-07-27 11:39:09 -0700599std::string CuttlefishConfig::device_title() const {
600 return (*dictionary_)[kDeviceTitle].asString();
601}
602
603void CuttlefishConfig::set_device_title(const std::string& title) {
604 (*dictionary_)[kDeviceTitle] = title;
605}
606
Greg Hartmanf1f28c52018-09-20 17:19:19 -0700607std::string CuttlefishConfig::setupwizard_mode() const {
608 return (*dictionary_)[kSetupWizardMode].asString();
609}
610
611void CuttlefishConfig::set_setupwizard_mode(const std::string& mode) {
612 (*dictionary_)[kSetupWizardMode] = mode;
613}
614
Cody Schuffelen58980032018-10-11 17:11:13 -0700615std::string CuttlefishConfig::qemu_binary() const {
616 return (*dictionary_)[kQemuBinary].asString();
617}
618
619void CuttlefishConfig::set_qemu_binary(const std::string& qemu_binary) {
620 (*dictionary_)[kQemuBinary] = qemu_binary;
621}
622
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800623std::string CuttlefishConfig::crosvm_binary() const {
624 return (*dictionary_)[kCrosvmBinary].asString();
625}
626
627void CuttlefishConfig::set_crosvm_binary(const std::string& crosvm_binary) {
628 (*dictionary_)[kCrosvmBinary] = crosvm_binary;
629}
630
Cody Schuffelen5bc697c2019-01-28 21:05:29 -0800631std::string CuttlefishConfig::ivserver_binary() const {
632 return (*dictionary_)[kIvServerBinary].asString();
633}
634
635void CuttlefishConfig::set_ivserver_binary(const std::string& ivserver_binary) {
636 (*dictionary_)[kIvServerBinary] = ivserver_binary;
637}
638
Cody Schuffelen55676ca2019-01-28 22:00:05 -0800639std::string CuttlefishConfig::kernel_log_monitor_binary() const {
640 return (*dictionary_)[kKernelLogMonitorBinary].asString();
641}
642
643void CuttlefishConfig::set_kernel_log_monitor_binary(
644 const std::string& kernel_log_monitor_binary) {
645 (*dictionary_)[kKernelLogMonitorBinary] = kernel_log_monitor_binary;
646}
647
Cody Schuffelen17b34722019-01-28 22:57:14 -0800648bool CuttlefishConfig::enable_vnc_server() const {
649 return (*dictionary_)[kEnableVncServer].asBool();
650}
651
652void CuttlefishConfig::set_enable_vnc_server(bool enable_vnc_server) {
653 (*dictionary_)[kEnableVncServer] = enable_vnc_server;
654}
655
656std::string CuttlefishConfig::vnc_server_binary() const {
657 return (*dictionary_)[kVncServerBinary].asString();
658}
659
660void CuttlefishConfig::set_vnc_server_binary(
661 const std::string& vnc_server_binary) {
662 (*dictionary_)[kVncServerBinary] = vnc_server_binary;
663}
664
665int CuttlefishConfig::vnc_server_port() const {
666 return (*dictionary_)[kVncServerPort].asInt();
667}
668
669void CuttlefishConfig::set_vnc_server_port(int vnc_server_port) {
670 (*dictionary_)[kVncServerPort] = vnc_server_port;
671}
672
673bool CuttlefishConfig::enable_stream_audio() const {
674 return (*dictionary_)[kEnableStreamAudio].asBool();
675}
676
677void CuttlefishConfig::set_enable_stream_audio(bool enable_stream_audio) {
678 (*dictionary_)[kEnableStreamAudio] = enable_stream_audio;
679}
680
681std::string CuttlefishConfig::stream_audio_binary() const {
682 return (*dictionary_)[kStreamAudioBinary].asString();
683}
684
685void CuttlefishConfig::set_stream_audio_binary(
686 const std::string& stream_audio_binary) {
687 (*dictionary_)[kStreamAudioBinary] = stream_audio_binary;
688}
689
690int CuttlefishConfig::stream_audio_port() const {
691 return (*dictionary_)[kStreamAudioPort].asInt();
692}
693
694void CuttlefishConfig::set_stream_audio_port(int stream_audio_port) {
695 (*dictionary_)[kStreamAudioPort] = stream_audio_port;
696}
697
Cody Schuffelend12e0c92019-01-29 15:44:45 -0800698bool CuttlefishConfig::restart_subprocesses() const {
699 return (*dictionary_)[kRestartSubprocesses].asBool();
700}
701
702void CuttlefishConfig::set_restart_subprocesses(bool restart_subprocesses) {
703 (*dictionary_)[kRestartSubprocesses] = restart_subprocesses;
704}
705
Cody Schuffelen1f08d972019-01-29 16:21:12 -0800706bool CuttlefishConfig::run_adb_connector() const {
Jorge E. Moreirab86922b2019-02-04 10:52:58 -0800707 return (*dictionary_)[kRunAdbConnector].asBool();
Cody Schuffelen1f08d972019-01-29 16:21:12 -0800708}
709
710void CuttlefishConfig::set_run_adb_connector(bool run_adb_connector) {
711 (*dictionary_)[kRunAdbConnector] = run_adb_connector;
712}
713
714std::string CuttlefishConfig::adb_connector_binary() const {
715 return (*dictionary_)[kAdbConnectorBinary].asString();
716}
717
718void CuttlefishConfig::set_adb_connector_binary(
719 const std::string& adb_connector_binary) {
720 (*dictionary_)[kAdbConnectorBinary] = adb_connector_binary;
721}
722
723std::string CuttlefishConfig::virtual_usb_manager_binary() const {
724 return (*dictionary_)[kVirtualUsbManagerBinary].asString();
725}
726
727void CuttlefishConfig::set_virtual_usb_manager_binary(
728 const std::string& virtual_usb_manager_binary) {
729 (*dictionary_)[kVirtualUsbManagerBinary] = virtual_usb_manager_binary;
730}
731
732std::string CuttlefishConfig::socket_forward_proxy_binary() const {
733 return (*dictionary_)[kSocketForwardProxyBinary].asString();
734}
735
736void CuttlefishConfig::set_socket_forward_proxy_binary(
737 const std::string& socket_forward_proxy_binary) {
738 (*dictionary_)[kSocketForwardProxyBinary] = socket_forward_proxy_binary;
739}
740
741std::string CuttlefishConfig::socket_vsock_proxy_binary() const {
742 return (*dictionary_)[kSocketVsockProxyBinary].asString();
743}
744
745void CuttlefishConfig::set_socket_vsock_proxy_binary(
746 const std::string& socket_vsock_proxy_binary) {
747 (*dictionary_)[kSocketVsockProxyBinary] = socket_vsock_proxy_binary;
748}
749
Cody Schuffelen50249732019-01-29 16:42:55 -0800750bool CuttlefishConfig::run_as_daemon() const {
751 return (*dictionary_)[kRunAsDaemon].asBool();
752}
753
754void CuttlefishConfig::set_run_as_daemon(bool run_as_daemon) {
755 (*dictionary_)[kRunAsDaemon] = run_as_daemon;
756}
757
Cody Schuffelen3a640952019-01-29 17:14:41 -0800758bool CuttlefishConfig::run_e2e_test() const {
759 return (*dictionary_)[kRunE2eTest].asBool();
760}
761
762void CuttlefishConfig::set_run_e2e_test(bool run_e2e_test) {
763 (*dictionary_)[kRunE2eTest] = run_e2e_test;
764}
765
766std::string CuttlefishConfig::e2e_test_binary() const {
767 return (*dictionary_)[kE2eTestBinary].asString();
768}
769
770void CuttlefishConfig::set_e2e_test_binary(const std::string& e2e_test_binary) {
771 (*dictionary_)[kE2eTestBinary] = e2e_test_binary;
772}
773
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800774std::string CuttlefishConfig::data_policy() const {
775 return (*dictionary_)[kDataPolicy].asString();
776}
777
778void CuttlefishConfig::set_data_policy(const std::string& data_policy) {
779 (*dictionary_)[kDataPolicy] = data_policy;
780}
781
782int CuttlefishConfig::blank_data_image_mb() const {
Cody Schuffelene3269ed2019-02-11 18:38:42 -0800783 return (*dictionary_)[kBlankDataImageMb].asInt();
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800784}
785
786void CuttlefishConfig::set_blank_data_image_mb(int blank_data_image_mb) {
787 (*dictionary_)[kBlankDataImageMb] = blank_data_image_mb;
788}
789
790std::string CuttlefishConfig::blank_data_image_fmt() const {
791 return (*dictionary_)[kBlankDataImageFmt].asString();
792}
793
794void CuttlefishConfig::set_blank_data_image_fmt(const std::string& blank_data_image_fmt) {
795 (*dictionary_)[kBlankDataImageFmt] = blank_data_image_fmt;
796}
797
Jorge E. Moreiraf4b202b2019-02-05 17:51:06 -0800798
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800799void CuttlefishConfig::set_logcat_mode(const std::string& mode) {
800 (*dictionary_)[kLogcatMode] = mode;
801}
802
803std::string CuttlefishConfig::logcat_mode() const {
804 return (*dictionary_)[kLogcatMode].asString();
805}
806
807void CuttlefishConfig::set_logcat_vsock_port(int port) {
808 (*dictionary_)[kLogcatVsockPort] = port;
809}
810
811int CuttlefishConfig::logcat_vsock_port() const {
812 return (*dictionary_)[kLogcatVsockPort].asInt();
813}
814
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800815void CuttlefishConfig::set_frames_vsock_port(int port) {
816 (*dictionary_)[kFramesVsockPort] = port;
817}
818
819int CuttlefishConfig::frames_vsock_port() const {
820 return (*dictionary_)[kFramesVsockPort].asInt();
821}
822
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800823void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) {
824 SetPath(kLogcatReceiverBinary, binary);
825}
826
827std::string CuttlefishConfig::logcat_receiver_binary() const {
828 return (*dictionary_)[kLogcatReceiverBinary].asString();
829}
830
Jorge E. Moreiraf4b202b2019-02-05 17:51:06 -0800831bool CuttlefishConfig::enable_ivserver() const {
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800832 return hardware_name() == "cutf_ivsh";
Jorge E. Moreiraf4b202b2019-02-05 17:51:06 -0800833}
834
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800835std::string CuttlefishConfig::touch_socket_path() const {
836 return PerInstancePath("touch.sock");
837}
838
839std::string CuttlefishConfig::keyboard_socket_path() const {
840 return PerInstancePath("keyboard.sock");
841}
842
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700843// Creates the (initially empty) config object and populates it with values from
Cody Schuffelenf219a162018-10-11 19:01:57 -0700844// the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700845// Returns nullptr if there was an error loading from file
846/*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() {
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700847 auto config_file_path = cvd::StringFromEnv(kCuttlefishConfigEnvVarName,
848 vsoc::GetGlobalConfigFileLink());
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700849 auto ret = new CuttlefishConfig();
Jorge E. Moreiraa00584e2018-10-25 15:07:52 -0700850 if (ret) {
851 auto loaded = ret->LoadFromFile(config_file_path.c_str());
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700852 if (!loaded) {
Cody Schuffelenf219a162018-10-11 19:01:57 -0700853 delete ret;
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700854 return nullptr;
855 }
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700856 }
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700857 return ret;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700858}
859
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700860/*static*/ CuttlefishConfig* CuttlefishConfig::Get() {
861 static std::shared_ptr<CuttlefishConfig> config(BuildConfigImpl());
862 return config.get();
863}
864
865CuttlefishConfig::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
868CuttlefishConfig::~CuttlefishConfig() {}
869
870bool CuttlefishConfig::LoadFromFile(const char* file) {
Ryan Hainingd3f185d2018-07-19 12:11:47 -0700871 auto real_file_path = cvd::AbsolutePath(file);
Ryan Haining10e42312018-07-17 12:11:52 -0700872 if (real_file_path.empty()) {
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700873 LOG(ERROR) << "Could not get real path for file " << file;
874 return false;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700875 }
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700876 Json::Reader reader;
877 std::ifstream ifs(real_file_path);
878 if (!reader.parse(ifs, *dictionary_)) {
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700879 LOG(ERROR) << "Could not read config file " << file << ": "
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700880 << reader.getFormattedErrorMessages();
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700881 return false;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700882 }
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700883 return true;
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700884}
885bool 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
895std::string CuttlefishConfig::PerInstancePath(const char* file_name) const {
896 return (instance_dir() + "/") + file_name;
897}
898
899std::string CuttlefishConfig::instance_name() const {
900 return GetPerInstanceDefault("cvd-");
901}
902
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700903int GetInstance() {
904 static int instance_id = InstanceFromEnvironment();
905 return instance_id;
906}
907
Jorge E. Moreira015c0002018-07-19 11:01:16 -0700908std::string GetGlobalConfigFileLink() {
909 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish_config.json";
910}
911
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700912std::string GetDomain() {
913 return CuttlefishConfig::Get()->ivshmem_client_socket_path();
914}
915
916std::string GetPerInstanceDefault(const char* prefix) {
917 std::ostringstream stream;
918 stream << prefix << std::setfill('0') << std::setw(2) << GetInstance();
919 return stream.str();
920}
921int GetPerInstanceDefault(int base) { return base + GetInstance() - 1; }
922
923std::string GetDefaultPerInstanceDir() {
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700924 std::ostringstream stream;
Jorge E. Moreirad9668372019-01-28 22:40:42 -0800925 stream << std::getenv("HOME") << "/cuttlefish_runtime";
Jorge E. Moreira577383b2018-05-24 14:17:51 -0700926 return stream.str();
927}
928
Jorge E. Moreira077d3002018-07-20 11:43:05 -0700929std::string GetDefaultMempath() {
930 return GetPerInstanceDefault("/var/run/shm/cvd-");
931}
932
Cody Schuffelend946b5f2018-12-12 11:54:48 -0800933int GetDefaultPerInstanceVsockCid() {
934 constexpr int kFirstGuestCid = 3;
935 return vsoc::HostSupportsVsock() ? GetPerInstanceDefault(kFirstGuestCid) : 0;
936}
937
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -0700938std::string DefaultHostArtifactsPath(const std::string& file_name) {
939 return (cvd::StringFromEnv("ANDROID_HOST_OUT",
940 cvd::StringFromEnv("HOME", ".")) +
941 "/") +
942 file_name;
943}
944
945std::string DefaultGuestImagePath(const std::string& file_name) {
946 return (cvd::StringFromEnv("ANDROID_PRODUCT_OUT",
Ryan Haining6d821ec2018-07-27 11:39:09 -0700947 cvd::StringFromEnv("HOME", ".")) +
Jorge E. Moreiraa8142f92018-06-13 17:33:55 -0700948 "/") +
949 file_name;
950}
951
952bool 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 Schuffelend946b5f2018-12-12 11:54:48 -0800958
959bool 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. Moreira577383b2018-05-24 14:17:51 -0700965} // namespace vsoc