• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 implement stub class.
15  * Author: zhangge
16  * Create: 2022-06-15
17  */
18 
19 #include "cast_session_impl_stub.h"
20 
21 #include "cast_engine_common_helper.h"
22 #include "cast_session_listener_impl_proxy.h"
23 #include "permission.h"
24 
25 namespace OHOS {
26 namespace CastEngine {
27 namespace CastEngineService {
28 DEFINE_CAST_ENGINE_LABEL("Cast-SessionImpl");
29 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int CastSessionImplStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
31     MessageOption &option)
32 {
33     RETRUEN_IF_WRONG_TASK(code, data, reply, option);
34     return EXECUTE_SINGLE_STUB_TASK(code, data, reply);
35 }
36 
CastSessionImplStub()37 CastSessionImplStub::CastSessionImplStub()
38 {
39     FILL_SINGLE_STUB_TASK(REGISTER_LISTENER, &CastSessionImplStub::DoRegisterListenerTask);
40     FILL_SINGLE_STUB_TASK(UNREGISTER_LISTENER, &CastSessionImplStub::DoUnregisterListenerTask);
41     FILL_SINGLE_STUB_TASK(ADD_DEVICE, &CastSessionImplStub::DoAddDeviceTask);
42     FILL_SINGLE_STUB_TASK(REMOVE_DEVICE, &CastSessionImplStub::DoRemoveDeviceTask);
43     FILL_SINGLE_STUB_TASK(START_AUTH, &CastSessionImplStub::DoStartAuthTask);
44     FILL_SINGLE_STUB_TASK(GET_SESSION_ID, &CastSessionImplStub::DoGetSessionIdTask);
45     FILL_SINGLE_STUB_TASK(GET_DEVICE_STATE, &CastSessionImplStub::DoGetDeviceStateTask);
46     FILL_SINGLE_STUB_TASK(SET_SESSION_PROPERTY, &CastSessionImplStub::DoSetSessionProperty);
47     FILL_SINGLE_STUB_TASK(CREAT_MIRROR_PLAYER, &CastSessionImplStub::DoCreateMirrorPlayer);
48     FILL_SINGLE_STUB_TASK(CREAT_STREAM_PLAYER, &CastSessionImplStub::DoCreateStreamPlayer);
49     FILL_SINGLE_STUB_TASK(NOTIFY_EVENT, &CastSessionImplStub::DoNotifyEvent);
50     FILL_SINGLE_STUB_TASK(SET_CAST_MODE, &CastSessionImplStub::DoSetCastMode);
51     FILL_SINGLE_STUB_TASK(RELEASE, &CastSessionImplStub::DoRelease);
52 }
53 
DoRegisterListenerTask(MessageParcel & data,MessageParcel & reply)54 int32_t CastSessionImplStub::DoRegisterListenerTask(MessageParcel &data, MessageParcel &reply)
55 {
56     sptr<IRemoteObject> obj = data.ReadRemoteObject();
57     if (obj == nullptr) {
58         return ERR_NULL_OBJECT;
59     }
60 
61     sptr<ICastSessionListenerImpl> listener{ new CastSessionListenerImplProxy(obj) };
62     if (!reply.WriteInt32(RegisterListener(listener))) {
63         CLOGE("Failed to write int value");
64         return IPC_STUB_WRITE_PARCEL_ERR;
65     }
66 
67     return ERR_NONE;
68 }
69 
DoUnregisterListenerTask(MessageParcel & data,MessageParcel & reply)70 int32_t CastSessionImplStub::DoUnregisterListenerTask(MessageParcel &data, MessageParcel &reply)
71 {
72     static_cast<void>(data);
73     if (!reply.WriteInt32(UnregisterListener())) {
74         CLOGE("Failed to write int value");
75         return IPC_STUB_WRITE_PARCEL_ERR;
76     }
77     return ERR_NONE;
78 }
79 
DoAddDeviceTask(MessageParcel & data,MessageParcel & reply)80 int32_t CastSessionImplStub::DoAddDeviceTask(MessageParcel &data, MessageParcel &reply)
81 {
82     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
83         return ERR_UNKNOWN_TRANSACTION;
84     }
85     auto device = ReadCastRemoteDevice(data);
86     if (device == nullptr) {
87         CLOGE("Invalid remote device object comes");
88         return ERR_INVALID_DATA;
89     }
90 
91     if (!reply.WriteInt32(AddDevice(*device))) {
92         CLOGE("Failed to write int value");
93         return IPC_STUB_WRITE_PARCEL_ERR;
94     }
95 
96     return ERR_NONE;
97 }
98 
DoRemoveDeviceTask(MessageParcel & data,MessageParcel & reply)99 int32_t CastSessionImplStub::DoRemoveDeviceTask(MessageParcel &data, MessageParcel &reply)
100 {
101     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
102         return ERR_UNKNOWN_TRANSACTION;
103     }
104 
105     std::string deviceId = data.ReadString();
106     if (deviceId.empty()) {
107         CLOGE("The device id is empty");
108         return ERR_INVALID_DATA;
109     }
110 
111     if (!reply.WriteInt32(RemoveDevice(deviceId))) {
112         CLOGE("Failed to write int value");
113         return IPC_STUB_WRITE_PARCEL_ERR;
114     }
115 
116     return ERR_NONE;
117 }
118 
DoStartAuthTask(MessageParcel & data,MessageParcel & reply)119 int32_t CastSessionImplStub::DoStartAuthTask(MessageParcel &data, MessageParcel &reply)
120 {
121     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
122         return ERR_UNKNOWN_TRANSACTION;
123     }
124 
125     auto authInfo = ReadAuthInfo(data);
126     if (authInfo == nullptr) {
127         CLOGE("Invalid auth info comes");
128         return ERR_INVALID_DATA;
129     }
130     if (!reply.WriteInt32(StartAuth(*authInfo))) {
131         CLOGE("Failed to write int value");
132         return IPC_STUB_WRITE_PARCEL_ERR;
133     }
134     return ERR_NONE;
135 }
136 
DoGetSessionIdTask(MessageParcel & data,MessageParcel & reply)137 int32_t CastSessionImplStub::DoGetSessionIdTask(MessageParcel &data, MessageParcel &reply)
138 {
139     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
140         return ERR_UNKNOWN_TRANSACTION;
141     }
142     static_cast<void>(data);
143     std::string sessionId{};
144     int32_t ret = GetSessionId(sessionId);
145     if (!reply.WriteInt32(ret)) {
146         CLOGE("Failed to write ret:%d", ret);
147         return IPC_STUB_WRITE_PARCEL_ERR;
148     }
149     if (!reply.WriteString(sessionId)) {
150         CLOGE("Failed to write session id:%s", sessionId.c_str());
151         return IPC_STUB_WRITE_PARCEL_ERR;
152     }
153 
154     return ERR_NONE;
155 }
156 
DoGetDeviceStateTask(MessageParcel & data,MessageParcel & reply)157 int32_t CastSessionImplStub::DoGetDeviceStateTask(MessageParcel &data, MessageParcel &reply)
158 {
159     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
160         return ERR_UNKNOWN_TRANSACTION;
161     }
162 
163     std::string deviceId = data.ReadString();
164     if (deviceId.empty()) {
165         CLOGE("The device id is empty");
166         return ERR_INVALID_DATA;
167     }
168 
169     DeviceState state = DeviceState::DISCONNECTED;
170     int32_t ret = GetDeviceState(deviceId, state);
171     if (!reply.WriteInt32(ret)) {
172         CLOGE("Failed to write ret:%d", ret);
173         return IPC_STUB_WRITE_PARCEL_ERR;
174     }
175     if (!reply.WriteInt32(static_cast<int32_t>(state))) {
176         CLOGE("Failed to write device state:%d", static_cast<int32_t>(state));
177         return IPC_STUB_WRITE_PARCEL_ERR;
178     }
179 
180     return ERR_NONE;
181 }
182 
DoSetSessionProperty(MessageParcel & data,MessageParcel & reply)183 int32_t CastSessionImplStub::DoSetSessionProperty(MessageParcel &data, MessageParcel &reply)
184 {
185     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
186         return ERR_UNKNOWN_TRANSACTION;
187     }
188 
189     auto property = ReadCastSessionProperty(data);
190     if (property == nullptr) {
191         CLOGE("Invalid property object");
192         return ERR_NULL_OBJECT;
193     }
194 
195     if (!reply.WriteInt32(SetSessionProperty(*property))) {
196         CLOGE("Failed to write int value");
197         return IPC_STUB_WRITE_PARCEL_ERR;
198     }
199 
200     return ERR_NONE;
201 }
202 
DoCreateMirrorPlayer(MessageParcel & data,MessageParcel & reply)203 int32_t CastSessionImplStub::DoCreateMirrorPlayer(MessageParcel &data, MessageParcel &reply)
204 {
205     if (!Permission::CheckMirrorPermission()) {
206         return ERR_UNKNOWN_TRANSACTION;
207     }
208     sptr<IMirrorPlayerImpl> mirrorPlayerStub;
209     int32_t ret = CreateMirrorPlayer(mirrorPlayerStub);
210     if (mirrorPlayerStub == nullptr) {
211         return IPC_STUB_ERR;
212     }
213     if (!reply.WriteInt32(ret)) {
214         CLOGE("Failed to write ret:%d", ret);
215         return IPC_STUB_WRITE_PARCEL_ERR;
216     }
217     if (!reply.WriteRemoteObject(mirrorPlayerStub->AsObject())) {
218         CLOGE("Failed to write mirror player");
219         return IPC_STUB_WRITE_PARCEL_ERR;
220     }
221     CLOGI("CreateMirrorPlayer");
222     return ERR_NONE;
223 }
224 
DoCreateStreamPlayer(MessageParcel & data,MessageParcel & reply)225 int32_t CastSessionImplStub::DoCreateStreamPlayer(MessageParcel &data, MessageParcel &reply)
226 {
227     if (!Permission::CheckStreamPermission()) {
228         return ERR_UNKNOWN_TRANSACTION;
229     }
230 
231     sptr<IStreamPlayerIpc> streamPlayerStub;
232     int32_t ret = CreateStreamPlayer(streamPlayerStub);
233     if (!streamPlayerStub) {
234         return IPC_STUB_ERR;
235     }
236     if (!reply.WriteInt32(ret)) {
237         CLOGE("Failed to write ret:%d", ret);
238         return IPC_STUB_WRITE_PARCEL_ERR;
239     }
240     if (!reply.WriteRemoteObject(streamPlayerStub->AsObject())) {
241         CLOGE("Failed to write stream player");
242         return IPC_STUB_WRITE_PARCEL_ERR;
243     }
244     return ERR_NONE;
245 }
246 
DoRelease(MessageParcel & data,MessageParcel & reply)247 int32_t CastSessionImplStub::DoRelease(MessageParcel &data, MessageParcel &reply)
248 {
249     if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
250         return ERR_UNKNOWN_TRANSACTION;
251     }
252 
253     static_cast<void>(data);
254 
255     if (!reply.WriteInt32(Release())) {
256         CLOGE("Failed to write int value");
257         return IPC_STUB_WRITE_PARCEL_ERR;
258     }
259     return ERR_NONE;
260 }
261 
DoSetCastMode(MessageParcel & data,MessageParcel & reply)262 int32_t CastSessionImplStub::DoSetCastMode(MessageParcel &data, MessageParcel &reply)
263 {
264     if (!Permission::CheckMirrorPermission()) {
265         return ERR_UNKNOWN_TRANSACTION;
266     }
267     int32_t mode = data.ReadInt32();
268     std::string jsonParam = data.ReadString();
269     if (!reply.WriteInt32(SetCastMode(static_cast<CastMode>(mode), jsonParam))) {
270         CLOGE("Failed to write int value");
271         return IPC_STUB_WRITE_PARCEL_ERR;
272     }
273     return ERR_NONE;
274 }
275 
DoNotifyEvent(MessageParcel & data,MessageParcel & reply)276 int32_t CastSessionImplStub::DoNotifyEvent(MessageParcel &data, MessageParcel &reply)
277 {
278     EventId eventId = static_cast<EventId>(data.ReadInt32());
279     std::string param = data.ReadString();
280     NotifyEvent(eventId, param);
281     return ERR_NONE;
282 }
283 } // namespace CastEngineService
284 } // namespace CastEngine
285 } // namespace OHOS
286