-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monitor connection to Border Router (CON-1415) #1153
Comments
These events are posted to Matter context at the state change callback of OpenThread. And there should not be a long delay for it (one minute is too long for event post). Could you check whether the post is successful at the state change callback? Note that the role change will not happen immediately after you shutdown the Thread Border Router, it will take some time for the thread end-device to know that its parent router disappears and select a new router or become a router/leader as it needs to detect whether there are other routers it can attached. If you want to know more information of the Thread state change. You can also try our OPENTHREAD_EVENT in ESP-IDF. |
Thank you, @wqx6, for the quick response and helpful hints! The ESP OpenThread events are indeed useful. However, some of them are already propagated by the
bool is_tbr_connected(otInstance *instance)
{
otExternalRouteConfig route_config;
otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
// Iterate over all external routes
while (otNetDataGetNextRoute(instance, &iterator, &route_config) == OT_ERROR_NONE)
{
if (route_config.mRloc16 != 0) {
// Found a valid external route
ESP_LOGI(TAG, "TBR detected: RLOC16 = 0x%04x, Prefix Length = %d", route_config.mRloc16, route_config.mPrefix.mLength);
return true;
}
}
return false;
} My primary goal is to indicate whether my device has a connection to a TBR or not. I understand that Thread is a robust protocol, and the network organizes itself. If a TBR gets disconnected, another one might be promoted, and this process takes time. Currently, the Any advice or suggestions on how to efficiently monitor the connection to a TBR would be greatly appreciated! |
Hi, the Thread End Device cannot immediately detect if the Thread Border Router (BR) gets disconnected, as the BR cannot notify other devices before disconnecting. The leader synchronizes with each router every 90 seconds and updates the router information in the network data. Therefore, if the Border Router disconnects, it may take up to 90 seconds for the leader to recognize the disconnection. (If the Border Router is the leader, additional time is required for another node to become the leader.) Once the leader detects the disconnection, it will update the OMR prefix in the network data. But you can get a callback when the network data is changed:
|
Thank you @wqx6, I used your code static void matter_api_tbr_check_connection()
{
static uint8_t connected = -1;
otBorderRouterConfig br_config;
otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
otError error = otNetDataGetNextOnMeshPrefix(esp_openthread_get_instance(), &iterator, &br_config);
if (error == OT_ERROR_NOT_FOUND && connected != 0)
{
connected = 0;
ESP_LOGW(TAG, "Thread Border Router is disconnected");
}
else if (error == OT_ERROR_NONE && connected != 1)
{
connected = 1;
ESP_LOGW(TAG, "Thread Border Router is connected");
}
} and integrated it into the event callback in static void matter_api_event_cb(const ChipDeviceEvent *event, intptr_t arg)
{
switch (event->Type)
{
case chip::DeviceLayer::DeviceEventType::kThreadConnectivityChange:
{
ESP_LOGI(TAG, "Thread connectivity changed");
matter_api_tbr_check_connection();
break;
}
case chip::DeviceLayer::DeviceEventType::kThreadStateChange:
{
if (event->ThreadStateChange.NetDataChanged)
{
ESP_LOGD(TAG, "Thread network data changed");
matter_api_tbr_check_connection();
}
break;
}
}
} Do you know if the OPENTHREAD_EVENT_LOST_IP6 and OPENTHREAD_EVENT_GOT_IP6 are always fired after the TBR is disconnected or connected? Or is it safer to use the netData change event? Also, I found a similar question here. As initially suggested, it seems the best option might be to get the IP address of the TBR and ping it periodically. Do you have any suggestions on how to implement this? |
The event callback in
esp_matter::start
provides useful information, such as whether the WiFi station has connected or disconnected. I'd like to achieve the same for Thread, with the goal to detect when the Border Router is unavailable or got disconnected. Typically, if there’s no OTBR, the device changes its role. Unfortunately, this is not every time the case. Sometimes, I can also monitor network and address changes. Currently, I use thekThreadStateChange
event to monitor changes:However, it sometimes takes over a minute before any events are triggered! Additionally, the events themselves are not very helpful. I would need to investigate specific APIs in OpenThread or Connectedhomeip. Is there a straightforward solution to this issue?
The text was updated successfully, but these errors were encountered: