• 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 #pragma once
17 
18 #include <memory>
19 #include <string>
20 #include <set>
21 
22 namespace Json {
23 class Value;
24 }
25 
26 namespace vsoc {
27 
28 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1";
29 constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
30 constexpr char kVsocUserPrefix[] = "vsoc-";
31 
32 // Holds the configuration of the cuttlefish instances.
33 class CuttlefishConfig {
34  public:
35   static CuttlefishConfig* Get();
36 
37   CuttlefishConfig();
38   ~CuttlefishConfig();
39 
40   // Saves the configuration object in a file, it can then be read in other
41   // processes by passing the --config_file option.
42   bool SaveToFile(const std::string& file) const;
43 
44   // Returns the path to a file with the given name in the instance directory..
45   std::string PerInstancePath(const char* file_name) const;
46 
47   std::string instance_name() const;
48 
disable_usb_adb()49   void disable_usb_adb() {
50     // This seems to be the way usb is being disbled in the launcher
51     set_usb_v1_socket_name("");
52   }
53 
54   std::string instance_dir() const;
55   void set_instance_dir(const std::string& instance_dir);
56 
57   std::string vm_manager() const;
58   void set_vm_manager(const std::string& name);
59 
60   std::string hardware_name() const;
61   void set_hardware_name(const std::string& name);
62 
63   std::string serial_number() const;
64   void set_serial_number(const std::string& serial_number);
65 
66   int cpus() const;
67   void set_cpus(int cpus);
68 
69   int memory_mb() const;
70   void set_memory_mb(int memory_mb);
71 
72   int dpi() const;
73   void set_dpi(int dpi);
74 
75   int x_res() const;
76   void set_x_res(int x_res);
77 
78   int y_res() const;
79   void set_y_res(int y_res);
80 
81   int num_screen_buffers() const;
82   void set_num_screen_buffers(int num_screen_buffers);
83 
84   int refresh_rate_hz() const;
85   void set_refresh_rate_hz(int refresh_rate_hz);
86 
87   // Returns kernel image extracted from the boot image or the user-provided one
88   // if given by command line to the launcher. This function should not be used
89   // to get the kernel image the vmm should boot, GetKernelImageToUse() should
90   // be used instead.
91   std::string kernel_image_path() const;
92   void set_kernel_image_path(const std::string& kernel_image_path);
93 
94   bool decompress_kernel() const;
95   void set_decompress_kernel(bool decompress_kernel);
96 
97   // Returns the path to the kernel image that should be given to the vm manager
98   // to boot, takes into account whether the original image was decompressed or
99   // not.
GetKernelImageToUse()100   std::string GetKernelImageToUse() const {
101     return decompress_kernel() ? decompressed_kernel_image_path()
102                                : kernel_image_path();
103   }
104 
105   std::string decompressed_kernel_image_path() const;
106   void set_decompressed_kernel_image_path(const std::string& path);
107 
108   bool use_unpacked_kernel() const;
109   void set_use_unpacked_kernel(bool use_unpacked_kernel);
110 
111   std::set<std::string> kernel_cmdline() const;
112   void set_kernel_cmdline(const std::set<std::string>& kernel_cmdline);
113   void add_kernel_cmdline(const std::string& arg);
114   void add_kernel_cmdline(const std::set<std::string>& kernel_cmdline);
115   std::string kernel_cmdline_as_string() const;
116 
117   std::string gdb_flag() const;
118   void set_gdb_flag(const std::string& gdb);
119 
120   std::string ramdisk_image_path() const;
121   void set_ramdisk_image_path(const std::string& ramdisk_image_path);
122 
123   std::string system_image_path() const;
124   void set_system_image_path(const std::string& system_image_path);
125 
126   std::string cache_image_path() const;
127   void set_cache_image_path(const std::string& cache_image_path);
128 
129   std::string data_image_path() const;
130   void set_data_image_path(const std::string& data_image_path);
131 
132   std::string vendor_image_path() const;
133   void set_vendor_image_path(const std::string& vendor_image_path);
134 
135   std::string metadata_image_path() const;
136   void set_metadata_image_path(const std::string& metadata_image_path);
137 
138   std::string product_image_path() const;
139   void set_product_image_path(const std::string& product_image_path);
140 
141   std::string super_image_path() const;
142   void set_super_image_path(const std::string& super_image_path);
143 
144   std::string dtb_path() const;
145   void set_dtb_path(const std::string& dtb_path);
146 
147   std::string gsi_fstab_path() const;
148   void set_gsi_fstab_path(const std::string& path);
149 
150   std::string mempath() const;
151   void set_mempath(const std::string& mempath);
152 
153   std::string ivshmem_qemu_socket_path() const;
154   void set_ivshmem_qemu_socket_path(
155       const std::string& ivshmem_qemu_socket_path);
156 
157   std::string ivshmem_client_socket_path() const;
158   void set_ivshmem_client_socket_path(
159       const std::string& ivshmem_client_socket_path);
160 
161   int ivshmem_vector_count() const;
162   void set_ivshmem_vector_count(int ivshmem_vector_count);
163 
164   // The name of the socket that will be used to forward access to USB gadget.
165   // This is for V1 of the USB bus.
166   std::string usb_v1_socket_name() const;
167   void set_usb_v1_socket_name(const std::string& usb_v1_socket_name);
168 
169   int vhci_port() const;
170   void set_vhci_port(int vhci_port);
171 
172   std::string usb_ip_socket_name() const;
173   void set_usb_ip_socket_name(const std::string& usb_ip_socket_name);
174 
175   std::string kernel_log_socket_name() const;
176   void set_kernel_log_socket_name(const std::string& kernel_log_socket_name);
177 
178   bool deprecated_boot_completed() const;
179   void set_deprecated_boot_completed(bool deprecated_boot_completed);
180 
181   std::string console_path() const;
182   void set_console_path(const std::string& console_path);
183 
184   std::string logcat_path() const;
185   void set_logcat_path(const std::string& logcat_path);
186 
187   std::string logcat_receiver_binary() const;
188   void set_logcat_receiver_binary(const std::string& binary);
189 
190   std::string launcher_log_path() const;
191   void set_launcher_log_path(const std::string& launcher_log_path);
192 
193   std::string launcher_monitor_socket_path() const;
194   void set_launcher_monitor_socket_path(
195       const std::string& launhcer_monitor_path);
196 
197   std::string mobile_bridge_name() const;
198   void set_mobile_bridge_name(const std::string& mobile_bridge_name);
199 
200   std::string mobile_tap_name() const;
201   void set_mobile_tap_name(const std::string& mobile_tap_name);
202 
203   std::string wifi_tap_name() const;
204   void set_wifi_tap_name(const std::string& wifi_tap_name);
205 
206   std::string wifi_guest_mac_addr() const;
207   void set_wifi_guest_mac_addr(const std::string& wifi_guest_mac_addr);
208 
209   std::string wifi_host_mac_addr() const;
210   void set_wifi_host_mac_addr(const std::string& wifi_host_mac_addr);
211 
212   std::string entropy_source() const;
213   void set_entropy_source(const std::string& entropy_source);
214 
215   void set_vsock_guest_cid(int vsock_guest_cid);
216   int vsock_guest_cid() const;
217 
218   std::string uuid() const;
219   void set_uuid(const std::string& uuid);
220 
221   void set_cuttlefish_env_path(const std::string& path);
222   std::string cuttlefish_env_path() const;
223 
224   void set_adb_mode(const std::set<std::string>& modes);
225   std::set<std::string> adb_mode() const;
226 
227   void set_adb_ip_and_port(const std::string& ip_port);
228   std::string adb_ip_and_port() const;
229 
230   std::string adb_device_name() const;
231 
232   void set_device_title(const std::string& title);
233   std::string device_title() const;
234 
235   void set_setupwizard_mode(const std::string& title);
236   std::string setupwizard_mode() const;
237 
238   void set_qemu_binary(const std::string& qemu_binary);
239   std::string qemu_binary() const;
240 
241   void set_crosvm_binary(const std::string& crosvm_binary);
242   std::string crosvm_binary() const;
243 
244   void set_ivserver_binary(const std::string& ivserver_binary);
245   std::string ivserver_binary() const;
246 
247   void set_kernel_log_monitor_binary(
248       const std::string& kernel_log_monitor_binary);
249   std::string kernel_log_monitor_binary() const;
250 
251   void set_enable_vnc_server(bool enable_vnc_server);
252   bool enable_vnc_server() const;
253 
254   void set_vnc_server_port(int vnc_server_port);
255   int vnc_server_port() const;
256 
257   void set_vnc_server_binary(const std::string& vnc_server_binary);
258   std::string vnc_server_binary() const;
259 
260   void set_enable_stream_audio(bool enable_stream_audio);
261   bool enable_stream_audio() const;
262 
263   void set_stream_audio_port(int stream_audio_port);
264   int stream_audio_port() const;
265 
266   void set_stream_audio_binary(const std::string& stream_audio_binary);
267   std::string stream_audio_binary() const;
268 
269   void set_restart_subprocesses(bool restart_subprocesses);
270   bool restart_subprocesses() const;
271 
272   void set_run_adb_connector(bool run_adb_connector);
273   bool run_adb_connector() const;
274 
275   void set_adb_connector_binary(const std::string& adb_connector_binary);
276   std::string adb_connector_binary() const;
277 
278   void set_virtual_usb_manager_binary(const std::string& binary);
279   std::string virtual_usb_manager_binary() const;
280 
281   void set_socket_forward_proxy_binary(const std::string& binary);
282   std::string socket_forward_proxy_binary() const;
283 
284   void set_socket_vsock_proxy_binary(const std::string& binary);
285   std::string socket_vsock_proxy_binary() const;
286 
287   void set_run_as_daemon(bool run_as_daemon);
288   bool run_as_daemon() const;
289 
290   void set_run_e2e_test(bool run_e2e_test);
291   bool run_e2e_test() const;
292 
293   void set_e2e_test_binary(const std::string& e2e_test_binary);
294   std::string e2e_test_binary() const;
295 
296   void set_data_policy(const std::string& data_policy);
297   std::string data_policy() const;
298 
299   void set_blank_data_image_mb(int blank_data_image_mb);
300   int blank_data_image_mb() const;
301 
302   void set_blank_data_image_fmt(const std::string& blank_data_image_fmt);
303   std::string blank_data_image_fmt() const;
304 
305   void set_logcat_mode(const std::string& mode);
306   std::string logcat_mode() const;
307 
308   void set_logcat_vsock_port(int port);
309   int logcat_vsock_port() const;
310 
311   void set_frames_vsock_port(int port);
312   int frames_vsock_port() const;
313 
314   bool enable_ivserver() const;
315 
316   std::string touch_socket_path() const;
317   std::string keyboard_socket_path() const;
318 
319  private:
320   std::unique_ptr<Json::Value> dictionary_;
321 
322   void SetPath(const std::string& key, const std::string& path);
323   bool LoadFromFile(const char* file);
324   static CuttlefishConfig* BuildConfigImpl();
325 
326   CuttlefishConfig(const CuttlefishConfig&) = delete;
327   CuttlefishConfig& operator=(const CuttlefishConfig&) = delete;
328 };
329 
330 // Returns the instance number as obtained from the CUTTLEFISH_INSTANCE
331 // environment variable or the username.
332 int GetInstance();
333 // Returns a path where the launhcer puts a link to the config file which makes
334 // it easily discoverable regardless of what vm manager is in use
335 std::string GetGlobalConfigFileLink();
336 
337 // Returns the path to the ivserver's client socket.
338 std::string GetDomain();
339 
340 // These functions modify a given base value to make it different accross
341 // different instances by appending the instance id in case of strings or adding
342 // it in case of integers.
343 std::string GetPerInstanceDefault(const char* prefix);
344 int GetPerInstanceDefault(int base);
345 
346 std::string GetDefaultPerInstanceDir();
347 std::string GetDefaultMempath();
348 int GetDefaultPerInstanceVsockCid();
349 
350 std::string DefaultHostArtifactsPath(const std::string& file);
351 std::string DefaultGuestImagePath(const std::string& file);
352 
353 // Whether the host supports qemu
354 bool HostSupportsQemuCli();
355 bool HostSupportsVsock();
356 }  // namespace vsoc
357