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> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 25 | #include <weave/enum_to_string.h> |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 26 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 27 | #include "buffet/dbus_command_dispatcher.h" |
| 28 | #include "buffet/dbus_conversion.h" |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 29 | #include "buffet/http_transport_client.h" |
Alex Vakulenko | 15aea55 | 2015-08-17 09:34:08 -0700 | [diff] [blame^] | 30 | #include "buffet/mdns_client.h" |
| 31 | #include "buffet/network_client.h" |
Alex Vakulenko | d78756d | 2015-08-11 12:32:02 -0700 | [diff] [blame] | 32 | //#include "buffet/webserv_client.h" |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 33 | |
Christopher Wiley | 68c07cc | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 34 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | 1aa980e | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 35 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 36 | |
| 37 | namespace buffet { |
| 38 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 39 | namespace { |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 40 | |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 41 | const char kPairingSessionIdKey[] = "sessionId"; |
| 42 | const char kPairingModeKey[] = "mode"; |
| 43 | const char kPairingCodeKey[] = "code"; |
| 44 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 45 | const char kErrorDomain[] = "buffet"; |
| 46 | const char kNotImplemented[] = "notImplemented"; |
| 47 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 48 | } // anonymous namespace |
| 49 | |
Alex Vakulenko | b635153 | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 50 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 51 | : dbus_object_(object_manager.get(), |
| 52 | object_manager->GetBus(), |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 53 | org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) { |
| 54 | } |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 55 | |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 56 | Manager::~Manager() { |
| 57 | } |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 58 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 59 | void Manager::Start(const weave::Device::Options& options, |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 60 | const std::set<std::string>& device_whitelist, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 61 | AsyncEventSequencer* sequencer) { |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 62 | http_client_.reset(new HttpTransportClient); |
Alex Vakulenko | 15aea55 | 2015-08-17 09:34:08 -0700 | [diff] [blame^] | 63 | network_client_.reset(new NetworkClient/*{device_whitelist}*/); |
| 64 | if (!options.disable_privet) { |
| 65 | mdns_client_.reset(new MdnsClient); |
Alex Vakulenko | d78756d | 2015-08-11 12:32:02 -0700 | [diff] [blame] | 66 | // web_serv_client_.reset(new WebServClient{dbus_object_.GetBus(), sequencer}); |
Alex Vakulenko | 15aea55 | 2015-08-17 09:34:08 -0700 | [diff] [blame^] | 67 | } |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 68 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 69 | device_ = weave::Device::Create(); |
Alex Vakulenko | 15aea55 | 2015-08-17 09:34:08 -0700 | [diff] [blame^] | 70 | device_->Start(options, http_client_.get(), network_client_.get(), |
| 71 | nullptr /*mdns_client_.get()*/, nullptr /*web_serv_client_.get()*/); |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 72 | |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 73 | command_dispatcher_.reset(new DBusCommandDispacher{ |
Vitaly Buka | e8c3bab | 2015-07-29 16:39:34 -0700 | [diff] [blame] | 74 | dbus_object_.GetObjectManager(), device_->GetCommands()}); |
| 75 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 76 | device_->GetState()->AddOnChangedCallback( |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 77 | base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 78 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 79 | device_->GetConfig()->AddOnChangedCallback( |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 80 | base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 81 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 82 | device_->GetCloud()->AddOnRegistrationChangedCallback(base::Bind( |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 83 | &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 84 | |
Vitaly Buka | 1d63c9c | 2015-07-17 14:34:35 -0700 | [diff] [blame] | 85 | if (device_->GetPrivet()) { |
| 86 | device_->GetPrivet()->AddOnWifiSetupChangedCallback(base::Bind( |
| 87 | &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 88 | |
Vitaly Buka | 1d63c9c | 2015-07-17 14:34:35 -0700 | [diff] [blame] | 89 | device_->GetPrivet()->AddOnPairingChangedCallbacks( |
| 90 | base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()), |
| 91 | base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr())); |
| 92 | } else { |
| 93 | UpdateWiFiBootstrapState(weave::WifiSetupState::kDisabled); |
| 94 | } |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 95 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 96 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 97 | dbus_object_.RegisterAsync( |
| 98 | sequencer->GetHandler("Manager.RegisterAsync() failed.", true)); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void Manager::Stop() { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 102 | device_.reset(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 105 | // TODO(vitalybuka): Remove, it's just duplicate of property. |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 106 | void Manager::CheckDeviceRegistered( |
| 107 | DBusMethodResponsePtr<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 108 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 109 | response->Return(dbus_adaptor_.GetDeviceId()); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 112 | // TODO(vitalybuka): Remove or rename to leave for testing. |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 113 | void Manager::GetDeviceInfo(DBusMethodResponsePtr<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 114 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 115 | std::shared_ptr<DBusMethodResponse<std::string>> shared_response = |
| 116 | std::move(response); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 117 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 118 | device_->GetCloud()->GetDeviceInfo( |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 119 | base::Bind(&Manager::OnGetDeviceInfoSuccess, |
| 120 | weak_ptr_factory_.GetWeakPtr(), shared_response), |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 121 | base::Bind(&Manager::OnGetDeviceInfoError, weak_ptr_factory_.GetWeakPtr(), |
| 122 | shared_response)); |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 123 | } |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 124 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 125 | void Manager::OnGetDeviceInfoSuccess( |
| 126 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
| 127 | const base::DictionaryValue& device_info) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 128 | std::string device_info_str; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 129 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 130 | device_info, base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str); |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 131 | response->Return(device_info_str); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 134 | void Manager::OnGetDeviceInfoError( |
| 135 | const std::shared_ptr<DBusMethodResponse<std::string>>& response, |
| 136 | const chromeos::Error* error) { |
| 137 | response->ReplyWithError(error); |
| 138 | } |
| 139 | |
| 140 | void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | cbadabe | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 141 | const std::string& ticket_id) { |
Anton Muhin | 9cc03fd | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 142 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 143 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 144 | chromeos::ErrorPtr error; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 145 | std::string device_id = |
| 146 | device_->GetCloud()->RegisterDevice(ticket_id, &error); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 147 | if (!device_id.empty()) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 148 | response->Return(device_id); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 149 | return; |
| 150 | } |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 151 | CHECK(error); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 152 | response->ReplyWithError(error.get()); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 155 | void Manager::UpdateState(DBusMethodResponsePtr<> response, |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 156 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 157 | chromeos::ErrorPtr error; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 158 | auto properties = DictionaryFromDBusVariantDictionary(property_set, &error); |
Vitaly Buka | fb2584b | 2015-07-28 21:39:45 -0700 | [diff] [blame] | 159 | if (!properties) |
| 160 | response->ReplyWithError(error.get()); |
| 161 | |
| 162 | if (!device_->GetState()->SetProperties(*properties, &error)) |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 163 | response->ReplyWithError(error.get()); |
| 164 | else |
| 165 | response->Return(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 168 | bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) { |
Vitaly Buka | cd5e556 | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 169 | auto json = device_->GetState()->GetStateValuesAsJson(); |
| 170 | CHECK(json); |
Nathan Bullock | f5b91bf | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 171 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 172 | *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state); |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 176 | void Manager::AddCommand(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 177 | const std::string& json_command, |
| 178 | const std::string& in_user_role) { |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 179 | std::string error_message; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 180 | std::unique_ptr<base::Value> value( |
| 181 | base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC, |
| 182 | nullptr, &error_message) |
| 183 | .release()); |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 184 | const base::DictionaryValue* command{nullptr}; |
| 185 | if (!value || !value->GetAsDictionary(&command)) { |
| 186 | return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain, |
| 187 | chromeos::errors::json::kParseError, |
| 188 | error_message); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 189 | } |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 190 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 191 | chromeos::ErrorPtr error; |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 192 | weave::UserRole role; |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 193 | if (!StringToEnum(in_user_role, &role)) { |
| 194 | chromeos::Error::AddToPrintf(&error, FROM_HERE, kErrorDomain, |
| 195 | "invalid_user_role", "Invalid role: '%s'", |
| 196 | in_user_role.c_str()); |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 197 | return response->ReplyWithError(error.get()); |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 198 | } |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 199 | |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 200 | std::string id; |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 201 | if (!device_->GetCommands()->AddCommand(*command, role, &id, &error)) |
Vitaly Buka | a4e8d7f | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 202 | return response->ReplyWithError(error.get()); |
Vitaly Buka | c03ec2b | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 203 | |
Vitaly Buka | 59af7ac | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 204 | response->Return(id); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Alex Vakulenko | 6c37526 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 207 | void Manager::GetCommand(DBusMethodResponsePtr<std::string> response, |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 208 | const std::string& id) { |
Vitaly Buka | ba46373 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 209 | const weave::Command* command = device_->GetCommands()->FindCommand(id); |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 210 | if (!command) { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 211 | response->ReplyWithError(FROM_HERE, kErrorDomain, "unknown_command", |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 212 | "Can't find command with id: " + id); |
| 213 | return; |
| 214 | } |
| 215 | std::string command_str; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 216 | base::JSONWriter::WriteWithOptions( |
| 217 | *command->ToJson(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str); |
Vitaly Buka | 5515de0 | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 218 | response->Return(command_str); |
| 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 | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 274 | void Manager::OnStateChanged() { |
Vitaly Buka | cd5e556 | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 275 | auto state = device_->GetState()->GetStateValuesAsJson(); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 276 | CHECK(state); |
| 277 | std::string json; |
| 278 | base::JSONWriter::WriteWithOptions( |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 279 | *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 280 | dbus_adaptor_.SetState(json); |
| 281 | } |
| 282 | |
Vitaly Buka | 3b08631 | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 283 | void Manager::OnRegistrationChanged(weave::RegistrationStatus status) { |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 284 | dbus_adaptor_.SetStatus(weave::EnumToString(status)); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Vitaly Buka | 00b8349 | 2015-07-20 00:37:48 -0700 | [diff] [blame] | 287 | void Manager::OnConfigChanged(const weave::Settings& settings) { |
| 288 | dbus_adaptor_.SetDeviceId(settings.device_id); |
| 289 | dbus_adaptor_.SetOemName(settings.oem_name); |
| 290 | dbus_adaptor_.SetModelName(settings.model_name); |
| 291 | dbus_adaptor_.SetModelId(settings.model_id); |
| 292 | dbus_adaptor_.SetName(settings.name); |
| 293 | dbus_adaptor_.SetDescription(settings.description); |
| 294 | dbus_adaptor_.SetLocation(settings.location); |
| 295 | dbus_adaptor_.SetAnonymousAccessRole(settings.local_anonymous_access_role); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 298 | void Manager::UpdateWiFiBootstrapState(weave::WifiSetupState state) { |
| 299 | dbus_adaptor_.SetWiFiBootstrapState(weave::EnumToString(state)); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void Manager::OnPairingStart(const std::string& session_id, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 303 | weave::PairingType pairing_type, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 304 | const std::vector<uint8_t>& code) { |
| 305 | // For now, just overwrite the exposed PairInfo with |
| 306 | // the most recent pairing attempt. |
| 307 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{ |
| 308 | {kPairingSessionIdKey, session_id}, |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 309 | {kPairingModeKey, weave::EnumToString(pairing_type)}, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 310 | {kPairingCodeKey, code}, |
| 311 | }); |
| 312 | } |
| 313 | |
| 314 | void Manager::OnPairingEnd(const std::string& session_id) { |
| 315 | auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo(); |
| 316 | auto it = exposed_pairing_attempt.find(kPairingSessionIdKey); |
| 317 | if (it == exposed_pairing_attempt.end()) { |
| 318 | return; |
| 319 | } |
| 320 | std::string exposed_session{it->second.TryGet<std::string>()}; |
| 321 | if (exposed_session == session_id) { |
| 322 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{}); |
| 323 | } |
| 324 | } |
| 325 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 326 | } // namespace buffet |