feat(openthread): handle MeshCoP mDNS service in state change callback

This commit is contained in:
Xu Si Yu
2025-05-29 16:21:36 +08:00
parent 2b9d6c8820
commit ec25e21ae8
4 changed files with 76 additions and 1 deletions

View File

@@ -76,6 +76,14 @@ esp_netif_t *esp_openthread_get_backbone_netif(void);
*/
esp_err_t esp_openthread_set_meshcop_instance_name(const char *instance_name);
/**
* @brief Gets the meshcop(e) instance name.
*
* @return The instance name.
*
*/
const char* esp_openthread_get_meshcop_instance_name(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_openthread.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Publishes the OpenThread meshcop service in mDNS
*
* @param[in] instance_name Instance name of meshcop mDNS service
*
* @return
* - ESP_OK success
* - ESP_ERR_NO_MEM memory error
* - ESP_FAIL failed to add service
*
*/
esp_err_t esp_openthread_publish_meshcop_mdns(const char *instance_name);
/**
* @brief Removes the OpenThread meshcop service in mDNS
*
* @return
* - ESP_OK success
* - ESP_ERR_NO_MEM memory error
* - ESP_FAIL failed to remove service
*
*/
esp_err_t esp_openthread_remove_meshcop_mdns(void);
#ifdef __cplusplus
}
#endif

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -10,6 +10,11 @@
#include <esp_openthread.h>
#include <esp_openthread_types.h>
#define ESP_OPENTHREAD_BORDER_ROUTER_FLAG_OF_INTEREST \
(OT_CHANGED_THREAD_ROLE | OT_CHANGED_THREAD_EXT_PANID | OT_CHANGED_THREAD_NETWORK_NAME | \
OT_CHANGED_ACTIVE_DATASET | OT_CHANGED_THREAD_PARTITION_ID | OT_CHANGED_THREAD_BACKBONE_ROUTER_STATE | \
OT_CHANGED_PSKC)
#ifdef __cplusplus
extern "C" {
#endif

View File

@@ -10,7 +10,9 @@
#include <esp_check.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_openthread_border_router.h>
#include <esp_openthread_dns64.h>
#include <esp_openthread_meshcop_mdns.h>
#include <esp_openthread_netif_glue_priv.h>
#include <esp_openthread_radio.h>
#include <esp_openthread_state.h>
@@ -19,6 +21,19 @@
#define TAG "OT_STATE"
#if CONFIG_OPENTHREAD_BORDER_ROUTER
static void handle_ot_border_router_state_changed(otInstance* instance)
{
otDeviceRole role = otThreadGetDeviceRole(esp_openthread_get_instance());
if (role == OT_DEVICE_ROLE_CHILD || role == OT_DEVICE_ROLE_ROUTER || role == OT_DEVICE_ROLE_LEADER) {
esp_openthread_publish_meshcop_mdns(esp_openthread_get_meshcop_instance_name());
} else {
esp_openthread_remove_meshcop_mdns();
}
}
#endif
static void handle_ot_netif_state_change(otInstance* instance)
{
if (otIp6IsEnabled(instance)) {
@@ -123,6 +138,12 @@ static void ot_state_change_callback(otChangedFlags changed_flags, void* ctx)
return;
}
#if CONFIG_OPENTHREAD_BORDER_ROUTER
if (changed_flags & ESP_OPENTHREAD_BORDER_ROUTER_FLAG_OF_INTEREST) {
handle_ot_border_router_state_changed(instance);
}
#endif
if (changed_flags & OT_CHANGED_THREAD_ROLE) {
handle_ot_role_change(instance);
}