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 #ifndef SAMPLE_SERVICE_HDF_H
17 #define SAMPLE_SERVICE_HDF_H
18
19 #include <hdf_sbuf.h>
20 #include <hdf_log.h>
21 #include <osal_mem.h>
22 #include <securec.h>
23 #include <hdi_smq_meta.h>
24
25 struct HdfDeviceObject;
26 struct HdfDeviceIoClient;
27
28 struct DataBlock {
29 int a;
30 int b;
31 const char *str;
32 int c;
33 };
34
35 struct SampleSmqElement {
36 int32_t data32;
37 uint64_t data64;
38 };
39
40 enum {
41 SAMPLE_SERVICE_PING = 0,
42 SAMPLE_SERVICE_SUM,
43 SAMPLE_SERVICE_CALLBACK,
44 SAMPLE_STRUCT_TRANS,
45 SAMPLE_BUFFER_TRANS,
46 SAMPLE_REGISTER_DEVICE,
47 SAMPLE_UNREGISTER_DEVICE,
48 SAMPLE_UPDATE_SERVIE,
49 SAMPLE_TRANS_SMQ,
50 };
51
52 struct SampleHdi {
53 int32_t (*ping)(struct HdfDeviceObject *device, const char* info, char** infoOut);
54 int32_t (*sum)(struct HdfDeviceObject *device, int32_t x0, int32_t x1, int32_t *result);
55 int32_t (*callback)(struct HdfDeviceObject *device, struct HdfRemoteService *callback, int32_t code);
56 int32_t (*registerDevice)(struct HdfDeviceObject *device, const char *servName);
57 int32_t (*unregisterDevice)(struct HdfDeviceObject *device, const char *servName);
58 int32_t (*updateService)(struct HdfDeviceObject *device, const char *info);
59 int32_t (*tansSmq)(struct HdfDeviceObject *device, OHOS::HDI::Base::SharedMemQueueMeta<SampleSmqElement> *smqMeta,
60 uint32_t element);
61 };
62
63 const struct SampleHdi *SampleHdiImplInstance(void);
64
65 int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId,
66 struct HdfSBuf *data, struct HdfSBuf *reply);
67
68
DataBlockFree(struct DataBlock * dataBlock)69 static inline void DataBlockFree(struct DataBlock *dataBlock)
70 {
71 if (dataBlock != nullptr) {
72 OsalMemFree((void *)dataBlock->str);
73 OsalMemFree(dataBlock);
74 }
75 }
76
DataBlockBlockUnmarshalling(struct HdfSBuf * data)77 static inline struct DataBlock *DataBlockBlockUnmarshalling(struct HdfSBuf *data)
78 {
79 const struct DataBlock *dataBlock_ = nullptr;
80 uint32_t readSize = 0;
81
82 if (!HdfSbufReadBuffer(data, (const void **)&dataBlock_, &readSize)) {
83 HDF_LOGE("%{public}s: failed to read dataBlock", __func__);
84 return nullptr;
85 }
86
87 if (readSize != sizeof(struct DataBlock)) {
88 HDF_LOGE("%{public}s: dataBlock size mismatch %{public}d", __func__, readSize);
89 return nullptr;
90 }
91
92 struct DataBlock *dataBlock = (struct DataBlock *)OsalMemAlloc(sizeof(struct DataBlock));
93 if (dataBlock == nullptr) {
94 return nullptr;
95 }
96 HDF_LOGD("%{public}s: DataBlock mem: %{public}d %{public}d %{public}d", __func__,
97 dataBlock_->a, dataBlock_->b, dataBlock_->c);
98 if (memcpy_s(dataBlock, sizeof(*dataBlock), dataBlock_, sizeof(*dataBlock)) != EOK) {
99 return nullptr;
100 }
101
102 const char *str = nullptr;
103 if (!HdfSbufReadBuffer(data, (const void **)&str, &readSize)) {
104 HDF_LOGE("%{public}s: failed to read dataBlock.str", __func__);
105 return nullptr;
106 }
107
108 dataBlock->str = strdup(str);
109 if (dataBlock->str == nullptr) {
110 OsalMemFree(dataBlock);
111 return nullptr;
112 }
113
114 return dataBlock;
115 }
116
DataBlockBlockMarshalling(struct DataBlock * dataBlock,struct HdfSBuf * data)117 static inline bool DataBlockBlockMarshalling(struct DataBlock *dataBlock, struct HdfSBuf *data)
118 {
119 if (!HdfSbufWriteBuffer(data, dataBlock, sizeof(struct DataBlock))) {
120 return false;
121 }
122
123 if (!HdfSbufWriteBuffer(data, dataBlock->str, strlen(dataBlock->str) + 1)) {
124 return false;
125 }
126
127 return true;
128 }
129
130 #endif // SAMPLE_SERVICE_HDF_H