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