Skip to content

Commit aaab48e

Browse files
committed
- Creating an overload of DecodeMRPParametersIfPresent that takes SessionParameters as an outparam and does not touch PairingSession class member mRemoteSessionParameters
- a utility SetRemoteSessionParameters to be able to set PairingSession class member mRemoteSessionParameters
1 parent 2249628 commit aaab48e

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

src/protocols/secure_channel/PairingSession.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,104 @@ CHIP_ERROR PairingSession::DecodeMRPParametersIfPresent(TLV::Tag expectedTag, TL
241241
}
242242
return err;
243243
}
244+
CHIP_ERROR PairingSession::DecodeMRPParametersIfPresent(TLV::Tag expectedTag, TLV::ContiguousBufferTLVReader & tlvReader,
245+
SessionParameters & sessionParameters)
246+
{
247+
CHIP_ERROR err = CHIP_NO_ERROR;
248+
249+
// The MRP parameters are optional.
250+
if (tlvReader.GetTag() != expectedTag)
251+
{
252+
return CHIP_NO_ERROR;
253+
}
254+
255+
TLV::TLVType containerType = TLV::kTLVType_Structure;
256+
ReturnErrorOnFailure(tlvReader.EnterContainer(containerType));
257+
258+
ReturnErrorOnFailure(tlvReader.Next());
259+
260+
ChipLogDetail(SecureChannel, "Found MRP parameters in the message");
261+
262+
// All TLV elements in the structure are optional. If the first element is present, process it and move
263+
// the TLV reader to the next element.
264+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kSessionIdleInterval)
265+
{
266+
uint32_t idleRetransTimeout;
267+
ReturnErrorOnFailure(tlvReader.Get(idleRetransTimeout));
268+
sessionParameters.SetMRPIdleRetransTimeout(System::Clock::Milliseconds32(idleRetransTimeout));
269+
270+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
271+
SuccessOrExit(err = tlvReader.Next());
272+
}
273+
274+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kSessionActiveInterval)
275+
{
276+
uint32_t activeRetransTimeout;
277+
ReturnErrorOnFailure(tlvReader.Get(activeRetransTimeout));
278+
sessionParameters.SetMRPActiveRetransTimeout(System::Clock::Milliseconds32(activeRetransTimeout));
279+
280+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
281+
SuccessOrExit(err = tlvReader.Next());
282+
}
283+
284+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kSessionActiveThreshold)
285+
{
286+
uint16_t activeThresholdTime;
287+
ReturnErrorOnFailure(tlvReader.Get(activeThresholdTime));
288+
sessionParameters.SetMRPActiveThresholdTime(System::Clock::Milliseconds16(activeThresholdTime));
289+
290+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
291+
SuccessOrExit(err = tlvReader.Next());
292+
}
244293

294+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kDataModelRevision)
295+
{
296+
uint16_t dataModelRevision;
297+
ReturnErrorOnFailure(tlvReader.Get(dataModelRevision));
298+
sessionParameters.SetDataModelRevision(dataModelRevision);
299+
300+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
301+
SuccessOrExit(err = tlvReader.Next());
302+
}
303+
304+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kInteractionModelRevision)
305+
{
306+
uint16_t interactionModelRevision;
307+
ReturnErrorOnFailure(tlvReader.Get(interactionModelRevision));
308+
sessionParameters.SetInteractionModelRevision(interactionModelRevision);
309+
310+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
311+
SuccessOrExit(err = tlvReader.Next());
312+
}
313+
314+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kSpecificationVersion)
315+
{
316+
uint32_t specificationVersion;
317+
ReturnErrorOnFailure(tlvReader.Get(specificationVersion));
318+
sessionParameters.SetSpecificationVersion(specificationVersion);
319+
320+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
321+
SuccessOrExit(err = tlvReader.Next());
322+
}
323+
324+
if (TLV::TagNumFromTag(tlvReader.GetTag()) == SessionParameters::Tag::kMaxPathsPerInvoke)
325+
{
326+
uint16_t maxPathsPerInvoke;
327+
ReturnErrorOnFailure(tlvReader.Get(maxPathsPerInvoke));
328+
sessionParameters.SetMaxPathsPerInvoke(maxPathsPerInvoke);
329+
330+
// The next element is optional. If it's not present, return CHIP_NO_ERROR.
331+
SuccessOrExit(err = tlvReader.Next());
332+
}
333+
334+
// Future proofing - Don't error out if there are other tags
335+
exit:
336+
if (err == CHIP_END_OF_TLV)
337+
{
338+
return tlvReader.ExitContainer(containerType);
339+
}
340+
return err;
341+
}
245342
bool PairingSession::IsSessionEstablishmentInProgress()
246343
{
247344
if (!mSecureSessionHolder)

src/protocols/secure_channel/PairingSession.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class DLL_EXPORT PairingSession : public SessionDelegate
100100

101101
const ReliableMessageProtocolConfig & GetRemoteMRPConfig() const { return mRemoteSessionParams.GetMRPConfig(); }
102102
const SessionParameters & GetRemoteSessionParameters() const { return mRemoteSessionParams; }
103+
void SetRemoteSessionParameters(const SessionParameters & sessionParams) { mRemoteSessionParams = sessionParams; }
103104
void SetRemoteMRPConfig(const ReliableMessageProtocolConfig & config) { mRemoteSessionParams.SetMRPConfig(config); }
104105

105106
/**
@@ -208,7 +209,7 @@ class DLL_EXPORT PairingSession : public SessionDelegate
208209

209210
/**
210211
* Try to decode the current element (pointed by the TLV reader) as MRP parameters.
211-
* If the MRP parameters are found, mRemoteSessionParams is updated with the devoded values.
212+
* If the MRP parameters are found, mRemoteSessionParams is updated with the decoded values.
212213
*
213214
* MRP parameters are optional. So, if the TLV reader is not pointing to the MRP parameters,
214215
* the function is a noop.
@@ -218,6 +219,19 @@ class DLL_EXPORT PairingSession : public SessionDelegate
218219
*/
219220
CHIP_ERROR DecodeMRPParametersIfPresent(TLV::Tag expectedTag, TLV::ContiguousBufferTLVReader & tlvReader);
220221

222+
/**
223+
* Try to decode the current element (pointed by the TLV reader) as MRP parameters.
224+
* If the MRP parameters are found, outparam sessionParameters is updated with the decoded values.
225+
*
226+
* MRP parameters are optional. So, if the TLV reader is not pointing to the MRP parameters,
227+
* the function is a noop.
228+
*
229+
* If the parameters are present, but TLV reader fails to correctly parse it, the function will
230+
* return the corresponding error.
231+
*/
232+
static CHIP_ERROR DecodeMRPParametersIfPresent(TLV::Tag expectedTag, TLV::ContiguousBufferTLVReader & tlvReader,
233+
SessionParameters & sessionParameters);
234+
221235
bool IsSessionEstablishmentInProgress();
222236

223237
// TODO: remove Clear, we should create a new instance instead reset the old instance.

0 commit comments

Comments
 (0)