This commit is contained in:
Willy-JL
2024-06-11 22:46:09 +02:00
62 changed files with 1625 additions and 1133 deletions

View File

@@ -1,4 +1,3 @@
#include <core/common_defines.h>
#include <furi.h>
#include <furi_hal.h>

View File

@@ -72,7 +72,6 @@ CcidTestApp* ccid_test_app_alloc(void) {
//message queue
app->event_queue = furi_message_queue_alloc(8, sizeof(CcidTestAppEvent));
furi_check(app->event_queue);
view_port_input_callback_set(app->view_port, ccid_test_app_input_callback, app->event_queue);
return app;

View File

@@ -64,16 +64,9 @@ static void keypad_test_input_callback(InputEvent* input_event, void* ctx) {
int32_t keypad_test_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
furi_check(event_queue);
KeypadTestState state = {{false, false, false, false, false}, 0, 0, 0, 0, 0, NULL};
state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!state.mutex) {
FURI_LOG_E(TAG, "cannot create mutex");
return 0;
}
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, keypad_test_render_callback, &state);

View File

@@ -74,16 +74,8 @@ static void text_box_test_input_callback(InputEvent* input_event, void* ctx) {
int32_t text_box_element_test_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
furi_check(event_queue);
TextBoxTestState state = {.idx = 0, .mutex = NULL};
state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!state.mutex) {
FURI_LOG_E(TAG, "Cannot create mutex");
return 0;
}
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, text_box_test_render_callback, &state);

View File

@@ -76,6 +76,9 @@ void test_runner_free(TestRunner* instance) {
free(instance);
}
#define TEST_RUNNER_TMP_DIR EXT_PATH(".tmp")
#define TEST_RUNNER_TMP_UNIT_TESTS_DIR TEST_RUNNER_TMP_DIR "/unit_tests"
static bool test_runner_run_plugin(TestRunner* instance, const char* path) {
furi_assert(instance);
@@ -134,6 +137,16 @@ static void test_runner_run_internal(TestRunner* instance) {
File* directory = storage_file_alloc(instance->storage);
do {
if(!storage_simply_mkdir(instance->storage, TEST_RUNNER_TMP_DIR)) {
FURI_LOG_E(TAG, "Cannot create dir %s", TEST_RUNNER_TMP_DIR);
break;
}
if(!storage_simply_mkdir(instance->storage, TEST_RUNNER_TMP_UNIT_TESTS_DIR)) {
FURI_LOG_E(TAG, "Cannot create dir %s", TEST_RUNNER_TMP_UNIT_TESTS_DIR);
break;
}
if(!storage_dir_open(directory, PLUGINS_PATH)) {
FURI_LOG_E(TAG, "Failed to open directory %s", PLUGINS_PATH);
break;

View File

@@ -4,8 +4,8 @@
#include <toolbox/stream/stream.h>
#include "../test.h" // IWYU pragma: keep
#define TEST_DIR_NAME EXT_PATH(".tmp/unit_tests/ff")
#define TEST_DIR TEST_DIR_NAME "/"
#define TEST_DIR_NAME EXT_PATH("unit_tests_tmp")
static const char* test_filetype = "Flipper File test";
static const uint32_t test_version = 666;

View File

@@ -298,7 +298,8 @@ MU_TEST(flipper_format_string_test) {
MU_TEST(flipper_format_file_test) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
mu_check(flipper_format_file_open_always(flipper_format, EXT_PATH("flipper.fff")));
mu_check(
flipper_format_file_open_always(flipper_format, EXT_PATH(".tmp/unit_tests/flipper.fff")));
Stream* stream = flipper_format_get_raw_stream(flipper_format);
mu_check(flipper_format_write_header_cstr(flipper_format, test_filetype, test_version));

View File

@@ -0,0 +1,164 @@
#include "../test.h"
#include <furi.h>
#include <furi_hal.h>
#define TAG "TestFuriEventLoop"
#define EVENT_LOOP_EVENT_COUNT (256u)
typedef struct {
FuriMessageQueue* mq;
FuriEventLoop* producer_event_loop;
uint32_t producer_counter;
FuriEventLoop* consumer_event_loop;
uint32_t consumer_counter;
} TestFuriData;
bool test_furi_event_loop_producer_mq_callback(FuriMessageQueue* queue, void* context) {
furi_check(context);
TestFuriData* data = context;
furi_check(data->mq == queue, "Invalid queue");
FURI_LOG_I(
TAG, "producer_mq_callback: %lu %lu", data->producer_counter, data->consumer_counter);
// Remove and add should not cause crash
// if(data->producer_counter == EVENT_LOOP_EVENT_COUNT/2) {
// furi_event_loop_message_queue_remove(data->producer_event_loop, data->mq);
// furi_event_loop_message_queue_add(
// data->producer_event_loop,
// data->mq,
// FuriEventLoopEventOut,
// test_furi_event_loop_producer_mq_callback,
// data);
// }
if(data->producer_counter == EVENT_LOOP_EVENT_COUNT) {
furi_event_loop_stop(data->producer_event_loop);
return false;
}
data->producer_counter++;
furi_check(
furi_message_queue_put(data->mq, &data->producer_counter, 0) == FuriStatusOk,
"furi_message_queue_put failed");
furi_delay_us(furi_hal_random_get() % 1000);
return true;
}
int32_t test_furi_event_loop_producer(void* p) {
furi_check(p);
FURI_LOG_I(TAG, "producer start");
TestFuriData* data = p;
data->producer_event_loop = furi_event_loop_alloc();
furi_event_loop_message_queue_subscribe(
data->producer_event_loop,
data->mq,
FuriEventLoopEventOut,
test_furi_event_loop_producer_mq_callback,
data);
furi_event_loop_run(data->producer_event_loop);
furi_event_loop_message_queue_unsubscribe(data->producer_event_loop, data->mq);
furi_event_loop_free(data->producer_event_loop);
FURI_LOG_I(TAG, "producer end");
return 0;
}
bool test_furi_event_loop_consumer_mq_callback(FuriMessageQueue* queue, void* context) {
furi_check(context);
TestFuriData* data = context;
furi_check(data->mq == queue);
furi_delay_us(furi_hal_random_get() % 1000);
furi_check(furi_message_queue_get(data->mq, &data->consumer_counter, 0) == FuriStatusOk);
FURI_LOG_I(
TAG, "consumer_mq_callback: %lu %lu", data->producer_counter, data->consumer_counter);
// Remove and add should not cause crash
// if(data->producer_counter == EVENT_LOOP_EVENT_COUNT/2) {
// furi_event_loop_message_queue_remove(data->consumer_event_loop, data->mq);
// furi_event_loop_message_queue_add(
// data->consumer_event_loop,
// data->mq,
// FuriEventLoopEventIn,
// test_furi_event_loop_producer_mq_callback,
// data);
// }
if(data->consumer_counter == EVENT_LOOP_EVENT_COUNT) {
furi_event_loop_stop(data->consumer_event_loop);
return false;
}
return true;
}
int32_t test_furi_event_loop_consumer(void* p) {
furi_check(p);
FURI_LOG_I(TAG, "consumer start");
TestFuriData* data = p;
data->consumer_event_loop = furi_event_loop_alloc();
furi_event_loop_message_queue_subscribe(
data->consumer_event_loop,
data->mq,
FuriEventLoopEventIn,
test_furi_event_loop_consumer_mq_callback,
data);
furi_event_loop_run(data->consumer_event_loop);
furi_event_loop_message_queue_unsubscribe(data->consumer_event_loop, data->mq);
furi_event_loop_free(data->consumer_event_loop);
FURI_LOG_I(TAG, "consumer end");
return 0;
}
void test_furi_event_loop(void) {
TestFuriData data = {};
data.mq = furi_message_queue_alloc(16, sizeof(uint32_t));
FuriThread* producer_thread = furi_thread_alloc();
furi_thread_set_name(producer_thread, "producer_thread");
furi_thread_set_stack_size(producer_thread, 1 * 1024);
furi_thread_set_callback(producer_thread, test_furi_event_loop_producer);
furi_thread_set_context(producer_thread, &data);
furi_thread_start(producer_thread);
FuriThread* consumer_thread = furi_thread_alloc();
furi_thread_set_name(consumer_thread, "consumer_thread");
furi_thread_set_stack_size(consumer_thread, 1 * 1024);
furi_thread_set_callback(consumer_thread, test_furi_event_loop_consumer);
furi_thread_set_context(consumer_thread, &data);
furi_thread_start(consumer_thread);
// Wait for thread to complete their tasks
furi_thread_join(producer_thread);
furi_thread_join(consumer_thread);
// The test itself
mu_assert_int_eq(data.producer_counter, data.consumer_counter);
// Release memory
furi_thread_free(consumer_thread);
furi_thread_free(producer_thread);
furi_message_queue_free(data.mq);
}

View File

@@ -6,9 +6,9 @@
void test_furi_create_open(void);
void test_furi_concurrent_access(void);
void test_furi_pubsub(void);
void test_furi_memmgr(void);
void test_furi_memmgr_advanced(void);
void test_furi_event_loop(void);
static int foo = 0;
@@ -40,15 +40,19 @@ MU_TEST(mu_test_furi_memmgr) {
test_furi_memmgr_advanced();
}
MU_TEST(mu_test_furi_event_loop) {
test_furi_event_loop();
}
MU_TEST_SUITE(test_suite) {
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
MU_RUN_TEST(test_check);
// v2 tests
MU_RUN_TEST(mu_test_furi_create_open);
MU_RUN_TEST(mu_test_furi_pubsub);
MU_RUN_TEST(mu_test_furi_memmgr);
MU_RUN_TEST(mu_test_furi_event_loop);
}
int run_minunit_test_furi(void) {

View File

@@ -46,8 +46,8 @@ static RpcSessionContext rpc_session[TEST_RPC_SESSIONS];
#define MAX_RECEIVE_OUTPUT_TIMEOUT 3000
#define MAX_NAME_LENGTH 254
#define MAX_DATA_SIZE 512u // have to be exact as in rpc_storage.c
#define TEST_DIR_NAME EXT_PATH(".tmp/unit_tests/rpc")
#define TEST_DIR TEST_DIR_NAME "/"
#define TEST_DIR_NAME EXT_PATH("unit_tests_tmp")
#define MD5SUM_SIZE 16
#define PING_REQUEST 0

View File

@@ -15,6 +15,8 @@ static const char* stream_test_left_data = "There are two cardinal human sins ";
static const char* stream_test_right_data =
"from which all others derive: impatience and indolence.";
#define FILESTREAM_PATH EXT_PATH(".tmp/unit_tests/filestream.str")
MU_TEST_1(stream_composite_subtest, Stream* stream) {
const size_t data_size = 128;
uint8_t data[data_size];
@@ -304,15 +306,14 @@ MU_TEST(stream_composite_test) {
// test file stream
Storage* storage = furi_record_open(RECORD_STORAGE);
stream = file_stream_alloc(storage);
mu_check(
file_stream_open(stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
MU_RUN_TEST_1(stream_composite_subtest, stream);
stream_free(stream);
// test buffered file stream
stream = buffered_file_stream_alloc(storage);
mu_check(buffered_file_stream_open(
stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(
buffered_file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
MU_RUN_TEST_1(stream_composite_subtest, stream);
stream_free(stream);
furi_record_close(RECORD_STORAGE);
@@ -346,7 +347,7 @@ MU_TEST(stream_write_read_save_load_test) {
mu_check(stream_seek(stream_orig, 0, StreamOffsetFromStart));
mu_assert_int_eq(
strlen(stream_test_data),
stream_save_to_file(stream_orig, storage, EXT_PATH("filestream.str"), FSOM_CREATE_ALWAYS));
stream_save_to_file(stream_orig, storage, FILESTREAM_PATH, FSOM_CREATE_ALWAYS));
stream_free(stream_copy);
stream_free(stream_orig);
@@ -354,8 +355,7 @@ MU_TEST(stream_write_read_save_load_test) {
// load from file, read
Stream* stream_new = string_stream_alloc();
mu_assert_int_eq(
strlen(stream_test_data),
stream_load_from_file(stream_new, storage, EXT_PATH("filestream.str")));
strlen(stream_test_data), stream_load_from_file(stream_new, storage, FILESTREAM_PATH));
MU_RUN_TEST_1(stream_read_subtest, stream_new);
stream_free(stream_new);
@@ -396,15 +396,14 @@ MU_TEST(stream_split_test) {
// test file stream
Storage* storage = furi_record_open(RECORD_STORAGE);
stream = file_stream_alloc(storage);
mu_check(
file_stream_open(stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
MU_RUN_TEST_1(stream_split_subtest, stream);
stream_free(stream);
// test buffered stream
stream = buffered_file_stream_alloc(storage);
mu_check(buffered_file_stream_open(
stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(
buffered_file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
MU_RUN_TEST_1(stream_split_subtest, stream);
stream_free(stream);
@@ -424,8 +423,8 @@ MU_TEST(stream_buffered_write_after_read_test) {
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* stream = buffered_file_stream_alloc(storage);
mu_check(buffered_file_stream_open(
stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(
buffered_file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_assert_int_eq(strlen(stream_test_data), stream_write_cstring(stream, stream_test_data));
mu_check(stream_rewind(stream));
mu_assert_int_eq(prefix_len, stream_read(stream, (uint8_t*)buf, prefix_len));
@@ -458,8 +457,8 @@ MU_TEST(stream_buffered_large_file_test) {
// write test data to file
Stream* stream = buffered_file_stream_alloc(storage);
mu_check(buffered_file_stream_open(
stream, EXT_PATH("filestream.str"), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_check(
buffered_file_stream_open(stream, FILESTREAM_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS));
mu_assert_int_eq(0, stream_size(stream));
mu_assert_int_eq(furi_string_size(input_data), stream_write_string(stream, input_data));
mu_assert_int_eq(furi_string_size(input_data), stream_size(stream));

View File

@@ -6,11 +6,12 @@
#include <rpc/rpc_i.h>
#include <flipper.pb.h>
#include <core/event_loop.h>
static constexpr auto unit_tests_api_table = sort(create_array_t<sym_entry>(
API_METHOD(resource_manifest_reader_alloc, ResourceManifestReader*, (Storage*)),
API_METHOD(resource_manifest_reader_free, void, (ResourceManifestReader*)),
API_METHOD(resource_manifest_reader_open, bool, (ResourceManifestReader*, const char* filename)),
API_METHOD(resource_manifest_reader_open, bool, (ResourceManifestReader*, const char*)),
API_METHOD(resource_manifest_reader_next, ResourceManifestEntry*, (ResourceManifestReader*)),
API_METHOD(resource_manifest_reader_previous, ResourceManifestEntry*, (ResourceManifestReader*)),
API_METHOD(slix_process_iso15693_3_error, SlixError, (Iso15693_3Error)),
@@ -26,4 +27,17 @@ static constexpr auto unit_tests_api_table = sort(create_array_t<sym_entry>(
xQueueGenericSend,
BaseType_t,
(QueueHandle_t, const void* const, TickType_t, const BaseType_t)),
API_METHOD(furi_event_loop_alloc, FuriEventLoop*, (void)),
API_METHOD(furi_event_loop_free, void, (FuriEventLoop*)),
API_METHOD(
furi_event_loop_message_queue_subscribe,
void,
(FuriEventLoop*,
FuriMessageQueue*,
FuriEventLoopEvent,
FuriEventLoopMessageQueueCallback,
void*)),
API_METHOD(furi_event_loop_message_queue_unsubscribe, void, (FuriEventLoop*, FuriMessageQueue*)),
API_METHOD(furi_event_loop_run, void, (FuriEventLoop*)),
API_METHOD(furi_event_loop_stop, void, (FuriEventLoop*)),
API_VARIABLE(PB_Main_msg, PB_Main_msg_t)));

View File

@@ -40,7 +40,6 @@ static void usb_mouse_input_callback(InputEvent* input_event, void* ctx) {
int32_t usb_mouse_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(UsbMouseEvent));
furi_check(event_queue);
ViewPort* view_port = view_port_alloc();
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();

View File

@@ -17,7 +17,6 @@ MfUserDict* mf_user_dict_alloc(size_t max_keys_to_load) {
KeysDict* dict = keys_dict_alloc(
NFC_APP_MF_CLASSIC_DICT_USER_PATH, KeysDictModeOpenAlways, sizeof(MfClassicKey));
furi_assert(dict);
size_t dict_keys_num = keys_dict_get_total_keys(dict);
instance->keys_num = MIN(max_keys_to_load, dict_keys_num);
@@ -69,7 +68,6 @@ bool mf_user_dict_delete_key(MfUserDict* instance, uint32_t index) {
KeysDict* dict = keys_dict_alloc(
NFC_APP_MF_CLASSIC_DICT_USER_PATH, KeysDictModeOpenAlways, sizeof(MfClassicKey));
furi_assert(dict);
bool key_delete_success =
keys_dict_delete_key(dict, instance->keys_arr[index].data, sizeof(MfClassicKey));

View File

@@ -21,7 +21,7 @@ static void nfc_scene_info_on_enter_mf_plus(NfcApp* instance) {
nfc_render_mf_plus_info(data, NfcProtocolFormatTypeFull, temp_str);
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 52, furi_string_get_cstr(temp_str));
instance->widget, 0, 0, 128, 64, furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
}

View File

@@ -16,19 +16,15 @@ void nfc_scene_mf_classic_keys_on_enter(void* context) {
uint32_t flipper_dict_keys_total = 0;
KeysDict* dict = keys_dict_alloc(
NFC_APP_MF_CLASSIC_DICT_SYSTEM_PATH, KeysDictModeOpenExisting, sizeof(MfClassicKey));
if(dict) {
flipper_dict_keys_total = keys_dict_get_total_keys(dict);
keys_dict_free(dict);
}
flipper_dict_keys_total = keys_dict_get_total_keys(dict);
keys_dict_free(dict);
// Load user dict keys total
uint32_t user_dict_keys_total = 0;
dict = keys_dict_alloc(
NFC_APP_MF_CLASSIC_DICT_USER_PATH, KeysDictModeOpenAlways, sizeof(MfClassicKey));
if(dict) {
user_dict_keys_total = keys_dict_get_total_keys(dict);
keys_dict_free(dict);
}
user_dict_keys_total = keys_dict_get_total_keys(dict);
keys_dict_free(dict);
FuriString* temp_str = furi_string_alloc();
widget_add_string_element(

View File

@@ -31,7 +31,6 @@ bool nfc_scene_mf_classic_keys_add_on_event(void* context, SceneManagerEvent eve
// Add key to dict
KeysDict* dict = keys_dict_alloc(
NFC_APP_MF_CLASSIC_DICT_USER_PATH, KeysDictModeOpenAlways, sizeof(MfClassicKey));
furi_assert(dict);
MfClassicKey key = {};
memcpy(key.data, instance->byte_input_store, sizeof(MfClassicKey));

View File

@@ -343,7 +343,6 @@ void subghz_cli_command_rx(Cli* cli, FuriString* args, void* context) {
SubGhzCliCommandRx* instance = malloc(sizeof(SubGhzCliCommandRx));
instance->stream =
furi_stream_buffer_alloc(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
furi_check(instance->stream);
SubGhzEnvironment* environment = subghz_cli_environment_init();
@@ -424,7 +423,6 @@ void subghz_cli_command_rx_raw(Cli* cli, FuriString* args, void* context) {
SubGhzCliCommandRx* instance = malloc(sizeof(SubGhzCliCommandRx));
instance->stream =
furi_stream_buffer_alloc(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
furi_check(instance->stream);
// Configure radio
furi_hal_subghz_reset();

View File

@@ -23,7 +23,6 @@ Cli* cli_alloc(void) {
cli->session = NULL;
cli->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
furi_check(cli->mutex);
cli->idle_sem = furi_semaphore_alloc(1, 0);

View File

@@ -100,8 +100,8 @@ void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
furi_assert(dolphin);
furi_assert(event);
event->flag = furi_event_flag_alloc();
furi_check(event->flag);
furi_check(
furi_message_queue_put(dolphin->event_queue, event, FuriWaitForever) == FuriStatusOk);
furi_check(

View File

@@ -112,7 +112,6 @@ Popup* popup_alloc(void) {
Popup* popup = malloc(sizeof(Popup));
popup->view = view_alloc();
popup->timer = furi_timer_alloc(popup_timer_callback, FuriTimerTypeOnce, popup);
furi_assert(popup->timer);
popup->timer_period_in_ms = 1000;
popup->timer_enabled = false;

View File

@@ -83,7 +83,6 @@ void view_allocate_model(View* view, ViewModelType type, size_t size) {
} else if(view->model_type == ViewModelTypeLocking) {
ViewModelLocking* model = malloc(sizeof(ViewModelLocking));
model->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
furi_check(model->mutex);
model->data = malloc(size);
view->model = model;
} else {

View File

@@ -31,8 +31,23 @@ void view_dispatcher_free(ViewDispatcher* view_dispatcher) {
// Free ViewPort
view_port_free(view_dispatcher->view_port);
// Free internal queue
if(view_dispatcher->queue) {
furi_message_queue_free(view_dispatcher->queue);
if(view_dispatcher->input_queue) {
furi_event_loop_message_queue_unsubscribe(
view_dispatcher->event_loop, view_dispatcher->input_queue);
furi_message_queue_free(view_dispatcher->input_queue);
}
if(view_dispatcher->ascii_queue) {
furi_event_loop_message_queue_unsubscribe(
view_dispatcher->event_loop, view_dispatcher->ascii_queue);
furi_message_queue_free(view_dispatcher->ascii_queue);
}
if(view_dispatcher->event_queue) {
furi_event_loop_message_queue_unsubscribe(
view_dispatcher->event_loop, view_dispatcher->event_queue);
furi_message_queue_free(view_dispatcher->event_queue);
}
if(view_dispatcher->event_loop) {
furi_event_loop_free(view_dispatcher->event_loop);
}
// Free dispatcher
free(view_dispatcher);
@@ -40,8 +55,33 @@ void view_dispatcher_free(ViewDispatcher* view_dispatcher) {
void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->queue == NULL);
view_dispatcher->queue = furi_message_queue_alloc(16, sizeof(ViewDispatcherMessage));
furi_check(view_dispatcher->event_loop == NULL);
view_dispatcher->event_loop = furi_event_loop_alloc();
view_dispatcher->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
furi_event_loop_message_queue_subscribe(
view_dispatcher->event_loop,
view_dispatcher->input_queue,
FuriEventLoopEventIn,
view_dispatcher_run_input_callback,
view_dispatcher);
view_dispatcher->ascii_queue = furi_message_queue_alloc(8, sizeof(AsciiEvent));
furi_event_loop_message_queue_subscribe(
view_dispatcher->event_loop,
view_dispatcher->ascii_queue,
FuriEventLoopEventIn,
view_dispatcher_run_ascii_callback,
view_dispatcher);
view_dispatcher->event_queue = furi_message_queue_alloc(8, sizeof(uint32_t));
furi_event_loop_message_queue_subscribe(
view_dispatcher->event_loop,
view_dispatcher->event_queue,
FuriEventLoopEventIn,
view_dispatcher_run_event_callback,
view_dispatcher);
}
void view_dispatcher_set_event_callback_context(ViewDispatcher* view_dispatcher, void* context) {
@@ -72,50 +112,45 @@ void view_dispatcher_set_tick_event_callback(
view_dispatcher->tick_period = tick_period;
}
FuriEventLoop* view_dispatcher_get_event_loop(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop);
return view_dispatcher->event_loop;
}
void view_dispatcher_run(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->queue);
furi_check(view_dispatcher->event_loop);
uint32_t tick_period = view_dispatcher->tick_period == 0 ? FuriWaitForever :
view_dispatcher->tick_period;
ViewDispatcherMessage message;
while(1) {
if(furi_message_queue_get(view_dispatcher->queue, &message, tick_period) != FuriStatusOk) {
view_dispatcher_handle_tick_event(view_dispatcher);
continue;
}
if(message.type == ViewDispatcherMessageTypeStop) {
break;
} else if(message.type == ViewDispatcherMessageTypeInput) {
view_dispatcher_handle_input(view_dispatcher, &message.input);
} else if(message.type == ViewDispatcherMessageTypeAscii) {
view_dispatcher_handle_ascii(view_dispatcher, &message.ascii);
} else if(message.type == ViewDispatcherMessageTypeCustomEvent) {
view_dispatcher_handle_custom_event(view_dispatcher, message.custom_event);
}
}
furi_event_loop_tick_set(
view_dispatcher->event_loop,
tick_period,
view_dispatcher_handle_tick_event,
view_dispatcher);
furi_event_loop_run(view_dispatcher->event_loop);
// Wait till all input events delivered
InputEvent input;
while(view_dispatcher->ongoing_input) {
furi_message_queue_get(view_dispatcher->queue, &message, FuriWaitForever);
if(message.type == ViewDispatcherMessageTypeInput) {
uint8_t key_bit = (1 << message.input.key);
if(message.input.type == InputTypePress) {
view_dispatcher->ongoing_input |= key_bit;
} else if(message.input.type == InputTypeRelease) {
view_dispatcher->ongoing_input &= ~key_bit;
}
furi_message_queue_get(view_dispatcher->input_queue, &input, FuriWaitForever);
uint8_t key_bit = (1 << input.key);
if(input.type == InputTypePress) {
view_dispatcher->ongoing_input |= key_bit;
} else if(input.type == InputTypeRelease) {
view_dispatcher->ongoing_input &= ~key_bit;
}
}
}
void view_dispatcher_stop(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->queue);
ViewDispatcherMessage message;
message.type = ViewDispatcherMessageTypeStop;
furi_check(
furi_message_queue_put(view_dispatcher->queue, &message, FuriWaitForever) == FuriStatusOk);
furi_check(view_dispatcher->event_loop);
furi_event_loop_stop(view_dispatcher->event_loop);
}
void view_dispatcher_add_view(ViewDispatcher* view_dispatcher, uint32_t view_id, View* view) {
@@ -222,12 +257,9 @@ void view_dispatcher_draw_callback(Canvas* canvas, void* context) {
void view_dispatcher_input_callback(InputEvent* event, void* context) {
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->queue) {
ViewDispatcherMessage message;
message.type = ViewDispatcherMessageTypeInput;
message.input = *event;
if(view_dispatcher->input_queue) {
furi_check(
furi_message_queue_put(view_dispatcher->queue, &message, FuriWaitForever) ==
furi_message_queue_put(view_dispatcher->input_queue, event, FuriWaitForever) ==
FuriStatusOk);
} else {
view_dispatcher_handle_input(view_dispatcher, event);
@@ -239,12 +271,9 @@ bool view_dispatcher_ascii_callback(AsciiEvent* event, void* context) {
// So instead ViewDispatcher tells ViewPort that all events are consumed
// Then ViewDispatcher handles fallbacks the same way as ViewPort would have done
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->queue) {
ViewDispatcherMessage message;
message.type = ViewDispatcherMessageTypeAscii;
message.ascii = *event;
if(view_dispatcher->ascii_queue) {
furi_check(
furi_message_queue_put(view_dispatcher->queue, &message, FuriWaitForever) ==
furi_message_queue_put(view_dispatcher->ascii_queue, event, FuriWaitForever) ==
FuriStatusOk);
} else {
view_dispatcher_handle_ascii(view_dispatcher, event);
@@ -351,7 +380,8 @@ void view_dispatcher_handle_ascii(ViewDispatcher* view_dispatcher, AsciiEvent* e
}
}
void view_dispatcher_handle_tick_event(ViewDispatcher* view_dispatcher) {
void view_dispatcher_handle_tick_event(void* context) {
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->tick_event_callback) {
view_dispatcher->tick_event_callback(view_dispatcher->event_context);
}
@@ -370,14 +400,11 @@ void view_dispatcher_handle_custom_event(ViewDispatcher* view_dispatcher, uint32
void view_dispatcher_send_custom_event(ViewDispatcher* view_dispatcher, uint32_t event) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->queue);
ViewDispatcherMessage message;
message.type = ViewDispatcherMessageTypeCustomEvent;
message.custom_event = event;
furi_check(view_dispatcher->event_loop);
furi_check(
furi_message_queue_put(view_dispatcher->queue, &message, FuriWaitForever) == FuriStatusOk);
furi_message_queue_put(view_dispatcher->event_queue, &event, FuriWaitForever) ==
FuriStatusOk);
}
static const ViewPortOrientation view_dispatcher_view_port_orientation_table[] = {
@@ -409,7 +436,7 @@ void view_dispatcher_set_current_view(ViewDispatcher* view_dispatcher, View* vie
view_port_update(view_dispatcher->view_port);
} else {
view_port_enabled_set(view_dispatcher->view_port, false);
if(view_dispatcher->queue) {
if(view_dispatcher->event_loop) {
view_dispatcher_stop(view_dispatcher);
}
}
@@ -425,3 +452,39 @@ void view_dispatcher_update(View* view, void* context) {
view_port_update(view_dispatcher->view_port);
}
}
bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context) {
furi_assert(context);
ViewDispatcher* instance = context;
furi_assert(instance->event_queue == queue);
uint32_t event;
furi_check(furi_message_queue_get(instance->event_queue, &event, 0) == FuriStatusOk);
view_dispatcher_handle_custom_event(instance, event);
return true;
}
bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context) {
furi_assert(context);
ViewDispatcher* instance = context;
furi_assert(instance->input_queue == queue);
InputEvent input;
furi_check(furi_message_queue_get(instance->input_queue, &input, 0) == FuriStatusOk);
view_dispatcher_handle_input(instance, &input);
return true;
}
bool view_dispatcher_run_ascii_callback(FuriMessageQueue* queue, void* context) {
furi_assert(context);
ViewDispatcher* instance = context;
furi_assert(instance->ascii_queue == queue);
AsciiEvent ascii;
furi_check(furi_message_queue_get(instance->ascii_queue, &ascii, 0) == FuriStatusOk);
view_dispatcher_handle_ascii(instance, &ascii);
return true;
}

View File

@@ -47,8 +47,8 @@ void view_dispatcher_free(ViewDispatcher* view_dispatcher);
/** Enable queue support
*
* If queue enabled all input and custom events will be dispatched throw
* internal queue
* Allocates event_loop, input and event message queues. Must be used with
* `view_dispatcher_run`
*
* @param view_dispatcher ViewDispatcher instance
*/
@@ -101,6 +101,20 @@ void view_dispatcher_set_tick_event_callback(
*/
void view_dispatcher_set_event_callback_context(ViewDispatcher* view_dispatcher, void* context);
/** Get event_loop instance
*
* event_loop instance is allocated on `view_dispatcher_enable_queue` and used
* in view_dispatcher_run.
*
* You can add your objects into event_loop instance, but don't run the loop on
* your side it will cause issues with input processing on dispatcher stop.
*
* @param view_dispatcher ViewDispatcher instance
*
* @return The event_loop instance.
*/
FuriEventLoop* view_dispatcher_get_event_loop(ViewDispatcher* view_dispatcher);
/** Run ViewDispatcher
*
* Use only after queue enabled

View File

@@ -5,7 +5,6 @@
#pragma once
#include <furi.h>
#include <m-dict.h>
#include "view_dispatcher.h"
@@ -15,7 +14,11 @@
DICT_DEF2(ViewDict, uint32_t, M_DEFAULT_OPLIST, View*, M_PTR_OPLIST)
struct ViewDispatcher {
FuriMessageQueue* queue;
FuriEventLoop* event_loop;
FuriMessageQueue* input_queue;
FuriMessageQueue* ascii_queue;
FuriMessageQueue* event_queue;
Gui* gui;
ViewPort* view_port;
ViewDict_t views;
@@ -32,22 +35,6 @@ struct ViewDispatcher {
void* event_context;
};
typedef enum {
ViewDispatcherMessageTypeInput,
ViewDispatcherMessageTypeAscii,
ViewDispatcherMessageTypeCustomEvent,
ViewDispatcherMessageTypeStop,
} ViewDispatcherMessageType;
typedef struct {
ViewDispatcherMessageType type;
union {
InputEvent input;
AsciiEvent ascii;
uint32_t custom_event;
};
} ViewDispatcherMessage;
/** ViewPort Draw Callback */
void view_dispatcher_draw_callback(Canvas* canvas, void* context);
@@ -64,7 +51,7 @@ void view_dispatcher_handle_input(ViewDispatcher* view_dispatcher, InputEvent* e
void view_dispatcher_handle_ascii(ViewDispatcher* view_dispatcher, AsciiEvent* event);
/** Tick handler */
void view_dispatcher_handle_tick_event(ViewDispatcher* view_dispatcher);
void view_dispatcher_handle_tick_event(void* context);
/** Custom event handler */
void view_dispatcher_handle_custom_event(ViewDispatcher* view_dispatcher, uint32_t event);
@@ -74,3 +61,12 @@ void view_dispatcher_set_current_view(ViewDispatcher* view_dispatcher, View* vie
/** ViewDispatcher update event */
void view_dispatcher_update(View* view, void* context);
/** ViewDispatcher run event loop event callback */
bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context);
/** ViewDispatcher run event loop input callback */
bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context);
/** ViewDispatcher run event loop ascii callback */
bool view_dispatcher_run_ascii_callback(FuriMessageQueue* queue, void* context);

View File

@@ -99,8 +99,6 @@ static void render_callback(Canvas* canvas, void* _ctx) {
int32_t passport_app(void* p) {
UNUSED(p);
FuriSemaphore* semaphore = furi_semaphore_alloc(1, 0);
furi_assert(semaphore);
ViewPort* view_port = view_port_alloc();
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);