cli.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define RECORD_CLI "cli"
  31. /** Cli type anonymous structure */
  32. typedef struct Cli Cli;
  33. /** Cli callback function pointer. Implement this interface and use
  34. * add_cli_command
  35. * @param args string with what was passed after command
  36. * @param context pointer to whatever you gave us on cli_add_command
  37. */
  38. typedef void (*CliCallback)(Cli* cli, string_t args, void* context);
  39. /** Add cli command Registers you command callback
  40. *
  41. * @param cli pointer to cli instance
  42. * @param name command name
  43. * @param flags CliCommandFlag
  44. * @param callback callback function
  45. * @param context pointer to whatever we need to pass to callback
  46. */
  47. void cli_add_command(
  48. Cli* cli,
  49. const char* name,
  50. CliCommandFlag flags,
  51. CliCallback callback,
  52. void* context);
  53. /** Print unified cmd usage tip
  54. *
  55. * @param cmd cmd name
  56. * @param usage usage tip
  57. * @param arg arg passed by user
  58. */
  59. void cli_print_usage(const char* cmd, const char* usage, const char* arg);
  60. /** Delete cli command
  61. *
  62. * @param cli pointer to cli instance
  63. * @param name command name
  64. */
  65. void cli_delete_command(Cli* cli, const char* name);
  66. /** Read from terminal
  67. *
  68. * @param cli Cli instance
  69. * @param buffer pointer to buffer
  70. * @param size size of buffer in bytes
  71. *
  72. * @return bytes read
  73. */
  74. size_t cli_read(Cli* cli, uint8_t* buffer, size_t size);
  75. /** Non-blocking read from terminal
  76. *
  77. * @param cli Cli instance
  78. * @param buffer pointer to buffer
  79. * @param size size of buffer in bytes
  80. * @param timeout timeout value in ms
  81. *
  82. * @return bytes read
  83. */
  84. size_t cli_read_timeout(Cli* cli, uint8_t* buffer, size_t size, uint32_t timeout);
  85. /** Non-blocking check for interrupt command received
  86. *
  87. * @param cli Cli instance
  88. *
  89. * @return true if received
  90. */
  91. bool cli_cmd_interrupt_received(Cli* cli);
  92. /** Write to terminal Do it only from inside of cli call.
  93. *
  94. * @param cli Cli instance
  95. * @param buffer pointer to buffer
  96. * @param size size of buffer in bytes
  97. */
  98. void cli_write(Cli* cli, const uint8_t* buffer, size_t size);
  99. /** Read character
  100. *
  101. * @param cli Cli instance
  102. *
  103. * @return char
  104. */
  105. char cli_getc(Cli* cli);
  106. /** New line Send new ine sequence
  107. */
  108. void cli_nl();
  109. void cli_session_open(Cli* cli, void* session);
  110. void cli_session_close(Cli* cli);
  111. bool cli_is_connected(Cli* cli);
  112. #ifdef __cplusplus
  113. }
  114. #endif