|
| 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