blob: 3d5e450895bc190a3e73b63ef54b5f49eb6f6cdf [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070014
15#include "buffet/manager.h"
16
Alex Vakulenko7c3226e2014-05-07 17:35:24 -070017#include <map>
Alex Vakulenkoec41a0c2015-04-17 15:35:34 -070018#include <set>
Alex Vakulenko7c3226e2014-05-07 17:35:24 -070019#include <string>
20
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070021#include <base/bind.h>
22#include <base/bind_helpers.h>
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070023#include <base/files/file_enumerator.h>
24#include <base/files/file_util.h>
Alex Vakulenko4f7778e2014-09-11 16:57:24 -070025#include <base/json/json_reader.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070026#include <base/json/json_writer.h>
Alex Vakulenkof7754542015-06-26 12:59:50 -070027#include <base/message_loop/message_loop.h>
Christopher Wiley8994e012015-02-06 17:51:43 -080028#include <base/time/time.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070029#include <brillo/bind_lambda.h>
30#include <brillo/dbus/async_event_sequencer.h>
31#include <brillo/dbus/exported_object_manager.h>
32#include <brillo/errors/error.h>
33#include <brillo/http/http_transport.h>
34#include <brillo/http/http_utils.h>
35#include <brillo/key_value_store.h>
36#include <brillo/message_loops/message_loop.h>
37#include <brillo/mime_utils.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070038#include <dbus/bus.h>
Christopher Wiley54028f92014-04-01 17:33:29 -070039#include <dbus/object_path.h>
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070040#include <dbus/values_util.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -070041#include <weave/enum_to_string.h>
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070042
Robert Gindaf86123d2015-09-11 16:10:43 -070043#include "buffet/bluetooth_client.h"
Vitaly Buka1175a9b2015-08-15 10:42:17 -070044#include "buffet/buffet_config.h"
Vitaly Bukaa0305d32015-07-27 16:08:51 -070045#include "buffet/dbus_command_dispatcher.h"
46#include "buffet/dbus_conversion.h"
Vitaly Buka00882b72015-08-06 00:32:11 -070047#include "buffet/http_transport_client.h"
Alex Vakulenkof0f55342015-08-18 15:51:40 -070048#include "buffet/mdns_client.h"
Peter Qiu786a9062015-10-02 11:45:01 -070049#include "buffet/shill_client.h"
Vitaly Buka4f771532015-08-14 14:58:39 -070050#include "buffet/weave_error_conversion.h"
Alex Vakulenko1642bec2015-08-19 09:34:58 -070051#include "buffet/webserv_client.h"
Vitaly Buka03319c22015-07-17 14:48:30 -070052
Alex Vakulenko41705852015-10-13 10:12:06 -070053using brillo::dbus_utils::AsyncEventSequencer;
54using brillo::dbus_utils::DBusMethodResponse;
55using brillo::dbus_utils::ExportedObjectManager;
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070056
57namespace buffet {
58
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070059namespace {
Vitaly Buka7b3ba792015-06-09 17:01:54 -070060
Vitaly Buka7b3ba792015-06-09 17:01:54 -070061const char kPairingSessionIdKey[] = "sessionId";
62const char kPairingModeKey[] = "mode";
63const char kPairingCodeKey[] = "code";
64
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070065const char kErrorDomain[] = "buffet";
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070066const char kFileReadError[] = "file_read_error";
67
68bool LoadFile(const base::FilePath& file_path,
69 std::string* data,
Alex Vakulenko41705852015-10-13 10:12:06 -070070 brillo::ErrorPtr* error) {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070071 if (!base::ReadFileToString(file_path, data)) {
Alex Vakulenko41705852015-10-13 10:12:06 -070072 brillo::errors::system::AddSystemError(error, FROM_HERE, errno);
73 brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError,
74 "Failed to read file '%s'",
75 file_path.value().c_str());
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070076 return false;
77 }
78 return true;
79}
80
81void LoadCommandDefinitions(const BuffetConfig::Options& options,
82 weave::Device* device) {
83 auto load_packages = [device](const base::FilePath& root,
84 const base::FilePath::StringType& pattern) {
85 base::FilePath dir{root.Append("commands")};
86 LOG(INFO) << "Looking for command schemas in " << dir.value();
87 base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES,
88 pattern);
89 for (base::FilePath path = enumerator.Next(); !path.empty();
90 path = enumerator.Next()) {
91 LOG(INFO) << "Loading command schema from " << path.value();
92 std::string json;
93 CHECK(LoadFile(path, &json, nullptr));
94 device->AddCommandDefinitionsFromJson(json);
95 }
96 };
97 load_packages(options.definitions, FILE_PATH_LITERAL("*.json"));
98 load_packages(options.test_definitions, FILE_PATH_LITERAL("*test.json"));
99}
100
101void LoadStateDefinitions(const BuffetConfig::Options& options,
102 weave::Device* device) {
103 // Load component-specific device state definitions.
104 base::FilePath dir{options.definitions.Append("states")};
105 LOG(INFO) << "Looking for state definitions in " << dir.value();
106 base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES,
107 FILE_PATH_LITERAL("*.schema.json"));
108 std::vector<std::string> result;
109 for (base::FilePath path = enumerator.Next(); !path.empty();
110 path = enumerator.Next()) {
111 LOG(INFO) << "Loading state definition from " << path.value();
112 std::string json;
113 CHECK(LoadFile(path, &json, nullptr));
114 device->AddStateDefinitionsFromJson(json);
115 }
116}
117
118void LoadStateDefaults(const BuffetConfig::Options& options,
119 weave::Device* device) {
120 // Load component-specific device state defaults.
121 base::FilePath dir{options.definitions.Append("states")};
122 LOG(INFO) << "Looking for state defaults in " << dir.value();
123 base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES,
124 FILE_PATH_LITERAL("*.defaults.json"));
125 std::vector<std::string> result;
126 for (base::FilePath path = enumerator.Next(); !path.empty();
127 path = enumerator.Next()) {
128 LOG(INFO) << "Loading state defaults from " << path.value();
129 std::string json;
130 CHECK(LoadFile(path, &json, nullptr));
131 CHECK(device->SetStatePropertiesFromJson(json, nullptr));
132 }
133}
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700134
Alex Vakulenkoecf961a2014-10-28 13:50:16 -0700135} // anonymous namespace
136
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700137class Manager::TaskRunner : public weave::provider::TaskRunner {
Vitaly Bukae74c8722015-08-13 00:33:00 -0700138 public:
139 void PostDelayedTask(const tracked_objects::Location& from_here,
140 const base::Closure& task,
141 base::TimeDelta delay) override {
Alex Vakulenko41705852015-10-13 10:12:06 -0700142 brillo::MessageLoop::current()->PostDelayedTask(from_here, task, delay);
Vitaly Bukae74c8722015-08-13 00:33:00 -0700143 }
144};
145
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700146Manager::Manager(const Options& options,
147 const base::WeakPtr<ExportedObjectManager>& object_manager)
148 : options_{options},
149 dbus_object_(object_manager.get(),
Alex Vakulenkob6351532014-08-15 11:49:35 -0700150 object_manager->GetBus(),
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700151 com::android::Weave::ManagerAdaptor::GetObjectPath()) {}
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700152
Vitaly Buka91cc7152015-03-20 09:46:57 -0700153Manager::~Manager() {
154}
Alex Vakulenkoecf961a2014-10-28 13:50:16 -0700155
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700156void Manager::Start(AsyncEventSequencer* sequencer) {
157 RestartWeave(sequencer);
158
159 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Alex Vakulenkoc662a242015-10-15 13:04:06 -0700160 dbus_registration_handler_ =
161 sequencer->GetHandler("Manager.RegisterAsync() failed.", true);
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700162}
163
164void Manager::RestartWeave(AsyncEventSequencer* sequencer) {
165 Stop();
166
Vitaly Bukae74c8722015-08-13 00:33:00 -0700167 task_runner_.reset(new TaskRunner{});
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700168 config_.reset(new BuffetConfig{options_.config_options});
Vitaly Buka00882b72015-08-06 00:32:11 -0700169 http_client_.reset(new HttpTransportClient);
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700170 shill_client_.reset(new ShillClient{dbus_object_.GetBus(),
171 options_.device_whitelist,
172 !options_.xmpp_enabled});
Alex Vakulenko0022b752015-10-02 11:09:59 -0700173 weave::provider::HttpServer* http_server{nullptr};
Christopher Wileye925bb32015-08-03 20:09:18 -0700174#ifdef BUFFET_USE_WIFI_BOOTSTRAPPING
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700175 if (!options_.disable_privet) {
Robert Gindacf92c662015-08-20 09:30:11 -0700176 mdns_client_ = MdnsClient::CreateInstance(dbus_object_.GetBus());
Alex Vakulenkobe39e932015-10-09 08:10:36 -0700177 web_serv_client_.reset(new WebServClient{
178 dbus_object_.GetBus(), sequencer,
179 base::Bind(&Manager::CreateDevice, weak_ptr_factory_.GetWeakPtr())});
Robert Gindaf86123d2015-09-11 16:10:43 -0700180 bluetooth_client_ = BluetoothClient::CreateInstance();
Alex Vakulenko0022b752015-10-02 11:09:59 -0700181 http_server = web_serv_client_.get();
182
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700183 if (options_.enable_ping) {
Alex Vakulenkobe39e932015-10-09 08:10:36 -0700184 auto ping_handler = base::Bind(
185 [](std::unique_ptr<weave::provider::HttpServer::Request> request) {
Alex Vakulenko41705852015-10-13 10:12:06 -0700186 request->SendReply(brillo::http::status_code::Ok, "Hello, world!",
187 brillo::mime::text::kPlain);
Alex Vakulenko297114c2015-10-12 13:45:43 -0700188 });
Alex Vakulenkobe39e932015-10-09 08:10:36 -0700189 http_server->AddHttpRequestHandler("/privet/ping", ping_handler);
190 http_server->AddHttpsRequestHandler("/privet/ping", ping_handler);
Alex Vakulenko0022b752015-10-02 11:09:59 -0700191 }
Alex Vakulenkof0f55342015-08-18 15:51:40 -0700192 }
Christopher Wileye925bb32015-08-03 20:09:18 -0700193#endif // BUFFET_USE_WIFI_BOOTSTRAPPING
Vitaly Buka7042c582015-07-30 17:02:14 -0700194
Alex Vakulenkobe39e932015-10-09 08:10:36 -0700195 if (!http_server)
196 CreateDevice();
197}
198
199void Manager::CreateDevice() {
200 if (device_)
201 return;
202
Alex Vakulenko0022b752015-10-02 11:09:59 -0700203 device_ = weave::Device::Create(config_.get(), task_runner_.get(),
204 http_client_.get(), shill_client_.get(),
205 mdns_client_.get(), web_serv_client_.get(),
206 shill_client_.get(), bluetooth_client_.get());
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700207
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700208 LoadCommandDefinitions(options_.config_options, device_.get());
209 LoadStateDefinitions(options_.config_options, device_.get());
210 LoadStateDefaults(options_.config_options, device_.get());
211
Alex Vakulenko0022b752015-10-02 11:09:59 -0700212 device_->AddSettingsChangedCallback(
213 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700214
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700215 command_dispatcher_.reset(
216 new DBusCommandDispacher{dbus_object_.GetObjectManager(), device_.get()});
Vitaly Bukae8c3bab2015-07-29 16:39:34 -0700217
Alex Vakulenko0022b752015-10-02 11:09:59 -0700218 device_->AddStateChangedCallback(
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700219 base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700220
Alex Vakulenko0022b752015-10-02 11:09:59 -0700221 device_->AddGcdStateChangedCallback(
222 base::Bind(&Manager::OnGcdStateChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -0700223
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700224 device_->AddPairingChangedCallbacks(
225 base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
226 base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
Alex Vakulenkoc662a242015-10-15 13:04:06 -0700227
228 auto handler = dbus_registration_handler_;
229 if (handler.is_null())
230 handler = AsyncEventSequencer::GetDefaultCompletionAction();
231 dbus_object_.RegisterAsync(handler);
232 dbus_registration_handler_.Reset();
Vitaly Buka84fd6dd2015-06-09 17:22:18 -0700233}
234
235void Manager::Stop() {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700236 command_dispatcher_.reset();
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700237 device_.reset();
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700238#ifdef BUFFET_USE_WIFI_BOOTSTRAPPING
239 web_serv_client_.reset();
240 mdns_client_.reset();
241#endif // BUFFET_USE_WIFI_BOOTSTRAPPING
242 shill_client_.reset();
243 http_client_.reset();
244 config_.reset();
245 task_runner_.reset();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700246}
247
Alex Vakulenko6c375262015-06-19 11:01:42 -0700248void Manager::RegisterDevice(DBusMethodResponsePtr<std::string> response,
Vitaly Bukacbadabe2015-05-14 23:33:32 -0700249 const std::string& ticket_id) {
Anton Muhin9cc03fd2014-10-16 18:59:57 +0400250 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700251
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700252 device_->Register(ticket_id, base::Bind(&Manager::RegisterDeviceDone,
253 weak_ptr_factory_.GetWeakPtr(),
254 base::Passed(&response)));
255}
Alex Vakulenkobe39e932015-10-09 08:10:36 -0700256
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700257void Manager::RegisterDeviceDone(DBusMethodResponsePtr<std::string> response,
258 weave::ErrorPtr error) {
259 if (error) {
260 brillo::ErrorPtr brillo_error;
261 ConvertError(*error, &brillo_error);
262 return response->ReplyWithError(brillo_error.get());
263 }
264 LOG(INFO) << "Device registered: " << device_->GetSettings().cloud_id;
265 response->Return(device_->GetSettings().cloud_id);
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700266}
267
Alex Vakulenko6c375262015-06-19 11:01:42 -0700268void Manager::UpdateState(DBusMethodResponsePtr<> response,
Alex Vakulenko41705852015-10-13 10:12:06 -0700269 const brillo::VariantDictionary& property_set) {
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700270 brillo::ErrorPtr brillo_error;
Vitaly Buka4f771532015-08-14 14:58:39 -0700271 auto properties =
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700272 DictionaryFromDBusVariantDictionary(property_set, &brillo_error);
Vitaly Bukafb2584b2015-07-28 21:39:45 -0700273 if (!properties)
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700274 return response->ReplyWithError(brillo_error.get());
Vitaly Bukafb2584b2015-07-28 21:39:45 -0700275
Vitaly Buka4f771532015-08-14 14:58:39 -0700276 weave::ErrorPtr error;
Alex Vakulenko0022b752015-10-02 11:09:59 -0700277 if (!device_->SetStateProperties(*properties, &error)) {
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700278 ConvertError(*error, &brillo_error);
279 return response->ReplyWithError(brillo_error.get());
Vitaly Buka4f771532015-08-14 14:58:39 -0700280 }
281 response->Return();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700282}
283
Alex Vakulenko41705852015-10-13 10:12:06 -0700284bool Manager::GetState(brillo::ErrorPtr* error, std::string* state) {
Alex Vakulenko0022b752015-10-02 11:09:59 -0700285 auto json = device_->GetState();
Vitaly Bukacd5e5562015-07-28 15:33:55 -0700286 CHECK(json);
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400287 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700288 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800289 return true;
290}
291
Alex Vakulenko6c375262015-06-19 11:01:42 -0700292void Manager::AddCommand(DBusMethodResponsePtr<std::string> response,
Alex Vakulenko0022b752015-10-02 11:09:59 -0700293 const std::string& json_command) {
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700294 std::string error_message;
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700295 std::unique_ptr<base::Value> value(
296 base::JSONReader::ReadAndReturnError(json_command, base::JSON_PARSE_RFC,
297 nullptr, &error_message)
298 .release());
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700299 const base::DictionaryValue* command{nullptr};
300 if (!value || !value->GetAsDictionary(&command)) {
Alex Vakulenko41705852015-10-13 10:12:06 -0700301 return response->ReplyWithError(FROM_HERE, brillo::errors::json::kDomain,
302 brillo::errors::json::kParseError,
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700303 error_message);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700304 }
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700305
Vitaly Bukaa4e8d7f2015-06-09 09:46:53 -0700306 std::string id;
Vitaly Buka4f771532015-08-14 14:58:39 -0700307 weave::ErrorPtr error;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700308 if (!device_->AddCommand(*command, &id, &error)) {
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700309 brillo::ErrorPtr brillo_error;
310 ConvertError(*error, &brillo_error);
311 return response->ReplyWithError(brillo_error.get());
Vitaly Buka4f771532015-08-14 14:58:39 -0700312 }
Vitaly Bukac03ec2b2015-05-31 23:32:46 -0700313
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700314 response->Return(id);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700315}
316
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800317std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko35e3bab2014-08-15 11:45:46 -0700318 LOG(INFO) << "Received call to test method: " << message;
319 return message;
Christopher Wiley2ffa0042014-05-05 16:09:16 -0700320}
321
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700322void Manager::OnStateChanged() {
Alex Vakulenko0022b752015-10-02 11:09:59 -0700323 auto state = device_->GetState();
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700324 CHECK(state);
325 std::string json;
326 base::JSONWriter::WriteWithOptions(
Alex Vakulenkoab4e4ff2015-06-15 12:53:22 -0700327 *state, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka2b30e7a2015-05-27 09:27:08 -0700328 dbus_adaptor_.SetState(json);
329}
330
Alex Vakulenko0022b752015-10-02 11:09:59 -0700331void Manager::OnGcdStateChanged(weave::GcdState state) {
332 dbus_adaptor_.SetStatus(weave::EnumToString(state));
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700333}
334
Vitaly Buka00b83492015-07-20 00:37:48 -0700335void Manager::OnConfigChanged(const weave::Settings& settings) {
Alex Vakulenkoe4ef2612015-09-29 09:53:39 -0700336 dbus_adaptor_.SetDeviceId(settings.cloud_id);
Vitaly Buka00b83492015-07-20 00:37:48 -0700337 dbus_adaptor_.SetOemName(settings.oem_name);
338 dbus_adaptor_.SetModelName(settings.model_name);
339 dbus_adaptor_.SetModelId(settings.model_id);
340 dbus_adaptor_.SetName(settings.name);
341 dbus_adaptor_.SetDescription(settings.description);
342 dbus_adaptor_.SetLocation(settings.location);
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700343}
344
345void Manager::OnPairingStart(const std::string& session_id,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700346 weave::PairingType pairing_type,
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700347 const std::vector<uint8_t>& code) {
348 // For now, just overwrite the exposed PairInfo with
349 // the most recent pairing attempt.
Alex Vakulenko41705852015-10-13 10:12:06 -0700350 dbus_adaptor_.SetPairingInfo(brillo::VariantDictionary{
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700351 {kPairingSessionIdKey, session_id},
Vitaly Buka03319c22015-07-17 14:48:30 -0700352 {kPairingModeKey, weave::EnumToString(pairing_type)},
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700353 {kPairingCodeKey, code},
354 });
355}
356
357void Manager::OnPairingEnd(const std::string& session_id) {
358 auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo();
359 auto it = exposed_pairing_attempt.find(kPairingSessionIdKey);
360 if (it == exposed_pairing_attempt.end()) {
361 return;
362 }
363 std::string exposed_session{it->second.TryGet<std::string>()};
364 if (exposed_session == session_id) {
Alex Vakulenko41705852015-10-13 10:12:06 -0700365 dbus_adaptor_.SetPairingInfo(brillo::VariantDictionary{});
Vitaly Buka7b3ba792015-06-09 17:01:54 -0700366 }
367}
368
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700369} // namespace buffet