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_source_proxy.h"
17
18 #include "anonymous_string.h"
19 #include "dcamera_ipc_interface_code.h"
20 #include "distributed_camera_errno.h"
21 #include "distributed_hardware_log.h"
22 #include "iremote_object.h"
23 #include "message_option.h"
24 #include "message_parcel.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
InitSource(const std::string & params,const sptr<IDCameraSourceCallback> & callback)28 int32_t DistributedCameraSourceProxy::InitSource(const std::string& params,
29 const sptr<IDCameraSourceCallback>& callback)
30 {
31 DHLOGI("start");
32 if (params.empty() || params.size() > PARAM_MAX_SIZE) {
33 DHLOGE("params is invalid");
34 return DCAMERA_BAD_VALUE;
35 }
36 sptr<IRemoteObject> remote = Remote();
37 if (remote == nullptr) {
38 DHLOGE("DistributedCameraSourceProxy remote service null");
39 return DCAMERA_BAD_VALUE;
40 }
41 MessageParcel data;
42 MessageParcel reply;
43 MessageOption option;
44 if (!data.WriteInterfaceToken(DistributedCameraSourceProxy::GetDescriptor())) {
45 DHLOGE("write token failed");
46 return DCAMERA_BAD_VALUE;
47 }
48
49 if (!data.WriteString(params)) {
50 DHLOGE("write params failed");
51 return DCAMERA_BAD_VALUE;
52 }
53
54 if (!data.WriteRemoteObject(callback->AsObject())) {
55 DHLOGE("write callback failed");
56 return DCAMERA_BAD_VALUE;
57 }
58
59 remote->SendRequest(static_cast<uint32_t>(IDCameraSourceInterfaceCode::INIT_SOURCE), data, reply, option);
60 int32_t result = reply.ReadInt32();
61 return result;
62 }
63
ReleaseSource()64 int32_t DistributedCameraSourceProxy::ReleaseSource()
65 {
66 DHLOGI("start");
67 sptr<IRemoteObject> remote = Remote();
68 if (remote == nullptr) {
69 DHLOGE("DistributedCameraSourceProxy remote service null");
70 return DCAMERA_BAD_VALUE;
71 }
72 MessageParcel data;
73 MessageParcel reply;
74 MessageOption option;
75 if (!data.WriteInterfaceToken(DistributedCameraSourceProxy::GetDescriptor())) {
76 DHLOGE("write token failed");
77 return DCAMERA_BAD_VALUE;
78 }
79 remote->SendRequest(static_cast<uint32_t>(IDCameraSourceInterfaceCode::RELEASE_SOURCE), data, reply, option);
80 int32_t result = reply.ReadInt32();
81 return result;
82 }
83
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId,const EnableParam & param)84 int32_t DistributedCameraSourceProxy::RegisterDistributedHardware(const std::string& devId, const std::string& dhId,
85 const std::string& reqId, const EnableParam& param)
86 {
87 DHLOGI("devId: %{public}s dhId: %{public}s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
88 if (!CheckRegParams(devId, dhId, reqId, param)) {
89 DHLOGE("input is invalid");
90 return DCAMERA_BAD_VALUE;
91 }
92 sptr<IRemoteObject> remote = Remote();
93 if (remote == nullptr) {
94 DHLOGE("DistributedCameraSourceProxy remote service null");
95 return DCAMERA_BAD_VALUE;
96 }
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option;
100 if (!data.WriteInterfaceToken(DistributedCameraSourceProxy::GetDescriptor())) {
101 DHLOGE("write token failed");
102 return DCAMERA_BAD_VALUE;
103 }
104
105 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(reqId) ||
106 !data.WriteString(param.sinkVersion) || !data.WriteString(param.sinkAttrs)) {
107 DHLOGE("write params failed");
108 return DCAMERA_BAD_VALUE;
109 }
110 remote->SendRequest(static_cast<uint32_t>(IDCameraSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE),
111 data, reply, option);
112 int32_t result = reply.ReadInt32();
113 return result;
114 }
115
CheckRegParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const EnableParam & param)116 bool DistributedCameraSourceProxy::CheckRegParams(const std::string& devId, const std::string& dhId,
117 const std::string& reqId, const EnableParam& param)
118 {
119 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
120 DHLOGE("devId or dhId is invalid");
121 return false;
122 }
123
124 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
125 DHLOGE("reqId is invalid");
126 return false;
127 }
128
129 if (param.sinkVersion.empty() || param.sinkVersion.size() > PARAM_MAX_SIZE ||
130 param.sinkAttrs.empty() || param.sinkAttrs.size() > PARAM_MAX_SIZE) {
131 DHLOGE("param is invalid");
132 return false;
133 }
134 return true;
135 }
136
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)137 int32_t DistributedCameraSourceProxy::UnregisterDistributedHardware(const std::string& devId, const std::string& dhId,
138 const std::string& reqId)
139 {
140 DHLOGI("devId: %{public}s dhId: %{public}s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
141 if (!CheckUnregParams(devId, dhId, reqId)) {
142 DHLOGE("input is invalid");
143 return DCAMERA_BAD_VALUE;
144 }
145 sptr<IRemoteObject> remote = Remote();
146 if (remote == nullptr) {
147 DHLOGE("DistributedCameraSourceProxy remote service null");
148 return DCAMERA_BAD_VALUE;
149 }
150 MessageParcel data;
151 MessageParcel reply;
152 MessageOption option;
153 if (!data.WriteInterfaceToken(DistributedCameraSourceProxy::GetDescriptor())) {
154 DHLOGE("write token failed");
155 return DCAMERA_BAD_VALUE;
156 }
157
158 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(reqId)) {
159 DHLOGE("write params failed");
160 return DCAMERA_BAD_VALUE;
161 }
162 remote->SendRequest(static_cast<uint32_t>(IDCameraSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE),
163 data, reply, option);
164 int32_t result = reply.ReadInt32();
165 return result;
166 }
167
CheckUnregParams(const std::string & devId,const std::string & dhId,const std::string & reqId)168 bool DistributedCameraSourceProxy::CheckUnregParams(const std::string& devId, const std::string& dhId,
169 const std::string& reqId)
170 {
171 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
172 DHLOGE("devId or dhId is invalid");
173 return false;
174 }
175
176 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
177 DHLOGE("reqId is invalid");
178 return false;
179 }
180 return true;
181 }
182
DCameraNotify(const std::string & devId,const std::string & dhId,std::string & events)183 int32_t DistributedCameraSourceProxy::DCameraNotify(const std::string& devId, const std::string& dhId,
184 std::string& events)
185 {
186 DHLOGI("DCameraNotify devId: %{public}s dhId: %{public}s events: %{public}s",
187 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), events.c_str());
188 if (!CheckNotifyParams(devId, dhId, events)) {
189 DHLOGE("input is invalid");
190 return DCAMERA_BAD_VALUE;
191 }
192 sptr<IRemoteObject> remote = Remote();
193 if (remote == nullptr) {
194 DHLOGE("DistributedCameraSourceProxy remote service null");
195 return DCAMERA_BAD_VALUE;
196 }
197 MessageParcel data;
198 MessageParcel reply;
199 MessageOption option;
200 if (!data.WriteInterfaceToken(DistributedCameraSourceProxy::GetDescriptor())) {
201 DHLOGE("write token failed");
202 return DCAMERA_BAD_VALUE;
203 }
204
205 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(events)) {
206 DHLOGE("write params failed");
207 return DCAMERA_BAD_VALUE;
208 }
209 remote->SendRequest(static_cast<uint32_t>(IDCameraSourceInterfaceCode::CAMERA_NOTIFY),
210 data, reply, option);
211 int32_t result = reply.ReadInt32();
212 return result;
213 }
214
CheckNotifyParams(const std::string & devId,const std::string & dhId,std::string & events)215 bool DistributedCameraSourceProxy::CheckNotifyParams(const std::string& devId, const std::string& dhId,
216 std::string& events)
217 {
218 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
219 DHLOGE("devId or dhId is invalid");
220 return false;
221 }
222
223 if (events.empty() || events.size() > PARAM_MAX_SIZE) {
224 DHLOGE("events is invalid");
225 return false;
226 }
227 return true;
228 }
229 } // namespace DistributedHardware
230 } // namespace OHOS
231