AppMain.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <iostream>
  19. #include <thread>
  20. #include <platform/CHIPDeviceLayer.h>
  21. #include <platform/PlatformManager.h>
  22. #include <app/server/OnboardingCodesUtil.h>
  23. #include <app/server/Server.h>
  24. #include <core/CHIPError.h>
  25. #include <lib/shell/Engine.h>
  26. #include <setup_payload/QRCodeSetupPayloadGenerator.h>
  27. #include <setup_payload/SetupPayload.h>
  28. #include <support/CHIPMem.h>
  29. #include <support/RandUtils.h>
  30. #if defined(PW_RPC_ENABLED)
  31. #include <CommonRpc.h>
  32. #endif
  33. #include "Options.h"
  34. using namespace chip;
  35. using namespace chip::Inet;
  36. using namespace chip::Transport;
  37. using namespace chip::DeviceLayer;
  38. using chip::Shell::Engine;
  39. namespace {
  40. void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
  41. {
  42. (void) arg;
  43. if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished)
  44. {
  45. ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished");
  46. }
  47. }
  48. } // namespace
  49. int ChipLinuxAppInit(int argc, char ** argv)
  50. {
  51. CHIP_ERROR err = CHIP_NO_ERROR;
  52. err = chip::Platform::MemoryInit();
  53. SuccessOrExit(err);
  54. err = ParseArguments(argc, argv);
  55. SuccessOrExit(err);
  56. err = chip::DeviceLayer::PlatformMgr().InitChipStack();
  57. SuccessOrExit(err);
  58. ConfigurationMgr().LogDeviceConfig();
  59. PrintOnboardingCodes(chip::RendezvousInformationFlag::kBLE);
  60. #if defined(PW_RPC_ENABLED)
  61. chip::rpc::Init();
  62. ChipLogProgress(NotSpecified, "PW_RPC initialized.");
  63. #endif // defined(PW_RPC_ENABLED)
  64. chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0);
  65. chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX)
  66. #if CONFIG_NETWORK_LAYER_BLE
  67. chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false);
  68. #endif
  69. chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true);
  70. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  71. if (LinuxDeviceOptions::GetInstance().mWiFi)
  72. {
  73. chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
  74. }
  75. #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
  76. #if CHIP_ENABLE_OPENTHREAD
  77. if (LinuxDeviceOptions::GetInstance().mThread)
  78. {
  79. SuccessOrExit(err = chip::DeviceLayer::ThreadStackMgrImpl().InitThreadStack());
  80. ChipLogProgress(NotSpecified, "Thread initialized.");
  81. }
  82. #endif // CHIP_ENABLE_OPENTHREAD
  83. exit:
  84. if (err != CHIP_NO_ERROR)
  85. {
  86. ChipLogProgress(NotSpecified, "Failed to run Linux Lighting App: %s ", ErrorStr(err));
  87. // End the program with non zero error code to indicate a error.
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. void ChipLinuxAppMainLoop()
  93. {
  94. #if CHIP_ENABLE_SHELL
  95. std::thread shellThread([]() { Engine::Root().RunMainLoop(); });
  96. #endif
  97. // Init ZCL Data Model and CHIP App Server
  98. InitServer();
  99. chip::DeviceLayer::PlatformMgr().RunEventLoop();
  100. #if CHIP_ENABLE_SHELL
  101. shellThread.join();
  102. #endif
  103. }