memory_table_freq_data.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <stdio.h>
  2. #define PRINT_VALUE_MIN_SPACE (20) // DO NOT TOUCH! =20, see @printValues
  3. #define SCPI_ARGS_N 1
  4. #define SCPI_ARGS_MANDATORY_N 0
  5. #include "app/scpi/scpi_handler.h"
  6. // -----
  7. // @argTokens, @argTypes
  8. // Declare argument parser entities
  9. // Supported arguments: 1=CHARACTER
  10. DECLARE_SCPI_ARGS( eScpiArg_Character );
  11. // Argument 1 Character Values allowed list / Memory Bank
  12. DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_Bank );
  13. #include "app/scpi/commandHandlers/memory_table_freq_data.h"
  14. #include "app/nfm/nfm_base.h"
  15. // Refer to:
  16. // [1] SCPI Specification, revision 1999.0
  17. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  18. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  19. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  20. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  21. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  22. // =================================================================================
  23. // @fsqvbl_CommandHandlerMemoryTableFrequencyData
  24. // State's virtual table
  25. static size_t printValues( char * pcBuffer, size_t szBuffer, const double * pValues, size_t nValues, bool theLastOne );
  26. static void fsqe_CommandHandlerMemoryTableFrequencyData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  27. static void fsql_CommandHandlerMemoryTableFrequencyData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  28. static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableFrequencyData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  29. const fFSeqVTable_t fsqvbl_CommandHandlerMEMoryTABLeFREQuencyDATA =
  30. {
  31. .f = fsqf_CommandHandlerMemoryTableFrequencyData,
  32. .enter = fsqe_CommandHandlerMemoryTableFrequencyData,
  33. .leave = fsql_CommandHandlerMemoryTableFrequencyData
  34. };
  35. static void fsqe_CommandHandlerMemoryTableFrequencyData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  36. {
  37. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  38. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  39. common_ctx->MemTableFreqData.idx = 0;
  40. common_ctx->MemTableFreqData.nchs = 0;
  41. common_ctx->MemTableFreqData.bank = 0;
  42. common_ctx->MemTableFreqData.init = false;
  43. common_ctx->MemTableFreqData.isdone = false;
  44. memset( &common_ctx->MemTableFreqData.getctx, 0, sizeof(common_ctx->MemTableFreqData.getctx) );
  45. }
  46. static void fsql_CommandHandlerMemoryTableFrequencyData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  47. {
  48. }
  49. static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableFrequencyData( 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->MemTableFreqData.bank = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_Bank, 0 );
  74. else
  75. // default parameter
  76. common_ctx->MemTableFreqData.bank = 0; // Factory
  77. // check result
  78. if( SCPI_ARGUMENT_CHARACTER_INVALID_ID != common_ctx->MemTableFreqData.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->MemTableFreqData.idx >= common_ctx->MemTableFreqData.nchs )
  91. {
  92. if( common_ctx->MemTableFreqData.isdone ) break;
  93. // There no already prepared data in @tempBuffer
  94. if( ! common_ctx->MemTableFreqData.init ) // first reading
  95. {
  96. eChrz_t chrz = (eChrz_t)((eChFactory) + common_ctx->MemTableFreqData.bank);
  97. common_ctx->status = eProgramDataIllegalArgument; // forward set
  98. switch( NFMClass->methods.xCharacterization.getScaleFreqs_Init( chrz,
  99. common_ctx->MemTableFreqData.dFreqPoints,
  100. cellsof(common_ctx->MemTableFreqData.dFreqPoints),
  101. &common_ctx->MemTableFreqData.getctx ) )
  102. {
  103. case eNFMGetPointError_Success:
  104. common_ctx->MemTableFreqData.init = true;
  105. break;
  106. case eNFMGetPointError_DataError: goto L_FrequencySegment_DATAERR;
  107. case eNFMGetPointError_InvalidValue:
  108. default: goto L_FrequencySegment_PARAMERR;
  109. }
  110. }
  111. #if SCPI_MAX_CMD_TEMP_BUFFER < PRINT_VALUE_MIN_SPACE
  112. #error Invalid value 'SCPI_MAX_CMD_TEMP_BUFFER'
  113. #endif
  114. // The length of temporary string buffer, length of one printable number and number of cells in @MemTableFreqData.dFreqPoints
  115. // are interconnected. Number of cells @MemTableFreqData.dFreqPoints should be equal or be greater than maximum number of
  116. // printed numbers into the @tempBuffer. But it is recommend that these numbers be equal.
  117. // Equal (no unused cells in @dFreqPoints):
  118. STATIC_ASSERT( (sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE) == cellsof(common_ctx->MemTableFreqData.dFreqPoints), "Invalid size" );
  119. // Greater (there is unused cells in @dFreqPoints):
  120. //STATIC_ASSERT( (sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE) <= cellsof(common_ctx->MemTableFreqData.dFreqPoints), "Invalid size" );
  121. size_t nPointsRetrieve = sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE;
  122. switch( NFMClass->methods.xCharacterization.getScaleFreqs_Next( &common_ctx->MemTableFreqData.getctx,
  123. &nPointsRetrieve ) )
  124. {
  125. case eNFMGetPointError_Success: common_ctx->MemTableFreqData.isdone = true;
  126. case eNFMGetPointError_OutOfBuffer:
  127. case eNFMGetPointError_Limit:
  128. {
  129. size_t l = 0;
  130. if( nPointsRetrieve > 0 )
  131. {
  132. l = printValues( common_ctx->tempBuffer, sizeof( common_ctx->tempBuffer ),
  133. common_ctx->MemTableFreqData.dFreqPoints, nPointsRetrieve, common_ctx->MemTableFreqData.isdone );
  134. }
  135. common_ctx->tempBuffer[ l ] = '\0';
  136. common_ctx->MemTableFreqData.idx = 0; // reset prepared characters index
  137. common_ctx->MemTableFreqData.nchs = l; // prepared characters in buffer
  138. }
  139. break;
  140. case eNFMGetPointError_DataError: goto L_FrequencySegment_DATAERR;
  141. case eNFMGetPointError_InvalidValue:
  142. default: goto L_FrequencySegment_PARAMERR;
  143. }
  144. }
  145. // Since @done flag is set, this dispatcher shall not be called anymore.
  146. // Since this handler is implemented as a single-state automat, there no
  147. // ... other states to go to:
  148. (void)nextstate;
  149. // modify current postion index:
  150. SCPI_RESPONSE_HELPER_EXTDONE_IDXINC( common_ctx, common_ctx->MemTableFreqData.idx, common_ctx->MemTableFreqData.isdone );
  151. }
  152. // While output buffer swallows all the prepared data from @tempBuffer
  153. while( (common_ctx->MemTableFreqData.idx >= common_ctx->MemTableFreqData.nchs) );
  154. bool bufferEmpty = (common_ctx->MemTableFreqData.idx >= common_ctx->MemTableFreqData.nchs);
  155. SCPI_RESPONSE_HELPER_EXTDONE_SET( common_ctx, bufferEmpty && common_ctx->MemTableFreqData.isdone );
  156. }
  157. break;
  158. }
  159. return nextstate;
  160. L_FrequencySegment_DATAERR:
  161. // Formal call: in case the parameter is optional, the token is unfilled.
  162. // So it is required to fill the token with correct values from allowed list.
  163. SCPI_ARGUMENT_CHARACTER_VALUE_TOKEN( MemTable_AllowedValues_Bank,
  164. common_ctx->MemTableFreqData.bank,
  165. &common_ctx->argTokens[0] );
  166. fsq_RaiseErrorEx( SCPI_ERROR_DATA_CORRUPTED,
  167. SCPI_ERROR_DATA_CORRUPTED_MSG,
  168. common_ctx->argTokens[0].shead,
  169. common_ctx->argTokens[0].stail,
  170. global_ctx->sParser.xHandlerToken.shead,
  171. global_ctx->sParser.xHandlerToken.stail );
  172. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  173. return NULL;
  174. L_FrequencySegment_PARAMERR:
  175. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  176. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  177. global_ctx->sParser.xHandlerToken.shead,
  178. global_ctx->sParser.xHandlerToken.stail );
  179. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  180. return NULL;
  181. }
  182. // @printValues
  183. // Prints an array of double numbers into the buffer.
  184. // If there no free space in the output buffer while not all the numbers are printed, function returns 0;
  185. // @pcBuffer - output buffer;
  186. // @szBuffer - size of output buffer;
  187. // @pValues - an array of numbers to print;
  188. // @nValues - amount of numbers to print;
  189. // @theLastOne - the last call indicator
  190. // Returns:
  191. // - In case all of numbers are printed: number of bytes printed into the output buffer;
  192. // - 0 otherwise
  193. static size_t printValues( char * pcBuffer, size_t szBuffer, const double * pValues, size_t nValues, bool theLastOne )
  194. {
  195. // One point has the following format:
  196. // <Freq_i>,<SPACE>
  197. // One number has 16 symbols length plus 2 (comma and space)
  198. // So, 18 characters required to print one point.
  199. // To prevent overflowing by _snprintf() with printing null-term,
  200. // it is required at lease 20 characters free, see @PRINT_VALUE_MIN_SPACE
  201. size_t idx = 0;
  202. while( nValues > 0 )
  203. {
  204. size_t l = 0;
  205. if( (szBuffer-idx) >= PRINT_VALUE_MIN_SPACE )
  206. {
  207. if( theLastOne && (nValues==1) )
  208. l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%1.10e", (*pValues++) );
  209. else
  210. l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%1.10e%c%c", (*pValues++), ',' , ' ' );
  211. }
  212. if( l == 0 || l >= (szBuffer-idx) )
  213. {
  214. return 0; // insuffecient buffer
  215. }
  216. idx += l;
  217. nValues--;
  218. }
  219. return idx;
  220. }