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 
GetInstance()26 SocPerfClient& SocPerfClient::GetInstance()
27 {
28     static SocPerfClient instance;
29     return instance;
30 }
31 
CheckClientValid()32 bool SocPerfClient::CheckClientValid()
33 {
34     if (client) {
35         return true;
36     }
37 
38     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39     if (!samgr) {
40         SOC_PERF_LOGE("Failed to get SystemAbilityManager.");
41         return false;
42     }
43 
44     sptr<IRemoteObject> object = samgr->CheckSystemAbility(SOC_PERF_SERVICE_SA_ID);
45     if (!object) {
46         SOC_PERF_LOGE("Failed to get SystemAbility[1906].");
47         return false;
48     }
49 
50     client = iface_cast<ISocPerfService>(object);
51     if (!client || !client->AsObject()) {
52         SOC_PERF_LOGE("Failed to get SocPerfClient.");
53         return false;
54     }
55 
56     recipient_ = new (std::nothrow) SocPerfDeathRecipient(*this);
57     if (!recipient_) {
58         return false;
59     }
60     client->AsObject()->AddDeathRecipient(recipient_);
61 
62     return true;
63 }
64 
ResetClient()65 void SocPerfClient::ResetClient()
66 {
67     std::lock_guard<std::mutex> lock(mutex_);
68     if (client && client->AsObject()) {
69         client->AsObject()->RemoveDeathRecipient(recipient_);
70     }
71     client = nullptr;
72 }
73 
SocPerfDeathRecipient(SocPerfClient & socPerfClient)74 SocPerfClient::SocPerfDeathRecipient::SocPerfDeathRecipient(SocPerfClient &socPerfClient)
75     : socPerfClient_(socPerfClient) {}
76 
~SocPerfDeathRecipient()77 SocPerfClient::SocPerfDeathRecipient::~SocPerfDeathRecipient() {}
78 
OnRemoteDied(const wptr<IRemoteObject> & remote)79 void SocPerfClient::SocPerfDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
80 {
81     socPerfClient_.ResetClient();
82 }
83 
AddPidAndTidInfo(const std::string & msg)84 std::string SocPerfClient::AddPidAndTidInfo(const std::string& msg)
85 {
86     std::string str;
87     int32_t pid = getpid();
88     int32_t tid = gettid();
89     str.append("pid=").append(std::to_string(pid)).append("|");
90     str.append("tid=").append(std::to_string(tid));
91     if (msg.size() > 0) {
92         str.append("|").append(msg);
93     }
94     return str;
95 }
96 
PerfRequest(int32_t cmdId,const std::string & msg)97 void SocPerfClient::PerfRequest(int32_t cmdId, const std::string& msg)
98 {
99     std::lock_guard<std::mutex> lock(mutex_);
100     if (!CheckClientValid()) {
101         return;
102     }
103     std::string newMsg = AddPidAndTidInfo(msg);
104     client->PerfRequest(cmdId, newMsg);
105 }
106 
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)107 void SocPerfClient::PerfRequestEx(int32_t cmdId, bool onOffTag, 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->PerfRequestEx(cmdId, onOffTag, newMsg);
115 }
116 
PowerLimitBoost(bool onOffTag,const std::string & msg)117 void SocPerfClient::PowerLimitBoost(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->PowerLimitBoost(onOffTag, newMsg);
125 }
126 
ThermalLimitBoost(bool onOffTag,const std::string & msg)127 void SocPerfClient::ThermalLimitBoost(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->ThermalLimitBoost(onOffTag, newMsg);
135 }
136 
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)137 void SocPerfClient::LimitRequest(int32_t clientId,
138     const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     if (!CheckClientValid()) {
142         return;
143     }
144     std::string newMsg = AddPidAndTidInfo(msg);
145     client->LimitRequest(clientId, tags, configs, newMsg);
146 }
147 
SetRequestStatus(bool status,const std::string & msg)148 void SocPerfClient::SetRequestStatus(bool status, 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->SetRequestStatus(status, newMsg);
156 }
157 
SetThermalLevel(int32_t level)158 void SocPerfClient::SetThermalLevel(int32_t level)
159 {
160     std::lock_guard<std::mutex> lock(mutex_);
161     if (!CheckClientValid()) {
162         return;
163     }
164     client->SetThermalLevel(level);
165 }
166 
RequestDeviceMode(const std::string & mode,bool status)167 void SocPerfClient::RequestDeviceMode(const std::string& mode, bool status)
168 {
169     std::lock_guard<std::mutex> lock(mutex_);
170     if (!CheckClientValid() || mode.length() > MAX_MODE_LEN) {
171         return;
172     }
173     client->RequestDeviceMode(mode, status);
174 }
175 
RequestCmdIdCount(const std::string & msg)176 std::string SocPerfClient::RequestCmdIdCount(const std::string& msg)
177 {
178     std::lock_guard<std::mutex> lock(mutex_);
179     if (!CheckClientValid()) {
180         return "";
181     }
182     return client->RequestCmdIdCount(msg);
183 }
184 } // namespace SOCPERF
185 } // namespace OHOS
186