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