feat(openthread): Add dataset changed event and post it in state change callback

This commit is contained in:
WanqQixiang
2024-10-31 14:53:33 +08:00
parent 1acc70f476
commit 77656e2046
2 changed files with 44 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -46,6 +46,7 @@ typedef enum {
OPENTHREAD_EVENT_SET_DNS_SERVER, /*!< OpenThread stack set DNS server >*/ OPENTHREAD_EVENT_SET_DNS_SERVER, /*!< OpenThread stack set DNS server >*/
OPENTHREAD_EVENT_PUBLISH_MESHCOP_E, /*!< OpenThread stack start to publish meshcop-e service >*/ OPENTHREAD_EVENT_PUBLISH_MESHCOP_E, /*!< OpenThread stack start to publish meshcop-e service >*/
OPENTHREAD_EVENT_REMOVE_MESHCOP_E, /*!< OpenThread stack start to remove meshcop-e service >*/ OPENTHREAD_EVENT_REMOVE_MESHCOP_E, /*!< OpenThread stack start to remove meshcop-e service >*/
OPENTHREAD_EVENT_DATASET_CHANGED, /*!< OpenThread dataset changed >*/
} esp_openthread_event_t; } esp_openthread_event_t;
/** /**
@ -63,6 +64,24 @@ typedef struct {
otDeviceRole current_role; /*!< Current Thread role */ otDeviceRole current_role; /*!< Current Thread role */
} esp_openthread_role_changed_event_t; } esp_openthread_role_changed_event_t;
/**
* @brief OpenThread dataset type
*
*/
typedef enum {
OPENTHREAD_ACTIVE_DATASET, /*!< Active dataset */
OPENTHREAD_PENDING_DATASET, /*!< Pending dataset */
} esp_openthread_dataset_type_t;
/**
* @brief OpenThread dataset changed event data
*
*/
typedef struct {
esp_openthread_dataset_type_t type; /*!< Dataset type */
otOperationalDataset new_dataset; /*!< New dataset */
} esp_openthread_dataset_changed_event_t;
/** /**
* This structure represents a context for a select() based mainloop. * This structure represents a context for a select() based mainloop.
* *

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -80,6 +80,21 @@ exit:
s_previous_role = role; s_previous_role = role;
} }
static void handle_ot_dataset_change(esp_openthread_dataset_type_t type, otInstance *instance)
{
esp_openthread_dataset_changed_event_t event_data;
event_data.type = type;
memset(&event_data.new_dataset, 0, sizeof(event_data.new_dataset));
if (type == OPENTHREAD_ACTIVE_DATASET) {
(void)otDatasetGetActive(instance, &event_data.new_dataset);
} else if (type == OPENTHREAD_PENDING_DATASET) {
(void)otDatasetGetPending(instance, &event_data.new_dataset);
}
if (esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_DATASET_CHANGED, &event_data, sizeof(event_data), 0) != ESP_OK) {
ESP_LOGE(TAG, "Failed to post dataset changed event");
}
}
static void ot_state_change_callback(otChangedFlags changed_flags, void* ctx) static void ot_state_change_callback(otChangedFlags changed_flags, void* ctx)
{ {
OT_UNUSED_VARIABLE(ctx); OT_UNUSED_VARIABLE(ctx);
@ -99,6 +114,14 @@ static void ot_state_change_callback(otChangedFlags changed_flags, void* ctx)
if (changed_flags & OT_CHANGED_THREAD_NETIF_STATE) { if (changed_flags & OT_CHANGED_THREAD_NETIF_STATE) {
handle_ot_netif_state_change(instance); handle_ot_netif_state_change(instance);
} }
if (changed_flags & OT_CHANGED_ACTIVE_DATASET) {
handle_ot_dataset_change(OPENTHREAD_ACTIVE_DATASET, instance);
}
if (changed_flags & OT_CHANGED_PENDING_DATASET) {
handle_ot_dataset_change(OPENTHREAD_PENDING_DATASET, instance);
}
} }
esp_err_t esp_openthread_state_event_init(otInstance* instance) esp_err_t esp_openthread_state_event_init(otInstance* instance)