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 SAMPLE_END_HOST,
51 };
52
53 struct SampleHdi {
54 int32_t (*ping)(struct HdfDeviceObject *device, const char* info, char** infoOut);
55 int32_t (*sum)(struct HdfDeviceObject *device, int32_t x0, int32_t x1, int32_t *result);
56 int32_t (*callback)(struct HdfDeviceObject *device, struct HdfRemoteService *callback, int32_t code);
57 int32_t (*registerDevice)(struct HdfDeviceObject *device, const char *servName);
58 int32_t (*unregisterDevice)(struct HdfDeviceObject *device, const char *servName);
59 int32_t (*updateService)(struct HdfDeviceObject *device, const char *info);
60 int32_t (*tansSmq)(struct HdfDeviceObject *device, OHOS::HDI::Base::SharedMemQueueMeta<SampleSmqElement> *smqMeta,
61 uint32_t element);
62 int32_t (*endHost)(const struct HdfDeviceObject *device);
63 };
64
65 const struct SampleHdi *SampleHdiImplInstance(void);
66
67 int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId,
68 struct HdfSBuf *data, struct HdfSBuf *reply);
69
70
DataBlockFree(struct DataBlock * dataBlock)71 static inline void DataBlockFree(struct DataBlock *dataBlock)
72 {
73 if (dataBlock != nullptr) {
74 OsalMemFree(const_cast<void *>(static_cast<const void *>(dataBlock->str)));
75 OsalMemFree(dataBlock);
76 }
77 }
78
DataBlockBlockUnmarshalling(struct HdfSBuf * data)79 static inline struct DataBlock *DataBlockBlockUnmarshalling(struct HdfSBuf *data)
80 {
81 const struct DataBlock *block = nullptr;
82 uint32_t readSize = 0;
83
84 if (!HdfSbufReadBuffer(data, reinterpret_cast<const void **>(&block), &readSize)) {
85 HDF_LOGE("%{public}s: failed to read dataBlock", __func__);
86 return nullptr;
87 }
88
89 if (readSize != sizeof(struct DataBlock)) {
90 HDF_LOGE("%{public}s: dataBlock size mismatch %{public}d", __func__, readSize);
91 return nullptr;
92 }
93
94 struct DataBlock *dataBlock = static_cast<struct DataBlock *>(OsalMemAlloc(sizeof(*dataBlock)));
95 if (dataBlock == nullptr) {
96 return nullptr;
97 }
98 HDF_LOGD("%{public}s: DataBlock mem: %{public}d %{public}d %{public}d", __func__, block->a, block->b, block->c);
99 if (memcpy_s(dataBlock, sizeof(*dataBlock), block, sizeof(*dataBlock)) != EOK) {
100 return nullptr;
101 }
102
103 const char *str = nullptr;
104 if (!HdfSbufReadBuffer(data, reinterpret_cast<const void **>(&str), &readSize)) {
105 HDF_LOGE("%{public}s: failed to read dataBlock.str", __func__);
106 return nullptr;
107 }
108
109 dataBlock->str = strdup(str);
110 if (dataBlock->str == nullptr) {
111 OsalMemFree(dataBlock);
112 return nullptr;
113 }
114
115 return dataBlock;
116 }
117
DataBlockBlockMarshalling(const struct DataBlock * dataBlock,struct HdfSBuf * data)118 static inline bool DataBlockBlockMarshalling(const struct DataBlock *dataBlock, struct HdfSBuf *data)
119 {
120 if (!HdfSbufWriteBuffer(data, dataBlock, sizeof(struct DataBlock))) {
121 return false;
122 }
123
124 if (!HdfSbufWriteBuffer(data, dataBlock->str, strlen(dataBlock->str) + 1)) {
125 return false;
126 }
127
128 return true;
129 }
130
131 #endif // SAMPLE_SERVICE_HDF_H