cli.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. CliSymbolAsciiCR = 0x0D,
  18. CliSymbolAsciiEsc = 0x1B,
  19. CliSymbolAsciiUS = 0x1F,
  20. CliSymbolAsciiSpace = 0x20,
  21. CliSymbolAsciiDel = 0x7F,
  22. } CliSymbols;
  23. typedef enum {
  24. CliCommandFlagDefault = 0, /**< Default, loader lock is used */
  25. CliCommandFlagParallelSafe =
  26. (1 << 0), /**< Safe to run in parallel with other apps, loader lock is not used */
  27. CliCommandFlagInsomniaSafe = (1 << 1), /**< Safe to run with insomnia mode on */
  28. } CliCommandFlag;
  29. /** Cli type anonymous structure */
  30. typedef struct Cli Cli;
  31. /** Cli callback function pointer. Implement this interface and use
  32. * add_cli_command
  33. * @param args string with what was passed after command
  34. * @param context pointer to whatever you gave us on cli_add_command
  35. */
  36. typedef void (*CliCallback)(Cli* cli, string_t args, void* context);
  37. /** Add cli command Registers you command callback
  38. *
  39. * @param cli pointer to cli instance
  40. * @param name command name
  41. * @param flags CliCommandFlag
  42. * @param callback callback function
  43. * @param context pointer to whatever we need to pass to callback
  44. */
  45. void cli_add_command(
  46. Cli* cli,
  47. const char* name,
  48. CliCommandFlag flags,
  49. CliCallback callback,
  50. void* context);
  51. /** Print unified cmd usage tip
  52. *
  53. * @param cmd cmd name
  54. * @param usage usage tip
  55. * @param arg arg passed by user
  56. */
  57. void cli_print_usage(const char* cmd, const char* usage, const char* arg);
  58. /** Delete cli command
  59. *
  60. * @param cli pointer to cli instance
  61. * @param name command name
  62. */
  63. void cli_delete_command(Cli* cli, const char* name);
  64. /** Read from terminal Do it only from inside of cli call.
  65. *
  66. * @param cli Cli instance
  67. * @param buffer pointer to buffer
  68. * @param size size of buffer in bytes
  69. *
  70. * @return bytes written
  71. */
  72. size_t cli_read(Cli* cli, uint8_t* buffer, size_t size);
  73. /** Not blocking check for interrupt command received
  74. *
  75. * @param cli Cli instance
  76. *
  77. * @return true if received
  78. */
  79. bool cli_cmd_interrupt_received(Cli* cli);
  80. /** Write to terminal Do it only from inside of cli call.
  81. *
  82. * @param cli Cli instance
  83. * @param buffer pointer to buffer
  84. * @param size size of buffer in bytes
  85. */
  86. void cli_write(Cli* cli, const uint8_t* buffer, size_t size);
  87. /** Read character
  88. *
  89. * @param cli Cli instance
  90. *
  91. * @return char
  92. */
  93. char cli_getc(Cli* cli);
  94. /** New line Send new ine sequence
  95. */
  96. void cli_nl();
  97. #ifdef __cplusplus
  98. }
  99. #endif