scpi_core_private.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef SCPI_CORE_PRIVATE_H
  2. #define SCPI_CORE_PRIVATE_H
  3. #ifdef SCPI_CORE_PRIVATE
  4. #include "app/fseq/fseq.h"
  5. #include "app/scpi/scpi_parser.h"
  6. #include "app/tlst/tree_list.h" // sTreeList_t
  7. #include "app/scpi/scpi_errq.h"
  8. #include "app/scpi/scpi_errs.h"
  9. #include "app/scpi/scpi_gpib_core.h"
  10. // -----------------------------------------------------------
  11. // @sScpiParserStateEntry_t
  12. // SCPI Parser state-machine's state entry:
  13. // Includes the function-table @entry and the
  14. // ... state context reference @ctx_ref
  15. typedef struct
  16. {
  17. fFSeqEntry_t entry;
  18. void * const ctx_ref;
  19. }
  20. sScpiParserStateEntry_t;
  21. // -----------------------------------------------------------
  22. // @DECLARE_SCPI_STATE_ENTRY
  23. // Declares the state entry to initialize @sScpiCommandHandlerEntry_t
  24. #define DECLARE_SCPI_STATE_ENTRY(vtable_, ctx_)\
  25. { .entry = { .vtbl = &vtable_ }, .ctx_ref = ctx_ }
  26. // -----------------------------------------------------------
  27. // @sScpiCommandHandlerEntry_t
  28. // SCPI Command handler entry.
  29. // Links the state machine entry (function-object) and
  30. // ... the data context together to form the functor-object.
  31. typedef struct
  32. {
  33. fFSeqEntry_t entry; // state-machine's state entry
  34. const void * ctx; // state's data
  35. }
  36. sScpiCommandHandlerEntry_t;
  37. // -----------------------------------------------------------
  38. // eScpiParserStateId_t
  39. // SCPI Parser States
  40. typedef enum
  41. {
  42. eParserResetState, // restart parser for new SCPI message
  43. eSearchForCommandHeader, // searching for program command header
  44. eProcessCommandHeader, // interpreting the command header
  45. eProcessProgramData, // processing the command, processing the program data
  46. eProcessEndOfProgramData, // end of processing the program data, search message separator
  47. eHandleError, // error condition
  48. }
  49. eScpiParserStateId_t;
  50. // -----------------------------------------------------------
  51. typedef enum
  52. {
  53. // ---------------------// Common event codes:
  54. eScpiEventEmpty, // - No event;
  55. eScpiEventWrite, // - Chunk write / processing (eProcessProgramData);
  56. eScpiEventRead, // - Read chunk / processing (eProcessProgramData);
  57. eScpiEventRestart, // - Restart event
  58. eScpiEventContinue, // - Parse continue event (Error Handle Specific code)
  59. eScpiEventError, // - Special event (exception)
  60. }
  61. eScpiEventType_t;
  62. // -----------------------------------------------------------
  63. // The following block is disgned to grant exclusive write access to
  64. // some variables in the common context to prevent modifing by mistake.
  65. // Some variables marked as 'guarded' using _GUARD_ macro.
  66. // This means that this variable is only allowed to be modified if a special
  67. // guarding macro is defined in the module that includes this file.
  68. // This guarding macro is different for each file, and the following block can
  69. // select what file grant the access to.
  70. #if !defined(SCPI_CORE_C) && !defined(SCPI_RESET_STATE_C)
  71. #define _GUARD_ const // all files but excluding 'scpi_core.c' and 'reset_state.c'
  72. #else
  73. #define _GUARD_ // only 'scpi_core.c' and 'reset_state.c'
  74. #endif
  75. #if !defined(SCPI_END_OF_PROCESS_DATA_C) && !defined(SCPI_PROCESS_DATA_C)
  76. #define _GUARD_01_ _GUARD_ // all files but excluding 'endof_process_data.c' or 'process_data.c'
  77. #else
  78. #define _GUARD_01_ // only 'endof_process_data.c' or 'process_data.c'
  79. #endif
  80. #if !defined(SCPI_PROCESS_DATA_C) && !defined(SCPI_HANDLE_ERROR_C) && !defined(SCPI_PROCESS_COMMAND_C) && !defined(SCPI_END_OF_PROCESS_DATA_C)
  81. #define _GUARD_02_ _GUARD_ // all files but excluding 'process_data.c', 'endof_process_data.c', 'process_command.c' and 'handle_error.c'
  82. #else
  83. #define _GUARD_02_ // only 'process_data.c', 'process_command.c' and 'handle_error.c', 'endof_process_data.c'
  84. #endif
  85. #if !defined(SCPI_HANDLE_ERROR_C) && !defined(SCPI_END_OF_PROCESS_DATA_C)
  86. #define _GUARD_03_ _GUARD_ // all files but excluding 'endof_process_data.c' and 'handle_error.c'
  87. #else
  88. #define _GUARD_03_ // only 'handle_error.c' and 'endof_process_data.c'
  89. #endif
  90. //
  91. //
  92. // -----------------------------------------------------------
  93. // ==============================================================================================
  94. typedef struct
  95. {
  96. struct
  97. {
  98. const uint8_t * _GUARD_ pData; // [IN]
  99. size_t _GUARD_ nDataLength; // [IN]
  100. }
  101. sWrite;
  102. struct
  103. {
  104. uint8_t * _GUARD_ pBuffer; // [IN]
  105. size_t _GUARD_01_ nBufferSize; // [IN]
  106. uint8_t * pBufferIdx; // [IN, OUT]
  107. size_t nDataLength; // [IN, OUT], shall be modified by writer
  108. size_t nTotalLength; // [IN, OUT], shall be modified by writer
  109. const void * vLastBufferIdx; // [OUT]
  110. }
  111. sRead;
  112. struct
  113. {
  114. //bool bNewTransfer; // [IN]
  115. uint8_t _GUARD_03_ eCode; // [IN], refer to @eScpiEventType_t
  116. int8_t eStatus; // [OUT], refer to @eScpiStatus_t
  117. }
  118. sEvent;
  119. struct
  120. {
  121. const uint8_t * pStr; // [IN, OUT], current iterator in sWrite.pData[]
  122. const uint8_t * pEnd; // [IN, OUT], end-of-string in sWrite.pData[]
  123. bool _GUARD_ w_bEndOfMessage; // [IN], Write Event
  124. bool r_bEndOfMessage; // [IN, OUT], Read Event
  125. bool bQuery; // [IN, OUT]
  126. bool bLeadResponseSep; // [IN, OUT] place leading response separator
  127. }
  128. sMessage;
  129. struct
  130. {
  131. errq_t xScpiErrorQueue; // SCPI Log queue object
  132. int runtimeError; // SCPI Error code
  133. char xScpiErrorMessage[ SCPI_ERRORMSG_MAX ]; // SCPI Error description
  134. uint8_t xScpiErrorLogStorage[ SCPI_ERRORLOG_CAPACITY ]; // SCPI Log queue storage buffer
  135. bool errorQueueOverflow; // Error Queue Overflow indicator
  136. }
  137. sExecution;
  138. struct
  139. {
  140. // @xCtxObj: SCPI tokens parser object
  141. xParseEntry_t xCtxObj; // [IN, OUT]
  142. // @xCmdBranch: SCPI command tree branch
  143. sTreeList_t * xCmdBranch; // [IN, OUT]
  144. // @xHandler: SCPI command handler entry
  145. const sScpiCommandHandlerEntry_t * xHandler; // [OUT]
  146. // @xHandlerToken: linked handler string token
  147. sStrToken_t xHandlerToken; // [OUT]
  148. // @xArgToken: current argument token
  149. sStrToken_t xArgToken; // [OUT]
  150. // @bFirstCommandIndicator - the first command in the line indicator
  151. bool bFirstCommandIndicator; // [IN, OUT]
  152. }
  153. sParser;
  154. struct
  155. {
  156. xGPIBRegisters_t registers;
  157. }
  158. sGPIB;
  159. struct
  160. {
  161. uint8_t inputCommand[ SCPI_MAX_INPUT_COMMAND ];
  162. size_t nBytes;
  163. }
  164. sCache;
  165. }
  166. sScpiParserContext_t;
  167. // ==============================================================================================
  168. // @fsq_GetStateById
  169. // Get the state entry by state identifier @sid
  170. // @this - current state pointer
  171. // @sid - the state identifier (eScpiParserStateId_t)
  172. // Returns: the state entry pointer
  173. // Note: the caller must be the state from the same entity
  174. const struct fFSeqEntry_t * fsq_GetStateById( const struct fFSeqEntry_t * this, eScpiParserStateId_t sid );
  175. // ==============================================================================================
  176. // @fsq_RaiseError
  177. // Generate internal error event to place it into SCPI Error Log
  178. // @scpiError - SCPI error code
  179. // @msgHandler - optional null-terminated string prefix
  180. // @msgDescription - optional string message with @msgDescLength length
  181. // @msgDescriptionEnd - end of optional message string, is used to determine the string length (@msgDescription)
  182. void fsq_RaiseError( int scpiError, const char * msgHeader, const char * msgDescription, const char * msgDescEnd );
  183. #endif
  184. #endif