automation.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #define TOTP_CLI_COMMAND_AUTOMATION_ARG_INITIAL_DELAY_PREFIX "-w"
  20. static void print_method(AutomationMethod method, const char* color) {
  21. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  22. bool has_previous_method = false;
  23. #endif
  24. if(method & AutomationMethodBadUsb) {
  25. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"");
  26. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  27. has_previous_method = true;
  28. #endif
  29. }
  30. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  31. if(method & AutomationMethodBadBt) {
  32. if(has_previous_method) {
  33. TOTP_CLI_PRINTF_COLORFUL(color, " and ");
  34. }
  35. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "\"");
  36. }
  37. #endif
  38. if(method == AutomationMethodNone) {
  39. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "\"");
  40. }
  41. }
  42. static void print_kb_layout(AutomationKeyboardLayout layout, const char* color) {
  43. char* layoutToPrint;
  44. switch(layout) {
  45. case AutomationKeyboardLayoutQWERTY:
  46. layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY;
  47. break;
  48. case AutomationKeyboardLayoutAZERTY:
  49. layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY;
  50. break;
  51. case AutomationKeyboardLayoutQWERTZ:
  52. layoutToPrint = TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTZ;
  53. break;
  54. default:
  55. furi_crash("Unknown automation keyboard layout");
  56. break;
  57. }
  58. TOTP_CLI_PRINTF_COLORFUL(color, "%s", layoutToPrint);
  59. }
  60. static void print_initial_delay(uint16_t initial_delay, const char* color) {
  61. double delay_sec = initial_delay / 1000.0;
  62. TOTP_CLI_PRINTF_COLORFUL(color, "%.1f", delay_sec);
  63. }
  64. static bool
  65. parse_automation_keyboard_layout(const FuriString* str, AutomationKeyboardLayout* out) {
  66. bool result = true;
  67. if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY) == 0) {
  68. *out = AutomationKeyboardLayoutQWERTY;
  69. } else if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_AZERTY) == 0) {
  70. *out = AutomationKeyboardLayoutAZERTY;
  71. } else if(furi_string_cmpi_str(str, TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTZ) == 0) {
  72. *out = AutomationKeyboardLayoutQWERTZ;
  73. } else {
  74. result = false;
  75. }
  76. return result;
  77. }
  78. static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  79. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  80. return;
  81. }
  82. FuriString* temp_str = furi_string_alloc();
  83. bool new_method_provided = false;
  84. AutomationMethod new_method = AutomationMethodNone;
  85. AutomationKeyboardLayout new_kb_layout = plugin_state->automation_kb_layout;
  86. uint16_t new_initial_delay = plugin_state->automation_initial_delay;
  87. bool args_valid = true;
  88. while(args_read_string_and_trim(args, temp_str)) {
  89. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE) == 0) {
  90. new_method_provided = true;
  91. new_method = AutomationMethodNone;
  92. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB) == 0) {
  93. new_method_provided = true;
  94. new_method |= AutomationMethodBadUsb;
  95. }
  96. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  97. else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT) == 0) {
  98. new_method_provided = true;
  99. new_method |= AutomationMethodBadBt;
  100. }
  101. #endif
  102. else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_ARG_KB_LAYOUT_PREFIX) == 0) {
  103. if(!args_read_string_and_trim(args, temp_str) ||
  104. !parse_automation_keyboard_layout(temp_str, &new_kb_layout)) {
  105. args_valid = false;
  106. break;
  107. }
  108. } else if(
  109. furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_ARG_INITIAL_DELAY_PREFIX) ==
  110. 0) {
  111. float temp_float;
  112. char* strtof_endptr;
  113. if(args_read_string_and_trim(args, temp_str) &&
  114. (temp_float = strtof(furi_string_get_cstr(temp_str), &strtof_endptr)) >= 0 &&
  115. *strtof_endptr == 0) {
  116. new_initial_delay = (uint16_t)(temp_float * 1000.0f);
  117. } else {
  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 || new_kb_layout != plugin_state->automation_kb_layout ||
  132. new_initial_delay != plugin_state->automation_initial_delay) {
  133. TOTP_CLI_LOCK_UI(plugin_state);
  134. if(new_method_provided) {
  135. plugin_state->automation_method = new_method;
  136. }
  137. plugin_state->automation_kb_layout = new_kb_layout;
  138. plugin_state->automation_initial_delay = new_initial_delay;
  139. if(totp_config_file_update_automation_method(plugin_state)) {
  140. TOTP_CLI_PRINTF_SUCCESS("Automation method is set to ");
  141. print_method(plugin_state->automation_method, TOTP_CLI_COLOR_SUCCESS);
  142. TOTP_CLI_PRINTF_SUCCESS(" (");
  143. print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_SUCCESS);
  144. TOTP_CLI_PRINTF_SUCCESS(")");
  145. TOTP_CLI_PRINTF_SUCCESS(" [");
  146. print_initial_delay(
  147. plugin_state->automation_initial_delay, TOTP_CLI_COLOR_SUCCESS);
  148. TOTP_CLI_PRINTF_SUCCESS(" sec.]");
  149. cli_nl();
  150. } else {
  151. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  152. }
  153. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  154. if(!(new_method & AutomationMethodBadBt) &&
  155. plugin_state->bt_type_code_worker_context != NULL) {
  156. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  157. plugin_state->bt_type_code_worker_context = NULL;
  158. }
  159. #endif
  160. TOTP_CLI_UNLOCK_UI(plugin_state);
  161. } else {
  162. TOTP_CLI_PRINTF_INFO("Current automation method is ");
  163. print_method(plugin_state->automation_method, TOTP_CLI_COLOR_INFO);
  164. TOTP_CLI_PRINTF_INFO(" (");
  165. print_kb_layout(plugin_state->automation_kb_layout, TOTP_CLI_COLOR_INFO);
  166. TOTP_CLI_PRINTF_INFO(")");
  167. TOTP_CLI_PRINTF_INFO(" [");
  168. print_initial_delay(plugin_state->automation_initial_delay, TOTP_CLI_COLOR_INFO);
  169. TOTP_CLI_PRINTF_INFO(" sec.]");
  170. cli_nl();
  171. }
  172. } while(false);
  173. furi_string_free(temp_str);
  174. }
  175. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Automation", .handle = &handle};
  176. static const FlipperAppPluginDescriptor plugin_descriptor = {
  177. .appid = PLUGIN_APP_ID,
  178. .ep_api_version = PLUGIN_API_VERSION,
  179. .entry_point = &plugin,
  180. };
  181. const FlipperAppPluginDescriptor* totp_cli_automation_plugin_ep() {
  182. return &plugin_descriptor;
  183. }