#include <stdio.h>

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

#include "app/scpi/commandHandlers/trg.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

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

static void fsqe_CommandHandlerTRG( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerTRG( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerTRG( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerTRG = 
{
     .f = fsqf_CommandHandlerTRG,
     .enter = fsqe_CommandHandlerTRG,  
     .leave = fsql_CommandHandlerTRG
};

static void fsqe_CommandHandlerTRG( 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_CommandHandlerTRG( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
}

static const struct fFSeqEntry_t * fsqf_CommandHandlerTRG( 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
            {

               // "<...> A *TRG command is not supported by device, and an error is generated
               fsq_RaiseError(   SCPI_ERROR_TRIGGER_IGNORED,
                                 SCPI_ERROR_TRIGGER_IGNORED_MSG,
                                 global_ctx->sParser.xHandlerToken.shead,
                                 global_ctx->sParser.xHandlerToken.stail );

               common_ctx->status = eProgramData_SpecificError;  // 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;
}

