Skip to content

Commit

Permalink
Remove raw SessionHolder::Get (#18490)
Browse files Browse the repository at this point in the history
* Fix session holder.

* Review feedback
  • Loading branch information
mrjerryjohns authored and pull[bot] committed Aug 5, 2022
1 parent 4fa778b commit 9692716
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 66 deletions.
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void StartPinging(streamer_t * stream, char * destination)
err = EstablishSecureSession(stream, GetEchoPeerAddress());
SuccessOrExit(err);

err = gEchoClient.Init(&gExchangeManager, gSession.Get());
err = gEchoClient.Init(&gExchangeManager, gSession.Get().Value());
SuccessOrExit(err);

// Arrange to get a callback whenever an Echo Response is received.
Expand Down
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ CHIP_ERROR SendMessage(streamer_t * stream)
uint32_t payloadSize = gSendArguments.GetPayloadSize();

// Create a new exchange context.
auto * ec = gExchangeManager.NewContext(gSession.Get(), &gMockAppDelegate);
auto * ec = gExchangeManager.NewContext(gSession.Get().Value(), &gMockAppDelegate);
VerifyOrExit(ec != nullptr, err = CHIP_ERROR_NO_MEMORY);

payloadBuf = MessagePacketBuffer::New(payloadSize);
Expand Down
4 changes: 2 additions & 2 deletions src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ CHIP_ERROR OperationalDeviceProxy::UpdateDeviceData(const Transport::PeerAddress
return CHIP_NO_ERROR;
}

mSecureSession.Get()->AsSecureSession()->SetPeerAddress(addr);
mSecureSession.Get().Value()->AsSecureSession()->SetPeerAddress(addr);
}

return err;
Expand Down Expand Up @@ -305,7 +305,7 @@ CHIP_ERROR OperationalDeviceProxy::Disconnect()
ReturnErrorCodeIf(mState != State::SecureConnected, CHIP_ERROR_INCORRECT_STATE);
if (mSecureSession)
{
mInitParams.sessionManager->ExpirePairing(mSecureSession.Get());
mInitParams.sessionManager->ExpirePairing(mSecureSession.Get().Value());
}
MoveToState(State::Initialized);

Expand Down
2 changes: 1 addition & 1 deletion src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy,

Messaging::ExchangeManager * GetExchangeManager() const override { return mInitParams.exchangeMgr; }

chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.ToOptional(); }
chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.Get(); }

Transport::PeerAddress GetPeerAddress() const { return mDeviceAddress; }

Expand Down
8 changes: 6 additions & 2 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ CHIP_ERROR ReadClient::SendReadRequest(ReadPrepareParams & aReadPrepareParams)
ReturnErrorOnFailure(request.EndOfReadRequestMessage().GetError());
ReturnErrorOnFailure(writer.Finalize(&msgBuf));

mpExchangeCtx = mpExchangeMgr->NewContext(aReadPrepareParams.mSessionHolder.Get(), this);
VerifyOrReturnError(aReadPrepareParams.mSessionHolder, CHIP_ERROR_MISSING_SECURE_SESSION);

mpExchangeCtx = mpExchangeMgr->NewContext(aReadPrepareParams.mSessionHolder.Get().Value(), this);
VerifyOrReturnError(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);

mpExchangeCtx->SetResponseTimeout(aReadPrepareParams.mTimeout);
Expand Down Expand Up @@ -913,7 +915,9 @@ CHIP_ERROR ReadClient::SendSubscribeRequest(ReadPrepareParams & aReadPreparePara
ReturnErrorOnFailure(err = request.EndOfSubscribeRequestMessage().GetError());
ReturnErrorOnFailure(writer.Finalize(&msgBuf));

mpExchangeCtx = mpExchangeMgr->NewContext(aReadPrepareParams.mSessionHolder.Get(), this);
VerifyOrReturnError(aReadPrepareParams.mSessionHolder, CHIP_ERROR_MISSING_SECURE_SESSION);

mpExchangeCtx = mpExchangeMgr->NewContext(aReadPrepareParams.mSessionHolder.Get().Value(), this);
VerifyOrReturnError(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(kImMessageTimeout);

Expand Down
4 changes: 2 additions & 2 deletions src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ CHIP_ERROR ReadHandler::SendStatusReport(Protocols::InteractionModel::Status aSt
{
VerifyOrReturnLogError(mpExchangeCtx == nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnLogError(mSessionHandle, CHIP_ERROR_INCORRECT_STATE);
mpExchangeCtx = InteractionModelEngine::GetInstance()->GetExchangeManager()->NewContext(mSessionHandle.Get(), this);
mpExchangeCtx = InteractionModelEngine::GetInstance()->GetExchangeManager()->NewContext(mSessionHandle.Get().Value(), this);
}
VerifyOrReturnLogError(mpExchangeCtx != nullptr, CHIP_ERROR_INCORRECT_STATE);

Expand All @@ -240,7 +240,7 @@ CHIP_ERROR ReadHandler::SendReportData(System::PacketBufferHandle && aPayload, b
{
VerifyOrReturnLogError(mpExchangeCtx == nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnLogError(mSessionHandle, CHIP_ERROR_INCORRECT_STATE);
mpExchangeCtx = InteractionModelEngine::GetInstance()->GetExchangeManager()->NewContext(mSessionHandle.Get(), this);
mpExchangeCtx = InteractionModelEngine::GetInstance()->GetExchangeManager()->NewContext(mSessionHandle.Get().Value(), this);
}

VerifyOrReturnLogError(mpExchangeCtx != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down
10 changes: 5 additions & 5 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ CHIP_ERROR SendCommandRequest(std::unique_ptr<chip::app::CommandSender> && comma
err = commandSender->FinishCommand();
SuccessOrExit(err);

err = commandSender->SendCommandRequest(gSession.Get(), chip::MakeOptional(gMessageTimeout));
err = commandSender->SendCommandRequest(gSession.Get().Value(), chip::MakeOptional(gMessageTimeout));
SuccessOrExit(err);

gCommandCount++;
Expand Down Expand Up @@ -302,7 +302,7 @@ CHIP_ERROR SendBadCommandRequest(std::unique_ptr<chip::app::CommandSender> && co
err = commandSender->FinishCommand();
SuccessOrExit(err);

err = commandSender->SendCommandRequest(gSession.Get(), chip::MakeOptional(gMessageTimeout));
err = commandSender->SendCommandRequest(gSession.Get().Value(), chip::MakeOptional(gMessageTimeout));
SuccessOrExit(err);
gCommandCount++;
commandSender.release();
Expand Down Expand Up @@ -330,7 +330,7 @@ CHIP_ERROR SendReadRequest()

printf("\nSend read request message to Node: %" PRIu64 "\n", chip::kTestDeviceNodeId);

chip::app::ReadPrepareParams readPrepareParams(gSession.Get());
chip::app::ReadPrepareParams readPrepareParams(gSession.Get().Value());
readPrepareParams.mTimeout = gMessageTimeout;
readPrepareParams.mpAttributePathParamsList = &attributePathParams;
readPrepareParams.mAttributePathParamsListSize = 1;
Expand Down Expand Up @@ -367,7 +367,7 @@ CHIP_ERROR SendWriteRequest(chip::app::WriteClient & apWriteClient)

SuccessOrExit(err = apWriteClient.EncodeAttribute(
chip::app::AttributePathParams(2 /* endpoint */, 3 /* cluster */, 4 /* attribute */), true));
SuccessOrExit(err = apWriteClient.SendWriteRequest(gSession.Get(), gMessageTimeout));
SuccessOrExit(err = apWriteClient.SendWriteRequest(gSession.Get().Value(), gMessageTimeout));

gWriteCount++;

Expand All @@ -384,7 +384,7 @@ CHIP_ERROR SendSubscribeRequest()
CHIP_ERROR err = CHIP_NO_ERROR;
gLastMessageTime = chip::System::SystemClock().GetMonotonicTimestamp();

chip::app::ReadPrepareParams readPrepareParams(gSession.Get());
chip::app::ReadPrepareParams readPrepareParams(gSession.Get().Value());
chip::app::EventPathParams * eventPathParams = new chip::app::EventPathParams[2];
chip::app::AttributePathParams * attributePathParams = new chip::app::AttributePathParams[1];
readPrepareParams.mpEventPathParamsList = eventPathParams;
Expand Down
7 changes: 4 additions & 3 deletions src/controller/CommissioneeDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ CHIP_ERROR CommissioneeDeviceProxy::SendCommands(app::CommandSender * commandObj
{
VerifyOrReturnError(mSecureSession, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(commandObj != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
return commandObj->SendCommandRequest(mSecureSession.Get(), timeout);
VerifyOrReturnError(mSecureSession, CHIP_ERROR_MISSING_SECURE_SESSION);
return commandObj->SendCommandRequest(mSecureSession.Get().Value(), timeout);
}

void CommissioneeDeviceProxy::OnSessionReleased()
Expand All @@ -60,7 +61,7 @@ CHIP_ERROR CommissioneeDeviceProxy::CloseSession()
ReturnErrorCodeIf(mState != ConnectionState::SecureConnected, CHIP_ERROR_INCORRECT_STATE);
if (mSecureSession)
{
mSessionManager->ExpirePairing(mSecureSession.Get());
mSessionManager->ExpirePairing(mSecureSession.Get().Value());
}
mState = ConnectionState::NotConnected;
mPairing.Clear();
Expand All @@ -87,7 +88,7 @@ CHIP_ERROR CommissioneeDeviceProxy::UpdateDeviceData(const Transport::PeerAddres
return CHIP_NO_ERROR;
}

Transport::SecureSession * secureSession = mSecureSession.Get()->AsSecureSession();
Transport::SecureSession * secureSession = mSecureSession.Get().Value()->AsSecureSession();
secureSession->SetPeerAddress(addr);

return CHIP_NO_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/controller/CommissioneeDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CommissioneeDeviceProxy : public DeviceProxy, public SessionDelegate
CHIP_ERROR SetPeerId(ByteSpan rcac, ByteSpan noc) override;
const Transport::PeerAddress & GetPeerAddress() const { return mDeviceAddress; }

chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.ToOptional(); }
chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.Get(); }

Messaging::ExchangeManager * GetExchangeManager() const override { return mExchangeMgr; }

Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/clusters/attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ OnReportBeginCallback gOnReportEndCallback = nullptr;

void PythonResubscribePolicy(uint32_t aNumCumulativeRetries, uint32_t & aNextSubscriptionIntervalMsec, bool & aShouldResubscribe)
{
aShouldResubscribe = false;
aShouldResubscribe = true;
}

class ReadClientCallback : public ReadClient::Callback
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/CHIPError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, CHIP_ERROR err)
case CHIP_ERROR_TLV_TAG_NOT_FOUND.AsInteger():
desc = "TLV tag not found";
break;
case CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger():
desc = "Missing secure session";
break;
case CHIP_ERROR_INVALID_PATH_LIST.AsInteger():
desc = "Invalid TLV path list";
break;
Expand Down
13 changes: 8 additions & 5 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,14 @@ using CHIP_ERROR = ::chip::ChipError;
*/
#define CHIP_ERROR_TLV_TAG_NOT_FOUND CHIP_CORE_ERROR(0x76)

// unused CHIP_CORE_ERROR(0x77)
/**
* @def CHIP_ERROR_MISSING_SECURE_SESSION
*
* @brief
*
* A secure session is needed to do work, but is missing/is not present.
*/
#define CHIP_ERROR_MISSING_SECURE_SESSION CHIP_CORE_ERROR(0x77)

// unused CHIP_CORE_ERROR(0x78)

Expand Down Expand Up @@ -2388,10 +2395,6 @@ using CHIP_ERROR = ::chip::ChipError;
*/
#define CHIP_ERROR_IM_MALFORMED_STATUS_RESPONSE_MESSAGE CHIP_CORE_ERROR(0xe1)

/**
* @}
*/

// clang-format on

// !!!!! IMPORTANT !!!!! If you add new CHIP errors, please update the translation
Expand Down
20 changes: 17 additions & 3 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,24 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp
return CHIP_ERROR_INTERNAL;
}

//
// It is possible that we might have evicted a session as a side-effect of processing an inbound message on this exchange.
// We cannot proceed any further sending a message since we don't have an attached session, so let's error out.
//
// This should not happen to well-behaved logic attempting to sending messages on exchanges, so let's print out a warning
// to ensure it alerts someone to fixing their logic...
//
if (!mSession)
{
ChipLogError(ExchangeManager,
"WARNING: We shouldn't be sending a message on an exchange that has no attached session...");
return CHIP_ERROR_MISSING_SECURE_SESSION;
}

// Create a new scope for `err`, to avoid shadowing warning previous `err`.
CHIP_ERROR err = mDispatch.SendMessage(GetExchangeMgr()->GetSessionManager(), mSession.Get(), mExchangeId, IsInitiator(),
GetReliableMessageContext(), reliableTransmissionRequested, protocolId, msgType,
std::move(msgBuf));
CHIP_ERROR err = mDispatch.SendMessage(GetExchangeMgr()->GetSessionManager(), mSession.Get().Value(), mExchangeId,
IsInitiator(), GetReliableMessageContext(), reliableTransmissionRequested,
protocolId, msgType, std::move(msgBuf));
if (err != CHIP_NO_ERROR && IsResponseExpected())
{
CancelResponseTimer();
Expand Down
8 changes: 7 additions & 1 deletion src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext,

ExchangeMessageDispatch & GetMessageDispatch() { return mDispatch; }

SessionHandle GetSessionHandle() const { return mSession.Get(); }
SessionHandle GetSessionHandle() const
{
VerifyOrDie(mSession);
auto sessionHandle = mSession.Get();
return std::move(sessionHandle.Value());
}

bool HasSessionHandle() const { return mSession; }

uint16_t GetExchangeId() const { return mExchangeId; }
Expand Down
16 changes: 12 additions & 4 deletions src/messaging/tests/MessagingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ CHIP_ERROR MessagingContext::CreateSessionBobToFriends()

SessionHandle MessagingContext::GetSessionBobToAlice()
{
return mSessionBobToAlice.Get();
auto sessionHandle = mSessionBobToAlice.Get();
return std::move(sessionHandle.Value());
}

SessionHandle MessagingContext::GetSessionAliceToBob()
{
return mSessionAliceToBob.Get();
auto sessionHandle = mSessionAliceToBob.Get();
return std::move(sessionHandle.Value());
}

SessionHandle MessagingContext::GetSessionBobToFriends()
Expand All @@ -122,12 +124,18 @@ SessionHandle MessagingContext::GetSessionBobToFriends()

void MessagingContext::ExpireSessionBobToAlice()
{
mSessionManager.ExpirePairing(mSessionBobToAlice.Get());
if (mSessionBobToAlice)
{
mSessionManager.ExpirePairing(mSessionBobToAlice.Get().Value());
}
}

void MessagingContext::ExpireSessionAliceToBob()
{
mSessionManager.ExpirePairing(mSessionAliceToBob.Get());
if (mSessionAliceToBob)
{
mSessionManager.ExpirePairing(mSessionAliceToBob.Get().Value());
}
}

void MessagingContext::ExpireSessionBobToFriends()
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/tests/echo/echo_requester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ int main(int argc, char * argv[])
err = EstablishSecureSession();
SuccessOrExit(err);

err = gEchoClient.Init(&gExchangeManager, gSession.Get());
err = gEchoClient.Init(&gExchangeManager, gSession.Get().Value());
SuccessOrExit(err);

// Arrange to get a callback whenever an Echo Response is received.
Expand Down
4 changes: 3 additions & 1 deletion src/protocols/echo/EchoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ CHIP_ERROR EchoClient::SendEchoRequest(System::PacketBufferHandle && payload, Me
mExchangeCtx = nullptr;
}

VerifyOrReturnError(mSecureSession, CHIP_ERROR_INVALID_MESSAGE_TYPE);

// Create a new exchange context.
mExchangeCtx = mExchangeMgr->NewContext(mSecureSession.Get(), this);
mExchangeCtx = mExchangeMgr->NewContext(mSecureSession.Get().Value(), this);
if (mExchangeCtx == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
Expand Down
9 changes: 5 additions & 4 deletions src/protocols/secure_channel/PairingSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void PairingSession::Finish()
CHIP_ERROR err = ActivateSecureSession(address);
if (err == CHIP_NO_ERROR)
{
mDelegate->OnSessionEstablished(mSecureSessionHolder.Get());
VerifyOrDie(mSecureSessionHolder);
mDelegate->OnSessionEstablished(mSecureSessionHolder.Get().Value());
}
else
{
Expand Down Expand Up @@ -155,14 +156,14 @@ void PairingSession::Clear()

if (mSecureSessionHolder)
{
SessionHandle session = mSecureSessionHolder.Get();
auto session = mSecureSessionHolder.Get();
// Call Release before ExpirePairing because we don't want to receive OnSessionReleased() event here
mSecureSessionHolder.Release();
if (!session->AsSecureSession()->IsActiveSession() && mSessionManager != nullptr)
if (!session.Value()->AsSecureSession()->IsActiveSession() && mSessionManager != nullptr)
{
// Make sure to clean up our pending session, since we're the only
// ones who have access to it do do so.
mSessionManager->ExpirePairing(session);
mSessionManager->ExpirePairing(session.Value());
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/transport/SessionHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ class SessionHolder : public SessionDelegate, public IntrusiveListNodeBase
void Release();

operator bool() const { return mSession.HasValue(); }
SessionHandle Get() const { return SessionHandle{ mSession.Value().Get() }; }
Optional<SessionHandle> ToOptional() const
Optional<SessionHandle> Get() const
{
return mSession.HasValue() ? chip::MakeOptional<SessionHandle>(Get()) : chip::Optional<SessionHandle>::Missing();
//
// We cannot return mSession directly even if Optional<SessionHandle> is internally composed of the same bits,
// since they are not actually equivalent type-wise, and SessionHandle does not permit copy-construction.
//
// So, construct a new Optional<SessionHandle> from the underlying Transport::Session reference.
//
return mSession.HasValue() ? chip::MakeOptional<SessionHandle>(mSession.Value().Get())
: chip::Optional<SessionHandle>::Missing();
}

Transport::Session * operator->() const { return &mSession.Value().Get(); }
Expand Down
Loading

0 comments on commit 9692716

Please sign in to comment.