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 | |
| 42 | } // namespace |
| 43 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 44 | namespace buffet { |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 45 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 46 | namespace config_keys { |
| 47 | |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 48 | const char kClientId[] = "client_id"; |
| 49 | const char kClientSecret[] = "client_secret"; |
| 50 | const char kApiKey[] = "api_key"; |
| 51 | const char kOAuthURL[] = "oauth_url"; |
| 52 | const char kServiceURL[] = "service_url"; |
| 53 | const char kName[] = "name"; |
| 54 | const char kDescription[] = "description"; |
| 55 | const char kLocation[] = "location"; |
| 56 | const char kOemName[] = "oem_name"; |
| 57 | const char kModelName[] = "model_name"; |
| 58 | const char kModelId[] = "model_id"; |
| 59 | |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 60 | const char kPollingPeriodMs[] = "polling_period_ms"; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 61 | |
| 62 | } // namespace config_keys |
| 63 | |
| 64 | void BuffetConfig::Load(const base::FilePath& config_path) { |
| 65 | chromeos::KeyValueStore store; |
| 66 | if (store.Load(config_path)) { |
| 67 | Load(store); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void BuffetConfig::Load(const chromeos::KeyValueStore& store) { |
| 72 | store.GetString(config_keys::kClientId, &client_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 73 | CHECK(!client_id_.empty()); |
| 74 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 75 | store.GetString(config_keys::kClientSecret, &client_secret_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 76 | CHECK(!client_secret_.empty()); |
| 77 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 78 | store.GetString(config_keys::kApiKey, &api_key_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 79 | CHECK(!api_key_.empty()); |
| 80 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 81 | store.GetString(config_keys::kOAuthURL, &oauth_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 82 | CHECK(!oauth_url_.empty()); |
| 83 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 84 | store.GetString(config_keys::kServiceURL, &service_url_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 85 | CHECK(!service_url_.empty()); |
| 86 | |
| 87 | store.GetString(config_keys::kOemName, &oem_name_); |
| 88 | CHECK(!oem_name_.empty()); |
| 89 | |
| 90 | store.GetString(config_keys::kModelName, &model_name_); |
| 91 | CHECK(!model_name_.empty()); |
| 92 | |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 93 | store.GetString(config_keys::kModelId, &model_id_); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 94 | device_kind_ = GetDeviceKind(model_id_); |
| 95 | |
Christopher Wiley | 34eae04 | 2015-03-18 10:25:08 -0700 | [diff] [blame] | 96 | std::string polling_period_str; |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 97 | if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str)) |
| 98 | CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_)); |
| 99 | |
| 100 | store.GetString(config_keys::kName, &name_); |
| 101 | CHECK(!name_.empty()); |
| 102 | |
| 103 | store.GetString(config_keys::kDescription, &description_); |
| 104 | store.GetString(config_keys::kLocation, &location_); |
| 105 | } |
| 106 | |
| 107 | void BuffetConfig::set_name(const std::string& name) { |
| 108 | CHECK(!name.empty()); |
| 109 | name_ = name; |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace buffet |