From 4d309f3829fc95d82adf0c4457d2f6f8a47f6f70 Mon Sep 17 00:00:00 2001 From: q30043944 Date: Sat, 22 Feb 2025 14:56:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=9C=BA=E5=A4=84=E7=90=86=E5=90=8E?= =?UTF-8?q?=E5=8F=B0status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: q30043944 --- .../include/deviceprofile_connector.h | 11 +- .../src/deviceprofile_connector.cpp | 75 +++++++++ .../service/include/device_manager_service.h | 11 ++ .../include/hichain/hichain_listener.h | 25 ++- .../service/src/device_manager_service.cpp | 147 +++++++++++++++++ .../service/src/hichain/hichain_listener.cpp | 152 ++++++++++++++++++ 6 files changed, 418 insertions(+), 3 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index f37fc6b9b..12f52dc35 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -91,6 +91,12 @@ class IDeviceProfileConnector { public: virtual ~IDeviceProfileConnector() {} virtual int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) = 0; + virtual std::map GetDeviceIdAndBindLevel(std::vector userIds, + const std::string &localUdid) = 0; + virtual int32_t HandleUserSwitched(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) = 0; + virtual bool CheckAclByUserId(const std::string &localUdid, std::vector foregroundUserIds, + std::vector backgroundUserIds) = 0; }; class DeviceProfileConnector : public IDeviceProfileConnector { @@ -149,6 +155,8 @@ public: void DeleteAccessControlById(int64_t accessControlId); int32_t HandleUserSwitched(const std::string &localUdid, const std::vector &deviceVec, int32_t currentUserId, int32_t beforeUserId); + int32_t HandleUserSwitched(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void HandleSyncForegroundUserIdEvent(const std::vector &remoteUserIds, const std::string &remoteUdid, const std::vector &localUserIds, std::string &localUdid); std::vector GetOfflineProcessInfo(std::string &localUdid, const std::vector &localUserIds, @@ -168,7 +176,8 @@ public: int32_t UnSubscribeDeviceProfileInited(); int32_t PutAllTrustedDevices(const std::vector &deviceInfos); int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); - + bool CheckAclByUserId(const std::string &localUdid, std::vector foregroundUserIds, + std::vector backgroundUserIds); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 5d67e5518..5357d0227 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1257,6 +1257,32 @@ OHOS::DistributedHardware::ProcessInfo DeviceProfileConnector::HandleAppUnBindEv return processInfo; } +bool DeviceProfileConnector::CheckAclByUserId(const std::string &localUdid, std::vector foregroundUserIds, + std::vector backgroundUserIds) +{ + std::vector profiles = GetAllAccessControlProfile(); + LOGI("CheckAclByUserId profiles size is %{public}zu", profiles.size()); + for (auto &item : profiles) { + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE) || + (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE)) { + return true; + } + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE) || ( + item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE)) { + return true; + } + } + return false; +} + std::vector DeviceProfileConnector::GetAllAccessControlProfile() { std::vector profiles; @@ -1315,6 +1341,55 @@ int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid, return DM_OK; } +int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid, + const std::vector &deviceVec, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + LOGI("OnStart HandleUserSwitched"); + if (deviceVec.empty()) { + LOGI("no remote device."); + return DM_OK; + } + std::vector profiles = GetAllAccessControlProfile(); + std::vector activeProfiles; + std::vector inActiveProfiles; + for (auto &item : profiles) { + if (std::find(deviceVec.begin(), deviceVec.end(), item.GetTrustDeviceId()) == deviceVec.end()) { + continue; + } + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE) || + (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(backgroundUserIds.begin(), backgroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE)) { + item.SetStatus(INACTIVE); + inActiveProfiles.push_back(item); + continue; + } + if ((item.GetAccesser().GetAccesserDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccesser().GetAccesserUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE) || ( + item.GetAccessee().GetAccesseeDeviceId() == localUdid && + (find(foregroundUserIds.begin(), foregroundUserIds.end(), + item.GetAccessee().GetAccesseeUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE)) { + item.SetStatus(ACTIVE); + activeProfiles.push_back(item); + continue; + } + } + for (auto &item : inActiveProfiles) { + DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); + if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) { + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId()); + } + } + for (auto &item : activeProfiles) { + DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); + } + return DM_OK; +} + std::vector DeviceProfileConnector::GetAclProfileByUserId(const std::string &localUdid, int32_t userId, const std::string &remoteUdid) { diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index f57a29acb..61739979a 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -294,6 +294,17 @@ private: int32_t SendUserIdsByWifi(const std::string &networkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void HandleUserSwitchTimeout(int32_t curUserId, int32_t preUserId, const std::string &udid); + + void HandleUserSwitched(); + void NotifyRemoteLocalUserSwitch(const std::string &localUdid, const std::vector &peerUdids, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); + void NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, + const std::map &wifiDevices, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds); + void HandleUserSwitchTimeout(const std::string &localUdid, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds, const std::string &udid); + void UpdateAclAndDeleteGroup(const std::string &localUdid, const std::vector &deviceVec, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); void QueryDependsSwitchState(); diff --git a/services/service/include/hichain/hichain_listener.h b/services/service/include/hichain/hichain_listener.h index 0adb7b0c6..7eab49cd0 100644 --- a/services/service/include/hichain/hichain_listener.h +++ b/services/service/include/hichain/hichain_listener.h @@ -38,15 +38,36 @@ struct GroupInformation { } }; -void from_json(const nlohmann::json &jsonObject, GroupInformation &groupInfo); +struct GroupsInfo { + std::string groupName; + std::string groupId; + std::string groupOwner; + int32_t groupType; + int32_t groupVisibility; + std::string userId; + GroupsInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId("") + { + } +}; + +void from_json(const nlohmann::json &jsonObject, GroupInformation &groupInfo); +void from_json(const nlohmann::json &jsonObject, GroupsInfo &groupInfo); class HichainListener { public: HichainListener(); ~HichainListener(); void RegisterDataChangeCb(); static void OnHichainDeviceUnBound(const char *peerUdid, const char *groupInfo); - + void DeleteAllGroup(const std::string &localUdid, const std::vector &backgroundUserIds); + int32_t GetRelatedGroups(int32_t userId, const std::string &deviceId, + std::vector &groupList); + int32_t GetRelatedGroupsCommon(int32_t userId, const std::string &deviceId, const char* pkgName, + std::vector &groupList); + int32_t DeleteGroup(const int32_t userId, std::string &groupId); + int32_t GetRelatedGroupsExt(int32_t userId, const std::string &deviceId, std::vector &groupList); + int32_t DeleteGroupExt(int32_t userId, std::string &groupId); + int64_t GenRequestId(); private: const DeviceGroupManager *deviceGroupManager_ = nullptr; }; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 7c0745e07..14af23e08 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1701,6 +1701,7 @@ void DeviceManagerService::AccountCommonEventCallback(const std::string commonEv dmAccountInfo.accountName = MultipleUserConnector::GetOhosAccountName(); MultipleUserConnector::SetAccountInfo(currentUserId, dmAccountInfo); if (beforeUserId == -1 || currentUserId == -1) { + HandleUserSwitched(); return; } else if (beforeUserId != -1 && currentUserId != -1) { HandleUserSwitched(currentUserId, beforeUserId); @@ -1810,6 +1811,152 @@ void DeviceManagerService::HandleUserSwitched(int32_t curUserId, int32_t preUser NotifyRemoteLocalUserSwitch(curUserId, preUserId, peerUdids, foregroundUserVec, backgroundUserVec); } +void DeviceManagerService::HandleUserSwitched() +{ + LOGI("onStart, HandleUserSwitched."); + std::vector foregroundUserVec; + foregroundUserVec.clear(); + int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec); + std::vector backgroundUserVec; + backgroundUserVec.clear(); + int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec); + if (retFront != DM_OK || retBack != DM_OK || foregroundUserVec.empty()) { + LOGE("Get userids failed, retFront: %{public}d, retBack: %{public}d, foreground user num: %{public}d", + retFront, retBack, static_cast(foregroundUserVec.size())); + return; + } + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + std::map curUserDeviceMap; + std::map preUserDeviceMap; + std::vector peerUdids; + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + + if (!discoveryMgr_->GetCommonDependencyObj()->CheckAclByUserId(localUdid, foregroundUserVec, backgroundUserVec)) { + LOGI("no unreasonable data."); + return; + } + + curUserDeviceMap = discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(foregroundUserVec, localUdid); + preUserDeviceMap = discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(backgroundUserVec, localUdid); + for (const auto &item : curUserDeviceMap) { + peerUdids.push_back(item.first); + } + for (const auto &item : preUserDeviceMap) { + if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) { + peerUdids.push_back(item.first); + } + } + if (peerUdids.empty()) { + return; + } + NotifyRemoteLocalUserSwitch(localUdid, peerUdids, foregroundUserVec, backgroundUserVec); +} + +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +void DeviceManagerService::NotifyRemoteLocalUserSwitch(const std::string &localUdid, + const std::vector &peerUdids, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + LOGI("onstart UserSwitch, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s", + GetIntegerList(foregroundUserIds).c_str(), GetIntegerList(backgroundUserIds).c_str()); + if (peerUdids.empty()) { + return; + } + if (softbusListener_ == nullptr) { + UpdateAclAndDeleteGroup(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); + LOGE("softbusListener_ is null"); + return; + } + std::vector bleUdids; + std::map wifiDevices; + for (const auto &udid : peerUdids) { + std::string netWorkId = ""; + SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId); + if (netWorkId.empty()) { + LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str()); + bleUdids.push_back(udid); + continue; + } + int32_t networkType = 0; + int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType); + if (ret != DM_OK || networkType <= 0) { + LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str()); + bleUdids.push_back(udid); + continue; + } + if ((static_cast(networkType) & USERID_SYNC_DISCOVERY_TYPE_BLE_MASK) != 0x0) { + bleUdids.push_back(udid); + } else { + wifiDevices.insert(std::pair(udid, netWorkId)); + } + } + if (!bleUdids.empty()) { + UpdateAclAndDeleteGroup(localUdid, peerUdids, foregroundUserIds, backgroundUserIds); + SendUserIdsBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, true); + } + if (!wifiDevices.empty()) { + NotifyRemoteLocalUserSwitchByWifi(localUdid, wifiDevices, foregroundUserIds, backgroundUserIds); + } +} + +void DeviceManagerService::NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, + const std::map &wifiDevices, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + for (const auto &it : wifiDevices) { + int32_t result = SendUserIdsByWifi(it.second, foregroundUserIds, backgroundUserIds); + if (result != DM_OK) { + LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str()); + std::vector updateUdids; + updateUdids.push_back(it.first); + UpdateAclAndDeleteGroup(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); + continue; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + std::string udid = it.first; + timer_->StartTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid), + USER_SWITCH_BY_WIFI_TIMEOUT_S, + [this, localUdid, foregroundUserIds, backgroundUserIds, udid] (std::string name) { + DeviceManagerService::HandleUserSwitchTimeout(localUdid, foregroundUserIds, backgroundUserIds, udid); + }); + } +} + +void DeviceManagerService::HandleUserSwitchTimeout(const std::string &localUdid, + const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, + const std::string &udid) +{ + LOGI("start udid: %{public}s", GetAnonyString(udid).c_str()); + std::vector updateUdids; + updateUdids.push_back(udid); + UpdateAclAndDeleteGroup(localUdid, updateUdids, foregroundUserIds, backgroundUserIds); +} + +void DeviceManagerService::UpdateAclAndDeleteGroup(const std::string &localUdid, + const std::vector &deviceVec, const std::vector &foregroundUserIds, + const std::vector &backgroundUserIds) +{ + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + discoveryMgr_->GetCommonDependencyObj()->HandleUserSwitched(localUdid, deviceVec, + foregroundUserIds, backgroundUserIds); + //delete group + CHECK_NULL_VOID(hichainListener_); + hichainListener_->DeleteAllGroup(localUdid, backgroundUserIds); +} +#endif + void DeviceManagerService::HandleUserRemoved(int32_t removedUserId) { LOGI("PreUserId %{public}d.", removedUserId); diff --git a/services/service/src/hichain/hichain_listener.cpp b/services/service/src/hichain/hichain_listener.cpp index a4422fa74..39c8c99d6 100644 --- a/services/service/src/hichain/hichain_listener.cpp +++ b/services/service/src/hichain/hichain_listener.cpp @@ -19,6 +19,7 @@ #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_log.h" +#include "dm_random.h" #include "multiple_user_connector.h" namespace OHOS { @@ -27,6 +28,7 @@ namespace DistributedHardware { namespace { constexpr uint32_t MAX_DATA_LEN = 65536; constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1; + constexpr const char* DM_PKG_NAME_EXT = "com.huawei.devicemanager"; } static DataChangeListener dataChangeListener_ = { @@ -49,6 +51,34 @@ void from_json(const nlohmann::json &jsonObject, GroupInformation &groupInfo) } } +void from_json(const nlohmann::json &jsonObject, GroupsInfo &groupInfo) +{ + if (jsonObject.find(FIELD_GROUP_NAME) != jsonObject.end() && jsonObject.at(FIELD_GROUP_NAME).is_string()) { + groupInfo.groupName = jsonObject.at(FIELD_GROUP_NAME).get(); + } + + if (jsonObject.find(FIELD_GROUP_ID) != jsonObject.end() && jsonObject.at(FIELD_GROUP_ID).is_string()) { + groupInfo.groupId = jsonObject.at(FIELD_GROUP_ID).get(); + } + + if (jsonObject.find(FIELD_GROUP_OWNER) != jsonObject.end() && jsonObject.at(FIELD_GROUP_OWNER).is_string()) { + groupInfo.groupOwner = jsonObject.at(FIELD_GROUP_OWNER).get(); + } + + if (jsonObject.find(FIELD_GROUP_TYPE) != jsonObject.end() && jsonObject.at(FIELD_GROUP_TYPE).is_number_integer()) { + groupInfo.groupType = jsonObject.at(FIELD_GROUP_TYPE).get(); + } + + if (jsonObject.find(FIELD_GROUP_VISIBILITY) != jsonObject.end() && + jsonObject.at(FIELD_GROUP_VISIBILITY).is_number_integer()) { + groupInfo.groupVisibility = jsonObject.at(FIELD_GROUP_VISIBILITY).get(); + } + + if (jsonObject.find(FIELD_USER_ID) != jsonObject.end() && jsonObject.at(FIELD_USER_ID).is_string()) { + groupInfo.userId = jsonObject.at(FIELD_USER_ID).get(); + } +} + HichainListener::HichainListener() { LOGI("HichainListener constructor start."); @@ -111,5 +141,127 @@ void HichainListener::OnHichainDeviceUnBound(const char *peerUdid, const char *g return; } } + +void HichainListener::DeleteAllGroup(const std::string &localUdid, const std::vector &backgroundUserIds) +{ + LOGI("OnStart HichainListener::DeleteAllGroup"); + for (auto &userId : backgroundUserIds) { + std::vector groupList; + GetRelatedGroups(userId, localUdid, groupList); + for (auto &iter : groupList) { + if (iter.groupType == GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP) { + continue; + } + if (DeleteGroup(userId, iter.groupId) != DM_OK) { + LOGE("Delete groupId %{public}s failed.", GetAnonyString(iter.groupId).c_str()); + } + } + std::vector groupListExt; + GetRelatedGroupsExt(userId, localUdid, groupListExt); + for (auto &iter : groupListExt) { + if (iter.groupType == GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP) { + continue; + } + if (DeleteGroupExt(userId, iter.groupId) != DM_OK) { + LOGE("DeleteGroupExt groupId %{public}s failed.", GetAnonyString(iter.groupId).c_str()); + } + } + } +} + +int32_t HichainListener::GetRelatedGroups(int32_t userId, const std::string &deviceId, + std::vector &groupList) +{ + return GetRelatedGroupsCommon(userId, deviceId, DM_PKG_NAME, groupList); +} + +int32_t HichainListener::GetRelatedGroupsExt(int32_t userId, const std::string &deviceId, + std::vector &groupList) +{ + return GetRelatedGroupsCommon(userId, deviceId, DM_PKG_NAME_EXT, groupList); +} + +int32_t HichainListener::GetRelatedGroupsCommon(int32_t userId, const std::string &deviceId, const char* pkgName, + std::vector &groupList) +{ + LOGI("Start to get related groups."); + if (userId < 0) { + LOGE("user id failed"); + return ERR_DM_FAILED; + } + uint32_t groupNum = 0; + char *returnGroups = nullptr; + int32_t ret = + deviceGroupManager_->getRelatedGroups(userId, pkgName, deviceId.c_str(), &returnGroups, &groupNum); + if (ret != 0) { + LOGE("[HICHAIN] fail to get related groups with ret:%{public}d.", ret); + deviceGroupManager_->destroyInfo(&returnGroups); + return ERR_DM_FAILED; + } + if (returnGroups == nullptr) { + LOGE("[HICHAIN] return related goups point is nullptr"); + return ERR_DM_FAILED; + } + if (groupNum == 0) { + LOGE("[HICHAIN]return related goups number is zero."); + deviceGroupManager_->destroyInfo(&returnGroups); + return ERR_DM_FAILED; + } + std::string relatedGroups = std::string(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); + nlohmann::json jsonObject = nlohmann::json::parse(relatedGroups, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("returnGroups parse error"); + return ERR_DM_FAILED; + } + if (!jsonObject.is_array()) { + LOGE("jsonObject is not an array."); + return ERR_DM_FAILED; + } + std::vector groupInfos = jsonObject.get>(); + if (groupInfos.empty()) { + LOGE("HichainListener::GetRelatedGroups group failed, groupInfos is empty."); + return ERR_DM_FAILED; + } + groupList = groupInfos; + return DM_OK; +} + +int64_t HichainListener::GenRequestId() +{ + return GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID); +} + +int32_t HichainListener::DeleteGroup(const int32_t userId, std::string &groupId) +{ + if (userId < 0) { + LOGE("user id failed"); + return ERR_DM_FAILED; + } + int64_t requestId = GenRequestId(); + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_ID] = groupId; + std::string disbandParams = SafetyDump(jsonObj); + int32_t ret = deviceGroupManager_->deleteGroup(userId, requestId, DM_PKG_NAME, disbandParams.c_str()); + if (ret != 0) { + LOGE("[HICHAIN]fail to delete group with ret:%{public}d.", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t HichainListener::DeleteGroupExt(int32_t userId, std::string &groupId) +{ + int64_t requestId = GenRequestId(); + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_ID] = groupId; + std::string disbandParams = SafetyDump(jsonObj); + int32_t ret = deviceGroupManager_->deleteGroup(userId, requestId, DM_PKG_NAME_EXT, disbandParams.c_str()); + if (ret != 0) { + LOGE("[HICHAIN]fail to delete group with ret:%{public}d.", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee