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