diff --git a/common/include/dm_error_type.h b/common/include/dm_error_type.h index 0bca1de1bbf53d8f18f418ade87cc96e8c8fb410..722e9b2c8c646b3aeeb200fbc7efea9936bde22e 100644 --- a/common/include/dm_error_type.h +++ b/common/include/dm_error_type.h @@ -145,6 +145,7 @@ enum { ERR_DM_SOCKET_IN_USED = 969298356, ERR_DM_ANTI_DISTURB_MODE = 969298357, ERR_DM_SKIP_AUTHENTICATE = 969298358, + ERR_DM_SERVICE_BIND_PEER_SERVICE_ID_UNPUBLISH = 969298359, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index da0db3936076c2cece54906b35ba28a0a818fe77..5d5191e463e8114092aa264787fe24daa534d388 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,21 @@ 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 StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam, + std::shared_ptr callback) = 0; + virtual int32_t StopServiceDiscovery(const std::string &pkgName, int32_t discoveryServiceId) = 0; + virtual int32_t BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + std::map &bindParam, std::shared_ptr callback) = 0; + virtual int32_t UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) = 0; + virtual int32_t RegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) = 0; + virtual int32_t StartPublishService(const std::string &pkgName, const PublishServiceParam &publishServiceParam, + std::shared_ptr callback, int64_t &serviceId) = 0; + virtual int32_t StopPublishService(int64_t serviceId) = 0; + virtual int32_t RegisterServiceInfo(const ServiceRegInfo &serviceRegInfo, int32_t ®ServiceId) = 0; + virtual int32_t UnRegisterServiceInfo(int32_t regServiceId) = 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..6e449670dec58b87ab61fa4139d188ad587c1b08 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -439,7 +439,21 @@ public: virtual bool CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee) override; virtual bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; virtual bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee) override; - + virtual int32_t BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + std::map &bindParam, std::shared_ptr callback) override; + virtual int32_t UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) override; + + virtual int32_t StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam, + std::shared_ptr callback) override; + virtual int32_t StopServiceDiscovery(const std::string &pkgName, int32_t discoveryServiceId) override; + virtual int32_t RegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId, + std::shared_ptr callback) override; + virtual int32_t UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) override; + virtual int32_t StartPublishService(const std::string &pkgName, const PublishServiceParam &publishServiceParam, + std::shared_ptr callback, int64_t &serviceId) override; + virtual int32_t StopPublishService(int64_t serviceId) override; + virtual int32_t RegisterServiceInfo(const ServiceRegInfo &serviceRegInfo, int32_t ®ServiceId) override; + virtual int32_t UnRegisterServiceInfo(int32_t regServiceId) override; private: DeviceManagerImpl() = default; ~DeviceManagerImpl() = default; 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 23aa74e640daf4c18b077512f3d7bbd29869d739..9c46d2e43b062cd87441dd06c6dd699353c42fb0 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -56,17 +56,24 @@ #include "ipc_permission_req.h" #include "ipc_publish_req.h" #include "ipc_put_device_profile_info_list_req.h" +#include "ipc_publish_service_info_rsp.h" #include "ipc_register_serviceinfo_req.h" +#include "ipc_register_service_info_new_req.h" #include "ipc_set_credential_req.h" #include "ipc_set_credential_rsp.h" #include "ipc_set_local_device_name_req.h" #include "ipc_set_remote_device_name_req.h" #include "ipc_set_useroperation_req.h" #include "ipc_skeleton.h" +#include "ipc_start_service_discovery_req.h" #include "ipc_sync_callback_req.h" +#include "ipc_start_publish_service_req.h" +#include "ipc_stop_publish_service_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_unbind_device_req.h" +#include "ipc_unbind_service_target_req.h" #include "ipc_unpublish_req.h" +#include "ipc_unregister_service_info_req.h" #include "securec.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "ipc_model_codec.h" @@ -2996,5 +3003,252 @@ bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const Dm anonyLocalUdid_); return result; } + +int32_t DeviceManagerImpl::StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam, + std::shared_ptr callback) +{ + if (pkgName.empty() || callback == nullptr || discParam.serviceType.empty() || discParam.discoveryServiceId == 0) { + LOGE("error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + DeviceManagerNotify::GetInstance().RegisterServiceDiscoveryCallback(discParam.discoveryServiceId, callback); + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetDiscParam(discParam); + int32_t ret = ipcClientProxy_->SendRequest(START_SERVICE_DISCOVERING, req, rsp); + if (ret != DM_OK) { + LOGE("StartServiceDiscovery error: Send Request failed ret: %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterServiceDiscoveryCallback(discParam.discoveryServiceId); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("StartServiceDiscovery error: Failed with ret %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterServiceDiscoveryCallback(discParam.discoveryServiceId); + return ret; + } + + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::StopServiceDiscovery(const std::string &pkgName, int32_t discoveryServiceId) +{ + if (pkgName.empty() || discoveryServiceId == 0) { + LOGE("error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetInt32Param(discoveryServiceId); + int32_t ret = ipcClientProxy_->SendRequest(STOP_SERVICE_DISCOVERING, req, rsp); + if (ret != DM_OK) { + LOGE("StopServiceDiscovery error: Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("StopServiceDiscovery error: Failed with ret %{public}d :", ret); + return ret; + } + DeviceManagerNotify::GetInstance().UnRegisterServiceDiscoveryCallback(discoveryServiceId); + LOGI("StopServiceDiscovery completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + std::map &bindParam, std::shared_ptr callback) +{ + if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) { + LOGE("DeviceManagerImpl::BindServiceTarget failed: input pkgName or targetId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::string bindParamStr = ConvertMapToJsonString(bindParam); + DeviceManagerNotify::GetInstance().RegisterBindCallback(pkgName, targetId, callback); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetPeerTargetId(targetId); + req->SetBindParam(bindParamStr); + int32_t ret = ipcClientProxy_->SendRequest(BIND_SERVICE_TARGET, req, rsp); + if (ret != DM_OK) { + LOGE("BindServiceTarget error: Send Request failed ret: %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterBindCallback(pkgName, targetId); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("BindServiceTarget error: Failed with ret %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterBindCallback(pkgName, targetId); + return ret; + } + + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) +{ + if (pkgName.empty() || serviceId == 0) { + LOGE("UnbindServiceTarget failed: input pkgName or serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetServiceId(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(UNBIND_SERVICE_TARGET, req, rsp); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget error: Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget error: Failed with ret %{public}d", ret); + return ret; + } + + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId, + std::shared_ptr callback) +{ + if (pkgName.empty() || serviceId == 0) { + LOGE("RegisterServiceStateCallback failed: input pkgName or serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::string key = std::to_string(serviceId); + int32_t ret = DeviceManagerNotify::GetInstance().RegisterServiceStateCallback(key, callback); + if (ret != DM_OK) { + LOGE("error: register callback failed: %{public}d", ret); + return ret; + } + SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) +{ + if (pkgName.empty() || serviceId == 0) { + LOGE("UnRegisterServiceStateCallback failed: input pkgName or serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::string key = std::to_string(serviceId); + int32_t ret = DeviceManagerNotify::GetInstance().UnRegisterServiceStateCallback(key); + if (ret != DM_OK) { + LOGE("error: unregister callback failed: %{public}d", ret); + return ret; + } + SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_STATE, pkgName); + return DM_OK; +} + +int32_t DeviceManagerImpl::StartPublishService(const std::string &pkgName, + const PublishServiceParam &publishServiceParam, + std::shared_ptr callback, int64_t &serviceId) +{ + if (pkgName.empty() || callback == nullptr) { + LOGE("DeviceManagerImpl::StartPublishService error: pkgName or callback is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetPublishServiceParam(publishServiceParam); + req->SetServiceId(serviceId); + 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; + } + serviceId = rsp->GetServiceId(); + DeviceManagerNotify::GetInstance().RegisterServicePublishCallback(serviceId, callback); + LOGI("StartPublishService End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::StopPublishService(int64_t serviceId) +{ + if (serviceId == 0) { + LOGE("DeviceManagerImpl::StopPublishService error: service id is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::string key = std::to_string(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; + } + DeviceManagerNotify::GetInstance().UnRegisterServicePublishCallback(serviceId); + LOGI("StopPublishService End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterServiceInfo(const ServiceRegInfo &serviceInfo, int32_t ®ServiceId) +{ + if (serviceInfo.serviceInfo.serviceType.empty() || serviceInfo.serviceInfo.serviceName.empty() || + serviceInfo.serviceInfo.serviceDisplayName.empty()) { + LOGE("DeviceManagerImpl::RegisterServiceInfo error: service name or type or display name is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceRegInfo(serviceInfo); + req->SetRegServiceId(regServiceId); + 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; + } + regServiceId = rsp->GetRegServiceId(); + LOGI("End"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceInfo(int32_t regServiceId) +{ + if (regServiceId == 0) { + LOGE("DeviceManagerImpl::UnRegisterServiceInfo error: regServiceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetRegServiceId(regServiceId); + 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; +} } // 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..a4fca6ee374a6fc2f5c55dfc625fdf86c252ff93 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 @@ -56,6 +56,7 @@ #include "ipc_put_device_profile_info_list_req.h" #include "ipc_register_listener_req.h" #include "ipc_register_serviceinfo_req.h" +#include "ipc_register_service_info_new_req.h" #include "ipc_req.h" #include "ipc_rsp.h" #include "ipc_set_credential_req.h" @@ -63,12 +64,18 @@ #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_service_discovery_req.h" #include "ipc_sync_callback_req.h" +#include "ipc_start_publish_service_req.h" +#include "ipc_stop_publish_service_req.h" #include "ipc_permission_req.h" #include "ipc_publish_req.h" +#include "ipc_publish_service_info_rsp.h" #include "ipc_unbind_device_req.h" +#include "ipc_unbind_service_target_req.h" #include "ipc_unpublish_req.h" #include "ipc_unauthenticate_device_req.h" +#include "ipc_unregister_service_info_req.h" #include "securec.h" namespace OHOS { class IRemoteObject; } @@ -461,11 +468,14 @@ ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply DmDeviceBasicInfo dmDeviceBasicInfo; IpcModelCodec::DecodeDmDeviceBasicInfo(data, dmDeviceBasicInfo); + std::vector serviceIds; + IpcModelCodec::DecodeServiceIds(serviceIds, data); switch (deviceState) { case DEVICE_STATE_ONLINE: LOGI("Online pkgName:%{public}s", pkgName.c_str()); DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, dmDeviceInfo); DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, dmDeviceBasicInfo); + DeviceManagerNotify::GetInstance().OnServiceOnline(serviceIds); break; case DEVICE_STATE_OFFLINE: LOGI("Offline pkgName:%{public}s", pkgName.c_str()); @@ -2217,5 +2227,238 @@ 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_SERVICE_DISCOVERING, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); + if (!data.WriteString(pReq->GetPkgName())) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + DiscoveryServiceParam discParam = pReq->GetDiscParam(); + if (!IpcModelCodec::EncodeSrvDiscParam(discParam, data)) { + LOGE("write GetServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(START_SERVICE_DISCOVERING, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(STOP_SERVICE_DISCOVERING, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + if (!data.WriteString(pReq->GetPkgName())) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + int32_t discServiceId = pReq->GetInt32Param(); + if (!data.WriteInt32(discServiceId)) { + LOGE("write discServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(STOP_SERVICE_DISCOVERING, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_CMD(NOTIFY_SERVICE_FOUND, MessageParcel &data, MessageParcel &reply) +{ + int32_t discoveryServiceId = data.ReadInt32(); + DiscoveryServiceInfo discServiceInfo; + if (!IpcModelCodec::DecodeSrvDiscServiceInfo(data, discServiceInfo)) { + LOGE("DecodeSrvDiscServiceInfo failed"); + return ERR_DM_FAILED; + } + DeviceManagerNotify::GetInstance().OnServiceFound(discoveryServiceId, discServiceInfo); + + reply.WriteInt32(DM_OK); + return DM_OK; +} + +ON_IPC_CMD(NOTIFY_SERVICE_DISCOVERY_RESULT, MessageParcel &data, MessageParcel &reply) +{ + int32_t reason = data.ReadInt32(); + int32_t discoveryServiceId = data.ReadInt32(); + DeviceManagerNotify::GetInstance().OnServiceDiscoveryResult(discoveryServiceId, reason); + reply.WriteInt32(DM_OK); + return DM_OK; +} + +ON_IPC_SET_REQUEST(BIND_SERVICE_TARGET, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + PeerTargetId targetId = pReq->GetPeerTargetId(); + std::string bindParam = pReq->GetBindParam(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!IpcModelCodec::EncodePeerTargetId(targetId, data)) { + LOGE("write peer target id failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(bindParam)) { + LOGE("write bind parameter string failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(BIND_SERVICE_TARGET, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNBIND_SERVICE_TARGET, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + int64_t serviceId = pReq->GetServiceId(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt64(serviceId)) { + LOGE("write service id failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNBIND_SERVICE_TARGET, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_CMD(SERVICE_PUBLISH_RESULT, MessageParcel &data, MessageParcel &reply) +{ + int64_t serviceId = data.ReadInt64(); + int32_t publishResult = data.ReadInt32(); + DeviceManagerNotify::GetInstance().OnServicePublishResult(serviceId, publishResult); + reply.WriteInt32(DM_OK); + return DM_OK; +} + +ON_IPC_SET_REQUEST(START_PUBLISH_SERVICE, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + PublishServiceParam param = pReq->GetPublishServiceParam(); + if (!IpcModelCodec::EncodePublishServiceParam(param, data)) { + LOGE("write publishServiceParam failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + int64_t serviceId = pReq->GetServiceId(); + if (!data.WriteInt64(serviceId)) { + LOGE("write GetServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + std::string pkgName = pReq->GetPkgName(); + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(START_PUBLISH_SERVICE, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + pRsp->SetServiceId(reply.ReadInt64()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(STOP_PUBLISH_SERVICE, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, 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) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(REGISTER_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); + const ServiceRegInfo serviceRegInfo = pReq->GetServiceRegInfo(); + if (!IpcModelCodec::EncodeServiceRegInfo(serviceRegInfo, data)) { + LOGE("write serviceRegInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + int32_t regServiceId = pReq->GetRegServiceId(); + if (!data.WriteInt32(regServiceId)) { + LOGE("write regServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + pRsp->SetRegServiceId(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int32_t regServiceId = pReq->GetRegServiceId(); + if (!data.WriteInt32(regServiceId)) { + LOGE("write regServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/authentication_v2/dm_auth_message_processor.h b/services/implementation/include/authentication_v2/dm_auth_message_processor.h index 98144d108a6ab5aea249a730f24eec27cc4d7a19..78706f4a32403b70d50744a294b8786e8bed099e 100644 --- a/services/implementation/include/authentication_v2/dm_auth_message_processor.h +++ b/services/implementation/include/authentication_v2/dm_auth_message_processor.h @@ -357,6 +357,12 @@ private: void ParseUltrasonicSide(const JsonObject &jsonObject, std::shared_ptr context); void ParseCert(const JsonObject &jsonObject, std::shared_ptr context); void ParseAccesserInfo(const JsonObject &jsonObject, std::shared_ptr context); + void ParseServiceNego(const JsonObject &jsonObject, std::shared_ptr context); + void ParseSyncServiceInfo(const JsonObject &jsonObject, std::shared_ptr context); + + int32_t SetSyncMsgJson(std::shared_ptr &context, const DmAccess &accessSide, + const DmAccessToSync &accessToSync, JsonObject &syncMsgJson); + std::string GetAccesseeServiceInfo(int64_t serviceId); std::shared_ptr cryptoMgr_ = nullptr; std::unordered_map createMessageFuncMap_; std::unordered_map paraseMessageFuncMap_; diff --git a/services/implementation/include/authentication_v2/dm_auth_state.h b/services/implementation/include/authentication_v2/dm_auth_state.h index 4a241b935c0c3498ef23ee48a9157a6248f2d1a5..fa060f7d78cfa7f19468b44c311a6354920e1b9f 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state.h +++ b/services/implementation/include/authentication_v2/dm_auth_state.h @@ -553,6 +553,7 @@ private: bool IsAntiDisturbanceMode(const std::string &businessId); bool ParseAndCheckAntiDisturbanceMode(const std::string &businessId, const std::string &businessValue); void SetIsProxyBind(std::shared_ptr context); + int32_t SinkNegotiateService(std::shared_ptr context); }; class AuthSinkDataSyncState : public DmAuthState { diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 13759a00d3000ce20f2c6c065eac5f6ddbe2e3de..e4f7686b9650bc895a691bbd1cb022503165daea 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 BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); + int32_t UnbindServiceTarget(const std::string &pkgName, int64_t serviceId); void InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, const std::string &deviceUdidHash); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); @@ -304,6 +307,8 @@ private: const std::string &deviceId, int32_t reason, uint64_t tokenId); void GetBundleName(const DMAclQuadInfo &info, std::set &pkgNameSet); void DeleteSessionKey(int32_t userId, const DistributedDeviceProfile::AccessControlProfile &profile); + int32_t DeleteAclExtraDataServiceId(int64_t serviceId, int64_t tokenIdCaller, std::string &udid, + int32_t &bindLevel); private: std::mutex authMgrMtx_; std::shared_ptr authMgr_; // Old protocol only diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp index dcdb29027852f062fc13e2a7e945ac9439613596..424e006c1a4024ce11dcf9a9dfd09232371aa5be 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -53,6 +53,7 @@ namespace { const char* DM_ANTI_DISTURBANCE_MODE = "is_in_anti_disturbance_mode"; const int64_t DM_MIN_RANDOM = 1; const int64_t DM_MAX_RANDOM_INT64 = INT64_MAX; + constexpr int8_t SERVICE_UNPUBLISHED_STATE = 0; } DmAuthStateType AuthSrcStartState::GetStateType() @@ -209,6 +210,31 @@ int32_t AuthSinkNegotiateStateMachine::ProcRespNegotiate5_1_0(std::shared_ptraccessee.credTypeList = credTypeJson.Dump(); + if (context->isServiceBind) { + ret = SinkNegotiateService(context); + return ret; + } + return DM_OK; +} + +int32_t AuthSinkNegotiateStateMachine::SinkNegotiateService(std::shared_ptr context) +{ + CHECK_NULL_RETURN(context, ERR_DM_POINT_NULL); + int64_t serviceId = context.accessee.serviceId; + if (serviceId == 0) { + LOGE("service id invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + ServiceInfoProfile serviceInfoProfile; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId); + if (ret != DM_OK) { + LOGE("GetServiceInfoProfileByServiceId failed, ret %{public}d.", ret); + return ret; + } + if (serviceInfoProfile.publishState == SERVICE_UNPUBLISHED_STATE) { + LOGE("service id not publish."); + return ERR_DM_SERVICE_BIND_PEER_SERVICE_ID_UNPUBLISH; + } return DM_OK; } diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index 43f367ea373d73d62bd35b0dd2f81116ac354050..a3fb91cce54c9f6f3d6aff7dd950ad7a8538e9bc 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -105,6 +105,14 @@ const char* TAG_LANGUAGE = "language"; const char* TAG_ULTRASONIC_SIDE = "ultrasonicSide"; const char* TAG_REMAINING_FROZEN_TIME = "remainingFrozenTime"; const char* TAG_IS_SUPPORT_ULTRASONIC = "isSupportUltrasonic"; +const char* TAG_SERVICE_ID = "serviceId"; +const char* TAG_ACCESSEE_SERVICE_INFO = "accesseeServiceInfo"; +const char* TAG_REG_SERVICE_ID = "regServiceId"; +const char* TAG_DEVICE_ID = "deviceId"; +const char* TAG_PUBLISH_STATE = "publishState"; +const char* TAG_SERVICE_TYPE = "serviceType"; +const char* TAG_SERVICE_NAME = "serviceName"; +const char* TAG_SERVICE_DISPLAY_NAME = "serviceDisplayName"; constexpr const char* TAG_CUSTOM_DESCRIPTION = "CUSTOMDESC"; @@ -362,6 +370,7 @@ int32_t DmAuthMessageProcessor::PutAccessControlList(std::shared_ptrIsProxyBind || context->subjectProxyOnes.empty() || (context->IsCallingProxyAsSubject && !isAuthed)) { std::string isLnnAclFalse = std::string(ACL_IS_LNN_ACL_VAL_FALSE); extraData[ACL_IS_LNN_ACL_KEY] = isLnnAclFalse; + extraData[TAG_SERVICE_ID] = access.serviceId; profile.SetExtraData(extraData.Dump()); profile.SetBindLevel(access.bindLevel); SetTransmitAccessControlList(context, accesser, accessee); @@ -900,6 +910,8 @@ int32_t DmAuthMessageProcessor::CreateNegotiateMessage(std::shared_ptraccessee.displayId; jsonObject[TAG_PEER_PKG_NAME] = context->accessee.pkgName; jsonObject[TAG_HOST_PKGLABEL] = context->pkgLabel; + jsonObject[TAG_SERVICE_ID] = context->accessee.serviceId; + jsonObject[PARAM_KEY_IS_SERVICE_BIND] = context->isServiceBind; if (!context->businessId.empty()) { jsonObject[DM_BUSINESS_ID] = context->businessId; @@ -1202,9 +1214,53 @@ int32_t DmAuthMessageProcessor::ParseSyncMessage(std::shared_ptr } } ParseCert(jsonObject, context); + ParseSyncServiceInfo(jsonObject, context); return DM_OK; } +void DmAuthMessageProcessor::ParseSyncServiceInfo(const JsonObject &jsonObject, + std::shared_ptr context) +{ + std::string serviceInfoStr = ""; + if (IsString(jsonObject, TAG_ACCESSEE_SERVICE_INFO)) { + serviceInfoStr = jsonObject[TAG_ACCESSEE_SERVICE_INFO].Get(); + } + JsonObject json; + json.Parse(serviceInfoStr); + if (json.IsDiscarded()) { + return; + } + ServiceInfoProfile serviceInfoProfile; + if (IsInt32(json, TAG_REG_SERVICE_ID)) { + serviceInfoProfile.regServiceId = json[TAG_REG_SERVICE_ID].Get(); + } + if (IsString(json, TAG_DEVICE_ID)) { + serviceInfoProfile.deviceId = json[TAG_DEVICE_ID].Get(); + } + if (IsInt32(json, TAG_USER_ID)) { + serviceInfoProfile.userId = json[TAG_USER_ID].Get(); + } + if (IsInt64(json, TAG_TOKEN_ID)) { + serviceInfoProfile.tokenId = json[TAG_TOKEN_ID].Get(); + } + if (IsInt32(json, TAG_PUBLISH_STATE)) { + serviceInfoProfile.publishState = json[TAG_PUBLISH_STATE].Get(); + } + if (IsInt64(json, TAG_SERVICE_ID)) { + serviceInfoProfile.serviceId = json[TAG_SERVICE_ID].Get(); + } + if (IsString(json, TAG_SERVICE_TYPE)) { + serviceInfoProfile.serviceType = json[TAG_SERVICE_TYPE].Get(); + } + if (IsString(json, TAG_SERVICE_NAME)) { + serviceInfoProfile.serviceName = json[TAG_SERVICE_NAME].Get(); + } + if (IsString(json, TAG_SERVICE_DISPLAY_NAME)) { + serviceInfoProfile.serviceDisplayName = json[TAG_SERVICE_DISPLAY_NAME].Get(); + } + DeviceProfileConnector::GetInstance().PutServiceInfoProfile(serviceInfoProfile); +} + int32_t DmAuthMessageProcessor::ParseProxyAccessToSync(std::shared_ptr &context, JsonObject &jsonObject) { @@ -1411,10 +1467,26 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( ParseAccesserInfo(jsonObject, context); ParseUltrasonicSide(jsonObject, context); ParseProxyNegotiateMessage(jsonObject, context); + ParseServiceNego(jsonObject, context); context->authStateMachine->TransitionTo(std::make_shared()); return DM_OK; } +void DmAuthMessageProcessor::ParseServiceNego(const JsonObject &jsonObject, + std::shared_ptr context) +{ + context->isServiceBind = false; + if (IsBool(jsonObject, PARAM_KEY_IS_SERVICE_BIND)) { + context->isServiceBind = jsonObject[PARAM_KEY_IS_SERVICE_BIND].Get(); + } + if (!context->isServiceBind) { + return; + } + if (IsInt64(jsonObject, TAG_SERVICE_ID)) { + context->accessee.serviceId = jsonObject[TAG_SERVICE_ID].Get(); + } +} + void DmAuthMessageProcessor::ParseAccesserInfo(const JsonObject &jsonObject, std::shared_ptr context) { @@ -2008,6 +2080,27 @@ int32_t DmAuthMessageProcessor::EncryptSyncMessage(std::shared_ptrEncryptMessage(plainJson.Dump(), encSyncMsg); +} + +int32_t DmAuthMessageProcessor::SetSyncMsgJson(std::shared_ptr &context, const DmAccess &accessSide, + const DmAccessToSync &accessToSync, JsonObject &syncMsgJson) +{ syncMsgJson[TAG_TRANSMIT_SK_ID] = std::to_string(accessSide.transmitSessionKeyId); syncMsgJson[TAG_TRANSMIT_SK_TIMESTAMP] = accessSide.transmitSkTimeStamp; syncMsgJson[TAG_TRANSMIT_CREDENTIAL_ID] = accessSide.transmitCredentialId; @@ -2042,17 +2135,33 @@ int32_t DmAuthMessageProcessor::EncryptSyncMessage(std::shared_ptraccesser.isCommonFlag; syncMsgJson[TAG_DM_CERT_CHAIN] = context->accesser.cert; - CreateProxyAccessMessage(context, syncMsgJson); - std::string syncMsg = syncMsgJson.Dump(); - std::string compressMsg = CompressSyncMsg(syncMsg); - if (compressMsg.empty()) { - LOGE("DmAuthMessageProcessor::EncryptSyncMessage compress failed"); - return ERR_DM_FAILED; + if (context->isServiceBind) { + std::string serviceInfo = GetAccesseeServiceInfo(context->accessee.serviceId); + syncMsgJson[TAG_ACCESSEE_SERVICE_INFO] = serviceInfo; } - JsonObject plainJson; - plainJson[TAG_COMPRESS_ORI_LEN] = syncMsg.size(); - plainJson[TAG_COMPRESS] = Base64Encode(compressMsg); - return cryptoMgr_->EncryptMessage(plainJson.Dump(), encSyncMsg); + return DM_OK; +} + +std::string DmAuthMessageProcessor::GetAccesseeServiceInfo(int64_t serviceId) +{ + ServiceInfoProfile serviceInfoProfile; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, + serviceInfoProfile); + if (ret != DM_OK) { + LOGE("GetAccesseeServiceInfo GetServiceInfoProfileByServiceId failed."); + return ""; + } + JsonObject json; + json[TAG_REG_SERVICE_ID] = serviceInfoProfile.regServiceId; + json[TAG_DEVICE_ID] = serviceInfoProfile.deviceId; + json[TAG_USER_ID] = serviceInfoProfile.userId; + json[TAG_TOKEN_ID] = serviceInfoProfile.tokenId; + json[TAG_PUBLISH_STATE] = serviceInfoProfile.publishState; + json[TAG_SERVICE_ID] = serviceInfoProfile.serviceId; + json[TAG_SERVICE_TYPE] = serviceInfoProfile.serviceType; + json[TAG_SERVICE_NAME] = serviceInfoProfile.serviceName; + json[TAG_SERVICE_DISPLAY_NAME] = serviceInfoProfile.serviceDisplayName; + return json.Dump(); } int32_t DmAuthMessageProcessor::CreateProxyAccessMessage(std::shared_ptr &context, diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 54f5608cbaa13a383e71b74747e6f848bb0a7f92..0daf168e17313f3e59813ed6fb2185c854299e33 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -23,6 +23,7 @@ #include "app_manager.h" #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" +#include "distributed_device_profile_client.h" #include "dm_error_type.h" #include "dm_anonymous.h" #include "dm_constants.h" @@ -71,6 +72,7 @@ constexpr const char* BIND_CALLER_USERID = "bindCallerUserId"; const char* IS_NEED_JOIN_LNN = "IsNeedJoinLnn"; constexpr const char* NEED_JOIN_LNN = "0"; constexpr const char* NO_NEED_JOIN_LNN = "1"; +constexpr const char* TAG_SERVICE_ID = "serviceId"; // currently, we just support one bind session in one device at same time constexpr size_t MAX_NEW_PROC_SESSION_COUNT_TEMP = 1; const int32_t USLEEP_TIME_US_500000 = 500000; // 500ms @@ -1609,6 +1611,16 @@ static bool IsHmlSessionType(const JsonObject &jsonObject) int DeviceManagerServiceImpl::OpenAuthSession(const std::string& deviceId, const std::map &bindParam) { + if (bindParam.find(PARAM_KEY_IS_SERVICE_BIND) != bindParam.end() && + bindParam.at(PARAM_KEY_IS_SERVICE_BIND) == DM_VAL_TRUE) { + CHECK_NULL_RETURN(listener_, ERR_DM_FAILED); + if (IsNumberString(deviceId)) { + return listener_->OpenAuthSessionWithPara(std::stoll(deviceId)); + } else { + LOGE("OpenAuthSession failed"); + return ERR_DM_FAILED; + } + } bool hmlEnable160M = false; int32_t hmlActionId = 0; int invalidSessionId = -1; @@ -1782,6 +1794,10 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, LOGE("GetDeviceInfo failed, ret: %{public}d", ret); } deviceInfo->addrNum = static_cast(index); + if (bindParam.find(PARAM_KEY_IS_SERVICE_BIND) != bindParam.end() && + bindParam.at(PARAM_KEY_IS_SERVICE_BIND) == DM_VAL_TRUE) { + deviceId = std::to_string(targetId.serviceId); + } if (softbusConnector_->AddMemberToDiscoverMap(deviceId, deviceInfo) != DM_OK) { LOGE("DeviceManagerServiceImpl::ParseConnectAddr failed, AddMemberToDiscoverMap failed."); return ERR_DM_INPUT_PARA_INVALID; @@ -3204,6 +3220,97 @@ void DeviceManagerServiceImpl::DeleteSessionKey(int32_t userId, DeviceProfileConnector::GetInstance().DeleteSessionKey(userId, skId); } +int32_t DeviceManagerServiceImpl::BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (pkgName.empty()) { + LOGE("BindServiceTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::map bindParamTmp = bindParam; + bindParamTmp[PARAM_KEY_IS_SERVICE_BIND] = "true"; + return BindTarget(pkgName, targetId, bindParamTmp); +} + +int32_t DeviceManagerServiceImpl::UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) +{ + if (pkgName.empty()) { + LOGE("UnbindServiceTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + std::string peerDeviceId = ""; + int32_t bindLevel = -1; + int32_t ret = DeleteAclExtraDataServiceId(serviceId, tokenIdCaller, peerDeviceId, bindLevel); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, DeleteAclExtraDataServiceId failed."); + return ret; + } + + ServiceInfoProfile serviceInfoProfile; + int32_t userId = -1; + MultipleUserConnector::GetCallerUserId(userId); + ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, GetServiceInfoProfileByServiceId failed."); + return ret; + } + ret = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(serviceInfoProfile.regServiceId, userId); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, DeleteServiceInfoProfile failed."); + return ret; + } + ret = UnBindDevice(pkgName, peerDeviceId, bindLevel); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, UnBindDevice failed."); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::DeleteAclExtraDataServiceId(int64_t serviceId, int64_t tokenIdCaller, + std::string &udid, int32_t &bindLevel) +{ + bool isDeletedExtra = false; + char localDeviceIdTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceIdTemp, DEVICE_UUID_LENGTH); + std::string localDeviceId = std::string(localDeviceIdTemp); + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAllAclIncludeLnnAcl(); + for (auto &item : profiles) { + std::string extraData = item.GetExtraData(); + if (extraData.empty()) { + continue; + } + JsonObject json; + json.Parse(extraData); + if (json.IsDiscarded()) { + continue; + } + if (IsInt64(json, TAG_SERVICE_ID)) { + int64_t aclServiceId = json[TAG_SERVICE_ID].Get(); + if (aclServiceId != serviceId) { + continue; + } + } + std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); + int64_t tokenIdAcl = item.GetAccesser().GetAccesserTokenId(); + if (accesserUdid == localDeviceId && tokenIdCaller == tokenIdAcl) { + isDeletedExtra = true; + json.Erase(TAG_SERVICE_ID); + item.SetExtraData(json.Dump()); + udid = item.GetAccessee().GetAccesseeDeviceId(); + bindLevel = item.GetBindLevel(); + DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); + } + } + if (!isDeletedExtra) { + LOGE("DeleteAclExtraDataServiceId failed, local is sink, not allow unbind."); + return ERR_DM_INPUT_PARA_INVALID; + } + return DM_OK; +} + void DeviceManagerServiceImpl::InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, const std::string &deviceUdidHash) { diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index 7e0db378bd1aae0d6b219f10dd5cb5cee7397034..fbbb634b4fe6cbb942a3f0cd43285c56d405bb42 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -175,10 +175,31 @@ void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState LOGI("begin, devState = %{public}d networkId: %{public}s.", devState, GetAnonyString(devInfo.networkId).c_str()); CHECK_NULL_VOID(listener_); + std::vector remoteTokenIds; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid(localDeviceId); + std::string udid = softbusConnector_->GetDeviceUdidByUdidHash(devInfo.deviceId); + DeviceProfileConnector::GetInstance().GetRemoteTokenIds(localUdid, udid, remoteTokenIds); + std::vector remoteServiceIds; + for (const auto &item : remoteTokenIds) { + ServiceInfoProfile serviceInfo; + if (DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(item, serviceInfo) != DM_OK) { + LOGE("GetServiceInfoByTokenid failed."); + continue; + } + remoteServiceIds.push_back(serviceInfo.serviceId); + } + LOGI("ProcessDeviceStateChange, remoteServiceIds size: %{public}zu", remoteServiceIds.size()); + for (const auto &item : processInfoVec) { if (!item.pkgName.empty()) { LOGI("ProcessDeviceStateChange, pkgName = %{public}s", item.pkgName.c_str()); - listener_->OnDeviceStateChange(item, devState, devInfo); + if (!remoteServiceIds.empty() && devState == DEVICE_STATE_ONLINE) { + listener_->OnDeviceStateChange(item, devState, devInfo, remoteServiceIds); + } else { + listener_->OnDeviceStateChange(item, devState, devInfo); + } } } } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index b285a2d706b89b18e52f0504fdd257a9e3c6798c..bfdc8878c5ce598d6d16902ba38b983f722872a7 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -264,6 +264,7 @@ public: std::vector &networkIds); void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m); + int32_t OpenAuthSessionWithPara(int64_t serviceId); int32_t UnRegisterPinHolderCallback(const std::string &pkgName); void ProcessReceiveRspAppUninstall(const std::string &remoteUdid); void ProcessReceiveRspAppUnbind(const std::string &remoteUdid); @@ -275,6 +276,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 StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam); + int32_t StopServiceDiscovery(const std::string &pkgName, int32_t discServiceId); + int32_t BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); + int32_t UnbindServiceTarget(const std::string &pkgName, int64_t serviceId); + int32_t RegisterServiceInfo(const ServiceRegInfo &serviceRegInfo, int32_t ®ServiceId); + int32_t UnRegisterServiceInfo(int32_t regServiceId); + int32_t StartPublishService(const std::string &pkgName, + PublishServiceParam &publishServiceParam, int64_t &serviceId); + int32_t StopPublishService(int64_t serviceId); private: bool IsDMServiceImplReady(); bool IsDMImplSoLoaded(); @@ -418,6 +429,11 @@ private: void GetLocalUserIdFromDataBase(std::vector &foregroundUsers, std::vector &backgroundUsers); void PutLocalUserIdToDataBase(const std::vector &foregroundUsers, const std::vector &backgroundUsers); + int32_t UpdateServiceInfo(int64_t serviceId); + int32_t GenerateServiceId(int64_t &serviceId); + int32_t ConvertServiceInfoProfileByRegInfo(const ServiceRegInfo &serviceRegInfo, + ServiceInfoProfile &serviceInfoProfile); + int32_t GenerateRegServiceId(int32_t ®ServiceId); private: bool isImplsoLoaded_ = false; diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 08830f86f96369739066fecdcbad7e238870ba7f..0fb7d04b62915dd2c3799a605c6fec5fc07fb59b 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 BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) = 0; + virtual int32_t UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) = 0; virtual void InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, const std::string &deviceUdidHash) = 0; }; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 504ce2663d6e5b9599e9fecdef586febcfe6b5e3..65c51e53896ad1949a9e04c2e871a924da5e7fd9 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -18,9 +18,12 @@ #include "cJSON.h" #include #include +#include #include "app_manager.h" #include "dm_constants.h" #include "dm_crypto.h" +#include "dm_device_info.h" +#include "distributed_device_profile_client.h" #include "dm_hidumper.h" #include "dm_softbus_cache.h" #include "parameter.h" @@ -101,6 +104,9 @@ namespace { constexpr const char* LOCAL_ALL_USERID = "local_all_userId"; constexpr const char* LOCAL_FOREGROUND_USERID = "local_foreground_userId"; constexpr const char* LOCAL_BACKGROUND_USERID = "local_background_userId"; + constexpr int32_t GENERATE_SERVICE_ID_RETRY_TIME = 3; + constexpr int32_t SERVICE_UNPUBLISHED_STATE = 0; + constexpr int32_t SERVICE_PUBLISHED_STATE = 1; } DeviceManagerService::~DeviceManagerService() @@ -4195,6 +4201,15 @@ int32_t DeviceManagerService::OpenAuthSessionWithPara(const std::string &deviceI return dmServiceImplExtResident_->OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); } +int32_t DeviceManagerService::OpenAuthSessionWithPara(int64_t serviceId) +{ + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + return dmServiceImplExtResident_->OpenAuthSessionWithPara(serviceId); +} + int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkgName) { if (!PermissionManager::GetInstance().CheckPermission()) { @@ -4414,5 +4429,331 @@ void DeviceManagerService::InitTaskOfDelTimeOutAcl() } } #endif + +int32_t DeviceManagerService::StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || discParam.serviceType.empty() || discParam.discoveryServiceId == 0) { + LOGE("error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + ProcessInfo processInfo; + processInfo.pkgName = pkgName; + MultipleUserConnector::GetCallerUserId(processInfo.userId); + return dmServiceImplExtResident_->StartServiceDiscovery(processInfo, discParam); +} + +int32_t DeviceManagerService::StopServiceDiscovery(const std::string &pkgName, int32_t discServiceId) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || discServiceId == 0) { + LOGE("error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + return dmServiceImplExtResident_->StopServiceDiscovery(discServiceId); +} + +int32_t DeviceManagerService::BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("Start for pkgName = %{public}s", pkgName.c_str()); + if (targetId.serviceId == 0) { + LOGE("Invalid parameter, service id is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceImplReady()) { + LOGE("BindServiceTarget failed, DMServiceImpl instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->BindServiceTarget(pkgName, targetId, bindParam); +} + +int32_t DeviceManagerService::UnbindServiceTarget(const std::string &pkgName, int64_t serviceId) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME) || serviceId == 0) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("Start for pkgName = %{public}s", pkgName.c_str()); + if (!IsDMServiceImplReady()) { + LOGE("UnbindServiceTarget failed, DMServiceImpl instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->UnbindServiceTarget(pkgName, serviceId); +} + +int32_t DeviceManagerService::RegisterServiceInfo(const ServiceRegInfo &serviceRegInfo, int32_t ®ServiceId) +{ +#ifdef DEVICE_MANAGER_COMMON_FLAG + LOGI("RegisterServiceInfo not support in common version."); + return ERR_DM_UNSUPPORTED_METHOD; +#else + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The callerdoes not have permission to cal1 RegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + if (serviceRegInfo.serviceInfo.serviceType.empty() || serviceRegInfo.serviceInfo.serviceName.empty() || + serviceRegInfo.serviceInfo.serviceDisplayName.empty() || regServiceId == 0) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + ServiceInfoProfile serviceInfoProfile; + int32_t ret = GenerateRegServiceId(serviceInfoProfile.regServiceId); + if (ret != DM_OK) { + LOGE("Failed to generate regServiceId, ret: %{public}d.", ret); + return ret; + } + ret = ConvertServiceInfoProfileByRegInfo(serviceRegInfo, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("ConvertServiceInfoProfileByRegInfo failed, ret: %{public}d", ret); + return ret; + } + ret = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("PutServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + regServiceId = serviceInfoProfile.regServiceId; + LOGI("RegisterServiceInfo success, regServiceId: %{public}s.", GetAnonyInt32(regServiceId).c_str()); + return DM_OK; +#endif +} + +int32_t DeviceManagerService::UnRegisterServiceInfo(int32_t regServiceId) +{ +#ifdef DEVICE_MANAGER_COMMON_FLAG + LOGI("UnRegisterServiceInfo not support in common version."); + return ERR_DM_UNSUPPORTED_METHOD; +#else + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call UnRegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + if (regServiceId == 0) { + LOGE("Invalid parameter, regServiceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(tonkenIdCaller, serviceInfo); + if (ret != DM_OK || serviceInfo.regServiceId != regServiceId) { + LOGE("Invalid parameter, token id or regService id is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (serviceInfo.publishState == SERVICE_PUBLISHED_STATE) { + ret = StopPublishService(serviceInfo.serviceId); + if (ret != DM_OK) { + LOGE("StopPublishService failed, ret: %{public}d.", ret); + return ret; + } + } + int32_t userId = -1; + MultipleUserConnector::GetCallerUserId(userId); + ret = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(regServiceId, userId); + if (ret != DM_OK) { + LOGE("DeleteServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + LOGI("UnRegisterServiceInfo success."); + return DM_OK; +#endif +} + +int32_t DeviceManagerService::StartPublishService(const std::string &pkgName, + PublishServiceParam &publishServiceParam, int64_t &serviceId) +{ +#ifdef DEVICE_MANAGER_COMMON_FLAG + LOGI("StartPublishService not support in common version."); + return ERR_DM_UNSUPPORTED_METHOD; +#else + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call StantPublishService."); + return ERR_DM_NO_PERMISSION; + } + if (pkgName.empty() || publishServiceParam.regServiceId == 0 || + publishServiceParam.serviceInfo.serviceType.empty() || + publishServiceParam.serviceInfo.serviceName.empty() || + publishServiceParam.serviceInfo.serviceDisplayName.empty()) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(tonkenIdCaller, serviceInfo); + if (ret != DM_OK || serviceInfo.regServiceId != publishServiceParam.regServiceId) { + LOGE("Invalid parameter, token id or regService id is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + ret = GenerateServiceId(publishServiceParam.serviceInfo.serviceId); + if (ret != DM_OK) { + LOGE("GenerateServiceId failed, ret: %{public}d", ret); + return ret; + } + int32_t userId = -1; + MultipleUserConnector::GetCallerUserId(userId); + ServiceInfoProfile serviceInfoProfile = AppManager::GetInstance().CreateServiceInfoProfile(publishServiceParam, + userId); + ret = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("PutServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + serviceId = publishServiceParam.serviceInfo.serviceId; + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("StartPublishService failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + ProcessInfo processInfo; + processInfo.pkgName = pkgName; + processInfo.userId = userId; + return dmServiceImplExtResident_->StartPublishService(processInfo, publishServiceParam); +#endif +} + +int32_t DeviceManagerService::StopPublishService(int64_t serviceId) +{ +#ifdef DEVICE_MANAGER_COMMON_FLAG + LOGI("StopPublishService not support in common version."); + return ERR_DM_UNSUPPORTED_METHOD; +#else + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call1 StopPublishService."); + return ERR_DM_NO_PERMISSION; + } + if (serviceId == 0) { + LOGE("Invalid parameter, serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByServiceId(serviceId, serviceInfo); + if (ret != DM_OK || serviceInfo.publishState == SERVICE_UNPUBLISHED_STATE) { + LOGE("Invalid parameter, serviceId or publish state is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("StopPublishService failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + int32_t ret = dmServiceImplExtResident_->StopPublishService(serviceId); + if (ret != DM_OK) { + LOGE("StopPublishService failed in closed-source logic, ret: %{public}d", ret); + return ret; + } + ret = UpdateServiceInfo(serviceId); + if (ret != DM_OK) { + LOGE("StopPublishService failed in internal logic, ret: %{public}d", ret); + return ret; + } + LOGI("StopPublishService success."); + return DM_OK; +#endif +} + +int32_t DeviceManagerService::UpdateServiceInfo(int64_t serviceId) +{ + LOGI("UpdateServiceInfo Begin"); + ServiceInfoProfile serviceInfoProfile; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("GetServiceInfoProfileByServiceId failed, ret: %{public}d", ret); + return ret; + } + serviceInfoProfile.publishState = SERVICE_UNPUBLISHED_STATE; + ret = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("PutServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + for (auto &profile : profiles) { + JsonObject extraData(profile.GetExtraData()); + if (extraData.IsDiscarded()) { + continue; + } + if (extraData.Contains(SERVICE_ID_KEY) && extraData[SERVICE_ID_KEY].Get() == serviceId) { + extraData.Erase(SERVICE_ID_KEY); + profile.SetExtraData(extraData.Dump()); + DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(profile); + } + } + LOGI("UpdateServiceInfo success"); + return DM_OK; +} + +int32_t DeviceManagerService::GenerateServiceId(int64_t &serviceId) +{ + LOGI("GenerateServiceId Begin."); + for (int i = 0; i < GENERATE_SERVICE_ID_RETRY_TIME; i++) { + serviceId = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID) + ServiceInfoProfile serviceInfoProfile; + ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); + if (ret != DM_OK) { + LOGI("GenerateServiceId success, ret:%{public}d", ret); + return DM_OK; + } + } + LOGE("GenerateServiceId failed, serviceId already exists after retry"); + return ERR_DM_FAILED; +} + +int32_t DeviceManagerService::ConvertServiceInfoProfileByRegInfo( + const ServiceRegInfo &serviceRegInfo, ServiceInfoProfile &serviceInfoProfile) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + serviceInfoProfile.deviceId = std::string(localDeviceId); + MultipleUserConnector::GetCallerUserId(serviceInfoProfile.userId); + serviceInfoProfile.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + serviceInfoProfile.serviceId = serviceRegInfo.serviceInfo.serviceId; + serviceInfoProfile.serviceType = serviceRegInfo.serviceInfo.serviceType; + serviceInfoProfile.serviceName = serviceRegInfo.serviceInfo.serviceName; + serviceInfoProfile.serviceDisplayName = serviceRegInfo.serviceInfo.serviceDisplayName; + return DM_OK; +} + +int32_t DeviceManagerService::GenerateRegServiceId(int32_t ®ServiceId) +{ + LOGI("GenerateRegServiceId Begin."); + unsigned char buffer[16]; + if (RAND_bytes(buffer, sizeof(buffer)) != 1) { + LOGE("Failed to generate random bytes using OpenSSL."); + return ERR_DM_FAILED; + } + regServiceId = 0; + for (size_t i = 0; i < sizeof(int32_t); ++i) { + regServiceId = (regServiceId << 8) | buffer[i]; + } + LOGI("Generated regServiceId: %{public}s", GetAnonyInt32(regServiceId).c_str()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index f917117d4aa2a5fed7ead810c8b06ea515126196..a1b0fabd0bc92627488f0b8ed5330975d44b73ca 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -1044,6 +1044,15 @@ std::string DeviceManagerServiceListener::GetLocalDisplayDeviceName() #endif } +int32_t DeviceManagerServiceListener::OpenAuthSessionWithPara(int64_t serviceId) +{ +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + return DeviceManagerService::GetInstance().OpenAuthSessionWithPara(serviceId); +#else + return ERR_DM_UNSUPPORTED_METHOD; +#endif +} + int32_t DeviceManagerServiceListener::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) { diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index 3db87d9ab62c3529290c1824008b584ba61e68b0..5aa08daa176186f3b62fb774bc4643a19e7c3e87 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -40,10 +40,13 @@ #include "ipc_notify_get_device_profile_info_list_req.h" #include "ipc_notify_publish_result_req.h" #include "ipc_notify_pin_holder_event_req.h" +#include "ipc_notify_service_discover_result_req.h" +#include "ipc_notify_service_found_req.h" #include "ipc_notify_set_local_device_name_req.h" #include "ipc_notify_set_remote_device_name_req.h" #include "ipc_server_client_proxy.h" #include "ipc_server_stub.h" +#include "ipc_service_publish_result_req.h" #include "multiple_user_connector.h" #include "app_manager.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -171,6 +174,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 +185,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) @@ -191,6 +196,7 @@ ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, int32_t deviceState = pReq->GetDeviceState(); DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); DmDeviceBasicInfo deviceBasicInfo = pReq->GetDeviceBasicInfo(); + std::vector serviceIds = pReq->GetServiceIds(); if (!data.WriteString(pkgName)) { LOGE("write pkgName failed"); return ERR_DM_IPC_WRITE_FAILED; @@ -207,6 +213,10 @@ ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, LOGE("write dm device basic info failed"); return ERR_DM_IPC_WRITE_FAILED; } + if (!IpcModelCodec::EncodeServiceIds(serviceIds, data)) { + LOGE("write dm service info failed"); + return ERR_DM_IPC_WRITE_FAILED; + } return DM_OK; } @@ -2007,5 +2017,192 @@ ON_IPC_CMD(CHECK_SINK_SAME_ACCOUNT, MessageParcel &data, MessageParcel &reply) { return OnIpcCmd(CHECK_SINK_SAME_ACCOUNT, data, reply); } + +ON_IPC_CMD(START_SERVICE_DISCOVERING, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + DiscoveryServiceParam discParam; + if (!IpcModelCodec::DecodeSrvDiscParam(data, discParam)) { + LOGE("DecodeSrvDiscParam failed"); + return ERR_DM_FAILED; + } + int32_t result = DeviceManagerService::GetInstance().StartServiceDiscovery(pkgName, discParam); + + if (!reply.WriteInt32(result)) { + LOGE("Write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(STOP_SERVICE_DISCOVERING, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + int32_t discServiceId = data.ReadInt32(); + int32_t result = DeviceManagerService::GetInstance().StopServiceDiscovery(pkgName, discServiceId); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(BIND_SERVICE_TARGET, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + PeerTargetId targetId; + DecodePeerTargetId(data, targetId); + std::string bindParamStr = data.ReadString(); + std::map bindParam; + ParseMapFromJsonString(bindParamStr, bindParam); + int32_t result = DeviceManagerService::GetInstance().BindServiceTarget(pkgName, targetId, bindParam); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(NOTIFY_SERVICE_FOUND, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int32_t discServiceId = pReq->GetDiscServiceId(); + DiscoveryServiceInfo discServiceInfo = pReq->GetDiscServiceInfo(); + if (!data.WriteInt32(discServiceId)) { + LOGE("write errCode failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!IpcModelCodec::EncodeSrvDiscServiceInfo(discServiceInfo, data)) { + LOGE("write discServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(NOTIFY_SERVICE_FOUND, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(NOTIFY_SERVICE_DISCOVERY_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); + int32_t discServiceId = pReq->GetDiscServiceId(); + int32_t result = pReq->GetResult(); + if (!data.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(discServiceId)) { + LOGE("write discServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(NOTIFY_SERVICE_DISCOVERY_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_CMD(UNBIND_SERVICE_TARGET, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + int64_t serviceId = data.ReadInt64(); + int32_t result = DeviceManagerService::GetInstance().UnbindServiceTarget(pkgName, serviceId); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(REGISTER_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + ServiceRegInfo serviceRegInfo; + IpcModelCodec::DecodeServiceRegInfo(data, serviceRegInfo); + int32_t regServiceId = data.ReadInt32(); + int32_t result = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceRegInfo, regServiceId); + if (!reply.WriteInt32(result)) { + LOGE("Failed to write result to reply."); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!reply.WriteInt32(regServiceId)) { + LOGE("Failed to write regServiceId to reply."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(UNREGISTER_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + int32_t regServiceId = data.ReadInt32(); + int32_t result = DeviceManagerService::GetInstance().UnRegisterServiceInfo(regServiceId); + if (!reply.WriteInt32(result)) { + LOGE("Failed to write result to reply."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(START_PUBLISH_SERVICE, MessageParcel &data, MessageParcel &reply) +{ + PublishServiceParam publishServiceParam; + IpcModelCodec::DecodePublishServiceParam(data, publishServiceParam); + int64_t serviceId = data.ReadInt64(); + std::string pkgName = data.ReadString(); + int32_t result = DeviceManagerService::GetInstance().StartPublishService(pkgName, publishServiceParam, serviceId); + if (!reply.WriteInt32(result)) { + LOGE("Write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!reply.WriteInt64(serviceId)) { + LOGE("Failed to write serviceId to reply."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(STOP_PUBLISH_SERVICE, MessageParcel &data, MessageParcel &reply) +{ + int64_t serviceId = data.ReadInt64(); + int32_t result = DeviceManagerService::GetInstance().StopPublishService(serviceId); + if (!reply.WriteInt32(result)) { + LOGE("Write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(SERVICE_PUBLISH_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetServiceId(); + int32_t result = pReq->GetResult(); + if (!data.WriteInt64(serviceId)) { + LOGE("write serviceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(SERVICE_PUBLISH_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS