1/*
2 * Copyright (C) 2021-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 TIMER_H
17#define TIMER_H
18
19#include "base_def.h"
20#include <mutex>
21#include <functional>
22
23namespace utility {
24#define MS_PER_SECOND 1000
25#define NS_PER_MS 1000000
26
27class Timer {
28public:
29    /**
30     * @brief Construct a new Timer object
31     *
32     * @param callback Timer's callback function.
33     * @since 6
34     */
35    Timer(const std::function<void()> &callback);
36
37    /**
38     * @brief Destroy the Timer object
39     *
40     * @since 6
41     */
42    ~Timer();
43
44    /**
45     * @brief Start Running Timer.
46     *
47     * @param ms Countdown time.
48     * @param isPeriodic Timer isPeriodic.
49     * @return Success set timer return true, else return false.
50     * @since 6
51     */
52    bool Start(int ms, bool isPeriodic = false);
53
54    /**
55     * @brief Stop Running Timer.
56     *
57     * @return Success stop timer return true, else return false.
58     * @since 6
59     */
60    bool Stop();
61
62private:
63    int fd_ {-1};
64    std::mutex mutex_ {};
65    std::function<void()> callback_ {};
66
67    friend class TimerManager;
68    BT_DISALLOW_COPY_AND_ASSIGN(Timer);
69};
70}  // namespace utility
71
72#endif  // TIMER_H
73