#include <stdio.h>

#define SCPI_ARGS_N_Q 0
#define SCPI_ARGS_N_C 1
#include "app/scpi/scpi_handler.h"

const uint8_t fsqvbl_CommandHandlerServiceState  = 1;        // SERVICE:STATE
const uint8_t fsqvbl_CommandHandlerSwitchSerial  = 2;        // SERVICE:SERIAL
const uint8_t fsqvbl_CommandHandlerSwitchModel   = 3;        // SERVICE:MODEL
const uint8_t fsqvbl_CommandHandlerServiceReboot = 4;        // SERVICE:REBOOT

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

DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_ServiceState, 0, 1);
DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_Serial, 0, 99999999);
DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_Model, 0, 255);

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

static void fsqe_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerSERVice_group = 
{
     .f = fsqf_CommandHandlerService,
     .enter = fsqe_CommandHandlerService,  
     .leave = fsql_CommandHandlerService
};

static void fsqe_CommandHandlerService( 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->SwitchState.idx = 0;
    common_ctx->SwitchState.state = 0;
}

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

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

               if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceState )
               {
                   // process first argument (switch state)
                   const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_ServiceState, 0);
                   if( ScpiNumericSuccess != ne->error ) break;
                   NFMClass->properties.isServiceMode = ne->Value.demicalInteger;
               }
               
               if(NFMClass->properties.isServiceMode)
               {
                   if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial )
                   {
                       // process first argument (switch state)
                       const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Serial, 0);
                       if( ScpiNumericSuccess != ne->error ) break;
                       
                       // disable memory protection if needed
                       if(NFMClass->methods.xMemoryProtection.check())
                           NFMClass->methods.xMemoryProtection.disable();
                       
                       char serial[10];
                       memset(&serial, 0x30, sizeof(serial));
                       
                       int numberOfDigits = common_ctx->argTokens[0].stail - common_ctx->argTokens[0].shead;
                       const char* symbol = common_ctx->argTokens[0].stail - 1; // also skip \n
                       char* serialLastSymb = &serial[9];
                       
                       while(numberOfDigits--)
                           *serialLastSymb-- = *symbol--;
                       
                       if(!NFM_ROM_ChangeSerialNumber(serial))
                           common_ctx->status = eProgramData_SpecificError;
                       
                       NFMClass->methods.xMemoryProtection.enable();
                   }
                   
                   if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
                   {
                       // process first argument (switch state)
                       const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Model, 0);
                       if( ScpiNumericSuccess != ne->error ) break;
                       
                       eNFMModel_t deviceModel = (eNFMModel_t)ne->Value.demicalInteger;
                       // disable memory protection if needed
                       if(NFMClass->methods.xMemoryProtection.check())
                           NFMClass->methods.xMemoryProtection.disable();
                       
                       if(!NFM_ROM_ChangeModel(deviceModel))
                           common_ctx->status = eProgramData_SpecificError;
                       
                       NFMClass->methods.xMemoryProtection.enable();
                   }
               }
               
               // check result
               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->SwitchState.state )
               {
                   (void)common_ctx->status; // eProgramDataIllegalArgument
               }
               else
               {
                   size_t error = 0;
                   if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial ||
                      common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
                   {
                     if( common_ctx->status == eProgramData_SpecificError )
                     {
                         error = 1; // Serial Number writing error
                     }
                   }

                   switch( error )
                   {
                       case 1: // Serial Number writing error
                       {
                           (void)common_ctx->status; // eProgramData_SpecificError
                       }
                       break;
                   }

                   if( 0 != error ) break;

                   common_ctx->status = eProgramDataDone;  // request processed, wait for reading...
               }
            }
            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->SwitchState.idx == 0 ) // first reading
             {
                 size_t length = 0;
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceState )
                 {
                     length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", NFMClass->properties.isServiceMode ? "ENABLE\n" : "DISABLE\n");
                 }
                 
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceReboot )
                 {
                       #if CONFIG_REBOOT_FEATURE
                       RebootRequest();
                       #endif
                 }

                 if( length == 0 )
                 {
                     fsq_RaiseError(   SCPI_ERROR_INTERNAL_DEVICE,
                                       SCPI_ERROR_INTERNAL_DEVICE_MSG,
                                       global_ctx->sParser.xHandlerToken.shead,
                                       global_ctx->sParser.xHandlerToken.stail );

                     common_ctx->status = eProgramData_SpecificError; // specific error already generated
                     break;
                 }
                 else
                 {
                     // place null-terminator in the end of line
                     common_ctx->tempBuffer[length] = '\0';
                 }
             }

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

    return nextstate;
}
