shellmatta.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 state enumeration for ymodem receive state machine
  104. */
  105. typedef enum
  106. {
  107. INACTIVE, /* YModem module not initialised */
  108. WAIT_FOR_START, /* waiting for start of header */
  109. RECEIVE_PACKET, /* currently receiving a packet */
  110. } shellmatta_ymodem_state_t;
  111. typedef struct
  112. {
  113. void (*yModemCancelCallback)(void);
  114. void (*yModemRecvPacketCallback)(void);
  115. void (*ymodemTransmissionCompleteCallback)(void);
  116. } shellmatta_ymodem_callbacks_t;
  117. #ifdef SHELLMATTA_TRANSPORT_ENABLE
  118. /**
  119. * @brief definition of shellmatta transport layer states
  120. */
  121. typedef enum
  122. {
  123. SHELLMATTA_TRANSPORT_STATE_WAIT = 0u , /**< wait for start of header */
  124. SHELLMATTA_TRANSPORT_STATE_GET_HEADER , /**< read in header */
  125. SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD , /**< read in payload */
  126. SHELLMATTA_TRANSPORT_STATE_GET_CRC , /**< read crc and process data */
  127. } shellmatta_transport_state_t;
  128. /** @brief max length of a plain data payload */
  129. #define SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH ((uint8_t)(255))
  130. /**
  131. * @brief shellmatta transport crc function definition for custom crcs
  132. * @param[in] data data to calculate the crc of
  133. * @param[in] size size of the data in bytes
  134. */
  135. typedef uint32_t (*shellmatta_transport_crc_t)(const char* data, const uint32_t size);
  136. /**
  137. * @brief structure of one shellmatta transport header
  138. */
  139. typedef struct __attribute__((__packed__))
  140. {
  141. uint8_t startOfHeader; /** start of header field */
  142. uint8_t protocolVersion; /** protocol version of the packet */
  143. uint8_t packetType; /** type of the packet */
  144. uint8_t payloadLength; /** length of the payload */
  145. uint8_t source; /** source of the packet */
  146. uint8_t destination; /** destination of the packet */
  147. uint8_t sequenceH2S; /** sequence counter host to shellmatta */
  148. uint8_t sequenceS2H; /** sequence counter shellmatta to host */
  149. } shellmatta_transport_header_t;
  150. /**
  151. * @brief structure of one shellmatta transport packet
  152. */
  153. typedef struct __attribute__((__packed__))
  154. {
  155. shellmatta_transport_header_t header; /**< header of the packet */
  156. char payload[SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH]; /**< payload of the packet */
  157. uint32_t crc; /**< crc of the packet */
  158. } shellmatta_transport_packet_t;
  159. /**
  160. * @brief structure of one shellmatta transport layer instance
  161. */
  162. typedef struct
  163. {
  164. shellmatta_transport_state_t state; /**< current state of the transport layer reception */
  165. bool active; /**< is transport layer communication active */
  166. bool disableAutoFlush; /**< enforce manual flushing */
  167. bool mandatory; /**< is the transport layer enforced */
  168. uint8_t sequenceH2S; /**< sequence counter host to shellmatta */
  169. uint8_t sequenceS2H; /**< sequenc counter shellmatta to host */
  170. uint32_t headerIndex; /**< read indey of the header */
  171. uint32_t payloadIndex; /**< read index of the payload */
  172. uint32_t crcIndex; /**< read index of the checmsum */
  173. shellmatta_transport_packet_t inPacket; /**< buffer for the received packets */
  174. shellmatta_transport_packet_t outPacket; /**< buffer for the sent packets */
  175. shellmatta_write_t writeFct; /**< shellmatta write function */
  176. shellmatta_transport_crc_t customCrcFct; /**< use this function to calculate crcs */
  177. } shellmatta_transport_layer_t;
  178. #endif
  179. /**
  180. * @brief structure of one shellmatta instance
  181. */
  182. typedef struct
  183. {
  184. uint32_t magic; /**< magic number to check if initialized */
  185. char *buffer; /**< input buffer */
  186. uint32_t bufferSize; /**< size of the input buffer */
  187. uint32_t inputCount; /**< offset of the current write operation */
  188. uint32_t byteCounter; /**< counter used to loop over input data */
  189. uint32_t lastNewlineIdx; /**< index of the lest newline */
  190. uint32_t cursor; /**< offset where the cursor is at */
  191. uint32_t stdinIdx; /**< start index of stdin in buffer */
  192. uint32_t stdinLength; /**< length of the stdin data */
  193. char *historyBuffer; /**< buffer to store the last commands */
  194. uint32_t historyBufferSize; /**< size of the history buffer */
  195. uint32_t historyStart; /**< index of the oldest stored command */
  196. uint32_t historyEnd; /**< index of the newest stored command */
  197. uint32_t historyRead; /**< index of the current search */
  198. bool historyReadUp; /**< flag to show the last history dir */
  199. uint32_t tabCounter; /**< counts the tabulator key presses */
  200. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  201. char escapeChars[4u]; /**< buffer to save the escape characters */
  202. uint32_t hereStartIdx; /**< heredoc start of "<<" */
  203. uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
  204. uint32_t hereLength; /**< length of the heredoc delimiter */
  205. bool echoEnabled; /**< if true the input is printed */
  206. bool dirty; /**< dirty flag to show changes */
  207. const char *prompt; /**< prompt is printed after every command */
  208. char delimiter; /**< delimiter (return) to terminate a cmd */
  209. shellmatta_mode_t mode; /**< mode of the shell */
  210. shellmatta_write_t write; /**< pointer to write function */
  211. shellmatta_cmd_t helpCmd; /**< help command structure */
  212. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  213. shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
  214. shellmatta_cmd_t *busyCmd; /**< command to be polled (busy mode) */
  215. bool cmdListIsConst; /**< true if the #cmdList was passed during
  216. initialization */
  217. shellmatta_opt_t optionParser; /**< option parser sructure */
  218. shellmatta_ymodem_state_t ymodemState; /**< current state of the ymodem module */
  219. #ifdef SHELLMATTA_TRANSPORT_ENABLE
  220. bool transportEnabled; /**< if true the transport layer is enabled */
  221. uint32_t transportBusyMark; /**< transport processing position during
  222. busy cmd execution */
  223. shellmatta_transport_layer_t transportLayer; /**< transport layer instance */
  224. #endif
  225. } shellmatta_instance_t;
  226. /**
  227. * @brief helper macro for the send function
  228. */
  229. #ifdef SHELLMATTA_TRANSPORT_ENABLE
  230. #define SHELLMATTA_WRITE(data, length) inst->transportLayer.active == true ? \
  231. shellmatta_transport_write((shellmatta_transport_layer_t*)&inst->transportLayer, (data), (length)) : \
  232. inst->write((data), (length))
  233. #else
  234. #define SHELLMATTA_WRITE(data, length) inst->write((data), (length))
  235. #endif
  236. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  237. shellmatta_handle_t *handle,
  238. char *buffer,
  239. uint32_t bufferSize,
  240. char *historyBuffer,
  241. uint32_t historyBufferSize,
  242. const char *prompt,
  243. const shellmatta_cmd_t *cmdList,
  244. shellmatta_write_t writeFct);
  245. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
  246. bool printPrompt);
  247. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  248. shellmatta_cmd_t *cmd);
  249. shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
  250. shellmatta_cmd_t *cmd);
  251. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  252. shellmatta_mode_t mode,
  253. bool echoEnabled,
  254. char delimiter);
  255. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  256. char *data,
  257. uint32_t size);
  258. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  259. char *data,
  260. uint32_t length);
  261. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  262. char **data,
  263. uint32_t *length);
  264. shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
  265. const char *optionString,
  266. char *option,
  267. char **argument,
  268. uint32_t *argLen);
  269. shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
  270. const shellmatta_opt_long_t *longOptions,
  271. char *option,
  272. char **argument,
  273. uint32_t *argLen);
  274. #ifdef SHELLMATTA_TRANSPORT_ENABLE
  275. shellmatta_retCode_t shellmatta_transport_configure(shellmatta_handle_t handle,
  276. bool mandatory,
  277. bool disableAutoFlush,
  278. shellmatta_transport_crc_t customCrcFct);
  279. shellmatta_retCode_t shellmatta_transport_reset(shellmatta_handle_t handle);
  280. shellmatta_retCode_t shellmatta_transport_flush(shellmatta_handle_t handle);
  281. #endif
  282. #ifndef SHELLMATTA_STRIP_PRINTF
  283. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  284. const char *fmt,
  285. ...);
  286. #endif
  287. uint8_t shellmatta_ymodem( shellmatta_handle_t handle,
  288. uint8_t* buffer,
  289. uint32_t* fileSize,
  290. uint16_t* packetSize,
  291. shellmatta_ymodem_callbacks_t callbacks);
  292. void shellmatta_ymodem_end( shellmatta_handle_t handle,
  293. bool doCancel);
  294. void shellmatta_ymodem_set_enable(bool doEnable);
  295. bool shellmatta_ymodem_is_active(void);
  296. #endif
  297. /** @} */