AppTaskCommon.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. *
  3. * Copyright (c) 2022-2023 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. #pragma once
  19. #include "AppConfig.h"
  20. #include "AppEventCommon.h"
  21. #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
  22. #include "LEDWidget.h"
  23. #endif
  24. #if APP_USE_IDENTIFY_PWM
  25. #include "PWMDevice.h"
  26. #endif
  27. #include <zephyr/drivers/gpio.h>
  28. #include <zephyr/kernel.h>
  29. #include <zephyr/logging/log.h>
  30. #include <platform/CHIPDeviceLayer.h>
  31. #if CONFIG_CHIP_FACTORY_DATA
  32. #include <platform/telink/FactoryDataProvider.h>
  33. #endif
  34. #ifdef CONFIG_CHIP_PW_RPC
  35. #include "Rpc.h"
  36. #endif
  37. #include <credentials/examples/DeviceAttestationCredsExample.h>
  38. #include <cstdint>
  39. using namespace ::chip;
  40. using namespace ::chip::app;
  41. using namespace ::chip::Credentials;
  42. using namespace ::chip::DeviceLayer;
  43. namespace {
  44. inline constexpr EndpointId kExampleEndpointId = 1;
  45. inline constexpr uint8_t kDefaultMinLevel = 0;
  46. inline constexpr uint8_t kDefaultMaxLevel = 254;
  47. inline constexpr uint8_t kButtonPushEvent = 1;
  48. inline constexpr uint8_t kButtonReleaseEvent = 0;
  49. } // namespace
  50. class AppTaskCommon
  51. {
  52. public:
  53. #ifdef CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET
  54. void PowerOnFactoryReset(void);
  55. #endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */
  56. CHIP_ERROR StartApp();
  57. void PostEvent(AppEvent * event);
  58. static void IdentifyEffectHandler(Clusters::Identify::EffectIdentifierEnum aEffect);
  59. #ifdef CONFIG_CHIP_PW_RPC
  60. enum ButtonId_t
  61. {
  62. kButtonId_ExampleAction = 1,
  63. kButtonId_FactoryReset,
  64. kButtonId_StartThread,
  65. kButtonId_StartBleAdv
  66. } ButtonId;
  67. #endif
  68. protected:
  69. CHIP_ERROR InitCommonParts(void);
  70. void DispatchEvent(AppEvent * event);
  71. void GetEvent(AppEvent * aEvent);
  72. void InitButtons(void);
  73. static void FactoryResetTimerTimeoutCallback(k_timer * timer);
  74. static void FactoryResetTimerEventHandler(AppEvent * aEvent);
  75. static void FactoryResetButtonEventHandler(void);
  76. static void FactoryResetHandler(AppEvent * aEvent);
  77. #if APP_USE_BLE_START_BUTTON
  78. static void StartBleAdvButtonEventHandler(void);
  79. static void StartBleAdvHandler(AppEvent * aEvent);
  80. #endif
  81. #if APP_USE_THREAD_START_BUTTON
  82. static void StartThreadButtonEventHandler(void);
  83. static void StartThreadHandler(AppEvent * aEvent);
  84. #endif
  85. #if APP_USE_EXAMPLE_START_BUTTON
  86. static void ExampleActionButtonEventHandler(void);
  87. void SetExampleButtonCallbacks(EventHandler aAction_CB);
  88. EventHandler ExampleActionEventHandler;
  89. #endif
  90. static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
  91. #if APP_USE_IDENTIFY_PWM
  92. PWMDevice mPwmIdentifyLed;
  93. static void ActionIdentifyStateUpdateHandler(k_timer * timer);
  94. static void UpdateIdentifyStateEventHandler(AppEvent * aEvent);
  95. #endif
  96. #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
  97. static void UpdateLedStateEventHandler(AppEvent * aEvent);
  98. static void LEDStateUpdateHandler(LEDWidget * ledWidget);
  99. static void UpdateStatusLED(void);
  100. #endif
  101. #if CONFIG_CHIP_FACTORY_DATA
  102. chip::DeviceLayer::FactoryDataProvider<chip::DeviceLayer::ExternalFlashFactoryData> mFactoryDataProvider;
  103. #endif
  104. #ifdef CONFIG_CHIP_PW_RPC
  105. friend class chip::rpc::TelinkButton;
  106. static void ButtonEventHandler(ButtonId_t btnId, bool btnPressed);
  107. #endif
  108. };