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
QueryInterval(int queryItem,IntervalReply & queryRs)46 void ConcurrentTaskClient::QueryInterval(int queryItem, IntervalReply& queryRs)
47 {
48 if (TryConnect() != ERR_OK) {
49 CONCUR_LOGE("QueryInterval connnect fail");
50 return;
51 }
52 clientService_->QueryInterval(queryItem, queryRs);
53 return;
54 }
55
QueryDeadline(int queryItem,DeadlineReply & ddlReply,const std::unordered_map<pid_t,uint32_t> & mapPayload)56 void ConcurrentTaskClient::QueryDeadline(int queryItem, DeadlineReply& ddlReply,
57 const std::unordered_map<pid_t, uint32_t>& mapPayload)
58 {
59 if (TryConnect() != ERR_OK) {
60 return;
61 }
62 Json::Value payload;
63 for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
64 payload[std::to_string(it->first)] = std::to_string(it->second);
65 }
66 clientService_->QueryDeadline(queryItem, ddlReply, payload);
67 return;
68 }
69
RequestAuth(const std::unordered_map<std::string,std::string> & mapPayload)70 void ConcurrentTaskClient::RequestAuth(const std::unordered_map<std::string, std::string>& 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[it->first] = it->second;
78 }
79 clientService_->RequestAuth(payload);
80 return;
81 }
82
QueryDeadline(int queryItem,DeadlineReply & ddlReply,const std::unordered_map<std::string,std::string> & mapPayload)83 void ConcurrentTaskClient::QueryDeadline(int queryItem, DeadlineReply& ddlReply,
84 const std::unordered_map<std::string, std::string>& mapPayload)
85 {
86 if (TryConnect() != ERR_OK) {
87 return;
88 }
89 Json::Value payload;
90 for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
91 payload[it->first] = it->second;
92 }
93 clientService_->QueryDeadline(queryItem, ddlReply, payload);
94 return;
95 }
96
TryConnect()97 ErrCode ConcurrentTaskClient::TryConnect()
98 {
99 std::lock_guard<std::mutex> lock(mutex_);
100 if (clientService_) {
101 return ERR_OK;
102 }
103
104 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
105 if (!systemManager) {
106 CONCUR_LOGE("ConcurrentTaskClient::Fail to get registry.");
107 return GET_CONCURRENT_TASK_SERVICE_FAILED;
108 }
109
110 remoteObject_ = systemManager->GetSystemAbility(CONCURRENT_TASK_SERVICE_ID);
111 if (!remoteObject_) {
112 CONCUR_LOGE("ConcurrentTaskClient::Fail to connect concurrent task schedule service.");
113 return GET_CONCURRENT_TASK_SERVICE_FAILED;
114 }
115
116 clientService_ = iface_cast<IConcurrentTaskService>(remoteObject_);
117 if (!clientService_) {
118 return GET_CONCURRENT_TASK_SERVICE_FAILED;
119 }
120 try {
121 recipient_ = new ConcurrentTaskDeathRecipient(*this);
122 } catch (const std::bad_alloc& e) {
123 CONCUR_LOGE("ConcurrentTaskClient::New ConcurrentTaskDeathRecipient fail.");
124 }
125 if (!recipient_) {
126 return GET_CONCURRENT_TASK_SERVICE_FAILED;
127 }
128 clientService_->AsObject()->AddDeathRecipient(recipient_);
129 CONCUR_LOGD("ConcurrentTaskClient::Connect concurrent task service success.");
130 return ERR_OK;
131 }
132
StopRemoteObject()133 void ConcurrentTaskClient::StopRemoteObject()
134 {
135 if (clientService_ && clientService_->AsObject()) {
136 clientService_->AsObject()->RemoveDeathRecipient(recipient_);
137 }
138 clientService_ = nullptr;
139 }
140
ConcurrentTaskDeathRecipient(ConcurrentTaskClient & concurrentTaskClient)141 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::ConcurrentTaskDeathRecipient(
142 ConcurrentTaskClient& concurrentTaskClient) : concurrentTaskClient_(concurrentTaskClient) {}
143
~ConcurrentTaskDeathRecipient()144 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::~ConcurrentTaskDeathRecipient() {}
145
OnRemoteDied(const wptr<IRemoteObject> & object)146 void ConcurrentTaskClient::ConcurrentTaskDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
147 {
148 concurrentTaskClient_.StopRemoteObject();
149 }
150
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
CTC_QueryInterval(int queryItem,OHOS::ConcurrentTask::IntervalReply & queryRs)154 void CTC_QueryInterval(int queryItem, OHOS::ConcurrentTask::IntervalReply& queryRs)
155 {
156 OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().QueryInterval(queryItem, queryRs);
157 }
158 #ifdef __cplusplus
159 }
160 #endif
161 } // namespace ConcurrentTask
162 } // namespace OHOS
163