interface_switch.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "core/config.h"
  2. #include <stdio.h>
  3. #define SCPI_ARGS_N 0
  4. #include "app/scpi/scpi_handler.h"
  5. #include "app/scpi/commandHandlers/interface_switch.h"
  6. #include "app/nfm/nfm_base.h"
  7. // Refer to:
  8. // [1] SCPI Specification, revision 1999.0
  9. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  10. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  11. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  12. // =================================================================================
  13. // @fsqvbl_CommandHandlerInterfaceSwitch
  14. // State's virtual table
  15. static void fsqe_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  16. static void fsql_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  17. static const struct fFSeqEntry_t * fsqf_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  18. const fFSeqVTable_t fsqvbl_CommandHandlerInterfaceSwitch =
  19. {
  20. .f = fsqf_CommandHandlerInterfaceSwitch,
  21. .enter = fsqe_CommandHandlerInterfaceSwitch,
  22. .leave = fsql_CommandHandlerInterfaceSwitch
  23. };
  24. static void fsqe_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  25. {
  26. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  27. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  28. common_ctx->IntSwitch.idx = 0;
  29. }
  30. static void fsql_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  31. {
  32. }
  33. static const struct fFSeqEntry_t * fsqf_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this,
  34. tFSeqCtx_t ctx,
  35. const struct fFSeqEntry_t * * pDeferredNext )
  36. {
  37. const fFSeqEntry_t * nextstate = NULL;
  38. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  39. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  40. switch( common_ctx->event )
  41. {
  42. case eProgramData_Event_Write:
  43. {
  44. if( ! common_ctx->isQuery )
  45. {
  46. common_ctx->status = eProgramDataSyntaxError; // invalid command header type: COMMAND not supported
  47. }
  48. else if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  49. {
  50. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  51. }
  52. else
  53. {
  54. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  55. if( NFMClass->methods.usbInterface.setInterface( eNFM_IfaceUSBTMC ) )
  56. #else
  57. if( NFMClass->methods.usbInterface.setInterface( eNFM_IfaceUSBVendor ) )
  58. #endif
  59. {
  60. #if CONFIG_REBOOT_FEATURE
  61. RebootRequest();
  62. #endif
  63. common_ctx->status = eProgramDataNeedRead; // request processed
  64. }
  65. else
  66. {
  67. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  68. SCPI_ERROR_INTERNAL_INT_SWITCH_MSG,
  69. global_ctx->sParser.xHandlerToken.shead,
  70. global_ctx->sParser.xHandlerToken.stail );
  71. common_ctx->status = eProgramData_SpecificError; // request processed
  72. }
  73. // Since @done flag is set, this dispatcher shall not be called anymore.
  74. // Since this handler is implemented as a single-state automat, there no
  75. // ... other states to go to:
  76. (void)nextstate;
  77. }
  78. }
  79. break;
  80. case eProgramData_Event_Read:
  81. {
  82. if( 0 == common_ctx->IntSwitch.idx )
  83. {
  84. size_t l = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", SCPI_MESSAGE_INTERNAL_INT_SWITCH_MSG );
  85. if( l > 0 )
  86. {
  87. // place null-terminator in the end of line
  88. common_ctx->tempBuffer[l] = '\0';
  89. }
  90. else
  91. {
  92. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  93. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  94. global_ctx->sParser.xHandlerToken.shead,
  95. global_ctx->sParser.xHandlerToken.stail );
  96. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  97. break;
  98. }
  99. }
  100. // modify current postion index:
  101. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->IntSwitch.idx );
  102. }
  103. break;
  104. }
  105. return nextstate;
  106. }