23
23
#define IDLE_TIMEOUT 60000
24
24
25
25
static void render_callback (Canvas * const canvas , void * ctx ) {
26
- PluginState * plugin_state = acquire_mutex ((ValueMutex * )ctx , 25 );
27
- if (plugin_state != NULL ) {
26
+ furi_assert (ctx );
27
+ PluginState * plugin_state = ctx ;
28
+ if (furi_mutex_acquire (plugin_state -> mutex , 25 ) == FuriStatusOk ) {
28
29
totp_scene_director_render (canvas , plugin_state );
30
+ furi_mutex_release (plugin_state -> mutex );
29
31
}
30
-
31
- release_mutex ((ValueMutex * )ctx , plugin_state );
32
32
}
33
33
34
34
static void input_callback (InputEvent * input_event , FuriMessageQueue * event_queue ) {
@@ -102,6 +102,12 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
102
102
return false;
103
103
}
104
104
105
+ plugin_state -> mutex = furi_mutex_alloc (FuriMutexTypeNormal );
106
+ if (plugin_state -> mutex == NULL ) {
107
+ FURI_LOG_E (LOGGING_TAG , "Cannot create mutex\r\n" );
108
+ return false;
109
+ }
110
+
105
111
return true;
106
112
}
107
113
@@ -123,6 +129,8 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
123
129
if (plugin_state -> crypto_verify_data != NULL ) {
124
130
free (plugin_state -> crypto_verify_data );
125
131
}
132
+
133
+ furi_mutex_free (plugin_state -> mutex );
126
134
free (plugin_state );
127
135
}
128
136
@@ -137,13 +145,6 @@ int32_t totp_app() {
137
145
return 254 ;
138
146
}
139
147
140
- ValueMutex state_mutex ;
141
- if (!init_mutex (& state_mutex , plugin_state , sizeof (PluginState ))) {
142
- FURI_LOG_E (LOGGING_TAG , "Cannot create mutex\r\n" );
143
- totp_plugin_state_free (plugin_state );
144
- return 255 ;
145
- }
146
-
147
148
TotpCliContext * cli_context = totp_cli_register_command_handler (plugin_state , event_queue );
148
149
totp_scene_director_init_scenes (plugin_state );
149
150
if (!totp_activate_initial_scene (plugin_state )) {
@@ -157,7 +158,7 @@ int32_t totp_app() {
157
158
158
159
// Set system callbacks
159
160
ViewPort * view_port = view_port_alloc ();
160
- view_port_draw_callback_set (view_port , render_callback , & state_mutex );
161
+ view_port_draw_callback_set (view_port , render_callback , plugin_state );
161
162
view_port_input_callback_set (view_port , input_callback , event_queue );
162
163
163
164
// Open GUI and register view_port
@@ -169,26 +170,26 @@ int32_t totp_app() {
169
170
while (processing ) {
170
171
FuriStatus event_status = furi_message_queue_get (event_queue , & event , 100 );
171
172
172
- PluginState * plugin_state_m = acquire_mutex_block (& state_mutex );
173
-
174
- if (event_status == FuriStatusOk ) {
175
- if (event .type == EventTypeKey ) {
176
- last_user_interaction_time = furi_get_tick ();
173
+ if (furi_mutex_acquire (plugin_state -> mutex , FuriWaitForever ) == FuriStatusOk ) {
174
+ if (event_status == FuriStatusOk ) {
175
+ if (event .type == EventTypeKey ) {
176
+ last_user_interaction_time = furi_get_tick ();
177
+ }
178
+
179
+ if (event .type == EventForceCloseApp ) {
180
+ processing = false;
181
+ } else {
182
+ processing = totp_scene_director_handle_event (& event , plugin_state );
183
+ }
184
+ } else if (
185
+ plugin_state -> pin_set && plugin_state -> current_scene != TotpSceneAuthentication &&
186
+ furi_get_tick () - last_user_interaction_time > IDLE_TIMEOUT ) {
187
+ totp_scene_director_activate_scene (plugin_state , TotpSceneAuthentication , NULL );
177
188
}
178
189
179
- if (event .type == EventForceCloseApp ) {
180
- processing = false;
181
- } else {
182
- processing = totp_scene_director_handle_event (& event , plugin_state_m );
183
- }
184
- } else if (
185
- plugin_state_m -> pin_set && plugin_state_m -> current_scene != TotpSceneAuthentication &&
186
- furi_get_tick () - last_user_interaction_time > IDLE_TIMEOUT ) {
187
- totp_scene_director_activate_scene (plugin_state_m , TotpSceneAuthentication , NULL );
190
+ view_port_update (view_port );
191
+ furi_mutex_release (plugin_state -> mutex );
188
192
}
189
-
190
- view_port_update (view_port );
191
- release_mutex (& state_mutex , plugin_state_m );
192
193
}
193
194
194
195
totp_cli_unregister_command_handler (cli_context );
@@ -199,7 +200,6 @@ int32_t totp_app() {
199
200
gui_remove_view_port (plugin_state -> gui , view_port );
200
201
view_port_free (view_port );
201
202
furi_message_queue_free (event_queue );
202
- delete_mutex (& state_mutex );
203
203
totp_plugin_state_free (plugin_state );
204
204
return 0 ;
205
205
}
0 commit comments