Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -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/privet/publisher.h" |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 6 | |
| 7 | #include <map> |
| 8 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 9 | #include <weave/error.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 10 | #include <weave/provider/dns_service_discovery.h> |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 11 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 12 | #include "src/privet/cloud_delegate.h" |
| 13 | #include "src/privet/device_delegate.h" |
Vitaly Buka | 87eb788 | 2015-10-27 22:23:49 -0700 | [diff] [blame] | 14 | #include "src/privet/device_ui_kind.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 15 | #include "src/privet/wifi_bootstrap_manager.h" |
| 16 | #include "src/privet/wifi_ssid_generator.h" |
| 17 | #include "src/string_utils.h" |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 18 | |
| 19 | namespace weave { |
| 20 | namespace privet { |
| 21 | |
| 22 | namespace { |
| 23 | |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 24 | // The service type we'll expose via DNS-SD. |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 25 | const char kPrivetServiceType[] = "_privet._tcp"; |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | Publisher::Publisher(const DeviceDelegate* device, |
| 30 | const CloudDelegate* cloud, |
| 31 | const WifiDelegate* wifi, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 32 | provider::DnsServiceDiscovery* dns_sd) |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 33 | : dns_sd_{dns_sd}, device_{device}, cloud_{cloud}, wifi_{wifi} { |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 34 | CHECK(device_); |
| 35 | CHECK(cloud_); |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 36 | CHECK(dns_sd_); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Publisher::~Publisher() { |
| 40 | RemoveService(); |
| 41 | } |
| 42 | |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 43 | void Publisher::Update() { |
| 44 | if (device_->GetHttpEnpoint().first == 0) |
| 45 | return RemoveService(); |
| 46 | ExposeService(); |
| 47 | } |
| 48 | |
| 49 | void Publisher::ExposeService() { |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 50 | std::string name{cloud_->GetName()}; |
| 51 | std::string model_id{cloud_->GetModelId()}; |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 52 | DCHECK_EQ(model_id.size(), 5U); |
| 53 | |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 54 | VLOG(2) << "DNS-SD update requested"; |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 55 | const uint16_t port = device_->GetHttpEnpoint().first; |
| 56 | DCHECK_NE(port, 0); |
| 57 | |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 58 | std::vector<std::string> txt_record{ |
| 59 | {"txtvers=3"}, |
| 60 | {"ty=" + name}, |
Vitaly Buka | 87eb788 | 2015-10-27 22:23:49 -0700 | [diff] [blame] | 61 | {"services=" + GetDeviceUiKind(model_id)}, |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 62 | {"id=" + cloud_->GetDeviceId()}, |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 63 | {"mmid=" + model_id}, |
| 64 | {"flags=" + WifiSsidGenerator{cloud_, wifi_}.GenerateFlags()}, |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | if (!cloud_->GetCloudId().empty()) |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 68 | txt_record.emplace_back("gcd_id=" + cloud_->GetCloudId()); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 69 | |
| 70 | if (!cloud_->GetDescription().empty()) |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 71 | txt_record.emplace_back("note=" + cloud_->GetDescription()); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 72 | |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 73 | auto new_data = std::make_pair(port, txt_record); |
| 74 | if (published_ == new_data) |
| 75 | return; |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 76 | VLOG(1) << "Updating DNS-SD"; |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 77 | published_ = new_data; |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 78 | dns_sd_->PublishService(kPrivetServiceType, port, txt_record); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void Publisher::RemoveService() { |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 82 | if (!published_.first) |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 83 | return; |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 84 | published_ = {}; |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 85 | VLOG(1) << "Stopping service publishing"; |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 86 | dns_sd_->StopPublishing(kPrivetServiceType); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace privet |
| 90 | } // namespace weave |