#define SCPI_CORE_STATE__RESETSTATE_GUARD
#include "app/scpi/CommandParserStates/reset_state.h"
#include "app/scpi/CommandParserStates/search_for_command.h"
#define SCPI_CORE_PRIVATE    // access to 'scpi_core_private.h'
#define SCPI_RESET_STATE_C   // access to const values in 'scpi_core_private.h' structures
#include "app/scpi/scpi_core_private.h"

//----------------------------------------------------------------
// Refer to:
// [1] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
//     "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
//      Standard Digital Interface for Programmable Instrumentation"

// =================================================================================
// @fsqvbl_SearchForCommandHeader
// State's virtual table

static void fsqe_ParserResetState( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_ParserResetState( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_ParserResetState( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pnext );

const fFSeqVTable_t fsqvbl_ParserResetState = 
{
     .f = fsqf_ParserResetState,
     .enter = fsqe_ParserResetState,  
     .leave = fsql_ParserResetState
};

// =================================================================================
// @fsqe_ParserResetState
// State's enter routine
static void fsqe_ParserResetState( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sScpiParserContext_t * global_ctx = ctx;    

    (void)global_ctx;
}

// =================================================================================
// @fsql_ParserResetState
// State's leave routine
static void fsql_ParserResetState( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sScpiParserContext_t * global_ctx = ctx;    

    (void)global_ctx;
}

// =================================================================================
// @fsqf_ParserResetState
// State's body routine
static const struct fFSeqEntry_t * fsqf_ParserResetState( const struct fFSeqEntry_t * this, 
                                                                 tFSeqCtx_t ctx, 
                                                                  const struct fFSeqEntry_t * * pDeferredNext )
{
    // Function prepares the entire automat for work by initializing
    // ... global context.

    // --------------------------------------------------------------------------    
    sScpiParserContext_t * global_ctx = ctx;    
    // --------------------------------------------------------------------------

    // prepare global context:
    global_ctx->sWrite.pData = NULL;
    global_ctx->sWrite.nDataLength = 0;
    global_ctx->sMessage.pStr = NULL;
    global_ctx->sMessage.pEnd = NULL;
    global_ctx->sRead.pBuffer = NULL;
    global_ctx->sRead.pBufferIdx = NULL;
    global_ctx->sRead.nBufferSize = 0;
    global_ctx->sRead.nDataLength = 0;
    global_ctx->sRead.nTotalLength = 0;
    global_ctx->sRead.vLastBufferIdx = NULL;
    global_ctx->sEvent.eCode = eScpiEventEmpty; // no event occurred   
    global_ctx->sParser.xCmdBranch = NULL; // =NULL: reset command branch to the root
    global_ctx->sParser.xHandler = NULL;
    global_ctx->sParser.xHandlerToken.shead = NULL;
    global_ctx->sParser.xHandlerToken.stail = NULL;
    global_ctx->sParser.bFirstCommandIndicator = true; // set first command indicator
    global_ctx->sEvent.eCode = eScpiEventEmpty; // reset event code
    global_ctx->sEvent.eStatus = eScpiStatus_success; // reset status code
    global_ctx->sExecution.runtimeError = 0;
    (void)global_ctx->sExecution.errorQueueOverflow; // do not reset here
    memset( global_ctx->sExecution.xScpiErrorMessage, 0, sizeof(global_ctx->sExecution.xScpiErrorMessage) );

    // "6.1.10.2.1 Message Available Message (MAV)", [1]
    // Actually, there no data in the output buffer now. Reset the MAV
    // Force reset MAV bit
    GPIBMachine.fGPIB_set_message_available_bit( &global_ctx->sGPIB.registers, false );

    // "5.8 Device Clear Requirements", [1]
    // DO NOT clear Error Queue and any bits other than clearing MAV bit in GPIB

    return fsq_GetStateById(this,eSearchForCommandHeader);    // go to next state: eSearchForCommandHeader
}