/** \file
 *  \brief This file defines the serial console commands for this CPU.
 */

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

#if (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)

/* Local Definitions and Constants */

/* Private Function Prototypes */
static COMM_Console_Command_Result_T HandleConsoleHelp(char8 * data, uint32_t size);
static COMM_Console_Command_Result_T HandleConsoleComment(char8 * data, uint32_t size);
static COMM_Console_Command_Result_T HandleConsoleUptime(char8 * data, uint32_t size);

/* Public Variables */

const COMM_Console_Command_Table_Entry_T COMM_Console_Command_Table[] =
{
    {"?", "          Show this help.",                                                           HandleConsoleHelp},
    {"#", "          Comment (Do not omit the space after the #.)",                              HandleConsoleComment},
    {"event", "      Generate an event in the high-level state machine (\'event ?\' for help).", COMM_HandleEventCommand},
    {"ble", "        Interact with the Bluetooth Low Energy subsystem (try \'ble ?\').",         COMM_HandleBLECommand},
    {"up", "         Display uptime.",                                                           HandleConsoleUptime},
    {"cpu (r)", "    Display CPU usage ('r' to reset maximum).",                                 COMM_RTOS_HandleConsoleCPU}, 
    {"stack", "      Display stack usage.",                                                      COMM_RTOS_HandleConsoleStack}, 
    {"version", "    Display RTOS version.",                                                     COMM_RTOS_HandleConsoleVersion},
    {"reboot", "     Performs a software reset on both cores.",                                  COMM_RTOS_HandleConsoleReboot},
    {"nvm", "        Interact with the Nonvolatile Memory (try \'nvm ?\').",                     COMM_NVM_HandleConsoleNVMCommand},
	{"reprogram", "  Loads the KTag bootloader for OTA reprogramming.",                          COMM_RTOS_HandleConsoleReprogram},
};

//! Size of the #COMM_Console_Command_Table array (i.e. the number of console commands).
const uint_fast16_t COMM_N_CONSOLE_COMMANDS = (uint_fast16_t) (sizeof(COMM_Console_Command_Table) / sizeof(COMM_Console_Command_Table_Entry_T));

/* Private Variables */

/* Public Functions */

/* Private Functions */

static COMM_Console_Command_Result_T HandleConsoleHelp(char8 * data, uint32_t size)
{
    for (uint_fast16_t i = 0; i < COMM_N_CONSOLE_COMMANDS; i++)
    {
        COMM_Console_Print_String(COMM_Console_Command_Table[i].Command_Name);
        COMM_Console_Print_String("   ");
        COMM_Console_Print_String(COMM_Console_Command_Table[i].Help);
        COMM_Console_Print_String("\n");
        vTaskDelay(pdMS_TO_TICKS(10));
    }
	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}
    
static COMM_Console_Command_Result_T HandleConsoleComment(char8 * data, uint32_t size)
{
    COMM_Console_Print_String("Comment.\n");
	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

static COMM_Console_Command_Result_T HandleConsoleUptime(char8 * data, uint32_t size)
{
#if (configTICK_RATE_HZ != 1000)
#error This code assumes configTICK_RATE_HZ is set to 1000 (== 1ms ticks)!
#endif // (configTICK_RATE_HZ != 1000)
    COMM_Console_Print_String("Up ");
    COMM_Console_Print_UInt32(xTaskGetTickCount());
    COMM_Console_Print_String(" milliseconds.\n");
	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

#endif // (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)
