|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2021 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 "binding-handler.h" |
| 19 | + |
| 20 | +#include "app-common/zap-generated/ids/Clusters.h" |
| 21 | +#include "app-common/zap-generated/ids/Commands.h" |
| 22 | +#include "app/CommandSender.h" |
| 23 | +#include "app/clusters/bindings/BindingManager.h" |
| 24 | +#include "app/server/Server.h" |
| 25 | +#include "controller/InvokeInteraction.h" |
| 26 | +#include "lib/core/CHIPError.h" |
| 27 | + |
| 28 | +#if defined(ENABLE_CHIP_SHELL) |
| 29 | +#include "lib/shell/Engine.h" |
| 30 | + |
| 31 | +using chip::Shell::Engine; |
| 32 | +using chip::Shell::shell_command_t; |
| 33 | +using chip::Shell::streamer_get; |
| 34 | +using chip::Shell::streamer_printf; |
| 35 | +#endif // defined(ENABLE_CHIP_SHELL) |
| 36 | + |
| 37 | +static bool sSwitchOnOffState = false; |
| 38 | +#if defined(ENABLE_CHIP_SHELL) |
| 39 | +static void ToggleSwitchOnOff(bool newState) |
| 40 | +{ |
| 41 | + sSwitchOnOffState = newState; |
| 42 | + chip::BindingManager::GetInstance().NotifyBoundClusterChanged(1, chip::app::Clusters::OnOff::Id, nullptr); |
| 43 | +} |
| 44 | + |
| 45 | +static CHIP_ERROR SwitchCommandHandler(int argc, char ** argv) |
| 46 | +{ |
| 47 | + if (argc == 1 && strcmp(argv[0], "on") == 0) |
| 48 | + { |
| 49 | + ToggleSwitchOnOff(true); |
| 50 | + return CHIP_NO_ERROR; |
| 51 | + } |
| 52 | + if (argc == 1 && strcmp(argv[0], "off") == 0) |
| 53 | + { |
| 54 | + ToggleSwitchOnOff(false); |
| 55 | + return CHIP_NO_ERROR; |
| 56 | + } |
| 57 | + streamer_printf(streamer_get(), "Usage: switch [on|off]"); |
| 58 | + return CHIP_NO_ERROR; |
| 59 | +} |
| 60 | + |
| 61 | +static void RegisterSwitchCommands() |
| 62 | +{ |
| 63 | + static const shell_command_t sSwitchCommand = { SwitchCommandHandler, "switch", "Switch commands. Usage: switch [on|off]" }; |
| 64 | + Engine::Root().RegisterCommands(&sSwitchCommand, 1); |
| 65 | + return; |
| 66 | +} |
| 67 | +#endif // defined(ENABLE_CHIP_SHELL) |
| 68 | + |
| 69 | +static void BoundDeviceChangedHandler(const EmberBindingTableEntry * binding, chip::DeviceProxy * peer_device, void * context) |
| 70 | +{ |
| 71 | + using namespace chip; |
| 72 | + using namespace chip::app; |
| 73 | + |
| 74 | + if (binding->type == EMBER_MULTICAST_BINDING) |
| 75 | + { |
| 76 | + ChipLogError(NotSpecified, "Group binding is not supported now"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (binding->type == EMBER_UNICAST_BINDING && binding->local == 1 && binding->clusterId == Clusters::OnOff::Id) |
| 81 | + { |
| 82 | + auto onSuccess = [](const ConcreteCommandPath & commandPath, const StatusIB & status, const auto & dataResponse) { |
| 83 | + ChipLogProgress(NotSpecified, "OnOff command succeeds"); |
| 84 | + }; |
| 85 | + auto onFailure = [](const StatusIB & status, CHIP_ERROR error) { |
| 86 | + ChipLogError(NotSpecified, "OnOff command failed: %" CHIP_ERROR_FORMAT, error.Format()); |
| 87 | + }; |
| 88 | + |
| 89 | + if (sSwitchOnOffState) |
| 90 | + { |
| 91 | + Clusters::OnOff::Commands::On::Type onCommand; |
| 92 | + Controller::InvokeCommandRequest(peer_device->GetExchangeManager(), peer_device->GetSecureSession().Value(), |
| 93 | + binding->remote, onCommand, onSuccess, onFailure); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + Clusters::OnOff::Commands::Off::Type offCommand; |
| 98 | + Controller::InvokeCommandRequest(peer_device->GetExchangeManager(), peer_device->GetSecureSession().Value(), |
| 99 | + binding->remote, offCommand, onSuccess, onFailure); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +CHIP_ERROR InitBindingHandlers() |
| 105 | +{ |
| 106 | + chip::BindingManager::GetInstance().SetAppServer(&chip::Server::GetInstance()); |
| 107 | + chip::BindingManager::GetInstance().RegisterBoundDeviceChangedHandler(BoundDeviceChangedHandler); |
| 108 | +#if defined(ENABLE_CHIP_SHELL) |
| 109 | + RegisterSwitchCommands(); |
| 110 | +#endif |
| 111 | + return CHIP_NO_ERROR; |
| 112 | +} |
0 commit comments