#error Deprecated. 'esr_stb' is used instead

#include <stdio.h>

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

#include "app/scpi/commandHandlers/esr.h"
#include "app/acm/acm_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_CommandHandlerESR
// State's virtual table

static void fsqe_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerESR = 
{
     .f = fsqf_CommandHandlerESR,
     .enter = fsqe_CommandHandlerESR,  
     .leave = fsql_CommandHandlerESR
};

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

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

    (void)global_ctx;
}

static void fsql_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
    
    (void)common_ctx;
    (void)global_ctx;
}

static const struct fFSeqEntry_t * fsqf_CommandHandlerESR( 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;
    
    (void)common_ctx;
    (void)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 (only query)
            }
            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:
        {
             // Take the input buffer by @pBuffer and @nBufferLength
             // @dst = @pBuffer, output response buffer
             // @bsize = @nBufferLength, output buffer size
             uint8_t * dst = (uint8_t*)global_ctx->sRead.pBufferIdx;
             size_t bsize = global_ctx->sRead.nBufferSize;

             // @idx - current position of the source data to be outputed
             size_t idx = common_ctx->ESR.idx;
             
             if( 0 == idx )
             {
                 memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );

                 uint8_t esr = 0;
                 
                 // Read ESB:
                 // "11.5.1.2 Standard Event Status Register Operation", [3]
                 // "<...> The Standard Event Status Register is destructively read (that is, read and cleared) <...>", [3]
                 // retrieve ESB and clear it
                 GPIBMachine.fGPIB_get_event_status_register(  &global_ctx->sGPIB.registers, &esr );
                 
                 int iesr = (int)esr;

                 // first call: prepare buffer
                 _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d", iesr );

             }

             // @esr_str - response string
             const char * esr_str = common_ctx->tempBuffer;
             
             // Copy response string char by char to the output buffer
             // ... until the null-character is reached or the output
             // ... buffer free space is ran out:
             while( ('\0' != esr_str[idx]) && ( 0<bsize ) )
             {
                 *dst++ = esr_str[idx++]; bsize--;                 
             }

             // Set the @nDataLength to indicate the copied part size:
             // @bsize after copying represent the free space left in bytes;
             // @nBufferLength after copying represent the free space in bytes before copying;
             // Thus (@nBufferSize-@bsize) is the number of bytes has been copyied:
             global_ctx->sRead.nDataLength += global_ctx->sRead.nBufferSize - bsize;

             // Check for end-condition:
             if( '\0' == esr_str[idx] )
             {
                 common_ctx->status = eProgramDataDone;
             }

             // 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:
             common_ctx->ESR.idx = idx;
        }
        break;
    }

    return nextstate;
}

