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