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)27 int32_t DistributedCameraSinkProxy::InitSink(const std::string& params)
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 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::INIT_SINK), data, reply, option);
52 int32_t result = reply.ReadInt32();
53 return result;
54 }
55
ReleaseSink()56 int32_t DistributedCameraSinkProxy::ReleaseSink()
57 {
58 DHLOGI("start");
59 sptr<IRemoteObject> remote = Remote();
60 if (remote == nullptr) {
61 DHLOGE("remote service is null");
62 return DCAMERA_BAD_VALUE;
63 }
64
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option;
68 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
69 DHLOGE("write token failed");
70 return DCAMERA_BAD_VALUE;
71 }
72 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::RELEASE_SINK), data, reply, option);
73 int32_t result = reply.ReadInt32();
74 return result;
75 }
76
SubscribeLocalHardware(const std::string & dhId,const std::string & parameters)77 int32_t DistributedCameraSinkProxy::SubscribeLocalHardware(const std::string& dhId, const std::string& parameters)
78 {
79 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
80 if (parameters.empty() || parameters.size() > PARAM_MAX_SIZE || dhId.empty() ||
81 dhId.size() > DID_MAX_SIZE) {
82 DHLOGE("params is invalid");
83 return DCAMERA_BAD_VALUE;
84 }
85 sptr<IRemoteObject> remote = Remote();
86 if (remote == nullptr) {
87 DHLOGE("remote service is null");
88 return DCAMERA_BAD_VALUE;
89 }
90
91 MessageParcel data;
92 MessageParcel reply;
93 MessageOption option;
94 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
95 DHLOGE("write token failed");
96 return DCAMERA_BAD_VALUE;
97 }
98 if (!data.WriteString(dhId) || !data.WriteString(parameters)) {
99 DHLOGE("write params failed");
100 return DCAMERA_BAD_VALUE;
101 }
102 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::SUBSCRIBE_LOCAL_HARDWARE), data, reply,
103 option);
104 int32_t result = reply.ReadInt32();
105 return result;
106 }
107
UnsubscribeLocalHardware(const std::string & dhId)108 int32_t DistributedCameraSinkProxy::UnsubscribeLocalHardware(const std::string& dhId)
109 {
110 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
111 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
112 DHLOGE("params is invalid");
113 return DCAMERA_BAD_VALUE;
114 }
115 sptr<IRemoteObject> remote = Remote();
116 if (remote == nullptr) {
117 DHLOGE("remote service is null");
118 return DCAMERA_BAD_VALUE;
119 }
120
121 MessageParcel data;
122 MessageParcel reply;
123 MessageOption option;
124 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
125 DHLOGE("write token failed");
126 return DCAMERA_BAD_VALUE;
127 }
128 if (!data.WriteString(dhId)) {
129 DHLOGE("write params failed");
130 return DCAMERA_BAD_VALUE;
131 }
132 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::UNSUBSCRIBE_LOCAL_HARDWARE), data, reply,
133 option);
134 int32_t result = reply.ReadInt32();
135 return result;
136 }
137
StopCapture(const std::string & dhId)138 int32_t DistributedCameraSinkProxy::StopCapture(const std::string& dhId)
139 {
140 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
141 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
142 DHLOGE("params is invalid");
143 return DCAMERA_BAD_VALUE;
144 }
145 sptr<IRemoteObject> remote = Remote();
146 if (remote == nullptr) {
147 DHLOGE("remote service is null");
148 return DCAMERA_BAD_VALUE;
149 }
150
151 MessageParcel data;
152 MessageParcel reply;
153 MessageOption option = { MessageOption::TF_ASYNC };
154 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
155 DHLOGE("write token failed");
156 return DCAMERA_BAD_VALUE;
157 }
158 if (!data.WriteString(dhId)) {
159 DHLOGE("write params failed");
160 return DCAMERA_BAD_VALUE;
161 }
162 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::STOP_CAPTURE), data, reply, option);
163 int32_t result = reply.ReadInt32();
164 DHLOGI("async dhId: %s", GetAnonyString(dhId).c_str());
165 return result;
166 }
167
ChannelNeg(const std::string & dhId,std::string & channelInfo)168 int32_t DistributedCameraSinkProxy::ChannelNeg(const std::string& dhId, std::string& channelInfo)
169 {
170 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
171 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || channelInfo.empty() ||
172 channelInfo.size() > PARAM_MAX_SIZE) {
173 DHLOGE("params is invalid");
174 return DCAMERA_BAD_VALUE;
175 }
176 sptr<IRemoteObject> remote = Remote();
177 if (remote == nullptr) {
178 DHLOGE("remote service is null");
179 return DCAMERA_BAD_VALUE;
180 }
181
182 MessageParcel data;
183 MessageParcel reply;
184 MessageOption option;
185 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
186 DHLOGE("write token failed");
187 return DCAMERA_BAD_VALUE;
188 }
189 if (!data.WriteString(dhId) || !data.WriteString(channelInfo)) {
190 DHLOGE("write params failed");
191 return DCAMERA_BAD_VALUE;
192 }
193 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::CHANNEL_NEG), data, reply, option);
194 int32_t result = reply.ReadInt32();
195 return result;
196 }
197
GetCameraInfo(const std::string & dhId,std::string & cameraInfo)198 int32_t DistributedCameraSinkProxy::GetCameraInfo(const std::string& dhId, std::string& cameraInfo)
199 {
200 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
201 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
202 DHLOGE("parmas 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(cameraInfo)) {
219 DHLOGE("write params failed");
220 return DCAMERA_BAD_VALUE;
221 }
222 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::GET_CAMERA_INFO), data, reply, option);
223 int32_t result = reply.ReadInt32();
224 return result;
225 }
226
OpenChannel(const std::string & dhId,std::string & openInfo)227 int32_t DistributedCameraSinkProxy::OpenChannel(const std::string& dhId, std::string& openInfo)
228 {
229 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
230 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || openInfo.empty() ||
231 openInfo.size() > PARAM_MAX_SIZE) {
232 DHLOGE("params is invalid");
233 return DCAMERA_BAD_VALUE;
234 }
235 sptr<IRemoteObject> remote = Remote();
236 if (remote == nullptr) {
237 DHLOGE("remote service is null");
238 return DCAMERA_BAD_VALUE;
239 }
240
241 MessageParcel data;
242 MessageParcel reply;
243 MessageOption option;
244 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
245 DHLOGE("write token failed");
246 return DCAMERA_BAD_VALUE;
247 }
248 if (!data.WriteString(dhId) || !data.WriteString(openInfo)) {
249 DHLOGE("write params failed");
250 return DCAMERA_BAD_VALUE;
251 }
252 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::OPEN_CHANNEL), data, reply, option);
253 int32_t result = reply.ReadInt32();
254 return result;
255 }
256
CloseChannel(const std::string & dhId)257 int32_t DistributedCameraSinkProxy::CloseChannel(const std::string& dhId)
258 {
259 DHLOGI("dhId: %s", GetAnonyString(dhId).c_str());
260 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
261 DHLOGE("params is invalid");
262 return DCAMERA_BAD_VALUE;
263 }
264 sptr<IRemoteObject> remote = Remote();
265 if (remote == nullptr) {
266 DHLOGE("remote service is null");
267 return DCAMERA_BAD_VALUE;
268 }
269
270 MessageParcel data;
271 MessageParcel reply;
272 MessageOption option;
273 if (!data.WriteInterfaceToken(DistributedCameraSinkProxy::GetDescriptor())) {
274 DHLOGE("write token failed");
275 return DCAMERA_BAD_VALUE;
276 }
277 if (!data.WriteString(dhId)) {
278 DHLOGE("write params failed");
279 return DCAMERA_BAD_VALUE;
280 }
281 remote->SendRequest(static_cast<uint32_t>(IDCameraSinkInterfaceCode::CLOSE_CHANNEL), data, reply, option);
282 int32_t result = reply.ReadInt32();
283 return result;
284 }
285 } // namespace DistributedHardware
286 } // namespace OHOS