diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 12ad2f7fd1c914bfadfc2d17b64aa1b3aacac9c7..a571e7ac8000ae8f4278a3d6e96b0e759389a88e 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -806,6 +806,7 @@ void DeviceManagerServiceImpl::HandleAccountLogoutEvent(int32_t remoteUserId, co SoftbusCache::GetInstance().GetUuidByUdid(remoteUdid, uuid); listener_->OnDeviceTrustChange(remoteUdid, uuid, DmAuthForm::IDENTICAL_ACCOUNT); for (const auto &item : devIdAndUserMap) { + LOGI("remoteUdid %{public}s.", GetAnonyString(remoteUdid).c_str()); bool notifyOffline = DeviceProfileConnector::GetInstance().DeleteAclForAccountLogOut(item.first, item.second, remoteUdid, remoteUserId); if (notifyOffline) { diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 8b40da361de4cc8b9c948e410a4e99f8a483bb4d..98477167c37c337f84ebe940ccb1498b608eb978 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -236,6 +236,7 @@ public: bool IsPC(); int32_t GetDeviceNetworkIdList(const std::string &pkgName, const NetworkIdQueryFilter &queryFilter, std::vector &networkIds); + void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); private: bool IsDMServiceImplReady(); @@ -344,6 +345,8 @@ private: EXPORT void SubscribeDataShareCommonEvent(); #endif void CheckRegisterInfoWithWise(int32_t curUserId); + void NotifyRemoteLocalLogout(const std::vector &peerUdids, + const std::string &accountIdHash, const std::string &accountName, int32_t userId); private: bool isImplsoLoaded_ = false; diff --git a/services/service/include/relationshipsyncmgr/dm_comm_tool.h b/services/service/include/relationshipsyncmgr/dm_comm_tool.h index a4cd2befb5933de1b4db8080a18f5b70e4a23390..d60b00071748ae65af440cd7d58d020a75f21c82 100644 --- a/services/service/include/relationshipsyncmgr/dm_comm_tool.h +++ b/services/service/include/relationshipsyncmgr/dm_comm_tool.h @@ -56,6 +56,8 @@ public: void ProcessReceiveUserIdsEvent(const std::shared_ptr commMsg); void ProcessResponseUserIdsEvent(const std::shared_ptr commMsg); + int32_t SendLogoutAccountInfo(const std::string &rmtNetworkId, const std::string &accountId, int32_t userId); + void ProcessReceiveLogoutEvent(const std::shared_ptr commMsg); private: std::shared_ptr dmTransportPtr_; std::shared_ptr eventHandler_; diff --git a/services/service/include/relationshipsyncmgr/dm_transport_msg.h b/services/service/include/relationshipsyncmgr/dm_transport_msg.h index a4fb17712aa2588c93032584145189ab3bafd231..5c3ac7ace557ef242cc858d79f4b3fee49d36e79 100644 --- a/services/service/include/relationshipsyncmgr/dm_transport_msg.h +++ b/services/service/include/relationshipsyncmgr/dm_transport_msg.h @@ -64,6 +64,17 @@ struct NotifyUserIds { void ToJson(cJSON *jsonObject, const NotifyUserIds &userIds); void FromJson(const cJSON *jsonObject, NotifyUserIds &userIds); + +struct LogoutAccountMsg { + std::string accountId; + int32_t userId; + LogoutAccountMsg() : userId(0) {} + LogoutAccountMsg(const std::string &accountId, int32_t userId) + : accountId(accountId), userId(userId) {} +}; + +void ToJson(cJSON *jsonObject, const LogoutAccountMsg &accountInfo); +void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo); } // DistributedHardware } // OHOS #endif \ No newline at end of file diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index aa87475de926b8826e682a596f4ba2100eebe5aa..750dece0b5c5997b62d746a6aeb4d28c8731b2d8 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -2095,7 +2095,7 @@ void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string LOGE("GetAccountHash failed."); return; } - SendAccountLogoutBroadCast(peerUdids, std::string(accountIdHash), accountName, userId); + NotifyRemoteLocalLogout(peerUdids, std::string(accountIdHash), accountName, userId); } for (const auto &item : deviceMap) { dmServiceImpl_->HandleIdentAccountLogout(localUdid, userId, item.first, item.second); @@ -3361,5 +3361,56 @@ int32_t DeviceManagerService::GetDeviceNetworkIdList(const std::string &pkgName, } return DM_OK; } + +void DeviceManagerService::NotifyRemoteLocalLogout(const std::vector &peerUdids, + const std::string &accountIdHash, const std::string &accountName, int32_t userId) +{ + LOGI("Start."); + std::vector bleUdids; + std::vector 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.push_back(netWorkId); + } + } + if (!bleUdids.empty()) { + SendAccountLogoutBroadCast(bleUdids, accountIdHash, accountName, userId); + } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + for (const auto &it : wifiDevices) { + int32_t ret = DMCommTool::GetInstance()->SendLogoutAccountInfo(it, accountIdHash, userId); + if (ret != DM_OK) { + LOGE("Send LogoutAccount Info error, ret = %{public}d", ret); + } + } +#endif +} + +void DeviceManagerService::ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, + int32_t userId) +{ + LOGI("Start. process udid: %{public}s", GetAnonyString(peerUdid).c_str()); + if (!IsDMServiceImplReady()) { + LOGE("Imp instance not init or init failed."); + return; + } + dmServiceImpl_->HandleAccountLogoutEvent(userId, accountId, peerUdid); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 7047624ffed3b3b56a9dd3fec040fa6ff77b20fe..36c69cc4d71e189a5fd557b507d16f5a0625c2f1 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -33,6 +33,7 @@ constexpr int32_t DM_COMM_SEND_LOCAL_USERIDS = 1; constexpr int32_t DM_COMM_RSP_LOCAL_USERIDS = 2; constexpr int32_t DM_COMM_SEND_USER_STOP = 3; constexpr int32_t DM_COMM_RSP_USER_STOP = 4; +constexpr int32_t DM_COMM_ACCOUNT_LOGOUT = 5; const char* const USER_STOP_MSG_KEY = "stopUserId"; @@ -179,6 +180,10 @@ void DMCommTool::DMCommToolEventHandler::ProcessEvent( dmCommToolPtr->ProcessResponseUserStopEvent(commMsg); break; } + case DM_COMM_ACCOUNT_LOGOUT: { + dmCommToolPtr->ProcessReceiveLogoutEvent(commMsg); + break; + } default: LOGE("event is undefined, id is %{public}d", eventId); break; @@ -418,5 +423,79 @@ void DMCommTool::ProcessResponseUserStopEvent(const std::shared_ptr(localDeviceId); DeviceManagerService::GetInstance().HandleUserStop(stopUserId, localUdid, acceptEventUdids); } + +int32_t DMCommTool::SendLogoutAccountInfo(const std::string &rmtNetworkId, + const std::string &accountId, int32_t userId) +{ + if (!IsIdLengthValid(rmtNetworkId) || accountId.empty() || dmTransportPtr_ == nullptr) { + LOGE("param invalid, networkId: %{public}s, userId: %{public}d", + GetAnonyString(rmtNetworkId).c_str(), userId); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("Start, send networkId: %{public}s", GetAnonyString(rmtNetworkId).c_str()); + int32_t socketId = 0; + if (dmTransportPtr_->StartSocket(rmtNetworkId, socketId) != DM_OK || socketId <= 0) { + LOGE("Start socket error"); + return ERR_DM_FAILED; + } + + cJSON *root = cJSON_CreateObject(); + if (root == nullptr) { + LOGE("Create cJSON object failed."); + return ERR_DM_FAILED; + } + LogoutAccountMsg LogoutAccountMsg(accountId, userId); + ToJson(root, LogoutAccountMsg); + char *msg = cJSON_PrintUnformatted(root); + if (msg == nullptr) { + cJSON_Delete(root); + return ERR_DM_FAILED; + } + std::string msgStr(msg); + cJSON_Delete(root); + cJSON_free(msg); + CommMsg commMsg(DM_COMM_ACCOUNT_LOGOUT, msgStr); + std::string payload = GetCommMsgString(commMsg); + + int32_t ret = dmTransportPtr_->Send(rmtNetworkId, payload, socketId); + if (ret != DM_OK) { + LOGE("Send account logout failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + LOGI("Send account logout success"); + return DM_OK; +} + +void DMCommTool::ProcessReceiveLogoutEvent(const std::shared_ptr commMsg) +{ + CHECK_NULL_VOID(commMsg); + LOGI("Receive remote logout, networkId: %{public}s", GetAnonyString(commMsg->remoteNetworkId).c_str()); + std::string rmtUdid = ""; + SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); + if (rmtUdid.empty()) { + LOGE("Can not find remote udid by networkid: %{public}s", GetAnonyString(commMsg->remoteNetworkId).c_str()); + return; + } + + CHECK_NULL_VOID(commMsg->commMsg); + std::string payload = commMsg->commMsg->msg; + cJSON *root = cJSON_Parse(payload.c_str()); + if (root == NULL) { + LOGE("the msg is not json format"); + return; + } + LogoutAccountMsg logoutAccountMsg; + FromJson(root, logoutAccountMsg); + cJSON_Delete(root); + + if (logoutAccountMsg.accountId.empty() || logoutAccountMsg.userId == -1) { + LOGE("param invalid, accountId: %{public}s, userId: %{public}d", + GetAnonyString(logoutAccountMsg.accountId).c_str(), logoutAccountMsg.userId); + return; + } + DeviceManagerService::GetInstance().ProcessSyncAccountLogout(logoutAccountMsg.accountId, + rmtUdid, logoutAccountMsg.userId); + LOGI("process remote logout success."); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/services/service/src/relationshipsyncmgr/dm_transport.cpp b/services/service/src/relationshipsyncmgr/dm_transport.cpp index 3d102b082ed8412a26c2dbc45fc890adc8adfd9e..ba18320284caf8f268d2ffe4b0908248571f7fe9 100644 --- a/services/service/src/relationshipsyncmgr/dm_transport.cpp +++ b/services/service/src/relationshipsyncmgr/dm_transport.cpp @@ -117,7 +117,7 @@ void DMTransport::HandleReceiveMessage(const int32_t socketId, const std::string LOGE("payload invalid"); return; } - LOGI("Receive msg: %{public}s", payload.c_str()); + LOGI("Receive msg: %{public}s", GetAnonyString(payload).c_str()); cJSON *root = cJSON_Parse(payload.c_str()); if (root == NULL) { LOGE("the msg is not json format"); diff --git a/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp b/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp index 1cbd08a5247ca40a30303963fd019874c9a632c2..010236db3f3cdf2410d3038ae48cdfb412683e9e 100644 --- a/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp +++ b/services/service/src/relationshipsyncmgr/dm_transport_msg.cpp @@ -27,6 +27,7 @@ const char* const COMM_MSG_CODE_KEY = "code"; const char* const COMM_MSG_MSG_KEY = "msg"; const char* const DSOFTBUS_NOTIFY_USERIDS_UDIDKEY = "remoteUdid"; const char* const DSOFTBUS_NOTIFY_USERIDS_USERIDKEY = "foregroundUserIds"; +const char* const DSOFTBUS_NOTIFY_ACCOUNTID_KEY = "accountId"; } void ToJson(cJSON *jsonObject, const UserIdsMsg &userIdsMsg) { @@ -226,5 +227,33 @@ std::string NotifyUserIds::ToString() cJSON_free(retStr); return ret; } + +void ToJson(cJSON *jsonObject, const LogoutAccountMsg &accountInfo) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + + cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY, accountInfo.accountId.c_str()); + cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY, accountInfo.userId); +} + +void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo) +{ + if (jsonObject == nullptr) { + LOGE("Json pointer is nullptr!"); + return; + } + cJSON *accountIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY); + if (cJSON_IsString(accountIdObj)) { + accountInfo.accountId = accountIdObj->valuestring; + } + + cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY); + if (cJSON_IsNumber(userIdObj)) { + accountInfo.userId = userIdObj->valueint; + } +} } // DistributedHardware } // OHOS \ No newline at end of file