shellmatta.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file shellmatta.h
  10. * @brief API definition of the Shellmatta terminal implementation
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta_api Shellmatta API description
  15. * @{
  16. */
  17. #ifndef _SHELLMATTA_H_
  18. #define _SHELLMATTA_H_
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. /* global defines */
  22. /**
  23. * @brief definition of a shellmatta handle
  24. */
  25. typedef void* shellmatta_handle_t;
  26. /**
  27. * @brief definition of shellmatta return codes
  28. */
  29. typedef enum
  30. {
  31. SHELLMATTA_OK = 0u, /**< everything is OK */
  32. SHELLMATTA_ERROR , /**< error occured */
  33. SHELLMATTA_CONTINUE , /**< the function is not over */
  34. SHELLMATTA_USE_FAULT , /**< parameter error - wrong usage */
  35. SHELLMATTA_DUPLICATE , /**< duplicate command */
  36. SHELLMATTA_BUSY /**< command is busy keep calling */
  37. } shellmatta_retCode_t;
  38. /**
  39. * @brief definition of shellmatta insert mode
  40. */
  41. typedef enum
  42. {
  43. SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
  44. SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
  45. } shellmatta_mode_t;
  46. /**
  47. * @brief definition of shellmatta optionparser agument type
  48. */
  49. typedef enum
  50. {
  51. SHELLMATTA_OPT_ARG_NONE = 0u, /**< no argument expected */
  52. SHELLMATTA_OPT_ARG_REQUIRED, /**< argument is required */
  53. SHELLMATTA_OPT_ARG_OPTIONAL, /**< argument is optional */
  54. } shellmatta_opt_argtype_t;
  55. /**
  56. * @brief definition of shellmatta optionparser agument type
  57. */
  58. typedef struct
  59. {
  60. const char *paramLong; /**< long parameter string */
  61. const char paramShort; /**< short parameter char */
  62. shellmatta_opt_argtype_t argtype; /**< argument type expected */
  63. } shellmatta_opt_long_t;
  64. /**
  65. * @brief definition of shellmatta optionparser structure
  66. */
  67. typedef struct
  68. {
  69. uint32_t argStart; /**< start of the arguments of the command */
  70. uint32_t offset; /**< current offset of the option parser */
  71. uint32_t nextOffset; /**< offset of the next hunk */
  72. uint32_t len; /**< length of the current hunk */
  73. } shellmatta_opt_t;
  74. /**
  75. * @brief shellmatta command function definition
  76. * @param[in] handle pointer to the instance which is calling the cmd
  77. * @param[in] arguments argument string called to run this command beginning
  78. * with the command itself
  79. * @param[in] length length of the argument string
  80. */
  81. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
  82. const char *arguments,
  83. uint32_t length);
  84. /**
  85. * @brief shellmatta write function definition
  86. * @param[in] data data to be written to the output
  87. * @param[in] length length of the data to be written
  88. */
  89. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  90. /**
  91. * @brief structure of one shellmatta command
  92. */
  93. typedef struct shellmatta_cmd
  94. {
  95. char *cmd; /**< command name */
  96. char *cmdAlias; /**< command alias */
  97. char *helpText; /**< help text to print in "help" command */
  98. char *usageText; /**< usage text - printed on "help cmd" */
  99. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callback function */
  100. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  101. } shellmatta_cmd_t;
  102. /**
  103. * @brief definition of shellmatta transport layer states
  104. */
  105. typedef enum
  106. {
  107. STATE_GET_SOH =0u, /**< start of header state of transport layer */
  108. STATE_MANUAL_INPUT , /**< state for manual input => transport layer disabled */
  109. STATE_GET_PROTOCOL_VERSION , /**< protocol version state of transport layer */
  110. STATE_GET_PACKET_TYPE , /**< packet type state of transport layer */
  111. STATE_GET_PAYLOAD_LENGTH , /**< payload length state of transport layer */
  112. STATE_GET_SOURCE , /**< source state of transport layer */
  113. STATE_GET_DESTINATION , /**< destination state of transport layer */
  114. STATE_GET_H2S_SEQUENCE_CNT , /**< host to shellmatta sequence counter state of transport layer */
  115. STATE_GET_S2H_SEQUENCE_CNT , /**< shellmatta to host sequence counter state of transport layer */
  116. STATE_GET_PAYLOAD , /**< payload state of transport layer */
  117. STATE_GET_CRC , /**< crc state of transport layer */
  118. STATE_PROCESS_PAYLOAD
  119. } shellmatta_transport_state_t;
  120. /**
  121. * @brief definition of shellmatta transport layer packet types
  122. */
  123. typedef enum
  124. {
  125. PACKET_DATA = 0x00u, /**< packet type to send plain data */
  126. PACKET_SEQ_CNT_REQUEST = 0x01u, /**< packet type to request sequence counters */
  127. PACKET_SEQ_CNT_RESPOND = 0x81u, /**< packet type to respond with sequence counters */
  128. PACKET_MAX_BUFFERSIZE_REQUEST = 0x02u, /**< packet type to set and request max buffersize */
  129. PACKET_MAX_BUFFERSIZE_RESPOND = 0x82u, /**< packet type to respond with max buffersize */
  130. PACKET_SEARCH_DEVICE_REQUEST = 0x03u, /**< UNUSED: packet type to request search for a device by unique id */
  131. PACKET_SEARCH_DEVICE_RESPOND = 0x83u, /**< UNUSED: packet type to respond with search results */
  132. PACKET_SET_ADDRESS_REQUEST = 0x04u, /**< UNUSED: packet type to set and request an address */
  133. PACKET_SET_ADDRESS_RESPOND = 0x84u /**< UNUSED: packet type to respond with a set address */
  134. } shellmatta_transport_packet_t;
  135. /* payload length defines */
  136. /** @brief max length of a plain data payload */
  137. #define SHELLMATTA_PAYLOAD_MAXLENGTH ((uint8_t)(255))
  138. /**
  139. * @brief structure of one shellmatta transport layer instance
  140. */
  141. typedef struct
  142. {
  143. uint8_t h2s_sequenceCnt; /**< TODO */
  144. uint8_t s2h_sequenceCnt; /**< */
  145. shellmatta_transport_state_t state; /**< */
  146. bool mandatory; /**< */
  147. bool active; /**< */
  148. bool continueStep; /**< */
  149. shellmatta_write_t originalWrite; /**< */
  150. uint8_t protocolVersion; /**< */
  151. shellmatta_transport_packet_t packetType; /**< */
  152. uint32_t payloadLength; /**< */
  153. uint8_t source; /**< */
  154. uint8_t destination; /**< */
  155. uint32_t crc32; /**< */
  156. uint8_t payloadCounter; /**< */
  157. uint8_t crcCounter; /**< */
  158. uint8_t packetSequenceCounter_h2s; /**< */
  159. uint8_t packetSequenceCounter_s2h; /**< */
  160. char payloadBuffer[SHELLMATTA_PAYLOAD_MAXLENGTH]; /**< */
  161. } shellmatta_transport_layer_t;
  162. /**
  163. * @brief structure of one shellmatta instance
  164. */
  165. typedef struct
  166. {
  167. uint32_t magic; /**< magic number to check if initialized */
  168. char *buffer; /**< input buffer */
  169. uint32_t bufferSize; /**< size of the input buffer */
  170. uint32_t inputCount; /**< offset of the current write operation */
  171. uint32_t byteCounter; /**< counter used to loop over input data */
  172. uint32_t lastNewlineIdx; /**< index of the lest newline */
  173. uint32_t cursor; /**< offset where the cursor is at */
  174. uint32_t stdinIdx; /**< start index of stdin in buffer */
  175. uint32_t stdinLength; /**< length of the stdin data */
  176. char *historyBuffer; /**< buffer to store the last commands */
  177. uint32_t historyBufferSize; /**< size of the history buffer */
  178. uint32_t historyStart; /**< index of the oldest stored command */
  179. uint32_t historyEnd; /**< index of the newest stored command */
  180. uint32_t historyRead; /**< index of the current search */
  181. bool historyReadUp; /**< flag to show the last history dir */
  182. uint32_t tabCounter; /**< counts the tabulator key presses */
  183. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  184. char escapeChars[4u]; /**< buffer to save the escape characters */
  185. uint32_t hereStartIdx; /**< heredoc start of "<<" */
  186. uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
  187. uint32_t hereLength; /**< length of the heredoc delimiter */
  188. bool echoEnabled; /**< if true the input is printed */
  189. bool dirty; /**< dirty flag to show changes */
  190. const char *prompt; /**< prompt is printed after every command */
  191. char delimiter; /**< delimiter (return) to terminate a cmd */
  192. shellmatta_mode_t mode; /**< mode of the shell */
  193. shellmatta_write_t write; /**< pointer to write function */
  194. shellmatta_cmd_t helpCmd; /**< help command structure */
  195. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  196. shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
  197. shellmatta_cmd_t *busyCmd; /**< command to be polled (busy mode) */
  198. bool cmdListIsConst; /**< true if the #cmdList was passed during
  199. initialization */
  200. shellmatta_opt_t optionParser; /**< option parser sructure */
  201. shellmatta_transport_layer_t transportLayer; /**< transport layer instance */
  202. } shellmatta_instance_t;
  203. /**
  204. * @brief helper macro for the send function
  205. */
  206. #define SHELLMATTA_WRITE(data, length) inst->transportLayer.active == true ? \
  207. shellmatta_transport_write((shellmatta_transport_layer_t*)&inst->transportLayer, (data), (length)) : \
  208. inst->write((data), (length))
  209. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  210. shellmatta_handle_t *handle,
  211. char *buffer,
  212. uint32_t bufferSize,
  213. char *historyBuffer,
  214. uint32_t historyBufferSize,
  215. const char *prompt,
  216. const shellmatta_cmd_t *cmdList,
  217. shellmatta_write_t writeFct);
  218. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
  219. bool printPrompt);
  220. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  221. shellmatta_cmd_t *cmd);
  222. shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
  223. shellmatta_cmd_t *cmd);
  224. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  225. shellmatta_mode_t mode,
  226. bool echoEnabled,
  227. char delimiter);
  228. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  229. char *data,
  230. uint32_t size);
  231. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  232. char *data,
  233. uint32_t length);
  234. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  235. char **data,
  236. uint32_t *length);
  237. shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
  238. const char *optionString,
  239. char *option,
  240. char **argument,
  241. uint32_t *argLen);
  242. shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
  243. const shellmatta_opt_long_t *longOptions,
  244. char *option,
  245. char **argument,
  246. uint32_t *argLen);
  247. #ifndef SHELLMATTA_STRIP_PRINTF
  248. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  249. const char *fmt,
  250. ...);
  251. #endif
  252. #endif
  253. /** @} */