cli.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. CliCommandDict_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. const Version* version;
  61. printf("Flipper cli.\r\n");
  62. version = (const Version*)api_hal_version_get_boot_version();
  63. printf("Bootloader\r\n");
  64. cli_print_version(version);
  65. version = (const Version*)api_hal_version_get_fw_version();
  66. printf("Firmware\r\n");
  67. cli_print_version(version);
  68. }
  69. void cli_nl() {
  70. printf("\r\n");
  71. }
  72. void cli_prompt() {
  73. printf("\r\n>: ");
  74. fflush(stdout);
  75. }
  76. void cli_backspace(Cli* cli) {
  77. size_t s = string_size(cli->line);
  78. if(s > 0) {
  79. s--;
  80. string_left(cli->line, s);
  81. cli_putc(CliSymbolAsciiBackspace);
  82. cli_putc(CliSymbolAsciiSpace);
  83. cli_putc(CliSymbolAsciiBackspace);
  84. } else {
  85. cli_putc(CliSymbolAsciiBell);
  86. }
  87. }
  88. void cli_enter(Cli* cli) {
  89. // Normalize input
  90. string_strim(cli->line);
  91. if(string_size(cli->line) == 0) {
  92. cli_prompt();
  93. return;
  94. }
  95. // Get first word as command name
  96. string_t command;
  97. string_init(command);
  98. size_t ws = string_search_char(cli->line, ' ');
  99. if(ws == STRING_FAILURE) {
  100. string_set(command, cli->line);
  101. string_clear(cli->line);
  102. string_init(cli->line);
  103. } else {
  104. string_set_n(command, cli->line, 0, ws);
  105. string_right(cli->line, ws);
  106. string_strim(cli->line);
  107. }
  108. // Search for command
  109. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  110. CliCommand* cli_command = CliCommandDict_get(cli->commands, command);
  111. furi_check(osMutexRelease(cli->mutex) == osOK);
  112. if(cli_command) {
  113. cli_nl();
  114. cli_command->callback(cli->line, cli_command->context);
  115. cli_prompt();
  116. } else {
  117. cli_nl();
  118. printf("Command not found: ");
  119. printf(string_get_cstr(command));
  120. cli_prompt();
  121. cli_putc(CliSymbolAsciiBell);
  122. }
  123. string_clear(command);
  124. // Always finish with clean state
  125. cli_reset_state(cli);
  126. }
  127. void cli_process_input(Cli* cli) {
  128. char c = cli_getc(cli);
  129. size_t r;
  130. if(c == CliSymbolAsciiTab) {
  131. cli_putc(CliSymbolAsciiBell);
  132. } else if(c == CliSymbolAsciiSOH) {
  133. cli_motd();
  134. cli_prompt();
  135. } else if(c == CliSymbolAsciiEOT) {
  136. cli_reset_state(cli);
  137. } else if(c == CliSymbolAsciiEsc) {
  138. r = api_hal_vcp_rx((uint8_t*)&c, 1);
  139. if(r && c == '[') {
  140. api_hal_vcp_rx((uint8_t*)&c, 1);
  141. } else {
  142. cli_putc(CliSymbolAsciiBell);
  143. }
  144. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  145. cli_backspace(cli);
  146. } else if(c == CliSymbolAsciiCR) {
  147. cli_enter(cli);
  148. } else if(c >= 0x20 && c < 0x7F) {
  149. string_push_back(cli->line, c);
  150. cli_putc(c);
  151. } else {
  152. cli_putc(CliSymbolAsciiBell);
  153. }
  154. }
  155. void cli_add_command(Cli* cli, const char* name, CliCallback callback, void* context) {
  156. string_t name_str;
  157. string_init_set_str(name_str, name);
  158. string_strim(name_str);
  159. size_t name_replace;
  160. do {
  161. name_replace = string_replace_str(name_str, " ", "_");
  162. } while(name_replace != STRING_FAILURE);
  163. CliCommand c;
  164. c.callback = callback;
  165. c.context = context;
  166. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  167. CliCommandDict_set_at(cli->commands, name_str, c);
  168. furi_check(osMutexRelease(cli->mutex) == osOK);
  169. string_clear(name_str);
  170. }
  171. void cli_delete_command(Cli* cli, const char* name) {
  172. string_t name_str;
  173. string_init_set_str(name_str, name);
  174. string_strim(name_str);
  175. size_t name_replace;
  176. do {
  177. name_replace = string_replace_str(name_str, " ", "_");
  178. } while(name_replace != STRING_FAILURE);
  179. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  180. CliCommandDict_erase(cli->commands, name_str);
  181. furi_check(osMutexRelease(cli->mutex) == osOK);
  182. string_clear(name_str);
  183. }
  184. int32_t cli_task(void* p) {
  185. Cli* cli = cli_alloc();
  186. // Init basic cli commands
  187. cli_commands_init(cli);
  188. furi_record_create("cli", cli);
  189. furi_stdglue_set_thread_stdout_callback(cli_stdout_callback);
  190. while(1) {
  191. cli_process_input(cli);
  192. }
  193. return 0;
  194. }