#include <stdio.h>

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

#include "app/scpi/commandHandlers/idn.h"
#include "app/nfm/nfm_base.h"
// =================================================================================
// @fsqvbl_CommandHandlerIDN
// State's virtual table

static void fsqe_CommandHandlerIDN( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerIDN( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerIDN( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerIDN = 
{
     .f = fsqf_CommandHandlerIDN,
     .enter = fsqe_CommandHandlerIDN,  
     .leave = fsql_CommandHandlerIDN
};

static void fsqe_CommandHandlerIDN( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{    
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    
    common_ctx->IDN.idx = 0; // reset position

    SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerIDN( 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 if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
            {
               common_ctx->status = eProgramDataArgumentSyntax;  // parameter syntax error, caller should generate error message
            }
            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( 0 == common_ctx->IDN.idx )
             {
                 memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );

                 // first call: prepare buffer
                 _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s,%s,%s,%s", 
                                   NFMClass->properties.manufacturerId, 
                                   NFMClass->properties.modelName, 
                                   NFMClass->properties.serialNumber, 
                                   NFMClass->properties.firmwareId );

             }
             
             // 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->IDN.idx );
        }
        break;
    }

    return nextstate;
}

