#include <stdio.h>

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

#include "app/scpi/commandHandlers/rst.h"
#include "app/nfm/nfm_base.h"
#include "app/control_table/control_table.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_CommandHandlerRST
// State's virtual table

static void fsqe_CommandHandlerRST( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerRST( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerRST( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerRST = 
{
     .f = fsqf_CommandHandlerRST,
     .enter = fsqe_CommandHandlerRST,  
     .leave = fsql_CommandHandlerRST
};

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerRST( const struct fFSeqEntry_t * this, 
                                                            tFSeqCtx_t ctx, 
                                                             const struct fFSeqEntry_t * * pDeferredNext )
{
    const fFSeqEntry_t * nextstate = NULL;
    
    sProcessProgramDataCommonContext_t * common_ctx = 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 *RST command returns the instrument to a state where it is waiting for a command to
               // initiate measurements or other instrument actions. <...>", "10 *RST Conditions", [1]
               // "5 Reset and Clear", [2]
               // Provide device-specific reset
               //NFMClass->methods.keyStatesMethods.setKeyStateDefault();
               NFMClass->methods.portMethods.setPortState(eNFMPort_1, 0);
               NFMClass->methods.portMethods.setPortState(eNFMPort_2, 0);
               ControlHandle.SetDefaultState();

               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;
}

