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 #ifndef THERMAL_HDF_TIMER_H
17 #define THERMAL_HDF_TIMER_H
18 
19 #include <atomic>
20 #include <map>
21 #include <thread>
22 #include "thermal_hdf_config.h"
23 #include "thermal_simulation_node.h"
24 #include "thermal_zone_manager.h"
25 #include "v1_1/ithermal_callback.h"
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace Thermal {
30 namespace V1_1 {
31 enum EventType {
32     EVENT_UEVENT_FD,
33     EVENT_TIMER_FD,
34 };
35 class ThermalHdfTimer {
36 public:
37     using Callback = std::function<void(ThermalHdfTimer*, void*)>;
38 
39     ThermalHdfTimer(const std::shared_ptr<ThermalSimulationNode> &node,
40         const std::shared_ptr<ThermalZoneManager> &thermalZoneMgr);
41     ~ThermalHdfTimer();
42 
43     int32_t Init();
44     void ReportThermalData();
45     void LoopingThreadEntry();
46     void Run();
47     void StartThread();
48     void ResetCount();
49     void SetSimluationFlag();
50     int32_t GetSimluationFlag();
51     void SetSimFlag(int32_t flag);
52     void DumpSensorConfigInfo();
53 private:
54     void TimerProviderCallback();
55     int32_t CreateProviderFd();
56 
57     std::map<int32_t, Callback> callbackHandler_;
58     std::shared_ptr<ThermalSimulationNode> node_;
59     std::shared_ptr<ThermalZoneManager> thermalZoneMgr_;
60     HdfThermalCallbackInfo tzInfoEventV1_;
61     HdfThermalCallbackInfo tzInfoEventV2_;
62     HdfThermalCallbackInfo tzInfoEvent_;
63     std::vector<int32_t> multipleList_;
64     int32_t reportTime_;
65     int32_t isSim_ = 0;
66     std::atomic_bool isRunning_ {true};
67     std::unique_ptr<std::thread> callbackThread_ {nullptr};
68 };
69 } // V1_1
70 } // Thermal
71 } // HDI
72 } // OHOS
73 #endif // THERMAL_HDF_TIMER_H