• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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     HILOGI("%{public}s called.", __func__);
22     static DmsSaClient instance;
23     return instance;
24 }
25 
SubscribeDmsSA()26 bool DmsSaClient::SubscribeDmsSA()
27 {
28     HILOGI("%{public}s called.", __func__);
29     sptr<DmsSystemAbilityStatusChange> callback(new DmsSystemAbilityStatusChange());
30     int32_t ret = saMgrProxy_->SubscribeSystemAbility(DISTRIBUTED_SCHED_SA_ID, callback);
31     if (ret != ERR_OK) {
32         HILOGE("Failed to subscribe system ability DISTRIBUTED_SCHED_SA_ID ret:%{public}d", ret);
33         return false;
34     }
35     return true;
36 }
37 
AddDSchedEventListener(const std::string & type,const sptr<IDSchedEventListener> & listener)38 int32_t DmsSaClient::AddDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener)
39 {
40     HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
41     saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (saMgrProxy_ == nullptr) {
43         HILOGE("fail to get saMgrProxy.");
44         return AAFwk::INNER_ERR;
45     }
46     if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
47         DistributedClient distributedClient;
48         distributedClient.RegisterDSchedEventListener(type, listener);
49     }
50     std::lock_guard<std::mutex> lock(eventMutex_);
51     if (!hasSubscribeDmsSA_) {
52         if (SubscribeDmsSA()) {
53             hasSubscribeDmsSA_ = true;
54             listeners_[type] = listener;
55         } else {
56             return AAFwk::INNER_ERR;
57         }
58     }
59     return NO_ERROR;
60 }
61 
DelDSchedEventListener(const std::string & type,const sptr<IDSchedEventListener> & listener)62 int32_t DmsSaClient::DelDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener)
63 {
64     HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
65     if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
66         DistributedClient distributedClient;
67         distributedClient.UnRegisterDSchedEventListener(type, listener);
68     }
69     std::lock_guard<std::mutex> lock(eventMutex_);
70     listeners_.erase(type);
71     return NO_ERROR;
72 }
73 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)74 void DmsSaClient::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
75 {
76     HILOGI("%{public}s called, the systemAbilityId is %{public}d", __func__, systemAbilityId);
77     std::lock_guard<std::mutex> lock(eventMutex_);
78     if (systemAbilityId == DISTRIBUTED_SCHED_SA_ID) {
79         HILOGI("%{public}s listeners size: %{public}zu .", __func__, listeners_.size());
80         for (auto& listener : listeners_) {
81             DistributedClient distributedClient;
82             distributedClient.RegisterDSchedEventListener(listener.first, listener.second);
83         }
84     } else {
85         HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %{public}d", systemAbilityId);
86     }
87 }
88 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)89 void DmsSaClient::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
90 {
91 }
92 
DmsSystemAbilityStatusChange()93 DmsSystemAbilityStatusChange::DmsSystemAbilityStatusChange()
94 {
95     HILOGI("DmsSystemAbilityStatusChange create");
96 }
97 
~DmsSystemAbilityStatusChange()98 DmsSystemAbilityStatusChange::~DmsSystemAbilityStatusChange()
99 {
100     HILOGI("DmsSystemAbilityStatusChange delete");
101 }
102 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)103 void DmsSystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
104 {
105     HILOGI("OnAddSystemAbility called, the systemAbilityId is %d", systemAbilityId);
106     if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
107         HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %d", systemAbilityId);
108         return;
109     }
110 
111     DmsSaClient::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
112     HILOGI("OnAddSystemAbility called end, the systemAbilityId is %d", systemAbilityId);
113 }
114 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)115 void DmsSystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
116 {
117     HILOGI("OnRemoveSystemAbility called, the systemAbilityId is %d", systemAbilityId);
118     if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
119         HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %d", systemAbilityId);
120         return;
121     }
122 
123     // notify listener
124 }
125 }  // namespace DistributedSchedule
126 }  // namespace OHOS