cli.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file cli.h
  3. * Cli API
  4. */
  5. #pragma once
  6. #include <m-string.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. typedef enum {
  11. CliSymbolAsciiSOH = 0x01,
  12. CliSymbolAsciiETX = 0x03,
  13. CliSymbolAsciiEOT = 0x04,
  14. CliSymbolAsciiBell = 0x07,
  15. CliSymbolAsciiBackspace = 0x08,
  16. CliSymbolAsciiTab = 0x09,
  17. CliSymbolAsciiLF = 0x0A,
  18. CliSymbolAsciiCR = 0x0D,
  19. CliSymbolAsciiEsc = 0x1B,
  20. CliSymbolAsciiUS = 0x1F,
  21. CliSymbolAsciiSpace = 0x20,
  22. CliSymbolAsciiDel = 0x7F,
  23. } CliSymbols;
  24. typedef enum {
  25. CliCommandFlagDefault = 0, /**< Default, loader lock is used */
  26. CliCommandFlagParallelSafe =
  27. (1 << 0), /**< Safe to run in parallel with other apps, loader lock is not used */
  28. CliCommandFlagInsomniaSafe = (1 << 1), /**< Safe to run with insomnia mode on */
  29. } CliCommandFlag;
  30. /** Cli type anonymous structure */
  31. typedef struct Cli Cli;
  32. /** Cli callback function pointer. Implement this interface and use
  33. * add_cli_command
  34. * @param args string with what was passed after command
  35. * @param context pointer to whatever you gave us on cli_add_command
  36. */
  37. typedef void (*CliCallback)(Cli* cli, string_t args, void* context);
  38. /** Add cli command Registers you command callback
  39. *
  40. * @param cli pointer to cli instance
  41. * @param name command name
  42. * @param flags CliCommandFlag
  43. * @param callback callback function
  44. * @param context pointer to whatever we need to pass to callback
  45. */
  46. void cli_add_command(
  47. Cli* cli,
  48. const char* name,
  49. CliCommandFlag flags,
  50. CliCallback callback,
  51. void* context);
  52. /** Print unified cmd usage tip
  53. *
  54. * @param cmd cmd name
  55. * @param usage usage tip
  56. * @param arg arg passed by user
  57. */
  58. void cli_print_usage(const char* cmd, const char* usage, const char* arg);
  59. /** Delete cli command
  60. *
  61. * @param cli pointer to cli instance
  62. * @param name command name
  63. */
  64. void cli_delete_command(Cli* cli, const char* name);
  65. /** Read from terminal
  66. *
  67. * @param cli Cli instance
  68. * @param buffer pointer to buffer
  69. * @param size size of buffer in bytes
  70. *
  71. * @return bytes read
  72. */
  73. size_t cli_read(Cli* cli, uint8_t* buffer, size_t size);
  74. /** Non-blocking read from terminal
  75. *
  76. * @param cli Cli instance
  77. * @param buffer pointer to buffer
  78. * @param size size of buffer in bytes
  79. * @param timeout timeout value in ms
  80. *
  81. * @return bytes read
  82. */
  83. size_t cli_read_timeout(Cli* cli, uint8_t* buffer, size_t size, uint32_t timeout);
  84. /** Non-blocking check for interrupt command received
  85. *
  86. * @param cli Cli instance
  87. *
  88. * @return true if received
  89. */
  90. bool cli_cmd_interrupt_received(Cli* cli);
  91. /** Write to terminal Do it only from inside of cli call.
  92. *
  93. * @param cli Cli instance
  94. * @param buffer pointer to buffer
  95. * @param size size of buffer in bytes
  96. */
  97. void cli_write(Cli* cli, const uint8_t* buffer, size_t size);
  98. /** Read character
  99. *
  100. * @param cli Cli instance
  101. *
  102. * @return char
  103. */
  104. char cli_getc(Cli* cli);
  105. /** New line Send new ine sequence
  106. */
  107. void cli_nl();
  108. void cli_session_open(Cli* cli, void* session);
  109. void cli_session_close(Cli* cli);
  110. bool cli_is_connected(Cli* cli);
  111. #ifdef __cplusplus
  112. }
  113. #endif