blob: d8e03247cee2bcb9de1f0e44bba9490019a41183 [file] [log] [blame]
Christopher Wiley583d64b2015-03-24 14:30:17 -07001// 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 Wiley34eae042015-03-18 10:25:08 -07007#include <base/logging.h>
8#include <base/strings/string_number_conversions.h>
9
Vitaly Buka867b0882015-04-16 10:03:26 -070010namespace {
11
12// TODO(vitalybuka): Remove this when deviceKind is gone from server.
13std::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 Wiley583d64b2015-03-24 14:30:17 -070044namespace buffet {
Vitaly Buka867b0882015-04-16 10:03:26 -070045
Christopher Wiley583d64b2015-03-24 14:30:17 -070046namespace config_keys {
47
Vitaly Buka867b0882015-04-16 10:03:26 -070048const char kClientId[] = "client_id";
49const char kClientSecret[] = "client_secret";
50const char kApiKey[] = "api_key";
51const char kOAuthURL[] = "oauth_url";
52const char kServiceURL[] = "service_url";
53const char kName[] = "name";
54const char kDescription[] = "description";
55const char kLocation[] = "location";
56const char kOemName[] = "oem_name";
57const char kModelName[] = "model_name";
58const char kModelId[] = "model_id";
59
Christopher Wiley34eae042015-03-18 10:25:08 -070060const char kPollingPeriodMs[] = "polling_period_ms";
Christopher Wiley583d64b2015-03-24 14:30:17 -070061
62} // namespace config_keys
63
64void BuffetConfig::Load(const base::FilePath& config_path) {
65 chromeos::KeyValueStore store;
66 if (store.Load(config_path)) {
67 Load(store);
68 }
69}
70
71void BuffetConfig::Load(const chromeos::KeyValueStore& store) {
72 store.GetString(config_keys::kClientId, &client_id_);
Vitaly Buka867b0882015-04-16 10:03:26 -070073 CHECK(!client_id_.empty());
74
Christopher Wiley583d64b2015-03-24 14:30:17 -070075 store.GetString(config_keys::kClientSecret, &client_secret_);
Vitaly Buka867b0882015-04-16 10:03:26 -070076 CHECK(!client_secret_.empty());
77
Christopher Wiley583d64b2015-03-24 14:30:17 -070078 store.GetString(config_keys::kApiKey, &api_key_);
Vitaly Buka867b0882015-04-16 10:03:26 -070079 CHECK(!api_key_.empty());
80
Christopher Wiley583d64b2015-03-24 14:30:17 -070081 store.GetString(config_keys::kOAuthURL, &oauth_url_);
Vitaly Buka867b0882015-04-16 10:03:26 -070082 CHECK(!oauth_url_.empty());
83
Christopher Wiley583d64b2015-03-24 14:30:17 -070084 store.GetString(config_keys::kServiceURL, &service_url_);
Vitaly Buka867b0882015-04-16 10:03:26 -070085 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 Wiley583d64b2015-03-24 14:30:17 -070093 store.GetString(config_keys::kModelId, &model_id_);
Vitaly Buka867b0882015-04-16 10:03:26 -070094 device_kind_ = GetDeviceKind(model_id_);
95
Christopher Wiley34eae042015-03-18 10:25:08 -070096 std::string polling_period_str;
Vitaly Buka867b0882015-04-16 10:03:26 -070097 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
107void BuffetConfig::set_name(const std::string& name) {
108 CHECK(!name.empty());
109 name_ = name;
Christopher Wiley583d64b2015-03-24 14:30:17 -0700110}
111
112} // namespace buffet