ese.c 7.7 KB

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