xref: /openbmc/sdeventplus/example/heartbeat.cpp (revision 1072c7f0)
1 #include <chrono>
2 #include <cstdio>
3 #include <sdeventplus/clock.hpp>
4 #include <sdeventplus/event.hpp>
5 #include <sdeventplus/source/time.hpp>
6 #include <string>
7 #include <utility>
8 
9 using sdeventplus::Clock;
10 using sdeventplus::ClockId;
11 using sdeventplus::Event;
12 using sdeventplus::source::Time;
13 
14 int main(int argc, char* argv[])
15 {
16     if (argc != 2)
17     {
18         fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
19         return 1;
20     }
21 
22     unsigned interval = std::stoul(argv[1]);
23     fprintf(stderr, "Beating every %u seconds\n", interval);
24 
25     auto event = Event::get_default();
26     auto hbFunc = [interval](Time<ClockId::RealTime>& source,
27                              Time<ClockId::RealTime>::TimePoint time) {
28         printf("Beat\n");
29         source.set_time(time + std::chrono::seconds{interval});
30     };
31     Time<ClockId::RealTime> time(event, Clock<ClockId::RealTime>(event).now(),
32                                  std::chrono::seconds{1}, std::move(hbFunc));
33     time.set_enabled(SD_EVENT_ON);
34     return event.loop();
35 }
36