/** \file
 *  \brief This file defines the serial console commands for the Bluetooth Low Energy subsystem.
 */

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

#if (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED) && (CONFIG__FEATURE_BLE == 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 'ble' command.
COMM_Console_Command_Result_T COMM_HandleBLECommand(char8 * data, uint32_t size)
{
    // data[0] through data[3] is 'ble '.
    
    if (data[4] == '?')
    {
        COMM_Console_Print_String("ble ?          Display this help.\n");
        COMM_Console_Print_String("ble cmd <id>   Inject the BLE command with ID <id>.\n");
    }
    else if ( (data[4] == 'c') &&
              (data[5] == 'm') &&
              (data[6] == 'd') )
              
    {
        if (COMM_Console_IsEndOfMessage(data[7]))
        {
            COMM_Console_Print_String("ERROR: missing BLE command ID!\n");
        }
        else if (data[7] == ' ')
        {
            uint16_t id = 0;
        
            if (COMM_Console_DecodeParameterUInt16(&(data[8]), &id) == COMM_CONSOLE_PARAMETER_RESULT_SUCCESS)
            {
                if ((id > COMM_BLE_COMMAND_NO_OP) && (id < COMM_BLE_COMMAND_IS_OUT_OF_RANGE))
                {
                    COMM_BLE_Command_T command = {.ID = id, .Data = (void *)0x00};
                    xQueueSend(COMM_BLE_CommandQueue, &command, 0);
                }
                else
                {
                    COMM_Console_Print_String("ERROR: specified BLE command ID (");
                    COMM_Console_Print_UInt16(id);
                    COMM_Console_Print_String(") is invalid!\n");
                }
            }
            else
            {
                COMM_Console_Print_String("ERROR: could not comprehend BLE command ID!\n");
            }
        }
        else
        {
            COMM_Console_Print_String("ERROR: unrecognized or mangled command!\n");
        }
    }
    else
    {
        COMM_Console_Print_String("ERROR: Unknown BLE command!\n");
    }
    

	return COMM_CONSOLE_CMD_RESULT_SUCCESS;
}

#endif // (CONFIG__FEATURE_COMM_CONSOLE == CONFIG__FEATURE_ENABLED) && (CONFIG__FEATURE_BLE == CONFIG__FEATURE_ENABLED)
