/** \file
 *  \brief This file defines the serial console commands for the RTOS package.
 */

/* Include Files */
#include "KTag.h"

#if (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)

/* Local Definitions and Constants */

/* Private Function Prototypes */

/* Public Variables */

/* Private Variables */

/* Public Functions */

/* Private Functions */

COMM_Console_Command_Result_T COMM_RTOS_HandleConsoleVersion(char8 * data, uint32_t size)
{
    COMM_Console_Print_String("PSoC 6 running FreeRTOS ");

    COMM_Console_Print_String(tskKERNEL_VERSION_NUMBER);
    
#ifdef NDEBUG
    COMM_Console_Print_String(" (Release, compiled ");
#else
    COMM_Console_Print_String(" (Debug, compiled ");
#endif // NDEBUG

    COMM_Console_Print_String(__DATE__);
    COMM_Console_Print_String(" ");
    COMM_Console_Print_String(__TIME__);
    COMM_Console_Print_String(").\n");

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

COMM_Console_Command_Result_T COMM_RTOS_HandleConsoleStack(char8 * data, uint32_t size)
{
    for (uint_fast8_t i = 0; i < CONFIG_N_TASK_HANDLES; i++)
    {
        TaskStatus_t status;
        vTaskGetInfo(*CONFIG_TaskHandles[i], &status, pdTRUE, eInvalid);
        COMM_Console_Print_String(status.pcTaskName);
        COMM_Console_Print_String(": ");
        COMM_Console_Print_UInt16(status.usStackHighWaterMark);
        COMM_Console_Print_String("\n");
    }
    
    // Repeat for the Idle Task.
    {
        TaskStatus_t status;
        vTaskGetInfo(xTaskGetIdleTaskHandle(), &status, pdTRUE, eInvalid);
        COMM_Console_Print_String(status.pcTaskName);
        COMM_Console_Print_String(": ");
        COMM_Console_Print_UInt16(status.usStackHighWaterMark);
        COMM_Console_Print_String("\n");
    }

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

COMM_Console_Command_Result_T COMM_RTOS_HandleConsoleCPU(char8 * data, uint32_t size)
{
    // data[0] through data[3] is 'cpu '.    
    if (data[4] == 'r')
    {
        //COMM_Console_Print_String("Max CPU reset.\n");
        COMM_Console_Print_String("(Not yet implemented.)\n");
    }
    else
    {
        for (uint_fast8_t i = 0; i < CONFIG_N_TASK_HANDLES; i++)
        {
            TaskStatus_t status;
            vTaskGetInfo(*CONFIG_TaskHandles[i], &status, pdTRUE, eInvalid);
            COMM_Console_Print_String(status.pcTaskName);
            COMM_Console_Print_String(": ");
            COMM_Console_Print_UInt32(status.ulRunTimeCounter);
            COMM_Console_Print_String("\n");
        }
        
        // Repeat for the Idle Task.
        {
            TaskStatus_t status;
            vTaskGetInfo(xTaskGetIdleTaskHandle(), &status, pdTRUE, eInvalid);
            COMM_Console_Print_String(status.pcTaskName);
            COMM_Console_Print_String(": ");
            COMM_Console_Print_UInt16(status.ulRunTimeCounter);
            COMM_Console_Print_String("\n");
        }
    }

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

COMM_Console_Command_Result_T COMM_RTOS_HandleConsoleReboot(char8 * data, uint32_t size)
{    
    (void) COMM_SendMessageToOtherCore(COMM_SMM_RebootImmediately, NULL);

    // Not that it matters...
	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

COMM_Console_Command_Result_T COMM_RTOS_HandleConsoleReprogram(char8 * data, uint32_t size)
{
    COMM_Console_Print_String("Entering bootloader for BLE reprogramming.\n");
    vTaskDelay(pdMS_TO_TICKS(100));
    
    Cy_DFU_ExecuteApp(0u);

    // Not that it matters...
	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

#endif // (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)
