diff --git a/components/openthread/Kconfig b/components/openthread/Kconfig index 1742ccd239..90be8b4f2d 100644 --- a/components/openthread/Kconfig +++ b/components/openthread/Kconfig @@ -277,4 +277,13 @@ menu "OpenThread" default n help Only used for Thread1.2 certification + + config OPENTHREAD_TIME_SYNC + bool "Enable the time synchronization service feature" + depends on OPENTHREAD_ENABLED + default n + help + Select this option to enable time synchronization feature, the devices in the same Thread network could + sync to the same network time. + endmenu diff --git a/components/openthread/private_include/openthread-core-esp32x-ftd-config.h b/components/openthread/private_include/openthread-core-esp32x-ftd-config.h index 166edab9aa..88b68a0908 100644 --- a/components/openthread/private_include/openthread-core-esp32x-ftd-config.h +++ b/components/openthread/private_include/openthread-core-esp32x-ftd-config.h @@ -517,7 +517,6 @@ #define OPENTHREAD_CONFIG_OPERATIONAL_DATASET_AUTO_INIT 1 #endif - /** * * Define as 1 to enable support for allocating message pool buffer in PSRAM @@ -536,4 +535,14 @@ #define OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT 1 #endif +/** + * @def OPENTHREAD_CONFIG_TIME_SYNC_ENABLE + * + * Define as 1 to enable the time synchronization service feature. + * + */ +#if CONFIG_OPENTHREAD_TIME_SYNC +#define OPENTHREAD_CONFIG_TIME_SYNC_ENABLE 1 +#endif + #define OPENTHREAD_FTD 1 diff --git a/components/openthread/private_include/openthread-core-esp32x-mtd-config.h b/components/openthread/private_include/openthread-core-esp32x-mtd-config.h index f84a6f80eb..537d972611 100644 --- a/components/openthread/private_include/openthread-core-esp32x-mtd-config.h +++ b/components/openthread/private_include/openthread-core-esp32x-mtd-config.h @@ -242,4 +242,14 @@ */ #define OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE CONFIG_OPENTHREAD_DNS_CLIENT +/** + * @def OPENTHREAD_CONFIG_TIME_SYNC_ENABLE + * + * Define as 1 to enable the time synchronization service feature. + * + */ +#if CONFIG_OPENTHREAD_TIME_SYNC +#define OPENTHREAD_CONFIG_TIME_SYNC_ENABLE 1 +#endif + #define OPENTHREAD_MTD 1 diff --git a/components/openthread/src/port/esp_openthread_radio.c b/components/openthread/src/port/esp_openthread_radio.c index 6ad310fd03..314b6ab70b 100644 --- a/components/openthread/src/port/esp_openthread_radio.c +++ b/components/openthread/src/port/esp_openthread_radio.c @@ -711,15 +711,33 @@ void IRAM_ATTR esp_ieee802154_receive_sfd_done(void) void IRAM_ATTR esp_ieee802154_transmit_sfd_done(uint8_t *frame) { assert(frame == (uint8_t *)&s_transmit_psdu || frame == s_enhack); -#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - otRadioFrame ot_frame; - ot_frame.mPsdu = frame + 1; - ot_frame.mLength = frame[0]; +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE if (s_csl_period > 0) { + otRadioFrame ot_frame; + ot_frame.mPsdu = frame + 1; + ot_frame.mLength = frame[0]; + otMacFrameSetCslIe(&ot_frame, s_csl_period, get_csl_phase()); } #endif + +#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE + if (frame == (uint8_t *)&s_transmit_psdu && s_transmit_frame.mInfo.mTxInfo.mIeInfo->mTimeIeOffset != 0) + { + uint8_t *p_time_ie = s_transmit_frame.mPsdu + s_transmit_frame.mInfo.mTxInfo.mIeInfo->mTimeIeOffset; + uint64_t time = (uint64_t)((int64_t)otPlatTimeGet() + s_transmit_frame.mInfo.mTxInfo.mIeInfo->mNetworkTimeOffset); + + *p_time_ie = s_transmit_frame.mInfo.mTxInfo.mIeInfo->mTimeSyncSeq; + + *(++p_time_ie) = (uint8_t)(time & 0xff); + for (uint8_t i = 1; i < sizeof(uint64_t); i++) + { + time = time >> 8; + *(++p_time_ie) = (uint8_t)(time & 0xff); + } + } +#endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE } void IRAM_ATTR esp_ieee802154_energy_detect_done(int8_t power)