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
+
7
+ #define TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD "automation"
8
+ #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "none"
9
+ #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "usb"
10
+ #ifdef TOTP_BADBT_TYPE_ENABLED
11
+ #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
12
+ #endif
13
+
14
+ void totp_cli_command_automation_docopt_commands () {
15
+ TOTP_CLI_PRINTF (" " TOTP_CLI_COMMAND_AUTOMATION " Get or set automation method\r\n" );
16
+ }
17
+
18
+ void totp_cli_command_automation_docopt_usage () {
19
+ TOTP_CLI_PRINTF (" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_AUTOMATION " " DOCOPT_OPTIONAL (
20
+ DOCOPT_MULTIPLE (DOCOPT_ARGUMENT (TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD ))) "\r\n" );
21
+ }
22
+
23
+ void totp_cli_command_automation_docopt_arguments () {
24
+ TOTP_CLI_PRINTF (
25
+ " " TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD
26
+ " Automation method to be set. Must be one of [" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE
27
+ ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB
28
+ #ifdef TOTP_BADBT_TYPE_ENABLED
29
+ ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT
30
+ #endif
31
+ "]\r\n" );
32
+ }
33
+
34
+ static void totp_cli_command_automation_print_method (AutomationMethod method , char * color ) {
35
+ #ifdef TOTP_BADBT_TYPE_ENABLED
36
+ bool has_previous_method = false;
37
+ #endif
38
+ if (method & AutomationMethodBadUsb ) {
39
+ TOTP_CLI_PRINTF_COLORFUL (color , "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"" );
40
+ #ifdef TOTP_BADBT_TYPE_ENABLED
41
+ has_previous_method = true;
42
+ #endif
43
+ }
44
+
45
+ #ifdef TOTP_BADBT_TYPE_ENABLED
46
+ if (method & AutomationMethodBadBt ) {
47
+ if (has_previous_method ) {
48
+ TOTP_CLI_PRINTF_COLORFUL (color , " and " );
49
+ }
50
+
51
+ TOTP_CLI_PRINTF_COLORFUL (color , "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "\"" );
52
+ }
53
+ #endif
54
+
55
+ if (method == AutomationMethodNone ) {
56
+ TOTP_CLI_PRINTF_COLORFUL (color , "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "\"" );
57
+ }
58
+ }
59
+
60
+ void totp_cli_command_automation_handle (PluginState * plugin_state , FuriString * args , Cli * cli ) {
61
+ if (!totp_cli_ensure_authenticated (plugin_state , cli )) {
62
+ return ;
63
+ }
64
+
65
+ FuriString * temp_str = furi_string_alloc ();
66
+ bool new_method_provided = false;
67
+ AutomationMethod new_method = AutomationMethodNone ;
68
+ bool args_valid = true;
69
+ while (args_read_string_and_trim (args , temp_str )) {
70
+ if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE ) == 0 ) {
71
+ new_method_provided = true;
72
+ new_method = AutomationMethodNone ;
73
+ } else if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB ) == 0 ) {
74
+ new_method_provided = true;
75
+ new_method |= AutomationMethodBadUsb ;
76
+ }
77
+ #ifdef TOTP_BADBT_TYPE_ENABLED
78
+ else if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT ) == 0 ) {
79
+ new_method_provided = true;
80
+ new_method |= AutomationMethodBadBt ;
81
+ }
82
+ #endif
83
+ else {
84
+ args_valid = false;
85
+ break ;
86
+ }
87
+ }
88
+
89
+ do {
90
+ if (!args_valid ) {
91
+ TOTP_CLI_PRINT_INVALID_ARGUMENTS ();
92
+ break ;
93
+ }
94
+
95
+ if (new_method_provided ) {
96
+ Scene previous_scene = TotpSceneNone ;
97
+ if (plugin_state -> current_scene == TotpSceneGenerateToken ||
98
+ plugin_state -> current_scene == TotpSceneAppSettings ) {
99
+ previous_scene = plugin_state -> current_scene ;
100
+ totp_scene_director_activate_scene (plugin_state , TotpSceneNone , NULL );
101
+ }
102
+
103
+ plugin_state -> automation_method = new_method ;
104
+ if (totp_config_file_update_automation_method (new_method ) ==
105
+ TotpConfigFileUpdateSuccess ) {
106
+ TOTP_CLI_PRINTF_SUCCESS ("Automation method is set to " );
107
+ totp_cli_command_automation_print_method (new_method , TOTP_CLI_COLOR_SUCCESS );
108
+ cli_nl ();
109
+ } else {
110
+ TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE ();
111
+ }
112
+
113
+ #ifdef TOTP_BADBT_TYPE_ENABLED
114
+ if (!(new_method & AutomationMethodBadBt ) &&
115
+ plugin_state -> bt_type_code_worker_context != NULL ) {
116
+ totp_bt_type_code_worker_free (plugin_state -> bt_type_code_worker_context );
117
+ plugin_state -> bt_type_code_worker_context = NULL ;
118
+ }
119
+ #endif
120
+
121
+ if (previous_scene != TotpSceneNone ) {
122
+ totp_scene_director_activate_scene (plugin_state , previous_scene , NULL );
123
+ }
124
+ } else {
125
+ TOTP_CLI_PRINTF_INFO ("Current automation method is " );
126
+ totp_cli_command_automation_print_method (
127
+ plugin_state -> automation_method , TOTP_CLI_COLOR_INFO );
128
+ cli_nl ();
129
+ }
130
+ } while (false);
131
+
132
+ furi_string_free (temp_str );
133
+ }
0 commit comments