From 8c26d4c4e8b929453a0d2b192045e34dfe18df2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Wed, 27 Aug 2025 17:44:07 +0800 Subject: [PATCH 1/2] service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../device_manager_ipc_interface_code.h | 11 + common/include/dm_constants.h | 2 + .../ipc/model/ipc_notify_device_state_req.h | 11 + .../ipc_notify_service_discover_result_req.h | 53 +++ .../ipc/model/ipc_notify_service_found_req.h | 53 +++ .../ipc/model/ipc_publish_service_info_rsp.h | 53 +++ .../model/ipc_register_service_info_new_req.h | 53 +++ .../model/ipc_service_publish_result_req.h | 54 +++ .../ipc/model/ipc_start_publish_service_req.h | 55 +++ .../model/ipc_start_service_discovery_req.h | 42 +++ .../ipc/model/ipc_stop_publish_service_req.h | 42 +++ .../ipc/model/ipc_unbind_service_target_req.h | 42 +++ .../model/ipc_unregister_service_info_req.h | 43 +++ common/include/ipc/standard/ipc_model_codec.h | 10 + common/src/dm_anonymous.cpp | 3 +- common/src/dm_constants.cpp | 2 + common/src/ipc/standard/ipc_model_codec.cpp | 131 +++++++ .../include/deviceprofile_connector.h | 6 + .../src/deviceprofile_connector.cpp | 104 ++++++ .../cj/kits/include/device_manager_impl.h | 1 + .../native_cpp/include/device_manager.h | 15 + .../include/device_manager_callback.h | 26 ++ .../native_cpp/include/device_manager_impl.h | 16 +- .../native_cpp/include/dm_device_info.h | 78 +++- .../include/i_dm_service_impl_ext.h | 5 + .../include/i_dm_service_impl_ext_resident.h | 5 + .../idevice_manager_service_listener.h | 7 + .../include/notify/device_manager_notify.h | 16 + .../native_cpp/src/device_manager_impl.cpp | 239 ++++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 279 ++++++++++++++ .../src/notify/device_manager_notify.cpp | 186 ++++++++++ .../include/authentication_v2/auth_manager.h | 1 + .../authentication_v2/dm_auth_context.h | 1 + .../dm_auth_message_processor.h | 6 + .../include/device_manager_service_impl.h | 4 + .../src/authentication_v2/auth_manager.cpp | 14 + .../dm_auth_message_processor.cpp | 129 ++++++- .../src/device_manager_service_impl.cpp | 89 +++++ .../devicestate/dm_device_state_manager.cpp | 27 +- .../service/include/device_manager_service.h | 18 + .../include/device_manager_service_listener.h | 18 + .../include/idevice_manager_service_impl.h | 3 + .../service/src/device_manager_service.cpp | 350 ++++++++++++++++++ .../src/device_manager_service_listener.cpp | 142 +++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 197 ++++++++++ .../src/ipc/standard/ipc_server_stub.cpp | 2 +- 46 files changed, 2628 insertions(+), 16 deletions(-) create mode 100644 common/include/ipc/model/ipc_notify_service_discover_result_req.h create mode 100644 common/include/ipc/model/ipc_notify_service_found_req.h create mode 100644 common/include/ipc/model/ipc_publish_service_info_rsp.h create mode 100644 common/include/ipc/model/ipc_register_service_info_new_req.h create mode 100644 common/include/ipc/model/ipc_service_publish_result_req.h create mode 100644 common/include/ipc/model/ipc_start_publish_service_req.h create mode 100644 common/include/ipc/model/ipc_start_service_discovery_req.h create mode 100644 common/include/ipc/model/ipc_stop_publish_service_req.h create mode 100644 common/include/ipc/model/ipc_unbind_service_target_req.h create mode 100644 common/include/ipc/model/ipc_unregister_service_info_req.h diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index 418684851..e0219102a 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -122,6 +122,17 @@ enum DMIpcCmdInterfaceCode { CHECK_SINK_ACCESS_CONTROL, CHECK_SRC_SAME_ACCOUNT, CHECK_SINK_SAME_ACCOUNT, + START_SERVICE_DISCOVERING, + STOP_SERVICE_DISCOVERING, + NOTIFY_SERVICE_FOUND, + NOTIFY_SERVICE_DISCOVERY_RESULT, + BIND_SERVICE_TARGET, + UNBIND_SERVICE_TARGET, + REGISTER_SERVICE_INFO, + UNREGISTER_SERVICE_INFO, + START_PUBLISH_SERVICE, + STOP_PUBLISH_SERVICE, + SERVICE_PUBLISH_RESULT, // Add ipc msg here IPC_MSG_BUTT }; diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 403201b60..1a4a526b9 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -172,6 +172,7 @@ DM_EXPORT extern const char* APP_USER_DATA; DM_EXPORT extern const char* BUNDLE_INFO; DM_EXPORT extern const char* TITLE; DM_EXPORT extern const char* DM_BUSINESS_ID; +DM_EXPORT extern const char* PARAM_KEY_IS_SERVICE_BIND; // screen state enum ScreenState { @@ -190,6 +191,7 @@ const int32_t DEVICE_NAME_MAX_BYTES = 100; DM_EXPORT extern const char* ACL_IS_LNN_ACL_KEY; DM_EXPORT extern const char* ACL_IS_LNN_ACL_VAL_TRUE; DM_EXPORT extern const char* ACL_IS_LNN_ACL_VAL_FALSE; +DM_EXPORT extern const char* SERVICE_ID_KEY; extern const char* DM_VERSION_5_0_1; extern const char* DM_VERSION_5_0_2; diff --git a/common/include/ipc/model/ipc_notify_device_state_req.h b/common/include/ipc/model/ipc_notify_device_state_req.h index 26d2f59ea..161ea9a5c 100644 --- a/common/include/ipc/model/ipc_notify_device_state_req.h +++ b/common/include/ipc/model/ipc_notify_device_state_req.h @@ -80,10 +80,21 @@ public: dmDeviceBasicInfo_ = dmDeviceBasicInfo; } + void SetServiceIds(const std::vector &serviceIds) + { + serviceIds_ = serviceIds; + } + + const std::vector &GetServiceIds() const + { + return serviceIds_; + } + private: int32_t deviceState_ { 0 }; DmDeviceInfo dmDeviceInfo_; DmDeviceBasicInfo dmDeviceBasicInfo_; + std::vector serviceIds_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_notify_service_discover_result_req.h b/common/include/ipc/model/ipc_notify_service_discover_result_req.h new file mode 100644 index 000000000..c7d70355f --- /dev/null +++ b/common/include/ipc/model/ipc_notify_service_discover_result_req.h @@ -0,0 +1,53 @@ +/* + * 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_NOTIFY_SERVICE_DISCOVER_RESULT_REQ_H +#define OHOS_DM_IPC_NOTIFY_SERVICE_DISCOVER_RESULT_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyServiceDiscoverResultReq : public IpcReq { + DECLARE_IPC_MODEL(IpcNotifyServiceDiscoverResultReq); + +public: + int32_t GetDiscServiceId() const + { + return discServiceId_; + } + + void SetDiscServiceId(int32_t discServiceId) + { + discServiceId_ = discServiceId; + } + + int32_t GetResult() const + { + return result_; + } + + void SetResult(int32_t result) + { + result_ = result; + } + +private: + int32_t discServiceId_ { 0 }; + int32_t result_ { 0 }; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_NOTIFY_SERVICE_DISCOVER_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_notify_service_found_req.h b/common/include/ipc/model/ipc_notify_service_found_req.h new file mode 100644 index 000000000..527ad1aec --- /dev/null +++ b/common/include/ipc/model/ipc_notify_service_found_req.h @@ -0,0 +1,53 @@ +/* + * 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_NOTIFY_SERVICE_FOUND_REQ_H +#define OHOS_DM_IPC_NOTIFY_SERVICE_FOUND_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyServiceFoundReq : public IpcReq { + DECLARE_IPC_MODEL(IpcNotifyServiceFoundReq); + +public: + int32_t GetDiscServiceId() const + { + return discServiceId_; + } + + void SetDiscServiceId(int32_t discServiceId) + { + discServiceId_ = discServiceId; + } + + const DiscoveryServiceInfo &GetDiscServiceInfo() const + { + return discServiceInfo_; + } + + void SetDiscServiceInfo(const DiscoveryServiceInfo &discServiceInfo) + { + discServiceInfo_ = discServiceInfo; + } + +private: + int32_t discServiceId_ { 0 }; + DiscoveryServiceInfo discServiceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_NOTIFY_SERVICE_FOUND_REQ_H diff --git a/common/include/ipc/model/ipc_publish_service_info_rsp.h b/common/include/ipc/model/ipc_publish_service_info_rsp.h new file mode 100644 index 000000000..3f2ee1054 --- /dev/null +++ b/common/include/ipc/model/ipc_publish_service_info_rsp.h @@ -0,0 +1,53 @@ +/* + * 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_PUBLISH_SERVICE_INFO_RSP_H +#define OHOS_DM_IPC_PUBLISH_SERVICE_INFO_RSP_H + +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcPublishServiceInfoRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcPublishServiceInfoRsp); + +public: + int32_t GetRegServiceId() const + { + return regServiceId_; + } + + void SetRegServiceId(const int32_t ®ServiceId) + { + regServiceId_ = regServiceId; + } + + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(const int64_t &serviceId) + { + serviceId_ = serviceId; + } + +private: + int32_t regServiceId_; + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_PUBLISH_SERVICE_INFO_RSP_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 000000000..d2d4f940f --- /dev/null +++ b/common/include/ipc/model/ipc_register_service_info_new_req.h @@ -0,0 +1,53 @@ +/* + * 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; + } + + int32_t GetRegServiceId() const + { + return regServiceId_; + } + + void SetRegServiceId(int32_t regServiceId) + { + regServiceId_ = regServiceId; + } + +private: + ServiceRegInfo serviceRegInfo_; + int64_t regServiceId_; +}; +} // 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_service_publish_result_req.h b/common/include/ipc/model/ipc_service_publish_result_req.h new file mode 100644 index 000000000..e752d114b --- /dev/null +++ b/common/include/ipc/model/ipc_service_publish_result_req.h @@ -0,0 +1,54 @@ +/* + * 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_SERVICE_PUBLISH_RESULT_REQ_H +#define OHOS_DM_IPC_SERVICE_PUBLISH_RESULT_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServicePublishResultReq : public IpcReq { + DECLARE_IPC_MODEL(IpcServicePublishResultReq); + +public: + + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(int64_t serviceId) + { + serviceId_ = serviceId; + } + + int32_t GetResult() const + { + return result_; + } + + void SetResult(int32_t result) + { + result_ = result; + } + +private: + int64_t serviceId_ = 0; + int32_t result_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_SERVICE_PUBLISH_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_start_publish_service_req.h b/common/include/ipc/model/ipc_start_publish_service_req.h new file mode 100644 index 000000000..0a9c380eb --- /dev/null +++ b/common/include/ipc/model/ipc_start_publish_service_req.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H +#define OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H + +#include "dm_subscribe_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { + +class IpcStartPublishServiceReq : public IpcReq { + DECLARE_IPC_MODEL(IpcStartPublishServiceReq); + +public: + PublishServiceParam GetPublishServiceParam() + { + return publishServiceParam_; + } + + void SetPublishServiceParam(const PublishServiceParam& publishServiceParam) + { + publishServiceParam_ = publishServiceParam; + } + + int64_t GetServiceId() + { + return serviceId_; + } + + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; + PublishServiceParam publishServiceParam_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_START_PUBLISH_SERVICE_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_start_service_discovery_req.h b/common/include/ipc/model/ipc_start_service_discovery_req.h new file mode 100644 index 000000000..3ebb89310 --- /dev/null +++ b/common/include/ipc/model/ipc_start_service_discovery_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_START_SERVICE_DISCOVERY_REQ_H +#define OHOS_DM_IPC_START_SERVICE_DISCOVERY_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcStartServiceDiscoveryReq : public IpcReq { + DECLARE_IPC_MODEL(IpcStartServiceDiscoveryReq); + +public: + DiscoveryServiceParam GetDiscParam() const + { + return discParam_; + } + + void SetDiscParam(const DiscoveryServiceParam &discParam) + { + discParam_ = discParam; + } + +private: + DiscoveryServiceParam discParam_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_START_SERVICE_DISCOVERY_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_stop_publish_service_req.h b/common/include/ipc/model/ipc_stop_publish_service_req.h new file mode 100644 index 000000000..1ebda8dc8 --- /dev/null +++ b/common/include/ipc/model/ipc_stop_publish_service_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_STOP_PUBLISH_SERVICE_REQ_H +#define OHOS_DM_IPC_STOP_PUBLISH_SERVICE_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcStopPublishServiceReq : public IpcReq { + DECLARE_IPC_MODEL(IpcStopPublishServiceReq); + +public: + int64_t GetServiceId() + { + return serviceId_; + } + + void SetServiceId(const int64_t& serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_STOP_PUBLISH_SERVICE_REQ_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_unbind_service_target_req.h b/common/include/ipc/model/ipc_unbind_service_target_req.h new file mode 100644 index 000000000..7167da853 --- /dev/null +++ b/common/include/ipc/model/ipc_unbind_service_target_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_UNBIND_SERVICE_TARGET_REQ_H +#define OHOS_DM_IPC_UNBIND_SERVICE_TARGET_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcUnBindServiceTargetReq : public IpcReq { + DECLARE_IPC_MODEL(IpcUnBindServiceTargetReq); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(int64_t serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_UNBIND_SERVICE_TARGET_REQ_H 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 000000000..96d57e53d --- /dev/null +++ b/common/include/ipc/model/ipc_unregister_service_info_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_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: + int32_t GetRegServiceId() const + { + return regServiceId_; + } + + void SetRegServiceId(int32_t regServiceId) + { + regServiceId_ = regServiceId; + } + +private: + int32_t regServiceId_; +}; +} // 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/standard/ipc_model_codec.h b/common/include/ipc/standard/ipc_model_codec.h index e06af83a9..7bf1e7f1f 100644 --- a/common/include/ipc/standard/ipc_model_codec.h +++ b/common/include/ipc/standard/ipc_model_codec.h @@ -56,6 +56,16 @@ public: static bool DecodeNetworkIdQueryFilter(MessageParcel &parcel, NetworkIdQueryFilter &queryFilter); static bool EncodeStringVector(const std::vector &vec, MessageParcel &parcel); static bool DecodeStringVector(MessageParcel &parcel, std::vector &vec); + static bool EncodeSrvDiscParam(const DiscoveryServiceParam ¶m, MessageParcel &parcel); + static bool DecodeSrvDiscParam(MessageParcel &parcel, DiscoveryServiceParam ¶m); + static bool EncodeSrvDiscServiceInfo(const DiscoveryServiceInfo &serviceInfo, MessageParcel &parcel); + static bool DecodeSrvDiscServiceInfo(MessageParcel &parcel, DiscoveryServiceInfo &serviceInfo); + static bool EncodeServiceIds(const std::vector &serviceIds, MessageParcel &parcel); + static void DecodeServiceIds(std::vector &serviceIds, MessageParcel &parcel); + static bool EncodeServiceRegInfo(const ServiceRegInfo &serRegInfo, MessageParcel &parcel); + static bool DecodeServiceRegInfo(MessageParcel &parcel, ServiceRegInfo &serRegInfo); + static bool EncodePublishServiceParam(const PublishServiceParam &publishServiceParam, MessageParcel &parcel); + static bool DecodePublishServiceParam(MessageParcel &parcel, PublishServiceParam &publishServiceParam); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/src/dm_anonymous.cpp b/common/src/dm_anonymous.cpp index 43a0ecfed..b083472b1 100644 --- a/common/src/dm_anonymous.cpp +++ b/common/src/dm_anonymous.cpp @@ -225,7 +225,8 @@ void ParseMapFromJsonString(const std::string &jsonStr, std::map MAP_ERROR_CODE = { @@ -198,5 +199,6 @@ const int32_t DM_OH_OSTYPE = 10; const char* PEER_UDID = "peer_udid"; const char* PEER_OSTYPE = "peer_ostype"; const char* TIME_STAMP = "time_stamp"; +const char* SERVICE_ID_KEY = "serviceId"; } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index 2e336c5fd..c094e8749 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -76,6 +76,7 @@ bool IpcModelCodec::EncodePeerTargetId(const PeerTargetId &targetId, MessageParc 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; } @@ -86,6 +87,7 @@ void IpcModelCodec::DecodePeerTargetId(MessageParcel &parcel, PeerTargetId &targ targetId.bleMac = parcel.ReadString(); targetId.wifiIp = parcel.ReadString(); targetId.wifiPort = parcel.ReadUint16(); + targetId.serviceId = parcel.ReadInt64(); } bool IpcModelCodec::EncodeDmAccessCaller(const DmAccessCaller &caller, MessageParcel &parcel) @@ -324,6 +326,29 @@ bool IpcModelCodec::EncodeDmDeviceIconInfo(const DmDeviceIconInfo &deviceIconInf return bRet; } +bool IpcModelCodec::EncodeServiceIds(const std::vector &serviceIds, MessageParcel &parcel) +{ + int32_t size = serviceIds.size(); + if (!parcel.WriteInt32(size)) { + return false; + } + for (const auto &id : serviceIds) { + if (!parcel.WriteInt64(id)) { + return false; + } + } + return true; +} + +void IpcModelCodec::DecodeServiceIds(std::vector &serviceIds, MessageParcel &parcel) +{ + int32_t size = parcel.ReadInt32(); + serviceIds.resize(size); + for (int32_t i = 0; i < size; ++i) { + serviceIds[i] = parcel.ReadInt64(); + } +} + void IpcModelCodec::DecodeDmDeviceIconInfoFilterOptions(MessageParcel &parcel, DmDeviceIconInfoFilterOptions &filterOptions) { @@ -493,6 +518,112 @@ bool IpcModelCodec::DecodeStringVector(MessageParcel &parcel, std::vector(freq); + int32_t medium = 0; + READ_HELPER_RET(parcel, Int32, medium, false); + param.medium = static_cast(medium); + int32_t mode = 0; + READ_HELPER_RET(parcel, Int32, mode, false); + param.mode = static_cast(mode); + return true; +} + +bool IpcModelCodec::EncodeSrvDiscServiceInfo(const DiscoveryServiceInfo &serviceInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteString(serviceInfo.pkgName)); + bRet = (bRet && parcel.WriteInt64(serviceInfo.serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceInfo.serviceDisplayName)); + return bRet; +} + +bool IpcModelCodec::DecodeSrvDiscServiceInfo(MessageParcel &parcel, DiscoveryServiceInfo &serviceInfo) +{ + READ_HELPER_RET(parcel, String, serviceInfo.pkgName, false); + READ_HELPER_RET(parcel, Int64, serviceInfo.serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceInfo.serviceDisplayName, false); + return true; +} //LCOV_EXCL_STOP + +bool IpcModelCodec::EncodeServiceRegInfo(const ServiceRegInfo &serRegInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(serRegInfo.serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(serRegInfo.serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(serRegInfo.serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(serRegInfo.serviceInfo.serviceDisplayName)); + bRet = (bRet && parcel.WriteString(serRegInfo.customData)); + bRet = (bRet && parcel.WriteUint32(serRegInfo.dataLen)); + return bRet; +} + +bool IpcModelCodec::DecodeServiceRegInfo(MessageParcel &parcel, ServiceRegInfo &serRegInfo) +{ + READ_HELPER_RET(parcel, Int64, serRegInfo.serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, serRegInfo.serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, serRegInfo.serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, serRegInfo.serviceInfo.serviceDisplayName, false); + READ_HELPER_RET(parcel, String, serRegInfo.customData, false); + READ_HELPER_RET(parcel, Uint32, serRegInfo.dataLen, false); + return true; +} + +bool IpcModelCodec::EncodePublishServiceParam(const PublishServiceParam &publishServiceParam, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(publishServiceParam.serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(publishServiceParam.serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(publishServiceParam.serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(publishServiceParam.serviceInfo.serviceDisplayName)); + bRet = (bRet && parcel.WriteInt32(publishServiceParam.regServiceId)); + bRet = (bRet && parcel.WriteInt32(static_cast(publishServiceParam.discoverMode))); + bRet = (bRet && parcel.WriteInt32(static_cast(publishServiceParam.media))); + bRet = (bRet && parcel.WriteInt32(static_cast(publishServiceParam.freq))); + return bRet; +} + +bool IpcModelCodec::DecodePublishServiceParam(MessageParcel &parcel, PublishServiceParam &publishServiceParam) +{ + READ_HELPER_RET(parcel, Int64, publishServiceParam.serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, publishServiceParam.serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, publishServiceParam.serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, publishServiceParam.serviceInfo.serviceDisplayName, false); + READ_HELPER_RET(parcel, Int32, publishServiceParam.regServiceId, false); + int32_t discoverMode = static_cast(DMSrvDiscoveryMode::SERVICE_PUBLISH_MODE_ACTIVE); + READ_HELPER_RET(parcel, Int32, discoverMode, false); + publishServiceParam.discoverMode = static_cast(discoverMode); + int32_t media = static_cast(DMSrvMediumType::SERVICE_MEDIUM_TYPE_AUTO); + READ_HELPER_RET(parcel, Int32, media, false); + publishServiceParam.media = static_cast(media); + int32_t freq = static_cast(DmExchangeFreq::DM_LOW); + READ_HELPER_RET(parcel, Int32, freq, false); + publishServiceParam.freq = static_cast(freq); + return true; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 5bd29fa0c..f5e455b20 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -228,6 +228,9 @@ public: int32_t tokenId, const std::string &localUdid); DM_EXPORT DmOfflineParam HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId, const std::string &localUdid, int32_t peerTokenId); + DM_EXPORT int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfile &serviceInfoProfile); + DM_EXPORT int32_t PutServiceInfoProfile(const ServiceInfoProfile &serviceInfoProfile); + DM_EXPORT int32_t DeleteServiceInfoProfile(int32_t regServiceId, bool isMultiUser, int32_t userId); DM_EXPORT std::vector GetAllAccessControlProfile(); DM_EXPORT std::vector GetAllAclIncludeLnnAcl(); @@ -331,6 +334,9 @@ public: const std::vector &backGroundUserIds); DM_EXPORT bool IsAllowAuthAlways(const std::string &localUdid, int32_t userId, const std::string &peerUdid, const std::string &pkgName, int64_t tokenId); + DM_EXPORT void GetRemoteTokenIds(const std::string &localUdid, const std::string &udid, + std::vector &remoteTokenIds); + DM_EXPORT int32_t GetServiceInfoByTokenId(int64_t tokenId, ServiceInfoProfile &serviceInfo); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index cf201681d..f1f908a26 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -2325,6 +2325,66 @@ DmOfflineParam DeviceProfileConnector::HandleServiceUnBindEvent(int32_t remoteUs return offlineParam; } +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoProfileByServiceId(int64_t serviceId, + ServiceInfoProfile &serviceInfoProfile) +{ + LOGI("GetServiceInfoProfileByServiceId start"); + ServiceInfoProfileNew serviceInfoProfileDp; + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByServiceId( + serviceId, serviceInfoProfileDp); + if (ret != DM_OK) { + LOGE("GetServiceInfoProfileByServiceId failed, result: %{public}d", ret); + return ret; + } + serviceInfoProfile.regServiceId = serviceInfoProfileDp.GetRegServiceId(); + serviceInfoProfile.deviceId = serviceInfoProfileDp.GetDeviceId(); + serviceInfoProfile.userId = serviceInfoProfileDp.GetUserId(); + serviceInfoProfile.tokenId = serviceInfoProfileDp.GetTokenId(); + serviceInfoProfile.publishState = serviceInfoProfileDp.GetSerPubState(); + serviceInfoProfile.serviceId = serviceInfoProfileDp.GetServiceId(); + serviceInfoProfile.serviceType = serviceInfoProfileDp.GetServiceType(); + serviceInfoProfile.serviceName = serviceInfoProfileDp.GetServiceName(); + serviceInfoProfile.serviceDisplayName = serviceInfoProfileDp.GetServiceDisplayName(); + LOGI("GetServiceInfoProfileByServiceId success"); + return DM_OK; +} + +DM_EXPORT int32_t DeviceProfileConnector::PutServiceInfoProfile(const ServiceInfoProfile &serviceInfoProfile) +{ + LOGI("PutServiceInfoProfile start"); + ServiceInfoProfileNew serviceInfoProfileDp; + serviceInfoProfileDp.SetRegServiceId(serviceInfoProfile.regServiceId); + serviceInfoProfileDp.SetDeviceId(serviceInfoProfile.deviceId); + serviceInfoProfileDp.SetUserId(serviceInfoProfile.userId); + serviceInfoProfileDp.SetTokenId(serviceInfoProfile.tokenId); + serviceInfoProfileDp.SetSerPubState(serviceInfoProfile.publishState); + serviceInfoProfileDp.SetServiceId(serviceInfoProfile.serviceId); + serviceInfoProfileDp.SetServiceType(serviceInfoProfile.serviceType); + serviceInfoProfileDp.SetServiceName(serviceInfoProfile.serviceName); + serviceInfoProfileDp.SetServiceDisplayName(serviceInfoProfile.serviceDisplayName); + int32_t ret = DistributedDeviceProfileClient::GetInstance().PutServiceInfoProfileNew(serviceInfoProfileDp); + if (ret != DM_OK) { + LOGE("PutServiceInfoProfile failed, result: %{public}d", ret); + return ret; + } + LOGI("PutServiceInfoProfile success"); + return DM_OK; +} + +DM_EXPORT int32_t DeviceProfileConnector::DeleteServiceInfoProfile(int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + LOGI("DeleteServiceInfoProfile start"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteServiceInfoProfileNew( + regServiceId, isMultiUser, userId); + if (ret != DM_OK) { + LOGE("DeleteServiceInfoProfile failed, result: %{public}d", ret); + return ret; + } + LOGI("DeleteServiceInfoProfile success"); + return DM_OK; +} + DM_EXPORT std::vector DeviceProfileConnector::GetAllAccessControlProfile() { std::vector profiles; @@ -3480,6 +3540,50 @@ DM_EXPORT void DeviceProfileConnector::DeleteHoDevice(const std::string &peerUdi } } +DM_EXPORT void DeviceProfileConnector::GetRemoteTokenIds(const std::string &localUdid, + const std::string &udid, std::vector &remoteTokenIds) +{ + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::vector profiles = GetAclProfileByDeviceIdAndUserId(localUdid, userId); + uint64_t peerToken = 0; + for (auto &item : profiles) { + if(item.GetStatus() != ACTIVE) { + continue; + } + if (item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserDeviceId() == udid) { + peerToken = static_cast(item.GetAccesser().GetAccesserTokenId()); + remoteTokenIds.push_back(peerToken); + } + if (item.GetAccessee().GetAccesseeDeviceId()== udid && + item.GetAccesser().GetAccesserDeviceId() == localUdid) { + peerToken = static_cast(item.GetAccessee().GetAccesseeTokenId()); + remoteTokenIds.push_back(peerToken); + } + } +} + +DM_EXPORT int32_t DeviceProfileConnector::GetServiceInfoByTokenId(int64_t tokenId, + ServiceInfoProfile &serviceInfo) +{ + DistributedDeviceProfile::ServiceInfoProfileNew dpServiceInfo; + auto ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByTokenId(tokenId, dpServiceInfo); + if (ret != 0) { + LOGE("GetServiceInfoByTokenId GetServiceInfoProfileByTokenId failed."); + return ret; + } + serviceInfo.regServiceId = dpServiceInfo.GetRegServiceId(); + serviceInfo.deviceId = dpServiceInfo.GetDeviceId(); + serviceInfo.userId = dpServiceInfo.GetUserId(); + serviceInfo.tokenId = dpServiceInfo.GetTokenId(); + serviceInfo.publishState = dpServiceInfo.GetSerPubState(); + serviceInfo.serviceId = dpServiceInfo.GetServiceId(); + serviceInfo.serviceType = dpServiceInfo.GetServiceType(); + serviceInfo.serviceName = dpServiceInfo.GetServiceName(); + serviceInfo.serviceDisplayName = dpServiceInfo.GetServiceDisplayName(); + return DM_OK; +} + bool DeviceProfileConnector::CheckExtWhiteList(const std::string &pkgName) { LOGI("start pkgName %{public}s.", pkgName.c_str()); diff --git a/interfaces/cj/kits/include/device_manager_impl.h b/interfaces/cj/kits/include/device_manager_impl.h index 0d17884fe..b6531baa8 100644 --- a/interfaces/cj/kits/include/device_manager_impl.h +++ b/interfaces/cj/kits/include/device_manager_impl.h @@ -57,6 +57,7 @@ public: void OnPublishResult(int32_t publishId, int32_t publishResult); void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); void OnDmUiCall(const std::string ¶mJson); + void OnServicePublishResult(int64_t serviceId, int32_t publishResult); private: void ClearBundleCallbacks(); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index da0db3936..5d5191e46 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_callback.h b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h index e40343680..8cd0563dc 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h @@ -179,6 +179,32 @@ public: } virtual void OnCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode) = 0; }; + +class ServiceDiscoveryCallback { +public: + virtual ~ServiceDiscoveryCallback() + { + } + virtual void OnServiceFound(const DiscoveryServiceInfo &service) = 0; + virtual void OnServiceDiscoveryResult(int32_t resReason) = 0; +}; + +class ServiceInfoStateCallback { +public: + virtual ~ServiceInfoStateCallback() + { + } + virtual void OnServiceOnline(int64_t serviceId) = 0; + virtual void OnServiceOffline(int64_t serviceId) = 0; +}; + +class ServicePublishCallback { +public: + virtual ~ServicePublishCallback() + { + } + virtual void OnServicePublishResult(int64_t serviceId, int32_t reason) = 0; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CALLBACK_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 863626aef..6e449670d 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/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index b7704d94d..5cff684a8 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -20,6 +20,7 @@ #include #include "dm_app_image_info.h" +#include "dm_subscribe_info.h" #define DM_MAX_DEVICE_ID_LEN (97) #define DM_MAX_DEVICE_NAME_LEN (129) @@ -292,11 +293,16 @@ typedef struct PeerTargetId { * wlan ip port. */ uint16_t wifiPort = 0; + /** + * service id. + */ + int64_t serviceId; bool operator==(const PeerTargetId &other) const { return (deviceId == other.deviceId) && (brMac == other.brMac) && - (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort); + (bleMac == other.bleMac) && (wifiIp == other.wifiIp) && (wifiPort == other.wifiPort) && + (serviceId == other.serviceId); } bool operator<(const PeerTargetId &other) const @@ -306,7 +312,9 @@ typedef struct PeerTargetId { (deviceId == other.deviceId && brMac == other.brMac && bleMac < other.bleMac) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp < other.wifiIp) || (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && - wifiPort < other.wifiPort); + wifiPort < other.wifiPort) || + (deviceId == other.deviceId && brMac == other.brMac && bleMac == other.bleMac && wifiIp == other.wifiIp && + wifiPort == other.wifiPort && serviceId < other.serviceId); } } PeerTargetId; @@ -470,6 +478,72 @@ typedef struct DMAclQuadInfo { std::string peerUdid; int32_t peerUserId; } DMAclQuadInfo; + +typedef struct ServiceInfo { + int64_t serviceId; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; +} ServiceInfo; + +typedef enum DMSrvDiscoveryMode { + SERVICE_PUBLISH_MODE_PASSIVE = 0x15, + SERVICE_PUBLISH_MODE_ACTIVE = 0x25 +} DMSrvDiscoveryMode; + +typedef enum DMSrvMediumType { + SERVICE_MEDIUM_TYPE_AUTO = 0, + SERVICE_MEDIUM_TYPE_BLE, + SERVICE_MEDIUM_TYPE_BLE_TRIGGER, + SERVICE_MEDIUM_TYPE_MDNS, + SERVICE_MEDIUM_TYPE_BUTT, +} DMSrvMediumType; + +typedef struct DiscoveryServiceParam { + std::string serviceName; + std::string serviceType; + int32_t discoveryServiceId; + DmExchangeFreq freq; + DMSrvMediumType medium; + DMSrvDiscoveryMode mode; +} DiscoveryServiceParam; + +typedef struct DiscoveryServiceInfo { + ServiceInfo serviceInfo; + std::string pkgName; +} DiscoveryServiceInfo; + +typedef struct ServiceInfoProfile { + int32_t regServiceId; + std::string deviceId; + int32_t userId; + int64_t tokenId; + int8_t publishState; + int64_t serviceId; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; +} ServiceInfoProfile; + +typedef struct ServiceRegInfo { + ServiceInfo serviceInfo; + std::string customData; + uint32_t dataLen; +} ServiceRegInfo; + +typedef struct PublishServiceParam { + ServiceInfo serviceInfo; + DMSrvDiscoveryMode discoverMode; + int32_t regServiceId; + DMSrvMediumType media; + DmExchangeFreq freq; +} PublishServiceParam; + +typedef struct ServiceBindlocalInfo { + std::string localUdid; + int32_t userId; + uint32_t tokenId; +} ServiceBindLocalInfo; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h index cb4bb85bc..b3debcc17 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h @@ -107,6 +107,11 @@ public: virtual void OnSessionClosed(const int32_t sessionId) = 0; virtual void OnBytesReceived(const int32_t sessionId, const std::string message) = 0; virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; + virtual int32_t StartServiceDiscovery(const ProcessInfo &processInfo, const DiscoveryServiceParam &discParam) = 0; + virtual int32_t StopServiceDiscovery(int32_t discServiceId) = 0; + virtual int32_t OpenAuthSessionWithPara(const int64_t serviceId) = 0; + virtual int32_t StartPublishService(const ProcessInfo &processInfo, PublishServiceParam &publishServiceParam) = 0; + virtual int32_t StopPublishService(int64_t serviceId) = 0; }; using CreateDMServiceImplExtFuncPtr = IDMServiceImplExt *(*)(void); diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index 4f738fc7c..c13cca792 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -113,6 +113,11 @@ public: virtual void HandleScreenLockEvent(bool isLock) = 0; virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; virtual void HandleUserSwitchEvent(int32_t currentUserId, int32_t beforeUserId) = 0; + virtual int32_t StartServiceDiscovery(const ProcessInfo &processInfo, const DiscoveryServiceParam &discParam) = 0; + virtual int32_t StopServiceDiscovery(int32_t discServiceId) = 0; + virtual int32_t OpenAuthSessionWithPara(const int64_t serviceId) = 0; + virtual int32_t StartPublishService(const ProcessInfo &processInfo, PublishServiceParam &publishServiceParam) = 0; + virtual int32_t StopPublishService(int64_t serviceId) = 0; }; using CreateDMServiceExtResidentFuncPtr = IDMServiceImplExtResident *(*)(void); diff --git a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h index 336c5b631..fecaa011c 100644 --- a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h +++ b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h @@ -174,9 +174,16 @@ public: virtual void OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceId, const std::string &deviceName, int32_t code) = 0; virtual void SetExistPkgName(const std::set &pkgNameSet) = 0; + virtual void OnServiceFound(const ProcessInfo &processInfo, int32_t discServiceId, + const DiscoveryServiceInfo &discServiceInfo) = 0; + virtual void OnServiceDiscoveryResult(const ProcessInfo &processInfo, int32_t discServiceId, int32_t reason) = 0; virtual std::string GetLocalDisplayDeviceName() = 0; virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; + virtual int32_t OpenAuthSessionWithPara(int64_t serviceId) = 0; + virtual void OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) = 0; + virtual void OnServicePublishResult(const ProcessInfo &processInfo, int64_t serviceId, int32_t publishResult) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 39a229cc8..7bc4e5acd 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -87,6 +87,13 @@ public: std::shared_ptr callback); void OnSetRemoteDeviceNameResult(const std::string &pkgName, const std::string &deviceId, int32_t code); void UnRegisterPinHolderCallback(const std::string &pkgName); + void RegisterServiceDiscoveryCallback(int32_t discoveryServiceId, + std::shared_ptr callback); + void UnRegisterServiceDiscoveryCallback(int32_t discoveryServiceId); + int32_t RegisterServiceStateCallback(const std::string &key, std::shared_ptr callback); + int32_t UnRegisterServiceStateCallback(const std::string &key); + void RegisterServicePublishCallback(int64_t serviceId, std::shared_ptr callback); + void UnRegisterServicePublishCallback(int64_t serviceId); public: static void DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); @@ -103,6 +110,8 @@ public: std::shared_ptr tempCbk); static void DeviceTrustChange(const std::string &udid, const std::string &uuid, DmAuthForm authForm, std::shared_ptr tempCbk); + static void ServiceInfoOnline( + std::vector, int64_t>> callbackInfo); public: void OnRemoteDied(); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo); @@ -139,6 +148,10 @@ public: std::string content); std::shared_ptr GetDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId); void GetCallBack(std::map> &callbackMap); + void OnServiceFound(int32_t discoveryServiceId, const DiscoveryServiceInfo &service); + void OnServiceDiscoveryResult(int32_t discoveryServiceId, int32_t resReason); + void OnServiceOnline(const std::vector &serviceIds); + void OnServicePublishResult(int64_t serviceId, int32_t publishResult); private: #if !defined(__LITEOS_M__) @@ -166,6 +179,9 @@ private: std::map> setLocalDeviceNameCallback_; std::map>> setRemoteDeviceNameCallback_; + std::map> serviceDiscoveryCallbacks_; + std::map> serviceStateCallback_; + std::map> servicePublishCallback_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 23aa74e64..1bc5f1406 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,237 @@ 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) +{ + LOGI("StartServiceDiscovery start."); + 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); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("StartServiceDiscovery error: Failed with ret %{public}d", ret); + return ret; + } + + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::StopServiceDiscovery(const std::string &pkgName, int32_t discoveryServiceId) +{ + LOGI("StopServiceDiscovery Start"); + 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; + } + LOGI("start"); + 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); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("BindServiceTarget error: Failed with ret %{public}d", ret); + 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; + } + LOGI("Start, pkgName: %{public}s", pkgName.c_str()); + + 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) +{ + std::string key = std::to_string(serviceId); + LOGI("DeviceManagerImpl: RegisterServiceStateCallback key=%{public}s", GetAnonyString(key.c_str()).c_str()); + int32_t ret = DeviceManagerNotify::GetInstance().RegisterServiceStateCallback(key, callback); + if (ret != DM_OK) { + LOGE("error: register callback failed: %{public}d", ret); + return ret; + } + SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) +{ + std::string key = std::to_string(serviceId); + LOGI("DeviceManagerImpl: UnRegisterServiceStateCallback key=%{public}s", GetAnonyString(key.c_str()).c_str()); + int32_t ret = DeviceManagerNotify::GetInstance().UnRegisterServiceStateCallback(key); + if (ret != DM_OK) { + LOGE("error: unregister callback failed: %{public}d", ret); + return ret; + } + SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_STATE, pkgName); + return DM_OK; +} + +int32_t DeviceManagerImpl::StartPublishService(const std::string &pkgName, + const PublishServiceParam &publishServiceParam, + std::shared_ptr callback, int64_t &serviceId) +{ + LOGI("StartPublishService Start"); + if (pkgName.empty() || callback == nullptr) { + LOGE("DeviceManagerImpl::StartPublishService error: pkgName %{public}s invalid para", pkgName.c_str()); + 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) +{ + LOGI("StopPublishService Start"); + 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) +{ + LOGI("Start"); + 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) +{ + LOGI("Start"); + 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 a30e5493b..b4b2746ba 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,274 @@ 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) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return 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) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(STOP_SERVICE_DISCOVERING, 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.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) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRspis null"); + return 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) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return 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) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return 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) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetServiceId(); + if (!data.WriteInt64(serviceId)) { + LOGE("write GetServiceId failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(STOP_PUBLISH_SERVICE, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +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 (!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) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return 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) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return 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) +{ + 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/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 907efd298..6807b11b5 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -36,6 +36,7 @@ constexpr const char* DEVICE_OFFLINE = "deviceOffline"; constexpr const char* DEVICEINFO_CHANGE = "deviceInfoChange"; constexpr const char* DEVICE_READY = "deviceReady"; constexpr const char* DEVICE_TRUST_CHANGE = "deviceTrustChange"; +constexpr const char* SERVICE_ONLINE = "serviceOnLine"; #endif const uint16_t DM_INVALID_FLAG_ID = 0; void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName, @@ -1450,5 +1451,190 @@ void DeviceManagerNotify::UnRegisterPinHolderCallback(const std::string &pkgName std::lock_guard autoLock(lock_); pinHolderCallback_.erase(pkgName); } + +void DeviceManagerNotify::RegisterServiceDiscoveryCallback(int32_t discoveryServiceId, + std::shared_ptr callback) +{ + if (callback == nullptr) { + LOGE("Invalid parameter, pkgName is empty."); + return; + } + std::lock_guard autolock(lock_); + serviceDiscoveryCallbacks_[discoveryServiceId] = callback; +} + +void DeviceManagerNotify::UnRegisterServiceDiscoveryCallback(int32_t discoveryServiceId) +{ + std::lock_guard autolock(lock_); + if (serviceDiscoveryCallbacks_.count(discoveryServiceId) == 0) { + LOGE("error,Service Discovery not register."); + return; + } + serviceDiscoveryCallbacks_.erase(discoveryServiceId); +} + +void DeviceManagerNotify::OnServiceFound(int32_t discoveryServiceId, const DiscoveryServiceInfo &service) +{ + std::shared_ptr tempCbk; + std::lock_guard autolock(lock_); + auto iter = serviceDiscoveryCallbacks_.find(discoveryServiceId); + if (iter == serviceDiscoveryCallbacks_.end()) { + LOGE("error, callback not register for discoveryServiceId %{public}s", + GetAnonyInt32(discoveryServiceId).c_str()); + return; + } + tempCbk = iter->second; + if (tempCbk == nullptr) { + LOGE("OnServiceFound error, registered service discovery callback is nullptr."); + return; + } + LOGD("Complete with serviceInfo"); + tempCbk->OnServiceFound(service); +} + +void DeviceManagerNotify::OnServiceDiscoveryResult(int32_t discoveryServiceId, int32_t resReason) +{ + std::shared_ptr tempCbk; + std::lock_guard autolock(lock_); + auto iter = serviceDiscoveryCallbacks_.find(discoveryServiceId); + if (iter == serviceDiscoveryCallbacks_.end()) { + LOGE("error, callback not register for discoveryServiceId %{public}s", + GetAnonyInt32(discoveryServiceId).c_str()); + return; + } + tempCbk = iter->second; + if (tempCbk == nullptr) { + LOGE("OnServiceDiscoveryResult error, registered service discovery callback is nullptr."); + return; + } + LOGD("Complete with result"); + tempCbk->OnServiceDiscoveryResult(resReason); +} + +void DeviceManagerNotify::OnServiceOnline(const std::vector &serviceIds) +{ + if (serviceIds.empty()) { + LOGE("Invalid parameter, serviceIds is empty."); + return; + } + LOGI("In, serviceIds size: %{public}zu", serviceIds.size()); + std::string key = ""; + std::vector, int64_t>> callbackInfo; + std::lock_guard autoLock(lock_); + for (const auto &item : serviceIds) { + key = std::to_string(item); + auto iter = serviceStateCallback_.find(key); + if (iter == serviceStateCallback_.end()) { + continue; + } + callbackInfo.push_back( + std::pair, int64_t>(iter->second, item)); + } + if (callbackInfo.empty()) { + return; + } + +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::submit([=]() { ServiceInfoOnline(callbackInfo); }); +#else + std::thread serviceOnline([=]() { ServiceInfoOnline(callbackInfo);}); + int32_t ret = pthread_setname_np(serviceOnline.native_handle(), SERVICE_ONLINE); + if(ret != DM_OK) { + LOGE("DeviceManagerNotify serviceOnline setname failed."); + } + serviceOnline.detach(); +#endif +} + +void DeviceManagerNotify::ServiceInfoOnline( + std::vector, int64_t>> callbackInfo) +{ + for (auto &iter : callbackInfo) { + if (iter.first == nullptr) { + continue; + } + iter.first->OnServiceOnline(iter.second); + } +} + +int32_t DeviceManagerNotify::RegisterServiceStateCallback(const std::string &key, + std::shared_ptr callback) +{ + LOGI("DeviceManagerNotify::RegisterServiceStateCallback key = %{public}s", GetAnonyString(key).c_str()); + if (key.empty() || callback == nullptr) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::lock_guard autolock(lock_); + serviceStateCallback_[key] = callback; + return DM_OK; +} + +int32_t DeviceManagerNotify::UnRegisterServiceStateCallback(const std::string &key) +{ + LOGI("DeviceManagerNotify::UnRegisterServiceStateCallback key = %{public}s", GetAnonyString(key).c_str()); + std::lock_guard autolock(lock_); + if (key.empty() || serviceStateCallback_.find(key) == serviceStateCallback_.end()) { + LOGE("Invalid parameter."); + return ERR_DM_INPUT_PARA_INVALID; + } + serviceStateCallback_.erase(key); + return DM_OK; +} + +void DeviceManagerNotify::RegisterServicePublishCallback(int64_t serviceId, + std::shared_ptr callback) +{ + LOGI("RegisterPublishCallback start"); + if (callback == nullptr) { + LOGE("Invalid parameter, callback is nullptr."); + return; + } + std::lock_guard autoLock(lock_); + if (servicePublishCallback_.size() >= MAX_CONTAINER_SIZE) { + LOGE("servicePublishCallback_ map size is more than max size"); + return; + } + servicePublishCallback_[serviceId] = callback; + LOGI("RegisterPublishCallback success"); +} + +void DeviceManagerNotify::UnRegisterServicePublishCallback(int64_t serviceId) +{ + LOGI("UnRegisterServicePublishCallback start"); + if (serviceId == 0) { + LOGE("UnRegisterServicePublishCallback error: Invalid parameter, serviceId is 0."); + return; + } + std::lock_guard autoLock(lock_); + servicePublishCallback_.erase(serviceId); + LOGI("UnRegisterServicePublishCallback success"); +} + +void DeviceManagerNotify::OnServicePublishResult(int64_t serviceId, int32_t publishResult) +{ + LOGI("OnServicePublishResult start, serviceId: %{public}s, publishResult: %{public}d", + GetAnonyString(std::to_string(serviceId)).c_str(), publishResult); + std::shared_ptr tempCbk; + { + std::lock_guard autoLock(lock_); + auto iter = servicePublishCallback_.find(serviceId); + if (iter == servicePublishCallback_.end()) { + LOGE("OnServicePublishResult error, callback not registered for serviceId"); + return; + } + tempCbk = iter->second; + if (publishResult != DM_OK) { + servicePublishCallback_.erase(serviceId); + LOGI("OnServicePublishResult: Removed callback for serviceId"); + } + } + if (tempCbk == nullptr) { + LOGE("OnServicePublishResult error, registered callback is nullptr"); + return; + } + tempCbk->OnServicePublishResult(serviceId, publishResult); + LOGI("OnServicePublishResult end"); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index 02deef3fe..58f16d19a 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -154,6 +154,7 @@ private: void GetBindCallerInfo(); int32_t GetBindLevel(int32_t bindLevel); void GetIsNeedJoinLnnParam(const std::map &bindParam); + void ParseServiceInfo(const JsonObject &jsonObject); }; class AuthSrcManager : public AuthManager { diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index 4bb875565..95271a5ea 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -276,6 +276,7 @@ struct DmAuthContext { std::multimap proxy; // Multimap where the key is the accessor and the value is the accesssee bool isNeedJoinLnn{true}; bool IsProxyBind{false}; + bool isServiceBind{false}; bool IsCallingProxyAsSubject{true}; bool IsNeedSetProxy{false}; bool isNeedAuthenticate{true}; // apply for skip authenticate 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 98144d108..78706f4a3 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/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 60726df8f..55733d996 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); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); @@ -303,6 +306,7 @@ 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); private: std::mutex authMgrMtx_; std::shared_ptr authMgr_; // Old protocol only diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 4b85bd8fa..15bd19b1c 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -477,9 +477,19 @@ void AuthManager::ParseJsonObject(const JsonObject &jsonObject) } ParseHmlInfoInJsonObject(jsonObject); ParseProxyJsonObject(jsonObject); + ParseServiceInfo(jsonObject); return; } +void AuthManager::ParseServiceInfo(const JsonObject &jsonObject) +{ + if (context_ == nullptr || jsonObject.IsDiscarded() || !IsString(jsonObject, PARAM_KEY_IS_SERVICE_BIND) || + jsonObject[PARAM_KEY_IS_SERVICE_BIND].Get() != DM_VAL_TRUE) { + return; + } + context_->isServiceBind = true; +} + void AuthManager::ParseUltrasonicSide(const JsonObject &jsonObject) { if (jsonObject[TAG_ULTRASONIC_SIDE].IsString()) { @@ -580,6 +590,10 @@ void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, return; } ParseJsonObject(jsonObject); + if (context_->isServiceBind) { + context_->accessee.serviceId = std::stoll(deviceId); + context_->accesser.serviceId = std::stoll(deviceId); + } context_->accesser.accountId = MultipleUserConnector::GetOhosAccountIdByUserId(context_->accesser.userId); // compatible for old version 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 43f367ea3..a3fb91cce 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 f9feb56c2..12cae47a2 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 @@ -1606,6 +1608,11 @@ 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); + return listener_->OpenAuthSessionWithPara(std::stoll(deviceId)); + } bool hmlEnable160M = false; int32_t hmlActionId = 0; int invalidSessionId = -1; @@ -1779,6 +1786,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; @@ -3201,6 +3212,84 @@ 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; + } + + int32_t ret = DeleteAclExtraDataServiceId(serviceId); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, DeleteAclExtraDataServiceId failed."); + return ret; + } + + ServiceInfoProfile serviceInfoProfile; + int32_t userId = -1; + ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, GetServiceInfoProfileByServiceId failed."); + return ret; + } + DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(serviceInfoProfile.regServiceId, false, userId); + + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::DeleteAclExtraDataServiceId(int64_t serviceId) +{ + bool isDeletedExtra = false; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string deviceIdLocal = std::string(localDeviceId); + 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(); + int32_t accesserUserId = item.GetAccesser().GetAccesserUserId(); + if (accesserUdid == deviceIdLocal) { + isDeletedExtra = true; + json.Erase(TAG_SERVICE_ID); + item.SetExtraData(json.Dump()); + 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; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index e77e579b7..e7bc9f3d2 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -175,10 +175,35 @@ void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState LOGI("begin, devState = %{public}d networkId: %{public}s.", devState, GetAnonyString(devInfo.networkId).c_str()); CHECK_NULL_VOID(listener_); + std::vector remoteTokenIds; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = static_cast(localDeviceId); + std::string udid = softbusConnector_->GetDeviceUdidByUdidHash(devInfo.deviceId); + if (udid == "" || localUdid == "" || localUdid == udid) { + LOGE("ProcessDeviceStateChange udid error."); + return; + } + DeviceProfileConnector::GetInstance().GetRemoteTokenIds(localUdid, udid, remoteTokenIds); + std::vector remoteServiceIds; + for (const auto &item : remoteTokenIds) { + ServiceInfoProfile serviceInfo; + if (DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(item, serviceInfo) != DM_OK) { + LOGE("GetServiceInfoByTokenid failed."); + continue; + } + remoteServiceIds.push_back(serviceInfo.serviceId); + } + LOGI("ProcessDeviceStateChange, remoteServiceIds size: %{public}zu", remoteServiceIds.size()); + for (const auto &item : processInfoVec) { if (!item.pkgName.empty()) { LOGI("ProcessDeviceStateChange, pkgName = %{public}s", item.pkgName.c_str()); - listener_->OnDeviceStateChange(item, devState, devInfo); + if (!remoteServiceIds.empty() && devState == DEVICE_STATE_ONLINE) { + listener_->OnDeviceStateChange(item, devState, devInfo, remoteServiceIds); + } else { + listener_->OnDeviceStateChange(item, devState, devInfo); + } } } } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 709e7301f..08bd04014 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,13 @@ private: void GetLocalUserIdFromDataBase(std::vector &foregroundUsers, std::vector &backgroundUsers); void PutLocalUserIdToDataBase(const std::vector &foregroundUsers, const std::vector &backgroundUsers); + int32_t StopPublishServiceInternal(int64_t serviceId); + int32_t GenerateServiceId(int64_t &serviceId); + int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo); + ServiceInfoProfile CreateServiceInfoProfile(const PublishServiceParam &publishServiceParam, const int32_t &userId); + int32_t ConvertServiceInfoProfileByRegInfo(const ServiceRegInfo &serviceRegInfo, + ServiceInfoProfile &serviceInfoProfile); + int32_t GenerateRegServiceId(int32_t ®ServiceId); private: bool isImplsoLoaded_ = false; diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 216835016..42b08ec02 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -90,9 +90,16 @@ public: void OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceId, const std::string &deviceName, int32_t code) override; void SetExistPkgName(const std::set &pkgNameSet) override; + void OnServiceFound(const ProcessInfo &processInfo, int32_t discServiceId, + const DiscoveryServiceInfo &discServiceInfo) override; + void OnServiceDiscoveryResult(const ProcessInfo &processInfo, int32_t discServiceId, int32_t reason) override; + void OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) override; std::string GetLocalDisplayDeviceName() override; int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) override; + int32_t OpenAuthSessionWithPara(int64_t serviceId) override; + void OnServicePublishResult(const ProcessInfo &processInfo, int64_t serviceId, int32_t publishResult) override; private: void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, @@ -127,6 +134,17 @@ private: void ProcessAppOffline(const std::vector &procInfoVec, const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo); void RemoveNotExistProcess(); + void ProcessDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds); + void ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); + void ProcessDeviceOnline(const std::vector &procinfoVec, const ProcessInfo &processInfo, + const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); + void ProcessAppOnline(const std::vector &procInfoVec, const ProcessInfo &processInfo, + const DmDeviceState &state, const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds); private: #if !defined(__LITEOS_M__) IpcServerListener ipcServerListener_; diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 949542bed..3126d7fdc 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; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 45925abff..c892843b7 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,13 @@ 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 MIN_PIN_TOKEN = 1000000; + constexpr int32_t MAX_PIN_TOKEN = 90000000; + constexpr uint32_t SERVICE_ID_LENGTH = 16; + constexpr int32_t BASE_HEX = 16; + 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() @@ -4177,6 +4187,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()) { @@ -4364,5 +4383,336 @@ void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &c MultipleUserConnector::GetCurrentDMAccountInfo()); } #endif + +int32_t DeviceManagerService::StartServiceDiscovery(const std::string &pkgName, const DiscoveryServiceParam &discParam) +{ + LOGI("In"); + 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) +{ + LOGI("In"); + 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) +{ + LOGI("DeviceManagerService::BindServiceTarget"); + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + LOGI("Start for pkgName = %{public}s", pkgName.c_str()); + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { + LOGE("Invalid parameter, pkgName 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; + } + if (targetId.serviceId == 0) { + LOGE("Invalid parameter, service id is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + 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; + } + LOGI("Start for pkgName = %{public}s", pkgName.c_str()); + if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + 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 + LOGI("RegisterServiceInfo Begin."); + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The callerdoes not have permission to cal1 RegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + 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: [ANONYMIZED]."); + 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 + LOGI("UnRegisterServiceInfo Begin."); + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call UnRegisterServiceInfo."); + return ERR_DM_NO_PERMISSION; + } + int32_t userId = -1; + MultipleUserConnector::GetCallerUserId(userId); + int32_t ret = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(regServiceId, false, 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 + LOGI("StartPublishService Begin."); + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call StantPublishService."); + return ERR_DM_NO_PERMISSION; + } + int32_t 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 = 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 + LOGI("StopPublishService Begin."); + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call1 StopPublishService."); + return ERR_DM_NO_PERMISSION; + } + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("StartPublishService 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 = StopPublishServiceInternal(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::StopPublishServiceInternal(int64_t serviceId) +{ + LOGI("StopPublishServiceInternal 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("StopPublishServiceInternal success"); + return DM_OK; +} + +int32_t DeviceManagerService::GenerateServiceId(int64_t &serviceId) +{ + LOGI("GenerateServiceId Begin."); + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + LOGE("GenerateServiceId failed, get local info error"); + return ret; + } + 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); + serviceId = std::stoll(serviceIdStr, nullptr, BASE_HEX); + ServiceInfoProfile serviceInfoProfile; + ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); + if (ret != DM_OK) { + LOGI("GenerateServiceId success"); + return DM_OK; + } + } + LOGE("GenerateServiceId failed, serviceId already exists after retry"); + return ERR_DM_FAILED; +} + +int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + localInfo.localUdid = std::string(localDeviceId); + localInfo.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + MultipleUserConnector::GetCallerUserId(localInfo.userId); + return DM_OK; +} + +ServiceInfoProfile DeviceManagerService::CreateServiceInfoProfile(const PublishServiceParam &publishServiceParam, + const int32_t &userId) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + ServiceInfoProfile serviceInfoProfile; + serviceInfoProfile.regServiceId = publishServiceParam.regServiceId; + serviceInfoProfile.deviceId = localDeviceId; + serviceInfoProfile.userId = userId; + serviceInfoProfile.tokenId = IPCSkeleton::GetCallingTokenID(); + serviceInfoProfile.publishState = SERVICE_PUBLISHED_STATE; + serviceInfoProfile.serviceId = publishServiceParam.serviceInfo.serviceId; + serviceInfoProfile.serviceType = publishServiceParam.serviceInfo.serviceType; + serviceInfoProfile.serviceName = publishServiceParam.serviceInfo.serviceName; + serviceInfoProfile.serviceDisplayName = publishServiceParam.serviceInfo.serviceDisplayName; + return serviceInfoProfile; +} + +int32_t DeviceManagerService::ConvertServiceInfoProfileByRegInfo( + const ServiceRegInfo &serviceRegInfo, ServiceInfoProfile &serviceInfoProfile) +{ + ServiceBindLocalInfo localInfo; + int32_t ret = GetServiceBindLocalInfo(localInfo); + if (ret != DM_OK) { + LOGE("CovertServiceInfoProfileByRegInfo failed, get local info error"); + return ret; + } + serviceInfoProfile.deviceId = localInfo.localUdid; + serviceInfoProfile.userId = localInfo.userId; + serviceInfoProfile.tokenId = localInfo.tokenId; + 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 bc47a6a81..71e0fc48f 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -39,8 +39,11 @@ #include "ipc_notify_get_device_profile_info_list_req.h" #include "ipc_notify_pin_holder_event_req.h" #include "ipc_notify_publish_result_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_service_publish_result_req.h" #include "ipc_server_stub.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "datetime_ex.h" @@ -1032,6 +1035,101 @@ void DeviceManagerServiceListener::SetExistPkgName(const std::set & } } +void DeviceManagerServiceListener::ProcessDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + std::vector processInfoVec = GetNotifyProcessInfoByUserId(processInfo.userId, + DmCommonNotifyEvent::REG_DEVICE_STATE); + std::vector hpProcessInfoVec; + std::vector lpProcessInfoVec; + for (const auto &it : processInfoVec) { + if (highPriorityPkgNameSet_.find(it.pkgName) != highPriorityPkgNameSet_.end()) { + hpProcessInfoVec.push_back(it); + } else { + lpProcessInfoVec.push_back(it); + } + } + ProcessDeviceOnline(hpProcessInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); + ProcessDeviceOnline(lpProcessInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); +} + +void DeviceManagerServiceListener::ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo, + const std::vector &serviceIds) +{ + std::vector processInfoVec = GetWhiteListSAProcessInfo(DmCommonNotifyEvent::REG_DEVICE_STATE); + ProcessInfo bindProcessInfo = DealBindProcessInfo(processInfo); + processInfoVec.push_back(bindProcessInfo); + std::vector allProcessInfos = ipcServerListener_.GetAllProcessInfo(); + ProcessAppOnline(processInfoVec, processInfo, state, info, deviceBasicInfo, serviceIds); +} + +void DeviceManagerServiceListener::OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state, + const DmDeviceInfo &info, const std::vector &serviceIds) +{ + LOGI("OnDeviceStateChange, state = %{public}d", state); + DmDeviceBasicInfo deviceBasicInfo; + ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, info, deviceBasicInfo); + if (processInfo.pkgName == std::string(DM_PKG_NAME)) { + ProcessDeviceStateChange(processInfo, state, info, deviceBasicInfo, serviceIds); + } else { + ProcessAppStateChange(processInfo, state, info, deviceBasicInfo, serviceIds); + } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + KVAdapterManager::GetInstance().DeleteAgedEntry(); +#endif +} + +void DeviceManagerServiceListener::ProcessDeviceOnline(const std::vector &procInfoVec, + const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + LOGI("state %{public}d, udidhash %{public}s.", static_cast(state), + GetAnonyString(info.deviceId).c_str()); + for (const auto &it : procInfoVec) { + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetServiceIds(serviceIds); + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); + DmDeviceState notifyState = state; + { + std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + notifyState = DmDeviceState::DEVICE_INFO_CHANGED; + } else { + alreadyOnlinePkgName_[notifyPkgName] = info; + } + } + SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } +} + +void DeviceManagerServiceListener::ProcessAppOnline(const std::vector &procInfoVec, + const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info, + const DmDeviceBasicInfo &deviceBasicInfo, const std::vector &serviceIds) +{ + LOGI("state %{public}d, udidhash %{public}s.", static_cast(state), + GetAnonyString(info.deviceId).c_str()); + for (const auto &it : procInfoVec) { + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetServiceIds(serviceIds); + std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId); + DmDeviceState notifyState = state; + { + std::lock_guard autoLock(alreadyNotifyPkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) { + notifyState = DmDeviceState::DEVICE_INFO_CHANGED; + } else { + alreadyOnlinePkgName_[notifyPkgName] = info; + } + } + SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } +} + std::string DeviceManagerServiceListener::GetLocalDisplayDeviceName() { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -1052,5 +1150,49 @@ int32_t DeviceManagerServiceListener::OpenAuthSessionWithPara(const std::string return ERR_DM_UNSUPPORTED_METHOD; #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 +} + +void DeviceManagerServiceListener::OnServiceFound(const ProcessInfo &processInfo, int32_t discServiceId, + const DiscoveryServiceInfo &discServiceInfo) +{ + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetProcessInfo(processInfo); + pReq->SetDiscServiceId(discServiceId); + pReq->SetDiscServiceInfo(discServiceInfo); + ipcServerListener_.SendRequest(NOTIFY_SERVICE_FOUND, pReq, pRsp); +} + +void DeviceManagerServiceListener::OnServiceDiscoveryResult(const ProcessInfo &processInfo, int32_t discServiceId, + int32_t reason) +{ + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetProcessInfo(processInfo); + pReq->SetDiscServiceId(discServiceId); + pReq->SetResult(reason); + ipcServerListener_.SendRequest(NOTIFY_SERVICE_DISCOVERY_RESULT, pReq, pRsp); +} + +void DeviceManagerServiceListener::OnServicePublishResult(const ProcessInfo &processInfo, + int64_t serviceId, int32_t publishResult) +{ + LOGI("OnServicePublishResult start"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetServiceId(serviceId); + pReq->SetResult(publishResult); + pReq->SetPkgName(processInfo.pkgName); + pReq->SetProcessInfo(processInfo); + ipcServerListener_.SendRequest(SERVICE_PUBLISH_RESULT, pReq, pRsp); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index e8f908bca..9c927d92d 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 diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 6f0fc375c..534e94ea6 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -51,7 +51,7 @@ SERVER_DEVICE_DISCOVERY, SERVER_PUBLISH_FINISH, SERVER_AUTH_RESULT, SERVER_DEVIC SERVER_CREATE_PIN_HOLDER, SERVER_DESTROY_PIN_HOLDER, SERVER_CREATE_PIN_HOLDER_RESULT, SERVER_DESTROY_PIN_HOLDER_RESULT, SERVER_ON_PIN_HOLDER_EVENT, UNBIND_TARGET_RESULT, REMOTE_DEVICE_TRUST_CHANGE, SERVER_DEVICE_SCREEN_STATE_NOTIFY, SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, SINK_BIND_TARGET_RESULT, GET_DEVICE_PROFILE_INFO_LIST_RESULT, -GET_DEVICE_ICON_INFO_RESULT, SET_LOCAL_DEVICE_NAME_RESULT, SET_REMOTE_DEVICE_NAME_RESULT }; +GET_DEVICE_ICON_INFO_RESULT, SET_LOCAL_DEVICE_NAME_RESULT, SET_REMOTE_DEVICE_NAME_RESULT, SERVICE_PUBLISH_RESULT }; } DM_IMPLEMENT_SINGLE_INSTANCE(IpcServerStub); -- Gitee From 1f452198d313ec26f408ee1dc37e176f1e637875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Fri, 29 Aug 2025 10:29:49 +0800 Subject: [PATCH 2/2] modify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../inner_kits/native_cpp/include/i_dm_service_impl_ext.h | 2 +- .../native_cpp/include/i_dm_service_impl_ext_resident.h | 2 +- .../implementation/src/authentication_v2/dm_auth_state.cpp | 5 +++++ services/service/src/device_manager_service.cpp | 1 - 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h index b3debcc17..730b2a1e0 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h @@ -110,7 +110,7 @@ public: virtual int32_t StartServiceDiscovery(const ProcessInfo &processInfo, const DiscoveryServiceParam &discParam) = 0; virtual int32_t StopServiceDiscovery(int32_t discServiceId) = 0; virtual int32_t OpenAuthSessionWithPara(const int64_t serviceId) = 0; - virtual int32_t StartPublishService(const ProcessInfo &processInfo, PublishServiceParam &publishServiceParam) = 0; + virtual int32_t StartPublishService(const ProcessInfo &processInfo, const PublishServiceParam &publishServiceParam) = 0; virtual int32_t StopPublishService(int64_t serviceId) = 0; }; diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index c13cca792..d83e0fbde 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -116,7 +116,7 @@ public: virtual int32_t StartServiceDiscovery(const ProcessInfo &processInfo, const DiscoveryServiceParam &discParam) = 0; virtual int32_t StopServiceDiscovery(int32_t discServiceId) = 0; virtual int32_t OpenAuthSessionWithPara(const int64_t serviceId) = 0; - virtual int32_t StartPublishService(const ProcessInfo &processInfo, PublishServiceParam &publishServiceParam) = 0; + virtual int32_t StartPublishService(const ProcessInfo &processInfo, const PublishServiceParam &publishServiceParam) = 0; virtual int32_t StopPublishService(int64_t serviceId) = 0; }; diff --git a/services/implementation/src/authentication_v2/dm_auth_state.cpp b/services/implementation/src/authentication_v2/dm_auth_state.cpp index 5d5698a4d..4d2debb3e 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state.cpp @@ -986,6 +986,11 @@ void DmAuthState::JoinLnn(std::shared_ptr context) LOGI("The remote device is offline."); isForceJoin = true; } + if (context->isServiceBind) { + context->softbusConnector->JoinLnnByHml(context->sessionId, context->accesser.transmitSessionKeyId, + context->accessee.transmitSessionKeyId, isForceJoin); + return; + } if (context->connSessionType == CONN_SESSION_TYPE_HML) { context->softbusConnector->JoinLnnByHml(context->sessionId, context->accesser.transmitSessionKeyId, context->accessee.transmitSessionKeyId, isForceJoin); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index c892843b7..5cccac714 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4465,7 +4465,6 @@ int32_t DeviceManagerService::UnbindServiceTarget(const std::string &pkgName, in return dmServiceImpl_->UnbindServiceTarget(pkgName, serviceId); } - int32_t DeviceManagerService::RegisterServiceInfo(const ServiceRegInfo &serviceRegInfo, int32_t ®ServiceId) { #ifdef DEVICE_MANAGER_COMMON_FLAG -- Gitee