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 
casimir_nci_socket_path() const87 std::string CuttlefishConfig::EnvironmentSpecific::casimir_nci_socket_path()
88     const {
89   return PerEnvironmentUdsPath("casimir_nci.sock");
90 }
91 
casimir_rf_socket_path() const92 std::string CuttlefishConfig::EnvironmentSpecific::casimir_rf_socket_path()
93     const {
94   return PerEnvironmentUdsPath("casimir_rf.sock");
95 }
96 
97 static constexpr char kEnableWifi[] = "enable_wifi";
set_enable_wifi(bool enable_wifi)98 void CuttlefishConfig::MutableEnvironmentSpecific::set_enable_wifi(
99     bool enable_wifi) {
100   (*Dictionary())[kEnableWifi] = enable_wifi;
101 }
enable_wifi() const102 bool CuttlefishConfig::EnvironmentSpecific::enable_wifi() const {
103   return (*Dictionary())[kEnableWifi].asBool();
104 }
105 
106 static constexpr char kStartWmediumd[] = "start_wmediumd";
set_start_wmediumd(bool start)107 void CuttlefishConfig::MutableEnvironmentSpecific::set_start_wmediumd(
108     bool start) {
109   (*Dictionary())[kStartWmediumd] = start;
110 }
start_wmediumd() const111 bool CuttlefishConfig::EnvironmentSpecific::start_wmediumd() const {
112   return (*Dictionary())[kStartWmediumd].asBool();
113 }
114 
115 static constexpr char kVhostUserMac80211Hwsim[] = "vhost_user_mac80211_hwsim";
116 void CuttlefishConfig::MutableEnvironmentSpecific::
set_vhost_user_mac80211_hwsim(const std::string & path)117     set_vhost_user_mac80211_hwsim(const std::string& path) {
118   (*Dictionary())[kVhostUserMac80211Hwsim] = path;
119 }
vhost_user_mac80211_hwsim() const120 std::string CuttlefishConfig::EnvironmentSpecific::vhost_user_mac80211_hwsim()
121     const {
122   return (*Dictionary())[kVhostUserMac80211Hwsim].asString();
123 }
124 
125 static constexpr char kWmediumdApiServerSocket[] = "wmediumd_api_server_socket";
126 void CuttlefishConfig::MutableEnvironmentSpecific::
set_wmediumd_api_server_socket(const std::string & path)127     set_wmediumd_api_server_socket(const std::string& path) {
128   (*Dictionary())[kWmediumdApiServerSocket] = path;
129 }
wmediumd_api_server_socket() const130 std::string CuttlefishConfig::EnvironmentSpecific::wmediumd_api_server_socket()
131     const {
132   return (*Dictionary())[kWmediumdApiServerSocket].asString();
133 }
134 
135 static constexpr char kWmediumdConfig[] = "wmediumd_config";
set_wmediumd_config(const std::string & config)136 void CuttlefishConfig::MutableEnvironmentSpecific::set_wmediumd_config(
137     const std::string& config) {
138   (*Dictionary())[kWmediumdConfig] = config;
139 }
wmediumd_config() const140 std::string CuttlefishConfig::EnvironmentSpecific::wmediumd_config() const {
141   return (*Dictionary())[kWmediumdConfig].asString();
142 }
143 
144 static constexpr char kWmediumdMacPrefix[] = "wmediumd_mac_prefix";
set_wmediumd_mac_prefix(int mac_prefix)145 void CuttlefishConfig::MutableEnvironmentSpecific::set_wmediumd_mac_prefix(
146     int mac_prefix) {
147   (*Dictionary())[kWmediumdMacPrefix] = mac_prefix;
148 }
wmediumd_mac_prefix() const149 int CuttlefishConfig::EnvironmentSpecific::wmediumd_mac_prefix() const {
150   return (*Dictionary())[kWmediumdMacPrefix].asInt();
151 }
152 
153 }  // namespace cuttlefish
154