timing_alt.h 源码
2026/7/20大约 1 分钟附录源码附录MBED TLS定时器
timing_alt.h — MBED TLS 定时器适配头文件
路径: components/security/mbedtls/port/inc/timing_alt.h
功能: 定义 MBED TLS 定时器数据结构,将 OneOS 系统 tick 适配为 MBED TLS 的高精度定时器。TLS 握手过程中的超时控制、DTLS 的重传定时器都依赖此适配层。
引用章节: 7.3.1 MBED TLS 加密组件
完整源码
/**
* \file timing_alt.h
*
* \brief Portable interface to timeouts and to the CPU cycle counter
*/
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/
#ifndef MBEDTLS_TIMING_ALT_H
#define MBEDTLS_TIMING_ALT_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief timer structure
*/
struct mbedtls_timing_hr_time
{
uint64_t timer_ms;
};
/**
* \brief Context for mbedtls_timing_set/get_delay()
*/
typedef struct
{
struct mbedtls_timing_hr_time timer;
uint32_t int_ms;
uint32_t fin_ms;
} mbedtls_timing_delay_context;
#ifdef __cplusplus
}
#endif
#endif /* timing_alt.h */关键说明
| 模块 | 说明 |
|---|---|
| mbedtls_timing_hr_time | 高精度定时器结构,timer_ms 存储毫秒级时间戳,由 mbedtls_timing_get_timer() 填充 |
| mbedtls_timing_delay_context | 延时上下文,包含起始时间戳(timer)、中间延时(int_ms)和最终延时(fin_ms) |
| int_ms | 中间延时(毫秒),用于 DTLS 握手重传定时器,到达后触发重传 |
| fin_ms | 最终延时(毫秒),超时后放弃等待,返回错误 |
| 适配宏 | 通过 MBEDTLS_TIMING_ALT 宏启用,让 MBED TLS 使用 OneOS 的 os_tick_get_value() 作为时间源 |