1 /*
2 * Copyright (C) 2023-2023 Huawei Device Co., Ltd.
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.
14 * Description: supply cast session manager service proxy
15 * Author: zhangge
16 * Create: 2022-5-29
17 */
18
19 #include "cast_engine_errors.h"
20 #include "cast_session_manager_service_proxy.h"
21 #include "cast_engine_common_helper.h"
22 #include "cast_engine_log.h"
23
24 namespace OHOS {
25 namespace CastEngine {
26 namespace CastEngineClient {
27 DEFINE_CAST_ENGINE_LABEL("Cast-Client-Manager");
28
RegisterListener(sptr<ICastServiceListenerImpl> listener)29 int32_t CastSessionManagerServiceProxy::RegisterListener(sptr<ICastServiceListenerImpl> listener)
30 {
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 CLOGE("Failed to write the interface token");
37 return CAST_ENGINE_ERROR;
38 }
39 if (!data.WriteRemoteObject(listener->AsObject())) {
40 CLOGE("Failed to write cast service listener");
41 return CAST_ENGINE_ERROR;
42 }
43 if (Remote()->SendRequest(REGISTER_LISTENER, data, reply, option) != ERR_NONE) {
44 CLOGE("Failed to send ipc request when initing the cast service");
45 return CAST_ENGINE_ERROR;
46 }
47
48 return reply.ReadInt32();
49 }
50
UnregisterListener()51 int32_t CastSessionManagerServiceProxy::UnregisterListener()
52 {
53 MessageParcel data;
54 MessageParcel reply;
55 MessageOption option;
56
57 if (!data.WriteInterfaceToken(GetDescriptor())) {
58 CLOGE("Failed to write the interface token");
59 return CAST_ENGINE_ERROR;
60 }
61 int32_t ret = Remote()->SendRequest(UNREGISTER_LISTENER, data, reply, option);
62 if (ret == ERR_NO_PERMISSION) {
63 CLOGE("No permission when unregistering listener");
64 return ERR_NO_PERMISSION;
65 } else if (ret != ERR_NONE) {
66 CLOGE("Failed to send ipc request when unregistering listener");
67 return CAST_ENGINE_ERROR;
68 }
69
70 return reply.ReadInt32();
71 }
72
Release()73 int32_t CastSessionManagerServiceProxy::Release()
74 {
75 MessageParcel data;
76 MessageParcel reply;
77 MessageOption option;
78
79 if (!data.WriteInterfaceToken(GetDescriptor())) {
80 CLOGE("Failed to write the interface token");
81 return CAST_ENGINE_ERROR;
82 }
83 int32_t ret = Remote()->SendRequest(RELEASE, data, reply, option);
84 if (ret == ERR_NO_PERMISSION) {
85 CLOGE("No permission when Releasing the cast service");
86 return ERR_NO_PERMISSION;
87 } else if (ret != ERR_NONE) {
88 CLOGE("Failed to send ipc request when Releasing the cast service");
89 return CAST_ENGINE_ERROR;
90 }
91
92 return reply.ReadInt32();
93 }
94
CreateCastSession(const CastSessionProperty & property,sptr<ICastSessionImpl> & castSession)95 int32_t CastSessionManagerServiceProxy::CreateCastSession(const CastSessionProperty &property,
96 sptr<ICastSessionImpl> &castSession)
97 {
98 MessageParcel data;
99 MessageParcel reply;
100 MessageOption option;
101
102 if (!data.WriteInterfaceToken(GetDescriptor())) {
103 CLOGE("Failed to write the interface token");
104 return CAST_ENGINE_ERROR;
105 }
106 if (!WriteCastSessionProperty(data, property)) {
107 CLOGE("Failed to write cast session property");
108 return CAST_ENGINE_ERROR;
109 }
110
111 int32_t ret = Remote()->SendRequest(CREATE_CAST_SESSION, data, reply, option);
112 if (ret == ERR_NO_PERMISSION) {
113 CLOGE("No permission when creating cast session");
114 return ERR_NO_PERMISSION;
115 } else if (ret == ERR_INVALID_DATA) {
116 CLOGE("Invalid parameter when creating cast session");
117 return ERR_INVALID_PARAM;
118 } else if (ret != ERR_NONE) {
119 CLOGE("Failed to send ipc request when creating cast session");
120 return CAST_ENGINE_ERROR;
121 }
122
123 int32_t errorCode = reply.ReadInt32();
124 CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
125 auto object = reply.ReadRemoteObject();
126 if (object == nullptr) {
127 CLOGE("Failed to get the cast session object");
128 return CAST_ENGINE_ERROR;
129 }
130 castSession = iface_cast<ICastSessionImpl>(object);
131
132 return errorCode;
133 }
134
SetLocalDevice(const CastLocalDevice & localDevice)135 int32_t CastSessionManagerServiceProxy::SetLocalDevice(const CastLocalDevice &localDevice)
136 {
137 MessageParcel data;
138 MessageParcel reply;
139 MessageOption option;
140
141 if (!data.WriteInterfaceToken(GetDescriptor())) {
142 CLOGE("Failed to write the interface token");
143 return CAST_ENGINE_ERROR;
144 }
145 if (!WriteCastLocalDevice(data, localDevice)) {
146 CLOGE("Failed to write cast local device");
147 return CAST_ENGINE_ERROR;
148 }
149
150 int32_t ret = Remote()->SendRequest(SET_LOCAL_DEVICE, data, reply, option);
151 if (ret == ERR_NO_PERMISSION) {
152 CLOGE("No permission when setting local device");
153 return ERR_NO_PERMISSION;
154 } else if (ret == ERR_INVALID_DATA) {
155 CLOGE("Invalid parameter when setting local device");
156 return ERR_INVALID_PARAM;
157 } else if (ret != ERR_NONE) {
158 CLOGE("Failed to send ipc request when setting local device");
159 return CAST_ENGINE_ERROR;
160 }
161
162 return reply.ReadInt32();
163 }
164
SetSinkSessionCapacity(int sessionCapacity)165 int32_t CastSessionManagerServiceProxy::SetSinkSessionCapacity(int sessionCapacity)
166 {
167 MessageParcel data;
168 MessageParcel reply;
169 MessageOption option;
170
171 if (!data.WriteInterfaceToken(GetDescriptor())) {
172 CLOGE("Failed to write the interface token");
173 return CAST_ENGINE_ERROR;
174 }
175 if (!data.WriteInt32(sessionCapacity)) {
176 CLOGE("Failed to write the session capacity");
177 return CAST_ENGINE_ERROR;
178 }
179
180 int32_t ret = Remote()->SendRequest(SET_SINK_SESSION_CAPACITY, data, reply, option);
181 if (ret == ERR_NO_PERMISSION) {
182 CLOGE("No permission when setting sink session capacity");
183 return ERR_NO_PERMISSION;
184 } else if (ret != ERR_NONE) {
185 CLOGE("Failed to send ipc request when setting sink session capacity");
186 return CAST_ENGINE_ERROR;
187 }
188
189 return reply.ReadInt32();
190 }
191
SetDiscoverable(bool enable)192 int32_t CastSessionManagerServiceProxy::SetDiscoverable(bool enable)
193 {
194 MessageParcel data;
195 MessageParcel reply;
196 MessageOption option;
197
198 if (!data.WriteInterfaceToken(GetDescriptor())) {
199 CLOGE("Failed to write the interface token");
200 return CAST_ENGINE_ERROR;
201 }
202 if (!data.WriteBool(enable)) {
203 CLOGE("Failed to write discoverable value");
204 return CAST_ENGINE_ERROR;
205 }
206
207 int32_t ret = Remote()->SendRequest(SET_DISCOVERABLE, data, reply, option);
208 if (ret == ERR_NO_PERMISSION) {
209 CLOGE("No permission when setting discoverable");
210 return ERR_NO_PERMISSION;
211 } else if (ret != ERR_NONE) {
212 CLOGE("Failed to send ipc request when setting discoverable");
213 return CAST_ENGINE_ERROR;
214 }
215
216 return reply.ReadInt32();
217 }
218
StartDiscovery(int protocols,std::vector<std::string> drmSchemes)219 int32_t CastSessionManagerServiceProxy::StartDiscovery(int protocols, std::vector<std::string> drmSchemes)
220 {
221 MessageParcel data;
222 MessageParcel reply;
223 MessageOption option;
224
225 if (!data.WriteInterfaceToken(GetDescriptor())) {
226 CLOGE("Failed to write the interface token");
227 return CAST_ENGINE_ERROR;
228 }
229 if (!data.WriteInt32(protocols)) {
230 CLOGE("Failed to write the protocol type");
231 return CAST_ENGINE_ERROR;
232 }
233 if (!data.WriteUint32(drmSchemes.size())) {
234 CLOGE("Failed to write the size of drm scheme");
235 return CAST_ENGINE_ERROR;
236 }
237 for (auto iter = drmSchemes.begin(); iter != drmSchemes.end(); iter++) {
238 if (!data.WriteString(*iter)) {
239 CLOGE("Failed to write the drm scheme");
240 return CAST_ENGINE_ERROR;
241 }
242 }
243
244 int32_t ret = Remote()->SendRequest(START_DISCOVERY, data, reply, option);
245 if (ret == ERR_NO_PERMISSION) {
246 CLOGE("No permission when starting discovery");
247 return ERR_NO_PERMISSION;
248 } else if (ret == ERR_INVALID_DATA) {
249 CLOGE("Invalid parameter when starting discovery");
250 return ERR_INVALID_PARAM;
251 } else if (ret != ERR_NONE) {
252 CLOGE("Failed to send ipc request when starting discovery");
253 return CAST_ENGINE_ERROR;
254 }
255
256 return reply.ReadInt32();
257 }
258
StopDiscovery()259 int32_t CastSessionManagerServiceProxy::StopDiscovery()
260 {
261 MessageParcel data;
262 MessageParcel reply;
263 MessageOption option;
264
265 if (!data.WriteInterfaceToken(GetDescriptor())) {
266 CLOGE("Failed to write the interface token");
267 return CAST_ENGINE_ERROR;
268 }
269
270 int32_t ret = Remote()->SendRequest(STOP_DISCOVERY, data, reply, option);
271 if (ret == ERR_NO_PERMISSION) {
272 CLOGE("No permission when stop discoverable");
273 return ERR_NO_PERMISSION;
274 } else if (ret != ERR_NONE) {
275 CLOGE("Failed to send ipc request when setting discoverable");
276 return CAST_ENGINE_ERROR;
277 }
278
279 return reply.ReadInt32();
280 }
281
StartDeviceLogging(int32_t fd,uint32_t maxSize)282 int32_t CastSessionManagerServiceProxy::StartDeviceLogging(int32_t fd, uint32_t maxSize)
283 {
284 MessageParcel data;
285 MessageParcel reply;
286 MessageOption option;
287
288 if (!data.WriteInterfaceToken(GetDescriptor())) {
289 CLOGE("Failed to write the interface token");
290 return CAST_ENGINE_ERROR;
291 }
292 (void)data.WriteFileDescriptor(fd);
293 if (!data.WriteUint32(maxSize)) {
294 CLOGE("Failed to write maxSize");
295 return CAST_ENGINE_ERROR;
296 }
297
298 int32_t ret = Remote()->SendRequest(START_DEVICE_LOGGING, data, reply, option);
299 if (ret == ERR_NO_PERMISSION) {
300 CLOGE("No permission when starting device logging");
301 return ERR_NO_PERMISSION;
302 } else if (ret != ERR_NONE) {
303 CLOGE("Failed to send ipc request when starting device logging");
304 return CAST_ENGINE_ERROR;
305 }
306
307 return reply.ReadInt32();
308 }
309
GetSessionManagerService()310 sptr<IRemoteObject> CastSessionManagerServiceProxy::GetSessionManagerService()
311 {
312 return this->AsObject();
313 }
314
GetCastSession(std::string sessionId,sptr<ICastSessionImpl> & castSession)315 int32_t CastSessionManagerServiceProxy::GetCastSession(std::string sessionId, sptr<ICastSessionImpl> &castSession)
316 {
317 MessageParcel data, reply;
318 MessageOption option;
319
320 if (!data.WriteInterfaceToken(GetDescriptor())) {
321 CLOGE("Failed to write the interface token");
322 return CAST_ENGINE_ERROR;
323 }
324 if (!data.WriteString(sessionId)) {
325 CLOGE("Failed to write session ID");
326 return CAST_ENGINE_ERROR;
327 }
328 if (Remote()->SendRequest(GET_CAST_SESSION, data, reply, option) != ERR_NONE) {
329 CLOGE("Failed to send ipc request when get cast session");
330 return CAST_ENGINE_ERROR;
331 }
332
333 int32_t errorCode = reply.ReadInt32();
334 CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
335 auto object = reply.ReadRemoteObject();
336 if (object == nullptr) {
337 CLOGE("Failed to get the cast session object");
338 return CAST_ENGINE_ERROR;
339 }
340 castSession = iface_cast<ICastSessionImpl>(object);
341
342 return errorCode;
343 }
344 } // namespace CastEngineClient
345 } // namespace CastEngine
346 } // namespace OHOS
347