// =============================================================================
//                       Command description ESE
// =============================================================================
// Semantic for command: %command <%value>
// Syntax for command :  *ESE <numeric>
// Semantic for query: %query
// Syntax for query :  *ESE?
// Sets or reads the Standard Event Status Enable Register (command/request).
// Arguments:
//  @value - <numeric> from 0 to 255:
// Returns:
//  <numeric>.
// -----
// =============================================================================
//                       Command description SRE
// =============================================================================
// Semantic for command: %command <%value>
// Syntax for command :  *ESR <numeric>
// Semantic for query: %query
// Syntax for query :  *ESR?
// Sets or reads the Service Request Enable Register (command/request).
// Arguments:
//  @value - <numeric> from 0 to 255:
// Returns:
//  <numeric>.
// -----
#include <stdio.h>
#include <stdint.h>

const uint8_t fsqvbl_CommandHandlerESE_context = 1;
const uint8_t fsqvbl_CommandHandlerSRE_context = 2;

#define SCPI_ARGS_N_C 1
#define SCPI_ARGS_N_Q 0
#include "app/scpi/scpi_handler.h"
// -----
// @argTypesCommand
// Declare argument parser entities for command form
// Supported arguments: 1=NUMERIC
DECLARE_SCPI_ARGS_C( eScpiArg_Numeric );
// -----
// @argTypesQuery
// Declare argument parser entities for query form
// Supported arguments: 0
DECLARE_SCPI_ARGS_Q();

DECLARE_ARGUMENT_NUMERIC_VALUES_I8(AllowedValues_Argument1, 0, 255);

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

static void fsqe_CommandHandlerESESRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerESESRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerESESRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerESESRE = 
{
     .f = fsqf_CommandHandlerESESRE,
     .enter = fsqe_CommandHandlerESESRE,  
     .leave = fsql_CommandHandlerESESRE
};

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

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

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerESESRE( 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( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
            {
               common_ctx->status = eProgramDataArgumentSyntax;  // parameter syntax error, caller should generate error message
            }
            else
            if( ! common_ctx->isQuery )
            {
               common_ctx->status = eProgramDataSyntaxError; // forward set

               const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Argument1, 0);

               if( NULL != ne )
               switch( ne->error )
               {
                  case ScpiNumericSuccess:
                  {
                       if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerESE_context )
                       GPIBMachine.fGPIB_set_event_status_enable_register( &global_ctx->sGPIB.registers,
                                                                           (uint8_t)ne->Value.i );

                       if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSRE_context )
                       GPIBMachine.fGPIB_set_service_request_enable_register( &global_ctx->sGPIB.registers,
                                                                              (uint8_t)ne->Value.i );
                       common_ctx->status = eProgramDataDone;
                  }
                  break;

                  // -------- processed by SCPI_PROCESS_ARGUMENT_NUMERIC ----
                  // "11.5.1.1.5", [3]
                  // "10.10.6 Error Handling", [3]
                  // "<...> An out-of-range integer shall cause an Execution Error, see 11.5.1.1.5. <...>"
                  case ScpiNumericError_DEVICE_RANGE:   // the value is out of range supported by device
                  case ScpiNumericError_USER_RANGE:     // the value is out of user specified range:   // the value is out of range supported by device
                  case ScpiNumericError_USER_TYPE:      // the value does not match to the user expectation
                  case ScpiNumericInvalidArg:           // forbidden case: design bug                      
                  case ScpiNumericError:                // generic numeric conversion error
                  break;
                  // --------------------------------------------------------
               }
            }
            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->ESESRE.idx )
             {
                 memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );

                 uint8_t ESESRE = 0;
               
                 // Read ESE:
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerESE_context )
                 GPIBMachine.fGPIB_get_event_status_enable_register(  &global_ctx->sGPIB.registers, &ESESRE );
                 

                 // Read ESB:
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSRE_context )
                 GPIBMachine.fGPIB_get_service_request_enable_register(  &global_ctx->sGPIB.registers, &ESESRE );
                 
                 int iESESRE = (int)ESESRE;

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

             }

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

    return nextstate;
}

