Skip to content

Commit 0578255

Browse files
authored
Merge branch 'master' into feature/message-cluster-present-message-update
2 parents ae408c9 + 219c3f0 commit 0578255

File tree

11 files changed

+97
-64
lines changed

11 files changed

+97
-64
lines changed

examples/chip-tool/commands/clusters/ModelCommand.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ModelCommand.h"
2020

2121
#include <app/InteractionModelEngine.h>
22+
#include <app/icd/client/DefaultICDClientStorage.h>
2223
#include <inttypes.h>
2324

2425
using namespace ::chip;
@@ -35,6 +36,7 @@ CHIP_ERROR ModelCommand::RunCommand()
3536
}
3637

3738
ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mDestinationId);
39+
CheckPeerICDType();
3840

3941
CommissioneeDeviceProxy * commissioneeDeviceProxy = nullptr;
4042
if (CHIP_NO_ERROR == CurrentCommissioner().GetDeviceBeingCommissioned(mDestinationId, &commissioneeDeviceProxy))
@@ -73,3 +75,31 @@ void ModelCommand::Shutdown()
7375

7476
CHIPCommand::Shutdown();
7577
}
78+
79+
void ModelCommand::CheckPeerICDType()
80+
{
81+
if (mIsPeerLIT.HasValue())
82+
{
83+
ChipLogProgress(chipTool, "Peer ICD type is set to %s", mIsPeerLIT.Value() == 1 ? "LIT-ICD" : "non LIT-ICD");
84+
return;
85+
}
86+
87+
app::ICDClientInfo info;
88+
auto destinationPeerId = chip::ScopedNodeId(mDestinationId, CurrentCommissioner().GetFabricIndex());
89+
auto iter = CHIPCommand::sICDClientStorage.IterateICDClientInfo();
90+
if (iter == nullptr)
91+
{
92+
return;
93+
}
94+
app::DefaultICDClientStorage::ICDClientInfoIteratorWrapper clientInfoIteratorWrapper(iter);
95+
96+
while (iter->Next(info))
97+
{
98+
if (ScopedNodeId(info.peer_node.GetNodeId(), info.peer_node.GetFabricIndex()) == destinationPeerId)
99+
{
100+
ChipLogProgress(chipTool, "Peer is a registered LIT ICD.");
101+
mIsPeerLIT.SetValue(true);
102+
return;
103+
}
104+
}
105+
}

examples/chip-tool/commands/clusters/ModelCommand.h

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class ModelCommand : public CHIPCommand
5252
"Endpoint the command is targeted at.");
5353
}
5454
}
55+
AddArgument(
56+
"lit-icd-peer", 0, 1, &mIsPeerLIT,
57+
"Whether to treat the peer as a LIT ICD. false: Always no, true: Always yes, (not set): Yes if the peer is registered "
58+
"to this controller.");
5559
AddArgument("timeout", 0, UINT16_MAX, &mTimeout);
5660
}
5761

@@ -66,11 +70,16 @@ class ModelCommand : public CHIPCommand
6670
void Shutdown() override;
6771

6872
protected:
73+
bool IsPeerLIT() { return mIsPeerLIT.ValueOr(false); }
74+
6975
chip::Optional<uint16_t> mTimeout;
7076

7177
private:
7278
chip::NodeId mDestinationId;
7379
std::vector<chip::EndpointId> mEndPointId;
80+
chip::Optional<bool> mIsPeerLIT;
81+
82+
void CheckPeerICDType();
7483

7584
static void OnDeviceConnectedFn(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
7685
const chip::SessionHandle & sessionHandle);

examples/chip-tool/commands/clusters/ReportCommand.h

+3
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ class SubscribeAttribute : public SubscribeCommand
277277

278278
CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
279279
{
280+
SubscribeCommand::SetPeerLIT(IsPeerLIT());
280281
return SubscribeCommand::SubscribeAttribute(device, endpointIds, mClusterIds, mAttributeIds);
281282
}
282283

@@ -407,6 +408,7 @@ class SubscribeEvent : public SubscribeCommand
407408

408409
CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
409410
{
411+
SubscribeCommand::SetPeerLIT(IsPeerLIT());
410412
return SubscribeCommand::SubscribeEvent(device, endpointIds, mClusterIds, mEventIds);
411413
}
412414

@@ -538,6 +540,7 @@ class SubscribeAll : public SubscribeCommand
538540

539541
CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
540542
{
543+
SubscribeCommand::SetPeerLIT(IsPeerLIT());
541544
return SubscribeCommand::SubscribeAll(device, endpointIds, mClusterIds, mAttributeIds, mEventIds);
542545
}
543546

examples/chip-tool/commands/icd/ICDCommand.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "ICDCommand.h"
2020

21+
#include <app/icd/client/DefaultICDClientStorage.h>
2122
#include <crypto/DefaultSessionKeystore.h>
2223
#include <crypto/RawKeySessionKeystore.h>
2324

@@ -29,6 +30,11 @@ CHIP_ERROR ICDListCommand::RunCommand()
2930
auto iter = CHIPCommand::sICDClientStorage.IterateICDClientInfo();
3031
char icdAesKeyHex[Crypto::kAES_CCM128_Key_Length * 2 + 1];
3132
char icdHmacKeyHex[Crypto::kHMAC_CCM128_Key_Length * 2 + 1];
33+
if (iter == nullptr)
34+
{
35+
return CHIP_ERROR_NO_MEMORY;
36+
}
37+
app::DefaultICDClientStorage::ICDClientInfoIteratorWrapper clientInfoIteratorWrapper(iter);
3238
fprintf(stderr, " +-----------------------------------------------------------------------------+\n");
3339
fprintf(stderr, " | %-75s |\n", "Known ICDs:");
3440
fprintf(stderr, " +-----------------------------------------------------------------------------+\n");
@@ -54,8 +60,6 @@ CHIP_ERROR ICDListCommand::RunCommand()
5460
}
5561

5662
fprintf(stderr, " +-----------------------------------------------------------------------------+\n");
57-
58-
iter->Release();
5963
SetCommandExitStatus(CHIP_NO_ERROR);
6064
return CHIP_NO_ERROR;
6165
}

src/app/icd/client/DefaultICDClientStorage.h

+22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ class DefaultICDClientStorage : public ICDClientStorage
5353
public:
5454
using ICDClientInfoIterator = CommonIterator<ICDClientInfo>;
5555

56+
// ICDClientInfoIterator wrapper to release ICDClientInfoIterator when it is out of scope
57+
class ICDClientInfoIteratorWrapper
58+
{
59+
public:
60+
ICDClientInfoIteratorWrapper(ICDClientInfoIterator * apICDClientInfoIterator)
61+
{
62+
mpICDClientInfoIterator = apICDClientInfoIterator;
63+
}
64+
65+
~ICDClientInfoIteratorWrapper()
66+
{
67+
if (mpICDClientInfoIterator != nullptr)
68+
{
69+
mpICDClientInfoIterator->Release();
70+
mpICDClientInfoIterator = nullptr;
71+
}
72+
}
73+
74+
private:
75+
ICDClientInfoIterator * mpICDClientInfoIterator = nullptr;
76+
};
77+
5678
static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_ICD_CLIENTS_INFO_STORAGE_CONCURRENT_ITERATORS;
5779

5880
CHIP_ERROR Init(PersistentStorageDelegate * clientInfoStore, Crypto::SymmetricKeystore * keyStore);

src/app/tests/TestDefaultICDClientStorage.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ void TestClientInfoCountMultipleFabric(nlTestSuite * apSuite, void * apContext)
179179
err = manager.DeleteEntry(ScopedNodeId(nodeId1, fabricId1));
180180
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
181181
iterator = manager.IterateICDClientInfo();
182+
NL_TEST_ASSERT(apSuite, iterator != nullptr);
183+
DefaultICDClientStorage::ICDClientInfoIteratorWrapper clientInfoIteratorWrapper(iterator);
182184
NL_TEST_ASSERT(apSuite, iterator->Count() == 2);
183185

184186
err = manager.DeleteEntry(ScopedNodeId(nodeId2, fabricId1));
@@ -196,7 +198,7 @@ void TestClientInfoCountMultipleFabric(nlTestSuite * apSuite, void * apContext)
196198
{
197199
count++;
198200
}
199-
iterator->Release();
201+
200202
NL_TEST_ASSERT(apSuite, count == 0);
201203
}
202204

src/app/tests/suites/commands/interaction_model/InteractionModel.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::v
350350
{
351351
params.mKeepSubscriptions = mKeepSubscriptions.Value();
352352
}
353+
params.mIsPeerLIT = mIsPeerLIT;
353354
}
354355

355356
auto client = std::make_unique<ReadClient>(InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
@@ -479,6 +480,7 @@ CHIP_ERROR InteractionModelReports::ReportEvent(DeviceProxy * device, std::vecto
479480
{
480481
params.mKeepSubscriptions = mKeepSubscriptions.Value();
481482
}
483+
params.mIsPeerLIT = mIsPeerLIT;
482484
}
483485

484486
auto client = std::make_unique<ReadClient>(InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
@@ -652,6 +654,7 @@ CHIP_ERROR InteractionModelReports::ReportAll(chip::DeviceProxy * device, std::v
652654
{
653655
params.mKeepSubscriptions = mKeepSubscriptions.Value();
654656
}
657+
params.mIsPeerLIT = mIsPeerLIT;
655658
}
656659

657660
auto client = std::make_unique<ReadClient>(InteractionModelEngine::GetInstance(), device->GetExchangeManager(),

src/app/tests/suites/commands/interaction_model/InteractionModel.h

+8
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ class InteractionModelReports
205205
return *this;
206206
}
207207

208+
InteractionModelReports & SetPeerLIT(bool isPeerLIT)
209+
{
210+
mIsPeerLIT = isPeerLIT;
211+
return *this;
212+
}
213+
208214
void ResetOptions()
209215
{
210216
mDataVersions = chip::NullOptional;
@@ -213,6 +219,7 @@ class InteractionModelReports
213219
mFabricFiltered = chip::Optional<bool>(true);
214220
mKeepSubscriptions = chip::NullOptional;
215221
mAutoResubscribe = chip::NullOptional;
222+
mIsPeerLIT = false;
216223
mMinInterval = 0;
217224
mMaxInterval = 0;
218225
}
@@ -223,6 +230,7 @@ class InteractionModelReports
223230
chip::Optional<bool> mFabricFiltered;
224231
chip::Optional<bool> mKeepSubscriptions;
225232
chip::Optional<bool> mAutoResubscribe;
233+
bool mIsPeerLIT;
226234
uint16_t mMinInterval;
227235
uint16_t mMaxInterval;
228236
};

src/controller/java/CHIPDeviceController-JNI.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,8 @@ JNI_METHOD(jobject, getICDClientInfo)(JNIEnv * env, jobject self, jlong handle,
21762176
ChipLogError(Controller, "CreateArrayList failed!: %" CHIP_ERROR_FORMAT, err.Format()));
21772177

21782178
auto iter = wrapper->getICDClientStorage()->IterateICDClientInfo();
2179+
VerifyOrReturnValue(iter != nullptr, nullptr, ChipLogError(Controller, "IterateICDClientInfo failed!"));
2180+
app::DefaultICDClientStorage::ICDClientInfoIteratorWrapper clientInfoIteratorWrapper(iter);
21792181

21802182
jmethodID constructor;
21812183
jclass infoClass;
@@ -2219,8 +2221,6 @@ JNI_METHOD(jobject, getICDClientInfo)(JNIEnv * env, jobject self, jlong handle,
22192221
ChipLogError(Controller, "AddToList error!: %" CHIP_ERROR_FORMAT, err.Format()));
22202222
}
22212223

2222-
iter->Release();
2223-
22242224
return jInfo;
22252225
}
22262226

src/lib/core/TLVWriter.cpp

+11-58
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ CHIP_ERROR TLVWriter::Init(TLVBackingStore & backingStore, uint32_t maxLen /* =
101101
if (err != CHIP_NO_ERROR)
102102
return err;
103103

104+
VerifyOrReturnError(mBufStart != nullptr, CHIP_ERROR_INTERNAL);
104105
mWritePoint = mBufStart;
105106
mInitializationCookie = kExpectedInitializationCookie;
106107
return CHIP_NO_ERROR;
@@ -363,11 +364,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
363364
size_t dataLen;
364365
CHIP_ERROR err = CHIP_NO_ERROR;
365366
TLVFieldSize lenFieldSize;
366-
#if CONFIG_HAVE_VSNPRINTF_EX
367-
size_t skipLen;
368-
size_t writtenBytes;
369-
#elif CONFIG_HAVE_VCBPRINTF
370-
#else
367+
#if !CONFIG_HAVE_VCBPRINTF
371368
char * tmpBuf;
372369
#endif
373370
va_copy(aq, ap);
@@ -396,47 +393,14 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
396393
VerifyOrExit((mLenWritten + dataLen) <= mMaxLen, err = CHIP_ERROR_BUFFER_TOO_SMALL);
397394

398395
// write data
399-
#if CONFIG_HAVE_VSNPRINTF_EX
400-
401-
skipLen = 0;
402-
403-
do
404-
{
405-
va_copy(aq, ap);
406-
407-
vsnprintf_ex(reinterpret_cast<char *>(mWritePoint), mRemainingLen, skipLen, fmt, aq);
408-
409-
va_end(aq);
410-
411-
writtenBytes = (mRemainingLen >= (dataLen - skipLen)) ? dataLen - skipLen : mRemainingLen;
412-
skipLen += writtenBytes;
413-
mWritePoint += writtenBytes;
414-
mRemainingLen -= writtenBytes;
415-
mLenWritten += writtenBytes;
416-
if (skipLen < dataLen)
417-
{
418-
VerifyOrExit(mBackingStore != NULL, err = CHIP_ERROR_NO_MEMORY);
419-
420-
err = mBackingStore->FinalizeBuffer(*this, mBufHandle, mBufStart, mWritePoint - mBufStart);
421-
SuccessOrExit(err);
422-
423-
err = mBackingStore->GetNewBuffer(*this, mBufHandle, mBufStart, mRemainingLen);
424-
SuccessOrExit(err);
425-
426-
mWritePoint = mBufStart;
427-
}
428-
429-
} while (skipLen < dataLen);
430-
431-
#elif CONFIG_HAVE_VCBPRINTF
396+
#if CONFIG_HAVE_VCBPRINTF
432397

433398
va_copy(aq, ap);
434399

435400
vcbprintf(TLVWriterPutcharCB, this, dataLen, fmt, aq);
436401

437402
va_end(aq);
438-
439-
#else // CONFIG_HAVE_VSNPRINTF_EX
403+
#else // CONFIG_HAVE_VCBPRINTF
440404

441405
tmpBuf = static_cast<char *>(chip::Platform::MemoryAlloc(dataLen + 1));
442406
VerifyOrExit(tmpBuf != nullptr, err = CHIP_ERROR_NO_MEMORY);
@@ -450,7 +414,7 @@ CHIP_ERROR TLVWriter::VPutStringF(Tag tag, const char * fmt, va_list ap)
450414
err = WriteData(reinterpret_cast<uint8_t *>(tmpBuf), static_cast<uint32_t>(dataLen));
451415
chip::Platform::MemoryFree(tmpBuf);
452416

453-
#endif // CONFIG_HAVE_VSNPRINTF_EX
417+
#endif // CONFIG_HAVE_VCBPRINTF
454418

455419
exit:
456420

@@ -694,19 +658,13 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
694658
ABORT_ON_UNINITIALIZED_IF_ENABLED();
695659

696660
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
697-
uint8_t * p;
698-
uint8_t stagingBuf[17]; // 17 = 1 control byte + 8 tag bytes + 8 length/value bytes
699-
700661
if (IsContainerOpen())
701662
return CHIP_ERROR_TLV_CONTAINER_OPEN;
702663

664+
uint8_t stagingBuf[17]; // 17 = 1 control byte + 8 tag bytes + 8 length/value bytes
665+
uint8_t * p = stagingBuf;
703666
uint32_t tagNum = TagNumFromTag(tag);
704667

705-
if ((mRemainingLen >= sizeof(stagingBuf)) && (mMaxLen >= sizeof(stagingBuf)))
706-
p = mWritePoint;
707-
else
708-
p = stagingBuf;
709-
710668
if (IsSpecialTag(tag))
711669
{
712670
if (tagNum <= Tag::kContextTagMaxNum)
@@ -799,15 +757,9 @@ CHIP_ERROR TLVWriter::WriteElementHead(TLVElementType elemType, Tag tag, uint64_
799757
break;
800758
}
801759

802-
if ((mRemainingLen >= sizeof(stagingBuf)) && (mMaxLen >= sizeof(stagingBuf)))
803-
{
804-
uint32_t len = static_cast<uint32_t>(p - mWritePoint);
805-
mWritePoint = p;
806-
mRemainingLen -= len;
807-
mLenWritten += len;
808-
return CHIP_NO_ERROR;
809-
}
810-
return WriteData(stagingBuf, static_cast<uint32_t>(p - stagingBuf));
760+
uint32_t bytesStaged = static_cast<uint32_t>(p - stagingBuf);
761+
VerifyOrDie(bytesStaged <= sizeof(stagingBuf));
762+
return WriteData(stagingBuf, bytesStaged);
811763
}
812764

813765
CHIP_ERROR TLVWriter::WriteElementWithData(TLVType type, Tag tag, const uint8_t * data, uint32_t dataLen)
@@ -855,6 +807,7 @@ CHIP_ERROR TLVWriter::WriteData(const uint8_t * p, uint32_t len)
855807
ReturnErrorOnFailure(mBackingStore->FinalizeBuffer(*this, mBufStart, static_cast<uint32_t>(mWritePoint - mBufStart)));
856808

857809
ReturnErrorOnFailure(mBackingStore->GetNewBuffer(*this, mBufStart, mRemainingLen));
810+
VerifyOrReturnError(mRemainingLen > 0, CHIP_ERROR_NO_MEMORY);
858811

859812
mWritePoint = mBufStart;
860813

src/system/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ buildconfig_header("system_buildconfig") {
118118
"HAVE_NETINET_ICMP6_H=true",
119119
"HAVE_ICMP6_FILTER=true",
120120
"CONFIG_HAVE_VCBPRINTF=false",
121-
"CONFIG_HAVE_VSNPRINTF_EX=false",
122121
"HAVE_SYS_SOCKET_H=${chip_system_config_use_sockets}",
123122
]
124123

0 commit comments

Comments
 (0)