Skip to content

Commit

Permalink
[OPSTATE] fix :optimize event api (#28285)
Browse files Browse the repository at this point in the history
* [feature]: optimize OPSTATE event api

* Restyled by clang-format

* optimize OPSTATE api

* move the place of OPSTATE event class

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Dec 28, 2023
1 parent 8312623 commit f02ba68
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,6 @@ struct GenericOperationalPhase
char mPhaseNameBuffer[kOperationalPhaseNameMaxSize];
};

/**
* A class which represents the operational completion of an Operational State cluster derivation instance.
*/
struct GenericOperationCompletion : public app::Clusters::OperationalState::Events::OperationCompletion::Type
{
GenericOperationCompletion(uint8_t aCompletionErrorCode,
const Optional<DataModel::Nullable<uint32_t>> & aTotalOperationalTime = NullOptional,
const Optional<DataModel::Nullable<uint32_t>> & aPausedTime = NullOptional)
{
completionErrorCode = aCompletionErrorCode;
totalOperationalTime = aTotalOperationalTime;
pausedTime = aPausedTime;
}
};

/**
* A delegate to handle application logic of the Operational State aliased Cluster.
* The delegate API assumes there will be separate delegate objects for each cluster instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/EventLoggingDelegate.h>
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/af.h>
Expand All @@ -46,6 +45,55 @@ using namespace chip::app::Clusters::OperationalState::Attributes;

using Status = Protocols::InteractionModel::Status;

/**
* A class which represents the operational error event of an Operational State cluster derivation instance.
*/
class GenericErrorEvent : private app::Clusters::OperationalState::Events::OperationalError::Type
{
using super = app::Clusters::OperationalState::Events::OperationalError::Type;

public:
GenericErrorEvent(ClusterId aClusterId, const Structs::ErrorStateStruct::Type & aError) : mClusterId(aClusterId)
{
errorState = aError;
}
using super::GetEventId;
using super::GetPriorityLevel;
ClusterId GetClusterId() const { return mClusterId; }
using super::Encode;
using super::kIsFabricScoped;

private:
ClusterId mClusterId;
};

/**
* A class which represents the operational completion event of an Operational State cluster derivation instance.
*/
class GenericOperationCompletionEvent : private app::Clusters::OperationalState::Events::OperationCompletion::Type
{
using super = app::Clusters::OperationalState::Events::OperationCompletion::Type;

public:
GenericOperationCompletionEvent(ClusterId aClusterId, uint8_t aCompletionErrorCode,
const Optional<DataModel::Nullable<uint32_t>> & aTotalOperationalTime = NullOptional,
const Optional<DataModel::Nullable<uint32_t>> & aPausedTime = NullOptional) :
mClusterId(aClusterId)
{
completionErrorCode = aCompletionErrorCode;
totalOperationalTime = aTotalOperationalTime;
pausedTime = aPausedTime;
}
using super::GetEventId;
using super::GetPriorityLevel;
ClusterId GetClusterId() const { return mClusterId; }
using super::Encode;
using super::kIsFabricScoped;

private:
ClusterId mClusterId;
};

CHIP_ERROR OperationalStateServer::Init()
{
// Check if the cluster has been selected in zap
Expand Down Expand Up @@ -302,44 +350,34 @@ CHIP_ERROR OperationalStateServer::Read(const ConcreteReadAttributePath & aPath,
return CHIP_NO_ERROR;
}

void OperationalStateServer::OnOperationalErrorDetect(const Structs::ErrorStateStruct::Type & aError)
void OperationalStateServer::OnOperationalErrorDetected(const Structs::ErrorStateStruct::Type & aError)
{
ChipLogDetail(Zcl, "OperationalStateServer: OnOperationalErrorDetect");
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, OperationalState::Attributes::OperationalState::Id);
ChipLogDetail(Zcl, "OperationalStateServer: OnOperationalErrorDetected");
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::OperationalState::Id);

GenericErrorEvent event(mClusterId, aError);
EventNumber eventNumber;
Events::OperationalError::Type event{ aError };
EventLogger<Events::OperationalError::Type> eventData(event);
ConcreteEventPath path(mEndpointId, mClusterId, event.GetEventId());
EventManagement & logMgmt = chip::app::EventManagement::GetInstance();
EventOptions eventOptions;
eventOptions.mPath = path;
eventOptions.mPriority = event.GetPriorityLevel();

CHIP_ERROR err = logMgmt.LogEvent(&eventData, eventOptions, eventNumber);
if (err != CHIP_NO_ERROR)
CHIP_ERROR error = app::LogEvent(event, mEndpointId, eventNumber);

if (error != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "OperationalStateServer: Failed to record OperationalError event: %" CHIP_ERROR_FORMAT, err.Format());
ChipLogError(Zcl, "OperationalStateServer: Failed to record OperationalError event: %" CHIP_ERROR_FORMAT, error.Format());
}
}

void OperationalStateServer::OnOperationCompletionDetect(const Events::OperationCompletion::Type & aEvent)
void OperationalStateServer::OnOperationCompletionDetected(uint8_t aCompletionErrorCode,
const Optional<DataModel::Nullable<uint32_t>> & aTotalOperationalTime,
const Optional<DataModel::Nullable<uint32_t>> & aPausedTime)
{
ChipLogDetail(Zcl, "OperationalStateServer: OnOperationCompletionDetect");
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, OperationalState::Attributes::OperationalState::Id);
ChipLogDetail(Zcl, "OperationalStateServer: OnOperationCompletionDetected");

GenericOperationCompletionEvent event(mClusterId, aCompletionErrorCode, aTotalOperationalTime, aPausedTime);
EventNumber eventNumber;
EventLogger<Events::OperationCompletion::Type> eventData(aEvent);
ConcreteEventPath path(mEndpointId, mClusterId, aEvent.GetEventId());
EventManagement & logMgmt = chip::app::EventManagement::GetInstance();
EventOptions eventOptions;
eventOptions.mPath = path;
eventOptions.mPriority = aEvent.GetPriorityLevel();

CHIP_ERROR err = logMgmt.LogEvent(&eventData, eventOptions, eventNumber);
if (err != CHIP_NO_ERROR)
CHIP_ERROR error = app::LogEvent(event, mEndpointId, eventNumber);

if (error != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "OperationalStateServer: Failed to record OnOperationCompletionDetect event: %" CHIP_ERROR_FORMAT,
err.Format());
ChipLogError(Zcl, "OperationalStateServer: Failed to record OperationCompletion event: %" CHIP_ERROR_FORMAT,
error.Format());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ class OperationalStateServer : public CommandHandlerInterface, public AttributeA
* @brief Called when the Node detects a OperationalError has been raised.
* @param aError OperationalError which detects
*/
void OnOperationalErrorDetect(const Structs::ErrorStateStruct::Type & aError);
void OnOperationalErrorDetected(const Structs::ErrorStateStruct::Type & aError);

/**
* @brief Called when the Node detects a OperationCompletion has been raised.
* @param aEvent OperationCompletion event
* @param aCompletionErrorCode CompletionErrorCode
* @param aTotalOperationalTime TotalOperationalTime
* @param aPausedTime PausedTime
*/
void OnOperationCompletionDetect(const Events::OperationCompletion::Type & aEvent);
void OnOperationCompletionDetected(uint8_t aCompletionErrorCode,
const Optional<DataModel::Nullable<uint32_t>> & aTotalOperationalTime = NullOptional,
const Optional<DataModel::Nullable<uint32_t>> & aPausedTime = NullOptional);

/**
* Creates an operational state cluster instance. The Init() function needs to be called for this instance to be registered and
Expand Down
37 changes: 0 additions & 37 deletions src/app/tests/TestOperationalStateDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,42 +542,6 @@ void TestStructGenericOperationalPhaseCopyAssignment(nlTestSuite * inSuite, void
memcmp(const_cast<char *>(phase.mPhaseName.Value().data()), phaseBuffer2, kOperationalPhaseNameMaxSize) == 0);
}

void TestStructGenericOperationalCompletionConstructor(nlTestSuite * inSuite, void * inContext)
{
using namespace chip::app;
using namespace chip::app::Clusters::OperationalState;

// completion with only CompletionErrorCode
GenericOperationCompletion genericOperationCompletion(to_underlying(OperationalStateEnum::kError));
NL_TEST_ASSERT(inSuite, genericOperationCompletion.completionErrorCode == to_underlying(OperationalStateEnum::kError));
NL_TEST_ASSERT(inSuite, genericOperationCompletion.totalOperationalTime.HasValue() == false);
NL_TEST_ASSERT(inSuite, genericOperationCompletion.pausedTime.HasValue() == false);

// completion with errorCode and TotalOperationalTime
uint32_t kTotalOperationalTime = 500;
GenericOperationCompletion genericOperationCompletion2(
to_underlying(OperationalStateEnum::kError),
Optional<DataModel::Nullable<uint32_t>>(DataModel::Nullable<uint32_t>(kTotalOperationalTime)));
NL_TEST_ASSERT(inSuite, genericOperationCompletion2.completionErrorCode == to_underlying(OperationalStateEnum::kError));

NL_TEST_ASSERT(inSuite, genericOperationCompletion2.totalOperationalTime.HasValue() == true);
NL_TEST_ASSERT(inSuite, genericOperationCompletion2.totalOperationalTime.Value().Value() == kTotalOperationalTime);
NL_TEST_ASSERT(inSuite, genericOperationCompletion2.pausedTime.HasValue() == false);

// completion with errorCode, TotalOperationalTime and PausedTime
uint32_t kPausedTime = 2000;
GenericOperationCompletion genericOperationCompletion3(
to_underlying(OperationalStateEnum::kError),
Optional<DataModel::Nullable<uint32_t>>(DataModel::Nullable<uint32_t>(kTotalOperationalTime)),
Optional<DataModel::Nullable<uint32_t>>(DataModel::Nullable<uint32_t>(kPausedTime)));
NL_TEST_ASSERT(inSuite, genericOperationCompletion3.completionErrorCode == to_underlying(OperationalStateEnum::kError));

NL_TEST_ASSERT(inSuite, genericOperationCompletion3.totalOperationalTime.HasValue() == true);
NL_TEST_ASSERT(inSuite, genericOperationCompletion3.totalOperationalTime.Value().Value() == kTotalOperationalTime);
NL_TEST_ASSERT(inSuite, genericOperationCompletion3.pausedTime.HasValue() == true);
NL_TEST_ASSERT(inSuite, genericOperationCompletion3.pausedTime.Value().Value() == kPausedTime);
}

const nlTest sTests[] = {
NL_TEST_DEF("Test struct GenericOperationalState: constructor with only StateID",
TestStructGenericOperationalStateConstructorWithOnlyStateID),
Expand All @@ -598,7 +562,6 @@ const nlTest sTests[] = {
NL_TEST_DEF("Test struct GenericOperationalPhase: constructor", TestStructGenericOperationalPhaseConstructor),
NL_TEST_DEF("Test struct GenericOperationalPhase: copy constructor", TestStructGenericOperationalPhaseCopyConstructor),
NL_TEST_DEF("Test struct GenericOperationalPhase: copy assignment", TestStructGenericOperationalPhaseCopyAssignment),
NL_TEST_DEF("Test struct GenericOperationalCompletion: constructor", TestStructGenericOperationalCompletionConstructor),
NL_TEST_SENTINEL()
};

Expand Down

0 comments on commit f02ba68

Please sign in to comment.