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