#include <stdio.h>

#define SCPI_ARGS_N 0
#include "app/scpi/scpi_handler.h"

#include "app/scpi/commandHandlers/cls.h"
#include "app/nfm/nfm_base.h"

// Refer to:
// [1] SCPI Specification, revision 1999.0
//     "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
// [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
//     Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
// [3] 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

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

static void fsqe_CommandHandlerCLS( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerCLS( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerCLS( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerCLS = 
{
     .f = fsqf_CommandHandlerCLS,
     .enter = fsqe_CommandHandlerCLS,  
     .leave = fsql_CommandHandlerCLS
};

static void fsqe_CommandHandlerCLS( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    
    SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
}

static void fsql_CommandHandlerCLS( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
}

// "4.1.3.2 *CLS", [1]
// "10.3 *CLS, Clear Status Command", [3]  
static const struct fFSeqEntry_t * fsqf_CommandHandlerCLS( const struct fFSeqEntry_t * this, 
                                                            tFSeqCtx_t ctx, 
                                                             const struct fFSeqEntry_t * * pDeferredNext )
{
    const fFSeqEntry_t * nextstate = NULL;
    
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
    
    switch( common_ctx->event )
    {
        case eProgramData_Event_Write:
        {
            if( common_ctx->isQuery )
            {
               common_ctx->status = eProgramDataSyntaxError; // invalid command header type: QUERY not supported
            }
            else if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
            {
               common_ctx->status = eProgramDataArgumentSyntax;  // parameter syntax error, caller should generate error message
            }
            else
            {

               // clear ESB:
               // "11.5.1.2 Standard Event Status Register Operation", [3]
               // "<...> The Standard Event Status Register is destructively read (that is, read and cleared) <...>", [3]
               uint8_t esr;
               GPIBMachine.fGPIB_get_event_status_register( &global_ctx->sGPIB.registers, &esr );

               // clear STB:
               GPIBMachine.fGPIB_clr_status_byte(  &global_ctx->sGPIB.registers );

               // Clear error queue
               errq_clear(&global_ctx->sExecution.xScpiErrorQueue); 

               common_ctx->status = eProgramDataDone;  // request processed

               // Since @done flag is set, this dispatcher shall not be called anymore.
               // Since this handler is implemented as a single-state automat, there no
               // ... other states to go to:
               (void)nextstate;
            }
        }
        break;
    }

    return nextstate;
}
