1 /*
2  * Copyright (c) 2022-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 
16 #include "socperf_client.h"
17 #include <unistd.h>              // for getpid, gettid
18 #include "iservice_registry.h"
19 #include "socperf_log.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace SOCPERF {
24 const int32_t MAX_MODE_LEN = 64;
25 
SocPerfClient()26 SocPerfClient::SocPerfClient()
27 {
28 }
29 
~SocPerfClient()30 SocPerfClient::~SocPerfClient()
31 {
32     SOC_PERF_LOGI("SocPerfClient:~SocPerfClien");
33 }
34 
GetInstance()35 SocPerfClient& SocPerfClient::GetInstance()
36 {
37     static SocPerfClient instance;
38     return instance;
39 }
40 
CheckClientValid()41 bool SocPerfClient::CheckClientValid()
42 {
43     if (client) {
44         return true;
45     }
46 
47     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48     if (!samgr) {
49         SOC_PERF_LOGE("Failed to get SystemAbilityManager.");
50         return false;
51     }
52 
53     sptr<IRemoteObject> object = samgr->CheckSystemAbility(SOC_PERF_SERVICE_SA_ID);
54     if (!object) {
55         SOC_PERF_LOGE("Failed to get SystemAbility[1906].");
56         return false;
57     }
58 
59     client = iface_cast<ISocPerfService>(object);
60     if (!client || !client->AsObject()) {
61         SOC_PERF_LOGE("Failed to get SocPerfClient.");
62         return false;
63     }
64 
65     recipient_ = new (std::nothrow) SocPerfDeathRecipient(*this);
66     if (!recipient_) {
67         return false;
68     }
69     client->AsObject()->AddDeathRecipient(recipient_);
70     SOC_PERF_LOGI("SocPerfClient:new client");
71     return true;
72 }
73 
ResetClient()74 void SocPerfClient::ResetClient()
75 {
76     std::lock_guard<std::mutex> lock(mutex_);
77     SOC_PERF_LOGI("SocPerfClient:ResetClient");
78     if (client && client->AsObject()) {
79         client->AsObject()->RemoveDeathRecipient(recipient_);
80     }
81     client = nullptr;
82 }
83 
SocPerfDeathRecipient(SocPerfClient &socPerfClient)84 SocPerfClient::SocPerfDeathRecipient::SocPerfDeathRecipient(SocPerfClient &socPerfClient)
85     : socPerfClient_(socPerfClient) {}
86 
~SocPerfDeathRecipient()87 SocPerfClient::SocPerfDeathRecipient::~SocPerfDeathRecipient() {}
88 
OnRemoteDied(const wptr<IRemoteObject> &remote)89 void SocPerfClient::SocPerfDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
90 {
91     socPerfClient_.ResetClient();
92 }
93 
AddPidAndTidInfo(const std::string& msg)94 std::string SocPerfClient::AddPidAndTidInfo(const std::string& msg)
95 {
96     std::string str;
97     int32_t pid = getpid();
98     int32_t tid = gettid();
99     str.append("pid=").append(std::to_string(pid)).append("|");
100     str.append("tid=").append(std::to_string(tid));
101     if (msg.size() > 0) {
102         str.append("|").append(msg);
103     }
104     return str;
105 }
106 
PerfRequest(int32_t cmdId, const std::string& msg)107 void SocPerfClient::PerfRequest(int32_t cmdId, const std::string& msg)
108 {
109     std::lock_guard<std::mutex> lock(mutex_);
110     if (!CheckClientValid()) {
111         return;
112     }
113     std::string newMsg = AddPidAndTidInfo(msg);
114     client->PerfRequest(cmdId, newMsg);
115 }
116 
PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)117 void SocPerfClient::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
118 {
119     std::lock_guard<std::mutex> lock(mutex_);
120     if (!CheckClientValid()) {
121         return;
122     }
123     std::string newMsg = AddPidAndTidInfo(msg);
124     client->PerfRequestEx(cmdId, onOffTag, newMsg);
125 }
126 
PowerLimitBoost(bool onOffTag, const std::string& msg)127 void SocPerfClient::PowerLimitBoost(bool onOffTag, const std::string& msg)
128 {
129     std::lock_guard<std::mutex> lock(mutex_);
130     if (!CheckClientValid()) {
131         return;
132     }
133     std::string newMsg = AddPidAndTidInfo(msg);
134     client->PowerLimitBoost(onOffTag, newMsg);
135 }
136 
ThermalLimitBoost(bool onOffTag, const std::string& msg)137 void SocPerfClient::ThermalLimitBoost(bool onOffTag, const std::string& msg)
138 {
139     std::lock_guard<std::mutex> lock(mutex_);
140     if (!CheckClientValid()) {
141         return;
142     }
143     std::string newMsg = AddPidAndTidInfo(msg);
144     client->ThermalLimitBoost(onOffTag, newMsg);
145 }
146 
LimitRequest(int32_t clientId, const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)147 void SocPerfClient::LimitRequest(int32_t clientId,
148     const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
149 {
150     std::lock_guard<std::mutex> lock(mutex_);
151     if (!CheckClientValid()) {
152         return;
153     }
154     std::string newMsg = AddPidAndTidInfo(msg);
155     client->LimitRequest(clientId, tags, configs, newMsg);
156 }
157 
SetRequestStatus(bool status, const std::string& msg)158 void SocPerfClient::SetRequestStatus(bool status, const std::string& msg)
159 {
160     std::lock_guard<std::mutex> lock(mutex_);
161     if (!CheckClientValid()) {
162         return;
163     }
164     std::string newMsg = AddPidAndTidInfo(msg);
165     client->SetRequestStatus(status, newMsg);
166 }
167 
SetThermalLevel(int32_t level)168 void SocPerfClient::SetThermalLevel(int32_t level)
169 {
170     std::lock_guard<std::mutex> lock(mutex_);
171     if (!CheckClientValid()) {
172         return;
173     }
174     client->SetThermalLevel(level);
175 }
176 
RequestDeviceMode(const std::string& mode, bool status)177 void SocPerfClient::RequestDeviceMode(const std::string& mode, bool status)
178 {
179     std::lock_guard<std::mutex> lock(mutex_);
180     if (!CheckClientValid() || mode.length() > MAX_MODE_LEN) {
181         return;
182     }
183     client->RequestDeviceMode(mode, status);
184 }
185 
RequestCmdIdCount(const std::string& msg)186 std::string SocPerfClient::RequestCmdIdCount(const std::string& msg)
187 {
188     std::lock_guard<std::mutex> lock(mutex_);
189     if (!CheckClientValid()) {
190         return "";
191     }
192     return client->RequestCmdIdCount(msg);
193 }
194 } // namespace SOCPERF
195 } // namespace OHOS
196