| /* |
| * |
| * Copyright (c) 2021 Google LLC. |
| * All rights reserved. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| // ================================================================================ |
| // Main Code |
| // ================================================================================ |
| |
| #include "openthread/platform/logging.h" |
| #include <mbedtls/platform.h> |
| #include <openthread-system.h> |
| #include <openthread/cli.h> |
| #include <openthread/error.h> |
| #include <openthread/heap.h> |
| |
| #include <ChipShellCollection.h> |
| #include <core/CHIPError.h> |
| #include <lib/core/CHIPCore.h> |
| #include <lib/shell/Engine.h> |
| #include <platform/CHIPDeviceLayer.h> |
| #include <platform/ThreadStackManager.h> |
| #include <support/CHIPMem.h> |
| #include <support/CHIPPlatformMemory.h> |
| #include <support/logging/CHIPLogging.h> |
| |
| #include "FreeRtosMbedtlsUtils.h" |
| #include "app_config.h" |
| |
| #include "radio.h" |
| |
| using namespace ::chip; |
| using namespace ::chip::Inet; |
| using namespace ::chip::DeviceLayer; |
| using namespace ::chip::Logging; |
| using chip::Shell::Engine; |
| |
| typedef void (*InitFunc)(void); |
| extern InitFunc __init_array_start; |
| extern InitFunc __init_array_end; |
| |
| /* needed for FreeRtos Heap 4 */ |
| uint8_t __attribute__((section(".heap"))) ucHeap[0xF000]; |
| |
| unsigned int sleep(unsigned int seconds) |
| { |
| const TickType_t xDelay = 1000 * seconds / portTICK_PERIOD_MS; |
| vTaskDelay(xDelay); |
| return 0; |
| } |
| |
| extern "C" void main_task(void const * argument) |
| { |
| CHIP_ERROR ret = CHIP_CONFIG_CORE_ERROR_MAX; |
| |
| /* Call C++ constructors */ |
| InitFunc * pFunc = &__init_array_start; |
| for (; pFunc < &__init_array_end; ++pFunc) |
| { |
| (*pFunc)(); |
| } |
| |
| mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree); |
| |
| /* Used for HW initializations */ |
| otSysInit(0, NULL); |
| |
| K32W_LOG("Welcome to NXP Shell Demo App"); |
| |
| /* Mbedtls Threading support is needed because both |
| * Thread and Weave tasks are using it */ |
| freertos_mbedtls_mutex_init(); |
| |
| // Init Chip memory management before the stack |
| chip::Platform::MemoryInit(); |
| |
| ret = PlatformMgr().InitChipStack(); |
| if (ret != CHIP_NO_ERROR) |
| { |
| K32W_LOG("Error during PlatformMgr().InitWeaveStack()"); |
| goto exit; |
| } |
| |
| ret = ThreadStackMgr().InitThreadStack(); |
| if (ret != CHIP_NO_ERROR) |
| { |
| K32W_LOG("Error during ThreadStackMgr().InitThreadStack()"); |
| goto exit; |
| } |
| |
| ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); |
| if (ret != CHIP_NO_ERROR) |
| { |
| goto exit; |
| } |
| |
| ret = PlatformMgr().StartEventLoopTask(); |
| if (ret != CHIP_NO_ERROR) |
| { |
| K32W_LOG("Error during PlatformMgr().StartEventLoopTask();"); |
| goto exit; |
| } |
| |
| // Start OpenThread task |
| ret = ThreadStackMgrImpl().StartThreadTask(); |
| if (ret != CHIP_NO_ERROR) |
| { |
| K32W_LOG("Error during ThreadStackMgrImpl().StartThreadTask()"); |
| goto exit; |
| } |
| |
| ret = chip::Shell::streamer_init(chip::Shell::streamer_get()); |
| if (ret != 0) |
| { |
| K32W_LOG("Error during streamer_init"); |
| goto exit; |
| } |
| |
| cmd_otcli_init(); |
| cmd_ping_init(); |
| cmd_send_init(); |
| |
| Engine::Root().RunMainLoop(); |
| |
| exit: |
| return; |
| } |