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
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int CastSessionManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
32 MessageOption &option)
33 {
34 RETRUEN_IF_WRONG_TASK(code, data, reply, option);
35 return EXECUTE_SINGLE_STUB_TASK(code, data, reply);
36 }
37
CastSessionManagerServiceStub()38 CastSessionManagerServiceStub::CastSessionManagerServiceStub()
39 {
40 FILL_SINGLE_STUB_TASK(REGISTER_LISTENER, &CastSessionManagerServiceStub::DoRegisterListenerTask);
41 FILL_SINGLE_STUB_TASK(UNREGISTER_LISTENER, &CastSessionManagerServiceStub::DoUnregisterListenerTask);
42 FILL_SINGLE_STUB_TASK(RELEASE, &CastSessionManagerServiceStub::DoReleaseTask);
43 FILL_SINGLE_STUB_TASK(SET_LOCAL_DEVICE, &CastSessionManagerServiceStub::DoSetLocalDeviceTask);
44 FILL_SINGLE_STUB_TASK(CREATE_CAST_SESSION, &CastSessionManagerServiceStub::DoCreateCastSessionTask);
45 FILL_SINGLE_STUB_TASK(SET_SINK_SESSION_CAPACITY, &CastSessionManagerServiceStub::DoSetSinkSessionCapacityTask);
46 FILL_SINGLE_STUB_TASK(START_DISCOVERY, &CastSessionManagerServiceStub::DoStartDiscoveryTask);
47 FILL_SINGLE_STUB_TASK(SET_DISCOVERABLE, &CastSessionManagerServiceStub::DoSetDiscoverableTask);
48 FILL_SINGLE_STUB_TASK(STOP_DISCOVERY, &CastSessionManagerServiceStub::DoStopDiscoveryTask);
49 FILL_SINGLE_STUB_TASK(GET_CAST_SESSION, &CastSessionManagerServiceStub::DoGetCastSessionTask);
50 }
51
~CastSessionManagerServiceStub()52 CastSessionManagerServiceStub::~CastSessionManagerServiceStub()
53 {
54 CLOGD("destructor in");
55 }
56
DoRegisterListenerTask(MessageParcel & data,MessageParcel & reply)57 int32_t CastSessionManagerServiceStub::DoRegisterListenerTask(MessageParcel &data, MessageParcel &reply)
58 {
59 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission() &&
60 !Permission::CheckPidPermission()) {
61 return ERR_UNKNOWN_TRANSACTION;
62 }
63 sptr<IRemoteObject> obj = data.ReadRemoteObject();
64 if (obj == nullptr) {
65 return ERR_NULL_OBJECT;
66 }
67
68 sptr<ICastServiceListenerImpl> listener{ new (std::nothrow) CastServiceListenerImplProxy(obj) };
69 if (listener == nullptr) {
70 CLOGE("RegisterListener failed, listener is null");
71 }
72
73 if (!reply.WriteInt32(RegisterListener(listener))) {
74 CLOGE("Failed to write int value");
75 return IPC_STUB_WRITE_PARCEL_ERR;
76 }
77
78 return ERR_NONE;
79 }
80
DoUnregisterListenerTask(MessageParcel & data,MessageParcel & reply)81 int32_t CastSessionManagerServiceStub::DoUnregisterListenerTask(MessageParcel &data, MessageParcel &reply)
82 {
83 if (!reply.WriteInt32(UnregisterListener())) {
84 CLOGE("Failed to write int value");
85 return IPC_STUB_WRITE_PARCEL_ERR;
86 }
87
88 return ERR_NONE;
89 }
90
DoReleaseTask(MessageParcel & data,MessageParcel & reply)91 int32_t CastSessionManagerServiceStub::DoReleaseTask(MessageParcel &data, MessageParcel &reply)
92 {
93 static_cast<void>(data);
94
95 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
96 return ERR_UNKNOWN_TRANSACTION;
97 }
98
99 if (!reply.WriteInt32(Release())) {
100 CLOGE("Failed to write int value");
101 return IPC_STUB_WRITE_PARCEL_ERR;
102 }
103
104 return ERR_NONE;
105 }
106
DoSetLocalDeviceTask(MessageParcel & data,MessageParcel & reply)107 int32_t CastSessionManagerServiceStub::DoSetLocalDeviceTask(MessageParcel &data, MessageParcel &reply)
108 {
109 if (!Permission::CheckMirrorPermission()) {
110 return ERR_UNKNOWN_TRANSACTION;
111 }
112
113 auto device = ReadCastLocalDevice(data);
114 if (device == nullptr) {
115 CLOGE("Invalid device object comes");
116 return ERR_INVALID_DATA;
117 }
118
119 if (!reply.WriteInt32(SetLocalDevice(*device))) {
120 CLOGE("Failed to write int value");
121 return IPC_STUB_WRITE_PARCEL_ERR;
122 }
123 return ERR_NONE;
124 }
125
DoCreateCastSessionTask(MessageParcel & data,MessageParcel & reply)126 int32_t CastSessionManagerServiceStub::DoCreateCastSessionTask(MessageParcel &data, MessageParcel &reply)
127 {
128 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
129 return ERR_UNKNOWN_TRANSACTION;
130 }
131
132 auto property = ReadCastSessionProperty(data);
133 if (property == nullptr) {
134 CLOGE("Invalid property object comes");
135 return ERR_INVALID_DATA;
136 }
137
138 sptr<ICastSessionImpl> sessionStub;
139 int32_t ret = CreateCastSession(*property, sessionStub);
140 if (sessionStub == nullptr) {
141 return IPC_STUB_ERR;
142 }
143 if (!reply.WriteInt32(ret)) {
144 CLOGE("Failed to write ret:%d", ret);
145 return IPC_STUB_WRITE_PARCEL_ERR;
146 }
147 if (!reply.WriteRemoteObject(sessionStub->AsObject())) {
148 CLOGE("Failed to write cast session");
149 return IPC_STUB_WRITE_PARCEL_ERR;
150 }
151
152 return ERR_NONE;
153 }
154
DoSetSinkSessionCapacityTask(MessageParcel & data,MessageParcel & reply)155 int32_t CastSessionManagerServiceStub::DoSetSinkSessionCapacityTask(MessageParcel &data, MessageParcel &reply)
156 {
157 static_cast<void>(reply);
158
159 if (!Permission::CheckMirrorPermission()) {
160 return ERR_UNKNOWN_TRANSACTION;
161 }
162
163 int32_t capacity = data.ReadInt32();
164 int32_t ret = SetSinkSessionCapacity(capacity);
165 if (!reply.WriteInt32(ret)) {
166 CLOGE("Failed to write ret:%d", ret);
167 return IPC_STUB_WRITE_PARCEL_ERR;
168 }
169
170 return ERR_NONE;
171 }
172
DoStartDiscoveryTask(MessageParcel & data,MessageParcel & reply)173 int32_t CastSessionManagerServiceStub::DoStartDiscoveryTask(MessageParcel &data, MessageParcel &reply)
174 {
175 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
176 return ERR_UNKNOWN_TRANSACTION;
177 }
178
179 int32_t type = data.ReadInt32();
180 if (!IsProtocolType(type)) {
181 CLOGE("Invalid protocol type comes, %{public}d", type);
182 return ERR_INVALID_DATA;
183 }
184 if (!reply.WriteInt32(StartDiscovery(type))) {
185 CLOGE("Failed to write int value");
186 return IPC_STUB_WRITE_PARCEL_ERR;
187 }
188 return ERR_NONE;
189 }
190
DoSetDiscoverableTask(MessageParcel & data,MessageParcel & reply)191 int32_t CastSessionManagerServiceStub::DoSetDiscoverableTask(MessageParcel &data, MessageParcel &reply)
192 {
193 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
194 return ERR_UNKNOWN_TRANSACTION;
195 }
196
197 bool enable = data.ReadBool();
198 if (!reply.WriteInt32(SetDiscoverable(enable))) {
199 CLOGE("Failed to write int value");
200 return IPC_STUB_WRITE_PARCEL_ERR;
201 }
202 return ERR_NONE;
203 }
204
DoStopDiscoveryTask(MessageParcel & data,MessageParcel & reply)205 int32_t CastSessionManagerServiceStub::DoStopDiscoveryTask(MessageParcel &data, MessageParcel &reply)
206 {
207 if (!Permission::CheckMirrorPermission() && !Permission::CheckStreamPermission()) {
208 return ERR_UNKNOWN_TRANSACTION;
209 }
210
211 if (!reply.WriteInt32(StopDiscovery())) {
212 CLOGE("Failed to write int value");
213 return IPC_STUB_WRITE_PARCEL_ERR;
214 }
215 return ERR_NONE;
216 }
217
DoGetCastSessionTask(MessageParcel & data,MessageParcel & reply)218 int32_t CastSessionManagerServiceStub::DoGetCastSessionTask(MessageParcel &data, MessageParcel &reply)
219 {
220 sptr<ICastSessionImpl> sessionStub;
221 int32_t ret = GetCastSession(data.ReadString(), sessionStub);
222 if (sessionStub == nullptr) {
223 return IPC_STUB_ERR;
224 }
225 if (!reply.WriteInt32(ret)) {
226 CLOGE("Failed to write ret:%d", ret);
227 return IPC_STUB_WRITE_PARCEL_ERR;
228 }
229 if (!reply.WriteRemoteObject(sessionStub->AsObject())) {
230 CLOGE("Failed to write cast session");
231 return IPC_STUB_WRITE_PARCEL_ERR;
232 }
233 return ERR_NONE;
234 }
235 } // namespace CastEngineService
236 } // namespace CastEngine
237 } // namespace OHOS
238