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 <android-base/logging.h>
20 #include <json/json.h>
21 
22 #include "common/libs/utils/files.h"
23 
24 namespace cuttlefish {
25 namespace {
26 
27 const char* kInstances = "instances";
28 const char* kSerialNumber = "serial_number";
29 const char* kInstanceDir = "instance_dir";
30 const char* kDeviceTitle = "device_title";
31 
32 const char* kVirtualDiskPaths = "virtual_disk_paths";
33 
34 const char* kMobileBridgeName = "mobile_bridge_name";
35 const char* kMobileTapName = "mobile_tap_name";
36 const char* kWifiTapName = "wifi_tap_name";
37 const char* kVsockGuestCid = "vsock_guest_cid";
38 
39 const char* kUuid = "uuid";
40 const char* kModemSimulatorPorts = "modem_simulator_ports";
41 
42 const char* kHostPort = "host_port";
43 const char* kTpmPort = "tpm_port";
44 const char* kAdbIPAndPort = "adb_ip_and_port";
45 
46 const char* kConfigServerPort = "config_server_port";
47 const char* kVncServerPort = "vnc_server_port";
48 const char* kTombstoneReceiverPort = "tombstone_receiver_port";
49 
50 const char* kWebrtcDeviceId = "webrtc_device_id";
51 const char* kStartSigServer = "webrtc_start_sig_server";
52 
53 const char* kFramesServerPort = "frames_server_port";
54 const char* kTouchServerPort = "touch_server_port";
55 const char* kKeyboardServerPort = "keyboard_server_port";
56 
57 const char* kKeymasterVsockPort = "keymaster_vsock_port";
58 const char* kGatekeeperVsockPort = "gatekeeper_vsock_port";
59 const char* kWifiMacAddress = "wifi_mac_address";
60 
61 }  // namespace
62 
Dictionary()63 Json::Value* CuttlefishConfig::MutableInstanceSpecific::Dictionary() {
64   return &(*config_->dictionary_)[kInstances][id_];
65 }
66 
Dictionary() const67 const Json::Value* CuttlefishConfig::InstanceSpecific::Dictionary() const {
68   return &(*config_->dictionary_)[kInstances][id_];
69 }
70 
instance_dir() const71 std::string CuttlefishConfig::InstanceSpecific::instance_dir() const {
72   return (*Dictionary())[kInstanceDir].asString();
73 }
set_instance_dir(const std::string & instance_dir)74 void CuttlefishConfig::MutableInstanceSpecific::set_instance_dir(
75     const std::string& instance_dir) {
76   (*Dictionary())[kInstanceDir] = instance_dir;
77 }
78 
instance_internal_dir() const79 std::string CuttlefishConfig::InstanceSpecific::instance_internal_dir() const {
80   return PerInstancePath(kInternalDirName);
81 }
82 
serial_number() const83 std::string CuttlefishConfig::InstanceSpecific::serial_number() const {
84   return (*Dictionary())[kSerialNumber].asString();
85 }
set_serial_number(const std::string & serial_number)86 void CuttlefishConfig::MutableInstanceSpecific::set_serial_number(
87     const std::string& serial_number) {
88   (*Dictionary())[kSerialNumber] = serial_number;
89 }
90 
virtual_disk_paths() const91 std::vector<std::string> CuttlefishConfig::InstanceSpecific::virtual_disk_paths() const {
92   std::vector<std::string> virtual_disks;
93   auto virtual_disks_json_obj = (*Dictionary())[kVirtualDiskPaths];
94   for (const auto& disk : virtual_disks_json_obj) {
95     virtual_disks.push_back(disk.asString());
96   }
97   return virtual_disks;
98 }
set_virtual_disk_paths(const std::vector<std::string> & virtual_disk_paths)99 void CuttlefishConfig::MutableInstanceSpecific::set_virtual_disk_paths(
100     const std::vector<std::string>& virtual_disk_paths) {
101   Json::Value virtual_disks_json_obj(Json::arrayValue);
102   for (const auto& arg : virtual_disk_paths) {
103     virtual_disks_json_obj.append(arg);
104   }
105   (*Dictionary())[kVirtualDiskPaths] = virtual_disks_json_obj;
106 }
107 
kernel_log_pipe_name() const108 std::string CuttlefishConfig::InstanceSpecific::kernel_log_pipe_name() const {
109   return cuttlefish::AbsolutePath(PerInstanceInternalPath("kernel-log-pipe"));
110 }
111 
console_in_pipe_name() const112 std::string CuttlefishConfig::InstanceSpecific::console_in_pipe_name() const {
113   return cuttlefish::AbsolutePath(PerInstanceInternalPath("console-in-pipe"));
114 }
115 
console_out_pipe_name() const116 std::string CuttlefishConfig::InstanceSpecific::console_out_pipe_name() const {
117   return cuttlefish::AbsolutePath(PerInstanceInternalPath("console-out-pipe"));
118 }
119 
logcat_pipe_name() const120 std::string CuttlefishConfig::InstanceSpecific::logcat_pipe_name() const {
121   return cuttlefish::AbsolutePath(PerInstanceInternalPath("logcat-pipe"));
122 }
123 
access_kregistry_path() const124 std::string CuttlefishConfig::InstanceSpecific::access_kregistry_path() const {
125   return cuttlefish::AbsolutePath(PerInstancePath("access-kregistry"));
126 }
127 
pstore_path() const128 std::string CuttlefishConfig::InstanceSpecific::pstore_path() const {
129   return cuttlefish::AbsolutePath(PerInstancePath("pstore"));
130 }
131 
console_path() const132 std::string CuttlefishConfig::InstanceSpecific::console_path() const {
133   return cuttlefish::AbsolutePath(PerInstancePath("console"));
134 }
135 
logcat_path() const136 std::string CuttlefishConfig::InstanceSpecific::logcat_path() const {
137   return cuttlefish::AbsolutePath(PerInstancePath("logcat"));
138 }
139 
launcher_monitor_socket_path() const140 std::string CuttlefishConfig::InstanceSpecific::launcher_monitor_socket_path()
141     const {
142   return cuttlefish::AbsolutePath(PerInstancePath("launcher_monitor.sock"));
143 }
144 
modem_simulator_ports() const145 std::string CuttlefishConfig::InstanceSpecific::modem_simulator_ports() const {
146   return (*Dictionary())[kModemSimulatorPorts].asString();
147 }
148 
set_modem_simulator_ports(const std::string & modem_simulator_ports)149 void CuttlefishConfig::MutableInstanceSpecific::set_modem_simulator_ports(
150     const std::string& modem_simulator_ports) {
151   (*Dictionary())[kModemSimulatorPorts] = modem_simulator_ports;
152 }
153 
launcher_log_path() const154 std::string CuttlefishConfig::InstanceSpecific::launcher_log_path() const {
155   return cuttlefish::AbsolutePath(PerInstancePath("launcher.log"));
156 }
157 
sdcard_path() const158 std::string CuttlefishConfig::InstanceSpecific::sdcard_path() const {
159   return cuttlefish::AbsolutePath(PerInstancePath("sdcard.img"));
160 }
161 
mobile_bridge_name() const162 std::string CuttlefishConfig::InstanceSpecific::mobile_bridge_name() const {
163   return (*Dictionary())[kMobileBridgeName].asString();
164 }
set_mobile_bridge_name(const std::string & mobile_bridge_name)165 void CuttlefishConfig::MutableInstanceSpecific::set_mobile_bridge_name(
166     const std::string& mobile_bridge_name) {
167   (*Dictionary())[kMobileBridgeName] = mobile_bridge_name;
168 }
169 
mobile_tap_name() const170 std::string CuttlefishConfig::InstanceSpecific::mobile_tap_name() const {
171   return (*Dictionary())[kMobileTapName].asString();
172 }
set_mobile_tap_name(const std::string & mobile_tap_name)173 void CuttlefishConfig::MutableInstanceSpecific::set_mobile_tap_name(
174     const std::string& mobile_tap_name) {
175   (*Dictionary())[kMobileTapName] = mobile_tap_name;
176 }
177 
wifi_tap_name() const178 std::string CuttlefishConfig::InstanceSpecific::wifi_tap_name() const {
179   return (*Dictionary())[kWifiTapName].asString();
180 }
set_wifi_tap_name(const std::string & wifi_tap_name)181 void CuttlefishConfig::MutableInstanceSpecific::set_wifi_tap_name(
182     const std::string& wifi_tap_name) {
183   (*Dictionary())[kWifiTapName] = wifi_tap_name;
184 }
185 
vsock_guest_cid() const186 int CuttlefishConfig::InstanceSpecific::vsock_guest_cid() const {
187   return (*Dictionary())[kVsockGuestCid].asInt();
188 }
189 
set_vsock_guest_cid(int vsock_guest_cid)190 void CuttlefishConfig::MutableInstanceSpecific::set_vsock_guest_cid(
191     int vsock_guest_cid) {
192   (*Dictionary())[kVsockGuestCid] = vsock_guest_cid;
193 }
194 
uuid() const195 std::string CuttlefishConfig::InstanceSpecific::uuid() const {
196   return (*Dictionary())[kUuid].asString();
197 }
set_uuid(const std::string & uuid)198 void CuttlefishConfig::MutableInstanceSpecific::set_uuid(const std::string& uuid) {
199   (*Dictionary())[kUuid] = uuid;
200 }
201 
host_port() const202 int CuttlefishConfig::InstanceSpecific::host_port() const {
203   return (*Dictionary())[kHostPort].asInt();
204 }
205 
set_host_port(int host_port)206 void CuttlefishConfig::MutableInstanceSpecific::set_host_port(int host_port) {
207   (*Dictionary())[kHostPort] = host_port;
208 }
209 
tpm_port() const210 int CuttlefishConfig::InstanceSpecific::tpm_port() const {
211   return (*Dictionary())[kTpmPort].asInt();
212 }
213 
set_tpm_port(int tpm_port)214 void CuttlefishConfig::MutableInstanceSpecific::set_tpm_port(int tpm_port) {
215   (*Dictionary())[kTpmPort] = tpm_port;
216 }
217 
adb_ip_and_port() const218 std::string CuttlefishConfig::InstanceSpecific::adb_ip_and_port() const {
219   return (*Dictionary())[kAdbIPAndPort].asString();
220 }
221 
set_adb_ip_and_port(const std::string & ip_port)222 void CuttlefishConfig::MutableInstanceSpecific::set_adb_ip_and_port(
223     const std::string& ip_port) {
224   (*Dictionary())[kAdbIPAndPort] = ip_port;
225 }
226 
adb_device_name() const227 std::string CuttlefishConfig::InstanceSpecific::adb_device_name() const {
228   if (adb_ip_and_port() != "") {
229     return adb_ip_and_port();
230   }
231   LOG(ERROR) << "no adb_mode found, returning bad device name";
232   return "NO_ADB_MODE_SET_NO_VALID_DEVICE_NAME";
233 }
234 
device_title() const235 std::string CuttlefishConfig::InstanceSpecific::device_title() const {
236   return (*Dictionary())[kDeviceTitle].asString();
237 }
238 
set_device_title(const std::string & title)239 void CuttlefishConfig::MutableInstanceSpecific::set_device_title(
240     const std::string& title) {
241   (*Dictionary())[kDeviceTitle] = title;
242 }
243 
vnc_server_port() const244 int CuttlefishConfig::InstanceSpecific::vnc_server_port() const {
245   return (*Dictionary())[kVncServerPort].asInt();
246 }
247 
set_vnc_server_port(int vnc_server_port)248 void CuttlefishConfig::MutableInstanceSpecific::set_vnc_server_port(int vnc_server_port) {
249   (*Dictionary())[kVncServerPort] = vnc_server_port;
250 }
251 
frames_server_port() const252 int CuttlefishConfig::InstanceSpecific::frames_server_port() const {
253   return (*Dictionary())[kFramesServerPort].asInt();
254 }
255 
set_frames_server_port(int frames_server_port)256 void CuttlefishConfig::MutableInstanceSpecific::set_frames_server_port(int frames_server_port) {
257   (*Dictionary())[kFramesServerPort] = frames_server_port;
258 }
259 
touch_server_port() const260 int CuttlefishConfig::InstanceSpecific::touch_server_port() const {
261   return (*Dictionary())[kTouchServerPort].asInt();
262 }
263 
set_touch_server_port(int touch_server_port)264 void CuttlefishConfig::MutableInstanceSpecific::set_touch_server_port(int touch_server_port) {
265   (*Dictionary())[kTouchServerPort] = touch_server_port;
266 }
267 
keyboard_server_port() const268 int CuttlefishConfig::InstanceSpecific::keyboard_server_port() const {
269   return (*Dictionary())[kKeyboardServerPort].asInt();
270 }
271 
set_keyboard_server_port(int keyboard_server_port)272 void CuttlefishConfig::MutableInstanceSpecific::set_keyboard_server_port(int keyboard_server_port) {
273   (*Dictionary())[kKeyboardServerPort] = keyboard_server_port;
274 }
275 
keymaster_vsock_port() const276 int CuttlefishConfig::InstanceSpecific::keymaster_vsock_port() const {
277   return (*Dictionary())[kKeymasterVsockPort].asInt();
278 }
279 
set_keymaster_vsock_port(int keymaster_vsock_port)280 void CuttlefishConfig::MutableInstanceSpecific::set_keymaster_vsock_port(int keymaster_vsock_port) {
281   (*Dictionary())[kKeymasterVsockPort] = keymaster_vsock_port;
282 }
283 
gatekeeper_vsock_port() const284 int CuttlefishConfig::InstanceSpecific::gatekeeper_vsock_port() const {
285   return (*Dictionary())[kGatekeeperVsockPort].asInt();
286 }
287 
set_gatekeeper_vsock_port(int gatekeeper_vsock_port)288 void CuttlefishConfig::MutableInstanceSpecific::set_gatekeeper_vsock_port(int gatekeeper_vsock_port) {
289   (*Dictionary())[kGatekeeperVsockPort] = gatekeeper_vsock_port;
290 }
291 
tombstone_receiver_port() const292 int CuttlefishConfig::InstanceSpecific::tombstone_receiver_port() const {
293   return (*Dictionary())[kTombstoneReceiverPort].asInt();
294 }
295 
set_tombstone_receiver_port(int tombstone_receiver_port)296 void CuttlefishConfig::MutableInstanceSpecific::set_tombstone_receiver_port(int tombstone_receiver_port) {
297   (*Dictionary())[kTombstoneReceiverPort] = tombstone_receiver_port;
298 }
299 
config_server_port() const300 int CuttlefishConfig::InstanceSpecific::config_server_port() const {
301   return (*Dictionary())[kConfigServerPort].asInt();
302 }
303 
set_config_server_port(int config_server_port)304 void CuttlefishConfig::MutableInstanceSpecific::set_config_server_port(int config_server_port) {
305   (*Dictionary())[kConfigServerPort] = config_server_port;
306 }
307 
set_webrtc_device_id(const std::string & id)308 void CuttlefishConfig::MutableInstanceSpecific::set_webrtc_device_id(
309     const std::string& id) {
310   (*Dictionary())[kWebrtcDeviceId] = id;
311 }
312 
webrtc_device_id() const313 std::string CuttlefishConfig::InstanceSpecific::webrtc_device_id() const {
314   return (*Dictionary())[kWebrtcDeviceId].asString();
315 }
316 
set_start_webrtc_signaling_server(bool start)317 void CuttlefishConfig::MutableInstanceSpecific::set_start_webrtc_signaling_server(bool start) {
318   (*Dictionary())[kStartSigServer] = start;
319 }
320 
start_webrtc_sig_server() const321 bool CuttlefishConfig::InstanceSpecific::start_webrtc_sig_server() const {
322   return (*Dictionary())[kStartSigServer].asBool();
323 }
324 
touch_socket_path() const325 std::string CuttlefishConfig::InstanceSpecific::touch_socket_path() const {
326   return PerInstanceInternalPath("touch.sock");
327 }
328 
keyboard_socket_path() const329 std::string CuttlefishConfig::InstanceSpecific::keyboard_socket_path() const {
330   return PerInstanceInternalPath("keyboard.sock");
331 }
332 
frames_socket_path() const333 std::string CuttlefishConfig::InstanceSpecific::frames_socket_path() const {
334   return PerInstanceInternalPath("frames.sock");
335 }
336 
set_wifi_mac_address(const std::array<unsigned char,6> & mac_address)337 void CuttlefishConfig::MutableInstanceSpecific::set_wifi_mac_address(
338     const std::array<unsigned char, 6>& mac_address) {
339   Json::Value mac_address_obj(Json::arrayValue);
340   for (const auto& num : mac_address) {
341     mac_address_obj.append(num);
342   }
343   (*Dictionary())[kWifiMacAddress] = mac_address_obj;
344 }
345 
wifi_mac_address() const346 std::array<unsigned char, 6> CuttlefishConfig::InstanceSpecific::wifi_mac_address() const {
347   std::array<unsigned char, 6> mac_address{0, 0, 0, 0, 0, 0};
348   auto mac_address_obj = (*Dictionary())[kWifiMacAddress];
349   if (mac_address_obj.size() != 6) {
350     LOG(ERROR) << kWifiMacAddress << " entry had wrong size";
351     return {};
352   }
353   for (int i = 0; i < 6; i++) {
354     mac_address[i] = mac_address_obj[i].asInt();
355   }
356   return mac_address;
357 }
358 
PerInstancePath(const char * file_name) const359 std::string CuttlefishConfig::InstanceSpecific::PerInstancePath(
360     const char* file_name) const {
361   return (instance_dir() + "/") + file_name;
362 }
363 
PerInstanceInternalPath(const char * file_name) const364 std::string CuttlefishConfig::InstanceSpecific::PerInstanceInternalPath(
365     const char* file_name) const {
366   if (file_name[0] == '\0') {
367     // Don't append a / if file_name is empty.
368     return PerInstancePath(kInternalDirName);
369   }
370   auto relative_path = (std::string(kInternalDirName) + "/") + file_name;
371   return PerInstancePath(relative_path.c_str());
372 }
373 
instance_name() const374 std::string CuttlefishConfig::InstanceSpecific::instance_name() const {
375   return ForCurrentInstance("cvd-");
376 }
377 
378 }  // namespace cuttlefish
379