|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 4 | + * Copyright (c) 2025 Google LLC. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include "AppTask.h" |
| 23 | +#include "CRTPHelpers.h" |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief CRTP override layer for `AppTask`. |
| 27 | + * |
| 28 | + * Each public entry point dispatches to a corresponding `Derived::*Impl()` hook. |
| 29 | + * The default `*Impl()` (declared private below) forwards to `AppTask`, so |
| 30 | + * `Derived` only needs to override the hooks whose behavior it wants to |
| 31 | + * customize. |
| 32 | + * |
| 33 | + * Customer code overrides these hooks in `CustomerAppTask`; see the app README |
| 34 | + * ("How to Override APIs"). |
| 35 | + * |
| 36 | + * @tparam Derived The CRTP derived class (`CustomerAppTask`). |
| 37 | + */ |
| 38 | +template <typename Derived> |
| 39 | +class AppTaskImpl : public AppTask |
| 40 | +{ |
| 41 | +public: |
| 42 | + // Common AppTask bring up |
| 43 | + CHIP_ERROR AppInit() override { CRTP_OPTIONAL_DISPATCH(AppTaskImpl, Derived, AppInitImpl); } |
| 44 | + |
| 45 | + // Handle button press |
| 46 | + static void ButtonEventHandler(uint8_t button, uint8_t btnAction) |
| 47 | + { |
| 48 | + CRTP_OPTIONAL_STATIC_DISPATCH(AppTaskImpl, Derived, ButtonEventHandlerImpl, button, btnAction); |
| 49 | + } |
| 50 | + |
| 51 | + // AppTask thread handler for the closure button action event |
| 52 | + static void ClosureButtonActionEventHandler(AppEvent * aEvent) |
| 53 | + { |
| 54 | + CRTP_OPTIONAL_STATIC_DISPATCH(AppTaskImpl, Derived, ClosureButtonActionEventHandlerImpl, aEvent); |
| 55 | + } |
| 56 | + |
| 57 | + // Data model hook invoked when a cluster attribute changes |
| 58 | + void DMPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, |
| 59 | + uint8_t * value) |
| 60 | + { |
| 61 | + CRTP_OPTIONAL_VOID_DISPATCH(AppTaskImpl, Derived, DMPostAttributeChangeCallbackImpl, attributePath, type, size, value); |
| 62 | + } |
| 63 | + |
| 64 | + // Data model hook for ClosureControl cluster attribute changes |
| 65 | + void DMClosureControlClusterAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath) |
| 66 | + { |
| 67 | + CRTP_OPTIONAL_VOID_DISPATCH(AppTaskImpl, Derived, DMClosureControlClusterAttributeChangedCallbackImpl, attributePath); |
| 68 | + } |
| 69 | + |
| 70 | + // Data model hook for ClosureDimension cluster attribute changes |
| 71 | + void DMClosureDimensionClusterAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath) |
| 72 | + { |
| 73 | + CRTP_OPTIONAL_VOID_DISPATCH(AppTaskImpl, Derived, DMClosureDimensionClusterAttributeChangedCallbackImpl, attributePath); |
| 74 | + } |
| 75 | + |
| 76 | +private: |
| 77 | + friend Derived; |
| 78 | + |
| 79 | + // Default *Impl() hooks, each forwards to the matching AppTask method |
| 80 | + // Override the corresponding hook in CustomerAppTask to customize behavior |
| 81 | + |
| 82 | + CHIP_ERROR AppInitImpl() { return AppTask::AppInit(); } |
| 83 | + |
| 84 | + void ButtonEventHandlerImpl(uint8_t button, uint8_t btnAction) { AppTask::ButtonEventHandler(button, btnAction); } |
| 85 | + |
| 86 | + void ClosureButtonActionEventHandlerImpl(AppEvent * aEvent) { AppTask::ClosureButtonActionEventHandler(aEvent); } |
| 87 | + |
| 88 | + void DMPostAttributeChangeCallbackImpl(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, |
| 89 | + uint8_t * value) |
| 90 | + { |
| 91 | + AppTask::DMPostAttributeChangeCallback(attributePath, type, size, value); |
| 92 | + } |
| 93 | + |
| 94 | + void DMClosureControlClusterAttributeChangedCallbackImpl(const chip::app::ConcreteAttributePath & attributePath) |
| 95 | + { |
| 96 | + AppTask::DMClosureControlClusterAttributeChangedCallback(attributePath); |
| 97 | + } |
| 98 | + |
| 99 | + void DMClosureDimensionClusterAttributeChangedCallbackImpl(const chip::app::ConcreteAttributePath & attributePath) |
| 100 | + { |
| 101 | + AppTask::DMClosureDimensionClusterAttributeChangedCallback(attributePath); |
| 102 | + } |
| 103 | +}; |
0 commit comments