From 43098bd8167d2b2f6e156335c1bf28087a95db01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Mon, 21 Jul 2025 14:39:01 +0800 Subject: [PATCH] service_code_5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../device_manager_ipc_interface_code.h | 4 +- .../ipc/model/ipc_start_publish_service_req.h | 55 ++++++ .../ipc/model/ipc_stop_publish_service_req.h | 41 +++++ .../include/deviceprofile_connector.h | 8 + .../src/deviceprofile_connector.cpp | 97 ++++++++++ .../native_cpp/include/device_manager.h | 2 + .../native_cpp/include/device_manager_impl.h | 4 + .../native_cpp/include/dm_device_info.h | 54 ++++++ .../native_cpp/src/device_manager_impl.cpp | 51 ++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 71 +++++++- services/implementation/BUILD.gn | 2 + .../dependency/softbus/softbus_connector.h | 3 +- .../include/device_manager_service_impl.h | 8 +- .../device_manager_service_impl_lite.h | 4 + .../publish/dm_service_publish_manager.h | 44 +++++ .../dependency/softbus/softbus_connector.cpp | 31 ++++ .../src/device_manager_service_impl.cpp | 37 ++++ .../src/device_manager_service_impl_lite.cpp | 18 ++ .../publish/dm_service_publish_manager.cpp | 98 +++++++++++ .../service/include/device_manager_service.h | 6 + .../include/idevice_manager_service_impl.h | 5 + .../service/src/device_manager_service.cpp | 84 +++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 51 ++++++ .../UTTest_softbus_connector.cpp | 20 +++ .../UTTest_device_manager_impl_three.cpp | 87 +++++++++ ...Test_device_manager_service_impl_first.cpp | 138 +++++++++++++++ .../UTTest_device_manager_service_three.cpp | 165 ++++++++++++++++++ .../UTTest_device_manager_service_three.h | 3 + .../mock/device_manager_service_impl_mock.cpp | 16 ++ .../mock/device_manager_service_impl_mock.h | 8 + .../mock/deviceprofile_connector_mock.cpp | 6 + .../mock/deviceprofile_connector_mock.h | 2 + test/unittest/mock/softbus_connector_mock.cpp | 6 + test/unittest/mock/softbus_connector_mock.h | 5 +- 34 files changed, 1225 insertions(+), 9 deletions(-) create mode 100644 common/include/ipc/model/ipc_start_publish_service_req.h create mode 100644 common/include/ipc/model/ipc_stop_publish_service_req.h create mode 100644 services/implementation/include/publish/dm_service_publish_manager.h create mode 100644 services/implementation/src/publish/dm_service_publish_manager.cpp diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index 418684851..8aa34ad48 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -123,7 +123,9 @@ enum DMIpcCmdInterfaceCode { CHECK_SRC_SAME_ACCOUNT, CHECK_SINK_SAME_ACCOUNT, // Add ipc msg here - IPC_MSG_BUTT + IPC_MSG_BUTT, + START_PUBLISH_SERVICE, + STOP_PUBLISH_SERVICE, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_start_publish_service_req.h b/common/include/ipc/model/ipc_start_publish_service_req.h new file mode 100644 index 000000000..3895ca750 --- /dev/null +++ b/common/include/ipc/model/ipc_start_publish_service_req.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H +#define OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H + +#include "dm_subscribe_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { + +class IpcStartPublishServiceReq : public IpcReq { + DECLARE_IPC_MODEL(IpcStartPublishServiceReq); + +public: + DmDiscoverMode GetDiscoverMode() + { + return discoverMode_; + } + + void SetDiscoverMode(const DmDiscoverMode& discoverMode) + { + discoverMode_ = discoverMode; + } + + int64_t GetServiceId() + { + return serviceId_; + } + + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; + DmDiscoverMode discoverMode_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_stop_publish_service_req.h b/common/include/ipc/model/ipc_stop_publish_service_req.h new file mode 100644 index 000000000..2db22f423 --- /dev/null +++ b/common/include/ipc/model/ipc_stop_publish_service_req.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_IPC_STOP_PUBLISH_SERVICE_REQ_H +#define OHOS_DM_IPC_STOP_PUBLISH_SERVICE_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcStopPublishServiceReq : public IpcReq { + DECLARE_IPC_MODEL(IpcStopPublishServiceReq); + +public: + int64_t GetServiceId() + { + return serviceId_; + } + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_STOP_PUBLISH_SERVICE_REQ_H \ No newline at end of file diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index b6e6df3ec..2b4f2b089 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -328,6 +328,14 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); DM_EXPORT void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); + + DM_EXPORT int32_t GetServiceInfoByServiceId(ServiceInfoUniqueKey key, ServiceInfoProfile& serviceInfoProfile); + DM_EXPORT int32_t PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile); + DM_EXPORT int32_t UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile); + DM_EXPORT int32_t DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key); + DM_EXPORT int32_t GetServiceInfoProfileByUniqueKey( + const ServiceInfoUniqueKey& key, ServiceInfoProfile& serviceInfoProfile); + 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 132a0a795..16ab20b0c 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3489,6 +3489,103 @@ bool DeviceProfileConnector::CheckExtWhiteList(const std::string &pkgName) return false; } +int32_t DeviceProfileConnector::GetServiceInfoByServiceId( + ServiceInfoUniqueKey key, ServiceInfoProfile& serviceInfoProfile) +{ + // DistributedDeviceProfile::ServiceInfoProfile info; + // DistributedDeviceProfile::ServiceInfoUniqueKey keyIndex(key.deviceId, key.userId, key.tokenId, key.serviceId); + // auto ret - DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByUniquekey(keyIndex, info); + // if (ret != 0) { + // return ret; + // } + // ServiceInfo& serviceInfo = serviceInfoProfile.serviceRegInfo.serviceInfo; + // serviceInfoProfile.deviceId = info.GetDeviceId(); + // serviceInfoProfile.userId = info.GetUserId(); + // serviceInfoProfile.networkId = info.GetNetworkId(); + // serviceInfoProfile.tokenId = info.GetTokenId(); + + // serviceInfo.serviceId = info.GetServiceId(); + // serviceInfo.serviceType = info.GetServiceType(); + // serviceInfo.serviceName = info.GetServiceName(); + // serviceInfo.serviceDisplayName = info.GetServiceDisplayName(); + // serviceInfo.customData = info.GetCustomData(); + // serviceInfo.dataLen = info.GetCustomDataLen(); + // serviceInfo.bundleName = info.GetBundleName(); + // serviceInfo.moduleName = info.GetModuleName(); + // serviceInfo.abilityName = info.GetAbilityName(); + // serviceInfo.authBoxType = info.GetAuthBoxType(); + // serviceInfo.authType = info.GetAuthType(); + // serviceInfo.pinExchangeType = info.GetPinExchangeType(); + // serviceInfo.pinCode = info.GetPinCode(); + // serviceInfo.description = info.GetDescription(); + // serviceInfo.serviceDicoveryScope = info.GetServiceDicoveryScope(); + // serviceInfoProfile.extraInfo = info.GetExtraInfo(); + return DM_OK; +} + +int32_t DeviceProfileConnector::PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) +{ + // DistributedDeviceProfile::ServiceInfoProfile dpserviceInfoprofile; + // TransferDmToDpServiceInfoProfile(serviceInfoProfile, dpServiceInfoProfile); + // int32_t ret = DistributedDeviceProfileClient::GetInstance().PutServiceInfoProfile(dpServi ceInfoProfile); + // LOGI("SQW put dp ret=%{public}d, service name=%{puublic}s,service id=%{public}ld, deviceid=%{public}s, " + // "tokenid=%{public}s, userid=%(public)d", + // reet, dpServiceInfoProfile.GetServiceName().c_str(), + // dpsServiceInfoProfile.GetServiceId() dpserviceInfoProfile.GetDeviceId().c_str(), + // dpServiceInfoProfile.GetTokenId().c_str() dpServiceInfoProfile.GetUserId()); + // if (ret != DM_OK) { + // LOGE("failed: %(public}d", ret); + // return ret; + // } + return DM_OK; +} + +int32_t DeviceProfileConnector::UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) +{ + // DistributedDeviceProfile::ServiceInfoProfile dpserviceinfoprofile; + // TransferDmToDpServiceInfoProfile(dmServiceInfoProtfile, dpServiceInfoProfile); + // int32_t ret = DistributedDeviceProfileClient::GetInstance().UpdateServiceInfoProfile(dpse rviceInfoProfile); + // if (ret != DM_OK) { + // LOGE("failed: %(public}d", ret); + // return ret; + // } + return DM_OK; +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const ServiceInfoUniqueKey& 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(dpke y); + // if (ret != DM_OK) { + // LOGE("failed: %{public}d", ret); + // return ret; + // } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileByUniqueKey( + const ServiceInfoUniqueKey& key, ServiceInfoProfile& serviceInfoProfile) +{ + // DistributedDeviceProfile::ServiceInfoUniqueKey dpKeey; + // dpkey.SetDeviceId(key.deviceId); + // dpKey.SetServiceId(key.serviceId); + // dpKey.SetTokenid(key.tokenid); + // dpkey.SetUserId(key.userId); + // ServiceInfoProfile dpServiceInfoProfile; + // int32_t ret = + // DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByUniqueKey(dpkey, dpserviceinfoProfile); + // if (ret != DM_OK) { + // LOGE("failed: %{public}d", ret); + // return ret; + // } + // TransferDpToDmServiceInfoProfile(dpServiceInfoProfile, serviceInfoProfile); + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index da0db3936..92358c679 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,8 @@ 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 StartPublishService(const ServiceFilter& serviceFilter, const DmDiscoverMode& discoverMode) = 0; + virtual int32_t StopPublishService(const ServiceFilter& serviceFilter) = 0; }; } // namespace DistributedHardware } // namespace OHOS 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..87b0a5af9 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,10 @@ public: virtual bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; + virtual int32_t StartPublishService( + const ServiceFilter& serviceFilter, const DmDiscoverMode& discoverMode) override; + virtual int32_t StopPublishService(const ServiceFilter& serviceFilter) 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..d360e0301 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -292,6 +292,7 @@ typedef struct PeerTargetId { * wlan ip port. */ uint16_t wifiPort = 0; + int64_t serviceId = 0; bool operator==(const PeerTargetId &other) const { @@ -469,6 +470,59 @@ typedef struct DMAclQuadInfo { std::string peerUdid; int32_t peerUserId; } DMAclQuadInfo; + +typedef struct ServiceInfo { + int64_t serviceId; // Unique service ID (64 bytes) m + std::string serviceType; // 15 字节 + std::string serviceName; // 9字节,应用对该服务起 + std::string serviceDisplayName; // 暂定7~128宁节, + std::string customData; // 0-512字节,业务自定义字 + uint32_t dataLen; // 自定义宁段长度 + std::string bundleName; // 业务包名(FA必填) + std::string moduleName; // 一个包名对应多个module + std::string abilityName; // 一个module对应多个能力(F +} ServiceInfo; + +typedef struct ServiceRegInfo { + ServiceInfo serviceInfo; + std::string authorizationDescription; // authorization服务sink端瑞弹框显示的信息(不能同步) + std::string extraInfo; // 可扩展字段 +} ServiceRegInfo; + +typedef struct ServiceFilter { + int64_t serviceId; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; +} ServiceFilter; + +typedef struct ServiceInfoUniqueKey { + std::string deviceId; + int32_t userId; + std::string tokenId; + int64_t serviceId; +} ServiceInfoUniqueKey; + +typedef struct ServiceInfoProfile { + ServiceRegInfo serviceRegInfo; + std::string deviceId; + int32_t userId; + std::string networkid; + std::string tokenId; + std::string extraInfo; +} ServiceInfoProfile; + +typedef struct ServiceBindlocalInfo { + std::string localUdid; + int32_t userId; + uint32_t tokenId; +} ServiceBindLocalInfo; + +typedef struct DmServiceAddress { + std::string bleMac; + uint32_t actionid; +} DmServiceAddress; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file 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..594ed80be 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -14,7 +14,10 @@ */ #include "device_manager_impl.h" + #include + +#include "device_manager_ipc_interface_code.h" #include "device_manager_notify.h" #include "dm_anonymous.h" #include "dm_constants.h" @@ -63,11 +66,14 @@ #include "ipc_set_remote_device_name_req.h" #include "ipc_set_useroperation_req.h" #include "ipc_skeleton.h" +#include "ipc_start_publish_service_req.h" +#include "ipc_stop_publish_service_req.h" #include "ipc_sync_callback_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_unbind_device_req.h" #include "ipc_unpublish_req.h" #include "securec.h" + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "ipc_model_codec.h" #include "iservice_registry.h" @@ -2994,5 +3000,50 @@ bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const Dm anonyLocalUdid_); return result; } + +int32_t DeviceManagerImpl::StartPublishService(const ServiceFilter& serviceFilter, const DmDiscoverMode& discoverMode) +{ + int64_t serviceId = serviceFilter.serviceId; + LOGI("StartPublishService Start"); + LOGE("serviceId:%{public}lld, discoverMode:%{public}u", serviceId, discoverMode); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceId(serviceId); + req->SetDiscoverMode(discoverMode); + int32_t ret = ipcClientProxy_->SendRequest(START_PUBLISH_SERVICE, req, rsp); + if (ret != DM_OK) { + LOGE("error: Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("error: Failed with ret %{public}d", ret); + return ret; + } + LOGI("StartPublishService End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::StopPublishService(const ServiceFilter& serviceFilter) +{ + int64_t serviceId = serviceFilter.serviceId; + LOGI("StopPublishService Start"); + LOGE("serviceId:%{public}lld", serviceId); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceId(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(STOP_PUBLISH_SERVICE, req, rsp); + if (ret != DM_OK) { + LOGE("error:Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("error: Failed with ret %{public}d", ret); + return ret; + } + LOGI("StopPublishService End"); + 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 a30e5493b..928c91e40 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 @@ -28,8 +28,8 @@ #include "ipc_cmd_register.h" #include "ipc_common_param_req.h" #include "ipc_create_pin_holder_req.h" -#include "ipc_destroy_pin_holder_req.h" #include "ipc_def.h" +#include "ipc_destroy_pin_holder_req.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" #include "ipc_get_anony_local_udid_rsp.h" @@ -41,8 +41,8 @@ #include "ipc_get_device_screen_status_req.h" #include "ipc_get_device_screen_status_rsp.h" #include "ipc_get_encrypted_uuid_req.h" -#include "ipc_get_info_by_network_rsp.h" #include "ipc_get_info_by_network_req.h" +#include "ipc_get_info_by_network_rsp.h" #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_local_device_name_rsp.h" #include "ipc_get_local_display_device_name_req.h" @@ -53,6 +53,8 @@ #include "ipc_import_auth_code_req.h" #include "ipc_model_codec.h" #include "ipc_notify_event_req.h" +#include "ipc_permission_req.h" +#include "ipc_publish_req.h" #include "ipc_put_device_profile_info_list_req.h" #include "ipc_register_listener_req.h" #include "ipc_register_serviceinfo_req.h" @@ -63,13 +65,15 @@ #include "ipc_set_local_device_name_req.h" #include "ipc_set_remote_device_name_req.h" #include "ipc_set_useroperation_req.h" +#include "ipc_start_publish_service_req.h" +#include "ipc_stop_publish_service_req.h" #include "ipc_sync_callback_req.h" -#include "ipc_permission_req.h" -#include "ipc_publish_req.h" +#include "ipc_unauthenticate_device_req.h" #include "ipc_unbind_device_req.h" #include "ipc_unpublish_req.h" -#include "ipc_unauthenticate_device_req.h" #include "securec.h" +#include "ipc_start_publish_service_req.h" +#include "ipc_stop_publish_service_req.h" namespace OHOS { class IRemoteObject; } namespace OHOS { @@ -2217,5 +2221,62 @@ ON_IPC_READ_RESPONSE(CHECK_SINK_SAME_ACCOUNT, MessageParcel &reply, std::shared_ { return ReadResponse(CHECK_SINK_SAME_ACCOUNT, reply, pBaseRsp); } + +ON_IPC_SET_REQUEST(START_PUBLISH_SERVICE, std::shared_ptr pBaseReq, MessageParcel& data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetServiceId(); + if (!data.WriteInt64(serviceId)) { + LOGE("write GetServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + DmDiscoverMode discoverMode = pReq->GetDiscoverMode(); + if (!data.WriteInt32(static_cast(discoverMode))) { + LOGE("write GetDiscoverMode failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(START_PUBLISH_SERVICE, 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); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(STOP_PUBLISH_SERVICE, std::shared_ptr pBaseReq, MessageParcel& data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetServiceId(); + if (!data.WriteInt64(serviceId)) { + LOGE("write GetServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(STOP_PUBLISH_SERVICE, 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); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index d8c4ef707..8af615038 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -121,6 +121,7 @@ if (defined(ohos_lite)) { "include/credential", "include/cryptomgr", "include/devicestate", + "include/publish", "include/dependency/commonevent", "include/dependency/deviceprofile", "include/dependency/multipleuser", @@ -211,6 +212,7 @@ if (defined(ohos_lite)) { "src/device_manager_service_impl.cpp", "src/devicestate/dm_device_state_manager.cpp", "src/i18n/dm_language_manager.cpp", + "src/publish/dm_service_publish_manager.cpp", ] public_configs = [ ":devicemanagerserviceimpl_config" ] diff --git a/services/implementation/include/dependency/softbus/softbus_connector.h b/services/implementation/include/dependency/softbus/softbus_connector.h index be6e0468b..9213abc63 100644 --- a/services/implementation/include/dependency/softbus/softbus_connector.h +++ b/services/implementation/include/dependency/softbus/softbus_connector.h @@ -106,7 +106,6 @@ public: * @tc.type: FUNC */ int32_t UnRegisterConnectorCallback(); - public: SoftbusConnector(); ~SoftbusConnector(); @@ -146,6 +145,8 @@ public: int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string &aclList); + int32_t PublishService(const ServiceInfoProfile& serviceInfoProfile, const DmDiscoverMode& discoverMode); + int32_t UnPublishService(int64_t serviceId); private: static std::shared_ptr SetAddrAndJson(const ConnectionAddr *addr, JsonObject &jsonPara, std::string &connectAddr); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 9efda6ff9..e76727ac2 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -25,6 +25,7 @@ #include "access_control_profile.h" #include "auth_manager.h" +#include "deviceprofile_connector.h" #include "dm_ability_manager.h" #include "dm_auth_manager.h" #include "dm_auth_manager_base.h" @@ -32,13 +33,13 @@ #include "dm_credential_manager.h" #include "dm_device_info.h" #include "dm_device_state_manager.h" +#include "dm_service_publish_manager.h" #include "dm_single_instance.h" #include "dp_inited_callback.h" #include "idevice_manager_service_impl.h" #include "ipc_skeleton.h" #include "mine_hichain_connector.h" #include "softbus_connector.h" -#include "deviceprofile_connector.h" namespace OHOS { namespace DistributedHardware { @@ -205,6 +206,9 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); + int32_t CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key); + int32_t PublishServiceInfoByServiceId(const ServiceInfoUniqueKey& key, const DmDiscoverMode& discoverNode); + int32_t UnPublishServiceInfoByServiceId(int64_t serviceId); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); @@ -332,6 +336,8 @@ private: std::map> configsMap_; // Import when authMgr is not initialized std::mutex authMgrMapMtx_; std::map> authMgrMap_; // New protocol sharing + + std::shared_ptr servicePublishMgr_; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 8ec4d49a2..118b6e1f5 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -182,6 +182,10 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); + + virtual int32_t CheckServiceInfoByServiceId(ServiceInfoUniqueKey& key) override; + virtual int32_t PublishServiceInfoByserviceId(ServiceInfoUniqueKey& key, DmDiscoverMode& discoverMode) override; + virtual int32_t UnPublishServiceInfoByServiceId(int64_t serviceId) override; private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/publish/dm_service_publish_manager.h b/services/implementation/include/publish/dm_service_publish_manager.h new file mode 100644 index 000000000..ca7c1c188 --- /dev/null +++ b/services/implementation/include/publish/dm_service_publish_manager.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_SERVICE_PUBLISH_MANAGER_H +#define OHOS_DM_SERVICE_PUBLISH_MANAGER_H + +#include "dm_timer.h" +#include "softbus_connector.h" + +namespace OHOS { +namespace DistributedHardware { +class DmServicePublishManager final : public std::enable_shared_from_this { +public: + DmServicePublishManager(std::shared_ptr softbusConnector); + ~DmServicePublishManager(); + int32_t PublishService(const ServiceInfoProfile& serviceInfoProfile, const DmDiscoverMode& discoverMode); + int32_t UnPublishService(int64_t serviceId); + void HandlePublishTimeout(const std::string& name); + +private: + void ServicePublishTimer(int64_t serviceid); + int32_t CheckPublishMap(int64_t serviceId); + +private: + std::shared_ptr softbusConnector_; + std::map servicePublishMap_; + std::shared_ptr timer_; + std::mutex locks_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_SERVICE_PUBLISH_MANAGER_H \ No newline at end of file diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index db5974f1b..258c1f3da 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -896,5 +896,36 @@ void SoftbusConnector::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeB dmDeviceInfo.extraData = ToString(extraJson); } } + +int32_t SoftbusConnector::PublishService( + const ServiceInfoProfile& serviceInfoProfile, const DmDiscoverMode& discoverMode) +{ + const ServiceInfo& serviceInfo = serviceInfoProfile.serviceRegInfo.serviceInfo; + std::string serviceId = std::to_string(serviceInfo.serviceId); + // ServiceDiscoveryParam publishInfo; + // ServiceDiscoverMode mode = + // (discoverMode == DM_DISCOVER_MODE_PASSIVE) ? SERVICE_DISCOVER_MODE_PASSIVE : SERVICE_DISCOVER_MODE_ACTIVE; + // // publishInfo.media = SERVICE_MEDIUM_TYPE_AUTO; + // publishInfo.mode = mode; + // // publishInfo.freq = FREQ_EXTREMELY_HIGH; + + // int32_t ret = ::PublishService(serviceId.c_str(), &publishInfo); + // if (ret != DM_OK) { + // LOGE("[SOFTBUS]Publishservice failed, ret %{public}d.", ret); + // return ret; + // } + return DM_OK; +} + +int32_t SoftbusConnector::UnPublishService(int64_t serviceId) +{ + std::string serviceIdStr = std::to_string(serviceId); + // int32_t ret = ::UnPublishService(serviceIdStr.c_str()); + // if (ret != DM_OK) { + // LOGE("[SOFTBUS]Unpublishservice failed, ret %{public}d.", ret); + // return ret; + // } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 3b29c520a..cda2bc4f8 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -486,6 +486,9 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr(new DpInitedCallback()); DeviceProfileConnector::GetInstance().SubscribeDeviceProfileInited(dpInitedCallback_); } + if (servicePublishMgr_ == nullptr) { + servicePublishMgr_ = std::make_shared(softbusConnector_); + } listener_ = listener; CreateGlobalClassicalAuthMgr(); if (authMgr_ != nullptr) { @@ -3080,6 +3083,40 @@ void DeviceManagerServiceImpl::DeleteSessionKey(int32_t userId, DeviceProfileConnector::GetInstance().DeleteSessionKey(userId, skId); } +int32_t DeviceManagerServiceImpl::CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key) +{ + ServiceInfoProfile serviceInfo; + return DeviceProfileConnector::GetInstance().GetServiceInfoByServiceId(key, serviceInfo); +} + +int32_t DeviceManagerServiceImpl::PublishServiceInfoByServiceId( + const ServiceInfoUniqueKey& key, const DmDiscoverMode& discoverMode) +{ + if (softbusConnector_ == nullptr || servicePublishMgr_ == nullptr) { + LOGE("softbusConnector_ is nullpter!"); + return ERR_DM_PUBLISH_FAILED; + } + // 获取要发布的服务信息 + ServiceInfoProfile serviceInfo; + auto ret = DeviceProfileConnector::GetInstance().GetServiceInfoByServiceId(key, serviceInfo); + if (ret != 0) { + LOGE("GetserviceInfoByserviceId error, falled ret: %{public}d", ret); + return ret; + } + // 调用发布管理发布服务 + return servicePublishMgr_->PublishService(serviceInfo, discoverMode); +} + +int32_t DeviceManagerServiceImpl::UnPublishServiceInfoByServiceId(int64_t serviceId) +{ + if (softbusConnector_ == nullptr || servicePublishMgr_ == nullptr) { + LOGE("softbusConnector_ is nullpter!"); + return ERR_DM_PUBLISH_FAILED; + } + // 调用发布管理发布服务 + return servicePublishMgr_->UnPublishService(serviceId); +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 445f2b070..17b2ffdb8 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -740,6 +740,24 @@ void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, (void)backGroundUserIds; return; } +int32_t DeviceManagerServiceImpl::CheckServiceInfoByServiceId(ServiceInfoUniqueKey& key) +{ + (void)key; + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::PublishServiceInfoByserviceId(ServiceInfoUniqueKey& key, DmDiscoverMode& discoverMode) +{ + (void)key; + (void)discoverMode; + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::UnPublishServiceInfoByServiceId(int64_t serviceId) +{ + (void)serviceId; + return DM_OK; +} extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { diff --git a/services/implementation/src/publish/dm_service_publish_manager.cpp b/services/implementation/src/publish/dm_service_publish_manager.cpp new file mode 100644 index 000000000..26ddbcb24 --- /dev/null +++ b/services/implementation/src/publish/dm_service_publish_manager.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dm_service_publish_manager.h" + +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_log.h" +namespace OHOS { +namespace DistributedHardware { +constexpr const char* PUBLISH_SERVICE_TIMEOUT_TASK = "deviceManagerTimer:publishService"; +const int32_t PUBLISH_SERVICE_TIMEOUT = 120; + +DmServicePublishManager::DmServicePublishManager(std::shared_ptr softbusConnector) + : softbusConnector_(softbusConnector) +{ + LOGI("DmServicePublishManager constructor"); +} + +DmServicePublishManager::~DmServicePublishManager() +{ + LOGI("DmServicePublishManager destructor"); +} + +void DmServicePublishManager::ServicePublishTimer(int64_t serviceId) +{ + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + timer_->StartTimer(std::string(PUBLISH_SERVICE_TIMEOUT_TASK) + std::to_string(serviceId), PUBLISH_SERVICE_TIMEOUT, + [this](std::string name) { HandlePublishTimeout(name); }); +} + +int32_t DmServicePublishManager::CheckPublishMap(int64_t serviceId) +{ + if (servicePublishMap_.empty()) { + return DM_OK; + } + auto item = servicePublishMap_.find(serviceId); + if (item == servicePublishMap_.end()) { + LOGE("DmServicePublishManager: serviceId:%{public}lld PublishService repeated", serviceId); + return ERR_DM_PUBLISH_REPEATED; + } + return DM_OK; +} + +int32_t DmServicePublishManager::PublishService( + const ServiceInfoProfile& serviceInfoProfile, const DmDiscoverMode& discoverMode) +{ + const ServiceInfo& serviceInfo = serviceInfoProfile.serviceRegInfo.serviceInfo; + int32_t ret = CheckPublishMap(serviceInfo.serviceId); + if (ret != DM_OK) { + return ret; + } + std::lock_guard autolock(locks_); + servicePublishMap_.emplace(serviceInfo.serviceId, serviceInfoProfile); + ServicePublishTimer(serviceInfo.serviceId); + return softbusConnector_->PublishService(serviceInfoProfile, discoverMode); +} + +int32_t DmServicePublishManager::UnPublishService(int64_t serviceId) +{ + std::lock_guard autolock(locks_); + auto item = servicePublishMap_.find(serviceId); + if (item == servicePublishMap_.end()) { + LOGE("DmServicePublishManager:serviceId:%{public}lld UnpublishService", serviceId); + return DM_OK; + } + if (timer_ != nullptr) { + timer_->DeleteTimer(std::string(PUBLISH_SERVICE_TIMEOUT_TASK) + std::to_string(serviceId)); + servicePublishMap_.erase(serviceId); + } + return softbusConnector_->UnPublishService(serviceId); +} + +void DmServicePublishManager::HandlePublishTimeout(const std::string& name) +{ + LOGI("DmServicePublishManager::HandlePublishTimeout"); + std::string serviceIdStr = name.substr(strlen(PUBLISH_SERVICE_TIMEOUT_TASK)); + int64_t serviceId; + sscanf(serviceIdStr.c_str(), "%llx", &serviceId); + UnPublishService(serviceId); +} + +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 069a2ad13..567c2e0f4 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -29,6 +29,8 @@ #include "pin_holder.h" #include "device_manager_service_listener.h" #include "idevice_manager_service_impl.h" +#include "dm_device_info.h" +#include "dm_subscribe_info.h" #include "hichain_listener.h" #include "i_dm_check_api_white_list.h" #include "i_dm_service_impl_ext_resident.h" @@ -49,6 +51,7 @@ namespace OHOS { namespace DistributedHardware { class DeviceManagerService { DM_DECLARE_SINGLE_INSTANCE_BASE(DeviceManagerService); + public: DeviceManagerService() {} @@ -275,6 +278,9 @@ public: bool CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee); bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); + int32_t StartPublishService(const ServiceFilter& serviceFilter, const DmDiscoverMode& discoverMode); + int32_t StopPublishService(const ServiceFilter& serviceFilter); + private: bool IsDMServiceImplReady(); bool IsDMImplSoLoaded(); diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 949542bed..cd899dc96 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -286,6 +286,11 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid) = 0; virtual void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) = 0; + + virtual int32_t CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key) = 0; + virtual int32_t PublishServiceInfoByServiceId( + const ServiceInfoUniqueKey& key, const DmDiscoverMode& discoverMode) = 0; + virtual int32_t UnPublishServiceInfoByServiceId(int64_t serviceId) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index f68075c02..6da78fabd 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4638,5 +4638,89 @@ void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &c MultipleUserConnector::GetCurrentDMAccountInfo()); } #endif + +int32_t DeviceManagerService::StartPublishService( + const ServiceFilter& serviceFilter, const DmDiscoverMode& discoverMode) +{ + LOGI("StartPublishService Begin."); + LOGE("serviceId:%{public}" PRId64 ", discoverMode:%{public}d", serviceFilter.serviceId, discoverMode); + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller does not have permission to call StantPublishService."); + return ERR_DM_NO_PERMISSION; + } + // 查询user ID + int32_t userId = -1; + MultipleUserConnector::GetCallerUserId(userId); + // 查询前台userID + std::vector userVec; + int32_t ret = MultipleUserConnector::GetForegroundUserIds(userVec); + if (ret != DM_OK || userVec.size() == 0) { + LOGE("get userId error ret: %{public}d.", ret); + return ERR_DM_PUBLISH_FAILED; + } + // 校验是否允许发布 + bool isAllowe = false; + for (auto id : userVec) { + if (userId == id) { + isAllowe = true; + break; + } + } + isAllowe = isAllowe || (userId == 0); + if (!isAllowe) { + return ERR_DM_PUBLISH_FAILED; + } + if (!IsDMServiceImplReady()) { + LOGE("StartPublishService failed, instance not init oninit failed."); + return ERR_DM_INIT_FAILED; + } + // 获取服务信息 + DmDeviceInfo deviceInfo; + GetLocalDeviceInfo(deviceInfo); + ServiceInfoUniqueKey key; + key.deviceId = deviceInfo.deviceId; + key.userId = userId; + key.tokenId = std::to_string(IPCSkeleton::GetCallingTokenID()); + key.serviceId = serviceFilter.serviceId; + LOGE("serviceId:%{public}lld, userId:%{public}d", key.serviceId, key.userId); + LOGE("deviceId:%{public}s", key.deviceId.c_str()); + LOGE("tokenId:%{public}s", key.tokenId.c_str()); + // TODO:没有实现 + // 检查服务是否已注册 + ret = dmServiceImpl_->CheckServiceInfoByServiceId(key); + if (ret != 0) { + LOGE("CheckServiceInfoByServiceId error, failed ret: %{public}d", ret); + return ret; + } + // 发布服务 + ret = dmServiceImpl_->PublishServiceInfoByServiceId(key, discoverMode); + if (ret != 0) { + LOGE("PublishService error, failed ret: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerService::StopPublishService(const ServiceFilter& serviceFilter) +{ + LOGI("StopPublishService Begin."); + LOGE("serviceId:%{public}lld", serviceFilter.serviceId); + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller does not have permission to call1 StopPublishService."); + return ERR_DM_NO_PERMISSION; + } + // TODO:检查接口是否存在?? 尚未实现 + if (!IsDMServiceImplReady()) { + LOGE("StopPublishService failed, instance not init orinit failed."); + return ERR_DM_INIT_FAILED; + } + // 停止发布服务 + auto ret = dmServiceImpl_->UnPublishServiceInfoByServiceId(serviceFilter.serviceId); + if (ret != 0) { + LOGE("PublishService error, failed ret: %{public}d", ret); + return ret; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index e8f908bca..448f4f7cc 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -132,6 +132,28 @@ void CancelXcollieTimer(int32_t id) OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id); } +bool EncodeDmServiceInfo(const ServiceInfo& serviceInfo, MessageParcel& parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(serviceInfo.serviceId)); + std::string serviceTypeStr(serviceInfo.serviceType); + bRet = (bRet && parcel.WriteString(serviceTypeStr)); + std::string serviceNameStr(serviceInfo.serviceName); + bRet = (bRet && parcel.WriteString(serviceNameStr)); + std::string serviceDisplayNameStr(serviceInfo.serviceDisplayName); + bRet = (bRet && parcel.WriteString(serviceDisplayNameStr)); + std::string customDataStr(serviceInfo.customData); + bRet = (bRet && parcel.WriteString(customDataStr)); + bRet = (bRet && parcel.WriteUint32(serviceInfo.dataLen)); + std::string bundleNameStr(serviceInfo.bundleName); + bRet = (bRet && parcel.WriteString(bundleNameStr)); + std::string moduleNameStr(serviceInfo.moduleName); + bRet = (bRet && parcel.WriteString(moduleNameStr)); + std::string abilityNamestr(serviceInfo.abilityName); + bRet = (bRet && parcel.WriteString(abilityNamestr)); + return bRet; +} + bool EncodeDmDeviceInfo(const DmDeviceInfo &devInfo, MessageParcel &parcel) { bool bRet = true; @@ -171,6 +193,7 @@ bool EncodePeerTargetId(const PeerTargetId &targetId, MessageParcel &parcel) bRet = (bRet && parcel.WriteString(targetId.bleMac)); bRet = (bRet && parcel.WriteString(targetId.wifiIp)); bRet = (bRet && parcel.WriteUint16(targetId.wifiPort)); + bRet = (bRet && parcel.WriteInt64(targetId.serviceId)); return bRet; } @@ -181,6 +204,7 @@ void DecodePeerTargetId(MessageParcel &parcel, PeerTargetId &targetId) targetId.bleMac = parcel.ReadString(); targetId.wifiIp = parcel.ReadString(); targetId.wifiPort = parcel.ReadUint16(); + targetId.serviceId = parcel.ReadInt64(); } ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, MessageParcel &data) @@ -1988,6 +2012,33 @@ ON_IPC_CMD(GET_LOCAL_DEVICE_NAME_OLD, MessageParcel &data, MessageParcel &reply) return DM_OK; } +ON_IPC_CMD(START_PUBLISH_SERVICE, MessageParcel& data, MessageParcel& reply) +{ + int64_t serviceId = data.ReadInt64(); + DmDiscoverMode discoverMode = static_cast(data.ReadInt32()); + ServiceFilter serviceFilter; + serviceFilter.serviceId = serviceId; + int32_t result = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + if (!reply.WriteInt32(result)) { + LOGE("Write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(STOP_PUBLISH_SERVICE, MessageParcel& data, MessageParcel& reply) +{ + int64_t serviceId = data.ReadInt64(); + ServiceFilter serviceFilter; + serviceFilter.serviceId = serviceId; + int32_t result = DeviceManagerService::GetInstance().StopPublishService(serviceFilter); + if (!reply.WriteInt32(result)) { + LOGE("Write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + ON_IPC_CMD(CHECK_SRC_ACCESS_CONTROL, MessageParcel &data, MessageParcel &reply) { return OnIpcCmd(CHECK_SRC_ACCESS_CONTROL, data, reply); diff --git a/test/softbusunittest/UTTest_softbus_connector.cpp b/test/softbusunittest/UTTest_softbus_connector.cpp index 7d97ecee6..33acf716e 100644 --- a/test/softbusunittest/UTTest_softbus_connector.cpp +++ b/test/softbusunittest/UTTest_softbus_connector.cpp @@ -845,6 +845,26 @@ HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_002, testing::ext::TestSi bool isForceJoin = false; softbusConnector->JoinLnn(deviceId, isForceJoin); } + +HWTEST_F(SoftbusConnectorTest, PublishService_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfile serviceInfoProfile; + serviceInfoProfile.serviceRegInfo.serviceInfo.serviceId = 12345; + serviceInfoProfile.serviceRegInfo.serviceInfo.serviceName = "TestService"; + + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + std::shared_ptr softbusConnector = std::make_shared(); + int32_t ret = softbusConnector->PublishService(serviceInfoProfile, discoverMode); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(SoftbusConnectorTest, UnPublishService_001, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + std::shared_ptr softbusConnector = std::make_shared(); + int32_t ret = softbusConnector->UnPublishService(serviceId); + EXPECT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS- \ No newline at end of file diff --git a/test/unittest/UTTest_device_manager_impl_three.cpp b/test/unittest/UTTest_device_manager_impl_three.cpp index 4fec18209..e31ec6a4b 100644 --- a/test/unittest/UTTest_device_manager_impl_three.cpp +++ b/test/unittest/UTTest_device_manager_impl_three.cpp @@ -1436,6 +1436,93 @@ HWTEST_F(DeviceManagerImplTest, ExportAuthCode_301, testing::ext::TestSize.Level int32_t ret = DeviceManager::GetInstance().ExportAuthCode(authCode); ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); } + +HWTEST_F(DeviceManagerImplTest, StartPublishService_001, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(START_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Return(ERR_DM_IPC_SEND_REQUEST_FAILED)); + + int32_t ret = DeviceManager::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, StartPublishService_002, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(START_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Invoke([](int32_t, const std::shared_ptr&, const std::shared_ptr& rsp) { + rsp->SetErrCode(ERR_DM_IPC_SEND_REQUEST_FAILED); + return DM_OK; + })); + + int32_t ret = DeviceManager::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, StartPublishService_003, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(START_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Invoke([](int32_t, const std::shared_ptr&, const std::shared_ptr& rsp) { + rsp->SetErrCode(DM_OK); + return DM_OK; + })); + + int32_t ret = DeviceManager::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerImplTest, StopPublishService_001, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(STOP_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Return(ERR_DM_IPC_SEND_REQUEST_FAILED)); + + int32_t ret = DeviceManager::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, StopPublishService_002, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(STOP_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Invoke([](int32_t, const std::shared_ptr&, const std::shared_ptr& rsp) { + rsp->SetErrCode(ERR_DM_IPC_SEND_REQUEST_FAILED); + return DM_OK; + })); + + int32_t ret = DeviceManager::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} + +HWTEST_F(DeviceManagerImplTest, StopPublishService_003, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(STOP_PUBLISH_SERVICE, testing::_, testing::_)) + .WillOnce(testing::Invoke([](int32_t, const std::shared_ptr&, const std::shared_ptr& rsp) { + rsp->SetErrCode(DM_OK); + return DM_OK; + })); + + int32_t ret = DeviceManager::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_impl_first.cpp b/test/unittest/UTTest_device_manager_service_impl_first.cpp index d778f7436..92942878d 100644 --- a/test/unittest/UTTest_device_manager_service_impl_first.cpp +++ b/test/unittest/UTTest_device_manager_service_impl_first.cpp @@ -822,6 +822,144 @@ HWTEST_F(DeviceManagerServiceImplFirstTest, DeleteAclByTokenId_002, testing::ext EXPECT_TRUE(delACLInfoVec.empty()); EXPECT_TRUE(userIdVec.empty()); } + +HWTEST_F(DeviceManagerServiceImplFirstTest, CheckServiceInfoByServiceId_001, testing::ext::TestSize.Level1) +{ + ServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + key.serviceId = 12345; + + ServiceInfoProfile serviceInfo; + + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoByServiceId(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }), testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<1>(serviceInfo), testing::Return(DM_OK))); + int32_t ret = deviceManagerServiceImpl_->CheckServiceInfoByServiceId(key); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, CheckServiceInfoByServiceId_002, testing::ext::TestSize.Level1) +{ + ServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + key.serviceId = 12345; + + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoByServiceId(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }), testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + int32_t ret = deviceManagerServiceImpl_->CheckServiceInfoByServiceId(key); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, PublishServiceInfoByServiceId_001, testing::ext::TestSize.Level1) +{ + deviceManagerServiceImpl_->softbusConnector_ = nullptr; + + ServiceInfoUniqueKey key; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + int32_t ret = deviceManagerServiceImpl_->PublishServiceInfoByServiceId(key, discoverMode); + EXPECT_EQ(ret, ERR_DM_PUBLISH_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, PublishServiceInfoByServiceId_002, testing::ext::TestSize.Level1) +{ + ServiceInfoUniqueKey key; + key.deviceId = "testDeviceId"; + key.userId = 1; + key.tokenId = "testTokenId"; + key.serviceId = 12345; + + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoByServiceId(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }), testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + int32_t ret = deviceManagerServiceImpl_->PublishServiceInfoByServiceId(key, discoverMode); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, PublishServiceInfoByServiceId_003, testing::ext::TestSize.Level1) +{ + ServiceInfoUniqueKey key; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + ServiceInfoProfile serviceInfo; + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoByServiceId(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }), testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<1>(serviceInfo), testing::Return(DM_OK))); + + EXPECT_CALL(*softbusConnectorMock_, + PublishService(testing::Truly([&serviceInfo](const ServiceInfoProfile& actualServiceInfo) { + return actualServiceInfo.serviceRegInfo.serviceInfo.serviceId == + serviceInfo.serviceRegInfo.serviceInfo.serviceId; + }), testing::_)) + .WillOnce(testing::Return(ERR_DM_FAILED)); + + int32_t ret = deviceManagerServiceImpl_->PublishServiceInfoByServiceId(key, discoverMode); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, PublishServiceInfoByServiceId_004, testing::ext::TestSize.Level1) +{ + ServiceInfoUniqueKey key; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + ServiceInfoProfile serviceInfo; + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoByServiceId(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }), testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<1>(serviceInfo), testing::Return(DM_OK))); + + EXPECT_CALL(*softbusConnectorMock_, + PublishService(testing::Truly([&serviceInfo](const ServiceInfoProfile& actualServiceInfo) { + return actualServiceInfo.serviceRegInfo.serviceInfo.serviceId == + serviceInfo.serviceRegInfo.serviceInfo.serviceId; + }), testing::_)) + .WillOnce(testing::Return(DM_OK)); + + int32_t ret = deviceManagerServiceImpl_->PublishServiceInfoByServiceId(key, discoverMode); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, UnPublishServiceInfoByServiceId_001, testing::ext::TestSize.Level1) +{ + deviceManagerServiceImpl_->softbusConnector_ = nullptr; + int64_t serviceId = 12345; + + int32_t ret = deviceManagerServiceImpl_->UnPublishServiceInfoByServiceId(serviceId); + EXPECT_EQ(ret, ERR_DM_PUBLISH_FAILED); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_three.cpp b/test/unittest/UTTest_device_manager_service_three.cpp index 5ca56ab14..ac06712f4 100644 --- a/test/unittest/UTTest_device_manager_service_three.cpp +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -64,6 +64,7 @@ void DeviceManagerServiceThreeTest::TearDown() Mock::VerifyAndClearExpectations(permissionManagerMock_.get()); Mock::VerifyAndClearExpectations(softbusListenerMock_.get()); Mock::VerifyAndClearExpectations(deviceManagerServiceImplMock_.get()); + Mock::VerifyAndClearExpectations(multipleUserConnectorMock_.get()); } void DeviceManagerServiceThreeTest::SetUpTestCase() @@ -72,6 +73,7 @@ void DeviceManagerServiceThreeTest::SetUpTestCase() DmPermissionManager::dmPermissionManager = permissionManagerMock_; DmSoftbusListener::dmSoftbusListener = softbusListenerMock_; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = deviceManagerServiceImplMock_; + DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnectorMock_; } void DeviceManagerServiceThreeTest::TearDownTestCase() @@ -84,6 +86,8 @@ void DeviceManagerServiceThreeTest::TearDownTestCase() softbusListenerMock_ = nullptr; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = nullptr; deviceManagerServiceImplMock_ = nullptr; + DmMultipleUserConnector::dmMultipleUserConnector = nullptr; + multipleUserConnectorMock_ = nullptr; } namespace { @@ -909,6 +913,167 @@ HWTEST_F(DeviceManagerServiceThreeTest, UnloadDMDeviceRiskDetect_301, testing::e DeviceManagerService::GetInstance().UnloadDMDeviceRiskDetect(); EXPECT_EQ(DeviceManagerService::GetInstance().dmDeviceRiskDetect_, nullptr); } + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_001, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_002, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)).WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_PUBLISH_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_003, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)) + .WillOnce(DoAll(SetArgReferee<0>(std::vector{2, 3}), Return(DM_OK))); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_PUBLISH_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_004, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)) + .WillOnce(DoAll(SetArgReferee<0>(std::vector{1}), Return(DM_OK))); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_INIT_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_005, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)) + .WillOnce(DoAll(SetArgReferee<0>(std::vector{1}), Return(DM_OK))); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceImplMock_, CheckServiceInfoByServiceId(testing::_)).WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_006, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)) + .WillOnce(DoAll(SetArgReferee<0>(std::vector{1}), Return(DM_OK))); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceImplMock_, CheckServiceInfoByServiceId(testing::_)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceImplMock_, PublishServiceInfoByServiceId(testing::_, testing::_)).WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StartPublishService_007, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + DmDiscoverMode discoverMode = DM_DISCOVER_MODE_ACTIVE; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(testing::_)) + .WillOnce(DoAll(SetArgReferee<0>(std::vector{1}), Return(DM_OK))); + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(testing::_)).WillOnce(SetArgReferee<0>(1)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceImplMock_, CheckServiceInfoByServiceId(testing::_)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceImplMock_, PublishServiceInfoByServiceId(testing::_, testing::_)).WillOnce(Return(DM_OK)); + + int32_t ret = DeviceManagerService::GetInstance().StartPublishService(serviceFilter, discoverMode); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopPublishService_001, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopPublishService_002, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, ERR_DM_INIT_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopPublishService_003, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceImplMock_, UnPublishServiceInfoByServiceId(serviceFilter.serviceId)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopPublishService_004, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + serviceFilter.serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceImplMock_, UnPublishServiceInfoByServiceId(serviceFilter.serviceId)) + .WillOnce(Return(DM_OK)); + + int32_t ret = DeviceManagerService::GetInstance().StopPublishService(serviceFilter); + EXPECT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_three.h b/test/unittest/UTTest_device_manager_service_three.h index 4f7c7666b..3f03c34cc 100644 --- a/test/unittest/UTTest_device_manager_service_three.h +++ b/test/unittest/UTTest_device_manager_service_three.h @@ -31,6 +31,7 @@ #include "softbus_listener_mock.h" #include "device_manager_service_mock.h" #include "device_manager_service_impl_mock.h" +#include "multiple_user_connector_mock.h" namespace OHOS { namespace DistributedHardware { @@ -49,6 +50,8 @@ public: std::make_shared(); static inline std::shared_ptr deviceManagerServiceImplMock_ = std::make_shared(); + static inline std::shared_ptr multipleUserConnectorMock_ = + std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/device_manager_service_impl_mock.cpp b/test/unittest/mock/device_manager_service_impl_mock.cpp index 80ac6e8c6..2864f0135 100644 --- a/test/unittest/mock/device_manager_service_impl_mock.cpp +++ b/test/unittest/mock/device_manager_service_impl_mock.cpp @@ -116,5 +116,21 @@ bool DeviceManagerServiceImpl::CheckIsSameAccount(const DmAccessCaller &caller, return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->CheckIsSameAccount(caller, srcUdid, callee, sinkUdid); } + +int32_t DeviceManagerServiceImpl::CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key) +{ + return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->CheckServiceInfoByServiceId(key); +} + +int32_t DeviceManagerServiceImpl::PublishServiceInfoByServiceId(const ServiceInfoUniqueKey& key, + const DmDiscoverMode& discoverMode) +{ + return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->PublishServiceInfoByServiceId(key, discoverMode); +} + +int32_t DeviceManagerServiceImpl::UnPublishServiceInfoByServiceId(int64_t serviceId) +{ + return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->UnPublishServiceInfoByServiceId(serviceId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_service_impl_mock.h b/test/unittest/mock/device_manager_service_impl_mock.h index 2886de7e3..07e58767f 100644 --- a/test/unittest/mock/device_manager_service_impl_mock.h +++ b/test/unittest/mock/device_manager_service_impl_mock.h @@ -50,6 +50,10 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid) = 0; virtual bool CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid) = 0; + virtual int32_t CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key) = 0; + virtual int32_t PublishServiceInfoByServiceId(const ServiceInfoUniqueKey& key, + const DmDiscoverMode& discoverMode) = 0; + virtual int32_t UnPublishServiceInfoByServiceId(int64_t serviceId) = 0; public: static inline std::shared_ptr dmDeviceManagerServiceImpl = nullptr; }; @@ -77,6 +81,10 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid)); MOCK_METHOD(bool, CheckIsSameAccount, (const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid)); + MOCK_METHOD(int32_t, CheckServiceInfoByServiceId, (const ServiceInfoUniqueKey& key)); + MOCK_METHOD(int32_t, PublishServiceInfoByServiceId, (const ServiceInfoUniqueKey& key, + const DmDiscoverMode& discoverMode)); + MOCK_METHOD(int32_t, UnPublishServiceInfoByServiceId, (int64_t serviceId)); }; } } diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index 603e20b2a..d4845e59a 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -200,5 +200,11 @@ bool DeviceProfileConnector::IsLnnAcl(const DistributedDeviceProfile::AccessCont { return DmDeviceProfileConnector::dmDeviceProfileConnector->IsLnnAcl(profile); } + +int32_t DeviceProfileConnector::GetServiceInfoByServiceId( + ServiceInfoUniqueKey key, ServiceInfoProfile& serviceInfoProfile) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetServiceInfoByServiceId(key, serviceInfoProfile); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/deviceprofile_connector_mock.h b/test/unittest/mock/deviceprofile_connector_mock.h index 17d9b9333..8345610c2 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -75,6 +75,7 @@ public: virtual void CacheAcerAclId(const DistributedDeviceProfile::AccessControlProfile &profile, std::vector &aclInfos) = 0; virtual bool IsLnnAcl(const DistributedDeviceProfile::AccessControlProfile &profile) = 0; + virtual int32_t GetServiceInfoByServiceId(ServiceInfoUniqueKey key, ServiceInfoProfile& serviceInfoProfile) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -124,6 +125,7 @@ public: MOCK_METHOD(void, CacheAcerAclId, (const DistributedDeviceProfile::AccessControlProfile &profile, std::vector &aclInfos)); MOCK_METHOD(bool, IsLnnAcl, (const DistributedDeviceProfile::AccessControlProfile &profile)); + MOCK_METHOD(int32_t, GetServiceInfoByServiceId, (ServiceInfoUniqueKey key, ServiceInfoProfile& serviceInfoProfile)); }; } } diff --git a/test/unittest/mock/softbus_connector_mock.cpp b/test/unittest/mock/softbus_connector_mock.cpp index d49c8c253..fb8f7552f 100644 --- a/test/unittest/mock/softbus_connector_mock.cpp +++ b/test/unittest/mock/softbus_connector_mock.cpp @@ -59,5 +59,11 @@ int32_t SoftbusConnector::SyncLocalAclListProcess(const DevUserInfo &localDevUse return DmSoftbusConnector::dmSoftbusConnector->SyncLocalAclListProcess(localDevUserInfo, remoteDevUserInfo, remoteAclList); } + +int32_t SoftbusConnector::PublishService(const ServiceInfoProfile& serviceInfoProfile, + const DmDiscoverMode& discoverMode) +{ + return DmSoftbusConnector::dmSoftbusConnector->PublishService(serviceInfoProfile, discoverMode); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/softbus_connector_mock.h b/test/unittest/mock/softbus_connector_mock.h index e71d229f6..f36e20806 100644 --- a/test/unittest/mock/softbus_connector_mock.h +++ b/test/unittest/mock/softbus_connector_mock.h @@ -35,7 +35,8 @@ public: virtual void SetProcessInfoVec(std::vector processInfoVec) = 0; virtual int32_t SyncLocalAclListProcess(const DevUserInfo &localDevUserInfo, const DevUserInfo &remoteDevUserInfo, std::string remoteAclList) = 0; - + virtual int32_t PublishService(const ServiceInfoProfile& serviceInfoProfile, + const DmDiscoverMode& discoverMode) = 0; public: static inline std::shared_ptr dmSoftbusConnector = nullptr; }; @@ -50,6 +51,8 @@ public: MOCK_METHOD(void, SetProcessInfo, (ProcessInfo processInfo)); MOCK_METHOD(void, SetProcessInfoVec, (std::vector processInfoVec)); MOCK_METHOD(int32_t, SyncLocalAclListProcess, (const DevUserInfo &, const DevUserInfo &, std::string)); + MOCK_METHOD(int32_t, PublishService, (const ServiceInfoProfile& serviceInfoProfile, + const DmDiscoverMode& discoverMode)); }; } } -- Gitee