cli.c 6.7 KB

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