Skip to content

Commit

Permalink
Add operational state cluster implement
Browse files Browse the repository at this point in the history
  • Loading branch information
mideayanghui committed Jun 21, 2023
1 parent 834663d commit 4927843
Show file tree
Hide file tree
Showing 15 changed files with 1,753 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once


#include <app/clusters/operational-state-server/operational-state-delegate.h>
#include <app/util/af.h>
#include <app/util/config.h>

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

// This is an application level delegate to handle operational state commands according to the specific business logic.
class OperationalStateDelegate : public Delegate
{

private:
CHIP_ERROR Init() override;

public:
/**
* Handle Command Callback: Pause
* @param state operational state.
* @param error operational error.
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code.
*/
void HandlePauseState(OperationalStateStruct & state, OperationalErrorStateStruct & error) override;

/**
* Handle Command Callback: Resume
* @param state operational state.
* @param error operational error.
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code.
*/
void HandleResumeState(OperationalStateStruct & state, OperationalErrorStateStruct & error) override;

/**
* Handle Command Callback: Start
* @param state operational state.
* @param error operational error.
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code.
*/
void HandleStartState(OperationalStateStruct & state, OperationalErrorStateStruct & error) override;

/**
* Handle Command Callback: Stop
* @param state operational state.
* @param error operational error.
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code.
*/
void HandleStopState(OperationalStateStruct & state, OperationalErrorStateStruct & error) override;

~OperationalStateDelegate() override = default;
};

} // namespace OperationalState
} // namespace Clusters
} // namespace app
} // namespace chip
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/util/config.h>
#include <operational-state-delegates.h>

using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OperationalState;
using chip::Protocols::InteractionModel::Status;


static OperationalErrorStateStruct & setOperationalNoError(OperationalErrorStateStruct & error)
{
char opNoError[64] = "No Error";
char opNoErrorDetails[64] = "No Error Details";

error.ErrorStateID = static_cast<uint8_t>(ErrorStateEnum::kNoError);
memset(error.ErrorStateLabel, 0, sizeof(error.ErrorStateLabel));
memset(error.ErrorStateDetails, 0, sizeof(error.ErrorStateDetails));
memcpy(error.ErrorStateLabel, opNoError, sizeof(opNoError));
memcpy(error.ErrorStateDetails, opNoErrorDetails, sizeof(opNoErrorDetails));

return error;
}
//-- Operational state delegate functions

CHIP_ERROR OperationalStateDelegate::Init()
{
return CHIP_NO_ERROR;
}

void OperationalStateDelegate::HandlePauseState(OperationalStateStruct & state, OperationalErrorStateStruct & error)
{
ChipLogDetail(Zcl, "Op: HandlePauseState");
state.OperationalStateID = static_cast<uint8_t>(OperationalStateEnum::kPaused);
setOperationalNoError(error);
}

void OperationalStateDelegate::HandleResumeState(OperationalStateStruct & state, OperationalErrorStateStruct & error)
{
ChipLogDetail(Zcl, "Op: HandleResumeState");
state.OperationalStateID = static_cast<uint8_t>(OperationalStateEnum::kRunning);
setOperationalNoError(error);
}

void OperationalStateDelegate::HandleStartState(OperationalStateStruct & state, OperationalErrorStateStruct & error)
{
ChipLogDetail(Zcl, "Op: HandleStartState");
state.OperationalStateID = static_cast<uint8_t>(OperationalStateEnum::kRunning);
setOperationalNoError(error);
}

void OperationalStateDelegate::HandleStopState(OperationalStateStruct & state, OperationalErrorStateStruct & error)
{
ChipLogDetail(Zcl, "Op: HandleStopState");
state.OperationalStateID = static_cast<uint8_t>(OperationalStateEnum::kStopped);
setOperationalNoError(error);
}
1 change: 1 addition & 0 deletions examples/all-clusters-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source_set("chip-all-clusters-common") {
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp",
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/fan-stub.cpp",
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp",
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/operational-state-delegates.cpp",
"AllClustersCommandDelegate.cpp",
"AppOptions.cpp",
"WindowCoveringManager.cpp",
Expand Down
72 changes: 72 additions & 0 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
#include "WindowCoveringManager.h"
#include "include/tv-callbacks.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-enums.h>
#include <app/CommandHandler.h>
#include <app/att-storage.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/clusters/operational-state-server/operational-state-server.h>
#include <app/clusters/operational-state-server/operational-state-delegate.h>
#include <operational-state-delegates.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <lib/support/CHIPMem.h>
Expand Down Expand Up @@ -197,6 +201,12 @@ CHIP_ERROR ExampleDeviceInstanceInfoProvider::GetProductPrimaryColor(Clusters::B
ExampleDeviceInstanceInfoProvider gExampleDeviceInstanceInfoProvider;

} // namespace
namespace {
//operational state cluster
Clusters::OperationalState::OperationalStateDelegate operationalStateDelegate;
Clusters::OperationalState::OperationalStateServer operationalstateServer(0x01, Clusters::OperationalState::Id, &operationalStateDelegate);
}


void ApplicationInit()
{
Expand Down Expand Up @@ -260,6 +270,68 @@ void ApplicationInit()
sChipNamedPipeCommands.Stop();
}

{
//operational state cluster
using OperationalStateStructType = Clusters::OperationalState::Structs::OperationalStateStruct::Type;
using OperationalStateStructTypeList = DataModel::List<OperationalStateStructType>;
using PhaseListType = chip::CharSpan;
using PhaseList = chip::app::DataModel::List<const chip::CharSpan>;
using namespace Clusters::OperationalState;

const auto makeOperationalStateStructType = [](Clusters::OperationalState::OperationalStateEnum stateId, char * stateLabel, size_t len = 0) {
OperationalStateStructType state;
state.operationalStateID = stateId;
if (len)
state.operationalStateLabel = chip::CharSpan(stateLabel, len);
return state;
};

char opStopped[8] = "Stopped";
char opRunning[32] = "Running";
char opPaused[64] = "Paused";

OperationalStateStructType opS[3] = { makeOperationalStateStructType(Clusters::OperationalState::OperationalStateEnum::kStopped, opStopped, sizeof(opStopped)),
makeOperationalStateStructType(Clusters::OperationalState::OperationalStateEnum::kRunning, opRunning, sizeof(opRunning)),
makeOperationalStateStructType(Clusters::OperationalState::OperationalStateEnum::kPaused, opPaused, sizeof(opPaused)) };
OperationalStateStructTypeList opL(opS);
Clusters::OperationalState::OperationalStateStruct op;
Clusters::OperationalState::OperationalErrorStateStruct opErr;

operationalstateServer.Init();
//set operational state list
operationalstateServer.SetOperationalStateList<OperationalStateStructType>(opL);
op.OperationalStateID = static_cast<uint8_t>(OperationalStateEnum::kRunning);
//set operational state
operationalstateServer.SetOperationalState(op);

//set operational error
char opNoError[64] = "No Error";
char opNoErrorDetails[64] = "No Error Details";
opErr.ErrorStateID = static_cast<uint8_t>(ErrorStateEnum::kNoError);
memcpy(opErr.ErrorStateLabel, opNoError, sizeof(opNoError));
memcpy(opErr.ErrorStateDetails, opNoErrorDetails, sizeof(opNoErrorDetails));
operationalstateServer.SetOperationalError(opErr);

//set phase list
char opPauseList[][32] = {
"pre-soak",
"rinse",
"spin"
};
const auto makePhaseType = [](char * str, size_t len = 0) {
return CharSpan::fromCharString(str);
};

PhaseListType phase[3] = {
makePhaseType(opPauseList[0]),
makePhaseType(opPauseList[1]),
makePhaseType(opPauseList[2]),
};

PhaseList phaseList(phase);
operationalstateServer.SetPhaseList(phaseList);
}

auto * defaultProvider = GetDeviceInstanceInfoProvider();
if (defaultProvider != &gExampleDeviceInstanceInfoProvider)
{
Expand Down
5 changes: 5 additions & 0 deletions src/app/chip_data_model.gni
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ template("chip_data_model") {
"${_app_root}/clusters/scenes-server/ExtensionFieldSetsImpl.cpp",
"${_app_root}/clusters/scenes-server/SceneTableImpl.cpp",
]
} else if (cluster == "operational-state-server") {
sources += [
"${_app_root}/clusters/${cluster}/${cluster}.cpp",
"${_app_root}/clusters/${cluster}/OperationalStateDataProvider.cpp",
]
} else {
sources += [ "${_app_root}/clusters/${cluster}/${cluster}.cpp" ]
}
Expand Down
Loading

0 comments on commit 4927843

Please sign in to comment.