Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/buffet_config.h" |
| 6 | |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | #include <base/strings/string_number_conversions.h> |
| 9 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 10 | namespace { |
| 11 | |
| 12 | // TODO(vitalybuka): Remove this when deviceKind is gone from server. |
| 13 | std::string GetDeviceKind(const std::string& manifest_id) { |
| 14 | CHECK_EQ(5u, manifest_id.size()); |
| 15 | std::string kind = manifest_id.substr(0, 2); |
| 16 | if (kind == "AC") |
| 17 | return "accessPoint"; |
| 18 | if (kind == "AK") |
| 19 | return "aggregator"; |
| 20 | if (kind == "AM") |
| 21 | return "camera"; |
| 22 | if (kind == "AB") |
| 23 | return "developmentBoard"; |
| 24 | if (kind == "AE") |
| 25 | return "printer"; |
| 26 | if (kind == "AF") |
| 27 | return "scanner"; |
| 28 | if (kind == "AD") |
| 29 | return "speaker"; |
| 30 | if (kind == "AL") |
| 31 | return "storage"; |
| 32 | if (kind == "AJ") |
| 33 | return "toy"; |
| 34 | if (kind == "AA") |
| 35 | return "vendor"; |
| 36 | if (kind == "AN") |
| 37 | return "video"; |
| 38 | LOG(FATAL) << "Invalid model id: " << manifest_id; |
| 39 | return std::string(); |
| 40 | } |
| 41 | |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 42 | bool IsValidAccessRole(const std::string& role) { |
Vitaly Buka | 9467520 | 2015-05-13 11:48:54 -0700 | [diff] [blame] | 43 | return role == "none" || role == "viewer" || role == "user"; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 46 | } // namespace |
| 47 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 48 | namespace buffet { |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 49 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 50 | namespace config_keys { |
| 51 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 52 | const char kClientId[] = "client_id"; |
| 53 | const char kClientSecret[] = "client_secret"; |
| 54 | const char kApiKey[] = "api_key"; |
| 55 | const char kOAuthURL[] = "oauth_url"; |
| 56 | const char kServiceURL[] = "service_url"; |
| 57 | const char kName[] = "name"; |
| 58 | const char kDescription[] = "description"; |
| 59 | const char kLocation[] = "location"; |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 60 | const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role"; |
| 61 | const char kLocalDiscoveryEnabled[] = "local_discovery_enabled"; |
| 62 | const char kLocalPairingEnabled[] = "local_pairing_enabled"; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 63 | const char kOemName[] = "oem_name"; |
| 64 | const char kModelName[] = "model_name"; |
| 65 | const char kModelId[] = "model_id"; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 66 | const char kPollingPeriodMs[] = "polling_period_ms"; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 67 | |
| 68 | } // namespace config_keys |
| 69 | |
| 70 | void BuffetConfig::Load(const base::FilePath& config_path) { |
| 71 | chromeos::KeyValueStore store; |
| 72 | if (store.Load(config_path)) { |
| 73 | Load(store); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void BuffetConfig::Load(const chromeos::KeyValueStore& store) { |
| 78 | store.GetString(config_keys::kClientId, &client_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 79 | CHECK(!client_id_.empty()); |
| 80 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 81 | store.GetString(config_keys::kClientSecret, &client_secret_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 82 | CHECK(!client_secret_.empty()); |
| 83 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 84 | store.GetString(config_keys::kApiKey, &api_key_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 85 | CHECK(!api_key_.empty()); |
| 86 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 87 | store.GetString(config_keys::kOAuthURL, &oauth_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 88 | CHECK(!oauth_url_.empty()); |
| 89 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 90 | store.GetString(config_keys::kServiceURL, &service_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 91 | CHECK(!service_url_.empty()); |
| 92 | |
| 93 | store.GetString(config_keys::kOemName, &oem_name_); |
| 94 | CHECK(!oem_name_.empty()); |
| 95 | |
| 96 | store.GetString(config_keys::kModelName, &model_name_); |
| 97 | CHECK(!model_name_.empty()); |
| 98 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 99 | store.GetString(config_keys::kModelId, &model_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 100 | device_kind_ = GetDeviceKind(model_id_); |
| 101 | |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 102 | std::string polling_period_str; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 103 | if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str)) |
| 104 | CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_)); |
| 105 | |
| 106 | store.GetString(config_keys::kName, &name_); |
| 107 | CHECK(!name_.empty()); |
| 108 | |
| 109 | store.GetString(config_keys::kDescription, &description_); |
| 110 | store.GetString(config_keys::kLocation, &location_); |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 111 | |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 112 | store.GetString(config_keys::kLocalAnonymousAccessRole, |
| 113 | &local_anonymous_access_role_); |
| 114 | CHECK(IsValidAccessRole(local_anonymous_access_role_)) |
| 115 | << "Invalid role: " << local_anonymous_access_role_; |
| 116 | |
| 117 | store.GetBoolean(config_keys::kLocalDiscoveryEnabled, |
| 118 | &local_discovery_enabled_); |
| 119 | store.GetBoolean(config_keys::kLocalPairingEnabled, &local_pairing_enabled_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 122 | bool BuffetConfig::set_name(const std::string& name) { |
| 123 | if (name.empty()) { |
| 124 | LOG(ERROR) << "Invalid name: " << name; |
| 125 | return false; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 126 | } |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 127 | name_ = name; |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool BuffetConfig::set_local_anonymous_access_role(const std::string& role) { |
| 132 | if (!IsValidAccessRole(role)) { |
| 133 | LOG(ERROR) << "Invalid role: " << role; |
| 134 | return false; |
| 135 | } |
| 136 | local_anonymous_access_role_ = role; |
| 137 | return true; |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 140 | } // namespace buffet |