diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index 418684851ef4e18d1ffd2b47eeff7818525a521e..f2db6e2744b1b46c2932730ec5ec6458f7b67973 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -123,7 +123,12 @@ enum DMIpcCmdInterfaceCode { CHECK_SRC_SAME_ACCOUNT, CHECK_SINK_SAME_ACCOUNT, // Add ipc msg here - IPC_MSG_BUTT + IPC_MSG_BUTT, + + GENERATE_SERVICE_ID, + REGISTER_SERVICE_INFO, + UNREGISTER_SERVICE_INFO, + UPDATE_SERVICE_INFO, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_common_service_id_req.h b/common/include/ipc/model/ipc_common_service_id_req.h new file mode 100644 index 0000000000000000000000000000000000000000..88890de7b2c62db2159ce8b5d37756679e544295 --- /dev/null +++ b/common/include/ipc/model/ipc_common_service_id_req.h @@ -0,0 +1,43 @@ +/* + * 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_COMMON_SERVICE_ID_REQ_H +#define OHOS_DM_IPC_COMMON_SERVICE_ID_REQ_H +#include + +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcCommonServiceIdReq : public IpcReq { + DECLARE_IPC_MODEL(IpcCommonServiceIdReq); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + void SetServiceId(int64_t serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_COMMON_SERVICE_ID_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_generate_service_id_rsp.h b/common/include/ipc/model/ipc_generate_service_id_rsp.h new file mode 100644 index 0000000000000000000000000000000000000000..d70a2328574c7ca4b2cbf078a0cfa2ede8755863 --- /dev/null +++ b/common/include/ipc/model/ipc_generate_service_id_rsp.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_GENERATE_SERVICE_ID_REQ_H +#define OHOS_DM_IPC_GENERATE_SERVICE_ID_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGenerateServiceIdRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGenerateServiceIdRsp); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + void SetServiceId(int64_t serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GENERATE_SERVICE_ID_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_register_service_info_new_req.h b/common/include/ipc/model/ipc_register_service_info_new_req.h new file mode 100644 index 0000000000000000000000000000000000000000..9e48703ae2f21b90bc7e8fc8de9510b8dcadb086 --- /dev/null +++ b/common/include/ipc/model/ipc_register_service_info_new_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_REGISTER_SERVICE_INFO_NEW_REQ_H +#define OHOS_DM_IPC_REGISTER_SERVICE_INFO_NEW_REQ_H + +#include "dm_device_info.h" +#include "ipc_req.h" +namespace OHOS { +namespace DistributedHardware { +class IpcRegisterServiceInfoNewReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegisterServiceInfoNewReq); + +public: + ServiceRegInfo GetServiceRegInfo() const + { + return serviceRegInfo_; + } + void SetServiceRegInfo(const ServiceRegInfo& serviceRegInfo) + { + serviceRegInfo_ = serviceRegInfo; + } + +private: + ServiceRegInfo serviceRegInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_REGISTER_SERVICE_INFO_NEW_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_unregister_service_info_req.h b/common/include/ipc/model/ipc_unregister_service_info_req.h new file mode 100644 index 0000000000000000000000000000000000000000..31d2cec1465d2737a60b27ca5d0e2afdd660215b --- /dev/null +++ b/common/include/ipc/model/ipc_unregister_service_info_req.h @@ -0,0 +1,42 @@ +/* + * 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_UNREGISTER_SERVICE_INFO_REQ_H +#define OHOS_DM_IPC_UNREGISTER_SERVICE_INFO_REQ_H + +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcUnRegisterServiceInfoReq : public IpcReq { + DECLARE_IPC_MODEL(IpcUnRegisterServiceInfoReq); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_UNREGISTER_SERVICE_INFO_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_update_service_info_req.h b/common/include/ipc/model/ipc_update_service_info_req.h new file mode 100644 index 0000000000000000000000000000000000000000..32f8068f12c4daf85ab55f2c281af07a16c11137 --- /dev/null +++ b/common/include/ipc/model/ipc_update_service_info_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_UPDATE_SERVICE_INFO_REQ_H +#define OHOS_DM_IPC_UPDATE_SERVICE_INFO_REQ_H + +#include "dm_subscribe_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { + +class IpcUpdateServiceInfoReq : public IpcReq { + DECLARE_IPC_MODEL(IpcUpdateServiceInfoReq); + +public: + ServiceRegInfo GetServiceRegInfo() + { + return serviceRegInfo_; + } + + void SetServiceRegInfo(const ServiceRegInfo& serviceRegInfo) + { + serviceRegInfo_ = serviceRegInfo; + } + + int64_t GetServiceId() + { + return serviceId_; + } + + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; + ServiceRegInfo serviceRegInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_UPDATE_SERVICE_INFO_REQ_H \ No newline at end of file diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index b6e6df3ec18feb3880ea38abefe2ed3e8a2336d9..2b4f2b089e241c7fe7953db994616526c7dfdf63 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 132a0a795028e47908e2b7245565dc99b7f9f611..16ab20b0c3615f87e84323a7906f038a7457c9b5 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 da0db3936076c2cece54906b35ba28a0a818fe77..741db6e13a8ffb9121c4574a5c5bc638ee9a86dd 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,11 @@ 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 GenerateServiceId(int64_t& serviceId) = 0; + virtual int32_t RegisterServiceInfo(const ServiceRegInfo& serviceRegInfo) = 0; + virtual int32_t UnRegisterServiceInfo(const int64_t& serviceId) = 0; + virtual int32_t UpdateServiceInfo(int64_t serviceId, const ServiceRegInfo& serviceInfo) = 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 863626aef135a41ee472de50ce98df36a35c596b..fda3cc905419c1e4feaf81e386eac724195e4358 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,11 @@ public: virtual bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; + virtual int32_t GenerateServiceId(int64_t& serviceId) override; + virtual int32_t RegisterServiceInfo(const ServiceRegInfo& serviceRegInfo) override; + virtual int32_t UnRegisterServiceInfo(const int64_t& serviceId) override; + virtual int32_t UpdateServiceInfo(int64_t serviceId, const ServiceRegInfo& serviceInfo) 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 67c3a32aec0a6114bef43e50c0ce4ad577cbf7eb..d360e030118d10c23ed737671a11e9a55ffe74b1 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 0b2bfebaeeaaacfb6e73813b8ddab509eb866af2..f0b99df2fe5d28aa24c2f696dff2b711114de73a 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" @@ -34,6 +37,7 @@ #include "ipc_destroy_pin_holder_req.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_generate_service_id_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -56,6 +60,7 @@ #include "ipc_permission_req.h" #include "ipc_publish_req.h" #include "ipc_put_device_profile_info_list_req.h" +#include "ipc_register_service_info_new_req.h" #include "ipc_register_serviceinfo_req.h" #include "ipc_set_credential_req.h" #include "ipc_set_credential_rsp.h" @@ -67,7 +72,10 @@ #include "ipc_unauthenticate_device_req.h" #include "ipc_unbind_device_req.h" #include "ipc_unpublish_req.h" +#include "ipc_unregister_service_info_req.h" +#include "ipc_update_service_info_req.h" #include "securec.h" + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "ipc_model_codec.h" #include "iservice_registry.h" @@ -2994,5 +3002,88 @@ bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const Dm anonyLocalUdid_); return result; } + +int32_t DeviceManagerImpl::GenerateServiceId(int64_t& serviceId) +{ + LOGI("Start"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GENERATE_SERVICE_ID, 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; + } + serviceId = rsp->GetServiceId(); + LOGI("End, serviceId : %{public}lld", serviceId); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterServiceInfo(const ServiceRegInfo& serviceInfo) +{ + LOGI("Start"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceRegInfo(serviceInfo); + int32_t ret = ipcClientProxy_->SendRequest(REGISTER_SERVICE_INFO, 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("End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceInfo(const int64_t& serviceId) +{ + LOGI("Start"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceId(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_SERVICE_INFO, 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("End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UpdateServiceInfo(int64_t serviceId, const ServiceRegInfo& serviceInfo) +{ + LOGI("Start"); + uint32_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + LOGI("SQW tokenid=%{public}d", tokenId); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceId(serviceId); + req->SetServiceRegInfo(serviceInfo); + int32_t ret = ipcClientProxy_->SendRequest(UPDATE_SERVICE_INFO, 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("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 a30e5493b2b6ea81ff6113a7f7f820ed819f662a..9dc14df959421c293294804079a020cf517aba77 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 @@ -27,11 +27,13 @@ #include "ipc_check_access_control.h" #include "ipc_cmd_register.h" #include "ipc_common_param_req.h" +#include "ipc_common_service_id_req.h" #include "ipc_create_pin_holder_req.h" #include "ipc_destroy_pin_holder_req.h" #include "ipc_def.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_generate_service_id_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -55,6 +57,7 @@ #include "ipc_notify_event_req.h" #include "ipc_put_device_profile_info_list_req.h" #include "ipc_register_listener_req.h" +#include "ipc_register_service_info_new_req.h" #include "ipc_register_serviceinfo_req.h" #include "ipc_req.h" #include "ipc_rsp.h" @@ -68,6 +71,8 @@ #include "ipc_publish_req.h" #include "ipc_unbind_device_req.h" #include "ipc_unpublish_req.h" +#include "ipc_unregister_service_info_req.h" +#include "ipc_update_service_info_req.h" #include "ipc_unauthenticate_device_req.h" #include "securec.h" namespace OHOS { class IRemoteObject; } @@ -2217,5 +2222,96 @@ ON_IPC_READ_RESPONSE(CHECK_SINK_SAME_ACCOUNT, MessageParcel &reply, std::shared_ { return ReadResponse(CHECK_SINK_SAME_ACCOUNT, reply, pBaseRsp); } + +ON_IPC_READ_RESPONSE(GENERATE_SERVICE_ID, 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->SetServiceId(reply.ReadInt64()); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(REGISTER_SERVICE_INFO, 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); + const ServiceRegInfo serviceRegInfo = pReq->GetServiceRegInfo(); + if (!data.WriteRawData(&serviceRegInfo, sizeof(ServiceRegInfo))) { + LOGE("write serviceRegInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_SERVICE_INFO, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_SERVICE_INFO, 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); + if (!data.WriteInt64(pReq->GetServiceId())) { + LOGE("write serviceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_SERVICE_INFO, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO, 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); + if (!data.WriteInt64(pReq->GetServiceId())) { + LOGE("write serviceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + const ServiceRegInfo serviceRegInfo = pReq->GetServiceRegInfo(); + if (!data.WriteRawData(&serviceRegInfo, sizeof(ServiceRegInfo))) { + LOGE("write serviceRegInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 9efda6ff9b6370b71ba5bf01aec3133ab07da5dc..53471619acc798b32f8d96ed4d065de0b941b0a8 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -205,6 +205,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 GenerateServiceId(int64_t serviceId); + int32_t CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 8ec4d49a25f0968d43a1658e3728e7f48ea24dbe..9933aaab37cce27202b247c1a340d9da59abe4b4 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -182,6 +182,9 @@ 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 GenerateServiceId(int64_t& serviceId) override; + virtual int32_t CheckServiceInfoByServiceId(ServiceInfoUniqueKey& key) override; private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 3b29c520a3b927e421c9069d177360d88aa86bee..129e879ab40e1a32e42b1f47401e61584e6244d8 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -3080,6 +3080,18 @@ 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::GenerateServiceId(int64_t serviceId) +{ + (void)serviceId; + return DM_OK; +} + 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 445f2b0705a6c32ed583a1a5d39ffb8a6074e6c2..a571c31e888be7f0c8e491663b8f6a862faec3ee 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -741,6 +741,18 @@ void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, return; } +int32_t DeviceManagerServiceImpl::GenerateServiceId(int64_t& serviceId) +{ + (void)serviceId; + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::CheckServiceInfoByServiceId(ServiceInfoUniqueKey& key) +{ + (void)key; + return DM_OK; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 069a2ad13e7f8ffb62039b4ea70a995ac1fc86be..f2db0959692a9ab5c7a8353b8e42f32fbebd9b23 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,16 @@ 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 GetServiceInfoByServiceId(int64_t& serviceId, ServiceInfo& serviceInfo); + bool CheckIsDeviceOnline(std::vector& onlineDevicelist, std::string& peerUdid); + int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo& localInfo); + int32_t InitServiceInfoProfileByRegInfo( + const ServiceRegInfo& serviceRegInfo, ServiceInfoProfile& serviceInfoProfile); + int32_t GenerateServiceId(int64_t serviceId); + int32_t RegisterServiceInfo(const ServiceRegInfo& serviceRegInfo); + int32_t UnRegisterServiceInfo(int64_t serviceId); + int32_t UpdateServiceInfo(const ServiceFilter& serviceFilter, const ServiceRegInfo& serviceRegInfo); 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 949542bedeb80a1f942cc5aecc0edaf66183fd77..8bcc6029051fc0e3a12ad5d8b054215b7293f35b 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -286,6 +286,9 @@ 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 GenerateServiceId(int64_t serviceId) = 0; + virtual int32_t CheckServiceInfoByServiceId(const ServiceInfoUniqueKey& key) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index f68075c02fe5c9ccd2bab490d0b61054f9250331..ff47f18ba276ef61a81defaec7f386947767ceef 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -68,7 +68,9 @@ constexpr const char* LIB_DM_DEVICE_RISK_DETECT_NAME = "libdevicemanagerriskdete #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG) constexpr const char* LIB_DM_CHECK_API_WHITE_LIST_NAME = "libdm_check_api_whitelist.z.so"; #endif - +constexpr int32_t MIN_PIN_TOKEN = 1000000; +constexpr int32_t MAX_PIN_TOKEN = 90000000; +constexpr uint32_t SERVICE_ID_LENGTH = 16; namespace OHOS { namespace DistributedHardware { DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService); @@ -4638,5 +4640,229 @@ void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &c MultipleUserConnector::GetCurrentDMAccountInfo()); } #endif + +int32_t DeviceManagerService::GetServiceInfoByServiceId(int64_t& serviceId, ServiceInfo& serviceInfo) +{ + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + ServiceInfoUniqueKey serviceInfoUniqueKey; + serviceInfoUniqueKey.serviceId = serviceId; + serviceInfoUniqueKey.tokenId = std::to_string(localInfo.tokenId); + serviceInfoUniqueKey.userId = localInfo.userId; + serviceInfoUniqueKey.deviceId = localInfo.localUdid; + LOGI("SQW GetServiceInfoByServiceId key serviceId=%{public}" PRId64 ", tokenId=%{public}s, userId=%{public}d, " + "deviceId=%{public}s", + serviceInfoUniqueKey.serviceId, serviceInfoUniqueKey.tokenId.c_str(), serviceInfoUniqueKey.userId, + serviceInfoUniqueKey.deviceId.c_str()); + ServiceInfoProfile dmServiceInfoProfile; + ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey( + serviceInfoUniqueKey, dmServiceInfoProfile); + // LOGI("SQW GetserviceInfoByserviceId ret=%(public)d, serviceId=%(public)ld, service name=%(public)s", ret, + // dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId, + // dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceName.c_str()); + if (ret != DM_OK) { + return ret; + } + // serviceInfo = dmServiceInfoProfile.serviceRegInfo.serviceInfo; + return DM_OK; +} + +bool DeviceManagerService::CheckIsDeviceOnline(std::vector& onlineDeviceList, std::string& peerUdid) +{ + for (auto deviceInfo : onlineDeviceList) { + if (deviceInfo.deviceId == peerUdid) { + return true; + } + } + return false; +} + +int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo& localInfo) +{ + DmDeviceInfo localDeviceInfo; + int32_t ret = GetLocalDeviceInfo(localDeviceInfo); + if (ret != DM_OK) { + LOGE("Get LocalDeviceInfo failed"); + return ret; + } + localInfo.localUdid = localDeviceInfo.deviceId; + localInfo.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + MultipleUserConnector::GetCallerUserId(localInfo.userId); + return DM_OK; +} + +int32_t DeviceManagerService::InitServiceInfoProfileByRegInfo( + const ServiceRegInfo& serviceRegInfo, ServiceInfoProfile& serviceInfoProfile) +{ + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + serviceInfoProfile.serviceRegInfo = serviceRegInfo; + serviceInfoProfile.deviceId = localInfo.localUdid; + serviceInfoProfile.userId = localInfo.userId; + serviceInfoProfile.tokenId = std::to_string(localInfo.tokenId); + serviceInfoProfile.extraInfo = serviceRegInfo.extraInfo; + return DM_OK; +} + +// void DeviceManagerService::InitSoftBusServiceInfoByRegInfo( +// int64_t serviceId, const ServiceRegInfo& serviceRegInfo, ServiceInfo& softBusServiceInfo) +// { +// memcpy_s(softBusServiceInfo.serviceId, sizeof(softBusServiceInfo.serviceId) , serviceRegInfo.serviceInfo.serviceId, +// sizeof(serviceRegInfo.serviceInfo.serviceId)); +// softBusServiceInfo.serviceType = serviceReginfo.serviceInfoserviceType.c_str(); +// softBusServiceInfo.serviceName = serviceReginfo.serviceInfo.serviceName.c_str(); +// softBusServiceInfo.serviceDisplayName = serviceRegInfo.serviceInfo.serviceDisplayName.c_str(); +// for (int i = 0; i < (int)serviceRegInfo.serviceInfo.customData.length(); i++) { +// softBusServiceInfo.customData[i] = serviceRegInfo.servviceInfo.customData[i]; +// } +// softBusServiceInfo.datalen = serviceReginfo.servviceInfo.datalen +// } + +int32_t DeviceManagerService::GenerateServiceId(int64_t serviceId) +{ + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller does not have permission to call Get LocalDeviceInfo."); + return ERR_DM_NO_PERMISSION; + } + ServiceInfo serviceInfoProfiles; + + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + + if (ret != DM_OK) { + LOGE("GenerateServiceId failed, get local info error"); + return ret; + } + // TODO: 待修改 + int GENERATE_SERVICE_ID_RETRY_TIME = 3; + bool isServiceIdExist = true; + for (int i = 0; i < GENERATE_SERVICE_ID_RETRY_TIME; i++) { + int32_t randNum = GenRandInt(MIN_PIN_TOKEN, MAX_PIN_TOKEN); + std::string serviceIdStr = + Crypto::GetHashWithSalt(std::to_string(randNum), std::to_string(localInfo.tokenId) + localInfo.localUdid); + serviceIdStr = serviceIdStr.substr(0, SERVICE_ID_LENGTH - 1); + // TODO: 未定义 + // serviceId = GetNumberFromHexString(serviceIdStr); + serviceId = stoi(serviceIdStr); + ServiceInfo serviceInfo; + if (GetServiceInfoByServiceId(serviceId, serviceInfo) != DM_OK) { + isServiceIdExist = false; + ret = DM_OK; + break; + } + } + if (isServiceIdExist) { + LOGE("GenerateServiceId failed, ret = %{public}d ", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t DeviceManagerService::RegisterServiceInfo(const ServiceRegInfo& serviceRegInfo) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The callerdoes not have permission to cal1 RegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + LOGI("SQW RegisterServiceInfo 1 service name=%{public}s, service id=%{public}lld", + serviceRegInfo.serviceInfo.serviceName.c_str(), serviceRegInfo.serviceInfo.serviceId); + + ServiceInfoProfile serviceInfoProfile; + ret = InitServiceInfoProfileByRegInfo(serviceRegInfo, serviceInfoProfile); + if (ret != DM_OK) { + return ret; + } + int32_t dp_result = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + // LOGI("SQW RegisterServiceInfo ret=%{public}d, servicename=%{public}s, service id=%{public}lld, " + // "udid=%{public}s,userid=%{public}d,tokenid=%{public}s", + // dp_result, serviceInfoProfile.serviceRegInfo.serviceInfo.serviceName.c_str(), + // serviceInfoProfile.serviceRegInfo.serviceInfo.serviceId, serviceInfoProfile.deviceId.c_str(), + // serviceInfoProfile.userId, serviceInfoProfile.tokenId.c_str()); + // TODO: ServiceInfo为softbus端定义的,并非现有定义 + // ServiceInfo serviceInfo; + // InitSoftBusServiceInfoByRegInfo(serviceRegInfo, serviceInfo); + // int32_t softbus_result = RegisterService(&serviceInfo); + // todo:管理服务信息的生命周期 + + if (dp_result != DM_OK) { + return dp_result; + } + // if (softbus_result != DM_OK) { + // return softbus_result; + // } + return DM_OK; +} + +int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call UnRegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + return ret; + } + ServiceInfoUniqueKey key; + key.deviceId = localInfo.localUdid; + key.userId = localInfo.userId; + key.tokenId = std::to_string(localInfo.tokenId); + key.serviceId = serviceId; + + LOGI("SQW UnRegisterServiceInfo 1 service id=%{public}lld", serviceId); + + // TODO:需要新建到softbus_connector类中 + int32_t dp_result = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(key); + + // TODO:需要新建到softbus_connector类中 + // int32_t softbus_result = UnRegisterService(std::to_string(serviceId).c_str()); + // LOGI("UnRegisterServiceInfo dp_result=%{public}d, softbus_result=%{public}d", dp_result, + // softbus_result); + // if (dp_result != DM_OK) { + // return dp_result; + // } + // if (softbus_result != DM_OK) { + // return softbus_result; + // } + return DM_OK; +} + +int32_t DeviceManagerService::UpdateServiceInfo( + const ServiceFilter& serviceFilter, const ServiceRegInfo& serviceRegInfo) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call StartPpublishService."); + return ERR_DM_NO_PERMISSION; + } + ServiceInfoProfile serviceInfoProfile; + int32_t ret = InitServiceInfoProfileByRegInfo(serviceRegInfo, serviceInfoProfile); + if (ret != DM_OK) { + return ret; + } + int32_t dp_result = DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(serviceInfoProfile); + ServiceInfo serviceInfo; + // InitSoftBusServiceInfoByRegInfo(serviceId, serviceReginfo, serviceInfo); + // int32_t softbus_result = UpdateService(&serviceInfo); + // LOGI("UpdateServiceInfo dp_result=%{public}d, softbus_reesult=%{public}d", dp_result, softbus_result); + if (dp_result != DM_OK) { + return dp_result; + } + // if (softbus_result != DM_OK) { + // return softbus_result; + // } + 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 e8f908bca808e91b7bef061b7d87ed46e4c9e3c3..320241534d71b0abb19264d19761349f9f9e6486 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ + #include "xcollie/xcollie.h" #include "xcollie/xcollie_define.h" @@ -132,6 +133,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 +194,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 +205,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) diff --git a/test/unittest/UTTest_device_manager_impl_three.cpp b/test/unittest/UTTest_device_manager_impl_three.cpp index 4fec18209a9c054faae66cda9e76164cba92ba0c..dc115d02444ff34b01756cbed3ed6604264cb68a 100644 --- a/test/unittest/UTTest_device_manager_impl_three.cpp +++ b/test/unittest/UTTest_device_manager_impl_three.cpp @@ -1436,6 +1436,16 @@ 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, GenerateServiceId_001, testing::ext::TestSize.Level0) +{ + int64_t serviceId = 0; + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(GENERATE_SERVICE_ID, testing::_, testing::_)) + .WillOnce(testing::Return(ERR_DM_IPC_SEND_REQUEST_FAILED)); + + int32_t ret = DeviceManagerImpl::GetInstance().GenerateServiceId(serviceId); + EXPECT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); +} } // 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 d778f74362f2f7d8802669a6e4a23023f3c1f05a..d46f41fd0dadedaad395e94064f50ec82450dcf5 100644 --- a/test/unittest/UTTest_device_manager_service_impl_first.cpp +++ b/test/unittest/UTTest_device_manager_service_impl_first.cpp @@ -822,6 +822,58 @@ 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; + }), _)) + .WillOnce(DoAll(SetArgReferee<1>(serviceInfo), 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 = "invalidDeviceId"; + key.userId = -1; + key.tokenId = "invalidTokenId"; + 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; + }), _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = deviceManagerServiceImpl_->CheckServiceInfoByServiceId(key); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceImplFirstTest, GenerateServiceId_001, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + int32_t ret = deviceManagerServiceImpl_->GenerateServiceId(serviceId); + EXPECT_EQ(ret, DM_OK); +} + } // 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 5ca56ab1440f80cfc8d88825cf57f927149b6c42..a32eeeb8efe2c804325956d05879e20b3c065f23 100644 --- a/test/unittest/UTTest_device_manager_service_three.cpp +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -64,6 +64,9 @@ void DeviceManagerServiceThreeTest::TearDown() Mock::VerifyAndClearExpectations(permissionManagerMock_.get()); Mock::VerifyAndClearExpectations(softbusListenerMock_.get()); Mock::VerifyAndClearExpectations(deviceManagerServiceImplMock_.get()); + Mock::VerifyAndClearExpectations(deviceProfileConnectorMock_.get()); + Mock::VerifyAndClearExpectations(multipleUserConnectorMock_.get()); + Mock::VerifyAndClearExpectations(cryptoMock_.get()); } void DeviceManagerServiceThreeTest::SetUpTestCase() @@ -72,6 +75,9 @@ void DeviceManagerServiceThreeTest::SetUpTestCase() DmPermissionManager::dmPermissionManager = permissionManagerMock_; DmSoftbusListener::dmSoftbusListener = softbusListenerMock_; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = deviceManagerServiceImplMock_; + DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_; + DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnectorMock_; + DmCrypto::dmCrypto = cryptoMock_; } void DeviceManagerServiceThreeTest::TearDownTestCase() @@ -84,6 +90,12 @@ void DeviceManagerServiceThreeTest::TearDownTestCase() softbusListenerMock_ = nullptr; DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = nullptr; deviceManagerServiceImplMock_ = nullptr; + DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr; + deviceProfileConnectorMock_ = nullptr; + DmMultipleUserConnector::dmMultipleUserConnector = nullptr; + multipleUserConnectorMock_ = nullptr; + DmCrypto::dmCrypto = nullptr; + cryptoMock_ = nullptr; } namespace { @@ -909,6 +921,528 @@ HWTEST_F(DeviceManagerServiceThreeTest, UnloadDMDeviceRiskDetect_301, testing::e DeviceManagerService::GetInstance().UnloadDMDeviceRiskDetect(); EXPECT_EQ(DeviceManagerService::GetInstance().dmDeviceRiskDetect_, nullptr); } + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoByServiceId_001, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + ServiceInfo serviceInfo; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoByServiceId(serviceId, serviceInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoByServiceId_002, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + ServiceInfo serviceInfo; + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + ServiceInfoUniqueKey expectedKey; + expectedKey.serviceId = serviceId; + expectedKey.tokenId = std::to_string(localInfo.tokenId); + expectedKey.userId = localInfo.userId; + expectedKey.deviceId = localInfo.localUdid; + + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoProfileByUniqueKey(testing::Truly([&expectedKey](const ServiceInfoUniqueKey& actualKey) { + return actualKey.serviceId == expectedKey.serviceId && + actualKey.tokenId == expectedKey.tokenId && + actualKey.userId == expectedKey.userId && + actualKey.deviceId == expectedKey.deviceId; + }), _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoByServiceId(serviceId, serviceInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceInfoByServiceId_003, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + ServiceInfo serviceInfo; + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + ServiceInfoUniqueKey expectedKey; + expectedKey.serviceId = serviceId; + expectedKey.tokenId = std::to_string(localInfo.tokenId); + expectedKey.userId = localInfo.userId; + expectedKey.deviceId = localInfo.localUdid; + + ServiceInfoProfile dmServiceInfoProfile; + dmServiceInfoProfile.serviceRegInfo.serviceInfo.serviceId = serviceId; + + EXPECT_CALL(*deviceProfileConnectorMock_, + GetServiceInfoProfileByUniqueKey(testing::Truly([&expectedKey](const ServiceInfoUniqueKey& actualKey) { + return actualKey.serviceId == expectedKey.serviceId && + actualKey.tokenId == expectedKey.tokenId && + actualKey.userId == expectedKey.userId && + actualKey.deviceId == expectedKey.deviceId; + }), _)) + .WillOnce(DoAll(SetArgReferee<1>(dmServiceInfoProfile), Return(DM_OK))); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceInfoByServiceId(serviceId, serviceInfo); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_001, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList; + std::string peerUdid = "testDeviceId"; + + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + EXPECT_FALSE(result); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_002, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList = { + { .deviceId = "deviceId1" }, + { .deviceId = "deviceId2" } + }; + std::string peerUdid = "testDeviceId"; + + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + EXPECT_FALSE(result); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsDeviceOnline_003, testing::ext::TestSize.Level1) +{ + std::vector onlineDeviceList = { + { .deviceId = "deviceId1" }, + { .deviceId = "testDeviceId" } + }; + std::string peerUdid = "testDeviceId"; + + bool result = DeviceManagerService::GetInstance().CheckIsDeviceOnline(onlineDeviceList, peerUdid); + EXPECT_TRUE(result); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceBindLocalInfo_001, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo; + + EXPECT_CALL(*deviceManagerServiceMock_, GetLocalDeviceInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceBindLocalInfo(localInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceBindLocalInfo_002, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo; + DmDeviceInfo localDeviceInfo; + strncpy(localDeviceInfo.deviceId, "testDeviceId", sizeof(localDeviceInfo.deviceId) - 1); + localDeviceInfo.deviceId[sizeof(localDeviceInfo.deviceId) - 1] = '\0'; + + EXPECT_CALL(*deviceManagerServiceMock_, GetLocalDeviceInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localDeviceInfo), Return(DM_OK))); + + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(_)) + .WillOnce(Invoke([](int32_t& userId) { + userId = -1; + })); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceBindLocalInfo(localInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GetServiceBindLocalInfo_003, testing::ext::TestSize.Level1) +{ + ServiceBindLocalInfo localInfo; + DmDeviceInfo localDeviceInfo; + strncpy(localDeviceInfo.deviceId, "testDeviceId", sizeof(localDeviceInfo.deviceId) - 1); + localDeviceInfo.deviceId[sizeof(localDeviceInfo.deviceId) - 1] = '\0'; + + EXPECT_CALL(*deviceManagerServiceMock_, GetLocalDeviceInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localDeviceInfo), Return(DM_OK))); + + EXPECT_CALL(*multipleUserConnectorMock_, GetCallerUserId(_)) + .WillOnce(Invoke([](int32_t& userId) { + userId = 1; + })); + + int32_t ret = DeviceManagerService::GetInstance().GetServiceBindLocalInfo(localInfo); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(localInfo.localUdid, "testDeviceId"); + EXPECT_EQ(localInfo.userId, 1); +} + +HWTEST_F(DeviceManagerServiceThreeTest, InitServiceInfoProfileByRegInfo_001, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + ServiceInfoProfile serviceInfoProfile; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().InitServiceInfoProfileByRegInfo(serviceRegInfo, serviceInfoProfile); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, InitServiceInfoProfileByRegInfo_002, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + serviceRegInfo.extraInfo = "testExtraInfo"; + + ServiceInfoProfile serviceInfoProfile; + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + int32_t ret = DeviceManagerService::GetInstance().InitServiceInfoProfileByRegInfo(serviceRegInfo, serviceInfoProfile); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(serviceInfoProfile.deviceId, "testDeviceId"); + EXPECT_EQ(serviceInfoProfile.userId, 1); + EXPECT_EQ(serviceInfoProfile.tokenId, "67890"); + EXPECT_EQ(serviceInfoProfile.extraInfo, "testExtraInfo"); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GenerateServiceId_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()) + .WillOnce(Return(false)); + + int64_t serviceId = 0; + int32_t ret = DeviceManagerService::GetInstance().GenerateServiceId(serviceId); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GenerateServiceId_002, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()) + .WillOnce(Return(true)); + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int64_t serviceId = 0; + int32_t ret = DeviceManagerService::GetInstance().GenerateServiceId(serviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GenerateServiceId_003, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + EXPECT_CALL(*cryptoMock_, GetHashWithSalt(_, _)) + .WillRepeatedly(Return("123456789abcdef")); + + EXPECT_CALL(*deviceProfileConnectorMock_, GetServiceInfoByServiceId(_, _)) + .WillRepeatedly(Return(DM_OK)); + + int64_t serviceId = 0; + int32_t ret = DeviceManagerService::GetInstance().GenerateServiceId(serviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, GenerateServiceId_004, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*permissionManagerMock_, CheckNewPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + EXPECT_CALL(*cryptoMock_, GetHashWithSalt(_, _)) + .WillRepeatedly(Return("123456789abcdef")); + + EXPECT_CALL(*deviceProfileConnectorMock_, GetServiceInfoByServiceId(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int64_t serviceId = 0; + int32_t ret = DeviceManagerService::GetInstance().GenerateServiceId(serviceId); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterServiceInfo_001, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterServiceInfo_002, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterServiceInfo_003, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterServiceInfo_004, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(Return(DM_OK)); + + EXPECT_CALL(*deviceProfileConnectorMock_, PutServiceInfoProfile(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterServiceInfo_005, testing::ext::TestSize.Level1) +{ + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(Return(DM_OK)); + + EXPECT_CALL(*deviceProfileConnectorMock_, PutServiceInfoProfile(_)) + .WillOnce(Return(DM_OK)); + + int32_t ret = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnRegisterServiceInfo_001, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnRegisterServiceInfo_002, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnRegisterServiceInfo_003, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + ServiceInfoUniqueKey key; + key.deviceId = localInfo.localUdid; + key.userId = localInfo.userId; + key.tokenId = std::to_string(localInfo.tokenId); + key.serviceId = serviceId; + + EXPECT_CALL(*deviceProfileConnectorMock_, + DeleteServiceInfoProfile(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }))) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnRegisterServiceInfo_004, testing::ext::TestSize.Level1) +{ + int64_t serviceId = 12345; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceBindLocalInfo localInfo; + localInfo.localUdid = "testDeviceId"; + localInfo.tokenId = 67890; + localInfo.userId = 1; + + EXPECT_CALL(*deviceManagerServiceMock_, GetServiceBindLocalInfo(_)) + .WillOnce(DoAll(SetArgReferee<0>(localInfo), Return(DM_OK))); + + ServiceInfoUniqueKey key; + key.deviceId = localInfo.localUdid; + key.userId = localInfo.userId; + key.tokenId = std::to_string(localInfo.tokenId); + key.serviceId = serviceId; + + EXPECT_CALL(*deviceProfileConnectorMock_, + DeleteServiceInfoProfile(testing::Truly([&key](const ServiceInfoUniqueKey& actualKey) { + return actualKey.deviceId == key.deviceId && + actualKey.userId == key.userId && + actualKey.tokenId == key.tokenId && + actualKey.serviceId == key.serviceId; + }))) + .WillOnce(Return(DM_OK)); + + int32_t ret = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UpdateServiceInfo_001, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(false)); + + int32_t ret = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceFilter, serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_NO_PERMISSION); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UpdateServiceInfo_002, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceFilter, serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UpdateServiceInfo_003, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceInfoProfile serviceInfoProfile; + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(serviceInfoProfile), Return(DM_OK))); + + EXPECT_CALL(*deviceProfileConnectorMock_, UpdateServiceInfoProfile(_)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceFilter, serviceRegInfo); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UpdateServiceInfo_004, testing::ext::TestSize.Level1) +{ + ServiceFilter serviceFilter; + ServiceRegInfo serviceRegInfo; + + EXPECT_CALL(*permissionManagerMock_, CheckPermission()) + .WillOnce(Return(true)); + + ServiceInfoProfile serviceInfoProfile; + EXPECT_CALL(*deviceManagerServiceMock_, InitServiceInfoProfileByRegInfo(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(serviceInfoProfile), Return(DM_OK))); + + EXPECT_CALL(*deviceProfileConnectorMock_, UpdateServiceInfoProfile(_)) + .WillOnce(Return(DM_OK)); + + int32_t ret = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceFilter, serviceRegInfo); + 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 4f7c7666b6e5a05ef8999c06b4083109734670a1..a351ad60189823a914be3e5af820a2b165546875 100644 --- a/test/unittest/UTTest_device_manager_service_three.h +++ b/test/unittest/UTTest_device_manager_service_three.h @@ -31,6 +31,9 @@ #include "softbus_listener_mock.h" #include "device_manager_service_mock.h" #include "device_manager_service_impl_mock.h" +#include "deviceprofile_connector_mock.h" +#include "dm_crypto_mock.h" +#include "multiple_user_connector_mock.h" namespace OHOS { namespace DistributedHardware { @@ -49,6 +52,11 @@ public: std::make_shared(); static inline std::shared_ptr deviceManagerServiceImplMock_ = std::make_shared(); + static inline std::shared_ptr deviceProfileConnectorMock_ = + std::make_shared(); + static inline std::shared_ptr multipleUserConnectorMock_ = + std::make_shared(); + static inline std::shared_ptr cryptoMock_ = std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/device_manager_service_mock.cpp b/test/unittest/mock/device_manager_service_mock.cpp index a6db438a686920ae7ed5cf985651ae175e4c75fe..7ae52687cec2a06142aa54e78248e91e4c0cb80d 100644 --- a/test/unittest/mock/device_manager_service_mock.cpp +++ b/test/unittest/mock/device_manager_service_mock.cpp @@ -39,5 +39,22 @@ bool DeviceManagerService::IsDMServiceAdapterSoLoaded() { return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceAdapterSoLoaded(); } + +int32_t DeviceManagerService::GetLocalDeviceInfo(DmDeviceInfo &info) +{ + return DmDeviceManagerService::dmDeviceManagerService->GetLocalDeviceInfo(info); +} + +int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo& localInfo) +{ + return DmDeviceManagerService::dmDeviceManagerService->GetServiceBindLocalInfo(localInfo); +} + +int32_t DeviceManagerService::InitServiceInfoProfileByRegInfo(const ServiceRegInfo& serviceRegInfo, + ServiceInfoProfile& serviceInfoProfile) +{ + return DmDeviceManagerService::dmDeviceManagerService->InitServiceInfoProfileByRegInfo(serviceRegInfo, + serviceInfoProfile); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_service_mock.h b/test/unittest/mock/device_manager_service_mock.h index 2d5230c0e441f343c131d0dab5daa63170e70bdb..b97524464608c5a60472448b07483d2506bda38a 100644 --- a/test/unittest/mock/device_manager_service_mock.h +++ b/test/unittest/mock/device_manager_service_mock.h @@ -30,6 +30,10 @@ public: virtual int32_t GetTrustedDeviceList(const std::string &pkgName, std::vector &deviceList) = 0; virtual bool IsDMServiceAdapterResidentLoad() = 0; virtual bool IsDMServiceAdapterSoLoaded() = 0;; + virtual int32_t GetLocalDeviceInfo(DmDeviceInfo &info) = 0; + virtual int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo& localInfo) = 0; + virtual int32_t InitServiceInfoProfileByRegInfo(const ServiceRegInfo& serviceRegInfo, + ServiceInfoProfile& serviceInfoProfile) = 0; public: static inline std::shared_ptr dmDeviceManagerService = nullptr; }; @@ -40,6 +44,10 @@ public: MOCK_METHOD(int32_t, GetTrustedDeviceList, (const std::string &, std::vector &)); MOCK_METHOD(bool, IsDMServiceAdapterResidentLoad, ()); MOCK_METHOD(bool, IsDMServiceAdapterSoLoaded, ()); + MOCK_METHOD(int32_t, GetLocalDeviceInfo, (DmDeviceInfo &info)); + MOCK_METHOD(int32_t, GetServiceBindLocalInfo, (ServiceBindLocalInfo& localInfo)); + MOCK_METHOD(int32_t, InitServiceInfoProfileByRegInfo, (const ServiceRegInfo& serviceRegInfo, + ServiceInfoProfile& serviceInfoProfile)); }; } } diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index 603e20b2a48f7c8b48e37efc5d648bccd748d18c..845ac93d9d3f44ff89eaac9f4cd6be42d0c0a045 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -200,5 +200,32 @@ 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); +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileByUniqueKey( + const ServiceInfoUniqueKey& key, ServiceInfoProfile& serviceInfoProfile) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->GetServiceInfoProfileByUniqueKey(key, serviceInfoProfile); +} + +int32_t DeviceProfileConnector::PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->PutServiceInfoProfile(serviceInfoProfile); +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->DeleteServiceInfoProfile(key); +} + +int32_t DeviceProfileConnector::UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->UpdateServiceInfoProfile(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 17d9b9333d44c612de92d4b5f2367e9e07a1aff6..262246b965e2a39f5d81e2ef1f123620a51f290b 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -75,6 +75,12 @@ 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; + virtual int32_t GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key, + ServiceInfoProfile& serviceInfoProfile) = 0; + virtual int32_t PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) = 0; + virtual int32_t DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key) = 0; + virtual int32_t UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -124,6 +130,12 @@ 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)); + MOCK_METHOD(int32_t, GetServiceInfoProfileByUniqueKey, (const ServiceInfoUniqueKey& key, + ServiceInfoProfile& serviceInfoProfile)); + MOCK_METHOD(int32_t, PutServiceInfoProfile, (const ServiceInfoProfile& serviceInfoProfile)); + MOCK_METHOD(int32_t, DeleteServiceInfoProfile, (const ServiceInfoUniqueKey& key)); + MOCK_METHOD(int32_t, UpdateServiceInfoProfile, (const ServiceInfoProfile& serviceInfoProfile)); }; } } diff --git a/test/unittest/mock/dm_crypto_mock.cpp b/test/unittest/mock/dm_crypto_mock.cpp index a559c2d01f8d96feaa07544eb0b3437d0b1e8fc1..94a4206b93533168c903a26d83b415d218ece168 100644 --- a/test/unittest/mock/dm_crypto_mock.cpp +++ b/test/unittest/mock/dm_crypto_mock.cpp @@ -47,5 +47,10 @@ int32_t Crypto::ConvertUdidHashToAnoyDeviceId(const std::string &appId, const st { return DmCrypto::dmCrypto->ConvertUdidHashToAnoyDeviceId(appId, udidHash, kvValue); } + +std::string Crypto::GetHashWithSalt(const std::string &text, const std::string &salt) +{ + return DmCrypto::dmCrypto->GetHashWithSalt(text, salt); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/dm_crypto_mock.h b/test/unittest/mock/dm_crypto_mock.h index 0bc47fe358ce158b8a990f0c645f52119cd7078f..60c9d14911cdef5fcf0b9b4d748e38266644fe76 100644 --- a/test/unittest/mock/dm_crypto_mock.h +++ b/test/unittest/mock/dm_crypto_mock.h @@ -33,6 +33,7 @@ public: virtual std::string GetGroupIdHash(const std::string &groupId) = 0; virtual int32_t ConvertUdidHashToAnoyDeviceId(const std::string &appId, const std::string &udidHash, DmKVValue &kvValue) = 0; + virtual std::string GetHashWithSalt(const std::string &text, const std::string &salt) = 0; public: static inline std::shared_ptr dmCrypto = nullptr; }; @@ -44,6 +45,7 @@ public: MOCK_METHOD(int32_t, GetUdidHash, (const std::string &, unsigned char *)); MOCK_METHOD(std::string, GetGroupIdHash, (const std::string &)); MOCK_METHOD(int32_t, ConvertUdidHashToAnoyDeviceId, (const std::string &, const std::string &, DmKVValue &)); + MOCK_METHOD(std::string, GetHashWithSalt, (const std::string &, const std::string &)); }; } }