-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathwatchdog.h
101 lines (89 loc) · 3.02 KB
/
watchdog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2024 Toyota Connected North America
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SHELL_WATCHDOG_H_
#define SHELL_WATCHDOG_H_
#include <atomic>
#include <chrono>
#include <thread>
/**
* @brief The default timeout interval for the Watchdog.
*/
class Watchdog {
public:
static constexpr uint64_t kDefaultTimeout = 5'000'000;
static constexpr uint64_t kDefaultSleepTime = 250;
/**
* @brief Default constructor for the Watchdog class.
*
* This constructor initializes the Watchdog object with the default timeout
* interval. It also sets the stop flag to false and logs the interval set for
* the Watchdog Timer.
*/
explicit Watchdog();
/**
* @brief Destructor for the Watchdog class
*
* The destructor for the Watchdog class. It stops the watchdog timer, if it
* is running, and performs any necessary cleanup. If the watchdog thread is
* joinable, it will stop the thread and notify the system (if
* BUILD_SYSTEMD_WATCHDOG is defined) that it is stopping.
*
* @see Watchdog::stop(), sd_notify()
*/
~Watchdog();
/**
* @brief Starts the watchdog timer.
*
* This method starts the watchdog timer by creating a separate thread that
* continuously checks if the timeout has occurred. If the timeout has
* occurred, it logs a critical message and takes appropriate action based on
* the build configuration. The watchdog timer will continue running until the
* stop() method is called or the timeout occurs.
*
* @see stop()
*/
void start();
/**
* @brief Stops the watchdog timer.
*
* This method stops the watchdog timer if it is running. It sets the stop
* flag to true, which will cause the watchdog thread to exit. If the watchdog
* thread is joinable, it will wait for the thread to complete before
* returning.
*
* @see Watchdog::start()
*/
void stop();
/**
* @brief Resets the watchdog timer deadline to the current time plus the
* interval.
*
* This method updates the deadline for the watchdog timer by calculating the
* current time plus the interval. It also notifies the system (if
* BUILD_SYSTEMD_WATCHDOG is defined) that the watchdog is active.
*
* @see start(), sd_notify()
*/
void pet();
Watchdog(const Watchdog&) = delete;
const Watchdog& operator=(const Watchdog&) = delete;
private:
std::chrono::microseconds interval_;
std::thread watchdog_thread_;
std::atomic<bool> stop_flag_;
std::chrono::steady_clock::time_point deadline_;
};
#endif // SHELL_WATCHDOG_H_