timing_alt.c 源码
2026/7/20大约 2 分钟附录源码附录MBED TLS定时器
timing_alt.c — MBED TLS 定时器适配实现
路径: components/security/mbedtls/port/src/timing_alt.c
功能: 实现 MBED TLS 定时器接口,将 OneOS 系统 tick 转换为毫秒级时间戳。提供 TLS 握手超时控制、DTLS 重传定时器以及 hardclock 调试接口。
引用章节: 7.3.1 MBED TLS 加密组件
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file thread_alt.c
*
* @brief Implementation of the functions when MBEDTLS_THREADING_ALT defined for mbedtls.
*
* @revision
* Date Author Notes
* 2020-08-25 OneOs Team First Version
***********************************************************************************************************************
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_TIMING_ALT)
#include <sys/time.h>
#include <os_clock.h>
#include "timing_alt.h"
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
struct mbedtls_timing_hr_time now;
now.timer_ms = ((uint64_t)os_tick_get_value() * (1000 / os_tick_from_ms(1000)));
if (reset)
{
val->timer_ms = now.timer_ms;
}
return (unsigned long)(now.timer_ms - val->timer_ms);
}
/*
* Set delays to watch
*/
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
{
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *)data;
ctx->int_ms = int_ms;
ctx->fin_ms = fin_ms;
if (fin_ms != 0)
{
(void)mbedtls_timing_get_timer(&ctx->timer, 1);
}
}
/*
* Get number of delays expired
*/
int mbedtls_timing_get_delay( void *data )
{
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
unsigned long elapsed_ms;
if( ctx->fin_ms == 0 )
return( -1 );
elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
if( elapsed_ms >= ctx->fin_ms )
return( 2 );
if( elapsed_ms >= ctx->int_ms )
return( 1 );
return( 0 );
}
unsigned long mbedtls_timing_hardclock(void)
{
#ifdef OS_USING_RTC
static int hardclock_init = 0;
static struct timeval tv_init;
struct timeval tv_cur;
if (hardclock_init == 0)
{
gettimeofday(&tv_init, NULL);
hardclock_init = 1;
}
gettimeofday(&tv_cur, NULL);
return ((tv_cur.tv_sec - tv_init.tv_sec) * 1000000 + (tv_cur.tv_usec - tv_init.tv_usec));
#else
static os_tick_t fisrt_tick = 0;
if (!fisrt_tick)
fisrt_tick = os_tick_get_value();
os_tick_t tick = os_tick_get_value();
tick -= fisrt_tick;
return tick ? tick * (1000000 / OS_TICK_PER_SECOND) : 1;
#endif
}
#endif关键说明
| 模块 | 说明 |
|---|---|
| mbedtls_timing_get_timer | 核心定时器函数,将 os_tick_get_value() 的 tick 值转换为毫秒。reset=1 时重置基准时间,reset=0 时返回经过的毫秒数 |
| tick → ms 转换 | 公式 tick * (1000 / os_tick_from_ms(1000)) 确保与系统 tick 频率无关的毫秒精度 |
| mbedtls_timing_set_delay | 设置延时参数,fin_ms != 0 时自动记录起始时间戳 |
| mbedtls_timing_get_delay | 返回延时状态:0=未到达,1=中间延时到达(DTLS 重传),2=最终延时到达(超时),-1=无延时 |
| DTLS 重传 | 中间延时 int_ms 和最终延时 fin_ms 的双重定时器设计,专为 DTLS 握手重传机制服务 |
| hardclock | 调试用高精度时钟,启用 RTC 时使用 gettimeofday 微秒精度,否则回退到 tick 计数 |