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 #include "session_listener_stub.h"
16 #include "avsession_log.h"
17 #include "avsession_errors.h"
18 #include "avsession_trace.h"
19 
20 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)21 bool SessionListenerStub::CheckInterfaceToken(MessageParcel& data)
22 {
23     auto localDescriptor = ISessionListener::GetDescriptor();
24     auto remoteDescriptor = data.ReadInterfaceToken();
25     if (remoteDescriptor != localDescriptor) {
26         SLOGI("interface token is not equal");
27         return false;
28     }
29     return true;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t SessionListenerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
33                                              MessageOption& option)
34 {
35     if (!CheckInterfaceToken(data)) {
36         return AVSESSION_ERROR;
37     }
38     if (code >= static_cast<uint32_t>(ISessionListener::LISTENER_CMD_ON_CREATE)
39         && code < static_cast<uint32_t>(ISessionListener::LISTENER_CMD_MAX)) {
40         return handlers[code](data, reply);
41     }
42     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
43 }
44 
HandleOnSessionCreate(MessageParcel & data,MessageParcel & reply)45 int32_t SessionListenerStub::HandleOnSessionCreate(MessageParcel& data, MessageParcel& reply)
46 {
47     AVSESSION_TRACE_SYNC_START("SessionListenerStub::OnSessionCreate");
48     AVSessionDescriptor descriptor;
49     CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(data), ERR_NONE, "read descriptor failed");
50     OnSessionCreate(descriptor);
51     return ERR_NONE;
52 }
53 
HandleOnSessionRelease(MessageParcel & data,MessageParcel & reply)54 int32_t SessionListenerStub::HandleOnSessionRelease(MessageParcel& data, MessageParcel& reply)
55 {
56     AVSessionDescriptor descriptor;
57     CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(data), ERR_NONE, "read descriptor failed");
58     OnSessionRelease(descriptor);
59     return ERR_NONE;
60 }
61 
HandleOnTopSessionChange(MessageParcel & data,MessageParcel & reply)62 int32_t SessionListenerStub::HandleOnTopSessionChange(MessageParcel& data, MessageParcel& reply)
63 {
64     AVSESSION_TRACE_SYNC_START("SessionListenerStub::OnTopSessionChange");
65     AVSessionDescriptor descriptor;
66     CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(data), ERR_NONE, "read descriptor failed");
67     OnTopSessionChange(descriptor);
68     return ERR_NONE;
69 }
70 
HandleOnAudioSessionChecked(MessageParcel & data,MessageParcel & reply)71 int32_t SessionListenerStub::HandleOnAudioSessionChecked(MessageParcel& data, MessageParcel& reply)
72 {
73     AVSESSION_TRACE_SYNC_START("SessionListenerStub::OnAudioSessionChecked");
74     int32_t uid = data.ReadInt32();
75     OnAudioSessionChecked(uid);
76     reply.WriteInt32(AVSESSION_SUCCESS);
77     return ERR_NONE;
78 }
79 
HandleOnDeviceAvailable(MessageParcel & data,MessageParcel & reply)80 int32_t SessionListenerStub::HandleOnDeviceAvailable(MessageParcel& data, MessageParcel& reply)
81 {
82     AVSESSION_TRACE_SYNC_START("SessionListenerStub::HandleOnDeviceAvailable");
83     OutputDeviceInfo castOutputDeviceInfo;
84     CHECK_AND_RETURN_RET_LOG(castOutputDeviceInfo.ReadFromParcel(data), ERR_NONE, "read castOutputDeviceInfo failed");
85     OnDeviceAvailable(castOutputDeviceInfo);
86     return ERR_NONE;
87 }
88 
HandleOnDeviceLogEvent(MessageParcel & data,MessageParcel & reply)89 int32_t SessionListenerStub::HandleOnDeviceLogEvent(MessageParcel& data, MessageParcel& reply)
90 {
91     AVSESSION_TRACE_SYNC_START("SessionListenerStub::HandleOnDeviceLogEvent");
92     DeviceLogEventCode eventId = static_cast<DeviceLogEventCode>(data.ReadInt32());
93     int64_t param = data.ReadInt64();
94     OnDeviceLogEvent(eventId, param);
95     return ERR_NONE;
96 }
97 
HandleOnDeviceOffline(MessageParcel & data,MessageParcel & reply)98 int32_t SessionListenerStub::HandleOnDeviceOffline(MessageParcel& data, MessageParcel& reply)
99 {
100     AVSESSION_TRACE_SYNC_START("SessionListenerStub::HandleOnDeviceOffline");
101     auto deviceId = data.ReadString();
102     OnDeviceOffline(deviceId);
103     return ERR_NONE;
104 }
105 } // namespace OHOS::AVSession
106