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