• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_stub.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace Sample {
24 namespace V1_0 {
SampleStubBooleanTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const25 int32_t SampleServiceStub::SampleStubBooleanTypeTest(MessageParcel& data,
26     MessageParcel& reply, MessageOption& option) const
27 {
28     const bool input = data.ReadBool();
29     bool output = false;
30 
31     int32_t ec = service.BooleanTypeTest(input, output);
32     if (ec != HDF_SUCCESS) {
33         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
34         return ec;
35     }
36 
37     if (!reply.WriteBool(output)) {
38         HDF_LOGE("%{public}s: write result failed", __func__);
39         return HDF_ERR_INVALID_PARAM;
40     }
41 
42     return HDF_SUCCESS;
43 }
44 
SampleStubByteTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const45 int32_t SampleServiceStub::SampleStubByteTypeTest(MessageParcel& data,
46     MessageParcel& reply, MessageOption& option) const
47 {
48     const int8_t input = data.ReadInt8();
49     int8_t output;
50 
51     int32_t ec = service.ByteTypeTest(input, output);
52     if (ec != HDF_SUCCESS) {
53         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
54         return ec;
55     }
56 
57     if (!reply.WriteInt8(output)) {
58         HDF_LOGE("%{public}s: write result failed", __func__);
59         return HDF_ERR_INVALID_PARAM;
60     }
61 
62     return HDF_SUCCESS;
63 }
64 
SampleStubShortTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const65 int32_t SampleServiceStub::SampleStubShortTypeTest(MessageParcel& data,
66     MessageParcel& reply, MessageOption& option) const
67 {
68     const int16_t input = data.ReadInt16();
69     int16_t output;
70 
71     int32_t ec = service.ShortTypeTest(input, output);
72     if (ec != HDF_SUCCESS) {
73         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
74         return ec;
75     }
76 
77     if (!reply.WriteInt16(output)) {
78         HDF_LOGE("%{public}s: write result failed", __func__);
79         return HDF_ERR_INVALID_PARAM;
80     }
81 
82     return HDF_SUCCESS;
83 }
84 
SampleStubIntTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const85 int32_t SampleServiceStub::SampleStubIntTypeTest(MessageParcel& data,
86     MessageParcel& reply, MessageOption& option) const
87 {
88     const int32_t input = data.ReadInt32();
89     int32_t output;
90 
91     int32_t ec = service.IntTypeTest(input, output);
92     if (ec != HDF_SUCCESS) {
93         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
94         return ec;
95     }
96 
97     if (!reply.WriteInt32(output)) {
98         HDF_LOGE("%{public}s: write result failed", __func__);
99         return HDF_ERR_INVALID_PARAM;
100     }
101 
102     return HDF_SUCCESS;
103 }
104 
SampleStubLongTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const105 int32_t SampleServiceStub::SampleStubLongTypeTest(MessageParcel& data,
106     MessageParcel& reply, MessageOption& option) const
107 {
108     const int64_t input = data.ReadInt64();
109     int64_t output;
110 
111     int32_t ec = service.LongTypeTest(input, output);
112     if (ec != HDF_SUCCESS) {
113         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
114         return ec;
115     }
116 
117     if (!reply.WriteInt64(output)) {
118         HDF_LOGE("%{public}s: write result failed", __func__);
119         return HDF_ERR_INVALID_PARAM;
120     }
121 
122     return HDF_SUCCESS;
123 }
124 
SampleStubFloatTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const125 int32_t SampleServiceStub::SampleStubFloatTypeTest(MessageParcel& data,
126     MessageParcel& reply, MessageOption& option) const
127 {
128     const float input = data.ReadFloat();
129     float output;
130 
131     int32_t ec = service.FloatTypeTest(input, output);
132     if (ec != HDF_SUCCESS) {
133         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
134         return ec;
135     }
136 
137     if (!reply.WriteFloat(output)) {
138         HDF_LOGE("%{public}s: write result failed", __func__);
139         return HDF_ERR_INVALID_PARAM;
140     }
141 
142     return HDF_SUCCESS;
143 }
144 
SampleStubDoubleTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const145 int32_t SampleServiceStub::SampleStubDoubleTypeTest(MessageParcel& data,
146     MessageParcel& reply, MessageOption& option) const
147 {
148     const double input = data.ReadDouble();
149     double output;
150 
151     int32_t ec = service.DoubleTypeTest(input, output);
152     if (ec != HDF_SUCCESS) {
153         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
154         return ec;
155     }
156 
157     if (!reply.WriteDouble(output)) {
158         HDF_LOGE("%{public}s: write result failed", __func__);
159         return HDF_ERR_INVALID_PARAM;
160     }
161 
162     return HDF_SUCCESS;
163 }
164 
SampleStubStringTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const165 int32_t SampleServiceStub::SampleStubStringTypeTest(MessageParcel& data,
166     MessageParcel& reply, MessageOption& option) const
167 {
168     const std::string input = data.ReadString();
169     std::string output;
170 
171     int32_t ec = service.StringTypeTest(input, output);
172     if (ec != HDF_SUCCESS) {
173         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
174         return ec;
175     }
176 
177     if (!reply.WriteString(output)) {
178         HDF_LOGE("%{public}s: write result failed", __func__);
179         return HDF_ERR_INVALID_PARAM;
180     }
181 
182     return HDF_SUCCESS;
183 }
184 
SampleStubUcharTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const185 int32_t SampleServiceStub::SampleStubUcharTypeTest(MessageParcel& data,
186     MessageParcel& reply, MessageOption& option) const
187 {
188     const uint8_t input = data.ReadUint8();
189     uint8_t output;
190 
191     int32_t ec = service.UcharTypeTest(input, output);
192     if (ec != HDF_SUCCESS) {
193         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
194         return ec;
195     }
196 
197     if (!reply.WriteUint8(output)) {
198         HDF_LOGE("%{public}s: write result failed", __func__);
199         return HDF_ERR_INVALID_PARAM;
200     }
201 
202     return HDF_SUCCESS;
203 }
204 
SampleStubUshortTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const205 int32_t SampleServiceStub::SampleStubUshortTypeTest(MessageParcel& data,
206     MessageParcel& reply, MessageOption& option) const
207 {
208     const uint16_t input = data.ReadUint16();
209     uint16_t output;
210 
211     int32_t ec = service.UshortTypeTest(input, output);
212     if (ec != HDF_SUCCESS) {
213         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
214         return ec;
215     }
216 
217     if (!reply.WriteUint16(output)) {
218         HDF_LOGE("%{public}s: write result failed", __func__);
219         return HDF_ERR_INVALID_PARAM;
220     }
221 
222     return HDF_SUCCESS;
223 }
224 
SampleStubUintTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const225 int32_t SampleServiceStub::SampleStubUintTypeTest(MessageParcel& data,
226     MessageParcel& reply, MessageOption& option) const
227 {
228     const uint32_t input = data.ReadUint32();
229     uint32_t output;
230 
231     int32_t ec = service.UintTypeTest(input, output);
232     if (ec != HDF_SUCCESS) {
233         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
234         return ec;
235     }
236 
237     if (!reply.WriteUint32(output)) {
238         HDF_LOGE("%{public}s: write result failed", __func__);
239         return HDF_ERR_INVALID_PARAM;
240     }
241 
242     return HDF_SUCCESS;
243 }
244 
SampleStubUlongTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const245 int32_t SampleServiceStub::SampleStubUlongTypeTest(MessageParcel& data,
246     MessageParcel& reply, MessageOption& option) const
247 {
248     const uint64_t input = data.ReadUint64();
249     uint64_t output;
250 
251     int32_t ec = service.UlongTypeTest(input, output);
252     if (ec != HDF_SUCCESS) {
253         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
254         return ec;
255     }
256 
257     if (!reply.WriteUint64(output)) {
258         HDF_LOGE("%{public}s: write result failed", __func__);
259         return HDF_ERR_INVALID_PARAM;
260     }
261 
262     return HDF_SUCCESS;
263 }
264 
SampleStubListTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const265 int32_t SampleServiceStub::SampleStubListTypeTest(MessageParcel& data,
266     MessageParcel& reply, MessageOption& option) const
267 {
268     const uint32_t inSize = data.ReadUint32();
269     std::list<int8_t> input;
270     std::list<int8_t> output;
271 
272     for (uint32_t i = 0; i < inSize; i++) {
273         input.push_back(data.ReadInt8());
274     }
275 
276     int32_t ec = service.ListTypeTest(input, output);
277     if (ec != HDF_SUCCESS) {
278         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
279         return ec;
280     }
281 
282     if (!reply.WriteUint32(output.size())) {
283         HDF_LOGE("%{public}s: write result size failed", __func__);
284         return HDF_ERR_INVALID_PARAM;
285     }
286 
287     for (auto it : output) {
288         if (!reply.WriteInt8(it)) {
289             HDF_LOGE("%{public}s: write result failed", __func__);
290             return HDF_ERR_INVALID_PARAM;
291         }
292     }
293 
294     return HDF_SUCCESS;
295 }
296 
SampleStubMapTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const297 int32_t SampleServiceStub::SampleStubMapTypeTest(MessageParcel& data,
298     MessageParcel& reply, MessageOption& option) const
299 {
300     const uint32_t inSize = data.ReadUint32();
301     std::map<int8_t, int8_t> input;
302     std::map<int8_t, int8_t> output;
303 
304     for (uint32_t i = 0; i < inSize; i++) {
305         int8_t key = data.ReadInt8();
306         int8_t val = data.ReadInt8();
307         input[key] = val;
308     }
309 
310     int32_t ec = service.MapTypeTest(input, output);
311     if (ec != HDF_SUCCESS) {
312         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
313         return ec;
314     }
315 
316     if (!reply.WriteUint32(output.size())) {
317         HDF_LOGE("%{public}s: write result size failed", __func__);
318         return HDF_ERR_INVALID_PARAM;
319     }
320 
321     for (auto it : output) {
322         if (!reply.WriteInt8(it.first) || !reply.WriteInt8(it.second)) {
323             HDF_LOGE("%{public}s: write result size failed", __func__);
324             return HDF_ERR_INVALID_PARAM;
325         }
326     }
327 
328     return HDF_SUCCESS;
329 }
330 
SampleStubArrayTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const331 int32_t SampleServiceStub::SampleStubArrayTypeTest(MessageParcel& data,
332     MessageParcel& reply, MessageOption& option) const
333 {
334     const uint32_t inSize = data.ReadUint32();
335     std::vector<int8_t> input;
336     std::vector<int8_t> output;
337 
338     for (uint32_t i = 0; i < inSize; i++) {
339         input.push_back(data.ReadInt8());
340     }
341 
342     int32_t ec = service.ArrayTypeTest(input, output);
343     if (ec != HDF_SUCCESS) {
344         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
345         return ec;
346     }
347 
348     if (!reply.WriteUint32(output.size())) {
349         HDF_LOGE("%{public}s: write result size failed", __func__);
350         return HDF_ERR_INVALID_PARAM;
351     }
352 
353     for (auto it : output) {
354         if (!reply.WriteInt8(it)) {
355             HDF_LOGE("%{public}s: write result failed", __func__);
356             return HDF_ERR_INVALID_PARAM;
357         }
358     }
359 
360     return HDF_SUCCESS;
361 }
362 
SampleStubStructTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const363 int32_t SampleServiceStub::SampleStubStructTypeTest(MessageParcel& data,
364     MessageParcel& reply, MessageOption& option) const
365 {
366     StructSample *input = (StructSample *)data.ReadBuffer(sizeof(StructSample));
367     if (input == nullptr) {
368         HDF_LOGE("%{public}s: read struct data failed", __func__);
369         return HDF_ERR_INVALID_PARAM;
370     }
371     StructSample output;
372 
373     int32_t ec = service.StructTypeTest(*input, output);
374     if (ec != HDF_SUCCESS) {
375         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
376         return ec;
377     }
378 
379     if (!reply.WriteBuffer((void *)&output, sizeof(StructSample))) {
380         HDF_LOGE("%{public}s: write output data failed", __func__);
381         return HDF_ERR_INVALID_PARAM;
382     }
383 
384     return HDF_SUCCESS;
385 }
386 
SampleStubEnumTypeTest(MessageParcel & data,MessageParcel & reply,MessageOption & option) const387 int32_t SampleServiceStub::SampleStubEnumTypeTest(MessageParcel& data,
388     MessageParcel& reply, MessageOption& option) const
389 {
390     EnumSample input = (EnumSample)data.ReadUint32();
391     EnumSample output;
392 
393     int32_t ec = service.EnumTypeTest(input, output);
394     if (ec != HDF_SUCCESS) {
395         HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec);
396         return ec;
397     }
398 
399     if (!reply.WriteUint32((uint32_t)output)) {
400         HDF_LOGE("%{public}s: write result failed", __func__);
401         return HDF_ERR_INVALID_PARAM;
402     }
403 
404     return HDF_SUCCESS;
405 }
406 
SampleServiceStubOnRemoteRequest(int cmdId,MessageParcel & data,MessageParcel & reply,MessageOption & option) const407 int32_t SampleServiceStub::SampleServiceStubOnRemoteRequest(int cmdId,
408     MessageParcel& data, MessageParcel& reply, MessageOption& option) const
409 {
410     switch (cmdId) {
411         case CMD_BOOLEAN_TYPE_TEST:
412             return SampleStubBooleanTypeTest(data, reply, option);
413         case CMD_BYTE_TYPE_TEST:
414             return SampleStubByteTypeTest(data, reply, option);
415         case CMD_SHORT_TYPE_TEST:
416             return SampleStubShortTypeTest(data, reply, option);
417         case CMD_INT_TYPE_TEST:
418             return SampleStubIntTypeTest(data, reply, option);
419         case CMD_LONG_TYPE_TEST:
420             return SampleStubLongTypeTest(data, reply, option);
421         case CMD_FLOAT_TYPE_TEST:
422             return SampleStubFloatTypeTest(data, reply, option);
423         case CMD_DOUBLE_TYPE_TEST:
424             return SampleStubDoubleTypeTest(data, reply, option);
425         case CMD_STRING_TYPE_TEST:
426             return SampleStubStringTypeTest(data, reply, option);
427         case CMD_UCHAR_TYPE_TEST:
428             return SampleStubUcharTypeTest(data, reply, option);
429         case CMD_USHORT_TYPE_TEST:
430             return SampleStubUshortTypeTest(data, reply, option);
431         case CMD_UINT_TYPE_TEST:
432             return SampleStubUintTypeTest(data, reply, option);
433         case CMD_ULONG_TYPE_TEST:
434             return SampleStubUlongTypeTest(data, reply, option);
435         case CMD_LIST_TYPE_TEST:
436             return SampleStubListTypeTest(data, reply, option);
437         case CMD_MAP_TYPE_TEST:
438             return SampleStubMapTypeTest(data, reply, option);
439         case CMD_ARRAY_TYPE_TEST:
440             return SampleStubArrayTypeTest(data, reply, option);
441         case CMD_STRUCT_TYPE_TEST:
442             return SampleStubStructTypeTest(data, reply, option);
443         case CMD_ENUM_TYPE_TEST:
444             return SampleStubEnumTypeTest(data, reply, option);
445         default: {
446             HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId);
447             return HDF_ERR_INVALID_PARAM;
448         }
449     }
450     return HDF_SUCCESS;
451 }
452 }  // namespace V1_0
453 }  // namespace Sample
454 }  // namespace HDI
455 }  // namespace OHOS
456 
SampleStubInstance()457 void *SampleStubInstance()
458 {
459     using namespace OHOS::HDI::Sample::V1_0;
460     return reinterpret_cast<void *>(new SampleServiceStub());
461 }
462 
SampleStubRelease(void * obj)463 void SampleStubRelease(void *obj)
464 {
465     using namespace OHOS::HDI::Sample::V1_0;
466     delete reinterpret_cast<SampleServiceStub *>(obj);
467 }
468 
SampleServiceOnRemoteRequest(void * stub,int cmdId,struct HdfSBuf & data,struct HdfSBuf & reply)469 int32_t SampleServiceOnRemoteRequest(void *stub, int cmdId, struct HdfSBuf& data, struct HdfSBuf& reply)
470 {
471     using namespace OHOS::HDI::Sample::V1_0;
472     SampleServiceStub *sampleStub = reinterpret_cast<SampleServiceStub *>(stub);
473     OHOS::MessageParcel *dataParcel = nullptr;
474     OHOS::MessageParcel *replyParcel = nullptr;
475 
476     (void)SbufToParcel(&reply, &replyParcel);
477     if (SbufToParcel(&data, &dataParcel) != HDF_SUCCESS) {
478         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
479         return HDF_ERR_INVALID_PARAM;
480     }
481 
482     OHOS::MessageOption option;
483     return sampleStub->SampleServiceStubOnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
484 }
485