automation.c 6.2 KB

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