1 /*
2 * Copyright (c) 2021-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 */
15
16 #include "distributed_camera_sink_proxy.h"
17
18 #include "parcel.h"
19
20 #include "anonymous_string.h"
21 #include "dcamera_ipc_interface_code.h"
22 #include "distributed_camera_errno.h"
23 #include "distributed_hardware_log.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
InitSink(const std::string & params,const sptr<IDCameraSinkCallback> & sinkCallback)27 int32_t DistributedCameraSinkProxy::InitSink(const std::string& params, const sptr<IDCameraSinkCallback> &sinkCallback)
28 {
29 DHLOGI("start");
30 if (params.empty() || params.size() > PARAM_MAX_SIZE) {
31 DHLOGE("params is invalid");
32 return DCAMERA_BAD_VALUE;
33 }
34 sptr<IRemoteObject> remote = Remote();
35 if (remote == nullptr) {
36 DHLOGE("remote service is null");
37 return DCAMERA_BAD_VALUE;
38 }
39
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option;
43 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
44 DHLOGE("write token failed");
45 return DCAMERA_BAD_VALUE;
46 }
47 if (!data.WriteString(params)) {
48 DHLOGE("write params failed");
49 return DCAMERA_BAD_VALUE;
50 }
51 if (sinkCallback != nullptr && sinkCallback->AsObject() != nullptr) {
52 if (!data.WriteRemoteObject(sinkCallback->AsObject())) {
53 DHLOGE("write sinkCallback failed");
54 return DCAMERA_BAD_VALUE;
55 }
56 }
57 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::INIT_SINK), data, reply, option);
58 int32_t result = reply.ReadInt32();
59 return result;
60 }
61
ReleaseSink()62 int32_t DistributedCameraSinkProxy::ReleaseSink()
63 {
64 DHLOGI("start");
65 sptr<IRemoteObject> remote = Remote();
66 if (remote == nullptr) {
67 DHLOGE("remote service is null");
68 return DCAMERA_BAD_VALUE;
69 }
70
71 MessageParcel data;
72 MessageParcel reply;
73 MessageOption option;
74 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
75 DHLOGE("write token failed");
76 return DCAMERA_BAD_VALUE;
77 }
78 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::RELEASE_SINK), data, reply, option);
79 int32_t result = reply.ReadInt32();
80 return result;
81 }
82
SubscribeLocalHardware(const std::string & dhId,const std::string & parameters)83 int32_t DistributedCameraSinkProxy::SubscribeLocalHardware(const std::string& dhId, const std::string& parameters)
84 {
85 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
86 if (parameters.empty() || parameters.size() > PARAM_MAX_SIZE || dhId.empty() ||
87 dhId.size() > DID_MAX_SIZE) {
88 DHLOGE("params is invalid");
89 return DCAMERA_BAD_VALUE;
90 }
91 sptr<IRemoteObject> remote = Remote();
92 if (remote == nullptr) {
93 DHLOGE("remote service is null");
94 return DCAMERA_BAD_VALUE;
95 }
96
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option;
100 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
101 DHLOGE("write token failed");
102 return DCAMERA_BAD_VALUE;
103 }
104 if (!data.WriteString(dhId) || !data.WriteString(parameters)) {
105 DHLOGE("write params failed");
106 return DCAMERA_BAD_VALUE;
107 }
108 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::SUBSCRIBE_LOCAL_HARDWARE), data, reply,
109 option);
110 int32_t result = reply.ReadInt32();
111 return result;
112 }
113
UnsubscribeLocalHardware(const std::string & dhId)114 int32_t DistributedCameraSinkProxy::UnsubscribeLocalHardware(const std::string& dhId)
115 {
116 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
117 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
118 DHLOGE("params is invalid");
119 return DCAMERA_BAD_VALUE;
120 }
121 sptr<IRemoteObject> remote = Remote();
122 if (remote == nullptr) {
123 DHLOGE("remote service is null");
124 return DCAMERA_BAD_VALUE;
125 }
126
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option;
130 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
131 DHLOGE("write token failed");
132 return DCAMERA_BAD_VALUE;
133 }
134 if (!data.WriteString(dhId)) {
135 DHLOGE("write params failed");
136 return DCAMERA_BAD_VALUE;
137 }
138 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::UNSUBSCRIBE_LOCAL_HARDWARE), data, reply,
139 option);
140 int32_t result = reply.ReadInt32();
141 return result;
142 }
143
StopCapture(const std::string & dhId)144 int32_t DistributedCameraSinkProxy::StopCapture(const std::string& dhId)
145 {
146 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
147 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
148 DHLOGE("params is invalid");
149 return DCAMERA_BAD_VALUE;
150 }
151 sptr<IRemoteObject> remote = Remote();
152 if (remote == nullptr) {
153 DHLOGE("remote service is null");
154 return DCAMERA_BAD_VALUE;
155 }
156
157 MessageParcel data;
158 MessageParcel reply;
159 MessageOption option = { MessageOption::TF_ASYNC };
160 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
161 DHLOGE("write token failed");
162 return DCAMERA_BAD_VALUE;
163 }
164 if (!data.WriteString(dhId)) {
165 DHLOGE("write params failed");
166 return DCAMERA_BAD_VALUE;
167 }
168 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::STOP_CAPTURE), data, reply, option);
169 int32_t result = reply.ReadInt32();
170 DHLOGI("async dhId: %{public}s", GetAnonyString(dhId).c_str());
171 return result;
172 }
173
ChannelNeg(const std::string & dhId,std::string & channelInfo)174 int32_t DistributedCameraSinkProxy::ChannelNeg(const std::string& dhId, std::string& channelInfo)
175 {
176 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
177 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || channelInfo.empty() ||
178 channelInfo.size() > PARAM_MAX_SIZE) {
179 DHLOGE("params is invalid");
180 return DCAMERA_BAD_VALUE;
181 }
182 sptr<IRemoteObject> remote = Remote();
183 if (remote == nullptr) {
184 DHLOGE("remote service is null");
185 return DCAMERA_BAD_VALUE;
186 }
187
188 MessageParcel data;
189 MessageParcel reply;
190 MessageOption option;
191 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
192 DHLOGE("write token failed");
193 return DCAMERA_BAD_VALUE;
194 }
195 if (!data.WriteString(dhId) || !data.WriteString(channelInfo)) {
196 DHLOGE("write params failed");
197 return DCAMERA_BAD_VALUE;
198 }
199 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::CHANNEL_NEG), data, reply, option);
200 int32_t result = reply.ReadInt32();
201 return result;
202 }
203
GetCameraInfo(const std::string & dhId,std::string & cameraInfo)204 int32_t DistributedCameraSinkProxy::GetCameraInfo(const std::string& dhId, std::string& cameraInfo)
205 {
206 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
207 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
208 DHLOGE("parmas is invalid");
209 return DCAMERA_BAD_VALUE;
210 }
211 sptr<IRemoteObject> remote = Remote();
212 if (remote == nullptr) {
213 DHLOGE("remote service is null");
214 return DCAMERA_BAD_VALUE;
215 }
216
217 MessageParcel data;
218 MessageParcel reply;
219 MessageOption option;
220 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
221 DHLOGE("write token failed");
222 return DCAMERA_BAD_VALUE;
223 }
224 if (!data.WriteString(dhId) || !data.WriteString(cameraInfo)) {
225 DHLOGE("write params failed");
226 return DCAMERA_BAD_VALUE;
227 }
228 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::GET_CAMERA_INFO), data, reply, option);
229 int32_t result = reply.ReadInt32();
230 return result;
231 }
232
OpenChannel(const std::string & dhId,std::string & openInfo)233 int32_t DistributedCameraSinkProxy::OpenChannel(const std::string& dhId, std::string& openInfo)
234 {
235 DHLOGI("DistributedCameraSinkProxy OpenChannel Begin,dhId: %{public}s", GetAnonyString(dhId).c_str());
236 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || openInfo.empty() ||
237 openInfo.size() > PARAM_MAX_SIZE) {
238 DHLOGE("params is invalid");
239 return DCAMERA_BAD_VALUE;
240 }
241 sptr<IRemoteObject> remote = Remote();
242 if (remote == nullptr) {
243 DHLOGE("remote service is null");
244 return DCAMERA_BAD_VALUE;
245 }
246
247 MessageParcel data;
248 MessageParcel reply;
249 MessageOption option;
250 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
251 DHLOGE("write token failed");
252 return DCAMERA_BAD_VALUE;
253 }
254 if (!data.WriteString(dhId) || !data.WriteString(openInfo)) {
255 DHLOGE("write params failed");
256 return DCAMERA_BAD_VALUE;
257 }
258 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::OPEN_CHANNEL), data, reply, option);
259 int32_t result = reply.ReadInt32();
260 DHLOGI("DistributedCameraSinkProxy OpenChannel End,result: %{public}d", result);
261 return result;
262 }
263
CloseChannel(const std::string & dhId)264 int32_t DistributedCameraSinkProxy::CloseChannel(const std::string& dhId)
265 {
266 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
267 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
268 DHLOGE("params is invalid");
269 return DCAMERA_BAD_VALUE;
270 }
271 sptr<IRemoteObject> remote = Remote();
272 if (remote == nullptr) {
273 DHLOGE("remote service is null");
274 return DCAMERA_BAD_VALUE;
275 }
276
277 MessageParcel data;
278 MessageParcel reply;
279 MessageOption option;
280 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
281 DHLOGE("write token failed");
282 return DCAMERA_BAD_VALUE;
283 }
284 if (!data.WriteString(dhId)) {
285 DHLOGE("write params failed");
286 return DCAMERA_BAD_VALUE;
287 }
288 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::CLOSE_CHANNEL), data, reply, option);
289 int32_t result = reply.ReadInt32();
290 return result;
291 }
292
PauseDistributedHardware(const std::string & networkId)293 int32_t DistributedCameraSinkProxy::PauseDistributedHardware(const std::string &networkId)
294 {
295 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
296 sptr<IRemoteObject> remote = Remote();
297 if (remote == nullptr) {
298 DHLOGE("remote service is null");
299 return DCAMERA_BAD_VALUE;
300 }
301
302 MessageParcel data;
303 MessageParcel reply;
304 MessageOption option;
305 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
306 DHLOGE("write token failed");
307 return DCAMERA_BAD_VALUE;
308 }
309 if (!data.WriteString(networkId)) {
310 DHLOGE("write params failed");
311 return DCAMERA_BAD_VALUE;
312 }
313 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE),
314 data, reply, option);
315 int32_t result = reply.ReadInt32();
316 return result;
317 }
318
ResumeDistributedHardware(const std::string & networkId)319 int32_t DistributedCameraSinkProxy::ResumeDistributedHardware(const std::string &networkId)
320 {
321 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
322 sptr<IRemoteObject> remote = Remote();
323 if (remote == nullptr) {
324 DHLOGE("remote service is null");
325 return DCAMERA_BAD_VALUE;
326 }
327
328 MessageParcel data;
329 MessageParcel reply;
330 MessageOption option;
331 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
332 DHLOGE("write token failed");
333 return DCAMERA_BAD_VALUE;
334 }
335 if (!data.WriteString(networkId)) {
336 DHLOGE("write params failed");
337 return DCAMERA_BAD_VALUE;
338 }
339 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::RESUME_DISTRIBUTED_HARDWARE),
340 data, reply, option);
341 int32_t result = reply.ReadInt32();
342 return result;
343 }
344
StopDistributedHardware(const std::string & networkId)345 int32_t DistributedCameraSinkProxy::StopDistributedHardware(const std::string &networkId)
346 {
347 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
348 sptr<IRemoteObject> remote = Remote();
349 if (remote == nullptr) {
350 DHLOGE("remote service is null");
351 return DCAMERA_BAD_VALUE;
352 }
353
354 MessageParcel data;
355 MessageParcel reply;
356 MessageOption option;
357 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
358 DHLOGE("write token failed");
359 return DCAMERA_BAD_VALUE;
360 }
361 if (!data.WriteString(networkId)) {
362 DHLOGE("write params failed");
363 return DCAMERA_BAD_VALUE;
364 }
365 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::STOP_DISTRIBUTED_HARDWARE),
366 data, reply, option);
367 int32_t result = reply.ReadInt32();
368 return result;
369 }
370 } // namespace DistributedHardware
371 } // namespace OHOS