AppMain.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. *
  3. * Copyright (c) 2021 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 <app/server/Server.h>
  20. #include <controller/CHIPDeviceController.h>
  21. #include <controller/CommissionerDiscoveryController.h>
  22. #include <lib/core/CHIPError.h>
  23. #include <lib/core/DataModelTypes.h>
  24. #include <lib/core/Optional.h>
  25. #include <platform/CHIPDeviceLayer.h>
  26. #include <platform/PlatformManager.h>
  27. #include <transport/TransportMgr.h>
  28. #include "Options.h"
  29. // Applications can optionally provide the endpoint id of a secondary network
  30. // commissioning endpoint, if one is supported.
  31. int ChipLinuxAppInit(int argc, char * const argv[], chip::ArgParser::OptionSet * customOptions = nullptr,
  32. const chip::Optional<chip::EndpointId> secondaryNetworkCommissioningEndpoit = chip::NullOptional);
  33. /**
  34. * A main loop implementation describes how an application main loop is to be
  35. * run:
  36. * - how to execute the main loop
  37. * - what to do to stop it (inside a signal handler - CTRL+C is captured
  38. * by the main loop function)
  39. */
  40. class AppMainLoopImplementation
  41. {
  42. public:
  43. virtual ~AppMainLoopImplementation() = default;
  44. /**
  45. * Execute main loop. Generally should have at least some
  46. * `DeviceLayer::PlatformMgr().RunEventLoop();` or equivalent setup
  47. *
  48. * This is expected to RUN and BLOCK until SignalSafeStopMainLoop is
  49. * called or some internal close logic is run (e.g. a UI may
  50. * stop when the window close button is clicked.)
  51. */
  52. virtual void RunMainLoop() = 0;
  53. /**
  54. * Stop the above `RunMainLoop` function.
  55. *
  56. * Generally should contain at least a
  57. *
  58. * Server::GetInstance().GenerateShutDownEvent()
  59. *
  60. * and then call StopEventLoopTask() in whatever way is appropriate for the
  61. * way the event loop was started.
  62. */
  63. virtual void SignalSafeStopMainLoop() = 0;
  64. };
  65. class DefaultAppMainLoopImplementation : public AppMainLoopImplementation
  66. {
  67. public:
  68. void RunMainLoop() override { chip::DeviceLayer::PlatformMgr().RunEventLoop(); }
  69. void SignalSafeStopMainLoop() override
  70. {
  71. chip::Server::GetInstance().GenerateShutDownEvent();
  72. chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); });
  73. }
  74. };
  75. /**
  76. * Start up the Linux app and use the provided main loop for event processing.
  77. *
  78. * If no main loop implementation is provided, an equivalent of
  79. * DefaultAppMainLoopImplementation will be used.
  80. */
  81. void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl = nullptr);
  82. #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
  83. using chip::Controller::DeviceCommissioner;
  84. using chip::Transport::PeerAddress;
  85. CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, PeerAddress address);
  86. CHIP_ERROR CommissionerPairUDC(uint32_t pincode, size_t index);
  87. DeviceCommissioner * GetDeviceCommissioner();
  88. CommissionerDiscoveryController * GetCommissionerDiscoveryController();
  89. #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
  90. // For extra init calls, the function will be called right before running Matter main loop.
  91. void ApplicationInit();
  92. // For extra shutdown calls, the function will be called before any of the core Matter objects are shut down.
  93. void ApplicationShutdown();