| // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "buffet/buffet_config.h" |
| |
| #include <map> |
| #include <set> |
| |
| #include <base/files/file_enumerator.h> |
| #include <base/files/file_util.h> |
| #include <base/files/important_file_writer.h> |
| #include <base/logging.h> |
| #include <base/strings/string_number_conversions.h> |
| #include <chromeos/errors/error.h> |
| #include <chromeos/errors/error_codes.h> |
| #include <chromeos/strings/string_utils.h> |
| #include <weave/enum_to_string.h> |
| |
| namespace buffet { |
| |
| namespace { |
| |
| const char kErrorDomain[] = "buffet"; |
| const char kFileReadError[] = "file_read_error"; |
| |
| bool LoadFile(const base::FilePath& file_path, |
| std::string* data, |
| chromeos::ErrorPtr* error) { |
| if (!base::ReadFileToString(file_path, data)) { |
| chromeos::errors::system::AddSystemError(error, FROM_HERE, errno); |
| chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError, |
| "Failed to read file '%s'", |
| file_path.value().c_str()); |
| return false; |
| } |
| return true; |
| } |
| |
| } // namespace |
| |
| namespace config_keys { |
| |
| const char kClientId[] = "client_id"; |
| const char kClientSecret[] = "client_secret"; |
| const char kApiKey[] = "api_key"; |
| const char kOAuthURL[] = "oauth_url"; |
| const char kServiceURL[] = "service_url"; |
| const char kName[] = "name"; |
| const char kDescription[] = "description"; |
| const char kLocation[] = "location"; |
| const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role"; |
| const char kLocalDiscoveryEnabled[] = "local_discovery_enabled"; |
| const char kLocalPairingEnabled[] = "local_pairing_enabled"; |
| const char kOemName[] = "oem_name"; |
| const char kModelName[] = "model_name"; |
| const char kModelId[] = "model_id"; |
| const char kWifiAutoSetupEnabled[] = "wifi_auto_setup_enabled"; |
| const char kEmbeddedCode[] = "embedded_code"; |
| const char kPairingModes[] = "pairing_modes"; |
| |
| } // namespace config_keys |
| |
| BuffetConfig::BuffetConfig(const Options& options) : options_(options) {} |
| |
| bool BuffetConfig::LoadDefaults(weave::Settings* settings) { |
| // Keep this hard coded default for sometime. This previously was set by |
| // libweave. It should be set by overlays buffet.conf. |
| settings->client_id = "58855907228.apps.googleusercontent.com"; |
| settings->client_secret = "eHSAREAHrIqPsHBxCE9zPPBi"; |
| settings->api_key = "AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"; |
| settings->name = "Developer device"; |
| settings->oem_name = "Chromium"; |
| settings->model_name = "Brillo"; |
| settings->model_id = "AAAAA"; |
| |
| if (!base::PathExists(options_.defaults)) |
| return true; // Nothing to load. |
| |
| chromeos::KeyValueStore store; |
| if (!store.Load(options_.defaults)) |
| return false; |
| bool result = LoadDefaults(store, settings); |
| settings->disable_security = options_.disable_security; |
| settings->test_privet_ssid = options_.test_privet_ssid; |
| return result; |
| } |
| |
| std::map<std::string, std::string> BuffetConfig::LoadCommandDefs() { |
| std::map<std::string, std::string> result; |
| auto load_packages = [&result](const base::FilePath& root, |
| const base::FilePath::StringType& pattern) { |
| base::FilePath dir{root.Append("commands")}; |
| VLOG(2) << "Looking for commands in " << dir.value(); |
| base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| pattern); |
| for (base::FilePath path = enumerator.Next(); !path.empty(); |
| path = enumerator.Next()) { |
| LOG(INFO) << "Loading command schema from " << path.value(); |
| std::string category = path.BaseName().RemoveExtension().value(); |
| CHECK(LoadFile(path, &result[category], nullptr)); |
| } |
| }; |
| load_packages(options_.definitions, FILE_PATH_LITERAL("*.json")); |
| load_packages(options_.test_definitions, FILE_PATH_LITERAL("*test.json")); |
| return result; |
| } |
| |
| std::map<std::string, std::string> BuffetConfig::LoadStateDefs() { |
| // Load component-specific device state definitions. |
| base::FilePath dir{options_.definitions.Append("states")}; |
| base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| FILE_PATH_LITERAL("*.schema.json")); |
| std::map<std::string, std::string> result; |
| for (base::FilePath path = enumerator.Next(); !path.empty(); |
| path = enumerator.Next()) { |
| LOG(INFO) << "Loading state definition from " << path.value(); |
| std::string category = path.BaseName().RemoveExtension().value(); |
| CHECK(LoadFile(path, &result[category], nullptr)); |
| } |
| return result; |
| } |
| |
| std::vector<std::string> BuffetConfig::LoadStateDefaults() { |
| // Load component-specific device state defaults. |
| base::FilePath dir{options_.definitions.Append("states")}; |
| base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| FILE_PATH_LITERAL("*.defaults.json")); |
| std::vector<std::string> result; |
| for (base::FilePath path = enumerator.Next(); !path.empty(); |
| path = enumerator.Next()) { |
| LOG(INFO) << "Loading state defaults from " << path.value(); |
| std::string json; |
| CHECK(LoadFile(path, &json, nullptr)); |
| result.push_back(json); |
| } |
| return result; |
| } |
| |
| bool BuffetConfig::LoadDefaults(const chromeos::KeyValueStore& store, |
| weave::Settings* settings) { |
| store.GetString(config_keys::kClientId, &settings->client_id); |
| store.GetString(config_keys::kClientSecret, &settings->client_secret); |
| store.GetString(config_keys::kApiKey, &settings->api_key); |
| store.GetString(config_keys::kOAuthURL, &settings->oauth_url); |
| store.GetString(config_keys::kServiceURL, &settings->service_url); |
| store.GetString(config_keys::kOemName, &settings->oem_name); |
| store.GetString(config_keys::kModelName, &settings->model_name); |
| store.GetString(config_keys::kModelId, &settings->model_id); |
| |
| base::FilePath lsb_release_path("/etc/lsb-release"); |
| chromeos::KeyValueStore lsb_release_store; |
| if (lsb_release_store.Load(lsb_release_path) && |
| lsb_release_store.GetString("CHROMEOS_RELEASE_VERSION", |
| &settings->firmware_version)) { |
| } else { |
| LOG(ERROR) << "Failed to get CHROMEOS_RELEASE_VERSION from " |
| << lsb_release_path.value(); |
| } |
| |
| store.GetBoolean(config_keys::kWifiAutoSetupEnabled, |
| &settings->wifi_auto_setup_enabled); |
| store.GetString(config_keys::kEmbeddedCode, &settings->embedded_code); |
| |
| std::string modes_str; |
| if (store.GetString(config_keys::kPairingModes, &modes_str)) { |
| std::set<weave::PairingType> pairing_modes; |
| for (const std::string& mode : |
| chromeos::string_utils::Split(modes_str, ",", true, true)) { |
| weave::PairingType pairing_mode; |
| if (!StringToEnum(mode, &pairing_mode)) |
| return false; |
| pairing_modes.insert(pairing_mode); |
| } |
| settings->pairing_modes = std::move(pairing_modes); |
| } |
| |
| store.GetString(config_keys::kName, &settings->name); |
| store.GetString(config_keys::kDescription, &settings->description); |
| store.GetString(config_keys::kLocation, &settings->location); |
| |
| std::string role_str; |
| if (store.GetString(config_keys::kLocalAnonymousAccessRole, &role_str)) { |
| if (!StringToEnum(role_str, &settings->local_anonymous_access_role)) |
| return false; |
| } |
| store.GetBoolean(config_keys::kLocalDiscoveryEnabled, |
| &settings->local_discovery_enabled); |
| store.GetBoolean(config_keys::kLocalPairingEnabled, |
| &settings->local_pairing_enabled); |
| return true; |
| } |
| |
| std::string BuffetConfig::LoadSettings() { |
| std::string json_string; |
| base::ReadFileToString(options_.settings, &json_string); |
| return json_string; |
| } |
| |
| void BuffetConfig::SaveSettings(const std::string& settings) { |
| base::ImportantFileWriter::WriteFileAtomically(options_.settings, settings); |
| } |
| |
| } // namespace buffet |