Implement regular event launching using systemd event loop
[apps/low-level-can-service.git] / src / utils / timer.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <time.h>
19 #include <stdlib.h> 
20
21 #include "timer.hpp"
22
23 #define MS_PER_SECOND 1000
24
25 long long int system_time_us()
26 {
27         struct timespec t_usec;
28         long long int timestamp_usec;
29         
30         if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec))
31                 timestamp_usec = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll);
32         return timestamp_usec;
33 }
34
35 long long int system_time_ms()
36 {
37         struct timespec t_msec;
38         long long int timestamp_msec;
39         
40         if(!::clock_gettime(CLOCK_MONOTONIC, &t_msec))
41                 timestamp_msec = (t_msec.tv_nsec / 1000000ll) + (t_msec.tv_sec* 1000ll);
42         return timestamp_msec;
43 }
44
45 long long int system_time_s()
46 {
47         struct timespec t_sec;
48         long long int timestamp_sec;
49         
50         if(!::clock_gettime(CLOCK_MONOTONIC, &t_sec))
51                 timestamp_sec = t_sec.tv_sec;
52         return timestamp_sec;
53 }
54
55 frequency_clock_t::frequency_clock_t()
56         : frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
57 {}
58
59
60 frequency_clock_t::frequency_clock_t(float frequency)
61         : frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
62 {}
63
64 /// @brief Return the period in ms given the frequency in hertz.
65 float frequency_clock_t::frequency_to_period(float frequency)
66 {
67         return 1 / frequency * MS_PER_SECOND;
68 }
69
70 bool frequency_clock_t::started()
71 {
72         return last_tick_ != 0;
73 }
74
75 time_function_t frequency_clock_t::get_time_function()
76 {
77         return time_function_ != nullptr ? time_function_ : system_time_ms;
78 }
79
80 bool frequency_clock_t::elapsed(bool stagger)
81 {
82         float period = frequency_to_period(frequency_);
83         float elapsed_time = 0;
84         if(!started() && stagger)
85                 last_tick_ = get_time_function()() - (rand() % int(period));
86         else
87                 // Make sure it ticks the the first call
88                 elapsed_time = !started() ? period : get_time_function()() - last_tick_;
89
90         return frequency_ == 0 || elapsed_time >= period;
91 }
92
93 float frequency_clock_t::get_frequency() const
94 {
95         return frequency_;
96 }
97
98 /// @brief Force the clock to tick, regardless of it its time has actually
99 /// elapsed.
100 void frequency_clock_t::tick()
101 {
102         last_tick_ = get_time_function()();
103 }