main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <lib/shell/Engine.h>
  18. #include <app/server/Dnssd.h>
  19. #include <lib/core/CHIPCore.h>
  20. #include <lib/support/Base64.h>
  21. #include <lib/support/CHIPArgParser.hpp>
  22. #include <lib/support/CodeUtils.h>
  23. #include <lib/support/logging/CHIPLogging.h>
  24. #include <lib/support/CHIPMem.h>
  25. #include <platform/CHIPDeviceLayer.h>
  26. #include <app/server/OnboardingCodesUtil.h>
  27. #include <app/server/Server.h>
  28. #include <credentials/DeviceAttestationCredsProvider.h>
  29. #include <credentials/examples/DeviceAttestationCredsExample.h>
  30. #include <zephyr/logging/log.h>
  31. #if CONFIG_ENABLE_CHIP_SHELL || CONFIG_CHIP_LIB_SHELL
  32. #include <ChipShellCollection.h>
  33. #endif
  34. #ifdef CONFIG_ENABLE_PW_RPC
  35. #include "Rpc.h"
  36. #endif
  37. LOG_MODULE_REGISTER(app, CONFIG_CHIP_APP_LOG_LEVEL);
  38. using namespace chip;
  39. using namespace chip::Shell;
  40. using namespace chip::DeviceLayer;
  41. namespace {
  42. constexpr int kExtDiscoveryTimeoutSecs = 20;
  43. }
  44. int main()
  45. {
  46. CHIP_ERROR err = CHIP_NO_ERROR;
  47. #ifdef CONFIG_ENABLE_PW_RPC
  48. rpc::Init();
  49. #endif
  50. err = chip::Platform::MemoryInit();
  51. if (err != CHIP_NO_ERROR)
  52. {
  53. ChipLogError(AppServer, "Platform::MemoryInit() failed");
  54. return 1;
  55. }
  56. err = PlatformMgr().InitChipStack();
  57. if (err != CHIP_NO_ERROR)
  58. {
  59. ChipLogError(AppServer, "PlatformMgr().InitChipStack() failed");
  60. return 1;
  61. }
  62. // Network connectivity
  63. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  64. ConnectivityManagerImpl().StartWiFiManagement();
  65. #endif
  66. #if defined(CHIP_ENABLE_OPENTHREAD)
  67. err = ThreadStackMgr().InitThreadStack();
  68. if (err != CHIP_NO_ERROR)
  69. {
  70. ChipLogError(AppServer, "ThreadStackMgr().InitThreadStack() failed");
  71. return 1;
  72. }
  73. #ifdef CONFIG_OPENTHREAD_MTD
  74. err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
  75. #else
  76. err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
  77. #endif
  78. if (err != CHIP_NO_ERROR)
  79. {
  80. ChipLogError(AppServer, "ConnectivityMgr().SetThreadDeviceType() failed");
  81. return 1;
  82. }
  83. #elif !defined(CONFIG_WIFI_NRF700X)
  84. return CHIP_ERROR_INTERNAL;
  85. #endif
  86. // Device Attestation & Onboarding codes
  87. chip::Credentials::SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
  88. #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  89. chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(kExtDiscoveryTimeoutSecs);
  90. #endif /* CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY */
  91. // Start IM server
  92. static chip::CommonCaseDeviceServerInitParams initParams;
  93. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  94. err = chip::Server::GetInstance().Init(initParams);
  95. if (err != CHIP_NO_ERROR)
  96. {
  97. return 1;
  98. }
  99. chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
  100. err = chip::DeviceLayer::PlatformMgr().StartEventLoopTask();
  101. if (err != CHIP_NO_ERROR)
  102. {
  103. ChipLogError(AppServer, "PlatformMgr().StartEventLoopTask() failed");
  104. }
  105. // When SoftAP support becomes available, it should be added here.
  106. #if CONFIG_NETWORK_LAYER_BLE
  107. PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE));
  108. #else
  109. PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kOnNetwork));
  110. #endif /* CONFIG_NETWORK_LAYER_BLE */
  111. // Starts commissioning window automatically. Starts BLE advertising when BLE enabled
  112. if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() != CHIP_NO_ERROR)
  113. {
  114. ChipLogError(AppServer, "OpenBasicCommissioningWindow() failed");
  115. }
  116. #if CONFIG_CHIP_LIB_SHELL
  117. int rc = Engine::Root().Init();
  118. if (rc != 0)
  119. {
  120. ChipLogError(AppServer, "Streamer initialization failed: %d", rc);
  121. return 1;
  122. }
  123. cmd_misc_init();
  124. cmd_otcli_init();
  125. #endif
  126. #if CHIP_SHELL_ENABLE_CMD_SERVER
  127. cmd_app_server_init();
  128. #endif
  129. #if CONFIG_CHIP_LIB_SHELL
  130. Engine::Root().RunMainLoop();
  131. #endif
  132. return 0;
  133. }