diff --git a/applications/plugins/arkanoid/arkanoid_game.c b/applications/plugins/arkanoid/arkanoid_game.c index 0b9bb91e9..a8b846579 100644 --- a/applications/plugins/arkanoid/arkanoid_game.c +++ b/applications/plugins/arkanoid/arkanoid_game.c @@ -38,6 +38,7 @@ typedef struct { } BallState; typedef struct { + FuriMutex* mutex; BallState ball_state; BrickState brick_state; NotificationApp* notify; @@ -310,10 +311,9 @@ static void arkanoid_state_init(ArkanoidState* arkanoid_state) { } static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) { - ArkanoidState* arkanoid_state = acquire_mutex((ValueMutex*)ctx, 25); - if(arkanoid_state == NULL) { - return; - } + furi_assert(ctx); + ArkanoidState* arkanoid_state = ctx; + furi_mutex_acquire(arkanoid_state->mutex, FuriWaitForever); //Initial level draw if(!arkanoid_state->initialDraw) { @@ -352,7 +352,7 @@ static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) { arkanoid_state->score = 0; } - release_mutex((ValueMutex*)ctx, arkanoid_state); + furi_mutex_release(arkanoid_state->mutex); } static void arkanoid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -378,8 +378,8 @@ int32_t arkanoid_game_app(void* p) { ArkanoidState* arkanoid_state = malloc(sizeof(ArkanoidState)); arkanoid_state_init(arkanoid_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, arkanoid_state, sizeof(ArkanoidState))) { + arkanoid_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!arkanoid_state->mutex) { FURI_LOG_E(TAG, "Cannot create mutex\r\n"); return_code = 255; goto free_and_exit; @@ -387,7 +387,7 @@ int32_t arkanoid_game_app(void* p) { // Set system callbacks ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, arkanoid_draw_callback, &state_mutex); + view_port_draw_callback_set(view_port, arkanoid_draw_callback, arkanoid_state); view_port_input_callback_set(view_port, arkanoid_input_callback, event_queue); FuriTimer* timer = @@ -404,7 +404,7 @@ int32_t arkanoid_game_app(void* p) { GameEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - ArkanoidState* arkanoid_state = (ArkanoidState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(arkanoid_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // Key events @@ -461,7 +461,7 @@ int32_t arkanoid_game_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, arkanoid_state); + furi_mutex_release(arkanoid_state->mutex); } furi_timer_free(timer); view_port_enabled_set(view_port, false); @@ -469,7 +469,7 @@ int32_t arkanoid_game_app(void* p) { furi_record_close(RECORD_GUI); furi_record_close(RECORD_NOTIFICATION); view_port_free(view_port); - delete_mutex(&state_mutex); + furi_mutex_free(arkanoid_state->mutex); free_and_exit: free(arkanoid_state); diff --git a/applications/plugins/barcode_generator/barcode_generator.c b/applications/plugins/barcode_generator/barcode_generator.c index cbbb41671..2645bbcea 100644 --- a/applications/plugins/barcode_generator/barcode_generator.c +++ b/applications/plugins/barcode_generator/barcode_generator.c @@ -130,10 +130,9 @@ int calculate_check_digit(PluginState* plugin_state, BarcodeType* type) { } static void render_callback(Canvas* const canvas, void* ctx) { - PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } + furi_assert(ctx); + PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(plugin_state->mode == MenuMode) { canvas_set_color(canvas, ColorBlack); @@ -239,7 +238,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas_draw_box(canvas, (endSafetyPosition + 2), BARCODE_Y_START, 1, BARCODE_HEIGHT + 2); } - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -387,8 +386,9 @@ int32_t barcode_generator_app(void* p) { PluginState* plugin_state = malloc(sizeof(PluginState)); barcode_generator_state_init(plugin_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E("barcode_generator", "cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(plugin_state); @@ -397,7 +397,7 @@ int32_t barcode_generator_app(void* p) { // Set system callbacks 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); // Open GUI and register view_port @@ -407,7 +407,7 @@ int32_t barcode_generator_app(void* p) { PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -430,7 +430,7 @@ int32_t barcode_generator_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } view_port_enabled_set(view_port, false); @@ -438,6 +438,7 @@ int32_t barcode_generator_app(void* p) { furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); + furi_mutex_free(plugin_state->mutex); // save settings SAVE_BARCODE_SETTINGS(&plugin_state->barcode_state); free(plugin_state); diff --git a/applications/plugins/barcode_generator/barcode_generator.h b/applications/plugins/barcode_generator/barcode_generator.h index b39e43320..5d2c8307e 100644 --- a/applications/plugins/barcode_generator/barcode_generator.h +++ b/applications/plugins/barcode_generator/barcode_generator.h @@ -81,6 +81,7 @@ typedef struct { } BarcodeState; typedef struct { + FuriMutex* mutex; BarcodeState barcode_state; int editingIndex; //The index of the editing symbol int menuIndex; //The index of the menu cursor diff --git a/applications/plugins/blackjack/blackjack.c b/applications/plugins/blackjack/blackjack.c index 7e62ec539..95f9c72bb 100644 --- a/applications/plugins/blackjack/blackjack.c +++ b/applications/plugins/blackjack/blackjack.c @@ -33,11 +33,9 @@ static void draw_ui(Canvas* const canvas, const GameState* game_state) { } static void render_callback(Canvas* const canvas, void* ctx) { - const GameState* game_state = acquire_mutex((ValueMutex*)ctx, 25); - - if(game_state == NULL) { - return; - } + furi_assert(ctx); + const GameState* game_state = ctx; + furi_mutex_acquire(game_state->mutex, FuriWaitForever); canvas_set_color(canvas, ColorBlack); canvas_draw_frame(canvas, 0, 0, 128, 64); @@ -60,7 +58,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { settings_page(canvas, game_state); } - release_mutex((ValueMutex*)ctx, game_state); + furi_mutex_release(game_state->mutex); } //region card draw @@ -553,21 +551,21 @@ int32_t blackjack_app(void* p) { game_state->state = GameStateStart; - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, game_state, sizeof(GameState))) { + game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!game_state->mutex) { FURI_LOG_E(APP_NAME, "cannot create mutex\r\n"); return_code = 255; goto free_and_exit; } 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, game_state); view_port_input_callback_set(view_port, input_callback, event_queue); FuriTimer* timer = furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue); furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25); - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); AppEvent event; @@ -577,47 +575,44 @@ int32_t blackjack_app(void* p) { for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - GameState* localstate = (GameState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(game_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { if(event.type == EventTypeKey) { if(event.input.type == InputTypePress) { switch(event.input.key) { case InputKeyUp: - localstate->selectDirection = DirectionUp; + game_state->selectDirection = DirectionUp; break; case InputKeyDown: - localstate->selectDirection = DirectionDown; + game_state->selectDirection = DirectionDown; break; case InputKeyRight: - localstate->selectDirection = DirectionRight; + game_state->selectDirection = DirectionRight; break; case InputKeyLeft: - localstate->selectDirection = DirectionLeft; + game_state->selectDirection = DirectionLeft; break; case InputKeyBack: - if(localstate->state == GameStateSettings) { - localstate->state = GameStateStart; - save_settings(localstate->settings); + if(game_state->state == GameStateSettings) { + game_state->state = GameStateStart; + save_settings(game_state->settings); } else processing = false; break; case InputKeyOk: - localstate->selectDirection = Select; + game_state->selectDirection = Select; break; default: break; } } } else if(event.type == EventTypeTick) { - tick(localstate); - processing = localstate->processing; + tick(game_state); + processing = game_state->processing; } - } else { - //FURI_LOG_D(APP_NAME, "osMessageQueue: event timeout"); - // event timeout } view_port_update(view_port); - release_mutex(&state_mutex, localstate); + furi_mutex_release(game_state->mutex); } furi_timer_free(timer); @@ -625,7 +620,7 @@ int32_t blackjack_app(void* p) { gui_remove_view_port(gui, view_port); furi_record_close(RECORD_GUI); view_port_free(view_port); - delete_mutex(&state_mutex); + furi_mutex_free(game_state->mutex); free_and_exit: free(game_state->deck.cards); diff --git a/applications/plugins/blackjack/defines.h b/applications/plugins/blackjack/defines.h index b400badfb..0a3fdf53e 100644 --- a/applications/plugins/blackjack/defines.h +++ b/applications/plugins/blackjack/defines.h @@ -54,6 +54,7 @@ typedef enum { } Direction; typedef struct { + FuriMutex* mutex; Card player_cards[21]; Card dealer_cards[21]; uint8_t player_card_count; diff --git a/applications/plugins/doom/doom.c b/applications/plugins/doom/doom.c index 822f9b6fc..2fb99a635 100644 --- a/applications/plugins/doom/doom.c +++ b/applications/plugins/doom/doom.c @@ -38,6 +38,7 @@ typedef struct { } PluginEvent; typedef struct { + FuriMutex* mutex; Player player; Entity entity[MAX_ENTITIES]; StaticEntity static_entity[MAX_STATIC_ENTITIES]; @@ -765,10 +766,9 @@ void loopIntro(Canvas* const canvas) { } static void render_callback(Canvas* const canvas, void* ctx) { - PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } + furi_assert(ctx); + PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(plugin_state->init) setupDisplay(canvas); canvas_set_font(canvas, FontPrimary); @@ -792,7 +792,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { break; } } - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -962,8 +962,8 @@ int32_t doom_app() { FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); PluginState* plugin_state = malloc(sizeof(PluginState)); doom_state_init(plugin_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E("Doom_game", "cannot create mutex\r\n"); furi_record_close(RECORD_NOTIFICATION); furi_message_queue_free(event_queue); @@ -975,11 +975,11 @@ int32_t doom_app() { furi_timer_start(timer, furi_kernel_get_tick_frequency() / 12); // Set system callbacks 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); // Open GUI and register view_port - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); ////////////////////////////////// @@ -995,7 +995,7 @@ int32_t doom_app() { for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); #ifdef SOUND furi_check( furi_mutex_acquire(plugin_state->music_instance->model_mutex, FuriWaitForever) == @@ -1085,7 +1085,7 @@ int32_t doom_app() { furi_mutex_release(plugin_state->music_instance->model_mutex); #endif view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } #ifdef SOUND music_player_worker_free(plugin_state->music_instance->worker); @@ -1097,8 +1097,9 @@ int32_t doom_app() { furi_timer_free(timer); view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); - furi_record_close("gui"); + furi_record_close(RECORD_GUI); view_port_free(view_port); + furi_mutex_free(plugin_state->mutex); furi_message_queue_free(event_queue); free(plugin_state); return 0; diff --git a/applications/plugins/esp8266_deauth/esp8266_deauth.c b/applications/plugins/esp8266_deauth/esp8266_deauth.c index 3cc61a588..d32ca4c18 100644 --- a/applications/plugins/esp8266_deauth/esp8266_deauth.c +++ b/applications/plugins/esp8266_deauth/esp8266_deauth.c @@ -62,6 +62,7 @@ typedef struct SGpioButtons { } SGpioButtons; typedef struct SWiFiDeauthApp { + FuriMutex* mutex; Gui* m_gui; FuriThread* m_worker_thread; //NotificationApp* m_notification; @@ -121,10 +122,9 @@ static void esp8266_deauth_app_init(SWiFiDeauthApp* const app) { } static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ctx) { - SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)ctx, 25); - if(app == NULL) { - return; - } + furi_assert(ctx); + SWiFiDeauthApp* app = ctx; + furi_mutex_acquire(app->mutex, FuriWaitForever); //if(app->m_needUpdateGUI) //{ @@ -206,7 +206,7 @@ static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ct break; } - release_mutex((ValueMutex*)ctx, app); + furi_mutex_release(app->mutex); } static void @@ -235,14 +235,15 @@ static int32_t uart_worker(void* context) { furi_assert(context); DEAUTH_APP_LOG_I("[UART] Worker thread init"); - SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25); + SWiFiDeauthApp* app = context; + furi_mutex_acquire(app->mutex, FuriWaitForever); if(app == NULL) { return 1; } FuriStreamBuffer* rx_stream = app->m_rx_stream; - release_mutex((ValueMutex*)context, app); + furi_mutex_release(app->mutex); #if ENABLE_MODULE_POWER bool initialized = false; @@ -259,7 +260,8 @@ static int32_t uart_worker(void* context) { if(events & WorkerEventStop) break; if(events & WorkerEventRx) { DEAUTH_APP_LOG_I("[UART] Received data"); - SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25); + SWiFiDeauthApp* app = context; + furi_mutex_acquire(app->mutex, FuriWaitForever); if(app == NULL) { return 1; } @@ -307,7 +309,7 @@ static int32_t uart_worker(void* context) { } #endif // ENABLE_MODULE_POWER - release_mutex((ValueMutex*)context, app); + furi_mutex_release(app->mutex); } } @@ -356,8 +358,8 @@ int32_t esp8266_deauth_app(void* p) { #endif #endif // ENABLE_MODULE_DETECTION - ValueMutex app_data_mutex; - if(!init_mutex(&app_data_mutex, app, sizeof(SWiFiDeauthApp))) { + app->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!app->mutex) { DEAUTH_APP_LOG_E("cannot create mutex\r\n"); free(app); return 255; @@ -365,10 +367,10 @@ int32_t esp8266_deauth_app(void* p) { DEAUTH_APP_LOG_I("Mutex created"); - //app->m_notification = furi_record_open("notification"); + //app->m_notification = furi_record_open(RECORD_NOTIFICATION); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, esp8266_deauth_module_render_callback, &app_data_mutex); + view_port_draw_callback_set(view_port, esp8266_deauth_module_render_callback, app); view_port_input_callback_set(view_port, esp8266_deauth_module_input_callback, event_queue); // Open GUI and register view_port @@ -382,7 +384,7 @@ int32_t esp8266_deauth_app(void* p) { app->m_worker_thread = furi_thread_alloc(); furi_thread_set_name(app->m_worker_thread, "WiFiDeauthModuleUARTWorker"); furi_thread_set_stack_size(app->m_worker_thread, 1 * 1024); - furi_thread_set_context(app->m_worker_thread, &app_data_mutex); + furi_thread_set_context(app->m_worker_thread, app); furi_thread_set_callback(app->m_worker_thread, uart_worker); furi_thread_start(app->m_worker_thread); DEAUTH_APP_LOG_I("UART thread allocated"); @@ -398,7 +400,7 @@ int32_t esp8266_deauth_app(void* p) { SPluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - SWiFiDeauthApp* app = (SWiFiDeauthApp*)acquire_mutex_block(&app_data_mutex); + furi_mutex_acquire(app->mutex, FuriWaitForever); #if ENABLE_MODULE_DETECTION if(!app->m_wifiDeauthModuleAttached) { @@ -484,7 +486,7 @@ int32_t esp8266_deauth_app(void* p) { #endif view_port_update(view_port); - release_mutex(&app_data_mutex, app); + furi_mutex_release(app->mutex); } DEAUTH_APP_LOG_I("Start exit app"); @@ -514,7 +516,7 @@ int32_t esp8266_deauth_app(void* p) { // Close gui record furi_record_close(RECORD_GUI); - furi_record_close("notification"); + //furi_record_close(RECORD_NOTIFICATION); app->m_gui = NULL; view_port_free(view_port); @@ -523,7 +525,7 @@ int32_t esp8266_deauth_app(void* p) { furi_stream_buffer_free(app->m_rx_stream); - delete_mutex(&app_data_mutex); + furi_mutex_free(app->mutex); // Free rest free(app); diff --git a/applications/plugins/flappy_bird/flappy_bird.c b/applications/plugins/flappy_bird/flappy_bird.c index 0ff6f186a..b264d1b70 100644 --- a/applications/plugins/flappy_bird/flappy_bird.c +++ b/applications/plugins/flappy_bird/flappy_bird.c @@ -60,6 +60,7 @@ typedef struct { PILAR pilars[FLAPPY_PILAR_MAX]; bool debug; State state; + FuriMutex* mutex; } GameState; typedef struct { @@ -175,10 +176,9 @@ static void flappy_game_flap(GameState* const game_state) { } static void flappy_game_render_callback(Canvas* const canvas, void* ctx) { - const GameState* game_state = acquire_mutex((ValueMutex*)ctx, 25); - if(game_state == NULL) { - return; - } + furi_assert(ctx); + const GameState* game_state = ctx; + furi_mutex_acquire(game_state->mutex, FuriWaitForever); canvas_draw_frame(canvas, 0, 0, 128, 64); @@ -262,7 +262,7 @@ static void flappy_game_render_callback(Canvas* const canvas, void* ctx) { canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer); } - release_mutex((ValueMutex*)ctx, game_state); + furi_mutex_release(game_state->mutex); } static void flappy_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -288,8 +288,8 @@ int32_t flappy_game_app(void* p) { GameState* game_state = malloc(sizeof(GameState)); flappy_game_state_init(game_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, game_state, sizeof(GameState))) { + game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!game_state->mutex) { FURI_LOG_E(TAG, "cannot create mutex\r\n"); return_code = 255; goto free_and_exit; @@ -297,7 +297,7 @@ int32_t flappy_game_app(void* p) { // Set system callbacks ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, flappy_game_render_callback, &state_mutex); + view_port_draw_callback_set(view_port, flappy_game_render_callback, game_state); view_port_input_callback_set(view_port, flappy_game_input_callback, event_queue); FuriTimer* timer = @@ -314,7 +314,7 @@ int32_t flappy_game_app(void* p) { GameEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - GameState* game_state = (GameState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(game_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -356,7 +356,7 @@ int32_t flappy_game_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, game_state); + furi_mutex_release(game_state->mutex); } furi_timer_free(timer); @@ -364,7 +364,7 @@ int32_t flappy_game_app(void* p) { gui_remove_view_port(gui, view_port); furi_record_close(RECORD_GUI); view_port_free(view_port); - delete_mutex(&state_mutex); + furi_mutex_free(game_state->mutex); free_and_exit: flappy_game_state_free(game_state); diff --git a/applications/plugins/flipfrid/flipfrid.c b/applications/plugins/flipfrid/flipfrid.c index 2c5d1201d..9ddbdd42e 100644 --- a/applications/plugins/flipfrid/flipfrid.c +++ b/applications/plugins/flipfrid/flipfrid.c @@ -9,11 +9,9 @@ #define RFIDFUZZER_APP_FOLDER "/ext/rfidfuzzer" static void flipfrid_draw_callback(Canvas* const canvas, void* ctx) { - FlipFridState* flipfrid_state = (FlipFridState*)acquire_mutex((ValueMutex*)ctx, 100); - - if(flipfrid_state == NULL) { - return; - } + furi_assert(ctx); + FlipFridState* flipfrid_state = ctx; + furi_mutex_acquire(flipfrid_state->mutex, FuriWaitForever); // Draw correct Canvas switch(flipfrid_state->current_scene) { @@ -35,7 +33,7 @@ static void flipfrid_draw_callback(Canvas* const canvas, void* ctx) { break; } - release_mutex((ValueMutex*)ctx, flipfrid_state); + furi_mutex_release(flipfrid_state->mutex); } void flipfrid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -116,11 +114,9 @@ int32_t flipfrid_start(void* p) { FURI_LOG_I(TAG, "Initializing input"); FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(FlipFridEvent)); FlipFridState* flipfrid_state = flipfrid_alloc(); - ValueMutex flipfrid_state_mutex; - // Mutex - FURI_LOG_I(TAG, "Initializing flipfrid mutex"); - if(!init_mutex(&flipfrid_state_mutex, flipfrid_state, sizeof(FlipFridState))) { + flipfrid_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!flipfrid_state->mutex) { FURI_LOG_E(TAG, "cannot create mutex\r\n"); furi_message_queue_free(event_queue); furi_record_close(RECORD_NOTIFICATION); @@ -137,7 +133,7 @@ int32_t flipfrid_start(void* p) { // Configure view port FURI_LOG_I(TAG, "Initializing viewport"); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, flipfrid_draw_callback, &flipfrid_state_mutex); + view_port_draw_callback_set(view_port, flipfrid_draw_callback, flipfrid_state); view_port_input_callback_set(view_port, flipfrid_input_callback, event_queue); // Configure timer @@ -258,6 +254,7 @@ int32_t flipfrid_start(void* p) { furi_message_queue_free(event_queue); furi_record_close(RECORD_GUI); furi_record_close(RECORD_NOTIFICATION); + furi_mutex_free(flipfrid_state->mutex); flipfrid_free(flipfrid_state); return 0; diff --git a/applications/plugins/flipfrid/flipfrid.h b/applications/plugins/flipfrid/flipfrid.h index 6b8662e65..e4122054b 100644 --- a/applications/plugins/flipfrid/flipfrid.h +++ b/applications/plugins/flipfrid/flipfrid.h @@ -58,6 +58,7 @@ typedef struct { // STRUCTS typedef struct { + FuriMutex* mutex; bool is_running; bool is_attacking; FlipFridScene current_scene; diff --git a/applications/plugins/flipper_i2ctools/i2ctools.c b/applications/plugins/flipper_i2ctools/i2ctools.c index 9d73a73b8..6d4cc739f 100644 --- a/applications/plugins/flipper_i2ctools/i2ctools.c +++ b/applications/plugins/flipper_i2ctools/i2ctools.c @@ -1,7 +1,9 @@ #include "i2ctools_i.h" void i2ctools_draw_callback(Canvas* canvas, void* ctx) { - i2cTools* i2ctools = acquire_mutex((ValueMutex*)ctx, 25); + furi_assert(ctx); + i2cTools* i2ctools = ctx; + furi_mutex_acquire(i2ctools->mutex, FuriWaitForever); switch(i2ctools->main_view->current_view) { case MAIN_VIEW: @@ -23,7 +25,7 @@ void i2ctools_draw_callback(Canvas* canvas, void* ctx) { default: break; } - release_mutex((ValueMutex*)ctx, i2ctools); + furi_mutex_release(i2ctools->mutex); } void i2ctools_input_callback(InputEvent* input_event, void* ctx) { @@ -38,8 +40,8 @@ int32_t i2ctools_app(void* p) { // Alloc i2ctools i2cTools* i2ctools = malloc(sizeof(i2cTools)); - ValueMutex i2ctools_mutex; - if(!init_mutex(&i2ctools_mutex, i2ctools, sizeof(i2cTools))) { + i2ctools->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!i2ctools->mutex) { FURI_LOG_E(APP_NAME, "cannot create mutex\r\n"); free(i2ctools); return -1; @@ -47,7 +49,7 @@ int32_t i2ctools_app(void* p) { // Alloc viewport i2ctools->view_port = view_port_alloc(); - view_port_draw_callback_set(i2ctools->view_port, i2ctools_draw_callback, &i2ctools_mutex); + view_port_draw_callback_set(i2ctools->view_port, i2ctools_draw_callback, i2ctools); view_port_input_callback_set(i2ctools->view_port, i2ctools_input_callback, event_queue); // Register view port in GUI @@ -216,6 +218,7 @@ int32_t i2ctools_app(void* p) { i2c_scanner_free(i2ctools->scanner); i2c_sender_free(i2ctools->sender); i2c_main_view_free(i2ctools->main_view); + furi_mutex_free(i2ctools->mutex); free(i2ctools); furi_record_close(RECORD_GUI); return 0; diff --git a/applications/plugins/flipper_i2ctools/i2ctools_i.h b/applications/plugins/flipper_i2ctools/i2ctools_i.h index 33917dc34..f31e42478 100644 --- a/applications/plugins/flipper_i2ctools/i2ctools_i.h +++ b/applications/plugins/flipper_i2ctools/i2ctools_i.h @@ -13,6 +13,7 @@ // App datas typedef struct { + FuriMutex* mutex; ViewPort* view_port; i2cMainView* main_view; diff --git a/applications/plugins/game_2048/game_2048.c b/applications/plugins/game_2048/game_2048.c index 1a2f4a4d3..a1e6361b6 100644 --- a/applications/plugins/game_2048/game_2048.c +++ b/applications/plugins/game_2048/game_2048.c @@ -34,6 +34,7 @@ typedef enum { } State; typedef struct { + FuriMutex* mutex; State state; uint8_t table[CELLS_COUNT][CELLS_COUNT]; uint32_t score; @@ -104,8 +105,9 @@ static void gray_canvas(Canvas* const canvas) { } static void draw_callback(Canvas* const canvas, void* ctx) { - const GameState* game_state = acquire_mutex((ValueMutex*)ctx, 25); - if(game_state == NULL) return; + furi_assert(ctx); + const GameState* game_state = ctx; + furi_mutex_acquire(game_state->mutex, FuriWaitForever); canvas_clear(canvas); @@ -181,7 +183,7 @@ static void draw_callback(Canvas* const canvas, void* ctx) { canvas_draw_str_aligned(canvas, 64, 48, AlignCenter, AlignBottom, buf); } - release_mutex((ValueMutex*)ctx, game_state); + furi_mutex_release(game_state->mutex); } void calculate_move_to_left(uint8_t arr[], MoveResult* const move_result) { @@ -381,8 +383,8 @@ int32_t game_2048_app() { MoveResult* move_result = malloc(sizeof(MoveResult)); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, game_state, sizeof(GameState))) { + game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!game_state->mutex) { FURI_LOG_E("2048Game", "cannot create mutex\r\n"); free(game_state); return 255; @@ -392,7 +394,7 @@ int32_t game_2048_app() { FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent)); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, draw_callback, &state_mutex); + view_port_draw_callback_set(view_port, draw_callback, game_state); view_port_input_callback_set(view_port, input_callback, event_queue); Gui* gui = furi_record_open(RECORD_GUI); @@ -408,7 +410,7 @@ int32_t game_2048_app() { // handle only press event, ignore repeat/release events if(input.type != InputTypePress) continue; - GameState* game_state = (GameState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(game_state->mutex, FuriWaitForever); switch(game_state->state) { case GameStateMenu: @@ -493,7 +495,7 @@ int32_t game_2048_app() { } view_port_update(view_port); - release_mutex(&state_mutex, game_state); + furi_mutex_release(game_state->mutex); } } @@ -504,7 +506,7 @@ int32_t game_2048_app() { furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(game_state->mutex); free(game_state); free(move_result); diff --git a/applications/plugins/gps_nmea_uart/gps.c b/applications/plugins/gps_nmea_uart/gps.c index 62053cede..b36fd080b 100644 --- a/applications/plugins/gps_nmea_uart/gps.c +++ b/applications/plugins/gps_nmea_uart/gps.c @@ -15,10 +15,9 @@ typedef struct { } PluginEvent; static void render_callback(Canvas* const canvas, void* context) { - const GpsUart* gps_uart = acquire_mutex((ValueMutex*)context, 25); - if(gps_uart == NULL) { - return; - } + furi_assert(context); + const GpsUart* gps_uart = context; + furi_mutex_acquire(gps_uart->mutex, FuriWaitForever); canvas_set_font(canvas, FontPrimary); canvas_draw_str_aligned(canvas, 32, 8, AlignCenter, AlignBottom, "Latitude"); @@ -57,7 +56,7 @@ static void render_callback(Canvas* const canvas, void* context) { gps_uart->status.time_seconds); canvas_draw_str_aligned(canvas, 96, 62, AlignCenter, AlignBottom, buffer); - release_mutex((ValueMutex*)context, gps_uart); + furi_mutex_release(gps_uart->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -74,8 +73,8 @@ int32_t gps_app(void* p) { GpsUart* gps_uart = gps_uart_enable(); - ValueMutex gps_uart_mutex; - if(!init_mutex(&gps_uart_mutex, gps_uart, sizeof(GpsUart))) { + gps_uart->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!gps_uart->mutex) { FURI_LOG_E("GPS", "cannot create mutex\r\n"); free(gps_uart); return 255; @@ -83,18 +82,18 @@ int32_t gps_app(void* p) { // set system callbacks ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, render_callback, &gps_uart_mutex); + view_port_draw_callback_set(view_port, render_callback, gps_uart); view_port_input_callback_set(view_port, input_callback, event_queue); // open GUI and register view_port - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - GpsUart* gps_uart = (GpsUart*)acquire_mutex_block(&gps_uart_mutex); + furi_mutex_acquire(gps_uart->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -118,15 +117,15 @@ int32_t gps_app(void* p) { } view_port_update(view_port); - release_mutex(&gps_uart_mutex, gps_uart); + furi_mutex_release(gps_uart->mutex); } view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); - furi_record_close("gui"); + furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&gps_uart_mutex); + furi_mutex_free(gps_uart->mutex); gps_uart_disable(gps_uart); return 0; diff --git a/applications/plugins/gps_nmea_uart/gps_uart.h b/applications/plugins/gps_nmea_uart/gps_uart.h index d6aafae9f..fd33eb245 100644 --- a/applications/plugins/gps_nmea_uart/gps_uart.h +++ b/applications/plugins/gps_nmea_uart/gps_uart.h @@ -22,6 +22,7 @@ typedef struct { } GpsStatus; typedef struct { + FuriMutex* mutex; FuriThread* thread; FuriStreamBuffer* rx_stream; uint8_t rx_buf[RX_BUF_SIZE]; diff --git a/applications/plugins/hc_sr04/hc_sr04.c b/applications/plugins/hc_sr04/hc_sr04.c index db075247a..66ffdad30 100644 --- a/applications/plugins/hc_sr04/hc_sr04.c +++ b/applications/plugins/hc_sr04/hc_sr04.c @@ -24,6 +24,7 @@ typedef struct { } PluginEvent; typedef struct { + FuriMutex* mutex; NotificationApp* notification; bool have_5v; bool measurement_made; @@ -41,10 +42,10 @@ const NotificationSequence sequence_done = { }; static void render_callback(Canvas* const canvas, void* ctx) { - const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } + furi_assert(ctx); + const PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); + // border around the edge of the screen // canvas_draw_frame(canvas, 0, 0, 128, 64); @@ -85,7 +86,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { } } - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -184,8 +185,8 @@ int32_t hc_sr04_app() { furi_hal_console_disable(); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E("hc_sr04", "cannot create mutex\r\n"); if(furi_hal_power_is_otg_enabled()) { furi_hal_power_disable_otg(); @@ -201,7 +202,7 @@ int32_t hc_sr04_app() { // Set system callbacks 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); // Open GUI and register view_port @@ -212,7 +213,7 @@ int32_t hc_sr04_app() { for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -238,7 +239,7 @@ int32_t hc_sr04_app() { } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } if(furi_hal_power_is_otg_enabled()) { @@ -267,7 +268,8 @@ int32_t hc_sr04_app() { furi_record_close(RECORD_NOTIFICATION); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(plugin_state->mutex); + free(plugin_state); return 0; } diff --git a/applications/plugins/heap_defence_game/heap_defence.c b/applications/plugins/heap_defence_game/heap_defence.c index 2981d6c27..378a42823 100644 --- a/applications/plugins/heap_defence_game/heap_defence.c +++ b/applications/plugins/heap_defence_game/heap_defence.c @@ -87,6 +87,7 @@ typedef struct { Person* person; Animations animation; GameStatuses game_status; + FuriMutex* mutex; } GameState; typedef Box** Field; @@ -433,15 +434,15 @@ static void draw_box(Canvas* canvas, Box* box, int x, int y) { static void heap_defense_render_callback(Canvas* const canvas, void* mutex) { furi_assert(mutex); - - const GameState* game = acquire_mutex((ValueMutex*)mutex, 25); + const GameState* game = mutex; + furi_mutex_acquire(game->mutex, FuriWaitForever); ///Draw GameOver or Pause if(!(game->game_status & GameStatusInProgress)) { FURI_LOG_W(TAG, "[DAED_DRAW]func: [%s] line: %d ", __FUNCTION__, __LINE__); canvas_draw_icon_animation(canvas, 0, 0, animations[game->animation]); - release_mutex((ValueMutex*)mutex, game); + furi_mutex_release(game->mutex); return; } @@ -481,7 +482,7 @@ static void heap_defense_render_callback(Canvas* const canvas, void* mutex) { } } - release_mutex((ValueMutex*)mutex, game); + furi_mutex_release(game->mutex); } static void heap_defense_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -506,25 +507,25 @@ int32_t heap_defence_app(void* p) { FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent)); GameState* game = allocGameState(); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, game, sizeof(GameState))) { + game->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!game->mutex) { game_destroy(game); return 1; } assets_load(); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, heap_defense_render_callback, &state_mutex); + view_port_draw_callback_set(view_port, heap_defense_render_callback, game); view_port_input_callback_set(view_port, heap_defense_input_callback, event_queue); FuriTimer* timer = furi_timer_alloc(heap_defense_timer_callback, FuriTimerTypePeriodic, event_queue); furi_timer_start(timer, furi_kernel_get_tick_frequency() / TIMER_UPDATE_FREQ); - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); - NotificationApp* notification = furi_record_open("notification"); + NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); memset(game->field[Y_LAST], 128, ROW_BYTE_SIZE); game->person->p.y -= 2; @@ -540,7 +541,7 @@ int32_t heap_defence_app(void* p) { continue; } - game = (GameState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(game->mutex, FuriWaitForever); //unset vibration if(game->game_status & GameStatusVibro) { @@ -577,7 +578,7 @@ int32_t heap_defence_app(void* p) { notification_message(notification, &sequence_error); } } - release_mutex(&state_mutex, game); + furi_mutex_release(game->mutex); view_port_update(view_port); } @@ -585,11 +586,11 @@ int32_t heap_defence_app(void* p) { view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); view_port_free(view_port); - furi_record_close("gui"); - furi_record_close("notification"); + furi_record_close(RECORD_GUI); + furi_record_close(RECORD_NOTIFICATION); furi_message_queue_free(event_queue); assets_clear(); - delete_mutex(&state_mutex); + furi_mutex_free(game->mutex); game_destroy(game); return 0; diff --git a/applications/plugins/ibtn_fuzzer/ibtnfuzzer.c b/applications/plugins/ibtn_fuzzer/ibtnfuzzer.c index f02da3b82..c5f2a5f7c 100644 --- a/applications/plugins/ibtn_fuzzer/ibtnfuzzer.c +++ b/applications/plugins/ibtn_fuzzer/ibtnfuzzer.c @@ -9,11 +9,9 @@ #define IBTNFUZZER_APP_FOLDER "/ext/ibtnfuzzer" static void ibtnfuzzer_draw_callback(Canvas* const canvas, void* ctx) { - iBtnFuzzerState* ibtnfuzzer_state = (iBtnFuzzerState*)acquire_mutex((ValueMutex*)ctx, 100); - - if(ibtnfuzzer_state == NULL) { - return; - } + furi_assert(ctx); + iBtnFuzzerState* ibtnfuzzer_state = ctx; + furi_mutex_acquire(ibtnfuzzer_state->mutex, FuriWaitForever); // Draw correct Canvas switch(ibtnfuzzer_state->current_scene) { @@ -35,7 +33,7 @@ static void ibtnfuzzer_draw_callback(Canvas* const canvas, void* ctx) { break; } - release_mutex((ValueMutex*)ctx, ibtnfuzzer_state); + furi_mutex_release(ibtnfuzzer_state->mutex); } void ibtnfuzzer_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -120,11 +118,9 @@ int32_t ibtnfuzzer_start(void* p) { FURI_LOG_I(TAG, "Initializing input"); FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(iBtnFuzzerEvent)); iBtnFuzzerState* ibtnfuzzer_state = ibtnfuzzer_alloc(); - ValueMutex ibtnfuzzer_state_mutex; - // Mutex - FURI_LOG_I(TAG, "Initializing ibtnfuzzer mutex"); - if(!init_mutex(&ibtnfuzzer_state_mutex, ibtnfuzzer_state, sizeof(iBtnFuzzerState))) { + ibtnfuzzer_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!ibtnfuzzer_state->mutex) { FURI_LOG_E(TAG, "cannot create mutex\r\n"); furi_message_queue_free(event_queue); furi_record_close(RECORD_NOTIFICATION); @@ -141,7 +137,7 @@ int32_t ibtnfuzzer_start(void* p) { // Configure view port FURI_LOG_I(TAG, "Initializing viewport"); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, ibtnfuzzer_draw_callback, &ibtnfuzzer_state_mutex); + view_port_draw_callback_set(view_port, ibtnfuzzer_draw_callback, ibtnfuzzer_state); view_port_input_callback_set(view_port, ibtnfuzzer_input_callback, event_queue); // Configure timer @@ -160,6 +156,7 @@ int32_t ibtnfuzzer_start(void* p) { while(ibtnfuzzer_state->is_running) { // Get next event FuriStatus event_status = furi_message_queue_get(event_queue, &event, 25); + //furi_mutex_acquire(ibtnfuzzer_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { if(event.evt_type == EventTypeKey) { //Handle event key @@ -250,6 +247,7 @@ int32_t ibtnfuzzer_start(void* p) { view_port_update(view_port); } } + //furi_mutex_release(ibtnfuzzer_state->mutex); } // Cleanup @@ -262,6 +260,7 @@ int32_t ibtnfuzzer_start(void* p) { furi_message_queue_free(event_queue); furi_record_close(RECORD_GUI); furi_record_close(RECORD_NOTIFICATION); + furi_mutex_free(ibtnfuzzer_state->mutex); ibtnfuzzer_free(ibtnfuzzer_state); return 0; diff --git a/applications/plugins/ibtn_fuzzer/ibtnfuzzer.h b/applications/plugins/ibtn_fuzzer/ibtnfuzzer.h index 4a220dbd9..91a9c6b0c 100644 --- a/applications/plugins/ibtn_fuzzer/ibtnfuzzer.h +++ b/applications/plugins/ibtn_fuzzer/ibtnfuzzer.h @@ -56,6 +56,7 @@ typedef struct { // STRUCTS typedef struct { + FuriMutex* mutex; bool is_running; bool is_attacking; iBtnFuzzerScene current_scene; diff --git a/applications/plugins/metronome/metronome.c b/applications/plugins/metronome/metronome.c index 06bce1bff..a01f4418d 100644 --- a/applications/plugins/metronome/metronome.c +++ b/applications/plugins/metronome/metronome.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include @@ -50,31 +49,33 @@ typedef struct { enum OutputMode output_mode; FuriTimer* timer; NotificationApp* notifications; + FuriMutex* mutex; } MetronomeState; static void render_callback(Canvas* const canvas, void* ctx) { - const MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); - if(metronome_state == NULL) { - return; - } + furi_assert(ctx); + const MetronomeState* metronome_state = ctx; + furi_mutex_acquire(metronome_state->mutex, FuriWaitForever); - string_t tempStr; - string_init(tempStr); + FuriString* tempStr = furi_string_alloc(); canvas_draw_frame(canvas, 0, 0, 128, 64); canvas_set_font(canvas, FontPrimary); // draw bars/beat - string_printf(tempStr, "%d/%d", metronome_state->beats_per_bar, metronome_state->note_length); - canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, string_get_cstr(tempStr)); - string_reset(tempStr); + furi_string_printf( + tempStr, "%d/%d", metronome_state->beats_per_bar, metronome_state->note_length); + canvas_draw_str_aligned( + canvas, 64, 8, AlignCenter, AlignCenter, furi_string_get_cstr(tempStr)); + furi_string_reset(tempStr); // draw BPM value - string_printf(tempStr, "%.2f", metronome_state->bpm); + furi_string_printf(tempStr, "%.2f", metronome_state->bpm); canvas_set_font(canvas, FontBigNumbers); - canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, string_get_cstr(tempStr)); - string_reset(tempStr); + canvas_draw_str_aligned( + canvas, 64, 24, AlignCenter, AlignCenter, furi_string_get_cstr(tempStr)); + furi_string_reset(tempStr); // draw volume indicator // always draw first waves @@ -126,8 +127,8 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas, 8, 36, 112, (float)metronome_state->current_beat / metronome_state->beats_per_bar); // cleanup - string_clear(tempStr); - release_mutex((ValueMutex*)ctx, metronome_state); + furi_string_free(tempStr); + furi_mutex_release(metronome_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -139,7 +140,10 @@ static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queu static void timer_callback(void* ctx) { // this is where we go BEEP! - MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); + furi_assert(ctx); + MetronomeState* metronome_state = ctx; + furi_mutex_acquire(metronome_state->mutex, FuriWaitForever); + metronome_state->current_beat++; if(metronome_state->current_beat > metronome_state->beats_per_bar) { metronome_state->current_beat = 1; @@ -200,7 +204,7 @@ static void timer_callback(void* ctx) { } notification_message(metronome_state->notifications, &sequence_reset_rgb); - release_mutex((ValueMutex*)ctx, metronome_state); + furi_mutex_release(metronome_state->mutex); } static uint32_t state_to_sleep_ticks(MetronomeState* metronome_state) { @@ -273,8 +277,8 @@ int32_t metronome_app() { MetronomeState* metronome_state = malloc(sizeof(MetronomeState)); metronome_state_init(metronome_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, metronome_state, sizeof(MetronomeState))) { + metronome_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!metronome_state->mutex) { FURI_LOG_E("Metronome", "cannot create mutex\r\n"); free(metronome_state); return 255; @@ -282,19 +286,20 @@ int32_t metronome_app() { // Set system callbacks 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, metronome_state); view_port_input_callback_set(view_port, input_callback, event_queue); - metronome_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, &state_mutex); + metronome_state->timer = + furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, metronome_state); // Open GUI and register view_port - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - MetronomeState* metronome_state = (MetronomeState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(metronome_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { if(event.type == EventTypeKey) { @@ -373,21 +378,18 @@ int32_t metronome_app() { } } } - } else { - FURI_LOG_D("Metronome", "FuriMessageQueue: event timeout"); - // event timeout } view_port_update(view_port); - release_mutex(&state_mutex, metronome_state); + furi_mutex_release(metronome_state->mutex); } view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); - furi_record_close("gui"); + furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(metronome_state->mutex); furi_timer_free(metronome_state->timer); furi_record_close(RECORD_NOTIFICATION); free(metronome_state); diff --git a/applications/plugins/minesweeper/minesweeper.c b/applications/plugins/minesweeper/minesweeper.c index d5e2e715d..2adca65e9 100644 --- a/applications/plugins/minesweeper/minesweeper.c +++ b/applications/plugins/minesweeper/minesweeper.c @@ -57,6 +57,7 @@ typedef struct { int flags_set; bool game_started; uint32_t game_started_tick; + FuriMutex* mutex; } Minesweeper; static void timer_callback(void* ctx) { @@ -74,10 +75,10 @@ static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queu } static void render_callback(Canvas* const canvas, void* ctx) { - const Minesweeper* minesweeper_state = acquire_mutex((ValueMutex*)ctx, 25); - if(minesweeper_state == NULL) { - return; - } + furi_assert(ctx); + const Minesweeper* minesweeper_state = ctx; + furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever); + FuriString* mineStr; FuriString* timeStr; mineStr = furi_string_alloc(); @@ -162,7 +163,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { furi_string_free(mineStr); furi_string_free(timeStr); - release_mutex((ValueMutex*)ctx, minesweeper_state); + furi_mutex_release(minesweeper_state->mutex); } static void setup_playfield(Minesweeper* minesweeper_state) { @@ -396,8 +397,8 @@ int32_t minesweeper_app(void* p) { // setup minesweeper_state_init(minesweeper_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, minesweeper_state, sizeof(minesweeper_state))) { + minesweeper_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!minesweeper_state->mutex) { FURI_LOG_E("Minesweeper", "cannot create mutex\r\n"); free(minesweeper_state); return 255; @@ -406,12 +407,13 @@ int32_t minesweeper_app(void* p) { // Set system callbacks 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, minesweeper_state); view_port_input_callback_set(view_port, input_callback, event_queue); - minesweeper_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypeOnce, &state_mutex); + minesweeper_state->timer = + furi_timer_alloc(timer_callback, FuriTimerTypeOnce, minesweeper_state); // Open GUI and register view_port - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); // Call dolphin deed on game start @@ -420,7 +422,7 @@ int32_t minesweeper_app(void* p) { PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - Minesweeper* minesweeper_state = (Minesweeper*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events if(event.type == EventTypeKey) { @@ -508,19 +510,16 @@ int32_t minesweeper_app(void* p) { } } } - } else { - // event timeout - ; } view_port_update(view_port); - release_mutex(&state_mutex, minesweeper_state); + furi_mutex_release(minesweeper_state->mutex); } view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); - furi_record_close("gui"); + furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(minesweeper_state->mutex); furi_timer_free(minesweeper_state->timer); free(minesweeper_state); diff --git a/applications/plugins/mousejacker/mousejacker.c b/applications/plugins/mousejacker/mousejacker.c index 3b1b2e6f7..606e092ca 100644 --- a/applications/plugins/mousejacker/mousejacker.c +++ b/applications/plugins/mousejacker/mousejacker.c @@ -40,10 +40,10 @@ char target_address_str[12] = "None"; char target_text[30]; static void render_callback(Canvas* const canvas, void* ctx) { - const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } + furi_assert(ctx); + const PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); + // border around the edge of the screen canvas_draw_frame(canvas, 0, 0, 128, 64); @@ -82,7 +82,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas_draw_str_aligned(canvas, 3, 30, AlignLeft, AlignBottom, "to exit"); } - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -289,8 +289,8 @@ int32_t mousejacker_app(void* p) { PluginState* plugin_state = malloc(sizeof(PluginState)); mousejacker_state_init(plugin_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E("mousejacker", "cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(plugin_state); @@ -299,7 +299,7 @@ int32_t mousejacker_app(void* p) { // Set system callbacks 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); // Open GUI and register view_port @@ -330,7 +330,7 @@ int32_t mousejacker_app(void* p) { PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -380,7 +380,7 @@ int32_t mousejacker_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } furi_thread_free(plugin_state->mjthread); @@ -391,6 +391,7 @@ int32_t mousejacker_app(void* p) { furi_record_close(RECORD_STORAGE); view_port_free(view_port); furi_message_queue_free(event_queue); + furi_mutex_free(plugin_state->mutex); free(plugin_state); return 0; diff --git a/applications/plugins/mousejacker/mousejacker_ducky.h b/applications/plugins/mousejacker/mousejacker_ducky.h index b4922ed38..e1a422ea7 100644 --- a/applications/plugins/mousejacker/mousejacker_ducky.h +++ b/applications/plugins/mousejacker/mousejacker_ducky.h @@ -21,6 +21,7 @@ typedef struct { } MJDuckyKey; typedef struct { + FuriMutex* mutex; bool ducky_err; bool addr_err; bool is_thread_running; diff --git a/applications/plugins/multi_converter/multi_converter.c b/applications/plugins/multi_converter/multi_converter.c index 590730357..bd2b62587 100644 --- a/applications/plugins/multi_converter/multi_converter.c +++ b/applications/plugins/multi_converter/multi_converter.c @@ -8,10 +8,9 @@ #include "multi_converter_mode_select.h" static void multi_converter_render_callback(Canvas* const canvas, void* ctx) { - const MultiConverterState* multi_converter_state = acquire_mutex((ValueMutex*)ctx, 25); - if(multi_converter_state == NULL) { - return; - } + furi_assert(ctx); + const MultiConverterState* multi_converter_state = ctx; + furi_mutex_acquire(multi_converter_state->mutex, FuriWaitForever); if(multi_converter_state->mode == ModeDisplay) { multi_converter_mode_display_draw(canvas, multi_converter_state); @@ -19,7 +18,7 @@ static void multi_converter_render_callback(Canvas* const canvas, void* ctx) { multi_converter_mode_select_draw(canvas, multi_converter_state); } - release_mutex((ValueMutex*)ctx, multi_converter_state); + furi_mutex_release(multi_converter_state->mutex); } static void @@ -62,8 +61,8 @@ int32_t multi_converter_app(void* p) { MultiConverterState* multi_converter_state = malloc(sizeof(MultiConverterState)); // set mutex for plugin state (different threads can access it) - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, multi_converter_state, sizeof(multi_converter_state))) { + multi_converter_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!multi_converter_state->mutex) { FURI_LOG_E("MultiConverter", "cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(multi_converter_state); @@ -72,11 +71,11 @@ int32_t multi_converter_app(void* p) { // register callbacks for drawing and input processing ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, multi_converter_render_callback, &state_mutex); + view_port_draw_callback_set(view_port, multi_converter_render_callback, multi_converter_state); view_port_input_callback_set(view_port, multi_converter_input_callback, event_queue); // open GUI and register view_port - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); multi_converter_init(multi_converter_state); @@ -85,8 +84,7 @@ int32_t multi_converter_app(void* p) { MultiConverterEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - MultiConverterState* multi_converter_state = - (MultiConverterState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(multi_converter_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -148,20 +146,18 @@ int32_t multi_converter_app(void* p) { } else if(multi_converter_state->keyboard_lock) { multi_converter_state->keyboard_lock = 0; } - } else { - // event timeout } view_port_update(view_port); - release_mutex(&state_mutex, multi_converter_state); + furi_mutex_release(multi_converter_state->mutex); } view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); - furi_record_close("gui"); + furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(multi_converter_state->mutex); free(multi_converter_state); return 0; diff --git a/applications/plugins/multi_converter/multi_converter_definitions.h b/applications/plugins/multi_converter/multi_converter_definitions.h index 3bed192a0..2f79bc7d9 100644 --- a/applications/plugins/multi_converter/multi_converter_definitions.h +++ b/applications/plugins/multi_converter/multi_converter_definitions.h @@ -70,6 +70,7 @@ struct MultiConverterUnit { }; struct MultiConverterState { + FuriMutex* mutex; char buffer_orig[MULTI_CONVERTER_NUMBER_DIGITS + 1]; char buffer_dest[MULTI_CONVERTER_NUMBER_DIGITS + 1]; MultiConverterUnitType unit_type_orig; diff --git a/applications/plugins/nrfsniff/nrfsniff.c b/applications/plugins/nrfsniff/nrfsniff.c index 094775426..56bc33d05 100644 --- a/applications/plugins/nrfsniff/nrfsniff.c +++ b/applications/plugins/nrfsniff/nrfsniff.c @@ -29,8 +29,7 @@ typedef struct { } PluginEvent; typedef struct { - int x; - int y; + FuriMutex* mutex; } PluginState; char rate_text_fmt[] = "Transfer rate: %dMbps"; @@ -96,13 +95,13 @@ static void insert_addr(uint8_t* addr, uint8_t addr_size) { } static void render_callback(Canvas* const canvas, void* ctx) { + furi_assert(ctx); + const PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); + uint8_t rate = 2; char sniffing[] = "Yes"; - const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } // border around the edge of the screen canvas_draw_frame(canvas, 0, 0, 128, 64); canvas_set_font(canvas, FontSecondary); @@ -126,7 +125,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas_draw_str_aligned(canvas, 30, 50, AlignLeft, AlignBottom, addresses_header_text); canvas_draw_str_aligned(canvas, 30, 60, AlignLeft, AlignBottom, sniffed_address); - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -320,8 +319,8 @@ int32_t nrfsniff_app(void* p) { hexlify(address, 5, top_address); FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); PluginState* plugin_state = malloc(sizeof(PluginState)); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { furi_message_queue_free(event_queue); FURI_LOG_E(TAG, "cannot create mutex\r\n"); free(plugin_state); @@ -332,7 +331,7 @@ int32_t nrfsniff_app(void* p) { // Set system callbacks 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); // Open GUI and register view_port @@ -347,7 +346,7 @@ int32_t nrfsniff_app(void* p) { PluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -435,7 +434,7 @@ int32_t nrfsniff_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } clear_cache(); @@ -450,6 +449,7 @@ int32_t nrfsniff_app(void* p) { furi_record_close(RECORD_STORAGE); view_port_free(view_port); furi_message_queue_free(event_queue); + furi_mutex_free(plugin_state->mutex); free(plugin_state); return 0; diff --git a/applications/plugins/sentry_safe/sentry_safe.c b/applications/plugins/sentry_safe/sentry_safe.c index 4d9c12fc7..789b43f2c 100644 --- a/applications/plugins/sentry_safe/sentry_safe.c +++ b/applications/plugins/sentry_safe/sentry_safe.c @@ -7,6 +7,7 @@ typedef struct { uint8_t status; + FuriMutex* mutex; } SentryState; typedef enum { @@ -22,10 +23,9 @@ typedef struct { const char* status_texts[3] = {"[Press OK to open safe]", "Sending...", "Done !"}; static void sentry_safe_render_callback(Canvas* const canvas, void* ctx) { - const SentryState* sentry_state = acquire_mutex((ValueMutex*)ctx, 25); - if(sentry_state == NULL) { - return; - } + furi_assert(ctx); + const SentryState* sentry_state = ctx; + furi_mutex_acquire(sentry_state->mutex, FuriWaitForever); // Before the function is called, the state is set with the canvas_reset(canvas) @@ -41,7 +41,7 @@ static void sentry_safe_render_callback(Canvas* const canvas, void* ctx) { canvas_draw_str_aligned( canvas, 64, 50, AlignCenter, AlignBottom, status_texts[sentry_state->status]); - release_mutex((ValueMutex*)ctx, sentry_state); + furi_mutex_release(sentry_state->mutex); } static void sentry_safe_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -89,8 +89,8 @@ int32_t sentry_safe_app(void* p) { sentry_state->status = 0; - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, sentry_state, sizeof(SentryState))) { + sentry_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!sentry_state->mutex) { FURI_LOG_E("SentrySafe", "cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(sentry_state); @@ -98,7 +98,7 @@ int32_t sentry_safe_app(void* p) { } ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, sentry_safe_render_callback, &state_mutex); + view_port_draw_callback_set(view_port, sentry_safe_render_callback, sentry_state); view_port_input_callback_set(view_port, sentry_safe_input_callback, event_queue); // Open GUI and register view_port @@ -109,7 +109,7 @@ int32_t sentry_safe_app(void* p) { for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - SentryState* sentry_state = (SentryState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(sentry_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // press events @@ -151,7 +151,7 @@ int32_t sentry_safe_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, sentry_state); + furi_mutex_release(sentry_state->mutex); } // Reset GPIO pins to default state @@ -162,7 +162,7 @@ int32_t sentry_safe_app(void* p) { furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(sentry_state->mutex); free(sentry_state); return 0; diff --git a/applications/plugins/solitaire/defines.h b/applications/plugins/solitaire/defines.h index 1456f1964..8734395e8 100644 --- a/applications/plugins/solitaire/defines.h +++ b/applications/plugins/solitaire/defines.h @@ -53,4 +53,5 @@ typedef struct { int8_t selected_card; CardAnimation animation; uint8_t* buffer; + FuriMutex* mutex; } GameState; \ No newline at end of file diff --git a/applications/plugins/solitaire/solitaire.c b/applications/plugins/solitaire/solitaire.c index 26e0187dd..c9de533d3 100644 --- a/applications/plugins/solitaire/solitaire.c +++ b/applications/plugins/solitaire/solitaire.c @@ -155,10 +155,9 @@ static void draw_animation(Canvas* const canvas, const GameState* game_state) { } static void render_callback(Canvas* const canvas, void* ctx) { - const GameState* game_state = acquire_mutex((ValueMutex*)ctx, 25); - if(game_state == NULL) { - return; - } + furi_assert(ctx); + const GameState* game_state = ctx; + furi_mutex_acquire(game_state->mutex, FuriWaitForever); switch(game_state->state) { case GameStateAnimate: @@ -174,7 +173,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { break; } - release_mutex((ValueMutex*)ctx, game_state); + furi_mutex_release(game_state->mutex); } void remove_drag(GameState* game_state) { @@ -470,8 +469,8 @@ int32_t solitaire_app(void* p) { game_state->state = GameStateStart; game_state->processing = true; - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, game_state, sizeof(GameState))) { + game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!game_state->mutex) { FURI_LOG_E(APP_NAME, "cannot create mutex\r\n"); return_code = 255; goto free_and_exit; @@ -481,13 +480,13 @@ int32_t solitaire_app(void* p) { notification_message_block(notification, &sequence_display_backlight_enforce_on); 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, game_state); view_port_input_callback_set(view_port, input_callback, event_queue); FuriTimer* timer = furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue); furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30); - Gui* gui = furi_record_open("gui"); + Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); AppEvent event; @@ -497,7 +496,7 @@ int32_t solitaire_app(void* p) { for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 150); - GameState* localstate = (GameState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(game_state->mutex, FuriWaitForever); bool hadChange = false; if(event_status == FuriStatusOk) { if(event.type == EventTypeKey) { @@ -509,7 +508,7 @@ int32_t solitaire_app(void* p) { case InputKeyRight: case InputKeyLeft: case InputKeyOk: - localstate->input = event.input.key; + game_state->input = event.input.key; break; case InputKeyBack: processing = false; @@ -525,12 +524,12 @@ int32_t solitaire_app(void* p) { case InputKeyRight: case InputKeyLeft: case InputKeyOk: - if(event.input.key == InputKeyOk && localstate->state == GameStateStart) { - localstate->state = GameStatePlay; + if(event.input.key == InputKeyOk && game_state->state == GameStateStart) { + game_state->state = GameStatePlay; init(game_state); } else { hadChange = true; - localstate->input = event.input.key; + game_state->input = event.input.key; } break; case InputKeyBack: @@ -543,16 +542,16 @@ int32_t solitaire_app(void* p) { } } } else if(event.type == EventTypeTick) { - tick(localstate, notification); - processing = localstate->processing; - localstate->input = InputKeyMAX; + tick(game_state, notification); + processing = game_state->processing; + game_state->input = InputKeyMAX; } } else { //FURI_LOG_W(APP_NAME, "osMessageQueue: event timeout"); // event timeout } if(hadChange || game_state->state == GameStateAnimate) view_port_update(view_port); - release_mutex(&state_mutex, localstate); + furi_mutex_release(game_state->mutex); } notification_message_block(notification, &sequence_display_backlight_enforce_auto); @@ -562,7 +561,7 @@ int32_t solitaire_app(void* p) { furi_record_close(RECORD_GUI); furi_record_close(RECORD_NOTIFICATION); view_port_free(view_port); - delete_mutex(&state_mutex); + furi_mutex_free(game_state->mutex); free_and_exit: free(game_state->animation.buffer); diff --git a/applications/plugins/tetris_game/tetris_game.c b/applications/plugins/tetris_game/tetris_game.c index 0b813f26a..a569f46d4 100644 --- a/applications/plugins/tetris_game/tetris_game.c +++ b/applications/plugins/tetris_game/tetris_game.c @@ -71,6 +71,7 @@ typedef struct { uint16_t fallSpeed; GameState gameState; FuriTimer* timer; + FuriMutex* mutex; } TetrisState; typedef enum { @@ -127,11 +128,9 @@ static void tetris_game_draw_playfield(Canvas* const canvas, const TetrisState* } static void tetris_game_render_callback(Canvas* const canvas, void* ctx) { - const TetrisState* tetris_state = acquire_mutex((ValueMutex*)ctx, 25); - if(tetris_state == NULL) { - FURI_LOG_E("TetrisGame", "it null"); - return; - } + furi_assert(ctx); + const TetrisState* tetris_state = ctx; + furi_mutex_acquire(tetris_state->mutex, FuriWaitForever); tetris_game_draw_border(canvas); tetris_game_draw_playfield(canvas, tetris_state); @@ -159,7 +158,7 @@ static void tetris_game_render_callback(Canvas* const canvas, void* ctx) { canvas_set_font(canvas, FontSecondary); canvas_draw_str_aligned(canvas, 32, 73, AlignCenter, AlignBottom, buffer); } - release_mutex((ValueMutex*)ctx, tetris_state); + furi_mutex_release(tetris_state->mutex); } static void tetris_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -358,8 +357,8 @@ int32_t tetris_game_app() { TetrisState* tetris_state = malloc(sizeof(TetrisState)); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, tetris_state, sizeof(TetrisState))) { + tetris_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!tetris_state->mutex) { FURI_LOG_E("TetrisGame", "cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(tetris_state); @@ -376,7 +375,7 @@ int32_t tetris_game_app() { ViewPort* view_port = view_port_alloc(); view_port_set_orientation(view_port, ViewPortOrientationVertical); - view_port_draw_callback_set(view_port, tetris_game_render_callback, &state_mutex); + view_port_draw_callback_set(view_port, tetris_game_render_callback, tetris_state); view_port_input_callback_set(view_port, tetris_game_input_callback, event_queue); // Open GUI and register view_port @@ -399,7 +398,7 @@ int32_t tetris_game_app() { // This 10U implicitly sets the game loop speed. downRepeatCounter relies on this value FuriStatus event_status = furi_message_queue_get(event_queue, &event, 10U); - TetrisState* tetris_state = (TetrisState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(tetris_state->mutex, FuriWaitForever); memcpy(newPiece, &tetris_state->currPiece, sizeof(tetris_state->currPiece)); bool wasDownMove = false; @@ -466,7 +465,7 @@ int32_t tetris_game_app() { tetris_game_process_step(tetris_state, newPiece, wasDownMove); view_port_update(view_port); - release_mutex(&state_mutex, tetris_state); + furi_mutex_release(tetris_state->mutex); } furi_timer_free(tetris_state->timer); @@ -475,7 +474,7 @@ int32_t tetris_game_app() { furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(tetris_state->mutex); vTaskPrioritySet(timer_task, origTimerPrio); free(newPiece); free(tetris_state); diff --git a/applications/plugins/tictactoe_game/tictactoe_game.c b/applications/plugins/tictactoe_game/tictactoe_game.c index ec82192a8..e617e7c18 100644 --- a/applications/plugins/tictactoe_game/tictactoe_game.c +++ b/applications/plugins/tictactoe_game/tictactoe_game.c @@ -10,6 +10,7 @@ typedef enum { EventTypeTick, EventTypeKey } EventType; typedef struct { + FuriMutex* mutex; FuriTimer* timer; uint8_t selBoxX; uint8_t selBoxY; @@ -173,10 +174,9 @@ static void tictactoe_state_init(TicTacToeState* tictactoe_state) { } static void tictactoe_draw_callback(Canvas* const canvas, void* ctx) { - TicTacToeState* ticst = acquire_mutex((ValueMutex*)ctx, 25); - if(ticst == NULL) { - return; - } + furi_assert(ctx); + TicTacToeState* ticst = ctx; + furi_mutex_acquire(ticst->mutex, FuriWaitForever); if(ticst->selX > 3) { ticst->selX = 3; @@ -285,7 +285,7 @@ static void tictactoe_draw_callback(Canvas* const canvas, void* ctx) { tictactoe_draw(canvas, ticst); - release_mutex((ValueMutex*)ctx, ticst); + furi_mutex_release(ticst->mutex); } static void tictactoe_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -308,8 +308,9 @@ int32_t tictactoe_game_app(void* p) { TicTacToeState* tictactoe_state = malloc(sizeof(TicTacToeState)); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, tictactoe_state, sizeof(TicTacToeState))) { + tictactoe_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + + if(!tictactoe_state->mutex) { FURI_LOG_E(TAG, "Cannot create mutex\r\n"); furi_message_queue_free(event_queue); free(tictactoe_state); @@ -318,7 +319,7 @@ int32_t tictactoe_game_app(void* p) { // Set system callbacks ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, tictactoe_draw_callback, &state_mutex); + view_port_draw_callback_set(view_port, tictactoe_draw_callback, tictactoe_state); view_port_input_callback_set(view_port, tictactoe_input_callback, event_queue); tictactoe_state->timer = @@ -337,7 +338,7 @@ int32_t tictactoe_game_app(void* p) { GameEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - TicTacToeState* tictactoe_state = (TicTacToeState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(tictactoe_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { // Key events @@ -370,7 +371,7 @@ int32_t tictactoe_game_app(void* p) { } view_port_update(view_port); - release_mutex(&state_mutex, tictactoe_state); + furi_mutex_release(tictactoe_state->mutex); } furi_timer_free(tictactoe_state->timer); @@ -379,7 +380,7 @@ int32_t tictactoe_game_app(void* p) { furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(tictactoe_state->mutex); free(tictactoe_state); return 0; diff --git a/applications/plugins/totp/totp_app.c b/applications/plugins/totp/totp_app.c index f791e99de..322acd36c 100644 --- a/applications/plugins/totp/totp_app.c +++ b/applications/plugins/totp/totp_app.c @@ -23,12 +23,14 @@ #define IDLE_TIMEOUT 60000 static void render_callback(Canvas* const canvas, void* ctx) { - PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); + furi_assert(ctx); + PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(plugin_state != NULL) { totp_scene_director_render(canvas, plugin_state); } - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -137,8 +139,8 @@ int32_t totp_app() { return 254; } - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n"); totp_plugin_state_free(plugin_state); return 255; @@ -157,7 +159,7 @@ int32_t totp_app() { // Set system callbacks 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); // Open GUI and register view_port @@ -169,7 +171,7 @@ int32_t totp_app() { while(processing) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state_m = acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { if(event.type == EventTypeKey) { @@ -179,16 +181,16 @@ int32_t totp_app() { if(event.type == EventForceCloseApp) { processing = false; } else { - processing = totp_scene_director_handle_event(&event, plugin_state_m); + processing = totp_scene_director_handle_event(&event, plugin_state); } } else if( - plugin_state_m->pin_set && plugin_state_m->current_scene != TotpSceneAuthentication && + 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_m, TotpSceneAuthentication, NULL); + totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL); } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state_m); + furi_mutex_release(plugin_state->mutex); } totp_cli_unregister_command_handler(cli_context); @@ -199,7 +201,7 @@ int32_t totp_app() { gui_remove_view_port(plugin_state->gui, view_port); view_port_free(view_port); furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); + furi_mutex_free(plugin_state->mutex); totp_plugin_state_free(plugin_state); return 0; } diff --git a/applications/plugins/totp/types/plugin_state.h b/applications/plugins/totp/types/plugin_state.h index 0aad2e125..b5e78d0cf 100644 --- a/applications/plugins/totp/types/plugin_state.h +++ b/applications/plugins/totp/types/plugin_state.h @@ -87,4 +87,5 @@ typedef struct { * @brief Notification method */ NotificationMethod notification_method; + FuriMutex* mutex; } PluginState; diff --git a/applications/plugins/totp/workers/type_code/type_code.c b/applications/plugins/totp/workers/type_code/type_code.c index 3eb59047a..06fe7897e 100644 --- a/applications/plugins/totp/workers/type_code/type_code.c +++ b/applications/plugins/totp/workers/type_code/type_code.c @@ -57,8 +57,10 @@ static void totp_type_code_worker_type_code(TotpTypeCodeWorkerContext* context) } static int32_t totp_type_code_worker_callback(void* context) { - ValueMutex context_mutex; - if(!init_mutex(&context_mutex, context, sizeof(TotpTypeCodeWorkerContext))) { + furi_assert(context); + TotpTypeCodeWorkerContext* ctxx = context; + ctxx->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!ctxx->mutex) { return 251; } @@ -70,15 +72,16 @@ static int32_t totp_type_code_worker_callback(void* context) { furi_check((flags & FuriFlagError) == 0); //-V562 if(flags & TotpTypeCodeWorkerEventStop) break; - TotpTypeCodeWorkerContext* h_context = acquire_mutex_block(&context_mutex); + TotpTypeCodeWorkerContext* h_context = context; + furi_mutex_acquire(ctxx->mutex, FuriWaitForever); if(flags & TotpTypeCodeWorkerEventType) { totp_type_code_worker_type_code(h_context); } - release_mutex(&context_mutex, h_context); + furi_mutex_release(ctxx->mutex); } - delete_mutex(&context_mutex); + furi_mutex_free(ctxx->mutex); return 0; } diff --git a/applications/plugins/totp/workers/type_code/type_code.h b/applications/plugins/totp/workers/type_code/type_code.h index 27f2e02d4..b696420bc 100644 --- a/applications/plugins/totp/workers/type_code/type_code.h +++ b/applications/plugins/totp/workers/type_code/type_code.h @@ -12,6 +12,7 @@ typedef struct { FuriThread* thread; FuriMutex* string_sync; FuriHalUsbInterface* usb_mode_prev; + FuriMutex* mutex; } TotpTypeCodeWorkerContext; enum TotpTypeCodeWorkerEvents { diff --git a/applications/plugins/wifi_scanner/wifi_scanner.c b/applications/plugins/wifi_scanner/wifi_scanner.c index 826048e26..3a79ce16e 100644 --- a/applications/plugins/wifi_scanner/wifi_scanner.c +++ b/applications/plugins/wifi_scanner/wifi_scanner.c @@ -83,6 +83,7 @@ typedef enum EWorkerEventFlags { } EWorkerEventFlags; typedef struct SWiFiScannerApp { + FuriMutex* mutex; Gui* m_gui; FuriThread* m_worker_thread; NotificationApp* m_notification; @@ -162,10 +163,9 @@ void DrawSignalStrengthBar(Canvas* canvas, int rssi, int x, int y, int width, in } static void wifi_module_render_callback(Canvas* const canvas, void* ctx) { - SWiFiScannerApp* app = acquire_mutex((ValueMutex*)ctx, 25); - if(app == NULL) { - return; - } + furi_assert(ctx); + SWiFiScannerApp* app = ctx; + furi_mutex_acquire(app->mutex, FuriWaitForever); canvas_clear(canvas); @@ -433,7 +433,7 @@ static void wifi_module_render_callback(Canvas* const canvas, void* ctx) { break; } } - release_mutex((ValueMutex*)ctx, app); + furi_mutex_release(app->mutex); } static void wifi_module_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -460,14 +460,15 @@ static void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) { static int32_t uart_worker(void* context) { furi_assert(context); - SWiFiScannerApp* app = acquire_mutex((ValueMutex*)context, 25); + SWiFiScannerApp* app = context; + furi_mutex_acquire(app->mutex, FuriWaitForever); if(app == NULL) { return 1; } FuriStreamBuffer* rx_stream = app->m_rx_stream; - release_mutex((ValueMutex*)context, app); + furi_mutex_release(app->mutex); while(true) { uint32_t events = furi_thread_flags_wait( @@ -527,7 +528,8 @@ static int32_t uart_worker(void* context) { } while(end < stringSize); furi_string_free(chunk); - app = acquire_mutex((ValueMutex*)context, 25); + app = context; + furi_mutex_acquire(app->mutex, FuriWaitForever); if(app == NULL) { return 1; } @@ -584,7 +586,7 @@ static int32_t uart_worker(void* context) { } } - release_mutex((ValueMutex*)context, app); + furi_mutex_release(app->mutex); // Clear string array for(index = 0; index < EChunkArrayData_ENUM_MAX; ++index) { @@ -665,8 +667,9 @@ int32_t wifi_scanner_app(void* p) { #endif // ENABLE_MODULE_POWER #endif // ENABLE_MODULE_DETECTION - ValueMutex app_data_mutex; - if(!init_mutex(&app_data_mutex, app, sizeof(SWiFiScannerApp))) { + app->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + + if(!app->mutex) { WIFI_APP_LOG_E("cannot create mutex\r\n"); free(app); return 255; @@ -674,10 +677,10 @@ int32_t wifi_scanner_app(void* p) { WIFI_APP_LOG_I("Mutex created"); - app->m_notification = furi_record_open("notification"); + app->m_notification = furi_record_open(RECORD_NOTIFICATION); ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, wifi_module_render_callback, &app_data_mutex); + view_port_draw_callback_set(view_port, wifi_module_render_callback, app); view_port_input_callback_set(view_port, wifi_module_input_callback, event_queue); // Open GUI and register view_port @@ -691,7 +694,7 @@ int32_t wifi_scanner_app(void* p) { app->m_worker_thread = furi_thread_alloc(); furi_thread_set_name(app->m_worker_thread, "WiFiModuleUARTWorker"); furi_thread_set_stack_size(app->m_worker_thread, 1024); - furi_thread_set_context(app->m_worker_thread, &app_data_mutex); + furi_thread_set_context(app->m_worker_thread, app); furi_thread_set_callback(app->m_worker_thread, uart_worker); furi_thread_start(app->m_worker_thread); WIFI_APP_LOG_I("UART thread allocated"); @@ -710,7 +713,7 @@ int32_t wifi_scanner_app(void* p) { SPluginEvent event; for(bool processing = true; processing;) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - SWiFiScannerApp* app = (SWiFiScannerApp*)acquire_mutex_block(&app_data_mutex); + furi_mutex_acquire(app->mutex, FuriWaitForever); #if ENABLE_MODULE_DETECTION if(!app->m_wifiModuleAttached) { @@ -807,7 +810,7 @@ int32_t wifi_scanner_app(void* p) { #endif view_port_update(view_port); - release_mutex(&app_data_mutex, app); + furi_mutex_release(app->mutex); } WIFI_APP_LOG_I("Start exit app"); @@ -831,7 +834,7 @@ int32_t wifi_scanner_app(void* p) { // Close gui record furi_record_close(RECORD_GUI); - furi_record_close("notification"); + furi_record_close(RECORD_NOTIFICATION); app->m_gui = NULL; view_port_free(view_port); @@ -840,7 +843,7 @@ int32_t wifi_scanner_app(void* p) { furi_stream_buffer_free(app->m_rx_stream); - delete_mutex(&app_data_mutex); + furi_mutex_free(app->mutex); // Free rest free(app); diff --git a/applications/plugins/zombiez/zombiez.c b/applications/plugins/zombiez/zombiez.c index 2677d13e0..602eea723 100644 --- a/applications/plugins/zombiez/zombiez.c +++ b/applications/plugins/zombiez/zombiez.c @@ -52,6 +52,7 @@ typedef struct { } Projectile; typedef struct { + FuriMutex* mutex; GameState game_state; Player player; @@ -66,10 +67,9 @@ typedef struct { } PluginState; static void render_callback(Canvas* const canvas, void* ctx) { - const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); - if(plugin_state == NULL) { - return; - } + furi_assert(ctx); + const PluginState* plugin_state = ctx; + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); canvas_draw_frame(canvas, 0, 0, 128, 64); @@ -181,7 +181,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { //canvas_draw_str_aligned(canvas, 32, 16, AlignLeft, AlignBottom, info); //free(info); - release_mutex((ValueMutex*)ctx, plugin_state); + furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -295,8 +295,8 @@ int32_t zombiez_game_app(void* p) { PluginState* plugin_state = malloc(sizeof(PluginState)); zombiez_state_init(plugin_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(!plugin_state->mutex) { FURI_LOG_E("Zombiez", "cannot create mutex\r\n"); return_code = 255; goto free_and_exit; @@ -304,7 +304,7 @@ int32_t zombiez_game_app(void* p) { // Set system callbacks 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); FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue); @@ -321,7 +321,7 @@ int32_t zombiez_game_app(void* p) { bool isRunning = true; while(isRunning) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); + furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); if(event_status == FuriStatusOk) { if(event.type == EventTypeKey) { if(event.input.type == InputTypePress) { @@ -381,12 +381,10 @@ int32_t zombiez_game_app(void* p) { } else if(event.type == EventTypeTick) { tick(plugin_state); } - } else { - // event timeout } view_port_update(view_port); - release_mutex(&state_mutex, plugin_state); + furi_mutex_release(plugin_state->mutex); } furi_timer_free(timer); @@ -394,7 +392,7 @@ int32_t zombiez_game_app(void* p) { gui_remove_view_port(gui, view_port); furi_record_close(RECORD_GUI); view_port_free(view_port); - delete_mutex(&state_mutex); + furi_mutex_free(plugin_state->mutex); free_and_exit: free(plugin_state);