1 /*
2 * Copyright (c) 2023-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 #include "dms_sa_client.h"
16
17 namespace OHOS {
18 namespace DistributedSchedule {
GetInstance()19 DmsSaClient &DmsSaClient::GetInstance()
20 {
21 HILOGD("called.");
22 static DmsSaClient instance;
23 return instance;
24 }
25
SubscribeDmsSA()26 bool DmsSaClient::SubscribeDmsSA()
27 {
28 HILOGD("called.");
29 if (!saMgrProxy_) {
30 HILOGE("saMgrProxy_ is null.");
31 return false;
32 }
33 sptr<DmsSystemAbilityStatusChange> callback(new DmsSystemAbilityStatusChange());
34 if (!callback) {
35 HILOGE("Failed to create callback object.");
36 return false;
37 }
38
39 int32_t ret = saMgrProxy_->SubscribeSystemAbility(DISTRIBUTED_SCHED_SA_ID, callback);
40 if (ret != ERR_OK) {
41 HILOGE("Failed to subscribe system ability DISTRIBUTED_SCHED_SA_ID ret:%{public}d", ret);
42 return false;
43 }
44 return true;
45 }
46
AddDSchedEventListener(const DSchedEventType & type,const sptr<IDSchedEventListener> & listener)47 int32_t DmsSaClient::AddDSchedEventListener(const DSchedEventType& type, const sptr<IDSchedEventListener>& listener)
48 {
49 HILOGI("%{public}s called, the type is %{public}d", __func__, type);
50 {
51 std::lock_guard<std::mutex> lock(saMgrMutex_);
52 saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53 }
54 if (saMgrProxy_ == nullptr) {
55 HILOGE("fail to get saMgrProxy.");
56 return AAFwk::INNER_ERR;
57 }
58 if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
59 DistributedClient distributedClient;
60 distributedClient.RegisterDSchedEventListener(type, listener);
61 }
62 if (!hasSubscribeDmsSA_) {
63 if (SubscribeDmsSA()) {
64 hasSubscribeDmsSA_ = true;
65 {
66 std::lock_guard<std::mutex> lock(eventMutex_);
67 listeners_[type] = listener;
68 }
69 } else {
70 return AAFwk::INNER_ERR;
71 }
72 }
73 return NO_ERROR;
74 }
75
DelDSchedEventListener(const DSchedEventType & type,const sptr<IDSchedEventListener> & listener)76 int32_t DmsSaClient::DelDSchedEventListener(const DSchedEventType& type, const sptr<IDSchedEventListener>& listener)
77 {
78 HILOGI("%{public}s called, the type is %{public}d", __func__, type);
79 {
80 std::lock_guard<std::mutex> lock(saMgrMutex_);
81 saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82 }
83 if (saMgrProxy_ == nullptr) {
84 HILOGE("fail to get saMgrProxy.");
85 return AAFwk::INNER_ERR;
86 }
87 if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
88 DistributedClient distributedClient;
89 distributedClient.UnRegisterDSchedEventListener(type, listener);
90 }
91 std::lock_guard<std::mutex> lock(eventMutex_);
92 listeners_.erase(type);
93 return NO_ERROR;
94 }
95
GetContinueInfo(ContinueInfo & continueInfo)96 int32_t DmsSaClient::GetContinueInfo(ContinueInfo &continueInfo)
97 {
98 HILOGI("%{public}s called", __func__);
99 {
100 std::lock_guard<std::mutex> lock(saMgrMutex_);
101 saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 }
103 if (saMgrProxy_ == nullptr) {
104 HILOGE("fail to get saMgrProxy.");
105 return AAFwk::INNER_ERR;
106 }
107 if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
108 DistributedClient distributedClient;
109 distributedClient.GetContinueInfo(continueInfo);
110 }
111 return NO_ERROR;
112 }
113
GetDSchedEventInfo(const DSchedEventType & type,std::vector<EventNotify> & events)114 int32_t DmsSaClient::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
115 {
116 HILOGI("%{public}s called", __func__);
117 {
118 std::lock_guard<std::mutex> lock(saMgrMutex_);
119 saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
120 }
121 if (saMgrProxy_ == nullptr) {
122 HILOGE("Get SA manager proxy fail.");
123 return AAFwk::INNER_ERR;
124 }
125 if (!saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
126 HILOGE("Get SA manager proxy fail.");
127 return AAFwk::INNER_ERR;
128 }
129
130 DistributedClient distributedClient;
131 int32_t ret = distributedClient.GetDSchedEventInfo(type, events);
132 if (ret != ERR_OK) {
133 HILOGE("Get dms event Info proxy call fail, ret %{public}d.", ret);
134 }
135 return ret;
136 }
137
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)138 void DmsSaClient::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
139 {
140 HILOGI("%{public}s called, the systemAbilityId is %{public}d", __func__, systemAbilityId);
141 std::lock_guard<std::mutex> lock(eventMutex_);
142 if (systemAbilityId == DISTRIBUTED_SCHED_SA_ID) {
143 HILOGI("%{public}s listeners size: %{public}zu .", __func__, listeners_.size());
144 for (auto& listener : listeners_) {
145 DistributedClient distributedClient;
146 distributedClient.RegisterDSchedEventListener(listener.first, listener.second);
147 }
148 } else {
149 HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %{public}d", systemAbilityId);
150 }
151 }
152
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)153 void DmsSaClient::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
154 {
155 }
156
DmsSystemAbilityStatusChange()157 DmsSystemAbilityStatusChange::DmsSystemAbilityStatusChange()
158 {
159 HILOGI("DmsSystemAbilityStatusChange create");
160 }
161
~DmsSystemAbilityStatusChange()162 DmsSystemAbilityStatusChange::~DmsSystemAbilityStatusChange()
163 {
164 HILOGI("DmsSystemAbilityStatusChange delete");
165 }
166
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)167 void DmsSystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
168 {
169 HILOGI("OnAddSystemAbility called, the systemAbilityId is %d", systemAbilityId);
170 if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
171 HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID, but it is %{public}d", systemAbilityId);
172 return;
173 }
174
175 DmsSaClient::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
176 HILOGI("OnAddSystemAbility called end, the systemAbilityId is %d", systemAbilityId);
177 }
178
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)179 void DmsSystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
180 {
181 HILOGI("OnRemoveSystemAbility called, the systemAbilityId is %d", systemAbilityId);
182 if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
183 HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID, but it is %{public}d", systemAbilityId);
184 return;
185 }
186
187 // notify listener
188 }
189 } // namespace DistributedSchedule
190 } // namespace OHOS