1 /* 2 * Copyright (c) 2020 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 _LITEIPC_ADAPTER_H 17 #define _LITEIPC_ADAPTER_H 18 19 #include <pthread.h> 20 #include <stdbool.h> 21 #include <stdint.h> 22 #include <unistd.h> 23 24 #include "serializer.h" 25 26 #ifdef __cplusplus 27 #if __cplusplus 28 extern "C" { 29 #endif /* __cplusplus */ 30 #endif /* __cplusplus */ 31 32 #define MMAP_DEFAULT_SIZE 65536UL /* 64KB */ 33 #define MMAP_MAX_SIZE 262144UL /* 256KB */ 34 #define MAX_DEATHCB_PER_SVC 5 35 #define IPC_WAIT_FOREVER 0XFFFFFFFF 36 #define IPC_TIMEOUT_MIN 10 /* unit:ms */ 37 #define IPC_INVAILD_TIMER_ID (timer_t)-1 38 #define IPC_IO_DATA_MAX 8192UL 39 40 typedef enum { 41 LITEIPC_FLAG_DEFAULT = 0, // send and reply 42 LITEIPC_FLAG_ONEWAY, // send message only 43 } IpcFlag; 44 45 typedef enum { 46 ONCE = 0, // Callback will be removed when timeout 47 RESET_AFTER_USE, // The interval will be refresh when the callback is called 48 } IpcCbMode; 49 50 enum IpcErr { 51 LITEIPC_DEAD_OBJECT = -32, /* Object is dead */ 52 LITEIPC_EINVAL = -10, /* Invalid argument */ 53 LITEIPC_EBADF, /* Bad file descriptor */ 54 LITEIPC_ENOMEM, /* No memory */ 55 LITEIPC_ENOENT, /* Service is not avaliable; For example, Transact on a timeout ipc callback */ 56 LITEIPC_EINTNL, /* Internal error, check strerrno in log */ 57 LITEIPC_OK = 0, 58 }; 59 60 enum { DEATH_CODE = 0 }; 61 62 typedef int32_t (*IpcMsgHandler)(const IpcContext* context, void* ipcMsg, IpcIo* data, void* arg); 63 64 #define LITEIPC_DEFAULT_MAP_SIZE 0X10000 65 66 IpcContext* OpenLiteIpc(size_t mmapSize); 67 void CloseLiteIpc(IpcContext* context); 68 69 /* thread-unsafe, cannot be repeatedly invoked */ 70 int32_t SetSaManager(const IpcContext* context, size_t maxMsgSize); 71 int32_t AddServiceAccess(SvcIdentity sid, pid_t pid); 72 int32_t GenServiceHandle(SvcIdentity* sid, pid_t tid); 73 74 int32_t StartLoop(const IpcContext* context, IpcMsgHandler func, void* arg); 75 int32_t GetToken(const void* ipcMsg, uint32_t* token); 76 int32_t GetCode(const void* ipcMsg, uint32_t* code); 77 int32_t GetFlag(const void* ipcMsg, uint32_t* flag); 78 pid_t GetCallingTid(const void* ipcMsg); 79 pid_t GetCallingPid(const void* ipcMsg); 80 pid_t GetCallingUid(const void* ipcMsg); 81 pid_t GetCallingGid(const void* ipcMsg); 82 83 #define Transact SendRequest 84 int32_t SendRequest(const IpcContext* context, SvcIdentity sid, uint32_t code, 85 IpcIo* data, IpcIo* reply, IpcFlag flag, uintptr_t* buffer); 86 int32_t SendReply(const IpcContext* context, void* ipcMsg, IpcIo* reply); 87 int32_t FreeBuffer(const IpcContext* context, void* ptr); 88 int32_t RegisterIpcCallback(IpcMsgHandler func, 89 uint32_t mode, 90 uint32_t timeoutMs, 91 SvcIdentity* sid, 92 void* arg); 93 int32_t UnregisterIpcCallback(SvcIdentity sid); 94 int32_t RegisterDeathCallback(const IpcContext* context, SvcIdentity sid, IpcMsgHandler func, void* arg, uint32_t* cbId); 95 int32_t UnregisterDeathCallback(SvcIdentity sid, uint32_t cbId); 96 int32_t BinderAcquire(const IpcContext* context, uint32_t handle); 97 int32_t BinderRelease(const IpcContext* context, uint32_t handle); 98 #ifdef __cplusplus 99 #if __cplusplus 100 } 101 #endif /* __cplusplus */ 102 #endif /* __cplusplus */ 103 104 #endif 105