blob: a97b65437587b97013b7a5f99c40adc4ea513da6 [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
Vitaly Buka2c1029f2015-04-30 22:47:18 -070042bool IsValidAccessRole(const std::string& role) {
Vitaly Buka94675202015-05-13 11:48:54 -070043 return role == "none" || role == "viewer" || role == "user";
Vitaly Buka2c1029f2015-04-30 22:47:18 -070044}
45
Vitaly Buka867b0882015-04-16 10:03:26 -070046} // namespace
47
Christopher Wiley583d64b2015-03-24 14:30:17 -070048namespace buffet {
Vitaly Buka867b0882015-04-16 10:03:26 -070049
Christopher Wiley583d64b2015-03-24 14:30:17 -070050namespace config_keys {
51
Vitaly Buka867b0882015-04-16 10:03:26 -070052const char kClientId[] = "client_id";
53const char kClientSecret[] = "client_secret";
54const char kApiKey[] = "api_key";
55const char kOAuthURL[] = "oauth_url";
56const char kServiceURL[] = "service_url";
57const char kName[] = "name";
58const char kDescription[] = "description";
59const char kLocation[] = "location";
Vitaly Buka4a3a9a22015-05-13 16:06:01 -070060const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role";
61const char kLocalDiscoveryEnabled[] = "local_discovery_enabled";
62const char kLocalPairingEnabled[] = "local_pairing_enabled";
Vitaly Buka867b0882015-04-16 10:03:26 -070063const char kOemName[] = "oem_name";
64const char kModelName[] = "model_name";
65const char kModelId[] = "model_id";
Vitaly Buka2c1029f2015-04-30 22:47:18 -070066const char kPollingPeriodMs[] = "polling_period_ms";
Christopher Wiley583d64b2015-03-24 14:30:17 -070067
68} // namespace config_keys
69
70void BuffetConfig::Load(const base::FilePath& config_path) {
71 chromeos::KeyValueStore store;
72 if (store.Load(config_path)) {
73 Load(store);
74 }
75}
76
77void BuffetConfig::Load(const chromeos::KeyValueStore& store) {
78 store.GetString(config_keys::kClientId, &client_id_);
Vitaly Buka867b0882015-04-16 10:03:26 -070079 CHECK(!client_id_.empty());
80
Christopher Wiley583d64b2015-03-24 14:30:17 -070081 store.GetString(config_keys::kClientSecret, &client_secret_);
Vitaly Buka867b0882015-04-16 10:03:26 -070082 CHECK(!client_secret_.empty());
83
Christopher Wiley583d64b2015-03-24 14:30:17 -070084 store.GetString(config_keys::kApiKey, &api_key_);
Vitaly Buka867b0882015-04-16 10:03:26 -070085 CHECK(!api_key_.empty());
86
Christopher Wiley583d64b2015-03-24 14:30:17 -070087 store.GetString(config_keys::kOAuthURL, &oauth_url_);
Vitaly Buka867b0882015-04-16 10:03:26 -070088 CHECK(!oauth_url_.empty());
89
Christopher Wiley583d64b2015-03-24 14:30:17 -070090 store.GetString(config_keys::kServiceURL, &service_url_);
Vitaly Buka867b0882015-04-16 10:03:26 -070091 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 Wiley583d64b2015-03-24 14:30:17 -070099 store.GetString(config_keys::kModelId, &model_id_);
Vitaly Buka867b0882015-04-16 10:03:26 -0700100 device_kind_ = GetDeviceKind(model_id_);
101
Christopher Wiley34eae042015-03-18 10:25:08 -0700102 std::string polling_period_str;
Vitaly Buka867b0882015-04-16 10:03:26 -0700103 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 Buka2c1029f2015-04-30 22:47:18 -0700111
Vitaly Buka4a3a9a22015-05-13 16:06:01 -0700112 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 Buka867b0882015-04-16 10:03:26 -0700120}
121
Vitaly Buka4a3a9a22015-05-13 16:06:01 -0700122bool BuffetConfig::set_name(const std::string& name) {
123 if (name.empty()) {
124 LOG(ERROR) << "Invalid name: " << name;
125 return false;
Vitaly Buka2c1029f2015-04-30 22:47:18 -0700126 }
Vitaly Buka4a3a9a22015-05-13 16:06:01 -0700127 name_ = name;
128 return true;
129}
130
131bool 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 Buka2c1029f2015-04-30 22:47:18 -0700138}
139
Christopher Wiley583d64b2015-03-24 14:30:17 -0700140} // namespace buffet