From 28aad9d95fecd5f9e6a2b5aef915acfaa48f653d Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Fri, 29 Jul 2022 11:06:29 +0800 Subject: [PATCH 1/8] add DHFWK publisher Signed-off-by: hwzhangchuang --- bundle.json | 3 +- .../include/distributed_hardware_errno.h | 6 +- common/utils/include/i_publisher_listener.h | 53 +++++++ common/utils/include/idistributed_hardware.h | 15 +- common/utils/include/single_instance.h | 7 + interfaces/inner_kits/BUILD.gn | 58 +++++++ .../include/distributed_hardware_fwk_kit.h | 55 +++++++ .../inner_kits/include/ipc/dhfwk_sa_manager.h | 60 ++++++++ .../include/ipc/publisher_listener_proxy.h | 38 +++++ .../include/ipc/publisher_listener_stub.h | 36 +++++ .../src/distributed_hardware_fwk_kit.cpp | 131 ++++++++++++++++ .../inner_kits/src/ipc/dhfwk_sa_manager.cpp | 141 ++++++++++++++++++ .../src/ipc/publisher_listener_proxy.cpp | 63 ++++++++ .../src/ipc/publisher_listener_stub.cpp | 53 +++++++ .../distributedhardwarefwkservice/BUILD.gn | 3 + .../include/distributed_hardware_proxy.h | 4 +- .../include/distributed_hardware_service.h | 3 + .../include/distributed_hardware_stub.h | 5 + .../include/publisher/publisher.h | 40 +++++ .../include/publisher/publisher_item.h | 45 ++++++ .../src/distributed_hardware_proxy.cpp | 117 ++++++++++++++- .../src/distributed_hardware_service.cpp | 32 +++- .../src/distributed_hardware_stub.cpp | 105 ++++++++++--- .../src/publisher/publisher.cpp | 48 ++++++ .../src/publisher/publisher_item.cpp | 68 +++++++++ 25 files changed, 1154 insertions(+), 35 deletions(-) create mode 100644 common/utils/include/i_publisher_listener.h create mode 100644 interfaces/inner_kits/BUILD.gn create mode 100644 interfaces/inner_kits/include/distributed_hardware_fwk_kit.h create mode 100644 interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h create mode 100644 interfaces/inner_kits/include/ipc/publisher_listener_proxy.h create mode 100644 interfaces/inner_kits/include/ipc/publisher_listener_stub.h create mode 100644 interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp create mode 100644 interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp create mode 100644 interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp create mode 100644 interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp create mode 100644 services/distributedhardwarefwkservice/include/publisher/publisher.h create mode 100644 services/distributedhardwarefwkservice/include/publisher/publisher_item.h create mode 100644 services/distributedhardwarefwkservice/src/publisher/publisher.cpp create mode 100644 services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp diff --git a/bundle.json b/bundle.json index 1e6faf8c..8f00567a 100644 --- a/bundle.json +++ b/bundle.json @@ -54,7 +54,8 @@ "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhfwk_sa_profile", - "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg" + "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits:libdhfwk_sdk" ], "test": [ "//foundation/distributedhardware/distributed_hardware_fwk/utils/test/unittest:utils_test", diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a4378588..3fc32ef7 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -83,6 +83,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_SERVICE_LOCAL_VERSION_NOT_EXIST = -10804; constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL = -10805; constexpr int32_t ERR_DH_FWK_SERVICE_REMOTE_IS_NULL = -10806; + constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL = -10807; /* AccessManager errno, range: [-10900, -10999] */ constexpr int32_t ERR_DH_FWK_ACCESS_INIT_DM_FAILED = -10900; @@ -90,10 +91,13 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED = -10902; constexpr int32_t ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED = -10903; - /* Hidump errno, range: [-11000, -11000] */ + /* Hidump errno, range: [-11000, -11099] */ constexpr int32_t ERR_DH_FWK_HIDUMP_ERROR = -11000; constexpr int32_t ERR_DH_FWK_HIDUMP_INVALID_ARGS = -11001; constexpr int32_t ERR_DH_FWK_HIDUMP_DPRINTF_ERROR = -11002; + + /* DHFWK Publisher errno, range: [-11100, -11199] */ + constexpr int32_t ERR_DH_FWK_PUBLISH_MSG_FAILED = -11100; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/common/utils/include/i_publisher_listener.h b/common/utils/include/i_publisher_listener.h new file mode 100644 index 00000000..b227c55d --- /dev/null +++ b/common/utils/include/i_publisher_listener.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 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 I_PUBLISHER_LISTENER_H +#define I_PUBLISHER_LISTENER_H + +#include +#include + +#include + +namespace OHOS { +namespace DistributedHardware { +enum class DHTopic : uint32_t { + // Topic min border, not use for real topic + TOPIC_MIN = 0, + // Start project distributed screen + TOPIC_START_DSCREEN = 1, + // Publish Sink Project Window Info + TOPIC_SINK_PROJECT_WINDOW_INFO = 2, + // Stop distributed screen project + TOPIC_STOP_DSCREEN = 3, + // publish device offline message + TOPIC_DEV_OFFLINE = 4, + // Topic max border, not use for real topic + TOPIC_MAX = 5 +}; + +class IPublisherListener : public IRemoteBroker { +public: + virtual void OnMessage(const DHTopic topic, const std::string& message) = 0; + + enum class Message : uint32_t { + ON_MESSAGE, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedHardwareFwk.IPublisherListener"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // I_PUBLISHER_LISTENER_H \ No newline at end of file diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index c0db4318..860c5878 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -16,11 +16,15 @@ #ifndef OHOS_IDISTRIBUTED_HARDWARE_H #define OHOS_IDISTRIBUTED_HARDWARE_H +#include +#include #include #include "iremote_broker.h" #include "device_type.h" +#include "i_publisher_listener.h" + namespace OHOS { namespace DistributedHardware { class IDistributedHardware : public OHOS::IRemoteBroker { @@ -29,10 +33,15 @@ public: IDistributedHardware() = default; virtual ~IDistributedHardware() = default; virtual int32_t QuerySinkVersion(std::unordered_map &versionMap) = 0; - + virtual int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) = 0; + virtual int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) = 0; + virtual int32_t PublishMessage(const DHTopic topic, const std::string &msg) = 0; public: - enum { - QUERY_SINK_VERSION = 1 + enum class Message : uint32_t { + QUERY_SINK_VERSION = 1, + REG_PUBLISHER_LISTNER = 2, + UNREG_PUBLISHER_LISTENER = 3, + PUBLISH_MESSAGE = 4 }; }; } // namespace DistributedHardware diff --git a/common/utils/include/single_instance.h b/common/utils/include/single_instance.h index 78da97ce..f70cfbbc 100644 --- a/common/utils/include/single_instance.h +++ b/common/utils/include/single_instance.h @@ -18,6 +18,13 @@ namespace OHOS { namespace DistributedHardware { +#define REMOVE_NO_USE_CONSTRUCTOR(className) \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + #define DECLARE_SINGLE_INSTANCE_BASE(className) \ public: \ static className & GetInstance(); \ diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn new file mode 100644 index 00000000..d6f09b3d --- /dev/null +++ b/interfaces/inner_kits/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2022 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. + +import("//build/ohos.gni") +import("//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +ohos_shared_library("libdhfwk_sdk") { + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + "${common_path}/log/include", + "${common_path}/utils/include", + "${utils_path}/include/log", + ] + sources = [ + "${innerkits_path}/src/distributed_hardware_fwk_kit.cpp", + "${innerkits_path}/src/ipc/dhfwk_sa_manager.cpp", + "${innerkits_path}/src/ipc/publisher_listener_proxy.cpp", + "${innerkits_path}/src/ipc/publisher_listener_stub.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "${utils_path}:distributedhardwareutils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dhfwkkit\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "ipc:ipc_core", + "safwk:system_ability_fwk", + "hiviewdfx_hilog_native:libhilog", + "samgr_standard:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_hardware_fwk" +} diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h new file mode 100644 index 00000000..f9bd790f --- /dev/null +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 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 DISTRIBUTED_HARDWARE_FWK_KIT_H +#define DISTRIBUTED_HARDWARE_FWK_KIT_H + +#include +#include +#include +#include +#include +#include "refbase.h" + +#include "i_publisher_listener.h" +#include "idistributed_hardware.h" + +#ifndef API_EXPORT +#define API_EXPORT __attribute__((visibility("default"))) +#endif + +namespace OHOS { +namespace DistributedHardware { + +class DistributedHardwareFwkKit final { +public: + API_EXPORT DistributedHardwareFwkKit(); + API_EXPORT ~DistributedHardwareFwkKit(); + API_EXPORT int32_t RegisterPublisherListener(const DHTopic topic, sptr listener); + API_EXPORT int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener); + API_EXPORT int32_t PublishMessage(const DHTopic topic, const std::string &message); + + void OnDHFWKOnLine(bool isOnLine); +private: + bool IsDHTopicValid(DHTopic topic); + +private: + std::mutex listenerMutex_; + std::unordered_map>> listenerMap_; + std::atomic isDHFWKOnLine_; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h new file mode 100644 index 00000000..d022ae5a --- /dev/null +++ b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022 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 DHFWK_SA_MANAGER_H +#define DHFWK_SA_MANAGER_H + +#include +#include +#include + +#include "refbase.h" +#include "system_ability_status_change_stub.h" + +#include "single_instance.h" +#include "idistributed_hardware.h" + +namespace OHOS { +namespace DistributedHardware { +// DHFWK sa state callback, param true for started, false for stopped. +using DHFWKSAStateCb = std::function; +class DHFWKSAManager { +DECLARE_SINGLE_INSTANCE_BASE(DHFWKSAManager); +public: + DHFWKSAManager(); + virtual ~DHFWKSAManager(); + void RegisterAbilityListener(); + sptr GetDHFWKProxy(); + void RegisterSAStateCallback(DHFWKSAStateCb callback); + +public: +class SystemAbilityListener : public SystemAbilityStatusChangeStub { +public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; +}; + +private: + std::atomic dhfwkOnLine_; + std::atomic isSubscribeDHFWKSAChangeListener; + std::mutex proxyMutex_; + sptr dhfwkProxy_; + sptr saListener_; + std::mutex saStatCbMutex_; + DHFWKSAStateCb saStateCallback; +}; +} // DistributedHardware +} // OHOS +#endif // DHFWK_SA_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/publisher_listener_proxy.h b/interfaces/inner_kits/include/ipc/publisher_listener_proxy.h new file mode 100644 index 00000000..6ae0147d --- /dev/null +++ b/interfaces/inner_kits/include/ipc/publisher_listener_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 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 PUBLISHER_LISTENER_PROXY_H +#define PUBLISHER_LISTENER_PROXY_H + +#include "i_publisher_listener.h" + +#include "iremote_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class PublisherListenerProxy : public IRemoteProxy { +public: + explicit PublisherListenerProxy(const sptr &object); + + virtual ~PublisherListenerProxy() override; + + virtual void OnMessage(const DHTopic topic, const std::string& message) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // DistributedHardware +} // OHOS +#endif // PUBLISHER_LISTENER_PROXY_H \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/publisher_listener_stub.h b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h new file mode 100644 index 00000000..e8c2d6cf --- /dev/null +++ b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 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 PUBLISHER_LISTENER_STUB_H +#define PUBLISHER_LISTENER_STUB_H + +#include "i_publisher_listener.h" + +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class PublisherListenerStub : public IRemoteStub { +public: + PublisherListenerStub(); + virtual ~PublisherListenerStub() override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + DISALLOW_COPY_AND_MOVE(PublisherListenerStub); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp new file mode 100644 index 00000000..64ec531c --- /dev/null +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_hardware_fwk_kit.h" + +#include + +#include "dhfwk_sa_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "idistributed_hardware.h" + +namespace OHOS { +namespace DistributedHardware { +DistributedHardwareFwkKit::DistributedHardwareFwkKit() : listenerMap_({}), isDHFWKOnLine_(false) +{ + DHLOGI("Ctor DistributedHardwareFwkKit"); + DHFWKSAManager::GetInstance().RegisterAbilityListener(); + DHFWKSAManager::GetInstance().RegisterSAStateCallback( + std::bind(&DistributedHardwareFwkKit::OnDHFWKOnLine, this, std::placeholders::_1)); +} + +DistributedHardwareFwkKit::~DistributedHardwareFwkKit() +{ + DHLOGI("Dtor DistributedHardwareFwkKit"); +} + +int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr listener) +{ + DHLOGI("Register publisher listener, topic: %" PRIu32, (uint32_t)topic); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: " PRIu32 , (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + + if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); + DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret); + } else { + DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); + std::lock_guard lock(listenerMutex_); + listenerMap_[topic].insert(listener); + } + + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr listener) +{ + DHLOGI("Unregister publisher listener, topic: %" PRIu32, (uint32_t)topic); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: " PRIu32 , (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + + if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + DHLOGI("Unregister publisher listener to DHFWK"); + DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); + } + std::lock_guard lock(listenerMutex_); + listenerMap_[topic].erase(listener); + + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message) +{ + DHLOGI("Publish message, topic: %" PRIu32 ", msg: %s", (uint32_t)topic, message.c_str()); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: " PRIu32 , (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + + if (!isDHFWKOnLine_ || DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online, can not publish message"); + return ERR_DH_FWK_PUBLISH_MSG_FAILED; + } + + DHLOGI("Publish message to DHFWK"); + DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message); + + return DH_FWK_SUCCESS; +} + +bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic) +{ + return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX; +} + +void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine) +{ + DHLOGI("Receive DHFWK online callback, %s", (isOnLine ? "true" : "false")); + isDHFWKOnLine_ = isOnLine; + + if (!isDHFWKOnLine_ || DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + return; + } + + DHLOGI("DHFWK online, register saved listener to it"); + std::unordered_map>> regSuccListeners; + std::lock_guard lock(listenerMutex_); + for (const auto &entry : listenerMap_) { + for (const auto &listener : entry.second) { + int32_t ret = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); + if (ret == 0) { + regSuccListeners[entry.first].insert(listener); + } + } + } + + for (const auto &succ : regSuccListeners) { + for (const auto &listener : succ.second) { + listenerMap_[succ.first].erase(listener); + } + } +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp new file mode 100644 index 00000000..e2fe4a46 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dhfwk_sa_manager.h" + +#include + +#include "iservice_registry.h" +#include "system_ability_definition.h" + +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DHFWKSAManager); +DHFWKSAManager::DHFWKSAManager() + : dhfwkOnLine_(false), isSubscribeDHFWKSAChangeListener(false), dhfwkProxy_(nullptr), + saListener_(new SystemAbilityListener()), saStateCallback(nullptr) +{ + DHLOGI("Ctor DHFWKSAManager"); +} +DHFWKSAManager::~DHFWKSAManager() +{ + DHLOGI("Dtor DHFWKSAManager"); + dhfwkOnLine_ = false; + isSubscribeDHFWKSAChangeListener = false; + dhfwkProxy_ = nullptr; + saListener_ = nullptr; +} + +void DHFWKSAManager::RegisterAbilityListener() +{ + DHLOGI("Register DHFWK sa listener"); + sptr saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + DHLOGE("Get System Ability Manager failed"); + return; + } + + if (!isSubscribeDHFWKSAChangeListener) { + DHLOGI("try subscribe sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_SA_ID); + int32_t ret = saMgr->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, saListener_); + if (ret != 0) { + DHLOGE("subscribe DHFWK sa change listener failed, ret: %d", ret); + return; + } + isSubscribeDHFWKSAChangeListener = true; + } +} + +sptr DHFWKSAManager::GetDHFWKProxy() +{ + if (!isSubscribeDHFWKSAChangeListener) { + RegisterAbilityListener(); + } + + if (!dhfwkOnLine_) { + DHLOGW("DHFWK not online, return"); + return nullptr; + } + + std::lock_guard lock(proxyMutex_); + if (dhfwkProxy_ == nullptr) { + sptr saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + DHLOGE("Get System Ability Manager failed"); + return nullptr; + } + DHLOGI("Try get DHFWK sa"); + sptr remoteObject = saMgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SA_ID); + if (remoteObject == nullptr) { + DHLOGE("Get DHFWK proxy return null"); + return nullptr; + } + + dhfwkProxy_ = iface_cast(remoteObject); + } + + return dhfwkProxy_; +} + +void DHFWKSAManager::RegisterSAStateCallback(DHFWKSAStateCb callback) +{ + std::lock_guard lock(saStatCbMutex_); + saStateCallback = callback; +} + +void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + (void)deviceId; + if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { + DHLOGW("Receive SA Start, but sa id is not DHFWK, id: %" PRId32, systemAbilityId); + return; + } + + DHFWKSAManager::GetInstance().dhfwkOnLine_ = true; + { + std::lock_guard lock(DHFWKSAManager::GetInstance().saStatCbMutex_); + if (DHFWKSAManager::GetInstance().saStateCallback != nullptr) { + DHFWKSAManager::GetInstance().saStateCallback(true); + } + } + DHLOGI("sa %" PRId32 " started", systemAbilityId); +} + +void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + (void)deviceId; + if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { + DHLOGW("Receive SA Stop, but sa id is not DHFWK, id: %" PRId32, systemAbilityId); + return; + } + + DHFWKSAManager::GetInstance().dhfwkOnLine_ = false; + { + std::lock_guard lock(DHFWKSAManager::GetInstance().proxyMutex_); + DHFWKSAManager::GetInstance().dhfwkProxy_ = nullptr; + } + { + std::lock_guard lock(DHFWKSAManager::GetInstance().saStatCbMutex_); + if (DHFWKSAManager::GetInstance().saStateCallback != nullptr) { + DHFWKSAManager::GetInstance().saStateCallback(false); + } + } + DHLOGI("sa %" PRId32 " stopped", systemAbilityId); +} + +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp b/interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp new file mode 100644 index 00000000..704675e9 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisher_listener_proxy.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +PublisherListenerProxy::PublisherListenerProxy(const sptr &object) + : IRemoteProxy(object) +{ +} + +PublisherListenerProxy::~PublisherListenerProxy() +{ +} + +void PublisherListenerProxy::OnMessage(const DHTopic topic, const std::string& message) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed"); + return; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("PublisherListenerProxy write token failed"); + return; + } + + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("Parcel write topic failed"); + return; + } + + if (!data.WriteString(message)) { + DHLOGE("Parcel write message failed"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IPublisherListener::Message::ON_MESSAGE), data, reply, option); + if (ret != 0) { + DHLOGE("PublisherListenerProxy send requeset failed, ret: %d", ret); + return; + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp new file mode 100644 index 00000000..84be82c9 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisher_listener_stub.h" + +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +PublisherListenerStub::PublisherListenerStub() +{ +} + +PublisherListenerStub::~PublisherListenerStub() +{ +} + +int32_t PublisherListenerStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("PublisherListenerStub read valid token failed"); + return ERR_INVALID_DATA; + } + IPublisherListener::Message msgCode = static_cast(code); + switch (msgCode) { + case IPublisherListener::Message::ON_MESSAGE: { + DHTopic topic = (DHTopic)data.ReadUint32(); + std::string message = data.ReadString(); + OnMessage(topic, message); + break; + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 977bec65..32ffdbf3 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "include/task", "include/utils", "include/localhardwaremanager", + "include/publisher", "include/resourcemanager", "include/hidumphelper", "${utils_path}/include/eventbus", @@ -55,6 +56,8 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/hidumphelper/hidump_helper.cpp", "src/localhardwaremanager/local_hardware_manager.cpp", "src/localhardwaremanager/plugin_listener_impl.cpp", + "src/publisher/publisher_item.cpp", + "src/publisher/publisher.cpp", "src/resourcemanager/capability_info.cpp", "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h b/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h index 13bea7e2..77b8e240 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h @@ -33,10 +33,12 @@ public: virtual ~DistributedHardwareProxy() {} int32_t QuerySinkVersion(std::unordered_map &versionMap) override; + int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; private: std::unordered_map FromJson(const std::string &json) const; - static inline BrokerDelegator delegator_; }; } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h index 21c671f5..b11d5dbc 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -35,6 +35,9 @@ public: DistributedHardwareService(int32_t saId, bool runOnCreate); ~DistributedHardwareService() = default; int32_t QuerySinkVersion(std::unordered_map &versionMap) override; + int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; int Dump(int32_t fd, const std::vector& args) override; protected: diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index 80d1d268..774bf562 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -24,6 +24,11 @@ public: int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: + int32_t QuerySinkVersionInner(MessageParcel &reply); + int32_t RegisterPublisherListenerInner(MessageParcel &data); + int32_t UnregisterPublisherListenerInner(MessageParcel &data); + int32_t PublishMessageInner(MessageParcel &data); + bool ValidTopic(uint32_t topic); std::string ToJson(const std::unordered_map &versionMap) const; }; } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkservice/include/publisher/publisher.h b/services/distributedhardwarefwkservice/include/publisher/publisher.h new file mode 100644 index 00000000..ca0fef59 --- /dev/null +++ b/services/distributedhardwarefwkservice/include/publisher/publisher.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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 PUBLISHER_H +#define PUBLISHER_H + +#include +#include + +#include "i_publisher_listener.h" +#include "publisher_item.h" + +namespace OHOS { +namespace DistributedHardware { +class Publisher { +DECLARE_SINGLE_INSTANCE_BASE(Publisher); +public: + virtual ~Publisher(); + void RegisterListener(const DHTopic topic, const sptr &listener); + void UnregisterListener(const DHTopic topic, const sptr &listener); + void PublishMessage(const DHTopic topic, const std::string &message); +private: + Publisher(); + std::unordered_map> publisherItems_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/publisher/publisher_item.h b/services/distributedhardwarefwkservice/include/publisher/publisher_item.h new file mode 100644 index 00000000..8c7c4e45 --- /dev/null +++ b/services/distributedhardwarefwkservice/include/publisher/publisher_item.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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 PUBLISHER_ITEM_H +#define PUBLISHER_ITEM_H +#include +#include +#include + +#include "refbase.h" + +#include "i_publisher_listener.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class PublisherItem { +REMOVE_NO_USE_CONSTRUCTOR(PublisherItem); +public: + PublisherItem(); + explicit PublisherItem(DHTopic topic); + virtual ~PublisherItem(); + void AddListener(const sptr &listener); + void RemoveListener(const sptr &listener); + void PublishMessage(const std::string &message); +private: + DHTopic topic_; + std::mutex mutex_; + std::set> listeners_; +}; +} +} +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp index a1f797bd..01babc20 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp @@ -35,6 +35,12 @@ const std::unordered_set DH_TYPE_SET { int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_map &versionMap) { + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; MessageParcel reply; MessageOption option; @@ -43,12 +49,9 @@ int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_map remote = Remote(); - if (remote == nullptr) { - DHLOGE("remote service is null"); - return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; - } - int32_t error = remote->SendRequest(QUERY_SINK_VERSION, data, reply, option); + + int32_t error = remote->SendRequest((uint32_t)IDistributedHardware::Message::QUERY_SINK_VERSION, + data, reply, option); if (error != NO_ERROR) { DHLOGE("SendRequest failed, errCode = %d", error); return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; @@ -63,6 +66,108 @@ int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_map listener) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::REG_PUBLISHER_LISTNER, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topic, sptr listener) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std::string &msg) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(msg)) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return DH_FWK_SUCCESS; +} + void from_json(const nlohmann::json &jsonObj, std::unordered_map &versionMap) { for (const auto &item : jsonObj.value(DH_COMPONENT_VERSIONS, nlohmann::json {})) { diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index fecf6941..8bd6521d 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -26,6 +26,8 @@ #include "dh_utils_hisysevent.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "distributed_hardware_manager_factory.h" +#include "publisher.h" namespace OHOS { namespace DistributedHardware { @@ -89,8 +91,34 @@ void DistributedHardwareService::OnStop() int32_t DistributedHardwareService::QuerySinkVersion(std::unordered_map &versionMap) { - (void)versionMap; - return 0; + auto ret = DistributedHardwareManagerFactory::GetInstance().GetComponentVersion(versionMap); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetComponentVersion failed, errCode = %d", ret); + return ret; + } + if (versionMap.empty()) { + DHLOGE("versionMap is empty"); + return ERR_DH_FWK_SERVICE_LOCAL_VERSION_NOT_EXIST; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, sptr listener) +{ + Publisher::GetInstance().RegisterListener(topic, listener); + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, sptr listener) +{ + Publisher::GetInstance().UnregisterListener(topic, listener); + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::PublishMessage(const DHTopic topic, const std::string &msg) +{ + Publisher::GetInstance().PublishMessage(topic, msg); + return DH_FWK_SUCCESS; } int DistributedHardwareService::Dump(int32_t fd, const std::vector& args) diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index 5db8573c..f407ebee 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -15,44 +15,107 @@ #include "distributed_hardware_stub.h" +#include + +#include "nlohmann/json.hpp" + #include "anonymous_string.h" #include "constants.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" -#include "distributed_hardware_manager_factory.h" -#include "nlohmann/json.hpp" + namespace OHOS { namespace DistributedHardware { int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { if (data.ReadInterfaceToken() != GetDescriptor()) { - DHLOGE("ReadInterfaceToken fail!"); + DHLOGE("IPC Token valid fail!"); return ERR_INVALID_DATA; } switch (code) { - case QUERY_SINK_VERSION: { - std::unordered_map versionMap; - auto ret = DistributedHardwareManagerFactory::GetInstance().GetComponentVersion(versionMap); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("GetComponentVersion failed, errCode = %d", ret); - return ret; - } - if (versionMap.empty()) { - DHLOGE("versionMap is empty"); - return ERR_DH_FWK_SERVICE_LOCAL_VERSION_NOT_EXIST; - } - auto version = ToJson(versionMap); - if (!reply.WriteString(version)) { - DHLOGE("write version failed"); - return ERR_DH_FWK_SERVICE_IPC_WRITE_PARA_FAIL; - } - return DH_FWK_SUCCESS; + case (uint32_t)IDistributedHardware::Message::QUERY_SINK_VERSION: { + return QuerySinkVersionInner(reply); + } + case (uint32_t)IDistributedHardware::Message::REG_PUBLISHER_LISTNER: { + return RegisterPublisherListenerInner(data); + } + case (uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER: { + return UnregisterPublisherListenerInner(data); + } + case (uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE: { + return PublishMessageInner(data); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } - return 0; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::QuerySinkVersionInner(MessageParcel &reply) +{ + std::unordered_map versionMap; + QuerySinkVersion(versionMap); + auto version = ToJson(versionMap); + if (!reply.WriteString(version)) { + DHLOGE("write version failed"); + return ERR_DH_FWK_SERVICE_IPC_WRITE_PARA_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data) +{ + uint32_t topicInt = data.ReadUint32(); + if (!ValidTopic(topicInt)) { + DHLOGE("Topic invalid: %" PRIu32, topicInt); + return ERR_DH_FWK_PARA_INVALID; + } + + DHTopic topic = (DHTopic)topicInt; + sptr listener = iface_cast(data.ReadRemoteObject()); + DHLOGI("Register listener, topic: %" PRIu32 , (uint32_t)topic); + RegisterPublisherListener(topic, listener); + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data) +{ + uint32_t topicInt = data.ReadUint32(); + if (!ValidTopic(topicInt)) { + DHLOGE("Topic invalid: %" PRIu32, topicInt); + return ERR_DH_FWK_PARA_INVALID; + } + + DHTopic topic = (DHTopic)topicInt; + sptr listener = iface_cast(data.ReadRemoteObject()); + DHLOGI("Unregister listener, topic: %" PRIu32 , (uint32_t)topic); + UnregisterPublisherListener(topic, listener); + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data) +{ + uint32_t topicInt = data.ReadUint32(); + if (!ValidTopic(topicInt)) { + DHLOGE("Topic invalid: %" PRIu32, topicInt); + return ERR_DH_FWK_PARA_INVALID; + } + + DHTopic topic = (DHTopic)topicInt; + std::string message = data.ReadString(); + DHLOGI("Publish Message, topic: %" PRIu32 ", message: %s", (uint32_t)topic, message.c_str()); + PublishMessage(topic, message); + return DH_FWK_SUCCESS; +} + +bool DistributedHardwareStub::ValidTopic(uint32_t topic) +{ + if (topic <= (uint32_t)DHTopic::TOPIC_MIN || topic >= (uint32_t)DHTopic::TOPIC_MAX) { + return false; + } + + return true; } std::string DistributedHardwareStub::ToJson(const std::unordered_map &versionMap) const diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp new file mode 100644 index 00000000..0b3a5994 --- /dev/null +++ b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisher.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(Publisher); +Publisher::Publisher() : publisherItems_({ + { DHTopic::TOPIC_START_DSCREEN, std::make_shared(DHTopic::TOPIC_START_DSCREEN) }, + { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, std::make_shared(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) }, + { DHTopic::TOPIC_STOP_DSCREEN, std::make_shared(DHTopic::TOPIC_STOP_DSCREEN) } + }) +{ +} + +Publisher::~Publisher() +{ +} + +void Publisher::RegisterListener(const DHTopic topic, const sptr &listener) +{ + publisherItems_[topic]->AddListener(listener); +} + +void Publisher::UnregisterListener(const DHTopic topic, const sptr &listener) +{ + publisherItems_[topic]->RemoveListener(listener); +} + +void Publisher::PublishMessage(const DHTopic topic, const std::string &message) +{ + publisherItems_[topic]->PublishMessage(message); +} +} //namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp new file mode 100644 index 00000000..b53c9d7a --- /dev/null +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisher_item.h" + +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +PublisherItem::PublisherItem() : topic_(DHTopic::TOPIC_MIN) +{ +} + +PublisherItem::PublisherItem(DHTopic topic) : topic_(topic) +{ + DHLOGE("Ctor PublisherItem, topic: %d", topic); +} + +PublisherItem::~PublisherItem() +{ + DHLOGE("Dtor PublisherItem, topic: %d", topic_); + std::lock_guard lock(mutex_); + listeners_.clear(); +} + +void PublisherItem::AddListener(const sptr &listener) +{ + if (listener == nullptr) { + DHLOGE("Add null publisher listener"); + return; + } + + std::lock_guard lock(mutex_); + listeners_.insert(listener); +} + +void PublisherItem::RemoveListener(const sptr &listener) +{ + if (listener == nullptr) { + DHLOGE("Remove null publisher listener"); + return; + } + + std::lock_guard lock(mutex_); + listeners_.erase(listener); +} + +void PublisherItem::PublishMessage(const std::string &message) +{ + std::lock_guard lock(mutex_); + for (const auto &listener : listeners_) { + listener->OnMessage(topic_, message); + } +} +} // DistributedHardware +} // OHOS \ No newline at end of file -- Gitee From 838c0a081d7e899d4d6f80591933e67f4f6b4e93 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 16:22:57 +0800 Subject: [PATCH 2/8] modify problem Signed-off-by: hwzhangchuang --- common/utils/include/idistributed_hardware.h | 12 ++-- interfaces/inner_kits/BUILD.gn | 7 ++- .../include/ipc}/distributed_hardware_proxy.h | 5 +- .../src/distributed_hardware_fwk_kit.cpp | 18 +++--- .../inner_kits/src/ipc/dhfwk_sa_manager.cpp | 7 ++- .../src/ipc}/distributed_hardware_proxy.cpp | 60 +++++++------------ .../distributedhardwarefwkservice/BUILD.gn | 3 +- .../componentmanager/component_manager.h | 2 - .../include/distributed_hardware_service.h | 5 +- .../include/distributed_hardware_stub.h | 7 +-- .../include/ipc/publisher_listener_proxy.h | 0 .../componentmanager/component_manager.cpp | 36 ----------- .../src/distributed_hardware_service.cpp | 18 +----- .../src/distributed_hardware_stub.cpp | 44 +++++++------- .../src/ipc/publisher_listener_proxy.cpp | 0 .../src/publisher/publisher.cpp | 3 +- .../src/publisher/publisher_item.cpp | 15 +++-- 17 files changed, 95 insertions(+), 147 deletions(-) rename {services/distributedhardwarefwkservice/include => interfaces/inner_kits/include/ipc}/distributed_hardware_proxy.h (82%) rename {services/distributedhardwarefwkservice/src => interfaces/inner_kits/src/ipc}/distributed_hardware_proxy.cpp (81%) rename {interfaces/inner_kits => services/distributedhardwarefwkservice}/include/ipc/publisher_listener_proxy.h (100%) rename {interfaces/inner_kits => services/distributedhardwarefwkservice}/src/ipc/publisher_listener_proxy.cpp (100%) diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index 860c5878..6d252fc2 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -32,16 +32,14 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.distributedhardwarefwk"); IDistributedHardware() = default; virtual ~IDistributedHardware() = default; - virtual int32_t QuerySinkVersion(std::unordered_map &versionMap) = 0; - virtual int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) = 0; - virtual int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) = 0; + virtual int32_t RegisterPublisherListener(const DHTopic topic, const sptr &listener) = 0; + virtual int32_t UnregisterPublisherListener(const DHTopic topic, const sptr &listener) = 0; virtual int32_t PublishMessage(const DHTopic topic, const std::string &msg) = 0; public: enum class Message : uint32_t { - QUERY_SINK_VERSION = 1, - REG_PUBLISHER_LISTNER = 2, - UNREG_PUBLISHER_LISTENER = 3, - PUBLISH_MESSAGE = 4 + REG_PUBLISHER_LISTNER = 1, + UNREG_PUBLISHER_LISTENER = 2, + PUBLISH_MESSAGE = 3 }; }; } // namespace DistributedHardware diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index d6f09b3d..14c65ede 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -22,12 +22,16 @@ ohos_shared_library("libdhfwk_sdk") { "${innerkits_path}/include/ipc", "${common_path}/log/include", "${common_path}/utils/include", + "${utils_path}/include", "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkservice/include", + "//third_party/json/include", ] + sources = [ "${innerkits_path}/src/distributed_hardware_fwk_kit.cpp", "${innerkits_path}/src/ipc/dhfwk_sa_manager.cpp", - "${innerkits_path}/src/ipc/publisher_listener_proxy.cpp", + "${innerkits_path}/src/ipc/distributed_hardware_proxy.cpp", "${innerkits_path}/src/ipc/publisher_listener_stub.cpp", ] @@ -35,6 +39,7 @@ ohos_shared_library("libdhfwk_sdk") { "//utils/native/base:utils", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", "${utils_path}:distributedhardwareutils", + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", ] defines = [ diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h similarity index 82% rename from services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h rename to interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 77b8e240..53ba416a 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -32,9 +32,8 @@ public: } virtual ~DistributedHardwareProxy() {} - int32_t QuerySinkVersion(std::unordered_map &versionMap) override; - int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) override; - int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t RegisterPublisherListener(const DHTopic topic, const sptr &listener) override; + int32_t UnregisterPublisherListener(const DHTopic topic, const sptr &listener) override; int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; private: diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 64ec531c..c0bf17d6 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -45,8 +45,9 @@ int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic return ERR_DH_FWK_PARA_INVALID; } + int32_t ret = DH_FWK_SUCCESS; if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { - int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); + ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret); } else { DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); @@ -54,7 +55,7 @@ int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic listenerMap_[topic].insert(listener); } - return DH_FWK_SUCCESS; + return ret; } int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr listener) @@ -65,14 +66,15 @@ int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic top return ERR_DH_FWK_PARA_INVALID; } + int32_t ret = DH_FWK_SUCCESS; if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { - DHLOGI("Unregister publisher listener to DHFWK"); - DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); + ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); + DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret); } std::lock_guard lock(listenerMutex_); listenerMap_[topic].erase(listener); - return DH_FWK_SUCCESS; + return ret; } int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message) @@ -88,10 +90,10 @@ int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std return ERR_DH_FWK_PUBLISH_MSG_FAILED; } - DHLOGI("Publish message to DHFWK"); - DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message); + int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message); + DHLOGI("Publish message to DHFWK, ret: " PRId32, ret); - return DH_FWK_SUCCESS; + return ret; } bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic) diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp index e2fe4a46..6cf64ca5 100644 --- a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -21,6 +21,7 @@ #include "system_ability_definition.h" #include "distributed_hardware_log.h" +#include "distributed_hardware_proxy.h" namespace OHOS { namespace DistributedHardware { @@ -79,13 +80,17 @@ sptr DHFWKSAManager::GetDHFWKProxy() return nullptr; } DHLOGI("Try get DHFWK sa"); - sptr remoteObject = saMgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SA_ID); + sptr remoteObject = saMgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_SA_ID); if (remoteObject == nullptr) { DHLOGE("Get DHFWK proxy return null"); return nullptr; } dhfwkProxy_ = iface_cast(remoteObject); + if (!dhfwkProxy_ || !dhfwkProxy_->AsObject()) { + DHLOGE("Failed to Get DHFWK Proxy"); + return nullptr; + } } return dhfwkProxy_; diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp similarity index 81% rename from services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp rename to interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index 01babc20..53d2e6e1 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -33,40 +33,8 @@ const std::unordered_set DH_TYPE_SET { DHType::INPUT, DHType::HFP, DHType::A2D, DHType::VIRMODEM_MIC, DHType::VIRMODEM_SPEAKER, DHType::MAX_DH, }; -int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_map &versionMap) -{ - sptr remote = Remote(); - if (remote == nullptr) { - DHLOGE("remote service is null"); - return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; - } - - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(GetDescriptor())) { - DHLOGE("WriteInterfaceToken fail!"); - return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; - } - - int32_t error = remote->SendRequest((uint32_t)IDistributedHardware::Message::QUERY_SINK_VERSION, - data, reply, option); - if (error != NO_ERROR) { - DHLOGE("SendRequest failed, errCode = %d", error); - return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; - } - auto sinkVersion = reply.ReadString(); - if (sinkVersion.empty()) { - DHLOGE("sinkVersion is empty"); - return ERR_DH_FWK_SERVICE_STRING_IS_EMPTY; - } - versionMap = FromJson(sinkVersion); - DHLOGI("success, sinkVersion = %s", sinkVersion.c_str()); - return DH_FWK_SUCCESS; -} - -int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, sptr listener) +int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, + const sptr &listener) { sptr remote = Remote(); if (remote == nullptr) { @@ -97,10 +65,16 @@ int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; } - return DH_FWK_SUCCESS; + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Register Publisher Listener failed, ret: %d", ret); + } + + return ret; } -int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topic, sptr listener) +int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topic, + const sptr &listener) { sptr remote = Remote(); if (remote == nullptr) { @@ -131,7 +105,12 @@ int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topi return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; } - return DH_FWK_SUCCESS; + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Unregister Publisher Listener failed, ret: %d", ret); + } + + return ret; } int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std::string &msg) @@ -165,7 +144,12 @@ int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std: return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; } - return DH_FWK_SUCCESS; + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("PublishMessage failed, ret: %d", ret); + } + + return ret; } void from_json(const nlohmann::json &jsonObj, std::unordered_map &versionMap) diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 32ffdbf3..38a1b985 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -38,6 +38,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "include/publisher", "include/resourcemanager", "include/hidumphelper", + "include/ipc", "${utils_path}/include/eventbus", ] @@ -49,11 +50,11 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/componentmanager/component_manager.cpp", "src/distributed_hardware_manager.cpp", "src/distributed_hardware_manager_factory.cpp", - "src/distributed_hardware_proxy.cpp", "src/distributed_hardware_service.cpp", "src/distributed_hardware_stub.cpp", "src/hidumphelper/enabled_comps_dump.cpp", "src/hidumphelper/hidump_helper.cpp", + "src/ipc/publisher_listener_proxy.cpp", "src/localhardwaremanager/local_hardware_manager.cpp", "src/localhardwaremanager/plugin_listener_impl.cpp", "src/publisher/publisher_item.cpp", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 005008e6..302c11fd 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -72,8 +72,6 @@ private: std::string &sinkVersion); int32_t GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &sinkVersion); - int32_t GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, - DHType dhType, std::string &sinkVersion); int32_t GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType, std::string &sinkVersion); void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo); diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h index b11d5dbc..373df81a 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -34,9 +34,8 @@ DECLARE_SYSTEM_ABILITY(DistributedHardwareService); public: DistributedHardwareService(int32_t saId, bool runOnCreate); ~DistributedHardwareService() = default; - int32_t QuerySinkVersion(std::unordered_map &versionMap) override; - int32_t RegisterPublisherListener(const DHTopic topic, sptr listener) override; - int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener) override; + int32_t RegisterPublisherListener(const DHTopic topic, const sptr &listener) override; + int32_t UnregisterPublisherListener(const DHTopic topic, const sptr &listener) override; int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; int Dump(int32_t fd, const std::vector& args) override; diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index 774bf562..186a4276 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -24,10 +24,9 @@ public: int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: - int32_t QuerySinkVersionInner(MessageParcel &reply); - int32_t RegisterPublisherListenerInner(MessageParcel &data); - int32_t UnregisterPublisherListenerInner(MessageParcel &data); - int32_t PublishMessageInner(MessageParcel &data); + int32_t RegisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t PublishMessageInner(MessageParcel &data, MessageParcel &reply); bool ValidTopic(uint32_t topic); std::string ToJson(const std::unordered_map &versionMap) const; }; diff --git a/interfaces/inner_kits/include/ipc/publisher_listener_proxy.h b/services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h similarity index 100% rename from interfaces/inner_kits/include/ipc/publisher_listener_proxy.h rename to services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 5bb89fa8..6dde137d 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -391,37 +391,6 @@ int32_t ComponentManager::GetSinkVersionFromVerInfoMgr(const std::string &uuid, return DH_FWK_SUCCESS; } -int32_t ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, - DHType dhType, std::string &sinkVersion) -{ - DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); - sptr dhms = GetRemoteDHMS(networkId); - if (dhms == nullptr) { - DHLOGI("GetRemoteDHMS failed, networkId = %s", GetAnonyString(networkId).c_str()); - return ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED; - } - - std::unordered_map versions; - auto ret = dhms->QuerySinkVersion(versions); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("QuerySinkVersion failed, errCode = %d", ret); - return ret; - } - - auto iter = versions.find(dhType); - if (iter == versions.end()) { - DHLOGE("can not find component version for uuid = %s, dhType = %#X", - GetAnonyString(uuid).c_str(), dhType); - return ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND; - } - DHLOGI("QuerySinkVersion success, sinkVersion = %s, uuid = %s, dhType = %#X", - iter->second.c_str(), GetAnonyString(uuid).c_str(), dhType); - UpdateVersionCache(uuid, versions); - sinkVersion = iter->second; - - return DH_FWK_SUCCESS; -} - int32_t ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType, std::string &sinkVersion) { @@ -435,11 +404,6 @@ int32_t ComponentManager::GetSinkVersion(const std::string &networkId, const std return DH_FWK_SUCCESS; } - ret = GetSinkVersionFromRPC(networkId, uuid, dhType, sinkVersion); - if ((ret == DH_FWK_SUCCESS) && (!sinkVersion.empty())) { - return DH_FWK_SUCCESS; - } - return ret; } diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 8bd6521d..2785376a 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -89,27 +89,13 @@ void DistributedHardwareService::OnStop() registerToService_ = false; } -int32_t DistributedHardwareService::QuerySinkVersion(std::unordered_map &versionMap) -{ - auto ret = DistributedHardwareManagerFactory::GetInstance().GetComponentVersion(versionMap); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("GetComponentVersion failed, errCode = %d", ret); - return ret; - } - if (versionMap.empty()) { - DHLOGE("versionMap is empty"); - return ERR_DH_FWK_SERVICE_LOCAL_VERSION_NOT_EXIST; - } - return DH_FWK_SUCCESS; -} - -int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, sptr listener) +int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, const sptr &listener) { Publisher::GetInstance().RegisterListener(topic, listener); return DH_FWK_SUCCESS; } -int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, sptr listener) +int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, const sptr &listener) { Publisher::GetInstance().UnregisterListener(topic, listener); return DH_FWK_SUCCESS; diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index f407ebee..e1fcee7e 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -21,6 +21,7 @@ #include "anonymous_string.h" #include "constants.h" +#include "publisher_listener_proxy.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -34,17 +35,14 @@ int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &d return ERR_INVALID_DATA; } switch (code) { - case (uint32_t)IDistributedHardware::Message::QUERY_SINK_VERSION: { - return QuerySinkVersionInner(reply); - } case (uint32_t)IDistributedHardware::Message::REG_PUBLISHER_LISTNER: { - return RegisterPublisherListenerInner(data); + return RegisterPublisherListenerInner(data, reply); } case (uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER: { - return UnregisterPublisherListenerInner(data); + return UnregisterPublisherListenerInner(data, reply); } case (uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE: { - return PublishMessageInner(data); + return PublishMessageInner(data, reply); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -52,53 +50,56 @@ int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &d return DH_FWK_SUCCESS; } -int32_t DistributedHardwareStub::QuerySinkVersionInner(MessageParcel &reply) -{ - std::unordered_map versionMap; - QuerySinkVersion(versionMap); - auto version = ToJson(versionMap); - if (!reply.WriteString(version)) { - DHLOGE("write version failed"); - return ERR_DH_FWK_SERVICE_IPC_WRITE_PARA_FAIL; - } - return DH_FWK_SUCCESS; -} - -int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data) +int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply) { uint32_t topicInt = data.ReadUint32(); if (!ValidTopic(topicInt)) { DHLOGE("Topic invalid: %" PRIu32, topicInt); + reply.WriteInt32(ERR_DH_FWK_PARA_INVALID); return ERR_DH_FWK_PARA_INVALID; } DHTopic topic = (DHTopic)topicInt; sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Register publisher listener is null"); + reply.WriteInt32(ERR_DH_FWK_PARA_INVALID); + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Register listener, topic: %" PRIu32 , (uint32_t)topic); RegisterPublisherListener(topic, listener); + reply.WriteInt32(DH_FWK_SUCCESS); return DH_FWK_SUCCESS; } -int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data) +int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply) { uint32_t topicInt = data.ReadUint32(); if (!ValidTopic(topicInt)) { DHLOGE("Topic invalid: %" PRIu32, topicInt); + reply.WriteInt32(ERR_DH_FWK_PARA_INVALID); return ERR_DH_FWK_PARA_INVALID; } DHTopic topic = (DHTopic)topicInt; sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Unregister publisher listener is null"); + reply.WriteInt32(ERR_DH_FWK_PARA_INVALID); + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Unregister listener, topic: %" PRIu32 , (uint32_t)topic); UnregisterPublisherListener(topic, listener); + reply.WriteInt32(DH_FWK_SUCCESS); return DH_FWK_SUCCESS; } -int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data) +int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data, MessageParcel &reply) { uint32_t topicInt = data.ReadUint32(); if (!ValidTopic(topicInt)) { DHLOGE("Topic invalid: %" PRIu32, topicInt); + reply.WriteInt32(ERR_DH_FWK_PARA_INVALID); return ERR_DH_FWK_PARA_INVALID; } @@ -106,6 +107,7 @@ int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data) std::string message = data.ReadString(); DHLOGI("Publish Message, topic: %" PRIu32 ", message: %s", (uint32_t)topic, message.c_str()); PublishMessage(topic, message); + reply.WriteInt32(DH_FWK_SUCCESS); return DH_FWK_SUCCESS; } diff --git a/interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp similarity index 100% rename from interfaces/inner_kits/src/ipc/publisher_listener_proxy.cpp rename to services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp index 0b3a5994..4be5ad67 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp @@ -21,7 +21,8 @@ IMPLEMENT_SINGLE_INSTANCE(Publisher); Publisher::Publisher() : publisherItems_({ { DHTopic::TOPIC_START_DSCREEN, std::make_shared(DHTopic::TOPIC_START_DSCREEN) }, { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, std::make_shared(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) }, - { DHTopic::TOPIC_STOP_DSCREEN, std::make_shared(DHTopic::TOPIC_STOP_DSCREEN) } + { DHTopic::TOPIC_STOP_DSCREEN, std::make_shared(DHTopic::TOPIC_STOP_DSCREEN) }, + { DHTopic::TOPIC_DEV_OFFLINE, std::make_shared(DHTopic::TOPIC_DEV_OFFLINE) } }) { } diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp index b53c9d7a..b7e518f3 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -31,7 +31,7 @@ PublisherItem::PublisherItem(DHTopic topic) : topic_(topic) PublisherItem::~PublisherItem() { DHLOGE("Dtor PublisherItem, topic: %d", topic_); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); listeners_.clear(); } @@ -42,7 +42,7 @@ void PublisherItem::AddListener(const sptr &listener) return; } - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); listeners_.insert(listener); } @@ -53,13 +53,18 @@ void PublisherItem::RemoveListener(const sptr &listener) return; } - std::lock_guard lock(mutex_); - listeners_.erase(listener); + std::lock_guard lock(mutex_); + for (const auto &lis : listeners_) { + if (lis->AsObject().GetRefPtr() == listener->AsObject().GetRefPtr()) { + listeners_.erase(lis); + break; + } + } } void PublisherItem::PublishMessage(const std::string &message) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (const auto &listener : listeners_) { listener->OnMessage(topic_, message); } -- Gitee From 6566a39e9f03ef4f2b1e5a02722959fdf763ff61 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 16:47:50 +0800 Subject: [PATCH 3/8] modify Signed-off-by: hwzhangchuang --- interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index c0bf17d6..1befb0b5 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -117,7 +117,7 @@ void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine) for (const auto &listener : entry.second) { int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); - if (ret == 0) { + if (ret == DH_FWK_SUCCESS) { regSuccListeners[entry.first].insert(listener); } } -- Gitee From 6b9a7a274aad02d9af961e5ebbd84b6f297c6da8 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 17:05:07 +0800 Subject: [PATCH 4/8] modify format Signed-off-by: hwzhangchuang --- common/utils/include/distributed_hardware_errno.h | 2 +- common/utils/include/idistributed_hardware.h | 4 ++-- .../utils/include/idistributed_hardware_manager.h | 2 +- ..._publisher_listener.h => ipublisher_listener.h} | 6 +++--- common/utils/include/single_instance.h | 2 +- .../include/distributed_hardware_fwk_kit.h | 13 ++++++------- .../inner_kits/include/ipc/dhfwk_sa_manager.h | 6 +++--- .../include/ipc/distributed_hardware_proxy.h | 2 +- .../include/ipc/publisher_listener_stub.h | 8 ++++---- .../include/distributed_hardware_stub.h | 4 ++++ .../include/ipc/publisher_listener_proxy.h | 8 ++++---- .../include/publisher/publisher.h | 10 +++++----- .../include/publisher/publisher_item.h | 14 +++++++------- 13 files changed, 42 insertions(+), 39 deletions(-) rename common/utils/include/{i_publisher_listener.h => ipublisher_listener.h} (93%) diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 3fc32ef7..a0c0009e 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -100,4 +100,4 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_PUBLISH_MSG_FAILED = -11100; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_ERRNO diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index 6d252fc2..84a4a4b6 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -23,7 +23,7 @@ #include "iremote_broker.h" #include "device_type.h" -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" namespace OHOS { namespace DistributedHardware { @@ -44,4 +44,4 @@ public: }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_IDISTRIBUTED_HARDWARE_H diff --git a/common/utils/include/idistributed_hardware_manager.h b/common/utils/include/idistributed_hardware_manager.h index 46dcd6ad..dc2cf894 100644 --- a/common/utils/include/idistributed_hardware_manager.h +++ b/common/utils/include/idistributed_hardware_manager.h @@ -38,4 +38,4 @@ public: }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_IDISTRIBUTED_HARDWARE_MANAGER_H diff --git a/common/utils/include/i_publisher_listener.h b/common/utils/include/ipublisher_listener.h similarity index 93% rename from common/utils/include/i_publisher_listener.h rename to common/utils/include/ipublisher_listener.h index b227c55d..5ac03510 100644 --- a/common/utils/include/i_publisher_listener.h +++ b/common/utils/include/ipublisher_listener.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef I_PUBLISHER_LISTENER_H -#define I_PUBLISHER_LISTENER_H +#ifndef OHOS_IPUBLISHER_LISTENER_H +#define OHOS_IPUBLISHER_LISTENER_H #include #include @@ -50,4 +50,4 @@ public: }; } // namespace DistributedHardware } // namespace OHOS -#endif // I_PUBLISHER_LISTENER_H \ No newline at end of file +#endif // OHOS_IPUBLISHER_LISTENER_H \ No newline at end of file diff --git a/common/utils/include/single_instance.h b/common/utils/include/single_instance.h index f70cfbbc..104681ac 100644 --- a/common/utils/include/single_instance.h +++ b/common/utils/include/single_instance.h @@ -48,4 +48,4 @@ className & className::GetInstance() \ } } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_SINGLE_INSTANCE_H diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h index f9bd790f..bcd53d91 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef DISTRIBUTED_HARDWARE_FWK_KIT_H -#define DISTRIBUTED_HARDWARE_FWK_KIT_H +#ifndef OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H +#define OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H #include #include @@ -23,7 +23,7 @@ #include #include "refbase.h" -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" #include "idistributed_hardware.h" #ifndef API_EXPORT @@ -32,7 +32,6 @@ namespace OHOS { namespace DistributedHardware { - class DistributedHardwareFwkKit final { public: API_EXPORT DistributedHardwareFwkKit(); @@ -50,6 +49,6 @@ private: std::unordered_map>> listenerMap_; std::atomic isDHFWKOnLine_; }; -} -} -#endif \ No newline at end of file +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h index d022ae5a..ef35c38a 100644 --- a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h +++ b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef DHFWK_SA_MANAGER_H -#define DHFWK_SA_MANAGER_H +#ifndef OHOS_DHFWK_SA_MANAGER_H +#define OHOS_DHFWK_SA_MANAGER_H #include #include @@ -57,4 +57,4 @@ private: }; } // DistributedHardware } // OHOS -#endif // DHFWK_SA_MANAGER_H \ No newline at end of file +#endif // OHOS_DHFWK_SA_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 53ba416a..5bd57379 100644 --- a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -42,4 +42,4 @@ private: }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_PROXY_H diff --git a/interfaces/inner_kits/include/ipc/publisher_listener_stub.h b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h index e8c2d6cf..30a0e906 100644 --- a/interfaces/inner_kits/include/ipc/publisher_listener_stub.h +++ b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h @@ -13,10 +13,10 @@ * limitations under the License. */ -#ifndef PUBLISHER_LISTENER_STUB_H -#define PUBLISHER_LISTENER_STUB_H +#ifndef OHOS_PUBLISHER_LISTENER_STUB_H +#define OHOS_PUBLISHER_LISTENER_STUB_H -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" #include "iremote_stub.h" @@ -33,4 +33,4 @@ private: }; } // namespace DistributedHardware } // namespace OHOS -#endif \ No newline at end of file +#endif // OHOS_PUBLISHER_LISTENER_STUB_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index 186a4276..f549c24e 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -13,6 +13,9 @@ * limitations under the License. */ +#ifndef OHOS_DISTRIBUTED_HARDWARE_STUB_H +#define OHOS_DISTRIBUTED_HARDWARE_STUB_H + #include "iremote_stub.h" #include "idistributed_hardware.h" @@ -32,3 +35,4 @@ private: }; } // namespace DistributedHardware } // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_STUB_H diff --git a/services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h b/services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h index 6ae0147d..549b8f09 100644 --- a/services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h +++ b/services/distributedhardwarefwkservice/include/ipc/publisher_listener_proxy.h @@ -13,10 +13,10 @@ * limitations under the License. */ -#ifndef PUBLISHER_LISTENER_PROXY_H -#define PUBLISHER_LISTENER_PROXY_H +#ifndef OHOS_PUBLISHER_LISTENER_PROXY_H +#define OHOS_PUBLISHER_LISTENER_PROXY_H -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" #include "iremote_proxy.h" @@ -35,4 +35,4 @@ private: }; } // DistributedHardware } // OHOS -#endif // PUBLISHER_LISTENER_PROXY_H \ No newline at end of file +#endif // OHOS_PUBLISHER_LISTENER_PROXY_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/publisher/publisher.h b/services/distributedhardwarefwkservice/include/publisher/publisher.h index ca0fef59..a90e73ef 100644 --- a/services/distributedhardwarefwkservice/include/publisher/publisher.h +++ b/services/distributedhardwarefwkservice/include/publisher/publisher.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,13 +13,13 @@ * limitations under the License. */ -#ifndef PUBLISHER_H -#define PUBLISHER_H +#ifndef OHOS_PUBLISHER_H +#define OHOS_PUBLISHER_H #include #include -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" #include "publisher_item.h" namespace OHOS { @@ -37,4 +37,4 @@ private: }; } // namespace DistributedHardware } // namespace OHOS -#endif \ No newline at end of file +#endif // OHOS_PUBLISHER_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/publisher/publisher_item.h b/services/distributedhardwarefwkservice/include/publisher/publisher_item.h index 8c7c4e45..c63be2fe 100644 --- a/services/distributedhardwarefwkservice/include/publisher/publisher_item.h +++ b/services/distributedhardwarefwkservice/include/publisher/publisher_item.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,15 +13,15 @@ * limitations under the License. */ -#ifndef PUBLISHER_ITEM_H -#define PUBLISHER_ITEM_H +#ifndef OHOS_PUBLISHER_ITEM_H +#define OHOS_PUBLISHER_ITEM_H #include #include #include #include "refbase.h" -#include "i_publisher_listener.h" +#include "ipublisher_listener.h" #include "single_instance.h" namespace OHOS { @@ -40,6 +40,6 @@ private: std::mutex mutex_; std::set> listeners_; }; -} -} -#endif \ No newline at end of file +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_PUBLISHER_ITEM_H \ No newline at end of file -- Gitee From 265427ee5951798b0071b051265cb4d6184219c5 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 17:10:28 +0800 Subject: [PATCH 5/8] modify format Signed-off-by: hwzhangchuang --- .../distributedhardwarefwkservice/src/publisher/publisher.cpp | 2 +- .../src/publisher/publisher_item.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp index 4be5ad67..0118d2d9 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp index b7e518f3..1ce2e5c8 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 -- Gitee From 8bf61ca7f1c5832249c6f5da1f7f27d194aa0210 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 17:21:09 +0800 Subject: [PATCH 6/8] modify gn format Signed-off-by: hwzhangchuang --- interfaces/inner_kits/BUILD.gn | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index 14c65ede..c33d5da2 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -12,7 +12,8 @@ # limitations under the License. import("//build/ohos.gni") -import("//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") ohos_shared_library("libdhfwk_sdk") { include_dirs = [ @@ -36,10 +37,10 @@ ohos_shared_library("libdhfwk_sdk") { ] deps = [ - "//utils/native/base:utils", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - "${utils_path}:distributedhardwareutils", "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//utils/native/base:utils", ] defines = [ @@ -49,9 +50,9 @@ ohos_shared_library("libdhfwk_sdk") { ] external_deps = [ + "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", - "hiviewdfx_hilog_native:libhilog", "samgr_standard:samgr_proxy", ] -- Gitee From 568883a10efbc8a3dd63798b620ad5e3b6e8fb1e Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 17:24:47 +0800 Subject: [PATCH 7/8] modify format Signed-off-by: hwzhangchuang --- interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp | 1 - .../src/distributed_hardware_service.cpp | 6 ++++-- .../src/publisher/publisher.cpp | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp index 6cf64ca5..aa5082b3 100644 --- a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -141,6 +141,5 @@ void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t system } DHLOGI("sa %" PRId32 " stopped", systemAbilityId); } - } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 2785376a..389d742c 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -89,13 +89,15 @@ void DistributedHardwareService::OnStop() registerToService_ = false; } -int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, const sptr &listener) +int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, + const sptr &listener) { Publisher::GetInstance().RegisterListener(topic, listener); return DH_FWK_SUCCESS; } -int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, const sptr &listener) +int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, + const sptr &listener) { Publisher::GetInstance().UnregisterListener(topic, listener); return DH_FWK_SUCCESS; diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp index 0118d2d9..0ce8172b 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp @@ -20,7 +20,8 @@ namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(Publisher); Publisher::Publisher() : publisherItems_({ { DHTopic::TOPIC_START_DSCREEN, std::make_shared(DHTopic::TOPIC_START_DSCREEN) }, - { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, std::make_shared(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) }, + { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, + std::make_shared(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) }, { DHTopic::TOPIC_STOP_DSCREEN, std::make_shared(DHTopic::TOPIC_STOP_DSCREEN) }, { DHTopic::TOPIC_DEV_OFFLINE, std::make_shared(DHTopic::TOPIC_DEV_OFFLINE) } }) @@ -45,5 +46,5 @@ void Publisher::PublishMessage(const DHTopic topic, const std::string &message) { publisherItems_[topic]->PublishMessage(message); } -} //namespace DistributedHardware +} // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee From aacbe648e0dedf6ed316b6a9d224ca247f7e9866 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 30 Jul 2022 17:44:46 +0800 Subject: [PATCH 8/8] modify gn format Signed-off-by: hwzhangchuang --- services/distributedhardwarefwkservice/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 38a1b985..1029c2ff 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -57,8 +57,8 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/ipc/publisher_listener_proxy.cpp", "src/localhardwaremanager/local_hardware_manager.cpp", "src/localhardwaremanager/plugin_listener_impl.cpp", - "src/publisher/publisher_item.cpp", "src/publisher/publisher.cpp", + "src/publisher/publisher_item.cpp", "src/resourcemanager/capability_info.cpp", "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", -- Gitee