main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. *
  3. * Copyright (c) 2021 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 "nvs_flash.h"
  18. #include <lib/shell/Engine.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 <ChipShellCollection.h>
  25. #include <lib/support/CHIPMem.h>
  26. #include <platform/CHIPDeviceLayer.h>
  27. #include <app/clusters/network-commissioning/network-commissioning.h>
  28. #include <app/server/OnboardingCodesUtil.h>
  29. #include <app/server/Server.h>
  30. #include <credentials/DeviceAttestationCredsProvider.h>
  31. #include <credentials/examples/DeviceAttestationCredsExample.h>
  32. #include <platform/ESP32/ESP32Utils.h>
  33. #include <platform/ESP32/NetworkCommissioningDriver.h>
  34. #include <app-common/zap-generated/callback.h>
  35. #include <app-common/zap-generated/cluster-objects.h>
  36. #include <app/att-storage.h>
  37. #include <app/server/Dnssd.h>
  38. #include <app/util/af.h>
  39. #include <setup_payload/QRCodeSetupPayloadGenerator.h>
  40. #include "Display.h"
  41. #include "QRCodeScreen.h"
  42. #include "ScreenManager.h"
  43. #if CONFIG_ENABLE_PW_RPC
  44. #include "Rpc.h"
  45. #endif /* CONFIG_ENABLE_PW_RPC */
  46. using namespace chip;
  47. using namespace chip::Shell;
  48. using chip::Shell::Engine;
  49. using namespace chip::DeviceLayer;
  50. #if CONFIG_ENABLE_CHIP_SHELL
  51. static void chip_shell_task(void * args)
  52. {
  53. cmd_misc_init();
  54. Engine::Root().RunMainLoop();
  55. }
  56. #endif /* CONFIG_ENABLE_CHIP_SHELL */
  57. void DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
  58. {
  59. switch (event->Type)
  60. {
  61. case DeviceEventType::kInternetConnectivityChange:
  62. if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
  63. {
  64. ChipLogProgress(Shell, "IPv4 Server ready...");
  65. chip::app::DnssdServer::Instance().StartServer();
  66. }
  67. else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
  68. {
  69. ChipLogProgress(Shell, "Lost IPv4 connectivity...");
  70. }
  71. if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
  72. {
  73. ChipLogProgress(Shell, "IPv6 Server ready...");
  74. chip::app::DnssdServer::Instance().StartServer();
  75. }
  76. else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
  77. {
  78. ChipLogProgress(Shell, "Lost IPv6 connectivity...");
  79. }
  80. break;
  81. case DeviceEventType::kCHIPoBLEConnectionEstablished:
  82. ChipLogProgress(Shell, "CHIPoBLE connection established");
  83. break;
  84. case DeviceEventType::kCHIPoBLEConnectionClosed:
  85. ChipLogProgress(Shell, "CHIPoBLE disconnected");
  86. break;
  87. case DeviceEventType::kCommissioningComplete:
  88. ChipLogProgress(Shell, "Commissioning complete");
  89. break;
  90. case DeviceEventType::kInterfaceIpAddressChanged:
  91. if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) ||
  92. (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned))
  93. {
  94. // DNSSD server restart on any ip assignment: if link local ipv6 is configured, that
  95. // will not trigger a 'internet connectivity change' as there is no internet
  96. // connectivity. DNSSD still wants to refresh its listening interfaces to include the
  97. // newly selected address.
  98. chip::app::DnssdServer::Instance().StartServer();
  99. }
  100. break;
  101. }
  102. ChipLogProgress(Shell, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
  103. }
  104. const char * TAG = "chef-app";
  105. #if CONFIG_HAVE_DISPLAY
  106. void printQRCode()
  107. {
  108. // Create buffer for QR code that can fit max size and null terminator.
  109. char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
  110. chip::MutableCharSpan qrCodeText(qrCodeBuffer);
  111. GetQRCode(qrCodeText, chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));
  112. // Initialize the display device.
  113. esp_err_t err = InitDisplay();
  114. if (err != ESP_OK)
  115. {
  116. ChipLogError(Shell, "InitDisplay() failed: %s", esp_err_to_name(err));
  117. return;
  118. }
  119. // Initialize the screen manager
  120. ScreenManager::Init();
  121. ESP_LOGI(TAG, "Opening QR code screen");
  122. ESP_LOGI(TAG, "QR CODE Text: '%s'", qrCodeText.data());
  123. ScreenManager::PushScreen(chip::Platform::New<QRCodeScreen>(qrCodeText.data()));
  124. }
  125. #endif // CONFIG_HAVE_DISPLAY
  126. app::Clusters::NetworkCommissioning::Instance
  127. sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance()));
  128. void InitServer(intptr_t)
  129. {
  130. // Start IM server
  131. static chip::CommonCaseDeviceServerInitParams initParams;
  132. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  133. chip::Server::GetInstance().Init(initParams);
  134. // Device Attestation & Onboarding codes
  135. chip::Credentials::SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
  136. sWiFiNetworkCommissioningInstance.Init();
  137. chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
  138. if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() != CHIP_NO_ERROR)
  139. {
  140. ChipLogError(Shell, "OpenBasicCommissioningWindow() failed");
  141. }
  142. // Register a function to receive events from the CHIP device layer. Note that calls to
  143. // this function will happen on the CHIP event loop thread, not the app_main thread.
  144. PlatformMgr().AddEventHandler(DeviceEventCallback, reinterpret_cast<intptr_t>(nullptr));
  145. }
  146. extern "C" void app_main(void)
  147. {
  148. ESP_ERROR_CHECK(nvs_flash_init());
  149. ESP_ERROR_CHECK(esp_event_loop_create_default());
  150. chip::Platform::MemoryInit();
  151. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
  152. if (DeviceLayer::Internal::ESP32Utils::InitWiFiStack() != CHIP_NO_ERROR)
  153. {
  154. ESP_LOGE(TAG, "Failed to initialize the Wi-Fi stack");
  155. return;
  156. }
  157. #endif
  158. chip::DeviceLayer::PlatformMgr().InitChipStack();
  159. chip::DeviceLayer::PlatformMgr().StartEventLoopTask();
  160. #if CONFIG_ENABLE_CHIP_SHELL
  161. int ret = Engine::Root().Init();
  162. VerifyOrDie(ret == 0);
  163. #endif /* CONFIG_ENABLE_CHIP_SHELL */
  164. // Network connectivity
  165. // Note to integration: StartWiFiManagement does not exist on ESP32
  166. chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer);
  167. PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE));
  168. #if CONFIG_HAVE_DISPLAY
  169. printQRCode();
  170. #endif // CONFIG_HAVE_DISPLAY
  171. #if CONFIG_ENABLE_PW_RPC
  172. chip::rpc::Init();
  173. #endif // CONFIG_ENABLE_PW_RPC
  174. #if CONFIG_ENABLE_CHIP_SHELL
  175. xTaskCreate(&chip_shell_task, "chip_shell", 8192, NULL, 5, NULL);
  176. #endif /* CONFIG_ENABLE_CHIP_SHELL */
  177. while (true)
  178. {
  179. vTaskDelay(100 / portTICK_PERIOD_MS);
  180. }
  181. }