-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[app] decouple CASESession from OperationalDevice (#12148)
* [app] decouple CASESession from OperationalDevice * [app] make size of CASE client and device pool configurable
- Loading branch information
Showing
12 changed files
with
409 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <app/CASEClient.h> | ||
|
||
namespace chip { | ||
|
||
CASEClient::CASEClient(const CASEClientInitParams & params) : mInitParams(params) {} | ||
|
||
void CASEClient::SetMRPIntervals(const ReliableMessageProtocolConfig & mrpConfig) | ||
{ | ||
mCASESession.SetMRPConfig(mrpConfig); | ||
} | ||
|
||
CHIP_ERROR CASEClient::EstablishSession(PeerId peer, const Transport::PeerAddress & peerAddress, | ||
const ReliableMessageProtocolConfig & mrpConfig, OnCASEConnected onConnection, | ||
OnCASEConnectionFailure onFailure, void * context) | ||
{ | ||
// Create a UnauthenticatedSession for CASE pairing. | ||
// Don't use mSecureSession here, because mSecureSession is for encrypted communication. | ||
Optional<SessionHandle> session = mInitParams.sessionManager->CreateUnauthenticatedSession(peerAddress, mrpConfig); | ||
VerifyOrReturnError(session.HasValue(), CHIP_ERROR_NO_MEMORY); | ||
|
||
Messaging::ExchangeContext * exchange = mInitParams.exchangeMgr->NewContext(session.Value(), &mCASESession); | ||
VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_INTERNAL); | ||
|
||
ReturnErrorOnFailure(mCASESession.MessageDispatch().Init(mInitParams.sessionManager)); | ||
|
||
uint16_t keyID = 0; | ||
ReturnErrorOnFailure(mInitParams.idAllocator->Allocate(keyID)); | ||
|
||
ReturnErrorOnFailure( | ||
mCASESession.EstablishSession(peerAddress, mInitParams.fabricInfo, peer.GetNodeId(), keyID, exchange, this)); | ||
mConnectionSuccessCallback = onConnection; | ||
mConnectionFailureCallback = onFailure; | ||
mConectionContext = context; | ||
mPeerId = peer; | ||
mPeerAddress = peerAddress; | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
void CASEClient::OnSessionEstablishmentError(CHIP_ERROR error) | ||
{ | ||
mInitParams.idAllocator->Free(mCASESession.GetLocalSessionId()); | ||
|
||
if (mConnectionFailureCallback) | ||
{ | ||
mConnectionFailureCallback(mConectionContext, this, error); | ||
} | ||
} | ||
|
||
void CASEClient::OnSessionEstablished() | ||
{ | ||
// On successfull CASE connection, the local session ID will be used for the derived secure session. | ||
if (mConnectionSuccessCallback) | ||
{ | ||
mConnectionSuccessCallback(mConectionContext, this); | ||
} | ||
} | ||
|
||
CHIP_ERROR CASEClient::DeriveSecureSessionHandle(Optional<SessionHandle> & handle) | ||
{ | ||
CHIP_ERROR err = mInitParams.sessionManager->NewPairing( | ||
Optional<Transport::PeerAddress>::Value(mPeerAddress), mPeerId.GetNodeId(), &mCASESession, | ||
CryptoContext::SessionRole::kInitiator, mInitParams.fabricInfo->GetFabricIndex()); | ||
if (err != CHIP_NO_ERROR) | ||
{ | ||
ChipLogError(Controller, "Failed in setting up CASE secure channel: err %s", ErrorStr(err)); | ||
return err; | ||
} | ||
handle.SetValue(SessionHandle(mPeerId.GetNodeId(), mCASESession.GetLocalSessionId(), mCASESession.GetPeerSessionId(), | ||
mInitParams.fabricInfo->GetFabricIndex())); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
}; // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <messaging/ExchangeMgr.h> | ||
#include <messaging/ReliableMessageProtocolConfig.h> | ||
#include <protocols/secure_channel/CASESession.h> | ||
#include <protocols/secure_channel/SessionIDAllocator.h> | ||
|
||
namespace chip { | ||
|
||
class CASEClient; | ||
|
||
typedef void (*OnCASEConnected)(void * context, CASEClient * client); | ||
typedef void (*OnCASEConnectionFailure)(void * context, CASEClient * client, CHIP_ERROR error); | ||
|
||
struct CASEClientInitParams | ||
{ | ||
SessionManager * sessionManager = nullptr; | ||
Messaging::ExchangeManager * exchangeMgr = nullptr; | ||
SessionIDAllocator * idAllocator = nullptr; | ||
FabricInfo * fabricInfo = nullptr; | ||
}; | ||
|
||
class DLL_EXPORT CASEClient : public SessionEstablishmentDelegate | ||
{ | ||
public: | ||
CASEClient(const CASEClientInitParams & params); | ||
|
||
void SetMRPIntervals(const ReliableMessageProtocolConfig & mrpConfig); | ||
|
||
CHIP_ERROR EstablishSession(PeerId peer, const Transport::PeerAddress & peerAddress, | ||
const ReliableMessageProtocolConfig & mrpConfig, OnCASEConnected onConnection, | ||
OnCASEConnectionFailure onFailure, void * context); | ||
|
||
// Implementation of SessionEstablishmentDelegate | ||
void OnSessionEstablishmentError(CHIP_ERROR error) override; | ||
|
||
void OnSessionEstablished() override; | ||
|
||
CHIP_ERROR DeriveSecureSessionHandle(Optional<SessionHandle> & handle); | ||
|
||
private: | ||
CASEClientInitParams mInitParams; | ||
|
||
CASESession mCASESession; | ||
PeerId mPeerId; | ||
Transport::PeerAddress mPeerAddress; | ||
|
||
OnCASEConnected mConnectionSuccessCallback = nullptr; | ||
OnCASEConnectionFailure mConnectionFailureCallback = nullptr; | ||
void * mConectionContext = nullptr; | ||
}; | ||
|
||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <app/CASEClient.h> | ||
#include <lib/support/Pool.h> | ||
|
||
namespace chip { | ||
|
||
class CASEClientPoolDelegate | ||
{ | ||
public: | ||
virtual CASEClient * Allocate(CASEClientInitParams params) = 0; | ||
|
||
virtual void Release(CASEClient * client) = 0; | ||
|
||
virtual ~CASEClientPoolDelegate() {} | ||
}; | ||
|
||
template <size_t N> | ||
class CASEClientPool : public CASEClientPoolDelegate | ||
{ | ||
public: | ||
CASEClient * Allocate(CASEClientInitParams params) override { return mClientPool.CreateObject(params); } | ||
|
||
void Release(CASEClient * client) override { mClientPool.ReleaseObject(client); } | ||
|
||
private: | ||
BitMapObjectPool<CASEClient, N, OnObjectPoolDestruction::IgnoreUnsafeDoNotUseInNewCode> mClientPool; | ||
}; | ||
|
||
}; // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.