|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "chef-rpc-actions-worker.h" |
| 20 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 21 | +#include <app-common/zap-generated/callback.h> |
| 22 | +#include <app/data-model/Nullable.h> |
| 23 | +#include <app/util/config.h> |
| 24 | +#include <lib/core/DataModelTypes.h> |
| 25 | +#include <map> |
| 26 | +#include <platform/CHIPDeviceLayer.h> |
| 27 | + |
| 28 | +using chip::app::DataModel::Nullable; |
| 29 | + |
| 30 | +using namespace chip; |
| 31 | +using namespace chip::app; |
| 32 | +using namespace chip::app::Clusters; |
| 33 | +using namespace chip::rpc; |
| 34 | + |
| 35 | +static std::map<ClusterId, ActionsDelegate *> gActionsDelegateMap{}; |
| 36 | + |
| 37 | +ActionsDelegate * RpcFindActionsDelegate(ClusterId clusterId) |
| 38 | +{ |
| 39 | + if (gActionsDelegateMap.find(clusterId) != gActionsDelegateMap.end()) |
| 40 | + { |
| 41 | + return gActionsDelegateMap[clusterId]; |
| 42 | + } |
| 43 | + |
| 44 | + return nullptr; |
| 45 | +} |
| 46 | + |
| 47 | +static void RpcActionsTaskCallback(System::Layer * systemLayer, void * data) |
| 48 | +{ |
| 49 | + ChefRpcActionsWorker * worker = (ChefRpcActionsWorker *) data; |
| 50 | + |
| 51 | + worker->ProcessActionQueue(); |
| 52 | +} |
| 53 | + |
| 54 | +bool ChefRpcActionsCallback(EndpointId endpointId, ClusterId clusterId, uint8_t type, uint32_t delayMs, uint32_t actionId, |
| 55 | + std::vector<uint32_t> args) |
| 56 | +{ |
| 57 | + ActionTask task(endpointId, clusterId, static_cast<ActionType>(type), delayMs, actionId, args); |
| 58 | + |
| 59 | + return ChefRpcActionsWorker::Instance().EnqueueAction(task); |
| 60 | +} |
| 61 | + |
| 62 | +bool ChefRpcActionsWorker::EnqueueAction(ActionTask task) |
| 63 | +{ |
| 64 | + bool kickTimer = false; |
| 65 | + |
| 66 | + if (queue.empty()) |
| 67 | + { |
| 68 | + kickTimer = true; // kick timer when the first task is adding to the queue |
| 69 | + } |
| 70 | + |
| 71 | + queue.push(task); |
| 72 | + |
| 73 | + if (kickTimer) |
| 74 | + { |
| 75 | + (void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(task.delayMs), RpcActionsTaskCallback, this); |
| 76 | + } |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +void ChefRpcActionsWorker::ProcessActionQueue() |
| 81 | +{ |
| 82 | + // Dequeue the first item |
| 83 | + ActionTask task = queue.front(); |
| 84 | + queue.pop(); |
| 85 | + |
| 86 | + ActionsDelegate * delegate = RpcFindActionsDelegate(task.clusterId); |
| 87 | + if (nullptr == delegate) |
| 88 | + { |
| 89 | + ChipLogError(NotSpecified, |
| 90 | + "Cannot run action due to not finding delegate: endpointId=%d, clusterId=%04lx, attributeId=%04lx", |
| 91 | + task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId)); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + ActionType type = static_cast<ActionType>(task.type); |
| 96 | + |
| 97 | + switch (type) |
| 98 | + { |
| 99 | + case ActionType::WRITE_ATTRIBUTE: { |
| 100 | + ChipLogProgress(NotSpecified, "Writing Attribute: endpointId=%d, clusterId=%04lx, attributeId=%04lx, args.size=%lu", |
| 101 | + task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), |
| 102 | + static_cast<unsigned long>(task.args.size())); |
| 103 | + delegate->AttributeWriteHandler(task.endpointId, static_cast<chip::AttributeId>(task.actionId), task.args); |
| 104 | + } |
| 105 | + break; |
| 106 | + case ActionType::RUN_COMMAND: { |
| 107 | + ChipLogProgress(NotSpecified, "Running Command: endpointId=%d, clusterId=%04lx, commandId=%04lx, args.size=%lu", |
| 108 | + task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), |
| 109 | + static_cast<unsigned long>(task.args.size())); |
| 110 | + delegate->CommandHandler(task.endpointId, static_cast<chip::CommandId>(task.actionId), task.args); |
| 111 | + } |
| 112 | + break; |
| 113 | + case ActionType::EMIT_EVENT: { |
| 114 | + ChipLogProgress(NotSpecified, "Emitting Event: endpointId=%d, clusterId=%04lx, eventIdId=%04lx, args.size=%lu", |
| 115 | + task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), |
| 116 | + static_cast<unsigned long>(task.args.size())); |
| 117 | + delegate->EventHandler(task.endpointId, static_cast<chip::EventId>(task.actionId), task.args); |
| 118 | + } |
| 119 | + break; |
| 120 | + default: |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + if (queue.empty()) |
| 125 | + { |
| 126 | + // Return due to no more actions in queue |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Run next action |
| 131 | + task = queue.front(); |
| 132 | + ChipLogProgress(NotSpecified, "StartTimer: endpointId=%d, clusterId=%04lx, eventIdId=%04lx, task.delyMs=%lu", task.endpointId, |
| 133 | + static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), |
| 134 | + static_cast<unsigned long>(task.delayMs)); |
| 135 | + (void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(task.delayMs), RpcActionsTaskCallback, this); |
| 136 | +} |
| 137 | + |
| 138 | +void ChefRpcActionsWorker::RegisterRpcActionsDelegate(ClusterId clusterId, ActionsDelegate * delegate) |
| 139 | +{ |
| 140 | + // Register by cluster |
| 141 | + if (nullptr == RpcFindActionsDelegate(clusterId)) |
| 142 | + { |
| 143 | + gActionsDelegateMap[clusterId] = delegate; |
| 144 | + return; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +ChefRpcActionsWorker::ChefRpcActionsWorker() |
| 149 | +{ |
| 150 | + chip::rpc::SubscribeActions(ChefRpcActionsCallback); |
| 151 | +} |
| 152 | + |
| 153 | +static ChefRpcActionsWorker instance; |
| 154 | + |
| 155 | +ChefRpcActionsWorker & ChefRpcActionsWorker::Instance() |
| 156 | +{ |
| 157 | + return instance; |
| 158 | +} |
0 commit comments