|
| 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 "BindingHandler.h" |
| 19 | + |
| 20 | +#include "AppConfig.h" |
| 21 | +#include "app/CommandSender.h" |
| 22 | +#include "app/clusters/bindings/BindingManager.h" |
| 23 | +#include "app/server/Server.h" |
| 24 | +#include "controller/InvokeInteraction.h" |
| 25 | +#include "platform/CHIPDeviceLayer.h" |
| 26 | +#include <app/clusters/bindings/bindings.h> |
| 27 | +#include <lib/support/CodeUtils.h> |
| 28 | + |
| 29 | +using namespace chip; |
| 30 | +using namespace chip::app; |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +void ProcessOnOffUnicastBindingCommand(CommandId commandId, const EmberBindingTableEntry & binding, |
| 35 | + Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle) |
| 36 | +{ |
| 37 | + auto onSuccess = [](const ConcreteCommandPath & commandPath, const StatusIB & status, const auto & dataResponse) { |
| 38 | + ChipLogProgress(NotSpecified, "OnOff command succeeds"); |
| 39 | + }; |
| 40 | + |
| 41 | + auto onFailure = [](CHIP_ERROR error) { |
| 42 | + ChipLogError(NotSpecified, "OnOff command failed: %" CHIP_ERROR_FORMAT, error.Format()); |
| 43 | + }; |
| 44 | + |
| 45 | + switch (commandId) |
| 46 | + { |
| 47 | + case Clusters::OnOff::Commands::Toggle::Id: |
| 48 | + Clusters::OnOff::Commands::Toggle::Type toggleCommand; |
| 49 | + Controller::InvokeCommandRequest(exchangeMgr, sessionHandle, binding.remote, toggleCommand, onSuccess, onFailure); |
| 50 | + break; |
| 51 | + |
| 52 | + case Clusters::OnOff::Commands::On::Id: |
| 53 | + Clusters::OnOff::Commands::On::Type onCommand; |
| 54 | + Controller::InvokeCommandRequest(exchangeMgr, sessionHandle, binding.remote, onCommand, onSuccess, onFailure); |
| 55 | + break; |
| 56 | + |
| 57 | + case Clusters::OnOff::Commands::Off::Id: |
| 58 | + Clusters::OnOff::Commands::Off::Type offCommand; |
| 59 | + Controller::InvokeCommandRequest(exchangeMgr, sessionHandle, binding.remote, offCommand, onSuccess, onFailure); |
| 60 | + break; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void ProcessOnOffGroupBindingCommand(CommandId commandId, const EmberBindingTableEntry & binding) |
| 65 | +{ |
| 66 | + Messaging::ExchangeManager & exchangeMgr = Server::GetInstance().GetExchangeManager(); |
| 67 | + |
| 68 | + switch (commandId) |
| 69 | + { |
| 70 | + case Clusters::OnOff::Commands::Toggle::Id: |
| 71 | + Clusters::OnOff::Commands::Toggle::Type toggleCommand; |
| 72 | + Controller::InvokeGroupCommandRequest(&exchangeMgr, binding.fabricIndex, binding.groupId, toggleCommand); |
| 73 | + break; |
| 74 | + |
| 75 | + case Clusters::OnOff::Commands::On::Id: |
| 76 | + Clusters::OnOff::Commands::On::Type onCommand; |
| 77 | + Controller::InvokeGroupCommandRequest(&exchangeMgr, binding.fabricIndex, binding.groupId, onCommand); |
| 78 | + |
| 79 | + break; |
| 80 | + |
| 81 | + case Clusters::OnOff::Commands::Off::Id: |
| 82 | + Clusters::OnOff::Commands::Off::Type offCommand; |
| 83 | + Controller::InvokeGroupCommandRequest(&exchangeMgr, binding.fabricIndex, binding.groupId, offCommand); |
| 84 | + break; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void LightSwitchChangedHandler(const EmberBindingTableEntry & binding, OperationalDeviceProxy * peer_device, void * context) |
| 89 | +{ |
| 90 | + VerifyOrReturn(context != nullptr, ChipLogError(NotSpecified, "OnDeviceConnectedFn: context is null")); |
| 91 | + BindingCommandData * data = static_cast<BindingCommandData *>(context); |
| 92 | + |
| 93 | + if (binding.type == EMBER_MULTICAST_BINDING && data->isGroup) |
| 94 | + { |
| 95 | + switch (data->clusterId) |
| 96 | + { |
| 97 | + case Clusters::OnOff::Id: |
| 98 | + ProcessOnOffGroupBindingCommand(data->commandId, binding); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + else if (binding.type == EMBER_UNICAST_BINDING && !data->isGroup) |
| 103 | + { |
| 104 | + switch (data->clusterId) |
| 105 | + { |
| 106 | + case Clusters::OnOff::Id: |
| 107 | + VerifyOrDie(peer_device != nullptr && peer_device->ConnectionReady()); |
| 108 | + ProcessOnOffUnicastBindingCommand(data->commandId, binding, peer_device->GetExchangeManager(), |
| 109 | + peer_device->GetSecureSession().Value()); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void LightSwitchContextReleaseHandler(void * context) |
| 116 | +{ |
| 117 | + VerifyOrReturn(context != nullptr, ChipLogError(NotSpecified, "LightSwitchContextReleaseHandler: context is null")); |
| 118 | + Platform::Delete(static_cast<BindingCommandData *>(context)); |
| 119 | +} |
| 120 | + |
| 121 | +void InitBindingHandlerInternal(intptr_t arg) |
| 122 | +{ |
| 123 | + auto & server = chip::Server::GetInstance(); |
| 124 | + chip::BindingManager::GetInstance().Init( |
| 125 | + { &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }); |
| 126 | + chip::BindingManager::GetInstance().RegisterBoundDeviceChangedHandler(LightSwitchChangedHandler); |
| 127 | + chip::BindingManager::GetInstance().RegisterBoundDeviceContextReleaseHandler(LightSwitchContextReleaseHandler); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace |
| 131 | + |
| 132 | +/******************************************************** |
| 133 | + * Switch functions |
| 134 | + *********************************************************/ |
| 135 | + |
| 136 | +void SwitchWorkerFunction(intptr_t context) |
| 137 | +{ |
| 138 | + VerifyOrReturn(context != 0, ChipLogError(NotSpecified, "SwitchWorkerFunction - Invalid work data")); |
| 139 | + |
| 140 | + BindingCommandData * data = reinterpret_cast<BindingCommandData *>(context); |
| 141 | + BindingManager::GetInstance().NotifyBoundClusterChanged(data->localEndpointId, data->clusterId, static_cast<void *>(data)); |
| 142 | +} |
| 143 | + |
| 144 | +void BindingWorkerFunction(intptr_t context) |
| 145 | +{ |
| 146 | + VerifyOrReturn(context != 0, ChipLogError(NotSpecified, "BindingWorkerFunction - Invalid work data")); |
| 147 | + |
| 148 | + EmberBindingTableEntry * entry = reinterpret_cast<EmberBindingTableEntry *>(context); |
| 149 | + AddBindingEntry(*entry); |
| 150 | + |
| 151 | + Platform::Delete(entry); |
| 152 | +} |
| 153 | + |
| 154 | +CHIP_ERROR InitBindingHandler() |
| 155 | +{ |
| 156 | + // The initialization of binding manager will try establishing connection with unicast peers |
| 157 | + // so it requires the Server instance to be correctly initialized. Post the init function to |
| 158 | + // the event queue so that everything is ready when initialization is conducted. |
| 159 | + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitBindingHandlerInternal); |
| 160 | + return CHIP_NO_ERROR; |
| 161 | +} |
0 commit comments