Skip to content

Commit

Permalink
update eventDataIB and add eventReport, eventReports
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Nov 3, 2021
1 parent fe62374 commit 27566a0
Show file tree
Hide file tree
Showing 24 changed files with 1,194 additions and 988 deletions.
10 changes: 6 additions & 4 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ static_library("app") {
"MessageDef/CommandDataIB.cpp",
"MessageDef/CommandPathIB.cpp",
"MessageDef/CommandStatusIB.cpp",
"MessageDef/EventDataElement.cpp",
"MessageDef/EventDataElement.h",
"MessageDef/EventDataIB.cpp",
"MessageDef/EventDataIB.h",
"MessageDef/EventFilterIB.cpp",
"MessageDef/EventFilters.cpp",
"MessageDef/EventList.cpp",
"MessageDef/EventList.h",
"MessageDef/EventPathIB.cpp",
"MessageDef/EventPathIB.h",
"MessageDef/EventPaths.cpp",
"MessageDef/EventPaths.h",
"MessageDef/EventReportIB.cpp",
"MessageDef/EventReportIB.h",
"MessageDef/EventReports.cpp",
"MessageDef/EventReports.h",
"MessageDef/EventStatusIB.cpp",
"MessageDef/EventStatusIB.h",
"MessageDef/InvokeRequestMessage.cpp",
Expand Down
144 changes: 98 additions & 46 deletions src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct EventEnvelopeContext
{
EventEnvelopeContext() {}

uint16_t mFieldsToRead = 0;
int mFieldsToRead = 0;
/* PriorityLevel and DeltaSystemTimestamp are there if that is not first event when putting events in report*/
Timestamp mDeltaSystemTime = Timestamp::System(System::Clock::kZero);
Timestamp mDeltaUtc = Timestamp::UTC(0);
Expand Down Expand Up @@ -173,8 +173,7 @@ CHIP_ERROR EventManagement::CopyToNextBuffer(CircularEventBuffer * apEventBuffer
err = writer.Finalize();
SuccessOrExit(err);

ChipLogProgress(EventLogging, "Copy Event to next buffer with priority %u",
static_cast<unsigned>(nextBuffer->GetPriorityLevel()));
ChipLogProgress(EventLogging, "Copy Event to next buffer with priority %u", static_cast<unsigned>(nextBuffer->GetPriority()));
exit:
if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -302,8 +301,12 @@ CHIP_ERROR EventManagement::ConstructEvent(EventLoadOutContext * apContext, Even
CHIP_ERROR err = CHIP_NO_ERROR;
TLVWriter checkpoint = apContext->mWriter;
TLV::TLVType dataContainerType;
EventDataElement::Builder eventDataElementBuilder;
EventReportIB::Builder eventReportBuilder;
EventDataIB::Builder eventDataIBBuilder;
EventPathIB::Builder eventPathBuilder;
EventStatusIB::Builder eventStatusIBBuilder;
StatusIB::Builder statusIBBuilder;
StatusIB status;
uint64_t deltatime = 0;

VerifyOrExit(apContext->mCurrentEventNumber >= apContext->mStartingEventNumber,
Expand All @@ -312,9 +315,32 @@ CHIP_ERROR EventManagement::ConstructEvent(EventLoadOutContext * apContext, Even
VerifyOrExit(apOptions != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(apOptions->mTimestamp.mType != Timestamp::Type::kInvalid, err = CHIP_ERROR_INVALID_ARGUMENT);

eventDataElementBuilder.Init(&(apContext->mWriter));
eventPathBuilder = eventDataElementBuilder.CreateEventPath();
err = eventPathBuilder.GetError();
eventReportBuilder.Init(&(apContext->mWriter));
// TODO: Update IsUrgent, issue 11386
// TODO: Update statusIB, issue 11388
eventStatusIBBuilder = eventReportBuilder.CreateEventStatus();
eventPathBuilder = eventStatusIBBuilder.CreatePath();
err = eventStatusIBBuilder.GetError();
SuccessOrExit(err);
eventPathBuilder.Node(apOptions->mpEventSchema->mNodeId)
.Endpoint(apOptions->mpEventSchema->mEndpointId)
.Cluster(apOptions->mpEventSchema->mClusterId)
.Event(apOptions->mpEventSchema->mEventId)
.IsUrgent(false)
.EndOfEventPathIB();
err = eventPathBuilder.GetError();
SuccessOrExit(err);
statusIBBuilder = eventStatusIBBuilder.CreateErrorStatus();
err = eventStatusIBBuilder.GetError();
SuccessOrExit(err);
statusIBBuilder.EncodeStatusIB(status);
eventStatusIBBuilder.EndOfEventStatusIB();
err = statusIBBuilder.GetError();
SuccessOrExit(err);

eventDataIBBuilder = eventReportBuilder.CreateEventData();
eventPathBuilder = eventDataIBBuilder.CreatePath();
err = eventDataIBBuilder.GetError();
SuccessOrExit(err);

// TODO: Revisit NodeId since the the encoding spec and the IM seem to disagree on how this stuff works
Expand All @@ -327,15 +353,16 @@ CHIP_ERROR EventManagement::ConstructEvent(EventLoadOutContext * apContext, Even
err = eventPathBuilder.GetError();
SuccessOrExit(err);

eventDataElementBuilder.PriorityLevel(static_cast<uint8_t>(apContext->mPriority));
eventDataIBBuilder.Priority(chip::to_underlying(apContext->mPriority));

// TODO: need to add utc and systen system check here
deltatime = apOptions->mTimestamp.mValue - apContext->mCurrentSystemTime.mValue;
eventDataElementBuilder.DeltaSystemTimestamp(deltatime);
err = eventDataElementBuilder.GetError();
eventDataIBBuilder.DeltaSystemTimestamp(deltatime);
err = eventDataIBBuilder.GetError();
SuccessOrExit(err);

err = apContext->mWriter.StartContainer(ContextTag(EventDataElement::kCsTag_Data), TLV::kTLVType_Structure, dataContainerType);
err = apContext->mWriter.StartContainer(ContextTag(chip::to_underlying(EventDataIB::Tag::kData)), TLV::kTLVType_Structure,
dataContainerType);
SuccessOrExit(err);
// Callback to write the EventData
err = apDelegate->WriteEvent(apContext->mWriter);
Expand All @@ -344,8 +371,10 @@ CHIP_ERROR EventManagement::ConstructEvent(EventLoadOutContext * apContext, Even
err = apContext->mWriter.EndContainer(dataContainerType);
SuccessOrExit(err);

eventDataElementBuilder.EndOfEventDataElement();
SuccessOrExit(err = eventDataElementBuilder.GetError());
eventDataIBBuilder.EndOfEventDataIB();
SuccessOrExit(err = eventDataIBBuilder.GetError());
eventReportBuilder.EndOfEventReportIB();
SuccessOrExit(err = eventReportBuilder.GetError());

err = apContext->mWriter.Finalize();
SuccessOrExit(err);
Expand Down Expand Up @@ -438,16 +467,16 @@ CHIP_ERROR EventManagement::CopyAndAdjustDeltaTime(const TLVReader & aReader, si
TLVReader reader(aReader);

// TODO: Add UTC timestamp support
if (aReader.GetTag() == TLV::ContextTag(EventDataElement::kCsTag_DeltaSystemTimestamp))
if (aReader.GetTag() == TLV::ContextTag(to_underlying(EventDataIB::Tag::kDeltaSystemTimestamp)))
{
if (ctx->mpContext->mFirst) // First event gets a timestamp, subsequent ones get a delta T
{
err = ctx->mpWriter->Put(TLV::ContextTag(EventDataElement::kCsTag_SystemTimestamp),
err = ctx->mpWriter->Put(TLV::ContextTag(to_underlying(EventDataIB::Tag::kSystemTimestamp)),
ctx->mpContext->mCurrentSystemTime.mValue);
}
else
{
err = ctx->mpWriter->Put(TLV::ContextTag(EventDataElement::kCsTag_DeltaSystemTimestamp),
err = ctx->mpWriter->Put(TLV::ContextTag(to_underlying(EventDataIB::Tag::kDeltaSystemTimestamp)),
ctx->mpContext->mCurrentSystemTime.mValue - ctx->mpContext->mPreviousSystemTime.mValue);
}
}
Expand All @@ -458,11 +487,12 @@ CHIP_ERROR EventManagement::CopyAndAdjustDeltaTime(const TLVReader & aReader, si

// First event in the sequence gets a event number neatly packaged
// right after the priority to keep tags ordered
if (aReader.GetTag() == TLV::ContextTag(EventDataElement::kCsTag_PriorityLevel))
if (aReader.GetTag() == TLV::ContextTag(to_underlying(EventDataIB::Tag::kPriority)))
{
if (ctx->mpContext->mFirst)
{
err = ctx->mpWriter->Put(TLV::ContextTag(EventDataElement::kCsTag_Number), ctx->mpContext->mCurrentEventNumber);
err = ctx->mpWriter->Put(TLV::ContextTag(to_underlying(EventDataIB::Tag::kEventNumber)),
ctx->mpContext->mCurrentEventNumber);
}
}

Expand Down Expand Up @@ -583,31 +613,46 @@ CHIP_ERROR EventManagement::CopyEvent(const TLVReader & aReader, TLVWriter & aWr
{
TLVReader reader;
TLVType containerType;
TLVType containerType1;
TLVType containerType2;
CopyAndAdjustDeltaTimeContext context(&aWriter, apContext);
CHIP_ERROR err = CHIP_NO_ERROR;

reader.Init(aReader);
err = reader.EnterContainer(containerType);
SuccessOrExit(err);

err = aWriter.StartContainer(AnonymousTag, kTLVType_Structure, containerType);
SuccessOrExit(err);
ReturnErrorOnFailure(reader.EnterContainer(containerType));
ReturnErrorOnFailure(aWriter.StartContainer(AnonymousTag, kTLVType_Structure, containerType));

ReturnErrorOnFailure(reader.Next());
ReturnErrorOnFailure(reader.EnterContainer(containerType1));
ReturnErrorOnFailure(aWriter.StartContainer(TLV::ContextTag(to_underlying(EventReportIB::Tag::kEventStatus)),
kTLVType_Structure, containerType1));
ReturnErrorOnFailure(reader.Next());
do
{
ReturnErrorOnFailure(aWriter.CopyElement(reader));
} while (CHIP_NO_ERROR == (err = reader.Next()));
if (err == CHIP_END_OF_TLV)
{
err = CHIP_NO_ERROR;
}
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(reader.ExitContainer(containerType1));
ReturnErrorOnFailure(aWriter.EndContainer(containerType1));

ReturnErrorOnFailure(reader.Next());
ReturnErrorOnFailure(reader.EnterContainer(containerType2));
ReturnErrorOnFailure(
aWriter.StartContainer(TLV::ContextTag(to_underlying(EventReportIB::Tag::kEventData)), kTLVType_Structure, containerType2));
err = TLV::Utilities::Iterate(reader, CopyAndAdjustDeltaTime, &context, false /*recurse*/);
if (err == CHIP_END_OF_TLV)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);

err = aWriter.EndContainer(containerType);
SuccessOrExit(err);

err = aWriter.Finalize();
SuccessOrExit(err);

exit:
return err;
ReturnErrorOnFailure(err);
ReturnErrorOnFailure(aWriter.EndContainer(containerType2));
ReturnErrorOnFailure(aWriter.EndContainer(containerType));
ReturnErrorOnFailure(aWriter.Finalize());
return CHIP_NO_ERROR;
}

static bool IsInterestedEventPaths(EventLoadOutContext * eventLoadOutContext, const EventEnvelopeContext & event)
Expand All @@ -634,11 +679,15 @@ CHIP_ERROR EventManagement::EventIterator(const TLVReader & aReader, size_t aDep
CHIP_ERROR err = CHIP_NO_ERROR;
TLVReader innerReader;
TLVType tlvType;
TLVType tlvType1;
EventEnvelopeContext event;

innerReader.Init(aReader);
ReturnErrorOnFailure(innerReader.EnterContainer(tlvType));

ReturnErrorOnFailure(innerReader.Next());
// Skip EventStatus Element
ReturnErrorOnFailure(innerReader.Next());
ReturnErrorOnFailure(innerReader.EnterContainer(tlvType1));
err = TLV::Utilities::Iterate(innerReader, FetchEventParameters, &event, false /*recurse*/);
if (event.mFieldsToRead != kRequiredEventField)
{
Expand All @@ -659,7 +708,6 @@ CHIP_ERROR EventManagement::EventIterator(const TLVReader & aReader, size_t aDep
return CHIP_EVENT_ID_FOUND;
}
}

return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -750,30 +798,30 @@ CHIP_ERROR EventManagement::FetchEventParameters(const TLVReader & aReader, size
TLVReader reader;
reader.Init(aReader);

if (reader.GetTag() == TLV::ContextTag(EventDataElement::kCsTag_EventPath))
if (reader.GetTag() == TLV::ContextTag(to_underlying(EventDataIB::Tag::kPath)))
{
EventPathIB::Parser path;
ReturnErrorOnFailure(path.Init(aReader));
ReturnErrorOnFailure(path.GetNode(&(envelope->mNodeId)));
ReturnErrorOnFailure(path.GetEndpoint(&(envelope->mEndpointId)));
ReturnErrorOnFailure(path.GetCluster(&(envelope->mClusterId)));
ReturnErrorOnFailure(path.GetEvent(&(envelope->mEventId)));
envelope->mFieldsToRead |= 1 << EventDataElement::kCsTag_EventPath;
envelope->mFieldsToRead |= 1 << to_underlying(EventDataIB::Tag::kPath);
}

if (reader.GetTag() == TLV::ContextTag(EventDataElement::kCsTag_PriorityLevel))
if (reader.GetTag() == TLV::ContextTag(to_underlying(EventDataIB::Tag::kPriority)))
{
uint16_t extPriority; // Note: the type here matches the type case in EventManagement::LogEvent, priority section
ReturnErrorOnFailure(reader.Get(extPriority));
envelope->mPriority = static_cast<PriorityLevel>(extPriority);
envelope->mFieldsToRead |= 1 << EventDataElement::kCsTag_PriorityLevel;
envelope->mFieldsToRead |= 1 << to_underlying(EventDataIB::Tag::kPriority);
}

if (reader.GetTag() == TLV::ContextTag(EventDataElement::kCsTag_DeltaSystemTimestamp))
if (reader.GetTag() == TLV::ContextTag(to_underlying(EventDataIB::Tag::kDeltaSystemTimestamp)))
{
ReturnErrorOnFailure(reader.Get(envelope->mDeltaSystemTime.mValue));

envelope->mFieldsToRead |= 1 << EventDataElement::kCsTag_DeltaSystemTimestamp;
envelope->mFieldsToRead |= 1 << to_underlying(EventDataIB::Tag::kDeltaSystemTimestamp);
}

return CHIP_NO_ERROR;
Expand All @@ -785,8 +833,12 @@ CHIP_ERROR EventManagement::EvictEvent(CHIPCircularTLVBuffer & apBuffer, void *
ReturnErrorOnFailure(aReader.Next());

TLVType containerType;
TLVType containerType1;
ReturnErrorOnFailure(aReader.EnterContainer(containerType));

ReturnErrorOnFailure(aReader.Next());
// Skip EventStatus
ReturnErrorOnFailure(aReader.Next());
ReturnErrorOnFailure(aReader.EnterContainer(containerType1));
EventEnvelopeContext context;
constexpr bool recurse = false;
CHIP_ERROR err = TLV::Utilities::Iterate(aReader, FetchEventParameters, &context, recurse);
Expand All @@ -796,8 +848,8 @@ CHIP_ERROR EventManagement::EvictEvent(CHIPCircularTLVBuffer & apBuffer, void *
}
ReturnErrorOnFailure(err);

ReturnErrorOnFailure(aReader.ExitContainer(containerType1));
ReturnErrorOnFailure(aReader.ExitContainer(containerType));

const PriorityLevel imp = static_cast<PriorityLevel>(context.mPriority);

ReclaimEventCtx * const ctx = static_cast<ReclaimEventCtx *>(apAppData);
Expand All @@ -812,7 +864,7 @@ CHIP_ERROR EventManagement::EvictEvent(CHIPCircularTLVBuffer & apBuffer, void *
EventLogging,
"Dropped events from buffer with priority %u due to overflow: { event priority_level: %u, count: 0x" ChipLogFormatX64
" };",
static_cast<unsigned>(eventBuffer->GetPriorityLevel()), static_cast<unsigned>(imp), ChipLogValueX64(numEventsToDrop));
static_cast<unsigned>(eventBuffer->GetPriority()), static_cast<unsigned>(imp), ChipLogValueX64(numEventsToDrop));
ctx->mSpaceNeededForMovedEvent = 0;
return CHIP_NO_ERROR;
}
Expand All @@ -838,9 +890,9 @@ void EventManagement::SetScheduledEventEndpoint(EventNumber * apEventEndpoints)

while (eventBuffer != nullptr)
{
if (eventBuffer->GetPriorityLevel() >= PriorityLevel::First && (eventBuffer->GetPriorityLevel() <= PriorityLevel::Last))
if (eventBuffer->GetPriority() >= PriorityLevel::First && (eventBuffer->GetPriority() <= PriorityLevel::Last))
{
apEventEndpoints[static_cast<uint8_t>(eventBuffer->GetPriorityLevel())] = eventBuffer->GetLastEventNumber();
apEventEndpoints[static_cast<uint8_t>(eventBuffer->GetPriority())] = eventBuffer->GetLastEventNumber();
}
eventBuffer = eventBuffer->GetNextCircularEventBuffer();
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/EventManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "EventLoggingDelegate.h"
#include "EventLoggingTypes.h"
#include <app/ClusterInfo.h>
#include <app/MessageDef/EventDataElement.h>
#include <app/MessageDef/EventDataIB.h>
#include <app/util/basic-types.h>
#include <lib/core/CHIPCircularTLVBuffer.h>
#include <lib/support/PersistedCounter.h>
Expand All @@ -41,8 +41,8 @@
namespace chip {
namespace app {
constexpr size_t kMaxEventSizeReserve = 512;
constexpr uint16_t kRequiredEventField = (1 << EventDataElement::kCsTag_PriorityLevel) |
(1 << EventDataElement::kCsTag_DeltaSystemTimestamp) | (1 << EventDataElement::kCsTag_EventPath);
constexpr uint16_t kRequiredEventField = (1 << to_underlying(EventDataIB::Tag::kPriority)) |
(1 << to_underlying(EventDataIB::Tag::kDeltaSystemTimestamp)) | (1 << to_underlying(EventDataIB::Tag::kPath));

/**
* @brief
Expand Down Expand Up @@ -126,7 +126,7 @@ class CircularEventBuffer : public TLV::CHIPCircularTLVBuffer
mFirstEventNumber = mpEventNumberCounter->GetValue();
}

PriorityLevel GetPriorityLevel() { return mPriority; }
PriorityLevel GetPriority() { return mPriority; }

CircularEventBuffer * GetPreviousCircularEventBuffer() { return mpPrev; }
CircularEventBuffer * GetNextCircularEventBuffer() { return mpNext; }
Expand Down
2 changes: 1 addition & 1 deletion src/app/MessageDef/AttributeStatusList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CHIP_ERROR AttributeStatusList::Parser::CheckSchemaValidity() const
PRETTY_PRINT("AttributeStatusList =");
PRETTY_PRINT("[");

// make a copy of the EventList reader
// make a copy of the EventReports reader
reader.Init(mReader);

while (CHIP_NO_ERROR == (err = reader.Next()))
Expand Down
Loading

0 comments on commit 27566a0

Please sign in to comment.