• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 IPC_UTILS_FFI_H
17 #define IPC_UTILS_FFI_H
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 
23 #include "hilog/log.h"
24 
25 namespace OHOS {
26 const unsigned int LOG_ID_IPC_BASE = 0xD0057C0;
27 const unsigned int LOG_ID_IPC_FFI = LOG_ID_IPC_BASE | 0x08;
28 const int64_t INVALID_ID = 0;
29 const int32_t INVALID_REMOTE_TYPE = -1;
30 const int32_t REMOTE_OBJECT = 0;
31 const int32_t REMOTE_PROXY = 1;
32 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_FFI, "RPC_FFI" };
33 
34 constexpr size_t MAX_CAPACITY_TO_WRITE = 200 * 1024;
35 constexpr size_t MAX_BYTES_LENGTH = 40960;
36 constexpr size_t BYTE_SIZE_8 = 1;
37 constexpr size_t BYTE_SIZE_16 = 2;
38 constexpr size_t BYTE_SIZE_32 = 4;
39 constexpr size_t BYTE_SIZE_64 = 8;
40 
41 struct RequestResult {
42     int32_t errCode;
43     uint32_t code;
44     int64_t data;
45     int64_t reply;
46 };
47 
48 enum errorDesc {
49     CHECK_PARAM_ERROR = 401,
50     OS_MMAP_ERROR = 1900001,
51     OS_IOCTL_ERROR,
52     WRITE_TO_ASHMEM_ERROR,
53     READ_FROM_ASHMEM_ERROR,
54     ONLY_PROXY_OBJECT_PERMITTED_ERROR,
55     ONLY_REMOTE_OBJECT_PERMITTED_ERROR,
56     COMMUNICATION_ERROR,
57     PROXY_OR_REMOTE_OBJECT_INVALID_ERROR,
58     WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR,
59     READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR,
60     PARCEL_MEMORY_ALLOC_ERROR,
61     CALL_JS_METHOD_ERROR,
62     OS_DUP_ERROR
63 };
64 
65 enum TypeCode {
66     INT8_ARRAY = 0,
67     UINT8_ARRAY = 1,
68     INT16_ARRAY = 2,
69     UINT16_ARRAY = 3,
70     INT32_ARRAY = 4,
71     UINT32_ARRAY = 5,
72     FLOAT32_ARRAY = 6,
73     FLOAT64_ARRAY = 7,
74     BIGINT64_ARRAY = 8,
75     BIGUINT64_ARRAY = 9,
76 };
77 
78 typedef struct {
79     int8_t* data;
80     uint32_t len;
81 } CJByteArray;
82 
83 typedef struct {
84     int16_t* data;
85     uint32_t len;
86 } CJShortArray;
87 
88 typedef struct {
89     uint16_t* data;
90     uint32_t len;
91 } CJUInt16Array;
92 
93 typedef struct {
94     int32_t* data;
95     uint32_t len;
96 } CJIntArray;
97 
98 typedef struct {
99     uint32_t* data;
100     uint32_t len;
101 } CJUInt32Array;
102 
103 typedef struct {
104     int64_t* data;
105     uint32_t len;
106 } CJLongArray;
107 
108 typedef struct {
109     uint64_t* data;
110     uint32_t len;
111 } CJUInt64Array;
112 
113 typedef struct {
114     float* data;
115     uint32_t len;
116 } CJFloatArray;
117 
118 typedef struct {
119     double* data;
120     uint32_t len;
121 } CJDoubleArray;
122 
123 typedef struct {
124     uint8_t* data;
125     uint32_t len;
126 } CJCharArray;
127 
128 typedef struct {
129     char** data;
130     uint32_t len;
131 } CJStringArray;
132 
133 typedef struct {
134     int32_t* type;
135     int64_t* id;
136     uint32_t len;
137 } RemoteObjectArray;
138 
139 char* MallocCString(const std::string& origin);
140 
141 #define ZLOGF(LOG_LABEL, fmt, args...)                                                                            \
142     HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_LABEL.domain, LOG_LABEL.tag, "%{public}s %{public}d: " fmt, __FUNCTION__, \
143         __LINE__, ##args)
144 #define ZLOGE(LOG_LABEL, fmt, args...)                                                                            \
145     HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_LABEL.domain, LOG_LABEL.tag, "%{public}s %{public}d: " fmt, __FUNCTION__, \
146         __LINE__, ##args)
147 #define ZLOGW(LOG_LABEL, fmt, args...)                                                                           \
148     HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_LABEL.domain, LOG_LABEL.tag, "%{public}s %{public}d: " fmt, __FUNCTION__, \
149         __LINE__, ##args)
150 #define ZLOGI(LOG_LABEL, fmt, args...)                                                                           \
151     HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_LABEL.domain, LOG_LABEL.tag, "%{public}s %{public}d: " fmt, __FUNCTION__, \
152         __LINE__, ##args)
153 #define ZLOGD(LOG_LABEL, fmt, args...)                                                                            \
154     HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_LABEL.domain, LOG_LABEL.tag, "%{public}s %{public}d: " fmt, __FUNCTION__, \
155         __LINE__, ##args)
156 } // namespace OHOS
157 #endif