main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. *
  3. * Copyright (c) 2020 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. #include "LightingAppCommandDelegate.h"
  19. #include "LightingManager.h"
  20. #include <AppMain.h>
  21. #include <app-common/zap-generated/ids/Attributes.h>
  22. #include <app-common/zap-generated/ids/Clusters.h>
  23. #include <app/ConcreteAttributePath.h>
  24. #include <app/server/Server.h>
  25. #include <lib/support/logging/CHIPLogging.h>
  26. #if defined(CHIP_IMGUI_ENABLED) && CHIP_IMGUI_ENABLED
  27. #include <imgui_ui/ui.h>
  28. #include <imgui_ui/windows/light.h>
  29. #include <imgui_ui/windows/occupancy_sensing.h>
  30. #include <imgui_ui/windows/qrcode.h>
  31. #endif
  32. using namespace chip;
  33. using namespace chip::app;
  34. using namespace chip::app::Clusters;
  35. namespace {
  36. constexpr const char kChipEventFifoPathPrefix[] = "/tmp/chip_lighting_fifo_";
  37. NamedPipeCommands sChipNamedPipeCommands;
  38. LightingAppCommandDelegate sLightingAppCommandDelegate;
  39. } // namespace
  40. void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
  41. uint8_t * value)
  42. {
  43. if (attributePath.mClusterId == OnOff::Id && attributePath.mAttributeId == OnOff::Attributes::OnOff::Id)
  44. {
  45. LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION);
  46. }
  47. }
  48. /** @brief OnOff Cluster Init
  49. *
  50. * This function is called when a specific cluster is initialized. It gives the
  51. * application an opportunity to take care of cluster initialization procedures.
  52. * It is called exactly once for each endpoint where cluster is present.
  53. *
  54. * @param endpoint Ver.: always
  55. *
  56. * TODO Issue #3841
  57. * emberAfOnOffClusterInitCallback happens before the stack initialize the cluster
  58. * attributes to the default value.
  59. * The logic here expects something similar to the deprecated Plugins callback
  60. * emberAfPluginOnOffClusterServerPostInitCallback.
  61. *
  62. */
  63. void emberAfOnOffClusterInitCallback(EndpointId endpoint)
  64. {
  65. // TODO: implement any additional Cluster Server init actions
  66. }
  67. void ApplicationInit()
  68. {
  69. std::string path = kChipEventFifoPathPrefix + std::to_string(getpid());
  70. if (sChipNamedPipeCommands.Start(path, &sLightingAppCommandDelegate) != CHIP_NO_ERROR)
  71. {
  72. ChipLogError(NotSpecified, "Failed to start CHIP NamedPipeCommands");
  73. sChipNamedPipeCommands.Stop();
  74. }
  75. }
  76. void ApplicationShutdown()
  77. {
  78. if (sChipNamedPipeCommands.Stop() != CHIP_NO_ERROR)
  79. {
  80. ChipLogError(NotSpecified, "Failed to stop CHIP NamedPipeCommands");
  81. }
  82. }
  83. int main(int argc, char * argv[])
  84. {
  85. if (ChipLinuxAppInit(argc, argv) != 0)
  86. {
  87. return -1;
  88. }
  89. CHIP_ERROR err = LightingMgr().Init();
  90. if (err != CHIP_NO_ERROR)
  91. {
  92. ChipLogError(AppServer, "Failed to initialize lighting manager: %" CHIP_ERROR_FORMAT, err.Format());
  93. chip::DeviceLayer::PlatformMgr().Shutdown();
  94. return -1;
  95. }
  96. #if defined(CHIP_IMGUI_ENABLED) && CHIP_IMGUI_ENABLED
  97. example::Ui::ImguiUi ui;
  98. ui.AddWindow(std::make_unique<example::Ui::Windows::QRCode>());
  99. ui.AddWindow(std::make_unique<example::Ui::Windows::OccupancySensing>(chip::EndpointId(1), "Occupancy"));
  100. ui.AddWindow(std::make_unique<example::Ui::Windows::Light>(chip::EndpointId(1)));
  101. ChipLinuxAppMainLoop(&ui);
  102. #else
  103. ChipLinuxAppMainLoop();
  104. #endif
  105. return 0;
  106. }