Skip to content

Commit

Permalink
Allow onFailure and onSetupFailure to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Dec 22, 2023
1 parent c2544aa commit 8f29cac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/app/CASESessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Cal
);
}

void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
nullptr_t
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
,
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
#endif
)
{
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, nullptr
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
,
attemptCount, onRetry
#endif
);
}

void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerId,
Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
Expand All @@ -74,10 +90,6 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
ChipLogDetail(CASESessionManager, "FindOrEstablishSession: PeerId = [%d:" ChipLogFormatX64 "]", peerId.GetFabricIndex(),
ChipLogValueX64(peerId.GetNodeId()));

// Ensure that exactly one of onFailure or onSetupFailure is non-null
VerifyOrReturn((onFailure == nullptr) != (onSetupFailure == nullptr),
ChipLogError(CASESessionManager, "Invalid callback parameters"));

bool forAddressUpdate = false;
OperationalSessionSetup * session = FindExistingSessionSetup(peerId, forAddressUpdate);
if (session == nullptr)
Expand All @@ -91,7 +103,8 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
{
onFailure->mCall(onFailure->mContext, peerId, CHIP_ERROR_NO_MEMORY);
}
else if (onSetupFailure != nullptr)

if (onSetupFailure != nullptr)
{
OperationalSessionSetup::ConnnectionFailureInfo failureInfo(peerId, CHIP_ERROR_NO_MEMORY,
SessionEstablishmentStage::kUnknown);
Expand All @@ -113,12 +126,11 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
{
session->Connect(onConnection, onFailure);
}
else if (onSetupFailure != nullptr)

if (onSetupFailure != nullptr)
{
session->Connect(onConnection, onSetupFailure);
}

return;
}

void CASESessionManager::ReleaseSessionsForFabric(FabricIndex fabricIndex)
Expand Down
24 changes: 24 additions & 0 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
);

/**
* Find an existing session for the given node ID or trigger a new session request.
*
* The caller can optionally provide `onConnection`
* callback objects. If provided, these will be used to inform the caller about successful connection establishment.
*
* If the connection is already established, the `onConnection` callback will be immediately called,
* before `FindOrEstablishSession` returns.
*
* The `attemptCount` parameter can be used to automatically retry multiple times if session setup is
* not successful.
*
* @param peerId The node ID to find or establish a session with.
* @param onConnection A callback to be called upon successful connection establishment.
* @param attemptCount The number of retry attempts if session setup fails (default is 1).
* @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
*/
void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection, nullptr_t
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
,
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
);

void ReleaseSessionsForFabric(FabricIndex fabricIndex);

void ReleaseAllSessions();
Expand Down

0 comments on commit 8f29cac

Please sign in to comment.