1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreTimestamp |
---|
7 | * |
---|
8 | * @brief This header file provides interfaces of the |
---|
9 | * @ref RTEMSScoreTimestamp which are only used by the implementation. |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2009. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef _RTEMS_SCORE_TIMESTAMPIMPL_H |
---|
39 | #define _RTEMS_SCORE_TIMESTAMPIMPL_H |
---|
40 | |
---|
41 | /** |
---|
42 | * @addtogroup RTEMSScoreTimestamp |
---|
43 | * |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | |
---|
47 | #include <rtems/score/timestamp.h> |
---|
48 | #include <rtems/score/basedefs.h> |
---|
49 | |
---|
50 | #include <sys/time.h> |
---|
51 | |
---|
52 | #ifdef __cplusplus |
---|
53 | extern "C" { |
---|
54 | #endif |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief Sets timestamp to specified seconds and nanoseconds. |
---|
58 | * |
---|
59 | * This method sets the timestamp to the specified @a _seconds and @a _nanoseconds |
---|
60 | * value. |
---|
61 | * |
---|
62 | * @param[out] _time The timestamp instance to set. |
---|
63 | * @param _seconds The seconds portion of the timestamp. |
---|
64 | * @param _nanoseconds The nanoseconds portion of the timestamp. |
---|
65 | */ |
---|
66 | static inline void _Timestamp_Set( |
---|
67 | Timestamp_Control *_time, |
---|
68 | time_t _seconds, |
---|
69 | long _nanoseconds |
---|
70 | ) |
---|
71 | { |
---|
72 | struct timespec _ts; |
---|
73 | |
---|
74 | _ts.tv_sec = _seconds; |
---|
75 | _ts.tv_nsec = _nanoseconds; |
---|
76 | |
---|
77 | *_time = tstosbt(_ts); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief Sets the timestamp to zero. |
---|
82 | * |
---|
83 | * This method sets the timestamp to zero. |
---|
84 | * value. |
---|
85 | * |
---|
86 | * @param[out] _time The timestamp instance to zero. |
---|
87 | */ |
---|
88 | |
---|
89 | static inline void _Timestamp_Set_to_zero( |
---|
90 | Timestamp_Control *_time |
---|
91 | ) |
---|
92 | { |
---|
93 | *_time = 0; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief Checks if the left hand side timestamp is less than the right one. |
---|
98 | * |
---|
99 | * This method is the less than operator for timestamps. |
---|
100 | * |
---|
101 | * @param _lhs The left hand side timestamp. |
---|
102 | * @param _rhs The right hand side timestamp. |
---|
103 | * |
---|
104 | * @retval true @a _lhs is less than the @a _rhs. |
---|
105 | * @retval false @a _lhs is greater or equal than @a rhs. |
---|
106 | */ |
---|
107 | |
---|
108 | static inline bool _Timestamp_Less_than( |
---|
109 | const Timestamp_Control *_lhs, |
---|
110 | const Timestamp_Control *_rhs |
---|
111 | ) |
---|
112 | { |
---|
113 | return *_lhs < *_rhs; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * @brief Checks if the left hand side timestamp is greater than the right one. |
---|
118 | * |
---|
119 | * This method is the greater than operator for timestamps. |
---|
120 | * |
---|
121 | * @param _lhs The left hand side timestamp. |
---|
122 | * @param _rhs The right hand side timestamp. |
---|
123 | * |
---|
124 | * @retval true @a _lhs is greater than the @a _rhs. |
---|
125 | * @retval false @a _lhs is less or equal than @a _rhs. |
---|
126 | */ |
---|
127 | |
---|
128 | static inline bool _Timestamp_Greater_than( |
---|
129 | const Timestamp_Control *_lhs, |
---|
130 | const Timestamp_Control *_rhs |
---|
131 | ) |
---|
132 | { |
---|
133 | return *_lhs > *_rhs; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * @brief Checks if the timestamps are equal. |
---|
138 | * |
---|
139 | * This method is the is equal to than operator for timestamps. |
---|
140 | * |
---|
141 | * @param _lhs The left hand side timestamp. |
---|
142 | * @param _rhs The right hand side timestamp. |
---|
143 | * |
---|
144 | * @retval true @a _lhs is equal to @a _rhs |
---|
145 | * @retval false @a _lhs is not equal to @a _rhs. |
---|
146 | */ |
---|
147 | |
---|
148 | static inline bool _Timestamp_Equal_to( |
---|
149 | const Timestamp_Control *_lhs, |
---|
150 | const Timestamp_Control *_rhs |
---|
151 | ) |
---|
152 | { |
---|
153 | return *_lhs == *_rhs; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief Adds two timestamps. |
---|
158 | * |
---|
159 | * This routine adds two timestamps. The second argument is added |
---|
160 | * to the first. |
---|
161 | * |
---|
162 | * @param[in, out] _time The base time to be added to. |
---|
163 | * @param _add points The timestamp to add to the first argument. |
---|
164 | */ |
---|
165 | static inline void _Timestamp_Add_to( |
---|
166 | Timestamp_Control *_time, |
---|
167 | const Timestamp_Control *_add |
---|
168 | ) |
---|
169 | { |
---|
170 | *_time += *_add; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * @brief Subtracts two timestamps. |
---|
175 | * |
---|
176 | * This routine subtracts two timestamps. @a result is set to |
---|
177 | * @a end - @a start. |
---|
178 | * |
---|
179 | * @param _start The starting time. |
---|
180 | * @param _end The ending time. |
---|
181 | * @param[out] _result Contains the difference between starting and ending |
---|
182 | * time after the method call. |
---|
183 | */ |
---|
184 | static inline void _Timestamp_Subtract( |
---|
185 | const Timestamp_Control *_start, |
---|
186 | const Timestamp_Control *_end, |
---|
187 | Timestamp_Control *_result |
---|
188 | ) |
---|
189 | { |
---|
190 | *_result = *_end - *_start; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * @brief Divides a timestamp by another timestamp. |
---|
195 | * |
---|
196 | * This routine divides a timestamp by another timestamp. The |
---|
197 | * intended use is for calculating percentages to three decimal points. |
---|
198 | * |
---|
199 | * @param _lhs The left hand number. |
---|
200 | * @param _rhs The right hand number. |
---|
201 | * @param[out] _ival_percentage The integer portion of the average. |
---|
202 | * @param[out] _fval_percentage The thousandths of percentage. |
---|
203 | */ |
---|
204 | static inline void _Timestamp_Divide( |
---|
205 | const Timestamp_Control *_lhs, |
---|
206 | const Timestamp_Control *_rhs, |
---|
207 | uint32_t *_ival_percentage, |
---|
208 | uint32_t *_fval_percentage |
---|
209 | ) |
---|
210 | { |
---|
211 | struct timespec _ts_left; |
---|
212 | struct timespec _ts_right; |
---|
213 | |
---|
214 | _ts_left = sbttots( *_lhs ); |
---|
215 | _ts_right = sbttots( *_rhs ); |
---|
216 | |
---|
217 | _Timespec_Divide( |
---|
218 | &_ts_left, |
---|
219 | &_ts_right, |
---|
220 | _ival_percentage, |
---|
221 | _fval_percentage |
---|
222 | ); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief Gets seconds portion of timestamp. |
---|
227 | * |
---|
228 | * This method returns the seconds portion of the specified timestamp. |
---|
229 | * |
---|
230 | * @param _time The timestamp. |
---|
231 | * |
---|
232 | * @return The seconds portion of @a _time. |
---|
233 | */ |
---|
234 | static inline time_t _Timestamp_Get_seconds( |
---|
235 | const Timestamp_Control *_time |
---|
236 | ) |
---|
237 | { |
---|
238 | return (*_time >> 32); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * @brief Gets nanoseconds portion of timestamp. |
---|
243 | * |
---|
244 | * This method returns the nanoseconds portion of the specified timestamp. |
---|
245 | * |
---|
246 | * @param _time The timestamp. |
---|
247 | * |
---|
248 | * @return The nanoseconds portion of @a _time. |
---|
249 | */ |
---|
250 | static inline uint32_t _Timestamp_Get_nanoseconds( |
---|
251 | const Timestamp_Control *_time |
---|
252 | ) |
---|
253 | { |
---|
254 | struct timespec _ts; |
---|
255 | |
---|
256 | _ts = sbttots( *_time ); |
---|
257 | |
---|
258 | return (uint32_t) _ts.tv_nsec; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @brief Gets the timestamp as nanoseconds. |
---|
263 | * |
---|
264 | * This method returns the timestamp as nanoseconds. |
---|
265 | * |
---|
266 | * @param _time The timestamp. |
---|
267 | * |
---|
268 | * @return The time in nanoseconds. |
---|
269 | */ |
---|
270 | static inline uint64_t _Timestamp_Get_as_nanoseconds( |
---|
271 | const Timestamp_Control *_time |
---|
272 | ) |
---|
273 | { |
---|
274 | struct timespec _ts; |
---|
275 | |
---|
276 | _ts = sbttots( *_time ); |
---|
277 | |
---|
278 | return _Timespec_Get_as_nanoseconds( &_ts ); |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * @brief Converts timestamp to struct timespec. |
---|
283 | * |
---|
284 | * @param _timestamp The timestamp. |
---|
285 | * @param[out] _timespec The timespec to be filled in by the method. |
---|
286 | */ |
---|
287 | static inline void _Timestamp_To_timespec( |
---|
288 | const Timestamp_Control *_timestamp, |
---|
289 | struct timespec *_timespec |
---|
290 | ) |
---|
291 | { |
---|
292 | *_timespec = sbttots( *_timestamp ); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @brief Converts timestamp to struct timeval. |
---|
297 | * |
---|
298 | * @param _timestamp The timestamp. |
---|
299 | * @param[out] _timeval The timeval to be filled in by the method. |
---|
300 | */ |
---|
301 | static inline void _Timestamp_To_timeval( |
---|
302 | const Timestamp_Control *_timestamp, |
---|
303 | struct timeval *_timeval |
---|
304 | ) |
---|
305 | { |
---|
306 | *_timeval = sbttotv( *_timestamp ); |
---|
307 | } |
---|
308 | |
---|
309 | #ifdef __cplusplus |
---|
310 | } |
---|
311 | #endif |
---|
312 | |
---|
313 | /** @} */ |
---|
314 | |
---|
315 | #endif |
---|
316 | /* end of include file */ |
---|