Skip to content

Add background event handling for CASE establish #24099

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

Merged
merged 47 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
acafb52
Add background event handling for CASE establish
mlepage-google Dec 9, 2022
3a80c45
Remove WIP code
mlepage-google Dec 15, 2022
9743633
Address some comments from code review
mlepage-google Jan 16, 2023
20759c4
Remove cruft from testing.
mlepage-google Jan 16, 2023
85d5c12
Remove some conditional compilation
mlepage-google Jan 17, 2023
dba0871
Remove some conditional compilation
mlepage-google Jan 17, 2023
bc6f1bf
Remove more conditional compilation
mlepage-google Jan 17, 2023
a0ec1e3
Move function back where it was
mlepage-google Jan 17, 2023
397c376
Add some documentation
mlepage-google Jan 17, 2023
2f10b38
Use platform new/delete
mlepage-google Jan 19, 2023
4227532
Undo changes that are merely reordering
mlepage-google Jan 19, 2023
3451a66
Undo changes that are merely reordering
mlepage-google Jan 19, 2023
847aebf
Remove include file fix (C/C++)
mlepage-google Jan 19, 2023
b0d65d8
Add documentation to background processing API
mlepage-google Jan 19, 2023
8ae5359
Use alternate fabrics table API
mlepage-google Jan 19, 2023
55b7846
Merge branch 'master' into async-sigma3
mlepage-google Jan 19, 2023
26b85bf
Merge branch 'master' into async-sigma3
mlepage-google Jan 19, 2023
1fdc61a
Merge branch 'master' into async-sigma3
mlepage-google Jan 20, 2023
6007254
Improve documentation
mlepage-google Jan 20, 2023
788355e
Add assertion
mlepage-google Jan 20, 2023
d15e187
Undo some unrelated cleanup
mlepage-google Jan 20, 2023
f784728
Update src/protocols/secure_channel/CASESession.cpp
mlepage-google Jan 20, 2023
0a1d6d1
Merge branch 'master' into async-sigma3
mlepage-google Jan 23, 2023
eebf6ff
Merge branch 'master' into async-sigma3
mlepage-google Jan 24, 2023
1797bd9
Ensure root cert buf keeps span
mlepage-google Jan 24, 2023
4cce9d8
Restyled by whitespace
restyled-commits Jan 24, 2023
4a491e0
Restyled by clang-format
restyled-commits Jan 24, 2023
1b0e186
Merge branch 'master' into async-sigma3
mlepage-google Jan 24, 2023
f4785ee
Add new functions to GenericPlatformManagerImpl
mlepage-google Jan 24, 2023
62fd9d7
Merge branch 'master' into async-sigma3
mlepage-google Jan 24, 2023
dc1d207
Attempt to fix build errors on some platforms
mlepage-google Jan 24, 2023
66357d3
Improving host test environment
mlepage-google Feb 2, 2023
160208e
Remove log statements and clean up
mlepage-google Feb 3, 2023
ef0a22e
Merge branch 'master' into async-sigma3
mlepage-google Feb 3, 2023
2b7c22a
Merge branch 'master' into async-sigma3
mlepage-google Feb 3, 2023
9b840f2
Update fake PlatformManagerImpl
mlepage-google Feb 3, 2023
e320b38
Merge branch 'master' into async-sigma3
mlepage-google Feb 7, 2023
f7c2027
Increase timeout on fake linux CI
mlepage-google Feb 7, 2023
04a7757
Merge branch 'master' into async-sigma3
mlepage-google Feb 8, 2023
f8905a0
Redo changes to make tests work
mlepage-google Feb 10, 2023
9ffd859
Merge branch 'master' into async-sigma3
mlepage-google Feb 10, 2023
348383c
Undo SetSystemLayerForTesting
mlepage-google Feb 13, 2023
8759d39
Merge branch 'master' into async-sigma3
mlepage-google Feb 13, 2023
77ef3cc
Change fake linux tests timeout back to 15 mins
mlepage-google Feb 13, 2023
7f02ce7
Restyle
mlepage-google Feb 14, 2023
134fed0
Merge branch 'master' into async-sigma3
mlepage-google Feb 14, 2023
42c8fbf
Init/shutdown platform mgr in TestCASESession
mlepage-google Feb 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/platform/esp32/common/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
// this function will happen on the CHIP event loop thread, not the app_main thread.
PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));

// Start a task to run the CHIP Device background event loop.
ReturnErrorOnFailure(PlatformMgr().StartBackgroundEventLoopTask());

// Start a task to run the CHIP Device event loop.
return PlatformMgr().StartEventLoopTask();
}
Expand Down
13 changes: 6 additions & 7 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,12 @@ class DLL_EXPORT FabricTable
FabricId & outFabricId, NodeId & outNodeId, Crypto::P256PublicKey & outNocPubkey,
Crypto::P256PublicKey * outRootPublicKey = nullptr) const;

// Verifies credentials, using the provided root certificate.
// This call is done whenever a fabric is "directly" added
static CHIP_ERROR VerifyCredentials(const ByteSpan & noc, const ByteSpan & icac, const ByteSpan & rcac,
Credentials::ValidationContext & context, CompressedFabricId & outCompressedFabricId,
FabricId & outFabricId, NodeId & outNodeId, Crypto::P256PublicKey & outNocPubkey,
Crypto::P256PublicKey * outRootPublicKey = nullptr);
/**
* @brief Enables FabricInfo instances to collide and reference the same logical fabric (i.e Root Public Key + FabricId).
*
Expand Down Expand Up @@ -1093,13 +1099,6 @@ class DLL_EXPORT FabricTable
mStateFlags.HasAll(StateFlags::kIsPendingFabricDataPresent, StateFlags::kIsUpdatePending);
}

// Verifies credentials, using the provided root certificate.
// This call is done whenever a fabric is "directly" added
static CHIP_ERROR VerifyCredentials(const ByteSpan & noc, const ByteSpan & icac, const ByteSpan & rcac,
Credentials::ValidationContext & context, CompressedFabricId & outCompressedFabricId,
FabricId & outFabricId, NodeId & outNodeId, Crypto::P256PublicKey & outNocPubkey,
Crypto::P256PublicKey * outRootPublicKey);

// Validate an NOC chain at time of adding/updating a fabric (uses VerifyCredentials with additional checks).
// The `existingFabricId` is passed for UpdateNOC, and must match the Fabric, to make sure that we are
// not trying to change FabricID with UpdateNOC. If set to kUndefinedFabricId, we are doing AddNOC and
Expand Down
50 changes: 48 additions & 2 deletions src/include/platform/CHIPDeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* The priority of the chip task.
*/
#ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY 1
#define CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY 2
#endif

/**
Expand All @@ -91,6 +91,52 @@
#define CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE 100
#endif

/**
* CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
*
* Enable support for background event processing.
*/
#ifndef CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
#define CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING 0
#endif


/**
* CHIP_DEVICE_CONFIG_BG_TASK_NAME
*
* The name of the background task.
*/
#ifndef CHIP_DEVICE_CONFIG_BG_TASK_NAME
#define CHIP_DEVICE_CONFIG_BG_TASK_NAME "BG"
#endif

/**
* CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE
*
* The size (in bytes) of the background task stack.
*/
#ifndef CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE (6*1024)
#endif

/**
* CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY
*
* The priority of the background task.
*/
#ifndef CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY 1
#endif

/**
* CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE
*
* The maximum number of events that can be held in the chip background event queue.
*/
#ifndef CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE
#define CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE 1
#endif

/**
* CHIP_DEVICE_CONFIG_ENABLE_SED
*
Expand Down Expand Up @@ -638,7 +684,7 @@
* The priority of the OpenThread task.
*/
#ifndef CHIP_DEVICE_CONFIG_THREAD_TASK_PRIORITY
#define CHIP_DEVICE_CONFIG_THREAD_TASK_PRIORITY 2
#define CHIP_DEVICE_CONFIG_THREAD_TASK_PRIORITY 3
#endif

/**
Expand Down
10 changes: 5 additions & 5 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ enum OtaState
kOtaApplyFailed,
};

/**
* A pointer to a function that performs work asynchronously.
*/
typedef void (*AsyncWorkFunct)(intptr_t arg);

inline ConnectivityChange GetConnectivityChange(bool prevState, bool newState)
{
if (prevState == newState)
Expand All @@ -326,11 +331,6 @@ inline ConnectivityChange GetConnectivityChange(bool prevState, bool newState)
return kConnectivity_Lost;
}

/**
* A pointer to a function that performs work asynchronously.
*/
typedef void (*AsyncWorkFunct)(intptr_t arg);

} // namespace DeviceLayer
} // namespace chip

Expand Down
132 changes: 82 additions & 50 deletions src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class PlatformManager
* other.
*/
CHIP_ERROR InitChipStack();
void Shutdown();

void LockChipStack();
bool TryLockChipStack();
void UnlockChipStack();
#if CHIP_STACK_LOCK_TRACKING_ENABLED
bool IsChipStackLockedByCurrentThread() const;
#endif

CHIP_ERROR AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
void RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
void SetDelegate(PlatformManagerDelegate * delegate) { mDelegate = delegate; }
Expand Down Expand Up @@ -137,7 +146,16 @@ class PlatformManager
* processing, the callback function may be called (on the work item
* processing thread) before ScheduleWork returns.
*/
void ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg = 0);
CHIP_ERROR ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg = 0);

/*
* PostEvent can be called safely on any thread without locking the stack.
* When called from a thread that is not doing the stack work item
* processing, the event might get dispatched (on the work item processing
* thread) before PostEvent returns.
*/
[[nodiscard]] CHIP_ERROR PostEvent(const ChipDeviceEvent * event);
void PostEventOrDie(const ChipDeviceEvent * event);

/**
* Process work items until StopEventLoopTask is called. RunEventLoop will
Expand Down Expand Up @@ -183,23 +201,12 @@ class PlatformManager
* returns.
*/
CHIP_ERROR StopEventLoopTask();
void LockChipStack();
bool TryLockChipStack();
void UnlockChipStack();
void Shutdown();

#if CHIP_STACK_LOCK_TRACKING_ENABLED
bool IsChipStackLockedByCurrentThread() const;
#endif

/*
* PostEvent can be called safely on any thread without locking the stack.
* When called from a thread that is not doing the stack work item
* processing, the event might get dispatched (on the work item processing
* thread) before PostEvent returns.
*/
[[nodiscard]] CHIP_ERROR PostEvent(const ChipDeviceEvent * event);
void PostEventOrDie(const ChipDeviceEvent * event);
CHIP_ERROR ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg = 0);
CHIP_ERROR PostBackgroundEvent(const ChipDeviceEvent * event);
void RunBackgroundEventLoop();
CHIP_ERROR StartBackgroundEventLoopTask();
CHIP_ERROR StopBackgroundEventLoopTask();

private:
bool mInitialized = false;
Expand Down Expand Up @@ -306,13 +313,6 @@ class StackUnlock
namespace chip {
namespace DeviceLayer {

#if CHIP_STACK_LOCK_TRACKING_ENABLED
inline bool PlatformManager::IsChipStackLockedByCurrentThread() const
{
return static_cast<const ImplClass *>(this)->_IsChipStackLockedByCurrentThread();
}
#endif

inline CHIP_ERROR PlatformManager::InitChipStack()
{
// NOTE: this is NOT thread safe and cannot be as the chip stack lock is prepared by
Expand All @@ -332,6 +332,40 @@ inline CHIP_ERROR PlatformManager::InitChipStack()
return err;
}

/**
* @brief
* Shuts down and cleans up the main objects in the CHIP stack.
* This DOES NOT stop the chip thread or event queue from running.
*
*/
inline void PlatformManager::Shutdown()
{
static_cast<ImplClass *>(this)->_Shutdown();
mInitialized = false;
}

inline void PlatformManager::LockChipStack()
{
static_cast<ImplClass *>(this)->_LockChipStack();
}

inline bool PlatformManager::TryLockChipStack()
{
return static_cast<ImplClass *>(this)->_TryLockChipStack();
}

inline void PlatformManager::UnlockChipStack()
{
static_cast<ImplClass *>(this)->_UnlockChipStack();
}

#if CHIP_STACK_LOCK_TRACKING_ENABLED
inline bool PlatformManager::IsChipStackLockedByCurrentThread() const
{
return static_cast<const ImplClass *>(this)->_IsChipStackLockedByCurrentThread();
}
#endif

inline CHIP_ERROR PlatformManager::AddEventHandler(EventHandlerFunct handler, intptr_t arg)
{
return static_cast<ImplClass *>(this)->_AddEventHandler(handler, arg);
Expand All @@ -352,9 +386,21 @@ inline void PlatformManager::HandleServerShuttingDown()
static_cast<ImplClass *>(this)->_HandleServerShuttingDown();
}

inline void PlatformManager::ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
inline CHIP_ERROR PlatformManager::ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
{
return static_cast<ImplClass *>(this)->_ScheduleWork(workFunct, arg);
}

inline CHIP_ERROR PlatformManager::PostEvent(const ChipDeviceEvent * event)
{
static_cast<ImplClass *>(this)->_ScheduleWork(workFunct, arg);
return static_cast<ImplClass *>(this)->_PostEvent(event);
}

inline void PlatformManager::PostEventOrDie(const ChipDeviceEvent * event)
{
CHIP_ERROR status = static_cast<ImplClass *>(this)->_PostEvent(event);
VerifyOrDieWithMsg(status == CHIP_NO_ERROR, DeviceLayer, "Failed to post event %d: %" CHIP_ERROR_FORMAT,
static_cast<int>(event->Type), status.Format());
}

inline void PlatformManager::RunEventLoop()
Expand Down Expand Up @@ -393,43 +439,29 @@ inline CHIP_ERROR PlatformManager::StopEventLoopTask()
return static_cast<ImplClass *>(this)->_StopEventLoopTask();
}

/**
* @brief
* Shuts down and cleans up the main objects in the CHIP stack.
* This DOES NOT stop the chip thread or event queue from running.
*
*/
inline void PlatformManager::Shutdown()
inline CHIP_ERROR PlatformManager::ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg)
{
static_cast<ImplClass *>(this)->_Shutdown();
mInitialized = false;
return static_cast<ImplClass *>(this)->_ScheduleBackgroundWork(workFunct, arg);
}

inline void PlatformManager::LockChipStack()
inline CHIP_ERROR PlatformManager::PostBackgroundEvent(const ChipDeviceEvent * event)
{
static_cast<ImplClass *>(this)->_LockChipStack();
return static_cast<ImplClass *>(this)->_PostBackgroundEvent(event);
}

inline bool PlatformManager::TryLockChipStack()
inline void PlatformManager::RunBackgroundEventLoop()
{
return static_cast<ImplClass *>(this)->_TryLockChipStack();
static_cast<ImplClass *>(this)->_RunBackgroundEventLoop();
}

inline void PlatformManager::UnlockChipStack()
inline CHIP_ERROR PlatformManager::StartBackgroundEventLoopTask()
{
static_cast<ImplClass *>(this)->_UnlockChipStack();
return static_cast<ImplClass *>(this)->_StartBackgroundEventLoopTask();
}

inline CHIP_ERROR PlatformManager::PostEvent(const ChipDeviceEvent * event)
inline CHIP_ERROR PlatformManager::StopBackgroundEventLoopTask()
{
return static_cast<ImplClass *>(this)->_PostEvent(event);
}

inline void PlatformManager::PostEventOrDie(const ChipDeviceEvent * event)
{
CHIP_ERROR status = static_cast<ImplClass *>(this)->_PostEvent(event);
VerifyOrDieWithMsg(status == CHIP_NO_ERROR, DeviceLayer, "Failed to post event %d: %" CHIP_ERROR_FORMAT,
static_cast<int>(event->Type), status.Format());
return static_cast<ImplClass *>(this)->_StopBackgroundEventLoopTask();
}

inline void PlatformManager::DispatchEvent(const ChipDeviceEvent * event)
Expand Down
3 changes: 2 additions & 1 deletion src/include/platform/internal/GenericPlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class GenericPlatformManagerImpl
void _RemoveEventHandler(PlatformManager::EventHandlerFunct handler, intptr_t arg);
void _HandleServerStarted();
void _HandleServerShuttingDown();
void _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg);
CHIP_ERROR _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg);
CHIP_ERROR _ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg);
void _DispatchEvent(const ChipDeviceEvent * event);

// ===== Support methods that can be overridden by the implementation subclass.
Expand Down
37 changes: 29 additions & 8 deletions src/include/platform/internal/GenericPlatformManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,39 @@ void GenericPlatformManagerImpl<ImplClass>::_HandleServerShuttingDown()
}

template <class ImplClass>
void GenericPlatformManagerImpl<ImplClass>::_ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kCallWorkFunct;
event.CallWorkFunct.WorkFunct = workFunct;
event.CallWorkFunct.Arg = arg;
ChipDeviceEvent event{
.Type = DeviceEventType::kCallWorkFunct,
.CallWorkFunct {.WorkFunct = workFunct, .Arg = arg}
};

CHIP_ERROR status = Impl()->PostEvent(&event);
if (status != CHIP_NO_ERROR)
CHIP_ERROR err = Impl()->PostEvent(&event);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to schedule work: %" CHIP_ERROR_FORMAT, status.Format());
ChipLogError(DeviceLayer, "Failed to schedule work: %" CHIP_ERROR_FORMAT, err.Format());
}
return err;
}

template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg)
{
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
ChipDeviceEvent event{
.Type = DeviceEventType::kCallWorkFunct,
.CallWorkFunct {.WorkFunct = workFunct, .Arg = arg}
};
auto err = Impl()->PostBackgroundEvent(&event);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to schedule background work: %" CHIP_ERROR_FORMAT, err.Format());
}
return err;
#else
// Use foreground event loop for background events
return _ScheduleWork(workFunct, arg);
#endif
}

template <class ImplClass>
Expand Down
Loading