1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "distributed_hardware_fwk_kit.h"
17
18 #include <cinttypes>
19
20 #include "constants.h"
21 #include "dhfwk_sa_manager.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24 #include "idistributed_hardware.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
DistributedHardwareFwkKit()28 DistributedHardwareFwkKit::DistributedHardwareFwkKit() : listenerMap_({}), isDHFWKOnLine_(false)
29 {
30 DHLOGI("Ctor DistributedHardwareFwkKit");
31 DHFWKSAManager::GetInstance().RegisterSAStateCallback(
32 std::bind(&DistributedHardwareFwkKit::OnDHFWKOnLine, this, std::placeholders::_1));
33 DHFWKSAManager::GetInstance().RegisterAbilityListener();
34 }
35
~DistributedHardwareFwkKit()36 DistributedHardwareFwkKit::~DistributedHardwareFwkKit()
37 {
38 DHLOGI("Dtor DistributedHardwareFwkKit");
39 }
40
RegisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)41 int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
42 {
43 DHLOGI("Register publisher listener, topic: %" PRIu32 ", is DHFWK online: %s",
44 (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
45 if (!IsDHTopicValid(topic)) {
46 DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
47 return ERR_DH_FWK_PARA_INVALID;
48 }
49
50 int32_t ret = DH_FWK_SUCCESS;
51 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
52 ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener);
53 DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret);
54 } else {
55 DHLOGI("DHFWK not online, or get proxy failed, save listener temporary");
56 std::lock_guard<std::mutex> lock(listenerMutex_);
57 if (listenerMap_.size() >= MAX_TOPIC_SIZE || listenerMap_[topic].size() >= MAX_LISTENER_SIZE) {
58 DHLOGE("listeners are over size!");
59 return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE;
60 }
61 listenerMap_[topic].insert(listener);
62 }
63
64 return ret;
65 }
66
UnregisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)67 int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
68 {
69 DHLOGI("Unregister publisher listener, topic: %" PRIu32 ", is DHFWK online: %s",
70 (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
71 if (!IsDHTopicValid(topic)) {
72 DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
73 return ERR_DH_FWK_PARA_INVALID;
74 }
75
76 int32_t ret = DH_FWK_SUCCESS;
77 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
78 ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener);
79 DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret);
80 }
81 std::lock_guard<std::mutex> lock(listenerMutex_);
82 listenerMap_[topic].erase(listener);
83
84 return ret;
85 }
86
PublishMessage(const DHTopic topic,const std::string & message)87 int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message)
88 {
89 DHLOGI("Publish message, topic: %" PRIu32, (uint32_t)topic);
90 if (!IsDHTopicValid(topic)) {
91 DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
92 return ERR_DH_FWK_PARA_INVALID;
93 }
94 if (message.empty() || message.size() > MAX_MESSAGE_LEN) {
95 DHLOGE("Message size is invalid!");
96 return ERR_DH_FWK_PARA_INVALID;
97 }
98
99 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
100 DHLOGI("DHFWK not online, can not publish message");
101 return ERR_DH_FWK_PUBLISH_MSG_FAILED;
102 }
103
104 int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message);
105 DHLOGI("Publish message to DHFWK, ret: %" PRId32, ret);
106
107 return ret;
108 }
109
IsDHTopicValid(DHTopic topic)110 bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic)
111 {
112 return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX;
113 }
114
OnDHFWKOnLine(bool isOnLine)115 void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine)
116 {
117 DHLOGI("Receive DHFWK online callback, %s", (isOnLine ? "true" : "false"));
118 isDHFWKOnLine_ = isOnLine;
119
120 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
121 return;
122 }
123
124 DHLOGI("DHFWK online, register saved listener to it");
125 std::unordered_map<DHTopic, std::set<sptr<IPublisherListener>>> regSuccListeners;
126 std::lock_guard<std::mutex> lock(listenerMutex_);
127 for (const auto &entry : listenerMap_) {
128 for (const auto &listener : entry.second) {
129 int32_t ret =
130 DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener);
131 if (ret == DH_FWK_SUCCESS) {
132 regSuccListeners[entry.first].insert(listener);
133 }
134 }
135 }
136
137 for (const auto &succ : regSuccListeners) {
138 for (const auto &listener : succ.second) {
139 listenerMap_[succ.first].erase(listener);
140 }
141 }
142 }
143 } // DistributedHardware
144 } // OHOS