feat: 添加时间同步管理功能
- 新增time_manager模块用于统一管理时间同步状态 - 实现BLE时间同步GATT服务(time_sync_event和ble_time_sync_module) - 添加time_sync_protocol定义统一的协议帧格式 - 支持UTC时间戳、时区偏移和精度信息的时间同步 - 实现settings持久化存储时间校准数据 - 提供time_manager快照API供其他模块查询当前时间状态 - 增加对BLE/USB/手动三种同步源的支持和区分
This commit is contained in:
68
inc/time_manager.h
Normal file
68
inc/time_manager.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef TIME_MANAGER_H__
|
||||
#define TIME_MANAGER_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 时间同步来源保持传输无关:
|
||||
* - BLE/USB/手动设置都复用同一套枚举;
|
||||
* - 后续如果新增其他同步链路,只需要补枚举值,不需要改事件语义。
|
||||
*/
|
||||
enum time_sync_source {
|
||||
TIME_SYNC_SOURCE_NONE = 0,
|
||||
TIME_SYNC_SOURCE_BLE,
|
||||
TIME_SYNC_SOURCE_USB,
|
||||
TIME_SYNC_SOURCE_MANUAL,
|
||||
};
|
||||
|
||||
/*
|
||||
* 时间同步更新载荷:
|
||||
* - utc_ms 统一使用 UTC 毫秒时间戳,避免内部状态受本地时区影响;
|
||||
* - timezone_min 记录“显示层”所需的时区偏移,当前不拆 DST;
|
||||
* - accuracy_ms 允许上位机表达这次校时的可信度,未知时传 0 即可。
|
||||
*/
|
||||
struct time_sync_update {
|
||||
uint64_t utc_ms;
|
||||
int16_t timezone_min;
|
||||
uint32_t accuracy_ms;
|
||||
enum time_sync_source source;
|
||||
};
|
||||
|
||||
/*
|
||||
* 时间快照用于提供给显示、日志或后续 USB/调试接口:
|
||||
* - synchronized=true 表示当前开机周期内已经收到有效校时;
|
||||
* - has_persisted_time=true 仅表示 flash 里存过一次历史校时,不代表当前时间仍然可信;
|
||||
* - ready=false 表示 time_manager 还没等到 settings_loader 完成初始化。
|
||||
*/
|
||||
struct time_manager_snapshot {
|
||||
uint64_t utc_ms;
|
||||
int16_t timezone_min;
|
||||
uint32_t accuracy_ms;
|
||||
enum time_sync_source source;
|
||||
bool ready;
|
||||
bool synchronized;
|
||||
bool has_persisted_time;
|
||||
};
|
||||
|
||||
/* 返回当前模块是否已经完成初始化,供同步入口快速拒绝“过早写入”。 */
|
||||
bool time_manager_is_ready(void);
|
||||
|
||||
/*
|
||||
* 获取当前时间快照:
|
||||
* - 返回 0:snapshot 已填充;
|
||||
* - 返回 -EINVAL:参数为空;
|
||||
* - 返回 -EAGAIN:模块未 ready;
|
||||
* - 返回 -ENODATA:当前开机周期尚未完成有效校时。
|
||||
*/
|
||||
int time_manager_get_snapshot(struct time_manager_snapshot *snapshot);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TIME_MANAGER_H__ */
|
||||
33
inc/time_sync_protocol.h
Normal file
33
inc/time_sync_protocol.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef TIME_SYNC_PROTOCOL_H__
|
||||
#define TIME_SYNC_PROTOCOL_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
/*
|
||||
* 统一定义时间同步协议帧格式,方便 BLE/USB 两条链路共享:
|
||||
*
|
||||
* byte 0 : version
|
||||
* byte 1 : flags
|
||||
* byte 2-3 : timezone_min (little-endian, int16)
|
||||
* byte 4-11: utc_ms (little-endian, uint64)
|
||||
* byte 12-15: accuracy_ms (little-endian, uint32)
|
||||
*/
|
||||
#define TIME_SYNC_PROTOCOL_VERSION 1U
|
||||
#define TIME_SYNC_PROTOCOL_PAYLOAD_SIZE 16U
|
||||
|
||||
#define TIME_SYNC_PROTOCOL_OFFSET_VERSION 0U
|
||||
#define TIME_SYNC_PROTOCOL_OFFSET_FLAGS 1U
|
||||
#define TIME_SYNC_PROTOCOL_OFFSET_TIMEZONE 2U
|
||||
#define TIME_SYNC_PROTOCOL_OFFSET_UTC_MS 4U
|
||||
#define TIME_SYNC_PROTOCOL_OFFSET_ACCURACY_MS 12U
|
||||
|
||||
/*
|
||||
* 预留 flags 字段:
|
||||
* - 当前版本只要求时区字段有效;
|
||||
* - 后续如果要加 DST、闰秒或来源质量扩展,可以继续复用这个字节。
|
||||
*/
|
||||
#define TIME_SYNC_PROTOCOL_FLAG_TIMEZONE_VALID BIT(0)
|
||||
|
||||
#endif /* TIME_SYNC_PROTOCOL_H__ */
|
||||
Reference in New Issue
Block a user