forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventHandlerLibShell.cpp
215 lines (171 loc) · 7.1 KB
/
EventHandlerLibShell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
*
* Copyright (c) 2020 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 lockEndpoint = 1;
using namespace chip;
using namespace chip::app;
using namespace Clusters::DoorLock;
using Shell::Engine;
using Shell::shell_command_t;
using Shell::streamer_get;
using Shell::streamer_printf;
Engine sShellDoorlockSubCommands;
Engine sShellDoorlockEventSubCommands;
Engine sShellDoorlockEventAlarmSubCommands;
Engine sShellDoorlockEventDoorStateSubCommands;
/********************************************************
* Doorlock shell functions
*********************************************************/
CHIP_ERROR DoorlockHelpHandler(int argc, char ** argv)
{
sShellDoorlockSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}
CHIP_ERROR DoorlockCommandHandler(int argc, char ** argv)
{
if (argc == 0)
{
return DoorlockHelpHandler(argc, argv);
}
return sShellDoorlockSubCommands.ExecCommand(argc, argv);
}
/********************************************************
* Event shell functions
*********************************************************/
CHIP_ERROR EventHelpHandler(int argc, char ** argv)
{
sShellDoorlockEventSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}
CHIP_ERROR EventDoorlockCommandHandler(int argc, char ** argv)
{
if (argc == 0)
{
return EventHelpHandler(argc, argv);
}
return sShellDoorlockEventSubCommands.ExecCommand(argc, argv);
}
/********************************************************
* Alarm shell functions
*********************************************************/
CHIP_ERROR AlarmHelpHandler(int argc, char ** argv)
{
sShellDoorlockEventAlarmSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}
CHIP_ERROR AlarmEventHandler(int argc, char ** argv)
{
if (argc == 0)
{
return AlarmHelpHandler(argc, argv);
}
if (argc >= 2)
{
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
return CHIP_ERROR_INVALID_ARGUMENT;
}
AlarmEventData * data = Platform::New<AlarmEventData>();
data->eventId = Events::DoorLockAlarm::Id;
data->alarmCode = static_cast<AlarmCodeEnum>(atoi(argv[0]));
DeviceLayer::PlatformMgr().ScheduleWork(EventWorkerFunction, reinterpret_cast<intptr_t>(data));
return CHIP_NO_ERROR;
}
/********************************************************
* Door state shell functions
*********************************************************/
CHIP_ERROR DoorStateHelpHandler(int argc, char ** argv)
{
sShellDoorlockEventDoorStateSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}
CHIP_ERROR DoorStateEventHandler(int argc, char ** argv)
{
if (argc == 0)
{
return DoorStateHelpHandler(argc, argv);
}
if (argc >= 2)
{
ChipLogError(Zcl, "Too many arguments provided to function %s, line %d", __func__, __LINE__);
return CHIP_ERROR_INVALID_ARGUMENT;
}
DoorStateEventData * data = Platform::New<DoorStateEventData>();
data->eventId = Events::DoorStateChange::Id;
data->doorState = static_cast<DoorStateEnum>(atoi(argv[0]));
DeviceLayer::PlatformMgr().ScheduleWork(EventWorkerFunction, reinterpret_cast<intptr_t>(data));
return CHIP_NO_ERROR;
}
/**
* @brief configures lock matter shell
*
*/
CHIP_ERROR RegisterLockEvents()
{
static const shell_command_t sDoorlockSubCommands[] = { { &DoorlockHelpHandler, "help", "Usage: doorlock <subcommand>" },
{ &EventDoorlockCommandHandler, "event",
" Usage: doorlock event <subcommand>" } };
static const shell_command_t sDoorlockEventSubCommands[] = {
{ &EventHelpHandler, "help", "Usage : doorlock event <subcommand>" },
{ &AlarmEventHandler, "lock-alarm", "Sends lock alarm event to lock app" },
{ &DoorStateEventHandler, "door-state-change", "Sends door state change event to lock app" }
};
static const shell_command_t sDoorlockEventAlarmSubCommands[] = { { &AlarmHelpHandler, "help",
"Usage : doorlock event lock-alarm AlarmCode" } };
static const shell_command_t sDoorlockEventDoorStateSubCommands[] = {
{ &DoorStateHelpHandler, "help", "Usage : doorlock event door-state-change DoorState" }
};
static const shell_command_t sDoorLockCommand = { &DoorlockCommandHandler, "doorlock",
"doorlock commands. Usage: doorlock <subcommand>" };
sShellDoorlockEventAlarmSubCommands.RegisterCommands(sDoorlockEventAlarmSubCommands,
MATTER_ARRAY_SIZE(sDoorlockEventAlarmSubCommands));
sShellDoorlockEventDoorStateSubCommands.RegisterCommands(sDoorlockEventDoorStateSubCommands,
MATTER_ARRAY_SIZE(sDoorlockEventDoorStateSubCommands));
sShellDoorlockEventSubCommands.RegisterCommands(sDoorlockEventSubCommands, MATTER_ARRAY_SIZE(sDoorlockEventSubCommands));
sShellDoorlockSubCommands.RegisterCommands(sDoorlockSubCommands, MATTER_ARRAY_SIZE(sDoorlockSubCommands));
Engine::Root().RegisterCommands(&sDoorLockCommand, 1);
return CHIP_NO_ERROR;
}
void EventWorkerFunction(intptr_t context)
{
VerifyOrReturn(context != 0, ChipLogError(NotSpecified, "EventWorkerFunction - Invalid work data"));
EventData * data = reinterpret_cast<EventData *>(context);
switch (data->eventId)
{
case Events::DoorLockAlarm::Id: {
AlarmEventData * alarmData = reinterpret_cast<AlarmEventData *>(context);
DoorLockServer::Instance().SendLockAlarmEvent(lockEndpoint, alarmData->alarmCode);
break;
}
case Events::DoorStateChange::Id: {
DoorStateEventData * doorStateData = reinterpret_cast<DoorStateEventData *>(context);
DoorLockServer::Instance().SetDoorState(lockEndpoint, doorStateData->doorState);
break;
}
default: {
ChipLogError(Zcl, "Invalid Event Id %s, line %d", __func__, __LINE__);
break;
}
}
// free the allocated memory from the event handlers
Platform::Delete(data);
}