Skip to content

Commit

Permalink
Updated the Operational State dishwasher-app example following the Op…
Browse files Browse the repository at this point in the history
…eerational State Server refactor.
  • Loading branch information
hicklin committed Aug 5, 2023
1 parent 6c1bff9 commit 1262928
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class GenericOperationalStateDelegateImpl : public Delegate
class OperationalStateDelegate : public GenericOperationalStateDelegateImpl
{
private:
const GenericOperationalState rvcOpStateList[7] = {
const GenericOperationalState rvcOpStateList[4] = {
GenericOperationalState(to_underlying(OperationalStateEnum::kStopped)),
GenericOperationalState(to_underlying(OperationalStateEnum::kRunning)),
GenericOperationalState(to_underlying(OperationalStateEnum::kPaused)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <app/clusters/operational-state-server/operational-state-delegate.h>
#include <app/clusters/operational-state-server/operational-state-server.h>
#include <app/util/af-enums.h>
#include <protocols/interaction_model/StatusCode.h>

Expand All @@ -33,12 +33,6 @@ class OperationalStateDelegate : public Delegate
{

public:
/**
* Get the current operational state.
* @return The current operational state value
*/
uint8_t GetCurrentOperationalState() override;

/**
* Get the list of supported operational states.
* Fills in the provided GenericOperationalState with the state at index `index` if there is one,
Expand All @@ -57,48 +51,6 @@ class OperationalStateDelegate : public Delegate
*/
CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) override;

/**
* Get current operational error.
* @param error The GenericOperationalError to fill with the current operational error value
*/
void GetCurrentOperationalError(GenericOperationalError & error) override;

/**
* Get current phase
* @param phase The app::DataModel::Nullable<uint8_t> to fill with the current phase value
*/
void GetCurrentPhase(app::DataModel::Nullable<uint8_t> & phase) override;

/**
* Get countdown time
* @param time The app::DataModel::Nullable<uint32_t> to fill with the coutdown time value
*/
void GetCountdownTime(app::DataModel::Nullable<uint32_t> & time) override;

/**
* Set operational error.
* @param opErrState The new operational error.
*/
CHIP_ERROR SetOperationalError(const GenericOperationalError & opErrState) override;

/**
* Set current operational state.
* @param opState The operational state that should now be the current one.
*/
CHIP_ERROR SetOperationalState(uint8_t opState) override;

/**
* Set operational phase.
* @param phase The operational phase that should now be the current one.
*/
CHIP_ERROR SetPhase(const app::DataModel::Nullable<uint8_t> & phase) override;

/**
* Set coutdown time.
* @param time The coutdown time that should now be the current one.
*/
CHIP_ERROR SetCountdownTime(const app::DataModel::Nullable<uint32_t> & time) override;

// command callback
/**
* Handle Command Callback in application: Pause
Expand All @@ -124,26 +76,26 @@ class OperationalStateDelegate : public Delegate
*/
void HandleStopStateCallback(GenericOperationalError & err) override;

OperationalStateDelegate(uint8_t aOperationalState, GenericOperationalError aOperationalError,
Span<const GenericOperationalState> aOperationalStateList,
Span<const GenericOperationalPhase> aOperationalPhaseList,
app::DataModel::Nullable<uint8_t> aPhase = DataModel::Nullable<uint8_t>(),
app::DataModel::Nullable<uint32_t> aCountdownTime = DataModel::Nullable<uint32_t>()) :
mOperationalState(aOperationalState),
mOperationalError(aOperationalError), mOperationalStateList(aOperationalStateList),
mOperationalPhaseList(aOperationalPhaseList), mOperationalPhase(aPhase), mCountdownTime(aCountdownTime)
{}
~OperationalStateDelegate() = default;

private:
uint8_t mOperationalState;
GenericOperationalError mOperationalError;
app::DataModel::List<const GenericOperationalState> mOperationalStateList;
Span<const GenericOperationalPhase> mOperationalPhaseList;
app::DataModel::Nullable<uint8_t> mOperationalPhase;
app::DataModel::Nullable<uint32_t> mCountdownTime;
const GenericOperationalState rvcOpStateList[4] = {
GenericOperationalState(to_underlying(OperationalStateEnum::kStopped)),
GenericOperationalState(to_underlying(OperationalStateEnum::kRunning)),
GenericOperationalState(to_underlying(OperationalStateEnum::kPaused)),
GenericOperationalState(to_underlying(OperationalStateEnum::kError)),
};

app::DataModel::List<const GenericOperationalState> mOperationalStateList = Span<const GenericOperationalState>(rvcOpStateList);

const GenericOperationalPhase opPhaseList[1] = {
// Phase List is null
GenericOperationalPhase(DataModel::Nullable<CharSpan>()),
};

Span<const GenericOperationalPhase> mOperationalPhaseList = Span<const GenericOperationalPhase>(opPhaseList);
};

void Shutdown();

} // namespace OperationalState
} // namespace Clusters
} // namespace app
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,10 @@
*/
#include <operational-state-delegate-impl.h>

namespace chip {
namespace app {
namespace Clusters {
namespace OperationalState {

using chip::Protocols::InteractionModel::Status;

CHIP_ERROR OperationalStateDelegate::SetOperationalState(uint8_t opState)
{
mOperationalState = opState;
return CHIP_NO_ERROR;
}

CHIP_ERROR OperationalStateDelegate::SetPhase(const app::DataModel::Nullable<uint8_t> & phase)
{
mOperationalPhase = phase;
return CHIP_NO_ERROR;
}

CHIP_ERROR OperationalStateDelegate::SetCountdownTime(const app::DataModel::Nullable<uint32_t> & time)
{
mCountdownTime = time;
return CHIP_NO_ERROR;
}

uint8_t OperationalStateDelegate::GetCurrentOperationalState()
{
return mOperationalState;
}

CHIP_ERROR OperationalStateDelegate::SetOperationalError(const GenericOperationalError & opErrState)
{
mOperationalError = opErrState;
return CHIP_NO_ERROR;
}

void OperationalStateDelegate::GetCurrentOperationalError(GenericOperationalError & error)
{
error = mOperationalError;
}

void OperationalStateDelegate::GetCurrentPhase(app::DataModel::Nullable<uint8_t> & phase)
{
phase = mOperationalPhase;
}

void OperationalStateDelegate::GetCountdownTime(app::DataModel::Nullable<uint32_t> & time)
{
time = mCountdownTime;
}
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OperationalState;

CHIP_ERROR OperationalStateDelegate::GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState)
{
Expand All @@ -91,32 +45,84 @@ CHIP_ERROR OperationalStateDelegate::GetOperationalPhaseAtIndex(size_t index, Ge
void OperationalStateDelegate::HandlePauseStateCallback(GenericOperationalError & err)
{
// placeholder implementation
mOperationalState = to_underlying(OperationalStateEnum::kPaused);
err.Set(to_underlying(ErrorStateEnum::kNoError));
auto error = mServer->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kPaused));
if (error == CHIP_NO_ERROR)
{
err.Set(to_underlying(ErrorStateEnum::kNoError));
}
else
{
err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation));
}
}

void OperationalStateDelegate::HandleResumeStateCallback(GenericOperationalError & err)
{
// placeholder implementation
mOperationalState = to_underlying(OperationalStateEnum::kRunning);
err.Set(to_underlying(ErrorStateEnum::kNoError));
auto error = mServer->SetOperationalState(to_underlying(OperationalStateEnum::kRunning));
if (error == CHIP_NO_ERROR)
{
err.Set(to_underlying(ErrorStateEnum::kNoError));
}
else
{
err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation));
}
}

void OperationalStateDelegate::HandleStartStateCallback(GenericOperationalError & err)
{
// placeholder implementation
mOperationalState = to_underlying(OperationalStateEnum::kRunning);
err.Set(to_underlying(ErrorStateEnum::kNoError));
auto error = mServer->SetOperationalState(to_underlying(OperationalStateEnum::kRunning));
if (error == CHIP_NO_ERROR)
{
err.Set(to_underlying(ErrorStateEnum::kNoError));
}
else
{
err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation));
}
}

void OperationalStateDelegate::HandleStopStateCallback(GenericOperationalError & err)
{
// placeholder implementation
mOperationalState = to_underlying(OperationalStateEnum::kStopped);
err.Set(to_underlying(ErrorStateEnum::kNoError));
auto error = mServer->SetOperationalState(to_underlying(OperationalStateEnum::kStopped));
if (error == CHIP_NO_ERROR)
{
err.Set(to_underlying(ErrorStateEnum::kNoError));
}
else
{
err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation));
}
}

} // namespace OperationalState
} // namespace Clusters
} // namespace app
} // namespace chip
static OperationalState::OperationalStateServer * gOperationalStateServer = nullptr;
static OperationalStateDelegate * gOperationalStateDelegate = nullptr;

void OperationalState::Shutdown()
{
if (gOperationalStateServer != nullptr)
{
delete gOperationalStateServer;
gOperationalStateServer = nullptr;
}
if (gOperationalStateDelegate != nullptr)
{
delete gOperationalStateDelegate;
gOperationalStateDelegate = nullptr;
}
}

void emberAfOperationalStateClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
VerifyOrDie(gOperationalStateServer == nullptr && gOperationalStateDelegate == nullptr);
gOperationalStateDelegate = new OperationalStateDelegate;
gOperationalStateServer = new OperationalStateServer(gOperationalStateDelegate, 0x01,
Clusters::OperationalState::Id);
gOperationalStateServer->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kStopped));
gOperationalStateServer->SetOperationalError(to_underlying(OperationalState::ErrorStateEnum::kNoError));
gOperationalStateServer->Init();
}
Loading

0 comments on commit 1262928

Please sign in to comment.