automation.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "automation.h"
  2. #include <lib/toolbox/args.h>
  3. #include "../../../services/config/config.h"
  4. #include "../../../ui/scene_director.h"
  5. #include "../../cli_helpers.h"
  6. #define TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD "automation"
  7. #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "none"
  8. #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "usb"
  9. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  10. #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
  11. #endif
  12. #define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY "QWERTY"
  13. #define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY "AZERTY"
  14. #define TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX "-k"
  15. #define TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT "layout"
  16. void totp_cli_command_automation_docopt_commands() {
  17. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_AUTOMATION " Get or set automation settings\r\n");
  18. }
  19. void totp_cli_command_automation_docopt_usage() {
  20. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_AUTOMATION " " DOCOPT_OPTIONAL(DOCOPT_OPTION(
  21. TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX,
  22. DOCOPT_ARGUMENT(
  23. TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT))) " " DOCOPT_OPTIONAL(DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD))) "\r\n");
  24. }
  25. void totp_cli_command_automation_docopt_arguments() {
  26. TOTP_CLI_PRINTF(
  27. " " TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD
  28. " Automation method to be set. Must be one of: " TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE
  29. ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB
  30. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  31. ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT
  32. #endif
  33. "\r\n");
  34. }
  35. void totp_cli_command_automation_docopt_options() {
  36. TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
  37. TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX,
  38. DOCOPT_ARGUMENT(
  39. TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT)) " Automation keyboard layout. Must be one of: " TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY
  40. ", " TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY
  41. "\r\n");
  42. }
  43. static void print_method(AutomationMethod method, const char* color) {
  44. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  45. bool has_previous_method = false;
  46. #endif
  47. if(method & AutomationMethodBadUsb) {
  48. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"");
  49. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  50. has_previous_method = true;
  51. #endif
  52. }
  53. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  54. if(method & AutomationMethodBadBt) {
  55. if(has_previous_method) {
  56. TOTP_CLI_PRINTF_COLORFUL(color, " and ");
  57. }
  58. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "\"");
  59. }
  60. #endif
  61. if(method == AutomationMethodNone) {
  62. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "\"");
  63. }
  64. }
  65. static void print_kb_layout(AutomationKeyboardLayout layout, const char* color) {
  66. char* layoutToPrint;
  67. switch(layout) {
  68. case AutomationKeyboardLayoutQWERTY:
  69. layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY;
  70. break;
  71. case AutomationKeyboardLayoutAZERTY:
  72. layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY;
  73. break;
  74. default:
  75. furi_crash("Unknown automation keyboard layout");
  76. break;
  77. }
  78. TOTP_CLI_PRINTF_COLORFUL(color, "%s", layoutToPrint);
  79. }
  80. static bool
  81. parse_automation_keyboard_layout(const FuriString* str, AutomationKeyboardLayout* out) {
  82. bool result = true;
  83. if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY) == 0) {
  84. *out = AutomationKeyboardLayoutQWERTY;
  85. } else if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY) == 0) {
  86. *out = AutomationKeyboardLayoutAZERTY;
  87. } else {
  88. result = false;
  89. }
  90. return result;
  91. }
  92. void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  93. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  94. return;
  95. }
  96. FuriString* temp_str = furi_string_alloc();
  97. bool new_method_provided = false;
  98. AutomationMethod new_method = AutomationMethodNone;
  99. AutomationKeyboardLayout new_kb_layout = plugin_state->automation_kb_layout;
  100. bool args_valid = true;
  101. while(args_read_string_and_trim(args, temp_str)) {
  102. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE) == 0) {
  103. new_method_provided = true;
  104. new_method = AutomationMethodNone;
  105. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB) == 0) {
  106. new_method_provided = true;
  107. new_method |= AutomationMethodBadUsb;
  108. }
  109. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  110. else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT) == 0) {
  111. new_method_provided = true;
  112. new_method |= AutomationMethodBadBt;
  113. }
  114. #endif
  115. else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX) == 0) {
  116. if(!args_read_string_and_trim(args, temp_str) ||
  117. !parse_automation_keyboard_layout(temp_str, &new_kb_layout)) {
  118. args_valid = false;
  119. break;
  120. }
  121. } else {
  122. args_valid = false;
  123. break;
  124. }
  125. }
  126. do {
  127. if(!args_valid) {
  128. totp_cli_print_invalid_arguments();
  129. break;
  130. }
  131. if(new_method_provided) {
  132. TOTP_CLI_LOCK_UI(plugin_state);
  133. plugin_state->automation_method = new_method;
  134. plugin_state->automation_kb_layout = new_kb_layout;
  135. if(totp_config_file_update_automation_method(plugin_state)) {
  136. TOTP_CLI_PRINTF_SUCCESS("Automation method is set to ");
  137. print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
  138. TOTP_CLI_PRINTF_SUCCESS(" (");
  139. print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_SUCCESS);
  140. TOTP_CLI_PRINTF_SUCCESS(")");
  141. cli_nl();
  142. } else {
  143. totp_cli_print_error_updating_config_file();
  144. }
  145. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  146. if(!(new_method & AutomationMethodBadBt) &&
  147. plugin_state->bt_type_code_worker_context != NULL) {
  148. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  149. plugin_state->bt_type_code_worker_context = NULL;
  150. }
  151. #endif
  152. TOTP_CLI_UNLOCK_UI(plugin_state);
  153. } else {
  154. TOTP_CLI_PRINTF_INFO("Current automation method is ");
  155. print_method(plugin_state->automation_method, TOTP_CLI_COLOR_INFO);
  156. TOTP_CLI_PRINTF_INFO(" (");
  157. print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_INFO);
  158. TOTP_CLI_PRINTF_INFO(")");
  159. cli_nl();
  160. }
  161. } while(false);
  162. furi_string_free(temp_str);
  163. }