diff --git a/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c b/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c index d57e61f67c4e0f8d0165518f998327a0c203d001..41fbc1efa56afc9c5a9b9aa0334ad6b56ac87210 100644 --- a/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c +++ b/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c @@ -42,15 +42,19 @@ static const int GROUP_TYPE_ACROSS_ACCOUNT_GROUP = 1282; static const int AUTH_FORM_IDENTICAL_ACCOUNT_GROUP = 1; static const int AUTH_FORM_PEER_TO_PEER_GROUP = 0; static const int AUTH_FORM_ACROSS_ACCOUNT_GROUP = 2; +static const int IMPORT_SELF_CREDENTIAL = 0; +static const int DELETE_SELF_CREDENTIAL = 1; static const DeviceGroupManager *g_deviceGroupManager = NULL; static const GroupAuthManager *g_groupAuthManager = NULL; static UINT32 g_createGroupSem = LOSCFG_BASE_IPC_SEM_LIMIT; +static UINT32 g_deleteGroupSem = LOSCFG_BASE_IPC_SEM_LIMIT; // It is only the id of a group operator request an has no security risk. static const int64_t CREATE_GROUP_REQUESTID = 159357462; static bool g_createGroupFlag = true; // strict operating timings do not require lock protection +static bool g_deleteGroupFlag = true; // maximum length of JSON character string input for credential operation static const UINT32 DM_MAX_REQUEST_JSON_LEN = 4096; @@ -102,6 +106,11 @@ int InitHichainModle(void) DMLOGE("failed to create group semaphore with ret: %d.", ret); retValue = ERR_DM_LITEOS_CREATE_MUTEX_OR_SEM; } + osRet = LOS_BinarySemCreate(HICHAIN_SEM_INIT_COUNT, &g_deleteGroupSem); + if (osRet != LOS_OK) { + DMLOGE("failed to create group semaphore with ret: %d.", ret); + retValue = ERR_DM_LITEOS_CREATE_MUTEX_OR_SEM; + } if (retValue != DM_OK) { DMLOGE("failed to init hichain modle with retValue: %d.", retValue); UnInitHichainModle(); @@ -134,6 +143,13 @@ int UnInitHichainModle(void) } else { g_createGroupSem = LOSCFG_BASE_IPC_SEM_LIMIT; } + osRet = LOS_SemDelete(g_deleteGroupSem); + if (osRet != LOS_OK && osRet != LOS_ERRNO_SEM_INVALID) { + DMLOGE("failed to delete group semaphore with ret: %d.", osRet); + retValue = ERR_DM_LITEOS_DELETE_MUTEX_OR_SEM; + } else { + g_deleteGroupSem = LOSCFG_BASE_IPC_SEM_LIMIT; + } if (retValue != DM_OK) { DMLOGE("failed to uninit hichain modle with retValue: %d.", retValue); return retValue; @@ -227,11 +243,22 @@ static void OnFinish(int64_t requestId, int operationCode, const char *returnDat #ifdef MINE_HARMONY_HICHAIN DMLOGI("start to notify the asynchronous operation group of the successful result."); if (requestId == CREATE_GROUP_REQUESTID) { - g_createGroupFlag = true; - UINT32 osRet = LOS_SemPost(g_createGroupSem); - if (osRet != LOS_OK) { - DMLOGE("failed to post create group semaphore with ret: %u.", osRet); - return; + if (operationCode == IMPORT_SELF_CREDENTIAL) { + g_createGroupFlag = true; + DMLOGI("create group successfully."); + UINT32 osRet = LOS_SemPost(g_createGroupSem); + if (osRet != LOS_OK) { + DMLOGE("failed to post create group semaphore with ret: %u.", osRet); + return; + } + } else if (operationCode == DELETE_SELF_CREDENTIAL) { + g_deleteGroupFlag = true; + DMLOGI("delete group successfully."); + UINT32 osRet = LOS_SemPost(g_deleteGroupSem); + if (osRet != LOS_OK) { + DMLOGE("failed to post delete group semaphore with ret: %u.", osRet); + return; + } } } #endif @@ -243,11 +270,22 @@ static void OnError(int64_t requestId, int operationCode, int errorCode, const c (void)errorReturn; #ifdef MINE_HARMONY_HICHAIN if (requestId == CREATE_GROUP_REQUESTID) { - g_createGroupFlag = false; - UINT32 osRet = LOS_SemPost(g_createGroupSem); - if (osRet != LOS_OK) { - DMLOGE("failed to post create group semaphore with ret: %u.", osRet); - return; + if (operationCode == IMPORT_SELF_CREDENTIAL) { + g_createGroupFlag = false; + DMLOGE("failed to create group."); + UINT32 osRet = LOS_SemPost(g_createGroupSem); + if (osRet != LOS_OK) { + DMLOGE("failed to post create group semaphore with ret: %u.", osRet); + return; + } + } else if (operationCode == DELETE_SELF_CREDENTIAL) { + g_deleteGroupFlag = false; + DMLOGE("failed to delete group."); + UINT32 osRet = LOS_SemPost(g_deleteGroupSem); + if (osRet != LOS_OK) { + DMLOGE("failed to post delete group semaphore with ret: %u.", osRet); + return; + } } } #endif @@ -460,6 +498,11 @@ static int DeleteCredentialAndGroup(void) DMLOGE("[hichian]failed to delete hichain credential and group with ret: %d.", retValue); return retValue; } + UINT32 osRet = LOS_SemPend(g_deleteGroupSem, HICHAIN_DELAY_TICK_COUNT); + if (osRet != LOS_OK || (osRet == LOS_OK && !g_deleteGroupFlag)) { + DMLOGE("failed to acquire semaphore or delete group with ret: %d or callback notify.", (int)osRet); + return retValue; + } return DM_OK; }