Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/device_manager.h" |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 11 | #include "src/base_api_handler.h" |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 12 | #include "src/commands/schema_constants.h" |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 13 | #include "src/component_manager_impl.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 14 | #include "src/config.h" |
| 15 | #include "src/device_registration_info.h" |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 16 | #include "src/privet/auth_manager.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 17 | #include "src/privet/privet_manager.h" |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 18 | #include "src/string_utils.h" |
| 19 | #include "src/utils.h" |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 20 | |
| 21 | namespace weave { |
| 22 | |
Vitaly Buka | c5bdd17 | 2015-10-01 19:48:51 -0700 | [diff] [blame] | 23 | DeviceManager::DeviceManager(provider::ConfigStore* config_store, |
| 24 | provider::TaskRunner* task_runner, |
| 25 | provider::HttpClient* http_client, |
| 26 | provider::Network* network, |
| 27 | provider::DnsServiceDiscovery* dns_sd, |
| 28 | provider::HttpServer* http_server, |
| 29 | provider::Wifi* wifi, |
Vitaly Buka | 666b43e | 2015-12-08 16:35:47 -0800 | [diff] [blame] | 30 | provider::Bluetooth* bluetooth) |
| 31 | : config_{new Config{config_store}}, |
| 32 | component_manager_{new ComponentManagerImpl} { |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 33 | if (http_server) { |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 34 | auth_manager_.reset(new privet::AuthManager( |
| 35 | config_.get(), http_server->GetHttpsCertificateFingerprint())); |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 38 | device_info_.reset(new DeviceRegistrationInfo( |
Vitaly Buka | 666b43e | 2015-12-08 16:35:47 -0800 | [diff] [blame] | 39 | config_.get(), component_manager_.get(), task_runner, http_client, |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 40 | network, auth_manager_.get())); |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 41 | base_api_handler_.reset(new BaseApiHandler{device_info_.get(), this}); |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 42 | |
| 43 | device_info_->Start(); |
| 44 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 45 | if (http_server) { |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 46 | StartPrivet(task_runner, network, dns_sd, http_server, wifi, bluetooth); |
Vitaly Buka | 2ad9734 | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 47 | } else { |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 48 | CHECK(!dns_sd); |
Vitaly Buka | 2ad9734 | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 49 | } |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Vitaly Buka | c5bdd17 | 2015-10-01 19:48:51 -0700 | [diff] [blame] | 52 | DeviceManager::~DeviceManager() {} |
| 53 | |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 54 | const Settings& DeviceManager::GetSettings() const { |
Vitaly Buka | ae6b35d | 2015-09-29 23:16:54 -0700 | [diff] [blame] | 55 | return device_info_->GetSettings(); |
| 56 | } |
| 57 | |
| 58 | void DeviceManager::AddSettingsChangedCallback( |
| 59 | const SettingsChangedCallback& callback) { |
| 60 | device_info_->GetMutableConfig()->AddOnChangedCallback(callback); |
| 61 | } |
| 62 | |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 63 | Config* DeviceManager::GetConfig() { |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 64 | return device_info_->GetMutableConfig(); |
| 65 | } |
| 66 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 67 | void DeviceManager::StartPrivet(provider::TaskRunner* task_runner, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 68 | provider::Network* network, |
| 69 | provider::DnsServiceDiscovery* dns_sd, |
| 70 | provider::HttpServer* http_server, |
| 71 | provider::Wifi* wifi, |
| 72 | provider::Bluetooth* bluetooth) { |
Vitaly Buka | 5fe7609 | 2015-10-08 13:37:53 -0700 | [diff] [blame] | 73 | privet_.reset(new privet::Manager{task_runner}); |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 74 | privet_->Start(network, dns_sd, http_server, wifi, auth_manager_.get(), |
| 75 | device_info_.get(), component_manager_.get()); |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 78 | GcdState DeviceManager::GetGcdState() const { |
| 79 | return device_info_->GetGcdState(); |
| 80 | } |
| 81 | |
| 82 | void DeviceManager::AddGcdStateChangedCallback( |
| 83 | const GcdStateChangedCallback& callback) { |
| 84 | device_info_->AddGcdStateChangedCallback(callback); |
| 85 | } |
| 86 | |
Alex Vakulenko | e79fa91 | 2015-12-04 17:24:15 -0800 | [diff] [blame] | 87 | void DeviceManager::AddTraitDefinitionsFromJson(const std::string& json) { |
| 88 | CHECK(component_manager_->LoadTraits(json, nullptr)); |
| 89 | } |
| 90 | |
| 91 | void DeviceManager::AddTraitDefinitions(const base::DictionaryValue& dict) { |
| 92 | CHECK(component_manager_->LoadTraits(dict, nullptr)); |
| 93 | } |
| 94 | |
| 95 | const base::DictionaryValue& DeviceManager::GetTraits() const { |
| 96 | return component_manager_->GetTraits(); |
| 97 | } |
| 98 | |
Alex Vakulenko | ec9d848 | 2015-12-10 10:16:21 -0800 | [diff] [blame] | 99 | void DeviceManager::AddTraitDefsChangedCallback(const base::Closure& callback) { |
| 100 | component_manager_->AddTraitDefChangedCallback(callback); |
| 101 | } |
| 102 | |
Alex Vakulenko | e79fa91 | 2015-12-04 17:24:15 -0800 | [diff] [blame] | 103 | bool DeviceManager::AddComponent(const std::string& name, |
| 104 | const std::vector<std::string>& traits, |
| 105 | ErrorPtr* error) { |
| 106 | return component_manager_->AddComponent("", name, traits, error); |
| 107 | } |
| 108 | |
Alex Vakulenko | ce850b5 | 2016-01-04 09:27:50 -0800 | [diff] [blame] | 109 | bool DeviceManager::RemoveComponent(const std::string& name, ErrorPtr* error) { |
| 110 | return component_manager_->RemoveComponent("", name, error); |
| 111 | } |
| 112 | |
Alex Vakulenko | e79fa91 | 2015-12-04 17:24:15 -0800 | [diff] [blame] | 113 | void DeviceManager::AddComponentTreeChangedCallback( |
| 114 | const base::Closure& callback) { |
| 115 | component_manager_->AddComponentTreeChangedCallback(callback); |
| 116 | } |
| 117 | |
| 118 | const base::DictionaryValue& DeviceManager::GetComponents() const { |
| 119 | return component_manager_->GetComponents(); |
| 120 | } |
| 121 | |
| 122 | bool DeviceManager::SetStatePropertiesFromJson(const std::string& component, |
| 123 | const std::string& json, |
| 124 | ErrorPtr* error) { |
| 125 | return component_manager_->SetStatePropertiesFromJson(component, json, error); |
| 126 | } |
| 127 | |
| 128 | bool DeviceManager::SetStateProperties(const std::string& component, |
| 129 | const base::DictionaryValue& dict, |
| 130 | ErrorPtr* error) { |
| 131 | return component_manager_->SetStateProperties(component, dict, error); |
| 132 | } |
| 133 | |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 134 | const base::Value* DeviceManager::GetStateProperty(const std::string& component, |
| 135 | const std::string& name, |
| 136 | ErrorPtr* error) const { |
Alex Vakulenko | e79fa91 | 2015-12-04 17:24:15 -0800 | [diff] [blame] | 137 | return component_manager_->GetStateProperty(component, name, error); |
| 138 | } |
| 139 | |
| 140 | bool DeviceManager::SetStateProperty(const std::string& component, |
| 141 | const std::string& name, |
| 142 | const base::Value& value, |
| 143 | ErrorPtr* error) { |
| 144 | return component_manager_->SetStateProperty(component, name, value, error); |
| 145 | } |
| 146 | |
| 147 | void DeviceManager::AddCommandHandler(const std::string& component, |
| 148 | const std::string& command_name, |
| 149 | const CommandHandlerCallback& callback) { |
| 150 | component_manager_->AddCommandHandler(component, command_name, callback); |
| 151 | } |
| 152 | |
Vitaly Buka | 3110deb | 2015-10-06 19:54:09 -0700 | [diff] [blame] | 153 | void DeviceManager::AddCommandDefinitionsFromJson(const std::string& json) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 154 | auto dict = LoadJsonDict(json, nullptr); |
| 155 | CHECK(dict); |
| 156 | AddCommandDefinitions(*dict); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void DeviceManager::AddCommandDefinitions(const base::DictionaryValue& dict) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 160 | CHECK(component_manager_->AddLegacyCommandDefinitions(dict, nullptr)); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 163 | bool DeviceManager::AddCommand(const base::DictionaryValue& command, |
| 164 | std::string* id, |
| 165 | ErrorPtr* error) { |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 166 | auto command_instance = component_manager_->ParseCommandInstance( |
| 167 | command, Command::Origin::kLocal, UserRole::kOwner, id, error); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 168 | if (!command_instance) |
| 169 | return false; |
| 170 | component_manager_->AddCommand(std::move(command_instance)); |
| 171 | return true; |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Command* DeviceManager::FindCommand(const std::string& id) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 175 | return component_manager_->FindCommand(id); |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 178 | void DeviceManager::AddCommandHandler(const std::string& command_name, |
| 179 | const CommandHandlerCallback& callback) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 180 | if (command_name.empty()) |
| 181 | return component_manager_->AddCommandHandler("", "", callback); |
| 182 | |
| 183 | auto trait = SplitAtFirst(command_name, ".", true).first; |
| 184 | std::string component = component_manager_->FindComponentWithTrait(trait); |
| 185 | CHECK(!component.empty()); |
| 186 | component_manager_->AddCommandHandler(component, command_name, callback); |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 189 | void DeviceManager::AddStateChangedCallback(const base::Closure& callback) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 190 | component_manager_->AddStateChangedCallback(callback); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Vitaly Buka | 3110deb | 2015-10-06 19:54:09 -0700 | [diff] [blame] | 193 | void DeviceManager::AddStateDefinitionsFromJson(const std::string& json) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 194 | auto dict = LoadJsonDict(json, nullptr); |
| 195 | CHECK(dict); |
| 196 | AddStateDefinitions(*dict); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void DeviceManager::AddStateDefinitions(const base::DictionaryValue& dict) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 200 | CHECK(component_manager_->AddLegacyStateDefinitions(dict, nullptr)); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Vitaly Buka | 216e86d | 2015-10-06 20:23:02 -0700 | [diff] [blame] | 203 | bool DeviceManager::SetStatePropertiesFromJson(const std::string& json, |
| 204 | ErrorPtr* error) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 205 | auto dict = LoadJsonDict(json, error); |
| 206 | return dict && SetStateProperties(*dict, error); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Vitaly Buka | 216e86d | 2015-10-06 20:23:02 -0700 | [diff] [blame] | 209 | bool DeviceManager::SetStateProperties(const base::DictionaryValue& dict, |
| 210 | ErrorPtr* error) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 211 | for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 212 | std::string component = |
| 213 | component_manager_->FindComponentWithTrait(it.key()); |
| 214 | if (component.empty()) { |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 215 | Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
| 216 | "unrouted_state", |
| 217 | "Unable to set property value because there is no " |
| 218 | "component supporting " |
| 219 | "trait '%s'", |
| 220 | it.key().c_str()); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | base::DictionaryValue trait_state; |
| 224 | trait_state.Set(it.key(), it.value().DeepCopy()); |
| 225 | if (!component_manager_->SetStateProperties(component, trait_state, error)) |
| 226 | return false; |
| 227 | } |
| 228 | return true; |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 231 | const base::Value* DeviceManager::GetStateProperty( |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 232 | const std::string& name) const { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 233 | auto trait = SplitAtFirst(name, ".", true).first; |
| 234 | std::string component = component_manager_->FindComponentWithTrait(trait); |
| 235 | if (component.empty()) |
| 236 | return nullptr; |
| 237 | return component_manager_->GetStateProperty(component, name, nullptr); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool DeviceManager::SetStateProperty(const std::string& name, |
| 241 | const base::Value& value, |
| 242 | ErrorPtr* error) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 243 | auto trait = SplitAtFirst(name, ".", true).first; |
| 244 | std::string component = component_manager_->FindComponentWithTrait(trait); |
| 245 | if (component.empty()) { |
| 246 | Error::AddToPrintf( |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 247 | error, FROM_HERE, errors::commands::kDomain, "unrouted_state", |
| 248 | "Unable set value of state property '%s' because there is no component " |
| 249 | "supporting trait '%s'", |
| 250 | name.c_str(), trait.c_str()); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | return component_manager_->SetStateProperty(component, name, value, error); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 256 | const base::DictionaryValue& DeviceManager::GetState() const { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 257 | return component_manager_->GetLegacyState(); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 260 | void DeviceManager::Register(const std::string& ticket_id, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 261 | const DoneCallback& callback) { |
| 262 | device_info_->RegisterDevice(ticket_id, callback); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Vitaly Buka | e607361 | 2015-10-01 17:58:52 -0700 | [diff] [blame] | 265 | void DeviceManager::AddPairingChangedCallbacks( |
Vitaly Buka | 9aa15c7 | 2015-10-01 19:14:44 -0700 | [diff] [blame] | 266 | const PairingBeginCallback& begin_callback, |
| 267 | const PairingEndCallback& end_callback) { |
| 268 | if (privet_) |
| 269 | privet_->AddOnPairingChangedCallbacks(begin_callback, end_callback); |
Vitaly Buka | e607361 | 2015-10-01 17:58:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Vitaly Buka | c5bdd17 | 2015-10-01 19:48:51 -0700 | [diff] [blame] | 272 | std::unique_ptr<Device> Device::Create(provider::ConfigStore* config_store, |
| 273 | provider::TaskRunner* task_runner, |
| 274 | provider::HttpClient* http_client, |
| 275 | provider::Network* network, |
| 276 | provider::DnsServiceDiscovery* dns_sd, |
| 277 | provider::HttpServer* http_server, |
| 278 | provider::Wifi* wifi, |
| 279 | provider::Bluetooth* bluetooth) { |
| 280 | return std::unique_ptr<Device>{ |
| 281 | new DeviceManager{config_store, task_runner, http_client, network, dns_sd, |
| 282 | http_server, wifi, bluetooth}}; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | } // namespace weave |