Skip to content

Commit

Permalink
Extend the PCC cluster with more implementation details (#17588)
Browse files Browse the repository at this point in the history
* * Added a callback framework for the pump-app
* Added some documentation to the PCC cluster impl.

* * Making re-style bot happy

* * More re-style bot happiness

* * Changed the PumpStatus from BITMAP16 to the actual PumpState bitfield in the PCC XML

* * Make style bot happy
* And implement generic LevelControl handling in PCC

* * Better handling of PCC bitfields

* * WIP

* Restyled by whitespace

* Restyled by clang-format

* * Re-ran zap_regen_all.py

* * Removed some test code

* * Small modifications after preliminary review

* * Added some comments to the code for clarity

* * Re-enabled logging output
* Re-arranged the init order in the pump-app AppTask
* Added support for MTD_SED, MTD and FTD Thread device options

* * Re-ran zap_regen_all.py

* * Fixed uninitialized status variable

* * Reran zap_regen_all.py

* * Reran zap_regen_all.py

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
tlykkeberg-grundfos and restyled-commits authored May 20, 2022
1 parent 3df3415 commit b4bbe98
Show file tree
Hide file tree
Showing 47 changed files with 1,201 additions and 593 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2752,7 +2752,7 @@ server cluster PumpConfigurationAndControl = 512 {
readonly attribute nullable int16u maxConstFlow = 10;
readonly attribute nullable int16s minConstTemp = 11;
readonly attribute nullable int16s maxConstTemp = 12;
readonly attribute bitmap16 pumpStatus = 16;
readonly attribute PumpStatus pumpStatus = 16;
readonly attribute PumpOperationMode effectiveOperationMode = 17;
readonly attribute PumpControlMode effectiveControlMode = 18;
readonly attribute nullable int16s capacity = 19;
Expand All @@ -2762,7 +2762,6 @@ server cluster PumpConfigurationAndControl = 512 {
attribute access(write: manage) nullable int32u lifetimeEnergyConsumed = 23;
attribute access(write: manage) PumpOperationMode operationMode = 32;
attribute access(write: manage) PumpControlMode controlMode = 33;
readonly attribute bitmap16 alarmMask = 34;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
Expand Down Expand Up @@ -4294,7 +4293,6 @@ endpoint 1 {
ram attribute lifetimeEnergyConsumed;
ram attribute operationMode;
ram attribute controlMode;
ram attribute alarmMask;
ram attribute featureMap;
ram attribute clusterRevision default = 3;
}
Expand Down
4 changes: 1 addition & 3 deletions examples/placeholder/linux/apps/app1/config.matter
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,7 @@ server cluster PumpConfigurationAndControl = 512 {
readonly attribute nullable int16u maxConstFlow = 10;
readonly attribute nullable int16s minConstTemp = 11;
readonly attribute nullable int16s maxConstTemp = 12;
readonly attribute bitmap16 pumpStatus = 16;
readonly attribute PumpStatus pumpStatus = 16;
readonly attribute PumpOperationMode effectiveOperationMode = 17;
readonly attribute PumpControlMode effectiveControlMode = 18;
readonly attribute nullable int16s capacity = 19;
Expand All @@ -1921,7 +1921,6 @@ server cluster PumpConfigurationAndControl = 512 {
attribute access(write: manage) nullable int32u lifetimeEnergyConsumed = 23;
attribute access(write: manage) PumpOperationMode operationMode = 32;
attribute access(write: manage) PumpControlMode controlMode = 33;
readonly attribute bitmap16 alarmMask = 34;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
Expand Down Expand Up @@ -2808,7 +2807,6 @@ endpoint 0 {
ram attribute lifetimeEnergyConsumed;
ram attribute operationMode;
ram attribute controlMode;
ram attribute alarmMask;
ram attribute featureMap;
ram attribute clusterRevision default = 3;
}
Expand Down
4 changes: 1 addition & 3 deletions examples/placeholder/linux/apps/app2/config.matter
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,7 @@ server cluster PumpConfigurationAndControl = 512 {
readonly attribute nullable int16u maxConstFlow = 10;
readonly attribute nullable int16s minConstTemp = 11;
readonly attribute nullable int16s maxConstTemp = 12;
readonly attribute bitmap16 pumpStatus = 16;
readonly attribute PumpStatus pumpStatus = 16;
readonly attribute PumpOperationMode effectiveOperationMode = 17;
readonly attribute PumpControlMode effectiveControlMode = 18;
readonly attribute nullable int16s capacity = 19;
Expand All @@ -1921,7 +1921,6 @@ server cluster PumpConfigurationAndControl = 512 {
attribute access(write: manage) nullable int32u lifetimeEnergyConsumed = 23;
attribute access(write: manage) PumpOperationMode operationMode = 32;
attribute access(write: manage) PumpControlMode controlMode = 33;
readonly attribute bitmap16 alarmMask = 34;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
Expand Down Expand Up @@ -2808,7 +2807,6 @@ endpoint 0 {
ram attribute lifetimeEnergyConsumed;
ram attribute operationMode;
ram attribute controlMode;
ram attribute alarmMask;
ram attribute featureMap;
ram attribute clusterRevision default = 3;
}
Expand Down
2 changes: 2 additions & 0 deletions examples/pump-app/cc13x2x7_26x2x7/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ ti_simplelink_executable("pump_app") {

sources = [
"${project_dir}/main/AppTask.cpp",
"${project_dir}/main/CHIPDeviceManager.cpp",
"${project_dir}/main/DeviceCallbacks.cpp",
"${project_dir}/main/PumpManager.cpp",
"${project_dir}/main/ZclCallbacks.cpp",
"${project_dir}/main/main.cpp",
Expand Down
3 changes: 1 addition & 2 deletions examples/pump-app/cc13x2x7_26x2x7/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ lwip_debug = false
chip_enable_ota_requestor = true

# Disable CHIP Logging
chip_progress_logging = false

#chip_progress_logging = false
#chip_detail_logging = false
#chip_automation_logging = false

Expand Down
106 changes: 83 additions & 23 deletions examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include <app/server/Dnssd.h>
#include <app/server/Server.h>

Expand Down Expand Up @@ -64,6 +66,7 @@ using namespace chip;
using namespace chip::app;
using namespace chip::Credentials;
using namespace chip::DeviceLayer;
using namespace chip::DeviceManager;
using namespace chip::app::Clusters;

static TaskHandle_t sAppTaskHandle;
Expand All @@ -76,6 +79,8 @@ static Button_Handle sAppRightHandle;

AppTask AppTask::sAppTask;

static DeviceCallbacks sDeviceCallbacks;

#if defined(CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR)
static DefaultOTARequestor sRequestorCore;
static DefaultOTARequestorStorage sRequestorStorage;
Expand Down Expand Up @@ -151,22 +156,20 @@ int AppTask::Init()
;
}

#ifdef CONFIG_OPENTHREAD_MTD_SED
ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
#elif CONFIG_OPENTHREAD_MTD
ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
#else
ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
#endif
if (ret != CHIP_NO_ERROR)
{
PLAT_LOG("ConnectivityMgr().SetThreadDeviceType() failed");
while (1)
;
}

ret = PlatformMgr().StartEventLoopTask();
if (ret != CHIP_NO_ERROR)
{
PLAT_LOG("PlatformMgr().StartEventLoopTask() failed");
while (1)
;
}

ret = ThreadStackMgrImpl().StartThreadTask();
if (ret != CHIP_NO_ERROR)
{
Expand All @@ -175,19 +178,6 @@ int AppTask::Init()
;
}

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(EXTENDED_DISCOVERY_TIMEOUT_SEC);
#endif

// Init ZCL Data Model and start server
PLAT_LOG("Initialize Server");
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
chip::Server::GetInstance().Init(initParams);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

// Initialize LEDs
PLAT_LOG("Initialize LEDs");
LED_init();
Expand Down Expand Up @@ -222,6 +212,18 @@ int AppTask::Init()

PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted);

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(EXTENDED_DISCOVERY_TIMEOUT_SEC);
#endif

// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
chip::Server::GetInstance().Init(initParams);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

ConfigurationMgr().LogDeviceConfig();

#if defined(CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR)
Expand All @@ -231,6 +233,15 @@ int AppTask::Init()
// QR code will be used with CHIP Tool
PrintOnboardingCodes(RendezvousInformationFlags(RendezvousInformationFlag::kBLE));

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();
ret = deviceMgr.Init(&sDeviceCallbacks);
if (ret != CHIP_NO_ERROR)
{
PLAT_LOG("CHIPDeviceManager::Init() failed: %s", ErrorStr(ret));
while (1)
;
}

return 0;
}

Expand Down Expand Up @@ -327,6 +338,8 @@ void AppTask::ActionCompleted(PumpManager::Action_t aAction, int32_t aActor)
LED_setOn(sAppGreenHandle, LED_BRIGHTNESS_MAX);
LED_stopBlinking(sAppRedHandle);
LED_setOn(sAppRedHandle, LED_BRIGHTNESS_MAX);
// Signal to the PCC cluster, that the pump is running
sAppTask.UpdateClusterState();
}
else if (aAction == PumpManager::STOP_ACTION)
{
Expand All @@ -335,6 +348,8 @@ void AppTask::ActionCompleted(PumpManager::Action_t aAction, int32_t aActor)
LED_setOff(sAppGreenHandle);
LED_stopBlinking(sAppRedHandle);
LED_setOff(sAppRedHandle);
// Signal to the PCC cluster, that the pump is NOT running
sAppTask.UpdateClusterState();
}
if (aActor == AppEvent::kEventType_ButtonLeft)
{
Expand Down Expand Up @@ -442,11 +457,56 @@ void AppTask::InitOnOffClusterState()

void AppTask::InitPCCClusterState() {}

void AppTask::UpdateClusterState()
void AppTask::UpdateClusterState(void)
{
// We must ensure that the Cluster accessors gets called in the right context
// which is the Matter mainloop thru ScheduleWork()
chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateCluster, reinterpret_cast<intptr_t>(nullptr));
}

void AppTask::UpdateCluster(intptr_t context)
{
EmberStatus status;
BitFlags<PumpConfigurationAndControl::PumpStatus> pumpStatus;

ChipLogProgress(NotSpecified, "Update Cluster State");

ChipLogProgress(NotSpecified, "UpdateClusterState");
// Update the PumpStatus
PumpConfigurationAndControl::Attributes::PumpStatus::Get(PCC_CLUSTER_ENDPOINT, &pumpStatus);
if (PumpMgr().IsStopped())
{
pumpStatus.Clear(PumpConfigurationAndControl::PumpStatus::kRunning);
}
else
{
pumpStatus.Set(PumpConfigurationAndControl::PumpStatus::kRunning);
}
PumpConfigurationAndControl::Attributes::PumpStatus::Set(PCC_CLUSTER_ENDPOINT, pumpStatus);

status = PumpConfigurationAndControl::Attributes::ControlMode::Set(PCC_CLUSTER_ENDPOINT,
PumpConfigurationAndControl::PumpControlMode::kConstantFlow);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: Constant Flow error %x", status);
}
status = PumpConfigurationAndControl::Attributes::ControlMode::Set(
PCC_CLUSTER_ENDPOINT, PumpConfigurationAndControl::PumpControlMode::kConstantPressure);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: Constant Pressure error %x", status);
}
status = PumpConfigurationAndControl::Attributes::ControlMode::Set(
PCC_CLUSTER_ENDPOINT, PumpConfigurationAndControl::PumpControlMode::kConstantSpeed);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: Constant Speed error %x", status);
}
status = PumpConfigurationAndControl::Attributes::ControlMode::Set(
PCC_CLUSTER_ENDPOINT, PumpConfigurationAndControl::PumpControlMode::kConstantTemperature);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: Constant Temperature error %x", status);
}

// Write the new values
bool onOffState = !PumpMgr().IsStopped();
Expand Down
92 changes: 92 additions & 0 deletions examples/pump-app/cc13x2x7_26x2x7/main/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

/**
* @file
* This file implements the CHIP Device Interface that is used by
* applications to interact with the CHIP stack
*
*/

#include <stdlib.h>

#include "AppConfig.h"
#include "CHIPDeviceManager.h"

#include <app/ConcreteAttributePath.h>
#include <app/util/basic-types.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <setup_payload/SetupPayload.h>

using namespace ::chip;

namespace chip {
namespace DeviceManager {

using namespace ::chip::DeviceLayer;

void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg)
{
CHIPDeviceManagerCallbacks * cb = reinterpret_cast<CHIPDeviceManagerCallbacks *>(arg);
if (cb != nullptr)
{
cb->DeviceEventCallback(event, reinterpret_cast<intptr_t>(cb));
}
}

CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
{
mCB = cb;

// Register a function to receive events from the CHIP device layer. Note that calls to
// this function will happen on the CHIP event loop thread, not the app_main thread.
PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));

// Start a task to run the CHIP Device event loop.
return PlatformMgr().StartEventLoopTask();
}
} // namespace DeviceManager
} // namespace chip

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t mask, uint8_t type,
uint16_t size, uint8_t * value)
{
chip::DeviceManager::CHIPDeviceManagerCallbacks * cb =
chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks();
if (cb != nullptr)
{
cb->PostAttributeChangeCallback(attributePath.mEndpointId, attributePath.mClusterId, attributePath.mAttributeId, mask, type,
size, value);
}
}

chip::Protocols::InteractionModel::Status MatterPreAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath,
uint8_t mask, uint8_t type, uint16_t size,
uint8_t * value)
{
chip::DeviceManager::CHIPDeviceManagerCallbacks * cb =
chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks();
if (cb != nullptr)
{
return cb->PreAttributeChangeCallback(attributePath.mEndpointId, attributePath.mClusterId, attributePath.mAttributeId, mask,
type, size, value);
}

return chip::Protocols::InteractionModel::Status::Success;
}
Loading

0 comments on commit b4bbe98

Please sign in to comment.