Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / 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 #include <cmath>
21
22 #include "timer.hpp"
23
24 long long int system_time_us()
25 {
26         struct timespec t_usec;
27         long long int timestamp_usec = 0;
28
29         if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec))
30                 timestamp_usec = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll);
31         return timestamp_usec;
32 }
33
34 long long int system_time_ms()
35 {
36         struct timespec t_msec;
37         long long int timestamp_msec = 0;
38
39         if(!::clock_gettime(CLOCK_MONOTONIC, &t_msec))
40                 timestamp_msec = (t_msec.tv_nsec / 1000000ll) + (t_msec.tv_sec* 1000ll);
41         return timestamp_msec;
42 }
43
44 long long int system_time_s()
45 {
46         struct timespec t_sec;
47         long long int timestamp_sec = 0;
48
49         if(!::clock_gettime(CLOCK_MONOTONIC, &t_sec))
50                 timestamp_sec = t_sec.tv_sec;
51         return timestamp_sec;
52 }
53
54 frequency_clock_t::frequency_clock_t()
55         : unit_{1000000}, frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
56 {}
57
58 frequency_clock_t::frequency_clock_t(float frequency)
59         : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
60 {
61         if(frequency_ <= 0)
62                 frequency_ = 2000;
63 }
64
65 frequency_clock_t::frequency_clock_t(float frequency, uint64_t last_tick, time_function_t time_function)
66         : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
67 {
68         if(frequency_ <= 0)
69                 frequency_ = 1;
70 }
71
72 /// @brief Return the period in ms given the frequency in hertz.
73 /// @param[in] frequency - Frequency to convert, in hertz
74 float frequency_clock_t::frequency_to_period() const
75 {
76         return frequency_ == 0 ? 0 : 1 / frequency_;
77 }
78
79 /// @brief Return a timeval struct based on the frequency_ member. used to
80 /// specified CAN BCM timers.
81 const struct timeval frequency_clock_t::get_timeval_from_period() const
82 {
83         struct timeval freq = {0, 0};
84         float f;
85         freq.tv_usec = (long int)(std::modf(frequency_to_period(), &f) * unit_);
86         freq.tv_sec = (time_t)f;
87
88         return freq;
89 }
90
91 bool frequency_clock_t::started()
92 {
93         return last_tick_ != 0;
94 }
95
96 time_function_t frequency_clock_t::get_time_function()
97 {
98         return time_function_ != nullptr ? time_function_ : system_time_us;
99 }
100
101 bool frequency_clock_t::elapsed(bool stagger)
102 {
103         float period = frequency_to_period();
104         float elapsed_time = 0;
105         if(!started() && stagger)
106                 last_tick_ = get_time_function()() - (rand() % int(period));
107
108         // Make sure it ticks the the first call
109         elapsed_time = !started() ? period : (float)get_time_function()() - (float)last_tick_;
110
111         return frequency_ == 0 || elapsed_time >= period;
112 }
113
114 float frequency_clock_t::get_frequency() const
115 {
116         return frequency_;
117 }
118
119 uint64_t frequency_clock_t::get_last_tick() const
120 {
121         return last_tick_;
122 }
123
124 /// @brief Force the clock to tick, regardless of it its time has actually
125 /// elapsed.
126 void frequency_clock_t::tick(uint64_t timestamp)
127 {
128         last_tick_ = timestamp;
129 }