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 stub class.
15 * Author: zhangge
16 * Create: 2022-5-29
17 */
18
19 #include "cast_session_manager_service_stub.h"
20
21 #include "cast_engine_common_helper.h"
22 #include "cast_engine_log.h"
23 #include "cast_service_listener_impl_proxy.h"
24 #include "permission.h"
25
26 namespace OHOS {
27 namespace CastEngine {
28 namespace CastEngineService {
29 DEFINE_CAST_ENGINE_LABEL("Cast-Service");
30 constexpr uint32_t MAX_CYCLES_NUM = 10;
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int CastSessionManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
33 MessageOption &option)
34 {
35 RETURN_IF_WRONG_TASK(code, data, reply, option);
36 return EXECUTE_SINGLE_STUB_TASK(code, data, reply);
37 }
38
CastSessionManagerServiceStub()39 CastSessionManagerServiceStub::CastSessionManagerServiceStub()
40 {
41 FILL_SINGLE_STUB_TASK(REGISTER_LISTENER, &CastSessionManagerServiceStub::DoRegisterListenerTask);
42 FILL_SINGLE_STUB_TASK(UNREGISTER_LISTENER, &CastSessionManagerServiceStub::DoUnregisterListenerTask);
43 FILL_SINGLE_STUB_TASK(RELEASE, &CastSessionManagerServiceStub::DoReleaseTask);
44 FILL_SINGLE_STUB_TASK(SET_LOCAL_DEVICE, &CastSessionManagerServiceStub::DoSetLocalDeviceTask);
45 FILL_SINGLE_STUB_TASK(CREATE_CAST_SESSION, &CastSessionManagerServiceStub::DoCreateCastSessionTask);
46 FILL_SINGLE_STUB_TASK(SET_SINK_SESSION_CAPACITY, &CastSessionManagerServiceStub::DoSetSinkSessionCapacityTask);
47 FILL_SINGLE_STUB_TASK(START_DISCOVERY, &CastSessionManagerServiceStub::DoStartDiscoveryTask);
48 FILL_SINGLE_STUB_TASK(SET_DISCOVERABLE, &CastSessionManagerServiceStub::DoSetDiscoverableTask);
49 FILL_SINGLE_STUB_TASK(STOP_DISCOVERY, &CastSessionManagerServiceStub::DoStopDiscoveryTask);
50 FILL_SINGLE_STUB_TASK(START_DEVICE_LOGGING, &CastSessionManagerServiceStub::DoStartDeviceLoggingTask);
51 FILL_SINGLE_STUB_TASK(GET_CAST_SESSION, &CastSessionManagerServiceStub::DoGetCastSessionTask);
52 }
53
~CastSessionManagerServiceStub()54 CastSessionManagerServiceStub::~CastSessionManagerServiceStub()
55 {
56 CLOGD("destructor in");
57 }
58
DoRegisterListenerTask(MessageParcel & data,MessageParcel & reply)59 int32_t CastSessionManagerServiceStub::DoRegisterListenerTask(MessageParcel &data, MessageParcel &reply)
60 {
61 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission() &&
62 !Permission::CheckPidPermission()) {
63 return ERR_NO_PERMISSION;
64 }
65 sptr<IRemoteObject> obj = data.ReadRemoteObject();
66 if (obj == nullptr) {
67 return ERR_NULL_OBJECT;
68 }
69
70 sptr<ICastServiceListenerImpl> listener{ new (std::nothrow) CastServiceListenerImplProxy(obj) };
71 if (listener == nullptr) {
72 CLOGE("RegisterListener failed, listener is null");
73 }
74
75 if (!reply.WriteInt32(RegisterListener(listener))) {
76 CLOGE("Failed to write int value");
77 return IPC_STUB_WRITE_PARCEL_ERR;
78 }
79
80 return ERR_NONE;
81 }
82
DoUnregisterListenerTask(MessageParcel & data,MessageParcel & reply)83 int32_t CastSessionManagerServiceStub::DoUnregisterListenerTask(MessageParcel &data, MessageParcel &reply)
84 {
85 if (!reply.WriteInt32(UnregisterListener())) {
86 CLOGE("Failed to write int value");
87 return IPC_STUB_WRITE_PARCEL_ERR;
88 }
89
90 return ERR_NONE;
91 }
92
DoReleaseTask(MessageParcel & data,MessageParcel & reply)93 int32_t CastSessionManagerServiceStub::DoReleaseTask(MessageParcel &data, MessageParcel &reply)
94 {
95 static_cast<void>(data);
96
97 if ((!Permission::CheckMirrorPermission() &&
98 !Permission::CheckStreamPermission()) ||
99 !Permission::CheckPidPermission()) {
100 return ERR_NO_PERMISSION;
101 }
102
103 if (!reply.WriteInt32(Release())) {
104 CLOGE("Failed to write int value");
105 return IPC_STUB_WRITE_PARCEL_ERR;
106 }
107
108 return ERR_NONE;
109 }
110
DoSetLocalDeviceTask(MessageParcel & data,MessageParcel & reply)111 int32_t CastSessionManagerServiceStub::DoSetLocalDeviceTask(MessageParcel &data, MessageParcel &reply)
112 {
113 if (!Permission::CheckMirrorPermission() || !Permission::CheckPidPermission()) {
114 return ERR_NO_PERMISSION;
115 }
116
117 auto device = ReadCastLocalDevice(data);
118 if (device == nullptr) {
119 CLOGE("Invalid device object comes");
120 return ERR_INVALID_DATA;
121 }
122
123 if (!reply.WriteInt32(SetLocalDevice(*device))) {
124 CLOGE("Failed to write int value");
125 return IPC_STUB_WRITE_PARCEL_ERR;
126 }
127 return ERR_NONE;
128 }
129
DoCreateCastSessionTask(MessageParcel & data,MessageParcel & reply)130 int32_t CastSessionManagerServiceStub::DoCreateCastSessionTask(MessageParcel &data, MessageParcel &reply)
131 {
132 if ((!Permission::CheckMirrorPermission() &&
133 !Permission::CheckStreamPermission()) ||
134 !Permission::CheckPidPermission()) {
135 return ERR_NO_PERMISSION;
136 }
137
138 auto property = ReadCastSessionProperty(data);
139 if (property == nullptr) {
140 CLOGE("Invalid property object comes");
141 return ERR_INVALID_DATA;
142 }
143
144 sptr<ICastSessionImpl> sessionStub;
145 int32_t ret = CreateCastSession(*property, sessionStub);
146 if (sessionStub == nullptr) {
147 return IPC_STUB_ERR;
148 }
149 if (!reply.WriteInt32(ret)) {
150 CLOGE("Failed to write ret:%d", ret);
151 return IPC_STUB_WRITE_PARCEL_ERR;
152 }
153 if (!reply.WriteRemoteObject(sessionStub->AsObject())) {
154 CLOGE("Failed to write cast session");
155 return IPC_STUB_WRITE_PARCEL_ERR;
156 }
157
158 return ERR_NONE;
159 }
160
DoSetSinkSessionCapacityTask(MessageParcel & data,MessageParcel & reply)161 int32_t CastSessionManagerServiceStub::DoSetSinkSessionCapacityTask(MessageParcel &data, MessageParcel &reply)
162 {
163 static_cast<void>(reply);
164
165 if (!Permission::CheckMirrorPermission() || !Permission::CheckPidPermission()) {
166 return ERR_NO_PERMISSION;
167 }
168
169 int32_t capacity = data.ReadInt32();
170 int32_t ret = SetSinkSessionCapacity(capacity);
171 if (!reply.WriteInt32(ret)) {
172 CLOGE("Failed to write ret:%d", ret);
173 return IPC_STUB_WRITE_PARCEL_ERR;
174 }
175
176 return ERR_NONE;
177 }
178
DoStartDiscoveryTask(MessageParcel & data,MessageParcel & reply)179 int32_t CastSessionManagerServiceStub::DoStartDiscoveryTask(MessageParcel &data, MessageParcel &reply)
180 {
181 if ((!Permission::CheckMirrorPermission() &&
182 !Permission::CheckStreamPermission()) ||
183 !Permission::CheckPidPermission()) {
184 return ERR_NO_PERMISSION;
185 }
186
187 int32_t type = data.ReadInt32();
188 if (!IsProtocolType(type)) {
189 CLOGE("Invalid protocol type comes, %{public}d", type);
190 return ERR_INVALID_DATA;
191 }
192 std::vector<std::string> drmSchemes;
193 uint32_t drmSchemeSize = data.ReadUint32();
194 for (uint32_t i = 0; (i < drmSchemeSize) && (i < MAX_CYCLES_NUM); i++) {
195 drmSchemes.push_back(data.ReadString());
196 }
197 if (!reply.WriteInt32(StartDiscovery(type, drmSchemes))) {
198 CLOGE("Failed to write int value");
199 return IPC_STUB_WRITE_PARCEL_ERR;
200 }
201 return ERR_NONE;
202 }
203
DoSetDiscoverableTask(MessageParcel & data,MessageParcel & reply)204 int32_t CastSessionManagerServiceStub::DoSetDiscoverableTask(MessageParcel &data, MessageParcel &reply)
205 {
206 if ((!Permission::CheckMirrorPermission() &&
207 !Permission::CheckStreamPermission()) ||
208 !Permission::CheckPidPermission()) {
209 return ERR_NO_PERMISSION;
210 }
211
212 bool enable = data.ReadBool();
213 if (!reply.WriteInt32(SetDiscoverable(enable))) {
214 CLOGE("Failed to write int value");
215 return IPC_STUB_WRITE_PARCEL_ERR;
216 }
217 return ERR_NONE;
218 }
219
DoStopDiscoveryTask(MessageParcel & data,MessageParcel & reply)220 int32_t CastSessionManagerServiceStub::DoStopDiscoveryTask(MessageParcel &data, MessageParcel &reply)
221 {
222 if ((!Permission::CheckMirrorPermission() &&
223 !Permission::CheckStreamPermission()) ||
224 !Permission::CheckPidPermission()) {
225 return ERR_NO_PERMISSION;
226 }
227
228 if (!reply.WriteInt32(StopDiscovery())) {
229 CLOGE("Failed to write int value");
230 return IPC_STUB_WRITE_PARCEL_ERR;
231 }
232 return ERR_NONE;
233 }
234
DoStartDeviceLoggingTask(MessageParcel & data,MessageParcel & reply)235 int32_t CastSessionManagerServiceStub::DoStartDeviceLoggingTask(MessageParcel &data, MessageParcel &reply)
236 {
237 if ((!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) ||
238 !Permission::CheckPidPermission()) {
239 return ERR_NO_PERMISSION;
240 }
241 int32_t fd = data.ReadFileDescriptor();
242 uint32_t maxSize = data.ReadUint32();
243 if (!reply.WriteInt32(StartDeviceLogging(fd, maxSize))) {
244 CLOGE("Failed to write int value");
245 return IPC_STUB_WRITE_PARCEL_ERR;
246 }
247 return ERR_NONE;
248 }
249
DoGetCastSessionTask(MessageParcel & data,MessageParcel & reply)250 int32_t CastSessionManagerServiceStub::DoGetCastSessionTask(MessageParcel &data, MessageParcel &reply)
251 {
252 sptr<ICastSessionImpl> sessionStub;
253 if (!Permission::CheckPidPermission()) {
254 return ERR_NO_PERMISSION;
255 }
256 int32_t ret = GetCastSession(data.ReadString(), sessionStub);
257 if (sessionStub == nullptr) {
258 return IPC_STUB_ERR;
259 }
260 if (!reply.WriteInt32(ret)) {
261 CLOGE("Failed to write ret:%d", ret);
262 return IPC_STUB_WRITE_PARCEL_ERR;
263 }
264 if (!reply.WriteRemoteObject(sessionStub->AsObject())) {
265 CLOGE("Failed to write cast session");
266 return IPC_STUB_WRITE_PARCEL_ERR;
267 }
268 return ERR_NONE;
269 }
270 } // namespace CastEngineService
271 } // namespace CastEngine
272 } // namespace OHOS
273