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 "component_monitor.h"
17
18 #include <cinttypes>
19
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 #include "anonymous_string.h"
24 #include "component_loader.h"
25 #include "component_manager.h"
26 #include "device_type.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
ComponentMonitor()32 ComponentMonitor::ComponentMonitor() : saListeners_({})
33 {
34 DHLOGI("Ctor ComponentMonitor");
35 }
36
~ComponentMonitor()37 ComponentMonitor::~ComponentMonitor()
38 {
39 DHLOGI("Dtor ComponentMonitor");
40 std::lock_guard<std::mutex> lock(saListenersMtx_);
41 saListeners_.clear();
42 }
43
OnAddSystemAbility(int32_t saId,const std::string & deviceId)44 void ComponentMonitor::CompSystemAbilityListener::OnAddSystemAbility(int32_t saId, const std::string &deviceId)
45 {
46 DHLOGI("OnAddSystemAbility, saId: %d, deviceId: %s", saId, GetAnonyString(deviceId).c_str());
47 }
48
OnRemoveSystemAbility(int32_t saId,const std::string & deviceId)49 void ComponentMonitor::CompSystemAbilityListener::OnRemoveSystemAbility(int32_t saId, const std::string &deviceId)
50 {
51 DHLOGI("OnRemoveSystemAbility, saId: %d, deviceId: %s", saId, GetAnonyString(deviceId).c_str());
52 DHType dhType = ComponentLoader::GetInstance().GetDHTypeBySrcSaId(saId);
53 if (dhType == DHType::UNKNOWN) {
54 DHLOGE("Can not find DHType by sa Id: %d", saId);
55 return;
56 }
57
58 DHLOGI("Try Recover Component, dhType: %" PRIu32, (uint32_t)dhType);
59 ComponentManager::GetInstance().Recover(dhType);
60 }
61
AddSAMonitor(int32_t saId)62 void ComponentMonitor::AddSAMonitor(int32_t saId)
63 {
64 DHLOGI("Try add sa monitor, saId: %" PRIu32, saId);
65 std::lock_guard<std::mutex> lock(saListenersMtx_);
66 if (saListeners_.find(saId) != saListeners_.end()) {
67 DHLOGW("SaId is in monitor, id: %" PRIu32, saId);
68 return;
69 }
70
71 sptr<CompSystemAbilityListener> listener = new CompSystemAbilityListener();
72 sptr<ISystemAbilityManager> systemAbilityManager =
73 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74 if (!systemAbilityManager) {
75 DHLOGE("get system ability manager failed.");
76 return;
77 }
78
79 int32_t ret = systemAbilityManager->SubscribeSystemAbility(saId, listener);
80 if (ret != DH_FWK_SUCCESS) {
81 DHLOGE("subscribe sa change listener failed: %d", ret);
82 return;
83 }
84
85 saListeners_[saId] = listener;
86 DHLOGI("subscribe sa change listener success.");
87 return;
88 }
89
RemoveSAMonitor(int32_t saId)90 void ComponentMonitor::RemoveSAMonitor(int32_t saId)
91 {
92 DHLOGI("Try remove sa monitor, saId: %" PRIu32, saId);
93 std::lock_guard<std::mutex> lock(saListenersMtx_);
94 if (saListeners_.find(saId) == saListeners_.end()) {
95 DHLOGW("can not find sa listener info, id: %" PRIu32, saId);
96 return;
97 }
98
99 sptr<ISystemAbilityManager> systemAbilityManager =
100 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101 if (!systemAbilityManager) {
102 DHLOGE("get system ability manager failed.");
103 return;
104 }
105
106 int32_t ret = systemAbilityManager->UnSubscribeSystemAbility(saId, saListeners_[saId]);
107 if (ret != DH_FWK_SUCCESS) {
108 DHLOGE("unsubscribe sa change listener failed: %d", ret);
109 return;
110 }
111
112 saListeners_.erase(saId);
113 DHLOGI("unsubscribe sa change listener success");
114 return;
115 }
116 } // namespace DistributedHardware
117 } // namespace OHOS