1 /*
2  * Copyright (C) 2023 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 "common/libs/utils/files.h"
20 
21 const char* kEnvironments = "environments";
22 
23 namespace cuttlefish {
24 
Dictionary()25 Json::Value* CuttlefishConfig::EnvironmentSpecific::Dictionary() {
26   return &(*config_->dictionary_)[kEnvironments][envName_];
27 }
28 
Dictionary() const29 const Json::Value* CuttlefishConfig::EnvironmentSpecific::Dictionary() const {
30   return &(*config_->dictionary_)[kEnvironments][envName_];
31 }
32 
Dictionary()33 Json::Value* CuttlefishConfig::MutableEnvironmentSpecific::Dictionary() {
34   return &(*config_->dictionary_)[kEnvironments][envName_];
35 }
36 
environment_name() const37 std::string CuttlefishConfig::EnvironmentSpecific::environment_name() const {
38   return envName_;
39 }
40 
environment_uds_dir() const41 std::string CuttlefishConfig::EnvironmentSpecific::environment_uds_dir() const {
42   return config_->EnvironmentsUdsPath(envName_);
43 }
44 
PerEnvironmentUdsPath(const std::string & file_name) const45 std::string CuttlefishConfig::EnvironmentSpecific::PerEnvironmentUdsPath(
46     const std::string& file_name) const {
47   return (environment_uds_dir() + "/") + file_name;
48 }
49 
environment_dir() const50 std::string CuttlefishConfig::EnvironmentSpecific::environment_dir() const {
51   return config_->EnvironmentsPath(envName_);
52 }
53 
PerEnvironmentPath(const std::string & file_name) const54 std::string CuttlefishConfig::EnvironmentSpecific::PerEnvironmentPath(
55     const std::string& file_name) const {
56   return (environment_dir() + "/") + file_name;
57 }
58 
PerEnvironmentLogPath(const std::string & file_name) const59 std::string CuttlefishConfig::EnvironmentSpecific::PerEnvironmentLogPath(
60     const std::string& file_name) const {
61   if (file_name.size() == 0) {
62     // Don't append a / if file_name is empty.
63     return PerEnvironmentPath(kLogDirName);
64   }
65   auto relative_path = (std::string(kLogDirName) + "/") + file_name;
66   return PerEnvironmentPath(relative_path.c_str());
67 }
68 
PerEnvironmentGrpcSocketPath(const std::string & file_name) const69 std::string CuttlefishConfig::EnvironmentSpecific::PerEnvironmentGrpcSocketPath(
70     const std::string& file_name) const {
71   if (file_name.size() == 0) {
72     // Don't append a / if file_name is empty.
73     return PerEnvironmentPath(kGrpcSocketDirName);
74   }
75   auto relative_path = (std::string(kGrpcSocketDirName) + "/") + file_name;
76   return PerEnvironmentPath(relative_path.c_str());
77 }
78 
control_socket_path() const79 std::string CuttlefishConfig::EnvironmentSpecific::control_socket_path() const {
80   return PerEnvironmentUdsPath("env_control.sock");
81 }
82 
launcher_log_path() const83 std::string CuttlefishConfig::EnvironmentSpecific::launcher_log_path() const {
84   return AbsolutePath(PerEnvironmentLogPath("launcher.log"));
85 }
86 
87 static constexpr char kEnableWifi[] = "enable_wifi";
set_enable_wifi(bool enable_wifi)88 void CuttlefishConfig::MutableEnvironmentSpecific::set_enable_wifi(
89     bool enable_wifi) {
90   (*Dictionary())[kEnableWifi] = enable_wifi;
91 }
enable_wifi() const92 bool CuttlefishConfig::EnvironmentSpecific::enable_wifi() const {
93   return (*Dictionary())[kEnableWifi].asBool();
94 }
95 
96 static constexpr char kStartWmediumd[] = "start_wmediumd";
set_start_wmediumd(bool start)97 void CuttlefishConfig::MutableEnvironmentSpecific::set_start_wmediumd(
98     bool start) {
99   (*Dictionary())[kStartWmediumd] = start;
100 }
start_wmediumd() const101 bool CuttlefishConfig::EnvironmentSpecific::start_wmediumd() const {
102   return (*Dictionary())[kStartWmediumd].asBool();
103 }
104 
105 static constexpr char kVhostUserMac80211Hwsim[] = "vhost_user_mac80211_hwsim";
106 void CuttlefishConfig::MutableEnvironmentSpecific::
set_vhost_user_mac80211_hwsim(const std::string & path)107     set_vhost_user_mac80211_hwsim(const std::string& path) {
108   (*Dictionary())[kVhostUserMac80211Hwsim] = path;
109 }
vhost_user_mac80211_hwsim() const110 std::string CuttlefishConfig::EnvironmentSpecific::vhost_user_mac80211_hwsim()
111     const {
112   return (*Dictionary())[kVhostUserMac80211Hwsim].asString();
113 }
114 
115 static constexpr char kWmediumdApiServerSocket[] = "wmediumd_api_server_socket";
116 void CuttlefishConfig::MutableEnvironmentSpecific::
set_wmediumd_api_server_socket(const std::string & path)117     set_wmediumd_api_server_socket(const std::string& path) {
118   (*Dictionary())[kWmediumdApiServerSocket] = path;
119 }
wmediumd_api_server_socket() const120 std::string CuttlefishConfig::EnvironmentSpecific::wmediumd_api_server_socket()
121     const {
122   return (*Dictionary())[kWmediumdApiServerSocket].asString();
123 }
124 
125 static constexpr char kWmediumdConfig[] = "wmediumd_config";
set_wmediumd_config(const std::string & config)126 void CuttlefishConfig::MutableEnvironmentSpecific::set_wmediumd_config(
127     const std::string& config) {
128   (*Dictionary())[kWmediumdConfig] = config;
129 }
wmediumd_config() const130 std::string CuttlefishConfig::EnvironmentSpecific::wmediumd_config() const {
131   return (*Dictionary())[kWmediumdConfig].asString();
132 }
133 
134 static constexpr char kWmediumdMacPrefix[] = "wmediumd_mac_prefix";
set_wmediumd_mac_prefix(int mac_prefix)135 void CuttlefishConfig::MutableEnvironmentSpecific::set_wmediumd_mac_prefix(
136     int mac_prefix) {
137   (*Dictionary())[kWmediumdMacPrefix] = mac_prefix;
138 }
wmediumd_mac_prefix() const139 int CuttlefishConfig::EnvironmentSpecific::wmediumd_mac_prefix() const {
140   return (*Dictionary())[kWmediumdMacPrefix].asInt();
141 }
142 
143 }  // namespace cuttlefish
144