sre.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #error Deprecated. 'ese_sre' is used instead
  2. #include <stdio.h>
  3. const int fsqvbl_CommandHandlerSRE_context = 3;
  4. #define SCPI_ARGS_N_C 1
  5. #define SCPI_ARGS_N_Q 0
  6. #include "app/scpi/scpi_handler.h"
  7. // -----
  8. // @argTypesCommand
  9. // Declare argument parser entities for command form
  10. // Supported arguments: 1=NUMERIC
  11. DECLARE_SCPI_ARGS_C( eScpiArg_Numeric );
  12. // -----
  13. // @argTypesQuery
  14. // Declare argument parser entities for query form
  15. // Supported arguments: 0
  16. DECLARE_SCPI_ARGS_Q();
  17. DECLARE_ARGUMENT_NUMERIC_VALUES_I8(AllowedValues_Argument1, 0, 255);
  18. #include "app/scpi/commandHandlers/sre.h"
  19. #include "app/acm/acm_base.h"
  20. // Refer to:
  21. // [1] SCPI Specification, revision 1999.0
  22. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  23. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  24. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  25. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  26. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  27. // =================================================================================
  28. // @fsqvbl_CommandHandlerSRE
  29. // State's virtual table
  30. static void fsqe_CommandHandlerSRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  31. static void fsql_CommandHandlerSRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  32. static const struct fFSeqEntry_t * fsqf_CommandHandlerSRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  33. const fFSeqVTable_t fsqvbl_CommandHandlerSRE =
  34. {
  35. .f = fsqf_CommandHandlerSRE,
  36. .enter = fsqe_CommandHandlerSRE,
  37. .leave = fsql_CommandHandlerSRE
  38. };
  39. static void fsqe_CommandHandlerSRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  40. {
  41. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  42. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  43. common_ctx->SRE.idx = 0; // rsret position
  44. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  45. (void)global_ctx;
  46. }
  47. static void fsql_CommandHandlerSRE( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  48. {
  49. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  50. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  51. (void)common_ctx;
  52. (void)global_ctx;
  53. }
  54. static const struct fFSeqEntry_t * fsqf_CommandHandlerSRE( const struct fFSeqEntry_t * this,
  55. tFSeqCtx_t ctx,
  56. const struct fFSeqEntry_t * * pDeferredNext )
  57. {
  58. const fFSeqEntry_t * nextstate = NULL;
  59. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  60. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  61. (void)common_ctx;
  62. (void)global_ctx;
  63. switch( common_ctx->event )
  64. {
  65. case eProgramData_Event_Write:
  66. {
  67. if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  68. {
  69. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  70. }
  71. else
  72. if( ! common_ctx->isQuery )
  73. {
  74. common_ctx->status = eProgramDataSyntaxError; // forward set
  75. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Argument1, 0);
  76. if( NULL != ne )
  77. switch( ne->error )
  78. {
  79. case ScpiNumericSuccess:
  80. {
  81. GPIBMachine.fGPIB_set_service_request_enable_register( &global_ctx->sGPIB.registers,
  82. (uint8_t)ne->Value.i );
  83. common_ctx->status = eProgramDataDone;
  84. }
  85. break;
  86. // -------- processed by SCPI_PROCESS_ARGUMENT_NUMERIC ----
  87. // "11.5.1.1.5", [3]
  88. // "10.10.6 Error Handling", [3]
  89. // "<...> An out-of-range integer shall cause an Execution Error, see 11.5.1.1.5. <...>"
  90. case ScpiNumericError_DEVICE_RANGE: // the value is out of range supported by device
  91. case ScpiNumericError_USER_RANGE: // the value is out of user specified range: // the value is out of range supported by device
  92. case ScpiNumericError_USER_TYPE: // the value does not match to the user expectation
  93. case ScpiNumericInvalidArg: // forbidden case: design bug
  94. case ScpiNumericError: // generic numeric conversion error
  95. break;
  96. // --------------------------------------------------------
  97. }
  98. }
  99. else
  100. {
  101. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  102. (void)nextstate; // stay in this state
  103. }
  104. }
  105. break;
  106. case eProgramData_Event_Read:
  107. {
  108. // Take the input buffer by @pBuffer and @nBufferLength
  109. // @dst = @pBuffer, output response buffer
  110. // @bsize = @nBufferLength, output buffer size
  111. uint8_t * dst = (uint8_t*)global_ctx->sRead.pBufferIdx;
  112. size_t bsize = global_ctx->sRead.nBufferSize;
  113. // @idx - current position of the source data to be outputed
  114. size_t idx = common_ctx->SRE.idx;
  115. if( 0 == idx )
  116. {
  117. memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );
  118. uint8_t SRE = 0;
  119. // Read ESB:
  120. // "11.5.1.2 Standard Event Status Register Operation", [3]
  121. // "<...> The Standard Event Status Register is destructively read (that is, read and cleared) <...>", [3]
  122. // retrieve ESB and clear it
  123. GPIBMachine.fGPIB_get_service_request_enable_register( &global_ctx->sGPIB.registers, &SRE );
  124. int iSRE = (int)SRE;
  125. // first call: prepare buffer
  126. _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d", iSRE );
  127. }
  128. // @SRE_str - response string
  129. const char * SRE_str = common_ctx->tempBuffer;
  130. // Copy response string char by char to the output buffer
  131. // ... until the null-character is reached or the output
  132. // ... buffer free space is ran out:
  133. while( ('\0' != SRE_str[idx]) && ( 0<bsize ) )
  134. {
  135. *dst++ = SRE_str[idx++]; bsize--;
  136. }
  137. // Set the @nDataLength to indicate the copied part size:
  138. // @bsize after copying reprsrent the free space left in bytes;
  139. // @nBufferLength after copying reprsrent the free space in bytes before copying;
  140. // Thus (@nBufferSize-@bsize) is the number of bytes has been copyied:
  141. global_ctx->sRead.nDataLength += global_ctx->sRead.nBufferSize - bsize;
  142. // Check for end-condition:
  143. if( '\0' == SRE_str[idx] )
  144. {
  145. common_ctx->status = eProgramDataDone;
  146. }
  147. // Since @done flag is set, this dispatcher shall not be called anymore.
  148. // Since this handler is implemented as a single-state automat, there no
  149. // ... other states to go to:
  150. (void)nextstate;
  151. // modify current postion index:
  152. common_ctx->SRE.idx = idx;
  153. }
  154. break;
  155. }
  156. return nextstate;
  157. }