1 /*
2 * Copyright (c) 2022-2024 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 "dhfwk_sa_manager.h"
17
18 #include <cinttypes>
19
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 #include "constants.h"
24 #include "distributed_hardware_log.h"
25 #include "distributed_hardware_proxy.h"
26 #include "distributed_hardware_errno.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 IMPLEMENT_SINGLE_INSTANCE(DHFWKSAManager);
DHFWKSAManager()31 DHFWKSAManager::DHFWKSAManager()
32 : dhfwkOnLine_(false), isSubscribeDHFWKSAChangeListener_(false), dhfwkProxy_(nullptr),
33 saListener_(sptr<SystemAbilityListener>(new SystemAbilityListener())), saStateCallback_(nullptr),
34 publisherListenersCache_({}), avTransControlCenterCbCache_({})
35 {
36 DHLOGI("Ctor DHFWKSAManager");
37 }
~DHFWKSAManager()38 DHFWKSAManager::~DHFWKSAManager()
39 {
40 DHLOGI("Dtor DHFWKSAManager");
41 dhfwkOnLine_ = false;
42 isSubscribeDHFWKSAChangeListener_.store(false);
43 dhfwkProxy_ = nullptr;
44 saListener_ = nullptr;
45 }
46
RegisterAbilityListener()47 void DHFWKSAManager::RegisterAbilityListener()
48 {
49 DHLOGI("Register DHFWK sa listener");
50 sptr<ISystemAbilityManager> saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51 if (saMgr == nullptr) {
52 DHLOGE("Get System Ability Manager failed");
53 return;
54 }
55
56 if (!isSubscribeDHFWKSAChangeListener_.load()) {
57 DHLOGI("try subscribe sa change listener, sa id: %{public}d", DISTRIBUTED_HARDWARE_SA_ID);
58 int32_t ret = saMgr->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, saListener_);
59 if (ret != 0) {
60 DHLOGE("subscribe DHFWK sa change listener failed, ret: %{public}d", ret);
61 return;
62 }
63 isSubscribeDHFWKSAChangeListener_.store(true);
64 }
65 }
66
GetDHFWKProxy()67 sptr<IDistributedHardware> DHFWKSAManager::GetDHFWKProxy()
68 {
69 if (!isSubscribeDHFWKSAChangeListener_.load()) {
70 RegisterAbilityListener();
71 }
72
73 std::lock_guard<std::mutex> lock(proxyMutex_);
74 if (dhfwkProxy_ == nullptr) {
75 sptr<ISystemAbilityManager> saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (saMgr == nullptr) {
77 DHLOGE("Get System Ability Manager failed");
78 return nullptr;
79 }
80 DHLOGI("Try get DHFWK sa");
81 sptr<IRemoteObject> remoteObject = saMgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_SA_ID);
82 if (remoteObject == nullptr) {
83 DHLOGE("Get DHFWK proxy return null");
84 return nullptr;
85 }
86
87 dhfwkProxy_ = iface_cast<IDistributedHardware>(remoteObject);
88 if (!dhfwkProxy_ || !dhfwkProxy_->AsObject()) {
89 DHLOGE("Failed to Get DHFWK Proxy");
90 return nullptr;
91 }
92 }
93
94 return dhfwkProxy_;
95 }
96
RegisterSAStateCallback(DHFWKSAStateCb callback)97 void DHFWKSAManager::RegisterSAStateCallback(DHFWKSAStateCb callback)
98 {
99 std::lock_guard<std::mutex> lock(saStatCbMutex_);
100 saStateCallback_ = callback;
101 }
102
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)103 void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
104 {
105 (void)deviceId;
106 if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) {
107 DHLOGW("Receive SA Start, but sa id is not DHFWK, id: %{public}" PRId32, systemAbilityId);
108 return;
109 }
110
111 DHFWKSAManager::GetInstance().dhfwkOnLine_ = true;
112 {
113 std::lock_guard<std::mutex> lock(DHFWKSAManager::GetInstance().saStatCbMutex_);
114 if (DHFWKSAManager::GetInstance().saStateCallback_ != nullptr) {
115 DHFWKSAManager::GetInstance().saStateCallback_(true);
116 }
117 }
118 if (DHFWKSAManager::GetInstance().RestoreListener() != DH_FWK_SUCCESS) {
119 DHLOGE("Partial listeners failed to restore");
120 }
121 DHLOGI("sa %{public}" PRId32 " started", systemAbilityId);
122 }
123
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)124 void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
125 {
126 (void)deviceId;
127 if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) {
128 DHLOGW("Receive SA Stop, but sa id is not DHFWK, id: %{public}" PRId32, systemAbilityId);
129 return;
130 }
131
132 DHFWKSAManager::GetInstance().dhfwkOnLine_ = false;
133 {
134 std::lock_guard<std::mutex> lock(DHFWKSAManager::GetInstance().proxyMutex_);
135 DHFWKSAManager::GetInstance().dhfwkProxy_ = nullptr;
136 }
137 {
138 std::lock_guard<std::mutex> lock(DHFWKSAManager::GetInstance().saStatCbMutex_);
139 if (DHFWKSAManager::GetInstance().saStateCallback_ != nullptr) {
140 DHFWKSAManager::GetInstance().saStateCallback_(false);
141 }
142 }
143 DHLOGI("sa %{public}" PRId32 " stopped", systemAbilityId);
144 }
145
RestoreListener()146 int32_t DHFWKSAManager::RestoreListener()
147 {
148 DHLOGI("Restore the failed listeners due to sa crash");
149 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
150 return ERR_DH_FWK_POINTER_IS_NULL;
151 }
152 int32_t ret = DH_FWK_SUCCESS;
153 {
154 std::lock_guard<std::mutex> publisherListenersLock(publisherListenersMutex_);
155 for (const auto &entry : publisherListenersCache_) {
156 for (const auto &listener : entry.second) {
157 int32_t innerRet =
158 DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener);
159 if (ret != DH_FWK_SUCCESS) {
160 ret = innerRet;
161 DHLOGE("Register publisher listener failed, topic: %{public}" PRIu32, (uint32_t)entry.first);
162 }
163 }
164 }
165 }
166 {
167 std::lock_guard<std::mutex> avTransControlCenterCbLock(avTransControlCenterCbMutex_);
168 for (auto &entry : avTransControlCenterCbCache_) {
169 int32_t innerRet =
170 DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(entry.first, entry.second);
171 if (innerRet != DH_FWK_SUCCESS) {
172 ret = innerRet;
173 DHLOGE("Restore register av control center callback failed, engineId: %{public}" PRId32, entry.first);
174 }
175 }
176 }
177 return ret;
178 }
179
AddPublisherListenerToCache(const DHTopic topic,sptr<IPublisherListener> listener)180 int32_t DHFWKSAManager::AddPublisherListenerToCache(const DHTopic topic, sptr<IPublisherListener> listener)
181 {
182 std::lock_guard<std::mutex> publisherListenersLock(publisherListenersMutex_);
183 if (publisherListenersCache_.size() >= MAX_TOPIC_SIZE ||
184 publisherListenersCache_[topic].size() >= MAX_LISTENER_SIZE) {
185 DHLOGE("listeners are over size!");
186 return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE;
187 }
188 publisherListenersCache_[topic].insert(listener);
189 return DH_FWK_SUCCESS;
190 }
191
RemovePublisherListenerFromCache(const DHTopic topic,sptr<IPublisherListener> listener)192 void DHFWKSAManager::RemovePublisherListenerFromCache(const DHTopic topic, sptr<IPublisherListener> listener)
193 {
194 std::lock_guard<std::mutex> publisherListenersLock(publisherListenersMutex_);
195 publisherListenersCache_[topic].erase(listener);
196 }
197
AddAVTransControlCenterCbToCache(int32_t engineId,const sptr<IAVTransControlCenterCallback> callback)198 void DHFWKSAManager::AddAVTransControlCenterCbToCache(int32_t engineId,
199 const sptr<IAVTransControlCenterCallback> callback)
200 {
201 std::lock_guard<std::mutex> avTransControlCenterCbLock(avTransControlCenterCbMutex_);
202 avTransControlCenterCbCache_[engineId] = callback;
203 }
204
RemoveAVTransControlCenterCbFromCache(int32_t engineId)205 void DHFWKSAManager::RemoveAVTransControlCenterCbFromCache(int32_t engineId)
206 {
207 std::lock_guard<std::mutex> avTransControlCenterCbLock(avTransControlCenterCbMutex_);
208 avTransControlCenterCbCache_.erase(engineId);
209 }
210 } // DistributedHardware
211 } // OHOS