scpi_tlst_ex.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef SCPI_TREE_LIST_EXTENTION_H
  2. #define SCPI_TREE_LIST_EXTENTION_H
  3. #include "app/tlst/tree_list.h"
  4. #include "app/scpi/scpi_core.h"
  5. // Author: Sychov A., Jan 2021
  6. // Extension module to adopt the Tree-List algorithms to the SCPI module.
  7. //----------------------------------------------------------------
  8. // Refer to:
  9. // [1] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  10. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  11. // Standard Digital Interface for Programmable Instrumentation"
  12. // [2] SCPI Specification, revision 1999.0
  13. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  14. //----------------------------------------------------------------
  15. // ========================================================================================================
  16. // @scpi_command_header_compare
  17. // Compares the program mnemonic of registered command (@command) with
  18. // ... the program mnemonic of received command header (@src_key).
  19. // Function ignores the tailing question mark of the command header mnemonic.
  20. // References:
  21. // "7.6 Program Header Functional Elements", [1]
  22. // @commandString - the registered command header part, null-term string,
  23. // ... attached to the node using @nodekey_ parameter of DECLARE_TREE_LIST_LEAF.
  24. // @comparingToken - the searching command header part, a pair of (@shead, @stail)
  25. // ... pointers grouped into the @sStrToken_t.
  26. // Note: the @sStrToken_t structure referred by @src_key shall be writable.
  27. // Note: @command can points to the part of the original command string.
  28. // Returns: The function returns true in case the part specified by @src_key
  29. // ... matchs to the part of the command specified by @command and nearest
  30. // ... mnemonic separator (colon).
  31. bool scpi_command_header_compare( const char * commandString,
  32. const sStrToken_t * comparingToken );
  33. // @treelist_header_compare
  34. // Function-wrapper for Tree-List.
  35. // Compares the program mnemonic of registered command (@command) with
  36. // ... the program mnemonic of received command header (@src_key).
  37. // @v_command = @commandString (@scpi_command_header_compare)
  38. // @v_src_key = @comparingToken (@scpi_command_header_compare)
  39. // Refer: @command_header_compare for details.
  40. static inline bool treelist_header_compare( const void * v_command,
  41. const void * v_src_key )
  42. {
  43. return scpi_command_header_compare( (const char*)v_command, (const sStrToken_t*)v_src_key );
  44. }
  45. // ========================================================================================================
  46. // @scpi_is_default_node
  47. // Checks if the passed string is the default (optional) SCPI command mnemonic
  48. // Is used to determine the default node in the tree-list.
  49. // References:
  50. // "5.1 Interpreting Command Tables", [2]
  51. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  52. // "7.6.1.5 Header Compounding Rules", [1]
  53. // @commandString - SCPI command simple mnemonic;
  54. // Note: compound mnemonics are not supported;
  55. // Note: the passed mnemonic must be valid, the mnemonic will not be validated.
  56. // Returns:
  57. // - true: the specified @commandString is the default command mnemonic;
  58. // - false: the specified @commandString is not the default command mnemonic;
  59. bool scpi_is_default_node( const char * commandString );
  60. // @treelist_is_default_node
  61. // Function-wrapper for Tree-List.
  62. // Checks if the passed string is the default (optional) SCPI command mnemonic
  63. // Is used to determine the default node in the tree-list.
  64. // @node - node to check;
  65. // @ctx - optional user dependent context, can be NULL;
  66. // References:
  67. // "5.1 Interpreting Command Tables", [2]
  68. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  69. // "7.6.1.5 Header Compounding Rules", [1]
  70. // Returns:
  71. // - true: the specified @node is the default node;
  72. // - false: the specified @node is not the default node;
  73. // Note: compound mnemonics are not supported;
  74. // Note: the passed mnemonic must be valid, the mnemonic will not be validated.
  75. static inline bool treelist_is_default_node( const sTreeList_t * node, void * ctx )
  76. {
  77. return scpi_is_default_node( node->c_key );
  78. }
  79. // ========================================================================================================
  80. #if TREE_LIST_RECURSIVE_LIMIT_ENABLE
  81. // @scpi_search_recursive_limiter
  82. // Limits the depth of recursive calls of @tree_list_search_deep
  83. // Is used to prevent stack overflow.
  84. // @dir - direction: true - enter recursive call, false - leave call;
  85. // @ctx - mandatory user dependent context.
  86. // Returns, if @dir is 'true':
  87. // - true: recursive call is possible;
  88. // - false: recursive call is impossible;
  89. // Returns, if @dir is 'false': does not matter;
  90. bool scpi_search_recursive_limiter( bool dir, void * ctx );
  91. // @treelist_recursive_limiter
  92. // Function-wrapper for Tree-List.
  93. // Limits the depth of recursive calls of @tree_list_search_deep
  94. // Is used to prevent stack overflow.
  95. // @dir - direction: true - enter recursive call, false - leave call;
  96. // @ctx - mandatory user dependent context.
  97. // Returns, if @dir is 'true':
  98. // - true: recursive call is possible;
  99. // - false: recursive call is impossible;
  100. // Returns, if @dir is 'false': does not matter;
  101. static inline bool treelist_recursive_limiter( bool dir, void * ctx )
  102. {
  103. return scpi_search_recursive_limiter( dir, ctx );
  104. }
  105. #endif
  106. // ========================================================================================================
  107. // @sScpiCmdGetNextKey_t
  108. // Used in @scpi_command_getnext_key
  109. // Search key context.
  110. typedef struct
  111. {
  112. sStrToken_t found_token; // current command mnemonic token
  113. sStrToken_t full_token; // full command line token
  114. }
  115. sScpiCmdGetNextKey_t;
  116. // ========================================================================================================
  117. // @sTreeListSearchCtx_t
  118. // SCPI Command search context
  119. typedef struct
  120. {
  121. #if TREE_LIST_RECURSIVE_LIMIT_ENABLE
  122. int call_depth; // recursive call depth
  123. #endif
  124. sScpiCmdGetNextKey_t cmdKey; // command key iterator context
  125. const sTreeList_t * parent; // parent of found SCPI command node
  126. }
  127. sTreeListSearchCtx_t;
  128. // ========================================================================================================
  129. // @scpi_command_getnext_key
  130. // Get next command mnemonic token in the chain specified in @ctx
  131. // @ctx - SCPI command search context;
  132. // Returns:
  133. // NULL - no key found / end of the command line;
  134. // NON NULL: string token containing command mnemonic (key)
  135. const sStrToken_t* scpi_command_getnext_key( sScpiCmdGetNextKey_t * ctx );
  136. #if 0
  137. // @scpi_command_getnext_prepare
  138. // Prepare context for @scpi_command_getnext_key before calling @tree_list_search_deep
  139. // @ctx - search key context, is used to iterate upon the command mnemonic chain (compound command)
  140. // @cmd_shead - compound SCPI command mnemonic head
  141. // @cmd_stail - compound SCPI command mnemonic tail
  142. void scpi_command_getnext_prepare( sScpiCmdGetNextKey_t * ctx, const char * cmd_shead, const char * cmd_stail );
  143. #endif
  144. // @scpi_command_search_prepare
  145. // Prepare the SCPI Command search context before calling @tree_list_search_deep
  146. // @ctx - search context, is used in call of @tree_list_search_deep
  147. // @cmd_shead - compound SCPI command mnemonic head
  148. // @cmd_stail - compound SCPI command mnemonic tail
  149. void scpi_command_search_prepare( sTreeListSearchCtx_t * ctx, const char * cmd_shead, const char * cmd_stail );
  150. // @treelist_getnext_key
  151. // Function-wrapper for Tree-List.
  152. // Get next key in the chain for search
  153. // @ctx - routine context;
  154. // Returns:
  155. // NULL - no key found / end of the chain;
  156. // NON NULL: valid key to continue searching
  157. static inline const void * treelist_getnext_key( void * _ctx )
  158. {
  159. sTreeListSearchCtx_t * ctx = (sTreeListSearchCtx_t*)_ctx;
  160. return scpi_command_getnext_key( &ctx->cmdKey );
  161. }
  162. // @scpi_command_command_found
  163. // SCPI search command result notifee.
  164. void scpi_command_command_found( sTreeListSearchCtx_t * ctx, const sTreeList_t * found, const sTreeList_t * parent );
  165. // @treelist_result_agree
  166. // Function-wrapper for Tree-List.
  167. // Tree list result notification/agreeing function.
  168. // Notifies the SCPI-level about found command node and agrees the first search result.
  169. static inline bool treelist_result_agree( const sTreeList_t * found, const sTreeList_t * parent, void * _ctx )
  170. {
  171. sTreeListSearchCtx_t * ctx = (sTreeListSearchCtx_t*)_ctx;
  172. scpi_command_command_found( ctx, found, parent );
  173. return true; // agree result
  174. }
  175. // ========================================================================================================
  176. // @scpi_commands_tlds - functional set for @tree_list_search_deep
  177. extern const sTreeListSearchDeep_FSet_t scpi_commands_tlds;
  178. // ========================================================================================================
  179. #endif