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