-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffet_config.cc
149 lines (126 loc) · 5.39 KB
/
buffet_config.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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 <utility>
#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 <brillo/errors/error.h>
#include <brillo/errors/error_codes.h>
#include <brillo/strings/string_utils.h>
#include <weave/enum_to_string.h>
namespace buffet {
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 hardcoded default for sometime. This previously was set by
// libweave. It should be set by overlay's buffet.conf.
// Keys owners: avakulenko, gene, vitalybuka.
settings->client_id =
"338428340000-vkb4p6h40c7kja1k3l70kke8t615cjit.apps.googleusercontent."
"com";
settings->client_secret = "LS_iPYo_WIOE0m2VnLdduhnx";
settings->api_key = "AIzaSyACK3oZtmIylUKXiTMqkZqfuRiCgQmQSAQ";
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.
brillo::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;
if (!options_.client_id.empty())
settings->client_id = options_.client_id;
if (!options_.client_secret.empty())
settings->client_secret = options_.client_secret;
if (!options_.api_key.empty())
settings->api_key = options_.api_key;
if (!options_.oauth_url.empty())
settings->oauth_url = options_.oauth_url;
if (!options_.service_url.empty())
settings->service_url = options_.service_url;
return result;
}
bool BuffetConfig::LoadDefaults(const brillo::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");
brillo::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 :
brillo::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