cli.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "cli_i.h"
  2. #include "cli_commands.h"
  3. #include <version.h>
  4. #include <api-hal-version.h>
  5. Cli* cli_alloc() {
  6. Cli* cli = furi_alloc(sizeof(Cli));
  7. CliCommandTree_init(cli->commands);
  8. cli->mutex = osMutexNew(NULL);
  9. furi_check(cli->mutex);
  10. cli_reset_state(cli);
  11. return cli;
  12. }
  13. void cli_free(Cli* cli) {
  14. free(cli);
  15. }
  16. void cli_reset_state(Cli* cli) {
  17. // Release allocated buffer, reset state
  18. string_clear(cli->line);
  19. string_init(cli->line);
  20. }
  21. void cli_putc(char c) {
  22. api_hal_vcp_tx((uint8_t*)&c, 1);
  23. }
  24. char cli_getc(Cli* cli) {
  25. furi_assert(cli);
  26. char c;
  27. if(api_hal_vcp_rx((uint8_t*)&c, 1) == 0) {
  28. cli_reset_state(cli);
  29. }
  30. return c;
  31. }
  32. void cli_stdout_callback(void* _cookie, const char* data, size_t size) {
  33. api_hal_vcp_tx((const uint8_t*)data, size);
  34. }
  35. void cli_write(Cli* cli, uint8_t* buffer, size_t size) {
  36. return api_hal_vcp_tx(buffer, size);
  37. }
  38. size_t cli_read(Cli* cli, uint8_t* buffer, size_t size) {
  39. return api_hal_vcp_rx(buffer, size);
  40. }
  41. bool cli_cmd_interrupt_received(Cli* cli) {
  42. char c;
  43. api_hal_vcp_rx_with_timeout((uint8_t*)&c, 1, 1);
  44. return c == CliSymbolAsciiETX;
  45. }
  46. void cli_print_version(const Version* version) {
  47. if(version) {
  48. printf("\tVersion:\t%s\r\n", version_get_version(version));
  49. printf("\tBuild date:\t%s\r\n", version_get_builddate(version));
  50. printf(
  51. "\tGit Commit:\t%s (%s)\r\n",
  52. version_get_githash(version),
  53. version_get_gitbranchnum(version));
  54. printf("\tGit Branch:\t%s\r\n", version_get_gitbranch(version));
  55. } else {
  56. printf("\tNo build info\r\n");
  57. }
  58. }
  59. void cli_motd() {
  60. printf("Flipper cli.\r\n");
  61. printf("Bootloader\r\n");
  62. cli_print_version(api_hal_version_get_boot_version());
  63. printf("Firmware\r\n");
  64. cli_print_version(api_hal_version_get_fw_version());
  65. }
  66. void cli_nl() {
  67. printf("\r\n");
  68. }
  69. void cli_prompt() {
  70. printf("\r\n>: ");
  71. fflush(stdout);
  72. }
  73. void cli_backspace(Cli* cli) {
  74. size_t s = string_size(cli->line);
  75. if(s > 0) {
  76. s--;
  77. string_left(cli->line, s);
  78. cli_putc(CliSymbolAsciiBackspace);
  79. cli_putc(CliSymbolAsciiSpace);
  80. cli_putc(CliSymbolAsciiBackspace);
  81. } else {
  82. cli_putc(CliSymbolAsciiBell);
  83. }
  84. }
  85. void cli_enter(Cli* cli) {
  86. // Normalize input
  87. string_strim(cli->line);
  88. if(string_size(cli->line) == 0) {
  89. cli_prompt();
  90. return;
  91. }
  92. // Get first word as command name
  93. string_t command;
  94. string_init(command);
  95. size_t ws = string_search_char(cli->line, ' ');
  96. if(ws == STRING_FAILURE) {
  97. string_set(command, cli->line);
  98. string_clear(cli->line);
  99. string_init(cli->line);
  100. } else {
  101. string_set_n(command, cli->line, 0, ws);
  102. string_right(cli->line, ws);
  103. string_strim(cli->line);
  104. }
  105. // Search for command
  106. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  107. CliCommand* cli_command = CliCommandTree_get(cli->commands, command);
  108. furi_check(osMutexRelease(cli->mutex) == osOK);
  109. if(cli_command) {
  110. cli_nl();
  111. cli_command->callback(cli, cli->line, cli_command->context);
  112. cli_prompt();
  113. } else {
  114. cli_nl();
  115. printf("Command not found: ");
  116. printf(string_get_cstr(command));
  117. cli_prompt();
  118. cli_putc(CliSymbolAsciiBell);
  119. }
  120. string_clear(command);
  121. // Always finish with clean state
  122. cli_reset_state(cli);
  123. }
  124. void cli_process_input(Cli* cli) {
  125. char c = cli_getc(cli);
  126. size_t r;
  127. if(c == CliSymbolAsciiTab) {
  128. cli_putc(CliSymbolAsciiBell);
  129. } else if(c == CliSymbolAsciiSOH) {
  130. cli_motd();
  131. cli_prompt();
  132. } else if(c == CliSymbolAsciiEOT) {
  133. cli_reset_state(cli);
  134. } else if(c == CliSymbolAsciiEsc) {
  135. r = api_hal_vcp_rx((uint8_t*)&c, 1);
  136. if(r && c == '[') {
  137. api_hal_vcp_rx((uint8_t*)&c, 1);
  138. } else {
  139. cli_putc(CliSymbolAsciiBell);
  140. }
  141. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  142. cli_backspace(cli);
  143. } else if(c == CliSymbolAsciiCR) {
  144. cli_enter(cli);
  145. } else if(c >= 0x20 && c < 0x7F) {
  146. string_push_back(cli->line, c);
  147. cli_putc(c);
  148. } else {
  149. cli_putc(CliSymbolAsciiBell);
  150. }
  151. }
  152. void cli_add_command(Cli* cli, const char* name, CliCallback callback, void* context) {
  153. string_t name_str;
  154. string_init_set_str(name_str, name);
  155. string_strim(name_str);
  156. size_t name_replace;
  157. do {
  158. name_replace = string_replace_str(name_str, " ", "_");
  159. } while(name_replace != STRING_FAILURE);
  160. CliCommand c;
  161. c.callback = callback;
  162. c.context = context;
  163. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  164. CliCommandTree_set_at(cli->commands, name_str, c);
  165. furi_check(osMutexRelease(cli->mutex) == osOK);
  166. string_clear(name_str);
  167. }
  168. void cli_delete_command(Cli* cli, const char* name) {
  169. string_t name_str;
  170. string_init_set_str(name_str, name);
  171. string_strim(name_str);
  172. size_t name_replace;
  173. do {
  174. name_replace = string_replace_str(name_str, " ", "_");
  175. } while(name_replace != STRING_FAILURE);
  176. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  177. CliCommandTree_erase(cli->commands, name_str);
  178. furi_check(osMutexRelease(cli->mutex) == osOK);
  179. string_clear(name_str);
  180. }
  181. int32_t cli_task(void* p) {
  182. Cli* cli = cli_alloc();
  183. // Init basic cli commands
  184. cli_commands_init(cli);
  185. furi_record_create("cli", cli);
  186. furi_stdglue_set_thread_stdout_callback(cli_stdout_callback);
  187. while(1) {
  188. cli_process_input(cli);
  189. }
  190. return 0;
  191. }