main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. *
  3. * Copyright (c) 2021 Google LLC.
  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. // ================================================================================
  19. // Main Code
  20. // ================================================================================
  21. #include "openthread/platform/logging.h"
  22. #include <mbedtls/platform.h>
  23. #include <openthread-system.h>
  24. #include <openthread/cli.h>
  25. #include <openthread/error.h>
  26. #include <openthread/heap.h>
  27. #include <ChipShellCollection.h>
  28. #include <core/CHIPError.h>
  29. #include <lib/core/CHIPCore.h>
  30. #include <lib/shell/Engine.h>
  31. #include <platform/CHIPDeviceLayer.h>
  32. #include <platform/ThreadStackManager.h>
  33. #include <support/CHIPMem.h>
  34. #include <support/CHIPPlatformMemory.h>
  35. #include <support/logging/CHIPLogging.h>
  36. #include "FreeRtosMbedtlsUtils.h"
  37. #include "app_config.h"
  38. #include "radio.h"
  39. using namespace ::chip;
  40. using namespace ::chip::Inet;
  41. using namespace ::chip::DeviceLayer;
  42. using namespace ::chip::Logging;
  43. using chip::Shell::Engine;
  44. typedef void (*InitFunc)(void);
  45. extern InitFunc __init_array_start;
  46. extern InitFunc __init_array_end;
  47. /* needed for FreeRtos Heap 4 */
  48. uint8_t __attribute__((section(".heap"))) ucHeap[0xF000];
  49. unsigned int sleep(unsigned int seconds)
  50. {
  51. const TickType_t xDelay = 1000 * seconds / portTICK_PERIOD_MS;
  52. vTaskDelay(xDelay);
  53. return 0;
  54. }
  55. extern "C" void main_task(void const * argument)
  56. {
  57. /* Call C++ constructors */
  58. InitFunc * pFunc = &__init_array_start;
  59. for (; pFunc < &__init_array_end; ++pFunc)
  60. {
  61. (*pFunc)();
  62. }
  63. mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
  64. /* Used for HW initializations */
  65. otSysInit(0, NULL);
  66. K32W_LOG("Welcome to NXP Shell Demo App");
  67. /* Mbedtls Threading support is needed because both
  68. * Thread and Weave tasks are using it */
  69. freertos_mbedtls_mutex_init();
  70. // Init Chip memory management before the stack
  71. chip::Platform::MemoryInit();
  72. CHIP_ERROR ret = PlatformMgr().InitChipStack();
  73. if (ret != CHIP_NO_ERROR)
  74. {
  75. K32W_LOG("Error during PlatformMgr().InitWeaveStack()");
  76. goto exit;
  77. }
  78. ret = ThreadStackMgr().InitThreadStack();
  79. if (ret != CHIP_NO_ERROR)
  80. {
  81. K32W_LOG("Error during ThreadStackMgr().InitThreadStack()");
  82. goto exit;
  83. }
  84. ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
  85. if (ret != CHIP_NO_ERROR)
  86. {
  87. goto exit;
  88. }
  89. ret = PlatformMgr().StartEventLoopTask();
  90. if (ret != CHIP_NO_ERROR)
  91. {
  92. K32W_LOG("Error during PlatformMgr().StartEventLoopTask();");
  93. goto exit;
  94. }
  95. // Start OpenThread task
  96. ret = ThreadStackMgrImpl().StartThreadTask();
  97. if (ret != CHIP_NO_ERROR)
  98. {
  99. K32W_LOG("Error during ThreadStackMgrImpl().StartThreadTask()");
  100. goto exit;
  101. }
  102. ret = chip::Shell::streamer_init(chip::Shell::streamer_get());
  103. if (ret != 0)
  104. {
  105. K32W_LOG("Error during streamer_init");
  106. goto exit;
  107. }
  108. cmd_otcli_init();
  109. cmd_ping_init();
  110. cmd_send_init();
  111. Engine::Root().RunMainLoop();
  112. exit:
  113. return;
  114. }