cli.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. CliCommandFlagInsomniaSafe = (1 << 1), /** Safe to run with insomnia mode on */
  24. } CliCommandFlag;
  25. /* Cli type
  26. * Anonymous structure. Use cli_i.h if you need to go deeper.
  27. */
  28. typedef struct Cli Cli;
  29. /* Cli callback function pointer.
  30. * Implement this interface and use add_cli_command
  31. * @param args - string with what was passed after command
  32. * @param context - pointer to whatever you gave us on cli_add_command
  33. */
  34. typedef void (*CliCallback)(Cli* cli, string_t args, void* context);
  35. /* Add cli command
  36. * Registers you command callback
  37. * @param cli - pointer to cli instance
  38. * @param name - command name
  39. * @param callback - callback function
  40. * @param context - pointer to whatever we need to pass to callback
  41. */
  42. void cli_add_command(
  43. Cli* cli,
  44. const char* name,
  45. CliCommandFlag flags,
  46. CliCallback callback,
  47. void* context);
  48. /* Print unified cmd usage tip
  49. * @param cmd - cmd name
  50. * @param usage - usage tip
  51. * @param arg - arg passed by user
  52. */
  53. void cli_print_usage(const char* cmd, const char* usage, const char* arg);
  54. /* Delete cli command
  55. * @param cli - pointer to cli instance
  56. * @param name - command name
  57. */
  58. void cli_delete_command(Cli* cli, const char* name);
  59. /* Read from terminal
  60. * Do it only from inside of cli call.
  61. * @param cli - Cli instance
  62. * @param buffer - pointer to buffer
  63. * @param size - size of buffer in bytes
  64. * @return bytes written
  65. */
  66. size_t cli_read(Cli* cli, uint8_t* buffer, size_t size);
  67. /* Not blocking check for interrupt command received
  68. * @param cli - Cli instance
  69. */
  70. bool cli_cmd_interrupt_received(Cli* cli);
  71. /* Write to terminal
  72. * Do it only from inside of cli call.
  73. * @param cli - Cli instance
  74. * @param buffer - pointer to buffer
  75. * @param size - size of buffer in bytes
  76. * @return bytes written
  77. */
  78. void cli_write(Cli* cli, uint8_t* buffer, size_t size);
  79. /* Read character
  80. * @param cli - Cli instance
  81. * @return char
  82. */
  83. char cli_getc(Cli* cli);
  84. /* New line
  85. * Send new ine sequence
  86. */
  87. void cli_nl();
  88. #ifdef __cplusplus
  89. }
  90. #endif