1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <cmath>
17094332d3Sopenharmony_ci#include <cstdio>
18094332d3Sopenharmony_ci#include <gtest/gtest.h>
19094332d3Sopenharmony_ci#include <mutex>
20094332d3Sopenharmony_ci#include <fcntl.h>
21094332d3Sopenharmony_ci#include <functional>
22094332d3Sopenharmony_ci#include <securec.h>
23094332d3Sopenharmony_ci#include <unistd.h>
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ci#include "hdf_base.h"
26094332d3Sopenharmony_ci#include "osal_time.h"
27094332d3Sopenharmony_ci#include "v1_1/ifan_callback.h"
28094332d3Sopenharmony_ci#include "v1_1/ithermal_interface.h"
29094332d3Sopenharmony_ci#include "v1_1/ithermal_callback.h"
30094332d3Sopenharmony_ci#include "v1_1/thermal_types.h"
31094332d3Sopenharmony_ci#include "thermal_log.h"
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ciusing namespace OHOS::HDI;
34094332d3Sopenharmony_ciusing namespace OHOS::HDI::Thermal::V1_1;
35094332d3Sopenharmony_ciusing namespace testing::ext;
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_cinamespace {
38094332d3Sopenharmony_ciclass ThermalCallbackMock : public IThermalCallback {
39094332d3Sopenharmony_cipublic:
40094332d3Sopenharmony_ci    virtual ~ThermalCallbackMock() {}
41094332d3Sopenharmony_ci    using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
42094332d3Sopenharmony_ci    static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
43094332d3Sopenharmony_ci    {
44094332d3Sopenharmony_ci        (void)eventCb;
45094332d3Sopenharmony_ci        return 0;
46094332d3Sopenharmony_ci    }
47094332d3Sopenharmony_ci    int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
48094332d3Sopenharmony_ci    {
49094332d3Sopenharmony_ci        (void)event;
50094332d3Sopenharmony_ci        return 0;
51094332d3Sopenharmony_ci    }
52094332d3Sopenharmony_ci};
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ciclass FanCallbackMock : public IFanCallback {
55094332d3Sopenharmony_cipublic:
56094332d3Sopenharmony_ci    virtual ~FanCallbackMock() {}
57094332d3Sopenharmony_ci    using FanEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
58094332d3Sopenharmony_ci    static int32_t RegisterFanEvent(const FanEventCallback &eventCb)
59094332d3Sopenharmony_ci    {
60094332d3Sopenharmony_ci        (void)eventCb;
61094332d3Sopenharmony_ci        return 0;
62094332d3Sopenharmony_ci    }
63094332d3Sopenharmony_ci    int32_t OnFanDataEvent(const HdfThermalCallbackInfo &event) override
64094332d3Sopenharmony_ci    {
65094332d3Sopenharmony_ci        (void)event;
66094332d3Sopenharmony_ci        return 0;
67094332d3Sopenharmony_ci    }
68094332d3Sopenharmony_ci};
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_cisptr<IThermalInterface> g_thermalInterface = nullptr;
71094332d3Sopenharmony_cisptr<IThermalCallback> g_callback = new ThermalCallbackMock();
72094332d3Sopenharmony_cisptr<IFanCallback> g_fanCallback = new FanCallbackMock();
73094332d3Sopenharmony_cistd::mutex g_mutex;
74094332d3Sopenharmony_ciconst uint32_t MAX_PATH = 256;
75094332d3Sopenharmony_ciconst std::string CPU_FREQ_PATH = "/data/service/el0/thermal/cooling/cpu/freq";
76094332d3Sopenharmony_ciconst std::string GPU_FREQ_PATH = "/data/service/el0/thermal/cooling/gpu/freq";
77094332d3Sopenharmony_ciconst std::string BATTERY_CHARGER_CURRENT_PATH = "/data/service/el0/thermal/cooling/battery/current";
78094332d3Sopenharmony_ciconst std::string ISOLATE_PATH = "/data/service/el0/thermal/sensor/soc/isolate";
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ciclass HdfThermalHdiTest : public testing::Test {
81094332d3Sopenharmony_cipublic:
82094332d3Sopenharmony_ci    static void SetUpTestCase();
83094332d3Sopenharmony_ci    static void TearDownTestCase();
84094332d3Sopenharmony_ci    void SetUp();
85094332d3Sopenharmony_ci    void TearDown();
86094332d3Sopenharmony_ci    static int32_t ReadFile(const char *path, char *buf, size_t size);
87094332d3Sopenharmony_ci    static int32_t ConvertInt(const std::string &value);
88094332d3Sopenharmony_ci};
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_civoid HdfThermalHdiTest::SetUpTestCase()
91094332d3Sopenharmony_ci{
92094332d3Sopenharmony_ci    g_thermalInterface = IThermalInterface::Get();
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_civoid HdfThermalHdiTest::TearDownTestCase()
96094332d3Sopenharmony_ci{
97094332d3Sopenharmony_ci}
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_civoid HdfThermalHdiTest::SetUp()
100094332d3Sopenharmony_ci{
101094332d3Sopenharmony_ci}
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_civoid HdfThermalHdiTest::TearDown()
104094332d3Sopenharmony_ci{
105094332d3Sopenharmony_ci}
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ciint32_t HdfThermalHdiTest::ReadFile(const char *path, char *buf, size_t size)
108094332d3Sopenharmony_ci{
109094332d3Sopenharmony_ci    std::lock_guard<std::mutex> lck(g_mutex);
110094332d3Sopenharmony_ci    int32_t ret;
111094332d3Sopenharmony_ci
112094332d3Sopenharmony_ci    int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
113094332d3Sopenharmony_ci    if (fd < HDF_SUCCESS) {
114094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to open file %{public}d", fd);
115094332d3Sopenharmony_ci        return HDF_FAILURE;
116094332d3Sopenharmony_ci    }
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ci    ret = read(fd, buf, size);
119094332d3Sopenharmony_ci    if (ret < HDF_SUCCESS) {
120094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to read file %{public}d", ret);
121094332d3Sopenharmony_ci        close(fd);
122094332d3Sopenharmony_ci        return HDF_FAILURE;
123094332d3Sopenharmony_ci    }
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    close(fd);
126094332d3Sopenharmony_ci    buf[size - 1] = '\0';
127094332d3Sopenharmony_ci    return HDF_SUCCESS;
128094332d3Sopenharmony_ci}
129094332d3Sopenharmony_ci
130094332d3Sopenharmony_ciint32_t HdfThermalHdiTest::ConvertInt(const std::string &value)
131094332d3Sopenharmony_ci{
132094332d3Sopenharmony_ci    return std::stoi(value);
133094332d3Sopenharmony_ci}
134094332d3Sopenharmony_ci}
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_cinamespace {
137094332d3Sopenharmony_ci/**
138094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest001
139094332d3Sopenharmony_ci  * @tc.desc: Get a client and check whether the client is empty.
140094332d3Sopenharmony_ci  * @tc.type: FUNC
141094332d3Sopenharmony_ci  */
142094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest001, TestSize.Level1)
143094332d3Sopenharmony_ci{
144094332d3Sopenharmony_ci    ASSERT_NE(nullptr, g_thermalInterface);
145094332d3Sopenharmony_ci}
146094332d3Sopenharmony_ci
147094332d3Sopenharmony_ci/**
148094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest002
149094332d3Sopenharmony_ci  * @tc.desc: set cpu freq
150094332d3Sopenharmony_ci  * @tc.type: FUNC
151094332d3Sopenharmony_ci  */
152094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level1)
153094332d3Sopenharmony_ci{
154094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest002: start.");
155094332d3Sopenharmony_ci    int32_t cpuFreq = 1994100;
156094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
157094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
158094332d3Sopenharmony_ci
159094332d3Sopenharmony_ci    char cpuBuf[MAX_PATH] = {0};
160094332d3Sopenharmony_ci    char freqValue[MAX_PATH] = {0};
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ci    if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, CPU_FREQ_PATH.c_str()) < EOK) {
163094332d3Sopenharmony_ci        return;
164094332d3Sopenharmony_ci    }
165094332d3Sopenharmony_ci
166094332d3Sopenharmony_ci    ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
167094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
168094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest002: Failed to read file ");
169094332d3Sopenharmony_ci        return;
170094332d3Sopenharmony_ci    }
171094332d3Sopenharmony_ci
172094332d3Sopenharmony_ci    std::string freq = freqValue;
173094332d3Sopenharmony_ci    int32_t value = HdfThermalHdiTest::ConvertInt(freq);
174094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
175094332d3Sopenharmony_ci    EXPECT_EQ(value, cpuFreq) << "HdfThermalHdiTest002 failed";
176094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest002: return.");
177094332d3Sopenharmony_ci}
178094332d3Sopenharmony_ci
179094332d3Sopenharmony_ci/**
180094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest003
181094332d3Sopenharmony_ci  * @tc.desc: set gpu freq
182094332d3Sopenharmony_ci  * @tc.type: FUNC
183094332d3Sopenharmony_ci  */
184094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level1)
185094332d3Sopenharmony_ci{
186094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: start.");
187094332d3Sopenharmony_ci    int32_t gpuFreq = 40000;
188094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
189094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
190094332d3Sopenharmony_ci
191094332d3Sopenharmony_ci    char cpuBuf[MAX_PATH] = {0};
192094332d3Sopenharmony_ci    char freqValue[MAX_PATH] = {0};
193094332d3Sopenharmony_ci
194094332d3Sopenharmony_ci    if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, GPU_FREQ_PATH.c_str()) < EOK) {
195094332d3Sopenharmony_ci        return;
196094332d3Sopenharmony_ci    }
197094332d3Sopenharmony_ci
198094332d3Sopenharmony_ci    ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
199094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
200094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest003: Failed to read file ");
201094332d3Sopenharmony_ci        return;
202094332d3Sopenharmony_ci    }
203094332d3Sopenharmony_ci
204094332d3Sopenharmony_ci    std::string freq = freqValue;
205094332d3Sopenharmony_ci    int32_t value = HdfThermalHdiTest::ConvertInt(freq);
206094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
207094332d3Sopenharmony_ci    EXPECT_EQ(value, gpuFreq) << "HdfThermalHdiTest003 failed";
208094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: return.");
209094332d3Sopenharmony_ci}
210094332d3Sopenharmony_ci
211094332d3Sopenharmony_ci/**
212094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest004
213094332d3Sopenharmony_ci  * @tc.desc: set battery current
214094332d3Sopenharmony_ci  * @tc.type: FUNC
215094332d3Sopenharmony_ci  */
216094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level1)
217094332d3Sopenharmony_ci{
218094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: start.");
219094332d3Sopenharmony_ci    int32_t batteryCurrent = 1000;
220094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
221094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
222094332d3Sopenharmony_ci
223094332d3Sopenharmony_ci    char cpuBuf[MAX_PATH] = {0};
224094332d3Sopenharmony_ci    char currentValue[MAX_PATH] = {0};
225094332d3Sopenharmony_ci
226094332d3Sopenharmony_ci    if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, BATTERY_CHARGER_CURRENT_PATH.c_str()) < EOK) {
227094332d3Sopenharmony_ci        return;
228094332d3Sopenharmony_ci    }
229094332d3Sopenharmony_ci
230094332d3Sopenharmony_ci    ret = HdfThermalHdiTest::ReadFile(cpuBuf, currentValue, sizeof(currentValue));
231094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
232094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest004: Failed to read file ");
233094332d3Sopenharmony_ci        return;
234094332d3Sopenharmony_ci    }
235094332d3Sopenharmony_ci
236094332d3Sopenharmony_ci    std::string current = currentValue;
237094332d3Sopenharmony_ci    int32_t value = HdfThermalHdiTest::ConvertInt(current);
238094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
239094332d3Sopenharmony_ci    EXPECT_EQ(value, batteryCurrent) << "HdfThermalHdiTest004 failed";
240094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: return.");
241094332d3Sopenharmony_ci}
242094332d3Sopenharmony_ci
243094332d3Sopenharmony_ci/**
244094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest005
245094332d3Sopenharmony_ci  * @tc.desc: get thermal zone info
246094332d3Sopenharmony_ci  * @tc.type: FUNC
247094332d3Sopenharmony_ci  */
248094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level1)
249094332d3Sopenharmony_ci{
250094332d3Sopenharmony_ci    HdfThermalCallbackInfo event;
251094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: start.");
252094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->GetThermalZoneInfo(event);
253094332d3Sopenharmony_ci    EXPECT_EQ(0, ret) << "HdfThermalHdiTest005 failed";
254094332d3Sopenharmony_ci    for (auto iter : event.info) {
255094332d3Sopenharmony_ci        THERMAL_HILOGD(LABEL_TEST, "type is %{public}s", iter.type.c_str());
256094332d3Sopenharmony_ci        THERMAL_HILOGD(LABEL_TEST, "temp is %{public}d", iter.temp);
257094332d3Sopenharmony_ci    }
258094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: return.");
259094332d3Sopenharmony_ci}
260094332d3Sopenharmony_ci
261094332d3Sopenharmony_ci/**
262094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest006
263094332d3Sopenharmony_ci  * @tc.desc: register callback
264094332d3Sopenharmony_ci  * @tc.type: FUNC
265094332d3Sopenharmony_ci  */
266094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level1)
267094332d3Sopenharmony_ci{
268094332d3Sopenharmony_ci    HdfThermalCallbackInfo event;
269094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: start.");
270094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->Register(g_callback);
271094332d3Sopenharmony_ci    EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
272094332d3Sopenharmony_ci    ret = g_thermalInterface->Unregister();
273094332d3Sopenharmony_ci    EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
274094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: return.");
275094332d3Sopenharmony_ci}
276094332d3Sopenharmony_ci
277094332d3Sopenharmony_ci/**
278094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest007
279094332d3Sopenharmony_ci  * @tc.desc: isolate cpu num
280094332d3Sopenharmony_ci  * @tc.type: FUNC
281094332d3Sopenharmony_ci  */
282094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest007, TestSize.Level1)
283094332d3Sopenharmony_ci{
284094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest007: start.");
285094332d3Sopenharmony_ci    int32_t isolateNum = 2;
286094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
287094332d3Sopenharmony_ci
288094332d3Sopenharmony_ci    char path[MAX_PATH] = {0};
289094332d3Sopenharmony_ci    char valueBuf[MAX_PATH] = {0};
290094332d3Sopenharmony_ci
291094332d3Sopenharmony_ci    if (snprintf_s(path, MAX_PATH, sizeof(path) - 1, ISOLATE_PATH.c_str()) < EOK) {
292094332d3Sopenharmony_ci        return;
293094332d3Sopenharmony_ci    }
294094332d3Sopenharmony_ci
295094332d3Sopenharmony_ci    ret = HdfThermalHdiTest::ReadFile(path, valueBuf, sizeof(valueBuf));
296094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
297094332d3Sopenharmony_ci        THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest007: Failed to read file ");
298094332d3Sopenharmony_ci        return;
299094332d3Sopenharmony_ci    }
300094332d3Sopenharmony_ci
301094332d3Sopenharmony_ci    std::string isolateNumStr = valueBuf;
302094332d3Sopenharmony_ci    int32_t value = HdfThermalHdiTest::ConvertInt(isolateNumStr);
303094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "isolate cpu num is %{public}d", value);
304094332d3Sopenharmony_ci    EXPECT_EQ(value, isolateNum) << "HdfThermalHdiTest007 failed";
305094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest007: return.");
306094332d3Sopenharmony_ci}
307094332d3Sopenharmony_ci
308094332d3Sopenharmony_ci/**
309094332d3Sopenharmony_ci  * @tc.name: HdfThermalHdiTest008
310094332d3Sopenharmony_ci  * @tc.desc: register fan callback
311094332d3Sopenharmony_ci  * @tc.type: FUNC
312094332d3Sopenharmony_ci  */
313094332d3Sopenharmony_ciHWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest008, TestSize.Level1)
314094332d3Sopenharmony_ci{
315094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest008: start.");
316094332d3Sopenharmony_ci    int32_t ret = g_thermalInterface->RegisterFanCallback(g_fanCallback);
317094332d3Sopenharmony_ci    EXPECT_EQ(0, ret) << "HdfThermalHdiTest008 failed";
318094332d3Sopenharmony_ci    ret = g_thermalInterface->UnregisterFanCallback();
319094332d3Sopenharmony_ci    EXPECT_EQ(0, ret) << "HdfThermalHdiTest008 failed";
320094332d3Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest008: return.");
321094332d3Sopenharmony_ci}
322094332d3Sopenharmony_ci}
323