#include <stdio.h>

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

const uint8_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyStart = 1;  // MEMory:TABLe:THERmo:CORRection:FREQuency:STARt
const uint8_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyStop = 2;   // MEMory:TABLe:THERmo:CORRection:FREQuency:STOP
const uint8_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyPoints = 3; // MEMory:TABLe:THERmo:CORRection:FREQuency:POINts

#include "app/scpi/commandHandlers/memory_table_ther_corr_group.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_CommandHandlerMEMoryTABLeTHERmoCORRection_group
// State's virtual table

static void fsqe_CommandHandlerMemoryTableThermoCorrection_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerMemoryTableThermoCorrection_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableThermoCorrection_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerMEMoryTABLeTHERmoCORRection_group = 
{
     .f = fsqf_CommandHandlerMemoryTableThermoCorrection_Group,
     .enter = fsqe_CommandHandlerMemoryTableThermoCorrection_Group,  
     .leave = fsql_CommandHandlerMemoryTableThermoCorrection_Group
};

static void fsqe_CommandHandlerMemoryTableThermoCorrection_Group( 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

    common_ctx->MemTableThermoCorrCommon.idx = 0;
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableThermoCorrection_Group( 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: 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...
            }
        }
        break;

        case eProgramData_Event_Read:
        {
             // @idx - current position of the source data to be outputed
             if( common_ctx->MemTableThermoCorrCommon.idx == 0 ) // first reading
             {
                 size_t length = 0;
                 bool rc = false;

                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyStart )
                 {
                     double freq = 0.0;
                     rc = NFMClass->methods.xThermo.getStartFreq( &freq );

                     if( rc ) length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%1.8E", freq );
                 }
                 else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyStop )
                 {
                     double freq = 0.0;
                     rc = NFMClass->methods.xThermo.getStopFreq( &freq );

                     if( rc ) length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%1.8E", freq );
                 }
                 else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionFrequencyPoints )
                 {
                     int16_t points = 0;
                     rc = NFMClass->methods.xThermo.getPointsCountSafe( &points );

                     if( rc ) length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d", points );
                 }

                 if( rc )
                 {
                     if( length == 0 )
                     {
                         common_ctx->tempBuffer[0] = '0';
                         length = 1;
                     }
                     // place null-terminator in the end of line
                     common_ctx->tempBuffer[length] = '\0';
                 }
                 else
                 {
                     
                     fsq_RaiseError(   SCPI_ERROR_THERM_DATA_NOTFOUND,
                                       SCPI_ERROR_THERM_DATA_NOTFOUND_MSG,
                                       global_ctx->sParser.xHandlerToken.shead,
                                       global_ctx->sParser.xHandlerToken.stail );

                     common_ctx->status = eProgramData_SpecificError; // specific error already generated
                     break;
                 }
             }

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

    return nextstate;
}
