/** \file
 *  \brief This file defines the serial console commands for the Nonvolatile Memory.
 */

/* 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 */

//! Console command handler for subcommands of the 'nvm' command.
COMM_Console_Command_Result_T COMM_NVM_HandleConsoleNVMCommand(char8 * data, uint32_t size)
{
    // data[0] through data[3] is 'nvm '.    
    if (data[4] == '?')
    {
        COMM_Console_Print_String("nvm ?                         Display this help.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("nvm dump                      Display the entire Nonvolatile Memory.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("nvm names                     Display the NVM parameter names.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("nvm get <parameter>           Display an individual parameter from NVM.\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("nvm set <parameter> <value>   Assign a value to an individual parameter in NVM (be careful!).\n");
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    else if ((data[4] == 'd') && (data[5] == 'u') && (data[6] == 'm') && (data[7] == 'p'))
    {
        for (uint8_t i = 0; i < NVM_N_ONCHIP_EEPROM_ENTRIES; i++)
        {
            COMM_Console_Print_String("NVM[");
            COMM_Console_Print_UInt16(i);
            COMM_Console_Print_String("]: ");
            
            for (uint8_t j = 0; j < NVM_OnChipEEPROMEntries[i]->Size; j++)
            {
                char8 buffer[3];
                
                COMM_Console_ByteToHex(buffer, *(NVM_OnChipEEPROMEntries[i]->Value + j));
                COMM_Console_Print_String("0x");
                COMM_Console_Print_String(buffer);
                COMM_Console_Print_String(" ");
            }
            COMM_Console_Print_String("\n");
        }
    }
    else if ((data[4] == 'n') && (data[5] == 'a') && (data[6] == 'm') && (data[7] == 'e') && (data[8] == 's'))
    {
        COMM_Console_Print_String("Valid NVM parameters:\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("   test_1\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("   test_2\n");
        vTaskDelay(pdMS_TO_TICKS(10));
        COMM_Console_Print_String("   volume\n");
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    else if ((data[4] == 'g') && (data[5] == 'e') && (data[6] == 't') && (data[7] == ' '))
    {
        if (strncmp(&data[8], "volume", 6) == 0)
        {
            COMM_Console_Print_String("Volume: ");
            COMM_Console_Print_UInt8(NVM_VOLUME);
            COMM_Console_Print_String("\n");
        }
        else if (strncmp(&data[8], "test_1", 6) == 0)
        {
            COMM_Console_Print_String("Test 1: ");
            COMM_Console_Print_UInt16(NVM_ONCHIP_TEST_1);
            COMM_Console_Print_String("\n");
        }
        else if (strncmp(&data[8], "test_2", 6) == 0)
        {
            COMM_Console_Print_String("Test 2: ");
            COMM_Console_Print_UInt32(NVM_ONCHIP_TEST_2);
            COMM_Console_Print_String("\n");
        }
        else if (strncmp(&data[8], "test_3", 6) == 0)
        {
            COMM_Console_Print_String("Test 3: ");
            COMM_Console_Print_UInt16(NVM_EXTERNAL_TEST_3);
            COMM_Console_Print_String("\n");
        }
        else if (strncmp(&data[8], "test_4", 6) == 0)
        {
            COMM_Console_Print_String("Test 4: ");
            COMM_Console_Print_UInt32(NVM_EXTERNAL_TEST_4);
            COMM_Console_Print_String("\n");
        }
        else
        {
            COMM_Console_Print_String("ERROR: Unknown NVM parameter!\n");
        }
    }
    else if ((data[4] == 's') && (data[5] == 'e') && (data[6] == 't') && (data[7] == ' '))
    {
        if (strncmp(&data[8], "volume", 6) == 0)
        {
            uint8_t volume = 0;
            if (COMM_Console_DecodeParameterUInt8(&(data[15]), &volume) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                NVM_VOLUME = volume;
                NVM_SaveExternalEEPROMEntry(NVM_VOLUME_ENTRY_PTR);
                
                COMM_Console_Print_String("Volume changed to ");
                COMM_Console_Print_UInt8(NVM_VOLUME);
                COMM_Console_Print_String("\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: Parameter value unrecognized or missing!\n");
            }
        }
        else if (strncmp(&data[8], "test_1", 6) == 0)
        {
            uint16_t test_value = 0;
            if (COMM_Console_DecodeParameterUInt16(&(data[15]), &test_value) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                NVM_ONCHIP_TEST_1 = test_value;
                NVM_SaveOnChipEEPROMEntry(NVM_ONCHIP_TEST_1_ENTRY_PTR);
                
                COMM_Console_Print_String("Test 1 value changed to ");
                COMM_Console_Print_UInt16(NVM_ONCHIP_TEST_1);
                COMM_Console_Print_String("\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: Parameter value unrecognized or missing!\n");
            }
        }
        else if (strncmp(&data[8], "test_2", 6) == 0)
        {
            uint32_t test_value = 0;
            if (COMM_Console_DecodeParameterUInt32(&(data[15]), &test_value) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                NVM_ONCHIP_TEST_2 = test_value;
                NVM_SaveOnChipEEPROMEntry(NVM_ONCHIP_TEST_2_ENTRY_PTR);
                
                COMM_Console_Print_String("Test 2 value changed to ");
                COMM_Console_Print_UInt32(NVM_ONCHIP_TEST_2);
                COMM_Console_Print_String("\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: Parameter value unrecognized or missing!\n");
            }
        }
        else if (strncmp(&data[8], "test_3", 6) == 0)
        {
            uint16_t test_value = 0;
            if (COMM_Console_DecodeParameterUInt16(&(data[15]), &test_value) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                NVM_EXTERNAL_TEST_3 = test_value;
                NVM_SaveExternalEEPROMEntry(NVM_EXTERNAL_TEST_3_ENTRY_PTR);
                
                COMM_Console_Print_String("Test 3 value changed to ");
                COMM_Console_Print_UInt16(NVM_EXTERNAL_TEST_3);
                COMM_Console_Print_String("\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: Parameter value unrecognized or missing!\n");
            }
        }
        else if (strncmp(&data[8], "test_4", 6) == 0)
        {
            uint32_t test_value = 0;
            if (COMM_Console_DecodeParameterUInt32(&(data[15]), &test_value) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                NVM_EXTERNAL_TEST_4 = test_value;
                NVM_SaveExternalEEPROMEntry(NVM_EXTERNAL_TEST_4_ENTRY_PTR);
                
                COMM_Console_Print_String("Test 4 value changed to ");
                COMM_Console_Print_UInt32(NVM_EXTERNAL_TEST_4);
                COMM_Console_Print_String("\n");
            }
            else
            {
                COMM_Console_Print_String("ERROR: Parameter value unrecognized or missing!\n");
            }
        }
        else
        {
            COMM_Console_Print_String("ERROR: Unknown NVM parameter!\n");
        }
    }
    else
    {
        COMM_Console_Print_String("ERROR: Unknown NVM command!\n");
    }

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

#endif // (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED)
