Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 7 | #include <map> |
Alex Vakulenko | ec41a0c | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 8 | #include <set> |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
| 12 | #include <base/bind_helpers.h> |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 13 | #include <base/json/json_reader.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 14 | #include <base/json/json_writer.h> |
Alex Vakulenko | f775454 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 15 | #include <base/message_loop/message_loop.h> |
Christopher Wiley | 8994e01 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 16 | #include <base/time/time.h> |
Alex Vakulenko | 24e5f5d | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 17 | #include <chromeos/dbus/async_event_sequencer.h> |
| 18 | #include <chromeos/dbus/exported_object_manager.h> |
| 19 | #include <chromeos/errors/error.h> |
Garret Kelly | 89ba036 | 2015-05-29 15:56:52 -0400 | [diff] [blame] | 20 | #include <chromeos/http/http_transport.h> |
Anton Muhin | 53e882a | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 21 | #include <chromeos/key_value_store.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 22 | #include <dbus/bus.h> |
Christopher Wiley | 54028f9 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 23 | #include <dbus/object_path.h> |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 24 | #include <dbus/values_util.h> |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 25 | |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 26 | #include "weave/enum_to_string.h" |
| 27 | |
Christopher Wiley | 68c07cc | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 28 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | 1aa980e | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 29 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 30 | |
| 31 | namespace buffet { |
| 32 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 33 | namespace { |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 34 | |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 35 | const char kPairingSessionIdKey[] = "sessionId"; |
| 36 | const char kPairingModeKey[] = "mode"; |
| 37 | const char kPairingCodeKey[] = "code"; |
| 38 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 39 | const char kErrorDomain[] = "buffet"; |
| 40 | const char kNotImplemented[] = "notImplemented"; |
| 41 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 42 | } // anonymous namespace |
| 43 | |
Alex Vakulenko | b635153 | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 44 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 45 | : dbus_object_(object_manager.get(), |
| 46 | object_manager->GetBus(), |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 47 | org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) { |
| 48 | } |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 49 | |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 50 | Manager::~Manager() { |
| 51 | } |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 52 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 53 | void Manager::Start(const weave::Device::Options& options, |
| 54 | AsyncEventSequencer* sequencer) { |
| 55 | device_ = weave::Device::Create(); |
| 56 | device_->Start(options, &dbus_object_, sequencer); |
| 57 | |
| 58 | device_->GetCommands()->AddOnCommandDefChanged(base::Bind( |
Vitaly Buka | e43871f | 2015-05-11 15:41:33 -0700 | [diff] [blame] | 59 | &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 60 | |
| 61 | device_->GetState()->AddOnChangedCallback( |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 62 | base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 63 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 64 | device_->GetConfig()->AddOnChangedCallback( |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 65 | base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 66 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 67 | device_->GetCloud()->AddOnRegistrationChangedCallback(base::Bind( |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 68 | &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 69 | |
Vitaly Buka | 1d63c9c | 2015-07-17 14:34:35 -0700 | [diff] [blame] | 70 | if (device_->GetPrivet()) { |
| 71 | device_->GetPrivet()->AddOnWifiSetupChangedCallback(base::Bind( |
| 72 | &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 73 | |
Vitaly Buka | 1d63c9c | 2015-07-17 14:34:35 -0700 | [diff] [blame] | 74 | device_->GetPrivet()->AddOnPairingChangedCallbacks( |
| 75 | base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()), |
| 76 | base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr())); |
| 77 | } else { |
| 78 | UpdateWiFiBootstrapState(weave::WifiSetupState::kDisabled); |
| 79 | } |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 80 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 81 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 82 | dbus_object_.RegisterAsync( |
| 83 | sequencer->GetHandler("Manager.RegisterAsync() failed.", true)); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Manager::Stop() { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 87 | device_.reset(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 90 | // TODO(vitalybuka): Remove, it's just duplicate of property. |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 91 | void Manager::CheckDeviceRegistered( |
| 92 | DBusMethodResponsePtr<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 93 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 94 | response->Return(dbus_adaptor_.GetDeviceId()); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 97 | // TODO(vitalybuka): Remove or rename to leave for testing. |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 98 | void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 99 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 100 | std::shared_ptr<DBusMethodResponse<std::string>> shared_response = |
| 101 | std::move(response); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 102 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 103 | device_->GetCloud()->GetDeviceInfo( |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 104 | base::Bind(&Manager::OnGetDeviceInfoSuccess, |
| 105 | weak_ptr_factory_.GetWeakPtr(), shared_response), |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 106 | base::Bind(&Manager::OnGetDeviceInfoError, weak_ptr_factory_.GetWeakPtr(), |
| 107 | shared_response)); |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 108 | } |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 109 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 110 | void Manager::OnGetDeviceInfoSuccess( |
| 111 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
| 112 | const base::DictionaryValue& device_info) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 113 | std::string device_info_str; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 114 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 115 | device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str); |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 116 | response->Return(device_info_str); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 119 | void Manager::OnGetDeviceInfoError( |
| 120 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
| 121 | const chromeos::Error* error) { |
| 122 | response->ReplyWithError(error); |
| 123 | } |
| 124 | |
| 125 | void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | cbadabe | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 126 | const std::string& ticket_id) { |
Anton Muhin | 9cc03fd | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 127 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 128 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 129 | chromeos::ErrorPtr error; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 130 | std::string device_id = |
| 131 | device_->GetCloud()->RegisterDevice(ticket_id, &error); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 132 | if (!device_id.empty()) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 133 | response->Return(device_id); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 134 | return; |
| 135 | } |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 136 | CHECK(error); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 137 | response->ReplyWithError(error.get()); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 140 | void Manager::UpdateState(DBusMethodResponsePtr<> response, |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 141 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 142 | chromeos::ErrorPtr error; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 143 | if (!device_->GetState()->SetProperties(property_set, &error)) |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 144 | response->ReplyWithError(error.get()); |
| 145 | else |
| 146 | response->Return(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 149 | bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 150 | auto json = device_->GetState()->GetStateValuesAsJson(error); |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 151 | if (!json) |
| 152 | return false; |
Nathan Bullock | f5b91bf | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 153 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 154 | *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state); |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 158 | void Manager::AddCommand(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 159 | const std::string& json_command, |
| 160 | const std::string& in_user_role) { |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 161 | std::string error_message; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 162 | std::unique_ptr<base::Value> value( |
| 163 | base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC, |
| 164 | nullptr, &error_message) |
| 165 | .release()); |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 166 | const base::DictionaryValue* command{nullptr}; |
| 167 | if (!value || !value->GetAsDictionary(&command)) { |
| 168 | return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain, |
| 169 | chromeos::errors::json::kParseError, |
| 170 | error_message); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 171 | } |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 172 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 173 | chromeos::ErrorPtr error; |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 174 | weave::UserRole role; |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 175 | if (!StringToEnum(in_user_role, &role)) { |
| 176 | chromeos::Error::AddToPrintf(&error, FROM_HERE, kErrorDomain, |
| 177 | "invalid_user_role", "Invalid role: '%s'", |
| 178 | in_user_role.c_str()); |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 179 | return response->ReplyWithError(error.get()); |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 180 | } |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 181 | |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 182 | std::string id; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 183 | if (!device_->GetCommands()->AddCommand(*command, role, &id, &error)) |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 184 | return response->ReplyWithError(error.get()); |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 185 | |
Vitaly Buka | 59af7ac | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 186 | response->Return(id); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 189 | void Manager::GetCommand(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 190 | const std::string& id) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 191 | const weave::CommandInstance* command = |
| 192 | device_->GetCommands()->FindCommand(id); |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 193 | if (!command) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 194 | response->ReplyWithError(FROM_HERE, kErrorDomain, "unknown_command", |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 195 | "Can't find command with id: " + id); |
| 196 | return; |
| 197 | } |
| 198 | std::string command_str; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 199 | base::JSONWriter::WriteWithOptions( |
| 200 | *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str); |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 201 | response->Return(command_str); |
| 202 | } |
| 203 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 204 | void Manager::SetCommandVisibility(DBusMethodResponsePtr<> response, |
| 205 | const std::vector<std::string>& in_names, |
| 206 | const std::string& in_visibility) { |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 207 | weave::CommandDefinition::Visibility visibility; |
Alex Vakulenko | acec6aa | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 208 | chromeos::ErrorPtr error; |
| 209 | if (!visibility.FromString(in_visibility, &error)) { |
| 210 | response->ReplyWithError(error.get()); |
| 211 | return; |
| 212 | } |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 213 | if (!device_->GetCommands()->SetCommandVisibility(in_names, visibility, |
| 214 | &error)) { |
Alex Vakulenko | acec6aa | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 215 | response->ReplyWithError(error.get()); |
| 216 | return; |
| 217 | } |
| 218 | response->Return(); |
| 219 | } |
| 220 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 221 | std::string Manager::TestMethod(const std::string& message) { |
Alex Vakulenko | 35e3bab | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 222 | LOG(INFO) << "Received call to test method: " << message; |
| 223 | return message; |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 226 | bool Manager::EnableWiFiBootstrapping( |
| 227 | chromeos::ErrorPtr* error, |
| 228 | const dbus::ObjectPath& in_listener_path, |
| 229 | const chromeos::VariantDictionary& in_options) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 230 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 231 | "Manual WiFi bootstrapping is not implemented"); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 236 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 237 | "Manual WiFi bootstrapping is not implemented"); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | bool Manager::EnableGCDBootstrapping( |
| 242 | chromeos::ErrorPtr* error, |
| 243 | const dbus::ObjectPath& in_listener_path, |
| 244 | const chromeos::VariantDictionary& in_options) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 245 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 246 | "Manual GCD bootstrapping is not implemented"); |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 251 | chromeos::Error::AddTo(error, FROM_HERE, kErrorDomain, kNotImplemented, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 252 | "Manual GCD bootstrapping is not implemented"); |
| 253 | return false; |
| 254 | } |
| 255 | |
Vitaly Buka | 760d632 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 256 | bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 257 | const std::string& name, |
| 258 | const std::string& description, |
| 259 | const std::string& location) { |
| 260 | return device_->GetCloud()->UpdateDeviceInfo(name, description, location, |
| 261 | error); |
Christopher Wiley | 2f77293 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Vitaly Buka | 7cd0197 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 264 | bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error, |
| 265 | const std::string& client_id, |
| 266 | const std::string& client_secret, |
| 267 | const std::string& api_key, |
| 268 | const std::string& oauth_url, |
| 269 | const std::string& service_url) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 270 | return device_->GetCloud()->UpdateServiceConfig( |
| 271 | client_id, client_secret, api_key, oauth_url, service_url, error); |
Vitaly Buka | 7cd0197 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Vitaly Buka | 1fa6901 | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 274 | void Manager::OnCommandDefsChanged() { |
Alex Vakulenko | ec41a0c | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 275 | // Limit only to commands that are visible to the local clients. |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 276 | auto commands = |
| 277 | device_->GetCommands()->GetCommandDictionary().GetCommandsAsJson( |
| 278 | [](const weave::CommandDefinition* def) { |
| 279 | return def->GetVisibility().local; |
| 280 | }, |
| 281 | true, nullptr); |
Vitaly Buka | 1fa6901 | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 282 | CHECK(commands); |
| 283 | std::string json; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 284 | base::JSONWriter::WriteWithOptions( |
| 285 | *commands, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Vitaly Buka | 1fa6901 | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 286 | dbus_adaptor_.SetCommandDefs(json); |
| 287 | } |
| 288 | |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 289 | void Manager::OnStateChanged() { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 290 | auto state = device_->GetState()->GetStateValuesAsJson(nullptr); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 291 | CHECK(state); |
| 292 | std::string json; |
| 293 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 294 | *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 295 | dbus_adaptor_.SetState(json); |
| 296 | } |
| 297 | |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 298 | void Manager::OnRegistrationChanged(weave::RegistrationStatus status) { |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 299 | dbus_adaptor_.SetStatus(weave::EnumToString(status)); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 302 | void Manager::OnConfigChanged(const weave::BuffetConfig& config) { |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 303 | dbus_adaptor_.SetDeviceId(config.device_id()); |
| 304 | dbus_adaptor_.SetOemName(config.oem_name()); |
| 305 | dbus_adaptor_.SetModelName(config.model_name()); |
| 306 | dbus_adaptor_.SetModelId(config.model_id()); |
| 307 | dbus_adaptor_.SetName(config.name()); |
| 308 | dbus_adaptor_.SetDescription(config.description()); |
| 309 | dbus_adaptor_.SetLocation(config.location()); |
| 310 | dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role()); |
| 311 | } |
| 312 | |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 313 | void Manager::UpdateWiFiBootstrapState(weave::WifiSetupState state) { |
| 314 | dbus_adaptor_.SetWiFiBootstrapState(weave::EnumToString(state)); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void Manager::OnPairingStart(const std::string& session_id, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 318 | weave::PairingType pairing_type, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 319 | const std::vector<uint8_t>& code) { |
| 320 | // For now, just overwrite the exposed PairInfo with |
| 321 | // the most recent pairing attempt. |
| 322 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{ |
| 323 | {kPairingSessionIdKey, session_id}, |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame^] | 324 | {kPairingModeKey, weave::EnumToString(pairing_type)}, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 325 | {kPairingCodeKey, code}, |
| 326 | }); |
| 327 | } |
| 328 | |
| 329 | void Manager::OnPairingEnd(const std::string& session_id) { |
| 330 | auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo(); |
| 331 | auto it = exposed_pairing_attempt.find(kPairingSessionIdKey); |
| 332 | if (it == exposed_pairing_attempt.end()) { |
| 333 | return; |
| 334 | } |
| 335 | std::string exposed_session{it->second.TryGet<std::string>()}; |
| 336 | if (exposed_session == session_id) { |
| 337 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{}); |
| 338 | } |
| 339 | } |
| 340 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 341 | } // namespace buffet |