Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chef sample app for dishwasher #35465

Merged
merged 15 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions examples/chef/common/chef-dishwasher-alarm-delegate-impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
*
* Copyright (c) 2024 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/cluster-objects.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;

#ifdef MATTER_DM_PLUGIN_DISHWASHER_ALARM_SERVER
#include <app/clusters/dishwasher-alarm-server/dishwasher-alarm-server.h>
using namespace chip::app::Clusters::DishwasherAlarm;

class DishwasherAlarmDelegate : public Delegate
{
public:
/**
* @brief
* A notification that the Mask attribute will be changed. When this happens, some previously suppressed
* alarms may need to be enabled, and previously enabled alarms may need to be suppressed.
* @param[in] mask The new value of the Mask attribute.
* @return The cluster will do this update if ModifyEnabledAlarmsCallback() returns true.
* The cluster will not do this update if ModifyEnabledAlarmsCallback() returns false.
*/
bool ModifyEnabledAlarmsCallback(const BitMask<AlarmMap> mask) override;

/**
* @brief
* A notification that resets active alarms (if possible)
* @param[in] alarms The value of reset alarms
* @return The cluster will reset active alarms if ResetAlarmsCallback() returns true.
* The cluster will not reset active alarms if ResetAlarmsCallback() returns false.
*/
bool ResetAlarmsCallback(const BitMask<AlarmMap> alarms) override;

~DishwasherAlarmDelegate() = default;
};

bool DishwasherAlarmDelegate::ModifyEnabledAlarmsCallback(const BitMask<AlarmMap> mask)
{
// placeholder implementation
return true;
}

bool DishwasherAlarmDelegate::ResetAlarmsCallback(const BitMask<AlarmMap> alarms)
{
// placeholder implementation
return true;
}

/*
* An example to present device's endpointId
*/
static constexpr EndpointId kDemoEndpointId = 1;

void MatterDishwasherAlarmServerInit()
{
using namespace app::Clusters;
using namespace app::Clusters::DishwasherAlarm;

static DishwasherAlarm::DishwasherAlarmDelegate delegate;
DishwasherAlarm::SetDefaultDelegate(kDemoEndpointId, &delegate);

// Set Supported attribute = 0x3F = 63
// Bit Name Value
// 0 InflowError 1
// 1 DrainError 1
// 2 DoorError 1
// 3 TempTooLow 1
// 4 TempTooHigh 1
// 5 WaterLevelError 1
BitMask<AlarmMap> supported;
supported.SetField(AlarmMap::kInflowError, 1);
supported.SetField(AlarmMap::kDrainError, 1);
supported.SetField(AlarmMap::kDoorError, 1);
supported.SetField(AlarmMap::kTempTooLow, 1);
supported.SetField(AlarmMap::kTempTooHigh, 1);
supported.SetField(AlarmMap::kWaterLevelError, 1);
DishwasherAlarmServer::Instance().SetSupportedValue(kDemoEndpointId, supported);

// Set Mask attribute = 0x0 = 0
// Bit Name Value
// 0 InflowError 0
// 1 DrainError 0
// 2 DoorError 0
// 3 TempTooLow 0
// 4 TempTooHigh 0
// 5 WaterLevelError 0
BitMask<AlarmMap> mask;
mask.SetField(AlarmMap::kInflowError, 0);
mask.SetField(AlarmMap::kDrainError, 0);
mask.SetField(AlarmMap::kDoorError, 0);
mask.SetField(AlarmMap::kTempTooLow, 0);
mask.SetField(AlarmMap::kTempTooHigh, 0);
mask.SetField(AlarmMap::kWaterLevelError, 0);
DishwasherAlarmServer::Instance().SetMaskValue(kDemoEndpointId, mask);

// Set Latch attribute = 0x30
// Bit Name Value
// 0 InflowError 1
// 1 DrainError 1
// 2 DoorError 0
// 3 TempTooLow 0
// 4 TempTooHigh 0
// 5 WaterLevelError 0
BitMask<AlarmMap> latch;
latch.SetField(AlarmMap::kInflowError, 1);
latch.SetField(AlarmMap::kDrainError, 1);
latch.SetField(AlarmMap::kDoorError, 0);
latch.SetField(AlarmMap::kTempTooLow, 0);
latch.SetField(AlarmMap::kTempTooHigh, 0);
latch.SetField(AlarmMap::kWaterLevelError, 0);
DishwasherAlarmServer::Instance().SetLatchValue(kDemoEndpointId, latch);

// Set State attribute = 0x00
// Bit Name Value
// 0 InflowError 0
// 1 DrainError 0
// 2 DoorError 0
// 3 TempTooLow 0
// 4 TempTooHigh 0
// 5 WaterLevelError 0
BitMask<AlarmMap> state;
state.SetField(AlarmMap::kInflowError, 0);
state.SetField(AlarmMap::kDrainError, 0);
state.SetField(AlarmMap::kDoorError, 0);
state.SetField(AlarmMap::kTempTooLow, 0);
state.SetField(AlarmMap::kTempTooHigh, 0);
state.SetField(AlarmMap::kWaterLevelError, 0);
DishwasherAlarmServer::Instance().SetStateValue(kDemoEndpointId, state);
}

#endif // MATTER_DM_PLUGIN_DISHWASHER_ALARM_SERVER
159 changes: 159 additions & 0 deletions examples/chef/common/chef-dishwasher-mode-delegate-impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
*
* Copyright (c) 2024 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>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using chip::Protocols::InteractionModel::Status;
template <typename T>
using List = chip::app::DataModel::List<T>;
using ModeTagStructType = chip::app::Clusters::detail::Structs::ModeTagStruct::Type;

#ifdef MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER
#include <chef-dishwasher-mode-delegate-impl.h>
using namespace chip::app::Clusters::DishwasherMode;

static std::unique_ptr<DishwasherModeDelegate> gDishwasherModeDelegate;
static std::unique_ptr<ModeBase::Instance> gDishwasherModeInstance;

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

void DishwasherModeDelegate::HandleChangeToMode(uint8_t NewMode, ModeBase::Commands::ChangeToModeResponse::Type & response)
{
response.status = to_underlying(ModeBase::StatusCode::kSuccess);
}

CHIP_ERROR DishwasherModeDelegate::GetModeLabelByIndex(uint8_t modeIndex, chip::MutableCharSpan & label)
{
if (modeIndex >= ArraySize(kModeOptions))
{
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
return chip::CopyCharSpanToMutableCharSpan(kModeOptions[modeIndex].label, label);
}

CHIP_ERROR DishwasherModeDelegate::GetModeValueByIndex(uint8_t modeIndex, uint8_t & value)
{
if (modeIndex >= ArraySize(kModeOptions))
{
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
value = kModeOptions[modeIndex].mode;
return CHIP_NO_ERROR;
}

CHIP_ERROR DishwasherModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<ModeTagStructType> & tags)
{
if (modeIndex >= ArraySize(kModeOptions))
{
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}

if (tags.size() < kModeOptions[modeIndex].modeTags.size())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}

std::copy(kModeOptions[modeIndex].modeTags.begin(), kModeOptions[modeIndex].modeTags.end(), tags.begin());
tags.reduce_size(kModeOptions[modeIndex].modeTags.size());

return CHIP_NO_ERROR;
}

// ModeBase::Instance * DishwasherMode::Instance()
// {
// return gDishwasherModeInstance;
// }

void DishwasherMode::Shutdown()
{
gDishwasherModeInstance.reset();
gDishwasherModeDelegate.reset();
}

chip::Protocols::InteractionModel::Status chefDishwasherModeWriteCallback(chip::EndpointId endpointId, chip::ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1
VerifyOrDie(gDishwasherModeInstance != nullptr);
chip::Protocols::InteractionModel::Status ret;
chip::AttributeId attributeId = attributeMetadata->attributeId;

switch (attributeId)
{
case chip::app::Clusters::DishwasherMode::Attributes::CurrentMode::Id: {
uint8_t m = static_cast<uint8_t>(buffer[0]);
ret = gDishwasherModeInstance->UpdateCurrentMode(m);
if (chip::Protocols::InteractionModel::Status::Success != ret)
{
ChipLogError(DeviceLayer, "Invalid Attribute Write to CurrentMode : %d", static_cast<int>(ret));
}
}
break;
default:
ret = chip::Protocols::InteractionModel::Status::UnsupportedWrite;
ChipLogError(DeviceLayer, "Unsupported Writng Attribute ID: %d", static_cast<int>(attributeId));
break;
}

return ret;
}

chip::Protocols::InteractionModel::Status chefDishwasherModeReadCallback(chip::EndpointId endpointId, chip::ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer, uint16_t maxReadLength)
{
VerifyOrReturnValue(maxReadLength == 1, chip::Protocols::InteractionModel::Status::ResourceExhausted);
buffer[0] = gDishwasherModeInstance->GetCurrentMode();

chip::Protocols::InteractionModel::Status ret = chip::Protocols::InteractionModel::Status::Success;
chip::AttributeId attributeId = attributeMetadata->attributeId;

switch (attributeId)
{
case chip::app::Clusters::DishwasherMode::Attributes::CurrentMode::Id: {
*buffer = gDishwasherModeInstance->GetCurrentMode();
ChipLogDetail(DeviceLayer, "Reading DishwasherMode CurrentMode : %d", static_cast<int>(attributeId));
}
break;
default:
ret = chip::Protocols::InteractionModel::Status::UnsupportedRead;
ChipLogDetail(DeviceLayer, "Unsupported attributeId %d from reading DishwasherMode", static_cast<int>(attributeId));
break;
}

return ret;
}

void emberAfDishwasherModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
VerifyOrDie(!gDishwasherModeDelegate && !gDishwasherModeInstance);

gDishwasherModeDelegate = std::make_unique<DishwasherModeDelegate>();
gDishwasherModeInstance = std::make_unique<ModeBase::Instance>(gDishwasherModeDelegate.get(), endpointId, DishwasherMode::Id,
chip::to_underlying(Feature::kOnOff));
gDishwasherModeInstance->Init();
}
#endif // MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER
90 changes: 90 additions & 0 deletions examples/chef/common/chef-dishwasher-mode-delegate-impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
*
* Copyright (c) 2024 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/mode-base-server/mode-base-server.h>
#include <app/util/config.h>
#include <cstring>
#include <utility>

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

namespace chip {
namespace app {
namespace Clusters {

namespace DishwasherMode {

const uint8_t ModeNormal = 0;
const uint8_t ModeHeavy = 1;
const uint8_t ModeLight = 2;

/// This is an application level delegate to handle DishwasherMode commands according to the specific business logic.
class DishwasherModeDelegate : public ModeBase::Delegate
{
private:
using ModeTagStructType = detail::Structs::ModeTagStruct::Type;
ModeTagStructType modeTagsNormal[1] = { { .value = to_underlying(ModeTag::kNormal) } };
ModeTagStructType modeTagsHeavy[2] = { { .value = to_underlying(ModeBase::ModeTag::kMax) },
{ .value = to_underlying(ModeTag::kHeavy) } };
ModeTagStructType modeTagsLight[3] = { { .value = to_underlying(ModeTag::kLight) },
{ .value = to_underlying(ModeBase::ModeTag::kNight) },
{ .value = to_underlying(ModeBase::ModeTag::kQuiet) } };

const detail::Structs::ModeOptionStruct::Type kModeOptions[3] = {
detail::Structs::ModeOptionStruct::Type{ .label = CharSpan::fromCharString("Normal"),
.mode = ModeNormal,
.modeTags = DataModel::List<const ModeTagStructType>(modeTagsNormal) },
detail::Structs::ModeOptionStruct::Type{ .label = CharSpan::fromCharString("Heavy"),
.mode = ModeHeavy,
.modeTags = DataModel::List<const ModeTagStructType>(modeTagsHeavy) },
detail::Structs::ModeOptionStruct::Type{ .label = CharSpan::fromCharString("Light"),
.mode = ModeLight,
.modeTags = DataModel::List<const ModeTagStructType>(modeTagsLight) }
};

CHIP_ERROR Init() override;
void HandleChangeToMode(uint8_t mode, ModeBase::Commands::ChangeToModeResponse::Type & response) override;

CHIP_ERROR GetModeLabelByIndex(uint8_t modeIndex, MutableCharSpan & label) override;
CHIP_ERROR GetModeValueByIndex(uint8_t modeIndex, uint8_t & value) override;
CHIP_ERROR GetModeTagsByIndex(uint8_t modeIndex, DataModel::List<ModeTagStructType> & tags) override;

public:
~DishwasherModeDelegate() override = default;
};

ModeBase::Instance * Instance();

void Shutdown();

} // namespace DishwasherMode

} // namespace Clusters
} // namespace app
} // namespace chip

#ifdef MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER
chip::Protocols::InteractionModel::Status chefDishwasherModeWriteCallback(chip::EndpointId endpoint, chip::ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer);
chip::Protocols::InteractionModel::Status chefDishwasherModeReadCallback(chip::EndpointId endpoint, chip::ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata,
uint8_t * buffer, uint16_t maxReadLength);
#endif // MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER
Loading
Loading