automation.c 6.7 KB

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