Skip to content

Commit

Permalink
API SecureManager::SendMessage, use SecureSessoinHandle instead NodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Dec 14, 2020
1 parent 34d8115 commit 7ced116
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 165 deletions.
8 changes: 4 additions & 4 deletions examples/common/chip-app-server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class ServerCallback : public SecureSessionMgrDelegate
{
public:
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
const Transport::PeerConnectionState * state, System::PacketBufferHandle buffer,
SecureSessionHandle session, System::PacketBufferHandle buffer,
SecureSessionMgr * mgr) override
{
auto state = mgr->GetPeerConnectionState(session);
const size_t data_len = buffer->DataLength();
char src_addr[PeerAddress::kMaxToStringSize];

Expand Down Expand Up @@ -92,7 +93,7 @@ class ServerCallback : public SecureSessionMgrDelegate
}
}

void OnNewConnection(const Transport::PeerConnectionState * state, SecureSessionMgr * mgr) override
void OnNewConnection(SecureSessionHandle session, SecureSessionMgr * mgr) override
{
ChipLogProgress(AppServer, "Received a new connection.");
}
Expand Down Expand Up @@ -181,11 +182,10 @@ void InitServer(AppDelegate * delegate)
SuccessOrExit(err);
#endif

gSessions.SetDelegate(&gCallbacks);
err = gSessions.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing);
SuccessOrExit(err);

gSessions.SetDelegate(&gCallbacks);

exit:
if (err != CHIP_NO_ERROR)
{
Expand Down
3 changes: 2 additions & 1 deletion src/app/util/chip-message-send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ EmberStatus chipSendUnicast(NodeId destination, EmberApsFrame * apsFrame, uint16
memcpy(buffer->Start() + frameSize, message, messageLength);
buffer->SetDataLength(dataLength);

CHIP_ERROR err = SessionManager().SendMessage(destination, std::move(buffer));
// TODO: temprary create a handle from node id, will be fix in PR 3602
CHIP_ERROR err = SessionManager().SendMessage({destination, Transport::kAnyKeyId}, std::move(buffer));
if (err != CHIP_NO_ERROR)
{
// FIXME: Figure out better translations between our error types?
Expand Down
20 changes: 15 additions & 5 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ CHIP_ERROR Device::SendMessage(System::PacketBufferHandle buffer)
resend = buffer.Retain();
}

err = mSessionManager->SendMessage(mDeviceId, std::move(buffer));
err = mSessionManager->SendMessage(mSecureSession, std::move(buffer));
buffer = nullptr;
ChipLogDetail(Controller, "SendMessage returned %d", err);

Expand All @@ -87,7 +87,7 @@ CHIP_ERROR Device::SendMessage(System::PacketBufferHandle buffer)
err = LoadSecureSessionParameters(ResetTransport::kYes);
SuccessOrExit(err);

err = mSessionManager->SendMessage(mDeviceId, std::move(resend));
err = mSessionManager->SendMessage(mSecureSession, std::move(resend));
ChipLogDetail(Controller, "Re-SendMessage returned %d", err);
SuccessOrExit(err);
}
Expand Down Expand Up @@ -175,8 +175,20 @@ CHIP_ERROR Device::Deserialize(const SerializedDevice & input)
return error;
}

void Device::OnNewConnection(SecureSessionHandle session, SecureSessionMgr * mgr)
{
mState = ConnectionState::SecureConnected;
mSecureSession = session;
}

void Device::OnConnectionExpired(SecureSessionHandle session, SecureSessionMgr * mgr)
{
mState = ConnectionState::NotConnected;
mSecureSession = SecureSessionHandle{};
}

void Device::OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
const Transport::PeerConnectionState * state, System::PacketBufferHandle msgBuf,
SecureSessionHandle session, System::PacketBufferHandle msgBuf,
SecureSessionMgr * mgr)
{
if (mState == ConnectionState::SecureConnected)
Expand Down Expand Up @@ -255,8 +267,6 @@ CHIP_ERROR Device::LoadSecureSessionParameters(ResetTransport resetNeeded)
&pairingSession);
SuccessOrExit(err);

mState = ConnectionState::SecureConnected;

exit:

if (err != CHIP_NO_ERROR)
Expand Down
30 changes: 28 additions & 2 deletions src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ class DLL_EXPORT Device
**/
CHIP_ERROR Deserialize(const SerializedDevice & input);

/**
* @brief
* Called when a new pairing is being established
*
* @param session A handle to the secure session
* @param mgr A pointer to the SecureSessionMgr
*/
void OnNewConnection(SecureSessionHandle session, SecureSessionMgr * mgr);

/**
* @brief
* Called when a connection is closing.
*
* The receiver should release all resources associated with the connection.
*
* @param session A handle to the secure session
* @param mgr A pointer to the SecureSessionMgr
*/
void OnConnectionExpired(SecureSessionHandle session, SecureSessionMgr * mgr);

/**
* @brief
* This function is called when a message is received from the corresponding CHIP
Expand All @@ -164,12 +184,12 @@ class DLL_EXPORT Device
*
* @param[in] header Reference to common packet header of the received message
* @param[in] payloadHeader Reference to payload header in the message
* @param[in] state Pointer to the peer connection state on which message is received
* @param[in] session A handle to the secure session
* @param[in] msgBuf The message buffer
* @param[in] mgr Pointer to secure session manager which received the message
*/
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
const Transport::PeerConnectionState * state, System::PacketBufferHandle msgBuf, SecureSessionMgr * mgr);
SecureSessionHandle session, System::PacketBufferHandle msgBuf, SecureSessionMgr * mgr);

/**
* @brief
Expand All @@ -180,6 +200,8 @@ class DLL_EXPORT Device

void SetActive(bool active) { mActive = active; }

bool IsSecureConnected() const { return IsActive() && mState == ConnectionState::SecureConnected; }

void Reset()
{
SetActive(false);
Expand All @@ -191,6 +213,8 @@ class DLL_EXPORT Device

NodeId GetDeviceId() const { return mDeviceId; }

bool MatchesSession(SecureSessionHandle session) const { return mSecureSession == session; }

void SetAddress(const Inet::IPAddress & deviceAddr) { mDeviceAddr = deviceAddr; }

SecurePairingSessionSerializable & GetPairing() { return mPairing; }
Expand Down Expand Up @@ -242,6 +266,8 @@ class DLL_EXPORT Device

DeviceTransportMgr * mTransportMgr;

SecureSessionHandle mSecureSession = {};

/* Track all outstanding response callbacks for this device. The callbacks are
registered when a command is sent to the device, to get notified with the results. */
Callback::CallbackDeque mResponses;
Expand Down
60 changes: 54 additions & 6 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,58 @@ CHIP_ERROR DeviceController::ServiceEventSignal()
return err;
}

void DeviceController::OnNewConnection(const Transport::PeerConnectionState * peerConnection, SecureSessionMgr * mgr) {}
void DeviceController::OnNewConnection(SecureSessionHandle session, SecureSessionMgr * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);

index = FindDeviceIndex(mgr->GetPeerConnectionState(session)->GetPeerNodeId());
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnNewConnection(session, mgr);

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to process received message: err %d", err);
}
}

void DeviceController::OnConnectionExpired(SecureSessionHandle session, SecureSessionMgr * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);

index = FindDeviceIndex(session);
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnConnectionExpired(session, mgr);

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to process received message: err %d", err);
}
}

void DeviceController::OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
const Transport::PeerConnectionState * state, System::PacketBufferHandle msgBuf,
SecureSessionHandle session, System::PacketBufferHandle msgBuf,
SecureSessionMgr * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;
NodeId peer;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(header.GetSourceNodeId().HasValue(), err = CHIP_ERROR_INVALID_ARGUMENT);

peer = header.GetSourceNodeId().Value();
index = FindDeviceIndex(peer);
index = FindDeviceIndex(session);
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnMessageReceived(header, payloadHeader, state, std::move(msgBuf), mgr);
mActiveDevices[index].OnMessageReceived(header, payloadHeader, session, std::move(msgBuf), mgr);

exit:
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -395,6 +429,20 @@ void DeviceController::ReleaseAllDevices()
}
}

uint16_t DeviceController::FindDeviceIndex(SecureSessionHandle session)
{
uint16_t i = 0;
while (i < kNumMaxActiveDevices)
{
if (mActiveDevices[i].IsActive() && mActiveDevices[i].IsSecureConnected() && mActiveDevices[i].MatchesSession(session))
{
return i;
}
i++;
}
return i;
}

uint16_t DeviceController::FindDeviceIndex(NodeId id)
{
uint16_t i = 0;
Expand Down
8 changes: 5 additions & 3 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,19 @@ class DLL_EXPORT DeviceController : public SecureSessionMgrDelegate, public Pers

uint16_t mListenPort;
uint16_t GetInactiveDeviceIndex();
uint16_t FindDeviceIndex(NodeId id);
uint16_t FindDeviceIndex(SecureSessionHandle session);
[[deprecated("only peer node id is not sufficient to identify a device")]] uint16_t FindDeviceIndex(NodeId id);
void ReleaseDevice(uint16_t index);
CHIP_ERROR SetPairedDeviceList(const char * pairedDeviceSerializedSet);

private:
//////////// SecureSessionMgrDelegate Implementation ///////////////
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
const Transport::PeerConnectionState * state, System::PacketBufferHandle msgBuf,
SecureSessionHandle session, System::PacketBufferHandle msgBuf,
SecureSessionMgr * mgr) override;

void OnNewConnection(const Transport::PeerConnectionState * state, SecureSessionMgr * mgr) override;
void OnNewConnection(SecureSessionHandle session, SecureSessionMgr * mgr) override;
void OnConnectionExpired(SecureSessionHandle session, SecureSessionMgr * mgr) override;

//////////// PersistentStorageResultDelegate Implementation ///////////////
void OnValue(const char * key, const char * value) override;
Expand Down
15 changes: 8 additions & 7 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ CHIP_ERROR ExchangeContext::SendMessage(uint16_t protocolId, uint8_t msgType, Pa

payloadHeader.SetInitiator(IsInitiator());

err = mExchangeMgr->GetSessionMgr()->SendMessage(payloadHeader, mPeerNodeId, std::move(msgBuf));
err = mExchangeMgr->GetSessionMgr()->SendMessage(mSecureSession, payloadHeader, std::move(msgBuf));
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -191,7 +191,7 @@ void ExchangeContext::Reset()
*this = ExchangeContext();
}

ExchangeContext * ExchangeContext::Alloc(ExchangeManager * em, uint16_t ExchangeId, uint64_t PeerNodeId, bool Initiator,
ExchangeContext * ExchangeContext::Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegate * delegate)
{
VerifyOrDie(delegate != nullptr);
Expand All @@ -201,8 +201,8 @@ ExchangeContext * ExchangeContext::Alloc(ExchangeManager * em, uint16_t Exchange
Retain();
mExchangeMgr = em;
em->IncrementContextsInUse();
mExchangeId = ExchangeId;
mPeerNodeId = PeerNodeId;
mExchangeId = ExchangeId;
mSecureSession = session;
mFlags.Set(ExFlagValues::kFlagInitiator, Initiator);
mDelegate = delegate;

Expand Down Expand Up @@ -236,16 +236,17 @@ void ExchangeContext::Free()
SYSTEM_STATS_DECREMENT(chip::System::Stats::kExchangeMgr_NumContexts);
}

bool ExchangeContext::MatchExchange(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader)
bool ExchangeContext::MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader,
const PayloadHeader & payloadHeader)
{
// A given message is part of a particular exchange if...
return

// The exchange identifier of the message matches the exchange identifier of the context.
(mExchangeId == payloadHeader.GetExchangeID())

// AND The message was received from the peer node associated with the exchange, or the peer node identifier is 'any'.
&& ((mPeerNodeId == kAnyNodeId) || (mPeerNodeId == packetHeader.GetSourceNodeId().Value()))
// AND The message was received from the peer node associated with the exchange
&& (mSecureSession == session)

// AND The message was sent by an initiator and the exchange context is a responder (IsInitiator==false)
// OR The message was sent by a responder and the exchange context is an initiator (IsInitiator==true) (for the broadcast
Expand Down
12 changes: 7 additions & 5 deletions src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch

ExchangeManager * GetExchangeMgr() const { return mExchangeMgr; }

uint64_t GetPeerNodeId() const { return mPeerNodeId; }
SecureSessionHandle GetSecureSession() { return mSecureSession; }

uint16_t GetExchangeId() const { return mExchangeId; }

Expand All @@ -141,7 +141,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
void Close();
void Abort();

ExchangeContext * Alloc(ExchangeManager * em, uint16_t ExchangeId, uint64_t PeerNodeId, bool Initiator,
ExchangeContext * Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
ExchangeDelegate * delegate);
void Free();
void Reset();
Expand All @@ -159,22 +159,24 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
ExchangeDelegate * mDelegate = nullptr;
ExchangeManager * mExchangeMgr = nullptr;

uint64_t mPeerNodeId; // Node ID of peer node.
uint16_t mExchangeId; // Assigned exchange ID.
SecureSessionHandle mSecureSession; // The connection state
uint16_t mExchangeId; // Assigned exchange ID.

BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags

/**
* Search for an existing exchange that the message applies to.
*
* @param[in] session The secure session of the received message.
*
* @param[in] packetHeader A reference to the PacketHeader object.
*
* @param[in] payloadHeader A reference to the PayloadHeader object.
*
* @retval true If a match is found.
* @retval false If a match is not found.
*/
bool MatchExchange(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader);
bool MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader);

CHIP_ERROR StartResponseTimer();
void CancelResponseTimer();
Expand Down
Loading

0 comments on commit 7ced116

Please sign in to comment.