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 int32_t ipcResult = remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::SUBSCRIBE_LOCAL_HARDWARE),
109 data, reply, option);
110 if (ipcResult != DCAMERA_OK) {
111 DHLOGE("SendRequest for code failed");
112 return DCAMERA_BAD_VALUE;
113 }
114
115 int32_t result;
116
117 if (!reply.ReadInt32(result)) {
118 DHLOGE("read reply failed");
119 return DCAMERA_BAD_VALUE;
120 }
121
122 return result;
123 }
124
UnsubscribeLocalHardware(const std::string & dhId)125 int32_t DistributedCameraSinkProxy::UnsubscribeLocalHardware(const std::string& dhId)
126 {
127 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
128 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
129 DHLOGE("params is invalid");
130 return DCAMERA_BAD_VALUE;
131 }
132 sptr<IRemoteObject> remote = Remote();
133 if (remote == nullptr) {
134 DHLOGE("remote service is null");
135 return DCAMERA_BAD_VALUE;
136 }
137
138 MessageParcel data;
139 MessageParcel reply;
140 MessageOption option;
141 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
142 DHLOGE("write token failed");
143 return DCAMERA_BAD_VALUE;
144 }
145 if (!data.WriteString(dhId)) {
146 DHLOGE("write params failed");
147 return DCAMERA_BAD_VALUE;
148 }
149 int32_t ipcResult = remote->SendRequest(
150 static_cast<uint32_t>(IDCameraSinkInterfaceCode::UNSUBSCRIBE_LOCAL_HARDWARE), data, reply, option);
151 if (ipcResult != DCAMERA_OK) {
152 DHLOGE("SendRequest for code failed");
153 return DCAMERA_BAD_VALUE;
154 }
155 int32_t result;
156 if (!reply.ReadInt32(result)) {
157 DHLOGE("read reply failed");
158 return DCAMERA_BAD_VALUE;
159 }
160 return result;
161 }
162
StopCapture(const std::string & dhId)163 int32_t DistributedCameraSinkProxy::StopCapture(const std::string& dhId)
164 {
165 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
166 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
167 DHLOGE("params is invalid");
168 return DCAMERA_BAD_VALUE;
169 }
170 sptr<IRemoteObject> remote = Remote();
171 if (remote == nullptr) {
172 DHLOGE("remote service is null");
173 return DCAMERA_BAD_VALUE;
174 }
175
176 MessageParcel data;
177 MessageParcel reply;
178 MessageOption option = { MessageOption::TF_ASYNC };
179 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
180 DHLOGE("write token failed");
181 return DCAMERA_BAD_VALUE;
182 }
183 if (!data.WriteString(dhId)) {
184 DHLOGE("write params failed");
185 return DCAMERA_BAD_VALUE;
186 }
187 int32_t ipcResult = remote->SendRequest(
188 static_cast<uint32_t>(IDCameraSinkInterfaceCode::STOP_CAPTURE), data, reply, option);
189 if (ipcResult != DCAMERA_OK) {
190 DHLOGE("SendRequest for code failed");
191 return DCAMERA_BAD_VALUE;
192 }
193 DHLOGI("async dhId: %{public}s", GetAnonyString(dhId).c_str());
194 return DCAMERA_OK;
195 }
196
ChannelNeg(const std::string & dhId,std::string & channelInfo)197 int32_t DistributedCameraSinkProxy::ChannelNeg(const std::string& dhId, std::string& channelInfo)
198 {
199 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
200 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || channelInfo.empty() ||
201 channelInfo.size() > PARAM_MAX_SIZE) {
202 DHLOGE("params is invalid");
203 return DCAMERA_BAD_VALUE;
204 }
205 sptr<IRemoteObject> remote = Remote();
206 if (remote == nullptr) {
207 DHLOGE("remote service is null");
208 return DCAMERA_BAD_VALUE;
209 }
210
211 MessageParcel data;
212 MessageParcel reply;
213 MessageOption option;
214 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
215 DHLOGE("write token failed");
216 return DCAMERA_BAD_VALUE;
217 }
218 if (!data.WriteString(dhId) || !data.WriteString(channelInfo)) {
219 DHLOGE("write params failed");
220 return DCAMERA_BAD_VALUE;
221 }
222 int32_t ipcResult = remote->SendRequest(
223 static_cast<uint32_t>(IDCameraSinkInterfaceCode::CHANNEL_NEG), data, reply, option);
224 if (ipcResult != DCAMERA_OK) {
225 DHLOGE("SendRequest for code failed");
226 return DCAMERA_BAD_VALUE;
227 }
228 int32_t result;
229 if (!reply.ReadInt32(result)) {
230 DHLOGE("read reply failed");
231 return DCAMERA_BAD_VALUE;
232 }
233 return result;
234 }
235
GetCameraInfo(const std::string & dhId,std::string & cameraInfo)236 int32_t DistributedCameraSinkProxy::GetCameraInfo(const std::string& dhId, std::string& cameraInfo)
237 {
238 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
239 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
240 DHLOGE("params is invalid");
241 return DCAMERA_BAD_VALUE;
242 }
243 sptr<IRemoteObject> remote = Remote();
244 if (remote == nullptr) {
245 DHLOGE("remote service is null");
246 return DCAMERA_BAD_VALUE;
247 }
248
249 MessageParcel data;
250 MessageParcel reply;
251 MessageOption option;
252 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
253 DHLOGE("write token failed");
254 return DCAMERA_BAD_VALUE;
255 }
256 if (!data.WriteString(dhId) || !data.WriteString(cameraInfo)) {
257 DHLOGE("write params failed");
258 return DCAMERA_BAD_VALUE;
259 }
260 int32_t ipcResult = remote->SendRequest(
261 static_cast<uint32_t>(IDCameraSinkInterfaceCode::GET_CAMERA_INFO), data, reply, option);
262 if (ipcResult != DCAMERA_OK) {
263 DHLOGE("SendRequest for code failed");
264 return DCAMERA_BAD_VALUE;
265 }
266 int32_t result;
267 if (!reply.ReadInt32(result)) {
268 DHLOGE("read reply failed");
269 return DCAMERA_BAD_VALUE;
270 }
271 return result;
272 }
273
OpenChannel(const std::string & dhId,std::string & openInfo)274 int32_t DistributedCameraSinkProxy::OpenChannel(const std::string& dhId, std::string& openInfo)
275 {
276 DHLOGI("DistributedCameraSinkProxy OpenChannel Begin,dhId: %{public}s", GetAnonyString(dhId).c_str());
277 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || openInfo.empty() ||
278 openInfo.size() > PARAM_MAX_SIZE) {
279 DHLOGE("params is invalid");
280 return DCAMERA_BAD_VALUE;
281 }
282 sptr<IRemoteObject> remote = Remote();
283 if (remote == nullptr) {
284 DHLOGE("remote service is null");
285 return DCAMERA_BAD_VALUE;
286 }
287
288 MessageParcel data;
289 MessageParcel reply;
290 MessageOption option;
291 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
292 DHLOGE("write token failed");
293 return DCAMERA_BAD_VALUE;
294 }
295 if (!data.WriteString(dhId) || !data.WriteString(openInfo)) {
296 DHLOGE("write params failed");
297 return DCAMERA_BAD_VALUE;
298 }
299 int32_t ipcResult = remote->SendRequest(
300 static_cast<uint32_t>(IDCameraSinkInterfaceCode::OPEN_CHANNEL), data, reply, option);
301 if (ipcResult != DCAMERA_OK) {
302 DHLOGE("SendRequest for code failed");
303 return DCAMERA_BAD_VALUE;
304 }
305 int32_t result;
306 if (!reply.ReadInt32(result)) {
307 DHLOGE("read reply failed");
308 return DCAMERA_BAD_VALUE;
309 }
310 DHLOGI("DistributedCameraSinkProxy OpenChannel End,result: %{public}d", result);
311 return result;
312 }
313
CloseChannel(const std::string & dhId)314 int32_t DistributedCameraSinkProxy::CloseChannel(const std::string& dhId)
315 {
316 DHLOGI("dhId: %{public}s", GetAnonyString(dhId).c_str());
317 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
318 DHLOGE("params is invalid");
319 return DCAMERA_BAD_VALUE;
320 }
321 sptr<IRemoteObject> remote = Remote();
322 if (remote == nullptr) {
323 DHLOGE("remote service is null");
324 return DCAMERA_BAD_VALUE;
325 }
326
327 MessageParcel data;
328 MessageParcel reply;
329 MessageOption option;
330 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
331 DHLOGE("write token failed");
332 return DCAMERA_BAD_VALUE;
333 }
334 if (!data.WriteString(dhId)) {
335 DHLOGE("write params failed");
336 return DCAMERA_BAD_VALUE;
337 }
338 int32_t ipcResult = remote->SendRequest(
339 static_cast<uint32_t>(IDCameraSinkInterfaceCode::CLOSE_CHANNEL), data, reply, option);
340 if (ipcResult != DCAMERA_OK) {
341 DHLOGE("SendRequest for code failed");
342 return DCAMERA_BAD_VALUE;
343 }
344 int32_t result;
345 if (!reply.ReadInt32(result)) {
346 DHLOGE("read reply failed");
347 return DCAMERA_BAD_VALUE;
348 }
349 return result;
350 }
351
PauseDistributedHardware(const std::string & networkId)352 int32_t DistributedCameraSinkProxy::PauseDistributedHardware(const std::string &networkId)
353 {
354 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
355 sptr<IRemoteObject> remote = Remote();
356 if (remote == nullptr) {
357 DHLOGE("remote service is null");
358 return DCAMERA_BAD_VALUE;
359 }
360
361 MessageParcel data;
362 MessageParcel reply;
363 MessageOption option;
364 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
365 DHLOGE("write token failed");
366 return DCAMERA_BAD_VALUE;
367 }
368 if (!data.WriteString(networkId)) {
369 DHLOGE("write params failed");
370 return DCAMERA_BAD_VALUE;
371 }
372 int32_t ipcResult = remote->SendRequest(
373 static_cast<uint32_t>(IDCameraSinkInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE), data, reply, option);
374 if (ipcResult != DCAMERA_OK) {
375 DHLOGE("SendRequest for code failed");
376 return DCAMERA_BAD_VALUE;
377 }
378 int32_t result;
379 if (!reply.ReadInt32(result)) {
380 DHLOGE("read reply failed");
381 return DCAMERA_BAD_VALUE;
382 }
383 return result;
384 }
385
ResumeDistributedHardware(const std::string & networkId)386 int32_t DistributedCameraSinkProxy::ResumeDistributedHardware(const std::string &networkId)
387 {
388 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
389 sptr<IRemoteObject> remote = Remote();
390 if (remote == nullptr) {
391 DHLOGE("remote service is null");
392 return DCAMERA_BAD_VALUE;
393 }
394
395 MessageParcel data;
396 MessageParcel reply;
397 MessageOption option;
398 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
399 DHLOGE("write token failed");
400 return DCAMERA_BAD_VALUE;
401 }
402 if (!data.WriteString(networkId)) {
403 DHLOGE("write params failed");
404 return DCAMERA_BAD_VALUE;
405 }
406 int32_t ipcResult = remote->SendRequest(
407 static_cast<uint32_t>(IDCameraSinkInterfaceCode::RESUME_DISTRIBUTED_HARDWARE), data, reply, option);
408 if (ipcResult != DCAMERA_OK) {
409 DHLOGE("SendRequest for code failed");
410 return DCAMERA_BAD_VALUE;
411 }
412 int32_t result;
413 if (!reply.ReadInt32(result)) {
414 DHLOGE("read reply failed");
415 return DCAMERA_BAD_VALUE;
416 }
417 return result;
418 }
419
StopDistributedHardware(const std::string & networkId)420 int32_t DistributedCameraSinkProxy::StopDistributedHardware(const std::string &networkId)
421 {
422 DHLOGI("networkId: %{public}s", GetAnonyString(networkId).c_str());
423 sptr<IRemoteObject> remote = Remote();
424 if (remote == nullptr) {
425 DHLOGE("remote service is null");
426 return DCAMERA_BAD_VALUE;
427 }
428
429 MessageParcel data;
430 MessageParcel reply;
431 MessageOption option;
432 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
433 DHLOGE("write token failed");
434 return DCAMERA_BAD_VALUE;
435 }
436 if (!data.WriteString(networkId)) {
437 DHLOGE("write params failed");
438 return DCAMERA_BAD_VALUE;
439 }
440 int32_t ipcResult = remote->SendRequest(
441 static_cast<uint32_t>(IDCameraSinkInterfaceCode::STOP_DISTRIBUTED_HARDWARE), data, reply, option);
442 if (ipcResult != DCAMERA_OK) {
443 DHLOGE("SendRequest for code failed");
444 return DCAMERA_BAD_VALUE;
445 }
446 int32_t result;
447 if (!reply.ReadInt32(result)) {
448 DHLOGE("read reply failed");
449 return DCAMERA_BAD_VALUE;
450 }
451 return result;
452 }
453 } // namespace DistributedHardware
454 } // namespace OHOS