Skip to content

Commit 78b2c94

Browse files
rerasooljmartinez-silabs
authored andcommitted
Pull request #220: Cherry-Pick "EFR32 lock event generation using matter shell (project-chip#23248)" from silabs to silabs_1.0
Merge in WMN_TOOLS/matter from cherry-pick/lock_events to silabs_1.0 Squashed commit of the following: commit 19de8e8d120abbcffdf199813e4e400f3cdb2b90 Author: Michael Rupp <95718139+mykrupp@users.noreply.github.com> Date: Fri Oct 21 18:42:40 2022 -0400 EFR32 lock event generation using matter shell (project-chip#23248) * EFR32 lock event generation using matter shell code review comment cleanup * code review comments
1 parent c7ff04b commit 78b2c94

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

examples/lock-app/efr32/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ efr32_executable("lock_app") {
187187
sources += [ "${examples_plat_dir}/uart.cpp" ]
188188
}
189189

190+
if (chip_build_libshell) {
191+
sources += [ "src/EventHandlerLibShell.cpp" ]
192+
}
193+
190194
deps = [
191195
":sdk",
192196
"${chip_root}/examples/lock-app/lock-common",

examples/lock-app/efr32/include/AppTask.h

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
5050
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)
5151
#define APP_ERROR_ALLOCATION_FAILED CHIP_APPLICATION_ERROR(0x07)
52+
#if defined(ENABLE_CHIP_SHELL)
53+
#define APP_ERROR_TOO_MANY_SHELL_ARGUMENTS CHIP_APPLICATION_ERROR(0x08)
54+
#endif // ENABLE_CHIP_SHELL
5255

5356
/**********************************************************
5457
* AppTask Declaration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#pragma once
18+
19+
#include <app/clusters/door-lock-server/door-lock-server.h>
20+
21+
class EventData
22+
{
23+
public:
24+
chip::EventId eventId;
25+
};
26+
27+
class AlarmEventData : public EventData
28+
{
29+
public:
30+
DlAlarmCode alarmCode;
31+
};
32+
33+
class DoorStateEventData : public EventData
34+
{
35+
public:
36+
DlDoorState doorState;
37+
};
38+
39+
CHIP_ERROR RegisterLockEvents();
40+
void EventWorkerFunction(intptr_t context);

examples/lock-app/efr32/src/AppTask.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "AppTask.h"
2121
#include "AppConfig.h"
2222
#include "AppEvent.h"
23+
#if defined(ENABLE_CHIP_SHELL)
24+
#include "EventHandlerLibShell.h"
25+
#endif // ENABLE_CHIP_SHELL
2326

2427
#ifdef ENABLE_WSTK_LEDS
2528
#include "LEDWidget.h"
@@ -163,6 +166,15 @@ CHIP_ERROR AppTask::Init()
163166
appError(err);
164167
}
165168

169+
#if defined(ENABLE_CHIP_SHELL)
170+
err = RegisterLockEvents();
171+
if (err != CHIP_NO_ERROR)
172+
{
173+
EFR32_LOG("RegisterLockEvents() failed");
174+
appError(err);
175+
}
176+
#endif // ENABLE_CHIP_SHELL
177+
166178
// Initial lock state
167179
chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state;
168180
chip::EndpointId endpointId{ 1 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "EventHandlerLibShell.h"
19+
#include "AppTask.h"
20+
#include "lib/shell/Engine.h"
21+
#include "lib/shell/commands/Help.h"
22+
23+
#include "app/server/Server.h"
24+
#include "platform/CHIPDeviceLayer.h"
25+
#include <lib/support/CodeUtils.h>
26+
27+
constexpr uint8_t lockEndpoint = 1;
28+
29+
using namespace chip;
30+
using namespace chip::app;
31+
using namespace Clusters::DoorLock;
32+
using Shell::Engine;
33+
using Shell::shell_command_t;
34+
using Shell::streamer_get;
35+
using Shell::streamer_printf;
36+
37+
Engine sShellDoorlockSubCommands;
38+
Engine sShellDoorlockEventSubCommands;
39+
Engine sShellDoorlockEventAlarmSubCommands;
40+
Engine sShellDoorlockEventDoorStateSubCommands;
41+
42+
/********************************************************
43+
* Doorlock shell functions
44+
*********************************************************/
45+
46+
CHIP_ERROR DoorlockHelpHandler(int argc, char ** argv)
47+
{
48+
sShellDoorlockSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
49+
return CHIP_NO_ERROR;
50+
}
51+
52+
CHIP_ERROR DoorlockCommandHandler(int argc, char ** argv)
53+
{
54+
if (argc == 0)
55+
{
56+
return DoorlockHelpHandler(argc, argv);
57+
}
58+
59+
return sShellDoorlockSubCommands.ExecCommand(argc, argv);
60+
}
61+
62+
/********************************************************
63+
* Event shell functions
64+
*********************************************************/
65+
66+
CHIP_ERROR EventHelpHandler(int argc, char ** argv)
67+
{
68+
sShellDoorlockEventSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
69+
return CHIP_NO_ERROR;
70+
}
71+
72+
CHIP_ERROR EventDoorlockCommandHandler(int argc, char ** argv)
73+
{
74+
if (argc == 0)
75+
{
76+
return EventHelpHandler(argc, argv);
77+
}
78+
79+
return sShellDoorlockEventSubCommands.ExecCommand(argc, argv);
80+
}
81+
82+
/********************************************************
83+
* Alarm shell functions
84+
*********************************************************/
85+
86+
CHIP_ERROR AlarmHelpHandler(int argc, char ** argv)
87+
{
88+
sShellDoorlockEventAlarmSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
89+
return CHIP_NO_ERROR;
90+
}
91+
92+
CHIP_ERROR AlarmEventHandler(int argc, char ** argv)
93+
{
94+
95+
if (argc == 0)
96+
{
97+
return AlarmHelpHandler(argc, argv);
98+
}
99+
if (argc >= 2)
100+
{
101+
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
102+
return APP_ERROR_TOO_MANY_SHELL_ARGUMENTS;
103+
}
104+
105+
AlarmEventData * data = Platform::New<AlarmEventData>();
106+
data->eventId = Events::DoorLockAlarm::Id;
107+
data->alarmCode = static_cast<DlAlarmCode>(atoi(argv[0]));
108+
109+
DeviceLayer::PlatformMgr().ScheduleWork(EventWorkerFunction, reinterpret_cast<intptr_t>(data));
110+
111+
return CHIP_NO_ERROR;
112+
}
113+
114+
/********************************************************
115+
* Door state shell functions
116+
*********************************************************/
117+
118+
CHIP_ERROR DoorStateHelpHandler(int argc, char ** argv)
119+
{
120+
sShellDoorlockEventDoorStateSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
121+
return CHIP_NO_ERROR;
122+
}
123+
124+
CHIP_ERROR DoorStateEventHandler(int argc, char ** argv)
125+
{
126+
127+
if (argc == 0)
128+
{
129+
return DoorStateHelpHandler(argc, argv);
130+
}
131+
if (argc >= 2)
132+
{
133+
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
134+
return APP_ERROR_TOO_MANY_SHELL_ARGUMENTS;
135+
}
136+
137+
DoorStateEventData * data = Platform::New<DoorStateEventData>();
138+
data->eventId = Events::DoorStateChange::Id;
139+
data->doorState = static_cast<DlDoorState>(atoi(argv[0]));
140+
141+
DeviceLayer::PlatformMgr().ScheduleWork(EventWorkerFunction, reinterpret_cast<intptr_t>(data));
142+
143+
return CHIP_NO_ERROR;
144+
}
145+
146+
/**
147+
* @brief configures lock matter shell
148+
*
149+
*/
150+
151+
CHIP_ERROR RegisterLockEvents()
152+
{
153+
static const shell_command_t sDoorlockSubCommands[] = { { &DoorlockHelpHandler, "help", "Usage: doorlock <subcommand>" },
154+
{ &EventDoorlockCommandHandler, "event",
155+
" Usage: doorlock event <subcommand>" } };
156+
157+
static const shell_command_t sDoorlockEventSubCommands[] = {
158+
{ &EventHelpHandler, "help", "Usage : doorlock event <subcommand>" },
159+
{ &AlarmEventHandler, "lock-alarm", "Sends lock alarm event to lock app" },
160+
{ &DoorStateEventHandler, "door-state-change", "Sends door state change event to lock app" }
161+
};
162+
163+
static const shell_command_t sDoorlockEventAlarmSubCommands[] = { { &AlarmHelpHandler, "help",
164+
"Usage : doorlock event lock-alarm AlarmCode" } };
165+
166+
static const shell_command_t sDoorlockEventDoorStateSubCommands[] = {
167+
{ &DoorStateHelpHandler, "help", "Usage : doorlock event door-state-change DoorState" }
168+
};
169+
170+
static const shell_command_t sDoorLockCommand = { &DoorlockCommandHandler, "doorlock",
171+
"doorlock commands. Usage: doorlock <subcommand>" };
172+
173+
sShellDoorlockEventAlarmSubCommands.RegisterCommands(sDoorlockEventAlarmSubCommands, ArraySize(sDoorlockEventAlarmSubCommands));
174+
175+
sShellDoorlockEventDoorStateSubCommands.RegisterCommands(sDoorlockEventDoorStateSubCommands,
176+
ArraySize(sDoorlockEventDoorStateSubCommands));
177+
178+
sShellDoorlockEventSubCommands.RegisterCommands(sDoorlockEventSubCommands, ArraySize(sDoorlockEventSubCommands));
179+
sShellDoorlockSubCommands.RegisterCommands(sDoorlockSubCommands, ArraySize(sDoorlockSubCommands));
180+
181+
Engine::Root().RegisterCommands(&sDoorLockCommand, 1);
182+
183+
return CHIP_NO_ERROR;
184+
}
185+
186+
void EventWorkerFunction(intptr_t context)
187+
{
188+
VerifyOrReturn(context != 0, ChipLogError(NotSpecified, "EventWorkerFunction - Invalid work data"));
189+
190+
EventData * data = reinterpret_cast<EventData *>(context);
191+
192+
switch (data->eventId)
193+
{
194+
case Events::DoorLockAlarm::Id: {
195+
AlarmEventData * alarmData = reinterpret_cast<AlarmEventData *>(context);
196+
DoorLockServer::Instance().SendLockAlarmEvent(lockEndpoint, alarmData->alarmCode);
197+
break;
198+
}
199+
200+
case Events::DoorStateChange::Id: {
201+
DoorStateEventData * doorStateData = reinterpret_cast<DoorStateEventData *>(context);
202+
DoorLockServer::Instance().SetDoorState(lockEndpoint, doorStateData->doorState);
203+
break;
204+
}
205+
206+
default: {
207+
ChipLogError(Zcl, "Invalid Event Id %s, line %d", __func__, __LINE__);
208+
break;
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)