Skip to content

Commit f189ec0

Browse files
authored
fix(cloud): defer meta report and align timer with time sync (#561)
Report device meta after time sync via delayed work; publish EVENT_DEVICE_META_REPORT for timer init. Publish TIME_SYNC earlier in OTA upgrade flow. TLS: use 4096 max fragment when ENABLE_MBEDTLS_SSL_MAX_CONTENT_LEN is set. Bump ESP32 platform ref; ESP32 board and switch_demo Kconfig; ignore .clangd.
1 parent 4a60bcf commit f189ec0

8 files changed

Lines changed: 70 additions & 15 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ __pycache__
33
.tags
44
.env.json
55
.cache
6+
.clangd
67

78
CMakeCache.txt
89
/CMakeFiles
@@ -28,4 +29,3 @@ dist
2829
/dist
2930
*monitor.log
3031

31-
/.omc

boards/ESP32/config/ESP32.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
CONFIG_BOARD_CHOICE_ESP32=y
1+
CONFIG_BOARD_CHOICE_ESP32=y
2+
CONFIG_ENABLE_MBEDTLS_SSL_MAX_CONTENT_LEN=4096

platform/platform_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ platforms:
2222
- name: ESP32
2323
repo: https://github.com/tuya/TuyaOpen-esp32
2424
branch: master
25-
commit: 5763a03c9979c61272b5331393fff07d6b4ff6b0
25+
commit: 22a2b3e8ae8a3aa6d3d43c206dff63b27a74e4de
2626

2727
- name: LN882H
2828
repo: https://github.com/tuya/TuyaOpen-ln882h

src/tal_system/include/tal_event_info.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extern "C" {
2323
/***********************************************************
2424
************************macro define************************
2525
***********************************************************/
26+
/*
27+
event name max length is 16, EVENT_NAME_MAX_LEN is defined in tal_event.h
28+
*/
29+
2630
#define EVENT_REBOOT_REQ \
2731
"dev.reboot.req" // device health check reboot request, application should subscribe it if needed
2832
#define EVENT_REBOOT_ACK "dev.reboot.ack" // device health check reboot ack, application should publish when it ready
@@ -37,6 +41,8 @@ extern "C" {
3741
#define EVENT_TIME_SYNC "time.sync" // time sync
3842
#define EVENT_LINK_ACTIVATE "link.activate" // linkage got activate info
3943

44+
#define EVENT_DEVICE_META_REPORT "dev.meta.rpt" // device meta report success
45+
4046
/***********************************************************
4147
***********************typedef define***********************
4248
***********************************************************/

src/tuya_cloud_service/cloud/tuya_device_meta.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "atop_base.h"
1111
#include "tal_log.h"
12+
#include "tal_workq_service.h"
13+
#include "tuya_cloud_types.h"
1214
#include "tuya_iot.h"
1315
#include "tal_api.h"
1416

@@ -28,6 +30,8 @@
2830
typedef struct {
2931
cJSON *json;
3032
MUTEX_HANDLE mutex;
33+
34+
DELAYED_WORK_HANDLE delayed_work;
3135
} tuya_device_meta_t;
3236

3337
/***********************************************************
@@ -44,14 +48,52 @@ static tuya_device_meta_t s_meta = {0};
4448
***********************function define**********************
4549
***********************************************************/
4650

51+
static void __device_meta_event_workq(void *data)
52+
{
53+
(void)data;
54+
55+
OPERATE_RET rt = OPRT_OK;
56+
57+
rt = tuya_device_meta_add_number("timerCapability", 1);
58+
if (OPRT_OK != rt) {
59+
PR_ERR("add timerCapability failed:%d", rt);
60+
return;
61+
}
62+
63+
rt = tuya_device_meta_report();
64+
if (OPRT_OK != rt) {
65+
PR_ERR("report meta failed:%d", rt);
66+
return;
67+
}
68+
69+
PR_DEBUG("device meta report success");
70+
71+
tal_event_publish(EVENT_DEVICE_META_REPORT, NULL);
72+
73+
PR_DEBUG("device meta report success, event publish");
74+
75+
tal_workq_cancel_delayed(s_meta.delayed_work);
76+
s_meta.delayed_work = NULL;
77+
}
78+
4779
static int __device_meta_event_cb(void *data)
4880
{
4981
(void)data;
5082

83+
OPERATE_RET rt = OPRT_OK;
84+
5185
/* Add common default device meta here. Meta for other features may be added
5286
* after tuya_iot_init() and before tuya_iot_start(). */
53-
tuya_device_meta_add_number("timerCapability", 1);
54-
tuya_device_meta_report();
87+
rt = tal_workq_init_delayed(WORKQ_SYSTEM, __device_meta_event_workq, NULL, &s_meta.delayed_work);
88+
if (OPRT_OK != rt) {
89+
PR_ERR("init delayed work failed:%d", rt);
90+
return -1;
91+
}
92+
rt = tal_workq_start_delayed(s_meta.delayed_work, 5*1000, LOOP_CYCLE);
93+
if (OPRT_OK != rt) {
94+
PR_ERR("start delayed work failed:%d", rt);
95+
return -1;
96+
}
5597

5698
return 0;
5799
}
@@ -70,7 +112,7 @@ OPERATE_RET tuya_device_meta_init(void)
70112
return rt;
71113
}
72114

73-
tal_event_subscribe(EVENT_MQTT_CONNECTED, "tuya_device_meta", __device_meta_event_cb,
115+
tal_event_subscribe(EVENT_TIME_SYNC, "tuya_device_meta", __device_meta_event_cb,
74116
SUBSCRIBE_TYPE_ONETIME);
75117

76118
return OPRT_OK;
@@ -228,7 +270,8 @@ OPERATE_RET tuya_device_meta_report(void)
228270
tal_mutex_lock(s_meta.mutex);
229271

230272
/* 1. Build payload: update timestamp and serialize */
231-
cJSON *new_time = cJSON_CreateNumber((double)tal_time_get_posix());
273+
TIME_T timestamp = tal_time_get_posix();
274+
cJSON *new_time = cJSON_CreateNumber((double)timestamp);
232275
if (new_time == NULL) {
233276
rt = OPRT_MALLOC_FAILED;
234277
goto __EXIT;
@@ -251,7 +294,7 @@ OPERATE_RET tuya_device_meta_report(void)
251294
atop_request.devid = client->activate.devid;
252295
atop_request.key = client->activate.seckey;
253296
atop_request.path = "/d.json";
254-
atop_request.timestamp = (uint32_t)tal_time_get_posix();
297+
atop_request.timestamp = (uint32_t)timestamp;
255298
atop_request.api = DEVICE_META_SAVE_API;
256299
atop_request.version = "1.0";
257300
atop_request.data = buffer;

src/tuya_cloud_service/cloud/tuya_device_timer.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,27 +552,29 @@ static void __timer_check_timer_cb(TIMER_ID timer_id, void *arg)
552552
(void)arg;
553553

554554
if (s_ctx.inited == false || s_ctx.timer_task_list.mutex == NULL) {
555+
PR_ERR("device timer not initialized");
555556
return;
556557
}
557558

558559
if (tal_time_check_time_sync() != OPRT_OK) {
560+
PR_ERR("time sync failed");
559561
return;
560562
}
561563

562564
if (tuya_slist_empty(&s_ctx.timer_task_list.head)) {
565+
PR_ERR("timer task list is empty");
563566
return;
564567
}
565568

566569
POSIX_TM_S current_time = {0};
567570
rt = tal_time_get(&current_time);
568571
if (rt != OPRT_OK) {
572+
PR_ERR("tal_time_get failed");
569573
return;
570574
}
571575

572576
tal_mutex_lock(s_ctx.timer_task_list.mutex);
573577

574-
575-
576578
SLIST_HEAD *pos = NULL;
577579
tuya_timer_item_t *item = NULL;
578580
SLIST_FOR_EACH_ENTRY(item, tuya_timer_item_t, pos, &s_ctx.timer_task_list.head, node) {
@@ -1114,7 +1116,7 @@ int tuya_device_timer_init(void)
11141116
tuya_mqtt_dispatch_register(PRO_DEV_DA_RESP, "timer_full_syn", "device timer full sync", __on_timer_full_syn_ack_callback, NULL);
11151117
tuya_mqtt_dispatch_register(PRO_IOT_DA_REQ, "timer_sync", "device timer sync", __on_timer_sync_callback, NULL);
11161118

1117-
tal_event_subscribe(EVENT_MQTT_CONNECTED, "tuya_device_timer", __device_timer_task_event_cb, SUBSCRIBE_TYPE_ONETIME);
1119+
tal_event_subscribe(EVENT_DEVICE_META_REPORT, "tuya_device_timer", __device_timer_task_event_cb, SUBSCRIBE_TYPE_ONETIME);
11181120
tal_event_subscribe(EVENT_RESET, "tuya_device_timer", __device_timer_reset_event_cb, SUBSCRIBE_TYPE_ONETIME);
11191121

11201122
// Load from kv

src/tuya_cloud_service/cloud/tuya_iot.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ static void matop_upgrade_info_on(atop_base_response_t *response, void *user_dat
345345
client->event.value.asInteger = response->t;
346346
iot_dispatch_event(client);
347347

348+
/* Send timestamp sync event*/
349+
tal_event_publish(EVENT_TIME_SYNC, (void *)client);
350+
348351
/* Param verify */
349352
if (response->result == NULL) {
350353
return;
@@ -356,9 +359,6 @@ static void matop_upgrade_info_on(atop_base_response_t *response, void *user_dat
356359
client->event.value.asJSON = response->result;
357360
iot_dispatch_event(client);
358361

359-
/* Send timestamp sync event*/
360-
tal_event_publish(EVENT_TIME_SYNC, (void *)client);
361-
362362
TUYA_CALL_ERR_LOG(tuya_ota_start(response->result));
363363
}
364364

@@ -528,7 +528,7 @@ static int run_state_mqtt_connect_start(tuya_iot_client_t *client)
528528
tuya_mqtt_protocol_register(&client->mqctx, PRO_UPGD_REQ, mqtt_service_upgrade_notify_on, client);
529529
tuya_mqtt_protocol_register(&client->mqctx, PRO_MQ_DPCACHE_NOTIFY, mqtt_atop_dp_cache_notify_cb, client);
530530
tuya_mqtt_protocol_register(&client->mqctx, PRO_RTC_REQ, mqtt_rtc_req_notify_cb, client);
531-
531+
532532
return rt;
533533
}
534534

src/tuya_cloud_service/tls/tuya_tls.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
*/
1313

14+
#include "tuya_cloud_types.h"
1415
#include "tuya_tls.h"
1516

1617
#if !defined(MBEDTLS_CONFIG_FILE)
@@ -613,6 +614,8 @@ OPERATE_RET tuya_tls_connect(tuya_tls_hander p_tls_handler, char *hostname, int
613614
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
614615
#if (MBEDTLS_SSL_MAX_CONTENT_LEN >= 4096)
615616
mbedtls_ssl_conf_max_frag_len(p_conf_ctx, MBEDTLS_SSL_MAX_FRAG_LEN_4096);
617+
#elif defined(ENABLE_MBEDTLS_SSL_MAX_CONTENT_LEN) && (ENABLE_MBEDTLS_SSL_MAX_CONTENT_LEN>=4096)
618+
mbedtls_ssl_conf_max_frag_len(p_conf_ctx, MBEDTLS_SSL_MAX_FRAG_LEN_4096);
616619
#else
617620
mbedtls_ssl_conf_max_frag_len(p_conf_ctx, MBEDTLS_SSL_MAX_FRAG_LEN_1024);
618621
#endif

0 commit comments

Comments
 (0)