blob: eee593f7b15ed8cb006d861bf64a47a63025de08 [file] [log] [blame]
Christopher Wileya4915c42014-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 Vakulenkob3aac252014-05-07 17:35:24 -07007#include <map>
Alex Vakulenko9ea5a322015-04-17 15:35:34 -07008#include <set>
Alex Vakulenkob3aac252014-05-07 17:35:24 -07009#include <string>
10
Christopher Wileya4915c42014-03-27 14:45:37 -070011#include <base/bind.h>
12#include <base/bind_helpers.h>
Alex Vakulenko665c8852014-09-11 16:57:24 -070013#include <base/json/json_reader.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070014#include <base/json/json_writer.h>
Christopher Wileycd419662015-02-06 17:51:43 -080015#include <base/time/time.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070016#include <chromeos/dbus/async_event_sequencer.h>
17#include <chromeos/dbus/exported_object_manager.h>
18#include <chromeos/errors/error.h>
Garret Kelly9a895382015-05-29 15:56:52 -040019#include <chromeos/http/http_transport.h>
Anton Muhin332df192014-11-22 05:59:14 +040020#include <chromeos/key_value_store.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070021#include <dbus/bus.h>
Christopher Wiley90016242014-04-01 17:33:29 -070022#include <dbus/object_path.h>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070023#include <dbus/values_util.h>
Christopher Wileya4915c42014-03-27 14:45:37 -070024
Vitaly Buka72410b22015-05-13 13:48:59 -070025#include "buffet/base_api_handler.h"
Alex Vakulenko665c8852014-09-11 16:57:24 -070026#include "buffet/commands/command_instance.h"
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070027#include "buffet/commands/schema_constants.h"
Vitaly Buka505c4132015-06-09 17:01:54 -070028#include "buffet/privet/constants.h"
29#include "buffet/privet/security_manager.h"
Vitaly Buka63cc3d22015-06-23 20:11:36 -070030#include "buffet/privet/shill_client.h"
Vitaly Buka505c4132015-06-09 17:01:54 -070031#include "buffet/privet/wifi_bootstrap_manager.h"
Alex Vakulenko57123b22014-10-28 13:50:16 -070032#include "buffet/states/state_change_queue.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070033#include "buffet/states/state_manager.h"
Christopher Wileye0fdeee2015-02-07 18:29:32 -080034#include "buffet/storage_impls.h"
Christopher Wileya4915c42014-03-27 14:45:37 -070035
Christopher Wiley2d2d92b2014-07-29 14:07:10 -070036using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wileyb5dd5ea2014-08-11 10:51:20 -070037using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wileya4915c42014-03-27 14:45:37 -070038
39namespace buffet {
40
Alex Vakulenko57123b22014-10-28 13:50:16 -070041namespace {
Vitaly Buka505c4132015-06-09 17:01:54 -070042
Alex Vakulenko57123b22014-10-28 13:50:16 -070043// Max of 100 state update events should be enough in the queue.
44const size_t kMaxStateChangeQueueSize = 100;
Garret Kelly9a895382015-05-29 15:56:52 -040045// The number of seconds each HTTP request will be allowed before timing out.
46const int kRequestTimeoutSeconds = 30;
Vitaly Buka505c4132015-06-09 17:01:54 -070047
48const char kPairingSessionIdKey[] = "sessionId";
49const char kPairingModeKey[] = "mode";
50const char kPairingCodeKey[] = "code";
51
Alex Vakulenko57123b22014-10-28 13:50:16 -070052} // anonymous namespace
53
Alex Vakulenkof2784de2014-08-15 11:49:35 -070054Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
55 : dbus_object_(object_manager.get(),
56 object_manager->GetBus(),
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070057 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
58}
Christopher Wileya4915c42014-03-27 14:45:37 -070059
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070060Manager::~Manager() {
61}
Alex Vakulenko57123b22014-10-28 13:50:16 -070062
Vitaly Bukaae96cc32015-06-09 17:22:18 -070063void Manager::Start(const Options& options, AsyncEventSequencer* sequencer) {
Alex Vakulenko95110752014-09-03 16:27:21 -070064 command_manager_ =
65 std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
Vitaly Buka5e6ff6c2015-05-11 15:41:33 -070066 command_manager_->AddOnCommandDefChanged(base::Bind(
67 &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr()));
Christopher Wileybb5b8482015-02-12 13:42:16 -080068 command_manager_->Startup(base::FilePath{"/etc/buffet"},
Vitaly Bukaae96cc32015-06-09 17:22:18 -070069 options.test_definitions_path);
Vitaly Buka72410b22015-05-13 13:48:59 -070070 state_change_queue_.reset(new StateChangeQueue(kMaxStateChangeQueueSize));
Alex Vakulenko57123b22014-10-28 13:50:16 -070071 state_manager_ = std::make_shared<StateManager>(state_change_queue_.get());
Vitaly Bukadbb8c352015-05-27 09:27:08 -070072 state_manager_->AddOnChangedCallback(
73 base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr()));
Alex Vakulenko07216fe2014-09-19 15:31:09 -070074 state_manager_->Startup();
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070075
Vitaly Bukaae96cc32015-06-09 17:22:18 -070076 std::unique_ptr<BuffetConfig> config{new BuffetConfig{options.state_path}};
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070077 config->AddOnChangedCallback(
78 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukaae96cc32015-06-09 17:22:18 -070079 config->Load(options.config_path);
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070080
Garret Kelly9a895382015-05-29 15:56:52 -040081 auto transport = chromeos::http::Transport::CreateDefault();
82 transport->SetDefaultTimeout(base::TimeDelta::FromSeconds(
83 kRequestTimeoutSeconds));
84
Vitaly Buka63cc3d22015-06-23 20:11:36 -070085 shill_client_.reset(new privetd::ShillClient(dbus_object_.GetBus(),
86 options.device_whitelist));
87
Christopher Wileye0fdeee2015-02-07 18:29:32 -080088 // TODO(avakulenko): Figure out security implications of storing
89 // device info state data unencrypted.
Vitaly Buka72410b22015-05-13 13:48:59 -070090 device_info_.reset(new DeviceRegistrationInfo(
Garret Kelly9a895382015-05-29 15:56:52 -040091 command_manager_, state_manager_, std::move(config), transport,
Vitaly Buka63cc3d22015-06-23 20:11:36 -070092 options.xmpp_enabled, shill_client_.get()));
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070093 device_info_->AddOnRegistrationChangedCallback(base::Bind(
94 &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Buka72410b22015-05-13 13:48:59 -070095
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070096 base_api_handler_.reset(new BaseApiHandler{
97 device_info_->AsWeakPtr(), state_manager_, command_manager_});
Vitaly Buka72410b22015-05-13 13:48:59 -070098
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070099 device_info_->Start();
Vitaly Bukaae96cc32015-06-09 17:22:18 -0700100
Alex Vakulenko2348e422014-11-21 08:57:57 -0800101 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Vitaly Bukaae96cc32015-06-09 17:22:18 -0700102 dbus_object_.RegisterAsync(
103 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
104
105 if (!options.privet.disable_privet)
106 StartPrivet(options.privet, sequencer);
107}
108
109void Manager::StartPrivet(const privetd::Manager::Options& options,
110 AsyncEventSequencer* sequencer) {
111 privet_.reset(new privetd::Manager{});
Vitaly Buka63cc3d22015-06-23 20:11:36 -0700112 privet_->Start(options, dbus_object_.GetBus(), shill_client_.get(),
113 device_info_.get(), command_manager_.get(),
114 state_manager_.get(), sequencer);
Vitaly Bukaae96cc32015-06-09 17:22:18 -0700115
116 if (privet_->GetWifiBootstrapManager()) {
117 privet_->GetWifiBootstrapManager()->RegisterStateListener(base::Bind(
118 &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr()));
119 } else {
120 UpdateWiFiBootstrapState(privetd::WifiBootstrapManager::kDisabled);
121 }
122
123 privet_->GetSecurityManager()->RegisterPairingListeners(
124 base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
125 base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
126 // TODO(wiley) Watch for appropriate state variables from |cloud_delegate|.
127}
128
129void Manager::Stop() {
130 if (privet_)
131 privet_->OnShutdown();
Christopher Wileya4915c42014-03-27 14:45:37 -0700132}
133
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700134void Manager::CheckDeviceRegistered(
135 DBusMethodResponsePtr<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700136 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700137 chromeos::ErrorPtr error;
Nathan Bullock5e022a32015-04-08 15:13:07 -0400138 bool registered = device_info_->HaveRegistrationCredentials(&error);
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700139 // If it fails due to any reason other than 'device not registered',
140 // treat it as a real error and report it to the caller.
141 if (!registered &&
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700142 !error->HasError(kErrorDomainGCD, "device_not_registered")) {
143 response->ReplyWithError(error.get());
144 return;
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700145 }
Christopher Wileya4915c42014-03-27 14:45:37 -0700146
Vitaly Bukaee7a3af2015-05-14 16:57:23 -0700147 response->Return(registered ? device_info_->GetConfig().device_id()
148 : std::string());
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700149}
150
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700151void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700152 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700153 std::shared_ptr<DBusMethodResponse<std::string>> shared_response =
154 std::move(response);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700155
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700156 device_info_->GetDeviceInfo(
157 base::Bind(&Manager::OnGetDeviceInfoSuccess,
158 weak_ptr_factory_.GetWeakPtr(), shared_response),
159 base::Bind(&Manager::OnGetDeviceInfoError,
160 weak_ptr_factory_.GetWeakPtr(), shared_response));
161}
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700162
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700163void Manager::OnGetDeviceInfoSuccess(
164 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
165 const base::DictionaryValue& device_info) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700166 std::string device_info_str;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700167 base::JSONWriter::WriteWithOptions(
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700168 device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700169 response->Return(device_info_str);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700170}
171
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700172void Manager::OnGetDeviceInfoError(
173 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
174 const chromeos::Error* error) {
175 response->ReplyWithError(error);
176}
177
178void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response,
Vitaly Bukacad2e332015-05-14 23:33:32 -0700179 const std::string& ticket_id) {
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400180 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700181
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700182 chromeos::ErrorPtr error;
Vitaly Bukacad2e332015-05-14 23:33:32 -0700183 std::string device_id = device_info_->RegisterDevice(ticket_id, &error);
David Zeuthen1dbad472015-02-12 15:24:21 -0500184 if (!device_id.empty()) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700185 response->Return(device_id);
David Zeuthen1dbad472015-02-12 15:24:21 -0500186 return;
187 }
188 if (!error) {
189 // TODO(zeuthen): This can be changed to CHECK(error) once
190 // RegisterDevice() has been fixed to set |error| when failing.
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700191 chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error",
David Zeuthen1dbad472015-02-12 15:24:21 -0500192 "device_id empty but error not set");
193 }
194 response->ReplyWithError(error.get());
Christopher Wileya4915c42014-03-27 14:45:37 -0700195}
196
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700197void Manager::UpdateState(DBusMethodResponsePtr<> response,
Alex Vakulenko2348e422014-11-21 08:57:57 -0800198 const chromeos::VariantDictionary& property_set) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700199 chromeos::ErrorPtr error;
Vitaly Buka247620b2015-05-26 15:42:20 -0700200 if (!state_manager_->SetProperties(property_set, &error))
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700201 response->ReplyWithError(error.get());
202 else
203 response->Return();
Christopher Wileya4915c42014-03-27 14:45:37 -0700204}
205
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800206bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
207 auto json = state_manager_->GetStateValuesAsJson(error);
208 if (!json)
209 return false;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400210 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700211 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800212 return true;
213}
214
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700215void Manager::AddCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Bukab2098422015-05-31 23:32:46 -0700216 const std::string& json_command,
217 const std::string& in_user_role) {
Alex Vakulenko665c8852014-09-11 16:57:24 -0700218 std::string error_message;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700219 std::unique_ptr<base::Value> value(
220 base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC,
221 nullptr, &error_message)
222 .release());
Vitaly Buka4547afa2015-06-09 09:46:53 -0700223 const base::DictionaryValue* command{nullptr};
224 if (!value || !value->GetAsDictionary(&command)) {
225 return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
226 chromeos::errors::json::kParseError,
227 error_message);
Alex Vakulenko665c8852014-09-11 16:57:24 -0700228 }
Vitaly Buka4547afa2015-06-09 09:46:53 -0700229
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700230 chromeos::ErrorPtr error;
Vitaly Bukab2098422015-05-31 23:32:46 -0700231 UserRole role;
Vitaly Buka4547afa2015-06-09 09:46:53 -0700232 if (!FromString(in_user_role, &role, &error))
233 return response->ReplyWithError(error.get());
Vitaly Bukab2098422015-05-31 23:32:46 -0700234
Vitaly Buka4547afa2015-06-09 09:46:53 -0700235 std::string id;
236 if (!command_manager_->AddCommand(*command, role, &id, &error))
237 return response->ReplyWithError(error.get());
Vitaly Bukab2098422015-05-31 23:32:46 -0700238
Vitaly Buka64fc5fc2015-03-24 12:42:24 -0700239 response->Return(id);
Alex Vakulenko665c8852014-09-11 16:57:24 -0700240}
241
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700242void Manager::GetCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Buka3886e8f2015-03-24 11:39:40 -0700243 const std::string& id) {
244 const CommandInstance* command = command_manager_->FindCommand(id);
245 if (!command) {
246 response->ReplyWithError(FROM_HERE, kErrorDomainGCD, "unknown_command",
247 "Can't find command with id: " + id);
248 return;
249 }
250 std::string command_str;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700251 base::JSONWriter::WriteWithOptions(
252 *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka3886e8f2015-03-24 11:39:40 -0700253 response->Return(command_str);
254}
255
Alex Vakulenko266b2b12015-06-19 11:01:42 -0700256void Manager::SetCommandVisibility(DBusMethodResponsePtr<> response,
257 const std::vector<std::string>& in_names,
258 const std::string& in_visibility) {
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -0700259 CommandDefinition::Visibility visibility;
260 chromeos::ErrorPtr error;
261 if (!visibility.FromString(in_visibility, &error)) {
262 response->ReplyWithError(error.get());
263 return;
264 }
265 if (!command_manager_->SetCommandVisibility(in_names, visibility, &error)) {
266 response->ReplyWithError(error.get());
267 return;
268 }
269 response->Return();
270}
271
Alex Vakulenko2348e422014-11-21 08:57:57 -0800272std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko7a1dc0b2014-08-15 11:45:46 -0700273 LOG(INFO) << "Received call to test method: " << message;
274 return message;
Christopher Wileyb76eb292014-05-05 16:09:16 -0700275}
276
Vitaly Buka505c4132015-06-09 17:01:54 -0700277bool Manager::EnableWiFiBootstrapping(
278 chromeos::ErrorPtr* error,
279 const dbus::ObjectPath& in_listener_path,
280 const chromeos::VariantDictionary& in_options) {
281 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
282 privetd::errors::kNotImplemented,
283 "Manual WiFi bootstrapping is not implemented");
284 return false;
285}
286
287bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
288 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
289 privetd::errors::kNotImplemented,
290 "Manual WiFi bootstrapping is not implemented");
291 return false;
292}
293
294bool Manager::EnableGCDBootstrapping(
295 chromeos::ErrorPtr* error,
296 const dbus::ObjectPath& in_listener_path,
297 const chromeos::VariantDictionary& in_options) {
298 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
299 privetd::errors::kNotImplemented,
300 "Manual GCD bootstrapping is not implemented");
301 return false;
302}
303
304bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
305 chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain,
306 privetd::errors::kNotImplemented,
307 "Manual GCD bootstrapping is not implemented");
308 return false;
309}
310
Vitaly Bukafa947062015-04-17 00:41:31 -0700311bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
312 const std::string& in_name,
313 const std::string& in_description,
314 const std::string& in_location) {
315 return device_info_->UpdateDeviceInfo(in_name, in_description, in_location,
316 error);
Christopher Wileyc900e482015-02-15 15:42:04 -0800317}
318
Vitaly Bukaff81db62015-05-14 21:25:45 -0700319bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error,
320 const std::string& client_id,
321 const std::string& client_secret,
322 const std::string& api_key,
323 const std::string& oauth_url,
324 const std::string& service_url) {
325 return device_info_->UpdateServiceConfig(client_id, client_secret, api_key,
326 oauth_url, service_url, error);
327}
328
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700329void Manager::OnCommandDefsChanged() {
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700330 // Limit only to commands that are visible to the local clients.
331 auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson(
332 [](const buffet::CommandDefinition* def) {
333 return def->GetVisibility().local;
Vitaly Bukadbb8c352015-05-27 09:27:08 -0700334 }, true, nullptr);
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700335 CHECK(commands);
336 std::string json;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700337 base::JSONWriter::WriteWithOptions(
338 *commands, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700339 dbus_adaptor_.SetCommandDefs(json);
340}
341
Vitaly Bukadbb8c352015-05-27 09:27:08 -0700342void Manager::OnStateChanged() {
343 auto state = state_manager_->GetStateValuesAsJson(nullptr);
344 CHECK(state);
345 std::string json;
346 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -0700347 *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Bukadbb8c352015-05-27 09:27:08 -0700348 dbus_adaptor_.SetState(json);
349}
350
Vitaly Bukaee7a3af2015-05-14 16:57:23 -0700351void Manager::OnRegistrationChanged(RegistrationStatus status) {
352 dbus_adaptor_.SetStatus(StatusToString(status));
353}
354
355void Manager::OnConfigChanged(const BuffetConfig& config) {
356 dbus_adaptor_.SetDeviceId(config.device_id());
357 dbus_adaptor_.SetOemName(config.oem_name());
358 dbus_adaptor_.SetModelName(config.model_name());
359 dbus_adaptor_.SetModelId(config.model_id());
360 dbus_adaptor_.SetName(config.name());
361 dbus_adaptor_.SetDescription(config.description());
362 dbus_adaptor_.SetLocation(config.location());
363 dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role());
364}
365
Vitaly Buka505c4132015-06-09 17:01:54 -0700366void Manager::UpdateWiFiBootstrapState(
367 privetd::WifiBootstrapManager::State state) {
Vitaly Bukab56872f2015-06-21 18:39:34 -0700368 if (auto wifi = privet_->GetWifiBootstrapManager()) {
369 const std::string& ssid{wifi->GetCurrentlyConnectedSsid()};
370 if (ssid != device_info_->GetConfig().last_configured_ssid()) {
371 BuffetConfig::Transaction change{device_info_->GetMutableConfig()};
372 change.set_last_configured_ssid(ssid);
373 }
374 }
375
Vitaly Buka505c4132015-06-09 17:01:54 -0700376 switch (state) {
377 case privetd::WifiBootstrapManager::kDisabled:
378 dbus_adaptor_.SetWiFiBootstrapState("disabled");
379 break;
380 case privetd::WifiBootstrapManager::kBootstrapping:
381 dbus_adaptor_.SetWiFiBootstrapState("waiting");
382 break;
383 case privetd::WifiBootstrapManager::kMonitoring:
384 dbus_adaptor_.SetWiFiBootstrapState("monitoring");
385 break;
386 case privetd::WifiBootstrapManager::kConnecting:
387 dbus_adaptor_.SetWiFiBootstrapState("connecting");
388 break;
389 }
390}
391
392void Manager::OnPairingStart(const std::string& session_id,
393 privetd::PairingType pairing_type,
394 const std::vector<uint8_t>& code) {
395 // For now, just overwrite the exposed PairInfo with
396 // the most recent pairing attempt.
397 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
398 {kPairingSessionIdKey, session_id},
399 {kPairingModeKey, PairingTypeToString(pairing_type)},
400 {kPairingCodeKey, code},
401 });
402}
403
404void Manager::OnPairingEnd(const std::string& session_id) {
405 auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
406 auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
407 if (it == exposed_pairing_attempt.end()) {
408 return;
409 }
410 std::string exposed_session{it->second.TryGet<std::string>()};
411 if (exposed_session == session_id) {
412 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
413 }
414}
415
Christopher Wileya4915c42014-03-27 14:45:37 -0700416} // namespace buffet