blob: 10bc71fa01c70023d3e71a71cfd4c3a650b0947d [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>
Vitaly Bukae74c8722015-08-13 00:33:00 -070022#include <chromeos/message_loops/message_loop.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070023#include <dbus/bus.h>
Christopher Wiley54028f92014-04-01 17:33:29 -070024#include <dbus/object_path.h>
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070025#include <dbus/values_util.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -070026#include <weave/enum_to_string.h>
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070027
Vitaly Buka1175a9b2015-08-15 10:42:17 -070028#include "buffet/buffet_config.h"
Vitaly Bukaa0305d32015-07-27 16:08:51 -070029#include "buffet/dbus_command_dispatcher.h"
30#include "buffet/dbus_conversion.h"
Vitaly Buka00882b72015-08-06 00:32:11 -070031#include "buffet/http_transport_client.h"
Alex Vakulenkof0f55342015-08-18 15:51:40 -070032#include "buffet/mdns_client.h"
33#include "buffet/network_client.h"
Vitaly Buka4f771532015-08-14 14:58:39 -070034#include "buffet/weave_error_conversion.h"
Alex Vakulenko1642bec2015-08-19 09:34:58 -070035#include "buffet/webserv_client.h"
Vitaly Buka03319c22015-07-17 14:48:30 -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
Vitaly Buka7b3ba792015-06-09 17:01:54 -070044const char kPairingSessionIdKey[] = "sessionId";
45const char kPairingModeKey[] = "mode";
46const char kPairingCodeKey[] = "code";
47
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070048const char kErrorDomain[] = "buffet";
49const char kNotImplemented[] = "notImplemented";
50
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070051} // anonymous namespace
52
Vitaly Bukae74c8722015-08-13 00:33:00 -070053class Manager::TaskRunner : public weave::TaskRunner {
54 public:
55 void PostDelayedTask(const tracked_objects::Location& from_here,
56 const base::Closure& task,
57 base::TimeDelta delay) override {
58 chromeos::MessageLoop::current()->PostDelayedTask(from_here, task, delay);
59 }
60};
61
Alex Vakulenkob6351532014-08-15 11:49:35 -070062Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
63 : dbus_object_(object_manager.get(),
64 object_manager->GetBus(),
Vitaly Buka91cc7152015-03-20 09:46:57 -070065 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
66}
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070067
Vitaly Buka91cc7152015-03-20 09:46:57 -070068Manager::~Manager() {
69}
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070070
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070071void Manager::Start(const weave::Device::Options& options,
Vitaly Bukabecd4612015-08-16 23:31:55 -070072 const BuffetConfigPaths& paths,
Vitaly Buka58a288b2015-07-31 00:33:31 -070073 const std::set<std::string>& device_whitelist,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070074 AsyncEventSequencer* sequencer) {
Vitaly Bukae74c8722015-08-13 00:33:00 -070075 task_runner_.reset(new TaskRunner{});
Vitaly Buka00882b72015-08-06 00:32:11 -070076 http_client_.reset(new HttpTransportClient);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070077 network_client_ = NetworkClient::CreateInstance(device_whitelist);
Christopher Wileye925bb32015-08-03 20:09:18 -070078#ifdef BUFFET_USE_WIFI_BOOTSTRAPPING
Alex Vakulenkof0f55342015-08-18 15:51:40 -070079 if (!options.disable_privet) {
80 mdns_client_ = MdnsClient::CreateInstance();
Alex Vakulenko1642bec2015-08-19 09:34:58 -070081 web_serv_client_.reset(new WebServClient{dbus_object_.GetBus(), sequencer});
Alex Vakulenkof0f55342015-08-18 15:51:40 -070082 }
Christopher Wileye925bb32015-08-03 20:09:18 -070083#endif // BUFFET_USE_WIFI_BOOTSTRAPPING
Vitaly Buka7042c582015-07-30 17:02:14 -070084
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070085 device_ = weave::Device::Create();
Vitaly Bukabecd4612015-08-16 23:31:55 -070086 config_.reset(new BuffetConfig{paths});
Vitaly Buka1175a9b2015-08-15 10:42:17 -070087 config_->AddOnChangedCallback(
88 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
89
90 device_->Start(options, config_.get(), task_runner_.get(), http_client_.get(),
Alex Vakulenkof0f55342015-08-18 15:51:40 -070091 network_client_.get(), mdns_client_.get(),
Alex Vakulenko1642bec2015-08-19 09:34:58 -070092 web_serv_client_.get());
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070093
Vitaly Bukaa0305d32015-07-27 16:08:51 -070094 command_dispatcher_.reset(new DBusCommandDispacher{
Vitaly Bukae8c3bab2015-07-29 16:39:34 -070095 dbus_object_.GetObjectManager(), device_->GetCommands()});
96
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070097 device_->GetState()->AddOnChangedCallback(
Vitaly Buka2b30e7a2015-05-27 09:27:08 -070098 base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukabf4ba652015-05-14 16:57:23 -070099
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700100 device_->GetCloud()->AddOnRegistrationChangedCallback(base::Bind(
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700101 &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -0700102
Vitaly Buka1d63c9c2015-07-17 14:34:35 -0700103 if (device_->GetPrivet()) {
104 device_->GetPrivet()->AddOnWifiSetupChangedCallback(base::Bind(
105 &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -0700106
Vitaly Buka1d63c9c2015-07-17 14:34:35 -0700107 device_->GetPrivet()->AddOnPairingChangedCallbacks(
108 base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
109 base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
110 } else {
111 UpdateWiFiBootstrapState(weave::WifiSetupState::kDisabled);
112 }
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700113
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800114 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700115 dbus_object_.RegisterAsync(
116 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700117}
118
119void Manager::Stop() {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700120 device_.reset();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700121}
122
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700123// TODO(vitalybuka): Remove, it's just duplicate of property.
Alex Vakulenko6c375262015-06-19 11:01:42 -0700124void Manager::CheckDeviceRegistered(
125 DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700126 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700127 response->Return(dbus_adaptor_.GetDeviceId());
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700128}
129
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700130// TODO(vitalybuka): Remove or rename to leave for testing.
Alex Vakulenko6c375262015-06-19 11:01:42 -0700131void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700132 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
Alex Vakulenko6c375262015-06-19 11:01:42 -0700133 std::shared_ptr<DBusMethodResponse<std::string>> shared_response =
134 std::move(response);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700135
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700136 device_->GetCloud()->GetDeviceInfo(
Alex Vakulenko6c375262015-06-19 11:01:42 -0700137 base::Bind(&Manager::OnGetDeviceInfoSuccess,
138 weak_ptr_factory_.GetWeakPtr(), shared_response),
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700139 base::Bind(&Manager::OnGetDeviceInfoError, weak_ptr_factory_.GetWeakPtr(),
140 shared_response));
Alex Vakulenko6c375262015-06-19 11:01:42 -0700141}
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700142
Alex Vakulenko6c375262015-06-19 11:01:42 -0700143void Manager::OnGetDeviceInfoSuccess(
144 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
145 const base::DictionaryValue& device_info) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700146 std::string device_info_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700147 base::JSONWriter::WriteWithOptions(
Alex Vakulenko6c375262015-06-19 11:01:42 -0700148 device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700149 response->Return(device_info_str);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700150}
151
Alex Vakulenko6c375262015-06-19 11:01:42 -0700152void Manager::OnGetDeviceInfoError(
153 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
Vitaly Buka4f771532015-08-14 14:58:39 -0700154 const weave::Error* error) {
155 chromeos::ErrorPtr chromeos_error;
156 ConvertError(*error, &chromeos_error);
157 response->ReplyWithError(chromeos_error.get());
Alex Vakulenko6c375262015-06-19 11:01:42 -0700158}
159
160void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response,
Vitaly Bukacbadabe2015-05-14 23:33:32 -0700161 const std::string& ticket_id) {
Anton Muhin9cc03fd2014-10-16 18:59:57 +0400162 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700163
Vitaly Buka4f771532015-08-14 14:58:39 -0700164 weave::ErrorPtr error;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700165 std::string device_id =
166 device_->GetCloud()->RegisterDevice(ticket_id, &error);
David Zeuthen2101c082015-02-12 15:24:21 -0500167 if (!device_id.empty()) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700168 response->Return(device_id);
David Zeuthen2101c082015-02-12 15:24:21 -0500169 return;
170 }
Vitaly Buka4f771532015-08-14 14:58:39 -0700171 chromeos::ErrorPtr chromeos_error;
172 ConvertError(*error, &chromeos_error);
173 response->ReplyWithError(chromeos_error.get());
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700174}
175
Alex Vakulenko6c375262015-06-19 11:01:42 -0700176void Manager::UpdateState(DBusMethodResponsePtr<> response,
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800177 const chromeos::VariantDictionary& property_set) {
Vitaly Buka4f771532015-08-14 14:58:39 -0700178 chromeos::ErrorPtr chromeos_error;
179 auto properties =
180 DictionaryFromDBusVariantDictionary(property_set, &chromeos_error);
Vitaly Bukafb2584b2015-07-28 21:39:45 -0700181 if (!properties)
Vitaly Buka4f771532015-08-14 14:58:39 -0700182 response->ReplyWithError(chromeos_error.get());
Vitaly Bukafb2584b2015-07-28 21:39:45 -0700183
Vitaly Buka4f771532015-08-14 14:58:39 -0700184 weave::ErrorPtr error;
185 if (!device_->GetState()->SetProperties(*properties, &error)) {
186 ConvertError(*error, &chromeos_error);
187 response->ReplyWithError(chromeos_error.get());
188 }
189 response->Return();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700190}
191
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800192bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
Vitaly Bukacd5e5562015-07-28 15:33:55 -0700193 auto json = device_->GetState()->GetStateValuesAsJson();
194 CHECK(json);
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400195 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700196 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800197 return true;
198}
199
Alex Vakulenko6c375262015-06-19 11:01:42 -0700200void Manager::AddCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700201 const std::string& json_command,
202 const std::string& in_user_role) {
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700203 std::string error_message;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700204 std::unique_ptr<base::Value> value(
205 base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC,
206 nullptr, &error_message)
207 .release());
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700208 const base::DictionaryValue* command{nullptr};
209 if (!value || !value->GetAsDictionary(&command)) {
210 return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
211 chromeos::errors::json::kParseError,
212 error_message);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700213 }
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700214
Vitaly Buka4f771532015-08-14 14:58:39 -0700215 chromeos::ErrorPtr chromeos_error;
Vitaly Buka3b086312015-07-09 14:59:23 -0700216 weave::UserRole role;
Vitaly Buka03319c22015-07-17 14:48:30 -0700217 if (!StringToEnum(in_user_role, &role)) {
Vitaly Buka4f771532015-08-14 14:58:39 -0700218 chromeos::Error::AddToPrintf(&chromeos_error, FROM_HERE, kErrorDomain,
Vitaly Buka03319c22015-07-17 14:48:30 -0700219 "invalid_user_role", "Invalid role: '%s'",
220 in_user_role.c_str());
Vitaly Buka4f771532015-08-14 14:58:39 -0700221 return response->ReplyWithError(chromeos_error.get());
Vitaly Buka03319c22015-07-17 14:48:30 -0700222 }
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700223
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700224 std::string id;
Vitaly Buka4f771532015-08-14 14:58:39 -0700225 weave::ErrorPtr error;
226 if (!device_->GetCommands()->AddCommand(*command, role, &id, &error)) {
227 ConvertError(*error, &chromeos_error);
228 return response->ReplyWithError(chromeos_error.get());
229 }
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700230
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700231 response->Return(id);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700232}
233
Alex Vakulenko6c375262015-06-19 11:01:42 -0700234void Manager::GetCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Buka5515de02015-03-24 11:39:40 -0700235 const std::string& id) {
Vitaly Bukaba463732015-07-14 19:19:39 -0700236 const weave::Command* command = device_->GetCommands()->FindCommand(id);
Vitaly Buka5515de02015-03-24 11:39:40 -0700237 if (!command) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700238 response->ReplyWithError(FROM_HERE, kErrorDomain, "unknown_command",
Vitaly Buka5515de02015-03-24 11:39:40 -0700239 "Can't find command with id: " + id);
240 return;
241 }
242 std::string command_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700243 base::JSONWriter::WriteWithOptions(
244 *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka5515de02015-03-24 11:39:40 -0700245 response->Return(command_str);
246}
247
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800248std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko35e3bab2014-08-15 11:45:46 -0700249 LOG(INFO) << "Received call to test method: " << message;
250 return message;
Christopher Wiley2ffa0042014-05-05 16:09:16 -0700251}
252
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700253bool Manager::EnableWiFiBootstrapping(
254 chromeos::ErrorPtr* error,
255 const dbus::ObjectPath& in_listener_path,
256 const chromeos::VariantDictionary& in_options) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700257 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700258 "Manual WiFi bootstrapping is not implemented");
259 return false;
260}
261
262bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700263 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700264 "Manual WiFi bootstrapping is not implemented");
265 return false;
266}
267
268bool Manager::EnableGCDBootstrapping(
269 chromeos::ErrorPtr* error,
270 const dbus::ObjectPath& in_listener_path,
271 const chromeos::VariantDictionary& in_options) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700272 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700273 "Manual GCD bootstrapping is not implemented");
274 return false;
275}
276
277bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700278 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700279 "Manual GCD bootstrapping is not implemented");
280 return false;
281}
282
Vitaly Buka4f771532015-08-14 14:58:39 -0700283bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* chromeos_error,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700284 const std::string& name,
285 const std::string& description,
286 const std::string& location) {
Vitaly Buka4f771532015-08-14 14:58:39 -0700287 weave::ErrorPtr error;
288 if (!device_->GetCloud()->UpdateDeviceInfo(name, description, location,
289 &error)) {
290 ConvertError(*error, chromeos_error);
291 return false;
292 }
293 return true;
Christopher Wiley2f772932015-02-15 15:42:04 -0800294}
295
Vitaly Buka4f771532015-08-14 14:58:39 -0700296bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* chromeos_error,
Vitaly Buka7cd01972015-05-14 21:25:45 -0700297 const std::string& client_id,
298 const std::string& client_secret,
299 const std::string& api_key,
300 const std::string& oauth_url,
301 const std::string& service_url) {
Vitaly Buka4f771532015-08-14 14:58:39 -0700302 weave::ErrorPtr error;
303 if (!device_->GetCloud()->UpdateServiceConfig(
304 client_id, client_secret, api_key, oauth_url, service_url, &error)) {
305 ConvertError(*error, chromeos_error);
306 return false;
307 }
308 return true;
Vitaly Buka7cd01972015-05-14 21:25:45 -0700309}
310
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700311void Manager::OnStateChanged() {
Vitaly Bukacd5e5562015-07-28 15:33:55 -0700312 auto state = device_->GetState()->GetStateValuesAsJson();
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700313 CHECK(state);
314 std::string json;
315 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700316 *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700317 dbus_adaptor_.SetState(json);
318}
319
Vitaly Buka3b086312015-07-09 14:59:23 -0700320void Manager::OnRegistrationChanged(weave::RegistrationStatus status) {
Vitaly Buka03319c22015-07-17 14:48:30 -0700321 dbus_adaptor_.SetStatus(weave::EnumToString(status));
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700322}
323
Vitaly Buka00b83492015-07-20 00:37:48 -0700324void Manager::OnConfigChanged(const weave::Settings& settings) {
325 dbus_adaptor_.SetDeviceId(settings.device_id);
326 dbus_adaptor_.SetOemName(settings.oem_name);
327 dbus_adaptor_.SetModelName(settings.model_name);
328 dbus_adaptor_.SetModelId(settings.model_id);
329 dbus_adaptor_.SetName(settings.name);
330 dbus_adaptor_.SetDescription(settings.description);
331 dbus_adaptor_.SetLocation(settings.location);
332 dbus_adaptor_.SetAnonymousAccessRole(settings.local_anonymous_access_role);
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700333}
334
Vitaly Buka03319c22015-07-17 14:48:30 -0700335void Manager::UpdateWiFiBootstrapState(weave::WifiSetupState state) {
336 dbus_adaptor_.SetWiFiBootstrapState(weave::EnumToString(state));
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700337}
338
339void Manager::OnPairingStart(const std::string& session_id,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700340 weave::PairingType pairing_type,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700341 const std::vector<uint8_t>& code) {
342 // For now, just overwrite the exposed PairInfo with
343 // the most recent pairing attempt.
344 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
345 {kPairingSessionIdKey, session_id},
Vitaly Buka03319c22015-07-17 14:48:30 -0700346 {kPairingModeKey, weave::EnumToString(pairing_type)},
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700347 {kPairingCodeKey, code},
348 });
349}
350
351void Manager::OnPairingEnd(const std::string& session_id) {
352 auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
353 auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
354 if (it == exposed_pairing_attempt.end()) {
355 return;
356 }
357 std::string exposed_session{it->second.TryGet<std::string>()};
358 if (exposed_session == session_id) {
359 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
360 }
361}
362
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700363} // namespace buffet