Mbed OS Reference
Loading...
Searching...
No Matches
timing.h
Go to the documentation of this file.
1/**
2 * \file timing.h
3 *
4 * \brief Portable interface to timeouts and to the CPU cycle counter
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22#ifndef MBEDTLS_TIMING_H
23#define MBEDTLS_TIMING_H
24
25#if !defined(MBEDTLS_CONFIG_FILE)
26#include "mbedtls/config.h"
27#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
31#include <stdint.h>
32
33/**
34 * \addtogroup mbedtls
35 * \{
36 * \defgroup mbedtls_timing_module Timing
37 * \{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44#if !defined(MBEDTLS_TIMING_ALT)
45// Regular implementation
46//
47
48/**
49 * \brief timer structure
50 */
52{
53 unsigned char opaque[32];
54};
55
56/**
57 * \brief Context for mbedtls_timing_set/get_delay()
58 */
60{
61 struct mbedtls_timing_hr_time timer;
62 uint32_t int_ms;
63 uint32_t fin_ms;
65
66#else /* MBEDTLS_TIMING_ALT */
67#include "timing_alt.h"
68#endif /* MBEDTLS_TIMING_ALT */
69
70extern volatile int mbedtls_timing_alarmed;
71
72/**
73 * \brief Return the CPU cycle counter value
74 *
75 * \warning This is only a best effort! Do not rely on this!
76 * In particular, it is known to be unreliable on virtual
77 * machines.
78 *
79 * \note This value starts at an unspecified origin and
80 * may wrap around.
81 */
82unsigned long mbedtls_timing_hardclock( void );
83
84/**
85 * \brief Return the elapsed time in milliseconds
86 *
87 * \param val points to a timer structure
88 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
89 *
90 * \return Elapsed time since the previous reset in ms. When
91 * restarting, this is always 0.
92 *
93 * \note To initialize a timer, call this function with reset=1.
94 *
95 * Determining the elapsed time and resetting the timer is not
96 * atomic on all platforms, so after the sequence
97 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
98 * get_timer(0) }` the value time1+time2 is only approximately
99 * the delay since the first reset.
100 */
101unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
102
103/**
104 * \brief Setup an alarm clock
105 *
106 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
107 * (must be >=0)
108 *
109 * \warning Only one alarm at a time is supported. In a threaded
110 * context, this means one for the whole process, not one per
111 * thread.
112 */
113void mbedtls_set_alarm( int seconds );
114
115/**
116 * \brief Set a pair of delays to watch
117 * (See \c mbedtls_timing_get_delay().)
118 *
119 * \param data Pointer to timing data.
120 * Must point to a valid \c mbedtls_timing_delay_context struct.
121 * \param int_ms First (intermediate) delay in milliseconds.
122 * The effect if int_ms > fin_ms is unspecified.
123 * \param fin_ms Second (final) delay in milliseconds.
124 * Pass 0 to cancel the current delay.
125 *
126 * \note To set a single delay, either use \c mbedtls_timing_set_timer
127 * directly or use this function with int_ms == fin_ms.
128 */
129void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
130
131/**
132 * \brief Get the status of delays
133 * (Memory helper: number of delays passed.)
134 *
135 * \param data Pointer to timing data
136 * Must point to a valid \c mbedtls_timing_delay_context struct.
137 *
138 * \return -1 if cancelled (fin_ms = 0),
139 * 0 if none of the delays are passed,
140 * 1 if only the intermediate delay is passed,
141 * 2 if the final delay is passed.
142 */
143int mbedtls_timing_get_delay( void *data );
144
145#if defined(MBEDTLS_SELF_TEST)
146/**
147 * \brief Checkup routine
148 *
149 * \return 0 if successful, or 1 if a test failed
150 */
151int mbedtls_timing_self_test( int verbose );
152#endif
153
154#ifdef __cplusplus
155}
156#endif
157
158/// \}
159/// \}
160
161#endif /* timing.h */
Configuration options (set of defines)
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Return the elapsed time in milliseconds.
void mbedtls_set_alarm(int seconds)
Setup an alarm clock.
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Set a pair of delays to watch (See mbedtls_timing_get_delay().)
unsigned long mbedtls_timing_hardclock(void)
Return the CPU cycle counter value.
int mbedtls_timing_get_delay(void *data)
Get the status of delays (Memory helper: number of delays passed.)
Context for mbedtls_timing_set/get_delay()
Definition: timing.h:60
timer structure
Definition: timing.h:52