blob: f6b27a351ee9df00d8705a039fde62e9b7526e0f [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 Bukaa0305d32015-07-27 16:08:51 -070026#include "buffet/dbus_command_dispatcher.h"
27#include "buffet/dbus_conversion.h"
Vitaly Buka7042c582015-07-30 17:02:14 -070028#include "buffet/peerd_client.h"
Vitaly Buka03319c22015-07-17 14:48:30 -070029#include "weave/enum_to_string.h"
30
Christopher Wiley68c07cc2014-07-29 14:07:10 -070031using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wiley1aa980e2014-08-11 10:51:20 -070032using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070033
34namespace buffet {
35
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070036namespace {
Vitaly Buka7b3ba792015-06-09 17:01:54 -070037
Vitaly Buka7b3ba792015-06-09 17:01:54 -070038const char kPairingSessionIdKey[] = "sessionId";
39const char kPairingModeKey[] = "mode";
40const char kPairingCodeKey[] = "code";
41
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070042const char kErrorDomain[] = "buffet";
43const char kNotImplemented[] = "notImplemented";
44
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070045} // anonymous namespace
46
Alex Vakulenkob6351532014-08-15 11:49:35 -070047Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
48 : dbus_object_(object_manager.get(),
49 object_manager->GetBus(),
Vitaly Buka91cc7152015-03-20 09:46:57 -070050 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
51}
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070052
Vitaly Buka91cc7152015-03-20 09:46:57 -070053Manager::~Manager() {
54}
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070055
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070056void Manager::Start(const weave::Device::Options& options,
57 AsyncEventSequencer* sequencer) {
Vitaly Buka7042c582015-07-30 17:02:14 -070058 peerd_client_.reset(new PeerdClient{dbus_object_.GetBus()});
59
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070060 device_ = weave::Device::Create();
Vitaly Buka7042c582015-07-30 17:02:14 -070061 device_->Start(options, peerd_client_.get(), &dbus_object_, sequencer);
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070062
Vitaly Bukaa0305d32015-07-27 16:08:51 -070063 command_dispatcher_.reset(new DBusCommandDispacher{
Vitaly Bukae8c3bab2015-07-29 16:39:34 -070064 dbus_object_.GetObjectManager(), device_->GetCommands()});
65
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070066 device_->GetState()->AddOnChangedCallback(
Vitaly Buka2b30e7a2015-05-27 09:27:08 -070067 base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukabf4ba652015-05-14 16:57:23 -070068
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070069 device_->GetConfig()->AddOnChangedCallback(
Vitaly Bukabf4ba652015-05-14 16:57:23 -070070 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukabf4ba652015-05-14 16:57:23 -070071
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070072 device_->GetCloud()->AddOnRegistrationChangedCallback(base::Bind(
Vitaly Bukabf4ba652015-05-14 16:57:23 -070073 &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070074
Vitaly Buka1d63c9c2015-07-17 14:34:35 -070075 if (device_->GetPrivet()) {
76 device_->GetPrivet()->AddOnWifiSetupChangedCallback(base::Bind(
77 &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070078
Vitaly Buka1d63c9c2015-07-17 14:34:35 -070079 device_->GetPrivet()->AddOnPairingChangedCallbacks(
80 base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
81 base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
82 } else {
83 UpdateWiFiBootstrapState(weave::WifiSetupState::kDisabled);
84 }
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070085
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -080086 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070087 dbus_object_.RegisterAsync(
88 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070089}
90
91void Manager::Stop() {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070092 device_.reset();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070093}
94
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070095// TODO(vitalybuka): Remove, it's just duplicate of property.
Alex Vakulenko6c375262015-06-19 11:01:42 -070096void Manager::CheckDeviceRegistered(
97 DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070098 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070099 response->Return(dbus_adaptor_.GetDeviceId());
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700100}
101
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700102// TODO(vitalybuka): Remove or rename to leave for testing.
Alex Vakulenko6c375262015-06-19 11:01:42 -0700103void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700104 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
Alex Vakulenko6c375262015-06-19 11:01:42 -0700105 std::shared_ptr<DBusMethodResponse<std::string>> shared_response =
106 std::move(response);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700107
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700108 device_->GetCloud()->GetDeviceInfo(
Alex Vakulenko6c375262015-06-19 11:01:42 -0700109 base::Bind(&Manager::OnGetDeviceInfoSuccess,
110 weak_ptr_factory_.GetWeakPtr(), shared_response),
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700111 base::Bind(&Manager::OnGetDeviceInfoError, weak_ptr_factory_.GetWeakPtr(),
112 shared_response));
Alex Vakulenko6c375262015-06-19 11:01:42 -0700113}
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700114
Alex Vakulenko6c375262015-06-19 11:01:42 -0700115void Manager::OnGetDeviceInfoSuccess(
116 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
117 const base::DictionaryValue& device_info) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700118 std::string device_info_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700119 base::JSONWriter::WriteWithOptions(
Alex Vakulenko6c375262015-06-19 11:01:42 -0700120 device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700121 response->Return(device_info_str);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700122}
123
Alex Vakulenko6c375262015-06-19 11:01:42 -0700124void Manager::OnGetDeviceInfoError(
125 const std::shared_ptr<DBusMethodResponse<std::string>>& response,
126 const chromeos::Error* error) {
127 response->ReplyWithError(error);
128}
129
130void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response,
Vitaly Bukacbadabe2015-05-14 23:33:32 -0700131 const std::string& ticket_id) {
Anton Muhin9cc03fd2014-10-16 18:59:57 +0400132 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700133
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700134 chromeos::ErrorPtr error;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700135 std::string device_id =
136 device_->GetCloud()->RegisterDevice(ticket_id, &error);
David Zeuthen2101c082015-02-12 15:24:21 -0500137 if (!device_id.empty()) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700138 response->Return(device_id);
David Zeuthen2101c082015-02-12 15:24:21 -0500139 return;
140 }
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700141 CHECK(error);
David Zeuthen2101c082015-02-12 15:24:21 -0500142 response->ReplyWithError(error.get());
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700143}
144
Alex Vakulenko6c375262015-06-19 11:01:42 -0700145void Manager::UpdateState(DBusMethodResponsePtr<> response,
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800146 const chromeos::VariantDictionary& property_set) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700147 chromeos::ErrorPtr error;
Vitaly Bukaa0305d32015-07-27 16:08:51 -0700148 auto properties = DictionaryFromDBusVariantDictionary(property_set, &error);
Vitaly Bukafb2584b2015-07-28 21:39:45 -0700149 if (!properties)
150 response->ReplyWithError(error.get());
151
152 if (!device_->GetState()->SetProperties(*properties, &error))
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700153 response->ReplyWithError(error.get());
154 else
155 response->Return();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700156}
157
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800158bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
Vitaly Bukacd5e5562015-07-28 15:33:55 -0700159 auto json = device_->GetState()->GetStateValuesAsJson();
160 CHECK(json);
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400161 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700162 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800163 return true;
164}
165
Alex Vakulenko6c375262015-06-19 11:01:42 -0700166void Manager::AddCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700167 const std::string& json_command,
168 const std::string& in_user_role) {
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700169 std::string error_message;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700170 std::unique_ptr<base::Value> value(
171 base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC,
172 nullptr, &error_message)
173 .release());
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700174 const base::DictionaryValue* command{nullptr};
175 if (!value || !value->GetAsDictionary(&command)) {
176 return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
177 chromeos::errors::json::kParseError,
178 error_message);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700179 }
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700180
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700181 chromeos::ErrorPtr error;
Vitaly Buka3b086312015-07-09 14:59:23 -0700182 weave::UserRole role;
Vitaly Buka03319c22015-07-17 14:48:30 -0700183 if (!StringToEnum(in_user_role, &role)) {
184 chromeos::Error::AddToPrintf(&error, FROM_HERE, kErrorDomain,
185 "invalid_user_role", "Invalid role: '%s'",
186 in_user_role.c_str());
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700187 return response->ReplyWithError(error.get());
Vitaly Buka03319c22015-07-17 14:48:30 -0700188 }
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700189
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700190 std::string id;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700191 if (!device_->GetCommands()->AddCommand(*command, role, &id, &error))
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700192 return response->ReplyWithError(error.get());
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700193
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700194 response->Return(id);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700195}
196
Alex Vakulenko6c375262015-06-19 11:01:42 -0700197void Manager::GetCommand(DBusMethodResponsePtr<std::string> response,
Vitaly Buka5515de02015-03-24 11:39:40 -0700198 const std::string& id) {
Vitaly Bukaba463732015-07-14 19:19:39 -0700199 const weave::Command* command = device_->GetCommands()->FindCommand(id);
Vitaly Buka5515de02015-03-24 11:39:40 -0700200 if (!command) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700201 response->ReplyWithError(FROM_HERE, kErrorDomain, "unknown_command",
Vitaly Buka5515de02015-03-24 11:39:40 -0700202 "Can't find command with id: " + id);
203 return;
204 }
205 std::string command_str;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700206 base::JSONWriter::WriteWithOptions(
207 *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka5515de02015-03-24 11:39:40 -0700208 response->Return(command_str);
209}
210
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800211std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko35e3bab2014-08-15 11:45:46 -0700212 LOG(INFO) << "Received call to test method: " << message;
213 return message;
Christopher Wiley2ffa0042014-05-05 16:09:16 -0700214}
215
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700216bool Manager::EnableWiFiBootstrapping(
217 chromeos::ErrorPtr* error,
218 const dbus::ObjectPath& in_listener_path,
219 const chromeos::VariantDictionary& in_options) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700220 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700221 "Manual WiFi bootstrapping is not implemented");
222 return false;
223}
224
225bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700226 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700227 "Manual WiFi bootstrapping is not implemented");
228 return false;
229}
230
231bool Manager::EnableGCDBootstrapping(
232 chromeos::ErrorPtr* error,
233 const dbus::ObjectPath& in_listener_path,
234 const chromeos::VariantDictionary& in_options) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700235 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700236 "Manual GCD bootstrapping is not implemented");
237 return false;
238}
239
240bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700241 chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700242 "Manual GCD bootstrapping is not implemented");
243 return false;
244}
245
Vitaly Buka760d6322015-04-17 00:41:31 -0700246bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700247 const std::string& name,
248 const std::string& description,
249 const std::string& location) {
250 return device_->GetCloud()->UpdateDeviceInfo(name, description, location,
251 error);
Christopher Wiley2f772932015-02-15 15:42:04 -0800252}
253
Vitaly Buka7cd01972015-05-14 21:25:45 -0700254bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error,
255 const std::string& client_id,
256 const std::string& client_secret,
257 const std::string& api_key,
258 const std::string& oauth_url,
259 const std::string& service_url) {
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700260 return device_->GetCloud()->UpdateServiceConfig(
261 client_id, client_secret, api_key, oauth_url, service_url, error);
Vitaly Buka7cd01972015-05-14 21:25:45 -0700262}
263
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700264void Manager::OnStateChanged() {
Vitaly Bukacd5e5562015-07-28 15:33:55 -0700265 auto state = device_->GetState()->GetStateValuesAsJson();
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700266 CHECK(state);
267 std::string json;
268 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700269 *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700270 dbus_adaptor_.SetState(json);
271}
272
Vitaly Buka3b086312015-07-09 14:59:23 -0700273void Manager::OnRegistrationChanged(weave::RegistrationStatus status) {
Vitaly Buka03319c22015-07-17 14:48:30 -0700274 dbus_adaptor_.SetStatus(weave::EnumToString(status));
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700275}
276
Vitaly Buka00b83492015-07-20 00:37:48 -0700277void Manager::OnConfigChanged(const weave::Settings& settings) {
278 dbus_adaptor_.SetDeviceId(settings.device_id);
279 dbus_adaptor_.SetOemName(settings.oem_name);
280 dbus_adaptor_.SetModelName(settings.model_name);
281 dbus_adaptor_.SetModelId(settings.model_id);
282 dbus_adaptor_.SetName(settings.name);
283 dbus_adaptor_.SetDescription(settings.description);
284 dbus_adaptor_.SetLocation(settings.location);
285 dbus_adaptor_.SetAnonymousAccessRole(settings.local_anonymous_access_role);
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700286}
287
Vitaly Buka03319c22015-07-17 14:48:30 -0700288void Manager::UpdateWiFiBootstrapState(weave::WifiSetupState state) {
289 dbus_adaptor_.SetWiFiBootstrapState(weave::EnumToString(state));
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700290}
291
292void Manager::OnPairingStart(const std::string& session_id,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700293 weave::PairingType pairing_type,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700294 const std::vector<uint8_t>& code) {
295 // For now, just overwrite the exposed PairInfo with
296 // the most recent pairing attempt.
297 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{
298 {kPairingSessionIdKey, session_id},
Vitaly Buka03319c22015-07-17 14:48:30 -0700299 {kPairingModeKey, weave::EnumToString(pairing_type)},
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700300 {kPairingCodeKey, code},
301 });
302}
303
304void Manager::OnPairingEnd(const std::string& session_id) {
305 auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
306 auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
307 if (it == exposed_pairing_attempt.end()) {
308 return;
309 }
310 std::string exposed_session{it->second.TryGet<std::string>()};
311 if (exposed_session == session_id) {
312 dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{});
313 }
314}
315
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700316} // namespace buffet