Skip to content

Commit

Permalink
Rework Thread attachement event and unify implementation (project-chi…
Browse files Browse the repository at this point in the history
…p#19474)

* Rework Thread attachement event and unify the event to the genericThreadStackManager

* replace 0 by false

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and jepenven-silabs committed Jun 14, 2022
1 parent 4d54ca7 commit 2cdc71a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 87 deletions.
19 changes: 0 additions & 19 deletions src/platform/ESP32/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,5 @@ void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStop()
// Intentionally empty.
}

void ThreadStackManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
Internal::GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::_OnPlatformEvent(event);

if (event->Type == DeviceEventType::kThreadStateChange && event->ThreadStateChange.RoleChanged)
{
const bool isAttached = IsThreadAttached();
VerifyOrReturn(isAttached != mIsAttached);
ChipDeviceEvent attachEvent;
attachEvent.Type = DeviceEventType::kThreadConnectivityChange;
attachEvent.ThreadConnectivityChange.Result = isAttached ? kConnectivity_Established : kConnectivity_Lost;

CHIP_ERROR error = PlatformMgr().PostEvent(&attachEvent);
VerifyOrReturn(error == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, error.Format()));
mIsAttached = isAttached;
}
}

} // namespace DeviceLayer
} // namespace chip
3 changes: 0 additions & 3 deletions src/platform/ESP32/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,12 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
void _ProcessThreadActivity();
void _OnCHIPoBLEAdvertisingStart();
void _OnCHIPoBLEAdvertisingStop();
void _OnPlatformEvent(const ChipDeviceEvent * event);

private:
friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void);
friend ThreadStackManagerImpl & ::chip::DeviceLayer::ThreadStackMgrImpl(void);
static ThreadStackManagerImpl sInstance;
ThreadStackManagerImpl() = default;

bool mIsAttached = false;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,41 +209,38 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnPlatformEvent(const
}
}
#endif
Impl()->LockThreadStack();

#if CHIP_DETAIL_LOGGING

LogOpenThreadStateChange(mOTInst, event->ThreadStateChange.OpenThread.Flags);

#endif // CHIP_DETAIL_LOGGING

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
if (event->ThreadStateChange.RoleChanged || event->ThreadStateChange.AddressChanged)
bool isThreadAttached = Impl()->_IsThreadAttached();
// Avoid sending muliple events if the attachement state didn't change (Child->router or disable->Detached)
if (event->ThreadStateChange.RoleChanged && (isThreadAttached != mIsAttached))
{
bool isInterfaceUp;
isInterfaceUp = GenericThreadStackManagerImpl_OpenThread<ImplClass>::IsThreadInterfaceUpNoLock();
// Post an event signaling the change in Thread interface connectivity state.
ChipDeviceEvent attachEvent;
attachEvent.Clear();
attachEvent.Type = DeviceEventType::kThreadConnectivityChange;
attachEvent.ThreadConnectivityChange.Result = (isThreadAttached) ? kConnectivity_Established : kConnectivity_Lost;
CHIP_ERROR status = PlatformMgr().PostEvent(&attachEvent);
if (status == CHIP_NO_ERROR)
{
ChipDeviceEvent event;
event.Clear();
event.Type = DeviceEventType::kThreadConnectivityChange;
event.ThreadConnectivityChange.Result = (isInterfaceUp) ? kConnectivity_Established : kConnectivity_Lost;
CHIP_ERROR status = PlatformMgr().PostEvent(&event);
if (status != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format());
}
mIsAttached = isThreadAttached;
}

// Refresh Multicast listening
if (GenericThreadStackManagerImpl_OpenThread<ImplClass>::IsThreadAttachedNoLock())
else
{
ChipLogDetail(DeviceLayer, "Thread Attached updating Multicast address");
Server::GetInstance().RejoinExistingMulticastGroups();
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format());
}
}

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
if (event->ThreadStateChange.AddressChanged && isThreadAttached)
{
// Refresh Multicast listening
ChipLogDetail(DeviceLayer, "Thread Attached updating Multicast address");
Server::GetInstance().RejoinExistingMulticastGroups();
}
#endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Impl()->UnlockThreadStack();

#if CHIP_DETAIL_LOGGING
LogOpenThreadStateChange(mOTInst, event->ThreadStateChange.OpenThread.Flags);
#endif // CHIP_DETAIL_LOGGING
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class GenericThreadStackManagerImpl_OpenThread

otInstance * mOTInst;
uint64_t mOverrunCount = 0;
bool mIsAttached = false;

NetworkCommissioning::ThreadDriver::ScanCallback * mpScanCallback;
NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * mpConnectCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void GenericThreadStackManagerImpl_OpenThread_LwIP<ImplClass>::UpdateThreadInter
LOCK_TCPIP_CORE();
Impl()->LockThreadStack();

// Determine whether the device is attached to a Thread network.
// Determine whether the device Thread interface is up..
isInterfaceUp = GenericThreadStackManagerImpl_OpenThread<ImplClass>::IsThreadInterfaceUpNoLock();

// If needed, adjust the link state of the LwIP netif to reflect the state of the OpenThread stack.
Expand All @@ -155,19 +155,6 @@ void GenericThreadStackManagerImpl_OpenThread_LwIP<ImplClass>::UpdateThreadInter
netif_set_link_down(mNetIf);
}

// Post an event signaling the change in Thread interface connectivity state.
{
ChipDeviceEvent event;
event.Clear();
event.Type = DeviceEventType::kThreadConnectivityChange;
event.ThreadConnectivityChange.Result = (isInterfaceUp) ? kConnectivity_Established : kConnectivity_Lost;
CHIP_ERROR status = PlatformMgr().PostEvent(&event);
if (status != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format());
}
}

// Presume the interface addresses are also changing.
addrChange = true;
}
Expand Down
21 changes: 0 additions & 21 deletions src/platform/Zephyr/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,5 @@ void ThreadStackManagerImpl::_UnlockThreadStack()
openthread_api_mutex_unlock(openthread_get_default_context());
}

void ThreadStackManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
Internal::GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::_OnPlatformEvent(event);

if (event->Type == DeviceEventType::kThreadStateChange && event->ThreadStateChange.RoleChanged)
{
const bool isAttached = IsThreadAttached();
VerifyOrReturn(isAttached != mIsAttached);

ChipDeviceEvent attachEvent;
attachEvent.Type = DeviceEventType::kThreadConnectivityChange;
attachEvent.ThreadConnectivityChange.Result = isAttached ? kConnectivity_Established : kConnectivity_Lost;

CHIP_ERROR error = PlatformMgr().PostEvent(&attachEvent);
VerifyOrReturn(error == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, error.Format()));

mIsAttached = isAttached;
}
}

} // namespace DeviceLayer
} // namespace chip
3 changes: 0 additions & 3 deletions src/platform/Zephyr/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
// ===== Methods that override the GenericThreadStackManagerImpl_OpenThread abstract interface.

void _ProcessThreadActivity() {}
void _OnPlatformEvent(const ChipDeviceEvent * event);

//} // namespace Internal

Expand All @@ -87,8 +86,6 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
static ThreadStackManagerImpl sInstance;

// ===== Private members for use by this class only.

bool mIsAttached = false;
};

/**
Expand Down

0 comments on commit 2cdc71a

Please sign in to comment.