Skip to content

Commit

Permalink
Furi, FuriHal: remove FreeRTOS headers leaks (flipperdevices#3179)
Browse files Browse the repository at this point in the history
* Furi: remove direct FreeRTOS timers use
* Furi: eliminate FreeRTOS headers leak. What did it cost? Everything...
* SubGhz: proper public api for protocols. Format Sources.
* Furi: slightly less redundant declarations
* Desktop: proper types in printf
* Sync API Symbols
* Furi: add timer reset and fix dolphin service, fix unit tests
* Furi: proper timer restart method naming and correct behavior in timer stopped state.

---------

Co-authored-by: hedger <[email protected]>
  • Loading branch information
skotopes and hedger authored Nov 1, 2023
1 parent 7bd3bd7 commit aa06328
Show file tree
Hide file tree
Showing 68 changed files with 316 additions and 472 deletions.
2 changes: 1 addition & 1 deletion applications/debug/direct_draw/direct_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void direct_draw_run(DirectDraw* instance) {
size_t counter = 0;
float fps = 0;

vTaskPrioritySet(furi_thread_get_current_id(), FuriThreadPriorityIdle);
furi_thread_set_current_priority(FuriThreadPriorityIdle);

do {
size_t elapsed = DWT->CYCCNT - start;
Expand Down
10 changes: 6 additions & 4 deletions applications/debug/unit_tests/rpc/rpc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <cli/cli.h>
#include <loader/loader.h>
#include <protobuf_version.h>

#include <FreeRTOS.h>
#include <semphr.h>

LIST_DEF(MsgList, PB_Main, M_POD_OPLIST)
Expand All @@ -36,7 +38,7 @@ typedef struct {
FuriStreamBuffer* output_stream;
SemaphoreHandle_t close_session_semaphore;
SemaphoreHandle_t terminate_semaphore;
TickType_t timeout;
uint32_t timeout;
} RpcSessionContext;

static RpcSessionContext rpc_session[TEST_RPC_SESSIONS];
Expand Down Expand Up @@ -544,7 +546,7 @@ static bool test_rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_
RpcSessionContext* session_context = istream->state;
size_t bytes_received = 0;

TickType_t now = xTaskGetTickCount();
uint32_t now = furi_get_tick();
int32_t time_left = session_context->timeout - now;
time_left = MAX(time_left, 0);
bytes_received =
Expand Down Expand Up @@ -688,7 +690,7 @@ static void test_rpc_decode_and_compare(MsgList_t expected_msg_list, uint8_t ses
furi_check(!MsgList_empty_p(expected_msg_list));
furi_check(session < TEST_RPC_SESSIONS);

rpc_session[session].timeout = xTaskGetTickCount() + MAX_RECEIVE_OUTPUT_TIMEOUT;
rpc_session[session].timeout = furi_get_tick() + MAX_RECEIVE_OUTPUT_TIMEOUT;
pb_istream_t istream = {
.callback = test_rpc_pb_stream_read,
.state = &rpc_session[session],
Expand All @@ -712,7 +714,7 @@ static void test_rpc_decode_and_compare(MsgList_t expected_msg_list, uint8_t ses
pb_release(&PB_Main_msg, &result);
}

rpc_session[session].timeout = xTaskGetTickCount() + 50;
rpc_session[session].timeout = furi_get_tick() + 50;
if(pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
mu_fail("decoded more than expected");
}
Expand Down
4 changes: 2 additions & 2 deletions applications/debug/unit_tests/test_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const UnitTest unit_tests[] = {
void minunit_print_progress() {
static const char progress[] = {'\\', '|', '/', '-'};
static uint8_t progress_counter = 0;
static TickType_t last_tick = 0;
TickType_t current_tick = xTaskGetTickCount();
static uint32_t last_tick = 0;
uint32_t current_tick = furi_get_tick();
if(current_tick - last_tick > 20) {
last_tick = current_tick;
printf("[%c]\033[3D", progress[++progress_counter % COUNT_OF(progress)]);
Expand Down
5 changes: 2 additions & 3 deletions applications/main/infrared/infrared_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,17 @@ void infrared_play_notification_message(
}

void infrared_show_loading_popup(const InfraredApp* infrared, bool show) {
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
ViewStack* view_stack = infrared->view_stack;
Loading* loading = infrared->loading;

if(show) {
// Raise timer priority so that animations can play
vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
view_stack_add_view(view_stack, loading_get_view(loading));
} else {
view_stack_remove_view(view_stack, loading_get_view(loading));
// Restore default timer priority
vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
}
}

Expand Down
5 changes: 2 additions & 3 deletions applications/main/nfc/nfc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,14 @@ bool nfc_load_from_file_select(NfcApp* instance) {

void nfc_show_loading_popup(void* context, bool show) {
NfcApp* nfc = context;
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);

if(show) {
// Raise timer priority so that animations can play
vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewLoading);
} else {
// Restore default timer priority
vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
}
}

Expand Down
6 changes: 3 additions & 3 deletions applications/main/subghz/views/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void subghz_view_receiver_set_lock(SubGhzViewReceiver* subghz_receiver, bool loc
SubGhzViewReceiverModel * model,
{ model->bar_show = SubGhzViewReceiverBarShowLock; },
true);
furi_timer_start(subghz_receiver->timer, pdMS_TO_TICKS(1000));
furi_timer_start(subghz_receiver->timer, 1000);
} else {
with_view_model(
subghz_receiver->view,
Expand Down Expand Up @@ -316,7 +316,7 @@ bool subghz_view_receiver_input(InputEvent* event, void* context) {
{ model->bar_show = SubGhzViewReceiverBarShowToUnlockPress; },
true);
if(subghz_receiver->lock_count == 0) {
furi_timer_start(subghz_receiver->timer, pdMS_TO_TICKS(1000));
furi_timer_start(subghz_receiver->timer, 1000);
}
if(event->key == InputKeyBack && event->type == InputTypeShort) {
subghz_receiver->lock_count++;
Expand All @@ -330,7 +330,7 @@ bool subghz_view_receiver_input(InputEvent* event, void* context) {
{ model->bar_show = SubGhzViewReceiverBarShowUnlock; },
true);
//subghz_receiver->lock = false;
furi_timer_start(subghz_receiver->timer, pdMS_TO_TICKS(650));
furi_timer_start(subghz_receiver->timer, 650);
}

return true;
Expand Down
3 changes: 1 addition & 2 deletions applications/services/desktop/animations/animation_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdint.h>
#include <furi.h>
#include <furi_hal.h>
#include <portmacro.h>
#include <dolphin/dolphin.h>
#include <power/power_service/power.h>
#include <storage/storage.h>
Expand Down Expand Up @@ -450,7 +449,7 @@ void animation_manager_unload_and_stall_animation(AnimationManager* animation_ma
animation_manager->state = AnimationManagerStateFreezedIdle;

animation_manager->freezed_animation_time_left =
xTimerGetExpiryTime(animation_manager->idle_animation_timer) - xTaskGetTickCount();
furi_timer_get_expire_time(animation_manager->idle_animation_timer) - furi_get_tick();
if(animation_manager->freezed_animation_time_left < 0) {
animation_manager->freezed_animation_time_left = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions applications/services/desktop/animations/animation_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ static bool animation_storage_load_frames(
if(file_info.size > max_filesize) {
FURI_LOG_E(
TAG,
"Filesize %lld, max: %d (width %d, height %d)",
"Filesize %llu, max: %zu (width %u, height %u)",
file_info.size,
max_filesize,
width,
Expand All @@ -329,7 +329,7 @@ static bool animation_storage_load_frames(
if(!frames_ok) {
FURI_LOG_E(
TAG,
"Load \'%s\' failed, %dx%d, size: %lld",
"Load \'%s\' failed, %ux%u, size: %llu",
furi_string_get_cstr(filename),
width,
height,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct {
uint8_t active_bubbles;
uint8_t passive_bubbles;
uint8_t active_shift;
TickType_t active_ended_at;
uint32_t active_ended_at;
Icon* freeze_frame;
} BubbleAnimationViewModel;

Expand Down Expand Up @@ -154,7 +154,7 @@ static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
if(model->current != NULL) {
if(!force) {
if((model->active_ended_at + model->current->active_cooldown * 1000) >
xTaskGetTickCount()) {
furi_get_tick()) {
activate = false;
} else if(model->active_shift) {
activate = false;
Expand Down Expand Up @@ -215,7 +215,7 @@ static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
model->active_cycle = 0;
model->current_frame = 0;
model->current_bubble = bubble_animation_pick_bubble(model, false);
model->active_ended_at = xTaskGetTickCount();
model->active_ended_at = furi_get_tick();
}

if(model->current_bubble) {
Expand Down Expand Up @@ -355,7 +355,7 @@ void bubble_animation_view_set_animation(
furi_assert(model);
model->current = new_animation;

model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
model->active_ended_at = furi_get_tick() - (model->current->active_cooldown * 1000);
model->active_bubbles = 0;
model->passive_bubbles = 0;
for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include "one_shot_animation_view.h"
#include <furi.h>
#include <portmacro.h>
#include <gui/canvas.h>
#include <gui/view.h>
#include <gui/icon_i.h>
Expand All @@ -11,7 +10,7 @@ typedef void (*OneShotInteractCallback)(void*);

struct OneShotView {
View* view;
TimerHandle_t update_timer;
FuriTimer* update_timer;
OneShotInteractCallback interact_callback;
void* interact_callback_context;
};
Expand All @@ -22,8 +21,8 @@ typedef struct {
bool block_input;
} OneShotViewModel;

static void one_shot_view_update_timer_callback(TimerHandle_t xTimer) {
OneShotView* view = (void*)pvTimerGetTimerID(xTimer);
static void one_shot_view_update_timer_callback(void* context) {
OneShotView* view = context;

OneShotViewModel* model = view_get_model(view->view);
if((model->index + 1) < model->icon->frame_count) {
Expand Down Expand Up @@ -81,7 +80,7 @@ OneShotView* one_shot_view_alloc(void) {
OneShotView* view = malloc(sizeof(OneShotView));
view->view = view_alloc();
view->update_timer =
xTimerCreate(NULL, 1000, pdTRUE, view, one_shot_view_update_timer_callback);
furi_timer_alloc(one_shot_view_update_timer_callback, FuriTimerTypePeriodic, view);

view_allocate_model(view->view, ViewModelTypeLocking, sizeof(OneShotViewModel));
view_set_context(view->view, view);
Expand All @@ -94,7 +93,7 @@ OneShotView* one_shot_view_alloc(void) {
void one_shot_view_free(OneShotView* view) {
furi_assert(view);

xTimerDelete(view->update_timer, portMAX_DELAY);
furi_timer_free(view->update_timer);
view_free(view->view);
view->view = NULL;
free(view);
Expand All @@ -120,7 +119,7 @@ void one_shot_view_start_animation(OneShotView* view, const Icon* icon) {
model->icon = icon;
model->block_input = true;
view_commit_model(view->view, true);
xTimerChangePeriod(view->update_timer, 1000 / model->icon->frame_rate, portMAX_DELAY);
furi_timer_start(view->update_timer, 1000 / model->icon->frame_rate);
}

View* one_shot_view_get_view(OneShotView* view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <gui/scene_manager.h>
#include <gui/view_stack.h>
#include <stdint.h>
#include <portmacro.h>

#include "../desktop.h"
#include "../desktop_i.h"
Expand Down
23 changes: 9 additions & 14 deletions applications/services/desktop/scenes/desktop_scene_pin_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <gui/scene_manager.h>
#include <gui/view_stack.h>
#include <stdint.h>
#include <portmacro.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>

Expand All @@ -20,7 +19,7 @@
#define INPUT_PIN_VIEW_TIMEOUT 15000

typedef struct {
TimerHandle_t timer;
FuriTimer* timer;
} DesktopScenePinInputState;

static void desktop_scene_locked_light_red(bool value) {
Expand All @@ -33,17 +32,16 @@ static void desktop_scene_locked_light_red(bool value) {
furi_record_close(RECORD_NOTIFICATION);
}

static void
desktop_scene_pin_input_set_timer(Desktop* desktop, bool enable, TickType_t new_period) {
static void desktop_scene_pin_input_set_timer(Desktop* desktop, bool enable, uint32_t new_period) {
furi_assert(desktop);

DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
desktop->scene_manager, DesktopScenePinInput);
furi_assert(state);
if(enable) {
xTimerChangePeriod(state->timer, new_period, portMAX_DELAY);
furi_timer_start(state->timer, new_period);
} else {
xTimerStop(state->timer, portMAX_DELAY);
furi_timer_stop(state->timer);
}
}

Expand All @@ -64,8 +62,8 @@ static void desktop_scene_pin_input_done_callback(const PinCode* pin_code, void*
}
}

static void desktop_scene_pin_input_timer_callback(TimerHandle_t timer) {
Desktop* desktop = pvTimerGetTimerID(timer);
static void desktop_scene_pin_input_timer_callback(void* context) {
Desktop* desktop = context;

view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopPinInputEventResetWrongPinLabel);
Expand All @@ -84,7 +82,7 @@ void desktop_scene_pin_input_on_enter(void* context) {

DesktopScenePinInputState* state = malloc(sizeof(DesktopScenePinInputState));
state->timer =
xTimerCreate(NULL, 10000, pdFALSE, desktop, desktop_scene_pin_input_timer_callback);
furi_timer_alloc(desktop_scene_pin_input_timer_callback, FuriTimerTypeOnce, desktop);
scene_manager_set_scene_state(desktop->scene_manager, DesktopScenePinInput, (uint32_t)state);

desktop_view_pin_input_hide_pin(desktop->pin_input_view, true);
Expand Down Expand Up @@ -149,10 +147,7 @@ void desktop_scene_pin_input_on_exit(void* context) {

DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
desktop->scene_manager, DesktopScenePinInput);
xTimerStop(state->timer, portMAX_DELAY);
while(xTimerIsTimerActive(state->timer)) {
furi_delay_tick(1);
}
xTimerDelete(state->timer, portMAX_DELAY);

furi_timer_free(state->timer);
free(state);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <furi.h>
#include <FreeRTOS.h>
#include <portmacro.h>
#include <gui/scene_manager.h>

#include "../desktop_i.h"
Expand Down
Loading

0 comments on commit aa06328

Please sign in to comment.