Skip to content

Commit

Permalink
further memory optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-apple committed Apr 14, 2021
1 parent 549cc4b commit 23288cd
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 30 deletions.
12 changes: 12 additions & 0 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ using namespace chip::Callback;

namespace chip {
namespace Controller {
// TODO: This is a placeholder delegate for exchange context created in Device::SendMessage()
// Delete this class when Device::SendMessage() is obsoleted.
class DeviceExchangeDelegate : public Messaging::ExchangeDelegate
{
void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
System::PacketBufferHandle payload) override
{}
void OnResponseTimeout(Messaging::ExchangeContext * ec) override {}
};

CHIP_ERROR Device::SendMessage(Protocols::Id protocolId, uint8_t msgType, System::PacketBufferHandle buffer)
{
Expand Down Expand Up @@ -86,6 +95,9 @@ CHIP_ERROR Device::SendMessage(Protocols::Id protocolId, uint8_t msgType, System
// receive the ack message. This logic need to be deleted after we converting all legacy ZCL messages to IM messages.
sendFlags.Set(Messaging::SendMessageFlags::kFromInitiator).Set(Messaging::SendMessageFlags::kNoAutoRequestAck);

DeviceExchangeDelegate delegate;
exchange->SetDelegate(&delegate);

CHIP_ERROR err = exchange->SendMessage(protocolId, msgType, std::move(buffer), sendFlags);

buffer = nullptr;
Expand Down
6 changes: 4 additions & 2 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ CHIP_ERROR ExchangeContext::SendMessageImpl(Protocols::Id protocolId, uint8_t ms

bool reliableTransmissionRequested = !sendFlags.Has(SendMessageFlags::kNoAutoRequestAck);

ChipLogProgress(ExchangeManager, "Trying to send message using dispatch %p", GetMessageDispatch());

VerifyOrExit(GetMessageDispatch() != nullptr, err = CHIP_ERROR_INCORRECT_STATE);

// If a response message is expected...
Expand Down Expand Up @@ -241,7 +243,7 @@ ExchangeMessageDispatch * ExchangeContext::GetMessageDispatch()
}

ExchangeContext * ExchangeContext::Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegate * delegate)
ExchangeDelegateBase * delegate)
{
VerifyOrDie(mExchangeMgr == nullptr && GetReferenceCount() == 0);

Expand Down Expand Up @@ -345,7 +347,7 @@ void ExchangeContext::HandleResponseTimeout(System::Layer * aSystemLayer, void *
// NOTE: we don't set mResponseExpected to false here because the response could still arrive. If the user
// wants to never receive the response, they must close the exchange context.

ExchangeDelegate * delegate = ec->GetDelegate();
ExchangeDelegateBase * delegate = ec->GetDelegate();

// Call the user's timeout handler.
if (delegate != nullptr)
Expand Down
12 changes: 6 additions & 6 deletions src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
CHIP_ERROR HandleMessage(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
const Transport::PeerAddress & peerAddress, System::PacketBufferHandle msgBuf);

ExchangeDelegate * GetDelegate() const { return mDelegate; }
void SetDelegate(ExchangeDelegate * delegate) { mDelegate = delegate; }
ExchangeDelegateBase * GetDelegate() const { return mDelegate; }
void SetDelegate(ExchangeDelegateBase * delegate) { mDelegate = delegate; }
void SetReliableMessageDelegate(ReliableMessageDelegate * delegate) { mReliableMessageContext.SetDelegate(delegate); }

ExchangeManager * GetExchangeMgr() const { return mExchangeMgr; }
Expand Down Expand Up @@ -187,7 +187,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
void Abort();

ExchangeContext * Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegate * delegate);
ExchangeDelegateBase * delegate);
void Free();
void Reset();

Expand All @@ -202,9 +202,9 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch

Timeout mResponseTimeout; // Maximum time to wait for response (in milliseconds); 0 disables response timeout.
ReliableMessageContext mReliableMessageContext;
ExchangeDelegate * mDelegate = nullptr;
ExchangeManager * mExchangeMgr = nullptr;
ExchangeACL * mExchangeACL = nullptr;
ExchangeDelegateBase * mDelegate = nullptr;
ExchangeManager * mExchangeMgr = nullptr;
ExchangeACL * mExchangeACL = nullptr;

SecureSessionHandle mSecureSession; // The connection state
uint16_t mExchangeId; // Assigned exchange ID.
Expand Down
12 changes: 10 additions & 2 deletions src/messaging/ExchangeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class ExchangeContext;
* is interested in receiving these callbacks, they can specialize this class and handle
* each trigger in their implementation of this class.
*/
class DLL_EXPORT ExchangeDelegate
class DLL_EXPORT ExchangeDelegateBase
{
public:
virtual ~ExchangeDelegate() {}
virtual ~ExchangeDelegateBase() {}

/**
* @brief
Expand Down Expand Up @@ -77,6 +77,14 @@ class DLL_EXPORT ExchangeDelegate
*/
virtual void OnExchangeClosing(ExchangeContext * ec) {}

virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr) = 0;
};

class DLL_EXPORT ExchangeDelegate : public ExchangeDelegateBase
{
public:
virtual ~ExchangeDelegate() {}

virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr)
{
mMessageDispatch.Init(rmMgr, sessionMgr);
Expand Down
10 changes: 5 additions & 5 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ CHIP_ERROR ExchangeManager::Shutdown()
return CHIP_NO_ERROR;
}

ExchangeContext * ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegate * delegate)
ExchangeContext * ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegateBase * delegate)
{
return AllocContext(mNextExchangeId++, session, true, delegate);
}

CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegate * delegate)
CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegateBase * delegate)
{
return RegisterUMH(protocolId, kAnyMessageType, delegate);
}

CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType,
ExchangeDelegate * delegate)
ExchangeDelegateBase * delegate)
{
return RegisterUMH(protocolId, static_cast<int16_t>(msgType), delegate);
}
Expand All @@ -145,7 +145,7 @@ void ExchangeManager::OnReceiveError(CHIP_ERROR error, const Transport::PeerAddr
}

ExchangeContext * ExchangeManager::AllocContext(uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegate * delegate)
ExchangeDelegateBase * delegate)
{
CHIP_FAULT_INJECT(FaultInjection::kFault_AllocExchangeContext, return nullptr);

Expand All @@ -161,7 +161,7 @@ ExchangeContext * ExchangeManager::AllocContext(uint16_t ExchangeId, SecureSessi
return nullptr;
}

CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegate * delegate)
CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegateBase * delegate)
{
UnsolicitedMessageHandler * umh = UMHandlerPool;
UnsolicitedMessageHandler * selected = nullptr;
Expand Down
17 changes: 9 additions & 8 deletions src/messaging/ExchangeMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace chip {
namespace Messaging {

class ExchangeContext;
class ExchangeDelegate;
class ExchangeDelegateBase;

static constexpr int16_t kAnyMessageType = -1;

Expand Down Expand Up @@ -98,7 +98,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans
* @return A pointer to the created ExchangeContext object On success. Otherwise NULL if no object
* can be allocated or is available.
*/
ExchangeContext * NewContext(SecureSessionHandle session, ExchangeDelegate * delegate);
ExchangeContext * NewContext(SecureSessionHandle session, ExchangeDelegateBase * delegate);

/**
* Register an unsolicited message handler for a given protocol identifier. This handler would be
Expand All @@ -114,7 +114,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans
* is full and a new one cannot be allocated.
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegate * delegate);
CHIP_ERROR RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegateBase * delegate);

/**
* Register an unsolicited message handler for a given protocol identifier and message type.
Expand All @@ -129,13 +129,13 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans
* is full and a new one cannot be allocated.
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType, ExchangeDelegate * delegate);
CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType, ExchangeDelegateBase * delegate);

/**
* A strongly-message-typed version of RegisterUnsolicitedMessageHandlerForType.
*/
template <typename MessageType, typename = std::enable_if_t<std::is_enum<MessageType>::value>>
CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(MessageType msgType, ExchangeDelegate * delegate)
CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(MessageType msgType, ExchangeDelegateBase * delegate)
{
static_assert(std::is_same<std::underlying_type_t<MessageType>, uint8_t>::value, "Enum is wrong size; cast is not safe");
return RegisterUnsolicitedMessageHandlerForType(Protocols::MessageTypeTraits<MessageType>::ProtocolId(),
Expand Down Expand Up @@ -229,7 +229,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans
return ProtocolId == aProtocolId && MessageType == aMessageType;
}

ExchangeDelegate * Delegate;
ExchangeDelegateBase * Delegate;
Protocols::Id ProtocolId;
// Message types are normally 8-bit unsigned ints, but we use
// kAnyMessageType, which is negative, to represent a wildcard handler,
Expand All @@ -256,9 +256,10 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans
BitMapObjectPool<ChannelContext, CHIP_CONFIG_MAX_ACTIVE_CHANNELS> mChannelContexts;
BitMapObjectPool<ChannelContextHandleAssociation, CHIP_CONFIG_MAX_CHANNEL_HANDLES> mChannelHandles;

ExchangeContext * AllocContext(uint16_t ExchangeId, SecureSessionHandle session, bool Initiator, ExchangeDelegate * delegate);
ExchangeContext * AllocContext(uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegateBase * delegate);

CHIP_ERROR RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegate * delegate);
CHIP_ERROR RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegateBase * delegate);
CHIP_ERROR UnregisterUMH(Protocols::Id protocolId, int16_t msgType);

void OnReceiveError(CHIP_ERROR error, const Transport::PeerAddress & source, SecureSessionMgr * msgLayer) override;
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/secure_channel/PASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct PASESessionSerializable

typedef uint8_t PASEVerifier[2][kSpake2p_WS_Length];

class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public PairingSession
class DLL_EXPORT PASESession : public Messaging::ExchangeDelegateBase, public PairingSession
{
public:
PASESession();
Expand Down
10 changes: 4 additions & 6 deletions src/protocols/secure_channel/RendezvousSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,11 @@ class RendezvousSession : public SessionEstablishmentDelegate,

PASESession mPairingSession;
NetworkProvisioning mNetworkProvision;
TransportMgrBase * mTransportMgr;
uint16_t mNextKeyId = 0;
SecureSessionMgr * mSecureSessionMgr = nullptr;
SecureSessionHandle * mPairingSessionHandle = nullptr;
Messaging::ExchangeManager * mExchangeManager = nullptr;

// SessionEstablishmentExchangeDispatch mExchangeMessageDispatch;
TransportMgrBase * mTransportMgr;
uint16_t mNextKeyId = 0;
SecureSessionMgr * mSecureSessionMgr = nullptr;
SecureSessionHandle * mPairingSessionHandle = nullptr;

Transport::AdminPairingInfo * mAdmin = nullptr;

Expand Down

0 comments on commit 23288cd

Please sign in to comment.