diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index cf177158adaf63307f209f59b12556e1700d4d78..5097fa9c41858bddfaf635fbd5fff0e212c653f7 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -53,6 +53,11 @@ if (defined(ohos_lite)) { "//third_party/bounds_checking_function:libsec_static", ] + if (defined(harmony_industry_project) && + harmony_industry_project == "MineHarmony") { + defines = [ "MINE_HARMONY_HICHAIN" ] + } + cflags = [ "-Wall", "-O2", diff --git a/interfaces/inner_kits/native_cpp/include/mini/device_manager_impl_lite_m.h b/interfaces/inner_kits/native_cpp/include/mini/device_manager_impl_lite_m.h index a8c5e7a2d6b8ddd61c6722775a41e35df463db14..5a58becddffa2d079ec21273787f2cbd163d7540 100644 --- a/interfaces/inner_kits/native_cpp/include/mini/device_manager_impl_lite_m.h +++ b/interfaces/inner_kits/native_cpp/include/mini/device_manager_impl_lite_m.h @@ -19,7 +19,9 @@ #include #include "device_manager_common.h" +#ifdef MINE_HARMONY_SOFTBUS #include "softbus_common.h" +#endif #ifdef __cplusplus extern "C" { @@ -98,7 +100,9 @@ typedef struct { char networkId[DM_MAX_DEVICE_NETWORKID_LEN + 1]; char deviceName[DM_MAX_DEVICE_NAME_LEN + 1]; unsigned int deviceTypeId; +#ifdef MINE_HARMONY_SOFTBUS ConnectionAddr connectAddr; +#endif int range; int credible; bool isLocalExistCredential; @@ -143,7 +147,9 @@ int StopDiscovering(const char *pkgName, const int subscribeId); int BindTarget(const char *pkgName, const char *deviceId, const char *bindParam, OnBindResult cb); int UnBindTarget(const char *pkgName, const char *networkId); void FreeJsonStringMemory(char **jsonStr); -int RequestCredential(char **returnJsonStr); +// returned string, which needs to be manually release by the invoker. +int RequestCredential(const char *reqJsonStr, char **returnJsonStr); +// returned string, which needs to be manually release by the invoker. int CheckCredential(const char *reqJsonStr, char **returnJsonStr); int ImportCredential(const char *reqJsonStr, char **returnJsonStr); int DeleteCredential(const char *reqJsonStr, char **returnJsonStr); diff --git a/interfaces/inner_kits/native_cpp/include/mini/hichain_adapter.h b/interfaces/inner_kits/native_cpp/include/mini/hichain_adapter.h index 2e16ebec390e5a56729b919e20a4fddfc694980f..220ba99ca234fde7113e1ba50b8a05c5102b198a 100644 --- a/interfaces/inner_kits/native_cpp/include/mini/hichain_adapter.h +++ b/interfaces/inner_kits/native_cpp/include/mini/hichain_adapter.h @@ -25,8 +25,7 @@ extern "C" { int InitHichainModle(void); int UnInitHichainModle(void); int GetAuthFormByDeviceId(const char *deviceId, int authForm); -bool IsHichainCredentialExist(void); -int RequestHichainCredential(char **returnJsonStr); +int RequestHichainCredential(const char *reqJsonStr, char **returnJsonStr); void FreeHichainJsonStringMemory(char **jsonStr); int CheckHichainCredential(const char *reqJsonStr, char **returnJsonStr); int ImportHichainCredential(const char *reqJsonStr, char **returnJsonStr); diff --git a/interfaces/inner_kits/native_cpp/src/mini/device_manager_impl_lite_m.c b/interfaces/inner_kits/native_cpp/src/mini/device_manager_impl_lite_m.c index bf66373e671752d62b111bdd178b9214022d24de..b5876f9316d4ca15d40c610fe513e0ad7e5b5fad 100644 --- a/interfaces/inner_kits/native_cpp/src/mini/device_manager_impl_lite_m.c +++ b/interfaces/inner_kits/native_cpp/src/mini/device_manager_impl_lite_m.c @@ -38,11 +38,14 @@ int InitDeviceManager(void) DMLOGI("device manager module has been initialized."); return DM_OK; } - int retValue = InitSoftbusModle(); + int retValue = ERR_DM_FAILED; +#ifdef MINE_HARMONY_SOFTBUS + retValue = InitSoftbusModle(); if (retValue != DM_OK) { DMLOGE("failed to init softbus with ret: %d.", retValue); return retValue; } +#endif retValue = InitHichainModle(); if (retValue != DM_OK) { DMLOGE("failed to init hichain with ret: %d.", retValue); @@ -64,7 +67,9 @@ int UnInitDeviceManager(void) DMLOGI("device manager module not initialized."); return DM_OK; } +#ifdef MINE_HARMONY_SOFTBUS UnInitSoftbusModle(); +#endif UnInitHichainModle(); g_deviceManagerInitFlag = false; return DM_OK; @@ -224,13 +229,13 @@ int UnBindTarget(const char *pkgName, const char *networkId) return SoftbusUnBindTarget(pkgName, networkId); } -int RequestCredential(char **returnJsonStr) +int RequestCredential(const char *reqJsonStr, char **returnJsonStr) { if (!g_deviceManagerInitFlag) { DMLOGE("device manager module is not initialized."); return ERR_DM_NO_INIT; } - return RequestHichainCredential(returnJsonStr); + return RequestHichainCredential(reqJsonStr, returnJsonStr); } void FreeJsonStringMemory(char **jsonStr) 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 e453182aaeb95d1e56a5d485ab116a5e69463a6b..d57e61f67c4e0f8d0165518f998327a0c203d001 100644 --- a/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c +++ b/interfaces/inner_kits/native_cpp/src/mini/hichain_adapter.c @@ -31,8 +31,9 @@ static const UINT32 HICHAIN_SEM_INIT_COUNT = 0; static const char * const HICHAIN_PKG_NAME = "com.ohos.devicemanager"; static const char * const FILED_GROUP_TYPE = "groupType"; -static const char * const DM_GROUP_NAME = "devicemanager"; static const char * const FIELD_CREDENTIAL_EXISTS = "isCredentialExists"; +static const char* FIELD_DEV_CLOUDCRED = "devCloudCred"; +static const char* FIELD_CREDENTIAL = "credential"; static const int GROUP_TYPE_INVALID_GROUP = -1; static const int GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP = 1; @@ -54,12 +55,16 @@ static bool g_createGroupFlag = true; // strict operating timings do not re // maximum length of JSON character string input for credential operation static const UINT32 DM_MAX_REQUEST_JSON_LEN = 4096; -static int DeleteCredentialAndGroup(char **returnJsonStr); +static int DeleteCredentialAndGroup(void); static int ImportCredentialAndGroup(const char *reqJsonStr, char **returnJsonStr); -static int GetUserId(const cJSON *const root, char *userId); -static int CreateGroupJsonAddObject(const cJSON *const root, cJSON **jsonRoot); static void OnFinish(int64_t requestId, int operationCode, const char *returnData); static void OnError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn); +static int GetIdenticalAccountGroupInfo(char **groupVec); +static char *GetIdeAccountQueryJson(void); +static char *GetCredentialReqJsonString(const char *reqJsonStr); +static char *GetGroupIdReqJsonString(const char *groupId); +static bool IsHichainCredentialExist(void); +static char *GetIdentialAccountGroupIdJsonStr(void); static int ParseReturnJson(const char *returnJsonStr, int authForm); @@ -219,8 +224,7 @@ int GetAuthFormByDeviceId(const char *deviceId, int authForm) static void OnFinish(int64_t requestId, int operationCode, const char *returnData) { (void)returnData; - DMLOGI("OnFinish callback complete with requestId: %lld, operation: %d.", requestId, operationCode); -#ifdef MINE_HARMONY +#ifdef MINE_HARMONY_HICHAIN DMLOGI("start to notify the asynchronous operation group of the successful result."); if (requestId == CREATE_GROUP_REQUESTID) { g_createGroupFlag = true; @@ -231,14 +235,13 @@ static void OnFinish(int64_t requestId, int operationCode, const char *returnDat } } #endif + DMLOGI("OnFinish callback complete with requestId: %lld, operation: %d.", requestId, operationCode); } static void OnError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn) { (void)errorReturn; - DMLOGI("OnError callback complete with requestId: %lld, operation: %d, errorCode: %d.", - requestId, operationCode, errorCode); -#ifdef MINE_HARMONY +#ifdef MINE_HARMONY_HICHAIN if (requestId == CREATE_GROUP_REQUESTID) { g_createGroupFlag = false; UINT32 osRet = LOS_SemPost(g_createGroupSem); @@ -248,53 +251,63 @@ static void OnError(int64_t requestId, int operationCode, int errorCode, const c } } #endif + DMLOGI("OnError callback complete with requestId: %lld, operation: %d, errorCode: %d.", + requestId, operationCode, errorCode); } -int RequestHichainCredential(char **returnJsonStr) +// returned string, which needs to be manually release by the invoker. +int RequestHichainCredential(const char *reqJsonStr, char **returnJsonStr) { - if (returnJsonStr == NULL || g_deviceGroupManager == NULL) { - DMLOGE("returnJsonStr or g_deviceGroupManager is NULL."); + if (reqJsonStr == NULL || returnJsonStr == NULL || g_deviceGroupManager == NULL) { + DMLOGE("reqJsonStr, returnJsonStr or g_deviceGroupManager is NULL."); return ERR_DM_INPUT_INVALID_VALUE; } DMLOGI("start to request hichain credential."); -#ifdef MINE_HARMONY - int retValue = g_deviceGroupManager->getRegisterInfo(returnJsonStr); + int retValue = g_deviceGroupManager->getRegisterInfo(reqJsonStr, returnJsonStr); if (retValue != HC_SUCCESS || returnJsonStr == NULL || *returnJsonStr == NULL) { DMLOGE("failed to request hichain credential from hichain with ret: %d.", retValue); return ERR_DM_HICHAIN_GET_CREDENTIAL_INFO; } DMLOGI("request device credential info from hichain successfully with len: %u.", strlen(*returnJsonStr)); -#else - DMLOGI("request device credential info from hichain successfully."); -#endif return DM_OK; } +// returned string, which needs to be manually release by the invoker. int CheckHichainCredential(const char *reqJsonStr, char **returnJsonStr) { if (reqJsonStr == NULL || returnJsonStr == NULL || g_deviceGroupManager == NULL) { DMLOGE("reqJsonStr or returnJsonStr or g_deviceGroupManager is NULL."); return ERR_DM_INPUT_INVALID_VALUE; } - - size_t checkJsonStrLen = strlen(reqJsonStr); - if (checkJsonStrLen == 0 || checkJsonStrLen >= DM_MAX_REQUEST_JSON_LEN) { - DMLOGE("reqJsonStr is not meet the requirements with checkJsonStrLen: %u.", checkJsonStrLen); - return ERR_DM_INPUT_INVALID_VALUE; + int retValue = ERR_DM_FAILED; + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + DMLOGE("failed to create cjson object."); + return retValue; } - -#ifdef MINE_HARMONY - DMLOGI("start to check device credential info."); - int retValue = g_deviceGroupManager->processCredential(CREDENTIAL_QUERY, reqJsonStr, returnJsonStr); - if (retValue != HC_SUCCESS || returnJsonStr == NULL || *returnJsonStr == NULL) { - DMLOGE("failed to check device credential info with ret: %d.", retValue); - return ERR_DM_HICHAIN_GET_CREDENTIAL_INFO; + if (IsHichainCredentialExist()) { + if (cJSON_AddTrueToObject(root, FIELD_CREDENTIAL_EXISTS) == NULL) { + DMLOGE("failed to add true to object."); + cJSON_Delete(root); + return retValue; + } + } else { + if (cJSON_AddFalseToObject(root, FIELD_CREDENTIAL_EXISTS) == NULL) { + DMLOGE("failed to add false to object."); + cJSON_Delete(root); + return retValue; + } } - DMLOGI("check device credential info successfully with len: %u.", strlen(*returnJsonStr)); -#else - DMLOGI("check device credential info successfully."); -#endif + char *returnStr = cJSON_PrintUnformatted(root); + if (returnStr == NULL) { + DMLOGE("failed to print string from cjson object."); + cJSON_Delete(root); + return retValue; + } + *returnJsonStr = returnStr; + cJSON_Delete(root); + DMLOGI("check device credential info successfully with JsonStrLen:%u.", strlen(returnStr)); return DM_OK; } @@ -318,7 +331,6 @@ int ImportHichainCredential(const char *reqJsonStr, char **returnJsonStr) DMLOGI("start to import device credential info to hichain."); if (ImportCredentialAndGroup(reqJsonStr, returnJsonStr) != DM_OK) { DMLOGE("failed to import hichain credential and create group."); - DeleteCredentialAndGroup(returnJsonStr); return ERR_DM_HICHAIN_IMPORT_CREDENTIAL; } DMLOGI("import device credential info to hichain successfully."); @@ -338,7 +350,7 @@ int DeleteHichainCredential(const char *reqJsonStr, char **returnJsonStr) } DMLOGI("start to delete device credential info from hichain."); - if (DeleteCredentialAndGroup(returnJsonStr) != DM_OK) { + if (DeleteCredentialAndGroup() != DM_OK) { DMLOGE("failed to delete device credential from hichain."); return ERR_DM_HICHAIN_DELETE_CREDENTIAL; } @@ -349,106 +361,25 @@ int DeleteHichainCredential(const char *reqJsonStr, char **returnJsonStr) void FreeHichainJsonStringMemory(char **jsonStr) { if (jsonStr == NULL || *jsonStr == NULL || g_deviceGroupManager == NULL) { - DMLOGE("jsonStr or *jsonStr or g_deviceGroupManager is NULL"); + DMLOGE("jsonStr or *jsonStr or g_deviceGroupManager is NULL."); return; } g_deviceGroupManager->destroyInfo(jsonStr); } -// note: Internal interfaces ensure the validity of input parameters. -static int CreateGroupJsonAddObject(const cJSON *const root, cJSON **jsonRoot) -{ - cJSON *groupRoot = NULL; - char deviceUdid[DM_MAX_DEVICE_UDID_LEN + 1] = {0}; - char userId[DM_MAX_USER_ID_LEN + 1] = {0}; - - int retValue = GetDevUdid(deviceUdid, DM_MAX_DEVICE_UDID_LEN); - if (retValue != 0) { - DMLOGE("failed to get local device udid with ret: %d.", retValue); - return ERR_DM_FAILED; - } - if (GetUserId(root, userId) != DM_OK) { - DMLOGE("failed to get credential userId."); - return ERR_DM_FAILED; - } - - do { - groupRoot = cJSON_CreateObject(); - if (groupRoot == NULL) { - DMLOGE("failed to create cjson object."); - break; - } - if (cJSON_AddStringToObject(groupRoot, FIELD_DEVICE_ID, deviceUdid) == NULL) { - DMLOGE("failed to add groupid string to cjson object."); - break; - } - if (cJSON_AddStringToObject(groupRoot, FIELD_GROUP_NAME, DM_GROUP_NAME) == NULL) { - DMLOGE("failed to add group name string to cjson object."); - break; - } - if (cJSON_AddNumberToObject(groupRoot, FIELD_GROUP_TYPE, IDENTICAL_ACCOUNT_GROUP) == NULL) { - DMLOGE("failed to add groupType number to cjson object."); - break; - } - if (cJSON_AddStringToObject(groupRoot, FIELD_USER_ID, (const char*)userId) == NULL) { - DMLOGE("failed to add group userId string to cjson object."); - break; - } - *jsonRoot = groupRoot; - return DM_OK; - } while (false); - - cJSON_Delete(groupRoot); - return ERR_DM_FAILED; -} - -// note: Internal interfaces ensure the validity of input parameters. -static int GetUserId(const cJSON * const root, char *userId) -{ - cJSON *object = cJSON_GetObjectItem(root, FIELD_USER_ID); - if (object == NULL || !cJSON_IsString(object)) { - DMLOGE("failed to get cjson object or type is not string."); - return ERR_DM_FAILED; - } - size_t stringLen = strlen(object->valuestring); - if (stringLen > DM_MAX_USER_ID_LEN) { - DMLOGE("userId in the imported credential is too long with lenght: %u.", stringLen); - return ERR_DM_FAILED; - } - for (size_t i = 0; i < stringLen; i++) { - userId[i] = object->valuestring[i]; - } - return DM_OK; -} - static int ImportCredentialAndGroup(const char *reqJsonStr, char **returnJsonStr) { - cJSON *root = NULL; - cJSON *groupRoot = NULL; - char *createParams = NULL; int retValue = ERR_DM_FAILED; -#ifdef MINE_HARMONY do { - root = cJSON_Parse(reqJsonStr); - if (root == NULL) { - DMLOGE("failed to create cjson object."); - break; - } - int ret = g_deviceGroupManager->processCredential(CREDENTIAL_SAVE, reqJsonStr, returnJsonStr); - if (ret != HC_SUCCESS || returnJsonStr == NULL || *returnJsonStr == NULL) { - DMLOGE("failed to import credential info to hichain with ret: %d.", ret); - break; - } - if (CreateGroupJsonAddObject(root, &groupRoot) != DM_OK) { - DMLOGE("failed to add object to cjson string."); - break; - } - createParams = cJSON_Print(groupRoot); + char *createParams = GetCredentialReqJsonString(reqJsonStr); if (createParams == NULL) { - DMLOGE("failed to print string from cjson object."); + DMLOGE("get credential failed."); break; } - ret = g_deviceGroupManager->createGroup(CREATE_GROUP_REQUESTID, HICHAIN_PKG_NAME, createParams); + int osAccountUserId = 0; + int ret = g_deviceGroupManager->createGroup(osAccountUserId, CREATE_GROUP_REQUESTID, HICHAIN_PKG_NAME, + createParams); + cJSON_free(createParams); if (ret != HC_SUCCESS) { DMLOGE("failed to create hichain group with ret: %d.", ret); break; @@ -461,95 +392,201 @@ static int ImportCredentialAndGroup(const char *reqJsonStr, char **returnJsonStr retValue = DM_OK; } while (false); - cJSON_Delete(root); - cJSON_Delete(groupRoot); - cJSON_free(createParams); return retValue; -#else - return DM_OK; -#endif } -static int DeleteCredentialAndGroup(char **returnJsonStr) +// returned string, which needs to be manually release by the invoker. +char *GetCredentialReqJsonString(const char *reqJsonStr) { - cJSON *root = NULL; - char *deleteParams = NULL; - int retValue = ERR_DM_FAILED; - -#ifdef MINE_HARMONY + cJSON *root = cJSON_Parse(reqJsonStr); + cJSON *createGroupObject = cJSON_CreateObject(); + cJSON *credentailObject = cJSON_CreateObject(); + char *value = NULL; do { - root = cJSON_CreateObject(); - if (root == NULL) { + if (root == NULL || createGroupObject == NULL || credentailObject == NULL) { DMLOGE("failed to create cjson object."); break; } - if (cJSON_AddTrueToObject(root, FIELD_IS_DELETE_ALL) == NULL) { - DMLOGE("failed to add delete all boolean to cjson object."); + cJSON *credentialTypeObj = cJSON_GetObjectItem(root, FIELD_CREDENTIAL_TYPE); + cJSON *groupTypeObj = cJSON_GetObjectItem(root, FIELD_GROUP_TYPE); + if (credentialTypeObj == NULL || !cJSON_IsNumber(credentialTypeObj) || + groupTypeObj == NULL || !cJSON_IsNumber(groupTypeObj)) { + DMLOGE("failed to get credentialType or groupType object."); break; } - deleteParams = cJSON_Print(root); - if (deleteParams == NULL) { - DMLOGE("failed to print string from cjson object."); + cJSON *devCloudCredObj = cJSON_GetObjectItem(root, FIELD_DEV_CLOUDCRED); + cJSON *userIdObj = cJSON_GetObjectItem(root, FIELD_USER_ID); + if (devCloudCredObj == NULL || userIdObj == NULL || !cJSON_IsString(userIdObj)) { + DMLOGE("failed to get devCloudCred or userId object."); break; } - int ret = g_deviceGroupManager->processCredential(CREDENTIAL_CLEAR, deleteParams, returnJsonStr); - if (ret != HC_SUCCESS || returnJsonStr == NULL || *returnJsonStr == NULL) { - DMLOGE("failed to delete hichain credential with ret: %d.", ret); + if ((cJSON_AddNumberToObject(createGroupObject, FIELD_GROUP_TYPE, groupTypeObj->valueint) == NULL) || + (cJSON_AddStringToObject(createGroupObject, FIELD_USER_ID, userIdObj->valuestring) == NULL) || + (cJSON_AddNumberToObject(credentailObject, FIELD_CREDENTIAL_TYPE, credentialTypeObj->valueint) == NULL) || + !cJSON_AddItemReferenceToObject(credentailObject, FIELD_DEV_CLOUDCRED, devCloudCredObj)) { + DMLOGE("failed to add groupTye, userId, credentialType or devCloudcred to object."); + break; + } + if (!cJSON_AddItemReferenceToObject(createGroupObject, FIELD_CREDENTIAL, credentailObject)) { + DMLOGE("failed to add credential item to object."); + break; + } + value = cJSON_PrintUnformatted(createGroupObject); + if (value == NULL) { + DMLOGE("failed to get credential json string."); break; } - retValue = DM_OK; } while (false); cJSON_Delete(root); - cJSON_free(deleteParams); - return retValue; -#else + cJSON_Delete(createGroupObject); + cJSON_Delete(credentailObject); + return value; +} + +static int DeleteCredentialAndGroup(void) +{ + int retValue = ERR_DM_FAILED; + char *groupIdJsonStr = GetIdentialAccountGroupIdJsonStr(); + if (groupIdJsonStr == NULL) { + DMLOGE("get idential account group id failed."); + return retValue; + } + int osAccountUserId = 0; + retValue = g_deviceGroupManager->deleteGroup(osAccountUserId, CREATE_GROUP_REQUESTID, HICHAIN_PKG_NAME, + groupIdJsonStr); + cJSON_free(groupIdJsonStr); + if (retValue != HC_SUCCESS) { + DMLOGE("[hichian]failed to delete hichain credential and group with ret: %d.", retValue); + return retValue; + } return DM_OK; -#endif +} + +// returned string, which needs to be manually release by the invoker. +char *GetIdentialAccountGroupIdJsonStr(void) +{ + char *groupVecStr = NULL; + char *groupIdJsonStr = NULL; + int num = GetIdenticalAccountGroupInfo(&groupVecStr); + if (groupVecStr == NULL || num == 0) { + DMLOGE("get idential account group vec failed."); + return groupIdJsonStr; + } + cJSON *jsonObject = cJSON_Parse(groupVecStr); + g_deviceGroupManager->destroyInfo(&groupVecStr); + if (jsonObject == NULL) { + DMLOGE("returnGroups parse error."); + return groupIdJsonStr; + } + if (!cJSON_IsArray(jsonObject)) { + DMLOGE("json string is not array."); + cJSON_Delete(jsonObject); + return groupIdJsonStr; + } + int arraySize = cJSON_GetArraySize(jsonObject); + if (arraySize == 0) { + cJSON_Delete(jsonObject); + return groupIdJsonStr; + } + int index = 0; + cJSON *item = cJSON_GetArrayItem(jsonObject, index); + if (item == NULL) { + cJSON_Delete(jsonObject); + return groupIdJsonStr; + } + cJSON *groupIdObject = cJSON_GetObjectItem(item, FIELD_GROUP_ID); + if (groupIdObject == NULL || !cJSON_IsString(groupIdObject)) { + cJSON_Delete(jsonObject); + return groupIdJsonStr; + } + groupIdJsonStr = GetGroupIdReqJsonString(groupIdObject->valuestring); + cJSON_Delete(jsonObject); + if (groupIdJsonStr == NULL) { + DMLOGE("json string is not array."); + return groupIdJsonStr; + } + return groupIdJsonStr; } bool IsHichainCredentialExist(void) { - cJSON *root = NULL; - bool resultFlag = false; - char *reqJsonStr = "{\n}"; - char *returnJsonStr = NULL; + char *groupVecStr = NULL; + int num = GetIdenticalAccountGroupInfo(&groupVecStr); + g_deviceGroupManager->destroyInfo(&groupVecStr); + if (num > 0) { + return true; + } + return false; +} - if (g_deviceGroupManager == NULL) { - DMLOGE("g_deviceGroupManager is NULL."); - return resultFlag; +int GetIdenticalAccountGroupInfo(char **groupVec) +{ + int groupNum = 0; + char *queryJsonStr = GetIdeAccountQueryJson(); + if (queryJsonStr == NULL) { + DMLOGE("get idential account query json failed."); + return groupNum; + } + int32_t userId = 0; + int32_t ret = g_deviceGroupManager->getGroupInfo(userId, HICHAIN_PKG_NAME, queryJsonStr, groupVec, &groupNum); + cJSON_free(queryJsonStr); + if (ret != 0) { + DMLOGE("[HICHAIN]fail to get group info with ret:%d.", ret); + return groupNum; + } + if (*groupVec == NULL) { + DMLOGE("[HICHAIN]return groups info point is nullptr."); + return groupNum; + } + DMLOGI("MineHiChainConnector::GetGroupInfo groupNum(%u).", groupNum); + return groupNum; +} + +// returned string, which needs to be manually release by the invoker. +char* GetIdeAccountQueryJson(void) +{ + char *queryParams = NULL; + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + DMLOGE("failed to create cjson object."); + return queryParams; + } + if (cJSON_AddNumberToObject(root, FIELD_GROUP_TYPE, GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP) == NULL) { + DMLOGE("failed to add grouptype string to cjson object."); + cJSON_Delete(root); + return queryParams; + } + queryParams = cJSON_PrintUnformatted(root); + if (queryParams == NULL) { + DMLOGE("failed to print string from cjson object."); + cJSON_Delete(root); + return queryParams; } -#ifdef MINE_HARMONY - do { - int retValue = g_deviceGroupManager->processCredential(CREDENTIAL_QUERY, reqJsonStr, &returnJsonStr); - if (retValue != HC_SUCCESS || returnJsonStr == NULL) { - DMLOGE("failed to check device credential info with ret: %d.", retValue); - break; - } - root = cJSON_Parse(returnJsonStr); - if (root == NULL) { - DMLOGE("failed to create cjson object."); - break; - } - cJSON *object = cJSON_GetObjectItem(root, FIELD_CREDENTIAL_EXISTS); - if (object == NULL) { - DMLOGE("failed to get object from cjson object."); - break; - } - if (object->type != cJSON_False && object->type != cJSON_True) { - DMLOGE("object type is error with type: %d.", object->type); - break; - } - if (object->type == cJSON_False) { - DMLOGI("credential information does not exist on the current device."); - break; - } - DMLOGI("credential information exist on the current device."); - resultFlag = true; - } while (false); + cJSON_Delete(root); + return queryParams; +} +// returned string, which needs to be manually release by the invoker. +char *GetGroupIdReqJsonString(const char *groupId) +{ + char *strParams = NULL; + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + DMLOGE("failed to create cjson object."); + return strParams; + } + if (cJSON_AddStringToObject(root, FIELD_GROUP_ID, groupId) == NULL) { + DMLOGE("failed to add groupid string to cjson object."); + cJSON_Delete(root); + return strParams; + } + strParams = cJSON_PrintUnformatted(root); + if (strParams == NULL) { + DMLOGE("failed to print string from cjson object."); + cJSON_Delete(root); + return strParams; + } cJSON_Delete(root); - g_deviceGroupManager->destroyInfo(&returnJsonStr); -#endif - return resultFlag; + return strParams; } diff --git a/interfaces/inner_kits/native_cpp/src/mini/softbus_adapter.c b/interfaces/inner_kits/native_cpp/src/mini/softbus_adapter.c index 9ee2b801501babd0acd39c5e4db5f68f914ba11c..82b470b71732a23a6ee1e84e87a2eae970296a7a 100644 --- a/interfaces/inner_kits/native_cpp/src/mini/softbus_adapter.c +++ b/interfaces/inner_kits/native_cpp/src/mini/softbus_adapter.c @@ -241,6 +241,7 @@ static struct { bool valid; } g_devIdMap[DM_MAX_DEVICE_SIZE]; +#ifdef MINE_HARMONY_SOFTBUS IPublishCb g_publishLNNCallback = { .OnPublishResult = OnPublishLNNResult }; @@ -267,6 +268,7 @@ ISessionListener g_sessionListener = { OnJoinLNNResult g_joinLNNResult = OnJoinLNNCallback; OnLeaveLNNResult g_leaveLNNResult = OnLeaveLNNCallback; +#endif int InitSoftbusModle(void) { @@ -281,6 +283,7 @@ int InitSoftbusModle(void) } do { +#ifdef MINE_HARMONY_SOFTBUS InitSoftBusServer(); DMLOGI("Softbus adapter create session session server start."); int32_t ret = CreateSessionServer(DM_PKG_NAME, DM_SESSION_NAME, &g_sessionListener); @@ -295,6 +298,7 @@ int InitSoftbusModle(void) retValue = ERR_DM_SOFTBUS_REG_STATE_CALLBACK; break; } +#endif if (CreateSoftbusSemaphoreAndMutex() != DM_OK) { DMLOGE("failed to create mutex and semaphore."); retValue = ERR_DM_LITEOS_CREATE_MUTEX_OR_SEM; @@ -314,11 +318,13 @@ int InitSoftbusModle(void) int UnInitSoftbusModle(void) { int returnResult = DM_OK; +#ifdef MINE_HARMONY_SOFTBUS int retValue = UnregNodeDeviceStateCb(&g_softbusStatusChangeCb); if (retValue != SOFTBUS_OK) { DMLOGE("failed to unregister device state callback with ret: %d.", retValue); returnResult = ERR_DM_SOFTBUS_UNREG_STATE_CALLBACK; } +#endif if (DeleteSoftbusSemaphoreAndMutex() != DM_OK) { DMLOGE("failed to delete semaphore."); returnResult = ERR_DM_LITEOS_DELETE_MUTEX_OR_SEM; @@ -406,6 +412,7 @@ int GetSoftbusTrustedDeviceList(const char *pkgName, DmDeviceBasicInfo *deviceLi DMLOGE("input point is NULL."); return ERR_DM_INPUT_INVALID_VALUE; } +#ifdef MINE_HARMONY_SOFTBUS NodeBasicInfo *nodeInfo = NULL; int retValue = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, trustListLen); if (retValue != SOFTBUS_OK || *trustListLen < 0) { @@ -418,6 +425,7 @@ int GetSoftbusTrustedDeviceList(const char *pkgName, DmDeviceBasicInfo *deviceLi NodeBasicInfoCopyToDmDevice(&deviceList[i], &nodeInfo[i]); } FreeNodeInfo(nodeInfo); +#endif DMLOGI("get trusted device with trustDeviceCount: %d.", *trustListLen); return DM_OK; } @@ -610,6 +618,7 @@ static int ParseDiscoverFilterOption(const char *filterOption) static int SoftbusRefreshLNN(const char *pkgName, const int subscribeId) { +#ifdef MINE_HARMONY_SOFTBUS SubscribeInfo subscribeInfo; subscribeInfo.mode = DISCOVER_MODE_ACTIVE; subscribeInfo.medium = AUTO; @@ -625,6 +634,7 @@ static int SoftbusRefreshLNN(const char *pkgName, const int subscribeId) DMLOGE("failed to start to refresh discovery with ret: %d.", retValue); return ERR_DM_FAILED; } +#endif DMLOGI("softbus RefreshLNN successfully."); return DM_OK; } @@ -785,7 +795,9 @@ static bool ImportDeviceToAddrMap(const DmDeviceInfo *deviceInfo) return false; } g_deviceIdAddrMap[i].valid = true; +#ifdef MINE_HARMONY_SOFTBUS g_deviceIdAddrMap[i].connectAddr = deviceInfo->connectAddr; +#endif return true; } DMLOGE("addr map not memory for a new deviceid."); @@ -898,6 +910,7 @@ int StartSoftbusPublish(const char *pkgName, OnAdvertisingResult cb) DMLOGE("pkgName is invalid."); return ERR_DM_INPUT_INVALID_VALUE; } +#ifdef MINE_HARMONY_SOFTBUS PublishInfo publishInfo; publishInfo.publishId = DEVICEMANAGER_SA_ID; publishInfo.mode = DISCOVER_MODE_ACTIVE; @@ -911,6 +924,7 @@ int StartSoftbusPublish(const char *pkgName, OnAdvertisingResult cb) DMLOGE("failed to call softbus publishLNN function with ret: %d.", retValue); return ERR_DM_SOFTBUS_PUBLISH_LNN; } +#endif g_publishLNNFlag = true; if (!ImportPkgNameToAdvertisingMap(pkgName, cb)) { DMLOGE("failed to import pkgName to advertising map."); @@ -931,11 +945,13 @@ int StopSoftbusPublish(const char *pkgName) DMLOGE("pkgName is invalid."); return ERR_DM_INPUT_INVALID_VALUE; } +#ifdef MINE_HARMONY_SOFTBUS int retValue = StopPublishLNN(pkgName, DEVICEMANAGER_SA_ID); if (retValue != SOFTBUS_OK) { DMLOGE("failed to call stop softbus publishLNN function with ret: %d.", retValue); return ERR_DM_SOFTBUS_STOP_PUBLISH_LNN; } +#endif g_publishLNNFlag = false; g_advertisingCallbackMap.valid = false; DMLOGI("StopSoftbusPublish end."); @@ -1024,7 +1040,9 @@ static void DeviceInfoCopyToDmDevice(DmDeviceInfo *dmDeviceInfo, const DeviceInf dmDeviceInfo->credible = deviceInfo->isOnline; dmDeviceInfo->deviceTypeId = deviceInfo->devType; dmDeviceInfo->range = deviceInfo->range; +#ifdef MINE_HARMONY_SOFTBUS dmDeviceInfo->connectAddr = deviceInfo->addr[arrayStartPosition]; +#endif int ret = GetAuthFormByDeviceId(dmDeviceInfo->deviceId, dmDeviceInfo->authForm); if (ret != DM_OK) { DMLOGE("failed to get authForm from hichain, ret: %d.", ret); @@ -1208,6 +1226,7 @@ static char* CreateRespNegotiateMsg(const int bindType) static int OnSessionOpened(int sessionId, int result) { +#ifdef MINE_HARMONY_SOFTBUS if (result != SOFTBUS_OK) { DMLOGE("open auth session failed, ret: %d.", result); CloseSession(sessionId); @@ -1227,6 +1246,7 @@ static int OnSessionOpened(int sessionId, int result) return ERR_DM_FAILED; } cJSON_free(msg); +#endif return DM_OK; } @@ -1288,12 +1308,13 @@ static void ProcessSinkMsg(const char *data, unsigned int dataLen) DMLOGE("failed to create response negotiate message."); return; } - +#ifdef MINE_HARMONY_SOFTBUS if (SendBytes(g_sessionId, retStr, strlen(retStr)) != SOFTBUS_OK) { DMLOGE("send bytes failed, cmd: %d.", MSG_NEGOTIATE_RESP); cJSON_free(retStr); return; } +#endif cJSON_free(retStr); } @@ -1342,8 +1363,10 @@ static void ProcessSourceMsg(const char *data, unsigned int dataLen) DMLOGE("remote client no credential or not support bind type."); return; } +#ifdef MINE_HARMONY_SOFTBUS JoinLNN(DM_PKG_NAME, &g_bindAddr, g_joinLNNResult); CloseSession(g_sessionId); +#endif UINT32 osRet = LOS_SemPost(g_bindSem); if (osRet != LOS_OK) { DMLOGE("failed to post bind sem with ret: %u.", osRet); @@ -1372,7 +1395,9 @@ int SoftbusUnBindTarget(const char *pkgName, const char *networkId) DMLOGE("not meet the condition with network id len: %u.", len); return ERR_DM_INPUT_INVALID_VALUE; } +#ifdef MINE_HARMONY_SOFTBUS LeaveLNN(DM_PKG_NAME, networkId, g_leaveLNNResult); +#endif DMLOGI("SoftbusUnBindTarget stop."); return DM_OK; } \ No newline at end of file