117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#include "ipc_adapt.h"
1717fd14ceSopenharmony_ci#include "common_defs.h"
1817fd14ceSopenharmony_ci#include "device_auth_defines.h"
1917fd14ceSopenharmony_ci#include "hc_log.h"
2017fd14ceSopenharmony_ci#include "hc_types.h"
2117fd14ceSopenharmony_ci#include "hc_mutex.h"
2217fd14ceSopenharmony_ci#include "ipc_callback_stub.h"
2317fd14ceSopenharmony_ci#include "ipc_dev_auth_proxy.h"
2417fd14ceSopenharmony_ci#include "ipc_dev_auth_stub.h"
2517fd14ceSopenharmony_ci#include "ipc_sdk.h"
2617fd14ceSopenharmony_ci#include "ipc_service.h"
2717fd14ceSopenharmony_ci#include "ipc_skeleton.h"
2817fd14ceSopenharmony_ci#include "securec.h"
2917fd14ceSopenharmony_ci
3017fd14ceSopenharmony_ci#ifdef __cplusplus
3117fd14ceSopenharmony_ciextern "C" {
3217fd14ceSopenharmony_ci#endif
3317fd14ceSopenharmony_ci
3417fd14ceSopenharmony_ci#define BUFF_MAX_SZ 128
3517fd14ceSopenharmony_ci#define IPC_CALL_BACK_MAX_NODES 64
3617fd14ceSopenharmony_ci
3717fd14ceSopenharmony_citypedef struct {
3817fd14ceSopenharmony_ci    uintptr_t cbHook;
3917fd14ceSopenharmony_ci    const IpcDataInfo *cbDataCache;
4017fd14ceSopenharmony_ci    int32_t cacheNum;
4117fd14ceSopenharmony_ci    IpcIo *reply;
4217fd14ceSopenharmony_ci} CallbackParams;
4317fd14ceSopenharmony_ci
4417fd14ceSopenharmony_citypedef void (*CallbackStub)(CallbackParams params);
4517fd14ceSopenharmony_citypedef struct {
4617fd14ceSopenharmony_ci    union {
4717fd14ceSopenharmony_ci        DeviceAuthCallback devAuth;
4817fd14ceSopenharmony_ci        DataChangeListener listener;
4917fd14ceSopenharmony_ci    } cbCtx;
5017fd14ceSopenharmony_ci    int64_t requestId;
5117fd14ceSopenharmony_ci    char appId[BUFF_MAX_SZ];
5217fd14ceSopenharmony_ci    int32_t cbType;
5317fd14ceSopenharmony_ci    int32_t delOnFni;
5417fd14ceSopenharmony_ci    int32_t methodId;
5517fd14ceSopenharmony_ci    int32_t proxyId;
5617fd14ceSopenharmony_ci    int32_t nodeIdx;
5717fd14ceSopenharmony_ci} IpcCallBackNode;
5817fd14ceSopenharmony_ci
5917fd14ceSopenharmony_cistatic struct {
6017fd14ceSopenharmony_ci    IpcCallBackNode *ctx;
6117fd14ceSopenharmony_ci    int32_t nodeCnt;
6217fd14ceSopenharmony_ci} g_ipcCallBackList = {NULL, 0};
6317fd14ceSopenharmony_cistatic HcMutex g_cbListLock;
6417fd14ceSopenharmony_ci
6517fd14ceSopenharmony_cistatic StubDevAuthCb g_sdkCbStub;
6617fd14ceSopenharmony_cistatic IClientProxy *g_proxyInstance = NULL;
6717fd14ceSopenharmony_cistatic IpcObjectStub g_objectStub;
6817fd14ceSopenharmony_ci
6917fd14ceSopenharmony_cistatic void SetIpcCallBackNodeDefault(IpcCallBackNode *node)
7017fd14ceSopenharmony_ci{
7117fd14ceSopenharmony_ci    (void)memset_s(node, sizeof(IpcCallBackNode), 0, sizeof(IpcCallBackNode));
7217fd14ceSopenharmony_ci    node->proxyId = -1;
7317fd14ceSopenharmony_ci    node->nodeIdx = -1;
7417fd14ceSopenharmony_ci    return;
7517fd14ceSopenharmony_ci}
7617fd14ceSopenharmony_ci
7717fd14ceSopenharmony_ciint32_t InitIpcCallBackList(void)
7817fd14ceSopenharmony_ci{
7917fd14ceSopenharmony_ci    int32_t i;
8017fd14ceSopenharmony_ci
8117fd14ceSopenharmony_ci    LOGI("initializing ...");
8217fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx != NULL) {
8317fd14ceSopenharmony_ci        LOGI("has initialized");
8417fd14ceSopenharmony_ci        return HC_SUCCESS;
8517fd14ceSopenharmony_ci    }
8617fd14ceSopenharmony_ci
8717fd14ceSopenharmony_ci    g_ipcCallBackList.ctx = HcMalloc(sizeof(IpcCallBackNode) * IPC_CALL_BACK_MAX_NODES, 0);
8817fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
8917fd14ceSopenharmony_ci        LOGE("initialized failed");
9017fd14ceSopenharmony_ci        return HC_ERROR;
9117fd14ceSopenharmony_ci    }
9217fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
9317fd14ceSopenharmony_ci        SetIpcCallBackNodeDefault(g_ipcCallBackList.ctx + i);
9417fd14ceSopenharmony_ci    }
9517fd14ceSopenharmony_ci    (void)InitHcMutex(&g_cbListLock);
9617fd14ceSopenharmony_ci    g_ipcCallBackList.nodeCnt = 0;
9717fd14ceSopenharmony_ci    LOGI("initialized successful");
9817fd14ceSopenharmony_ci    return HC_SUCCESS;
9917fd14ceSopenharmony_ci}
10017fd14ceSopenharmony_ci
10117fd14ceSopenharmony_cistatic void ResetIpcCallBackNode(IpcCallBackNode *node)
10217fd14ceSopenharmony_ci{
10317fd14ceSopenharmony_ci    ResetRemoteObject(node->proxyId);
10417fd14ceSopenharmony_ci    SetIpcCallBackNodeDefault(node);
10517fd14ceSopenharmony_ci    return;
10617fd14ceSopenharmony_ci}
10717fd14ceSopenharmony_ci
10817fd14ceSopenharmony_cistatic void LockCallbackList(void)
10917fd14ceSopenharmony_ci{
11017fd14ceSopenharmony_ci    (void)LockHcMutex(&g_cbListLock);
11117fd14ceSopenharmony_ci    return;
11217fd14ceSopenharmony_ci}
11317fd14ceSopenharmony_ci
11417fd14ceSopenharmony_cistatic void UnLockCallbackList(void)
11517fd14ceSopenharmony_ci{
11617fd14ceSopenharmony_ci    UnlockHcMutex(&g_cbListLock);
11717fd14ceSopenharmony_ci    return;
11817fd14ceSopenharmony_ci}
11917fd14ceSopenharmony_ci
12017fd14ceSopenharmony_civoid DeInitIpcCallBackList(void)
12117fd14ceSopenharmony_ci{
12217fd14ceSopenharmony_ci    int32_t i;
12317fd14ceSopenharmony_ci
12417fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
12517fd14ceSopenharmony_ci        return;
12617fd14ceSopenharmony_ci    }
12717fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
12817fd14ceSopenharmony_ci        ResetIpcCallBackNode(g_ipcCallBackList.ctx + i);
12917fd14ceSopenharmony_ci    }
13017fd14ceSopenharmony_ci    HcFree((void *)g_ipcCallBackList.ctx);
13117fd14ceSopenharmony_ci    g_ipcCallBackList.ctx = NULL;
13217fd14ceSopenharmony_ci    DestroyHcMutex(&g_cbListLock);
13317fd14ceSopenharmony_ci    return;
13417fd14ceSopenharmony_ci}
13517fd14ceSopenharmony_ci
13617fd14ceSopenharmony_civoid ResetIpcCallBackNodeByNodeId(int32_t nodeIdx)
13717fd14ceSopenharmony_ci{
13817fd14ceSopenharmony_ci    LOGI("starting..., index %d", nodeIdx);
13917fd14ceSopenharmony_ci    if ((nodeIdx < 0) || (nodeIdx >= IPC_CALL_BACK_MAX_NODES)) {
14017fd14ceSopenharmony_ci        return;
14117fd14ceSopenharmony_ci    }
14217fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
14317fd14ceSopenharmony_ci        return;
14417fd14ceSopenharmony_ci    }
14517fd14ceSopenharmony_ci    LockCallbackList();
14617fd14ceSopenharmony_ci    ResetIpcCallBackNode(g_ipcCallBackList.ctx + nodeIdx);
14717fd14ceSopenharmony_ci    UnLockCallbackList();
14817fd14ceSopenharmony_ci    LOGI("done, index %d", nodeIdx);
14917fd14ceSopenharmony_ci    return;
15017fd14ceSopenharmony_ci}
15117fd14ceSopenharmony_ci
15217fd14ceSopenharmony_cistatic IpcCallBackNode *GetIpcCallBackByAppId(const char *appId, int32_t type)
15317fd14ceSopenharmony_ci{
15417fd14ceSopenharmony_ci    int32_t i;
15517fd14ceSopenharmony_ci    int32_t ret;
15617fd14ceSopenharmony_ci
15717fd14ceSopenharmony_ci    LOGI("appid: %s", appId);
15817fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
15917fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].appId[0] == 0) {
16017fd14ceSopenharmony_ci            continue;
16117fd14ceSopenharmony_ci        }
16217fd14ceSopenharmony_ci        ret = strcmp(g_ipcCallBackList.ctx[i].appId, appId);
16317fd14ceSopenharmony_ci        if ((ret == 0) && (g_ipcCallBackList.ctx[i].cbType == type)) {
16417fd14ceSopenharmony_ci            return &g_ipcCallBackList.ctx[i];
16517fd14ceSopenharmony_ci        }
16617fd14ceSopenharmony_ci    }
16717fd14ceSopenharmony_ci    return NULL;
16817fd14ceSopenharmony_ci}
16917fd14ceSopenharmony_ci
17017fd14ceSopenharmony_cistatic IpcCallBackNode *GetFreeIpcCallBackNode(void)
17117fd14ceSopenharmony_ci{
17217fd14ceSopenharmony_ci    int32_t i;
17317fd14ceSopenharmony_ci
17417fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
17517fd14ceSopenharmony_ci        if ((g_ipcCallBackList.ctx[i].appId[0] == 0) && (g_ipcCallBackList.ctx[i].cbType == 0)) {
17617fd14ceSopenharmony_ci            g_ipcCallBackList.ctx[i].nodeIdx = i;
17717fd14ceSopenharmony_ci            return &g_ipcCallBackList.ctx[i];
17817fd14ceSopenharmony_ci        }
17917fd14ceSopenharmony_ci    }
18017fd14ceSopenharmony_ci    return NULL;
18117fd14ceSopenharmony_ci}
18217fd14ceSopenharmony_ci
18317fd14ceSopenharmony_cistatic void SetCbDeathRecipient(int32_t type, int32_t objIdx, int32_t cbDataIdx)
18417fd14ceSopenharmony_ci{
18517fd14ceSopenharmony_ci    if ((type == CB_TYPE_DEV_AUTH) || (type == CB_TYPE_LISTENER)) {
18617fd14ceSopenharmony_ci        AddCbDeathRecipient(objIdx, cbDataIdx);
18717fd14ceSopenharmony_ci    }
18817fd14ceSopenharmony_ci    return;
18917fd14ceSopenharmony_ci}
19017fd14ceSopenharmony_ci
19117fd14ceSopenharmony_civoid AddIpcCbObjByAppId(const char *appId, int32_t objIdx, int32_t type)
19217fd14ceSopenharmony_ci{
19317fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
19417fd14ceSopenharmony_ci
19517fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
19617fd14ceSopenharmony_ci        LOGE("list not inited");
19717fd14ceSopenharmony_ci        return;
19817fd14ceSopenharmony_ci    }
19917fd14ceSopenharmony_ci
20017fd14ceSopenharmony_ci    LockCallbackList();
20117fd14ceSopenharmony_ci    if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
20217fd14ceSopenharmony_ci        UnLockCallbackList();
20317fd14ceSopenharmony_ci        LOGE("list is full");
20417fd14ceSopenharmony_ci        return;
20517fd14ceSopenharmony_ci    }
20617fd14ceSopenharmony_ci
20717fd14ceSopenharmony_ci    node = GetIpcCallBackByAppId(appId, type);
20817fd14ceSopenharmony_ci    if (node != NULL) {
20917fd14ceSopenharmony_ci        node->proxyId = objIdx;
21017fd14ceSopenharmony_ci        SetCbDeathRecipient(type, objIdx, node->nodeIdx);
21117fd14ceSopenharmony_ci        LOGI("ipc object add success, appid: %s, proxyId %d", appId, node->proxyId);
21217fd14ceSopenharmony_ci    }
21317fd14ceSopenharmony_ci    UnLockCallbackList();
21417fd14ceSopenharmony_ci    return;
21517fd14ceSopenharmony_ci}
21617fd14ceSopenharmony_ci
21717fd14ceSopenharmony_ciint32_t AddIpcCallBackByAppId(const char *appId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
21817fd14ceSopenharmony_ci{
21917fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
22017fd14ceSopenharmony_ci        LOGE("list not inited");
22117fd14ceSopenharmony_ci        return HC_ERROR;
22217fd14ceSopenharmony_ci    }
22317fd14ceSopenharmony_ci
22417fd14ceSopenharmony_ci    LockCallbackList();
22517fd14ceSopenharmony_ci    if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
22617fd14ceSopenharmony_ci        UnLockCallbackList();
22717fd14ceSopenharmony_ci        LOGE("list is full");
22817fd14ceSopenharmony_ci        return HC_ERROR;
22917fd14ceSopenharmony_ci    }
23017fd14ceSopenharmony_ci
23117fd14ceSopenharmony_ci    IpcCallBackNode *node = GetIpcCallBackByAppId(appId, type);
23217fd14ceSopenharmony_ci    if (node != NULL) {
23317fd14ceSopenharmony_ci        if (memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz) != EOK) {
23417fd14ceSopenharmony_ci            UnLockCallbackList();
23517fd14ceSopenharmony_ci            LOGE("callback context memory copy failed");
23617fd14ceSopenharmony_ci            return HC_ERR_MEMORY_COPY;
23717fd14ceSopenharmony_ci        }
23817fd14ceSopenharmony_ci        if (node->proxyId >= 0) {
23917fd14ceSopenharmony_ci            ResetRemoteObject(node->proxyId);
24017fd14ceSopenharmony_ci            node->proxyId = -1;
24117fd14ceSopenharmony_ci        }
24217fd14ceSopenharmony_ci        UnLockCallbackList();
24317fd14ceSopenharmony_ci        return HC_SUCCESS;
24417fd14ceSopenharmony_ci    }
24517fd14ceSopenharmony_ci
24617fd14ceSopenharmony_ci    node = GetFreeIpcCallBackNode();
24717fd14ceSopenharmony_ci    if (node == NULL) {
24817fd14ceSopenharmony_ci        UnLockCallbackList();
24917fd14ceSopenharmony_ci        LOGE("get free node failed");
25017fd14ceSopenharmony_ci        return HC_ERROR;
25117fd14ceSopenharmony_ci    }
25217fd14ceSopenharmony_ci    node->cbType = type;
25317fd14ceSopenharmony_ci    if (memcpy_s(&(node->appId), sizeof(node->appId), appId, HcStrlen(appId) + 1) != EOK) {
25417fd14ceSopenharmony_ci        ResetIpcCallBackNode(node);
25517fd14ceSopenharmony_ci        UnLockCallbackList();
25617fd14ceSopenharmony_ci        LOGE("appid memory copy failed");
25717fd14ceSopenharmony_ci        return HC_ERROR;
25817fd14ceSopenharmony_ci    }
25917fd14ceSopenharmony_ci    if (memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz) != EOK) {
26017fd14ceSopenharmony_ci        ResetIpcCallBackNode(node);
26117fd14ceSopenharmony_ci        UnLockCallbackList();
26217fd14ceSopenharmony_ci        LOGE("callback context memory copy failed");
26317fd14ceSopenharmony_ci        return HC_ERROR;
26417fd14ceSopenharmony_ci    }
26517fd14ceSopenharmony_ci    node->proxyId = -1;
26617fd14ceSopenharmony_ci    g_ipcCallBackList.nodeCnt++;
26717fd14ceSopenharmony_ci    UnLockCallbackList();
26817fd14ceSopenharmony_ci    return HC_SUCCESS;
26917fd14ceSopenharmony_ci}
27017fd14ceSopenharmony_ci
27117fd14ceSopenharmony_civoid DelIpcCallBackByAppId(const char *appId, int32_t type)
27217fd14ceSopenharmony_ci{
27317fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
27417fd14ceSopenharmony_ci
27517fd14ceSopenharmony_ci    if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == NULL)) {
27617fd14ceSopenharmony_ci        return;
27717fd14ceSopenharmony_ci    }
27817fd14ceSopenharmony_ci
27917fd14ceSopenharmony_ci    LockCallbackList();
28017fd14ceSopenharmony_ci    node = GetIpcCallBackByAppId(appId, type);
28117fd14ceSopenharmony_ci    if (node != NULL) {
28217fd14ceSopenharmony_ci        ResetIpcCallBackNode(node);
28317fd14ceSopenharmony_ci        g_ipcCallBackList.nodeCnt--;
28417fd14ceSopenharmony_ci    }
28517fd14ceSopenharmony_ci    UnLockCallbackList();
28617fd14ceSopenharmony_ci    return;
28717fd14ceSopenharmony_ci}
28817fd14ceSopenharmony_ci
28917fd14ceSopenharmony_cistatic IpcCallBackNode *GetIpcCallBackByReqId(int64_t reqId, int32_t type)
29017fd14ceSopenharmony_ci{
29117fd14ceSopenharmony_ci    int32_t i;
29217fd14ceSopenharmony_ci
29317fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
29417fd14ceSopenharmony_ci        if ((reqId == g_ipcCallBackList.ctx[i].requestId) &&
29517fd14ceSopenharmony_ci            (g_ipcCallBackList.ctx[i].cbType == type)) {
29617fd14ceSopenharmony_ci            return &g_ipcCallBackList.ctx[i];
29717fd14ceSopenharmony_ci        }
29817fd14ceSopenharmony_ci    }
29917fd14ceSopenharmony_ci    return NULL;
30017fd14ceSopenharmony_ci}
30117fd14ceSopenharmony_ci
30217fd14ceSopenharmony_ciint32_t AddReqIdByAppId(const char *appId, int64_t reqId)
30317fd14ceSopenharmony_ci{
30417fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
30517fd14ceSopenharmony_ci
30617fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
30717fd14ceSopenharmony_ci        LOGE("ipc callback list not inited");
30817fd14ceSopenharmony_ci        return HC_ERROR;
30917fd14ceSopenharmony_ci    }
31017fd14ceSopenharmony_ci
31117fd14ceSopenharmony_ci    LockCallbackList();
31217fd14ceSopenharmony_ci    node = GetIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
31317fd14ceSopenharmony_ci    if (node == NULL) {
31417fd14ceSopenharmony_ci        UnLockCallbackList();
31517fd14ceSopenharmony_ci        LOGE("ipc callback node not found, appid: %s", appId);
31617fd14ceSopenharmony_ci        return HC_ERROR;
31717fd14ceSopenharmony_ci    }
31817fd14ceSopenharmony_ci    node->requestId = reqId;
31917fd14ceSopenharmony_ci    node->delOnFni = 0;
32017fd14ceSopenharmony_ci    UnLockCallbackList();
32117fd14ceSopenharmony_ci    LOGI("success, appid: %s, requestId: %lld", appId, reqId);
32217fd14ceSopenharmony_ci    return HC_SUCCESS;
32317fd14ceSopenharmony_ci}
32417fd14ceSopenharmony_ci
32517fd14ceSopenharmony_civoid AddIpcCbObjByReqId(int64_t reqId, int32_t objIdx, int32_t type)
32617fd14ceSopenharmony_ci{
32717fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
32817fd14ceSopenharmony_ci
32917fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
33017fd14ceSopenharmony_ci        LOGE("list not inited");
33117fd14ceSopenharmony_ci        return;
33217fd14ceSopenharmony_ci    }
33317fd14ceSopenharmony_ci
33417fd14ceSopenharmony_ci    LockCallbackList();
33517fd14ceSopenharmony_ci    if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
33617fd14ceSopenharmony_ci        UnLockCallbackList();
33717fd14ceSopenharmony_ci        LOGE("list is full");
33817fd14ceSopenharmony_ci        return;
33917fd14ceSopenharmony_ci    }
34017fd14ceSopenharmony_ci
34117fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(reqId, type);
34217fd14ceSopenharmony_ci    if (node != NULL) {
34317fd14ceSopenharmony_ci        node->proxyId = objIdx;
34417fd14ceSopenharmony_ci        LOGI("ipc object add success, request id %lld, type %d, proxy id %d",
34517fd14ceSopenharmony_ci            reqId, type, node->proxyId);
34617fd14ceSopenharmony_ci    }
34717fd14ceSopenharmony_ci    UnLockCallbackList();
34817fd14ceSopenharmony_ci    return;
34917fd14ceSopenharmony_ci}
35017fd14ceSopenharmony_ci
35117fd14ceSopenharmony_ciint32_t AddIpcCallBackByReqId(int64_t reqId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
35217fd14ceSopenharmony_ci{
35317fd14ceSopenharmony_ci    errno_t eno;
35417fd14ceSopenharmony_ci
35517fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
35617fd14ceSopenharmony_ci        LOGE("list is full");
35717fd14ceSopenharmony_ci        return HC_ERROR;
35817fd14ceSopenharmony_ci    }
35917fd14ceSopenharmony_ci
36017fd14ceSopenharmony_ci    LockCallbackList();
36117fd14ceSopenharmony_ci    if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
36217fd14ceSopenharmony_ci        UnLockCallbackList();
36317fd14ceSopenharmony_ci        LOGE("list is full");
36417fd14ceSopenharmony_ci        return HC_ERROR;
36517fd14ceSopenharmony_ci    }
36617fd14ceSopenharmony_ci
36717fd14ceSopenharmony_ci    IpcCallBackNode *node = GetIpcCallBackByReqId(reqId, type);
36817fd14ceSopenharmony_ci    if (node != NULL) {
36917fd14ceSopenharmony_ci        eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
37017fd14ceSopenharmony_ci        if (eno != EOK) {
37117fd14ceSopenharmony_ci            UnLockCallbackList();
37217fd14ceSopenharmony_ci            LOGE("callback context memory copy failed");
37317fd14ceSopenharmony_ci            return HC_ERROR;
37417fd14ceSopenharmony_ci        }
37517fd14ceSopenharmony_ci        if (node->proxyId >= 0) {
37617fd14ceSopenharmony_ci            ResetRemoteObject(node->proxyId);
37717fd14ceSopenharmony_ci            node->proxyId = -1;
37817fd14ceSopenharmony_ci        }
37917fd14ceSopenharmony_ci        UnLockCallbackList();
38017fd14ceSopenharmony_ci        return HC_SUCCESS;
38117fd14ceSopenharmony_ci    }
38217fd14ceSopenharmony_ci
38317fd14ceSopenharmony_ci    node = GetFreeIpcCallBackNode();
38417fd14ceSopenharmony_ci    if (node == NULL) {
38517fd14ceSopenharmony_ci        UnLockCallbackList();
38617fd14ceSopenharmony_ci        LOGE("get free node failed");
38717fd14ceSopenharmony_ci        return HC_ERROR;
38817fd14ceSopenharmony_ci    }
38917fd14ceSopenharmony_ci    node->cbType = type;
39017fd14ceSopenharmony_ci    node->requestId = reqId;
39117fd14ceSopenharmony_ci    eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
39217fd14ceSopenharmony_ci    if (eno != EOK) {
39317fd14ceSopenharmony_ci        UnLockCallbackList();
39417fd14ceSopenharmony_ci        ResetIpcCallBackNode(node);
39517fd14ceSopenharmony_ci        LOGE("callback context memory copy failed");
39617fd14ceSopenharmony_ci        return HC_ERROR;
39717fd14ceSopenharmony_ci    }
39817fd14ceSopenharmony_ci    node->delOnFni = 1;
39917fd14ceSopenharmony_ci    node->proxyId = -1;
40017fd14ceSopenharmony_ci    g_ipcCallBackList.nodeCnt++;
40117fd14ceSopenharmony_ci    UnLockCallbackList();
40217fd14ceSopenharmony_ci    return HC_SUCCESS;
40317fd14ceSopenharmony_ci}
40417fd14ceSopenharmony_ci
40517fd14ceSopenharmony_cistatic void DelCallBackByReqId(int64_t reqId, int32_t type)
40617fd14ceSopenharmony_ci{
40717fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
40817fd14ceSopenharmony_ci
40917fd14ceSopenharmony_ci    if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == NULL)) {
41017fd14ceSopenharmony_ci        return;
41117fd14ceSopenharmony_ci    }
41217fd14ceSopenharmony_ci
41317fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(reqId, type);
41417fd14ceSopenharmony_ci    if ((node != NULL) && (node->delOnFni == 1)) {
41517fd14ceSopenharmony_ci        ResetIpcCallBackNode(node);
41617fd14ceSopenharmony_ci        g_ipcCallBackList.nodeCnt--;
41717fd14ceSopenharmony_ci    }
41817fd14ceSopenharmony_ci    return;
41917fd14ceSopenharmony_ci}
42017fd14ceSopenharmony_ci
42117fd14ceSopenharmony_civoid DelIpcCallBackByReqId(int64_t reqId, int32_t type, bool withLock)
42217fd14ceSopenharmony_ci{
42317fd14ceSopenharmony_ci    if (withLock) {
42417fd14ceSopenharmony_ci        LockCallbackList();
42517fd14ceSopenharmony_ci        DelCallBackByReqId(reqId, type);
42617fd14ceSopenharmony_ci        UnLockCallbackList();
42717fd14ceSopenharmony_ci        return;
42817fd14ceSopenharmony_ci    }
42917fd14ceSopenharmony_ci    DelCallBackByReqId(reqId, type);
43017fd14ceSopenharmony_ci    return;
43117fd14ceSopenharmony_ci}
43217fd14ceSopenharmony_ci
43317fd14ceSopenharmony_cistatic void OnTransmitStub(CallbackParams params)
43417fd14ceSopenharmony_ci{
43517fd14ceSopenharmony_ci    int64_t requestId = 0;
43617fd14ceSopenharmony_ci    int32_t inOutLen = sizeof(requestId);
43717fd14ceSopenharmony_ci    uint8_t *data = NULL;
43817fd14ceSopenharmony_ci    uint32_t dataLen = 0u;
43917fd14ceSopenharmony_ci    bool bRet = false;
44017fd14ceSopenharmony_ci    bool (*onTransmitHook)(int64_t, uint8_t *, uint32_t) = (bool (*)(int64_t, uint8_t *, uint32_t))(params.cbHook);
44117fd14ceSopenharmony_ci
44217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
44317fd14ceSopenharmony_ci        (uint8_t *)(&requestId), &inOutLen);
44417fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
44517fd14ceSopenharmony_ci        PARAM_TYPE_COMM_DATA, (uint8_t *)&data, (int32_t *)(&dataLen));
44617fd14ceSopenharmony_ci
44717fd14ceSopenharmony_ci    bRet = onTransmitHook(requestId, data, dataLen);
44817fd14ceSopenharmony_ci    (bRet == true) ? WriteInt32(params.reply, HC_SUCCESS) : WriteInt32(params.reply, HC_ERROR);
44917fd14ceSopenharmony_ci    return;
45017fd14ceSopenharmony_ci}
45117fd14ceSopenharmony_ci
45217fd14ceSopenharmony_cistatic void OnSessKeyStub(CallbackParams params)
45317fd14ceSopenharmony_ci{
45417fd14ceSopenharmony_ci    int64_t requestId = 0;
45517fd14ceSopenharmony_ci    int32_t inOutLen = sizeof(requestId);
45617fd14ceSopenharmony_ci    uint8_t *keyData = NULL;
45717fd14ceSopenharmony_ci    uint32_t dataLen = 0u;
45817fd14ceSopenharmony_ci    void (*onSessKeyHook)(int64_t, uint8_t *, uint32_t) = (void (*)(int64_t, uint8_t *, uint32_t))(params.cbHook);
45917fd14ceSopenharmony_ci
46017fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
46117fd14ceSopenharmony_ci        (uint8_t *)(&requestId), &inOutLen);
46217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_SESS_KEY,
46317fd14ceSopenharmony_ci        (uint8_t *)(&keyData), (int32_t *)(&dataLen));
46417fd14ceSopenharmony_ci
46517fd14ceSopenharmony_ci    onSessKeyHook(requestId, keyData, dataLen);
46617fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
46717fd14ceSopenharmony_ci    return;
46817fd14ceSopenharmony_ci}
46917fd14ceSopenharmony_ci
47017fd14ceSopenharmony_cistatic void OnFinishStub(CallbackParams params)
47117fd14ceSopenharmony_ci{
47217fd14ceSopenharmony_ci    int64_t requestId = 0;
47317fd14ceSopenharmony_ci    int32_t opCode = 0;
47417fd14ceSopenharmony_ci    int32_t inOutLen;
47517fd14ceSopenharmony_ci    char *data = NULL;
47617fd14ceSopenharmony_ci    void (*onFinishHook)(int64_t, int32_t, char *) = (void (*)(int64_t, int32_t, char *))(params.cbHook);
47717fd14ceSopenharmony_ci
47817fd14ceSopenharmony_ci    inOutLen = sizeof(requestId);
47917fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
48017fd14ceSopenharmony_ci        (uint8_t *)(&requestId), &inOutLen);
48117fd14ceSopenharmony_ci    inOutLen = sizeof(opCode);
48217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
48317fd14ceSopenharmony_ci        (uint8_t *)(&opCode), &inOutLen);
48417fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_COMM_DATA, (uint8_t *)(&data), NULL);
48517fd14ceSopenharmony_ci
48617fd14ceSopenharmony_ci    onFinishHook(requestId, opCode, data);
48717fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
48817fd14ceSopenharmony_ci    return;
48917fd14ceSopenharmony_ci}
49017fd14ceSopenharmony_ci
49117fd14ceSopenharmony_cistatic void OnErrorStub(CallbackParams params)
49217fd14ceSopenharmony_ci{
49317fd14ceSopenharmony_ci    int64_t requestId = 0;
49417fd14ceSopenharmony_ci    int32_t opCode = 0;
49517fd14ceSopenharmony_ci    int32_t errCode = 0;
49617fd14ceSopenharmony_ci    int32_t inOutLen;
49717fd14ceSopenharmony_ci    char *errInfo = NULL;
49817fd14ceSopenharmony_ci    void (*onErrorHook)(int64_t, int32_t, int32_t, char *) =
49917fd14ceSopenharmony_ci        (void (*)(int64_t, int32_t, int32_t, char *))(params.cbHook);
50017fd14ceSopenharmony_ci
50117fd14ceSopenharmony_ci    inOutLen = sizeof(requestId);
50217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
50317fd14ceSopenharmony_ci        (uint8_t *)(&requestId), &inOutLen);
50417fd14ceSopenharmony_ci    inOutLen = sizeof(opCode);
50517fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
50617fd14ceSopenharmony_ci        (uint8_t *)(&opCode), &inOutLen);
50717fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_ERRCODE,
50817fd14ceSopenharmony_ci        (uint8_t *)(&errCode), &inOutLen);
50917fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_ERR_INFO,
51017fd14ceSopenharmony_ci        (uint8_t *)(&errInfo), NULL);
51117fd14ceSopenharmony_ci
51217fd14ceSopenharmony_ci    onErrorHook(requestId, opCode, errCode, errInfo);
51317fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
51417fd14ceSopenharmony_ci    return;
51517fd14ceSopenharmony_ci}
51617fd14ceSopenharmony_ci
51717fd14ceSopenharmony_cistatic void OnRequestStub(CallbackParams params)
51817fd14ceSopenharmony_ci{
51917fd14ceSopenharmony_ci    int64_t requestId = 0;
52017fd14ceSopenharmony_ci    int32_t opCode = 0;
52117fd14ceSopenharmony_ci    int32_t inOutLen;
52217fd14ceSopenharmony_ci    char *reqParams = NULL;
52317fd14ceSopenharmony_ci    char *reqResult = NULL;
52417fd14ceSopenharmony_ci    char *(*onReqHook)(int64_t, int32_t, char *) = (char *(*)(int64_t, int32_t, char *))(params.cbHook);
52517fd14ceSopenharmony_ci
52617fd14ceSopenharmony_ci    inOutLen = sizeof(requestId);
52717fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
52817fd14ceSopenharmony_ci        (uint8_t *)(&requestId), &inOutLen);
52917fd14ceSopenharmony_ci    inOutLen = sizeof(opCode);
53017fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
53117fd14ceSopenharmony_ci        (uint8_t *)(&opCode), &inOutLen);
53217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQ_INFO,
53317fd14ceSopenharmony_ci        (uint8_t *)(&reqParams), NULL);
53417fd14ceSopenharmony_ci
53517fd14ceSopenharmony_ci    reqResult = onReqHook(requestId, opCode, reqParams);
53617fd14ceSopenharmony_ci    if (reqResult == NULL) {
53717fd14ceSopenharmony_ci        WriteInt32(params.reply, HC_ERROR);
53817fd14ceSopenharmony_ci        return;
53917fd14ceSopenharmony_ci    }
54017fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
54117fd14ceSopenharmony_ci    WriteString(params.reply, (const char *)(reqResult));
54217fd14ceSopenharmony_ci    HcFree(reqResult);
54317fd14ceSopenharmony_ci    reqResult = NULL;
54417fd14ceSopenharmony_ci    return;
54517fd14ceSopenharmony_ci}
54617fd14ceSopenharmony_ci
54717fd14ceSopenharmony_cistatic void OnGroupCreatedStub(CallbackParams params)
54817fd14ceSopenharmony_ci{
54917fd14ceSopenharmony_ci    const char *groupInfo = NULL;
55017fd14ceSopenharmony_ci    void (*onGroupCreatedHook)(const char *) = (void (*)(const char *))(params.cbHook);
55117fd14ceSopenharmony_ci
55217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
55317fd14ceSopenharmony_ci        PARAM_TYPE_GROUP_INFO, (uint8_t *)(&groupInfo), NULL);
55417fd14ceSopenharmony_ci
55517fd14ceSopenharmony_ci    onGroupCreatedHook(groupInfo);
55617fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
55717fd14ceSopenharmony_ci    return;
55817fd14ceSopenharmony_ci}
55917fd14ceSopenharmony_ci
56017fd14ceSopenharmony_cistatic void OnGroupDeletedStub(CallbackParams params)
56117fd14ceSopenharmony_ci{
56217fd14ceSopenharmony_ci    const char *groupInfo = NULL;
56317fd14ceSopenharmony_ci    void (*onDelGroupHook)(const char *) = (void (*)(const char *))(params.cbHook);
56417fd14ceSopenharmony_ci
56517fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
56617fd14ceSopenharmony_ci        PARAM_TYPE_GROUP_INFO, (uint8_t *)(&groupInfo), NULL);
56717fd14ceSopenharmony_ci
56817fd14ceSopenharmony_ci    onDelGroupHook(groupInfo);
56917fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
57017fd14ceSopenharmony_ci    return;
57117fd14ceSopenharmony_ci}
57217fd14ceSopenharmony_ci
57317fd14ceSopenharmony_cistatic void OnDevBoundStub(CallbackParams params)
57417fd14ceSopenharmony_ci{
57517fd14ceSopenharmony_ci    const char *groupInfo = NULL;
57617fd14ceSopenharmony_ci    const char *udid = NULL;
57717fd14ceSopenharmony_ci    void (*onDevBoundHook)(const char *, const char *) = (void (*)(const char *, const char *))(params.cbHook);
57817fd14ceSopenharmony_ci
57917fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID, (uint8_t *)(&udid), NULL);
58017fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
58117fd14ceSopenharmony_ci        PARAM_TYPE_GROUP_INFO, (uint8_t *)(&groupInfo), NULL);
58217fd14ceSopenharmony_ci
58317fd14ceSopenharmony_ci    onDevBoundHook(udid, groupInfo);
58417fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
58517fd14ceSopenharmony_ci    return;
58617fd14ceSopenharmony_ci}
58717fd14ceSopenharmony_ci
58817fd14ceSopenharmony_cistatic void OnDevUnboundStub(CallbackParams params)
58917fd14ceSopenharmony_ci{
59017fd14ceSopenharmony_ci    const char *groupInfo = NULL;
59117fd14ceSopenharmony_ci    const char *udid = NULL;
59217fd14ceSopenharmony_ci    void (*onDevUnBoundHook)(const char *, const char *) = (void (*)(const char *, const char *))(params.cbHook);
59317fd14ceSopenharmony_ci
59417fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID, (uint8_t *)(&udid), NULL);
59517fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
59617fd14ceSopenharmony_ci        PARAM_TYPE_GROUP_INFO, (uint8_t *)(&groupInfo), NULL);
59717fd14ceSopenharmony_ci
59817fd14ceSopenharmony_ci    onDevUnBoundHook(udid, groupInfo);
59917fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
60017fd14ceSopenharmony_ci    return;
60117fd14ceSopenharmony_ci}
60217fd14ceSopenharmony_ci
60317fd14ceSopenharmony_cistatic void OnDevUnTrustStub(CallbackParams params)
60417fd14ceSopenharmony_ci{
60517fd14ceSopenharmony_ci    const char *udid = NULL;
60617fd14ceSopenharmony_ci    void (*onDevUnTrustHook)(const char *) = (void (*)(const char *))(params.cbHook);
60717fd14ceSopenharmony_ci
60817fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID, (uint8_t *)(&udid), NULL);
60917fd14ceSopenharmony_ci
61017fd14ceSopenharmony_ci    onDevUnTrustHook(udid);
61117fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
61217fd14ceSopenharmony_ci    return;
61317fd14ceSopenharmony_ci}
61417fd14ceSopenharmony_ci
61517fd14ceSopenharmony_cistatic void OnDelLastGroupStub(CallbackParams params)
61617fd14ceSopenharmony_ci{
61717fd14ceSopenharmony_ci    const char *udid = NULL;
61817fd14ceSopenharmony_ci    int32_t groupType = 0;
61917fd14ceSopenharmony_ci    int32_t inOutLen;
62017fd14ceSopenharmony_ci    void (*onDelLastGroupHook)(const char *, int32_t) = (void (*)(const char *, int32_t))(params.cbHook);
62117fd14ceSopenharmony_ci
62217fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID, (uint8_t *)(&udid), NULL);
62317fd14ceSopenharmony_ci    inOutLen = sizeof(groupType);
62417fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
62517fd14ceSopenharmony_ci        PARAM_TYPE_GROUP_TYPE, (uint8_t *)(&groupType), &inOutLen);
62617fd14ceSopenharmony_ci
62717fd14ceSopenharmony_ci    onDelLastGroupHook(udid, groupType);
62817fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
62917fd14ceSopenharmony_ci    return;
63017fd14ceSopenharmony_ci}
63117fd14ceSopenharmony_ci
63217fd14ceSopenharmony_cistatic void OnTrustDevNumChangedStub(CallbackParams params)
63317fd14ceSopenharmony_ci{
63417fd14ceSopenharmony_ci    int32_t devNum = 0;
63517fd14ceSopenharmony_ci    int32_t inOutLen = sizeof(devNum);
63617fd14ceSopenharmony_ci    void (*onTrustDevNumChangedHook)(int32_t) = (void (*)(int32_t))(params.cbHook);
63717fd14ceSopenharmony_ci
63817fd14ceSopenharmony_ci    (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
63917fd14ceSopenharmony_ci        PARAM_TYPE_DATA_NUM, (uint8_t *)(&devNum), &inOutLen);
64017fd14ceSopenharmony_ci
64117fd14ceSopenharmony_ci    onTrustDevNumChangedHook(devNum);
64217fd14ceSopenharmony_ci    WriteInt32(params.reply, HC_SUCCESS);
64317fd14ceSopenharmony_ci    return;
64417fd14ceSopenharmony_ci}
64517fd14ceSopenharmony_ci
64617fd14ceSopenharmony_civoid ProcCbHook(int32_t callbackId, uintptr_t cbHook,
64717fd14ceSopenharmony_ci    const IpcDataInfo *cbDataCache, int32_t cacheNum, uintptr_t replyCtx)
64817fd14ceSopenharmony_ci{
64917fd14ceSopenharmony_ci    CallbackStub stubTable[] = {
65017fd14ceSopenharmony_ci        OnTransmitStub, OnSessKeyStub, OnFinishStub, OnErrorStub,
65117fd14ceSopenharmony_ci        OnRequestStub, OnGroupCreatedStub, OnGroupDeletedStub, OnDevBoundStub,
65217fd14ceSopenharmony_ci        OnDevUnboundStub, OnDevUnTrustStub, OnDelLastGroupStub, OnTrustDevNumChangedStub
65317fd14ceSopenharmony_ci    };
65417fd14ceSopenharmony_ci    IpcIo *reply = (IpcIo *)(replyCtx);
65517fd14ceSopenharmony_ci    if ((callbackId < CB_ID_ON_TRANS) || (callbackId > CB_ID_ON_TRUST_DEV_NUM_CHANGED)) {
65617fd14ceSopenharmony_ci        LOGE("Invalid call back id");
65717fd14ceSopenharmony_ci        return;
65817fd14ceSopenharmony_ci    }
65917fd14ceSopenharmony_ci    if (cbHook == 0x0) {
66017fd14ceSopenharmony_ci        LOGE("Invalid call back hook");
66117fd14ceSopenharmony_ci        return;
66217fd14ceSopenharmony_ci    }
66317fd14ceSopenharmony_ci    LOGI("call service callback start. CbId: %d", callbackId);
66417fd14ceSopenharmony_ci    CallbackParams params = { cbHook, cbDataCache, cacheNum, reply };
66517fd14ceSopenharmony_ci    stubTable[callbackId - 1](params);
66617fd14ceSopenharmony_ci    LOGI("call service callback end");
66717fd14ceSopenharmony_ci    return;
66817fd14ceSopenharmony_ci}
66917fd14ceSopenharmony_ci
67017fd14ceSopenharmony_cistatic uint32_t EncodeCallData(IpcIo *dataParcel, int32_t type, const uint8_t *param, int32_t paramSz)
67117fd14ceSopenharmony_ci{
67217fd14ceSopenharmony_ci    const uint8_t *paramTmp = NULL;
67317fd14ceSopenharmony_ci    int32_t zeroVal = 0;
67417fd14ceSopenharmony_ci
67517fd14ceSopenharmony_ci    paramTmp = param;
67617fd14ceSopenharmony_ci    if ((param == NULL) || (paramSz == 0)) {
67717fd14ceSopenharmony_ci        paramTmp = (const uint8_t *)(&zeroVal);
67817fd14ceSopenharmony_ci        paramSz = sizeof(zeroVal);
67917fd14ceSopenharmony_ci    }
68017fd14ceSopenharmony_ci    WriteInt32(dataParcel, type);
68117fd14ceSopenharmony_ci    WriteUint32(dataParcel, (uint32_t)paramSz);
68217fd14ceSopenharmony_ci    bool ret = WriteBuffer(dataParcel, (const void *)(paramTmp), (uint32_t)paramSz);
68317fd14ceSopenharmony_ci    if (!ret) {
68417fd14ceSopenharmony_ci        return (uint32_t)(HC_ERROR);
68517fd14ceSopenharmony_ci    }
68617fd14ceSopenharmony_ci    return (uint32_t)(HC_SUCCESS);
68717fd14ceSopenharmony_ci}
68817fd14ceSopenharmony_ci
68917fd14ceSopenharmony_ci/* group auth callback adapter */
69017fd14ceSopenharmony_cistatic bool GaCbOnTransmitWithType(int64_t requestId, const uint8_t *data, uint32_t dataLen, int32_t type)
69117fd14ceSopenharmony_ci{
69217fd14ceSopenharmony_ci    uint32_t ret;
69317fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
69417fd14ceSopenharmony_ci    IpcIo reply;
69517fd14ceSopenharmony_ci    uint8_t dataBuf[IPC_STACK_BUFF_SZ] = { 0 };
69617fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
69717fd14ceSopenharmony_ci
69817fd14ceSopenharmony_ci    LOGI("starting ... request id: %lld, type %d", requestId, type);
69917fd14ceSopenharmony_ci    IpcIoInit(&reply, (void *)dataBuf, sizeof(dataBuf), 0);
70017fd14ceSopenharmony_ci    LockCallbackList();
70117fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(requestId, type);
70217fd14ceSopenharmony_ci    if (node == NULL) {
70317fd14ceSopenharmony_ci        UnLockCallbackList();
70417fd14ceSopenharmony_ci        LOGE("onTransmit hook is null, request id %lld", requestId);
70517fd14ceSopenharmony_ci        return false;
70617fd14ceSopenharmony_ci    }
70717fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
70817fd14ceSopenharmony_ci    if (dataParcel == NULL) {
70917fd14ceSopenharmony_ci        UnLockCallbackList();
71017fd14ceSopenharmony_ci        return false;
71117fd14ceSopenharmony_ci    }
71217fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (const uint8_t *)(&requestId), sizeof(requestId));
71317fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA, data, dataLen);
71417fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
71517fd14ceSopenharmony_ci        UnLockCallbackList();
71617fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
71717fd14ceSopenharmony_ci        LOGE("build trans data failed");
71817fd14ceSopenharmony_ci        return false;
71917fd14ceSopenharmony_ci    }
72017fd14ceSopenharmony_ci    ActCallback(node->proxyId, CB_ID_ON_TRANS, (uintptr_t)(node->cbCtx.devAuth.onTransmit), dataParcel, &reply);
72117fd14ceSopenharmony_ci    UnLockCallbackList();
72217fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
72317fd14ceSopenharmony_ci    LOGI("process done, request id: %lld", requestId);
72417fd14ceSopenharmony_ci    int32_t value;
72517fd14ceSopenharmony_ci    ReadInt32(&reply, &value);
72617fd14ceSopenharmony_ci    if (value == HC_SUCCESS) {
72717fd14ceSopenharmony_ci        return true;
72817fd14ceSopenharmony_ci    }
72917fd14ceSopenharmony_ci    return false;
73017fd14ceSopenharmony_ci}
73117fd14ceSopenharmony_ci
73217fd14ceSopenharmony_cistatic bool IpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
73317fd14ceSopenharmony_ci{
73417fd14ceSopenharmony_ci    return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_DEV_AUTH);
73517fd14ceSopenharmony_ci}
73617fd14ceSopenharmony_ci
73717fd14ceSopenharmony_cistatic bool TmpIpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
73817fd14ceSopenharmony_ci{
73917fd14ceSopenharmony_ci    return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_TMP_DEV_AUTH);
74017fd14ceSopenharmony_ci}
74117fd14ceSopenharmony_ci
74217fd14ceSopenharmony_cistatic void GaCbOnSessionKeyRetWithType(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen, int32_t type)
74317fd14ceSopenharmony_ci{
74417fd14ceSopenharmony_ci    uint32_t ret;
74517fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
74617fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
74717fd14ceSopenharmony_ci
74817fd14ceSopenharmony_ci    LOGI("starting ... request id: %lld, type %d", requestId, type);
74917fd14ceSopenharmony_ci    LockCallbackList();
75017fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(requestId, type);
75117fd14ceSopenharmony_ci    if (node == NULL) {
75217fd14ceSopenharmony_ci        UnLockCallbackList();
75317fd14ceSopenharmony_ci        LOGE("onSessionKeyReturned hook is null, request id %lld", requestId);
75417fd14ceSopenharmony_ci        return;
75517fd14ceSopenharmony_ci    }
75617fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
75717fd14ceSopenharmony_ci    if (dataParcel == NULL) {
75817fd14ceSopenharmony_ci        UnLockCallbackList();
75917fd14ceSopenharmony_ci        return;
76017fd14ceSopenharmony_ci    }
76117fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
76217fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_SESS_KEY, sessKey, sessKeyLen);
76317fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
76417fd14ceSopenharmony_ci        UnLockCallbackList();
76517fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
76617fd14ceSopenharmony_ci        LOGE("build trans data failed");
76717fd14ceSopenharmony_ci        return;
76817fd14ceSopenharmony_ci    }
76917fd14ceSopenharmony_ci    ActCallback(node->proxyId, CB_ID_SESS_KEY_DONE,
77017fd14ceSopenharmony_ci        (uintptr_t)(node->cbCtx.devAuth.onSessionKeyReturned), dataParcel, NULL);
77117fd14ceSopenharmony_ci    UnLockCallbackList();
77217fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
77317fd14ceSopenharmony_ci    LOGI("process done, request id: %lld", requestId);
77417fd14ceSopenharmony_ci    return;
77517fd14ceSopenharmony_ci}
77617fd14ceSopenharmony_ci
77717fd14ceSopenharmony_cistatic void IpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
77817fd14ceSopenharmony_ci{
77917fd14ceSopenharmony_ci    GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_DEV_AUTH);
78017fd14ceSopenharmony_ci    return;
78117fd14ceSopenharmony_ci}
78217fd14ceSopenharmony_ci
78317fd14ceSopenharmony_cistatic void TmpIpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
78417fd14ceSopenharmony_ci{
78517fd14ceSopenharmony_ci    GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_TMP_DEV_AUTH);
78617fd14ceSopenharmony_ci    return;
78717fd14ceSopenharmony_ci}
78817fd14ceSopenharmony_ci
78917fd14ceSopenharmony_cistatic void GaCbOnFinishWithType(int64_t requestId, int32_t operationCode, const char *returnData, int32_t type)
79017fd14ceSopenharmony_ci{
79117fd14ceSopenharmony_ci    uint32_t ret;
79217fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
79317fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
79417fd14ceSopenharmony_ci
79517fd14ceSopenharmony_ci    LOGI("starting ... request id: %lld, type %d", requestId, type);
79617fd14ceSopenharmony_ci    LockCallbackList();
79717fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(requestId, type);
79817fd14ceSopenharmony_ci    if (node == NULL) {
79917fd14ceSopenharmony_ci        UnLockCallbackList();
80017fd14ceSopenharmony_ci        LOGE("onFinish hook is null, request id %lld", requestId);
80117fd14ceSopenharmony_ci        return;
80217fd14ceSopenharmony_ci    }
80317fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
80417fd14ceSopenharmony_ci    if (dataParcel == NULL) {
80517fd14ceSopenharmony_ci        UnLockCallbackList();
80617fd14ceSopenharmony_ci        return;
80717fd14ceSopenharmony_ci    }
80817fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
80917fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE, (uint8_t *)(&operationCode), sizeof(operationCode));
81017fd14ceSopenharmony_ci    if (returnData != NULL) {
81117fd14ceSopenharmony_ci        ret |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA, (const uint8_t *)(returnData),
81217fd14ceSopenharmony_ci            HcStrlen(returnData) + 1);
81317fd14ceSopenharmony_ci    }
81417fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
81517fd14ceSopenharmony_ci        UnLockCallbackList();
81617fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
81717fd14ceSopenharmony_ci        LOGE("build trans data failed");
81817fd14ceSopenharmony_ci        return;
81917fd14ceSopenharmony_ci    }
82017fd14ceSopenharmony_ci    ActCallback(node->proxyId, CB_ID_ON_FINISH, (uintptr_t)(node->cbCtx.devAuth.onFinish), dataParcel, NULL);
82117fd14ceSopenharmony_ci    /* delete request id */
82217fd14ceSopenharmony_ci    DelIpcCallBackByReqId(requestId, type, false);
82317fd14ceSopenharmony_ci    UnLockCallbackList();
82417fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
82517fd14ceSopenharmony_ci    LOGI("process done, request id: %lld", requestId);
82617fd14ceSopenharmony_ci    return;
82717fd14ceSopenharmony_ci}
82817fd14ceSopenharmony_ci
82917fd14ceSopenharmony_cistatic void IpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
83017fd14ceSopenharmony_ci{
83117fd14ceSopenharmony_ci    GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_DEV_AUTH);
83217fd14ceSopenharmony_ci    return;
83317fd14ceSopenharmony_ci}
83417fd14ceSopenharmony_ci
83517fd14ceSopenharmony_cistatic void TmpIpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
83617fd14ceSopenharmony_ci{
83717fd14ceSopenharmony_ci    GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_TMP_DEV_AUTH);
83817fd14ceSopenharmony_ci    return;
83917fd14ceSopenharmony_ci}
84017fd14ceSopenharmony_ci
84117fd14ceSopenharmony_cistatic void GaCbOnErrorWithType(int64_t requestId, int32_t operationCode,
84217fd14ceSopenharmony_ci    int32_t errorCode, const char *errorReturn, int32_t type)
84317fd14ceSopenharmony_ci{
84417fd14ceSopenharmony_ci    uint32_t ret;
84517fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
84617fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
84717fd14ceSopenharmony_ci
84817fd14ceSopenharmony_ci    LOGI("starting ... request id: %lld, type %d", requestId, type);
84917fd14ceSopenharmony_ci    LockCallbackList();
85017fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(requestId, type);
85117fd14ceSopenharmony_ci    if (node == NULL) {
85217fd14ceSopenharmony_ci        UnLockCallbackList();
85317fd14ceSopenharmony_ci        LOGE("onError hook is null, request id %lld", requestId);
85417fd14ceSopenharmony_ci        return;
85517fd14ceSopenharmony_ci    }
85617fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
85717fd14ceSopenharmony_ci    if (dataParcel == NULL) {
85817fd14ceSopenharmony_ci        UnLockCallbackList();
85917fd14ceSopenharmony_ci        return;
86017fd14ceSopenharmony_ci    }
86117fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
86217fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE, (uint8_t *)(&operationCode), sizeof(operationCode));
86317fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERRCODE, (uint8_t *)(&errorCode), sizeof(errorCode));
86417fd14ceSopenharmony_ci    if (errorReturn != NULL) {
86517fd14ceSopenharmony_ci        ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERR_INFO, (const uint8_t *)(errorReturn),
86617fd14ceSopenharmony_ci            HcStrlen(errorReturn) + 1);
86717fd14ceSopenharmony_ci    }
86817fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
86917fd14ceSopenharmony_ci        UnLockCallbackList();
87017fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
87117fd14ceSopenharmony_ci        LOGE("build trans data failed");
87217fd14ceSopenharmony_ci        return;
87317fd14ceSopenharmony_ci    }
87417fd14ceSopenharmony_ci    ActCallback(node->proxyId, CB_ID_ON_ERROR, (uintptr_t)(node->cbCtx.devAuth.onError), dataParcel, NULL);
87517fd14ceSopenharmony_ci    /* delete request id */
87617fd14ceSopenharmony_ci    DelIpcCallBackByReqId(requestId, type, false);
87717fd14ceSopenharmony_ci    UnLockCallbackList();
87817fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
87917fd14ceSopenharmony_ci    LOGI("process done, request id: %lld", requestId);
88017fd14ceSopenharmony_ci    return;
88117fd14ceSopenharmony_ci}
88217fd14ceSopenharmony_ci
88317fd14ceSopenharmony_cistatic void IpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
88417fd14ceSopenharmony_ci{
88517fd14ceSopenharmony_ci    GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_DEV_AUTH);
88617fd14ceSopenharmony_ci    return;
88717fd14ceSopenharmony_ci}
88817fd14ceSopenharmony_ci
88917fd14ceSopenharmony_cistatic void TmpIpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
89017fd14ceSopenharmony_ci{
89117fd14ceSopenharmony_ci    GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_TMP_DEV_AUTH);
89217fd14ceSopenharmony_ci    return;
89317fd14ceSopenharmony_ci}
89417fd14ceSopenharmony_ci
89517fd14ceSopenharmony_cistatic char *GaCbOnRequestWithType(int64_t requestId, int32_t operationCode, const char *reqParams, int32_t type)
89617fd14ceSopenharmony_ci{
89717fd14ceSopenharmony_ci    int32_t ret;
89817fd14ceSopenharmony_ci    uint32_t uRet;
89917fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
90017fd14ceSopenharmony_ci    IpcIo reply;
90117fd14ceSopenharmony_ci    uint8_t dataBuf[IPC_STACK_BUFF_SZ] = { 0 };
90217fd14ceSopenharmony_ci    const char *dPtr = NULL;
90317fd14ceSopenharmony_ci    IpcCallBackNode *node = NULL;
90417fd14ceSopenharmony_ci
90517fd14ceSopenharmony_ci    LOGI("starting ... request id: %lld, type %d", requestId, type);
90617fd14ceSopenharmony_ci    IpcIoInit(&reply, (void *)dataBuf, sizeof(dataBuf), 0);
90717fd14ceSopenharmony_ci    LockCallbackList();
90817fd14ceSopenharmony_ci    node = GetIpcCallBackByReqId(requestId, type);
90917fd14ceSopenharmony_ci    if (node == NULL) {
91017fd14ceSopenharmony_ci        UnLockCallbackList();
91117fd14ceSopenharmony_ci        LOGE("onRequest hook is null, request id %lld", requestId);
91217fd14ceSopenharmony_ci        return NULL;
91317fd14ceSopenharmony_ci    }
91417fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
91517fd14ceSopenharmony_ci    if (dataParcel == NULL) {
91617fd14ceSopenharmony_ci        UnLockCallbackList();
91717fd14ceSopenharmony_ci        return NULL;
91817fd14ceSopenharmony_ci    }
91917fd14ceSopenharmony_ci    uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
92017fd14ceSopenharmony_ci    uRet |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE, (uint8_t *)(&operationCode), sizeof(operationCode));
92117fd14ceSopenharmony_ci    if (reqParams != NULL) {
92217fd14ceSopenharmony_ci        uRet |= EncodeCallData(dataParcel, PARAM_TYPE_REQ_INFO, (const uint8_t *)(reqParams), HcStrlen(reqParams) + 1);
92317fd14ceSopenharmony_ci    }
92417fd14ceSopenharmony_ci    if (uRet != HC_SUCCESS) {
92517fd14ceSopenharmony_ci        UnLockCallbackList();
92617fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
92717fd14ceSopenharmony_ci        LOGE("build trans data failed");
92817fd14ceSopenharmony_ci        return NULL;
92917fd14ceSopenharmony_ci    }
93017fd14ceSopenharmony_ci
93117fd14ceSopenharmony_ci    ActCallback(node->proxyId, CB_ID_ON_REQUEST, (uintptr_t)(node->cbCtx.devAuth.onRequest), dataParcel, &reply);
93217fd14ceSopenharmony_ci    UnLockCallbackList();
93317fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
93417fd14ceSopenharmony_ci    ReadInt32(&reply, &ret);
93517fd14ceSopenharmony_ci    if (ret == HC_SUCCESS) {
93617fd14ceSopenharmony_ci        dPtr = (const char *)ReadString(&reply, NULL);
93717fd14ceSopenharmony_ci    }
93817fd14ceSopenharmony_ci    LOGI("process done, request id: %lld, %s result", requestId, (dPtr != NULL) ? "valid" : "invalid");
93917fd14ceSopenharmony_ci    return (dPtr != NULL) ? strdup(dPtr) : NULL;
94017fd14ceSopenharmony_ci}
94117fd14ceSopenharmony_ci
94217fd14ceSopenharmony_cistatic bool CanFindCbByReqId(int64_t requestId)
94317fd14ceSopenharmony_ci{
94417fd14ceSopenharmony_ci    LockCallbackList();
94517fd14ceSopenharmony_ci    IpcCallBackNode *node = GetIpcCallBackByReqId(requestId, CB_TYPE_DEV_AUTH);
94617fd14ceSopenharmony_ci    UnLockCallbackList();
94717fd14ceSopenharmony_ci    return (node != NULL) ? true : false;
94817fd14ceSopenharmony_ci}
94917fd14ceSopenharmony_ci
95017fd14ceSopenharmony_cistatic char *IpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
95117fd14ceSopenharmony_ci{
95217fd14ceSopenharmony_ci    if (!CanFindCbByReqId(requestId)) {
95317fd14ceSopenharmony_ci        CJson *reqParamsJson = CreateJsonFromString(reqParams);
95417fd14ceSopenharmony_ci        if (reqParamsJson == NULL) {
95517fd14ceSopenharmony_ci            LOGE("failed to create json from string!");
95617fd14ceSopenharmony_ci            return NULL;
95717fd14ceSopenharmony_ci        }
95817fd14ceSopenharmony_ci        const char *callerAppId = GetStringFromJson(reqParamsJson, FIELD_APP_ID);
95917fd14ceSopenharmony_ci        if (callerAppId == NULL) {
96017fd14ceSopenharmony_ci            LOGE("failed to get appId from json object!");
96117fd14ceSopenharmony_ci            FreeJson(reqParamsJson);
96217fd14ceSopenharmony_ci            return NULL;
96317fd14ceSopenharmony_ci        }
96417fd14ceSopenharmony_ci        int32_t ret = AddReqIdByAppId(callerAppId, requestId);
96517fd14ceSopenharmony_ci        FreeJson(reqParamsJson);
96617fd14ceSopenharmony_ci        if (ret != HC_SUCCESS) {
96717fd14ceSopenharmony_ci            return NULL;
96817fd14ceSopenharmony_ci        }
96917fd14ceSopenharmony_ci    }
97017fd14ceSopenharmony_ci    return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_DEV_AUTH);
97117fd14ceSopenharmony_ci}
97217fd14ceSopenharmony_ci
97317fd14ceSopenharmony_cistatic char *TmpIpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
97417fd14ceSopenharmony_ci{
97517fd14ceSopenharmony_ci    return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_TMP_DEV_AUTH);
97617fd14ceSopenharmony_ci}
97717fd14ceSopenharmony_ci
97817fd14ceSopenharmony_civoid IpcOnGroupCreated(const char *groupInfo)
97917fd14ceSopenharmony_ci{
98017fd14ceSopenharmony_ci    int32_t i;
98117fd14ceSopenharmony_ci    uint32_t ret;
98217fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
98317fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
98417fd14ceSopenharmony_ci
98517fd14ceSopenharmony_ci    if (groupInfo == NULL) {
98617fd14ceSopenharmony_ci        LOGE("IpcOnGroupCreated, params error");
98717fd14ceSopenharmony_ci        return;
98817fd14ceSopenharmony_ci    }
98917fd14ceSopenharmony_ci
99017fd14ceSopenharmony_ci    LockCallbackList();
99117fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
99217fd14ceSopenharmony_ci        UnLockCallbackList();
99317fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
99417fd14ceSopenharmony_ci        return;
99517fd14ceSopenharmony_ci    }
99617fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
99717fd14ceSopenharmony_ci    if (dataParcel == NULL) {
99817fd14ceSopenharmony_ci        UnLockCallbackList();
99917fd14ceSopenharmony_ci        return;
100017fd14ceSopenharmony_ci    }
100117fd14ceSopenharmony_ci
100217fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), HcStrlen(groupInfo) + 1);
100317fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
100417fd14ceSopenharmony_ci        UnLockCallbackList();
100517fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
100617fd14ceSopenharmony_ci        LOGE("IpcGaCbOnRequest, build trans data failed");
100717fd14ceSopenharmony_ci        return;
100817fd14ceSopenharmony_ci    }
100917fd14ceSopenharmony_ci
101017fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
101117fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
101217fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
101317fd14ceSopenharmony_ci            if (listener->onGroupCreated == NULL) {
101417fd14ceSopenharmony_ci                continue;
101517fd14ceSopenharmony_ci            }
101617fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_CREATED,
101717fd14ceSopenharmony_ci                (uintptr_t)(listener->onGroupCreated), dataParcel, NULL);
101817fd14ceSopenharmony_ci        }
101917fd14ceSopenharmony_ci    }
102017fd14ceSopenharmony_ci    UnLockCallbackList();
102117fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
102217fd14ceSopenharmony_ci    return;
102317fd14ceSopenharmony_ci}
102417fd14ceSopenharmony_ci
102517fd14ceSopenharmony_civoid IpcOnGroupDeleted(const char *groupInfo)
102617fd14ceSopenharmony_ci{
102717fd14ceSopenharmony_ci    int32_t i;
102817fd14ceSopenharmony_ci    uint32_t ret;
102917fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
103017fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
103117fd14ceSopenharmony_ci
103217fd14ceSopenharmony_ci    if (groupInfo == NULL) {
103317fd14ceSopenharmony_ci        LOGE("IpcOnGroupDeleted, params error");
103417fd14ceSopenharmony_ci        return;
103517fd14ceSopenharmony_ci    }
103617fd14ceSopenharmony_ci
103717fd14ceSopenharmony_ci    LockCallbackList();
103817fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
103917fd14ceSopenharmony_ci        UnLockCallbackList();
104017fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
104117fd14ceSopenharmony_ci        return;
104217fd14ceSopenharmony_ci    }
104317fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
104417fd14ceSopenharmony_ci    if (dataParcel == NULL) {
104517fd14ceSopenharmony_ci        UnLockCallbackList();
104617fd14ceSopenharmony_ci        return;
104717fd14ceSopenharmony_ci    }
104817fd14ceSopenharmony_ci
104917fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), HcStrlen(groupInfo) + 1);
105017fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
105117fd14ceSopenharmony_ci        UnLockCallbackList();
105217fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
105317fd14ceSopenharmony_ci        LOGE("IpcGaCbOnRequest, build trans data failed");
105417fd14ceSopenharmony_ci        return;
105517fd14ceSopenharmony_ci    }
105617fd14ceSopenharmony_ci
105717fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
105817fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
105917fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
106017fd14ceSopenharmony_ci            if (listener->onGroupDeleted == NULL) {
106117fd14ceSopenharmony_ci                continue;
106217fd14ceSopenharmony_ci            }
106317fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_DELETED,
106417fd14ceSopenharmony_ci                (uintptr_t)(listener->onGroupDeleted), dataParcel, NULL);
106517fd14ceSopenharmony_ci        }
106617fd14ceSopenharmony_ci    }
106717fd14ceSopenharmony_ci    UnLockCallbackList();
106817fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
106917fd14ceSopenharmony_ci    return;
107017fd14ceSopenharmony_ci}
107117fd14ceSopenharmony_ci
107217fd14ceSopenharmony_civoid IpcOnDeviceBound(const char *peerUdid, const char *groupInfo)
107317fd14ceSopenharmony_ci{
107417fd14ceSopenharmony_ci    int32_t i;
107517fd14ceSopenharmony_ci    uint32_t ret;
107617fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
107717fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
107817fd14ceSopenharmony_ci
107917fd14ceSopenharmony_ci    if ((peerUdid == NULL) || (groupInfo == NULL)) {
108017fd14ceSopenharmony_ci        LOGE("params error");
108117fd14ceSopenharmony_ci        return;
108217fd14ceSopenharmony_ci    }
108317fd14ceSopenharmony_ci
108417fd14ceSopenharmony_ci    LockCallbackList();
108517fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
108617fd14ceSopenharmony_ci        UnLockCallbackList();
108717fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
108817fd14ceSopenharmony_ci        return;
108917fd14ceSopenharmony_ci    }
109017fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
109117fd14ceSopenharmony_ci    if (dataParcel == NULL) {
109217fd14ceSopenharmony_ci        UnLockCallbackList();
109317fd14ceSopenharmony_ci        return;
109417fd14ceSopenharmony_ci    }
109517fd14ceSopenharmony_ci
109617fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), HcStrlen(peerUdid) + 1);
109717fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), HcStrlen(groupInfo) + 1);
109817fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
109917fd14ceSopenharmony_ci        UnLockCallbackList();
110017fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
110117fd14ceSopenharmony_ci        LOGE("build trans data failed");
110217fd14ceSopenharmony_ci        return;
110317fd14ceSopenharmony_ci    }
110417fd14ceSopenharmony_ci
110517fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
110617fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
110717fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
110817fd14ceSopenharmony_ci            if (listener->onDeviceBound == NULL) {
110917fd14ceSopenharmony_ci                continue;
111017fd14ceSopenharmony_ci            }
111117fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_BOUND,
111217fd14ceSopenharmony_ci                (uintptr_t)(listener->onDeviceBound), dataParcel, NULL);
111317fd14ceSopenharmony_ci        }
111417fd14ceSopenharmony_ci    }
111517fd14ceSopenharmony_ci    UnLockCallbackList();
111617fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
111717fd14ceSopenharmony_ci    return;
111817fd14ceSopenharmony_ci}
111917fd14ceSopenharmony_ci
112017fd14ceSopenharmony_civoid IpcOnDeviceUnBound(const char *peerUdid, const char *groupInfo)
112117fd14ceSopenharmony_ci{
112217fd14ceSopenharmony_ci    int32_t i;
112317fd14ceSopenharmony_ci    uint32_t ret;
112417fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
112517fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
112617fd14ceSopenharmony_ci
112717fd14ceSopenharmony_ci    if ((peerUdid == NULL) || (groupInfo == NULL)) {
112817fd14ceSopenharmony_ci        LOGE("params error");
112917fd14ceSopenharmony_ci        return;
113017fd14ceSopenharmony_ci    }
113117fd14ceSopenharmony_ci
113217fd14ceSopenharmony_ci    LockCallbackList();
113317fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
113417fd14ceSopenharmony_ci        UnLockCallbackList();
113517fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
113617fd14ceSopenharmony_ci        return;
113717fd14ceSopenharmony_ci    }
113817fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
113917fd14ceSopenharmony_ci    if (dataParcel == NULL) {
114017fd14ceSopenharmony_ci        UnLockCallbackList();
114117fd14ceSopenharmony_ci        return;
114217fd14ceSopenharmony_ci    }
114317fd14ceSopenharmony_ci
114417fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), HcStrlen(peerUdid) + 1);
114517fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), HcStrlen(groupInfo) + 1);
114617fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
114717fd14ceSopenharmony_ci        UnLockCallbackList();
114817fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
114917fd14ceSopenharmony_ci        LOGE("build trans data failed");
115017fd14ceSopenharmony_ci        return;
115117fd14ceSopenharmony_ci    }
115217fd14ceSopenharmony_ci
115317fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
115417fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
115517fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
115617fd14ceSopenharmony_ci            if (listener->onDeviceUnBound == NULL) {
115717fd14ceSopenharmony_ci                continue;
115817fd14ceSopenharmony_ci            }
115917fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNBOUND,
116017fd14ceSopenharmony_ci                (uintptr_t)(listener->onDeviceUnBound), dataParcel, NULL);
116117fd14ceSopenharmony_ci        }
116217fd14ceSopenharmony_ci    }
116317fd14ceSopenharmony_ci    UnLockCallbackList();
116417fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
116517fd14ceSopenharmony_ci    return;
116617fd14ceSopenharmony_ci}
116717fd14ceSopenharmony_ci
116817fd14ceSopenharmony_civoid IpcOnDeviceNotTrusted(const char *peerUdid)
116917fd14ceSopenharmony_ci{
117017fd14ceSopenharmony_ci    int32_t i;
117117fd14ceSopenharmony_ci    uint32_t ret;
117217fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
117317fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
117417fd14ceSopenharmony_ci
117517fd14ceSopenharmony_ci    if (peerUdid == NULL) {
117617fd14ceSopenharmony_ci        LOGE("params error");
117717fd14ceSopenharmony_ci        return;
117817fd14ceSopenharmony_ci    }
117917fd14ceSopenharmony_ci
118017fd14ceSopenharmony_ci    LockCallbackList();
118117fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
118217fd14ceSopenharmony_ci        UnLockCallbackList();
118317fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
118417fd14ceSopenharmony_ci        return;
118517fd14ceSopenharmony_ci    }
118617fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
118717fd14ceSopenharmony_ci    if (dataParcel == NULL) {
118817fd14ceSopenharmony_ci        UnLockCallbackList();
118917fd14ceSopenharmony_ci        return;
119017fd14ceSopenharmony_ci    }
119117fd14ceSopenharmony_ci
119217fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), HcStrlen(peerUdid) + 1);
119317fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
119417fd14ceSopenharmony_ci        UnLockCallbackList();
119517fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
119617fd14ceSopenharmony_ci        LOGE("build trans data failed");
119717fd14ceSopenharmony_ci        return;
119817fd14ceSopenharmony_ci    }
119917fd14ceSopenharmony_ci
120017fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
120117fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
120217fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
120317fd14ceSopenharmony_ci            if (listener->onDeviceNotTrusted == NULL) {
120417fd14ceSopenharmony_ci                continue;
120517fd14ceSopenharmony_ci            }
120617fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNTRUSTED,
120717fd14ceSopenharmony_ci                (uintptr_t)(listener->onDeviceNotTrusted), dataParcel, NULL);
120817fd14ceSopenharmony_ci        }
120917fd14ceSopenharmony_ci    }
121017fd14ceSopenharmony_ci    UnLockCallbackList();
121117fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
121217fd14ceSopenharmony_ci    return;
121317fd14ceSopenharmony_ci}
121417fd14ceSopenharmony_ci
121517fd14ceSopenharmony_civoid IpcOnLastGroupDeleted(const char *peerUdid, int32_t groupType)
121617fd14ceSopenharmony_ci{
121717fd14ceSopenharmony_ci    int32_t i;
121817fd14ceSopenharmony_ci    uint32_t ret;
121917fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
122017fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
122117fd14ceSopenharmony_ci
122217fd14ceSopenharmony_ci    if (peerUdid == NULL) {
122317fd14ceSopenharmony_ci        LOGE("params error");
122417fd14ceSopenharmony_ci        return;
122517fd14ceSopenharmony_ci    }
122617fd14ceSopenharmony_ci
122717fd14ceSopenharmony_ci    LockCallbackList();
122817fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
122917fd14ceSopenharmony_ci        UnLockCallbackList();
123017fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
123117fd14ceSopenharmony_ci        return;
123217fd14ceSopenharmony_ci    }
123317fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
123417fd14ceSopenharmony_ci    if (dataParcel == NULL) {
123517fd14ceSopenharmony_ci        UnLockCallbackList();
123617fd14ceSopenharmony_ci        return;
123717fd14ceSopenharmony_ci    }
123817fd14ceSopenharmony_ci
123917fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), HcStrlen(peerUdid) + 1);
124017fd14ceSopenharmony_ci    ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_TYPE, (const uint8_t *)(&groupType), sizeof(groupType));
124117fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
124217fd14ceSopenharmony_ci        UnLockCallbackList();
124317fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
124417fd14ceSopenharmony_ci        LOGE("build trans data failed");
124517fd14ceSopenharmony_ci        return;
124617fd14ceSopenharmony_ci    }
124717fd14ceSopenharmony_ci
124817fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
124917fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
125017fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
125117fd14ceSopenharmony_ci            if (listener->onLastGroupDeleted == NULL) {
125217fd14ceSopenharmony_ci                continue;
125317fd14ceSopenharmony_ci            }
125417fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_LAST_GROUP_DELETED,
125517fd14ceSopenharmony_ci                (uintptr_t)(listener->onLastGroupDeleted), dataParcel, NULL);
125617fd14ceSopenharmony_ci        }
125717fd14ceSopenharmony_ci    }
125817fd14ceSopenharmony_ci    UnLockCallbackList();
125917fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
126017fd14ceSopenharmony_ci    return;
126117fd14ceSopenharmony_ci}
126217fd14ceSopenharmony_ci
126317fd14ceSopenharmony_civoid IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)
126417fd14ceSopenharmony_ci{
126517fd14ceSopenharmony_ci    int32_t i;
126617fd14ceSopenharmony_ci    uint32_t ret;
126717fd14ceSopenharmony_ci    IpcIo *dataParcel = NULL;
126817fd14ceSopenharmony_ci    DataChangeListener *listener = NULL;
126917fd14ceSopenharmony_ci
127017fd14ceSopenharmony_ci    LockCallbackList();
127117fd14ceSopenharmony_ci    if (g_ipcCallBackList.ctx == NULL) {
127217fd14ceSopenharmony_ci        UnLockCallbackList();
127317fd14ceSopenharmony_ci        LOGE("IpcCallBackList un-initialized");
127417fd14ceSopenharmony_ci        return;
127517fd14ceSopenharmony_ci    }
127617fd14ceSopenharmony_ci
127717fd14ceSopenharmony_ci    dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
127817fd14ceSopenharmony_ci    if (dataParcel == NULL) {
127917fd14ceSopenharmony_ci        UnLockCallbackList();
128017fd14ceSopenharmony_ci        return;
128117fd14ceSopenharmony_ci    }
128217fd14ceSopenharmony_ci    ret = EncodeCallData(dataParcel, PARAM_TYPE_DATA_NUM,
128317fd14ceSopenharmony_ci        (const uint8_t *)(&curTrustedDeviceNum), sizeof(curTrustedDeviceNum));
128417fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
128517fd14ceSopenharmony_ci        UnLockCallbackList();
128617fd14ceSopenharmony_ci        HcFree((void *)dataParcel);
128717fd14ceSopenharmony_ci        LOGE("IpcOnTrustedDeviceNumChanged, build trans data failed");
128817fd14ceSopenharmony_ci        return;
128917fd14ceSopenharmony_ci    }
129017fd14ceSopenharmony_ci
129117fd14ceSopenharmony_ci    for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
129217fd14ceSopenharmony_ci        if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
129317fd14ceSopenharmony_ci            listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
129417fd14ceSopenharmony_ci            if (listener->onTrustedDeviceNumChanged == NULL) {
129517fd14ceSopenharmony_ci                continue;
129617fd14ceSopenharmony_ci            }
129717fd14ceSopenharmony_ci            ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_TRUST_DEV_NUM_CHANGED,
129817fd14ceSopenharmony_ci                (uintptr_t)(listener->onTrustedDeviceNumChanged), dataParcel, NULL);
129917fd14ceSopenharmony_ci        }
130017fd14ceSopenharmony_ci    }
130117fd14ceSopenharmony_ci    UnLockCallbackList();
130217fd14ceSopenharmony_ci    HcFree((void *)dataParcel);
130317fd14ceSopenharmony_ci    return;
130417fd14ceSopenharmony_ci}
130517fd14ceSopenharmony_ci
130617fd14ceSopenharmony_civoid InitDeviceAuthCbCtx(DeviceAuthCallback *ctx, int32_t type)
130717fd14ceSopenharmony_ci{
130817fd14ceSopenharmony_ci    if (ctx == NULL) {
130917fd14ceSopenharmony_ci        return;
131017fd14ceSopenharmony_ci    }
131117fd14ceSopenharmony_ci    if (type == CB_TYPE_DEV_AUTH) {
131217fd14ceSopenharmony_ci        ctx->onTransmit = IpcGaCbOnTransmit;
131317fd14ceSopenharmony_ci        ctx->onSessionKeyReturned = IpcGaCbOnSessionKeyReturned;
131417fd14ceSopenharmony_ci        ctx->onFinish = IpcGaCbOnFinish;
131517fd14ceSopenharmony_ci        ctx->onError = IpcGaCbOnError;
131617fd14ceSopenharmony_ci        ctx->onRequest = IpcGaCbOnRequest;
131717fd14ceSopenharmony_ci    }
131817fd14ceSopenharmony_ci    if (type == CB_TYPE_TMP_DEV_AUTH) {
131917fd14ceSopenharmony_ci        ctx->onTransmit = TmpIpcGaCbOnTransmit;
132017fd14ceSopenharmony_ci        ctx->onSessionKeyReturned = TmpIpcGaCbOnSessionKeyReturned;
132117fd14ceSopenharmony_ci        ctx->onFinish = TmpIpcGaCbOnFinish;
132217fd14ceSopenharmony_ci        ctx->onError = TmpIpcGaCbOnError;
132317fd14ceSopenharmony_ci        ctx->onRequest = TmpIpcGaCbOnRequest;
132417fd14ceSopenharmony_ci    }
132517fd14ceSopenharmony_ci    return;
132617fd14ceSopenharmony_ci}
132717fd14ceSopenharmony_ci
132817fd14ceSopenharmony_civoid InitDevAuthListenerCbCtx(DataChangeListener *ctx)
132917fd14ceSopenharmony_ci{
133017fd14ceSopenharmony_ci    if (ctx == NULL) {
133117fd14ceSopenharmony_ci        return;
133217fd14ceSopenharmony_ci    }
133317fd14ceSopenharmony_ci    ctx->onGroupCreated = IpcOnGroupCreated;
133417fd14ceSopenharmony_ci    ctx->onGroupDeleted = IpcOnGroupDeleted;
133517fd14ceSopenharmony_ci    ctx->onDeviceBound = IpcOnDeviceBound;
133617fd14ceSopenharmony_ci    ctx->onDeviceUnBound = IpcOnDeviceUnBound;
133717fd14ceSopenharmony_ci    ctx->onDeviceNotTrusted = IpcOnDeviceNotTrusted;
133817fd14ceSopenharmony_ci    ctx->onLastGroupDeleted = IpcOnLastGroupDeleted;
133917fd14ceSopenharmony_ci    ctx->onTrustedDeviceNumChanged = IpcOnTrustedDeviceNumChanged;
134017fd14ceSopenharmony_ci    return;
134117fd14ceSopenharmony_ci}
134217fd14ceSopenharmony_ci
134317fd14ceSopenharmony_ci/* ipc client process adapter */
134417fd14ceSopenharmony_ciint32_t CreateCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
134517fd14ceSopenharmony_ci{
134617fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = NULL;
134717fd14ceSopenharmony_ci
134817fd14ceSopenharmony_ci    (void)cbCtx;
134917fd14ceSopenharmony_ci    if (callCtx == NULL) {
135017fd14ceSopenharmony_ci        return HC_ERR_INVALID_PARAMS;
135117fd14ceSopenharmony_ci    }
135217fd14ceSopenharmony_ci
135317fd14ceSopenharmony_ci    dataCache = (ProxyDevAuthData *)HcMalloc(sizeof(ProxyDevAuthData), 0);
135417fd14ceSopenharmony_ci    if (dataCache == NULL) {
135517fd14ceSopenharmony_ci        LOGE("call context alloc failed");
135617fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
135717fd14ceSopenharmony_ci    }
135817fd14ceSopenharmony_ci    dataCache->data = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
135917fd14ceSopenharmony_ci    dataCache->tmpData = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
136017fd14ceSopenharmony_ci    dataCache->reply = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
136117fd14ceSopenharmony_ci    if ((dataCache->data == NULL) || (dataCache->tmpData == NULL) || (dataCache->reply == NULL)) {
136217fd14ceSopenharmony_ci        DestroyCallCtx((uintptr_t *)(dataCache), NULL);
136317fd14ceSopenharmony_ci        return HC_ERROR;
136417fd14ceSopenharmony_ci    }
136517fd14ceSopenharmony_ci    /* linux lite, ipc io init with : token + SvcIdentity */
136617fd14ceSopenharmony_ci    dataCache->ioBuffOffset = IpcIoBufferOffset();
136717fd14ceSopenharmony_ci    *callCtx = (uintptr_t)(dataCache);
136817fd14ceSopenharmony_ci    return HC_SUCCESS;
136917fd14ceSopenharmony_ci}
137017fd14ceSopenharmony_ci
137117fd14ceSopenharmony_civoid DestroyCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
137217fd14ceSopenharmony_ci{
137317fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = NULL;
137417fd14ceSopenharmony_ci
137517fd14ceSopenharmony_ci    (void)cbCtx;
137617fd14ceSopenharmony_ci    if ((callCtx != NULL) && (*callCtx != 0)) {
137717fd14ceSopenharmony_ci        dataCache = (ProxyDevAuthData *)(*callCtx);
137817fd14ceSopenharmony_ci        if (dataCache->data != NULL) {
137917fd14ceSopenharmony_ci            HcFree((void *)dataCache->data);
138017fd14ceSopenharmony_ci            dataCache->data = NULL;
138117fd14ceSopenharmony_ci        }
138217fd14ceSopenharmony_ci        if (dataCache->tmpData != NULL) {
138317fd14ceSopenharmony_ci            HcFree((void *)dataCache->tmpData);
138417fd14ceSopenharmony_ci            dataCache->tmpData = NULL;
138517fd14ceSopenharmony_ci        }
138617fd14ceSopenharmony_ci        if (dataCache->reply != NULL) {
138717fd14ceSopenharmony_ci            HcFree((void *)dataCache->reply);
138817fd14ceSopenharmony_ci            dataCache->reply = NULL;
138917fd14ceSopenharmony_ci        }
139017fd14ceSopenharmony_ci        HcFree((void *)dataCache);
139117fd14ceSopenharmony_ci        *callCtx = 0;
139217fd14ceSopenharmony_ci    }
139317fd14ceSopenharmony_ci    return;
139417fd14ceSopenharmony_ci}
139517fd14ceSopenharmony_ci
139617fd14ceSopenharmony_civoid SetCbCtxToDataCtx(uintptr_t callCtx, int32_t cbIdx)
139717fd14ceSopenharmony_ci{
139817fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = NULL;
139917fd14ceSopenharmony_ci    const SvcIdentity *stubInfo = &g_sdkCbStub.stubIdentity;
140017fd14ceSopenharmony_ci    (void)cbIdx;
140117fd14ceSopenharmony_ci    if (!g_sdkCbStub.registered) {
140217fd14ceSopenharmony_ci        LOGW("SDK callback stub un-registered");
140317fd14ceSopenharmony_ci        return;
140417fd14ceSopenharmony_ci    }
140517fd14ceSopenharmony_ci    ShowIpcSvcInfo(stubInfo);
140617fd14ceSopenharmony_ci    dataCache = (ProxyDevAuthData *)(callCtx);
140717fd14ceSopenharmony_ci    SetCallbackStub(dataCache, stubInfo);
140817fd14ceSopenharmony_ci    return;
140917fd14ceSopenharmony_ci}
141017fd14ceSopenharmony_ci
141117fd14ceSopenharmony_ciint32_t SetCallRequestParamInfo(uintptr_t callCtx, int32_t type, const uint8_t *param, int32_t paramSz)
141217fd14ceSopenharmony_ci{
141317fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
141417fd14ceSopenharmony_ci
141517fd14ceSopenharmony_ci    return EncodeCallRequest(dataCache, type, param, paramSz);
141617fd14ceSopenharmony_ci}
141717fd14ceSopenharmony_ci
141817fd14ceSopenharmony_ciint32_t DoBinderCall(uintptr_t callCtx, int32_t methodId, bool withSync)
141917fd14ceSopenharmony_ci{
142017fd14ceSopenharmony_ci    (void)withSync;
142117fd14ceSopenharmony_ci    int32_t ret;
142217fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
142317fd14ceSopenharmony_ci
142417fd14ceSopenharmony_ci    ret = FinalCallRequest(dataCache, methodId);
142517fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
142617fd14ceSopenharmony_ci        return ret;
142717fd14ceSopenharmony_ci    }
142817fd14ceSopenharmony_ci    return ActCall(g_proxyInstance, dataCache);
142917fd14ceSopenharmony_ci}
143017fd14ceSopenharmony_ci
143117fd14ceSopenharmony_ci/* ipc service process adapter */
143217fd14ceSopenharmony_ciuint32_t SetIpcCallMap(uintptr_t ipcInstance, IpcServiceCall method, int32_t methodId)
143317fd14ceSopenharmony_ci{
143417fd14ceSopenharmony_ci    (void)ipcInstance;
143517fd14ceSopenharmony_ci    if ((method == NULL) || (methodId <= 0)) {
143617fd14ceSopenharmony_ci        return HC_ERR_INVALID_PARAMS;
143717fd14ceSopenharmony_ci    }
143817fd14ceSopenharmony_ci
143917fd14ceSopenharmony_ci    return (uint32_t)SetCallMap(method, methodId);
144017fd14ceSopenharmony_ci}
144117fd14ceSopenharmony_ci
144217fd14ceSopenharmony_ciint32_t CreateServiceInstance(uintptr_t *ipcInstance)
144317fd14ceSopenharmony_ci{
144417fd14ceSopenharmony_ci    *ipcInstance = 0x0;
144517fd14ceSopenharmony_ci    return HC_SUCCESS;
144617fd14ceSopenharmony_ci}
144717fd14ceSopenharmony_ci
144817fd14ceSopenharmony_civoid DestroyServiceInstance(uintptr_t ipcInstance)
144917fd14ceSopenharmony_ci{
145017fd14ceSopenharmony_ci    (void)ipcInstance;
145117fd14ceSopenharmony_ci}
145217fd14ceSopenharmony_ci
145317fd14ceSopenharmony_ciint32_t AddDevAuthServiceToManager(uintptr_t serviceInstance)
145417fd14ceSopenharmony_ci{
145517fd14ceSopenharmony_ci    (void)serviceInstance;
145617fd14ceSopenharmony_ci    SAMGR_Bootstrap();
145717fd14ceSopenharmony_ci    InitCbStubTable();
145817fd14ceSopenharmony_ci    LOGI("AddSystemAbility to SA manager success");
145917fd14ceSopenharmony_ci    return HC_SUCCESS;
146017fd14ceSopenharmony_ci}
146117fd14ceSopenharmony_ci
146217fd14ceSopenharmony_ciint32_t IpcEncodeCallReply(uintptr_t replayCache, int32_t type, const uint8_t *result, int32_t resultSz)
146317fd14ceSopenharmony_ci{
146417fd14ceSopenharmony_ci    int32_t ret = HC_SUCCESS;
146517fd14ceSopenharmony_ci    IpcIo *replyParcel = NULL;
146617fd14ceSopenharmony_ci    unsigned long valZero = 0uL;
146717fd14ceSopenharmony_ci
146817fd14ceSopenharmony_ci    replyParcel = (IpcIo *)(replayCache);
146917fd14ceSopenharmony_ci    WriteInt32(replyParcel, type);
147017fd14ceSopenharmony_ci    bool value;
147117fd14ceSopenharmony_ci    if ((result != NULL) && (resultSz > 0)) {
147217fd14ceSopenharmony_ci        WriteUint32(replyParcel, (uint32_t)resultSz);
147317fd14ceSopenharmony_ci        value = WriteBuffer(replyParcel, (const void *)result, (uint32_t)resultSz);
147417fd14ceSopenharmony_ci    } else {
147517fd14ceSopenharmony_ci        WriteUint32(replyParcel, sizeof(valZero));
147617fd14ceSopenharmony_ci        value = WriteBuffer(replyParcel, (const void *)(&valZero), sizeof(valZero));
147717fd14ceSopenharmony_ci    }
147817fd14ceSopenharmony_ci    if (!value) {
147917fd14ceSopenharmony_ci        LOGE("encode call reply fail.");
148017fd14ceSopenharmony_ci        return HC_FALSE;
148117fd14ceSopenharmony_ci    }
148217fd14ceSopenharmony_ci    return ret;
148317fd14ceSopenharmony_ci}
148417fd14ceSopenharmony_ci
148517fd14ceSopenharmony_ciint32_t DecodeIpcData(uintptr_t data, int32_t *type, uint8_t **val, int32_t *valSz)
148617fd14ceSopenharmony_ci{
148717fd14ceSopenharmony_ci    IpcIo *dataPtr = NULL;
148817fd14ceSopenharmony_ci
148917fd14ceSopenharmony_ci    dataPtr = (IpcIo *)(data);
149017fd14ceSopenharmony_ci    if (dataPtr->bufferLeft <= 0) {
149117fd14ceSopenharmony_ci        return HC_SUCCESS;
149217fd14ceSopenharmony_ci    }
149317fd14ceSopenharmony_ci    ReadInt32(dataPtr, type);
149417fd14ceSopenharmony_ci    ReadUint32(dataPtr, (uint32_t *)valSz);
149517fd14ceSopenharmony_ci    *val = (uint8_t *)ReadBuffer(dataPtr, *valSz);
149617fd14ceSopenharmony_ci    return HC_SUCCESS;
149717fd14ceSopenharmony_ci}
149817fd14ceSopenharmony_ci
149917fd14ceSopenharmony_civoid DecodeCallReply(uintptr_t callCtx, IpcDataInfo *replyCache, int32_t cacheNum)
150017fd14ceSopenharmony_ci{
150117fd14ceSopenharmony_ci    int32_t i;
150217fd14ceSopenharmony_ci    int32_t ret;
150317fd14ceSopenharmony_ci    uint32_t replyLen;
150417fd14ceSopenharmony_ci
150517fd14ceSopenharmony_ci    ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
150617fd14ceSopenharmony_ci    ReadUint32(dataCache->reply, &replyLen);
150717fd14ceSopenharmony_ci    if (replyLen == 0) {
150817fd14ceSopenharmony_ci        return;
150917fd14ceSopenharmony_ci    }
151017fd14ceSopenharmony_ci
151117fd14ceSopenharmony_ci    for (i = 0; i < cacheNum; i++) {
151217fd14ceSopenharmony_ci        ret = DecodeIpcData((uintptr_t)(dataCache->reply),
151317fd14ceSopenharmony_ci            &(replyCache[i].type), &(replyCache[i].val), &(replyCache[i].valSz));
151417fd14ceSopenharmony_ci        if (ret != HC_SUCCESS) {
151517fd14ceSopenharmony_ci            return;
151617fd14ceSopenharmony_ci        }
151717fd14ceSopenharmony_ci    }
151817fd14ceSopenharmony_ci    return;
151917fd14ceSopenharmony_ci}
152017fd14ceSopenharmony_ci
152117fd14ceSopenharmony_cistatic bool IsTypeForSettingPtr(int32_t type)
152217fd14ceSopenharmony_ci{
152317fd14ceSopenharmony_ci    int32_t typeList[] = {
152417fd14ceSopenharmony_ci        PARAM_TYPE_APPID, PARAM_TYPE_DEV_AUTH_CB, PARAM_TYPE_LISTERNER, PARAM_TYPE_CREATE_PARAMS,
152517fd14ceSopenharmony_ci        PARAM_TYPE_GROUPID, PARAM_TYPE_UDID, PARAM_TYPE_ADD_PARAMS, PARAM_TYPE_DEL_PARAMS,
152617fd14ceSopenharmony_ci        PARAM_TYPE_QUERY_PARAMS, PARAM_TYPE_COMM_DATA, PARAM_TYPE_SESS_KEY,
152717fd14ceSopenharmony_ci        PARAM_TYPE_REQ_INFO, PARAM_TYPE_GROUP_INFO, PARAM_TYPE_AUTH_PARAMS, PARAM_TYPE_REQ_JSON,
152817fd14ceSopenharmony_ci        PARAM_TYPE_PSEUDONYM_ID, PARAM_TYPE_INDEX_KEY
152917fd14ceSopenharmony_ci    };
153017fd14ceSopenharmony_ci    int32_t i;
153117fd14ceSopenharmony_ci    int32_t n = sizeof(typeList) / sizeof(typeList[0]);
153217fd14ceSopenharmony_ci    for (i = 0; i < n; i++) {
153317fd14ceSopenharmony_ci        if (typeList[i] == type) {
153417fd14ceSopenharmony_ci            return true;
153517fd14ceSopenharmony_ci        }
153617fd14ceSopenharmony_ci    }
153717fd14ceSopenharmony_ci    return false;
153817fd14ceSopenharmony_ci}
153917fd14ceSopenharmony_ci
154017fd14ceSopenharmony_cistatic bool IsTypeForCpyData(int32_t type)
154117fd14ceSopenharmony_ci{
154217fd14ceSopenharmony_ci    int32_t typeList[] = {
154317fd14ceSopenharmony_ci        PARAM_TYPE_REQID, PARAM_TYPE_GROUP_TYPE, PARAM_TYPE_OPCODE, PARAM_TYPE_ERRCODE, PARAM_TYPE_OS_ACCOUNT_ID
154417fd14ceSopenharmony_ci    };
154517fd14ceSopenharmony_ci    int32_t i;
154617fd14ceSopenharmony_ci    int32_t n = sizeof(typeList) / sizeof(typeList[0]);
154717fd14ceSopenharmony_ci    for (i = 0; i < n; i++) {
154817fd14ceSopenharmony_ci        if (typeList[i] == type) {
154917fd14ceSopenharmony_ci            return true;
155017fd14ceSopenharmony_ci        }
155117fd14ceSopenharmony_ci    }
155217fd14ceSopenharmony_ci    return false;
155317fd14ceSopenharmony_ci}
155417fd14ceSopenharmony_ci
155517fd14ceSopenharmony_ciint32_t GetIpcRequestParamByType(const IpcDataInfo *ipcParams, int32_t paramNum,
155617fd14ceSopenharmony_ci    int32_t type, uint8_t *paramCache, int32_t *cacheLen)
155717fd14ceSopenharmony_ci{
155817fd14ceSopenharmony_ci    int32_t i;
155917fd14ceSopenharmony_ci    int32_t ret = HC_ERR_IPC_BAD_MSG_TYPE;
156017fd14ceSopenharmony_ci    errno_t eno;
156117fd14ceSopenharmony_ci
156217fd14ceSopenharmony_ci    for (i = 0; i < paramNum; i++) {
156317fd14ceSopenharmony_ci        if (ipcParams[i].type != type) {
156417fd14ceSopenharmony_ci            continue;
156517fd14ceSopenharmony_ci        }
156617fd14ceSopenharmony_ci        ret = HC_SUCCESS;
156717fd14ceSopenharmony_ci        if (IsTypeForSettingPtr(type)) {
156817fd14ceSopenharmony_ci            *(uint8_t **)paramCache = ipcParams[i].val;
156917fd14ceSopenharmony_ci            if (cacheLen != NULL) {
157017fd14ceSopenharmony_ci                *cacheLen = ipcParams[i].valSz;
157117fd14ceSopenharmony_ci            }
157217fd14ceSopenharmony_ci            break;
157317fd14ceSopenharmony_ci        }
157417fd14ceSopenharmony_ci        if (IsTypeForCpyData(type)) {
157517fd14ceSopenharmony_ci            if ((ipcParams[i].val == NULL) || (ipcParams[i].valSz <= 0)) {
157617fd14ceSopenharmony_ci                ret = HC_ERR_INVALID_PARAMS;
157717fd14ceSopenharmony_ci                break;
157817fd14ceSopenharmony_ci            }
157917fd14ceSopenharmony_ci            eno = memcpy_s(paramCache, *cacheLen, ipcParams[i].val, ipcParams[i].valSz);
158017fd14ceSopenharmony_ci            if (eno != EOK) {
158117fd14ceSopenharmony_ci                ret = HC_ERR_MEMORY_COPY;
158217fd14ceSopenharmony_ci            }
158317fd14ceSopenharmony_ci            *cacheLen = ipcParams[i].valSz;
158417fd14ceSopenharmony_ci            break;
158517fd14ceSopenharmony_ci        }
158617fd14ceSopenharmony_ci        if ((type == PARAM_TYPE_CB_OBJECT) && (*(uint32_t *)cacheLen >= sizeof(ipcParams[i].idx))) {
158717fd14ceSopenharmony_ci            *(int32_t *)paramCache = ipcParams[i].idx;
158817fd14ceSopenharmony_ci        }
158917fd14ceSopenharmony_ci        break;
159017fd14ceSopenharmony_ci    }
159117fd14ceSopenharmony_ci    return ret;
159217fd14ceSopenharmony_ci}
159317fd14ceSopenharmony_ci
159417fd14ceSopenharmony_cibool IsCallbackMethod(int32_t methodId)
159517fd14ceSopenharmony_ci{
159617fd14ceSopenharmony_ci    if ((methodId == IPC_CALL_ID_REG_CB) || (methodId == IPC_CALL_ID_REG_LISTENER) ||
159717fd14ceSopenharmony_ci        (methodId == IPC_CALL_ID_GA_PROC_DATA) || (methodId == IPC_CALL_ID_AUTH_DEVICE)) {
159817fd14ceSopenharmony_ci        return true;
159917fd14ceSopenharmony_ci    }
160017fd14ceSopenharmony_ci    return false;
160117fd14ceSopenharmony_ci}
160217fd14ceSopenharmony_ci
160317fd14ceSopenharmony_ciIpcIo *InitIpcDataCache(uint32_t buffSz)
160417fd14ceSopenharmony_ci{
160517fd14ceSopenharmony_ci    IpcIo *ioPtr = NULL;
160617fd14ceSopenharmony_ci    uint8_t *buf = NULL;
160717fd14ceSopenharmony_ci    uint32_t len;
160817fd14ceSopenharmony_ci
160917fd14ceSopenharmony_ci    if (buffSz == 0) {
161017fd14ceSopenharmony_ci        LOGE("invalid param");
161117fd14ceSopenharmony_ci        return NULL;
161217fd14ceSopenharmony_ci    }
161317fd14ceSopenharmony_ci    len = sizeof(IpcIo) + buffSz;
161417fd14ceSopenharmony_ci    ioPtr = (IpcIo *)HcMalloc(len, 0);
161517fd14ceSopenharmony_ci    if (ioPtr == NULL) {
161617fd14ceSopenharmony_ci        LOGE("alloc memory failed");
161717fd14ceSopenharmony_ci        return NULL;
161817fd14ceSopenharmony_ci    }
161917fd14ceSopenharmony_ci    buf = (uint8_t *)ioPtr + sizeof(IpcIo);
162017fd14ceSopenharmony_ci    /* ipcio inited with 4 svc objects */
162117fd14ceSopenharmony_ci    IpcIoInit(ioPtr, (void *)buf, buffSz, 4);
162217fd14ceSopenharmony_ci    return ioPtr;
162317fd14ceSopenharmony_ci}
162417fd14ceSopenharmony_ci
162517fd14ceSopenharmony_ciint32_t GetIpcIoDataLength(const IpcIo *io)
162617fd14ceSopenharmony_ci{
162717fd14ceSopenharmony_ci    uintptr_t beginPos;
162817fd14ceSopenharmony_ci    uintptr_t endPos;
162917fd14ceSopenharmony_ci
163017fd14ceSopenharmony_ci    if (io == NULL) {
163117fd14ceSopenharmony_ci        return 0;
163217fd14ceSopenharmony_ci    }
163317fd14ceSopenharmony_ci    beginPos = (uintptr_t)(io->bufferBase + IpcIoBufferOffset());
163417fd14ceSopenharmony_ci    endPos = (uintptr_t)(io->bufferCur);
163517fd14ceSopenharmony_ci    return (endPos <= beginPos) ? 0 : (int32_t)(endPos - beginPos);
163617fd14ceSopenharmony_ci}
163717fd14ceSopenharmony_ci
163817fd14ceSopenharmony_civoid ShowIpcSvcInfo(const SvcIdentity *svc)
163917fd14ceSopenharmony_ci{
164017fd14ceSopenharmony_ci    LOGI("svc information - handle(%u), token(%u), cookie(%u)",
164117fd14ceSopenharmony_ci        svc->handle, svc->token, svc->cookie);
164217fd14ceSopenharmony_ci}
164317fd14ceSopenharmony_ci
164417fd14ceSopenharmony_ciint32_t InitProxyAdapt(void)
164517fd14ceSopenharmony_ci{
164617fd14ceSopenharmony_ci    if (g_proxyInstance == NULL) {
164717fd14ceSopenharmony_ci        g_proxyInstance = (IClientProxy *)GetProxyInstance(DEV_AUTH_SERVICE_NAME);
164817fd14ceSopenharmony_ci        if (g_proxyInstance == NULL) {
164917fd14ceSopenharmony_ci            LOGE("get proxy instance failed");
165017fd14ceSopenharmony_ci            return HC_ERR_IPC_INIT;
165117fd14ceSopenharmony_ci        }
165217fd14ceSopenharmony_ci    }
165317fd14ceSopenharmony_ci
165417fd14ceSopenharmony_ci    if (!g_sdkCbStub.registered) {
165517fd14ceSopenharmony_ci        g_objectStub.func = CbStubOnRemoteRequest;
165617fd14ceSopenharmony_ci        g_objectStub.args = NULL;
165717fd14ceSopenharmony_ci        g_objectStub.isRemote = false;
165817fd14ceSopenharmony_ci        g_sdkCbStub.stubIdentity.handle = IPC_INVALID_HANDLE;
165917fd14ceSopenharmony_ci        g_sdkCbStub.stubIdentity.token = SERVICE_TYPE_ANONYMOUS;
166017fd14ceSopenharmony_ci        g_sdkCbStub.stubIdentity.cookie = (uintptr_t)&g_objectStub;
166117fd14ceSopenharmony_ci
166217fd14ceSopenharmony_ci        ShowIpcSvcInfo(&(g_sdkCbStub.stubIdentity));
166317fd14ceSopenharmony_ci        g_sdkCbStub.registered = true;
166417fd14ceSopenharmony_ci    }
166517fd14ceSopenharmony_ci    return HC_SUCCESS;
166617fd14ceSopenharmony_ci}
166717fd14ceSopenharmony_ci
166817fd14ceSopenharmony_civoid UnInitProxyAdapt(void)
166917fd14ceSopenharmony_ci{
167017fd14ceSopenharmony_ci    g_proxyInstance = NULL;
167117fd14ceSopenharmony_ci    g_sdkCbStub.registered = false;
167217fd14ceSopenharmony_ci    return;
167317fd14ceSopenharmony_ci}
167417fd14ceSopenharmony_ci
167517fd14ceSopenharmony_ciint32_t IpcIoBufferOffset(void)
167617fd14ceSopenharmony_ci{
167717fd14ceSopenharmony_ci    int8_t buf[64]; /* 64 buffer size */
167817fd14ceSopenharmony_ci    IpcIo ioCache;
167917fd14ceSopenharmony_ci
168017fd14ceSopenharmony_ci    IpcIoInit(&ioCache, (void *)buf, sizeof(buf), 0);
168117fd14ceSopenharmony_ci    if (ioCache.bufferCur <= ioCache.bufferBase) {
168217fd14ceSopenharmony_ci        return 0;
168317fd14ceSopenharmony_ci    }
168417fd14ceSopenharmony_ci    return (int32_t)((uintptr_t)(ioCache.bufferCur) - (uintptr_t)(ioCache.bufferBase));
168517fd14ceSopenharmony_ci}
168617fd14ceSopenharmony_ci
168717fd14ceSopenharmony_ci#ifdef __cplusplus
168817fd14ceSopenharmony_ci}
168917fd14ceSopenharmony_ci#endif
169017fd14ceSopenharmony_ci
1691