scpi_service.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <stdio.h>
  2. #define SCPI_ARGS_N_Q 0
  3. #define SCPI_ARGS_N_C 1
  4. #include "app/scpi/scpi_handler.h"
  5. const uint8_t fsqvbl_CommandHandlerServiceState = 1; // SERVICE:STATE
  6. const uint8_t fsqvbl_CommandHandlerSwitchSerial = 2; // SERVICE:SERIAL
  7. const uint8_t fsqvbl_CommandHandlerSwitchModel = 3; // SERVICE:MODEL
  8. const uint8_t fsqvbl_CommandHandlerServiceReboot = 4; // SERVICE:REBOOT
  9. // -----
  10. // @argTokens, @argTypes
  11. // Declare argument parser entities
  12. // Supported arguments: 1=CHARACTER
  13. DECLARE_SCPI_ARGS_C( eScpiArg_Numeric );
  14. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_ServiceState, 0, 9999999999);
  15. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_Serial, 0, 99999999);
  16. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_Model, 0, 255);
  17. #include "app/scpi/commandHandlers/scpi_service.h"
  18. #include "app/nfm/nfm_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_CommandHandlerSwitchState
  28. // State's virtual table
  29. static void fsqe_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  30. static void fsql_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  31. static const struct fFSeqEntry_t * fsqf_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  32. const fFSeqVTable_t fsqvbl_CommandHandlerSERVice_group =
  33. {
  34. .f = fsqf_CommandHandlerService,
  35. .enter = fsqe_CommandHandlerService,
  36. .leave = fsql_CommandHandlerService
  37. };
  38. static void fsqe_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  39. {
  40. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  41. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  42. common_ctx->SwitchState.idx = 0;
  43. common_ctx->SwitchState.state = 0;
  44. }
  45. static void fsql_CommandHandlerService( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  46. {
  47. }
  48. static const struct fFSeqEntry_t * fsqf_CommandHandlerService( const struct fFSeqEntry_t * this,
  49. tFSeqCtx_t ctx,
  50. const struct fFSeqEntry_t * * pDeferredNext )
  51. {
  52. const fFSeqEntry_t * nextstate = NULL;
  53. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  54. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  55. switch( common_ctx->event )
  56. {
  57. case eProgramData_Event_Write:
  58. {
  59. if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  60. {
  61. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  62. }
  63. else if( ! common_ctx->isQuery )
  64. {
  65. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  66. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceState )
  67. {
  68. // process first argument (switch state)
  69. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_ServiceState, 0);
  70. if( ScpiNumericSuccess != ne->error ) break;
  71. if (ne->Value.demicalInteger == 65534 || ne->Value.demicalInteger == 0)
  72. {
  73. NFMClass->properties.isServiceMode = ne->Value.demicalInteger;
  74. }
  75. }
  76. if(NFMClass->properties.isServiceMode)
  77. {
  78. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial )
  79. {
  80. // process first argument (switch state)
  81. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Serial, 0);
  82. if( ScpiNumericSuccess != ne->error ) break;
  83. // disable memory protection if needed
  84. if(NFMClass->methods.xMemoryProtection.check())
  85. NFMClass->methods.xMemoryProtection.disable();
  86. char serial[8];
  87. memset(&serial, 0x30, sizeof(serial));
  88. int numberOfDigits = common_ctx->argTokens[0].stail - common_ctx->argTokens[0].shead;
  89. const char* symbol = common_ctx->argTokens[0].stail - 1; // also skip \n
  90. char* serialLastSymb = &serial[7];
  91. while(numberOfDigits--)
  92. *serialLastSymb-- = *symbol--;
  93. if(!NFM_ROM_ChangeSerialNumber(serial))
  94. common_ctx->status = eProgramData_SpecificError;
  95. NFMClass->methods.xMemoryProtection.enable();
  96. }
  97. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
  98. {
  99. // process first argument (switch state)
  100. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Model, 0);
  101. if( ScpiNumericSuccess != ne->error ) break;
  102. eNFMModel_t deviceModel = (eNFMModel_t)ne->Value.demicalInteger;
  103. // disable memory protection if needed
  104. if(NFMClass->methods.xMemoryProtection.check())
  105. NFMClass->methods.xMemoryProtection.disable();
  106. if(!NFM_ROM_ChangeModel(deviceModel))
  107. common_ctx->status = eProgramData_SpecificError;
  108. NFMClass->methods.xMemoryProtection.enable();
  109. }
  110. }
  111. // check result
  112. if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->SwitchState.state )
  113. {
  114. (void)common_ctx->status; // eProgramDataIllegalArgument
  115. }
  116. else
  117. {
  118. size_t error = 0;
  119. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial ||
  120. common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
  121. {
  122. if( common_ctx->status == eProgramData_SpecificError )
  123. {
  124. error = 1; // Serial Number writing error
  125. }
  126. }
  127. switch( error )
  128. {
  129. case 1: // Serial Number writing error
  130. {
  131. (void)common_ctx->status; // eProgramData_SpecificError
  132. }
  133. break;
  134. }
  135. if( 0 != error ) break;
  136. common_ctx->status = eProgramDataDone; // request processed, wait for reading...
  137. }
  138. }
  139. else
  140. {
  141. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  142. }
  143. }
  144. break;
  145. case eProgramData_Event_Read:
  146. {
  147. // @idx - current position of the source data to be outputed
  148. if( common_ctx->SwitchState.idx == 0 ) // first reading
  149. {
  150. size_t length = 0;
  151. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceState )
  152. {
  153. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", NFMClass->properties.isServiceMode ? "ENABLE\n" : "DISABLE\n");
  154. }
  155. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceReboot )
  156. {
  157. #if CONFIG_REBOOT_FEATURE
  158. RebootRequest();
  159. #endif
  160. }
  161. if( length == 0 )
  162. {
  163. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  164. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  165. global_ctx->sParser.xHandlerToken.shead,
  166. global_ctx->sParser.xHandlerToken.stail );
  167. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  168. break;
  169. }
  170. else
  171. {
  172. // place null-terminator in the end of line
  173. common_ctx->tempBuffer[length] = '\0';
  174. }
  175. }
  176. // Since @done flag is set, this dispatcher shall not be called anymore.
  177. // Since this handler is implemented as a single-state automat, there no
  178. // ... other states to go to:
  179. (void)nextstate;
  180. // modify current postion index:
  181. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->SwitchState.idx );
  182. }
  183. break;
  184. }
  185. return nextstate;
  186. }