diff --git a/interfaces/innerkits/c/ipc/BUILD.gn b/interfaces/innerkits/c/ipc/BUILD.gn index eea8a19b88ba025ea2b83f485596b0500ffe9db2..024c9c15c54b3dd396d6abd7f6fd51211e21af9c 100644 --- a/interfaces/innerkits/c/ipc/BUILD.gn +++ b/interfaces/innerkits/c/ipc/BUILD.gn @@ -17,10 +17,7 @@ SUBSYSTEM_DIR = "//foundation/communication/ipc" IPC_CORE_ROOT = "$SUBSYSTEM_DIR/ipc/native/c" config("ipc_rpc_interface") { - include_dirs = [ - "$SUBSYSTEM_DIR/interfaces/innerkits/c/ipc/include", - "$SUBSYSTEM_DIR/interfaces/innerkits/c/dbinder/include", - ] + include_dirs = [ "$SUBSYSTEM_DIR/interfaces/innerkits/c/ipc/include" ] } if (ohos_kernel_type == "liteos_m") { @@ -31,28 +28,62 @@ if (ohos_kernel_type == "liteos_m") { "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits" } +ipc_common_include = [ + "$IPC_CORE_ROOT/ipc/include", + "$IPC_CORE_ROOT/manager/include", + "//third_party/bounds_checking_function/include", + "//utils/native/lite/include", + "$hilog_lite_include_path", +] + +ipc_common_src = [ + "$IPC_CORE_ROOT/manager/src/ipc_process_skeleton.c", + "$IPC_CORE_ROOT/manager/src/ipc_skeleton.c", + "$IPC_CORE_ROOT/manager/src/ipc_thread_pool.c", + "$IPC_CORE_ROOT/manager/src/iremote_invoker.c", + "$IPC_CORE_ROOT/manager/src/serializer.c", +] + if (ohos_kernel_type == "liteos_m") { static_library("ipc_single") { - sources = [ + sources = ipc_common_src + sources += [ "$IPC_CORE_ROOT/ipc/src/binder_invoker_virtual.c", - "$IPC_CORE_ROOT/manager/src/ipc_process_skeleton.c", - "$IPC_CORE_ROOT/manager/src/ipc_skeleton.c", - "$IPC_CORE_ROOT/manager/src/ipc_thread_pool.c", - "$IPC_CORE_ROOT/manager/src/iremote_invoker.c", "$IPC_CORE_ROOT/manager/src/rpc_log.c", - "$IPC_CORE_ROOT/manager/src/serializer.c", ] + include_dirs = ipc_common_include public_configs = [ ":ipc_rpc_interface" ] + deps = [ "//foundation/communication/ipc/ipc/native/c/adapter:rpc_adapter" ] + } +} else if (ohos_kernel_type == "liteos_a") { + shared_library("rpc_log") { + sources = [ "$IPC_CORE_ROOT/manager/src/rpc_log.c" ] include_dirs = [ - "$IPC_CORE_ROOT/ipc/include", - "$IPC_CORE_ROOT/rpc/include", "$IPC_CORE_ROOT/manager/include", - "$SUBSYSTEM_DIR/services/dbinder/c/include", - "//third_party/bounds_checking_function/include", - "//utils/native/lite/include", "$hilog_lite_include_path", ] - deps = [ "//foundation/communication/ipc/ipc/native/c/adapter:rpc_adapter" ] + public_deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//third_party/bounds_checking_function/:libsec_shared", + ] + } + + shared_library("ipc_single") { + sources = ipc_common_src + sources += [ "$IPC_CORE_ROOT/ipc/src/binder_invoker_virtual.c" ] + public_configs = [ ":ipc_rpc_interface" ] + include_dirs = ipc_common_include + ldflags = [ + "-lstdc++", + "-lpthread", + ] + cflags = [ "-fPIC" ] + cflags_cc = cflags + public_deps = [ + ":rpc_log", + "//foundation/communication/ipc/ipc/native/c/adapter:rpc_adapter", + "//third_party/bounds_checking_function/:libsec_shared", + ] } } else { shared_library("rpc_log") { @@ -68,34 +99,21 @@ if (ohos_kernel_type == "liteos_m") { } shared_library("ipc_single") { - sources = [ - "$IPC_CORE_ROOT/ipc/src/binder_invoker.c", - "$IPC_CORE_ROOT/manager/src/ipc_process_skeleton.c", - "$IPC_CORE_ROOT/manager/src/ipc_skeleton.c", - "$IPC_CORE_ROOT/manager/src/ipc_thread_pool.c", - "$IPC_CORE_ROOT/manager/src/iremote_invoker.c", - "$IPC_CORE_ROOT/manager/src/serializer.c", - ] + sources = ipc_common_src + sources += [ "$IPC_CORE_ROOT/ipc/src/binder_invoker.c" ] public_configs = [ ":ipc_rpc_interface" ] - include_dirs = [ - "$IPC_CORE_ROOT/ipc/include", - "$IPC_CORE_ROOT/rpc/include", - "$IPC_CORE_ROOT/manager/include", - "$SUBSYSTEM_DIR/services/dbinder/c/include", - "//third_party/bounds_checking_function/include", - "//utils/native/lite/include", - ] + include_dirs = ipc_common_include ldflags = [ "-lstdc++", "-lpthread", - "-fPIC", ] + cflags = [ "-fPIC" ] + cflags_cc = cflags public_deps = [ ":rpc_log", "//foundation/communication/ipc/ipc/native/c/adapter:rpc_adapter", "//third_party/bounds_checking_function/:libsec_shared", ] - configs -= [ "//build/lite/config:clang_opt" ] } } diff --git a/interfaces/innerkits/c/ipc/include/serializer.h b/interfaces/innerkits/c/ipc/include/serializer.h index 1c86cea77717a6407a4715d98260fd194a0e6af0..5582b8225048991c3295c80adaa2278aef2bb5ec 100644 --- a/interfaces/innerkits/c/ipc/include/serializer.h +++ b/interfaces/innerkits/c/ipc/include/serializer.h @@ -27,17 +27,17 @@ extern "C" { #endif /* __cplusplus */ typedef struct { - char* bufferBase; - size_t* offsetsBase; - char* bufferCur; - size_t* offsetsCur; + char *bufferBase; + size_t *offsetsBase; + char *bufferCur; + size_t *offsetsCur; size_t bufferLeft; size_t offsetsLeft; uint32_t flag; } IpcIo; typedef struct { - int32_t handle; // 是否按原ipc格式改成联合体 + int32_t handle; uintptr_t token; uintptr_t cookie; } SvcIdentity; @@ -46,61 +46,13 @@ typedef struct { #define IPC_IO_INITIALIZED 0x01 /* ipc flag indicates whether io is initialized */ #define IPC_IO_OVERFLOW 0x02 /* ipc flag indicates whether io is running out of space */ -/* allocate a IpcIo, providing a stack-allocated working - * buffer, size of the working buffer, and how many object - * offset entries to reserve from the buffer - */ -void IpcIoInit(IpcIo* io, void* data, size_t maxdata, size_t maxobjects); -/* Must ensure all the input is valid */ -void IpcIoPushChar(IpcIo* io, char c); -void IpcIoPushCharUnaligned(IpcIo* io, char c); -void IpcIoPushBool(IpcIo* io, bool b); -void IpcIoPushBoolUnaligned(IpcIo* io, bool b); -void IpcIoPushIntptr(IpcIo* io, intptr_t n); -void IpcIoPushUintptr(IpcIo* io, uintptr_t n); -void IpcIoPushInt8(IpcIo* io, int8_t n); -void IpcIoPushInt8Unaligned(IpcIo* io, int8_t n); -void IpcIoPushUint8(IpcIo* io, uint8_t n); -void IpcIoPushUint8Unaligned(IpcIo* io, uint8_t n); -void IpcIoPushInt16(IpcIo* io, int16_t n); -void IpcIoPushInt16Unaligned(IpcIo* io, int16_t n); -void IpcIoPushUint16(IpcIo* io, uint16_t n); -void IpcIoPushUint16Unaligned(IpcIo* io, uint16_t n); -void IpcIoPushInt32(IpcIo* io, int32_t n); -void IpcIoPushUint32(IpcIo* io, uint32_t n); -void IpcIoPushInt64(IpcIo* io, int64_t n); -void IpcIoPushUint64(IpcIo* io, uint64_t n); -void IpcIoPushFloat(IpcIo* io, float n); -void IpcIoPushDouble(IpcIo* io, double n); -void IpcIoPushString(IpcIo* io, const char* cstr); -void IpcIoPushFlatObj(IpcIo* io, const void* obj, uint32_t size); -bool IpcIoPushSvc(IpcIo* io, const SvcIdentity* svc); -bool IpcIoPushFd(IpcIo* io, uint32_t fd); +void IpcIoInit(IpcIo* io, void* buffer, size_t bufferSize, size_t maxobjects); + +bool WriteRemoteObject(IpcIo *io, const SvcIdentity *svc); +bool WriteFileDescriptor(IpcIo *io, uint32_t fd); -char IpcIoPopChar(IpcIo* io); -char IpcIoPopCharUnaligned(IpcIo* io); -bool IpcIoPopBool(IpcIo* io); -bool IpcIoPopBoolUnaligned(IpcIo* io); -intptr_t IpcIoPopIntptr(IpcIo* io); -uintptr_t IpcIoPopUintptr(IpcIo* io); -int8_t IpcIoPopInt8(IpcIo* io); -int8_t IpcIoPopInt8Unaligned(IpcIo* io); -uint8_t IpcIoPopUint8(IpcIo* io); -uint8_t IpcIoPopUint8Unaligned(IpcIo* io); -int16_t IpcIoPopInt16(IpcIo* io); -int16_t IpcIoPopInt16Unaligned(IpcIo* io); -uint16_t IpcIoPopUint16(IpcIo* io); -uint16_t IpcIoPopUint16Unaligned(IpcIo* io); -int32_t IpcIoPopInt32(IpcIo* io); -uint32_t IpcIoPopUint32(IpcIo* io); -int64_t IpcIoPopInt64(IpcIo* io); -uint64_t IpcIoPopUint64(IpcIo* io); -float IpcIoPopFloat(IpcIo* io); -double IpcIoPopDouble(IpcIo* io); -uint8_t* IpcIoPopString(IpcIo* io, size_t* sz); -void* IpcIoPopFlatObj(IpcIo* io, uint32_t* size); -bool IpcIoPopSvc(IpcIo* io, SvcIdentity* svc); -int32_t IpcIoPopFd(IpcIo* io); +bool ReadRemoteObject(IpcIo *io, SvcIdentity *svc); +int32_t ReadFileDescriptor(IpcIo *io); #ifdef __cplusplus #if __cplusplus diff --git a/ipc/native/c/adapter/Linux/rpc_os_adapter.c b/ipc/native/c/adapter/Linux/rpc_os_adapter.c index 9214d260134e70aff5fe616ef265a18fe749fadf..34c7bc974b618b7737ea9f510970a1b1df315ff3 100644 --- a/ipc/native/c/adapter/Linux/rpc_os_adapter.c +++ b/ipc/native/c/adapter/Linux/rpc_os_adapter.c @@ -23,5 +23,5 @@ int32_t RpcGetPid(void) int32_t RpcGetUid() { - return (int32_t)getuid(void); + return (int32_t)getuid(); } \ No newline at end of file diff --git a/ipc/native/c/ipc/src/binder_invoker.c b/ipc/native/c/ipc/src/binder_invoker.c index 34636d653120d40fca5fc20c29cfb0f38062dbc9..0b48cc66a2330873ecfa26156ec05ec2c7f1fca1 100644 --- a/ipc/native/c/ipc/src/binder_invoker.c +++ b/ipc/native/c/ipc/src/binder_invoker.c @@ -211,7 +211,7 @@ static int32_t SendReply(IpcIo *reply, const int32_t *status) static void HandleTransaction(const struct binder_transaction_data *tr) { - ThreadContext *threadContext = GetCurrentThreadContext(); // sender pid, uid作用域范围 + ThreadContext *threadContext = GetCurrentThreadContext(); const pid_t oldPid = threadContext->callerPid; const pid_t oldUid = threadContext->callerUid; threadContext->callerPid = tr->sender_pid; diff --git a/ipc/native/c/manager/include/ipc_thread_pool.h b/ipc/native/c/manager/include/ipc_thread_pool.h index 6d3a2a63be3ebf78ba514a116aca9ab594ca0135..5ae73d865c64e2f8831bc005257285b5d3d31b67 100644 --- a/ipc/native/c/manager/include/ipc_thread_pool.h +++ b/ipc/native/c/manager/include/ipc_thread_pool.h @@ -64,8 +64,7 @@ int32_t SpawnNewThread(ThreadPool *threadPool, int32_t policy, int32_t proto); ThreadContext *GetCurrentThreadContext(void); -// get or update invoker -RemoteInvoker *GetRemoteInvoker(int32_t proto); +RemoteInvoker *GetRemoteInvoker(void); void UpdateMaxThreadNum(ThreadPool *threadPool, int32_t maxThreadNum); diff --git a/ipc/native/c/manager/include/iremote_invoker.h b/ipc/native/c/manager/include/iremote_invoker.h index 7e9c8e81a27bc48df4b56325da3e55bed8c2a7a8..d2daa4448d47612ad77697b9c4ea55dbb26cdb27 100644 --- a/ipc/native/c/manager/include/iremote_invoker.h +++ b/ipc/native/c/manager/include/iremote_invoker.h @@ -43,8 +43,6 @@ typedef struct { } RemoteInvoker; RemoteInvoker *InitRemoteInvoker(int32_t proto); - -void DeleteInvoker(RemoteInvoker *invoker, int32_t proto); #ifdef __cplusplus #if __cplusplus } diff --git a/ipc/native/c/manager/src/ipc_process_skeleton.c b/ipc/native/c/manager/src/ipc_process_skeleton.c index 02dbd165315b65e76737f3ad838873814e22d93c..2cd04b8b5c6af0c5e770b4e8404d0016b386e21a 100644 --- a/ipc/native/c/manager/src/ipc_process_skeleton.c +++ b/ipc/native/c/manager/src/ipc_process_skeleton.c @@ -50,7 +50,7 @@ static IpcSkeleton* IpcProcessSkeleton() RPC_LOG_ERROR("init thread pool failed."); return NULL; } - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker == NULL) { DeleteIpcSkeleton(temp); RPC_LOG_ERROR("get remote invoker failed."); @@ -104,7 +104,7 @@ int32_t SetMaxWorkThread(int32_t maxThreadNum) } UpdateMaxThreadNum(g_ipcSkeleton->threadPool, maxThreadNum); - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { return (invoker->SetMaxWorkThread)(maxThreadNum); } @@ -114,7 +114,7 @@ int32_t SetMaxWorkThread(int32_t maxThreadNum) void JoinMainWorkThread(void) { - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { (invoker->JoinThread)(true); } @@ -146,7 +146,7 @@ const SvcIdentity *GetRegistryObject(void) int32_t SetRegistryObject(SvcIdentity target) { int32_t ret = ERR_THREAD_INVOKER_NOT_INIT; - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { ret = (invoker->SetRegistryObject)(); } @@ -177,7 +177,7 @@ static void DeleteDeadHandle(int32_t handle) } pthread_mutex_unlock(&g_ipcSkeleton->lock); if (isValidHandle) { - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { (invoker->ReleaseHandle)(handle); } @@ -188,7 +188,7 @@ int32_t ProcessSendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo MessageOption option, uintptr_t *buffer) { int32_t ret = ERR_THREAD_INVOKER_NOT_INIT; - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { ret = (invoker->SendRequest)(target, code, data, reply, option, buffer); } @@ -201,7 +201,7 @@ int32_t ProcessSendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo int32_t ProcessFreeBuffer(void *ptr) { - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { return (invoker->FreeBuffer)(ptr); } @@ -247,7 +247,7 @@ static void OnFirstStrongRef(int32_t handle) return; } if (FirstAddObject(handle)) { - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { (invoker->AcquireHandle)(handle); } @@ -309,7 +309,7 @@ int32_t ProcessAddDeathRecipient(int32_t handle, OnRemoteDead deathFunc, void *a pthread_mutex_unlock(&g_ipcSkeleton->lock); if (firstDeathNode) { RPC_LOG_ERROR("first add death callback for handle = %d.", handle); - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { ret = (invoker->AddDeathRecipient)(handle, node); } @@ -379,7 +379,7 @@ void SendObituary(DeathCallback *deathCallback) } pthread_mutex_unlock(&deathCallback->lock); if (deathNum > 0) { - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { (invoker->RemoveDeathRecipient)(deathCallback->handle, deathCallback); } @@ -404,7 +404,7 @@ void DeleteDeathCallback(DeathCallback *deathCallback) RPC_LOG_ERROR("invalid death node"); return; } - RemoteInvoker *invoker = GetRemoteInvoker(IF_PROT_DEFAULT); + RemoteInvoker *invoker = GetRemoteInvoker(); if (invoker != NULL) { (invoker->ReleaseHandle)(deathCallback->handle); } diff --git a/ipc/native/c/manager/src/ipc_thread_pool.c b/ipc/native/c/manager/src/ipc_thread_pool.c index 24b3c571de48dadcf2da27ccb2508b477f6ebda4..c901be3e67eed4d6027915ecd22f9e9f6ab03c5b 100644 --- a/ipc/native/c/manager/src/ipc_thread_pool.c +++ b/ipc/native/c/manager/src/ipc_thread_pool.c @@ -30,6 +30,26 @@ static pthread_key_t g_localKey = -1; static RemoteInvoker *g_invoker[PROTO_NUM]; +ThreadContext *GetCurrentThreadContext(void) +{ + ThreadContext *current = NULL; + void *curTLS = pthread_getspecific(g_localKey); + if (curTLS != NULL) { + current = (ThreadContext *)curTLS; + } else { + current = (ThreadContext *)calloc(1, sizeof(ThreadContext)); + if (current == NULL) { + return NULL; + } + current->threadId = pthread_self(); + current->proto = IF_PROT_DEFAULT; + current->callerPid = RpcGetPid(); + current->callerUid = RpcGetUid(); + pthread_setspecific(g_localKey, current); + } + return current; +} + static void TlsDestructor(void *args) { RPC_LOG_INFO("thread exit, call tls destructor"); @@ -53,6 +73,16 @@ static void ThreadContextDestructor(int32_t proto) pthread_mutex_unlock(&threadPool->lock); } +static RemoteInvoker *GetAndUpdateInvoker(int32_t proto) +{ + ThreadContext *threadContext = GetCurrentThreadContext(); + if (threadContext == NULL) { + return NULL; + } + threadContext->proto = proto; + return g_invoker[proto]; +} + static void *ThreadHandler(void *args) { ThreadContext *threadContext = (ThreadContext *)args; @@ -60,7 +90,7 @@ static void *ThreadHandler(void *args) int32_t policy = threadContext->policy; free(threadContext); threadContext = NULL; - RemoteInvoker *invoker = GetRemoteInvoker(proto); + RemoteInvoker *invoker = GetAndUpdateInvoker(proto); if (invoker != NULL) { switch (policy) { case SPAWN_PASSIVE: @@ -103,7 +133,7 @@ void DeinitThreadPool(ThreadPool *threadPool) pthread_key_delete(g_localKey); free(threadPool); for (int32_t index = 0; index < PROTO_NUM; ++index) { - DeleteInvoker(g_invoker[index], index); + g_invoker[index] = NULL; } } @@ -144,26 +174,6 @@ int32_t SpawnNewThread(ThreadPool *threadPool, int32_t policy, int32_t proto) return ERR_NONE; } -ThreadContext *GetCurrentThreadContext(void) -{ - ThreadContext *current = NULL; - void *curTLS = pthread_getspecific(g_localKey); - if (curTLS != NULL) { - current = (ThreadContext *)curTLS; - } else { - current = (ThreadContext *)calloc(1, sizeof(ThreadContext)); - if (current == NULL) { - return NULL; - } - current->threadId = pthread_self(); - current->proto = IF_PROT_DEFAULT; - current->callerPid = RpcGetPid(); - current->callerUid = RpcGetUid(); - pthread_setspecific(g_localKey, current); - } - return current; -} - void UpdateMaxThreadNum(ThreadPool *threadPool, int32_t maxThreadNum) { int32_t totalNum = maxThreadNum + maxThreadNum; @@ -183,11 +193,11 @@ void UpdateMaxThreadNum(ThreadPool *threadPool, int32_t maxThreadNum) pthread_mutex_unlock(&threadPool->lock); } -RemoteInvoker *GetRemoteInvoker(int32_t proto) +RemoteInvoker *GetRemoteInvoker(void) { ThreadContext *threadContext = GetCurrentThreadContext(); if (threadContext == NULL) { return NULL; } - return g_invoker[proto]; + return g_invoker[threadContext->proto]; } \ No newline at end of file diff --git a/ipc/native/c/manager/src/iremote_invoker.c b/ipc/native/c/manager/src/iremote_invoker.c index 7757a973a9bad5e3ecc1bc7f2617d771f90c68e7..ff852c488f65b37735648207fbce780fc0c50bd8 100644 --- a/ipc/native/c/manager/src/iremote_invoker.c +++ b/ipc/native/c/manager/src/iremote_invoker.c @@ -25,14 +25,4 @@ RemoteInvoker *InitRemoteInvoker(int32_t proto) remoteInvoker = GetIpcInvoker(); } return remoteInvoker; -} - -void DeleteInvoker(RemoteInvoker *remoteInvoker, int32_t proto) -{ - if (remoteInvoker == NULL) { - return; - } - if (proto == IF_PROT_BINDER) { - remoteInvoker = NULL; - } } \ No newline at end of file diff --git a/ipc/native/c/manager/src/serializer.c b/ipc/native/c/manager/src/serializer.c index c19711b941fd52862c26004ed015f63cfd383076..a3edcff6cdebc06b4ffbe62bf8027fccc7bf4c46 100644 --- a/ipc/native/c/manager/src/serializer.c +++ b/ipc/native/c/manager/src/serializer.c @@ -14,18 +14,18 @@ */ #include "serializer.h" -#include "securec.h" + #include #include #ifdef __LINUX__ #include "sys_binder.h" #endif -#include "rpc_log.h" #include "ipc_process_skeleton.h" +#include "rpc_log.h" +#include "securec.h" #define MAX_IO_SIZE 8192UL #define MAX_OBJ_NUM 32UL -#define MAX_DATA_BUFF_SIZE 65536UL #define ALIGN_SZ 4 #define IPC_IO_ALIGN(sz) (((sz) + ALIGN_SZ - 1) & (~(ALIGN_SZ - 1))) @@ -41,7 +41,6 @@ } \ } while (0) -static void* IoPop(IpcIo* io, size_t size); void IpcIoInit(IpcIo* io, void* buffer, size_t bufferSize, size_t maxobjects) { @@ -91,197 +90,6 @@ static void* IoPush(IpcIo* io, size_t size) } } -static void* IoPushUnaligned(IpcIo* io, size_t size) -{ - IPC_IO_RETURN_IF_FAIL(io != NULL); - IPC_IO_RETURN_IF_FAIL(IpcIoAvailable(io)); - if (size > io->bufferLeft) { - io->flag |= IPC_IO_OVERFLOW; - return NULL; - } else { - void* ptr = io->bufferCur; - io->bufferCur += size; - io->bufferLeft -= size; - return ptr; - } -} - -void IpcIoPushInt32(IpcIo* io, int32_t n) -{ - int32_t* ptr = (int32_t*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushUint32(IpcIo* io, uint32_t n) -{ - uint32_t* ptr = (uint32_t*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushChar(IpcIo* io, char c) -{ - IpcIoPushInt32(io, (int32_t)c); -} - -void IpcIoPushCharUnaligned(IpcIo* io, char c) -{ - char* ptr = (char*)IoPushUnaligned(io, sizeof(c)); - if (ptr != NULL) { - *ptr = c; - } -} - -void IpcIoPushBool(IpcIo* io, bool b) -{ - IpcIoPushInt32(io, (int32_t)b); -} - -void IpcIoPushBoolUnaligned(IpcIo* io, bool b) -{ - bool* ptr = (bool*)IoPushUnaligned(io, sizeof(b)); - if (ptr != NULL) { - *ptr = b; - } -} - -void IpcIoPushIntptr(IpcIo* io, intptr_t intptr) -{ - intptr_t* ptr = (intptr_t*)IoPush(io, sizeof(intptr)); - if (ptr != NULL) { - *ptr = intptr; - } -} - -void IpcIoPushUintptr(IpcIo* io, uintptr_t uintptr) -{ - uintptr_t* ptr = (uintptr_t*)IoPush(io, sizeof(uintptr)); - if (ptr != NULL) { - *ptr = uintptr; - } -} - -void IpcIoPushInt8(IpcIo* io, int8_t n) -{ - IpcIoPushInt32(io, (int32_t)n); -} - -void IpcIoPushInt8Unaligned(IpcIo* io, int8_t n) -{ - int8_t* ptr = (int8_t*)IoPushUnaligned(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushUint8(IpcIo* io, uint8_t n) -{ - IpcIoPushUint32(io, (uint32_t)n); -} - -void IpcIoPushUint8Unaligned(IpcIo* io, uint8_t n) -{ - uint8_t* ptr = (uint8_t*)IoPushUnaligned(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushInt16(IpcIo* io, int16_t n) -{ - IpcIoPushInt32(io, (int32_t)n); -} - -void IpcIoPushInt16Unaligned(IpcIo* io, int16_t n) -{ - int16_t* ptr = (int16_t*)IoPushUnaligned(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushUint16(IpcIo* io, uint16_t n) -{ - IpcIoPushUint32(io, (uint32_t)n); -} - -void IpcIoPushUint16Unaligned(IpcIo* io, uint16_t n) -{ - uint16_t* ptr = (uint16_t*)IoPushUnaligned(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushInt64(IpcIo* io, int64_t n) -{ - int64_t* ptr = (int64_t*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushUint64(IpcIo* io, uint64_t n) -{ - uint64_t* ptr = (uint64_t*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushFloat(IpcIo* io, float n) -{ - float* ptr = (float*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushDouble(IpcIo* io, double n) -{ - double* ptr = (double*)IoPush(io, sizeof(n)); - if (ptr != NULL) { - *ptr = n; - } -} - -void IpcIoPushString(IpcIo* io, const char* cstr) -{ - unsigned char* str = (unsigned char*)cstr; - size_t len; - uint8_t* ptr = NULL; - - if (io == NULL) { - return; - } - if (str == NULL) { - io->flag |= IPC_IO_OVERFLOW; - return; - } - - len = strlen(cstr); - if (len == MAX_IO_SIZE) { - io->flag |= IPC_IO_OVERFLOW; - return; - } - - /* Note: The payload will carry 32bit size instead of size_t */ - IpcIoPushUint32(io, (uint32_t)len); - ptr = (uint8_t*)IoPush(io, len + 1); - if (ptr != NULL) { - if (memset_s(ptr, IPC_IO_ALIGN(len + 1), 0, IPC_IO_ALIGN(len + 1)) != EOK) { - io->flag |= IPC_IO_OVERFLOW; - return; - } - if (memcpy_s(ptr, IPC_IO_ALIGN(len + 1), str, len + 1) != EOK) { - io->flag |= IPC_IO_OVERFLOW; - } - } -} - static void* IoPop(IpcIo* io, size_t size) { IPC_IO_RETURN_IF_FAIL(io != NULL); @@ -300,186 +108,6 @@ static void* IoPop(IpcIo* io, size_t size) } } -static void* IoPopUnaligned(IpcIo* io, size_t size) -{ - IPC_IO_RETURN_IF_FAIL(io != NULL); - IPC_IO_RETURN_IF_FAIL(IpcIoAvailable(io)); - - if (io->bufferLeft < size) { - io->bufferLeft = 0; - io->flag |= IPC_IO_OVERFLOW; - return NULL; - } else { - void* ptr = io->bufferCur; - io->bufferCur += size; - io->bufferLeft -= size; - return ptr; - } -} - -char IpcIoPopChar(IpcIo* io) -{ - char* ptr = (char*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -char IpcIoPopCharUnaligned(IpcIo* io) -{ - char* ptr = (char*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -bool IpcIoPopBool(IpcIo* io) -{ - bool* ptr = (bool*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -bool IpcIoPopBoolUnaligned(IpcIo* io) -{ - bool* ptr = (bool*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -intptr_t IpcIoPopIntptr(IpcIo* io) -{ - intptr_t* ptr = (intptr_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uintptr_t IpcIoPopUintptr(IpcIo* io) -{ - uintptr_t* ptr = (uintptr_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int8_t IpcIoPopInt8(IpcIo* io) -{ - int8_t* ptr = (int8_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int8_t IpcIoPopInt8Unaligned(IpcIo* io) -{ - int8_t* ptr = (int8_t*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint8_t IpcIoPopUint8(IpcIo* io) -{ - uint8_t* ptr = (uint8_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint8_t IpcIoPopUint8Unaligned(IpcIo* io) -{ - uint8_t* ptr = (uint8_t*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int16_t IpcIoPopInt16(IpcIo* io) -{ - int16_t* ptr = (int16_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int16_t IpcIoPopInt16Unaligned(IpcIo* io) -{ - int16_t* ptr = (int16_t*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint16_t IpcIoPopUint16(IpcIo* io) -{ - uint16_t* ptr = (uint16_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint16_t IpcIoPopUint16Unaligned(IpcIo* io) -{ - uint16_t* ptr = (uint16_t*)IoPopUnaligned(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int32_t IpcIoPopInt32(IpcIo* io) -{ - int32_t* ptr = (int32_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint32_t IpcIoPopUint32(IpcIo* io) -{ - uint32_t* ptr = (uint32_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -int64_t IpcIoPopInt64(IpcIo* io) -{ - int64_t* ptr = (int64_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint64_t IpcIoPopUint64(IpcIo* io) -{ - uint64_t* ptr = (uint64_t*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -float IpcIoPopFloat(IpcIo* io) -{ - float* ptr = (float*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -double IpcIoPopDouble(IpcIo* io) -{ - double* ptr = (double*)IoPop(io, sizeof(*ptr)); - return ptr ? *ptr : 0; -} - -uint8_t* IpcIoPopString(IpcIo* io, size_t* sz) -{ - size_t len; - - /* Note: The payload will carry 32bit size instead of size_t */ - len = (size_t)IpcIoPopUint32(io); - if (sz != NULL) { - *sz = len; - } - if (len > MAX_IO_SIZE) { - return NULL; - } - return (uint8_t*)IoPop(io, len + 1); -} - -void* IpcIoPopFlatObj(IpcIo* io, uint32_t* size) -{ - IPC_IO_RETURN_IF_FAIL(size != NULL); - *size = (size_t)IpcIoPopUint32(io); - if (*size > MAX_IO_SIZE) { - return NULL; - } - return (void*)IoPop(io, *size); -} - -void IpcIoPushFlatObj(IpcIo* io, const void* obj, uint32_t size) -{ - if (io == NULL) { - return; - } - if ((obj == NULL) || (size == 0) || (size > MAX_IO_SIZE)) { - io->flag |= IPC_IO_OVERFLOW; - return; - } - IpcIoPushUint32(io, size); - int32_t* ptr = (int32_t*)IoPush(io, size); - if (ptr != NULL) { - if (memcpy_s(ptr, size, obj, size) != EOK) { - io->flag |= IPC_IO_OVERFLOW; - } - } -} - #ifdef __LINUX__ static struct flat_binder_object* IoPushBinderObj(IpcIo* io) { @@ -543,7 +171,7 @@ struct flat_binder_object* IpcIoPopRef(IpcIo *io) return NULL; } -bool IpcIoPushSvc(IpcIo* io, const SvcIdentity* svc) +bool WriteRemoteObject(IpcIo *io, const SvcIdentity *svc) { if (io == NULL || svc == NULL) { RPC_LOG_ERROR("push io or svc is NULL ..."); @@ -558,14 +186,14 @@ bool IpcIoPushSvc(IpcIo* io, const SvcIdentity* svc) return res; } -bool IpcIoPopSvc(IpcIo* io, SvcIdentity *svc) +bool ReadRemoteObject(IpcIo *io, SvcIdentity *svc) { if (io == NULL || svc == NULL) { return false; } struct flat_binder_object* obj = IpcIoPopRef(io); if (obj == NULL) { - RPC_LOG_ERROR("IpcIoPopSvc failed: obj is null"); + RPC_LOG_ERROR("ReadRemoteObject failed: obj is null"); return false; } if (obj->type == BINDER_TYPE_BINDER) { @@ -580,13 +208,13 @@ bool IpcIoPopSvc(IpcIo* io, SvcIdentity *svc) return true; } -bool IpcIoPushFd(IpcIo* io, uint32_t fd) +bool WriteFileDescriptor(IpcIo *io, uint32_t fd) { if (io == NULL) { RPC_LOG_ERROR("push fd io is NULL."); return false; } - struct flat_binder_object* ptr = IoPushBinderObj(io); + struct flat_binder_object *ptr = IoPushBinderObj(io); if (ptr == NULL) { RPC_LOG_ERROR("Io push fd IPC_IO_OVERFLOW.\n"); return false; @@ -599,47 +227,67 @@ bool IpcIoPushFd(IpcIo* io, uint32_t fd) return true; } -int32_t IpcIoPopFd(IpcIo* io) +int32_t ReadFileDescriptor(IpcIo *io) { if (io == NULL) { return -1; } - struct flat_binder_object* obj = IpcIoPopRef(io); + struct flat_binder_object *obj = IpcIoPopRef(io); if (obj == NULL) { - RPC_LOG_ERROR("IpcIoPopFd failed: obj is null"); + RPC_LOG_ERROR("ReadFileDescriptor failed: obj is null"); return -1; } - if (obj && obj->type == BINDER_TYPE_FD) { + if (obj->type == BINDER_TYPE_FD) { return obj->handle; } - RPC_LOG_ERROR("IpcIoPopFd failed: type:%d", obj->type); + RPC_LOG_ERROR("ReadFileDescriptor failed: type:%d", obj->type); return -1; } + +bool IpcIoPushSvc(IpcIo *io, const SvcIdentity *svc) +{ + return false; +} + +bool IpcIoPopSvc(IpcIo *io, SvcIdentity *svc) +{ + return false; +} #else -bool IpcIoPushSvc(IpcIo* io, const SvcIdentity* svc) +bool WriteRemoteObject(IpcIo *io, const SvcIdentity *svc) { (void)io; (void)svc; return false; } -bool IpcIoPushFd(IpcIo* io, uint32_t fd) +bool WriteFileDescriptor(IpcIo* io, uint32_t fd) { (void)io; (void)fd; return false; } -bool IpcIoPopSvc(IpcIo* io, SvcIdentity* svc) +bool ReadRemoteObject(IpcIo* io, SvcIdentity* svc) { (void)io; (void)svc; return false; } -int32_t IpcIoPopFd(IpcIo* io) +int32_t ReadFileDescriptor(IpcIo* io) { (void)io; return -1; } + +bool IpcIoPushSvc(IpcIo* io, const SvcIdentity* svc) +{ + return; +} + +bool IpcIoPopSvc(IpcIo* io, SvcIdentity* svc) +{ + return; +} #endif \ No newline at end of file