1 /*
2 * Copyright (c) 2022-2023 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 <cmath>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <mutex>
20 #include <fcntl.h>
21 #include <functional>
22 #include <securec.h>
23 #include <unistd.h>
24
25 #include "hdf_base.h"
26 #include "osal_time.h"
27 #include "v1_1/ifan_callback.h"
28 #include "v1_1/ithermal_interface.h"
29 #include "v1_1/ithermal_callback.h"
30 #include "v1_1/thermal_types.h"
31
32 using namespace OHOS::HDI;
33 using namespace OHOS::HDI::Thermal::V1_1;
34 using namespace testing::ext;
35
36 namespace {
37 class ThermalCallbackMock : public IThermalCallback {
38 public:
~ThermalCallbackMock()39 virtual ~ThermalCallbackMock() {}
40 using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterThermalEvent(const ThermalEventCallback &eventCb)41 static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
42 {
43 (void)eventCb;
44 return 0;
45 }
46 int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
47 {
48 (void)event;
49 return 0;
50 }
51 };
52
53 class FanCallbackMock : public IFanCallback {
54 public:
~FanCallbackMock()55 virtual ~FanCallbackMock() {}
56 using FanEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterFanEvent(const FanEventCallback &eventCb)57 static int32_t RegisterFanEvent(const FanEventCallback &eventCb)
58 {
59 (void)eventCb;
60 return 0;
61 }
62 int32_t OnFanDataEvent(const HdfThermalCallbackInfo &event) override
63 {
64 (void)event;
65 return 0;
66 }
67 };
68
69 sptr<IThermalInterface> g_thermalInterface = nullptr;
70 sptr<IThermalCallback> g_callback = new ThermalCallbackMock();
71 sptr<IFanCallback> g_fanCallback = new FanCallbackMock();
72 std::mutex g_mutex;
73 const uint32_t MAX_PATH = 256;
74 const uint32_t WAIT_TIME = 1;
75 const std::string CPU_FREQ_PATH = "/data/service/el0/thermal/cooling/cpu/freq";
76 const std::string GPU_FREQ_PATH = "/data/service/el0/thermal/cooling/gpu/freq";
77 const std::string BATTERY_CHARGER_CURRENT_PATH = "/data/service/el0/thermal/cooling/battery/current";
78 const std::string ISOLATE_PATH = "/data/service/el0/thermal/sensor/soc/isolate";
79
80 class HdfThermalHdiTest : public testing::Test {
81 public:
82 static void SetUpTestCase();
83 static void TearDownTestCase();
84 void SetUp();
85 void TearDown();
86 static int32_t ReadFile(const char *path, char *buf, size_t size);
87 static int32_t ConvertInt(const std::string &value);
88 };
89
SetUpTestCase()90 void HdfThermalHdiTest::SetUpTestCase()
91 {
92 g_thermalInterface = IThermalInterface::Get(true);
93 }
94
TearDownTestCase()95 void HdfThermalHdiTest::TearDownTestCase()
96 {
97 }
98
SetUp()99 void HdfThermalHdiTest::SetUp()
100 {
101 }
102
TearDown()103 void HdfThermalHdiTest::TearDown()
104 {
105 }
106
ReadFile(const char *path, char *buf, size_t size)107 int32_t HdfThermalHdiTest::ReadFile(const char *path, char *buf, size_t size)
108 {
109 std::lock_guard<std::mutex> lck(g_mutex);
110 int32_t ret;
111
112 int32_t fd = open(path, O_RDONLY);
113 if (fd < HDF_SUCCESS) {
114 printf("WriteFile: failed to open file %d\n\r", fd);
115 return HDF_FAILURE;
116 }
117
118 ret = read(fd, buf, size);
119 if (ret < HDF_SUCCESS) {
120 printf("WriteFile: failed to read file %d\n\r", ret);
121 close(fd);
122 return HDF_FAILURE;
123 }
124
125 close(fd);
126 buf[size - 1] = '\0';
127 return HDF_SUCCESS;
128 }
129
ConvertInt(const std::string &value)130 int32_t HdfThermalHdiTest::ConvertInt(const std::string &value)
131 {
132 return std::stoi(value);
133 }
134 }
135
136 namespace {
137 /**
138 * @tc.name: HdfThermalHdiTest001
139 * @tc.desc: Get a client and check whether the client is empty.
140 * @tc.type: FUNC
141 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest001, TestSize.Level1)142 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest001, TestSize.Level1)
143 {
144 ASSERT_NE(nullptr, g_thermalInterface);
145 }
146
147 /**
148 * @tc.name: HdfThermalHdiTest002
149 * @tc.desc: SetCpuFreq
150 * @tc.type: FUNC
151 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level1)152 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level1)
153 {
154 printf("HdfThermalHdiTest002: start.");
155 int32_t cpuFreq = 1994100;
156 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
157 EXPECT_EQ(0, ret);
158
159 char cpuBuf[MAX_PATH] = {0};
160 char freqValue[MAX_PATH] = {0};
161
162 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, CPU_FREQ_PATH.c_str()) < EOK) {
163 return;
164 }
165
166 sleep(WAIT_TIME);
167 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
168 if (ret != HDF_SUCCESS) {
169 printf("HdfThermalHdiTest002: Failed to read file ");
170 return;
171 }
172
173 std::string freq = freqValue;
174 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
175 printf("freq is %d\n\r", value);
176 EXPECT_EQ(value, cpuFreq) << "HdfThermalHdiTest002 failed";
177 printf("HdfThermalHdiTest002: return.");
178 }
179
180 /**
181 * @tc.name: HdfThermalHdiTest003
182 * @tc.desc: SetGpuFreq
183 * @tc.type: FUNC
184 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level1)185 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level1)
186 {
187 printf("HdfThermalHdiTest003: start.");
188 int32_t gpuFreq = 40000;
189 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
190 EXPECT_EQ(0, ret);
191
192 char cpuBuf[MAX_PATH] = {0};
193 char freqValue[MAX_PATH] = {0};
194
195 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, GPU_FREQ_PATH.c_str()) < EOK) {
196 return;
197 }
198
199 sleep(WAIT_TIME);
200 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
201 if (ret != HDF_SUCCESS) {
202 printf("HdfThermalHdiTest003: Failed to read file ");
203 return;
204 }
205
206 std::string freq = freqValue;
207 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
208 printf("freq is %d\n\r", value);
209 EXPECT_EQ(value, gpuFreq) << "HdfThermalHdiTest003 failed";
210 printf("HdfThermalHdiTest003: return.");
211 }
212
213 /**
214 * @tc.name: HdfThermalHdiTest004
215 * @tc.desc: SetBatteryCurrent
216 * @tc.type: FUNC
217 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level1)218 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level1)
219 {
220 printf("HdfThermalHdiTest004: start.");
221 int32_t batteryCurrent = 1000;
222 int32_t ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
223 EXPECT_EQ(0, ret);
224
225 char cpuBuf[MAX_PATH] = {0};
226 char currentValue[MAX_PATH] = {0};
227
228 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, BATTERY_CHARGER_CURRENT_PATH.c_str()) < EOK) {
229 return;
230 }
231
232 sleep(WAIT_TIME);
233 ret = HdfThermalHdiTest::ReadFile(cpuBuf, currentValue, sizeof(currentValue));
234 if (ret != HDF_SUCCESS) {
235 printf("HdfThermalHdiTest004: Failed to read file ");
236 return;
237 }
238
239 std::string current = currentValue;
240 int32_t value = HdfThermalHdiTest::ConvertInt(current);
241 printf("freq is %d\n\r", value);
242 EXPECT_EQ(value, batteryCurrent) << "HdfThermalHdiTest004 failed";
243 printf("HdfThermalHdiTest004: return.");
244 }
245
246 /**
247 * @tc.name: HdfThermalHdiTest005
248 * @tc.desc: GetThermalZoneInfo
249 * @tc.type: FUNC
250 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level1)251 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level1)
252 {
253 HdfThermalCallbackInfo event;
254 printf("HdfThermalHdiTest005: start.");
255 int32_t ret = g_thermalInterface->GetThermalZoneInfo(event);
256 EXPECT_EQ(0, ret) << "HdfThermalHdiTest005 failed";
257 for (auto iter : event.info) {
258 printf("type is %s\n\r", iter.type.c_str());
259 printf("temp is %d\n\r", iter.temp);
260 }
261 printf("HdfThermalHdiTest005: return.");
262 }
263
264 /**
265 * @tc.name: HdfThermalHdiTest006
266 * @tc.desc: Register
267 * @tc.type: FUNC
268 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level1)269 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level1)
270 {
271 printf("HdfThermalHdiTest006: start.");
272 int32_t ret = g_thermalInterface->Register(g_callback);
273 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
274 printf("HdfThermalHdiTest006: return.");
275 }
276
277 /**
278 * @tc.name: HdfThermalHdiTest007
279 * @tc.desc: Unregister
280 * @tc.type: FUNC
281 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest007, TestSize.Level1)282 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest007, TestSize.Level1)
283 {
284 printf("HdfThermalHdiTest007: start.");
285 int32_t ret = g_thermalInterface->Unregister();
286 EXPECT_EQ(0, ret) << "HdfThermalHdiTest007 failed";
287 printf("HdfThermalHdiTest007: return.");
288 }
289
290 /**
291 * @tc.name: HdfThermalHdiTest008
292 * @tc.desc: RegisterFanCallback
293 * @tc.type: FUNC
294 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest008, TestSize.Level1)295 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest008, TestSize.Level1)
296 {
297 printf("HdfThermalHdiTest008: start.");
298 int32_t ret = g_thermalInterface->RegisterFanCallback(g_fanCallback);
299 EXPECT_EQ(0, ret) << "HdfThermalHdiTest008 failed";
300 printf("HdfThermalHdiTest008: return.");
301 }
302
303 /**
304 * @tc.name: HdfThermalHdiTest009
305 * @tc.desc: UnregisterFanCallback
306 * @tc.type: FUNC
307 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest009, TestSize.Level1)308 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest009, TestSize.Level1)
309 {
310 printf("HdfThermalHdiTest009: start.");
311 int32_t ret = g_thermalInterface->UnregisterFanCallback();
312 EXPECT_EQ(0, ret) << "HdfThermalHdiTest009 failed";
313 printf("HdfThermalHdiTest009: return.");
314 }
315
316 /**
317 * @tc.name: HdfThermalHdiTest010
318 * @tc.desc: IsolateCpu
319 * @tc.type: FUNC
320 */
HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest010, TestSize.Level1)321 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest010, TestSize.Level1)
322 {
323 printf("HdfThermalHdiTest010: start.");
324 int32_t isolateNum = 2;
325 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
326 if (ret != 0) {
327 return;
328 }
329
330 char path[MAX_PATH] = {0};
331 char valueBuf[MAX_PATH] = {0};
332
333 if (snprintf_s(path, MAX_PATH, sizeof(path) - 1, ISOLATE_PATH.c_str()) < EOK) {
334 return;
335 }
336
337 sleep(WAIT_TIME);
338 ret = HdfThermalHdiTest::ReadFile(path, valueBuf, sizeof(valueBuf));
339 if (ret != HDF_SUCCESS) {
340 printf("HdfThermalHdiTest010: Failed to read file ");
341 return;
342 }
343
344 std::string isolateNumStr = valueBuf;
345 int32_t value = HdfThermalHdiTest::ConvertInt(isolateNumStr);
346 EXPECT_EQ(value, isolateNum) << "HdfThermalHdiTest010 failed";
347 printf("HdfThermalHdiTest010: return.");
348 }
349 }
350