scpi_service.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, 1);
  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. NFMClass->properties.isServiceMode = ne->Value.demicalInteger;
  72. }
  73. if(NFMClass->properties.isServiceMode)
  74. {
  75. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial )
  76. {
  77. // process first argument (switch state)
  78. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Serial, 0);
  79. if( ScpiNumericSuccess != ne->error ) break;
  80. // disable memory protection if needed
  81. if(NFMClass->methods.xMemoryProtection.check())
  82. NFMClass->methods.xMemoryProtection.disable();
  83. char serial[10];
  84. memset(&serial, 0x30, sizeof(serial));
  85. int numberOfDigits = common_ctx->argTokens[0].stail - common_ctx->argTokens[0].shead;
  86. const char* symbol = common_ctx->argTokens[0].stail - 1; // also skip \n
  87. char* serialLastSymb = &serial[9];
  88. while(numberOfDigits--)
  89. *serialLastSymb-- = *symbol--;
  90. if(!NFM_ROM_ChangeSerialNumber(serial))
  91. common_ctx->status = eProgramData_SpecificError;
  92. NFMClass->methods.xMemoryProtection.enable();
  93. }
  94. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
  95. {
  96. // process first argument (switch state)
  97. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_Model, 0);
  98. if( ScpiNumericSuccess != ne->error ) break;
  99. eNFMModel_t deviceModel = (eNFMModel_t)ne->Value.demicalInteger;
  100. // disable memory protection if needed
  101. if(NFMClass->methods.xMemoryProtection.check())
  102. NFMClass->methods.xMemoryProtection.disable();
  103. if(!NFM_ROM_ChangeModel(deviceModel))
  104. common_ctx->status = eProgramData_SpecificError;
  105. NFMClass->methods.xMemoryProtection.enable();
  106. }
  107. }
  108. // check result
  109. if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->SwitchState.state )
  110. {
  111. (void)common_ctx->status; // eProgramDataIllegalArgument
  112. }
  113. else
  114. {
  115. size_t error = 0;
  116. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchSerial ||
  117. common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwitchModel )
  118. {
  119. if( common_ctx->status == eProgramData_SpecificError )
  120. {
  121. error = 1; // Serial Number writing error
  122. }
  123. }
  124. switch( error )
  125. {
  126. case 1: // Serial Number writing error
  127. {
  128. (void)common_ctx->status; // eProgramData_SpecificError
  129. }
  130. break;
  131. }
  132. if( 0 != error ) break;
  133. common_ctx->status = eProgramDataDone; // request processed, wait for reading...
  134. }
  135. }
  136. else
  137. {
  138. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  139. }
  140. }
  141. break;
  142. case eProgramData_Event_Read:
  143. {
  144. // @idx - current position of the source data to be outputed
  145. if( common_ctx->SwitchState.idx == 0 ) // first reading
  146. {
  147. size_t length = 0;
  148. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceState )
  149. {
  150. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", NFMClass->properties.isServiceMode ? "ENABLE\n" : "DISABLE\n");
  151. }
  152. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerServiceReboot )
  153. {
  154. #if CONFIG_REBOOT_FEATURE
  155. RebootRequest();
  156. #endif
  157. }
  158. if( length == 0 )
  159. {
  160. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  161. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  162. global_ctx->sParser.xHandlerToken.shead,
  163. global_ctx->sParser.xHandlerToken.stail );
  164. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  165. break;
  166. }
  167. else
  168. {
  169. // place null-terminator in the end of line
  170. common_ctx->tempBuffer[length] = '\0';
  171. }
  172. }
  173. // Since @done flag is set, this dispatcher shall not be called anymore.
  174. // Since this handler is implemented as a single-state automat, there no
  175. // ... other states to go to:
  176. (void)nextstate;
  177. // modify current postion index:
  178. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->SwitchState.idx );
  179. }
  180. break;
  181. }
  182. return nextstate;
  183. }