Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame] | 1 | // 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 Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 14 | |
| 15 | #include "buffet/manager.h" |
| 16 | |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 17 | #include <map> |
Alex Vakulenko | ec41a0c | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 18 | #include <set> |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 21 | #include <base/bind.h> |
| 22 | #include <base/bind_helpers.h> |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 23 | #include <base/files/file_enumerator.h> |
| 24 | #include <base/files/file_util.h> |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 25 | #include <base/json/json_reader.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 26 | #include <base/json/json_writer.h> |
Alex Vakulenko | f775454 | 2015-06-26 12:59:50 -0700 | [diff] [blame] | 27 | #include <base/message_loop/message_loop.h> |
Christopher Wiley | 8994e01 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 28 | #include <base/time/time.h> |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 29 | #include <binderwrapper/binder_wrapper.h> |
Alex Vakulenko | 790643f | 2015-11-19 14:38:30 -0800 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 31 | #include <brillo/bind_lambda.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 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 Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 38 | #include <dbus/bus.h> |
Christopher Wiley | 54028f9 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 39 | #include <dbus/object_path.h> |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 40 | #include <dbus/values_util.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 41 | #include <weave/enum_to_string.h> |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 42 | |
Alex Vakulenko | 790643f | 2015-11-19 14:38:30 -0800 | [diff] [blame] | 43 | #include "brillo/weaved_system_properties.h" |
Robert Ginda | f86123d | 2015-09-11 16:10:43 -0700 | [diff] [blame] | 44 | #include "buffet/bluetooth_client.h" |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 45 | #include "buffet/buffet_config.h" |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 46 | #include "buffet/http_transport_client.h" |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 47 | #include "buffet/mdns_client.h" |
Peter Qiu | 786a906 | 2015-10-02 11:45:01 -0700 | [diff] [blame] | 48 | #include "buffet/shill_client.h" |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 49 | #include "buffet/weave_error_conversion.h" |
Alex Vakulenko | 1642bec | 2015-08-19 09:34:58 -0700 | [diff] [blame] | 50 | #include "buffet/webserv_client.h" |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 51 | #include "common/binder_utils.h" |
Vitaly Buka | 03319c2 | 2015-07-17 14:48:30 -0700 | [diff] [blame] | 52 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 53 | using brillo::dbus_utils::AsyncEventSequencer; |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 54 | using NotificationListener = |
| 55 | android::weave::IWeaveServiceManagerNotificationListener; |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 56 | |
| 57 | namespace buffet { |
| 58 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 59 | namespace { |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 60 | |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 61 | const char kErrorDomain[] = "buffet"; |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 62 | const char kFileReadError[] = "file_read_error"; |
| 63 | |
| 64 | bool LoadFile(const base::FilePath& file_path, |
| 65 | std::string* data, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 66 | brillo::ErrorPtr* error) { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 67 | if (!base::ReadFileToString(file_path, data)) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 68 | brillo::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 69 | brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError, |
| 70 | "Failed to read file '%s'", |
| 71 | file_path.value().c_str()); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 77 | void LoadTraitDefinitions(const BuffetConfig::Options& options, |
| 78 | weave::Device* device) { |
| 79 | // Load component-specific device trait definitions. |
| 80 | base::FilePath dir{options.definitions.Append("traits")}; |
| 81 | LOG(INFO) << "Looking for trait definitions in " << dir.value(); |
| 82 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 83 | FILE_PATH_LITERAL("*.json")); |
| 84 | std::vector<std::string> result; |
| 85 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 86 | path = enumerator.Next()) { |
| 87 | LOG(INFO) << "Loading trait definition from " << path.value(); |
| 88 | std::string json; |
| 89 | CHECK(LoadFile(path, &json, nullptr)); |
| 90 | device->AddTraitDefinitionsFromJson(json); |
| 91 | } |
| 92 | } |
| 93 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 94 | void LoadCommandDefinitions(const BuffetConfig::Options& options, |
| 95 | weave::Device* device) { |
| 96 | auto load_packages = [device](const base::FilePath& root, |
| 97 | const base::FilePath::StringType& pattern) { |
| 98 | base::FilePath dir{root.Append("commands")}; |
| 99 | LOG(INFO) << "Looking for command schemas in " << dir.value(); |
| 100 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 101 | pattern); |
| 102 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 103 | path = enumerator.Next()) { |
| 104 | LOG(INFO) << "Loading command schema from " << path.value(); |
| 105 | std::string json; |
| 106 | CHECK(LoadFile(path, &json, nullptr)); |
| 107 | device->AddCommandDefinitionsFromJson(json); |
| 108 | } |
| 109 | }; |
| 110 | load_packages(options.definitions, FILE_PATH_LITERAL("*.json")); |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 111 | if (!options.test_definitions.empty()) |
| 112 | load_packages(options.test_definitions, FILE_PATH_LITERAL("*test.json")); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void LoadStateDefinitions(const BuffetConfig::Options& options, |
| 116 | weave::Device* device) { |
| 117 | // Load component-specific device state definitions. |
| 118 | base::FilePath dir{options.definitions.Append("states")}; |
| 119 | LOG(INFO) << "Looking for state definitions in " << dir.value(); |
| 120 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 121 | FILE_PATH_LITERAL("*.schema.json")); |
| 122 | std::vector<std::string> result; |
| 123 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 124 | path = enumerator.Next()) { |
| 125 | LOG(INFO) << "Loading state definition from " << path.value(); |
| 126 | std::string json; |
| 127 | CHECK(LoadFile(path, &json, nullptr)); |
| 128 | device->AddStateDefinitionsFromJson(json); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void LoadStateDefaults(const BuffetConfig::Options& options, |
| 133 | weave::Device* device) { |
| 134 | // Load component-specific device state defaults. |
| 135 | base::FilePath dir{options.definitions.Append("states")}; |
| 136 | LOG(INFO) << "Looking for state defaults in " << dir.value(); |
| 137 | base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES, |
| 138 | FILE_PATH_LITERAL("*.defaults.json")); |
| 139 | std::vector<std::string> result; |
| 140 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 141 | path = enumerator.Next()) { |
| 142 | LOG(INFO) << "Loading state defaults from " << path.value(); |
| 143 | std::string json; |
| 144 | CHECK(LoadFile(path, &json, nullptr)); |
| 145 | CHECK(device->SetStatePropertiesFromJson(json, nullptr)); |
| 146 | } |
| 147 | } |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 148 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 149 | // Updates the manager's state property if the new value is different from |
| 150 | // the current value. In this case also adds the appropriate notification ID |
| 151 | // to the array to record the state change for clients. |
| 152 | void UpdateValue(Manager* manager, |
| 153 | std::string Manager::* prop, |
| 154 | const std::string& new_value, |
| 155 | int notification, |
| 156 | std::vector<int>* notification_ids) { |
| 157 | if (manager->*prop != new_value) { |
| 158 | manager->*prop = new_value; |
| 159 | notification_ids->push_back(notification); |
| 160 | } |
| 161 | } |
| 162 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 163 | } // anonymous namespace |
| 164 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 165 | class Manager::TaskRunner : public weave::provider::TaskRunner { |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 166 | public: |
| 167 | void PostDelayedTask(const tracked_objects::Location& from_here, |
| 168 | const base::Closure& task, |
| 169 | base::TimeDelta delay) override { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 170 | brillo::MessageLoop::current()->PostDelayedTask(from_here, task, delay); |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 171 | } |
| 172 | }; |
| 173 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 174 | Manager::Manager(const Options& options, |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 175 | const scoped_refptr<dbus::Bus>& bus) |
| 176 | : options_{options}, bus_{bus} {} |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 177 | |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 178 | Manager::~Manager() { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 179 | android::BinderWrapper* binder_wrapper = android::BinderWrapper::Get(); |
| 180 | for (const auto& listener : notification_listeners_) { |
| 181 | binder_wrapper->UnregisterForDeathNotifications( |
| 182 | android::IInterface::asBinder(listener)); |
| 183 | } |
| 184 | for (const auto& pair : services_) { |
| 185 | binder_wrapper->UnregisterForDeathNotifications( |
| 186 | android::IInterface::asBinder(pair.first)); |
| 187 | } |
Vitaly Buka | 91cc715 | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 188 | } |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 189 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 190 | void Manager::Start(AsyncEventSequencer* sequencer) { |
| 191 | RestartWeave(sequencer); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void Manager::RestartWeave(AsyncEventSequencer* sequencer) { |
| 195 | Stop(); |
| 196 | |
Vitaly Buka | e74c872 | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 197 | task_runner_.reset(new TaskRunner{}); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 198 | config_.reset(new BuffetConfig{options_.config_options}); |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 199 | http_client_.reset(new HttpTransportClient); |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 200 | shill_client_.reset(new ShillClient{bus_, |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 201 | options_.device_whitelist, |
| 202 | !options_.xmpp_enabled}); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 203 | weave::provider::HttpServer* http_server{nullptr}; |
Christopher Wiley | e925bb3 | 2015-08-03 20:09:18 -0700 | [diff] [blame] | 204 | #ifdef BUFFET_USE_WIFI_BOOTSTRAPPING |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 205 | if (!options_.disable_privet) { |
Casey Dahlin | 494b724 | 2015-12-14 11:42:47 -0800 | [diff] [blame] | 206 | mdns_client_ = MdnsClient::CreateInstance(); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 207 | web_serv_client_.reset(new WebServClient{ |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 208 | bus_, sequencer, |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 209 | base::Bind(&Manager::CreateDevice, weak_ptr_factory_.GetWeakPtr())}); |
Robert Ginda | f86123d | 2015-09-11 16:10:43 -0700 | [diff] [blame] | 210 | bluetooth_client_ = BluetoothClient::CreateInstance(); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 211 | http_server = web_serv_client_.get(); |
| 212 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 213 | if (options_.enable_ping) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 214 | auto ping_handler = base::Bind( |
| 215 | [](std::unique_ptr<weave::provider::HttpServer::Request> request) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 216 | request->SendReply(brillo::http::status_code::Ok, "Hello, world!", |
| 217 | brillo::mime::text::kPlain); |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 218 | }); |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 219 | http_server->AddHttpRequestHandler("/privet/ping", ping_handler); |
| 220 | http_server->AddHttpsRequestHandler("/privet/ping", ping_handler); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 221 | } |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 222 | } |
Christopher Wiley | e925bb3 | 2015-08-03 20:09:18 -0700 | [diff] [blame] | 223 | #endif // BUFFET_USE_WIFI_BOOTSTRAPPING |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 224 | |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 225 | if (!http_server) |
| 226 | CreateDevice(); |
| 227 | } |
| 228 | |
| 229 | void Manager::CreateDevice() { |
| 230 | if (device_) |
| 231 | return; |
| 232 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 233 | device_ = weave::Device::Create(config_.get(), task_runner_.get(), |
| 234 | http_client_.get(), shill_client_.get(), |
| 235 | mdns_client_.get(), web_serv_client_.get(), |
| 236 | shill_client_.get(), bluetooth_client_.get()); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 237 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 238 | LoadTraitDefinitions(options_.config_options, device_.get()); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 239 | LoadCommandDefinitions(options_.config_options, device_.get()); |
| 240 | LoadStateDefinitions(options_.config_options, device_.get()); |
| 241 | LoadStateDefaults(options_.config_options, device_.get()); |
| 242 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 243 | device_->AddSettingsChangedCallback( |
| 244 | base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 245 | |
Alex Vakulenko | 14b3e6e | 2015-12-10 10:01:36 -0800 | [diff] [blame] | 246 | device_->AddTraitDefsChangedCallback( |
| 247 | base::Bind(&Manager::OnTraitDefsChanged, |
| 248 | weak_ptr_factory_.GetWeakPtr())); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 249 | device_->AddStateChangedCallback( |
Alex Vakulenko | 14b3e6e | 2015-12-10 10:01:36 -0800 | [diff] [blame] | 250 | base::Bind(&Manager::OnComponentTreeChanged, |
| 251 | weak_ptr_factory_.GetWeakPtr())); |
| 252 | device_->AddComponentTreeChangedCallback( |
| 253 | base::Bind(&Manager::OnComponentTreeChanged, |
| 254 | weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 255 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 256 | device_->AddGcdStateChangedCallback( |
| 257 | base::Bind(&Manager::OnGcdStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | e74fe3c | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 258 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 259 | device_->AddPairingChangedCallbacks( |
| 260 | base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()), |
| 261 | base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr())); |
Alex Vakulenko | c662a24 | 2015-10-15 13:04:06 -0700 | [diff] [blame] | 262 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 263 | CreateServicesForClients(); |
Vitaly Buka | 84fd6dd | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void Manager::Stop() { |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 267 | device_.reset(); |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 268 | #ifdef BUFFET_USE_WIFI_BOOTSTRAPPING |
| 269 | web_serv_client_.reset(); |
| 270 | mdns_client_.reset(); |
| 271 | #endif // BUFFET_USE_WIFI_BOOTSTRAPPING |
| 272 | shill_client_.reset(); |
| 273 | http_client_.reset(); |
| 274 | config_.reset(); |
| 275 | task_runner_.reset(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Alex Vakulenko | 14b3e6e | 2015-12-10 10:01:36 -0800 | [diff] [blame] | 278 | void Manager::OnTraitDefsChanged() { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 279 | NotifyServiceManagerChange({NotificationListener::TRAITS}); |
Alex Vakulenko | 14b3e6e | 2015-12-10 10:01:36 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void Manager::OnComponentTreeChanged() { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 283 | NotifyServiceManagerChange({NotificationListener::COMPONENTS}); |
Vitaly Buka | 2b30e7a | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 286 | void Manager::OnGcdStateChanged(weave::GcdState state) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 287 | state_ = weave::EnumToString(state); |
| 288 | NotifyServiceManagerChange({NotificationListener::STATE}); |
| 289 | property_set(weaved::system_properties::kState, state_.c_str()); |
Vitaly Buka | bf4ba65 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Vitaly Buka | 00b8349 | 2015-07-20 00:37:48 -0700 | [diff] [blame] | 292 | void Manager::OnConfigChanged(const weave::Settings& settings) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 293 | std::vector<int> ids; |
| 294 | UpdateValue(this, &Manager::cloud_id_, settings.cloud_id, |
| 295 | NotificationListener::CLOUD_ID, &ids); |
| 296 | UpdateValue(this, &Manager::device_id_, settings.device_id, |
| 297 | NotificationListener::DEVICE_ID, &ids); |
| 298 | UpdateValue(this, &Manager::device_name_, settings.name, |
| 299 | NotificationListener::DEVICE_NAME, &ids); |
| 300 | UpdateValue(this, &Manager::device_description_, settings.description, |
| 301 | NotificationListener::DEVICE_DESCRIPTION, &ids); |
| 302 | UpdateValue(this, &Manager::device_location_, settings.location, |
| 303 | NotificationListener::DEVICE_LOCATION, &ids); |
| 304 | UpdateValue(this, &Manager::oem_name_, settings.oem_name, |
| 305 | NotificationListener::OEM_NAME, &ids); |
| 306 | UpdateValue(this, &Manager::model_id_, settings.model_id, |
| 307 | NotificationListener::MODEL_ID, &ids); |
| 308 | UpdateValue(this, &Manager::model_name_, settings.model_name, |
| 309 | NotificationListener::MODEL_NAME, &ids); |
| 310 | NotifyServiceManagerChange(ids); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void Manager::OnPairingStart(const std::string& session_id, |
Vitaly Buka | 0c6dcd2 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 314 | weave::PairingType pairing_type, |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 315 | const std::vector<uint8_t>& code) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 316 | // For now, just overwrite the exposed PairInfo with the most recent pairing |
| 317 | // attempt. |
| 318 | std::vector<int> ids; |
| 319 | UpdateValue(this, &Manager::pairing_session_id_, session_id, |
| 320 | NotificationListener::PAIRING_SESSION_ID, &ids); |
| 321 | UpdateValue(this, &Manager::pairing_mode_, EnumToString(pairing_type), |
| 322 | NotificationListener::PAIRING_MODE, &ids); |
| 323 | std::string pairing_code{code.begin(), code.end()}; |
| 324 | UpdateValue(this, &Manager::pairing_code_, pairing_code, |
| 325 | NotificationListener::PAIRING_CODE, &ids); |
| 326 | NotifyServiceManagerChange(ids); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void Manager::OnPairingEnd(const std::string& session_id) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 330 | if (pairing_session_id_ != session_id) |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 331 | return; |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 332 | std::vector<int> ids; |
| 333 | UpdateValue(this, &Manager::pairing_session_id_, "", |
| 334 | NotificationListener::PAIRING_SESSION_ID, &ids); |
| 335 | UpdateValue(this, &Manager::pairing_mode_, "", |
| 336 | NotificationListener::PAIRING_MODE, &ids); |
| 337 | UpdateValue(this, &Manager::pairing_code_, "", |
| 338 | NotificationListener::PAIRING_CODE, &ids); |
| 339 | NotifyServiceManagerChange(ids); |
| 340 | } |
| 341 | |
| 342 | android::binder::Status Manager::connect( |
| 343 | const android::sp<android::weave::IWeaveClient>& client) { |
| 344 | pending_clients_.push_back(client); |
| 345 | if (device_) |
| 346 | CreateServicesForClients(); |
| 347 | return android::binder::Status::ok(); |
| 348 | } |
| 349 | |
| 350 | android::binder::Status Manager::registerNotificationListener( |
| 351 | const WeaveServiceManagerNotificationListener& listener) { |
| 352 | notification_listeners_.insert(listener); |
| 353 | android::BinderWrapper::Get()->RegisterForDeathNotifications( |
| 354 | android::IInterface::asBinder(listener), |
| 355 | base::Bind(&Manager::OnNotificationListenerDestroyed, |
| 356 | weak_ptr_factory_.GetWeakPtr(), listener)); |
| 357 | return android::binder::Status::ok(); |
| 358 | } |
| 359 | |
| 360 | android::binder::Status Manager::getCloudId(android::String16* id) { |
| 361 | *id = weaved::binder_utils::ToString16(cloud_id_); |
| 362 | return android::binder::Status::ok(); |
| 363 | } |
| 364 | |
| 365 | android::binder::Status Manager::getDeviceId(android::String16* id) { |
| 366 | *id = weaved::binder_utils::ToString16(device_id_); |
| 367 | return android::binder::Status::ok(); |
| 368 | } |
| 369 | |
| 370 | android::binder::Status Manager::getDeviceName(android::String16* name) { |
| 371 | *name = weaved::binder_utils::ToString16(device_name_); |
| 372 | return android::binder::Status::ok(); |
| 373 | } |
| 374 | |
| 375 | android::binder::Status Manager::getDeviceDescription( |
| 376 | android::String16* description) { |
| 377 | *description = weaved::binder_utils::ToString16(device_description_); |
| 378 | return android::binder::Status::ok(); |
| 379 | } |
| 380 | |
| 381 | android::binder::Status Manager::getDeviceLocation( |
| 382 | android::String16* location) { |
| 383 | *location = weaved::binder_utils::ToString16(device_location_); |
| 384 | return android::binder::Status::ok(); |
| 385 | } |
| 386 | |
| 387 | android::binder::Status Manager::getOemName(android::String16* name) { |
| 388 | *name = weaved::binder_utils::ToString16(oem_name_); |
| 389 | return android::binder::Status::ok(); |
| 390 | } |
| 391 | |
| 392 | android::binder::Status Manager::getModelName(android::String16* name) { |
| 393 | *name = weaved::binder_utils::ToString16(model_name_); |
| 394 | return android::binder::Status::ok(); |
| 395 | } |
| 396 | |
| 397 | android::binder::Status Manager::getModelId(android::String16* id) { |
| 398 | *id = weaved::binder_utils::ToString16(model_id_); |
| 399 | return android::binder::Status::ok(); |
| 400 | } |
| 401 | |
| 402 | android::binder::Status Manager::getPairingSessionId(android::String16* id) { |
| 403 | *id = weaved::binder_utils::ToString16(pairing_session_id_); |
| 404 | return android::binder::Status::ok(); |
| 405 | } |
| 406 | |
| 407 | android::binder::Status Manager::getPairingMode(android::String16* mode) { |
| 408 | *mode = weaved::binder_utils::ToString16(pairing_mode_); |
| 409 | return android::binder::Status::ok(); |
| 410 | } |
| 411 | |
| 412 | android::binder::Status Manager::getPairingCode(android::String16* code) { |
| 413 | *code = weaved::binder_utils::ToString16(pairing_code_); |
| 414 | return android::binder::Status::ok(); |
| 415 | } |
| 416 | |
| 417 | android::binder::Status Manager::getState(android::String16* state) { |
| 418 | *state = weaved::binder_utils::ToString16(state_); |
| 419 | return android::binder::Status::ok(); |
| 420 | } |
| 421 | |
| 422 | android::binder::Status Manager::getTraits(android::String16* traits) { |
| 423 | *traits = weaved::binder_utils::ToString16(device_->GetTraits()); |
| 424 | return android::binder::Status::ok(); |
| 425 | } |
| 426 | |
| 427 | android::binder::Status Manager::getComponents(android::String16* components) { |
| 428 | *components = weaved::binder_utils::ToString16(device_->GetComponents()); |
| 429 | return android::binder::Status::ok(); |
| 430 | } |
| 431 | |
| 432 | void Manager::CreateServicesForClients() { |
| 433 | CHECK(device_); |
| 434 | // For safety, iterate over a copy of |pending_clients_| and clear the |
| 435 | // original vector before performing the iterations. |
| 436 | std::vector<android::sp<android::weave::IWeaveClient>> pending_clients_copy; |
| 437 | std::swap(pending_clients_copy, pending_clients_); |
| 438 | for (const auto& client : pending_clients_copy) { |
| 439 | android::sp<BinderWeaveService> service = |
| 440 | new BinderWeaveService{device_.get(), client}; |
| 441 | services_.emplace(client, service); |
| 442 | client->onServiceConnected(service); |
| 443 | android::BinderWrapper::Get()->RegisterForDeathNotifications( |
| 444 | android::IInterface::asBinder(client), |
| 445 | base::Bind(&Manager::OnClientDisconnected, |
| 446 | weak_ptr_factory_.GetWeakPtr(), |
| 447 | client)); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 448 | } |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void Manager::OnClientDisconnected( |
| 452 | const android::sp<android::weave::IWeaveClient>& client) { |
| 453 | services_.erase(client); |
| 454 | } |
| 455 | |
| 456 | void Manager::OnNotificationListenerDestroyed( |
| 457 | const WeaveServiceManagerNotificationListener& notification_listener) { |
| 458 | notification_listeners_.erase(notification_listener); |
| 459 | } |
| 460 | |
| 461 | void Manager::NotifyServiceManagerChange( |
| 462 | const std::vector<int>& notification_ids) { |
| 463 | if (notification_ids.empty()) |
| 464 | return; |
| 465 | for (const auto& listener : notification_listeners_) |
| 466 | listener->notifyServiceManagerChange(notification_ids); |
Vitaly Buka | 7b3ba79 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 469 | } // namespace buffet |