784bb46a3a2042869610ff523993956366af4890
[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 long long int system_time_us()
24 {
25         struct timespec t_usec;
26         long long int timestamp_usec;
27         
28         if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec))
29                 timestamp_usec = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll);
30         return timestamp_usec;
31 }
32
33 long long int system_time_ms()
34 {
35         struct timespec t_msec;
36         long long int timestamp_msec;
37         
38         if(!::clock_gettime(CLOCK_MONOTONIC, &t_msec))
39                 timestamp_msec = (t_msec.tv_nsec / 1000000ll) + (t_msec.tv_sec* 1000ll);
40         return timestamp_msec;
41 }
42
43 long long int system_time_s()
44 {
45         struct timespec t_sec;
46         long long int timestamp_sec;
47         
48         if(!::clock_gettime(CLOCK_MONOTONIC, &t_sec))
49                 timestamp_sec = t_sec.tv_sec;
50         return timestamp_sec;
51 }
52
53 frequency_clock_t::frequency_clock_t()
54         : frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
55 {}
56
57
58 frequency_clock_t::frequency_clock_t(float frequency)
59         : frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
60 {}
61
62 /// @brief Return the period in ms given the frequency in hertz.
63 float frequency_clock_t::frequency_to_period(float frequency)
64 {
65         return 1 / frequency;
66 }
67
68 bool frequency_clock_t::started()
69 {
70         return last_tick_ != 0;
71 }
72
73 time_function_t frequency_clock_t::get_time_function()
74 {
75         return time_function_ != nullptr ? time_function_ : system_time_ms;
76 }
77
78 bool frequency_clock_t::elapsed(bool stagger)
79 {
80         float period = frequency_to_period(frequency_);
81         float elapsed_time = 0;
82         if(!started() && stagger)
83                 last_tick_ = get_time_function()() - (rand() % int(period));
84         else
85                 // Make sure it ticks the the first call
86                 elapsed_time = !started() ? period : get_time_function()() - last_tick_;
87
88         return frequency_ == 0 || elapsed_time >= period;
89 }
90
91 float frequency_clock_t::get_frequency() const
92 {
93         return frequency_;
94 }
95
96 /// @brief Force the clock to tick, regardless of it its time has actually
97 /// elapsed.
98 void frequency_clock_t::tick()
99 {
100         last_tick_ = get_time_function()();
101 }