Преглед изворни кода

Updated firmware references (#88)

* * Updated firmware references

* Updated code to make it compatible with latest firmware changes

* Dropped useless check
Alexander Kopachov пре 2 година
родитељ
комит
97c6116225
3 измењених фајлова са 44 додато и 38 уклоњено
  1. 30 30
      totp_app.c
  2. 5 0
      types/plugin_state.h
  3. 9 8
      workers/type_code/type_code.c

+ 30 - 30
totp_app.c

@@ -23,12 +23,12 @@
 #define IDLE_TIMEOUT 60000
 #define IDLE_TIMEOUT 60000
 
 
 static void render_callback(Canvas* const canvas, void* ctx) {
 static void render_callback(Canvas* const canvas, void* ctx) {
-    PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
-    if(plugin_state != NULL) {
+    furi_assert(ctx);
+    PluginState* plugin_state = ctx;
+    if (furi_mutex_acquire(plugin_state->mutex, 25) == FuriStatusOk) {
         totp_scene_director_render(canvas, plugin_state);
         totp_scene_director_render(canvas, plugin_state);
+        furi_mutex_release(plugin_state->mutex);
     }
     }
-
-    release_mutex((ValueMutex*)ctx, plugin_state);
 }
 }
 
 
 static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
 static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
@@ -102,6 +102,12 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
         return false;
         return false;
     }
     }
 
 
+    plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
+    if (plugin_state->mutex == NULL) {
+        FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
+        return false;
+    }
+
     return true;
     return true;
 }
 }
 
 
@@ -123,6 +129,8 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
     if(plugin_state->crypto_verify_data != NULL) {
     if(plugin_state->crypto_verify_data != NULL) {
         free(plugin_state->crypto_verify_data);
         free(plugin_state->crypto_verify_data);
     }
     }
+
+    furi_mutex_free(plugin_state->mutex);
     free(plugin_state);
     free(plugin_state);
 }
 }
 
 
@@ -137,13 +145,6 @@ int32_t totp_app() {
         return 254;
         return 254;
     }
     }
 
 
-    ValueMutex state_mutex;
-    if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
-        FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
-        totp_plugin_state_free(plugin_state);
-        return 255;
-    }
-
     TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
     TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
     totp_scene_director_init_scenes(plugin_state);
     totp_scene_director_init_scenes(plugin_state);
     if(!totp_activate_initial_scene(plugin_state)) {
     if(!totp_activate_initial_scene(plugin_state)) {
@@ -157,7 +158,7 @@ int32_t totp_app() {
 
 
     // Set system callbacks
     // Set system callbacks
     ViewPort* view_port = view_port_alloc();
     ViewPort* view_port = view_port_alloc();
-    view_port_draw_callback_set(view_port, render_callback, &state_mutex);
+    view_port_draw_callback_set(view_port, render_callback, plugin_state);
     view_port_input_callback_set(view_port, input_callback, event_queue);
     view_port_input_callback_set(view_port, input_callback, event_queue);
 
 
     // Open GUI and register view_port
     // Open GUI and register view_port
@@ -169,26 +170,26 @@ int32_t totp_app() {
     while(processing) {
     while(processing) {
         FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
         FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
 
 
-        PluginState* plugin_state_m = acquire_mutex_block(&state_mutex);
-
-        if(event_status == FuriStatusOk) {
-            if(event.type == EventTypeKey) {
-                last_user_interaction_time = furi_get_tick();
+        if (furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
+            if(event_status == FuriStatusOk) {
+                if(event.type == EventTypeKey) {
+                    last_user_interaction_time = furi_get_tick();
+                }
+
+                if(event.type == EventForceCloseApp) {
+                    processing = false;
+                } else {
+                    processing = totp_scene_director_handle_event(&event, plugin_state);
+                }
+            } else if(
+                plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
+                furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
+                totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
             }
             }
 
 
-            if(event.type == EventForceCloseApp) {
-                processing = false;
-            } else {
-                processing = totp_scene_director_handle_event(&event, plugin_state_m);
-            }
-        } else if(
-            plugin_state_m->pin_set && plugin_state_m->current_scene != TotpSceneAuthentication &&
-            furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
-            totp_scene_director_activate_scene(plugin_state_m, TotpSceneAuthentication, NULL);
+            view_port_update(view_port);
+            furi_mutex_release(plugin_state->mutex);
         }
         }
-
-        view_port_update(view_port);
-        release_mutex(&state_mutex, plugin_state_m);
     }
     }
 
 
     totp_cli_unregister_command_handler(cli_context);
     totp_cli_unregister_command_handler(cli_context);
@@ -199,7 +200,6 @@ int32_t totp_app() {
     gui_remove_view_port(plugin_state->gui, view_port);
     gui_remove_view_port(plugin_state->gui, view_port);
     view_port_free(view_port);
     view_port_free(view_port);
     furi_message_queue_free(event_queue);
     furi_message_queue_free(event_queue);
-    delete_mutex(&state_mutex);
     totp_plugin_state_free(plugin_state);
     totp_plugin_state_free(plugin_state);
     return 0;
     return 0;
 }
 }

+ 5 - 0
types/plugin_state.h

@@ -87,4 +87,9 @@ typedef struct {
      * @brief Notification method
      * @brief Notification method
      */
      */
     NotificationMethod notification_method;
     NotificationMethod notification_method;
+
+    /**
+     * @brief Main rendering loop mutex
+     */
+    FuriMutex* mutex;
 } PluginState;
 } PluginState;

+ 9 - 8
workers/type_code/type_code.c

@@ -57,8 +57,8 @@ static void totp_type_code_worker_type_code(TotpTypeCodeWorkerContext* context)
 }
 }
 
 
 static int32_t totp_type_code_worker_callback(void* context) {
 static int32_t totp_type_code_worker_callback(void* context) {
-    ValueMutex context_mutex;
-    if(!init_mutex(&context_mutex, context, sizeof(TotpTypeCodeWorkerContext))) {
+    FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
+    if(context_mutex == NULL) {
         return 251;
         return 251;
     }
     }
 
 
@@ -70,15 +70,16 @@ static int32_t totp_type_code_worker_callback(void* context) {
         furi_check((flags & FuriFlagError) == 0); //-V562
         furi_check((flags & FuriFlagError) == 0); //-V562
         if(flags & TotpTypeCodeWorkerEventStop) break;
         if(flags & TotpTypeCodeWorkerEventStop) break;
 
 
-        TotpTypeCodeWorkerContext* h_context = acquire_mutex_block(&context_mutex);
-        if(flags & TotpTypeCodeWorkerEventType) {
-            totp_type_code_worker_type_code(h_context);
-        }
+        if (furi_mutex_acquire(context_mutex, FuriWaitForever) == FuriStatusOk) {
+            if(flags & TotpTypeCodeWorkerEventType) {
+                totp_type_code_worker_type_code(context);
+            }
 
 
-        release_mutex(&context_mutex, h_context);
+            furi_mutex_release(context_mutex);
+        }
     }
     }
 
 
-    delete_mutex(&context_mutex);
+    furi_mutex_free(context_mutex);
 
 
     return 0;
     return 0;
 }
 }