blob: ca0f0ab413bf3bbc21986f33f904c399dcd28e0b [file] [log] [blame]
Christopher Wiley4b5f04c2014-03-27 14:45:37 -07001// Copyright 2014 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/manager.h"
6
Alex Vakulenko7c3226e2014-05-07 17:35:24 -07007#include <map>
Alex Vakulenkoec41a0c2015-04-17 15:35:34 -07008#include <set>
Alex Vakulenko7c3226e2014-05-07 17:35:24 -07009#include <string>
10
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070011#include <base/bind.h>
12#include <base/bind_helpers.h>
Alex Vakulenko4f7778e2014-09-11 16:57:24 -070013#include <base/json/json_reader.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070014#include <base/json/json_writer.h>
Alex Vakulenkof7754542015-06-26 12:59:50 -070015#include <base/message_loop/message_loop.h>
Christopher Wiley8994e012015-02-06 17:51:43 -080016#include <base/time/time.h>
Alex Vakulenko24e5f5d2014-08-27 11:00:57 -070017#include <chromeos/dbus/async_event_sequencer.h>
18#include <chromeos/dbus/exported_object_manager.h>
19#include <chromeos/errors/error.h>
Garret Kelly89ba0362015-05-29 15:56:52 -040020#include <chromeos/http/http_transport.h>
Anton Muhin53e882a2014-11-22 05:59:14 +040021#include <chromeos/key_value_store.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070022#include <dbus/bus.h>
Christopher Wiley54028f92014-04-01 17:33:29 -070023#include <dbus/object_path.h>
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070024#include <dbus/values_util.h>
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070025
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070026#include "buffet/base_api_handler.h"
Alex Vakulenko4f7778e2014-09-11 16:57:24 -070027#include "buffet/commands/command_instance.h"
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -070028#include "buffet/commands/schema_constants.h"
Vitaly Buka7b3ba792015-06-09 17:01:54 -070029#include "buffet/privet/constants.h"
30#include "buffet/privet/security_manager.h"
Vitaly Bukada2312c2015-06-23 20:11:36 -070031#include "buffet/privet/shill_client.h"
Vitaly Buka7b3ba792015-06-09 17:01:54 -070032#include "buffet/privet/wifi_bootstrap_manager.h"
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070033#include "buffet/states/state_change_queue.h"
Alex Vakulenko95877b52014-09-19 15:31:09 -070034#include "buffet/states/state_manager.h"
Christopher Wiley357deca2015-02-07 18:29:32 -080035#include "buffet/storage_impls.h"
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070036
Christopher Wiley68c07cc2014-07-29 14:07:10 -070037using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wiley1aa980e2014-08-11 10:51:20 -070038using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070039
40namespace buffet {
41
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070042namespace {
Vitaly Buka7b3ba792015-06-09 17:01:54 -070043
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070044// Max of 100 state update events should be enough in the queue.
45const size_t kMaxStateChangeQueueSize = 100;
Garret Kelly89ba0362015-05-29 15:56:52 -040046// The number of seconds each HTTP request will be allowed before timing out.
47const int kRequestTimeoutSeconds = 30;
Vitaly Buka7b3ba792015-06-09 17:01:54 -070048
49const char kPairingSessionIdKey[] = "sessionId";
50const char kPairingModeKey[] = "mode";
51const char kPairingCodeKey[] = "code";
52
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070053} // anonymous namespace
54
Alex Vakulenkob6351532014-08-15 11:49:35 -070055Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
56 : dbus_object_(object_manager.get(),
57 object_manager->GetBus(),
Vitaly Buka91cc7152015-03-20 09:46:57 -070058 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
59}
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070060
Vitaly Buka91cc7152015-03-20 09:46:57 -070061Manager::~Manager() {
62}
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070063
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070064void Manager::Start(const Options& options, AsyncEventSequencer* sequencer) {
Alex Vakulenko05fa6812014-09-03 16:27:21 -070065 command_manager_ =
66 std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
Vitaly Bukae43871f2015-05-11 15:41:33 -070067 command_manager_->AddOnCommandDefChanged(base::Bind(
68 &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr()));
Christopher Wileyc39f4a32015-02-12 13:42:16 -080069 command_manager_->Startup(base::FilePath{"/etc/buffet"},
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070070 options.test_definitions_path);
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070071 state_change_queue_.reset(new StateChangeQueue(kMaxStateChangeQueueSize));
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070072 state_manager_ = std::make_shared<StateManager>(state_change_queue_.get());
Vitaly Buka2b30e7a2015-05-27 09:27:08 -070073 state_manager_->AddOnChangedCallback(
74 base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr()));
Alex Vakulenko95877b52014-09-19 15:31:09 -070075 state_manager_->Startup();
Vitaly Bukabf4ba652015-05-14 16:57:23 -070076
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070077 std::unique_ptr<BuffetConfig> config{new BuffetConfig{options.state_path}};
Vitaly Bukabf4ba652015-05-14 16:57:23 -070078 config->AddOnChangedCallback(
79 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070080 config->Load(options.config_path);
Vitaly Bukabf4ba652015-05-14 16:57:23 -070081
Garret Kelly89ba0362015-05-29 15:56:52 -040082 auto transport = chromeos::http::Transport::CreateDefault();
83 transport->SetDefaultTimeout(base::TimeDelta::FromSeconds(
84 kRequestTimeoutSeconds));
85
Vitaly Bukada2312c2015-06-23 20:11:36 -070086 shill_client_.reset(new privetd::ShillClient(dbus_object_.GetBus(),
87 options.device_whitelist));
88
Christopher Wiley357deca2015-02-07 18:29:32 -080089 // TODO(avakulenko): Figure out security implications of storing
90 // device info state data unencrypted.
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070091 device_info_.reset(new DeviceRegistrationInfo(
Garret Kelly89ba0362015-05-29 15:56:52 -040092 command_manager_, state_manager_, std::move(config), transport,
Alex Vakulenkof7754542015-06-26 12:59:50 -070093 base::MessageLoop::current()->task_runner(),
Vitaly Bukada2312c2015-06-23 20:11:36 -070094 options.xmpp_enabled, shill_client_.get()));
Vitaly Bukabf4ba652015-05-14 16:57:23 -070095 device_info_->AddOnRegistrationChangedCallback(base::Bind(
96 &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070097
Vitaly Bukacf30ff92015-05-27 16:00:21 -070098 base_api_handler_.reset(new BaseApiHandler{
99 device_info_->AsWeakPtr(), state_manager_, command_manager_});
Vitaly Bukae74fe3c2015-05-13 13:48:59 -0700100
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700101 device_info_->Start();
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700102
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800103 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700104 dbus_object_.RegisterAsync(
105 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
106
107 if (!options.privet.disable_privet)
108 StartPrivet(options.privet, sequencer);
109}
110
111void Manager::StartPrivet(const privetd::Manager::Options& options,
112 AsyncEventSequencer* sequencer) {
113 privet_.reset(new privetd::Manager{});
Vitaly Bukada2312c2015-06-23 20:11:36 -0700114 privet_->Start(options, dbus_object_.GetBus(), shill_client_.get(),
115 device_info_.get(), command_manager_.get(),
116 state_manager_.get(), sequencer);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700117
118 if (privet_->GetWifiBootstrapManager()) {
119 privet_->GetWifiBootstrapManager()->RegisterStateListener(base::Bind(
120 &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr()));
121 } else {
122 UpdateWiFiBootstrapState(privetd::WifiBootstrapManager::kDisabled);
123 }
124
125 privet_->GetSecurityManager()->RegisterPairingListeners(
126 base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
127 base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
128 // TODO(wiley) Watch for appropriate state variables from |cloud_delegate|.
129}
130
131void Manager::Stop() {
132 if (privet_)
133 privet_->OnShutdown();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700134}
135
Alex Vakulenko6c375262015-06-19 11:01:42 -0700136void Manager::CheckDeviceRegistered(
137 DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700138 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700139 chromeos::ErrorPtr error;
Alex Vakulenko19208c72015-06-23 15:12:22 -0700140 bool registered = device_info_->VerifyRegistrationCredentials(&error);
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700141 // If it fails due to any reason other than 'device not registered',
142 // treat it as a real error and report it to the caller.
143 if (!registered &&
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700144 !error->HasError(kErrorDomainGCD, "device_not_registered")) {
145 response->ReplyWithError(error.get());
146 return;
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700147 }
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700148
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700149 response->Return(registered ? device_info_->GetConfig().device_id()
150 : std::string());
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700151}
152
Alex Vakulenko6c375262015-06-19 11:01:42 -0700153void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700154 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
Alex Vakulenko6c375262015-06-19 11:01:42 -0700155 std::shared_ptr<DBusMethodResponse<std::string>> shared_response =
156 std::move(response);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700157
Alex Vakulenko6c375262015-06-19 11:01:42 -0700158 device_info_->GetDeviceInfo(
159 base::Bind(&Manager::OnGetDeviceInfoSuccess,
160 weak_ptr_factory_.GetWeakPtr(), shared_response),
161 base::Bind(&Manager::OnGetDeviceInfoError,
162 weak_ptr_factory_.GetWeakPtr(), shared_response));
163}
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700164
Alex Vakulenko6c375262015-06-19 11:01:42 -0700165void Manager::OnGetDeviceInfoSuccess(
166 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
167 const base::DictionaryValue& device_info) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700168 std::string device_info_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700169 base::JSONWriter::WriteWithOptions(
Alex Vakulenko6c375262015-06-19 11:01:42 -0700170 device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700171 response->Return(device_info_str);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700172}
173
Alex Vakulenko6c375262015-06-19 11:01:42 -0700174void Manager::OnGetDeviceInfoError(
175 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
176 const chromeos::Error* error) {
177 response->ReplyWithError(error);
178}
179
180void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response,
Vitaly Bukacbadabe2015-05-14 23:33:32 -0700181 const std::string& ticket_id) {
Anton Muhin9cc03fd2014-10-16 18:59:57 +0400182 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700183
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700184 chromeos::ErrorPtr error;
Vitaly Bukacbadabe2015-05-14 23:33:32 -0700185 std::string device_id = device_info_->RegisterDevice(ticket_id, &error);
David Zeuthen2101c082015-02-12 15:24:21 -0500186 if (!device_id.empty()) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700187 response->Return(device_id);
David Zeuthen2101c082015-02-12 15:24:21 -0500188 return;
189 }
190 if (!error) {
191 // TODO(zeuthen): This can be changed to CHECK(error) once
192 // RegisterDevice() has been fixed to set |error| when failing.
Vitaly Buka91cc7152015-03-20 09:46:57 -0700193 chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error",
David Zeuthen2101c082015-02-12 15:24:21 -0500194 "device_id empty but error not set");
195 }
196 response->ReplyWithError(error.get());
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700197}
198
Alex Vakulenko6c375262015-06-19 11:01:42 -0700199void Manager::UpdateState(DBusMethodResponsePtr<> response,
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800200 const chromeos::VariantDictionary& property_set) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700201 chromeos::ErrorPtr error;
Vitaly Buka14eb0ba2015-05-26 15:42:20 -0700202 if (!state_manager_->SetProperties(property_set, &error))
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700203 response->ReplyWithError(error.get());
204 else
205 response->Return();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700206}
207
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800208bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
209 auto json = state_manager_->GetStateValuesAsJson(error);
210 if (!json)
211 return false;
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400212 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700213 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800214 return true;
215}
216
Alex Vakulenko6c375262015-06-19 11:01:42 -0700217void Manager::AddCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700218 const std::string& json_command,
219 const std::string& in_user_role) {
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700220 std::string error_message;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700221 std::unique_ptr<base::Value> value(
222 base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC,
223 nullptr, &error_message)
224 .release());
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700225 const base::DictionaryValue* command{nullptr};
226 if (!value || !value->GetAsDictionary(&command)) {
227 return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
228 chromeos::errors::json::kParseError,
229 error_message);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700230 }
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700231
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700232 chromeos::ErrorPtr error;
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700233 UserRole role;
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700234 if (!FromString(in_user_role, &role, &error))
235 return response->ReplyWithError(error.get());
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700236
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700237 std::string id;
238 if (!command_manager_->AddCommand(*command, role, &id, &error))
239 return response->ReplyWithError(error.get());
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700240
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700241 response->Return(id);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700242}
243
Alex Vakulenko6c375262015-06-19 11:01:42 -0700244void Manager::GetCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Buka5515de02015-03-24 11:39:40 -0700245 const std::string& id) {
246 const CommandInstance* command = command_manager_->FindCommand(id);
247 if (!command) {
248 response->ReplyWithError(FROM_HERE, kErrorDomainGCD, "unknown_command",
249 "Can't find command with id: " + id);
250 return;
251 }
252 std::string command_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700253 base::JSONWriter::WriteWithOptions(
254 *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka5515de02015-03-24 11:39:40 -0700255 response->Return(command_str);
256}
257
Alex Vakulenko6c375262015-06-19 11:01:42 -0700258void Manager::SetCommandVisibility(DBusMethodResponsePtr<> response,
259 const std::vector<std::string>& in_names,
260 const std::string& in_visibility) {
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -0700261 CommandDefinition::Visibility visibility;
262 chromeos::ErrorPtr error;
263 if (!visibility.FromString(in_visibility, &error)) {
264 response->ReplyWithError(error.get());
265 return;
266 }
267 if (!command_manager_->SetCommandVisibility(in_names, visibility, &error)) {
268 response->ReplyWithError(error.get());
269 return;
270 }
271 response->Return();
272}
273
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800274std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko35e3bab2014-08-15 11:45:46 -0700275 LOG(INFO) << "Received call to test method: " << message;
276 return message;
Christopher Wiley2ffa0042014-05-05 16:09:16 -0700277}
278
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700279bool Manager::EnableWiFiBootstrapping(
280 chromeos::ErrorPtr* error,
281 const dbus::ObjectPath& in_listener_path,
282 const chromeos::VariantDictionary& in_options) {
283 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
284 privetd::errors::kNotImplemented,
285 "Manual WiFi bootstrapping is not implemented");
286 return false;
287}
288
289bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
290 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
291 privetd::errors::kNotImplemented,
292 "Manual WiFi bootstrapping is not implemented");
293 return false;
294}
295
296bool Manager::EnableGCDBootstrapping(
297 chromeos::ErrorPtr* error,
298 const dbus::ObjectPath& in_listener_path,
299 const chromeos::VariantDictionary& in_options) {
300 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
301 privetd::errors::kNotImplemented,
302 "Manual GCD bootstrapping is not implemented");
303 return false;
304}
305
306bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
307 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
308 privetd::errors::kNotImplemented,
309 "Manual GCD bootstrapping is not implemented");
310 return false;
311}
312
Vitaly Buka760d6322015-04-17 00:41:31 -0700313bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
314 const std::string& in_name,
315 const std::string& in_description,
316 const std::string& in_location) {
317 return device_info_->UpdateDeviceInfo(in_name, in_description, in_location,
318 error);
Christopher Wiley2f772932015-02-15 15:42:04 -0800319}
320
Vitaly Buka7cd01972015-05-14 21:25:45 -0700321bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error,
322 const std::string& client_id,
323 const std::string& client_secret,
324 const std::string& api_key,
325 const std::string& oauth_url,
326 const std::string& service_url) {
327 return device_info_->UpdateServiceConfig(client_id, client_secret, api_key,
328 oauth_url, service_url, error);
329}
330
Vitaly Buka1fa69012015-03-18 23:33:44 -0700331void Manager::OnCommandDefsChanged() {
Alex Vakulenkoec41a0c2015-04-17 15:35:34 -0700332 // Limit only to commands that are visible to the local clients.
333 auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson(
334 [](const buffet::CommandDefinition* def) {
335 return def->GetVisibility().local;
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700336 }, true, nullptr);
Vitaly Buka1fa69012015-03-18 23:33:44 -0700337 CHECK(commands);
338 std::string json;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700339 base::JSONWriter::WriteWithOptions(
340 *commands, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka1fa69012015-03-18 23:33:44 -0700341 dbus_adaptor_.SetCommandDefs(json);
342}
343
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700344void Manager::OnStateChanged() {
345 auto state = state_manager_->GetStateValuesAsJson(nullptr);
346 CHECK(state);
347 std::string json;
348 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700349 *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700350 dbus_adaptor_.SetState(json);
351}
352
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700353void Manager::OnRegistrationChanged(RegistrationStatus status) {
354 dbus_adaptor_.SetStatus(StatusToString(status));
355}
356
357void Manager::OnConfigChanged(const BuffetConfig& config) {
358 dbus_adaptor_.SetDeviceId(config.device_id());
359 dbus_adaptor_.SetOemName(config.oem_name());
360 dbus_adaptor_.SetModelName(config.model_name());
361 dbus_adaptor_.SetModelId(config.model_id());
362 dbus_adaptor_.SetName(config.name());
363 dbus_adaptor_.SetDescription(config.description());
364 dbus_adaptor_.SetLocation(config.location());
365 dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role());
366}
367
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700368void Manager::UpdateWiFiBootstrapState(
369 privetd::WifiBootstrapManager::State state) {
Vitaly Buka0c68cac2015-06-21 18:39:34 -0700370 if (auto wifi = privet_->GetWifiBootstrapManager()) {
371 const std::string& ssid{wifi->GetCurrentlyConnectedSsid()};
372 if (ssid != device_info_->GetConfig().last_configured_ssid()) {
373 BuffetConfig::Transaction change{device_info_->GetMutableConfig()};
374 change.set_last_configured_ssid(ssid);
375 }
376 }
377
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700378 switch (state) {
379 case privetd::WifiBootstrapManager::kDisabled:
380 dbus_adaptor_.SetWiFiBootstrapState("disabled");
381 break;
382 case privetd::WifiBootstrapManager::kBootstrapping:
383 dbus_adaptor_.SetWiFiBootstrapState("waiting");
384 break;
385 case privetd::WifiBootstrapManager::kMonitoring:
386 dbus_adaptor_.SetWiFiBootstrapState("monitoring");
387 break;
388 case privetd::WifiBootstrapManager::kConnecting:
389 dbus_adaptor_.SetWiFiBootstrapState("connecting");
390 break;
391 }
392}
393
394void Manager::OnPairingStart(const std::string& session_id,
395 privetd::PairingType pairing_type,
396 const std::vector<uint8_t>& code) {
397 // For now, just overwrite the exposed PairInfo with
398 // the most recent pairing attempt.
399 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
400 {kPairingSessionIdKey, session_id},
401 {kPairingModeKey, PairingTypeToString(pairing_type)},
402 {kPairingCodeKey, code},
403 });
404}
405
406void Manager::OnPairingEnd(const std::string& session_id) {
407 auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
408 auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
409 if (it == exposed_pairing_attempt.end()) {
410 return;
411 }
412 std::string exposed_session{it->second.TryGet<std::string>()};
413 if (exposed_session == session_id) {
414 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
415 }
416}
417
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700418} // namespace buffet