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 "sample_proxy.h"
17 #include <hdf_base.h>
18 #include <message_parcel.h>
19
20 namespace OHOS {
21 namespace HDI {
22 namespace Sample {
23 namespace V1_0 {
BooleanTypeTest(const bool input,bool & output)24 int32_t SampleProxy::BooleanTypeTest(const bool input, bool& output)
25 {
26 MessageParcel data;
27 MessageParcel reply;
28 MessageOption option;
29
30 if (!data.WriteBool(input)) {
31 HDF_LOGE("%{public}s: write bool input failed!", __func__);
32 return HDF_ERR_INVALID_PARAM;
33 }
34
35 int32_t ec = Remote()->SendRequest(CMD_BOOLEAN_TYPE_TEST, data, reply, option);
36 if (ec != HDF_SUCCESS) {
37 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
38 return ec;
39 }
40 output = reply.ReadBool();
41
42 return HDF_SUCCESS;
43 }
44
ByteTypeTest(const int8_t input,int8_t & output)45 int32_t SampleProxy::ByteTypeTest(const int8_t input, int8_t& output)
46 {
47 MessageParcel data;
48 MessageParcel reply;
49 MessageOption option;
50
51 if (!data.WriteInt8(input)) {
52 HDF_LOGE("%{public}s: write input failed!", __func__);
53 return HDF_ERR_INVALID_PARAM;
54 }
55
56 int32_t ec = Remote()->SendRequest(CMD_BYTE_TYPE_TEST, data, reply, option);
57 if (ec != HDF_SUCCESS) {
58 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
59 return ec;
60 }
61 output = reply.ReadInt8();
62
63 return HDF_SUCCESS;
64 }
65
ShortTypeTest(const int16_t input,int16_t & output)66 int32_t SampleProxy::ShortTypeTest(const int16_t input, int16_t& output)
67 {
68 MessageParcel data;
69 MessageParcel reply;
70 MessageOption option;
71
72 if (!data.WriteInt16(input)) {
73 HDF_LOGE("%{public}s: write input failed!", __func__);
74 return HDF_ERR_INVALID_PARAM;
75 }
76
77 int32_t ec = Remote()->SendRequest(CMD_SHORT_TYPE_TEST, data, reply, option);
78 if (ec != HDF_SUCCESS) {
79 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
80 return ec;
81 }
82 output = reply.ReadInt16();
83
84 return HDF_SUCCESS;
85 }
86
IntTypeTest(const int32_t input,int32_t & output)87 int32_t SampleProxy::IntTypeTest(const int32_t input, int32_t& output)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option;
92
93 if (!data.WriteInt32(input)) {
94 HDF_LOGE("%{public}s: write input failed!", __func__);
95 return HDF_ERR_INVALID_PARAM;
96 }
97
98 int32_t ec = Remote()->SendRequest(CMD_INT_TYPE_TEST, data, reply, option);
99 if (ec != HDF_SUCCESS) {
100 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
101 return ec;
102 }
103 output = reply.ReadInt32();
104
105 return HDF_SUCCESS;
106 }
107
LongTypeTest(const int64_t input,int64_t & output)108 int32_t SampleProxy::LongTypeTest(const int64_t input, int64_t& output)
109 {
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option;
113
114 if (!data.WriteInt64(input)) {
115 HDF_LOGE("%{public}s: write input failed!", __func__);
116 return HDF_ERR_INVALID_PARAM;
117 }
118
119 int32_t ec = Remote()->SendRequest(CMD_LONG_TYPE_TEST, data, reply, option);
120 if (ec != HDF_SUCCESS) {
121 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
122 return ec;
123 }
124 output = reply.ReadInt64();
125
126 return HDF_SUCCESS;
127 }
128
FloatTypeTest(const float input,float & output)129 int32_t SampleProxy::FloatTypeTest(const float input, float& output)
130 {
131 MessageParcel data;
132 MessageParcel reply;
133 MessageOption option;
134
135 if (!data.WriteFloat(input)) {
136 HDF_LOGE("%{public}s: write input failed!", __func__);
137 return HDF_ERR_INVALID_PARAM;
138 }
139
140 int32_t ec = Remote()->SendRequest(CMD_FLOAT_TYPE_TEST, data, reply, option);
141 if (ec != HDF_SUCCESS) {
142 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
143 return ec;
144 }
145 output = reply.ReadFloat();
146
147 return HDF_SUCCESS;
148 }
149
DoubleTypeTest(const double input,double & output)150 int32_t SampleProxy::DoubleTypeTest(const double input, double& output)
151 {
152 MessageParcel data;
153 MessageParcel reply;
154 MessageOption option;
155
156 if (!data.WriteDouble(input)) {
157 HDF_LOGE("%{public}s: write input failed!", __func__);
158 return HDF_ERR_INVALID_PARAM;
159 }
160
161 int32_t ec = Remote()->SendRequest(CMD_DOUBLE_TYPE_TEST, data, reply, option);
162 if (ec != HDF_SUCCESS) {
163 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
164 return ec;
165 }
166 output = reply.ReadDouble();
167
168 return HDF_SUCCESS;
169 }
170
StringTypeTest(const std::string & input,std::string & output)171 int32_t SampleProxy::StringTypeTest(const std::string& input, std::string& output)
172 {
173 MessageParcel data;
174 MessageParcel reply;
175 MessageOption option;
176
177 if (!data.WriteString(input)) {
178 HDF_LOGE("%{public}s: write input failed!", __func__);
179 return HDF_ERR_INVALID_PARAM;
180 }
181
182 int32_t ec = Remote()->SendRequest(CMD_STRING_TYPE_TEST, data, reply, option);
183 if (ec != HDF_SUCCESS) {
184 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
185 return ec;
186 }
187 output = reply.ReadString();
188
189 return HDF_SUCCESS;
190 }
191
UcharTypeTest(const uint8_t input,uint8_t & output)192 int32_t SampleProxy::UcharTypeTest(const uint8_t input, uint8_t& output)
193 {
194 MessageParcel data;
195 MessageParcel reply;
196 MessageOption option;
197
198 if (!data.WriteUint8(input)) {
199 HDF_LOGE("%{public}s: write input failed!", __func__);
200 return HDF_ERR_INVALID_PARAM;
201 }
202
203 int32_t ec = Remote()->SendRequest(CMD_UCHAR_TYPE_TEST, data, reply, option);
204 if (ec != HDF_SUCCESS) {
205 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
206 return ec;
207 }
208 output = reply.ReadUint8();
209
210 return HDF_SUCCESS;
211 }
212
UshortTypeTest(const uint16_t input,uint16_t & output)213 int32_t SampleProxy::UshortTypeTest(const uint16_t input, uint16_t& output)
214 {
215 MessageParcel data;
216 MessageParcel reply;
217 MessageOption option;
218
219 if (!data.WriteUint16(input)) {
220 HDF_LOGE("%{public}s: write input failed!", __func__);
221 return HDF_ERR_INVALID_PARAM;
222 }
223
224 int32_t ec = Remote()->SendRequest(CMD_UCHAR_TYPE_TEST, data, reply, option);
225 if (ec != HDF_SUCCESS) {
226 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
227 return ec;
228 }
229 output = reply.ReadUint16();
230
231 return HDF_SUCCESS;
232 }
233
UintTypeTest(const uint32_t input,uint32_t & output)234 int32_t SampleProxy::UintTypeTest(const uint32_t input, uint32_t& output)
235 {
236 MessageParcel data;
237 MessageParcel reply;
238 MessageOption option;
239
240 if (!data.WriteUint32(input)) {
241 HDF_LOGE("%{public}s: write input failed!", __func__);
242 return HDF_ERR_INVALID_PARAM;
243 }
244
245 int32_t ec = Remote()->SendRequest(CMD_UINT_TYPE_TEST, data, reply, option);
246 if (ec != HDF_SUCCESS) {
247 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
248 return ec;
249 }
250 output = reply.ReadUint32();
251
252 return HDF_SUCCESS;
253 }
254
UlongTypeTest(const uint64_t input,uint64_t & output)255 int32_t SampleProxy::UlongTypeTest(const uint64_t input, uint64_t& output)
256 {
257 MessageParcel data;
258 MessageParcel reply;
259 MessageOption option;
260
261 if (!data.WriteUint64(input)) {
262 HDF_LOGE("%{public}s: write input failed!", __func__);
263 return HDF_ERR_INVALID_PARAM;
264 }
265
266 int32_t ec = Remote()->SendRequest(CMD_ULONG_TYPE_TEST, data, reply, option);
267 if (ec != HDF_SUCCESS) {
268 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
269 return ec;
270 }
271 output = reply.ReadUint64();
272
273 return HDF_SUCCESS;
274 }
275
ListTypeTest(const std::list<int8_t> & input,std::list<int8_t> & output)276 int32_t SampleProxy::ListTypeTest(const std::list<int8_t>& input, std::list<int8_t>& output)
277 {
278 MessageParcel data;
279 MessageParcel reply;
280 MessageOption option;
281
282 if (!data.WriteUint32(input.size())) {
283 HDF_LOGE("%{public}s: write input size failed!", __func__);
284 return HDF_ERR_INVALID_PARAM;
285 }
286
287 for (auto it : input) {
288 if (!data.WriteUint8(it)) {
289 HDF_LOGE("%{public}s: write input data failed!", __func__);
290 return HDF_ERR_INVALID_PARAM;
291 }
292 }
293
294 int32_t ec = Remote()->SendRequest(CMD_LIST_TYPE_TEST, data, reply, option);
295 if (ec != HDF_SUCCESS) {
296 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
297 return ec;
298 }
299
300 uint32_t outSize = reply.ReadUint32();
301 for (uint32_t i = 0; i < outSize; i++) {
302 uint8_t curData = reply.ReadUint8();
303 output.push_back(curData);
304 }
305
306 return HDF_SUCCESS;
307 }
308
MapTypeTest(const std::map<int8_t,int8_t> & input,std::map<int8_t,int8_t> & output)309 int32_t SampleProxy::MapTypeTest(const std::map<int8_t, int8_t>& input, std::map<int8_t, int8_t>& output)
310 {
311 MessageParcel data;
312 MessageParcel reply;
313 MessageOption option;
314
315 if (!data.WriteUint32(input.size())) {
316 HDF_LOGE("%{public}s: write input size failed!", __func__);
317 return HDF_ERR_INVALID_PARAM;
318 }
319
320 for (auto it : input) {
321 if (!data.WriteUint8(it.first)) {
322 HDF_LOGE("%{public}s: write input data->first failed!", __func__);
323 return HDF_ERR_INVALID_PARAM;
324 }
325
326 if (!data.WriteUint8(it.second)) {
327 HDF_LOGE("%{public}s: write input data->second failed!", __func__);
328 return HDF_ERR_INVALID_PARAM;
329 }
330 }
331
332 int32_t ec = Remote()->SendRequest(CMD_MAP_TYPE_TEST, data, reply, option);
333 if (ec != HDF_SUCCESS) {
334 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
335 return ec;
336 }
337
338 uint32_t outSize = reply.ReadUint32();
339 for (uint32_t i = 0; i < outSize; i++) {
340 uint8_t key = reply.ReadUint8();
341 uint8_t val = reply.ReadUint8();
342 output[key] = val;
343 }
344
345 return HDF_SUCCESS;
346 }
347
348
ArrayTypeTest(const std::vector<int8_t> & input,std::vector<int8_t> & output)349 int32_t SampleProxy::ArrayTypeTest(const std::vector<int8_t>& input, std::vector<int8_t>& output)
350 {
351 MessageParcel data;
352 MessageParcel reply;
353 MessageOption option;
354
355 if (!data.WriteInt8Vector(input)) {
356 HDF_LOGE("%{public}s: write input failed!", __func__);
357 return HDF_ERR_INVALID_PARAM;
358 }
359
360 int32_t ec = Remote()->SendRequest(CMD_ARRAY_TYPE_TEST, data, reply, option);
361 if (ec != HDF_SUCCESS) {
362 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
363 return ec;
364 }
365 reply.ReadInt8Vector(&output);
366
367 return HDF_SUCCESS;
368 }
369
StructTypeTest(const StructSample & input,StructSample & output)370 int32_t SampleProxy::StructTypeTest(const StructSample& input, StructSample& output)
371 {
372 MessageParcel data;
373 MessageParcel reply;
374 MessageOption option;
375
376 if (!data.WriteBuffer((void *)&input, sizeof(StructSample))) {
377 HDF_LOGE("%{public}s: write input data failed", __func__);
378 return HDF_ERR_INVALID_PARAM;
379 }
380
381 int32_t ec = Remote()->SendRequest(CMD_STRUCT_TYPE_TEST, data, reply, option);
382 if (ec != HDF_SUCCESS) {
383 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
384 return ec;
385 }
386
387 StructSample *result = (StructSample *)data.ReadBuffer(sizeof(StructSample));
388 if (result == nullptr) {
389 HDF_LOGE("%{public}s: read output failed!", __func__);
390 return HDF_ERR_INVALID_PARAM;
391 }
392
393 output.first = result->first;
394 output.second = result->second;
395 return HDF_SUCCESS;
396 }
397
EnumTypeTest(const EnumSample & input,EnumSample & output)398 int32_t SampleProxy::EnumTypeTest(const EnumSample& input, EnumSample& output)
399 {
400 MessageParcel data;
401 MessageParcel reply;
402 MessageOption option;
403
404 if (!data.WriteUint32((uint32_t)input)) {
405 HDF_LOGE("%{public}s: write input failed!", __func__);
406 return HDF_ERR_INVALID_PARAM;
407 }
408
409 int32_t ec = Remote()->SendRequest(CMD_ENUM_TYPE_TEST, data, reply, option);
410 if (ec != HDF_SUCCESS) {
411 HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec);
412 return ec;
413 }
414 output = (EnumSample)reply.ReadUint32();
415
416 return HDF_SUCCESS;
417 }
418 } // namespace V1_0
419 } // namespace Sample
420 } // namespace HDI
421 } // namespace OHOS