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 | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 13 | #include <base/files/file_enumerator.h> |
| 14 | #include <base/files/file_util.h> |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 15 | #include <base/json/json_reader.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 16 | #include <base/json/json_writer.h> |
Alex Vakulenko | f775454 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 17 | #include <base/message_loop/message_loop.h> |
Christopher Wiley | 8994e01 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 18 | #include <base/time/time.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 19 | #include <brillo/bind_lambda.h> |
| 20 | #include <brillo/dbus/async_event_sequencer.h> |
| 21 | #include <brillo/dbus/exported_object_manager.h> |
| 22 | #include <brillo/errors/error.h> |
| 23 | #include <brillo/http/http_transport.h> |
| 24 | #include <brillo/http/http_utils.h> |
| 25 | #include <brillo/key_value_store.h> |
| 26 | #include <brillo/message_loops/message_loop.h> |
| 27 | #include <brillo/mime_utils.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 28 | #include <dbus/bus.h> |
Christopher Wiley | 54028f9 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 29 | #include <dbus/object_path.h> |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 30 | #include <dbus/values_util.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 31 | #include <weave/enum_to_string.h> |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 32 | |
Robert Ginda | f86123d | 2015-09-11 16:10:43 -0700 | [diff] [blame] | 33 | #include "buffet/bluetooth_client.h" |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 34 | #include "buffet/buffet_config.h" |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 35 | #include "buffet/dbus_command_dispatcher.h" |
| 36 | #include "buffet/dbus_conversion.h" |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 37 | #include "buffet/http_transport_client.h" |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 38 | #include "buffet/mdns_client.h" |
Peter Qiu | 786a906 | 2015-10-02 11:45:01 -0700 | [diff] [blame] | 39 | #include "buffet/shill_client.h" |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 40 | #include "buffet/weave_error_conversion.h" |
Alex Vakulenko | 1642bec | 2015-08-19 09:34:58 -0700 | [diff] [blame] | 41 | #include "buffet/webserv_client.h" |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 42 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 43 | using brillo::dbus_utils::AsyncEventSequencer; |
| 44 | using brillo::dbus_utils::DBusMethodResponse; |
| 45 | using brillo::dbus_utils::ExportedObjectManager; |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 46 | |
| 47 | namespace buffet { |
| 48 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 49 | namespace { |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 50 | |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 51 | const char kPairingSessionIdKey[] = "sessionId"; |
| 52 | const char kPairingModeKey[] = "mode"; |
| 53 | const char kPairingCodeKey[] = "code"; |
| 54 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 55 | const char kErrorDomain[] = "buffet"; |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 56 | const char kFileReadError[] = "file_read_error"; |
| 57 | |
| 58 | bool LoadFile(const base::FilePath& file_path, |
| 59 | std::string* data, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 60 | brillo::ErrorPtr* error) { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 61 | if (!base::ReadFileToString(file_path, data)) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 62 | brillo::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 63 | brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError, |
| 64 | "Failed to read file '%s'", |
| 65 | file_path.value().c_str()); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | void LoadCommandDefinitions(const BuffetConfig::Options& options, |
| 72 | weave::Device* device) { |
| 73 | auto load_packages = [device](const base::FilePath& root, |
| 74 | const base::FilePath::StringType& pattern) { |
| 75 | base::FilePath dir{root.Append("commands")}; |
| 76 | LOG(INFO) << "Looking for command schemas in " << dir.value(); |
| 77 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 78 | pattern); |
| 79 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 80 | path = enumerator.Next()) { |
| 81 | LOG(INFO) << "Loading command schema from " << path.value(); |
| 82 | std::string json; |
| 83 | CHECK(LoadFile(path, &json, nullptr)); |
| 84 | device->AddCommandDefinitionsFromJson(json); |
| 85 | } |
| 86 | }; |
| 87 | load_packages(options.definitions, FILE_PATH_LITERAL("*.json")); |
| 88 | load_packages(options.test_definitions, FILE_PATH_LITERAL("*test.json")); |
| 89 | } |
| 90 | |
| 91 | void LoadStateDefinitions(const BuffetConfig::Options& options, |
| 92 | weave::Device* device) { |
| 93 | // Load component-specific device state definitions. |
| 94 | base::FilePath dir{options.definitions.Append("states")}; |
| 95 | LOG(INFO) << "Looking for state definitions in " << dir.value(); |
| 96 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 97 | FILE_PATH_LITERAL("*.schema.json")); |
| 98 | std::vector<std::string> result; |
| 99 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 100 | path = enumerator.Next()) { |
| 101 | LOG(INFO) << "Loading state definition from " << path.value(); |
| 102 | std::string json; |
| 103 | CHECK(LoadFile(path, &json, nullptr)); |
| 104 | device->AddStateDefinitionsFromJson(json); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void LoadStateDefaults(const BuffetConfig::Options& options, |
| 109 | weave::Device* device) { |
| 110 | // Load component-specific device state defaults. |
| 111 | base::FilePath dir{options.definitions.Append("states")}; |
| 112 | LOG(INFO) << "Looking for state defaults in " << dir.value(); |
| 113 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 114 | FILE_PATH_LITERAL("*.defaults.json")); |
| 115 | std::vector<std::string> result; |
| 116 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 117 | path = enumerator.Next()) { |
| 118 | LOG(INFO) << "Loading state defaults from " << path.value(); |
| 119 | std::string json; |
| 120 | CHECK(LoadFile(path, &json, nullptr)); |
| 121 | CHECK(device->SetStatePropertiesFromJson(json, nullptr)); |
| 122 | } |
| 123 | } |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 124 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 125 | void RegisterDeviceSuccess( |
| 126 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
| 127 | weave::Device* device) { |
| 128 | LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id; |
| 129 | response->Return(device->GetSettings().cloud_id); |
| 130 | } |
| 131 | |
| 132 | void RegisterDeviceError( |
| 133 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 134 | weave::ErrorPtr weave_error) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 135 | brillo::ErrorPtr error; |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 136 | ConvertError(*weave_error, &error); |
| 137 | response->ReplyWithError(error.get()); |
| 138 | } |
| 139 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 140 | } // anonymous namespace |
| 141 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 142 | class Manager::TaskRunner : public weave::provider::TaskRunner { |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 143 | public: |
| 144 | void PostDelayedTask(const tracked_objects::Location& from_here, |
| 145 | const base::Closure& task, |
| 146 | base::TimeDelta delay) override { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 147 | brillo::MessageLoop::current()->PostDelayedTask(from_here, task, delay); |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 148 | } |
| 149 | }; |
| 150 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 151 | Manager::Manager(const Options& options, |
| 152 | const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 153 | : options_{options}, |
| 154 | dbus_object_(object_manager.get(), |
Alex Vakulenko | b635153 | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 155 | object_manager->GetBus(), |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 156 | com::android::Weave::ManagerAdaptor::GetObjectPath()) {} |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 157 | |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 158 | Manager::~Manager() { |
| 159 | } |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 160 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 161 | void Manager::Start(AsyncEventSequencer* sequencer) { |
| 162 | RestartWeave(sequencer); |
| 163 | |
| 164 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
| 165 | dbus_object_.RegisterAsync( |
| 166 | sequencer->GetHandler("Manager.RegisterAsync() failed.", true)); |
| 167 | } |
| 168 | |
| 169 | void Manager::RestartWeave(AsyncEventSequencer* sequencer) { |
| 170 | Stop(); |
| 171 | |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 172 | task_runner_.reset(new TaskRunner{}); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 173 | config_.reset(new BuffetConfig{options_.config_options}); |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 174 | http_client_.reset(new HttpTransportClient); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 175 | shill_client_.reset(new ShillClient{dbus_object_.GetBus(), |
| 176 | options_.device_whitelist, |
| 177 | !options_.xmpp_enabled}); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 178 | weave::provider::HttpServer* http_server{nullptr}; |
Christopher Wiley | e925bb3 | 2015-08-03 20:09:18 -0700 | [diff] [blame] | 179 | #ifdef BUFFET_USE_WIFI_BOOTSTRAPPING |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 180 | if (!options_.disable_privet) { |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 181 | mdns_client_ = MdnsClient::CreateInstance(dbus_object_.GetBus()); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 182 | web_serv_client_.reset(new WebServClient{ |
| 183 | dbus_object_.GetBus(), sequencer, |
| 184 | base::Bind(&Manager::CreateDevice, weak_ptr_factory_.GetWeakPtr())}); |
Robert Ginda | f86123d | 2015-09-11 16:10:43 -0700 | [diff] [blame] | 185 | bluetooth_client_ = BluetoothClient::CreateInstance(); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 186 | http_server = web_serv_client_.get(); |
| 187 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 188 | if (options_.enable_ping) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 189 | auto ping_handler = base::Bind( |
| 190 | [](std::unique_ptr<weave::provider::HttpServer::Request> request) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 191 | request->SendReply(brillo::http::status_code::Ok, "Hello, world!", |
| 192 | brillo::mime::text::kPlain); |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 193 | }); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 194 | http_server->AddHttpRequestHandler("/privet/ping", ping_handler); |
| 195 | http_server->AddHttpsRequestHandler("/privet/ping", ping_handler); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 196 | } |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 197 | } |
Christopher Wiley | e925bb3 | 2015-08-03 20:09:18 -0700 | [diff] [blame] | 198 | #endif // BUFFET_USE_WIFI_BOOTSTRAPPING |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 199 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 200 | if (!http_server) |
| 201 | CreateDevice(); |
| 202 | } |
| 203 | |
| 204 | void Manager::CreateDevice() { |
| 205 | if (device_) |
| 206 | return; |
| 207 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 208 | device_ = weave::Device::Create(config_.get(), task_runner_.get(), |
| 209 | http_client_.get(), shill_client_.get(), |
| 210 | mdns_client_.get(), web_serv_client_.get(), |
| 211 | shill_client_.get(), bluetooth_client_.get()); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 212 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 213 | LoadCommandDefinitions(options_.config_options, device_.get()); |
| 214 | LoadStateDefinitions(options_.config_options, device_.get()); |
| 215 | LoadStateDefaults(options_.config_options, device_.get()); |
| 216 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 217 | device_->AddSettingsChangedCallback( |
| 218 | base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 219 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 220 | command_dispatcher_.reset( |
| 221 | new DBusCommandDispacher{dbus_object_.GetObjectManager(), device_.get()}); |
Vitaly Buka | e8c3bab | 2015-07-29 16:39:34 -0700 | [diff] [blame] | 222 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 223 | device_->AddStateChangedCallback( |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 224 | base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 225 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 226 | device_->AddGcdStateChangedCallback( |
| 227 | base::Bind(&Manager::OnGcdStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 228 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 229 | device_->AddPairingChangedCallbacks( |
| 230 | base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()), |
| 231 | base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void Manager::Stop() { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 235 | command_dispatcher_.reset(); |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 236 | device_.reset(); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 237 | #ifdef BUFFET_USE_WIFI_BOOTSTRAPPING |
| 238 | web_serv_client_.reset(); |
| 239 | mdns_client_.reset(); |
| 240 | #endif // BUFFET_USE_WIFI_BOOTSTRAPPING |
| 241 | shill_client_.reset(); |
| 242 | http_client_.reset(); |
| 243 | config_.reset(); |
| 244 | task_runner_.reset(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 247 | void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | cbadabe | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 248 | const std::string& ticket_id) { |
Anton Muhin | 9cc03fd | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 249 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 250 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 251 | std::shared_ptr<DBusMethodResponse<std::string>> shared_response = |
| 252 | std::move(response); |
| 253 | |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 254 | device_->Register(ticket_id, base::Bind(&RegisterDeviceSuccess, |
| 255 | shared_response, device_.get()), |
| 256 | base::Bind(&RegisterDeviceError, shared_response)); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 259 | void Manager::UpdateState(DBusMethodResponsePtr<> response, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 260 | const brillo::VariantDictionary& property_set) { |
| 261 | brillo::ErrorPtr chromeos_error; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 262 | auto properties = |
| 263 | DictionaryFromDBusVariantDictionary(property_set, &chromeos_error); |
Vitaly Buka | fb2584b | 2015-07-28 21:39:45 -0700 | [diff] [blame] | 264 | if (!properties) |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 265 | return response->ReplyWithError(chromeos_error.get()); |
Vitaly Buka | fb2584b | 2015-07-28 21:39:45 -0700 | [diff] [blame] | 266 | |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 267 | weave::ErrorPtr error; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 268 | if (!device_->SetStateProperties(*properties, &error)) { |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 269 | ConvertError(*error, &chromeos_error); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 270 | return response->ReplyWithError(chromeos_error.get()); |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 271 | } |
| 272 | response->Return(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 275 | bool Manager::GetState(brillo::ErrorPtr* error, std::string* state) { |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 276 | auto json = device_->GetState(); |
Vitaly Buka | cd5e556 | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 277 | CHECK(json); |
Nathan Bullock | f5b91bf | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 278 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 279 | *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state); |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 283 | void Manager::AddCommand(DBusMethodResponsePtr<std::string> response, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 284 | const std::string& json_command) { |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 285 | std::string error_message; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 286 | std::unique_ptr<base::Value> value( |
| 287 | base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC, |
| 288 | nullptr, &error_message) |
| 289 | .release()); |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 290 | const base::DictionaryValue* command{nullptr}; |
| 291 | if (!value || !value->GetAsDictionary(&command)) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 292 | return response->ReplyWithError(FROM_HERE, brillo::errors::json::kDomain, |
| 293 | brillo::errors::json::kParseError, |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 294 | error_message); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 295 | } |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 296 | |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 297 | std::string id; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 298 | weave::ErrorPtr error; |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 299 | if (!device_->AddCommand(*command, &id, &error)) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 300 | brillo::ErrorPtr chromeos_error; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 301 | ConvertError(*error, &chromeos_error); |
| 302 | return response->ReplyWithError(chromeos_error.get()); |
| 303 | } |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 304 | |
Vitaly Buka | 59af7ac | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 305 | response->Return(id); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 308 | std::string Manager::TestMethod(const std::string& message) { |
Alex Vakulenko | 35e3bab | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 309 | LOG(INFO) << "Received call to test method: " << message; |
| 310 | return message; |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 313 | void Manager::OnStateChanged() { |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 314 | auto state = device_->GetState(); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 315 | CHECK(state); |
| 316 | std::string json; |
| 317 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 318 | *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 319 | dbus_adaptor_.SetState(json); |
| 320 | } |
| 321 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 322 | void Manager::OnGcdStateChanged(weave::GcdState state) { |
| 323 | dbus_adaptor_.SetStatus(weave::EnumToString(state)); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Vitaly Buka | 00b8349 | 2015-07-20 00:37:48 -0700 | [diff] [blame] | 326 | void Manager::OnConfigChanged(const weave::Settings& settings) { |
Alex Vakulenko | e4ef261 | 2015-09-29 09:53:39 -0700 | [diff] [blame] | 327 | dbus_adaptor_.SetDeviceId(settings.cloud_id); |
Vitaly Buka | 00b8349 | 2015-07-20 00:37:48 -0700 | [diff] [blame] | 328 | dbus_adaptor_.SetOemName(settings.oem_name); |
| 329 | dbus_adaptor_.SetModelName(settings.model_name); |
| 330 | dbus_adaptor_.SetModelId(settings.model_id); |
| 331 | dbus_adaptor_.SetName(settings.name); |
| 332 | dbus_adaptor_.SetDescription(settings.description); |
| 333 | dbus_adaptor_.SetLocation(settings.location); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void Manager::OnPairingStart(const std::string& session_id, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 337 | weave::PairingType pairing_type, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 338 | const std::vector<uint8_t>& code) { |
| 339 | // For now, just overwrite the exposed PairInfo with |
| 340 | // the most recent pairing attempt. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 341 | dbus_adaptor_.SetPairingInfo(brillo::VariantDictionary{ |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 342 | {kPairingSessionIdKey, session_id}, |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 343 | {kPairingModeKey, weave::EnumToString(pairing_type)}, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 344 | {kPairingCodeKey, code}, |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | void Manager::OnPairingEnd(const std::string& session_id) { |
| 349 | auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo(); |
| 350 | auto it = exposed_pairing_attempt.find(kPairingSessionIdKey); |
| 351 | if (it == exposed_pairing_attempt.end()) { |
| 352 | return; |
| 353 | } |
| 354 | std::string exposed_session{it->second.TryGet<std::string>()}; |
| 355 | if (exposed_session == session_id) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 356 | dbus_adaptor_.SetPairingInfo(brillo::VariantDictionary{}); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 360 | } // namespace buffet |