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 26d2f59ead3845b723759210607823487af0c123..161ea9a5c3be8701cd468de535b868c69612bdc1 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,21 @@ public: dmDeviceBasicInfo_ = dmDeviceBasicInfo; } + void SetServiceIds(const std::vector &serviceIds) + { + serviceIds_ = serviceIds; + } + + const std::vector &GetServiceIds() const + { + return serviceIds_; + } + private: int32_t deviceState_ { 0 }; DmDeviceInfo dmDeviceInfo_; DmDeviceBasicInfo dmDeviceBasicInfo_; + std::vector serviceIds_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/standard/ipc_model_codec.h b/common/include/ipc/standard/ipc_model_codec.h index e06af83a94477da5100f2039832ccb033d070f7d..41d91eec985f94b6721932a4691bd0bc36115d68 100644 --- a/common/include/ipc/standard/ipc_model_codec.h +++ b/common/include/ipc/standard/ipc_model_codec.h @@ -56,6 +56,8 @@ public: static bool DecodeNetworkIdQueryFilter(MessageParcel &parcel, NetworkIdQueryFilter &queryFilter); static bool EncodeStringVector(const std::vector &vec, MessageParcel &parcel); static bool DecodeStringVector(MessageParcel &parcel, std::vector &vec); + static bool EncodeServiceIds(const std::vector &serviceIds, MessageParcel &parcel); + static void DecodeServiceIds(std::vector &serviceIds, MessageParcel &parcel); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index 63c05ae86989b470ecd36528fceafde5a3072e45..c0cd992a641ed215efbcce7bee868aa794fe37fc 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -313,6 +313,29 @@ bool IpcModelCodec::EncodeDmDeviceIconInfo(const DmDeviceIconInfo &deviceIconInf return bRet; } +bool IpcModelCodec::EncodeServiceIds(const std::vector &serviceIds, MessageParcel &parcel) +{ + int32_t size = serviceIds.size(); + if (!parcel.WriteInt32(size)) { + return false; + } + for (const auto &id : serviceIds) { + if (!parcel.WriteInt64(id)) { + return false; + } + } + return true; +} + +void IpcModelCodec::DecodeServiceIds(std::vector &serviceIds, MessageParcel &parcel) +{ + int32_t size = parcel.ReadInt32(); + serviceIds.resize(size); + for (int32_t i = 0; i < size; ++i) { + serviceIds[i] = parcel.ReadInt64(); + } +} + void IpcModelCodec::DecodeDmDeviceIconInfoFilterOptions(MessageParcel &parcel, DmDeviceIconInfoFilterOptions &filterOptions) { diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 3923a314d7a56f349674d0e3b69db2417a284201..95597ba0dfa6ca3576b01c027b67c17901dcc2d3 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -330,6 +330,9 @@ public: const std::vector &backGroundUserIds); DM_EXPORT bool IsAllowAuthAlways(const std::string &localUdid, int32_t userId, const std::string &peerUdid, const std::string &pkgName, int64_t tokenId); + DM_EXPORT void GetRemoteTokenIds(const std::string &localUdid, const std::string &udid, + std::vector &remoteTokenIds); + DM_EXPORT int32_t GetServiceInfoByTokenId(int64_t tokenId, ServiceInfoProfile &serviceInfo); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index b43bd68c156e7ed2a60c59b7122a8066f3c343d6..8132f812ba1c8d4bcab4c13f2bf516e611fa872c 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3471,6 +3471,50 @@ DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdi } } +DM_EXPORT void DeviceProfileConnector::GetRemoteTokenIds(const std::string &localUdid, + const std::string &udid, std::vector &remoteTokenIds) +{ + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::vector profiles = GetAclProfileByDeviceIdAndUserId(localUdid, userId); + uint64_t peerToken = 0; + for (auto &item : profiles) { + if(item.GetStatus() != ACTIVE) { + continue; + } + if (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserDeviceId() == udid) { + peerToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + remoteTokenIds.push_back(peerToken); + } + if (item.GetAccessee().GetAccesseeDeviceId()== udid && + item.GetAccesser().GetAccesserDeviceId() == localUdid) { + peerToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + remoteTokenIds.push_back(peerToken); + } + } +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoByTokenId(int64_t tokenId, + ServiceInfoProfile &serviceInfo) +{ + DistributedDeviceProfile::ServiceInfoProfileNew dpServiceInfo; + auto ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByTokenId(tokenId, dpServiceInfo); + if (ret != 0) { + LOGE("GetServiceInfoByTokenId GetServiceInfoProfileByTokenId failed."); + return ret; + } + serviceInfo.regServiceId = dpServiceInfo.GetRegServiceId(); + serviceInfo.deviceId = dpServiceInfo.GetDeviceId(); + serviceInfo.userId = dpServiceInfo.GetUserId(); + serviceInfo.tokenId = dpServiceInfo.GetTokenId(); + serviceInfo.publishState = dpServiceInfo.GetSerPubState(); + serviceInfo.serviceId = dpServiceInfo.GetServiceId(); + serviceInfo.serviceType = dpServiceInfo.GetServiceType(); + serviceInfo.serviceName = dpServiceInfo.GetServiceName(); + serviceInfo.serviceDisplayName = dpServiceInfo.GetServiceDisplayName(); + return DM_OK; +} + bool DeviceProfileConnector::CheckExtWhiteList(const std::string &pkgName) { LOGI("start pkgName %{public}s.", pkgName.c_str()); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index da0db3936076c2cece54906b35ba28a0a818fe77..53c547ac07ca8eca2cce1c2f1c25dac858e67321 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,9 @@ 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, int64_t serviceId, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) = 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 e403436809b85a6d2069e2e947ba7a8559dc6e39..3ef435ee731f8dcbc4995c51c660e9c69ab92ae4 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,15 @@ public: } virtual void OnCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode) = 0; }; + +class ServiceInfoStateCallback { +public: + virtual ~ServiceInfoStateCallback() + { + } + virtual void OnServiceOnline(int64_t serviceId) = 0; + virtual void OnServiceOffline(int64_t serviceId) = 0; +}; } // 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 863626aef135a41ee472de50ce98df36a35c596b..8c604013090d3d8a50e3084d0f7fea23c8982f46 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -439,6 +439,9 @@ public: virtual bool CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee) override; 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, int64_t serviceId, + std::shared_ptr callback) override; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) override; private: 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 b7704d94df412168e0e12abc80305242450246e6..74a5f0b7eae894340bc52d618cdc389422a8eb5b 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -470,6 +470,18 @@ typedef struct DMAclQuadInfo { std::string peerUdid; int32_t peerUserId; } DMAclQuadInfo; + +typedef struct ServiceInfoProfile { + int32_t regServiceId; + std::string deviceId; + int32_t userId; + int64_t tokenId; + int8_t publishState; + int64_t serviceId; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; +} ServiceInfoProfile; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h index 336c5b631bd1e523058e86a725f72225b2997688..9b3bf67a52a3113296ffd263545a41f6c9912d5e 100644 --- a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h +++ b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h @@ -177,6 +177,8 @@ public: virtual std::string GetLocalDisplayDeviceName() = 0; virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; + virtual void OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) = 0; }; } // namespace DistributedHardware } // namespace OHOS 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 39a229cc8fa23bb023351d03ab785b694637c7ab..a2aed06051514f5fc9e69be4662fddfefbf8e435 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 @@ -87,6 +87,8 @@ public: std::shared_ptr callback); 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); @@ -103,6 +105,8 @@ 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, int64_t>> callbackInfo); public: void OnRemoteDied(); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo); @@ -139,6 +143,7 @@ public: std::string content); std::shared_ptr GetDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId); void GetCallBack(std::map> &callbackMap); + void OnServiceOnline(const std::vector &serviceIds); private: #if !defined(__LITEOS_M__) @@ -166,6 +171,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 d07c6ff6860971db662beb87cfc9f1e12e74e489..b2874ebdb00575e1761af319ed749c8bc917581b 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -2998,5 +2998,32 @@ bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const Dm anonyLocalUdid_); return result; } + +int32_t DeviceManagerImpl::RegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId, + std::shared_ptr callback) +{ + std::string key = std::to_string(serviceId); + 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; + } + SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) +{ + std::string key = std::to_string(serviceId); + LOGI("DeviceManagerImpl: UnRegisterServiceStateCallback key=%{public}s", GetAnonyString(key.c_str()).c_str()); + int32_t ret = DeviceManagerNotify::GetInstance().UnRegisterServiceStateCallback(key); + if (ret != DM_OK) { + LOGE("error: unregister callback failed: %{public}d", ret); + return ret; + } + SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_STATE, pkgName); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS 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 a30e5493b2b6ea81ff6113a7f7f820ed819f662a..1dbe205ae712fd3ddbae1ac6bfa8131ce26eb6dc 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 @@ -461,11 +461,14 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply DmDeviceBasicInfo dmDeviceBasicInfo; IpcModelCodec::DecodeDmDeviceBasicInfo(data, dmDeviceBasicInfo); + std::vector serviceIds; + IpcModelCodec::DecodeServiceIds(serviceIds, 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(serviceIds); break; case DEVICE_STATE_OFFLINE: LOGI("Offline pkgName:%{public}s", pkgName.c_str()); 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 907efd2984775652335f81fb84458d28406fce53..fbc2984700ab3c49b65fea208fe56209e859b712 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,7 @@ 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"; #endif const uint16_t DM_INVALID_FLAG_ID = 0; void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName, @@ -1450,5 +1451,76 @@ void DeviceManagerNotify::UnRegisterPinHolderCallback(const std::string &pkgName std::lock_guard autoLock(lock_); pinHolderCallback_.erase(pkgName); } + +void DeviceManagerNotify::OnServiceOnline(const std::vector &serviceIds) +{ + if (serviceIds.empty()) { + LOGE("Invalid parameter, serviceIds is empty."); + return; + } + LOGI("In, serviceIds size: %{public}zu", serviceIds.size()); + std::string key = ""; + std::vector, int64_t>> callbackInfo; + std::lock_guard autoLock(lock_); + for (const auto &item : serviceIds) { + key = std::to_string(item); + auto iter = serviceStateCallback_.find(key); + if (iter == serviceStateCallback_.end()) { + continue; + } + callbackInfo.push_back( + std::pair, int64_t>(iter->second, item)); + } + if (callbackInfo.empty()) { + 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::ServiceInfoOnline( + std::vector, int64_t>> callbackInfo) +{ + for (auto &iter : callbackInfo) { + if (iter.first == nullptr) { + continue; + } + iter.first->OnServiceOnline(iter.second); + } +} + +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()); + std::lock_guard autolock(lock_); + if (key.empty() || serviceStateCallback_.find(key) == serviceStateCallback_.end()) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + serviceStateCallback_.erase(key); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index e77e579b7fd319f9819489bac22c05e69606b21f..e7bc9f3d284ce91e1dd687bd600ee6090d8c1a33 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -175,10 +175,35 @@ void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState LOGI("begin, devState = %{public}d networkId: %{public}s.", devState, GetAnonyString(devInfo.networkId).c_str()); CHECK_NULL_VOID(listener_); + std::vector remoteTokenIds; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = static_cast(localDeviceId); + std::string udid = softbusConnector_->GetDeviceUdidByUdidHash(devInfo.deviceId); + if (udid == "" || localUdid == "" || localUdid == udid) { + LOGE("ProcessDeviceStateChange udid error."); + return; + } + DeviceProfileConnector::GetInstance().GetRemoteTokenIds(localUdid, udid, remoteTokenIds); + std::vector remoteServiceIds; + for (const auto &item : remoteTokenIds) { + ServiceInfoProfile serviceInfo; + if (DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(item, serviceInfo) != DM_OK) { + LOGE("GetServiceInfoByTokenid failed."); + continue; + } + remoteServiceIds.push_back(serviceInfo.serviceId); + } + LOGI("ProcessDeviceStateChange, remoteServiceIds size: %{public}zu", remoteServiceIds.size()); + for (const auto &item : processInfoVec) { if (!item.pkgName.empty()) { LOGI("ProcessDeviceStateChange, pkgName = %{public}s", item.pkgName.c_str()); - listener_->OnDeviceStateChange(item, devState, devInfo); + if (!remoteServiceIds.empty() && devState == DEVICE_STATE_ONLINE) { + listener_->OnDeviceStateChange(item, devState, devInfo, remoteServiceIds); + } else { + listener_->OnDeviceStateChange(item, devState, devInfo); + } } } } diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 216835016a96327f3e1961ba1e80cab2facb1e72..6f75c0a6c4383526ce4178cc43440f0916c1d7eb 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -90,6 +90,8 @@ public: void OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceId, const std::string &deviceName, int32_t code) override; void SetExistPkgName(const std::set &pkgNameSet) override; + void OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) override; std::string GetLocalDisplayDeviceName() override; int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) override; @@ -127,6 +129,17 @@ private: void ProcessAppOffline(const std::vector &procInfoVec, const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo); void RemoveNotExistProcess(); + void ProcessDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds); + void ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); + void ProcessDeviceOnline(const std::vector &procinfoVec, const ProcessInfo &processInfo, + const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); + void ProcessAppOnline(const std::vector &procInfoVec, const ProcessInfo &processInfo, + const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); private: #if !defined(__LITEOS_M__) IpcServerListener ipcServerListener_; diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index bc47a6a81fc2c3d3908b7aa811a1d160a185c12a..d28782c3d1575b738bcd564c7529f34d64165226 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -1032,6 +1032,101 @@ void DeviceManagerServiceListener::SetExistPkgName(const std::set & } } +void DeviceManagerServiceListener::ProcessDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + std::vector processInfoVec = GetNotifyProcessInfoByUserId(processInfo.userId, + DmCommonNotifyEvent::REG_DEVICE_STATE); + std::vector hpProcessInfoVec; + std::vector lpProcessInfoVec; + for (const auto &it : processInfoVec) { + if (highPriorityPkgNameSet_.find(it.pkgName) != highPriorityPkgNameSet_.end()) { + hpProcessInfoVec.push_back(it); + } else { + lpProcessInfoVec.push_back(it); + } + } + ProcessDeviceOnline(hpProcessInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); + ProcessDeviceOnline(lpProcessInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); +} + +void DeviceManagerServiceListener::ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds) +{ + std::vector processInfoVec = GetWhiteListSAProcessInfo(DmCommonNotifyEvent::REG_DEVICE_STATE); + ProcessInfo bindProcessInfo = DealBindProcessInfo(processInfo); + processInfoVec.push_back(bindProcessInfo); + std::vector allProcessInfos = ipcServerListener_.GetAllProcessInfo(); + ProcessAppOnline(processInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); +} + +void DeviceManagerServiceListener::OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) +{ + LOGI("OnDeviceStateChange, state = %{public}d", state); + DmDeviceBasicInfo deviceBasicInfo; + ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, info, deviceBasicInfo); + if (processInfo.pkgName == std::string(DM_PKG_NAME)) { + ProcessDeviceStateChange(processInfo, state, info, deviceBasicInfo, serviceIds); + } else { + ProcessAppStateChange(processInfo, state, info, deviceBasicInfo, serviceIds); + } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + KVAdapterManager::GetInstance().DeleteAgedEntry(); +#endif +} + +void DeviceManagerServiceListener::ProcessDeviceOnline(const std::vector &procInfoVec, + const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + LOGI("userid %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast(state), + GetAnonyString(info.deviceId).c_str()); + for (const auto &it : procInfoVec) { + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetServiceIds(serviceIds); + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); + DmDeviceState notifyState = state; + { + std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + notifyState = DmDeviceState::DEVICE_INFO_CHANGED; + } else { + alreadyOnlinePkgName_[notifyPkgName] = info; + } + } + SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } +} + +void DeviceManagerServiceListener::ProcessAppOnline(const std::vector &procInfoVec, + const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + LOGI("userid %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast(state), + GetAnonyString(info.deviceId).c_str()); + for (const auto &it : procInfoVec) { + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetServiceIds(serviceIds); + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); + DmDeviceState notifyState = state; + { + std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + notifyState = DmDeviceState::DEVICE_INFO_CHANGED; + } else { + alreadyOnlinePkgName_[notifyPkgName] = info; + } + } + SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } +} + std::string DeviceManagerServiceListener::GetLocalDisplayDeviceName() { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index e8f908bca808e91b7bef061b7d87ed46e4c9e3c3..d335ebbc7741945f6226da00dfbbb03d488b7d87 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -191,6 +191,7 @@ ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, int32_t deviceState = pReq->GetDeviceState(); DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); DmDeviceBasicInfo deviceBasicInfo = pReq->GetDeviceBasicInfo(); + std::vector serviceIds = pReq->GetServiceIds(); if (!data.WriteString(pkgName)) { LOGE("write pkgName failed"); return ERR_DM_IPC_WRITE_FAILED; @@ -207,6 +208,10 @@ ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, LOGE("write dm device basic info failed"); return ERR_DM_IPC_WRITE_FAILED; } + if (!IpcModelCodec::EncodeServiceIds(serviceIds, data)) { + LOGE("write dm service info failed"); + return ERR_DM_IPC_WRITE_FAILED; + } return DM_OK; }