cli.c 7.0 KB

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