1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 typedef struct Timer Timer;
5 
6 #include "calendarspec.h"
7 #include "unit.h"
8 
9 typedef enum TimerBase {
10         TIMER_ACTIVE,
11         TIMER_BOOT,
12         TIMER_STARTUP,
13         TIMER_UNIT_ACTIVE,
14         TIMER_UNIT_INACTIVE,
15         TIMER_CALENDAR,
16         _TIMER_BASE_MAX,
17         _TIMER_BASE_INVALID = -EINVAL,
18 } TimerBase;
19 
20 typedef struct TimerValue {
21         TimerBase base;
22         bool disabled;
23 
24         usec_t value; /* only for monotonic events */
25         CalendarSpec *calendar_spec; /* only for calendar events */
26         usec_t next_elapse;
27 
28         LIST_FIELDS(struct TimerValue, value);
29 } TimerValue;
30 
31 typedef enum TimerResult {
32         TIMER_SUCCESS,
33         TIMER_FAILURE_RESOURCES,
34         TIMER_FAILURE_START_LIMIT_HIT,
35         _TIMER_RESULT_MAX,
36         _TIMER_RESULT_INVALID = -EINVAL,
37 } TimerResult;
38 
39 struct Timer {
40         Unit meta;
41 
42         usec_t accuracy_usec;
43         usec_t random_usec;
44 
45         LIST_HEAD(TimerValue, values);
46         usec_t next_elapse_realtime;
47         usec_t next_elapse_monotonic_or_boottime;
48         dual_timestamp last_trigger;
49 
50         TimerState state, deserialized_state;
51 
52         sd_event_source *monotonic_event_source;
53         sd_event_source *realtime_event_source;
54 
55         TimerResult result;
56 
57         bool persistent;
58         bool wake_system;
59         bool remain_after_elapse;
60         bool on_clock_change;
61         bool on_timezone_change;
62         bool fixed_random_delay;
63 
64         char *stamp_path;
65 };
66 
67 #define TIMER_MONOTONIC_CLOCK(t) ((t)->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC)
68 
69 void timer_free_values(Timer *t);
70 
71 extern const UnitVTable timer_vtable;
72 
73 const char *timer_base_to_string(TimerBase i) _const_;
74 TimerBase timer_base_from_string(const char *s) _pure_;
75 
76 const char* timer_result_to_string(TimerResult i) _const_;
77 TimerResult timer_result_from_string(const char *s) _pure_;
78 
79 DEFINE_CAST(TIMER, Timer);
80