更新硬件SDK

This commit is contained in:
kerwincui
2023-03-04 03:44:56 +08:00
parent dcdf6e1b7c
commit e39d3d2f03
1900 changed files with 663153 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
Name: 时间同步模块
NTP Component for Link SDK V4.0.0

View File

@@ -0,0 +1,342 @@
/**
* @file aiot_ntp_api.c
* @brief ntp模块的API接口实现, 提供获取utc时间的能力
*
* @copyright Copyright (C) 2015-2020 Alibaba Group Holding Limited
*
*/
/* TODO: 对本模块的头文件, 仅需包含ntp_private.h, 不需包含aiot_ntp_api.h */
#include "ntp_private.h"
/* TODO: 列出对core模块需要包含的头文件 */
#include "core_string.h"
#include "core_log.h"
#include "core_global.h"
#include "core_mqtt.h"
static void _core_ntp_exec_inc(ntp_handle_t *ntp_handle)
{
ntp_handle->sysdep->core_sysdep_mutex_lock(ntp_handle->data_mutex);
ntp_handle->exec_count++;
ntp_handle->sysdep->core_sysdep_mutex_unlock(ntp_handle->data_mutex);
}
static void _core_ntp_exec_dec(ntp_handle_t *ntp_handle)
{
ntp_handle->sysdep->core_sysdep_mutex_lock(ntp_handle->data_mutex);
ntp_handle->exec_count--;
ntp_handle->sysdep->core_sysdep_mutex_unlock(ntp_handle->data_mutex);
}
static void _ntp_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
{
ntp_handle_t *ntp_handle = (ntp_handle_t *)userdata;
switch (packet->type) {
case AIOT_MQTTRECV_PUB: {
char *dst_key = "deviceSendTime", *srt_key = "serverRecvTime", *sst_key = "serverSendTime";
char *dst_value = NULL, *srt_value = NULL, *sst_value = NULL;
uint32_t dst_value_len = 0, srt_value_len = 0, sst_value_len = 0;
uint64_t dst = 0, srt = 0, sst = 0, utc = 0;
if (core_json_value((char *)packet->data.pub.payload, packet->data.pub.payload_len, dst_key, (uint32_t)strlen(dst_key),
&dst_value, &dst_value_len) == STATE_SUCCESS &&
core_json_value((char *)packet->data.pub.payload, packet->data.pub.payload_len, srt_key, (uint32_t)strlen(srt_key),
&srt_value, &srt_value_len) == STATE_SUCCESS &&
core_json_value((char *)packet->data.pub.payload, packet->data.pub.payload_len, sst_key, (uint32_t)strlen(sst_key),
&sst_value, &sst_value_len) == STATE_SUCCESS) {
if (core_str2uint64(dst_value, (uint8_t)dst_value_len, &dst) == STATE_SUCCESS &&
core_str2uint64(srt_value, (uint8_t)srt_value_len, &srt) == STATE_SUCCESS &&
core_str2uint64(sst_value, (uint8_t)sst_value_len, &sst) == STATE_SUCCESS) {
core_date_t date;
utc = (srt + sst + ntp_handle->sysdep->core_sysdep_time() - dst) / 2;
core_log_set_timestamp(ntp_handle->sysdep, utc);
memset(&date, 0, sizeof(core_date_t));
core_utc2date(utc, ntp_handle->time_zone, &date);
if (ntp_handle->recv_handler != NULL) {
aiot_ntp_recv_t recv;
memset(&recv, 0, sizeof(aiot_ntp_recv_t));
recv.type = AIOT_NTPRECV_LOCAL_TIME;
recv.data.local_time.timestamp = utc;
recv.data.local_time.year = date.year;
recv.data.local_time.mon = date.mon;
recv.data.local_time.day = date.day;
recv.data.local_time.hour = date.hour;
recv.data.local_time.min = date.min;
recv.data.local_time.sec = date.sec;
recv.data.local_time.msec = date.msec;
ntp_handle->recv_handler(ntp_handle, &recv, ntp_handle->userdata);
}
} else {
if (ntp_handle->event_handler != NULL) {
aiot_ntp_event_t event;
memset(&event, 0, sizeof(aiot_ntp_event_t));
event.type = AIOT_NTPEVT_INVALID_TIME_FORMAT;
ntp_handle->event_handler(ntp_handle, &event, ntp_handle->userdata);
}
}
} else {
if (ntp_handle->event_handler != NULL) {
aiot_ntp_event_t event;
memset(&event, 0, sizeof(aiot_ntp_event_t));
event.type = AIOT_NTPEVT_INVALID_RESPONSE;
ntp_handle->event_handler(ntp_handle, &event, ntp_handle->userdata);
}
}
}
default: {
}
break;
}
}
static int32_t _ntp_operate_topic_map(ntp_handle_t *ntp_handle, aiot_mqtt_option_t option)
{
int32_t res = STATE_SUCCESS;
aiot_mqtt_topic_map_t map;
char *topic = NULL;
char *topic_src[] = { core_mqtt_get_product_key(ntp_handle->mqtt_handle), core_mqtt_get_device_name(ntp_handle->mqtt_handle) };
char *topic_fmt = NTP_RESPONSE_TOPIC_FMT;
memset(&map, 0, sizeof(aiot_mqtt_topic_map_t));
res = core_sprintf(ntp_handle->sysdep, &topic, topic_fmt, topic_src, sizeof(topic_src) / sizeof(char *),
NTP_MODULE_NAME);
if (res < STATE_SUCCESS) {
return res;
}
map.topic = topic;
map.handler = _ntp_recv_handler;
map.userdata = (void *)ntp_handle;
res = aiot_mqtt_setopt(ntp_handle->mqtt_handle, option, &map);
ntp_handle->sysdep->core_sysdep_free(topic);
return res;
}
static void _ntp_core_mqtt_process_handler(void *context, aiot_mqtt_event_t *event, core_mqtt_event_t *core_event)
{
ntp_handle_t *ntp_handle = (ntp_handle_t *)context;
if (core_event != NULL) {
switch (core_event->type) {
case CORE_MQTTEVT_DEINIT: {
ntp_handle->mqtt_handle = NULL;
return;
}
default: {
}
break;
}
}
}
static int32_t _ntp_core_mqtt_operate_process_handler(ntp_handle_t *ntp_handle, core_mqtt_option_t option)
{
core_mqtt_process_data_t process_data;
memset(&process_data, 0, sizeof(core_mqtt_process_data_t));
process_data.handler = _ntp_core_mqtt_process_handler;
process_data.context = ntp_handle;
return core_mqtt_setopt(ntp_handle->mqtt_handle, option, &process_data);
}
void *aiot_ntp_init(void)
{
ntp_handle_t *ntp_handle = NULL;
aiot_sysdep_portfile_t *sysdep = NULL;
sysdep = aiot_sysdep_get_portfile();
if (sysdep == NULL) {
return NULL;
}
ntp_handle = sysdep->core_sysdep_malloc(sizeof(ntp_handle_t), NTP_MODULE_NAME);
if (ntp_handle == NULL) {
return NULL;
}
memset(ntp_handle, 0, sizeof(ntp_handle_t));
ntp_handle->sysdep = sysdep;
ntp_handle->deinit_timeout_ms = NTP_DEFAULT_DEINIT_TIMEOUT_MS;
ntp_handle->data_mutex = sysdep->core_sysdep_mutex_init();
ntp_handle->exec_enabled = 1;
return ntp_handle;
}
int32_t aiot_ntp_setopt(void *handle, aiot_ntp_option_t option, void *data)
{
int32_t res = STATE_SUCCESS;
ntp_handle_t *ntp_handle = (ntp_handle_t *)handle;
if (handle == NULL || data == NULL) {
return STATE_USER_INPUT_NULL_POINTER;
}
if (option >= AIOT_NTPOPT_MAX) {
return STATE_USER_INPUT_OUT_RANGE;
}
if (ntp_handle->exec_enabled == 0) {
return STATE_USER_INPUT_EXEC_DISABLED;
}
_core_ntp_exec_inc(ntp_handle);
ntp_handle->sysdep->core_sysdep_mutex_lock(ntp_handle->data_mutex);
switch (option) {
case AIOT_NTPOPT_MQTT_HANDLE: {
ntp_handle->mqtt_handle = data;
ntp_handle->sysdep->core_sysdep_mutex_unlock(ntp_handle->data_mutex);
res = _ntp_operate_topic_map(ntp_handle, AIOT_MQTTOPT_APPEND_TOPIC_MAP);
if (res >= STATE_SUCCESS) {
res = _ntp_core_mqtt_operate_process_handler(ntp_handle, CORE_MQTTOPT_APPEND_PROCESS_HANDLER);
}
ntp_handle->sysdep->core_sysdep_mutex_lock(ntp_handle->data_mutex);
}
break;
case AIOT_NTPOPT_TIME_ZONE: {
ntp_handle->time_zone = *(int8_t *)data;
}
break;
case AIOT_NTPOPT_RECV_HANDLER: {
ntp_handle->recv_handler = (aiot_ntp_recv_handler_t)data;
}
break;
case AIOT_NTPOPT_EVENT_HANDLER: {
ntp_handle->event_handler = (aiot_ntp_event_handler_t)data;
}
break;
case AIOT_NTPOPT_USERDATA: {
ntp_handle->userdata = data;
}
break;
case AIOT_NTPOPT_DEINIT_TIMEOUT_MS: {
ntp_handle->deinit_timeout_ms = *(uint32_t *)data;
}
break;
default: {
res = STATE_USER_INPUT_UNKNOWN_OPTION;
}
break;
}
ntp_handle->sysdep->core_sysdep_mutex_unlock(ntp_handle->data_mutex);
_core_ntp_exec_dec(ntp_handle);
return res;
}
int32_t aiot_ntp_deinit(void **handle)
{
uint64_t deinit_timestart = 0;
ntp_handle_t *ntp_handle = NULL;
if (handle == NULL || *handle == NULL) {
return STATE_USER_INPUT_NULL_POINTER;
}
ntp_handle = *(ntp_handle_t **)handle;
if (ntp_handle->exec_enabled == 0) {
return STATE_USER_INPUT_EXEC_DISABLED;
}
ntp_handle->exec_enabled = 0;
_ntp_core_mqtt_operate_process_handler(ntp_handle, CORE_MQTTOPT_REMOVE_PROCESS_HANDLER);
_ntp_operate_topic_map(ntp_handle, AIOT_MQTTOPT_REMOVE_TOPIC_MAP);
deinit_timestart = ntp_handle->sysdep->core_sysdep_time();
do {
if (ntp_handle->exec_count == 0) {
break;
}
ntp_handle->sysdep->core_sysdep_sleep(NTP_DEINIT_INTERVAL_MS);
} while ((ntp_handle->sysdep->core_sysdep_time() - deinit_timestart) < ntp_handle->deinit_timeout_ms);
if (ntp_handle->exec_count != 0) {
return STATE_MQTT_DEINIT_TIMEOUT;
}
*handle = NULL;
ntp_handle->sysdep->core_sysdep_mutex_deinit(&ntp_handle->data_mutex);
ntp_handle->sysdep->core_sysdep_free(ntp_handle);
return STATE_SUCCESS;
}
int32_t aiot_ntp_send_request(void *handle)
{
int32_t res = STATE_SUCCESS;
char *topic = NULL, *payload = NULL;
ntp_handle_t *ntp_handle = (ntp_handle_t *)handle;
if (handle == NULL) {
return STATE_USER_INPUT_NULL_POINTER;
}
if (ntp_handle->mqtt_handle == NULL) {
return STATE_NTP_MISSING_MQTT_HANDLE;
}
if (ntp_handle->exec_enabled == 0) {
return STATE_USER_INPUT_EXEC_DISABLED;
}
_core_ntp_exec_inc(ntp_handle);
{
char *topic_src[] = { core_mqtt_get_product_key(ntp_handle->mqtt_handle), core_mqtt_get_device_name(ntp_handle->mqtt_handle) };
char *topic_fmt = NTP_REQUEST_TOPIC_FMT;
char time_str[21] = {0};
char *payload_src[] = { time_str };
char *payload_fmt = NTP_REQUEST_PAYLOAD_FMT;
res = core_sprintf(ntp_handle->sysdep, &topic, topic_fmt, topic_src, sizeof(topic_src) / sizeof(char *),
NTP_MODULE_NAME);
if (res < STATE_SUCCESS) {
_core_ntp_exec_dec(ntp_handle);
return res;
}
core_uint642str(ntp_handle->sysdep->core_sysdep_time(), time_str, NULL);
res = core_sprintf(ntp_handle->sysdep, &payload, payload_fmt, payload_src, sizeof(payload_src) / sizeof(char *),
NTP_MODULE_NAME);
if (res < STATE_SUCCESS) {
ntp_handle->sysdep->core_sysdep_free(topic);
_core_ntp_exec_dec(ntp_handle);
return res;
}
}
res = aiot_mqtt_pub(ntp_handle->mqtt_handle, topic, (uint8_t *)payload, (uint32_t)strlen(payload), 0);
ntp_handle->sysdep->core_sysdep_free(topic);
ntp_handle->sysdep->core_sysdep_free(payload);
if (res < STATE_SUCCESS) {
_core_ntp_exec_dec(ntp_handle);
return res;
}
_core_ntp_exec_dec(ntp_handle);
return STATE_SUCCESS;
}

View File

@@ -0,0 +1,264 @@
/**
* @file aiot_ntp_api.h
* @brief ntp模块头文件, 提供获取utc时间的能力
*
* @copyright Copyright (C) 2015-2020 Alibaba Group Holding Limited
*
* @details
*
* NTP模块用于从阿里云物联网平台上获取UTC时间, API的使用流程如下:
*
* 1. 首先参考 @ref aiot_mqtt_api.h 的说明, 保证成功建立与物联网平台的`MQTT`连接
*
* 2. 调用 @ref aiot_ntp_init 初始化ntp会话, 获取会话句柄
*
* 3. 调用 @ref aiot_ntp_setopt 配置NTP会话的参数, 常用配置项见 @ref aiot_ntp_setopt 的说明
*
* 4. 调用 @ref aiot_ntp_send_request 发送NTP请求
*
* 5. 收到的UTC时间经SDK处理后会调用由 @ref aiot_ntp_setopt 配置的 @ref AIOT_NTPOPT_RECV_HANDLER 回调函数, 通知用户当前的时间
*
*/
#ifndef __AIOT_NTP_API_H__
#define __AIOT_NTP_API_H__
#if defined(__cplusplus)
extern "C" {
#endif
#include <stdint.h>
/**
* @brief -0x1100~-0x11FF表达SDK在ntp模块内的状态码
*/
#define STATE_NTP_BASE (-0x1100)
/**
* @brief MQTT会话句柄未设置, 请通过 @ref aiot_ntp_setopt 设置MQTT会话句柄
*/
#define STATE_NTP_MISSING_MQTT_HANDLE (-0x1101)
/**
* @brief ntp模块收到从网络上来的报文时, 通知用户的报文类型
*/
typedef enum {
AIOT_NTPRECV_LOCAL_TIME
} aiot_ntp_recv_type_t;
/**
* @brief ntp模块收到从网络上来的报文时, 通知用户的报文内容
*/
typedef struct {
/**
* @brief 报文内容所对应的报文类型, 更多信息请参考@ref aiot_ntp_recv_type_t
*/
aiot_ntp_recv_type_t type;
union {
/**
* @brief utc事件戳以及时区换算后的日期, 以 @ref AIOT_NTPOPT_TIME_ZONE 设置的时区为准
*/
struct {
uint64_t timestamp;
uint32_t year;
uint32_t mon;
uint32_t day;
uint32_t hour;
uint32_t min;
uint32_t sec;
uint32_t msec;
} local_time;
} data;
} aiot_ntp_recv_t;
/**
* @brief ntp模块收到从网络上来的报文时, 通知用户所调用的数据回调函数
*
* @param[in] handle ntp会话句柄
* @param[in] packet ntp消息结构体, 存放收到的ntp报文内容
* @param[in] userdata 用户上下文
*
* @return void
*/
typedef void (* aiot_ntp_recv_handler_t)(void *handle,
const aiot_ntp_recv_t *packet, void *userdata);
/**
* @brief ntp内部事件类型
*/
typedef enum {
/**
* @brief 收到的ntp应答中字段不合法
*/
AIOT_NTPEVT_INVALID_RESPONSE,
/**
* @brief 收到的ntp应答中时间字段格式错误
*/
AIOT_NTPEVT_INVALID_TIME_FORMAT,
} aiot_ntp_event_type_t;
/**
* @brief NTP内部事件
*/
typedef struct {
/**
* @brief NTP内部事件类型. 更多信息请参考@ref aiot_ntp_event_type_t
*
*/
aiot_ntp_event_type_t type;
} aiot_ntp_event_t;
/**
* @brief ntp事件回调函数
*
* @details
*
* 当NTP内部事件被触发时, 调用此函数
*
*/
typedef void (*aiot_ntp_event_handler_t)(void *handle, const aiot_ntp_event_t *event, void *userdata);
/**
* @brief @ref aiot_ntp_setopt 接口的option参数可选值.
*
* @details 下文每个选项中的数据类型, 指的是@ref aiot_ntp_setopt 中, data参数的数据类型
*
* 1. data的数据类型是char *时, 以配置@ref AIOT_NTPOPT_MQTT_HANDLE 为例:
*
* void *mqtt_handle = aiot_mqtt_init();
* aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_MQTT_HANDLE, mqtt_handle);
*
* 2. data的数据类型是其他数据类型时, 以配置@ref AIOT_NTPOPT_TIME_ZONE 为例:
*
* int8_t time_zone = 8;
* aiot_mqtt_setopt(ntp_handle, AIOT_NTPOPT_TIME_ZONE, (void *)&time_zone);
*/
typedef enum {
/**
* @brief ntp会话 需要的MQTT句柄, 需要先建立MQTT连接, 再设置MQTT句柄
*
* @details
*
* 数据类型: (void *)
*/
AIOT_NTPOPT_MQTT_HANDLE,
/**
* @brief ntp会话 获取到utc时间后会根据此时区值转换成本地时间, 再通过 @ref aiot_ntp_recv_handler_t 通知
*
* @details
*
* 取值示例: 东8区, 取值为8; 西3区, 取值为-3
*
* 数据类型: (int8_t *)
*/
AIOT_NTPOPT_TIME_ZONE,
/**
* @brief 设置回调, 它在SDK收到网络报文的时候被调用, 告知用户
*
* @details
*
* 数据类型: ( @ref aiot_ntp_recv_handler_t )
*/
AIOT_NTPOPT_RECV_HANDLER,
/**
* @brief ntp内部发生的事件会从此回调函数进行通知
*
* @details
*
* 数据类型: ( @ref aiot_ntp_event_handler_t )
*/
AIOT_NTPOPT_EVENT_HANDLER,
/**
* @brief 用户需要SDK暂存的上下文
*
* @details 这个上下文指针会在 AIOT_NTPOPT_RECV_HANDLER 和 AIOT_NTPOPT_EVENT_HANDLER 设置的回调被调用时, 由SDK传给用户
*
* 数据类型: (void *)
*/
AIOT_NTPOPT_USERDATA,
/**
* @brief 销毁ntp实例时, 等待其他api执行完毕的时间
*
* @details
*
* 当调用@ref aiot_ntp_deinit 销毁NTP实例时, 若继续调用其他aiot_ntp_xxx API, API会返回@ref STATE_USER_INPUT_EXEC_DISABLED 错误
*
* 此时, 用户应该停止调用其他aiot_ntp_xxx API
*
* 数据类型: (uint32_t *) 默认值: (2 * 1000) ms
*/
AIOT_NTPOPT_DEINIT_TIMEOUT_MS,
AIOT_NTPOPT_MAX
} aiot_ntp_option_t;
/**
* @brief 创建ntp会话实例, 并以默认值配置会话参数
*
* @return void *
* @retval 非NULL ntp实例的句柄
* @retval NULL 初始化失败, 一般是内存分配失败导致
*
*/
void *aiot_ntp_init(void);
/**
* @brief 配置ntp会话
*
* @details
*
* 常见的配置项如下
*
* + `AIOT_NTPOPT_MQTT_HANDLE`: 已建立连接的MQTT会话句柄
*
* + `AIOT_NTPOPT_TIME_ZONE`: 时区设置, SDK会将收到的UTC时间按配置的时区进行转换
*
* + `AIOT_NTPOPT_RECV_HANDLER`: 时间数据接收回调函数, SDK将UTC时间转换完成后, 通过此回调函数输出
*
* @param[in] handle ntp会话句柄
* @param[in] option 配置选项, 更多信息请参考@ref aiot_ntp_option_t
* @param[in] data 配置选项数据, 更多信息请参考@ref aiot_ntp_option_t
*
* @return int32_t
* @retval <STATE_SUCCESS 参数配置失败
* @retval >=STATE_SUCCESS 参数配置成功
*
*/
int32_t aiot_ntp_setopt(void *handle, aiot_ntp_option_t option, void *data);
/**
* @brief 结束ntp会话, 销毁实例并回收资源
*
* @param[in] handle 指向ntp会话句柄的指针
*
* @return int32_t
* @retval <STATE_SUCCESS 执行失败
* @retval >=STATE_SUCCESS 执行成功
*
*/
int32_t aiot_ntp_deinit(void **handle);
/**
* @brief 向ntp服务器发送ntp消息请求
*
* @details
*
* 发送NTP请求, 然后SDK会调用通过 @ref aiot_ntp_setopt 配置的 @ref AIOT_NTPOPT_RECV_HANDLER 回调函数, 通知用户当前的时间
*
* @param handle ntp会话句柄
*
* @return int32_t
* @retval <STATE_SUCCESS 请求发送失败
* @retval >=STATE_SUCCESS 请求发送成功
*/
int32_t aiot_ntp_send_request(void *handle);
#if defined(__cplusplus)
}
#endif
#endif /* __AIOT_NTP_API_H__ */

View File

@@ -0,0 +1,58 @@
/**
* @file ntp_private.h
* @brief ntp模块内部的宏定义和数据结构声明, 不面向其它模块, 更不面向用户
*
* @copyright Copyright (C) 2015-2020 Alibaba Group Holding Limited
*
*/
#ifndef __NTP_PRIVATE_H__
#define __NTP_PRIVATE_H__
#if defined(__cplusplus)
extern "C" {
#endif
/* 用这种方式包含标准C库的头文件 */
#include "core_stdinc.h"
/* TODO: 这一段列出需要包含SDK其它模块头文件, 与上一段落以1个空行隔开 */
#include "aiot_state_api.h"
#include "aiot_sysdep_api.h"
#include "aiot_ntp_api.h" /* 内部头文件是用户可见头文件的超集 */
/* TODO: 定义ntp模块内部的会话句柄结构体, SDK用户不可见, 只能得到void *handle类型的指针 */
typedef struct {
aiot_sysdep_portfile_t *sysdep; /* 底层依赖回调合集的引用指针 */
void *mqtt_handle;
int8_t time_zone;
uint32_t deinit_timeout_ms;
aiot_ntp_recv_handler_t recv_handler; /* 组件从协议栈读到内容时, 通知用户的回调 */
aiot_ntp_event_handler_t event_handler;
void *userdata; /* 组件调用以上2个 ntp_handler 时的入参之一 */
/*---- 以上都是用户在API可配 ----*/
void *data_mutex;
uint8_t exec_enabled;
uint32_t exec_count;
} ntp_handle_t;
#define NTP_MODULE_NAME "ntp" /* 用于内存统计的模块名字符串 */
#define NTP_DEFAULT_DEINIT_TIMEOUT_MS (2 * 1000)
#define NTP_DEFAULT_TIME_ZONE (0)
#define NTP_REQUEST_TOPIC_FMT "/ext/ntp/%s/%s/request"
#define NTP_REQUEST_PAYLOAD_FMT "{\"deviceSendTime\":\"%s\"}"
#define NTP_RESPONSE_TOPIC_FMT "/ext/ntp/%s/%s/response"
#define NTP_DEINIT_INTERVAL_MS (100)
#if defined(__cplusplus)
}
#endif
#endif /* __NTP_PRIVATE_H__ */