1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_COMMON_PUBLIC_SERVICE_INFO_H_
6 #define CAST_COMMON_PUBLIC_SERVICE_INFO_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "discovery/dnssd/public/dns_sd_instance.h"
13 #include "discovery/dnssd/public/dns_sd_instance_endpoint.h"
14 #include "platform/base/ip_address.h"
15 
16 namespace openscreen {
17 namespace cast {
18 
19 // Constants to identify a CastV2 instance with DNS-SD.
20 constexpr char kCastV2ServiceId[] = "_googlecast._tcp";
21 constexpr char kCastV2DomainId[] = "local";
22 
23 // Constants to be used as keys when storing data inside of a DNS-SD TXT record.
24 constexpr char kUniqueIdKey[] = "id";
25 constexpr char kVersionKey[] = "ve";
26 constexpr char kCapabilitiesKey[] = "ca";
27 constexpr char kStatusKey[] = "st";
28 constexpr char kFriendlyNameKey[] = "fn";
29 constexpr char kModelNameKey[] = "md";
30 
31 // This represents the ‘st’ flag in the CastV2 TXT record.
32 enum ReceiverStatus {
33   // The receiver is idle and does not need to be connected now.
34   kIdle = 0,
35 
36   // The receiver is hosting an activity and invites the sender to join.  The
37   // receiver should connect to the running activity using the channel
38   // establishment protocol, and then query the activity to determine the next
39   // step, such as showing a description of the activity and prompting the user
40   // to launch the corresponding app.
41   kBusy = 1,
42   kJoin = kBusy
43 };
44 
45 constexpr uint8_t kCurrentCastVersion = 2;
46 
47 // Bits in the ‘ca’ bitfield, per the CastV2 spec.
48 constexpr uint64_t kHasVideoOutput = 1 << 0;
49 constexpr uint64_t kHasVideoInput = 1 << 1;
50 constexpr uint64_t kHasAudioOutput = 1 << 2;
51 constexpr uint64_t kHasAudioIntput = 1 << 3;
52 constexpr uint64_t kIsDevModeEnabled = 1 << 4;
53 
54 constexpr uint64_t kNoCapabilities = 0;
55 
56 // This is the top-level service info class for CastV2. It describes a specific
57 // service instance.
58 // TODO(crbug.com/openscreen/112): Rename this to CastReceiverInfo or similar.
59 struct ServiceInfo {
60   // returns the instance id associated with this ServiceInfo instance.
61   const std::string& GetInstanceId() const;
62 
63   // Returns whether all fields of this ServiceInfo are valid.
64   bool IsValid() const;
65 
66   // Addresses for the service. Present if an address of this address type
67   // exists and empty otherwise. When publishing a service instance, these
68   // values will be overridden based on |network_config| values provided in the
69   // discovery::Config object used to initialize discovery.
70   IPAddress v4_address;
71   IPAddress v6_address;
72 
73   // Port at which this service can be reached.
74   uint16_t port;
75 
76   // A UUID for the Cast receiver. This should be a universally unique
77   // identifier for the receiver, and should (but does not have to be) be stable
78   // across factory resets.
79   std::string unique_id;
80 
81   // Cast protocol version supported. Begins at 2 and is incremented by 1 with
82   // each version.
83   uint8_t protocol_version = kCurrentCastVersion;
84 
85   // Bitfield of ReceiverCapabilities supported by this service instance.
86   uint64_t capabilities = kNoCapabilities;
87 
88   // Status of the service instance.
89   ReceiverStatus status = ReceiverStatus::kIdle;
90 
91   // The model name of the device, e.g. “Eureka v1”, “Mollie”.
92   std::string model_name;
93 
94   // The friendly name of the device, e.g. “Living Room TV".
95   std::string friendly_name;
96 
97  private:
98   mutable std::string instance_id_ = "";
99 };
100 
101 inline bool operator==(const ServiceInfo& lhs, const ServiceInfo& rhs) {
102   return lhs.v4_address == rhs.v4_address && lhs.v6_address == rhs.v6_address &&
103          lhs.port == rhs.port && lhs.unique_id == rhs.unique_id &&
104          lhs.protocol_version == rhs.protocol_version &&
105          lhs.capabilities == rhs.capabilities && lhs.status == rhs.status &&
106          lhs.model_name == rhs.model_name &&
107          lhs.friendly_name == rhs.friendly_name;
108 }
109 
110 inline bool operator!=(const ServiceInfo& lhs, const ServiceInfo& rhs) {
111   return !(lhs == rhs);
112 }
113 
114 // Functions responsible for converting between CastV2 and DNS-SD
115 // representations of a service instance.
116 discovery::DnsSdInstance ServiceInfoToDnsSdInstance(const ServiceInfo& service);
117 
118 ErrorOr<ServiceInfo> DnsSdInstanceEndpointToServiceInfo(
119     const discovery::DnsSdInstanceEndpoint& endpoint);
120 
121 }  // namespace cast
122 }  // namespace openscreen
123 
124 #endif  // CAST_COMMON_PUBLIC_SERVICE_INFO_H_
125