diff --git a/bundle.json b/bundle.json index 1e6faf8c3f792dd74a7ed047e982dc073f0e3443..8f00567a1e84f8cf710d3050d637fb574c268905 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 a4378588df6d52b313bc0315d952d506944b9aae..a0c0009eed2237a250d1f6d827e7180c1934d187 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 +#endif // OHOS_DISTRIBUTED_HARDWARE_ERRNO diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index c0db431881465a385b768bd1c103b6f335e43791..84a4a4b6dc3e15030947dc5ea663dba2370b24a7 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 "ipublisher_listener.h" + namespace OHOS { namespace DistributedHardware { class IDistributedHardware : public OHOS::IRemoteBroker { @@ -28,13 +32,16 @@ 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, 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 { - QUERY_SINK_VERSION = 1 + enum class Message : uint32_t { + REG_PUBLISHER_LISTNER = 1, + UNREG_PUBLISHER_LISTENER = 2, + PUBLISH_MESSAGE = 3 }; }; } // 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 46dcd6ad026ce6d89fda841cb88c88cd32215248..dc2cf8948f1c1d6b342370cbfd1cfb9bb1e670d5 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/ipublisher_listener.h b/common/utils/include/ipublisher_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..5ac03510d990c0ff60b04cfbdb0388f205207481 --- /dev/null +++ b/common/utils/include/ipublisher_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 OHOS_IPUBLISHER_LISTENER_H +#define OHOS_IPUBLISHER_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 // 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 78da97ce7a495a383adbdfb7b1f46b1bbfc9d493..104681acb50af230904324ad9cc21b1580a8b396 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(); \ @@ -41,4 +48,4 @@ className & className::GetInstance() \ } } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_SINGLE_INSTANCE_H diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c33d5da2c880c43ca79ca0fafeffc7f970cdc5de --- /dev/null +++ b/interfaces/inner_kits/BUILD.gn @@ -0,0 +1,64 @@ +# 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", + "${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/distributed_hardware_proxy.cpp", + "${innerkits_path}/src/ipc/publisher_listener_stub.cpp", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//utils/native/base:utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dhfwkkit\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "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 0000000000000000000000000000000000000000..bcd53d91d4d226baa555a28acaae03ced6f85518 --- /dev/null +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -0,0 +1,54 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H +#define OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H + +#include +#include +#include +#include +#include +#include "refbase.h" + +#include "ipublisher_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_; +}; +} // 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 new file mode 100644 index 0000000000000000000000000000000000000000..ef35c38a4210f1994395b738f5994e595116605e --- /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 OHOS_DHFWK_SA_MANAGER_H +#define OHOS_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 // OHOS_DHFWK_SA_MANAGER_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h similarity index 79% rename from services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h rename to interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 13bea7e2ea169df21a5f923e804fc7ace51f6599..5bd57379b8b28d3c336c3c25a3399d8facb2891d 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -32,13 +32,14 @@ public: } virtual ~DistributedHardwareProxy() {} - int32_t QuerySinkVersion(std::unordered_map &versionMap) 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: std::unordered_map FromJson(const std::string &json) const; - static inline BrokerDelegator delegator_; }; } // 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 new file mode 100644 index 0000000000000000000000000000000000000000..30a0e90696ebb93d55696b1c79abf53be07955f4 --- /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 OHOS_PUBLISHER_LISTENER_STUB_H +#define OHOS_PUBLISHER_LISTENER_STUB_H + +#include "ipublisher_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 // OHOS_PUBLISHER_LISTENER_STUB_H \ 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 0000000000000000000000000000000000000000..1befb0b578e18f57eb1144979e5b12a33abba784 --- /dev/null +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -0,0 +1,133 @@ +/* + * 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; + } + + int32_t ret = DH_FWK_SUCCESS; + if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + 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 ret; +} + +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; + } + + int32_t ret = DH_FWK_SUCCESS; + if (isDHFWKOnLine_ && DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + 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 ret; +} + +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; + } + + int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message); + DHLOGI("Publish message to DHFWK, ret: " PRId32, ret); + + return ret; +} + +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 == DH_FWK_SUCCESS) { + 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 0000000000000000000000000000000000000000..aa5082b3fc8e0be092ff7c2260f75f45f75375cc --- /dev/null +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -0,0 +1,145 @@ +/* + * 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" +#include "distributed_hardware_proxy.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->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_; +} + +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/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp similarity index 42% rename from services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp rename to interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index a1f797bd229e3b0e874b30b5bbad4c79b9448e29..53d2e6e186818201d93c471a0d42210a9b8c10d1 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -33,8 +33,15 @@ 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) +int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, + const 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; @@ -43,24 +50,106 @@ int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_mapAsObject())) { + 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; + } + + 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, + const sptr &listener) +{ sptr 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); - if (error != NO_ERROR) { - DHLOGE("SendRequest failed, errCode = %d", error); + + 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; } - auto sinkVersion = reply.ReadString(); - if (sinkVersion.empty()) { - DHLOGE("sinkVersion is empty"); - return ERR_DH_FWK_SERVICE_STRING_IS_EMPTY; + + 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) +{ + 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; } - versionMap = FromJson(sinkVersion); - DHLOGI("success, sinkVersion = %s", sinkVersion.c_str()); - return DH_FWK_SUCCESS; + 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; + } + + 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/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84be82c9bf252ed85f21368f129d2448c653a58f --- /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 977bec65ed67616e7cb19f482ee52b964ab3921f..1029c2ff769f5b598644863bd47f74c8d8ff6cac 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -35,8 +35,10 @@ ohos_shared_library("distributedhardwarefwksvr") { "include/task", "include/utils", "include/localhardwaremanager", + "include/publisher", "include/resourcemanager", "include/hidumphelper", + "include/ipc", "${utils_path}/include/eventbus", ] @@ -48,13 +50,15 @@ 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.cpp", + "src/publisher/publisher_item.cpp", "src/resourcemanager/capability_info.cpp", "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 005008e6e96cd9bfa29ae43fde9071a02bed6b4a..302c11fdd8807fdca269f26f276a8c1eb09b7667 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 21c671f5d354c6f326049868f1ac70e120f91dd1..373df81a51904ab5c4415af1d8f9a3574602de83 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -34,7 +34,9 @@ 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, 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; protected: diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index 80d1d2685186d614168ef2eacce9f744ace7a9ce..f549c24e7faa762dcdcb980b8c60067cd4c846b6 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" @@ -24,7 +27,12 @@ public: int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: + 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; }; } // 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 new file mode 100644 index 0000000000000000000000000000000000000000..549b8f09a8ee20c83a32a7ae67791554bb3cf461 --- /dev/null +++ b/services/distributedhardwarefwkservice/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 OHOS_PUBLISHER_LISTENER_PROXY_H +#define OHOS_PUBLISHER_LISTENER_PROXY_H + +#include "ipublisher_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 // 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 new file mode 100644 index 0000000000000000000000000000000000000000..a90e73ef431c7523b0e1e981f329ddf1bc0c096a --- /dev/null +++ b/services/distributedhardwarefwkservice/include/publisher/publisher.h @@ -0,0 +1,40 @@ +/* + * 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 OHOS_PUBLISHER_H +#define OHOS_PUBLISHER_H + +#include +#include + +#include "ipublisher_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 // 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 new file mode 100644 index 0000000000000000000000000000000000000000..c63be2fe608f36b0625aef281ae54ae94e028b40 --- /dev/null +++ b/services/distributedhardwarefwkservice/include/publisher/publisher_item.h @@ -0,0 +1,45 @@ +/* + * 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 OHOS_PUBLISHER_ITEM_H +#define OHOS_PUBLISHER_ITEM_H +#include +#include +#include + +#include "refbase.h" + +#include "ipublisher_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_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_PUBLISHER_ITEM_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 5bb89fa84e5cd9bfd5d7f7a5fb92a93e3d0f54b2..6dde137dfb96d8f4c76063507fe529b06a74fa1b 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 fecf6941cb472b1a73567365a40d3067a3a7463a..389d742cf721c9f68073b92709afbfd70947a106 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 { @@ -87,10 +89,24 @@ void DistributedHardwareService::OnStop() registerToService_ = false; } -int32_t DistributedHardwareService::QuerySinkVersion(std::unordered_map &versionMap) +int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic, + const sptr &listener) { - (void)versionMap; - return 0; + Publisher::GetInstance().RegisterListener(topic, listener); + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic, + const 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 5db8573c25069b54999d1daec1e196e317291c59..e1fcee7ed68c787e303ed5aed2ea4c33cf7ea78d 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -15,44 +15,109 @@ #include "distributed_hardware_stub.h" +#include + +#include "nlohmann/json.hpp" + #include "anonymous_string.h" #include "constants.h" +#include "publisher_listener_proxy.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::REG_PUBLISHER_LISTNER: { + return RegisterPublisherListenerInner(data, reply); + } + case (uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER: { + return UnregisterPublisherListenerInner(data, reply); + } + case (uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE: { + return PublishMessageInner(data, reply); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } - return 0; + return DH_FWK_SUCCESS; +} + +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, 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, 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; + 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; +} + +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/ipc/publisher_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..704675e932e03b7157f5d2e86c11acb326c69f09 --- /dev/null +++ b/services/distributedhardwarefwkservice/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/services/distributedhardwarefwkservice/src/publisher/publisher.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ce8172be924b8c01544ba91a243c28729dc6b48 --- /dev/null +++ b/services/distributedhardwarefwkservice/src/publisher/publisher.cpp @@ -0,0 +1,50 @@ +/* + * 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.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) }, + { DHTopic::TOPIC_DEV_OFFLINE, std::make_shared(DHTopic::TOPIC_DEV_OFFLINE) } + }) +{ +} + +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 0000000000000000000000000000000000000000..1ce2e5c8fdc044c13631b00cb5ba9f14f54c39ff --- /dev/null +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -0,0 +1,73 @@ +/* + * 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_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_); + 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_); + for (const auto &listener : listeners_) { + listener->OnMessage(topic_, message); + } +} +} // DistributedHardware +} // OHOS \ No newline at end of file