scpi_table.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <stdio.h>
  2. #define PRINT_VALUE_MIN_SPACE 12
  3. #define SCPI_ARGS_N 1
  4. #define SCPI_ARGS_MANDATORY_N 1
  5. #include "app/scpi/scpi_handler.h"
  6. const uint8_t fsqvbl_CommandHandlerTableMatrixAddPoints = 1; // TABle:ADD
  7. const uint8_t fsqvbl_CommandHandlerTableMatrixClear = 2; // TABle:CLR
  8. const uint8_t fsqvbl_CommandHandlerTableMatrixDone = 3; // TABle:DONE
  9. const uint8_t fsqvbl_CommandHandlerTableMatrixEdit = 4; // TABle:EDIT
  10. const uint8_t fsqvbl_CommandHandlerTableMatrixGet = 5; // TABle:GET
  11. // -----
  12. // @argTokens, @argTypes
  13. // Declare argument parser entities
  14. // Supported arguments: ARRAY_ARG=ARRAY
  15. DECLARE_SCPI_ARGS( eScpiArg_Array_Decimal );
  16. // Arguments Decimal Values allowed
  17. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_TableNumbers, 0, 9999);
  18. #include "app/scpi/commandHandlers/scpi_table.h"
  19. #include "app/control_table/control_table.h"
  20. #include "app/nfm/nfm_base.h"
  21. // Refer to:
  22. // [1] SCPI Specification, revision 1999.0
  23. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  24. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  25. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  26. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  27. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  28. // =================================================================================
  29. // @fsqvbl_CommandHandlerMEASnSWITCH_group
  30. // State's virtual table
  31. static size_t printValues( char * pcBuffer, size_t szBuffer, const sSWTablePoint_t * pValues, size_t nValues, bool theLastOne );
  32. static void fsqe_CommandHandlerTable_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  33. static void fsql_CommandHandlerTable_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  34. static const struct fFSeqEntry_t * fsqf_CommandHandlerTable_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  35. const fFSeqVTable_t fsqvbl_CommandHandlerTable_group =
  36. {
  37. .f = fsqf_CommandHandlerTable_Group,
  38. .enter = fsqe_CommandHandlerTable_Group,
  39. .leave = fsql_CommandHandlerTable_Group
  40. };
  41. static void fsqe_CommandHandlerTable_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  42. {
  43. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  44. common_ctx->MeasAndSwitch.idx = 0;
  45. SCPI_PARSE_ARRAY_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  46. common_ctx->MemTablePlanData.idx = 0;
  47. common_ctx->MemTablePlanData.nchs = 0;
  48. common_ctx->MemTablePlanData.bank = 0;
  49. common_ctx->MemTablePlanData.init = false;
  50. common_ctx->MemTablePlanData.isdone = false;
  51. memset( &common_ctx->MemTablePlanData.getctx, 0, sizeof(common_ctx->MemTablePlanData.getctx) );
  52. }
  53. static void fsql_CommandHandlerTable_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  54. {
  55. }
  56. static const struct fFSeqEntry_t * fsqf_CommandHandlerTable_Group( const struct fFSeqEntry_t * this,
  57. tFSeqCtx_t ctx,
  58. const struct fFSeqEntry_t * * pDeferredNext )
  59. {
  60. const fFSeqEntry_t * nextstate = NULL;
  61. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  62. sScpiParserContext_t * global_ctx = common_ctx->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 if( ! common_ctx->isQuery )
  72. {
  73. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  74. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixAddPoints )
  75. {
  76. if(!TableHandle.AddPoints(common_ctx->argArray, common_ctx->args))
  77. {
  78. common_ctx->status = eProgramDataSyntaxError;
  79. break;
  80. }
  81. common_ctx->status = eProgramDataDone;
  82. break;
  83. }
  84. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixClear )
  85. {
  86. if(TableHandle.ClearTable())
  87. {
  88. common_ctx->status = eProgramDataDone;
  89. break;
  90. }
  91. }
  92. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixDone )
  93. {
  94. TableHandle.SetEditState(false); // set table done
  95. common_ctx->status = eProgramDataDone;
  96. break;
  97. }
  98. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixEdit )
  99. {
  100. TableHandle.SetEditState(true); // set table undone
  101. common_ctx->status = eProgramDataDone;
  102. break;
  103. }
  104. }
  105. else
  106. {
  107. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  108. }
  109. }
  110. break;
  111. case eProgramData_Event_Read:
  112. {
  113. // @idx - current position of the source data to be outputed
  114. if( common_ctx->MeasAndSwitch.idx == 0 ) // first reading
  115. {
  116. size_t length = 0;
  117. //common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_ARRAY(common_ctx, &AllowedValues_TableNumbers, 0);
  118. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixDone )
  119. {
  120. const char * temp = !TableHandle.GetEditState() ? "YES\n" : "NO\n"; // get table state done?
  121. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  122. }
  123. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerTableMatrixGet )
  124. {
  125. do
  126. {
  127. // @idx - current position of the source data to be outputed
  128. if( common_ctx->MemTablePlanData.idx >= common_ctx->MemTablePlanData.nchs )
  129. {
  130. if( common_ctx->MemTablePlanData.isdone ) break;
  131. // There no already prepared data in @tempBuffer
  132. if( ! common_ctx->MemTablePlanData.init ) // first reading
  133. {
  134. eChunks_t chrz = (eChunks_t)((eChValues) + common_ctx->MemTablePlanData.bank);
  135. common_ctx->status = eProgramDataIllegalArgument; // forward set
  136. switch( NFMClass->methods.xTable.getTablePoints_Init( chrz,
  137. common_ctx->MemTablePlanData.dTablePoints,
  138. cellsof(common_ctx->MemTablePlanData.dTablePoints),
  139. &common_ctx->MemTablePlanData.getctx ) )
  140. {
  141. case eNFMGetPointError_Success:
  142. common_ctx->MemTablePlanData.init = true;
  143. break;
  144. case eNFMGetPointError_DataError:
  145. goto L_TableSegment_DATAERR;
  146. case eNFMGetPointError_InvalidValue:
  147. default:
  148. goto L_TableSegment_PARAMERR;
  149. }
  150. }
  151. #if SCPI_MAX_CMD_TEMP_BUFFER < PRINT_VALUE_MIN_SPACE
  152. #error Invalid value 'SCPI_MAX_CMD_TEMP_BUFFER'
  153. #endif
  154. // The length of temporary string buffer, length of one printable number and number of cells in @MemTableFreqData.dTablePoints
  155. // are interconnected. Number of cells @MemTablePlanData.dTablePoints should be equal or be greater than maximum number of
  156. // printed numbers into the @tempBuffer. But it is recommend that these numbers be equal.
  157. // Equal (no unused cells in @dTablePoints):
  158. //STATIC_ASSERT( (sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE) == cellsof(common_ctx->MemTablePlanData.dTablePoints), "Invalid size" );
  159. // Greater (there is unused cells in @dTablePoints):
  160. STATIC_ASSERT( (sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE) <= cellsof(common_ctx->MemTablePlanData.dTablePoints), "Invalid size" );
  161. size_t nPointsRetrieve = sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE;
  162. switch( NFMClass->methods.xTable.getTablePoints_Next( &common_ctx->MemTablePlanData.getctx,
  163. &nPointsRetrieve) )
  164. {
  165. case eNFMGetPointError_Success: common_ctx->MemTablePlanData.isdone = true;
  166. case eNFMGetPointError_OutOfBuffer:
  167. case eNFMGetPointError_Limit:
  168. {
  169. size_t l = 0;
  170. if( nPointsRetrieve > 0 )
  171. {
  172. l = printValues( common_ctx->tempBuffer, sizeof( common_ctx->tempBuffer ),
  173. common_ctx->MemTablePlanData.dTablePoints, nPointsRetrieve, common_ctx->MemTablePlanData.isdone );
  174. }
  175. common_ctx->tempBuffer[ l ] = '\0';
  176. common_ctx->MemTablePlanData.idx = 0; // reset prepared characters index
  177. common_ctx->MemTablePlanData.nchs = l; // prepared characters in buffer
  178. }
  179. break;
  180. case eNFMGetPointError_DataError: goto L_TableSegment_DATAERR;
  181. case eNFMGetPointError_InvalidValue:
  182. default: goto L_TableSegment_PARAMERR;
  183. }
  184. }
  185. // Since @done flag is set, this dispatcher shall not be called anymore.
  186. // Since this handler is implemented as a single-state automat, there no
  187. // ... other states to go to:
  188. (void)nextstate;
  189. // modify current postion index:
  190. SCPI_RESPONSE_HELPER_EXTDONE_IDXINC( common_ctx, common_ctx->MemTablePlanData.idx, common_ctx->MemTablePlanData.isdone );
  191. }
  192. // While output buffer swallows all the prepared data from @tempBuffer
  193. while( (common_ctx->MemTablePlanData.idx >= common_ctx->MemTablePlanData.nchs) );
  194. bool bufferEmpty = (common_ctx->MemTablePlanData.idx >= common_ctx->MemTablePlanData.nchs);
  195. SCPI_RESPONSE_HELPER_EXTDONE_SET( common_ctx, bufferEmpty && common_ctx->MemTablePlanData.isdone );
  196. break;
  197. }
  198. if( length > 0 )
  199. {
  200. // place null-terminator in the end of line
  201. common_ctx->tempBuffer[length] = '\0';
  202. }
  203. else
  204. {
  205. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  206. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  207. global_ctx->sParser.xHandlerToken.shead,
  208. global_ctx->sParser.xHandlerToken.stail );
  209. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  210. break;
  211. }
  212. }
  213. // Since @done flag is set, this dispatcher shall not be called anymore.
  214. // Since this handler is implemented as a single-state automat, there no
  215. // ... other states to go to:
  216. (void)nextstate;
  217. // modify current postion index:
  218. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->MeasAndSwitch.idx );
  219. }
  220. break;
  221. }
  222. return nextstate;
  223. L_TableSegment_DATAERR:
  224. // Formal call: in case the parameter is optional, the token is unfilled.
  225. // So it is required to fill the token with correct values from allowed list.
  226. // SCPI_ARGUMENT_CHARACTER_VALUE_TOKEN( MemTable_AllowedValues_Bank,
  227. // common_ctx->MemTablePlanData.bank,
  228. // &common_ctx->argTokens[0] );
  229. fsq_RaiseErrorEx( SCPI_ERROR_DATA_CORRUPTED,
  230. SCPI_ERROR_DATA_CORRUPTED_MSG,
  231. common_ctx->argTokens[0].shead,
  232. common_ctx->argTokens[0].stail,
  233. global_ctx->sParser.xHandlerToken.shead,
  234. global_ctx->sParser.xHandlerToken.stail );
  235. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  236. return NULL;
  237. L_TableSegment_PARAMERR:
  238. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  239. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  240. global_ctx->sParser.xHandlerToken.shead,
  241. global_ctx->sParser.xHandlerToken.stail );
  242. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  243. return NULL;
  244. }
  245. // @printValues
  246. // Prints an array of double numbers into the buffer.
  247. // If there no free space in the output buffer while not all the numbers are printed, function returns 0;
  248. // @pcBuffer - output buffer;
  249. // @szBuffer - size of output buffer;
  250. // @pValues - an array of numbers to print;
  251. // @nValues - amount of numbers to print;
  252. // @theLastOne - the last call indicator
  253. // Returns:
  254. // - In case all of numbers are printed: number of bytes printed into the output buffer;
  255. // - 0 otherwise
  256. static size_t printValues( char * pcBuffer, size_t szBuffer, const sSWTablePoint_t * pValues, size_t nValues, bool theLastOne )
  257. {
  258. // One point has the following format:
  259. // <Freq_i>,<SPACE>
  260. // One number has 16 symbols length plus 2 (comma and space)
  261. // So, 18 characters required to print one point.
  262. // To prevent overflowing by _snprintf() with printing null-term,
  263. // it is required at lease 20 characters free, see @PRINT_VALUE_MIN_SPACE
  264. size_t idx = 0;
  265. while( nValues > 0 )
  266. {
  267. size_t l = 0;
  268. if( (szBuffer-idx) >= PRINT_VALUE_MIN_SPACE )
  269. {
  270. if( theLastOne && (nValues==1) )
  271. l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%d-%d", pValues->port1, pValues->port2 );
  272. else
  273. l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%d-%d%c%c", pValues->port1, pValues->port2, ',' , ' ' );
  274. pValues++;
  275. }
  276. if( l == 0 || l >= (szBuffer-idx) )
  277. {
  278. return 0; // insuffecient buffer
  279. }
  280. idx += l;
  281. nValues--;
  282. }
  283. return idx;
  284. }