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
16 #include "thermal_simulation_node.h"
17
18 #include <iostream>
19 #include <cstring>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24
25 #include "hdf_base.h"
26 #include "securec.h"
27 #include "thermal_log.h"
28
29 namespace OHOS {
30 namespace HDI {
31 namespace Thermal {
32 namespace V1_0 {
33 namespace {
34 const int32_t MAX_PATH = 256;
35 const int32_t ARG_0 = 0;
36 const int32_t ARG_1 = 1;
37 const int32_t ARG_2 = 2;
38 const int32_t ARG_3 = 3;
39 const int32_t ARG_4 = 4;
40 const int32_t NUM_ZERO = 0;
41 const std::string THERMAL_DIR = "/data/service/el0/thermal/sensor/";
42 const std::string THERMAL_NODE_DIR = "/data/service/el0/thermal/sensor/%s";
43 const std::string THERMAL_TYPE_DIR = "/data/service/el0/thermal/sensor/%s/type";
44 const std::string THERMAL_TEMP_DIR = "/data/service/el0/thermal/sensor/%s/temp";
45 const std::string MITIGATION_DIR = "/data/service/el0/thermal/cooling";
46 const std::string MITIGATION_NODE_DIR = "/data/service/el0/thermal/cooling/%s";
47 const std::string MITIGATION_NODE_FILE = "%s/%s";
48 }
NodeInit()49 int32_t ThermalSimulationNode::NodeInit()
50 {
51 int32_t ret = AddSensorTypeTemp();
52 if (ret != HDF_SUCCESS) {
53 return ret;
54 }
55 ret = AddMitigationDevice();
56 if (ret != HDF_SUCCESS) {
57 return ret;
58 }
59 return HDF_SUCCESS;
60 }
61
CreateNodeDir(std::string dir)62 int32_t ThermalSimulationNode::CreateNodeDir(std::string dir)
63 {
64 if (access(dir.c_str(), 0) != NUM_ZERO) {
65 int32_t flag = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH| S_IXOTH);
66 if (flag == NUM_ZERO) {
67 THERMAL_HILOGI(COMP_HDI, "Create directory successfully.");
68 } else {
69 THERMAL_HILOGE(COMP_HDI, "Fail to create directory, flag: %{public}d", flag);
70 return flag;
71 }
72 } else {
73 THERMAL_HILOGD(COMP_HDI, "This directory already exists.");
74 }
75 return HDF_SUCCESS;
76 }
77
CreateNodeFile(std::string filePath)78 int32_t ThermalSimulationNode::CreateNodeFile(std::string filePath)
79 {
80 if (access(filePath.c_str(), 0) != 0) {
81 int32_t fd = open(filePath.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
82 if (fd < NUM_ZERO) {
83 THERMAL_HILOGE(COMP_HDI, "open failed to file.");
84 return fd;
85 }
86 close(fd);
87 } else {
88 THERMAL_HILOGD(COMP_HDI, "the file already exists.");
89 }
90 return HDF_SUCCESS;
91 }
92
AddSensorTypeTemp()93 int32_t ThermalSimulationNode::AddSensorTypeTemp()
94 {
95 std::vector<std::string> vFile = {"type", "temp"};
96 std::map<std::string, int32_t> sensor;
97 char nodeBuf[MAX_PATH] = {0};
98 char fileBuf[MAX_PATH] = {0};
99 char typeBuf[MAX_PATH] = {0};
100 char tempBuf[MAX_PATH] = {0};
101 sensor["battery"] = 0;
102 sensor["charger"] = 0;
103 sensor["pa"] = 0;
104 sensor["ap"] = 0;
105 sensor["ambient"] = 0;
106 sensor["cpu"] = 0;
107 sensor["soc"] = 0;
108 sensor["shell"] = 0;
109 CreateNodeDir(THERMAL_DIR);
110 for (auto dir : sensor) {
111 int32_t ret = snprintf_s(nodeBuf, MAX_PATH, sizeof(nodeBuf) - ARG_1,
112 THERMAL_NODE_DIR.c_str(), dir.first.c_str());
113 if (ret < EOK) {
114 return HDF_FAILURE;
115 }
116 THERMAL_HILOGI(COMP_HDI, "node name: %{public}s", nodeBuf);
117 CreateNodeDir(static_cast<std::string>(nodeBuf));
118 for (const auto& file : vFile) {
119 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, "%s/%s", nodeBuf, file.c_str());
120 if (ret < EOK) {
121 return HDF_FAILURE;
122 }
123 THERMAL_HILOGI(COMP_HDI, "file name: %{public}s", fileBuf);
124 CreateNodeFile(static_cast<std::string>(fileBuf));
125 }
126 ret = snprintf_s(typeBuf, MAX_PATH, sizeof(typeBuf) - ARG_1, THERMAL_TYPE_DIR.c_str(), dir.first.c_str());
127 if (ret < EOK) {
128 return HDF_FAILURE;
129 }
130 std::string type = dir.first;
131 WriteFile(typeBuf, type, type.length());
132 ret = snprintf_s(tempBuf, MAX_PATH, sizeof(tempBuf) - ARG_1, THERMAL_TEMP_DIR.c_str(), dir.first.c_str());
133 if (ret < EOK) {
134 return HDF_FAILURE;
135 }
136 std::string temp = std::to_string(dir.second);
137 WriteFile(tempBuf, temp, temp.length());
138 }
139 return HDF_SUCCESS;
140 }
141
AddMitigationDevice()142 int32_t ThermalSimulationNode::AddMitigationDevice()
143 {
144 int32_t ret;
145 std::string sensor[] = {"cpu", "charger", "gpu", "battery"};
146 std::vector<std::string> vSensor(sensor, sensor + ARG_4);
147 std::string cpu = "freq";
148 std::string charger = "current";
149 std::string gpu = "freq";
150 std::string battery[] = {"current", "voltage"};
151 std::vector<std::string> vFile;
152 char nodeBuf[MAX_PATH] = {0};
153 char fileBuf[MAX_PATH] = {0};
154 int32_t temp = 0;
155 std::string sTemp = std::to_string(temp);
156 CreateNodeDir(MITIGATION_DIR);
157 for (auto dir : vSensor) {
158 ret = snprintf_s(nodeBuf, MAX_PATH, sizeof(nodeBuf) - ARG_1, MITIGATION_NODE_DIR.c_str(), dir.c_str());
159 if (ret < EOK) return HDF_FAILURE;
160 CreateNodeDir(static_cast<std::string>(nodeBuf));
161 vFile.push_back(nodeBuf);
162 }
163 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, MITIGATION_NODE_FILE.c_str(), vFile[ARG_0].c_str(),
164 cpu.c_str());
165 if (ret < EOK) return HDF_FAILURE;
166 CreateNodeFile(static_cast<std::string>(fileBuf));
167 WriteFile(fileBuf, sTemp, sTemp.length());
168 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, MITIGATION_NODE_FILE.c_str(), vFile[ARG_1].c_str(),
169 charger.c_str());
170 if (ret < EOK) return HDF_FAILURE;
171 CreateNodeFile(static_cast<std::string>(fileBuf));
172 WriteFile(fileBuf, sTemp, sTemp.length());
173 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, MITIGATION_NODE_FILE.c_str(), vFile[ARG_2].c_str(),
174 gpu.c_str());
175 if (ret < EOK) {
176 return HDF_FAILURE;
177 }
178 CreateNodeFile(static_cast<std::string>(fileBuf));
179 WriteFile(fileBuf, sTemp, sTemp.length());
180 std::vector<std::string> vBattery(battery, battery + ARG_2);
181 for (auto b : vBattery) {
182 ret = snprintf_s(fileBuf, MAX_PATH, sizeof(fileBuf) - ARG_1, MITIGATION_NODE_FILE.c_str(),
183 vFile[ARG_3].c_str(), b.c_str());
184 if (ret < EOK) {
185 return HDF_FAILURE;
186 }
187 CreateNodeFile(static_cast<std::string>(fileBuf));
188 WriteFile(fileBuf, sTemp, sTemp.length());
189 }
190 return HDF_SUCCESS;
191 }
192
WriteFile(std::string path,std::string buf,size_t size)193 int32_t ThermalSimulationNode::WriteFile(std::string path, std::string buf, size_t size)
194 {
195 int32_t fd = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
196 if (fd < NUM_ZERO) {
197 THERMAL_HILOGE(COMP_HDI, "open failed to file.");
198 }
199 write(fd, buf.c_str(), size);
200 close(fd);
201 return HDF_SUCCESS;
202 }
203 } // V1_0
204 } // Thermal
205 } // HDI
206 } // OHOS
207