blob: 152900341c40678e57201f5ee8f95dd4fac28146 [file] [log] [blame]
/*
*
* 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 <AppTask.h>
#include <mbedtls/platform.h>
#include <ChipShellCollection.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPError.h>
#include <lib/shell/Engine.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPPlatformMemory.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ThreadStackManager.h>
#include "FreeRtosHooks.h"
#include "app_config.h"
const uint16_t shell_task_size = 3096;
const uint8_t shell_task_priority = 0;
using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Logging;
using chip::Shell::Engine;
#include "MacSched.h"
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[HEAP_SIZE];
extern "C" unsigned int sleep(unsigned int seconds)
{
const TickType_t xDelay = 1000 * seconds / portTICK_PERIOD_MS;
vTaskDelay(xDelay);
return 0;
}
static void shell_task(void * args)
{
Engine::Root().Init();
Engine::Root().RunMainLoop();
}
extern "C" void main_task(void const * argument)
{
int status = 0;
char * argv[1] = { 0 };
BaseType_t shellTaskHandle;
CHIP_ERROR err = CHIP_NO_ERROR;
/* Call C++ constructors */
InitFunc * pFunc = &__init_array_start;
for (; pFunc < &__init_array_end; ++pFunc)
{
(*pFunc)();
}
err = PlatformMgrImpl().InitBoardFwk();
if (err != CHIP_NO_ERROR)
{
return;
}
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
K32W_LOG("Welcome to NXP Shell Demo App");
/* Mbedtls Threading support is needed because both
* Thread and Matter tasks are using it */
freertos_mbedtls_mutex_init();
// Init Chip memory management before the stack
chip::Platform::MemoryInit();
CHIP_ERROR ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
{
K32W_LOG("Error during PlatformMgr().InitMatterStack()");
goto exit;
}
ret = ThreadStackMgr().InitThreadStack();
if (ret != CHIP_NO_ERROR)
{
K32W_LOG("Error during ThreadStackMgr().InitThreadStack()");
goto exit;
}
/* Enable the MAC scheduler after BLEManagerImpl::_Init() and V2MMAC_Enable().
* This is needed to register properly the active protocols.
*/
sched_enable();
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;
}
shellTaskHandle = xTaskCreate(shell_task, "shell_task", shell_task_size / sizeof(StackType_t), NULL, shell_task_priority, NULL);
if (!shellTaskHandle)
{
K32W_LOG("Error while creating the shell task!");
goto exit;
}
ret = GetAppTask().StartAppTask();
if (ret != CHIP_NO_ERROR)
{
K32W_LOG("Error during GetAppTask().StartAppTask()");
goto exit;
}
GetAppTask().AppTaskMain(NULL);
exit:
return;
}
extern "C" void otSysEventSignalPending(void)
{
{
BaseType_t yieldRequired = ThreadStackMgrImpl().SignalThreadActivityPendingFromISR();
portYIELD_FROM_ISR(yieldRequired);
}
}