main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. *
  3. * Copyright (c) 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. #include <lib/shell/Engine.h>
  19. #include <app/server/Dnssd.h>
  20. #include "platform/CHIPDeviceLayer.h"
  21. #include <lib/support/CHIPMem.h>
  22. #include <app/server/OnboardingCodesUtil.h>
  23. #include <app/server/Server.h>
  24. #include <credentials/DeviceAttestationCredsProvider.h>
  25. #include <credentials/examples/DeviceAttestationCredsExample.h>
  26. #if CONFIG_ENABLE_CHIP_SHELL || CONFIG_CHIP_LIB_SHELL
  27. #include <ChipShellCollection.h>
  28. #endif
  29. #ifdef CONFIG_ENABLE_PW_RPC
  30. #include "Rpc.h"
  31. #endif
  32. LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
  33. using namespace chip;
  34. using namespace chip::Shell;
  35. using namespace chip::DeviceLayer;
  36. int main()
  37. {
  38. CHIP_ERROR err = CHIP_NO_ERROR;
  39. #ifdef CONFIG_ENABLE_PW_RPC
  40. rpc::Init();
  41. #endif
  42. err = chip::Platform::MemoryInit();
  43. if (err != CHIP_NO_ERROR)
  44. {
  45. ChipLogError(AppServer, "Platform::MemoryInit() failed");
  46. return 1;
  47. }
  48. err = PlatformMgr().InitChipStack();
  49. if (err != CHIP_NO_ERROR)
  50. {
  51. ChipLogError(AppServer, "PlatformMgr().InitChipStack() failed");
  52. return 1;
  53. }
  54. // Network connectivity
  55. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  56. ConnectivityManagerImpl().StartWiFiManagement();
  57. #endif
  58. #if defined(CHIP_ENABLE_OPENTHREAD)
  59. err = ThreadStackMgr().InitThreadStack();
  60. if (err != CHIP_NO_ERROR)
  61. {
  62. ChipLogError(AppServer, "ThreadStackMgr().InitThreadStack() failed");
  63. return 1;
  64. }
  65. #if defined(CONFIG_CHIP_THREAD_DEVICE_ROLE_ROUTER)
  66. err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
  67. #elif defined(CONFIG_CHIP_THREAD_DEVICE_ROLE_END_DEVICE)
  68. err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
  69. #elif defined(CONFIG_CHIP_THREAD_DEVICE_ROLE_SLEEPY_END_DEVICE)
  70. err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
  71. #else
  72. #error THREAD_DEVICE_ROLE not selected
  73. #endif
  74. if (err != CHIP_NO_ERROR)
  75. {
  76. ChipLogError(AppServer, "ConnectivityMgr().SetThreadDeviceType() failed");
  77. return 1;
  78. }
  79. #endif
  80. // Device Attestation & Onboarding codes
  81. chip::Credentials::SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
  82. #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  83. chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(kExtDiscoveryTimeoutSecs);
  84. #endif /* CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY */
  85. // Start IM server
  86. static chip::CommonCaseDeviceServerInitParams initParams;
  87. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  88. err = chip::Server::GetInstance().Init(initParams);
  89. if (err != CHIP_NO_ERROR)
  90. {
  91. return 1;
  92. }
  93. chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
  94. err = chip::DeviceLayer::PlatformMgr().StartEventLoopTask();
  95. if (err != CHIP_NO_ERROR)
  96. {
  97. ChipLogError(AppServer, "PlatformMgr().StartEventLoopTask() failed");
  98. }
  99. // When SoftAP support becomes available, it should be added here.
  100. #if CONFIG_NETWORK_LAYER_BLE
  101. PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE));
  102. #else
  103. PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kOnNetwork));
  104. #endif /* CONFIG_NETWORK_LAYER_BLE */
  105. // Starts commissioning window automatically. Starts BLE advertising when BLE enabled
  106. if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() != CHIP_NO_ERROR)
  107. {
  108. ChipLogError(AppServer, "OpenBasicCommissioningWindow() failed");
  109. }
  110. #if CONFIG_CHIP_LIB_SHELL
  111. int rc = Engine::Root().Init();
  112. if (rc != 0)
  113. {
  114. ChipLogError(AppServer, "Streamer initialization failed: %d", rc);
  115. return 1;
  116. }
  117. cmd_misc_init();
  118. cmd_otcli_init();
  119. #endif
  120. #if CHIP_SHELL_ENABLE_CMD_SERVER
  121. cmd_app_server_init();
  122. #endif
  123. #if CONFIG_CHIP_LIB_SHELL
  124. Engine::Root().RunMainLoop();
  125. #endif
  126. return 0;
  127. }