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_CHANNEL_MESSAGE_UTIL_H_
6 #define CAST_COMMON_CHANNEL_MESSAGE_UTIL_H_
7
8 #include <string>
9
10 #include "absl/strings/string_view.h"
11 #include "cast/common/channel/proto/cast_channel.pb.h"
12 #include "util/enum_name_table.h"
13
14 namespace Json {
15 class Value;
16 }
17
18 namespace openscreen {
19 namespace cast {
20
21 // Reserved message namespaces for internal messages.
22 static constexpr char kCastInternalNamespacePrefix[] =
23 "urn:x-cast:com.google.cast.";
24 static constexpr char kTransportNamespacePrefix[] =
25 "urn:x-cast:com.google.cast.tp.";
26 static constexpr char kAuthNamespace[] =
27 "urn:x-cast:com.google.cast.tp.deviceauth";
28 static constexpr char kHeartbeatNamespace[] =
29 "urn:x-cast:com.google.cast.tp.heartbeat";
30 static constexpr char kConnectionNamespace[] =
31 "urn:x-cast:com.google.cast.tp.connection";
32 static constexpr char kReceiverNamespace[] =
33 "urn:x-cast:com.google.cast.receiver";
34 static constexpr char kBroadcastNamespace[] =
35 "urn:x-cast:com.google.cast.broadcast";
36 static constexpr char kMediaNamespace[] = "urn:x-cast:com.google.cast.media";
37
38 // Sender and receiver IDs to use for platform messages.
39 static constexpr char kPlatformSenderId[] = "sender-0";
40 static constexpr char kPlatformReceiverId[] = "receiver-0";
41
42 static constexpr char kBroadcastId[] = "*";
43
44 static constexpr ::cast::channel::CastMessage_ProtocolVersion
45 kDefaultOutgoingMessageVersion =
46 ::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_0;
47
48 // JSON message key strings.
49 static constexpr char kMessageKeyType[] = "type";
50 static constexpr char kMessageKeyProtocolVersion[] = "protocolVersion";
51 static constexpr char kMessageKeyProtocolVersionList[] = "protocolVersionList";
52 static constexpr char kMessageKeyReasonCode[] = "reasonCode";
53 static constexpr char kMessageKeyAppId[] = "appId";
54 static constexpr char kMessageKeyRequestId[] = "requestId";
55 static constexpr char kMessageKeyResponseType[] = "responseType";
56 static constexpr char kMessageKeyTransportId[] = "transportId";
57 static constexpr char kMessageKeySessionId[] = "sessionId";
58
59 // JSON message field values.
60 static constexpr char kMessageTypeConnect[] = "CONNECT";
61 static constexpr char kMessageTypeClose[] = "CLOSE";
62 static constexpr char kMessageTypeConnected[] = "CONNECTED";
63 static constexpr char kMessageValueAppAvailable[] = "APP_AVAILABLE";
64 static constexpr char kMessageValueAppUnavailable[] = "APP_UNAVAILABLE";
65
66 // JSON message key strings specific to CONNECT messages.
67 static constexpr char kMessageKeyBrowserVersion[] = "browserVersion";
68 static constexpr char kMessageKeyConnType[] = "connType";
69 static constexpr char kMessageKeyConnectionType[] = "connectionType";
70 static constexpr char kMessageKeyUserAgent[] = "userAgent";
71 static constexpr char kMessageKeyOrigin[] = "origin";
72 static constexpr char kMessageKeyPlatform[] = "platform";
73 static constexpr char kMessageKeySdkType[] = "skdType";
74 static constexpr char kMessageKeySenderInfo[] = "senderInfo";
75 static constexpr char kMessageKeyVersion[] = "version";
76
77 // JSON message key strings specific to application control messages.
78 static constexpr char kMessageKeyAvailability[] = "availability";
79 static constexpr char kMessageKeyAppParams[] = "appParams";
80 static constexpr char kMessageKeyApplications[] = "applications";
81 static constexpr char kMessageKeyControlType[] = "controlType";
82 static constexpr char kMessageKeyDisplayName[] = "displayName";
83 static constexpr char kMessageKeyIsIdleScreen[] = "isIdleScreen";
84 static constexpr char kMessageKeyLaunchedFromCloud[] = "launchedFromCloud";
85 static constexpr char kMessageKeyLevel[] = "level";
86 static constexpr char kMessageKeyMuted[] = "muted";
87 static constexpr char kMessageKeyName[] = "name";
88 static constexpr char kMessageKeyNamespaces[] = "namespaces";
89 static constexpr char kMessageKeyReason[] = "reason";
90 static constexpr char kMessageKeyStatus[] = "status";
91 static constexpr char kMessageKeyStepInterval[] = "stepInterval";
92 static constexpr char kMessageKeyUniversalAppId[] = "universalAppId";
93 static constexpr char kMessageKeyUserEq[] = "userEq";
94 static constexpr char kMessageKeyVolume[] = "volume";
95
96 // JSON message field value strings specific to application control messages.
97 static constexpr char kMessageValueAttenuation[] = "attenuation";
98 static constexpr char kMessageValueBadParameter[] = "BAD_PARAMETER";
99 static constexpr char kMessageValueInvalidSessionId[] = "INVALID_SESSION_ID";
100 static constexpr char kMessageValueInvalidCommand[] = "INVALID_COMMAND";
101 static constexpr char kMessageValueNotFound[] = "NOT_FOUND";
102 static constexpr char kMessageValueSystemError[] = "SYSTEM_ERROR";
103
104 // TODO(crbug.com/openscreen/111): Add validation that each message type is
105 // received on the correct namespace. This will probably involve creating a
106 // data structure for mapping between type and namespace.
107 enum class CastMessageType {
108 // Heartbeat messages.
109 kPing,
110 kPong,
111
112 // RPC control/status messages used by Media Remoting. These occur at high
113 // frequency, up to dozens per second at times, and should not be logged.
114 kRpc,
115
116 kGetAppAvailability,
117 kGetStatus,
118
119 // Virtual connection request.
120 kConnect,
121
122 // Close virtual connection.
123 kCloseConnection,
124
125 // Application broadcast / precache.
126 kBroadcast,
127
128 // Session launch request.
129 kLaunch,
130
131 // Session stop request.
132 kStop,
133
134 kReceiverStatus,
135 kMediaStatus,
136
137 // Error from receiver.
138 kLaunchError,
139
140 kOffer,
141 kAnswer,
142 kCapabilitiesResponse,
143 kStatusResponse,
144
145 // The following values are part of the protocol but are not currently used.
146 kMultizoneStatus,
147 kInvalidPlayerState,
148 kLoadFailed,
149 kLoadCancelled,
150 kInvalidRequest,
151 kPresentation,
152 kGetCapabilities,
153
154 kOther, // Add new types above |kOther|.
155 kMaxValue = kOther,
156 };
157
158 enum class AppAvailabilityResult {
159 kAvailable,
160 kUnavailable,
161 kUnknown,
162 };
163
164 std::string ToString(AppAvailabilityResult availability);
165
166 static const EnumNameTable<CastMessageType, 25> kCastMessageTypeNames{
167 {{"PING", CastMessageType::kPing},
168 {"PONG", CastMessageType::kPong},
169 {"RPC", CastMessageType::kRpc},
170 {"GET_APP_AVAILABILITY", CastMessageType::kGetAppAvailability},
171 {"GET_STATUS", CastMessageType::kGetStatus},
172 {"CONNECT", CastMessageType::kConnect},
173 {"CLOSE", CastMessageType::kCloseConnection},
174 {"APPLICATION_BROADCAST", CastMessageType::kBroadcast},
175 {"LAUNCH", CastMessageType::kLaunch},
176 {"STOP", CastMessageType::kStop},
177 {"RECEIVER_STATUS", CastMessageType::kReceiverStatus},
178 {"MEDIA_STATUS", CastMessageType::kMediaStatus},
179 {"LAUNCH_ERROR", CastMessageType::kLaunchError},
180 {"OFFER", CastMessageType::kOffer},
181 {"ANSWER", CastMessageType::kAnswer},
182 {"CAPABILITIES_RESPONSE", CastMessageType::kCapabilitiesResponse},
183 {"STATUS_RESPONSE", CastMessageType::kStatusResponse},
184 {"MULTIZONE_STATUS", CastMessageType::kMultizoneStatus},
185 {"INVALID_PLAYER_STATE", CastMessageType::kInvalidPlayerState},
186 {"LOAD_FAILED", CastMessageType::kLoadFailed},
187 {"LOAD_CANCELLED", CastMessageType::kLoadCancelled},
188 {"INVALID_REQUEST", CastMessageType::kInvalidRequest},
189 {"PRESENTATION", CastMessageType::kPresentation},
190 {"GET_CAPABILITIES", CastMessageType::kGetCapabilities},
191 {"OTHER", CastMessageType::kOther}}};
192
CastMessageTypeToString(CastMessageType type)193 inline const char* CastMessageTypeToString(CastMessageType type) {
194 return GetEnumName(kCastMessageTypeNames, type).value("OTHER");
195 }
196
IsAuthMessage(const::cast::channel::CastMessage & message)197 inline bool IsAuthMessage(const ::cast::channel::CastMessage& message) {
198 return message.namespace_() == kAuthNamespace;
199 }
200
IsTransportNamespace(absl::string_view namespace_)201 inline bool IsTransportNamespace(absl::string_view namespace_) {
202 return (namespace_.size() > (sizeof(kTransportNamespacePrefix) - 1)) &&
203 (namespace_.find_first_of(kTransportNamespacePrefix) == 0);
204 }
205
206 ::cast::channel::CastMessage MakeSimpleUTF8Message(
207 const std::string& namespace_,
208 std::string payload);
209
210 ::cast::channel::CastMessage MakeConnectMessage(
211 const std::string& source_id,
212 const std::string& destination_id);
213 ::cast::channel::CastMessage MakeCloseMessage(
214 const std::string& source_id,
215 const std::string& destination_id);
216
217 // Returns a session/transport ID string that is unique within this application
218 // instance, having the format "prefix-12345". For example, calling this with a
219 // |prefix| of "sender" will result in a string like "sender-12345".
220 std::string MakeUniqueSessionId(const char* prefix);
221
222 // Returns true if the type field in |object| is set to the given |type|.
223 bool HasType(const Json::Value& object, CastMessageType type);
224 } // namespace cast
225 } // namespace openscreen
226
227 #endif // CAST_COMMON_CHANNEL_MESSAGE_UTIL_H_
228