• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_TIMER_H
17 #define THERMAL_TIMER_H
18 
19 #include <string>
20 #include <chrono>
21 #include <atomic>
22 #include <memory>
23 #include <mutex>
24 #include <functional>
25 #include <cstdint>
26 #include <vector>
27 #include <thread>
28 #include <condition_variable>
29 #include <inttypes.h>
30 #include <sys/time.h>
31 #include "time_service_client.h"
32 
33 namespace OHOS {
34 namespace PowerMgr {
35 class ThermalTimerInfo : public MiscServices::ITimerInfo {
36 public:
37     using TimerInfoCallback = std::function<void()>;
38     ThermalTimerInfo();
39     virtual ~ThermalTimerInfo();
40     virtual void OnTrigger() override;
41     virtual void SetType(const int &type) override;
42     virtual void SetRepeat(bool repeat) override;
43     virtual void SetInterval(const uint64_t &interval) override;
44     virtual void SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent) override;
45     void SetCallbackInfo(const TimerInfoCallback &callBack);
46 private:
47     TimerInfoCallback callback_ {nullptr};
48 };
49 
50 
51 class ThermalTimer {
52 public:
53     ThermalTimer() = default;
54     ~ThermalTimer() = default;
55     uint64_t CreateTimer(std::shared_ptr<ThermalTimerInfo>& timerInfo);
56     bool StartTimer(uint64_t timerId, uint64_t triggerTime);
57     bool StopTimer(uint64_t timerId);
58     bool DestroyTimer(uint64_t timerId);
59 };
60 
61 class ThermalTimerUtils {
62 public:
63     using NotifyTask = std::function<void()>;
ThermalTimerUtils()64     ThermalTimerUtils() : expired_(true), tryToExpire_(false)
65     {
66     }
ThermalTimerUtils(const ThermalTimerUtils & timerUtils)67     ThermalTimerUtils(const ThermalTimerUtils& timerUtils)
68     {
69         expired_ = timerUtils.expired_.load();
70         tryToExpire_ = timerUtils.tryToExpire_.load();
71     }
~ThermalTimerUtils()72     ~ThermalTimerUtils()
73     {
74         Stop();
75     }
76     void Start(int interval, NotifyTask &task);
77     void StartOnce(int delay, NotifyTask &task);
78     void Stop();
79 private:
80     std::atomic<bool> expired_; // timer stopped status
81     std::atomic<bool> tryToExpire_; // timer is in stop process
82     std::mutex mutex_;
83     std::condition_variable expiredCond_;
84 };
85 } // namespace PowerMgr
86 } // namespace OHOS
87 #endif // THERMAL_TIMER_H
88