Contiki-NG
Loading...
Searching...
No Matches
timing_alt.c
1/*
2 * Copyright (c) 2023, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "mbedtls/private_access.h"
35#include "timing_alt.h"
36
37/* Log configuration */
38#include "sys/log.h"
39#define LOG_MODULE "DTLS"
40#define LOG_LEVEL LOG_LEVEL_NONE
41
42volatile int mbedtls_timing_alarmed;
43static struct ctimer alarm_timer;
44
45/*---------------------------------------------------------------------------*/
46static void
47alarm_callback(void *ptr)
48{
49 LOG_DBG("Triggered an alarm\n");
50 mbedtls_timing_alarmed = 1;
51}
52/*---------------------------------------------------------------------------*/
53void
54mbedtls_set_alarm(int seconds)
55{
56 LOG_DBG("Set alarm in %d seconds\n", seconds);
57 mbedtls_timing_alarmed = 0;
58 ctimer_set(&alarm_timer, seconds * CLOCK_SECOND, alarm_callback, NULL);
59}
60/*---------------------------------------------------------------------------*/
61int
62mbedtls_timing_get_delay(void *data)
63{
64 struct mbedtls_timing_delay_context *ctx =
65 (struct mbedtls_timing_delay_context *)data;
66 unsigned long elapsed_ms =
67 mbedtls_timing_get_timer(&ctx->MBEDTLS_PRIVATE(timer), 0);
68
69 if(ctx->MBEDTLS_PRIVATE(fin_ms) == 0) {
70 return -1;
71 }
72
73 if(elapsed_ms > ctx->MBEDTLS_PRIVATE(fin_ms)) {
74 /* The final delay has passed. */
75 LOG_DBG("%s: the final delay has passed. %lu > %"PRIu32"\n",
76 __func__, elapsed_ms, ctx->MBEDTLS_PRIVATE(fin_ms));
77 return 2;
78 }
79
80 if(elapsed_ms > ctx->MBEDTLS_PRIVATE(int_ms)) {
81 /* The intermediate delay has passed. */
82 LOG_DBG("%s: the intermediate delay has passed. %lu > %"PRIu32"\n",
83 __func__, elapsed_ms, ctx->MBEDTLS_PRIVATE(int_ms));
84 return 1;
85 }
86
87 /* None of the delays have passed. */
88 LOG_DBG("%s: no delay has passed. elapsed ms %lu, fin ms %"PRIu32", int ms %"PRIu32"\n",
89 __func__, elapsed_ms,
90 ctx->MBEDTLS_PRIVATE(fin_ms), ctx->MBEDTLS_PRIVATE(int_ms));
91 return 0;
92}
93/*---------------------------------------------------------------------------*/
94unsigned long
95mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
96{
97 if(reset) {
98 timer_reset(&val->timer);
99 }
100 unsigned long ret = ((clock_time() - val->timer.start) * 1000) / CLOCK_SECOND;
101 return ret;
102}
103/*---------------------------------------------------------------------------*/
104unsigned long
105mbedtls_timing_hardclock(void)
106{
107 /* This should preferably be a CPU cycle counter, but we use the rtimer
108 value instead. */
109 return RTIMER_NOW();
110}
111/*---------------------------------------------------------------------------*/
112void
113mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
114{
115 struct mbedtls_timing_delay_context *ctx;
116
117 ctx = (struct mbedtls_timing_delay_context *)data;
118 struct timer *timer = (struct timer *)&ctx->MBEDTLS_PRIVATE(timer);
119
120 timer_set(timer, fin_ms * CLOCK_SECOND / 1000);
121 ctx->MBEDTLS_PRIVATE(int_ms) = int_ms;
122 ctx->MBEDTLS_PRIVATE(fin_ms) = fin_ms;
123}
124/*---------------------------------------------------------------------------*/
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition ctimer.h:150
#define RTIMER_NOW()
Get the current clock time.
Definition rtimer.h:191
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition timer.c:64
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition timer.c:84
Header file for the logging system.
A timer.
Definition timer.h:84