• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UNKNOWN_TRANSACTION) {
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_UNKNOWN_TRANSACTION) {
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 
SetLocalDevice(const CastLocalDevice & localDevice)95 int32_t CastSessionManagerServiceProxy::SetLocalDevice(const CastLocalDevice &localDevice)
96 {
97     MessageParcel data;
98     MessageParcel reply;
99     MessageOption option;
100 
101     if (!data.WriteInterfaceToken(GetDescriptor())) {
102         CLOGE("Failed to write the interface token");
103         return CAST_ENGINE_ERROR;
104     }
105     if (!WriteCastLocalDevice(data, localDevice)) {
106         CLOGE("Failed to write cast local device");
107         return CAST_ENGINE_ERROR;
108     }
109 
110     int32_t ret = Remote()->SendRequest(SET_LOCAL_DEVICE, data, reply, option);
111     if (ret == ERR_UNKNOWN_TRANSACTION) {
112         CLOGE("No permission when setting local device");
113         return ERR_NO_PERMISSION;
114     } else if (ret == ERR_INVALID_DATA) {
115         CLOGE("Invalid parameter when setting local device");
116         return ERR_INVALID_PARAM;
117     } else if (ret != ERR_NONE) {
118         CLOGE("Failed to send ipc request when setting local device");
119         return CAST_ENGINE_ERROR;
120     }
121 
122     return reply.ReadInt32();
123 }
124 
CreateCastSession(const CastSessionProperty & property,sptr<ICastSessionImpl> & castSession)125 int32_t CastSessionManagerServiceProxy::CreateCastSession(const CastSessionProperty &property,
126     sptr<ICastSessionImpl> &castSession)
127 {
128     MessageParcel data;
129     MessageParcel reply;
130     MessageOption option;
131 
132     if (!data.WriteInterfaceToken(GetDescriptor())) {
133         CLOGE("Failed to write the interface token");
134         return CAST_ENGINE_ERROR;
135     }
136     if (!WriteCastSessionProperty(data, property)) {
137         CLOGE("Failed to write cast session property");
138         return CAST_ENGINE_ERROR;
139     }
140 
141     int32_t ret = Remote()->SendRequest(CREATE_CAST_SESSION, data, reply, option);
142     if (ret == ERR_UNKNOWN_TRANSACTION) {
143         CLOGE("No permission when creating cast session");
144         return ERR_NO_PERMISSION;
145     } else if (ret == ERR_INVALID_DATA) {
146         CLOGE("Invalid parameter when creating cast session");
147         return ERR_INVALID_PARAM;
148     } else if (ret != ERR_NONE) {
149         CLOGE("Failed to send ipc request when creating cast session");
150         return CAST_ENGINE_ERROR;
151     }
152 
153     int32_t errorCode = reply.ReadInt32();
154     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
155     auto object = reply.ReadRemoteObject();
156     if (object == nullptr) {
157         CLOGE("Failed to get the cast session object");
158         return CAST_ENGINE_ERROR;
159     }
160     castSession = iface_cast<ICastSessionImpl>(object);
161 
162     return errorCode;
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_UNKNOWN_TRANSACTION) {
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 
StartDiscovery(int protocols)192 int32_t CastSessionManagerServiceProxy::StartDiscovery(int protocols)
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.WriteInt32(protocols)) {
203         CLOGE("Failed to write the protocol type");
204         return CAST_ENGINE_ERROR;
205     }
206 
207     int32_t ret = Remote()->SendRequest(START_DISCOVERY, data, reply, option);
208     if (ret == ERR_UNKNOWN_TRANSACTION) {
209         CLOGE("No permission when starting discovery");
210         return ERR_NO_PERMISSION;
211     } else if (ret == ERR_INVALID_DATA) {
212         CLOGE("Invalid parameter when starting discovery");
213         return ERR_INVALID_PARAM;
214     } else if (ret != ERR_NONE) {
215         CLOGE("Failed to send ipc request when starting discovery");
216         return CAST_ENGINE_ERROR;
217     }
218 
219     return reply.ReadInt32();
220 }
221 
SetDiscoverable(bool enable)222 int32_t CastSessionManagerServiceProxy::SetDiscoverable(bool enable)
223 {
224     MessageParcel data;
225     MessageParcel reply;
226     MessageOption option;
227 
228     if (!data.WriteInterfaceToken(GetDescriptor())) {
229         CLOGE("Failed to write the interface token");
230         return CAST_ENGINE_ERROR;
231     }
232     if (!data.WriteBool(enable)) {
233         CLOGE("Failed to write discoverable value");
234         return CAST_ENGINE_ERROR;
235     }
236 
237     int32_t ret = Remote()->SendRequest(SET_DISCOVERABLE, data, reply, option);
238     if (ret == ERR_UNKNOWN_TRANSACTION) {
239         CLOGE("No permission when setting discoverable");
240         return ERR_NO_PERMISSION;
241     } else if (ret != ERR_NONE) {
242         CLOGE("Failed to send ipc request when setting discoverable");
243         return CAST_ENGINE_ERROR;
244     }
245 
246     return reply.ReadInt32();
247 }
248 
StopDiscovery()249 int32_t CastSessionManagerServiceProxy::StopDiscovery()
250 {
251     MessageParcel data;
252     MessageParcel reply;
253     MessageOption option;
254 
255     if (!data.WriteInterfaceToken(GetDescriptor())) {
256         CLOGE("Failed to write the interface token");
257         return CAST_ENGINE_ERROR;
258     }
259 
260     int32_t ret = Remote()->SendRequest(STOP_DISCOVERY, data, reply, option);
261     if (ret == ERR_UNKNOWN_TRANSACTION) {
262         CLOGE("No permission when setting discoverable");
263         return ERR_NO_PERMISSION;
264     } else if (ret != ERR_NONE) {
265         CLOGE("Failed to send ipc request when setting discoverable");
266         return CAST_ENGINE_ERROR;
267     }
268 
269     return reply.ReadInt32();
270 }
271 
GetCastSession(std::string sessionId,sptr<ICastSessionImpl> & castSession)272 int32_t CastSessionManagerServiceProxy::GetCastSession(std::string sessionId, sptr<ICastSessionImpl> &castSession)
273 {
274     MessageParcel data, reply;
275     MessageOption option;
276 
277     if (!data.WriteInterfaceToken(GetDescriptor())) {
278         CLOGE("Failed to write the interface token");
279         return CAST_ENGINE_ERROR;
280     }
281     if (!data.WriteString(sessionId)) {
282         CLOGE("Failed to write session ID");
283         return CAST_ENGINE_ERROR;
284     }
285     if (Remote()->SendRequest(GET_CAST_SESSION, data, reply, option) != ERR_NONE) {
286         CLOGE("Failed to send ipc request when get cast session");
287         return CAST_ENGINE_ERROR;
288     }
289 
290     int32_t errorCode = reply.ReadInt32();
291     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
292     auto object = reply.ReadRemoteObject();
293     if (object == nullptr) {
294         CLOGE("Failed to get the cast session object");
295         return CAST_ENGINE_ERROR;
296     }
297     castSession = iface_cast<ICastSessionImpl>(object);
298 
299     return errorCode;
300 }
301 
GetSessionManagerService()302 sptr<IRemoteObject> CastSessionManagerServiceProxy::GetSessionManagerService()
303 {
304     return this->AsObject();
305 }
306 } // namespace CastEngineClient
307 } // namespace CastEngine
308 } // namespace OHOS
309