memory_table_freq_segm_data.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <stdio.h>
  2. #define SCPI_ARGS_N 1
  3. #define SCPI_ARGS_MANDATORY_N 0
  4. #include "app/scpi/scpi_handler.h"
  5. // -----
  6. // @argTokens, @argTypes
  7. // Declare argument parser entities
  8. // Supported arguments: 1=CHARACTER
  9. DECLARE_SCPI_ARGS( eScpiArg_Character );
  10. // Argument 1 Character Values allowed list / Memory Bank
  11. DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_Bank );
  12. #include "app/scpi/commandHandlers/memory_table_freq_segm_data.h"
  13. #include "app/nfm/nfm_base.h"
  14. // Refer to:
  15. // [1] SCPI Specification, revision 1999.0
  16. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  17. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  18. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  19. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  20. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  21. // =================================================================================
  22. // @fsqvbl_CommandHandlerMemoryTableFrequencySegmentData
  23. // State's virtual table
  24. static void fsqe_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  25. static void fsql_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  26. static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  27. const fFSeqVTable_t fsqvbl_CommandHandlerMEMoryTABLeFREQuencySEGMentDATA =
  28. {
  29. .f = fsqf_CommandHandlerMemoryTableFrequencySegmentData,
  30. .enter = fsqe_CommandHandlerMemoryTableFrequencySegmentData,
  31. .leave = fsql_CommandHandlerMemoryTableFrequencySegmentData
  32. };
  33. static void fsqe_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  34. {
  35. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  36. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  37. common_ctx->MemTableFreqSegmData.idx = 0;
  38. common_ctx->MemTableFreqSegmData.nchs = 0;
  39. common_ctx->MemTableFreqSegmData.bank = 0;
  40. common_ctx->MemTableFreqSegmData.iSeg = 0;
  41. common_ctx->MemTableFreqSegmData.nSeg = 0;
  42. common_ctx->MemTableFreqSegmData.init = false;
  43. common_ctx->MemTableFreqSegmData.isdone = false;
  44. (void)common_ctx->MemTableFreqSegmData.Segment;
  45. }
  46. static void fsql_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  47. {
  48. }
  49. static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableFrequencySegmentData( const struct fFSeqEntry_t * this,
  50. tFSeqCtx_t ctx,
  51. const struct fFSeqEntry_t * * pDeferredNext )
  52. {
  53. const fFSeqEntry_t * nextstate = NULL;
  54. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  55. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  56. switch( common_ctx->event )
  57. {
  58. case eProgramData_Event_Write:
  59. {
  60. if( ! common_ctx->isQuery )
  61. {
  62. common_ctx->status = eProgramDataSyntaxError; // invalid command header type: COMMAND not supported
  63. }
  64. else if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  65. {
  66. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  67. }
  68. else
  69. {
  70. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  71. if( common_ctx->args > 0 )
  72. // process first argument (bank)
  73. common_ctx->MemTableFreqSegmData.bank = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_Bank, 0 );
  74. else
  75. // default parameter
  76. common_ctx->MemTableFreqSegmData.bank = 0; // Factory
  77. // check result
  78. if( SCPI_ARGUMENT_CHARACTER_INVALID_ID != common_ctx->MemTableFreqSegmData.bank )
  79. {
  80. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  81. }
  82. }
  83. }
  84. break;
  85. case eProgramData_Event_Read:
  86. {
  87. do
  88. {
  89. // @idx - current position of the source data to be outputed
  90. if( common_ctx->MemTableFreqSegmData.idx >= common_ctx->MemTableFreqSegmData.nchs )
  91. {
  92. if( common_ctx->MemTableFreqSegmData.isdone ) break;
  93. common_ctx->MemTableFreqSegmData.idx = common_ctx->MemTableFreqSegmData.nchs = 0;
  94. eChrz_t chrz = (eChrz_t)((eChFactory) + common_ctx->MemTableFreqSegmData.bank);
  95. // @PRINT_VALUE_MIN_SPACE
  96. // Response format: <N>, <Start 1>, <Stop 1>, <NOP 1>, <Start 2>, <Stop 2>, <NOP 2>, … <Start N>, <Stop N>, <NOP N>
  97. // where "<N>, " is an initial record, number of segments;
  98. // where "<Start 1>, <Stop 1>, <NOP 1>, " is a frequency record;
  99. // A sum of a couple lengths of the following strings:
  100. // - number of segments, comma and space
  101. // - start frequency, comma, space, stop frequency, comma, space, number of points, comma, space
  102. // One frequency number has 16 symbols length plus 2 (comma and space)
  103. // A number of segments takes 5 characters max plus 2 (comma and space)
  104. // A number of points takes 5 characters max plus 2 (comma and space)
  105. // So, whole record takes 46 characters, and 1 for null-char: let it be 48
  106. // So the temporary buffer must have enough room for store at least one segment record plus the initial record.
  107. #define PRINT_VALUE_MIN_SPACE 48
  108. #if SCPI_MAX_CMD_TEMP_BUFFER < PRINT_VALUE_MIN_SPACE
  109. #error Invalid value 'SCPI_MAX_CMD_TEMP_BUFFER'
  110. #endif
  111. // Have already initialized?
  112. if( ! common_ctx->MemTableFreqSegmData.init )
  113. {
  114. // No, get number of segments:
  115. common_ctx->MemTableFreqSegmData.nSeg = NFMClass->methods.xCharacterization.getScaleSegment( chrz, NULL, 0 );
  116. // Check number of segments:
  117. if( 0 == common_ctx->MemTableFreqSegmData.nSeg ) goto L_FrequencySegment_DATAERR;
  118. // ok. initialized
  119. common_ctx->MemTableFreqSegmData.init = true;
  120. // see @PRINT_VALUE_MIN_SPACE, this call always prints correctly
  121. common_ctx->MemTableFreqSegmData.nchs = _snprintf( common_ctx->tempBuffer,
  122. sizeof(common_ctx->tempBuffer),
  123. "%d%c%c",
  124. common_ctx->MemTableFreqSegmData.nSeg,
  125. ',',' ' );
  126. }
  127. if( common_ctx->MemTableFreqSegmData.iSeg < common_ctx->MemTableFreqSegmData.nSeg )
  128. {
  129. if( 1 != NFMClass->methods.xCharacterization.getScaleSegment( chrz,
  130. &common_ctx->MemTableFreqSegmData.Segment,
  131. common_ctx->MemTableFreqSegmData.iSeg ) )
  132. {
  133. goto L_FrequencySegment_DATAERR; // can not load a segment
  134. }
  135. common_ctx->MemTableFreqSegmData.iSeg++;
  136. common_ctx->MemTableFreqSegmData.isdone = (common_ctx->MemTableFreqSegmData.iSeg >= common_ctx->MemTableFreqSegmData.nSeg);
  137. }
  138. size_t bufsz = sizeof(common_ctx->tempBuffer) - common_ctx->MemTableFreqSegmData.nchs;
  139. common_ctx->MemTableFreqSegmData.nchs += _snprintf( &common_ctx->tempBuffer[ common_ctx->MemTableFreqSegmData.nchs ],
  140. bufsz,
  141. "%1.10e%c%c%1.10e%c%c%d%c%c",
  142. common_ctx->MemTableFreqSegmData.Segment.Fstart, ',',' ',
  143. common_ctx->MemTableFreqSegmData.Segment.Fstop, ',',' ',
  144. common_ctx->MemTableFreqSegmData.Segment.Points, ',',' ' );
  145. if( 0 == common_ctx->MemTableFreqSegmData.nchs
  146. || common_ctx->MemTableFreqSegmData.nchs >= bufsz )
  147. {
  148. goto L_FrequencySegment_INTERR; // invalid length, forbidden state, must be covered by correct value of PRINT_VALUE_MIN_SPACE
  149. }
  150. if( common_ctx->MemTableFreqSegmData.isdone )
  151. {
  152. common_ctx->MemTableFreqSegmData.nchs -= 2;
  153. common_ctx->tempBuffer[ common_ctx->MemTableFreqSegmData.nchs ] = '\0'; // cut off ", " in the end
  154. }
  155. }
  156. // Since @done flag is set, this dispatcher shall not be called anymore.
  157. // Since this handler is implemented as a single-state automat, there no
  158. // ... other states to go to:
  159. (void)nextstate;
  160. // modify current postion index:
  161. SCPI_RESPONSE_HELPER_EXTDONE_IDXINC( common_ctx, common_ctx->MemTableFreqSegmData.idx, common_ctx->MemTableFreqSegmData.isdone );
  162. }
  163. // While output buffer swallows all the prepared data from @tempBuffer
  164. while( (common_ctx->MemTableFreqSegmData.nchs > 0) && (common_ctx->MemTableFreqSegmData.idx >= common_ctx->MemTableFreqSegmData.nchs) );
  165. bool bufferEmpty = (common_ctx->MemTableFreqSegmData.idx >= common_ctx->MemTableFreqSegmData.nchs);
  166. if( bufferEmpty )
  167. {
  168. common_ctx->MemTableFreqSegmData.idx = common_ctx->MemTableFreqSegmData.nchs = 0;
  169. }
  170. SCPI_RESPONSE_HELPER_EXTDONE_SET( common_ctx, bufferEmpty && common_ctx->MemTableFreqSegmData.isdone );
  171. }
  172. break;
  173. }
  174. return nextstate;
  175. L_FrequencySegment_DATAERR:
  176. // Formal call: in case the parameter is optional, the token is unfilled.
  177. // So it is required to fill the token with correct values from allowed list.
  178. SCPI_ARGUMENT_CHARACTER_VALUE_TOKEN( MemTable_AllowedValues_Bank,
  179. common_ctx->MemTableFreqSegmData.bank,
  180. &common_ctx->argTokens[0] );
  181. fsq_RaiseErrorEx( SCPI_ERROR_DATA_CORRUPTED,
  182. SCPI_ERROR_DATA_CORRUPTED_MSG,
  183. common_ctx->argTokens[0].shead,
  184. common_ctx->argTokens[0].stail,
  185. global_ctx->sParser.xHandlerToken.shead,
  186. global_ctx->sParser.xHandlerToken.stail );
  187. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  188. return NULL;
  189. //L_FrequencySegment_PARAMERR:
  190. L_FrequencySegment_INTERR:
  191. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  192. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  193. global_ctx->sParser.xHandlerToken.shead,
  194. global_ctx->sParser.xHandlerToken.stail );
  195. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  196. return NULL;
  197. }