• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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  */
15 
16 #include <cstdio>
17 
18 #include "av_router.h"
19 #include "avsession_log.h"
20 #include "avsession_errors.h"
21 #include "session_listener_proxy.h"
22 #include "client_death_proxy.h"
23 #include "avsession_trace.h"
24 #include "avsession_sysevent.h"
25 #include "parameter.h"
26 #include "parameters.h"
27 #include "avsession_service_stub.h"
28 #include "session_xcollie.h"
29 #include "permission_checker.h"
30 #include "iservice_registry.h"
31 #include "system_ability_definition.h"
32 
33 using namespace OHOS::AudioStandard;
34 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)35 bool AVSessionServiceStub::CheckInterfaceToken(MessageParcel& data)
36 {
37     auto localDescriptor = IAVSessionService::GetDescriptor();
38     auto remoteDescriptor = data.ReadInterfaceToken();
39     if (remoteDescriptor != localDescriptor) {
40         SLOGI("interface token is not equal");
41         return false;
42     }
43     return true;
44 }
45 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)46 int32_t AVSessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
47                                               MessageOption& option)
48 {
49     if (code >= static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION) &&
50         code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX) &&
51         code != static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST)) {
52         SessionXCollie sessionXCollie(mapCodeToFuncNameXCollie[code]);
53     }
54     if (!CheckInterfaceToken(data)) {
55         return AVSESSION_ERROR;
56     }
57     if (code >= static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION) &&
58         code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX)) {
59         return handlers[code](data, reply);
60     }
61     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 
HandleCreateSessionInner(MessageParcel & data,MessageParcel & reply)64 int32_t AVSessionServiceStub::HandleCreateSessionInner(MessageParcel& data, MessageParcel& reply)
65 {
66     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateSessionInner");
67     auto sessionTag = data.ReadString();
68     auto sessionType = data.ReadInt32();
69     sptr elementName = data.ReadParcelable<AppExecFwk::ElementName>();
70     if (elementName == nullptr) {
71         SLOGI("read element name failed");
72         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
73         return ERR_NONE;
74     }
75     sptr<IRemoteObject> object;
76     auto ret = CreateSessionInner(sessionTag, sessionType, *elementName, object);
77     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
78     if (ret == AVSESSION_SUCCESS) {
79         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
80     }
81     return ERR_NONE;
82 }
83 
HandleGetAllSessionDescriptors(MessageParcel & data,MessageParcel & reply)84 int32_t AVSessionServiceStub::HandleGetAllSessionDescriptors(MessageParcel& data, MessageParcel& reply)
85 {
86     int32_t err = PermissionChecker::GetInstance().CheckPermission(
87         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
88     if (err != ERR_NONE) {
89         SLOGE("GetAllSessionDescriptors: CheckPermission failed");
90         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
91             "ERROR_MSG", "avsessionservice getallsessiondescriptors checkpermission failed");
92         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
93         return ERR_NONE;
94     }
95     std::vector<AVSessionDescriptor> descriptors;
96     int32_t ret = GetAllSessionDescriptors(descriptors);
97     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
98     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
99     for (const auto& descriptor : descriptors) {
100         if (!descriptor.WriteToParcel(reply)) {
101             SLOGI("write descriptor failed");
102             break;
103         }
104     }
105     return ERR_NONE;
106 }
107 
HandleGetSessionDescriptorsById(MessageParcel & data,MessageParcel & reply)108 int32_t AVSessionServiceStub::HandleGetSessionDescriptorsById(MessageParcel& data, MessageParcel& reply)
109 {
110     AVSessionDescriptor descriptor;
111     int32_t ret = GetSessionDescriptorsBySessionId(data.ReadString(), descriptor);
112     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
113     if (ret == AVSESSION_SUCCESS) {
114         CHECK_AND_PRINT_LOG(descriptor.WriteToParcel(reply), "write AVSessionDescriptor failed");
115     }
116     return ERR_NONE;
117 }
118 
HandleGetHistoricalSessionDescriptors(MessageParcel & data,MessageParcel & reply)119 int32_t AVSessionServiceStub::HandleGetHistoricalSessionDescriptors(MessageParcel& data, MessageParcel& reply)
120 {
121     int32_t err = PermissionChecker::GetInstance().CheckPermission(
122         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
123         if (err != ERR_NONE) {
124         SLOGE("GetHistoricalSessionDescriptors: CheckPermission failed");
125         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
126             "ERROR_MSG", "avsessionservice getHistoricalSessionDescriptors checkpermission failed");
127         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
128         return ERR_NONE;
129     }
130     std::vector<AVSessionDescriptor> descriptors;
131     int32_t ret = GetHistoricalSessionDescriptors(data.ReadInt32(), descriptors);
132     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
133     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
134     for (const auto& descriptor : descriptors) {
135         if (!descriptor.WriteToParcel(reply)) {
136             SLOGI("write descriptor failed");
137             break;
138         }
139     }
140     return ERR_NONE;
141 }
142 
GetAVQueueInfosImgLength(std::vector<AVQueueInfo> & avQueueInfos)143 int32_t AVSessionServiceStub::GetAVQueueInfosImgLength(std::vector<AVQueueInfo>& avQueueInfos)
144 {
145     int sumLength = 0;
146     for (auto& avQueueInfo : avQueueInfos) {
147         int avQueueImgLen = 0;
148         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
149         if (pixelMap != nullptr) {
150             avQueueImgLen = static_cast<int>((pixelMap->GetInnerImgBuffer()).size());
151         }
152         avQueueInfo.SetAVQueueLength(avQueueImgLen);
153         sumLength += avQueueImgLen;
154     }
155     return sumLength;
156 }
157 
158 
MarshallingAVQueueInfos(MessageParcel & reply,const std::vector<AVQueueInfo> & avQueueInfos)159 void AVSessionServiceStub::MarshallingAVQueueInfos(MessageParcel &reply, const std::vector<AVQueueInfo>& avQueueInfos)
160 {
161     CHECK_AND_RETURN_LOG(reply.WriteUint32(avQueueInfos.size()), "MarshallingAVQueueInfos size failed");
162     for (const auto& avQueueInfo : avQueueInfos) {
163         reply.WriteString(avQueueInfo.GetBundleName());
164         reply.WriteString(avQueueInfo.GetAVQueueName());
165         reply.WriteString(avQueueInfo.GetAVQueueId());
166         reply.WriteString(avQueueInfo.GetAVQueueImageUri());
167         reply.WriteUint32(avQueueInfo.GetAVQueueLength());
168     }
169 }
170 
AVQueueInfoImgToBuffer(std::vector<AVQueueInfo> & avQueueInfos,unsigned char * buffer)171 void AVSessionServiceStub::AVQueueInfoImgToBuffer(std::vector<AVQueueInfo>& avQueueInfos, unsigned char *buffer)
172 {
173     int k = 0;
174     for (auto& avQueueInfo : avQueueInfos) {
175         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
176         if (pixelMap != nullptr) {
177             std::vector<uint8_t> imgBuffer = pixelMap->GetInnerImgBuffer();
178             int length = avQueueInfo.GetAVQueueLength();
179             for (int i = 0; i< length; i++, k++) {
180                 buffer[k] = imgBuffer[i];
181             }
182         }
183     }
184 }
185 
HandleGetHistoricalAVQueueInfos(MessageParcel & data,MessageParcel & reply)186 int32_t AVSessionServiceStub::HandleGetHistoricalAVQueueInfos(MessageParcel& data, MessageParcel& reply)
187 {
188     int32_t err = PermissionChecker::GetInstance().CheckPermission(
189         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
190     if (err != ERR_NONE) {
191         SLOGE("GetHistoricalAVQueueInfos: CheckPermission failed");
192         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
193             "ERROR_MSG", "avsessionservice GetHistoricalAVQueueInfos checkpermission failed");
194         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
195         return ERR_NONE;
196     }
197     std::vector<AVQueueInfo> avQueueInfos;
198     auto maxSize = data.ReadInt32();
199     auto maxAppSize = data.ReadInt32();
200     int32_t ret = GetHistoricalAVQueueInfos(maxSize, maxAppSize, avQueueInfos);
201     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
202 
203     int bufferLength = GetAVQueueInfosImgLength(avQueueInfos);
204     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(bufferLength), ERR_NONE, "write buffer length failed");
205     if (bufferLength == 0) {
206         CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(avQueueInfos.size()), ERR_NONE, "write size failed");
207         for (const auto& avQueueInfo : avQueueInfos) {
208             if (!avQueueInfo.Marshalling(reply)) {
209                 SLOGE("write avQueueInfo failed");
210                 break;
211             }
212         }
213         return ERR_NONE;
214     }
215 
216     unsigned char *buffer = new (std::nothrow) unsigned char[bufferLength];
217     if (buffer == nullptr) {
218         SLOGE("new buffer failed of length = %{public}d", bufferLength);
219         return AVSESSION_ERROR;
220     }
221 
222     MarshallingAVQueueInfos(reply, avQueueInfos);
223     AVQueueInfoImgToBuffer(avQueueInfos, buffer);
224     if (!reply.WriteRawData(buffer, bufferLength)) {
225         SLOGE("fail to write parcel");
226         delete[] buffer;
227         return AVSESSION_ERROR;
228     }
229 
230     delete[] buffer;
231     return ERR_NONE;
232 }
233 
HandleStartAVPlayback(MessageParcel & data,MessageParcel & reply)234 int32_t AVSessionServiceStub::HandleStartAVPlayback(MessageParcel& data, MessageParcel& reply)
235 {
236     int32_t err = PermissionChecker::GetInstance().CheckPermission(
237         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
238     if (err != ERR_NONE) {
239         SLOGE("StartAVPlayback: CheckPermission failed");
240         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
241             "ERROR_MSG", "avsessionservice StartAVPlayback checkpermission failed");
242         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
243         return ERR_NONE;
244     }
245     std::string bundleName = data.ReadString();
246     std::string asserId = data.ReadString();
247     int32_t ret = StartAVPlayback(bundleName, asserId);
248     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
249     return ERR_NONE;
250 }
251 
HandleCreateControllerInner(MessageParcel & data,MessageParcel & reply)252 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
253 {
254     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
255     int32_t err = PermissionChecker::GetInstance().CheckPermission(
256         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
257     if (err != ERR_NONE) {
258         SLOGE("CreateControllerInner: CheckPermission failed");
259         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
260             "CALLER_PID", GetCallingPid(), "SESSION_ID", data.ReadString(),
261             "ERROR_MSG", "avsessionservice createcontrollerinner checkpermission failed");
262         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
263         return ERR_NONE;
264     }
265     sptr<IRemoteObject> object;
266     int32_t ret = CreateControllerInner(data.ReadString(), object);
267     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
268     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
269         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
270     }
271     return ERR_NONE;
272 }
273 
HandleGetAVCastControllerInner(MessageParcel & data,MessageParcel & reply)274 int32_t AVSessionServiceStub::HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)
275 {
276 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
277     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetAVCastControllerInner");
278     int32_t err = PermissionChecker::GetInstance().CheckPermission(
279         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
280     if (err != ERR_NONE) {
281         SLOGE("GetAVCastControllerInner: CheckPermission failed");
282         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
283         return ERR_NONE;
284     }
285     sptr<IRemoteObject> object;
286     int32_t ret = GetAVCastControllerInner(data.ReadString(), object);
287     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
288     if (ret == AVSESSION_SUCCESS) {
289         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
290     }
291 #else
292     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
293 #endif
294     return ERR_NONE;
295 }
296 
HandleRegisterSessionListener(MessageParcel & data,MessageParcel & reply)297 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
298 {
299     int32_t err = PermissionChecker::GetInstance().CheckPermission(
300         PermissionChecker::CHECK_SYSTEM_PERMISSION);
301     if (err != ERR_NONE) {
302         SLOGE("RegisterSessionListener: CheckPermission failed");
303         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
304             "ERROR_MSG", "avsessionservice registersessionlistener checkpermission failed");
305         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
306         return ERR_NONE;
307     }
308     auto remoteObject = data.ReadRemoteObject();
309     if (remoteObject == nullptr) {
310         SLOGI("read remote object failed");
311         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
312         return ERR_NONE;
313     }
314     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
315     if (listener == nullptr) {
316         SLOGI("RegisterSessionListener but iface_cast remote object failed");
317         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
318         return ERR_NONE;
319     }
320     if (!reply.WriteInt32(RegisterSessionListener(listener))) {
321         SLOGI("reply write int32 failed");
322     }
323     return ERR_NONE;
324 }
325 
HandleRegisterSessionListenerForAllUsers(MessageParcel & data,MessageParcel & reply)326 int32_t AVSessionServiceStub::HandleRegisterSessionListenerForAllUsers(MessageParcel& data, MessageParcel& reply)
327 {
328     int32_t err = PermissionChecker::GetInstance().CheckPermission(
329         PermissionChecker::CHECK_SYSTEM_PERMISSION);
330     if (err != ERR_NONE) {
331         SLOGE("RegisterSessionListenerForAllUsers: CheckPermission failed");
332         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
333             "ERROR_MSG", "avsessionservice RegisterSessionListenerForAllUsers checkpermission failed");
334         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
335         return ERR_NONE;
336     }
337     auto remoteObject = data.ReadRemoteObject();
338     if (remoteObject == nullptr) {
339         SLOGI("read remote object failed");
340         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
341         return ERR_NONE;
342     }
343     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
344     if (listener == nullptr) {
345         SLOGI("RegisterSessionListenerForAllUsers but iface_cast remote object failed");
346         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
347         return ERR_NONE;
348     }
349     if (!reply.WriteInt32(RegisterSessionListenerForAllUsers(listener))) {
350         SLOGI("reply write int32 failed");
351     }
352     return ERR_NONE;
353 }
354 
HandleSendSystemAVKeyEvent(MessageParcel & data,MessageParcel & reply)355 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
356 {
357     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
358     auto keyEvent = MMI::KeyEvent::Create();
359     if (keyEvent == nullptr) {
360         SLOGI("create keyEvent failed");
361         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
362         return ERR_NONE;
363     }
364     if (!keyEvent->ReadFromParcel(data)) {
365         SLOGI("read keyEvent failed");
366         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
367         return ERR_NONE;
368     }
369     if (!keyEvent->IsValid()) {
370         SLOGI("keyEvent is not valid");
371         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
372         return ERR_NONE;
373     }
374     int32_t err = PermissionChecker::GetInstance().CheckPermission(
375         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
376     if (err != ERR_NONE) {
377         SLOGE("SendSystemAVKeyEvent: CheckPermission failed");
378         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
379             "KEY_CODE", keyEvent->GetKeyCode(), "KEY_ACTION", keyEvent->GetKeyAction(),
380             "ERROR_MSG", "avsessionservice sendsystemavkeyevent checkpermission failed");
381         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
382         return ERR_NONE;
383     }
384     sptr bluetoothWant = data.ReadParcelable<AAFwk::Want>();
385     if (bluetoothWant != nullptr) {
386         if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent, *bluetoothWant))) {
387         SLOGI("reply write int32 failed");
388         }
389         return ERR_NONE;
390     }
391     if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
392         SLOGI("reply write int32 failed");
393     }
394     return ERR_NONE;
395 }
396 
HandleSendSystemControlCommand(MessageParcel & data,MessageParcel & reply)397 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
398 {
399     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
400     sptr command = data.ReadParcelable<AVControlCommand>();
401     if (command == nullptr) {
402         SLOGI("read command failed");
403         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
404         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
405             "ERROR_INFO", "handle send system control command read command failed");
406         return ERR_NONE;
407     }
408     int32_t err = PermissionChecker::GetInstance().CheckPermission(
409         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
410     if (err != ERR_NONE) {
411         SLOGE("SendSystemControlCommand: CheckPermission failed");
412         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
413             "CALLER_PID", GetCallingPid(), "CMD", command->GetCommand(),
414             "ERROR_MSG", "avsessionservice sendsystemcontrolcommand checkpermission failed");
415         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
416         return ERR_NONE;
417     }
418     if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
419         SLOGI("reply write int32 failed");
420     }
421     return ERR_NONE;
422 }
423 
HandleRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)424 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
425 {
426     auto remoteObject = data.ReadRemoteObject();
427     if (remoteObject == nullptr) {
428         SLOGI("read remote object failed");
429         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
430         return ERR_NONE;
431     }
432     auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
433     if (clientDeathObserver == nullptr) {
434         SLOGI("iface_cast remote object failed");
435         reply.WriteInt32(ERR_INVALID_PARAM);
436         return ERR_NONE;
437     }
438     int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
439     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
440     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
441     return ERR_NONE;
442 }
443 
HandleClose(MessageParcel & data,MessageParcel & reply)444 int32_t AVSessionServiceStub::HandleClose(MessageParcel& data, MessageParcel& reply)
445 {
446     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
447     if (err != ERR_NONE) {
448         SLOGE("Close: CheckPermission failed");
449         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
450             "ERROR_MSG", "avsessionservice Close checkpermission failed");
451         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
452         return ERR_NONE;
453     }
454     int32_t ret = Close();
455     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Close failed");
456     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
457     return ERR_NONE;
458 }
459 
HandleCastAudio(MessageParcel & data,MessageParcel & reply)460 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
461 {
462     int32_t err = PermissionChecker::GetInstance().CheckPermission(
463         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
464     if (err != ERR_NONE) {
465         SLOGE("CastAudio: CheckPermission failed");
466         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
467             "ERROR_MSG", "avsessionservice CastAudio checkmission failed");
468         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
469         return ERR_NONE;
470     }
471     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
472     SLOGI("start");
473     SessionToken token {};
474     token.sessionId = data.ReadString();
475     token.pid = data.ReadInt32();
476     token.uid = data.ReadInt32();
477     int32_t deviceNum = data.ReadInt32();
478     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
479         SLOGI("receive deviceNum over range");
480         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
481         return ERR_NONE;
482     }
483 
484     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
485     for (int i = 0; i < deviceNum; i++) {
486         auto audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
487         if (audioDeviceDescriptor == nullptr) {
488             SLOGI("read AudioDeviceDescriptor failed");
489             CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
490             return ERR_NONE;
491         }
492         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
493               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
494         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
495     }
496     int32_t ret = CastAudio(token, sinkAudioDescriptors);
497     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
498     SLOGI("CastAudio ret %{public}d", ret);
499     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
500     SLOGI("success");
501     return ERR_NONE;
502 }
503 
HandleCastAudioForAll(MessageParcel & data,MessageParcel & reply)504 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
505 {
506     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
507     SLOGI("CastAudioForAll start");
508     int32_t err = PermissionChecker::GetInstance().CheckPermission(
509         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
510     if (err != ERR_NONE) {
511         SLOGE("CastAudioForAll: CheckPermission failed");
512         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
513             "ERROR_MSG", "avsessionservice CastAudioForAll checkpermission failed");
514         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
515         return ERR_NONE;
516     }
517     int32_t deviceNum = data.ReadInt32();
518     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
519         SLOGI("receive deviceNum over range");
520         reply.WriteInt32(ERR_INVALID_PARAM);
521         return ERR_NONE;
522     }
523 
524     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
525     for (int i = 0; i < deviceNum; i++) {
526         auto audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
527         if (audioDeviceDescriptor == nullptr) {
528             SLOGI("read AudioDeviceDescriptor failed");
529             reply.WriteInt32(ERR_UNMARSHALLING);
530             return ERR_NONE;
531         }
532         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
533               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
534         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
535     }
536     int32_t ret = CastAudioForAll(sinkAudioDescriptors);
537     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
538     SLOGI("CastAudioForAll ret %{public}d", ret);
539     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
540     return ERR_NONE;
541 }
542 
HandleRemoteCastAudio(MessageParcel & data,MessageParcel & reply)543 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
544 {
545     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
546     SLOGI("start");
547     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
548     if (err != ERR_NONE) {
549         SLOGE("ProcessCastAudioCommand: CheckPermission failed");
550         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
551             "ERROR_MSG", "avsessionservice ProcessCastAudioCommand checkpermission failed");
552         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
553         return ERR_NONE;
554     }
555     auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
556     std::string sessionInfo = data.ReadString();
557     std::string output;
558     int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
559     SLOGI("RemoteCastAudio ret %{public}d", ret);
560     if (ret != AVSESSION_SUCCESS) {
561         SLOGI("RemoteCastAudio failed");
562         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
563         return ERR_NONE;
564     }
565     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
566     CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
567     return ERR_NONE;
568 }
569 
HandleStartCastDiscovery(MessageParcel & data,MessageParcel & reply)570 int32_t AVSessionServiceStub::HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)
571 {
572     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCastDiscovery");
573     SLOGI("HandleStartCastDiscovery start");
574     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
575     if (err != ERR_NONE) {
576         SLOGE("StartCastDiscovery: CheckPermission failed");
577         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
578             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
579             "avsessionservice StartCastDiscovery checkPermission failed");
580         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
581         return ERR_NONE;
582     }
583 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
584     auto castDeviceCapability = data.ReadInt32();
585     int32_t drmSchemesLen = data.ReadInt32();
586     std::vector<std::string> drmSchemes;
587     int32_t maxDrmSchemesLen = 10;
588     CHECK_AND_RETURN_RET_LOG((drmSchemesLen >= 0) &&
589         (drmSchemesLen <= maxDrmSchemesLen), ERR_NONE, "drmSchemesLen is illegal");
590     for (int i = 0; i < drmSchemesLen; i++) {
591         std::string drmScheme = data.ReadString();
592         drmSchemes.emplace_back(drmScheme);
593     }
594     int32_t ret = AVRouter::GetInstance().StartCastDiscovery(castDeviceCapability, drmSchemes);
595     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
596     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartCastDiscovery failed");
597 #else
598     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
599 #endif
600     return ERR_NONE;
601 }
602 
HandleStopCastDiscovery(MessageParcel & data,MessageParcel & reply)603 int32_t AVSessionServiceStub::HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)
604 {
605     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCastDiscovery");
606     SLOGI("HandleStopCastDiscovery start");
607     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
608     if (err != ERR_NONE) {
609         SLOGE("StopCastDiscovery: CheckPermission failed");
610         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
611             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
612             "avsessionservice StopCastDiscovery checkPermission failed");
613         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
614         return ERR_NONE;
615     }
616 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
617     int32_t ret = AVRouter::GetInstance().StopCastDiscovery();
618     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
619     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopCastDiscovery failed");
620 #else
621     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
622 #endif
623     return ERR_NONE;
624 }
625 
HandleStartDeviceLogging(MessageParcel & data,MessageParcel & reply)626 int32_t AVSessionServiceStub::HandleStartDeviceLogging(MessageParcel& data, MessageParcel& reply)
627 {
628     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartDeviceLogging");
629     SLOGI("HandleStartDeviceLogging start");
630     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
631     if (err != ERR_NONE) {
632         SLOGE("HandleStartDeviceLogging: CheckPermission failed");
633         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
634             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
635             "avsessionservice StartDeviceLogging checkPermission failed");
636         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
637         return ERR_NONE;
638     }
639 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
640     int32_t fd = data.ReadFileDescriptor();
641     uint32_t maxSize = data.ReadUint32();
642     int32_t ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
643     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
644     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartDeviceLogging failed");
645 #else
646     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
647 #endif
648     return ERR_NONE;
649 }
650 
HandleStopDeviceLogging(MessageParcel & data,MessageParcel & reply)651 int32_t AVSessionServiceStub::HandleStopDeviceLogging(MessageParcel& data, MessageParcel& reply)
652 {
653     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopDeviceLogging");
654     SLOGI("HandleStopDeviceLogging start");
655     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
656     if (err != ERR_NONE) {
657         SLOGE("StopDeviceLogging: CheckPermission failed");
658         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
659             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
660             "avsessionservice StopDeviceLogging checkPermission failed");
661         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
662         return ERR_NONE;
663     }
664 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
665     int32_t ret = AVRouter::GetInstance().StopDeviceLogging();
666     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
667     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopDeviceLogging failed");
668 #else
669     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
670 #endif
671     return ERR_NONE;
672 }
673 
HandleSetDiscoverable(MessageParcel & data,MessageParcel & reply)674 int32_t AVSessionServiceStub::HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)
675 {
676     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleSetDiscoverable");
677     SLOGI("HandleSetDiscoverable start");
678     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
679     if (err != ERR_NONE) {
680         SLOGE("SetDiscoverable: CheckPermission failed");
681         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
682             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
683             "avsessionservice SetDiscoverable checkPermission failed");
684         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
685         return ERR_NONE;
686     }
687 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
688     bool enable;
689     CHECK_AND_RETURN_RET_LOG(data.ReadBool(enable), AVSESSION_ERROR, "write enable info failed");
690     int32_t ret = checkEnableCast(enable);
691     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
692     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleSetDiscoverable failed");
693 #else
694     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
695 #endif
696     return ERR_NONE;
697 }
698 
CheckBeforeHandleStartCast(MessageParcel & data,OutputDeviceInfo & outputDeviceInfo)699 int32_t AVSessionServiceStub::CheckBeforeHandleStartCast(MessageParcel& data, OutputDeviceInfo& outputDeviceInfo)
700 {
701     DeviceInfo deviceInfo;
702     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
703     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
704     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
705     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
706     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
707     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.networkId_), false, "Read networkId failed");
708     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
709     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
710     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
711     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
712         "Read supportedProtocols failed");
713     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
714         "Read authenticationStatus failed");
715     int32_t supportedDrmCapabilityLen = 0;
716     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
717         "read supportedDrmCapabilityLen failed");
718     int32_t maxSupportedDrmCapabilityLen = 10;
719     CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
720         (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
721     std::vector<std::string> supportedDrmCapabilities;
722     for (int i = 0; i < supportedDrmCapabilityLen; i++) {
723         std::string supportedDrmCapability;
724         CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
725             "read supportedDrmCapability failed");
726         supportedDrmCapabilities.emplace_back(supportedDrmCapability);
727     }
728     deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
729     CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
730     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
731     outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
732     return true;
733 }
734 
HandleStartCast(MessageParcel & data,MessageParcel & reply)735 int32_t AVSessionServiceStub::HandleStartCast(MessageParcel& data, MessageParcel& reply)
736 {
737 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
738     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCast");
739     int32_t err = PermissionChecker::GetInstance().CheckPermission(
740         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
741     if (err != ERR_NONE) {
742         SLOGE("StartCast: CheckPermission failed");
743         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
744             "ERROR_MSG", "avsessionservice StartCast checkPermission failed");
745         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
746         return ERR_NONE;
747     }
748     SessionToken sessionToken {};
749     sessionToken.sessionId = data.ReadString();
750     sessionToken.pid = data.ReadInt32();
751     sessionToken.uid = data.ReadInt32();
752 
753     OutputDeviceInfo outputDeviceInfo;
754     int32_t deviceInfoSize;
755     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
756 
757     if (deviceInfoSize > RECEIVE_DEVICE_NUM_MAX) {
758         SLOGI("receive deviceNum over range");
759         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
760         return ERR_NONE;
761     }
762     for (int i = 0; i < deviceInfoSize; i++) {
763         if (!CheckBeforeHandleStartCast(data, outputDeviceInfo)) {
764             SLOGE("check fail");
765             return ERR_NONE;
766         }
767     }
768     uint32_t callerToken = static_cast<uint32_t>(OHOS::IPCSkeleton::GetCallingTokenID());
769     int temp = SetFirstCallerTokenID(callerToken);
770     SLOGI("SetFirstCallerTokenID return %{public}d", temp);
771     if (temp < 0) {
772         SLOGE("SetFirstCallerTokenID fail");
773     }
774     int32_t ret = StartCast(sessionToken, outputDeviceInfo);
775     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StartCast failed");
776     SLOGI("StartCast ret %{public}d", ret);
777     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
778     SLOGI("HandleStartCast success");
779 #else
780     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
781 #endif
782     return ERR_NONE;
783 }
784 
HandleStopCast(MessageParcel & data,MessageParcel & reply)785 int32_t AVSessionServiceStub::HandleStopCast(MessageParcel& data, MessageParcel& reply)
786 {
787 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
788     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCast");
789     SLOGI("HandleStopCast start");
790     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
791     if (err != ERR_NONE) {
792         SLOGE("StopCast: CheckPermission failed");
793         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
794             "ERROR_MSG", "avsessionservice StopCast checkpermission failed");
795         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
796         return ERR_NONE;
797     }
798     SessionToken sessionToken {};
799     sessionToken.sessionId = data.ReadString();
800     sessionToken.pid = data.ReadInt32();
801     sessionToken.uid = data.ReadInt32();
802 
803     int32_t ret = StopCast(sessionToken);
804     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StopCast failed");
805     SLOGI("StopCast ret %{public}d", ret);
806     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
807     SLOGI("HandleStopCast success");
808 #else
809     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
810 #endif
811     return ERR_NONE;
812 }
813 
HandleGetDistributedSessionControllersInner(MessageParcel & data,MessageParcel & reply)814 int32_t AVSessionServiceStub::HandleGetDistributedSessionControllersInner(MessageParcel& data, MessageParcel& reply)
815 {
816     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetDistributedSessionControllersInner");
817     SLOGI("HandleGetDistributedSessionControllersInner start");
818     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
819     if (err != ERR_NONE) {
820         SLOGE("HandleGetDistributedSessionControllersInner: CheckPermission failed");
821         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
822         return ERR_NONE;
823     }
824     int32_t sessionTypeValue;
825     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(sessionTypeValue), false, "Read sessionType failed");
826     DistributedSessionType sessionType = DistributedSessionType(sessionTypeValue);
827     if (sessionType < DistributedSessionType::TYPE_SESSION_REMOTE ||
828         sessionType >= DistributedSessionType::TYPE_SESSION_MAX) {
829         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 result failed");
830         return ERR_NONE;
831     }
832     std::vector<sptr<IRemoteObject>> objects;
833     int32_t ret = GetDistributedSessionControllersInner(sessionType, objects);
834     SLOGI("HandleGetDistributedSessionControllersInner ret: %{public}d, size: %{public}d", ret, (int) objects.size());
835     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
836     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(objects.size()), ERR_NONE, "write size failed");
837     if (ret == AVSESSION_SUCCESS) {
838         for (const auto& object : objects) {
839             if (!reply.WriteRemoteObject(object)) {
840                 SLOGE("write object failed");
841                 break;
842             }
843         }
844     }
845     return ERR_NONE;
846 }
847 } // namespace OHOS::AVSession
848