#include <stdio.h>

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

#include "app/scpi/commandHandlers/syst_version.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"
//----------------------------------------------------------------
// Refer "21.21 :VERSion?", [1]

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

static void fsqe_CommandHandlerSystemVersion( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerSystemVersion( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerSystemVersion( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerSystemVersion = 
{
     .f = fsqf_CommandHandlerSystemVersion,
     .enter = fsqe_CommandHandlerSystemVersion,  
     .leave = fsql_CommandHandlerSystemVersion
};

static void fsqe_CommandHandlerSystemVersion( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{    
    sProcessProgramDataCommonContext_t * common_ctx = ctx;

    common_ctx->SYSTemVERSion.idx = 0; // reset position
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerSystemVersion( 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: COMMAND not supported
            }
            else
            {
               common_ctx->status = eProgramDataNeedRead;  // request processed, wait for reading...
               (void)nextstate; // stay in this state
            }
        }
        break;

        case eProgramData_Event_Read:
        {
             // @idx - current position of the source data to be outputed
             if( common_ctx->SYSTemVERSion.idx == 0 ) // first reading
             {
                 memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );

                 _snprintf( common_ctx->tempBuffer,
                           sizeof( common_ctx->tempBuffer ),
                           "%4d.%1d", SCPI_VERSION_YEAR, SCPI_VERSION_REVISION );
             }

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

             // modify current postion index:
             SCPI_RESPONSE_HELPER( common_ctx, common_ctx->SYSTemVERSion.idx);
        }
        break;
    }

    return nextstate;
}

