1 /*
2  *  Portable interface to the CPU cycle counter
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 /*
23  * mbedtls_timing_get_timer()m mbedtls_timing_set_delay() and
24  * mbedtls_timing_set_delay only abstracted from mbedtls/library/timing.c
25  * as that does not build on ESP-IDF but these 2 functions are needed for
26  * DTLS (in particular mbedtls_ssl_set_timer_cb() must be called for DTLS
27  * which requires these 2 delay functions).
28  */
29 
30 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include "mbedtls/config.h"
32 #else
33 #include MBEDTLS_CONFIG_FILE
34 #endif
35 
36 #if !defined(MBEDTLS_ESP_TIMING_C)
37 
38 #include <sys/time.h>
39 #include "mbedtls/timing.h"
40 
41 struct _hr_time
42 {
43     struct timeval start;
44 };
45 
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)46 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
47 {
48     struct _hr_time *t = (struct _hr_time *) val;
49 
50     if( reset )
51     {
52         gettimeofday( &t->start, NULL );
53         return( 0 );
54     }
55     else
56     {
57         unsigned long delta;
58         struct timeval now;
59         gettimeofday( &now, NULL );
60         delta = ( now.tv_sec  - t->start.tv_sec  ) * 1000ul
61               + ( now.tv_usec - t->start.tv_usec ) / 1000;
62         return( delta );
63     }
64 }
65 
66 /*
67  * Set delays to watch
68  */
mbedtls_timing_set_delay(void * data,uint32_t int_ms,uint32_t fin_ms)69 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
70 {
71     mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
72 
73     ctx->int_ms = int_ms;
74     ctx->fin_ms = fin_ms;
75 
76     if( fin_ms != 0 )
77         (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
78 }
79 
80 /*
81  * Get number of delays expired
82  */
mbedtls_timing_get_delay(void * data)83 int mbedtls_timing_get_delay( void *data )
84 {
85     mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
86     unsigned long elapsed_ms;
87 
88     if( ctx->fin_ms == 0 )
89         return( -1 );
90 
91     elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
92 
93     if( elapsed_ms >= ctx->fin_ms )
94         return( 2 );
95 
96     if( elapsed_ms >= ctx->int_ms )
97         return( 1 );
98 
99     return( 0 );
100 }
101 
102 #endif /* MBEDTLS_ESP_TIMING_C */
103