Skip to content
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

Add background event handling for CASE establish #24099

Merged
merged 47 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove some conditional compilation
  • Loading branch information
mlepage-google committed Jan 17, 2023
commit dba087196d0f23cc5b1cbe59b8d3d246bd5e3685
2 changes: 0 additions & 2 deletions examples/platform/esp32/common/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ 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));

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
// Start a task to run the CHIP Device background event loop.
ReturnErrorOnFailure(PlatformMgr().StartBackgroundEventLoopTask());
#endif

// Start a task to run the CHIP Device event loop.
return PlatformMgr().StartEventLoopTask();
Expand Down
10 changes: 0 additions & 10 deletions src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,11 @@ class PlatformManager
*/
CHIP_ERROR StopEventLoopTask();

// These functions should be gated by CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
// but the PlatformManagerImpl header (included below) makes that tricky
// so always declare them here even if they are not defined by the platform implementation
// (by using #if 1).
#if 1 || defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
CHIP_ERROR ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg = 0);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
CHIP_ERROR PostBackgroundEvent(const ChipDeviceEvent * event);
void RunBackgroundEventLoop();
CHIP_ERROR StartBackgroundEventLoopTask();
CHIP_ERROR StopBackgroundEventLoopTask();
#endif

private:
bool mInitialized = false;
Expand Down Expand Up @@ -445,8 +439,6 @@ inline CHIP_ERROR PlatformManager::StopEventLoopTask()
return static_cast<ImplClass *>(this)->_StopEventLoopTask();
}

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING

inline CHIP_ERROR PlatformManager::ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg)
{
return static_cast<ImplClass *>(this)->_ScheduleBackgroundWork(workFunct, arg);
Expand All @@ -472,8 +464,6 @@ inline CHIP_ERROR PlatformManager::StopBackgroundEventLoopTask()
return static_cast<ImplClass *>(this)->_StopBackgroundEventLoopTask();
}

#endif

inline void PlatformManager::DispatchEvent(const ChipDeviceEvent * event)
{
static_cast<ImplClass *>(this)->_DispatchEvent(event);
Expand Down
2 changes: 0 additions & 2 deletions src/include/platform/internal/GenericPlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ class GenericPlatformManagerImpl
void _HandleServerStarted();
void _HandleServerShuttingDown();
CHIP_ERROR _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg);
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
CHIP_ERROR _ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg);
#endif
void _DispatchEvent(const ChipDeviceEvent * event);

// ===== Support methods that can be overridden by the implementation subclass.
Expand Down
9 changes: 6 additions & 3 deletions src/include/platform/internal/GenericPlatformManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,25 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_ScheduleWork(AsyncWorkFunct w
return err;
}

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
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()->PostEvent(&event);
auto err = Impl()->PostBackgroundEvent(&event);
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
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>
void GenericPlatformManagerImpl<ImplClass>::_DispatchEvent(const ChipDeviceEvent * event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ class GenericPlatformManagerImpl_FreeRTOS : public GenericPlatformManagerImpl<Im
CHIP_ERROR _StartEventLoopTask(void);
CHIP_ERROR _StopEventLoopTask();

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
CHIP_ERROR _PostBackgroundEvent(const ChipDeviceEvent * event);
void _RunBackgroundEventLoop(void);
CHIP_ERROR _StartBackgroundEventLoopTask(void);
CHIP_ERROR _StopBackgroundEventLoopTask();
#endif

// ===== Methods available to the implementation subclass.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::EventLoopTaskMain(void * ar
// Or somehow get our caller to do it once this thread is joined?
}

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING

template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_PostBackgroundEvent(const ChipDeviceEvent * event)
{
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
if (mBackgroundEventQueue == NULL)
{
return CHIP_ERROR_INTERNAL;
Expand All @@ -314,11 +314,16 @@ CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_PostBackgroundEvent(
return CHIP_ERROR(chip::ChipError::Range::kOS, status);
}
return CHIP_NO_ERROR;
#else
// Use foreground event loop for background events
return _PostEvent(event);
#endif
}

template <class ImplClass>
void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunBackgroundEventLoop(void)
{
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
bool oldShouldRunBackgroundEventLoop = false;
if (!mShouldRunBackgroundEventLoop.compare_exchange_strong(oldShouldRunBackgroundEventLoop /* expected */, true /* desired */))
{
Expand All @@ -336,11 +341,15 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunBackgroundEventLoop(voi
eventReceived = xQueueReceive(mBackgroundEventQueue, &event, portMAX_DELAY) == pdTRUE;
}
}
#else
// Use foreground event loop for background events
#endif
}

template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_StartBackgroundEventLoopTask(void)
{
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
#if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_TASK) && CHIP_CONFIG_FREERTOS_USE_STATIC_TASK
mBackgroundEventLoopTask = xTaskCreateStatic(BackgroundEventLoopTaskMain, CHIP_DEVICE_CONFIG_BG_TASK_NAME, ArraySize(mBackgroundEventLoopStack), this,
CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY, mBackgroundEventLoopStack, &mBackgroundEventLoopTaskStruct);
Expand All @@ -349,27 +358,36 @@ CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_StartBackgroundEvent
this, CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY, &mBackgroundEventLoopTask);
#endif
return (mBackgroundEventLoopTask != NULL) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY;
#else
// Use foreground event loop for background events
return CHIP_NO_ERROR;
#endif
}

template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_StopBackgroundEventLoopTask(void)
{
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
bool oldShouldRunBackgroundEventLoop = true;
if (mShouldRunBackgroundEventLoop.compare_exchange_strong(oldShouldRunBackgroundEventLoop /* expected */, false /* desired */))
{
ChipDeviceEvent noop {.Type = DeviceEventType::kNoOp };
xQueueSendToBack(mBackgroundEventQueue, &noop, 0);
}
return CHIP_NO_ERROR;
#else
// Use foreground event loop for background events
return CHIP_NO_ERROR;
#endif
}

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
template <class ImplClass>
void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::BackgroundEventLoopTaskMain(void * arg)
{
ChipLogDetail(DeviceLayer, "CHIP background task running");
static_cast<GenericPlatformManagerImpl_FreeRTOS<ImplClass> *>(arg)->Impl()->RunBackgroundEventLoop();
}

#endif

template <class ImplClass>
Expand Down
39 changes: 0 additions & 39 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,16 +1317,10 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)

ChipLogProgress(SecureChannel, "Received Sigma3 msg");

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
auto * workPtr = new Sigma3Work();
VerifyOrExit(workPtr != nullptr, err = CHIP_ERROR_NO_MEMORY);
{
auto & work = *workPtr;
#else
Sigma3Work* workPtr = nullptr;
{
Sigma3Work work;
#endif

// Used to call back into the session after background event processing.
// It happens that there's only one pairing session (in CASEServer)
Expand Down Expand Up @@ -1445,17 +1439,12 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
}
}

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
SuccessOrExit(err = DeviceLayer::PlatformMgr().ScheduleBackgroundWork(
[](intptr_t arg) { HandleSigma3b(*reinterpret_cast<Sigma3Work*>(arg)); },
reinterpret_cast<intptr_t>(&work)));
workPtr = nullptr; // scheduling succeeded, so don't delete
mExchangeCtxt->WillSendMessage();
mState = State::kBackgroundPending;
#else
HandleSigma3b(work);
SuccessOrExit(HandleSigma3c(work));
#endif
}

exit:
Expand Down Expand Up @@ -1503,7 +1492,6 @@ void CASESession::HandleSigma3b(Sigma3Work & work)
exit:
work.status = err;

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
auto err2 = DeviceLayer::PlatformMgr().ScheduleWork(
[](intptr_t arg) {
auto & work2 = *reinterpret_cast<Sigma3Work*>(arg);
Expand All @@ -1515,23 +1503,18 @@ void CASESession::HandleSigma3b(Sigma3Work & work)
{
delete &work; // scheduling failed, so delete
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
}

CHIP_ERROR CASESession::HandleSigma3c(Sigma3Work & work)
{
CHIP_ERROR err = CHIP_NO_ERROR;
bool ignoreFailure = true;

ChipLogError(SecureChannel, "########## IN PART C");

#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
// Special case: if for whatever reason not in expected state or sequence,
// don't do anything, including sending a status report or aborting the
// pending establish.
VerifyOrExit(mState == State::kBackgroundPending, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mSequence == work.sequence, err = CHIP_ERROR_INCORRECT_STATE);
#endif

ignoreFailure = false;

Expand Down Expand Up @@ -1564,19 +1547,15 @@ CHIP_ERROR CASESession::HandleSigma3c(Sigma3Work & work)
Finish();

exit:
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
delete &work;
#endif

if (err != CHIP_NO_ERROR && !ignoreFailure)
{
SendStatusReport(mExchangeCtxt, kProtocolCodeInvalidParam);
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
// Abort the pending establish, which is normally done by the caller,
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
// but in the background processing case must be done here.
DiscardExchange();
AbortPendingEstablish(err);
#endif
}

return err;
Expand Down Expand Up @@ -1678,24 +1657,6 @@ CHIP_ERROR CASESession::ValidateSigmaResumeMIC(const ByteSpan & resumeMIC, const
return CHIP_NO_ERROR;
}

CHIP_ERROR CASESession::ValidatePeerIdentity(const ByteSpan & peerNOC, const ByteSpan & peerICAC, NodeId & peerNodeId,
Crypto::P256PublicKey & peerPublicKey)
{
ReturnErrorCodeIf(mFabricsTable == nullptr, CHIP_ERROR_INCORRECT_STATE);
const auto * fabricInfo = mFabricsTable->FindFabricWithIndex(mFabricIndex);
ReturnErrorCodeIf(fabricInfo == nullptr, CHIP_ERROR_INCORRECT_STATE);

ReturnErrorOnFailure(SetEffectiveTime());

CompressedFabricId unused;
FabricId peerFabricId;
ReturnErrorOnFailure(mFabricsTable->VerifyCredentials(mFabricIndex, peerNOC, peerICAC, mValidContext, unused, peerFabricId,
peerNodeId, peerPublicKey));
VerifyOrReturnError(fabricInfo->GetFabricId() == peerFabricId, CHIP_ERROR_INVALID_CASE_PARAMETER);

return CHIP_NO_ERROR;
}

CHIP_ERROR CASESession::ConstructTBSData(const ByteSpan & senderNOC, const ByteSpan & senderICAC, const ByteSpan & senderPubKey,
const ByteSpan & receiverPubKey, uint8_t * tbsData, size_t & tbsDataLen)
{
Expand Down
2 changes: 0 additions & 2 deletions src/protocols/secure_channel/CASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,

CHIP_ERROR ConstructSaltSigma2(const ByteSpan & rand, const Crypto::P256PublicKey & pubkey, const ByteSpan & ipk,
MutableByteSpan & salt);
CHIP_ERROR ValidatePeerIdentity(const ByteSpan & peerNOC, const ByteSpan & peerICAC, NodeId & peerNodeId,
Crypto::P256PublicKey & peerPublicKey);
CHIP_ERROR ConstructTBSData(const ByteSpan & senderNOC, const ByteSpan & senderICAC, const ByteSpan & senderPubKey,
const ByteSpan & receiverPubKey, uint8_t * tbsData, size_t & tbsDataLen);
CHIP_ERROR ConstructSaltSigma3(const ByteSpan & ipk, MutableByteSpan & salt);
Expand Down