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

[CSA-CP] Adds refrigerator app matter shell support for change door state (#36548) #124

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
4 changes: 2 additions & 2 deletions examples/lock-app/silabs/src/EventHandlerLibShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ CHIP_ERROR AlarmEventHandler(int argc, char ** argv)
if (argc >= 2)
{
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
return APP_ERROR_TOO_MANY_SHELL_ARGUMENTS;
return CHIP_ERROR_INVALID_ARGUMENT;
}

AlarmEventData * data = Platform::New<AlarmEventData>();
Expand Down Expand Up @@ -131,7 +131,7 @@ CHIP_ERROR DoorStateEventHandler(int argc, char ** argv)
if (argc >= 2)
{
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
return APP_ERROR_TOO_MANY_SHELL_ARGUMENTS;
return CHIP_ERROR_INVALID_ARGUMENT;
}

DoorStateEventData * data = Platform::New<DoorStateEventData>();
Expand Down
4 changes: 4 additions & 0 deletions examples/refrigerator-app/silabs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ silabs_executable("refrigerator_app") {
"src/refrigerator-and-temperature-controlled-cabinet-mode.cpp",
]

if (chip_build_libshell) {
sources += [ "src/EventHandlerLibShell.cpp" ]
}

if (use_temp_sensor) {
sources += [
"${efr32_sdk_root}/hardware/driver/si70xx/src/sl_si70xx.c",
Expand Down
36 changes: 36 additions & 0 deletions examples/refrigerator-app/silabs/include/EventHandlerLibShell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* Copyright (c) 2024 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.
*/
#pragma once

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/clusters/refrigerator-alarm-server/refrigerator-alarm-server.h>
#include <app/clusters/temperature-control-server/supported-temperature-levels-manager.h>

class EventData
{
public:
chip::EventId eventId;
};

class RefrigeratorAlarmEventData : public EventData
{
public:
chip::app::Clusters::RefrigeratorAlarm::AlarmBitmap doorState;
};

CHIP_ERROR RegisterRefrigeratorEvents();
void EventWorkerFunction(intptr_t context);
14 changes: 13 additions & 1 deletion examples/refrigerator-app/silabs/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"

#include "LEDWidget.h"

#ifdef DISPLAY_ENABLED
Expand All @@ -35,6 +34,10 @@
#endif // QR_CODE_ENABLED
#endif // DISPLAY_ENABLED

#if defined(ENABLE_CHIP_SHELL)
#include "EventHandlerLibShell.h"
#endif // ENABLE_CHIP_SHELL

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/callback.h>
#include <app-common/zap-generated/cluster-objects.h>
Expand Down Expand Up @@ -94,6 +97,15 @@ CHIP_ERROR AppTask::Init()
appError(err);
}

#if defined(ENABLE_CHIP_SHELL)
err = RegisterRefrigeratorEvents();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("RegisterRefrigeratorEvents() failed");
appError(err);
}
#endif // ENABLE_CHIP_SHELL

return err;
}

Expand Down
178 changes: 178 additions & 0 deletions examples/refrigerator-app/silabs/src/EventHandlerLibShell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
*
* Copyright (c) 2024 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.
*/

#include "EventHandlerLibShell.h"
#include "AppTask.h"
#include "lib/shell/Engine.h"
#include "lib/shell/commands/Help.h"

#include "app/server/Server.h"
#include "platform/CHIPDeviceLayer.h"
#include <lib/support/CodeUtils.h>

constexpr uint8_t kRefEndpointId = 1;

using namespace chip;
using namespace chip::app;
using namespace Clusters::RefrigeratorAlarm;
using namespace Clusters::TemperatureControl;
using Shell::Engine;
using Shell::shell_command_t;
using Shell::streamer_get;
using Shell::streamer_printf;

Engine sShellRefrigeratorSubCommands;
Engine sShellRefrigeratorEventSubCommands;
Engine sShellRefrigeratorEventAlarmDoorSubCommands;

/********************************************************
* Refrigerator shell functions
*********************************************************/

CHIP_ERROR RefrigeratorHelpHandler(int argc, char ** argv)
{
sShellRefrigeratorSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR RefrigeratorCommandHandler(int argc, char ** argv)
{
if (argc == 0)
{
return RefrigeratorHelpHandler(argc, argv);
}

return sShellRefrigeratorSubCommands.ExecCommand(argc, argv);
}

/********************************************************
* Event shell functions
*********************************************************/

CHIP_ERROR EventHelpHandler(int argc, char ** argv)
{
sShellRefrigeratorEventSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR EventRefrigeratorCommandHandler(int argc, char ** argv)
{
if (argc == 0)
{
return EventHelpHandler(argc, argv);
}

return sShellRefrigeratorEventSubCommands.ExecCommand(argc, argv);
}

/********************************************************
* Alarm shell functions
*********************************************************/

CHIP_ERROR AlarmHelpHandler(int argc, char ** argv)
{
sShellRefrigeratorEventAlarmDoorSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR RefrigeratorDoorEventHandler(int argc, char ** argv)
{

if (argc == 0)
{
return AlarmHelpHandler(argc, argv);
}
if (argc >= 2)
{
ChipLogError(Shell, "Too many arguments");
return CHIP_ERROR_INVALID_ARGUMENT;
}

// Check if the argument is a valid integer
if (argv[0] == nullptr || !std::all_of(argv[0], argv[0] + strlen(argv[0]), ::isdigit))
{
ChipLogError(Shell, "Invalid argument: Input must be a valid integer.");
return CHIP_ERROR_INVALID_ARGUMENT;
}

// Convert the argument to an integer
int value = std::stoi(argv[0]); // Safe to use now, as we validated the input earlier

RefrigeratorAlarmEventData * data = Platform::New<RefrigeratorAlarmEventData>();
data->eventId = Events::Notify::Id;
data->doorState = static_cast<AlarmBitmap>(value);

DeviceLayer::PlatformMgr().ScheduleWork(EventWorkerFunction, reinterpret_cast<intptr_t>(data));

return CHIP_NO_ERROR;
}

/**
* @brief configures Refrigerator matter shell
*
*/

CHIP_ERROR RegisterRefrigeratorEvents()
{
static const shell_command_t sRefrigeratorSubCommands[] = {
{ &RefrigeratorHelpHandler, "help", "Usage: refrigeratoralarm <subcommand>" },
{ &EventRefrigeratorCommandHandler, "event", " Usage: refrigeratoralarm event <subcommand>" }
};

static const shell_command_t sRefrigeratorEventSubCommands[] = {
{ &EventHelpHandler, "help", "Usage : refrigeratoralarm event <subcommand>" },
{ &RefrigeratorDoorEventHandler, "door-state-change", "Sends door state change event to Refrigerator app" }
};

static const shell_command_t sRefrigeratorEventAlarmDoorSubCommands[] = {
{ &AlarmHelpHandler, "help", "Usage : Refrigerator event to change door state" }
};

static const shell_command_t sRefrigeratorCommand = { &RefrigeratorCommandHandler, "refrigeratoralarm",
"refrigerator alarm commands. Usage: refrigeratoralarm <subcommand>" };

sShellRefrigeratorEventAlarmDoorSubCommands.RegisterCommands(sRefrigeratorEventAlarmDoorSubCommands,
ArraySize(sRefrigeratorEventAlarmDoorSubCommands));
sShellRefrigeratorEventSubCommands.RegisterCommands(sRefrigeratorEventSubCommands, ArraySize(sRefrigeratorEventSubCommands));
sShellRefrigeratorSubCommands.RegisterCommands(sRefrigeratorSubCommands, ArraySize(sRefrigeratorSubCommands));

Engine::Root().RegisterCommands(&sRefrigeratorCommand, 1);

return CHIP_NO_ERROR;
}

void EventWorkerFunction(intptr_t context)
{
VerifyOrReturn(reinterpret_cast<void *>(context) != nullptr, ChipLogError(Shell, "EventWorkerFunction - Invalid work data"));
EventData * data = reinterpret_cast<EventData *>(context);

switch (data->eventId)
{
case Events::Notify::Id: {
RefrigeratorAlarmEventData * alarmData = reinterpret_cast<RefrigeratorAlarmEventData *>(context);
RefrigeratorAlarmServer::Instance().SetStateValue(kRefEndpointId, alarmData->doorState);
break;
}

default: {
ChipLogError(Zcl, "Invalid Event Id %s, line %d", __func__, __LINE__);
break;
}
}
// Free memory
Platform::Delete(data);
}
Loading