#include <stdio.h>

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

// -----
// @argTokens, @argTypes
// Declare argument parser entities
// Supported arguments: 1=CHARACTER
DECLARE_SCPI_ARGS( eScpiArg_Character );

// Argument 1 Character Values allowed list / ACM Port
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_Port );

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

static void fsqe_CommandHandlerMemoryTableConnector( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerMemoryTableConnector( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableConnector( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerMEMoryTABLeCONNector = 
{
     .f = fsqf_CommandHandlerMemoryTableConnector,
     .enter = fsqe_CommandHandlerMemoryTableConnector,  
     .leave = fsql_CommandHandlerMemoryTableConnector
};

static void fsqe_CommandHandlerMemoryTableConnector( 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->MemTableCommon.idx = 0;
    common_ctx->MemTableCommon.bank = 0;
    common_ctx->MemTableCommon.port = 0;
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableConnector( 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 = eProgramDataIllegalArgument;  // forward set, illegal parameter value, caller should generate error message

               // process first argument (port)
               common_ctx->MemTableCommon.port = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_Port, 0 );
               
               // check result
               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID != common_ctx->MemTableCommon.port )
               {
                   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->MemTableCommon.idx == 0 ) // first reading
             {
                 ePortId_t port = (ePortId_t)((ePortId_A) + common_ctx->MemTableCommon.port); 
                 size_t length = 0;

                 if( NFMClass->methods.xCharacterization.getConnectorType( port,
                                                                           common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), 
                                                                           &length ) )
                 {
                     if( length == 0 )
                     {
                         common_ctx->tempBuffer[0] = '\'';
                         common_ctx->tempBuffer[1] = ' ';
                         common_ctx->tempBuffer[2] = '\'';
                         length = 3;
                     }
                     // place null-terminator in the end of line
                     common_ctx->tempBuffer[length] = '\0';
                 }
                 else
                 {
                     // Formal call: in case the parameter is optional, the token is unfilled.
                     // So it is required to fill the token with correct values from allowed list.
                     SCPI_ARGUMENT_CHARACTER_VALUE_TOKEN( MemTable_AllowedValues_Port,
                                                          common_ctx->MemTableCommon.port,
                                                         &common_ctx->argTokens[0] );

                     fsq_RaiseErrorEx( SCPI_ERROR_DATA_CORRUPTED,
                                       SCPI_ERROR_DATA_CORRUPTED_MSG,
                                       common_ctx->argTokens[0].shead,
                                       common_ctx->argTokens[0].stail,
                                       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->MemTableCommon.idx );
        }
        break;
    }

    return nextstate;
}
