cli.h 2.5 KB

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