• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "concurrent_task_client.h"
16 #include <cinttypes>
17 #include "if_system_ability_manager.h"
18 #include "iservice_registry.h"
19 #include "concurrent_task_log.h"
20 #include "concurrent_task_errors.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace ConcurrentTask {
GetInstance()25 ConcurrentTaskClient& ConcurrentTaskClient::GetInstance()
26 {
27     static ConcurrentTaskClient instance;
28     return instance;
29 }
30 
ReportData(uint32_t resType,int64_t value,const std::unordered_map<std::string,std::string> & mapPayload)31 void ConcurrentTaskClient::ReportData(uint32_t resType, int64_t value,
32                                       const std::unordered_map<std::string, std::string>& mapPayload)
33 {
34     CONCUR_LOGD("ConcurrentTaskClient::ReportData receive resType = %{public}u, value = %{public}" PRId64 ".",
35                   resType, value);
36     if (TryConnect() != ERR_OK) {
37         return;
38     }
39     Json::Value payload;
40     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
41         payload[it->first] = it->second;
42     }
43     clientService_->ReportData(resType, value, payload);
44 }
45 
ReportSceneInfo(uint32_t type,const std::unordered_map<std::string,std::string> & mapPayload)46 void ConcurrentTaskClient::ReportSceneInfo(uint32_t type,
47                                            const std::unordered_map<std::string, std::string>& mapPayload)
48 {
49     if (TryConnect() != ERR_OK) {
50         return;
51     }
52     Json::Value payload;
53     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
54         payload[it->first] = it->second;
55     }
56     clientService_->ReportSceneInfo(type, payload);
57 }
58 
QueryInterval(int queryItem,IntervalReply & queryRs)59 void ConcurrentTaskClient::QueryInterval(int queryItem, IntervalReply& queryRs)
60 {
61     if (TryConnect() != ERR_OK) {
62         CONCUR_LOGE("QueryInterval connnect fail");
63         return;
64     }
65     clientService_->QueryInterval(queryItem, queryRs);
66     return;
67 }
68 
QueryDeadline(int queryItem,DeadlineReply & ddlReply,const std::unordered_map<pid_t,uint32_t> & mapPayload)69 void ConcurrentTaskClient::QueryDeadline(int queryItem, DeadlineReply& ddlReply,
70                                          const std::unordered_map<pid_t, uint32_t>& mapPayload)
71 {
72     if (TryConnect() != ERR_OK) {
73         return;
74     }
75     Json::Value payload;
76     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
77         payload[std::to_string(it->first)] = std::to_string(it->second);
78     }
79     clientService_->QueryDeadline(queryItem, ddlReply, payload);
80     return;
81 }
82 
RequestAuth(const std::unordered_map<std::string,std::string> & mapPayload)83 void ConcurrentTaskClient::RequestAuth(const std::unordered_map<std::string, std::string>& mapPayload)
84 {
85     if (TryConnect() != ERR_OK) {
86         return;
87     }
88     Json::Value payload;
89     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
90         payload[it->first] = it->second;
91     }
92     clientService_->RequestAuth(payload);
93     return;
94 }
95 
QueryDeadline(int queryItem,DeadlineReply & ddlReply,const std::unordered_map<std::string,std::string> & mapPayload)96 void ConcurrentTaskClient::QueryDeadline(int queryItem, DeadlineReply& ddlReply,
97                                          const std::unordered_map<std::string, std::string>& mapPayload)
98 {
99     if (TryConnect() != ERR_OK) {
100         return;
101     }
102     Json::Value payload;
103     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
104         payload[it->first] = it->second;
105     }
106     clientService_->QueryDeadline(queryItem, ddlReply, payload);
107     return;
108 }
109 
TryConnect()110 ErrCode ConcurrentTaskClient::TryConnect()
111 {
112     std::lock_guard<std::mutex> lock(mutex_);
113     if (clientService_) {
114         return ERR_OK;
115     }
116 
117     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
118     if (!systemManager) {
119         CONCUR_LOGE("ConcurrentTaskClient::Fail to get registry.");
120         return GET_CONCURRENT_TASK_SERVICE_FAILED;
121     }
122 
123     remoteObject_ = systemManager->GetSystemAbility(CONCURRENT_TASK_SERVICE_ID);
124     if (!remoteObject_) {
125         CONCUR_LOGE("ConcurrentTaskClient::Fail to connect concurrent task schedule service.");
126         return GET_CONCURRENT_TASK_SERVICE_FAILED;
127     }
128 
129     clientService_ = iface_cast<IConcurrentTaskService>(remoteObject_);
130     if (!clientService_) {
131         return GET_CONCURRENT_TASK_SERVICE_FAILED;
132     }
133     try {
134         recipient_ = new ConcurrentTaskDeathRecipient(*this);
135     } catch (const std::bad_alloc& e) {
136         CONCUR_LOGE("ConcurrentTaskClient::New ConcurrentTaskDeathRecipient fail.");
137     }
138     if (!recipient_) {
139         return GET_CONCURRENT_TASK_SERVICE_FAILED;
140     }
141     clientService_->AsObject()->AddDeathRecipient(recipient_);
142     CONCUR_LOGD("ConcurrentTaskClient::Connect concurrent task service success.");
143     return ERR_OK;
144 }
145 
StopRemoteObject()146 void ConcurrentTaskClient::StopRemoteObject()
147 {
148     if (clientService_ && clientService_->AsObject()) {
149         clientService_->AsObject()->RemoveDeathRecipient(recipient_);
150     }
151     clientService_ = nullptr;
152 }
153 
ConcurrentTaskDeathRecipient(ConcurrentTaskClient & concurrentTaskClient)154 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::ConcurrentTaskDeathRecipient(
155     ConcurrentTaskClient& concurrentTaskClient) : concurrentTaskClient_(concurrentTaskClient) {}
156 
~ConcurrentTaskDeathRecipient()157 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::~ConcurrentTaskDeathRecipient() {}
158 
OnRemoteDied(const wptr<IRemoteObject> & object)159 void ConcurrentTaskClient::ConcurrentTaskDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
160 {
161     concurrentTaskClient_.StopRemoteObject();
162 }
163 
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
CTC_QueryInterval(int queryItem,OHOS::ConcurrentTask::IntervalReply & queryRs)167 void CTC_QueryInterval(int queryItem, OHOS::ConcurrentTask::IntervalReply& queryRs)
168 {
169     OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().QueryInterval(queryItem, queryRs);
170 }
171 #ifdef __cplusplus
172 }
173 #endif
174 } // namespace ConcurrentTask
175 } // namespace OHOS
176