From ec4124feb2a763713a89d24b0c981583ad30e7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Mon, 21 Jul 2025 11:34:26 +0800 Subject: [PATCH] service_code_2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../ipc/model/ipc_notify_device_state_req.h | 12 ++ .../include/deviceprofile_connector.h | 17 ++ .../src/deviceprofile_connector.cpp | 180 ++++++++++++++++++ .../native_cpp/include/device_manager.h | 6 + .../include/device_manager_callback.h | 11 ++ .../native_cpp/include/device_manager_impl.h | 6 + .../native_cpp/include/dm_device_info.h | 89 ++++++++- .../include/notify/device_manager_notify.h | 20 ++ .../native_cpp/src/device_manager_impl.cpp | 31 ++- .../src/ipc/standard/ipc_cmd_parser.cpp | 104 ++++++++++ .../src/notify/device_manager_notify.cpp | 168 ++++++++++++++++ .../devicestate/dm_device_state_manager.h | 2 +- .../devicestate/dm_device_state_manager.cpp | 25 ++- .../include/device_manager_service_listener.h | 14 ++ .../src/device_manager_service_listener.cpp | 140 ++++++++++++++ 15 files changed, 819 insertions(+), 6 deletions(-) diff --git a/common/include/ipc/model/ipc_notify_device_state_req.h b/common/include/ipc/model/ipc_notify_device_state_req.h index 26d2f59ea..f6e41e2ca 100644 --- a/common/include/ipc/model/ipc_notify_device_state_req.h +++ b/common/include/ipc/model/ipc_notify_device_state_req.h @@ -80,10 +80,22 @@ public: dmDeviceBasicInfo_ = dmDeviceBasicInfo; } + void SetServiceInfos(const std::map> &serviceInfos) + { + dmServiceInfos_ = serviceInfos; + } + + const std::map> &GetServiceInfos() const + { + return dmServiceInfos_; + } + + private: int32_t deviceState_ { 0 }; DmDeviceInfo dmDeviceInfo_; DmDeviceBasicInfo dmDeviceBasicInfo_; + std::map> dmServiceInfos_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index b6e6df3ec..ed1584e68 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -262,6 +262,18 @@ public: DM_EXPORT void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); + DM_EXPORT int32_t GetAllServiceInfoProfileList(std::vector &dmServiceInfoProfiles); + DM_EXPORT int32_t GetServiceInfoProfileListByTokenId(const DmServiceInfoUniqueKey &key, + std::vector &serviceInfos); + DM_EXPORT int32_t GetServiceInfoProfileListByDeviceId(const std::string &deviceId, + std::vector &serviceInfos); + DM_EXPORT int32_t DeleteServiceInfoProfile(const DmServiceInfoUniqueKey& key); + DM_EXPORT void GetLocalTokenIdByDeviceIdAndUserId(std::string localUdid, std::string udid, + std::map &tokenToKey); + DM_EXPORT int32_t GetServiceInfoByTokenId(DmServiceInfoUniqueKey key, + std::vector &serviceInfos); + + DM_EXPORT int32_t DeleteSessionKey(int32_t userId, int32_t sessionKeyId); DM_EXPORT int32_t GetSessionKey(int32_t userId, int32_t sessionKeyId, std::vector &sessionKeyArray); @@ -427,6 +439,11 @@ private: const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid); bool CheckExtWhiteList(const std::string &bundleName); + + // void TransferDpToDmServiceInfoProfile(const DmServiceInfoProfile &dmServiceInfoProfile, + // DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile); + // void TransferDpToDmServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile, + // DMServiceInfoProfile &dmServiceInfoProfile); }; extern "C" IDeviceProfileConnector *CreateDpConnectorInstance(); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 132a0a795..3fe0de948 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -592,6 +592,56 @@ void DeviceProfileConnector::FilterNeedDeleteACLInfos( } } +// void DeviceProfileConnector::TransferDpToDmServiceInfoProfile(const DmServiceInfoProfile &dmServiceInfoProfile, +// DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile) +// { +// dpServiceInfoProfile.SetDeviceId(dmServiceInfoProfile.deviceId); +// dpServiceInfoProfile.SetuserId(dmServiceInfoProfile.userId); +// dpServiceInfoProfile.SetNetworkId(dmServiceInfoProfile.networkId); +// dpServiceInfoProfile.SetTokenId(dmServiceInfoProfile.tokenId); +// dpServiceInfoProfile.SetServiceId(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId); +// dpServiceInfoProfile.SetServiceName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceName); +// dpServiceInfoProfile.SetServiceDisplayName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceDisplayName); +// dpServiceInfoProfile.SetCustomData(dmServiceInfoProfile.serviceRegInfo.serviceInfo.customData); +// dpServiceInfoProfile.SetCustomDataLen(dmServiceInfoProfile.serviceRegInfo.serviceInfo.dataLen); +// dpServiceInfoProfile.SetBundleName(dmServiceInfoProfile.serviceRegInfo.serviceInfo.bundleName); +// dpServiceInfoProfile.SetModuleName (dmServiceInfoProfile.serviceRegInfo.serviceInfo.moduleName); +// dpServiceInfoProfile.SetAbilityName(dmServiceInfoProfile.sserviceRegInfo.serviceInfo.abilityName); +// dpServiceInfoProfile.SetAuthBoxType(dmServiceInfoProfile.serviceRegInfo.authBoxType); +// dpServiceInfoProfile.SetAuthType(dmServiceInfoProfile.serviceRegInfo.authType); +// dpServiceInfoProfile.SetPinExchangeType (dmServiceInfoProfile.serviceRegInfo.pinExchangeType); +// dpServiceInfoProfile.SetPinCode(dmServiceInfoProfile.serviceRegInfo.pinCode); +// dpServiceInfoProfile.SetDescription(dmServiceInfoProfile.serviceRegInfo.description); +// dpServiceInfoProfile.SetServiceDicoveryScope(dmServiceInfoProfile.serviceRegInfo.serviceDiscoveryScope); +// dpServiceInfoProfile.SetExtraInfo(dmServiceInfoProfile.extraInfo); +// } + +// void DeviceProfileConnector::TransferDpToDmServiceInfoProfile( +// const DistributedDeviceProfile::ServiceInfoProfile &dpServiceInfoProfile, +// DMServiceInfoProfile &dmServiceInfoProfile) +// { +// dmServiceInfoProfile.deviceId = dpServiceInfoProfile.GetDeviceId(); +// dmServiceInfoProfile.userId = dpServiceInfoProfile.GetUserId(); +// dmServiceInfoProfile.networkId = dpServiceInfoProfile.GetNetworkId(); +// dmServiceInfoProfile.tokenId = dpServiceInfoProfile.GetTokenId(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId = dpServiceInfoProfile.GetServiceId(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceType = dpServiceInfoProfile.GetServiceType(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceName = dpServiceInfoProfile.GetServiceName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceDisplayName = dpServiceInfoProfile.GetServiceDisplayName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.customData = dpServiceInfoProfile.GetCustomData(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.dataLen = dpServiceInfoProfile.GetCustomDataLen(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.bundleName = dpServiceInfoProfile.GetBundleName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.moduleName = dpServiceInfoProfile.GetModuleName(); +// dmServiceInfoProfile.serviceRegInfo.serviceInfo.abilityName = dpServiceInfoProfile.GetAbilityName(); +// dmServiceInfoProfile.serviceRegInfo.authBoxType = dpServiceInfoProfile.GetAuthBoxType(); +// dmServiceInfoProfile.serviceReginfo.authType = dpServiceInfoProfile.GetAuthType(); +// dmServiceInfoProfile.serviceRegInfo.pinExchangeType= dpServiceInfoProfile.GetPinExchangeType(); +// dmServiceInfoProfile.serviceRegInfo.pinCode = dpServiceInfoProfile.GetPinCode(); +// dmServiceInfoProfile.serviceRegInfo.description = dpServiceInfoProfile.GetDescription(); +// dmServiceInfoProfile.serviceRegInfo.serviceDiscoveryScope = dpServiceInfoProfile.GetServieDicoveryScope(); +// dmServiceInfoProfile.extraInfo = dpServiceInfoProfile.GetExtraInfo(); +// } + DM_EXPORT std::vector DeviceProfileConnector::GetAccessControlProfile() { std::vector profiles; @@ -2716,6 +2766,136 @@ DM_EXPORT void DeviceProfileConnector::HandleDeviceUnBind(int32_t bindType, } } +DM_EXPORT int32_t DeviceProfileConnector::GetAllServiceInfoProfileList(std::vector &dmServiceInfoProfiles) +{ + // std::vector dpServiceInfoProfiles; + // int32_t ret = DistributedDeviceProfileClient::Getinstance().GetAllServiceInfoProfileList(dmServiceInfoProfiles); + // if (ret != DM_OK) { + // LOGE("failed: %{public}d",ret); + // return ret; + // } + // for (const auto &dpProfile : dpServiceInfoProfiles){ + // DmServiceInfoProfile dmProfile; + // TransferDpToDmServiceInfoProfile(dpProfile,dmProfile); + // dmServiceInfoProfiles.emplace_back(dmProfile); + // } + return DM_OK; +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoProfileListByTokenId(const DmServiceInfoUniqueKey &key, + std::vector &serviceInfos) +{ + ServiceInfoUniqueKey dpKey; + dpKey.SetTokenId(key.tokenId); + dpKey.SetDeviceId(key.deviceId); + dpKey.SetUserId(key.userId); + std::vector dpServiceInfos; + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId(dpKey, + dpServiceInfos); + if (ret != DM_OK) { + LOGE("failed: %{public}d",ret); + return ret; + } + for (auto dpServiceInfoProfile : dpServiceInfos) { + DmServiceInfoProfile dmServiceInfoProfile; + //TransferDpToDmServiceInfoProfile(dpServiceInfoProfile,dmServiceInfoProfile); + serviceInfos.push_back(dmServiceInfoProfile); + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileListByDeviceId(const std::string &deviceId, + std::vector &serviceInfos) +{ + std::vector serviceInfoProfiles; + + if (GetAllServiceInfoProfileList(serviceInfoProfiles)) { + return ERR_DM_FAILED; + } + if (serviceInfoProfiles.empty()) { + LOGE("GetServiceInfosByTokenId failed, serviceInfoProfiles is empty."); + return ERR_DM_FAILED; + } + for (auto const &item : serviceInfoProfiles) { + if (item.deviceId == deviceId) { + serviceInfos.push_back(item.serviceRegInfo.serviceInfo); + } + } + return DM_OK; +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const DmServiceInfoUniqueKey& key) +{ + DistributedDeviceProfile::ServiceInfoUniqueKey dpKey; + dpKey.SetDeviceId(key.deviceId); + dpKey.SetServiceId(key.serviceId); + dpKey.SetTokenId(key.tokenId); + dpKey.SetUserId(key.userId); + int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteServiceInfoProfile(dpKey); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +void DeviceProfileConnector::GetLocalTokenIdByDeviceIdAndUserId(std::string localUdid, std::string udid, + std::map &tokenToKey) +{ + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::vector profiles = GetAclProfileByDeviceIdAndUserId(localUdid, userId); + + uint64_t localToken; + uint64_t peerToken; + DmServiceInfoUniqueKey key = { 0 }; + key.deviceId = udid; + for (auto &item : profiles){ + if (item.GetStatus()!= ACTIVE) { + continue; + } + if (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserDeviceId() == udid) { + localToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + peerToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + key.tokenId = std::to_string(peerToken); + key.userId = item.GetAccesser().GetAccesserUserId(); + } + if (item.GetAccessee().GetAccesseeDeviceId() == udid && + item.GetAccesser().GetAccesserDeviceId() == localUdid) { + peerToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + localToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + key.tokenId = std::to_string(peerToken); + key.userId = item.GetAccessee().GetAccesseeUserId(); + } + tokenToKey.insert(std::pair(std::to_string(localToken), key)); + } +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoByTokenId(DmServiceInfoUniqueKey key, + std::vector &serviceInfos) +{ + std::vector infos; + DistributedDeviceProfile::ServiceInfoUniqueKey keyIndex(key.deviceId, key.userId, key.tokenId, -1); + auto ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId(keyIndex, infos); + if (ret != 0) { + return ret; + } + DmServiceInfo serviceInfo; + for (auto info : infos) { + serviceInfo.serviceId = info.GetServiceId(); + serviceInfo.serviceType = info.GetServiceType(); + serviceInfo.serviceName = info.GetServiceName(); + serviceInfo.serviceDisplayName = info.GetServiceDisplayName(); + serviceInfo.customData = info.GetCustomData(); + serviceInfo.customDataLen = info.GetCustomDataLen(); + serviceInfo.bundleName = info.GetBundleName(); + serviceInfo.moduleName = info.GetModuleName(); + serviceInfo.abilityName = info.GetAbilityName(); + serviceInfos.push_back(serviceInfo); + } + return DM_OK; +} + DM_EXPORT int32_t DeviceProfileConnector::SubscribeDeviceProfileInited( sptr dpInitedCallback) { diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index da0db3936..e2f242a64 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,12 @@ public: virtual bool CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee) = 0; virtual bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) = 0; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) = 0; + + virtual int32_t RegisterServiceStateCallback(const std::string &pkgName, const std::string &serviceType, + const std::map &filterOptions, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, + const std::string &serviceType) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h index e40343680..c33d2aa75 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h @@ -179,6 +179,17 @@ public: } virtual void OnCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode) = 0; }; + +class ServiceInfoStateCallback { +public: + virtual ~ServiceInfoStateCallback() + { + } + virtual void OnServiceOnline(const DmServiceInfo &serviceInfo); + virtual void OnServiceOffline(const DmServiceInfo &serviceInfo); + virtual void OnServiceInfoChange(const DmServiceInfo &serviceInfo); +}; + } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CALLBACK_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 863626aef..687401458 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -440,6 +440,12 @@ public: virtual bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; + virtual int32_t RegisterServiceStateCallback(const std::string &pkgName, const std::string &serviceType, + const std::map &filterOptions, + std::shared_ptr callback) override; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, + const std::string &serviceType) override; + private: DeviceManagerImpl() = default; ~DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index 67c3a32ae..d624a2675 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -30,6 +30,80 @@ namespace OHOS { namespace DistributedHardware { + +typedef struct DmServiceInfo { + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + uint32_t customDataLen = 0; + std::string bundleName; + std::string moduleName; + std::string abilityName; + //std::string extraInfo; +} DmServiceInfo; + +typedef struct ServiceRegInfo { + DmServiceInfo serviceInfo; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDiscoveryScope; +} ServiceRegInfo; + +typedef struct DmServiceInfoUniqueKey { + std::string deviceId; + int32_t userId = 0; + std::string tokenId; + int64_t serviceId = 0; +} DmServiceInfoUniqueKey; + +typedef struct DmServiceInfoProfile { + ServiceRegInfo serviceRegInfo; + std::string deviceId; + int32_t userId = 0; + std::string networkId; + std::string tokenId; + std::string extraInfo; +} DmServiceInfoProfile; + +typedef struct ServiceBindLocalInfo { + std::string localUdid; + int32_t userId = 0; + uint32_t tokenId = 0; +} ServiceBindLocalInfo; + +typedef struct DmServiceAddress{ + std::string bleMac; + uint32_t actionId = 0; +} DmServiceAddress; + +typedef struct DmServiceInfoDeviceProfile{ + std::string deviceId; + int32_t userId = 0; + std::string networkId; + std::string tokenId; + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + int32_t customDataLen = 0; + std::string bundleName; + std::string moduleName; + std::string abilityName; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDicoveryScope; + std::string extraInfo; +} DmServiceInfoDeviceProfile; + /** * @brief Device manager event notify. */ @@ -192,6 +266,9 @@ typedef struct DmDeviceInfo { * include json keys: "CONN_ADDR_TYPE", "BR_MAC_", "BLE_MAC", "WIFI_IP", "WIFI_PORT", "CUSTOM_DATA" */ std::string extraData; + + //std::vector serviceInfos; + std::map> tokenToServiceInfos; } DmDeviceInfo; /** @@ -292,11 +369,17 @@ typedef struct PeerTargetId { * wlan ip port. */ uint16_t wifiPort = 0; + /** + * service id. + */ + int64_t serviceId = 0; + bool operator==(const PeerTargetId &other) const { return (deviceId == other.deviceId) && (brMac == other.brMac) && - (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort); + (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort) && + (serviceId == other.serviceId); } bool operator<(const PeerTargetId &other) const @@ -306,7 +389,9 @@ typedef struct PeerTargetId { (deviceId == other.deviceId && brMac == other.brMac && bleMac < other.bleMac) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp < other.wifiIp) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && - wifiPort < other.wifiPort); + wifiPort < other.wifiPort) || + (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && + wifiPort == other.wifiPort && serviceId < other.serviceId); } } PeerTargetId; diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 39a229cc8..7ef2c8975 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -88,6 +88,9 @@ public: void OnSetRemoteDeviceNameResult(const std::string &pkgName, const std::string &deviceId, int32_t code); void UnRegisterPinHolderCallback(const std::string &pkgName); + int32_t RegisterServiceStateCallback(const std::string &key, std::shared_ptr callback); + int32_t UnRegisterServiceStateCallback(const std::string &key); + public: static void DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); static void DeviceInfoOffline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); @@ -103,6 +106,22 @@ public: std::shared_ptr tempCbk); static void DeviceTrustChange(const std::string &udid, const std::string &uuid, DmAuthForm authForm, std::shared_ptr tempCbk); + + static void ServiceInfoOnline( + std::vector, DmServiceInfo>> callbackInfo); + static void ServiceInfoOffline( + std::vector, DmServiceInfo>> callbackInfo); + static void ServiceInfoChange( + std::vector, DmServiceInfo>> callbackInfo); + +public: + void OnServiceOnline(std::string &pkgName, + std::map> &tokenToServiceInfos); + void OnServiceOffline(std::string &pkgName, + std::map> &tokenToServiceInfos); + void OnServiceChanged(std::string &pkgName, + std::map> &tokenToServiceInfos); + public: void OnRemoteDied(); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo); @@ -166,6 +185,7 @@ private: std::map> setLocalDeviceNameCallback_; std::map>> setRemoteDeviceNameCallback_; + std::map> serviceStateCallback_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 0b2bfebae..0da74038e 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -26,8 +26,8 @@ #include "dm_random.h" #include "ipc_acl_profile_req.h" #include "ipc_authenticate_device_req.h" -#include "ipc_bind_device_req.h" #include "ipc_bind_target_req.h" +#include "ipc_bind_device_req.h" #include "ipc_check_access_control.h" #include "ipc_common_param_req.h" #include "ipc_create_pin_holder_req.h" @@ -2994,5 +2994,32 @@ bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const Dm anonyLocalUdid_); return result; } + +int32_t DeviceManagerImpl::RegisterServiceStateCallback(const std::string &pkgName, const std::string &serviceType, + const std::map &filterOptions, std::shared_ptr callback) +{ + uint32_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + std::string key = pkgName + std::to_string(tokenId) + serviceType; + LOGI("DeviceManagerImpl::RegisterServiceStateCallback key=%{public}s", GetAnonyString(key.c_str()).c_str()); + int32_t ret = DeviceManagerNotify::GetInstance().RegisterServiceStateCallback(key, callback); + if (ret != DM_OK) { + LOGE("error: register callback failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceStateCallback(const std::string &pkgName, const std::string &serviceType) +{ + uint32_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + std::string key = pkgName + std::to_string(tokenId) + serviceType; + LOGI("SQW UnRegisterServiceStateCallback key=%{publiic}s", key.c_str()); + int32_t ret = DeviceManagerNotify::GetInstance().UnRegisterServiceStateCallback(key); + if (ret != DM_OK) { + LOGE("error: unregister callback failed: %{public}d", ret); + return ret; + } + return DM_OK; +} } // namespace DistributedHardware -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index a30e5493b..1178322d3 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -103,6 +103,78 @@ int32_t ReadResponse(const DMIpcCmdInterfaceCode &ipcCode, MessageParcel &reply, return DM_OK; } +void DecodeDmServiceInfo(MessageParcel &parcel, DmServiceInfo &serviceInfo) +{ + serviceInfo.serviceId = parcel.ReadInt64(); + serviceInfo.serviceType = parcel.ReadString(); + serviceInfo.serviceName = parcel.ReadString(); + serviceInfo.serviceDisplayName = parcel.ReadString(); + serviceInfo.customData = parcel.ReadString(); + serviceInfo.customDataLen = parcel.ReadUint32(); + serviceInfo.bundleName = parcel.ReadString(); + serviceInfo.moduleName = parcel.ReadString(); + serviceInfo.abilityName = parcel.ReadString(); +} + +void DecodeDmServiceInfo(std::map> &tokenToServiceInfos, + MessageParcel &parcel) +{ + int32_t mapSize = parcel.ReadInt32(); + if (mapSize == 0) { + return; + } + std::string token; + std::vector serviceInfos; + serviceInfos.reserve(10); + int32_t vectorSize; + int32_t dataLen; + int32_t i; + for(i = 0; i < mapSize; i++){ + serviceInfos.clear(); + token = parcel.ReadString(); + vectorSize = parcel.ReadInt32(); + dataLen = vectorSize * sizeof(DmServiceInfo); + DmServiceInfo *infoArray = static_cast(const_cast(parcel.ReadRawData(dataLen))); + if (infoArray != nullptr & memcpy_s(serviceInfos.data(),dataLen,infoArray,dataLen) != 0) { + LOGE("write serviceInfos failed"); + continue; + } + tokenToServiceInfos.emplace(token, serviceInfos); + } + return; +} + +void DecodeDmServiceInfoArray(std::map> &tokenToServiceInfos, + MessageParcel &parcel) +{ + int32_t mapSize = parcel.ReadInt32(); + if (mapSize == 0) { + return; + } + std::string token; + std::vector serviceInfos; + int32_t vectorSize; + int32_t dataLen; + int32_t i, j; + for (i = 0; i < mapSize; i++) { + serviceInfos.clear(); + token = parcel.ReadString(); + vectorSize = parcel.ReadInt32(); + LOGI("DmServiceInfo vector size:%{public}d", vectorSize); + dataLen = vectorSize * sizeof(DmServiceInfo); + DmServiceInfo *infoArray = static_cast(const_cast(parcel.ReadRawData(dataLen))); + if (infoArray == nullptr) { + LOGE("infoArray is nullptr"); + continue; + } + for (j = 0; j < vectorSize; j++) { + serviceInfos.push_back(infoArray[j]); + } + tokenToServiceInfos.emplace(token, serviceInfos); + } + return; +} + ON_IPC_SET_REQUEST(REGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, MessageParcel &data) { CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); @@ -461,20 +533,26 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply DmDeviceBasicInfo dmDeviceBasicInfo; IpcModelCodec::DecodeDmDeviceBasicInfo(data, dmDeviceBasicInfo); + + std::map> tokenToServiceInfos; + DecodeDmServiceInfoArray(tokenToServiceInfos, data); switch (deviceState) { case DEVICE_STATE_ONLINE: LOGI("Online pkgName:%{public}s", pkgName.c_str()); DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, dmDeviceInfo); DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, dmDeviceBasicInfo); + DeviceManagerNotify::GetInstance().OnServiceOnline(pkgName, tokenToServiceInfos); break; case DEVICE_STATE_OFFLINE: LOGI("Offline pkgName:%{public}s", pkgName.c_str()); DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, dmDeviceInfo); DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, dmDeviceBasicInfo); + DeviceManagerNotify::GetInstance().OnServiceOffline(pkgName, tokenToServiceInfos); break; case DEVICE_INFO_CHANGED: DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, dmDeviceInfo); DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, dmDeviceBasicInfo); + DeviceManagerNotify::GetInstance().OnServiceChanged(pkgName, tokenToServiceInfos); break; case DEVICE_INFO_READY: LOGI("OnDeviceReady in, pkgName:%{public}s, networkId: %{public}s.", @@ -2217,5 +2295,31 @@ ON_IPC_READ_RESPONSE(CHECK_SINK_SAME_ACCOUNT, MessageParcel &reply, std::shared_ { return ReadResponse(CHECK_SINK_SAME_ACCOUNT, reply, pBaseRsp); } + +// ON_IPC_SET_REQUEST(GET_CALLER_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +// { +// CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + +// return DM_OK; +// } + +// ON_IPC_READ_RESPONSE(GET_CALLER_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +// { +// if (pBaseRsp == nullptr) { +// LOGE("pBaseRsp is null"); +// return ERR_DM_FAILED; +// } +// std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); +// int32_t serviceNum = reply.ReadInt32(); +// std::vector serviceInfoVec; +// for (int32_t i = 0; i < serviceNum; i++) { +// DmServiceInfo serviceInfo; +// DecodeDmServiceInfo(reply, serviceInfo); +// serviceInfoVec.emplace_back(serviceInfo); +// } +// pRsp->SetServiceInfos(serviceInfoVec); +// pRsp->SetErrCode(reply.ReadInt32()); +// return DM_OK; +// } } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 278ebade4..7beda787c 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -36,6 +36,9 @@ constexpr const char* DEVICE_OFFLINE = "deviceOffline"; constexpr const char* DEVICEINFO_CHANGE = "deviceInfoChange"; constexpr const char* DEVICE_READY = "deviceReady"; constexpr const char* DEVICE_TRUST_CHANGE = "deviceTrustChange"; +constexpr const char* SERVICE_ONLINE = "serviceOnLine"; +constexpr const char* SERVICE_OFFLINE = "serviceOffLine"; +constexpr const char* SERVICEINFO_CHANGE = "serviceInfoChange"; #endif const uint16_t DM_INVALID_FLAG_ID = 0; void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName, @@ -1057,6 +1060,146 @@ void DeviceManagerNotify::DeviceTrustChange(const std::string &udid, const std:: tempCbk->OnDeviceTrustChange(udid, uuid, authForm); } +void DeviceManagerNotify::ServiceInfoOnline( + std::vector, DmServiceInfo>> callbackInfo) +{ + for (auto &iter : callbackInfo) { + if (iter.first == nullptr) { + continue; + } + iter.first->OnServiceOnline(iter.second); + } +} + +void DeviceManagerNotify::ServiceInfoOffline( + std::vector, DmServiceInfo>> callbackInfo) +{ + for (auto &iter : callbackInfo){ + if (iter.first == nullptr) { + continue; + } + iter.first->OnServiceOffline(iter.second); + } +} + +void DeviceManagerNotify::ServiceInfoChange( + std::vector, DmServiceInfo>> callbackInfo) +{ + for (auto &iter : callbackInfo) { + if (iter.first == nullptr) { + continue; + } + iter.first->OnServiceInfoChange(iter.second); + } +} + +void DeviceManagerNotify::OnServiceOnline(std::string &pkgName, + std::map> &tokenToServiceInfos) +{ + LOGI("Online with DmServiceInfo, pkgName:%{public}s", pkgName.c_str()); + if (pkgName.empty() || tokenToServiceInfos.size() == 0) { + LOGE("Invalid parameter, pkgName is empty or infoSize is 0."); + return; + } + + std::string key; + std::vector, DmServiceInfo>> callbackInfo; + for (auto it = tokenToServiceInfos.begin(); it != tokenToServiceInfos.end(); it++) { + std::lock_guard autoLock(lock_); + for (auto info : it->second) { + key = pkgName + it->first + info.serviceType; + auto iter = serviceStateCallback_.find(key); + if (iter == serviceStateCallback_.end()) { + continue; + } + callbackInfo.push_back( + std::pair,DmServiceInfo>(iter->second, info)); + } + } + LOGI("callbackInfo size:%{public}d", callbackInfo.size()); + if (callbackInfo.size() == 0) { + return; + } + +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::submit([=]() { ServiceInfoOnline(callbackInfo); }); +#else + std::thread serviceOnline([=]() { ServiceInfoOnline(callbackInfo);}); + int32_t ret = pthread_setname_np(serviceOnline.native_handle(), SERVICE_ONLINE); + if (ret != DM_OK) { + LOGE("DeviceManagerNotify serviceOnline setname failed."); + serviceOnline.detach(); +#endif +} + +void DeviceManagerNotify::OnServiceOffline(std::string &pkgName, + std::map> &tokenToServiceInfos) +{ + if (pkgName.empty() || tokenToServiceInfos.size() ==0 ) { + LOGE("Invalid parameter, pkgName is empty."); + return; + } + LOGI("Offline with DmServiceInfo, pkgName:%{public}s", pkgName.c_str()); + + std::string key; + std::vector, DmServiceInfo>> callbackInfo; + std::lock_guard autoLock(lock_); + for (auto it = tokenToServiceInfos.begin(); it != tokenToServiceInfos.end(); it++){ + for (auto info : it->second) { + key = pkgName + it->first + info.serviceType; + auto iter = serviceStateCallback_.find(key); + if(iter == serviceStateCallback_.end()) { + continue; + } + callbackInfo.push_back( + std::pair,DmServiceInfo>(iter->second, info)); + } + } +#if!(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::submit([=](){ServiceInfoOffline(callbackInfo);}); +#else + std::thread serviceOffline([=]() {ServiceInfoOffliine(callbackInfo);}); + int32_t ret = pthread_setname_np(serviceOffline.native_handle(),SERVICE_OFFLINE); + if(ret!=DM_OK){ + LOGE("DeviceManagerNotify serviceOffline setname failed."); + } + serviceOffline.detach(); +#endif +} + +void DeviceManagerNotify::OnServiceChanged(std::string &pkgName, + std::map> &tokenToServiceInfos) +{ + if (pkgName.empty() || tokenToServiceInfos.size() == 0) { + LOGE("Invalid parameter, pkgName is empty."); + return; + } + + std::string key; + std::vector, DmServiceInfo>> callbackInfo; + std::lock_guard autoLock(lock_); + for (auto it = tokenToServiceInfos.begin(); it != tokenToServiceInfos.end(); it++) { + for(auto info : it->second){ + key = pkgName + it->first + info.serviceType; + auto iter = serviceStateCallback_.find(key); + if(iter == serviceStateCallback_.end()) { + continue; + } + callbackInfo.push_back( + std::pair,DmServiceInfo>(iter->second, info)); + } + } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::submit([=](){ServiceInfoChange(callbackInfo);}); +#else + std::thread serviceChanged([=]() {ServiceInfoChangee(callbackInfo);}); + int32_t ret = pthread_setname_np(serviceChanged.native_handle(), SERVICEINFO_CHANGE); + if (ret != DM_OK) { + LOGE("DeviceManagerNotify serviceChanged setname failed."); + } + serviceChanged.detach(); +#endif +} void DeviceManagerNotify::RegisterDeviceScreenStatusCallback(const std::string &pkgName, std::shared_ptr callback) { @@ -1480,5 +1623,30 @@ void DeviceManagerNotify::UnRegisterPinHolderCallback(const std::string &pkgName std::lock_guard autoLock(lock_); pinHolderCallback_.erase(pkgName); } + +int32_t DeviceManagerNotify::RegisterServiceStateCallback(const std::string &key, + std::shared_ptr callback) +{ + LOGI("DeviceManagerNotify::RegisterServiceStateCallback key = %{public}s", GetAnonyString(key).c_str()); + if (key.empty() || callback == nullptr) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::lock_guard autolock(lock_); + serviceStateCallback_[key] = callback; + return DM_OK; +} + +int32_t DeviceManagerNotify::UnRegisterServiceStateCallback(const std::string &key) +{ + LOGI("DeviceManagerNotify::UnRegisterServiceStateCallback key = %{public}s", GetAnonyString(key).c_str()); + if (key.empty() || serviceStateCallback_.find(key) == serviceStateCallback_.end()) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::lock_guard autolock(lock_); + serviceStateCallback_.erase(key); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/devicestate/dm_device_state_manager.h b/services/implementation/include/devicestate/dm_device_state_manager.h index 254024269..9dafaf8be 100644 --- a/services/implementation/include/devicestate/dm_device_state_manager.h +++ b/services/implementation/include/devicestate/dm_device_state_manager.h @@ -113,7 +113,7 @@ private: int32_t DeleteGroupByDP(const std::string &deviceId); void DeleteCredential(DmOfflineParam offlineParam, const std::string &deviceId); #endif - void ProcessDeviceStateChange(const DmDeviceState devState, const DmDeviceInfo &devInfo); + void ProcessDeviceStateChange(const DmDeviceState devState, DmDeviceInfo &devInfo); private: std::mutex timerMapMutex_; std::mutex remoteDeviceInfosMutex_; diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index 573c67757..508e0bd93 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -169,12 +169,35 @@ void DmDeviceStateManager::HandleDeviceStatusChange(DmDeviceState devState, DmDe } } -void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState, const DmDeviceInfo &devInfo) +void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState, DmDeviceInfo &devInfo) { LOGI("begin, devState = %{public}d networkId: %{public}s.", devState, GetAnonyString(devInfo.networkId).c_str()); CHECK_NULL_VOID(softbusConnector_); CHECK_NULL_VOID(listener_); + + //DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByDeviceId(std::string(devInfo.deviceId), devInfo.serviceInfos); + //std::map> tokenToServiceInfos; + std::map tokenToKey; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = static_cast(localDeviceId); + std::string udid = softbusConnector_->GetDeviceUdidByUdidHash(devInfo.deviceId); + DeviceProfileConnector::GetInstance().GetLocalTokenIdByDeviceIdAndUserId(localUdid, udid, tokenToKey); + std::vector serviceInfos; + for (auto iter = tokenToKey.begin(); iter != tokenToKey.end();iter++) { + serviceInfos.clear(); + if (DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(iter->second, serviceInfos)) { + LOGE("GetServiceInfoByTokenId failed."); + continue; + } + if (serviceInfos.size() == 0) { + continue; + } + //tokenToServiceInfos.emplace(iter->first, serviceInfos); + devInfo.tokenToServiceInfos.emplace(iter->first, serviceInfos); + } + std::vector processInfoVec; if (devState == DEVICE_INFO_CHANGED) { processInfoVec = softbusConnector_->GetChangeProcessInfo(); diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 216835016..94a3b8047 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -104,6 +104,18 @@ private: const DmDeviceBasicInfo &deviceBasicInfo); void ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo); + + void SetDeviceInfo(std::shared_ptr pReq, const std::string &pkgName, + const DmDeviceState &state, const DmDeviceInfo &setDeviceInfo, const DmDeviceBasicInfo &deviceBasicInfo); + void ProcessDeviceStateChange(const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, + std::map> &tokenToServiceInfos); + void ProcessAppStateChange(const std::string &pkgName, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo&deviceBasicInfo, + std::map> &tokenToServiceInfos); + + std::string ComposeOnlineKey(const std::string &pkgName, const std::string &devId); + void SetDeviceScreenInfo(std::shared_ptr pReq, const ProcessInfo &processInfo, const DmDeviceInfo &deviceInfo); void RemoveOnlinePkgName(const DmDeviceInfo &info); @@ -135,6 +147,8 @@ private: static std::unordered_set highPriorityPkgNameSet_; static std::mutex actUnrelatedPkgNameLock_; static std::set actUnrelatedPkgName_; + static std::mutex alreadyOnlineSetLock_; + static std::unordered_set alreadyOnlineSet_; #endif }; } // namespace DistributedHardware diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index bc47a6a81..34583efcf 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -60,6 +60,10 @@ std::mutex DeviceManagerServiceListener::actUnrelatedPkgNameLock_; std::set DeviceManagerServiceListener::actUnrelatedPkgName_ = {}; std::unordered_set DeviceManagerServiceListener::highPriorityPkgNameSet_ = { "ohos.deviceprofile", "ohos.distributeddata.service" }; +const int32_t LAST_APP_ONLINE_NUMS = 8; + +std::mutex DeviceManagerServiceListener::alreadyOnlineSetLock_; +std::unordered_set DeviceManagerServiceListener::alreadyOnlineSet_ = {}; void handleExtraData(const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo) { @@ -248,6 +252,142 @@ void DeviceManagerServiceListener::ProcessAppStateChange(const ProcessInfo &proc } } +void DeviceManagerServiceListener::SetDeviceInfo(std::shared_ptr pReq, const std::string &pkgName, + const DmDeviceState &state, const DmDeviceInfo &deviceInfo, const DmDeviceBasicInfo &deviceBasicInfo) +{ + LOGD("DeviceManagerServicelistener::SetDeviceInfo"); + pReq->SetPkgName(pkgName); + pReq->SetDeviceState(state); +#if !(defined(_LITEOS_M__) || defined(LITE_DEVICE)) + std::string appId = ""; + if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) { + pReq->SetDeviceInfo(deviceInfo); + pReq->SetDeviceBasicInfo(deviceBasicInfo); + return; + } + DmDeviceInfo dmDeviceInfo = deviceInfo; + ConvertUdidHashToAnoyAndSave(pkgName,dmDeviceInfo); + DmDeviceBasicInfo dmDeviceBasicInfo = deviceBasicInfo; + (void)memset_s(dmDeviceBasicInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0,DM_MAX_DEVICE_ID_LEN); + if (memcpy_s(dmDeviceBasicInfo.deviceId, sizeof(dmDeviceBasicInfo.deviceId), dmDeviceInfo.deviceId, + std::min(sizeof(dmDeviceBasicInfo.deviceId),sizeof (dmDeviceInfo.deviceId))) != DM_OK) { + LOGE("ConvertNodeBasicinfoToDmDevice copy deviceid datafailed."); + } + pReq->SetDeviceInfo(dmDeviceInfo); + pReq->SetDeviceBasicInfo(dmDeviceBasicInfo); + return; +#endif + pReq->SetDeviceInfo(deviceInfo); + pReq->SetDeviceBasicInfo(deviceBasicInfo); +} + +void DeviceManagerServiceListener::ProcessDeviceStateChange(const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, + std::map> &tokenToServiceInfos) +{ + LOGI("In"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + std::vector ProcessInfo = ipcServerListener_.GetAllProcessInfo(); + if (state == DEVICE_STATE_OFFLINE) { + for (const auto &it : ProcessInfo) { + std::string notifyKey = ComposeOnlineKey(it.pkgName, std::string(info.deviceId)); + { + std::lock_guard autolock(alreadyOnlineSetLock_); + alreadyOnlineSet_.erase(notifyKey); + } + SetDeviceInfo(pReq, it.pkgName, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } + } + if (state == DEVICE_STATE_ONLINE) { + for (const auto &it : ProcessInfo) { + std::string notifyKey = ComposeOnlineKey(it.pkgName, std::string(info.deviceId)); + DmDeviceState notifyState = state; + { + std::lock_guard autolock(alreadyOnlineSetLock_); + if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) { + notifyState = DmDeviceState::DEVICE_INFO_CHANGED; + } else { + alreadyOnlineSet_.insert(notifyKey); + } + } + SetDeviceInfo(pReq, it.pkgName, notifyState, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } + } + if (state == DEVICE_INFO_READY || state == DEVICE_INFO_CHANGED) { + for (const auto &it : ProcessInfo) { + SetDeviceInfo(pReq, it.pkgName, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } + } +} + +void DeviceManagerServiceListener::ProcessAppStateChange(const std::string &pkgName, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo&deviceBasicInfo, + std::map> &tokenToServiceInfos) +{ + LOGI("In"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + std::unordered_set notifyPkgnames = std::unordered_set(); + notifyPkgnames.insert(pkgName); + if (state == DEVICE_STATE_ONLINE) { + for (const auto &it : notifyPkgnames) { + std::string notifyKey = it + "_" + info.deviceId; + { + std::lock_guard autoLock(alreadyOnlineSetLock_); + if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()){ + continue; + } + alreadyOnlineSet_.insert(notifyKey); + } + SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY,pReq, pRsp); + } + } + if (state == DEVICE_STATE_OFFLINE) { + if (alreadyOnlineSet_.size() == LAST_APP_ONLINE_NUMS) { + { + std::lock_guard autoLock(alreadyOnlineSetLock_); + alreadyOnlineSet_.clear(); + } + for (const auto &it : notifyPkgnames) { + SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY,pReq, pRsp); + } + } + else { + std::string notifyKey = pkgName + "_" + info.deviceId; + { + std::lock_guard autoLock(alreadyOnlineSetLock_); + if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) { + alreadyOnlineSet_.erase(notifyKey); + } + } + SetDeviceInfo(pReq, pkgName, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY,pReq, pRsp); + } + } + if (state == DEVICE_INFO_READY || state == DEVICE_INFO_CHANGED) { + SetDeviceInfo(pReq, pkgName, state, info, deviceBasicInfo); + pReq->SetServiceInfos(tokenToServiceInfos); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq,pRsp); + } +} + +std::string DeviceManagerServiceListener::ComposeOnlineKey(const std::string &pkgName, const std::string &devId) +{ + return pkgName + "_" + devId; +} + void DeviceManagerServiceListener::OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info) { -- Gitee