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 | 22a0475 | 2015-11-06 14:53:35 -0800 | [diff] [blame] | 37 | Update(); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Publisher::~Publisher() { |
| 41 | RemoveService(); |
| 42 | } |
| 43 | |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 44 | void Publisher::Update() { |
| 45 | if (device_->GetHttpEnpoint().first == 0) |
| 46 | return RemoveService(); |
| 47 | ExposeService(); |
| 48 | } |
| 49 | |
| 50 | void Publisher::ExposeService() { |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 51 | std::string name{cloud_->GetName()}; |
| 52 | std::string model_id{cloud_->GetModelId()}; |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 53 | DCHECK_EQ(model_id.size(), 5U); |
| 54 | |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 55 | VLOG(2) << "DNS-SD update requested"; |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 56 | const uint16_t port = device_->GetHttpEnpoint().first; |
| 57 | DCHECK_NE(port, 0); |
| 58 | |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 59 | std::vector<std::string> txt_record{ |
| 60 | {"txtvers=3"}, |
| 61 | {"ty=" + name}, |
Vitaly Buka | 87eb788 | 2015-10-27 22:23:49 -0700 | [diff] [blame] | 62 | {"services=" + GetDeviceUiKind(model_id)}, |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 63 | {"id=" + cloud_->GetDeviceId()}, |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 64 | {"mmid=" + model_id}, |
| 65 | {"flags=" + WifiSsidGenerator{cloud_, wifi_}.GenerateFlags()}, |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | if (!cloud_->GetCloudId().empty()) |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 69 | txt_record.emplace_back("gcd_id=" + cloud_->GetCloudId()); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 70 | |
| 71 | if (!cloud_->GetDescription().empty()) |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 72 | txt_record.emplace_back("note=" + cloud_->GetDescription()); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 73 | |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 74 | auto new_data = std::make_pair(port, txt_record); |
| 75 | if (published_ == new_data) |
| 76 | return; |
Vitaly Buka | 22a0475 | 2015-11-06 14:53:35 -0800 | [diff] [blame] | 77 | |
| 78 | VLOG(1) << "Updating service using DNS-SD, port: " << port; |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 79 | published_ = new_data; |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 80 | dns_sd_->PublishService(kPrivetServiceType, port, txt_record); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void Publisher::RemoveService() { |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 84 | if (!published_.first) |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 85 | return; |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 86 | published_ = {}; |
Vitaly Buka | 7a35005 | 2015-10-10 23:58:20 -0700 | [diff] [blame] | 87 | VLOG(1) << "Stopping service publishing"; |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 88 | dns_sd_->StopPublishing(kPrivetServiceType); |
Vitaly Buka | 0bd3213 | 2015-07-30 15:54:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace privet |
| 92 | } // namespace weave |