diff --git a/CHANGELOG.md b/CHANGELOG.md index c782166a4..2fec85590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,10 @@ - OFW: On SD insert load BT, Desktop, Dolphin, Expansion, Notification, Region files (by @gsurkov) - On SD insert also load Momentum settings, Asset Packs, FindMy Flipper, NameSpoof, SubGHz options, and migrate files (by @Willy-JL) - Furi: Re-enabled `FURI_TRACE` since LFS removal frees DFU, will get better crash messages with source code path (by @Willy-JL) +- OFW: Sub-GHz: Add Dickert MAHS garage door protocol (by @OevreFlataeker) - OFW: RFID: Add GProxII support (by @BarTenderNZ) - OFW: iButton: Support ID writing (by @Astrrra) +- OFW: GUI: Added a text input that only accepts full numbers (by @leedave) - FBT: - OFW: Add `-Wundef` to compiler options (by @hedger) - OFW: Ensure that all images conform specification (by @skyhawkillusions & @hedger) @@ -31,14 +33,22 @@ - Big cleanup of services and settings handling, refactor some old code (by @Willy-JL) - Update all settings paths to use equivalents like OFW or UL for better compatibility (by @Willy-JL) - OFW: NFC: Refactor detected protocols list (by @Astrrra) +- Furi: + - OFW: FuriEventLoop Pt.2 with `Mutex` `Semaphore` `StreamBuffer`, refactor Power service (by @gsurkov) + - OFW: Update string documentation (by @skotopes) - OFW: CCID: App refactor (by @kidbomb) -- OFW: Furi: Update string documentation (by @skotopes) - OFW: FBT: Toolchain v39 (by @hedger) ### Fixed: -- GUI: Fix Dark Mode after XOR canvas color, like in NFC dict attack (by @Willy-JL) -- OFW: NFC: Fix plantain balance string (by @Astrrra) +- GUI: + - Fix Dark Mode after XOR canvas color, like in NFC dict attack (by @Willy-JL) + - OFW: Make file extensions case-insensitive (by @gsurkov)) +- NFC: + - OFW: Fix plantain balance string (by @Astrrra) + - OFW: Now fifo size in ST25 chip is calculated properly (by @RebornedBrain) - OFW: Infrared: Fix cumulative error in infrared signals (by @gsurkov) +- OFW: Desktop: Separate callbacks for dolphin and storage subscriptions (by @skotopes) +- OFW: FBT: Improved size validator for updater image (by @hedger) - OFW: JS: Ensure proper closure of variadic function in `mjs_array` (by @derskythe) ### Removed: diff --git a/applications/debug/accessor/accessor_view_manager.cpp b/applications/debug/accessor/accessor_view_manager.cpp index 955c0b286..aeb90c297 100644 --- a/applications/debug/accessor/accessor_view_manager.cpp +++ b/applications/debug/accessor/accessor_view_manager.cpp @@ -5,45 +5,49 @@ AccessorAppViewManager::AccessorAppViewManager() { event_queue = furi_message_queue_alloc(10, sizeof(AccessorEvent)); - view_dispatcher = view_dispatcher_alloc(); - auto callback = cbc::obtain_connector(this, &AccessorAppViewManager::previous_view_callback); + view_holder = view_holder_alloc(); + auto callback = + cbc::obtain_connector(this, &AccessorAppViewManager::view_holder_back_callback); // allocate views submenu = submenu_alloc(); - add_view(ViewType::Submenu, submenu_get_view(submenu)); - popup = popup_alloc(); - add_view(ViewType::Popup, popup_get_view(popup)); + + // set back callback + view_holder_set_back_callback(view_holder, callback, NULL); gui = static_cast(furi_record_open(RECORD_GUI)); - view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen); - - // set previous view callback for all views - view_set_previous_callback(submenu_get_view(submenu), callback); - view_set_previous_callback(popup_get_view(popup), callback); + view_holder_attach_to_gui(view_holder, gui); } AccessorAppViewManager::~AccessorAppViewManager() { - // remove views - view_dispatcher_remove_view( - view_dispatcher, static_cast(AccessorAppViewManager::ViewType::Submenu)); - view_dispatcher_remove_view( - view_dispatcher, static_cast(AccessorAppViewManager::ViewType::Popup)); - + // remove current view + view_holder_set_view(view_holder, NULL); // free view modules furi_record_close(RECORD_GUI); submenu_free(submenu); popup_free(popup); - - // free dispatcher - view_dispatcher_free(view_dispatcher); - + // free view holder + view_holder_free(view_holder); // free event queue furi_message_queue_free(event_queue); } void AccessorAppViewManager::switch_to(ViewType type) { - view_dispatcher_switch_to_view(view_dispatcher, static_cast(type)); + View* view; + + switch(type) { + case ViewType::Submenu: + view = submenu_get_view(submenu); + break; + case ViewType::Popup: + view = popup_get_view(popup); + break; + default: + furi_crash(); + } + + view_holder_set_view(view_holder, view); } Submenu* AccessorAppViewManager::get_submenu() { @@ -65,16 +69,10 @@ void AccessorAppViewManager::send_event(AccessorEvent* event) { furi_check(result == FuriStatusOk); } -uint32_t AccessorAppViewManager::previous_view_callback(void*) { +void AccessorAppViewManager::view_holder_back_callback(void*) { if(event_queue != NULL) { AccessorEvent event; event.type = AccessorEvent::Type::Back; send_event(&event); } - - return VIEW_IGNORE; -} - -void AccessorAppViewManager::add_view(ViewType view_type, View* view) { - view_dispatcher_add_view(view_dispatcher, static_cast(view_type), view); } diff --git a/applications/debug/accessor/accessor_view_manager.h b/applications/debug/accessor/accessor_view_manager.h index 66e54e41c..c0a12cbe8 100644 --- a/applications/debug/accessor/accessor_view_manager.h +++ b/applications/debug/accessor/accessor_view_manager.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #include #include "accessor_event.h" @@ -10,7 +10,6 @@ public: enum class ViewType : uint8_t { Submenu, Popup, - Tune, }; FuriMessageQueue* event_queue; @@ -27,11 +26,10 @@ public: Popup* get_popup(void); private: - ViewDispatcher* view_dispatcher; Gui* gui; + ViewHolder* view_holder; - uint32_t previous_view_callback(void* context); - void add_view(ViewType view_type, View* view); + void view_holder_back_callback(void* context); // view elements Submenu* submenu; diff --git a/applications/debug/battery_test_app/battery_test_app.c b/applications/debug/battery_test_app/battery_test_app.c index 5f9934e77..363c8f4d5 100644 --- a/applications/debug/battery_test_app/battery_test_app.c +++ b/applications/debug/battery_test_app/battery_test_app.c @@ -42,7 +42,6 @@ BatteryTestApp* battery_test_alloc(void) { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_tick_event_callback( app->view_dispatcher, battery_test_battery_info_update_model, 500); diff --git a/applications/debug/bt_debug_app/bt_debug_app.c b/applications/debug/bt_debug_app/bt_debug_app.c index 109feee60..56c67e3e6 100644 --- a/applications/debug/bt_debug_app/bt_debug_app.c +++ b/applications/debug/bt_debug_app/bt_debug_app.c @@ -36,7 +36,6 @@ BtDebugApp* bt_debug_app_alloc(void) { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); // Views diff --git a/applications/debug/crash_test/crash_test.c b/applications/debug/crash_test/crash_test.c index ae0074fe1..2b2be13d6 100644 --- a/applications/debug/crash_test/crash_test.c +++ b/applications/debug/crash_test/crash_test.c @@ -66,7 +66,6 @@ CrashTest* crash_test_alloc(void) { instance->gui = furi_record_open(RECORD_GUI); instance->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(instance->view_dispatcher); view_dispatcher_attach_to_gui( instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen); diff --git a/applications/debug/display_test/display_test.c b/applications/debug/display_test/display_test.c index 3028a13b9..3b742906d 100644 --- a/applications/debug/display_test/display_test.c +++ b/applications/debug/display_test/display_test.c @@ -126,7 +126,6 @@ DisplayTest* display_test_alloc(void) { instance->gui = furi_record_open(RECORD_GUI); instance->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(instance->view_dispatcher); view_dispatcher_attach_to_gui( instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen); diff --git a/applications/debug/event_loop_blink_test/event_loop_blink_test.c b/applications/debug/event_loop_blink_test/event_loop_blink_test.c index 5c7e0ce55..7f00e63f2 100644 --- a/applications/debug/event_loop_blink_test/event_loop_blink_test.c +++ b/applications/debug/event_loop_blink_test/event_loop_blink_test.c @@ -82,7 +82,8 @@ static void view_port_input_callback(InputEvent* input_event, void* context) { furi_message_queue_put(app->input_queue, input_event, 0); } -static bool input_queue_callback(FuriMessageQueue* queue, void* context) { +static bool input_queue_callback(FuriEventLoopObject* object, void* context) { + FuriMessageQueue* queue = object; EventLoopBlinkTestApp* app = context; InputEvent event; @@ -144,7 +145,7 @@ int32_t event_loop_blink_test_app(void* arg) { gui_add_view_port(gui, view_port, GuiLayerFullscreen); furi_event_loop_tick_set(app.event_loop, 500, event_loop_tick_callback, &app); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( app.event_loop, app.input_queue, FuriEventLoopEventIn, input_queue_callback, &app); furi_event_loop_run(app.event_loop); @@ -154,7 +155,7 @@ int32_t event_loop_blink_test_app(void* arg) { furi_record_close(RECORD_GUI); - furi_event_loop_message_queue_unsubscribe(app.event_loop, app.input_queue); + furi_event_loop_unsubscribe(app.event_loop, app.input_queue); furi_message_queue_free(app.input_queue); for(size_t i = 0; i < TIMER_COUNT; ++i) { diff --git a/applications/debug/file_browser_test/file_browser_app.c b/applications/debug/file_browser_test/file_browser_app.c index 60ddf1b68..89b8b9274 100644 --- a/applications/debug/file_browser_test/file_browser_app.c +++ b/applications/debug/file_browser_test/file_browser_app.c @@ -33,8 +33,6 @@ FileBrowserApp* file_browser_app_alloc(char* arg) { app->dialogs = furi_record_open(RECORD_DIALOGS); app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); - app->scene_manager = scene_manager_alloc(&file_browser_scene_handlers, app); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); diff --git a/applications/debug/lfrfid_debug/lfrfid_debug.c b/applications/debug/lfrfid_debug/lfrfid_debug.c index 13c0b299f..962afd1c3 100644 --- a/applications/debug/lfrfid_debug/lfrfid_debug.c +++ b/applications/debug/lfrfid_debug/lfrfid_debug.c @@ -17,7 +17,6 @@ static LfRfidDebug* lfrfid_debug_alloc(void) { app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&lfrfid_debug_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( app->view_dispatcher, lfrfid_debug_custom_event_callback); diff --git a/applications/debug/locale_test/locale_test.c b/applications/debug/locale_test/locale_test.c index 1ca077db1..51d45a6b0 100644 --- a/applications/debug/locale_test/locale_test.c +++ b/applications/debug/locale_test/locale_test.c @@ -61,7 +61,6 @@ static LocaleTestApp* locale_test_alloc(void) { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); // Views diff --git a/applications/debug/rpc_debug_app/rpc_debug_app.c b/applications/debug/rpc_debug_app/rpc_debug_app.c index 5e53c221e..1536b8918 100644 --- a/applications/debug/rpc_debug_app/rpc_debug_app.c +++ b/applications/debug/rpc_debug_app/rpc_debug_app.c @@ -99,7 +99,6 @@ static RpcDebugApp* rpc_debug_app_alloc(void) { view_dispatcher_set_tick_event_callback( app->view_dispatcher, rpc_debug_app_tick_event_callback, 100); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); - view_dispatcher_enable_queue(app->view_dispatcher); app->widget = widget_alloc(); view_dispatcher_add_view( diff --git a/applications/debug/subghz_test/subghz_test_app.c b/applications/debug/subghz_test/subghz_test_app.c index 6eba864f6..dccdac213 100644 --- a/applications/debug/subghz_test/subghz_test_app.c +++ b/applications/debug/subghz_test/subghz_test_app.c @@ -30,7 +30,6 @@ SubGhzTestApp* subghz_test_app_alloc(void) { // View Dispatcher app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&subghz_test_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( diff --git a/applications/debug/text_box_view_test/text_box_view_test.c b/applications/debug/text_box_view_test/text_box_view_test.c index 7bbcb285b..4d63e3779 100644 --- a/applications/debug/text_box_view_test/text_box_view_test.c +++ b/applications/debug/text_box_view_test/text_box_view_test.c @@ -126,7 +126,6 @@ int32_t text_box_view_test_app(void* p) { Gui* gui = furi_record_open(RECORD_GUI); ViewDispatcher* view_dispatcher = view_dispatcher_alloc(); view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen); - view_dispatcher_enable_queue(view_dispatcher); TextBoxViewTest instance = { .text_box = text_box_alloc(), diff --git a/applications/debug/uart_echo/uart_echo.c b/applications/debug/uart_echo/uart_echo.c index 8e1884e9a..bf38ba4c2 100644 --- a/applications/debug/uart_echo/uart_echo.c +++ b/applications/debug/uart_echo/uart_echo.c @@ -242,7 +242,6 @@ static UartEchoApp* uart_echo_app_alloc(uint32_t baudrate) { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); // Views diff --git a/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_mahs.sub b/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_mahs.sub new file mode 100644 index 000000000..9737b71a6 --- /dev/null +++ b/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_mahs.sub @@ -0,0 +1,7 @@ +Filetype: Flipper SubGhz Key File +Version: 1 +Frequency: 433920000 +Preset: FuriHalSubGhzPresetOok650Async +Protocol: Dickert_MAHS +Bit: 36 +Key: 00 00 00 01 55 57 55 15 diff --git a/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_raw.sub b/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_raw.sub new file mode 100644 index 000000000..544fc7a1d --- /dev/null +++ b/applications/debug/unit_tests/resources/unit_tests/subghz/dickert_raw.sub @@ -0,0 +1,7 @@ +Filetype: Flipper SubGhz RAW File +Version: 1 +Frequency: 433920000 +Preset: FuriHalSubGhzPresetOok650Async +Protocol: RAW +RAW_Data: 112254 -62882 64 -8912 798 -844 416 -418 806 -850 396 -45206 440 -428 794 -442 804 -422 822 -810 414 -414 824 -832 412 -416 808 -848 376 -446 792 -846 382 -448 816 -828 410 -416 810 -844 382 -416 834 -818 410 -414 810 -856 408 -810 412 -836 384 -442 808 -814 402 -844 414 -834 378 -436 808 -844 396 -422 798 -844 416 -416 814 -404 812 -440 810 -842 396 -422 798 -840 414 -414 806 -850 398 -45210 450 -420 796 -436 780 -446 802 -848 380 -434 806 -846 400 -422 800 -840 410 -408 836 -812 414 -410 826 -840 378 -440 804 -848 396 -426 812 -810 426 -394 826 -844 414 -810 420 -834 378 -442 808 -832 412 -812 416 -830 410 -406 810 -844 400 -420 832 -810 414 -416 800 -446 798 -440 812 -808 426 -410 800 -836 412 -414 806 -836 412 -45216 450 -420 798 -434 806 -414 802 -846 382 -438 814 -832 410 -410 838 -834 396 -430 810 -842 394 -392 826 -840 414 -414 802 -850 396 -428 812 -842 394 -394 828 -842 414 -810 424 -812 392 -434 812 -844 398 -848 380 -844 408 -416 820 -810 414 -406 816 -836 412 -416 836 -414 816 -398 816 -840 420 -410 802 -844 416 -416 804 -824 410 -45232 446 -400 802 -442 810 -432 804 -842 396 -392 826 -842 410 -410 834 -818 378 -442 804 -854 406 -408 806 -838 408 -428 804 -844 396 -392 826 -840 410 -410 834 -810 414 -832 408 -834 380 -440 802 -826 410 -836 412 -838 396 -424 796 -842 414 -414 804 -848 396 -426 812 -412 814 -414 824 -832 410 -416 806 -848 382 -420 834 -814 422 -45228 416 -422 802 -446 810 -420 790 -846 382 -448 818 -828 408 -416 808 -848 382 -418 830 -816 410 -412 812 -856 410 -382 834 -846 382 -418 832 -818 408 -412 812 -856 408 -814 414 -838 396 -428 810 -808 424 -836 380 -844 404 -416 802 -840 424 -394 826 -840 414 -382 836 -412 822 -436 812 -806 424 -394 826 -844 416 -382 838 -816 402 -45228 438 -430 796 -444 806 -424 822 -810 412 -416 822 -832 412 -416 804 -844 408 -414 824 -812 412 -408 812 -834 410 -414 804 -848 408 -412 802 -840 424 -412 802 -834 412 -842 384 -848 396 -426 814 -808 424 -816 392 -866 382 -414 838 -816 414 -428 792 -846 380 -440 810 -438 812 -412 802 -846 380 -438 826 -840 380 -416 838 -814 404 -45226 450 -404 820 -408 806 -452 792 -848 382 -440 814 -832 410 -416 810 -846 378 -450 792 -846 380 -446 816 -830 410 -386 836 -846 376 -410 828 -846 380 -446 814 -828 410 -814 414 -836 396 -428 810 -842 394 -816 410 -836 406 -430 812 -810 426 -394 826 -838 +RAW_Data: 414 -414 808 -416 826 -438 814 -816 420 -414 834 -814 418 -418 808 -848 398 -45218 412 -438 824 -412 812 -418 832 -852 378 -446 782 -862 410 -386 838 -848 384 -420 836 -820 418 -414 814 -854 408 -388 838 -814 418 -422 836 -816 394 -434 812 -846 398 -850 380 -848 410 -418 822 -812 416 -850 368 -854 412 -418 810 -850 384 -422 834 -820 416 -414 812 -428 836 -412 804 -848 382 -450 818 -828 412 -418 808 -850 380 -45228 452 -420 798 -434 806 -416 834 -818 384 -440 810 -820 404 -420 834 -814 416 -418 834 -824 386 -442 810 -818 404 -420 834 -814 416 -418 834 -820 410 -414 810 -850 406 -812 414 -816 404 -420 818 -838 386 -848 394 -828 414 -414 838 -814 406 -420 820 -842 384 -446 794 -438 810 -412 802 -848 394 -432 812 -842 394 -392 830 -842 414 -105578 64 -1760 130 -196 130 -832 160 -128 62 -1278 194 -1316 230 -96 362 -64 64 -398 diff --git a/applications/debug/unit_tests/tests/furi/furi_event_loop.c b/applications/debug/unit_tests/tests/furi/furi_event_loop.c index 4eeecb2b8..291181c77 100644 --- a/applications/debug/unit_tests/tests/furi/furi_event_loop.c +++ b/applications/debug/unit_tests/tests/furi/furi_event_loop.c @@ -19,25 +19,24 @@ typedef struct { uint32_t consumer_counter; } TestFuriData; -bool test_furi_event_loop_producer_mq_callback(FuriMessageQueue* queue, void* context) { +bool test_furi_event_loop_producer_mq_callback(FuriEventLoopObject* object, void* context) { furi_check(context); TestFuriData* data = context; - furi_check(data->mq == queue, "Invalid queue"); + furi_check(data->mq == object, "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 / 2) { + furi_event_loop_unsubscribe(data->producer_event_loop, data->mq); + furi_event_loop_subscribe_message_queue( + 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); @@ -61,7 +60,7 @@ int32_t test_furi_event_loop_producer(void* p) { FURI_LOG_I(TAG, "producer start 1st run"); data->producer_event_loop = furi_event_loop_alloc(); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( data->producer_event_loop, data->mq, FuriEventLoopEventOut, @@ -73,7 +72,7 @@ int32_t test_furi_event_loop_producer(void* p) { // 2 EventLoop index, 0xFFFFFFFF - all possible flags, emulate uncleared flags xTaskNotifyIndexed(xTaskGetCurrentTaskHandle(), 2, 0xFFFFFFFF, eSetBits); - furi_event_loop_message_queue_unsubscribe(data->producer_event_loop, data->mq); + furi_event_loop_unsubscribe(data->producer_event_loop, data->mq); furi_event_loop_free(data->producer_event_loop); FURI_LOG_I(TAG, "producer start 2nd run"); @@ -81,7 +80,7 @@ int32_t test_furi_event_loop_producer(void* p) { data->producer_counter = 0; data->producer_event_loop = furi_event_loop_alloc(); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( data->producer_event_loop, data->mq, FuriEventLoopEventOut, @@ -90,7 +89,7 @@ int32_t test_furi_event_loop_producer(void* p) { furi_event_loop_run(data->producer_event_loop); - furi_event_loop_message_queue_unsubscribe(data->producer_event_loop, data->mq); + furi_event_loop_unsubscribe(data->producer_event_loop, data->mq); furi_event_loop_free(data->producer_event_loop); FURI_LOG_I(TAG, "producer end"); @@ -98,11 +97,11 @@ int32_t test_furi_event_loop_producer(void* p) { return 0; } -bool test_furi_event_loop_consumer_mq_callback(FuriMessageQueue* queue, void* context) { +bool test_furi_event_loop_consumer_mq_callback(FuriEventLoopObject* object, void* context) { furi_check(context); TestFuriData* data = context; - furi_check(data->mq == queue); + furi_check(data->mq == object); furi_delay_us(furi_hal_random_get() % 1000); furi_check(furi_message_queue_get(data->mq, &data->consumer_counter, 0) == FuriStatusOk); @@ -110,16 +109,15 @@ bool test_furi_event_loop_consumer_mq_callback(FuriMessageQueue* queue, void* co 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 / 2) { + furi_event_loop_unsubscribe(data->consumer_event_loop, data->mq); + furi_event_loop_subscribe_message_queue( + data->consumer_event_loop, + data->mq, + FuriEventLoopEventIn, + test_furi_event_loop_consumer_mq_callback, + data); + } if(data->consumer_counter == EVENT_LOOP_EVENT_COUNT) { furi_event_loop_stop(data->consumer_event_loop); @@ -137,7 +135,7 @@ int32_t test_furi_event_loop_consumer(void* p) { FURI_LOG_I(TAG, "consumer start 1st run"); data->consumer_event_loop = furi_event_loop_alloc(); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( data->consumer_event_loop, data->mq, FuriEventLoopEventIn, @@ -149,14 +147,14 @@ int32_t test_furi_event_loop_consumer(void* p) { // 2 EventLoop index, 0xFFFFFFFF - all possible flags, emulate uncleared flags xTaskNotifyIndexed(xTaskGetCurrentTaskHandle(), 2, 0xFFFFFFFF, eSetBits); - furi_event_loop_message_queue_unsubscribe(data->consumer_event_loop, data->mq); + furi_event_loop_unsubscribe(data->consumer_event_loop, data->mq); furi_event_loop_free(data->consumer_event_loop); FURI_LOG_I(TAG, "consumer start 2nd run"); data->consumer_counter = 0; data->consumer_event_loop = furi_event_loop_alloc(); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( data->consumer_event_loop, data->mq, FuriEventLoopEventIn, @@ -165,7 +163,7 @@ int32_t test_furi_event_loop_consumer(void* p) { furi_event_loop_run(data->consumer_event_loop); - furi_event_loop_message_queue_unsubscribe(data->consumer_event_loop, data->mq); + furi_event_loop_unsubscribe(data->consumer_event_loop, data->mq); furi_event_loop_free(data->consumer_event_loop); FURI_LOG_I(TAG, "consumer end"); diff --git a/applications/debug/unit_tests/tests/rpc/rpc_test.c b/applications/debug/unit_tests/tests/rpc/rpc_test.c index ef3e57091..55339d495 100644 --- a/applications/debug/unit_tests/tests/rpc/rpc_test.c +++ b/applications/debug/unit_tests/tests/rpc/rpc_test.c @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -35,8 +36,8 @@ static uint32_t command_id = 0; typedef struct { RpcSession* session; FuriStreamBuffer* output_stream; - FuriSemaphore* close_session_semaphore; - FuriSemaphore* terminate_semaphore; + FuriApiLock session_close_lock; + FuriApiLock session_terminate_lock; uint32_t timeout; } RpcSessionContext; @@ -92,8 +93,8 @@ static void test_rpc_setup(void) { rpc_session[0].output_stream = furi_stream_buffer_alloc(4096, 1); rpc_session_set_send_bytes_callback(rpc_session[0].session, output_bytes_callback); - rpc_session[0].close_session_semaphore = furi_semaphore_alloc(1, 0); - rpc_session[0].terminate_semaphore = furi_semaphore_alloc(1, 0); + rpc_session[0].session_close_lock = api_lock_alloc_locked(); + rpc_session[0].session_terminate_lock = api_lock_alloc_locked(); rpc_session_set_close_callback(rpc_session[0].session, test_rpc_session_close_callback); rpc_session_set_terminated_callback( rpc_session[0].session, test_rpc_session_terminated_callback); @@ -112,8 +113,8 @@ static void test_rpc_setup_second_session(void) { rpc_session[1].output_stream = furi_stream_buffer_alloc(1000, 1); rpc_session_set_send_bytes_callback(rpc_session[1].session, output_bytes_callback); - rpc_session[1].close_session_semaphore = furi_semaphore_alloc(1, 0); - rpc_session[1].terminate_semaphore = furi_semaphore_alloc(1, 0); + rpc_session[1].session_close_lock = api_lock_alloc_locked(); + rpc_session[1].session_terminate_lock = api_lock_alloc_locked(); rpc_session_set_close_callback(rpc_session[1].session, test_rpc_session_close_callback); rpc_session_set_terminated_callback( rpc_session[1].session, test_rpc_session_terminated_callback); @@ -121,36 +122,32 @@ static void test_rpc_setup_second_session(void) { } static void test_rpc_teardown(void) { - furi_check(rpc_session[0].close_session_semaphore); - furi_semaphore_acquire(rpc_session[0].terminate_semaphore, 0); + furi_check(rpc_session[0].session_close_lock); + api_lock_relock(rpc_session[0].session_terminate_lock); rpc_session_close(rpc_session[0].session); - furi_check( - furi_semaphore_acquire(rpc_session[0].terminate_semaphore, FuriWaitForever) == - FuriStatusOk); + api_lock_wait_unlock(rpc_session[0].session_terminate_lock); furi_record_close(RECORD_RPC); furi_stream_buffer_free(rpc_session[0].output_stream); - furi_semaphore_free(rpc_session[0].close_session_semaphore); - furi_semaphore_free(rpc_session[0].terminate_semaphore); + api_lock_free(rpc_session[0].session_close_lock); + api_lock_free(rpc_session[0].session_terminate_lock); ++command_id; rpc_session[0].output_stream = NULL; - rpc_session[0].close_session_semaphore = NULL; + rpc_session[0].session_close_lock = NULL; rpc = NULL; rpc_session[0].session = NULL; } static void test_rpc_teardown_second_session(void) { - furi_check(rpc_session[1].close_session_semaphore); - furi_semaphore_acquire(rpc_session[1].terminate_semaphore, 0); + furi_check(rpc_session[1].session_close_lock); + api_lock_relock(rpc_session[1].session_terminate_lock); rpc_session_close(rpc_session[1].session); - furi_check( - furi_semaphore_acquire(rpc_session[1].terminate_semaphore, FuriWaitForever) == - FuriStatusOk); + api_lock_wait_unlock(rpc_session[1].session_terminate_lock); furi_stream_buffer_free(rpc_session[1].output_stream); - furi_semaphore_free(rpc_session[1].close_session_semaphore); - furi_semaphore_free(rpc_session[1].terminate_semaphore); + api_lock_free(rpc_session[1].session_close_lock); + api_lock_free(rpc_session[1].session_terminate_lock); ++command_id; rpc_session[1].output_stream = NULL; - rpc_session[1].close_session_semaphore = NULL; + rpc_session[1].session_close_lock = NULL; rpc_session[1].session = NULL; } @@ -204,14 +201,14 @@ static void test_rpc_session_close_callback(void* context) { furi_check(context); RpcSessionContext* callbacks_context = context; - furi_check(furi_semaphore_release(callbacks_context->close_session_semaphore) == FuriStatusOk); + api_lock_unlock(callbacks_context->session_close_lock); } static void test_rpc_session_terminated_callback(void* context) { furi_check(context); RpcSessionContext* callbacks_context = context; - furi_check(furi_semaphore_release(callbacks_context->terminate_semaphore) == FuriStatusOk); + api_lock_unlock(callbacks_context->session_terminate_lock); } static void test_rpc_print_message_list(MsgList_t msg_list) { @@ -1645,7 +1642,7 @@ static void test_rpc_feed_rubbish_run( test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_DECODE, 0); - furi_check(furi_semaphore_acquire(rpc_session[0].close_session_semaphore, 0) != FuriStatusOk); + furi_check(api_lock_is_locked(rpc_session[0].session_close_lock)); test_rpc_encode_and_feed(input_before, 0); test_send_rubbish(rpc_session[0].session, pattern, pattern_size, size); test_rpc_encode_and_feed(input_after, 0); diff --git a/applications/debug/unit_tests/tests/subghz/subghz_test.c b/applications/debug/unit_tests/tests/subghz/subghz_test.c index f975ab1cd..68f83260e 100644 --- a/applications/debug/unit_tests/tests/subghz/subghz_test.c +++ b/applications/debug/unit_tests/tests/subghz/subghz_test.c @@ -663,6 +663,13 @@ MU_TEST(subghz_decoder_mastercode_test) { "Test decoder " SUBGHZ_PROTOCOL_MASTERCODE_NAME " error\r\n"); } +MU_TEST(subghz_decoder_dickert_test) { + mu_assert( + subghz_decoder_test( + EXT_PATH("unit_tests/subghz/dickert_raw.sub"), SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME), + "Test decoder " SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME " error\r\n"); +} + //test encoders MU_TEST(subghz_encoder_princeton_test) { mu_assert( @@ -827,6 +834,12 @@ MU_TEST(subghz_decoder_acurite_592txr_test) { "Test decoder " WS_PROTOCOL_ACURITE_592TXR_NAME " error\r\n"); } +MU_TEST(subghz_encoder_dickert_test) { + mu_assert( + subghz_encoder_test(EXT_PATH("unit_tests/subghz/dickert_mahs.sub")), + "Test encoder " SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME " error\r\n"); +} + MU_TEST(subghz_random_test) { mu_assert(subghz_decode_random_test(TEST_RANDOM_DIR_NAME), "Random test error\r\n"); } @@ -878,6 +891,7 @@ MU_TEST_SUITE(subghz) { MU_RUN_TEST(subghz_decoder_nice_one_test); MU_RUN_TEST(subghz_decoder_kinggates_stylo4k_test); MU_RUN_TEST(subghz_decoder_mastercode_test); + MU_RUN_TEST(subghz_decoder_dickert_test); MU_RUN_TEST(subghz_encoder_princeton_test); MU_RUN_TEST(subghz_encoder_came_test); @@ -906,6 +920,7 @@ MU_TEST_SUITE(subghz) { MU_RUN_TEST(subghz_encoder_dooya_test); MU_RUN_TEST(subghz_encoder_mastercode_test); MU_RUN_TEST(subghz_decoder_acurite_592txr_test); + MU_RUN_TEST(subghz_encoder_dickert_test); MU_RUN_TEST(subghz_random_test); subghz_test_deinit(); diff --git a/applications/debug/unit_tests/unit_test_api_table_i.h b/applications/debug/unit_tests/unit_test_api_table_i.h index 1adec4db2..50524e5b7 100644 --- a/applications/debug/unit_tests/unit_test_api_table_i.h +++ b/applications/debug/unit_tests/unit_test_api_table_i.h @@ -36,14 +36,10 @@ static constexpr auto unit_tests_api_table = sort(create_array_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, + furi_event_loop_subscribe_message_queue, void, - (FuriEventLoop*, - FuriMessageQueue*, - FuriEventLoopEvent, - FuriEventLoopMessageQueueCallback, - void*)), - API_METHOD(furi_event_loop_message_queue_unsubscribe, void, (FuriEventLoop*, FuriMessageQueue*)), + (FuriEventLoop*, FuriMessageQueue*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*)), + API_METHOD(furi_event_loop_unsubscribe, void, (FuriEventLoop*, FuriEventLoopObject*)), 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))); diff --git a/applications/debug/usb_test/usb_test.c b/applications/debug/usb_test/usb_test.c index ddec9d9b0..a71ac3c6e 100644 --- a/applications/debug/usb_test/usb_test.c +++ b/applications/debug/usb_test/usb_test.c @@ -63,7 +63,6 @@ UsbTestApp* usb_test_app_alloc(void) { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); // Views diff --git a/applications/examples/example_ble_beacon/ble_beacon_app.c b/applications/examples/example_ble_beacon/ble_beacon_app.c index faa3feb91..16979543c 100644 --- a/applications/examples/example_ble_beacon/ble_beacon_app.c +++ b/applications/examples/example_ble_beacon/ble_beacon_app.c @@ -75,7 +75,6 @@ static BleBeaconApp* ble_beacon_app_alloc(void) { view_dispatcher_set_tick_event_callback( app->view_dispatcher, ble_beacon_app_tick_event_callback, 100); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); - view_dispatcher_enable_queue(app->view_dispatcher); app->submenu = submenu_alloc(); view_dispatcher_add_view( diff --git a/applications/examples/example_event_loop/application.fam b/applications/examples/example_event_loop/application.fam new file mode 100644 index 000000000..a37ffb1a0 --- /dev/null +++ b/applications/examples/example_event_loop/application.fam @@ -0,0 +1,36 @@ +App( + appid="example_event_loop_timer", + name="Example: Event Loop Timer", + apptype=FlipperAppType.EXTERNAL, + sources=["example_event_loop_timer.c"], + entry_point="example_event_loop_timer_app", + fap_category="Examples", +) + +App( + appid="example_event_loop_mutex", + name="Example: Event Loop Mutex", + apptype=FlipperAppType.EXTERNAL, + sources=["example_event_loop_mutex.c"], + entry_point="example_event_loop_mutex_app", + fap_category="Examples", +) + +App( + appid="example_event_loop_stream_buffer", + name="Example: Event Loop Stream Buffer", + apptype=FlipperAppType.EXTERNAL, + sources=["example_event_loop_stream_buffer.c"], + entry_point="example_event_loop_stream_buffer_app", + fap_category="Examples", +) + +App( + appid="example_event_loop_multi", + name="Example: Event Loop Multi", + apptype=FlipperAppType.EXTERNAL, + sources=["example_event_loop_multi.c"], + entry_point="example_event_loop_multi_app", + requires=["gui"], + fap_category="Examples", +) diff --git a/applications/examples/example_event_loop/example_event_loop_multi.c b/applications/examples/example_event_loop/example_event_loop_multi.c new file mode 100644 index 000000000..ebfb00911 --- /dev/null +++ b/applications/examples/example_event_loop/example_event_loop_multi.c @@ -0,0 +1,342 @@ +/** + * @file example_event_loop_multi.c + * @brief Example application that demonstrates multiple primitives used with two FuriEventLoop instances. + * + * This application simulates a complex use case of having two concurrent event loops (each one executing in + * its own thread) using a stream buffer for communication and additional timers and message passing to handle + * the keypad input. Additionally, it shows how to use thread signals to stop an event loop in another thread. + * The GUI functionality is there only for the purpose of exclusive access to the input events. + * + * The application's functionality consists of the following: + * - Print keypad key names and types when pressed, + * - If the Back key is long-pressed, a countdown starts upon completion of which the app exits, + * - The countdown can be cancelled by long-pressing the Ok button, it also resets the counter, + * - Blocks of random data are periodically generated in a separate thread, + * - When ready, the main application thread gets notified and prints the data. + */ + +#include +#include +#include + +#include + +#define TAG "ExampleEventLoopMulti" + +#define COUNTDOWN_START_VALUE (5UL) +#define COUNTDOWN_INTERVAL_MS (1000UL) +#define WORKER_DATA_INTERVAL_MS (1500UL) + +#define INPUT_QUEUE_SIZE (8) +#define STREAM_BUFFER_SIZE (16) + +typedef struct { + FuriEventLoop* event_loop; + FuriEventLoopTimer* timer; + FuriStreamBuffer* stream_buffer; +} EventLoopMultiAppWorker; + +typedef struct { + Gui* gui; + ViewPort* view_port; + FuriThread* worker_thread; + FuriEventLoop* event_loop; + FuriMessageQueue* input_queue; + FuriEventLoopTimer* exit_timer; + FuriStreamBuffer* stream_buffer; + uint32_t exit_countdown_value; +} EventLoopMultiApp; + +/* + * Worker functions + */ + +// This function is executed each time the data is taken out of the stream buffer. It is used to restart the worker timer. +static bool + event_loop_multi_app_stream_buffer_worker_callback(FuriEventLoopObject* object, void* context) { + furi_assert(context); + EventLoopMultiAppWorker* worker = context; + + furi_assert(object == worker->stream_buffer); + + FURI_LOG_I(TAG, "Data was removed from buffer"); + // Restart the timer to generate another block of random data. + furi_event_loop_timer_start(worker->timer, WORKER_DATA_INTERVAL_MS); + + return true; +} + +// This function is executed when the worker timer expires. The timer will NOT restart automatically +// since it is of one-shot type. +static void event_loop_multi_app_worker_timer_callback(void* context) { + furi_assert(context); + EventLoopMultiAppWorker* worker = context; + + // Generate a block of random data. + uint8_t data[STREAM_BUFFER_SIZE]; + furi_hal_random_fill_buf(data, sizeof(data)); + // Put the generated data in the stream buffer. + // IMPORTANT: No waiting in the event handlers! + furi_check( + furi_stream_buffer_send(worker->stream_buffer, &data, sizeof(data), 0) == sizeof(data)); +} + +static EventLoopMultiAppWorker* + event_loop_multi_app_worker_alloc(FuriStreamBuffer* stream_buffer) { + EventLoopMultiAppWorker* worker = malloc(sizeof(EventLoopMultiAppWorker)); + // Create the worker event loop. + worker->event_loop = furi_event_loop_alloc(); + // Create the timer governing the data generation. + // It is of one-shot type, i.e. it will not restart automatically upon expiration. + worker->timer = furi_event_loop_timer_alloc( + worker->event_loop, + event_loop_multi_app_worker_timer_callback, + FuriEventLoopTimerTypeOnce, + worker); + + // Using the same stream buffer as the main thread (it was already created beforehand). + worker->stream_buffer = stream_buffer; + // Notify the worker event loop about data being taken out of the stream buffer. + furi_event_loop_subscribe_stream_buffer( + worker->event_loop, + worker->stream_buffer, + FuriEventLoopEventOut | FuriEventLoopEventFlagEdge, + event_loop_multi_app_stream_buffer_worker_callback, + worker); + + return worker; +} + +static void event_loop_multi_app_worker_free(EventLoopMultiAppWorker* worker) { + // IMPORTANT: The user code MUST unsubscribe from all events before deleting the event loop. + // Failure to do so will result in a crash. + furi_event_loop_unsubscribe(worker->event_loop, worker->stream_buffer); + // IMPORTANT: All timers MUST be deleted before deleting the associated event loop. + // Failure to do so will result in a crash. + furi_event_loop_timer_free(worker->timer); + // Now it is okay to delete the event loop. + furi_event_loop_free(worker->event_loop); + + free(worker); +} + +static void event_loop_multi_app_worker_run(EventLoopMultiAppWorker* worker) { + furi_event_loop_timer_start(worker->timer, WORKER_DATA_INTERVAL_MS); + furi_event_loop_run(worker->event_loop); +} + +// This function is the worker thread body and (obviously) is executed in the worker thread. +static int32_t event_loop_multi_app_worker_thread(void* context) { + furi_assert(context); + EventLoopMultiApp* app = context; + + // Because an event loop is used, it MUST be created in the thread it will be run in. + // Therefore, the worker creation and deletion is handled in the worker thread. + EventLoopMultiAppWorker* worker = event_loop_multi_app_worker_alloc(app->stream_buffer); + event_loop_multi_app_worker_run(worker); + event_loop_multi_app_worker_free(worker); + + return 0; +} + +/* + * Main application functions + */ + +// This function is executed in the GUI context each time an input event occurs (e.g. the user pressed a key) +static void event_loop_multi_app_input_callback(InputEvent* event, void* context) { + furi_assert(context); + EventLoopMultiApp* app = context; + // Pass the event to the the application's input queue + furi_check(furi_message_queue_put(app->input_queue, event, FuriWaitForever) == FuriStatusOk); +} + +// This function is executed each time new data is available in the stream buffer. +static bool + event_loop_multi_app_stream_buffer_callback(FuriEventLoopObject* object, void* context) { + furi_assert(context); + EventLoopMultiApp* app = context; + + furi_assert(object == app->stream_buffer); + // Get the data from the stream buffer + uint8_t data[STREAM_BUFFER_SIZE]; + // IMPORTANT: No waiting in the event handlers! + furi_check( + furi_stream_buffer_receive(app->stream_buffer, &data, sizeof(data), 0) == sizeof(data)); + + // Format the data for printing and print it to the debug output. + FuriString* tmp_str = furi_string_alloc(); + for(uint32_t i = 0; i < sizeof(data); ++i) { + furi_string_cat_printf(tmp_str, "%02X ", data[i]); + } + + FURI_LOG_I(TAG, "Received data: %s", furi_string_get_cstr(tmp_str)); + furi_string_free(tmp_str); + + return true; +} + +// This function is executed each time a new message is inserted in the input queue. +static bool event_loop_multi_app_input_queue_callback(FuriEventLoopObject* object, void* context) { + furi_assert(context); + EventLoopMultiApp* app = context; + + furi_assert(object == app->input_queue); + + InputEvent event; + // IMPORTANT: No waiting in the event handlers! + furi_check(furi_message_queue_get(app->input_queue, &event, 0) == FuriStatusOk); + + if(event.type == InputTypeLong) { + // The user has long-pressed the Back key, try starting the countdown. + if(event.key == InputKeyBack) { + if(!furi_event_loop_timer_is_running(app->exit_timer)) { + // Actually start the countdown + FURI_LOG_I(TAG, "Starting exit countdown!"); + furi_event_loop_timer_start(app->exit_timer, COUNTDOWN_INTERVAL_MS); + + } else { + // The countdown is already in progress, print a warning message + FURI_LOG_W(TAG, "Countdown has already been started"); + } + + // The user has long-pressed the Ok key, try stopping the countdown. + } else if(event.key == InputKeyOk) { + if(furi_event_loop_timer_is_running(app->exit_timer)) { + // Actually cancel the countdown + FURI_LOG_I(TAG, "Exit countdown cancelled!"); + app->exit_countdown_value = COUNTDOWN_START_VALUE; + furi_event_loop_timer_stop(app->exit_timer); + + } else { + // The countdown is not running, print a warning message + FURI_LOG_W(TAG, "Countdown has not been started yet"); + } + + } else { + // Not a Back or Ok key, just print its name. + FURI_LOG_I(TAG, "Long press: %s", input_get_key_name(event.key)); + } + + } else if(event.type == InputTypeShort) { + // Not a long press, just print the key's name. + FURI_LOG_I(TAG, "Short press: %s", input_get_key_name(event.key)); + } + + return true; +} + +// This function is executed each time the countdown timer expires. +static void event_loop_multi_app_exit_timer_callback(void* context) { + furi_assert(context); + EventLoopMultiApp* app = context; + + FURI_LOG_I(TAG, "Exiting in %lu ...", app->exit_countdown_value); + + // If the coundown value has reached 0, exit the application + if(app->exit_countdown_value == 0) { + FURI_LOG_I(TAG, "Exiting NOW!"); + + // Send a signal to the worker thread to exit. + // A signal handler that handles FuriSignalExit is already set by default. + furi_thread_signal(app->worker_thread, FuriSignalExit, NULL); + // Request the application event loop to stop. + furi_event_loop_stop(app->event_loop); + + // Otherwise just decrement it and wait for the next time the timer expires. + } else { + app->exit_countdown_value -= 1; + } +} + +static EventLoopMultiApp* event_loop_multi_app_alloc(void) { + EventLoopMultiApp* app = malloc(sizeof(EventLoopMultiApp)); + // Create event loop instances. + app->event_loop = furi_event_loop_alloc(); + + // Create a worker thread instance. The worker event loop will execute inside it. + app->worker_thread = furi_thread_alloc_ex( + "EventLoopMultiWorker", 1024, event_loop_multi_app_worker_thread, app); + // Create a message queue to receive the input events. + app->input_queue = furi_message_queue_alloc(INPUT_QUEUE_SIZE, sizeof(InputEvent)); + // Create a stream buffer to receive the generated data. + app->stream_buffer = furi_stream_buffer_alloc(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE); + // Create a timer to run the countdown. + app->exit_timer = furi_event_loop_timer_alloc( + app->event_loop, + event_loop_multi_app_exit_timer_callback, + FuriEventLoopTimerTypePeriodic, + app); + + app->gui = furi_record_open(RECORD_GUI); + app->view_port = view_port_alloc(); + // Start the countdown from this value + app->exit_countdown_value = COUNTDOWN_START_VALUE; + // Gain exclusive access to the input events + view_port_input_callback_set(app->view_port, event_loop_multi_app_input_callback, app); + gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen); + // Notify the event loop about incoming messages in the queue + furi_event_loop_subscribe_message_queue( + app->event_loop, + app->input_queue, + FuriEventLoopEventIn, + event_loop_multi_app_input_queue_callback, + app); + // Notify the event loop about new data in the stream buffer + furi_event_loop_subscribe_stream_buffer( + app->event_loop, + app->stream_buffer, + FuriEventLoopEventIn | FuriEventLoopEventFlagEdge, + event_loop_multi_app_stream_buffer_callback, + app); + + return app; +} + +static void event_loop_multi_app_free(EventLoopMultiApp* app) { + gui_remove_view_port(app->gui, app->view_port); + furi_record_close(RECORD_GUI); + // IMPORTANT: The user code MUST unsubscribe from all events before deleting the event loop. + // Failure to do so will result in a crash. + furi_event_loop_unsubscribe(app->event_loop, app->input_queue); + furi_event_loop_unsubscribe(app->event_loop, app->stream_buffer); + // Delete all instances + view_port_free(app->view_port); + furi_message_queue_free(app->input_queue); + furi_stream_buffer_free(app->stream_buffer); + // IMPORTANT: All timers MUST be deleted before deleting the associated event loop. + // Failure to do so will result in a crash. + furi_event_loop_timer_free(app->exit_timer); + furi_thread_free(app->worker_thread); + furi_event_loop_free(app->event_loop); + + free(app); +} + +static void event_loop_multi_app_run(EventLoopMultiApp* app) { + FURI_LOG_I(TAG, "Press keys to see them printed here."); + FURI_LOG_I(TAG, "Long press \"Back\" to exit after %lu seconds.", COUNTDOWN_START_VALUE); + FURI_LOG_I(TAG, "Long press \"Ok\" to cancel the countdown."); + + // Start the worker thread + furi_thread_start(app->worker_thread); + // Run the application event loop. This call will block until the application is about to exit. + furi_event_loop_run(app->event_loop); + // Wait for the worker thread to finish. + furi_thread_join(app->worker_thread); +} + +/******************************************************************* + * vvv START HERE vvv + * + * The application's entry point - referenced in application.fam + *******************************************************************/ +int32_t example_event_loop_multi_app(void* arg) { + UNUSED(arg); + + EventLoopMultiApp* app = event_loop_multi_app_alloc(); + event_loop_multi_app_run(app); + event_loop_multi_app_free(app); + + return 0; +} diff --git a/applications/examples/example_event_loop/example_event_loop_mutex.c b/applications/examples/example_event_loop/example_event_loop_mutex.c new file mode 100644 index 000000000..d043f3f89 --- /dev/null +++ b/applications/examples/example_event_loop/example_event_loop_mutex.c @@ -0,0 +1,140 @@ +/** + * @file example_event_loop_mutex.c + * @brief Example application that demonstrates the FuriEventLoop and FuriMutex integration. + * + * This application simulates a use case where a time-consuming blocking operation is executed + * in a separate thread and a mutex is being used for synchronization. The application runs 10 iterations + * of the above mentioned simulated work and prints the results to the debug output each time, then exits. + */ + +#include +#include + +#define TAG "ExampleEventLoopMutex" + +#define WORKER_ITERATION_COUNT (10) +// We are interested in IN events (for the mutex, that means that the mutex has been released), +// using edge trigger mode (reacting only to changes in mutex state) and +// employing one-shot mode to automatically unsubscribe before the event is processed. +#define MUTEX_EVENT_AND_FLAGS \ + (FuriEventLoopEventIn | FuriEventLoopEventFlagEdge | FuriEventLoopEventFlagOnce) + +typedef struct { + FuriEventLoop* event_loop; + FuriThread* worker_thread; + FuriMutex* worker_mutex; + uint8_t worker_result; +} EventLoopMutexApp; + +// This funciton is being run in a separate thread to simulate lenghty blocking operations +static int32_t event_loop_mutex_app_worker_thread(void* context) { + furi_assert(context); + EventLoopMutexApp* app = context; + + FURI_LOG_I(TAG, "Worker thread started"); + + // Run 10 iterations of simulated work + for(uint32_t i = 0; i < WORKER_ITERATION_COUNT; ++i) { + FURI_LOG_I(TAG, "Doing work ..."); + // Take the mutex so that no-one can access the worker_result variable + furi_check(furi_mutex_acquire(app->worker_mutex, FuriWaitForever) == FuriStatusOk); + // Simulate a blocking operation with a random delay between 900 and 1100 ms + const uint32_t work_time_ms = 900 + furi_hal_random_get() % 200; + furi_delay_ms(work_time_ms); + // Simulate a result with a random number between 0 and 255 + app->worker_result = furi_hal_random_get() % 0xFF; + + FURI_LOG_I(TAG, "Work done in %lu ms", work_time_ms); + // Release the mutex, which will notify the event loop that the result is ready + furi_check(furi_mutex_release(app->worker_mutex) == FuriStatusOk); + // Return control to the scheduler so that the event loop can take the mutex in its turn + furi_thread_yield(); + } + + FURI_LOG_I(TAG, "All work done, worker thread out!"); + // Request the event loop to stop + furi_event_loop_stop(app->event_loop); + + return 0; +} + +// This function is being run each time when the mutex gets released +static bool event_loop_mutex_app_event_callback(FuriEventLoopObject* object, void* context) { + furi_assert(context); + + EventLoopMutexApp* app = context; + furi_assert(object == app->worker_mutex); + + // Take the mutex so that no-one can access the worker_result variable + // IMPORTANT: the wait time MUST be 0, i.e. the event loop event callbacks + // must NOT ever block. If it is possible that the mutex will be taken by + // others, then the event callback code must take it into account. + furi_check(furi_mutex_acquire(app->worker_mutex, 0) == FuriStatusOk); + // Access the worker_result variable and print it. + FURI_LOG_I(TAG, "Result available! Value: %u", app->worker_result); + // Release the mutex, enabling the worker thread to continue when it's ready + furi_check(furi_mutex_release(app->worker_mutex) == FuriStatusOk); + // Subscribe for the mutex release events again, since we were unsubscribed automatically + // before processing the event. + furi_event_loop_subscribe_mutex( + app->event_loop, + app->worker_mutex, + MUTEX_EVENT_AND_FLAGS, + event_loop_mutex_app_event_callback, + app); + + return true; +} + +static EventLoopMutexApp* event_loop_mutex_app_alloc(void) { + EventLoopMutexApp* app = malloc(sizeof(EventLoopMutexApp)); + + // Create an event loop instance. + app->event_loop = furi_event_loop_alloc(); + // Create a worker thread instance. + app->worker_thread = furi_thread_alloc_ex( + "EventLoopMutexWorker", 1024, event_loop_mutex_app_worker_thread, app); + // Create a mutex instance. + app->worker_mutex = furi_mutex_alloc(FuriMutexTypeNormal); + // Subscribe for the mutex release events. + // Note that since FuriEventLoopEventFlagOneShot is used, we will be automatically unsubscribed + // from events before entering the event processing callback. This is necessary in order to not + // trigger on events caused by releasing the mutex in the callback. + furi_event_loop_subscribe_mutex( + app->event_loop, + app->worker_mutex, + MUTEX_EVENT_AND_FLAGS, + event_loop_mutex_app_event_callback, + app); + + return app; +} + +static void event_loop_mutex_app_free(EventLoopMutexApp* app) { + // IMPORTANT: The user code MUST unsubscribe from all events before deleting the event loop. + // Failure to do so will result in a crash. + furi_event_loop_unsubscribe(app->event_loop, app->worker_mutex); + // Delete all instances + furi_thread_free(app->worker_thread); + furi_mutex_free(app->worker_mutex); + furi_event_loop_free(app->event_loop); + + free(app); +} + +static void event_loop_mutex_app_run(EventLoopMutexApp* app) { + furi_thread_start(app->worker_thread); + furi_event_loop_run(app->event_loop); + furi_thread_join(app->worker_thread); +} + +// The application's entry point - referenced in application.fam +int32_t example_event_loop_mutex_app(void* arg) { + UNUSED(arg); + + EventLoopMutexApp* app = event_loop_mutex_app_alloc(); + event_loop_mutex_app_run(app); + event_loop_mutex_app_free(app); + + return 0; +} diff --git a/applications/examples/example_event_loop/example_event_loop_stream_buffer.c b/applications/examples/example_event_loop/example_event_loop_stream_buffer.c new file mode 100644 index 000000000..65dbd83cf --- /dev/null +++ b/applications/examples/example_event_loop/example_event_loop_stream_buffer.c @@ -0,0 +1,131 @@ +/** + * @file example_event_loop_stream_buffer.c + * @brief Example application that demonstrates the FuriEventLoop and FuriStreamBuffer integration. + * + * This application simulates a use case where some data data stream comes from a separate thread (or hardware) + * and a stream buffer is used to act as an intermediate buffer. The worker thread produces 10 iterations of 32 + * bytes of simulated data, and each time when the buffer is half-filled, the data is taken out of it and printed + * to the debug output. After completing all iterations, the application exits. + */ + +#include +#include + +#define TAG "ExampleEventLoopStreamBuffer" + +#define WORKER_ITERATION_COUNT (10) + +#define STREAM_BUFFER_SIZE (32) +#define STREAM_BUFFER_TRIG_LEVEL (STREAM_BUFFER_SIZE / 2) +#define STREAM_BUFFER_EVENT_AND_FLAGS (FuriEventLoopEventIn | FuriEventLoopEventFlagEdge) + +typedef struct { + FuriEventLoop* event_loop; + FuriThread* worker_thread; + FuriStreamBuffer* stream_buffer; +} EventLoopStreamBufferApp; + +// This funciton is being run in a separate thread to simulate data coming from a producer thread or some device. +static int32_t event_loop_stream_buffer_app_worker_thread(void* context) { + furi_assert(context); + EventLoopStreamBufferApp* app = context; + + FURI_LOG_I(TAG, "Worker thread started"); + + for(uint32_t i = 0; i < WORKER_ITERATION_COUNT; ++i) { + // Produce 32 bytes of simulated data. + for(uint32_t j = 0; j < STREAM_BUFFER_SIZE; ++j) { + // Simulate incoming data by generating a random byte. + uint8_t data = furi_hal_random_get() % 0xFF; + // Put the byte in the buffer. Depending on the use case, it may or may be not acceptable + // to wait for free space to become available. + furi_check( + furi_stream_buffer_send(app->stream_buffer, &data, 1, FuriWaitForever) == 1); + // Delay between 30 and 50 ms to slow down the output for clarity. + furi_delay_ms(30 + furi_hal_random_get() % 20); + } + } + + FURI_LOG_I(TAG, "All work done, worker thread out!"); + // Request the event loop to stop + furi_event_loop_stop(app->event_loop); + + return 0; +} + +// This function is being run each time when the number of bytes in the buffer is above its trigger level. +static bool + event_loop_stream_buffer_app_event_callback(FuriEventLoopObject* object, void* context) { + furi_assert(context); + EventLoopStreamBufferApp* app = context; + + furi_assert(object == app->stream_buffer); + + // Temporary buffer that can hold at most half of the stream buffer's capacity. + uint8_t data[STREAM_BUFFER_TRIG_LEVEL]; + // Receive the data. It is guaranteed that the amount of data in the buffer will be equal to + // or greater than the trigger level, therefore, no waiting delay is necessary. + furi_check( + furi_stream_buffer_receive(app->stream_buffer, data, sizeof(data), 0) == sizeof(data)); + + // Format the data for printing and print it to the debug output. + FuriString* tmp_str = furi_string_alloc(); + for(uint32_t i = 0; i < sizeof(data); ++i) { + furi_string_cat_printf(tmp_str, "%02X ", data[i]); + } + + FURI_LOG_I(TAG, "Received data: %s", furi_string_get_cstr(tmp_str)); + furi_string_free(tmp_str); + + return true; +} + +static EventLoopStreamBufferApp* event_loop_stream_buffer_app_alloc(void) { + EventLoopStreamBufferApp* app = malloc(sizeof(EventLoopStreamBufferApp)); + + // Create an event loop instance. + app->event_loop = furi_event_loop_alloc(); + // Create a worker thread instance. + app->worker_thread = furi_thread_alloc_ex( + "EventLoopStreamBufferWorker", 1024, event_loop_stream_buffer_app_worker_thread, app); + // Create a stream_buffer instance. + app->stream_buffer = furi_stream_buffer_alloc(STREAM_BUFFER_SIZE, STREAM_BUFFER_TRIG_LEVEL); + // Subscribe for the stream buffer IN events in edge triggered mode. + furi_event_loop_subscribe_stream_buffer( + app->event_loop, + app->stream_buffer, + STREAM_BUFFER_EVENT_AND_FLAGS, + event_loop_stream_buffer_app_event_callback, + app); + + return app; +} + +static void event_loop_stream_buffer_app_free(EventLoopStreamBufferApp* app) { + // IMPORTANT: The user code MUST unsubscribe from all events before deleting the event loop. + // Failure to do so will result in a crash. + furi_event_loop_unsubscribe(app->event_loop, app->stream_buffer); + // Delete all instances + furi_thread_free(app->worker_thread); + furi_stream_buffer_free(app->stream_buffer); + furi_event_loop_free(app->event_loop); + + free(app); +} + +static void event_loop_stream_buffer_app_run(EventLoopStreamBufferApp* app) { + furi_thread_start(app->worker_thread); + furi_event_loop_run(app->event_loop); + furi_thread_join(app->worker_thread); +} + +// The application's entry point - referenced in application.fam +int32_t example_event_loop_stream_buffer_app(void* arg) { + UNUSED(arg); + + EventLoopStreamBufferApp* app = event_loop_stream_buffer_app_alloc(); + event_loop_stream_buffer_app_run(app); + event_loop_stream_buffer_app_free(app); + + return 0; +} diff --git a/applications/examples/example_event_loop/example_event_loop_timer.c b/applications/examples/example_event_loop/example_event_loop_timer.c new file mode 100644 index 000000000..e255f6b61 --- /dev/null +++ b/applications/examples/example_event_loop/example_event_loop_timer.c @@ -0,0 +1,87 @@ +/** + * @file example_event_loop_timer.c + * @brief Example application that demonstrates FuriEventLoop's software timer capability. + * + * This application prints a countdown from 10 to 0 to the debug output and then exits. + * Despite only one timer being used in this example for clarity, an event loop instance can have + * an arbitrary number of independent timers of any type (periodic or one-shot). + * + */ +#include + +#define TAG "ExampleEventLoopTimer" + +#define COUNTDOWN_START_VALUE (10) +#define COUNTDOWN_INTERVAL_MS (1000) + +typedef struct { + FuriEventLoop* event_loop; + FuriEventLoopTimer* timer; + uint32_t countdown_value; +} EventLoopTimerApp; + +// This function is called each time the timer expires (i.e. once per 1000 ms (1s) in this example) +static void event_loop_timer_callback(void* context) { + furi_assert(context); + EventLoopTimerApp* app = context; + + // Print the countdown value + FURI_LOG_I(TAG, "T-00:00:%02lu", app->countdown_value); + + if(app->countdown_value == 0) { + // If the countdown reached 0, print the final line and stop the event loop + FURI_LOG_I(TAG, "Blast off to adventure!"); + // After this call, the control will be returned back to event_loop_timers_app_run() + furi_event_loop_stop(app->event_loop); + + } else { + // Decrement the countdown value + app->countdown_value -= 1; + } +} + +static EventLoopTimerApp* event_loop_timer_app_alloc(void) { + EventLoopTimerApp* app = malloc(sizeof(EventLoopTimerApp)); + + // Create an event loop instance. + app->event_loop = furi_event_loop_alloc(); + // Create a software timer instance. + // The timer is bound to the event loop instance and will execute in its context. + // Here, the timer type is periodic, i.e. it will restart automatically after expiring. + app->timer = furi_event_loop_timer_alloc( + app->event_loop, event_loop_timer_callback, FuriEventLoopTimerTypePeriodic, app); + // The countdown value will be tracked in this variable. + app->countdown_value = COUNTDOWN_START_VALUE; + + return app; +} + +static void event_loop_timer_app_free(EventLoopTimerApp* app) { + // IMPORTANT: All event loop timers MUST be deleted BEFORE deleting the event loop itself. + // Failure to do so will result in a crash. + furi_event_loop_timer_free(app->timer); + // With all timers deleted, it's safe to delete the event loop. + furi_event_loop_free(app->event_loop); + free(app); +} + +static void event_loop_timer_app_run(EventLoopTimerApp* app) { + FURI_LOG_I(TAG, "All systems go! Prepare for countdown!"); + + // Timers can be started either before the event loop is run, or in any + // callback function called by a running event loop. + furi_event_loop_timer_start(app->timer, COUNTDOWN_INTERVAL_MS); + // This call will block until furi_event_loop_stop() is called. + furi_event_loop_run(app->event_loop); +} + +// The application's entry point - referenced in application.fam +int32_t example_event_loop_timer_app(void* arg) { + UNUSED(arg); + + EventLoopTimerApp* app = event_loop_timer_app_alloc(); + event_loop_timer_app_run(app); + event_loop_timer_app_free(app); + + return 0; +} diff --git a/applications/examples/example_number_input/ReadMe.md b/applications/examples/example_number_input/ReadMe.md new file mode 100644 index 000000000..9d5a0a9e5 --- /dev/null +++ b/applications/examples/example_number_input/ReadMe.md @@ -0,0 +1,7 @@ +# Number Input + +Simple keyboard that limits user inputs to a full number (integer). Useful to enforce correct entries without the need of intense validations after a user input. + +Definition of min/max values is required. Numbers are of type int32_t. If negative numbers are allowed withing min - max, an additional button is displayed to switch the sign between + and -. + +It is also possible to define a header text, shown in this example app with the 3 different input options. \ No newline at end of file diff --git a/applications/examples/example_number_input/application.fam b/applications/examples/example_number_input/application.fam new file mode 100644 index 000000000..58cff4496 --- /dev/null +++ b/applications/examples/example_number_input/application.fam @@ -0,0 +1,10 @@ +App( + appid="example_number_input", + name="Example: Number Input", + apptype=FlipperAppType.EXTERNAL, + entry_point="example_number_input", + requires=["gui"], + stack_size=1 * 1024, + fap_icon="example_number_input_10px.png", + fap_category="Examples", +) diff --git a/applications/examples/example_number_input/example_number_input.c b/applications/examples/example_number_input/example_number_input.c new file mode 100644 index 000000000..19d787ef5 --- /dev/null +++ b/applications/examples/example_number_input/example_number_input.c @@ -0,0 +1,79 @@ +#include "example_number_input.h" + +bool example_number_input_custom_event_callback(void* context, uint32_t event) { + furi_assert(context); + ExampleNumberInput* app = context; + return scene_manager_handle_custom_event(app->scene_manager, event); +} + +static bool example_number_input_back_event_callback(void* context) { + furi_assert(context); + ExampleNumberInput* app = context; + return scene_manager_handle_back_event(app->scene_manager); +} + +static ExampleNumberInput* example_number_input_alloc() { + ExampleNumberInput* app = malloc(sizeof(ExampleNumberInput)); + app->gui = furi_record_open(RECORD_GUI); + + app->view_dispatcher = view_dispatcher_alloc(); + + app->scene_manager = scene_manager_alloc(&example_number_input_scene_handlers, app); + view_dispatcher_set_event_callback_context(app->view_dispatcher, app); + view_dispatcher_set_custom_event_callback( + app->view_dispatcher, example_number_input_custom_event_callback); + view_dispatcher_set_navigation_event_callback( + app->view_dispatcher, example_number_input_back_event_callback); + + app->number_input = number_input_alloc(); + view_dispatcher_add_view( + app->view_dispatcher, + ExampleNumberInputViewIdNumberInput, + number_input_get_view(app->number_input)); + + app->dialog_ex = dialog_ex_alloc(); + view_dispatcher_add_view( + app->view_dispatcher, + ExampleNumberInputViewIdShowNumber, + dialog_ex_get_view(app->dialog_ex)); + + app->current_number = 5; + app->min_value = INT32_MIN; + app->max_value = INT32_MAX; + + return app; +} + +static void example_number_input_free(ExampleNumberInput* app) { + furi_assert(app); + + view_dispatcher_remove_view(app->view_dispatcher, ExampleNumberInputViewIdShowNumber); + dialog_ex_free(app->dialog_ex); + + view_dispatcher_remove_view(app->view_dispatcher, ExampleNumberInputViewIdNumberInput); + number_input_free(app->number_input); + + scene_manager_free(app->scene_manager); + view_dispatcher_free(app->view_dispatcher); + + furi_record_close(RECORD_GUI); + app->gui = NULL; + + //Remove whatever is left + free(app); +} + +int32_t example_number_input(void* p) { + UNUSED(p); + ExampleNumberInput* app = example_number_input_alloc(); + + view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); + + scene_manager_next_scene(app->scene_manager, ExampleNumberInputSceneShowNumber); + + view_dispatcher_run(app->view_dispatcher); + + example_number_input_free(app); + + return 0; +} diff --git a/applications/examples/example_number_input/example_number_input.h b/applications/examples/example_number_input/example_number_input.h new file mode 100644 index 000000000..8d944e6fd --- /dev/null +++ b/applications/examples/example_number_input/example_number_input.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "scenes/example_number_input_scene.h" + +typedef struct ExampleNumberInputShowNumber ExampleNumberInputShowNumber; + +typedef enum { + ExampleNumberInputViewIdShowNumber, + ExampleNumberInputViewIdNumberInput, +} ExampleNumberInputViewId; + +typedef struct { + Gui* gui; + SceneManager* scene_manager; + ViewDispatcher* view_dispatcher; + + NumberInput* number_input; + DialogEx* dialog_ex; + + int32_t current_number; + int32_t min_value; + int32_t max_value; +} ExampleNumberInput; diff --git a/applications/examples/example_number_input/example_number_input_10px.png b/applications/examples/example_number_input/example_number_input_10px.png new file mode 100644 index 000000000..bdb494fcd Binary files /dev/null and b/applications/examples/example_number_input/example_number_input_10px.png differ diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene.c b/applications/examples/example_number_input/scenes/example_number_input_scene.c new file mode 100644 index 000000000..caf77fa8c --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene.c @@ -0,0 +1,30 @@ +#include "example_number_input_scene.h" + +// Generate scene on_enter handlers array +#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter, +void (*const example_number_input_on_enter_handlers[])(void*) = { +#include "example_number_input_scene_config.h" +}; +#undef ADD_SCENE + +// Generate scene on_event handlers array +#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event, +bool (*const example_number_input_on_event_handlers[])(void* context, SceneManagerEvent event) = { +#include "example_number_input_scene_config.h" +}; +#undef ADD_SCENE + +// Generate scene on_exit handlers array +#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit, +void (*const example_number_input_on_exit_handlers[])(void* context) = { +#include "example_number_input_scene_config.h" +}; +#undef ADD_SCENE + +// Initialize scene handlers configuration structure +const SceneManagerHandlers example_number_input_scene_handlers = { + .on_enter_handlers = example_number_input_on_enter_handlers, + .on_event_handlers = example_number_input_on_event_handlers, + .on_exit_handlers = example_number_input_on_exit_handlers, + .scene_num = ExampleNumberInputSceneNum, +}; diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene.h b/applications/examples/example_number_input/scenes/example_number_input_scene.h new file mode 100644 index 000000000..49fcd256f --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +// Generate scene id and total number +#define ADD_SCENE(prefix, name, id) ExampleNumberInputScene##id, +typedef enum { +#include "example_number_input_scene_config.h" + ExampleNumberInputSceneNum, +} ExampleNumberInputScene; +#undef ADD_SCENE + +extern const SceneManagerHandlers example_number_input_scene_handlers; + +// Generate scene on_enter handlers declaration +#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*); +#include "example_number_input_scene_config.h" +#undef ADD_SCENE + +// Generate scene on_event handlers declaration +#define ADD_SCENE(prefix, name, id) \ + bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event); +#include "example_number_input_scene_config.h" +#undef ADD_SCENE + +// Generate scene on_exit handlers declaration +#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context); +#include "example_number_input_scene_config.h" +#undef ADD_SCENE diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene_config.h b/applications/examples/example_number_input/scenes/example_number_input_scene_config.h new file mode 100644 index 000000000..71acbda52 --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene_config.h @@ -0,0 +1,4 @@ +ADD_SCENE(example_number_input, input_number, InputNumber) +ADD_SCENE(example_number_input, show_number, ShowNumber) +ADD_SCENE(example_number_input, input_max, InputMax) +ADD_SCENE(example_number_input, input_min, InputMin) diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene_input_max.c b/applications/examples/example_number_input/scenes/example_number_input_scene_input_max.c new file mode 100644 index 000000000..7478f58a7 --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene_input_max.c @@ -0,0 +1,39 @@ +#include "../example_number_input.h" + +void example_number_input_scene_input_max_callback(void* context, int32_t number) { + ExampleNumberInput* app = context; + app->max_value = number; + view_dispatcher_send_custom_event(app->view_dispatcher, 0); +} + +void example_number_input_scene_input_max_on_enter(void* context) { + furi_assert(context); + ExampleNumberInput* app = context; + NumberInput* number_input = app->number_input; + + number_input_set_header_text(number_input, "Enter the maximum value"); + number_input_set_result_callback( + number_input, + example_number_input_scene_input_max_callback, + context, + app->max_value, + app->min_value, + INT32_MAX); + + view_dispatcher_switch_to_view(app->view_dispatcher, ExampleNumberInputViewIdNumberInput); +} + +bool example_number_input_scene_input_max_on_event(void* context, SceneManagerEvent event) { + ExampleNumberInput* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + scene_manager_previous_scene(app->scene_manager); + return true; + } + return consumed; +} + +void example_number_input_scene_input_max_on_exit(void* context) { + UNUSED(context); +} diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene_input_min.c b/applications/examples/example_number_input/scenes/example_number_input_scene_input_min.c new file mode 100644 index 000000000..ad7656232 --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene_input_min.c @@ -0,0 +1,39 @@ +#include "../example_number_input.h" + +void example_number_input_scene_input_min_callback(void* context, int32_t number) { + ExampleNumberInput* app = context; + app->min_value = number; + view_dispatcher_send_custom_event(app->view_dispatcher, 0); +} + +void example_number_input_scene_input_min_on_enter(void* context) { + furi_assert(context); + ExampleNumberInput* app = context; + NumberInput* number_input = app->number_input; + + number_input_set_header_text(number_input, "Enter the minimum value"); + number_input_set_result_callback( + number_input, + example_number_input_scene_input_min_callback, + context, + app->min_value, + INT32_MIN, + app->max_value); + + view_dispatcher_switch_to_view(app->view_dispatcher, ExampleNumberInputViewIdNumberInput); +} + +bool example_number_input_scene_input_min_on_event(void* context, SceneManagerEvent event) { + ExampleNumberInput* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + scene_manager_previous_scene(app->scene_manager); + return true; + } + return consumed; +} + +void example_number_input_scene_input_min_on_exit(void* context) { + UNUSED(context); +} diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene_input_number.c b/applications/examples/example_number_input/scenes/example_number_input_scene_input_number.c new file mode 100644 index 000000000..d9b1fd52f --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene_input_number.c @@ -0,0 +1,42 @@ +#include "../example_number_input.h" + +void example_number_input_scene_input_number_callback(void* context, int32_t number) { + ExampleNumberInput* app = context; + app->current_number = number; + view_dispatcher_send_custom_event(app->view_dispatcher, 0); +} + +void example_number_input_scene_input_number_on_enter(void* context) { + furi_assert(context); + ExampleNumberInput* app = context; + NumberInput* number_input = app->number_input; + + char str[50]; + snprintf(str, sizeof(str), "Set Number (%ld - %ld)", app->min_value, app->max_value); + + number_input_set_header_text(number_input, str); + number_input_set_result_callback( + number_input, + example_number_input_scene_input_number_callback, + context, + app->current_number, + app->min_value, + app->max_value); + + view_dispatcher_switch_to_view(app->view_dispatcher, ExampleNumberInputViewIdNumberInput); +} + +bool example_number_input_scene_input_number_on_event(void* context, SceneManagerEvent event) { + ExampleNumberInput* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { //Back button pressed + scene_manager_previous_scene(app->scene_manager); + return true; + } + return consumed; +} + +void example_number_input_scene_input_number_on_exit(void* context) { + UNUSED(context); +} diff --git a/applications/examples/example_number_input/scenes/example_number_input_scene_show_number.c b/applications/examples/example_number_input/scenes/example_number_input_scene_show_number.c new file mode 100644 index 000000000..2afdaf5c1 --- /dev/null +++ b/applications/examples/example_number_input/scenes/example_number_input_scene_show_number.c @@ -0,0 +1,66 @@ +#include "../example_number_input.h" + +static void + example_number_input_scene_confirm_dialog_callback(DialogExResult result, void* context) { + ExampleNumberInput* app = context; + + view_dispatcher_send_custom_event(app->view_dispatcher, result); +} + +static void example_number_input_scene_update_view(void* context) { + ExampleNumberInput* app = context; + DialogEx* dialog_ex = app->dialog_ex; + + dialog_ex_set_header(dialog_ex, "The number is", 64, 0, AlignCenter, AlignTop); + + static char buffer[12]; //needs static for extended lifetime + + snprintf(buffer, sizeof(buffer), "%ld", app->current_number); + dialog_ex_set_text(dialog_ex, buffer, 64, 29, AlignCenter, AlignCenter); + + dialog_ex_set_left_button_text(dialog_ex, "Min"); + dialog_ex_set_right_button_text(dialog_ex, "Max"); + dialog_ex_set_center_button_text(dialog_ex, "Change"); + + dialog_ex_set_result_callback(dialog_ex, example_number_input_scene_confirm_dialog_callback); + dialog_ex_set_context(dialog_ex, app); +} + +void example_number_input_scene_show_number_on_enter(void* context) { + furi_assert(context); + ExampleNumberInput* app = context; + + example_number_input_scene_update_view(app); + + view_dispatcher_switch_to_view(app->view_dispatcher, ExampleNumberInputViewIdShowNumber); +} + +bool example_number_input_scene_show_number_on_event(void* context, SceneManagerEvent event) { + ExampleNumberInput* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + switch(event.event) { + case DialogExResultCenter: + scene_manager_next_scene(app->scene_manager, ExampleNumberInputSceneInputNumber); + consumed = true; + break; + case DialogExResultLeft: + scene_manager_next_scene(app->scene_manager, ExampleNumberInputSceneInputMin); + consumed = true; + break; + case DialogExResultRight: + scene_manager_next_scene(app->scene_manager, ExampleNumberInputSceneInputMax); + consumed = true; + break; + default: + break; + } + } + + return consumed; +} + +void example_number_input_scene_show_number_on_exit(void* context) { + UNUSED(context); +} diff --git a/applications/examples/example_view_dispatcher/application.fam b/applications/examples/example_view_dispatcher/application.fam new file mode 100644 index 000000000..f7b743bcf --- /dev/null +++ b/applications/examples/example_view_dispatcher/application.fam @@ -0,0 +1,8 @@ +App( + appid="example_view_dispatcher", + name="Example: ViewDispatcher", + apptype=FlipperAppType.EXTERNAL, + entry_point="example_view_dispatcher_app", + requires=["gui"], + fap_category="Examples", +) diff --git a/applications/examples/example_view_dispatcher/example_view_dispatcher.c b/applications/examples/example_view_dispatcher/example_view_dispatcher.c new file mode 100644 index 000000000..71d29edfd --- /dev/null +++ b/applications/examples/example_view_dispatcher/example_view_dispatcher.c @@ -0,0 +1,173 @@ +/** + * @file example_view_dispatcher.c + * @brief Example application demonstrating the usage of the ViewDispatcher library. + * + * This application can display one of two views: either a Widget or a Submenu. + * Each view has its own way of switching to another one: + * + * - A center button in the Widget view. + * - A submenu item in the Submenu view + * + * Press either to switch to a different view. Press Back to exit the application. + * + */ + +#include +#include + +#include +#include + +// Enumeration of the view indexes. +typedef enum { + ViewIndexWidget, + ViewIndexSubmenu, + ViewIndexCount, +} ViewIndex; + +// Enumeration of submenu items. +typedef enum { + SubmenuIndexNothing, + SubmenuIndexSwitchView, +} SubmenuIndex; + +// Main application structure. +typedef struct { + ViewDispatcher* view_dispatcher; + Widget* widget; + Submenu* submenu; +} ExampleViewDispatcherApp; + +// This function is called when the user has pressed the Back key. +static bool example_view_dispatcher_app_navigation_callback(void* context) { + furi_assert(context); + ExampleViewDispatcherApp* app = context; + // Back means exit the application, which can be done by stopping the ViewDispatcher. + view_dispatcher_stop(app->view_dispatcher); + return true; +} + +// This function is called when there are custom events to process. +static bool example_view_dispatcher_app_custom_event_callback(void* context, uint32_t event) { + furi_assert(context); + ExampleViewDispatcherApp* app = context; + // The event numerical value can mean different things (the application is responsible to uphold its chosen convention) + // In this example, the only possible meaning is the view index to switch to. + furi_assert(event < ViewIndexCount); + // Switch to the requested view. + view_dispatcher_switch_to_view(app->view_dispatcher, event); + + return true; +} + +// This function is called when the user presses the "Switch View" button on the Widget view. +static void example_view_dispatcher_app_button_callback( + GuiButtonType button_type, + InputType input_type, + void* context) { + furi_assert(context); + ExampleViewDispatcherApp* app = context; + // Only request the view switch if the user short-presses the Center button. + if(button_type == GuiButtonTypeCenter && input_type == InputTypeShort) { + // Request switch to the Submenu view via the custom event queue. + view_dispatcher_send_custom_event(app->view_dispatcher, ViewIndexSubmenu); + } +} + +// This function is called when the user activates the "Switch View" submenu item. +static void example_view_dispatcher_app_submenu_callback(void* context, uint32_t index) { + furi_assert(context); + ExampleViewDispatcherApp* app = context; + // Only request the view switch if the user activates the "Switch View" item. + if(index == SubmenuIndexSwitchView) { + // Request switch to the Widget view via the custom event queue. + view_dispatcher_send_custom_event(app->view_dispatcher, ViewIndexWidget); + } +} + +// Application constructor function. +static ExampleViewDispatcherApp* example_view_dispatcher_app_alloc() { + ExampleViewDispatcherApp* app = malloc(sizeof(ExampleViewDispatcherApp)); + // Access the GUI API instance. + Gui* gui = furi_record_open(RECORD_GUI); + // Create and initialize the Widget view. + app->widget = widget_alloc(); + widget_add_string_multiline_element( + app->widget, 64, 32, AlignCenter, AlignCenter, FontSecondary, "Press the Button below"); + widget_add_button_element( + app->widget, + GuiButtonTypeCenter, + "Switch View", + example_view_dispatcher_app_button_callback, + app); + // Create and initialize the Submenu view. + app->submenu = submenu_alloc(); + submenu_add_item(app->submenu, "Do Nothing", SubmenuIndexNothing, NULL, NULL); + submenu_add_item( + app->submenu, + "Switch View", + SubmenuIndexSwitchView, + example_view_dispatcher_app_submenu_callback, + app); + // Create the ViewDispatcher instance. + app->view_dispatcher = view_dispatcher_alloc(); + // Let the GUI know about this ViewDispatcher instance. + view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen); + // Register the views within the ViewDispatcher instance. This alone will not show any of them on the screen. + // Each view must have its own index to refer to it later (it is best done via an enumeration as shown here). + view_dispatcher_add_view(app->view_dispatcher, ViewIndexWidget, widget_get_view(app->widget)); + view_dispatcher_add_view( + app->view_dispatcher, ViewIndexSubmenu, submenu_get_view(app->submenu)); + // Set the custom event callback. It will be called each time a custom event is scheduled + // using the view_dispatcher_send_custom_callback() function. + view_dispatcher_set_custom_event_callback( + app->view_dispatcher, example_view_dispatcher_app_custom_event_callback); + // Set the navigation, or back button callback. It will be called if the user pressed the Back button + // and the event was not handled in the currently displayed view. + view_dispatcher_set_navigation_event_callback( + app->view_dispatcher, example_view_dispatcher_app_navigation_callback); + // The context will be passed to the callbacks as a parameter, so we have access to our application object. + view_dispatcher_set_event_callback_context(app->view_dispatcher, app); + + return app; +} + +// Application destructor function. +static void example_view_dispatcher_app_free(ExampleViewDispatcherApp* app) { + // All views must be un-registered (removed) from a ViewDispatcher instance + // before deleting it. Failure to do so will result in a crash. + view_dispatcher_remove_view(app->view_dispatcher, ViewIndexWidget); + view_dispatcher_remove_view(app->view_dispatcher, ViewIndexSubmenu); + // Now it is safe to delete the ViewDispatcher instance. + view_dispatcher_free(app->view_dispatcher); + // Delete the views + widget_free(app->widget); + submenu_free(app->submenu); + // End access to hte the GUI API. + furi_record_close(RECORD_GUI); + // Free the remaining memory. + free(app); +} + +static void example_view_dispatcher_app_run(ExampleViewDispatcherApp* app) { + // Display the Widget view on the screen. + view_dispatcher_switch_to_view(app->view_dispatcher, ViewIndexWidget); + // This function will block until view_dispatcher_stop() is called. + // Internally, it uses a FuriEventLoop (see FuriEventLoop examples for more info on this). + view_dispatcher_run(app->view_dispatcher); +} + +/******************************************************************* + * vvv START HERE vvv + * + * The application's entry point - referenced in application.fam + *******************************************************************/ +int32_t example_view_dispatcher_app(void* arg) { + UNUSED(arg); + + ExampleViewDispatcherApp* app = example_view_dispatcher_app_alloc(); + example_view_dispatcher_app_run(app); + example_view_dispatcher_app_free(app); + + return 0; +} diff --git a/applications/examples/example_view_holder/application.fam b/applications/examples/example_view_holder/application.fam new file mode 100644 index 000000000..19ad8d2ac --- /dev/null +++ b/applications/examples/example_view_holder/application.fam @@ -0,0 +1,8 @@ +App( + appid="example_view_holder", + name="Example: ViewHolder", + apptype=FlipperAppType.EXTERNAL, + entry_point="example_view_holder_app", + requires=["gui"], + fap_category="Examples", +) diff --git a/applications/examples/example_view_holder/example_view_holder.c b/applications/examples/example_view_holder/example_view_holder.c new file mode 100644 index 000000000..24907dbc2 --- /dev/null +++ b/applications/examples/example_view_holder/example_view_holder.c @@ -0,0 +1,78 @@ +/** + * @file example_view_holder.c + * @brief Example application demonstrating the usage of the ViewHolder library. + * + * This application will display a text box with some scrollable text in it. + * Press the Back key to exit the application. + */ + +#include +#include +#include + +#include + +// This function will be called when the user presses the Back button. +static void example_view_holder_back_callback(void* context) { + FuriApiLock exit_lock = context; + // Unlock the exit lock, thus enabling the app to exit. + api_lock_unlock(exit_lock); +} + +int32_t example_view_holder_app(void* arg) { + UNUSED(arg); + + // Access the GUI API instance. + Gui* gui = furi_record_open(RECORD_GUI); + // Create a TextBox view. The Gui object only accepts + // ViewPort instances, so we will need to address that later. + TextBox* text_box = text_box_alloc(); + // Set some text so that the text box is not empty. + text_box_set_text( + text_box, + "ViewHolder is being used\n" + "to show this TextBox view.\n\n" + "Scroll down to see more.\n\n\n" + "Press \"Back\" to exit."); + + // Create a ViewHolder instance. It will serve as an adapter to convert + // between the View type provided by the TextBox view and the ViewPort type + // that the GUI can actually display. + ViewHolder* view_holder = view_holder_alloc(); + // Let the GUI know about this ViewHolder instance. + view_holder_attach_to_gui(view_holder, gui); + // Set the view that we want to display. + view_holder_set_view(view_holder, text_box_get_view(text_box)); + + // The part below is not really related to this example, but is necessary for it to function. + // We need to somehow stall the application thread so that the view stays on the screen (otherwise + // the app will just exit and won't display anything) and at the same time we need a way to quit out + // of the application. + + // In this example, a simple FuriApiLock instance is used. A real-world application is likely to have some + // kind of event handling loop here instead. (see the ViewDispatcher example or one of FuriEventLoop + // examples for that). + + // Create a pre-locked FuriApiLock instance. + FuriApiLock exit_lock = api_lock_alloc_locked(); + // Set a Back event callback for the ViewHolder instance. It will be called when the user + // presses the Back button. We pass the exit lock instance as the context to be able to access + // it inside the callback function. + view_holder_set_back_callback(view_holder, example_view_holder_back_callback, exit_lock); + + // This call will block the application thread from running until the exit lock gets unlocked somehow + // (the only way it can happen in this example is via the back callback). + api_lock_wait_unlock_and_free(exit_lock); + + // The back key has been pressed, which unlocked the exit lock. The application is about to exit. + + // The view must be removed from a ViewHolder instance before deleting it. + view_holder_set_view(view_holder, NULL); + // Delete everything to prevent memory leaks. + view_holder_free(view_holder); + text_box_free(text_box); + // End access to the GUI API. + furi_record_close(RECORD_GUI); + + return 0; +} diff --git a/applications/main/archive/archive.c b/applications/main/archive/archive.c index 432417c6e..7746e2dfe 100644 --- a/applications/main/archive/archive.c +++ b/applications/main/archive/archive.c @@ -30,7 +30,6 @@ static ArchiveApp* archive_alloc(void) { archive->view_dispatcher = view_dispatcher_alloc(); ViewDispatcher* view_dispatcher = archive->view_dispatcher; - view_dispatcher_enable_queue(view_dispatcher); view_dispatcher_set_event_callback_context(view_dispatcher, archive); view_dispatcher_set_custom_event_callback(view_dispatcher, archive_custom_event_callback); view_dispatcher_set_navigation_event_callback(view_dispatcher, archive_back_event_callback); diff --git a/applications/main/archive/helpers/archive_files.c b/applications/main/archive/helpers/archive_files.c index 8d1d5dc1a..7c61e1b24 100644 --- a/applications/main/archive/helpers/archive_files.c +++ b/applications/main/archive/helpers/archive_files.c @@ -18,7 +18,7 @@ void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder } else { for(size_t i = 0; i < COUNT_OF(known_ext); i++) { if((known_ext[i][0] == '?') || (known_ext[i][0] == '*')) continue; - if(furi_string_end_with(file->path, known_ext[i])) { + if(furi_string_end_withi(file->path, known_ext[i])) { // Check for .txt containing folder if(strcmp(known_ext[i], ".txt") == 0) { const char* txt_path = NULL; diff --git a/applications/main/gpio/gpio_app.c b/applications/main/gpio/gpio_app.c index 1d201a730..178f30c66 100644 --- a/applications/main/gpio/gpio_app.c +++ b/applications/main/gpio/gpio_app.c @@ -32,7 +32,6 @@ GpioApp* gpio_app_alloc(void) { app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&gpio_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( diff --git a/applications/main/ibutton/ibutton.c b/applications/main/ibutton/ibutton.c index 26e599b48..6619041c5 100644 --- a/applications/main/ibutton/ibutton.c +++ b/applications/main/ibutton/ibutton.c @@ -87,7 +87,6 @@ iButton* ibutton_alloc(void) { ibutton->scene_manager = scene_manager_alloc(&ibutton_scene_handlers, ibutton); ibutton->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(ibutton->view_dispatcher); view_dispatcher_set_event_callback_context(ibutton->view_dispatcher, ibutton); view_dispatcher_set_custom_event_callback( ibutton->view_dispatcher, ibutton_custom_event_callback); diff --git a/applications/main/infrared/infrared_app.c b/applications/main/infrared/infrared_app.c index 94e113b6f..70a691f55 100644 --- a/applications/main/infrared/infrared_app.c +++ b/applications/main/infrared/infrared_app.c @@ -150,7 +150,6 @@ static InfraredApp* infrared_alloc(void) { infrared->gui = furi_record_open(RECORD_GUI); ViewDispatcher* view_dispatcher = infrared->view_dispatcher; - view_dispatcher_enable_queue(view_dispatcher); view_dispatcher_set_event_callback_context(view_dispatcher, infrared); view_dispatcher_set_custom_event_callback(view_dispatcher, infrared_custom_event_callback); view_dispatcher_set_navigation_event_callback(view_dispatcher, infrared_back_event_callback); diff --git a/applications/main/lfrfid/lfrfid.c b/applications/main/lfrfid/lfrfid.c index 2b1ca02c1..b93e07222 100644 --- a/applications/main/lfrfid/lfrfid.c +++ b/applications/main/lfrfid/lfrfid.c @@ -78,7 +78,6 @@ static LfRfid* lfrfid_alloc(void) { lfrfid->view_dispatcher = view_dispatcher_alloc(); lfrfid->scene_manager = scene_manager_alloc(&lfrfid_scene_handlers, lfrfid); - view_dispatcher_enable_queue(lfrfid->view_dispatcher); view_dispatcher_set_event_callback_context(lfrfid->view_dispatcher, lfrfid); view_dispatcher_set_custom_event_callback( lfrfid->view_dispatcher, lfrfid_debug_custom_event_callback); diff --git a/applications/main/nfc/nfc_app.c b/applications/main/nfc/nfc_app.c index 483d63c14..e61e172f0 100644 --- a/applications/main/nfc/nfc_app.c +++ b/applications/main/nfc/nfc_app.c @@ -42,7 +42,6 @@ NfcApp* nfc_app_alloc(void) { instance->view_dispatcher = view_dispatcher_alloc(); instance->scene_manager = scene_manager_alloc(&nfc_scene_handlers, instance); - view_dispatcher_enable_queue(instance->view_dispatcher); view_dispatcher_set_event_callback_context(instance->view_dispatcher, instance); view_dispatcher_set_custom_event_callback( instance->view_dispatcher, nfc_custom_event_callback); diff --git a/applications/main/subghz/subghz.c b/applications/main/subghz/subghz.c index 089ffd0e9..1d86d596d 100644 --- a/applications/main/subghz/subghz.c +++ b/applications/main/subghz/subghz.c @@ -102,7 +102,6 @@ SubGhz* subghz_alloc(bool alloc_for_tx_only) { // View Dispatcher subghz->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(subghz->view_dispatcher); subghz->scene_manager = scene_manager_alloc(&subghz_scene_handlers, subghz); view_dispatcher_set_event_callback_context(subghz->view_dispatcher, subghz); diff --git a/applications/main/u2f/u2f_app.c b/applications/main/u2f/u2f_app.c index 5ca5c03dc..5612c0b1e 100644 --- a/applications/main/u2f/u2f_app.c +++ b/applications/main/u2f/u2f_app.c @@ -30,7 +30,6 @@ U2fApp* u2f_app_alloc(void) { app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&u2f_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_tick_event_callback( app->view_dispatcher, u2f_app_tick_event_callback, 500); diff --git a/applications/services/desktop/animations/animation_manager.c b/applications/services/desktop/animations/animation_manager.c index 3e4e9c97e..a42d938cd 100644 --- a/applications/services/desktop/animations/animation_manager.c +++ b/applications/services/desktop/animations/animation_manager.c @@ -97,7 +97,7 @@ void animation_manager_set_interact_callback( animation_manager->interact_callback = callback; } -static void animation_manager_check_blocking_callback(const void* message, void* context) { +static void animation_manager_storage_callback(const void* message, void* context) { const StorageEvent* storage_event = message; switch(storage_event->type) { @@ -116,6 +116,22 @@ static void animation_manager_check_blocking_callback(const void* message, void* } } +static void animation_manager_dolphin_callback(const void* message, void* context) { + const DolphinPubsubEvent* dolphin_event = message; + + switch(*dolphin_event) { + case DolphinPubsubEventUpdate: + furi_assert(context); + AnimationManager* animation_manager = context; + if(animation_manager->check_blocking_callback) { + animation_manager->check_blocking_callback(animation_manager->context); + } + break; + default: + break; + } +} + static void animation_manager_timer_callback(void* context) { furi_assert(context); AnimationManager* animation_manager = context; @@ -296,12 +312,12 @@ AnimationManager* animation_manager_alloc(void) { Storage* storage = furi_record_open(RECORD_STORAGE); animation_manager->pubsub_subscription_storage = furi_pubsub_subscribe( - storage_get_pubsub(storage), animation_manager_check_blocking_callback, animation_manager); + storage_get_pubsub(storage), animation_manager_storage_callback, animation_manager); furi_record_close(RECORD_STORAGE); Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN); animation_manager->pubsub_subscription_dolphin = furi_pubsub_subscribe( - dolphin_get_pubsub(dolphin), animation_manager_check_blocking_callback, animation_manager); + dolphin_get_pubsub(dolphin), animation_manager_dolphin_callback, animation_manager); furi_record_close(RECORD_DOLPHIN); animation_manager->blocking_shown_sd_ok = true; diff --git a/applications/services/desktop/desktop.c b/applications/services/desktop/desktop.c index 40bde4a40..9e1bdc4b3 100644 --- a/applications/services/desktop/desktop.c +++ b/applications/services/desktop/desktop.c @@ -265,7 +265,6 @@ static Desktop* desktop_alloc(void) { desktop->view_dispatcher = view_dispatcher_alloc(); desktop->scene_manager = scene_manager_alloc(&desktop_scene_handlers, desktop); - view_dispatcher_enable_queue(desktop->view_dispatcher); view_dispatcher_attach_to_gui( desktop->view_dispatcher, desktop->gui, ViewDispatcherTypeDesktop); view_dispatcher_set_tick_event_callback( diff --git a/applications/services/dialogs/dialogs_module_file_browser.c b/applications/services/dialogs/dialogs_module_file_browser.c index b1558f1e9..12a7439e6 100644 --- a/applications/services/dialogs/dialogs_module_file_browser.c +++ b/applications/services/dialogs/dialogs_module_file_browser.c @@ -49,12 +49,11 @@ bool dialogs_app_process_module_file_browser(const DialogsAppMessageDataFileBrow file_browser_start(file_browser, data->preselected_filename); view_holder_set_view(view_holder, file_browser_get_view(file_browser)); - view_holder_start(view_holder); api_lock_wait_unlock(file_browser_context->lock); ret = file_browser_context->result; - view_holder_stop(view_holder); + view_holder_set_view(view_holder, NULL); view_holder_free(view_holder); file_browser_stop(file_browser); file_browser_free(file_browser); diff --git a/applications/services/dialogs/dialogs_module_message.c b/applications/services/dialogs/dialogs_module_message.c index a71f403c5..9dc9ff9cb 100644 --- a/applications/services/dialogs/dialogs_module_message.c +++ b/applications/services/dialogs/dialogs_module_message.c @@ -88,12 +88,11 @@ DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDa dialog_ex_set_right_button_text(dialog_ex, message->right_button_text); view_holder_set_view(view_holder, dialog_ex_get_view(dialog_ex)); - view_holder_start(view_holder); api_lock_wait_unlock(message_context->lock); ret = message_context->result; - view_holder_stop(view_holder); + view_holder_set_view(view_holder, NULL); view_holder_free(view_holder); dialog_ex_free(dialog_ex); api_lock_free(message_context->lock); diff --git a/applications/services/dolphin/dolphin.c b/applications/services/dolphin/dolphin.c index 4057c5323..fde0509f9 100644 --- a/applications/services/dolphin/dolphin.c +++ b/applications/services/dolphin/dolphin.c @@ -211,8 +211,8 @@ static void dolphin_reset_butthurt_timer(Dolphin* dolphin) { } } -static bool dolphin_process_event(FuriMessageQueue* queue, void* context) { - UNUSED(queue); +static bool dolphin_process_event(FuriEventLoopObject* object, void* context) { + UNUSED(object); Dolphin* dolphin = context; DolphinEvent event; @@ -300,7 +300,7 @@ int32_t dolphin_srv(void* p) { dolphin_init_state(dolphin); - furi_event_loop_message_queue_subscribe( + furi_event_loop_subscribe_message_queue( dolphin->event_loop, dolphin->event_queue, FuriEventLoopEventIn, diff --git a/applications/services/gui/application.fam b/applications/services/gui/application.fam index b7dd18baa..b24f5bbb6 100644 --- a/applications/services/gui/application.fam +++ b/applications/services/gui/application.fam @@ -19,6 +19,7 @@ App( "view_holder.h", "modules/button_menu.h", "modules/byte_input.h", + "modules/number_input.h", "modules/popup.h", "modules/text_input.h", "modules/widget.h", diff --git a/applications/services/gui/modules/file_browser_worker.c b/applications/services/gui/modules/file_browser_worker.c index 01c05c619..87e4c6041 100644 --- a/applications/services/gui/modules/file_browser_worker.c +++ b/applications/services/gui/modules/file_browser_worker.c @@ -137,7 +137,7 @@ static bool browser_filter_by_name(BrowserWorker* browser, FuriString* name, boo if((furi_string_empty(ext)) || (furi_string_cmp_str(ext, "*") == 0)) { return true; } - if(furi_string_end_with(name, ext)) { + if(furi_string_end_withi(name, ext)) { return true; } } diff --git a/applications/services/gui/modules/number_input.c b/applications/services/gui/modules/number_input.c new file mode 100644 index 000000000..777e55747 --- /dev/null +++ b/applications/services/gui/modules/number_input.c @@ -0,0 +1,443 @@ +#include "number_input.h" + +#include +#include +#include + +struct NumberInput { + View* view; +}; + +typedef struct { + const char text; + const size_t x; + const size_t y; +} NumberInputKey; + +typedef struct { + FuriString* header; + FuriString* text_buffer; + + int32_t current_number; + int32_t max_value; + int32_t min_value; + + NumberInputCallback callback; + void* callback_context; + + size_t selected_row; + size_t selected_column; +} NumberInputModel; + +static const size_t keyboard_origin_x = 7; +static const size_t keyboard_origin_y = 31; +static const size_t keyboard_row_count = 2; +static const char enter_symbol = '\r'; +static const char backspace_symbol = '\b'; +static const char sign_symbol = '-'; + +static const NumberInputKey keyboard_keys_row_1[] = { + {'0', 0, 12}, + {'1', 11, 12}, + {'2', 22, 12}, + {'3', 33, 12}, + {'4', 44, 12}, + {backspace_symbol, 103, 4}, +}; + +static const NumberInputKey keyboard_keys_row_2[] = { + {'5', 0, 26}, + {'6', 11, 26}, + {'7', 22, 26}, + {'8', 33, 26}, + {'9', 44, 26}, + {sign_symbol, 55, 17}, + {enter_symbol, 95, 17}, +}; + +static size_t number_input_get_row_size(size_t row_index) { + size_t row_size = 0; + + switch(row_index + 1) { + case 1: + row_size = COUNT_OF(keyboard_keys_row_1); + break; + case 2: + row_size = COUNT_OF(keyboard_keys_row_2); + break; + default: + furi_crash(); + } + + return row_size; +} + +static const NumberInputKey* number_input_get_row(size_t row_index) { + const NumberInputKey* row = NULL; + + switch(row_index + 1) { + case 1: + row = keyboard_keys_row_1; + break; + case 2: + row = keyboard_keys_row_2; + break; + default: + furi_crash(); + } + + return row; +} + +static void number_input_draw_input(Canvas* canvas, NumberInputModel* model) { + const size_t text_x = 8; + const size_t text_y = 25; + + elements_slightly_rounded_frame(canvas, 6, 14, 116, 15); + + canvas_draw_str(canvas, text_x, text_y, furi_string_get_cstr(model->text_buffer)); +} + +static bool number_input_use_sign(NumberInputModel* model) { + //only show sign button if allowed number range needs it + if(model->min_value < 0 && model->max_value >= 0) { + return true; + } + return false; +} + +static void number_input_backspace_cb(NumberInputModel* model) { + size_t text_length = furi_string_utf8_length(model->text_buffer); + if(text_length < 1 || (text_length < 2 && model->current_number <= 0)) { + return; + } + furi_string_set_strn( + model->text_buffer, furi_string_get_cstr(model->text_buffer), text_length - 1); + model->current_number = strtol(furi_string_get_cstr(model->text_buffer), NULL, 10); +} + +static void number_input_handle_up(NumberInputModel* model) { + if(model->selected_row > 0) { + model->selected_row--; + if(model->selected_column > number_input_get_row_size(model->selected_row) - 1) { + model->selected_column = number_input_get_row_size(model->selected_row) - 1; + } + } +} + +static void number_input_handle_down(NumberInputModel* model) { + if(model->selected_row < keyboard_row_count - 1) { + if(model->selected_column >= number_input_get_row_size(model->selected_row) - 1) { + model->selected_column = number_input_get_row_size(model->selected_row + 1) - 1; + } + model->selected_row += 1; + } + const NumberInputKey* keys = number_input_get_row(model->selected_row); + if(keys[model->selected_column].text == sign_symbol && !number_input_use_sign(model)) { + model->selected_column--; + } +} + +static void number_input_handle_left(NumberInputModel* model) { + if(model->selected_column > 0) { + model->selected_column--; + } else { + model->selected_column = number_input_get_row_size(model->selected_row) - 1; + } + const NumberInputKey* keys = number_input_get_row(model->selected_row); + if(keys[model->selected_column].text == sign_symbol && !number_input_use_sign(model)) { + model->selected_column--; + } +} + +static void number_input_handle_right(NumberInputModel* model) { + if(model->selected_column < number_input_get_row_size(model->selected_row) - 1) { + model->selected_column++; + } else { + model->selected_column = 0; + } + const NumberInputKey* keys = number_input_get_row(model->selected_row); + if(keys[model->selected_column].text == sign_symbol && !number_input_use_sign(model)) { + model->selected_column++; + } +} + +static bool is_number_too_large(NumberInputModel* model) { + int64_t value = strtoll(furi_string_get_cstr(model->text_buffer), NULL, 10); + if(value > (int64_t)model->max_value) { + return true; + } + return false; +} + +static bool is_number_too_small(NumberInputModel* model) { + int64_t value = strtoll(furi_string_get_cstr(model->text_buffer), NULL, 10); + if(value < (int64_t)model->min_value) { + return true; + } + return false; +} + +static void number_input_sign(NumberInputModel* model) { + int32_t number = strtol(furi_string_get_cstr(model->text_buffer), NULL, 10); + if(number == 0 && furi_string_cmp_str(model->text_buffer, "-") != 0) { + furi_string_set_str(model->text_buffer, "-"); + return; + } + number = number * -1; + furi_string_printf(model->text_buffer, "%ld", number); + if(is_number_too_large(model) || is_number_too_small(model)) { + furi_string_printf(model->text_buffer, "%ld", model->current_number); + return; + } + model->current_number = strtol(furi_string_get_cstr(model->text_buffer), NULL, 10); + if(model->current_number == 0) { + furi_string_set_str(model->text_buffer, ""); //show empty if 0, better for usability + } +} + +static void number_input_add_digit(NumberInputModel* model, char* newChar) { + furi_string_cat_str(model->text_buffer, newChar); + if((model->max_value >= 0 && is_number_too_large(model)) || + (model->min_value < 0 && is_number_too_small(model))) { + //you still need to be able to type invalid numbers in some cases to reach valid numbers on later keypress + furi_string_printf(model->text_buffer, "%ld", model->current_number); + return; + } + model->current_number = strtol(furi_string_get_cstr(model->text_buffer), NULL, 10); + if(model->current_number == 0) { + furi_string_reset(model->text_buffer); + } +} + +static void number_input_handle_ok(NumberInputModel* model) { + char selected = number_input_get_row(model->selected_row)[model->selected_column].text; + char temp_str[2] = {selected, '\0'}; + if(selected == enter_symbol) { + if(is_number_too_large(model) || is_number_too_small(model)) { + return; //Do nothing if number outside allowed range + } + model->current_number = strtol(furi_string_get_cstr(model->text_buffer), NULL, 10); + model->callback(model->callback_context, model->current_number); + } else if(selected == backspace_symbol) { + number_input_backspace_cb(model); + } else if(selected == sign_symbol) { + number_input_sign(model); + } else { + number_input_add_digit(model, temp_str); + } +} + +static void number_input_view_draw_callback(Canvas* canvas, void* _model) { + NumberInputModel* model = _model; + + number_input_draw_input(canvas, model); + + if(!furi_string_empty(model->header)) { + canvas_set_font(canvas, FontSecondary); + canvas_draw_str(canvas, 2, 9, furi_string_get_cstr(model->header)); + } + canvas_set_font(canvas, FontKeyboard); + // Draw keyboard + for(size_t row = 0; row < keyboard_row_count; row++) { + const size_t column_count = number_input_get_row_size(row); + const NumberInputKey* keys = number_input_get_row(row); + + for(size_t column = 0; column < column_count; column++) { + if(keys[column].text == sign_symbol && !number_input_use_sign(model)) { + continue; + } + + if(keys[column].text == enter_symbol) { + if(is_number_too_small(model) || is_number_too_large(model)) { + //in some cases you need to be able to type a number smaller/larger than the limits (expl. min = 50, clear all and editor must allow to type 9 and later 0 for 90) + if(model->selected_row == row && model->selected_column == column) { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySaveBlockedSelected_24x11); + } else { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySaveBlocked_24x11); + } + } else { + if(model->selected_row == row && model->selected_column == column) { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySaveSelected_24x11); + } else { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySave_24x11); + } + } + } else if(keys[column].text == backspace_symbol) { + if(model->selected_row == row && model->selected_column == column) { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeyBackspaceSelected_16x9); + } else { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeyBackspace_16x9); + } + } else if(keys[column].text == sign_symbol) { + if(model->selected_row == row && model->selected_column == column) { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySignSelected_21x11); + } else { + canvas_draw_icon( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + &I_KeySign_21x11); + } + } else { + if(model->selected_row == row && model->selected_column == column) { + canvas_draw_box( + canvas, + keyboard_origin_x + keys[column].x - 3, + keyboard_origin_y + keys[column].y - 10, + 11, + 13); + canvas_set_color(canvas, ColorWhite); + } + + canvas_draw_glyph( + canvas, + keyboard_origin_x + keys[column].x, + keyboard_origin_y + keys[column].y, + keys[column].text); + canvas_set_color(canvas, ColorBlack); + } + } + } +} + +static bool number_input_view_input_callback(InputEvent* event, void* context) { + furi_assert(context); + NumberInput* number_input = context; + + bool consumed = false; + + // Fetch the model + NumberInputModel* model = view_get_model(number_input->view); + + if(event->type == InputTypeShort || event->type == InputTypeLong || + event->type == InputTypeRepeat) { + consumed = true; + switch(event->key) { + case InputKeyLeft: + number_input_handle_left(model); + break; + case InputKeyRight: + number_input_handle_right(model); + break; + case InputKeyUp: + number_input_handle_up(model); + break; + case InputKeyDown: + number_input_handle_down(model); + break; + case InputKeyOk: + number_input_handle_ok(model); + break; + default: + consumed = false; + break; + } + } + + // commit view + view_commit_model(number_input->view, consumed); + + return consumed; +} + +NumberInput* number_input_alloc(void) { + NumberInput* number_input = malloc(sizeof(NumberInput)); + number_input->view = view_alloc(); + view_set_context(number_input->view, number_input); + view_allocate_model(number_input->view, ViewModelTypeLocking, sizeof(NumberInputModel)); + view_set_draw_callback(number_input->view, number_input_view_draw_callback); + view_set_input_callback(number_input->view, number_input_view_input_callback); + + with_view_model( + number_input->view, + NumberInputModel * model, + { + model->header = furi_string_alloc(); + model->text_buffer = furi_string_alloc(); + }, + true); + + return number_input; +} + +void number_input_free(NumberInput* number_input) { + furi_check(number_input); + with_view_model( + number_input->view, + NumberInputModel * model, + { + furi_string_free(model->header); + furi_string_free(model->text_buffer); + }, + true); + view_free(number_input->view); + free(number_input); +} + +View* number_input_get_view(NumberInput* number_input) { + furi_check(number_input); + return number_input->view; +} + +void number_input_set_result_callback( + NumberInput* number_input, + NumberInputCallback callback, + void* callback_context, + int32_t current_number, + int32_t min_value, + int32_t max_value) { + furi_check(number_input); + + current_number = CLAMP(current_number, max_value, min_value); + + with_view_model( + number_input->view, + NumberInputModel * model, + { + model->callback = callback; + model->callback_context = callback_context; + model->current_number = current_number; + furi_string_printf(model->text_buffer, "%ld", current_number); + model->min_value = min_value; + model->max_value = max_value; + }, + true); +} + +void number_input_set_header_text(NumberInput* number_input, const char* text) { + furi_check(number_input); + with_view_model( + number_input->view, + NumberInputModel * model, + { furi_string_set(model->header, text); }, + true); +} diff --git a/applications/services/gui/modules/number_input.h b/applications/services/gui/modules/number_input.h new file mode 100644 index 000000000..80e631e9b --- /dev/null +++ b/applications/services/gui/modules/number_input.h @@ -0,0 +1,69 @@ +/** + * @file number_input.h + * GUI: Integer string keyboard view module API + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** Number input anonymous structure */ +typedef struct NumberInput NumberInput; + +/** Callback to be called on save button press */ +typedef void (*NumberInputCallback)(void* context, int32_t number); + +/** Allocate and initialize Number input. + * + * This Number input is used to enter Numbers (Integers). + * + * @return NumberInput instance pointer + */ +NumberInput* number_input_alloc(void); + +/** Deinitialize and free byte input + * + * @param number_input Number input instance + */ +void number_input_free(NumberInput* number_input); + +/** Get byte input view + * + * @param number_input byte input instance + * + * @return View instance that can be used for embedding + */ +View* number_input_get_view(NumberInput* number_input); + +/** Set byte input result callback + * + * @param number_input byte input instance + * @param input_callback input callback fn + * @param callback_context callback context + * @param[in] current_number The current number + * @param min_value Min number value + * @param max_value Max number value + */ + +void number_input_set_result_callback( + NumberInput* number_input, + NumberInputCallback input_callback, + void* callback_context, + int32_t current_number, + int32_t min_value, + int32_t max_value); + +/** Set byte input header text + * + * @param number_input byte input instance + * @param text text to be shown + */ +void number_input_set_header_text(NumberInput* number_input, const char* text); + +#ifdef __cplusplus +} +#endif diff --git a/applications/services/gui/view_dispatcher.c b/applications/services/gui/view_dispatcher.c index 9e91c2c9c..4bef29350 100644 --- a/applications/services/gui/view_dispatcher.c +++ b/applications/services/gui/view_dispatcher.c @@ -2,6 +2,8 @@ #define TAG "ViewDispatcher" +#define VIEW_DISPATCHER_QUEUE_LEN (16U) + ViewDispatcher* view_dispatcher_alloc(void) { ViewDispatcher* view_dispatcher = malloc(sizeof(ViewDispatcher)); @@ -16,6 +18,35 @@ ViewDispatcher* view_dispatcher_alloc(void) { ViewDict_init(view_dispatcher->views); + view_dispatcher->event_loop = furi_event_loop_alloc(); + + view_dispatcher->input_queue = + furi_message_queue_alloc(VIEW_DISPATCHER_QUEUE_LEN, sizeof(InputEvent)); + furi_event_loop_subscribe_message_queue( + view_dispatcher->event_loop, + view_dispatcher->input_queue, + FuriEventLoopEventIn, + view_dispatcher_run_input_callback, + view_dispatcher); + + view_dispatcher->ascii_queue = + furi_message_queue_alloc(VIEW_DISPATCHER_QUEUE_LEN, sizeof(AsciiEvent)); + furi_event_loop_subscribe_message_queue( + view_dispatcher->event_loop, + view_dispatcher->ascii_queue, + FuriEventLoopEventIn, + view_dispatcher_run_ascii_callback, + view_dispatcher); + + view_dispatcher->event_queue = + furi_message_queue_alloc(VIEW_DISPATCHER_QUEUE_LEN, sizeof(uint32_t)); + furi_event_loop_subscribe_message_queue( + view_dispatcher->event_loop, + view_dispatcher->event_queue, + FuriEventLoopEventIn, + view_dispatcher_run_event_callback, + view_dispatcher); + return view_dispatcher; } @@ -31,57 +62,21 @@ void view_dispatcher_free(ViewDispatcher* view_dispatcher) { // Free ViewPort view_port_free(view_dispatcher->view_port); // Free internal 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); - } + furi_event_loop_unsubscribe(view_dispatcher->event_loop, view_dispatcher->input_queue); + furi_event_loop_unsubscribe(view_dispatcher->event_loop, view_dispatcher->ascii_queue); + furi_event_loop_unsubscribe(view_dispatcher->event_loop, view_dispatcher->event_queue); + + furi_message_queue_free(view_dispatcher->input_queue); + furi_message_queue_free(view_dispatcher->ascii_queue); + furi_message_queue_free(view_dispatcher->event_queue); + + furi_event_loop_free(view_dispatcher->event_loop); // Free dispatcher free(view_dispatcher); } void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher) { - furi_check(view_dispatcher); - furi_check(view_dispatcher->event_loop == NULL); - - view_dispatcher->event_loop = furi_event_loop_alloc(); - - view_dispatcher->input_queue = furi_message_queue_alloc(16, 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(16, 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(16, 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); + UNUSED(view_dispatcher); } void view_dispatcher_set_navigation_event_callback( @@ -114,14 +109,12 @@ void view_dispatcher_set_event_callback_context(ViewDispatcher* view_dispatcher, 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->event_loop); uint32_t tick_period = view_dispatcher->tick_period == 0 ? FuriWaitForever : view_dispatcher->tick_period; @@ -149,7 +142,6 @@ void view_dispatcher_run(ViewDispatcher* view_dispatcher) { void view_dispatcher_stop(ViewDispatcher* view_dispatcher) { furi_check(view_dispatcher); - furi_check(view_dispatcher->event_loop); furi_event_loop_stop(view_dispatcher->event_loop); } @@ -257,13 +249,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->input_queue) { - furi_check( - furi_message_queue_put(view_dispatcher->input_queue, event, FuriWaitForever) == - FuriStatusOk); - } else { - view_dispatcher_handle_input(view_dispatcher, event); - } + furi_check( + furi_message_queue_put(view_dispatcher->input_queue, event, FuriWaitForever) == + FuriStatusOk); } bool view_dispatcher_ascii_callback(AsciiEvent* event, void* context) { @@ -271,13 +259,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->ascii_queue) { - furi_check( - furi_message_queue_put(view_dispatcher->ascii_queue, event, FuriWaitForever) == - FuriStatusOk); - } else { - view_dispatcher_handle_ascii(view_dispatcher, event); - } + furi_check( + furi_message_queue_put(view_dispatcher->ascii_queue, event, FuriWaitForever) == + FuriStatusOk); return true; } @@ -400,7 +384,6 @@ 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->event_loop); furi_check( furi_message_queue_put(view_dispatcher->event_queue, &event, FuriWaitForever) == @@ -436,9 +419,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->event_loop) { - view_dispatcher_stop(view_dispatcher); - } + view_dispatcher_stop(view_dispatcher); } } @@ -453,10 +434,10 @@ void view_dispatcher_update(View* view, void* context) { } } -bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context) { +bool view_dispatcher_run_event_callback(FuriEventLoopObject* object, void* context) { furi_assert(context); ViewDispatcher* instance = context; - furi_assert(instance->event_queue == queue); + furi_assert(instance->event_queue == object); uint32_t event; furi_check(furi_message_queue_get(instance->event_queue, &event, 0) == FuriStatusOk); @@ -465,10 +446,10 @@ bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context) return true; } -bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context) { +bool view_dispatcher_run_input_callback(FuriEventLoopObject* object, void* context) { furi_assert(context); ViewDispatcher* instance = context; - furi_assert(instance->input_queue == queue); + furi_assert(instance->input_queue == object); InputEvent input; furi_check(furi_message_queue_get(instance->input_queue, &input, 0) == FuriStatusOk); @@ -477,10 +458,10 @@ bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context) return true; } -bool view_dispatcher_run_ascii_callback(FuriMessageQueue* queue, void* context) { +bool view_dispatcher_run_ascii_callback(FuriEventLoopObject* object, void* context) { furi_assert(context); ViewDispatcher* instance = context; - furi_assert(instance->ascii_queue == queue); + furi_assert(instance->ascii_queue == object); AsciiEvent ascii; furi_check(furi_message_queue_get(instance->ascii_queue, &ascii, 0) == FuriStatusOk); diff --git a/applications/services/gui/view_dispatcher.h b/applications/services/gui/view_dispatcher.h index 905c60975..9fbf89791 100644 --- a/applications/services/gui/view_dispatcher.h +++ b/applications/services/gui/view_dispatcher.h @@ -2,6 +2,14 @@ * @file view_dispatcher.h * @brief GUI: ViewDispatcher API * + * ViewDispatcher is used to connect several Views to a Gui instance, switch between them and handle various events. + * This is useful in applications featuring an advanced graphical user interface. + * + * Internally, ViewDispatcher employs a FuriEventLoop instance together with two separate + * message queues for input and custom event handling. See FuriEventLoop for more information. + * + * If no multi-view or complex event handling capabilities are required, consider using ViewHolder instead. + * * @warning Views added to a ViewDispatcher MUST NOT be in a ViewStack at the same time. */ @@ -40,6 +48,9 @@ typedef void (*ViewDispatcherTickEventCallback)(void* context); ViewDispatcher* view_dispatcher_alloc(void); /** Free ViewDispatcher instance + * + * @warning All added views MUST be removed using view_dispatcher_remove_view() + * before calling this function. * * @param view_dispatcher pointer to ViewDispatcher */ @@ -47,12 +58,13 @@ void view_dispatcher_free(ViewDispatcher* view_dispatcher); /** Enable queue support * - * Allocates event_loop, input and event message queues. Must be used with - * `view_dispatcher_run` + * @deprecated Do NOT use in new code and remove all calls to it from existing code. + * The queue support is now always enabled during construction. If no queue support + * is required, consider using ViewHolder instead. * * @param view_dispatcher ViewDispatcher instance */ -void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher); +FURI_DEPRECATED void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher); /** Send custom event * @@ -103,11 +115,11 @@ void view_dispatcher_set_event_callback_context(ViewDispatcher* view_dispatcher, /** Get event_loop instance * - * event_loop instance is allocated on `view_dispatcher_enable_queue` and used - * in view_dispatcher_run. + * Use the return value to connect additional supported primitives (message queues, timers, etc) + * to this ViewDispatcher instance's event loop. * - * You can add your objects into event_loop instance, but don't run the loop on - * your side as it will cause issues with input processing on dispatcher stop. + * @warning Do NOT call furi_event_loop_run() on the returned instance, it is done internally + * in the view_dispatcher_run() call. * * @param view_dispatcher ViewDispatcher instance * @@ -117,15 +129,14 @@ FuriEventLoop* view_dispatcher_get_event_loop(ViewDispatcher* view_dispatcher); /** Run ViewDispatcher * - * Use only after queue enabled + * This function will start the event loop and block until view_dispatcher_stop() is called + * or the current thread receives a FuriSignalExit signal. * * @param view_dispatcher ViewDispatcher instance */ void view_dispatcher_run(ViewDispatcher* view_dispatcher); /** Stop ViewDispatcher - * - * Use only after queue enabled * * @param view_dispatcher ViewDispatcher instance */ diff --git a/applications/services/gui/view_dispatcher_i.h b/applications/services/gui/view_dispatcher_i.h index ae2f819fe..6b2db9b54 100644 --- a/applications/services/gui/view_dispatcher_i.h +++ b/applications/services/gui/view_dispatcher_i.h @@ -64,10 +64,10 @@ void view_dispatcher_set_current_view(ViewDispatcher* view_dispatcher, View* vie void view_dispatcher_update(View* view, void* context); /** ViewDispatcher run event loop event callback */ -bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context); +bool view_dispatcher_run_event_callback(FuriEventLoopObject* object, void* context); /** ViewDispatcher run event loop input callback */ -bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context); +bool view_dispatcher_run_input_callback(FuriEventLoopObject* object, void* context); /** ViewDispatcher run event loop ascii callback */ -bool view_dispatcher_run_ascii_callback(FuriMessageQueue* queue, void* context); +bool view_dispatcher_run_ascii_callback(FuriEventLoopObject* object, void* context); diff --git a/applications/services/gui/view_holder.c b/applications/services/gui/view_holder.c index d00458507..6de5528a3 100644 --- a/applications/services/gui/view_holder.c +++ b/applications/services/gui/view_holder.c @@ -34,7 +34,8 @@ ViewHolder* view_holder_alloc(void) { } void view_holder_free(ViewHolder* view_holder) { - furi_assert(view_holder); + furi_check(view_holder); + furi_check(view_holder->view == NULL); if(view_holder->gui) { gui_remove_view_port(view_holder->gui, view_holder->view_port); @@ -50,12 +51,14 @@ void view_holder_free(ViewHolder* view_holder) { } void view_holder_set_view(ViewHolder* view_holder, View* view) { - furi_assert(view_holder); + furi_check(view_holder); + if(view_holder->view) { - if(view_holder->view->exit_callback) { - view_holder->view->exit_callback(view_holder->view->context); + while(view_holder->ongoing_input) { + furi_delay_tick(1); } + view_exit(view_holder->view); view_set_update_callback(view_holder->view, NULL); view_set_update_callback_context(view_holder->view, NULL); } @@ -63,12 +66,23 @@ void view_holder_set_view(ViewHolder* view_holder, View* view) { view_holder->view = view; if(view_holder->view) { + const ViewPortOrientation orientation = (ViewPortOrientation)view->orientation; + furi_assert(orientation < ViewPortOrientationMAX); + if(view_port_get_orientation(view_holder->view_port) != orientation) { + view_port_set_orientation(view_holder->view_port, orientation); + // we just rotated input keys, now it's time to sacrifice some input + view_holder->ongoing_input = 0; + } + view_set_update_callback(view_holder->view, view_holder_update); view_set_update_callback_context(view_holder->view, view_holder); - if(view_holder->view->enter_callback) { - view_holder->view->enter_callback(view_holder->view->context); - } + view_enter(view_holder->view); + view_port_enabled_set(view_holder->view_port, true); + view_port_update(view_holder->view_port); + + } else { + view_port_enabled_set(view_holder->view_port, false); } } @@ -76,7 +90,7 @@ void view_holder_set_free_callback( ViewHolder* view_holder, FreeCallback free_callback, void* free_context) { - furi_assert(view_holder); + furi_check(view_holder); view_holder->free_callback = free_callback; view_holder->free_context = free_context; } @@ -89,31 +103,22 @@ void view_holder_set_back_callback( ViewHolder* view_holder, BackCallback back_callback, void* back_context) { - furi_assert(view_holder); + furi_check(view_holder); view_holder->back_callback = back_callback; view_holder->back_context = back_context; } void view_holder_attach_to_gui(ViewHolder* view_holder, Gui* gui) { - furi_assert(gui); - furi_assert(view_holder); - view_holder->gui = gui; + furi_check(view_holder); + furi_check(view_holder->gui == NULL); + furi_check(gui); gui_add_view_port(gui, view_holder->view_port, GuiLayerFullscreen); -} - -void view_holder_start(ViewHolder* view_holder) { - view_port_enabled_set(view_holder->view_port, true); -} - -void view_holder_stop(ViewHolder* view_holder) { - while(view_holder->ongoing_input) - furi_delay_tick(1); - view_port_enabled_set(view_holder->view_port, false); + view_holder->gui = gui; } void view_holder_update(View* view, void* context) { - furi_assert(view); - furi_assert(context); + furi_check(view); + furi_check(context); ViewHolder* view_holder = context; if(view == view_holder->view) { @@ -121,6 +126,18 @@ void view_holder_update(View* view, void* context) { } } +void view_holder_send_to_front(ViewHolder* view_holder) { + furi_check(view_holder); + furi_check(view_holder->gui); + gui_view_port_send_to_front(view_holder->gui, view_holder->view_port); +} + +void view_holder_send_to_back(ViewHolder* view_holder) { + furi_check(view_holder); + furi_check(view_holder->gui); + gui_view_port_send_to_back(view_holder->gui, view_holder->view_port); +} + static void view_holder_draw_callback(Canvas* canvas, void* context) { ViewHolder* view_holder = context; if(view_holder->view) { diff --git a/applications/services/gui/view_holder.h b/applications/services/gui/view_holder.h index 90ce82b37..78dbfda0e 100644 --- a/applications/services/gui/view_holder.h +++ b/applications/services/gui/view_holder.h @@ -2,7 +2,10 @@ * @file view_holder.h * @brief GUI: ViewHolder API * - * @warning View added to a ViewHolder MUST NOT be in a ViewStack at the same time. + * ViewHolder is used to connect a single View to a Gui instance. This is useful in smaller applications + * with a simple user interface. If advanced view switching capabilites are required, consider using ViewDispatcher instead. + * + * @warning Views added to a ViewHolder MUST NOT be in a ViewStack at the same time. */ #pragma once @@ -22,7 +25,8 @@ typedef void (*FreeCallback)(void* free_context); /** * @brief Back callback type - * @warning comes from GUI thread + * + * @warning Will be called from the GUI thread */ typedef void (*BackCallback)(void* back_context); @@ -34,12 +38,17 @@ ViewHolder* view_holder_alloc(void); /** * @brief Free ViewHolder and call Free callback + * + * @warning The current view must be unset prior to freeing a ViewHolder instance. + * * @param view_holder pointer to ViewHolder */ void view_holder_free(ViewHolder* view_holder); /** * @brief Set view for ViewHolder + * + * Pass NULL as the view parameter to unset the current view. * * @param view_holder ViewHolder instance * @param view View instance @@ -59,13 +68,25 @@ void view_holder_set_free_callback( void* free_context); /** - * @brief Free callback context getter. Useful if your Free callback is a module destructor, so you can get an instance of the module using this method. + * @brief Free callback context getter. + * + * Useful if your Free callback is a module destructor, so you can get an instance of the module using this method. * * @param view_holder ViewHolder instance * @return void* free callback context */ void* view_holder_get_free_context(ViewHolder* view_holder); +/** + * @brief Set the back key callback. + * + * The callback function will be called if the user has pressed the Back key + * and the current view did not handle this event. + * + * @param view_holder ViewHolder instance + * @param back_callback pointer to the callback function + * @param back_context pointer to a user-specific object, can be NULL + */ void view_holder_set_back_callback( ViewHolder* view_holder, BackCallback back_callback, @@ -80,26 +101,27 @@ void view_holder_set_back_callback( void view_holder_attach_to_gui(ViewHolder* view_holder, Gui* gui); /** - * @brief Enable view processing - * - * @param view_holder - */ -void view_holder_start(ViewHolder* view_holder); - -/** - * @brief Disable view processing - * - * @param view_holder - */ -void view_holder_stop(ViewHolder* view_holder); - -/** View Update Handler + * @brief View Update Handler * - * @param view View Instance - * @param context ViewHolder instance + * @param view View Instance + * @param context ViewHolder instance */ void view_holder_update(View* view, void* context); +/** + * @brief Send ViewPort of this ViewHolder instance to front + * + * @param view_holder ViewHolder instance + */ +void view_holder_send_to_front(ViewHolder* view_holder); + +/** + * @brief Send ViewPort of this ViewHolder instance to back + * + * @param view_holder ViewHolder instance + */ +void view_holder_send_to_back(ViewHolder* view_holder); + #ifdef __cplusplus } #endif diff --git a/applications/services/loader/loader_applications.c b/applications/services/loader/loader_applications.c index 9b4b84ee9..aebb03d63 100644 --- a/applications/services/loader/loader_applications.c +++ b/applications/services/loader/loader_applications.c @@ -61,7 +61,6 @@ static LoaderApplicationsApp* loader_applications_app_alloc(void) { app->loading = loading_alloc(); view_holder_attach_to_gui(app->view_holder, app->gui); - view_holder_set_view(app->view_holder, loading_get_view(app->loading)); return app; } //-V773 @@ -152,7 +151,7 @@ static int32_t loader_applications_thread(void* p) { LoaderApplicationsApp* app = loader_applications_app_alloc(); // start loading animation - view_holder_start(app->view_holder); + view_holder_set_view(app->view_holder, loading_get_view(app->loading)); while(loader_applications_select_app(app)) { if(!furi_string_end_with(app->file_path, ".js")) { @@ -164,7 +163,7 @@ static int32_t loader_applications_thread(void* p) { } // stop loading animation - view_holder_stop(app->view_holder); + view_holder_set_view(app->view_holder, NULL); loader_applications_app_free(app); diff --git a/applications/services/loader/loader_menu.c b/applications/services/loader/loader_menu.c index 94f138fbc..2753a58c3 100644 --- a/applications/services/loader/loader_menu.c +++ b/applications/services/loader/loader_menu.c @@ -102,7 +102,6 @@ LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context, bool sett view_holder_attach_to_gui(loader_menu->view_holder, gui); view_holder_set_back_callback(loader_menu->view_holder, NULL, NULL); loader_menu_set_view(loader_menu, loader_menu->dummy); - view_holder_start(loader_menu->view_holder); loader_menu->loader = furi_record_open(RECORD_LOADER); loader_menu->subscription = furi_pubsub_subscribe( diff --git a/applications/services/power/power_cli.c b/applications/services/power/power_cli.c index 89cd723ee..0b2a9851b 100644 --- a/applications/services/power/power_cli.c +++ b/applications/services/power/power_cli.c @@ -17,13 +17,15 @@ void power_cli_off(Cli* cli, FuriString* args) { void power_cli_reboot(Cli* cli, FuriString* args) { UNUSED(cli); UNUSED(args); - power_reboot(PowerBootModeNormal); + Power* power = furi_record_open(RECORD_POWER); + power_reboot(power, PowerBootModeNormal); } void power_cli_reboot2dfu(Cli* cli, FuriString* args) { UNUSED(cli); UNUSED(args); - power_reboot(PowerBootModeDfu); + Power* power = furi_record_open(RECORD_POWER); + power_reboot(power, PowerBootModeDfu); } void power_cli_5v(Cli* cli, FuriString* args) { diff --git a/applications/services/power/power_service/power.h b/applications/services/power/power_service/power.h index ecb0566a4..5fe320315 100644 --- a/applications/services/power/power_service/power.h +++ b/applications/services/power/power_service/power.h @@ -1,9 +1,10 @@ #pragma once #include -#include #include +#include + #ifdef __cplusplus extern "C" { #endif @@ -67,7 +68,7 @@ void power_off(Power* power); * * @param mode PowerBootMode */ -void power_reboot(PowerBootMode mode); +void power_reboot(Power* power, PowerBootMode mode); /** Get power info * diff --git a/applications/services/power/power_service/power_i.h b/applications/services/power/power_service/power_i.h index 0c6b2658d..faecae05e 100644 --- a/applications/services/power/power_service/power_i.h +++ b/applications/services/power/power_service/power_i.h @@ -2,21 +2,17 @@ #include "power.h" -#include -#include #include +#include + +#include #include #include -#include #include "views/power_off.h" #include #include "views/power_unplug_usb.h" -#include - -#define POWER_BATTERY_HEALTHY_LEVEL 70 - typedef enum { PowerStateNotCharging, PowerStateCharging, @@ -24,26 +20,24 @@ typedef enum { } PowerState; struct Power { - ViewDispatcher* view_dispatcher; - PowerOff* power_off; - PowerUnplugUsb* power_unplug_usb; + ViewHolder* view_holder; + FuriPubSub* event_pubsub; + FuriEventLoop* event_loop; + FuriMessageQueue* message_queue; ViewPort* battery_view_port; - Gui* gui; - NotificationApp* notification; - FuriPubSub* event_pubsub; - PowerEvent event; + PowerOff* view_power_off; + PowerUnplugUsb* view_power_unplug_usb; + PowerEvent event; PowerState state; PowerInfo info; bool battery_low; - bool show_low_bat_level_message; + bool show_battery_low_warning; uint8_t battery_level; uint8_t power_off_timeout; - FuriMutex* api_mtx; - FuriPubSub* settings_events; FuriPubSub* input_events_pubsub; FuriPubSubSubscription* input_events_subscription; @@ -60,3 +54,21 @@ typedef enum { PowerViewOff, PowerViewUnplugUsb, } PowerView; + +typedef enum { + PowerMessageTypeShutdown, + PowerMessageTypeReboot, + PowerMessageTypeGetInfo, + PowerMessageTypeIsBatteryHealthy, + PowerMessageTypeShowBatteryLowWarning, +} PowerMessageType; + +typedef struct { + PowerMessageType type; + union { + PowerBootMode boot_mode; + PowerInfo* power_info; + bool* bool_param; + }; + FuriApiLock lock; +} PowerMessage; diff --git a/applications/services/rpc/rpc_system.c b/applications/services/rpc/rpc_system.c index 0b9fd33f9..1cc0f90eb 100644 --- a/applications/services/rpc/rpc_system.c +++ b/applications/services/rpc/rpc_system.c @@ -54,18 +54,21 @@ static void rpc_system_system_reboot_process(const PB_Main* request, void* conte RpcSession* session = (RpcSession*)context; furi_assert(session); + Power* power = furi_record_open(RECORD_POWER); const int mode = request->content.system_reboot_request.mode; if(mode == PB_System_RebootRequest_RebootMode_OS) { - power_reboot(PowerBootModeNormal); + power_reboot(power, PowerBootModeNormal); } else if(mode == PB_System_RebootRequest_RebootMode_DFU) { - power_reboot(PowerBootModeDfu); + power_reboot(power, PowerBootModeDfu); } else if(mode == PB_System_RebootRequest_RebootMode_UPDATE) { - power_reboot(PowerBootModeUpdateStart); + power_reboot(power, PowerBootModeUpdateStart); } else { rpc_send_and_release_empty( session, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS); } + + furi_record_close(RECORD_POWER); } static void rpc_system_system_device_info_callback( @@ -181,9 +184,9 @@ static void rpc_system_system_factory_reset_process(const PB_Main* request, void furi_hal_rtc_reset_registers(); furi_hal_rtc_set_flag(FuriHalRtcFlagStorageFormatInternal); - power_reboot(PowerBootModeNormal); - (void)session; + Power* power = furi_record_open(RECORD_POWER); + power_reboot(power, PowerBootModeNormal); } static void diff --git a/applications/services/storage/storage.c b/applications/services/storage/storage.c index a681932b9..6d0af00fb 100644 --- a/applications/services/storage/storage.c +++ b/applications/services/storage/storage.c @@ -126,11 +126,6 @@ int32_t storage_srv(void* p) { Storage* app = storage_app_alloc(); furi_record_create(RECORD_STORAGE, app); - if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagStorageFormatInternal)) { - FURI_LOG_W(TAG, "Format Internal not supported, clearing flag"); - furi_hal_rtc_reset_flag(FuriHalRtcFlagStorageFormatInternal); - } - StorageMessage message; while(1) { if(furi_message_queue_get(app->message_queue, &message, STORAGE_TICK) == FuriStatusOk) { diff --git a/applications/services/storage/storage_cli.c b/applications/services/storage/storage_cli.c index 3c0302d8a..73300ef84 100644 --- a/applications/services/storage/storage_cli.c +++ b/applications/services/storage/storage_cli.c @@ -701,9 +701,12 @@ static void storage_cli_factory_reset(Cli* cli, FuriString* args, void* context) char c = cli_getc(cli); if(c == 'y' || c == 'Y') { printf("Data will be wiped after reboot.\r\n"); + furi_hal_rtc_reset_registers(); furi_hal_rtc_set_flag(FuriHalRtcFlagStorageFormatInternal); - power_reboot(PowerBootModeNormal); + + Power* power = furi_record_open(RECORD_POWER); + power_reboot(power, PowerBootModeNormal); } else { printf("Safe choice.\r\n"); } diff --git a/applications/services/storage/storage_internal_dirname_i.h b/applications/services/storage/storage_internal_dirname_i.h new file mode 100644 index 000000000..889bdc497 --- /dev/null +++ b/applications/services/storage/storage_internal_dirname_i.h @@ -0,0 +1,3 @@ +#pragma once + +#define STORAGE_INTERNAL_DIR_NAME ".int" diff --git a/applications/services/storage/storage_processing.c b/applications/services/storage/storage_processing.c index f5adbd8c2..b60eeda42 100644 --- a/applications/services/storage/storage_processing.c +++ b/applications/services/storage/storage_processing.c @@ -1,7 +1,9 @@ -#include "storage_processing.h" #include #include +#include "storage_processing.h" +#include "storage_internal_dirname_i.h" + #define TAG "Storage" #define STORAGE_PATH_PREFIX_LEN 4u @@ -604,9 +606,9 @@ void storage_process_alias( } else if(furi_string_start_with(path, STORAGE_INT_PATH_PREFIX)) { furi_string_replace_at( - path, 0, strlen(STORAGE_INT_PATH_PREFIX), STORAGE_EXT_PATH_PREFIX "/.int"); + path, 0, strlen(STORAGE_INT_PATH_PREFIX), EXT_PATH(STORAGE_INTERNAL_DIR_NAME)); - FuriString* int_on_ext_path = furi_string_alloc_set(STORAGE_EXT_PATH_PREFIX "/.int"); + FuriString* int_on_ext_path = furi_string_alloc_set(EXT_PATH(STORAGE_INTERNAL_DIR_NAME)); if(storage_process_common_stat(app, int_on_ext_path, NULL) != FSE_OK) { storage_process_common_mkdir(app, int_on_ext_path); } diff --git a/applications/services/storage/storages/storage_ext.c b/applications/services/storage/storages/storage_ext.c index b6597f64e..276d057ae 100644 --- a/applications/services/storage/storages/storage_ext.c +++ b/applications/services/storage/storages/storage_ext.c @@ -1,11 +1,14 @@ -#include "fatfs.h" -#include "../filesystem_api_internal.h" -#include "storage_ext.h" +#include #include -#include "sd_notify.h" #include #include +#include "sd_notify.h" +#include "storage_ext.h" + +#include "../filesystem_api_internal.h" +#include "../storage_internal_dirname_i.h" + typedef FIL SDFile; typedef DIR SDDir; typedef FILINFO SDFileInfo; @@ -94,6 +97,64 @@ static bool sd_mount_card_internal(StorageData* storage, bool notify) { return result; } +static bool sd_remove_recursive(const char* path) { + SDDir* current_dir = malloc(sizeof(DIR)); + SDFileInfo* file_info = malloc(sizeof(FILINFO)); + FuriString* current_path = furi_string_alloc_set(path); + + bool go_deeper = false; + SDError status; + + while(true) { + status = f_opendir(current_dir, furi_string_get_cstr(current_path)); + if(status != FR_OK) break; + + while(true) { + status = f_readdir(current_dir, file_info); + if(status != FR_OK || !strlen(file_info->fname)) break; + + if(file_info->fattrib & AM_DIR) { + furi_string_cat_printf(current_path, "/%s", file_info->fname); + go_deeper = true; + break; + + } else { + FuriString* file_path = furi_string_alloc_printf( + "%s/%s", furi_string_get_cstr(current_path), file_info->fname); + status = f_unlink(furi_string_get_cstr(file_path)); + furi_string_free(file_path); + + if(status != FR_OK) break; + } + } + + status = f_closedir(current_dir); + if(status != FR_OK) break; + + if(go_deeper) { + go_deeper = false; + continue; + } + + status = f_unlink(furi_string_get_cstr(current_path)); + if(status != FR_OK) break; + + if(!furi_string_equal(current_path, path)) { + size_t last_char_pos = furi_string_search_rchar(current_path, '/'); + furi_assert(last_char_pos != FURI_STRING_FAILURE); + furi_string_left(current_path, last_char_pos); + } else { + break; + } + } + + free(current_dir); + free(file_info); + furi_string_free(current_path); + + return status == FR_OK; +} + FS_Error sd_unmount_card(StorageData* storage) { SDData* sd_data = storage->data; SDError error; @@ -113,21 +174,32 @@ FS_Error sd_mount_card(StorageData* storage, bool notify) { if(storage->status != StorageStatusOK) { FURI_LOG_E(TAG, "sd init error: %s", storage_data_status_text(storage)); - if(notify) { - NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); - sd_notify_error(notification); - furi_record_close(RECORD_NOTIFICATION); - } error = FSE_INTERNAL; + } else { FURI_LOG_I(TAG, "card mounted"); - if(notify) { - NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); - sd_notify_success(notification); - furi_record_close(RECORD_NOTIFICATION); - } +#ifndef FURI_RAM_EXEC + if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagStorageFormatInternal)) { + FURI_LOG_I(TAG, "deleting internal storage directory"); + error = sd_remove_recursive(STORAGE_INTERNAL_DIR_NAME) ? FSE_OK : FSE_INTERNAL; + } else { + error = FSE_OK; + } +#else + UNUSED(sd_remove_recursive); error = FSE_OK; +#endif + } + + if(notify) { + NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); + if(error != FSE_OK) { + sd_notify_error(notification); + } else { + sd_notify_success(notification); + } + furi_record_close(RECORD_NOTIFICATION); } return error; @@ -714,6 +786,10 @@ void storage_ext_init(StorageData* storage) { // do not notify on first launch, notifications app is waiting for our thread to read settings storage_ext_tick_internal(storage, false); +#ifndef FURI_RAM_EXEC + // always reset the flag to prevent accidental wipe on SD card insertion + furi_hal_rtc_reset_flag(FuriHalRtcFlagStorageFormatInternal); +#endif } #include "fatfs/ff_gen_drv.h" diff --git a/applications/settings/about/about.c b/applications/settings/about/about.c index 710668fc4..c5f839ec6 100644 --- a/applications/settings/about/about.c +++ b/applications/settings/about/about.c @@ -1,9 +1,12 @@ #include -#include + #include -#include +#include #include + +#include #include + #include #include #include @@ -210,7 +213,8 @@ const AboutDialogScreen about_screens[] = { about_screen_cert_china_0, about_screen_cert_china_1, about_screen_cert_taiwan, - about_screen_cert_mexico}; + about_screen_cert_mexico, +}; int32_t about_settings_app(void* p) { UNUSED(p); @@ -218,18 +222,15 @@ int32_t about_settings_app(void* p) { DialogMessage* message = dialog_message_alloc(); Gui* gui = furi_record_open(RECORD_GUI); - ViewDispatcher* view_dispatcher = view_dispatcher_alloc(); + ViewHolder* view_holder = view_holder_alloc(); EmptyScreen* empty_screen = empty_screen_alloc(); - const uint32_t empty_screen_index = 0; size_t screen_index = 0; DialogMessageButton screen_result; // draw empty screen to prevent menu flickering - view_dispatcher_add_view( - view_dispatcher, empty_screen_index, empty_screen_get_view(empty_screen)); - view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen); - view_dispatcher_switch_to_view(view_dispatcher, empty_screen_index); + view_holder_attach_to_gui(view_holder, gui); + view_holder_set_view(view_holder, empty_screen_get_view(empty_screen)); int32_t ret = 0; while(1) { @@ -266,8 +267,8 @@ int32_t about_settings_app(void* p) { dialog_message_free(message); furi_record_close(RECORD_DIALOGS); - view_dispatcher_remove_view(view_dispatcher, empty_screen_index); - view_dispatcher_free(view_dispatcher); + view_holder_set_view(view_holder, NULL); + view_holder_free(view_holder); empty_screen_free(empty_screen); furi_record_close(RECORD_GUI); diff --git a/applications/settings/bt_settings_app/bt_settings_app.c b/applications/settings/bt_settings_app/bt_settings_app.c index 897282064..174d0bcbb 100644 --- a/applications/settings/bt_settings_app/bt_settings_app.c +++ b/applications/settings/bt_settings_app/bt_settings_app.c @@ -21,7 +21,6 @@ BtSettingsApp* bt_settings_app_alloc(void) { // View Dispatcher and Scene Manager app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&bt_settings_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( diff --git a/applications/settings/desktop_settings/desktop_settings_app.c b/applications/settings/desktop_settings/desktop_settings_app.c index 2b7bd31d8..71b42f81a 100644 --- a/applications/settings/desktop_settings/desktop_settings_app.c +++ b/applications/settings/desktop_settings/desktop_settings_app.c @@ -57,7 +57,6 @@ DesktopSettingsApp* desktop_settings_app_alloc(void) { app->dialogs = furi_record_open(RECORD_DIALOGS); app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&desktop_settings_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( diff --git a/applications/settings/expansion_settings_app/expansion_settings_app.c b/applications/settings/expansion_settings_app/expansion_settings_app.c index 7544bea32..639f7f23d 100644 --- a/applications/settings/expansion_settings_app/expansion_settings_app.c +++ b/applications/settings/expansion_settings_app/expansion_settings_app.c @@ -33,7 +33,6 @@ static ExpansionSettingsApp* expansion_settings_app_alloc(void) { app->expansion = furi_record_open(RECORD_EXPANSION); app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); diff --git a/applications/settings/notification_settings/notification_settings_app.c b/applications/settings/notification_settings/notification_settings_app.c index 7576dcf3c..2462b32bd 100644 --- a/applications/settings/notification_settings/notification_settings_app.c +++ b/applications/settings/notification_settings/notification_settings_app.c @@ -242,7 +242,6 @@ static NotificationAppSettings* alloc_settings(void) { } app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); view_dispatcher_add_view(app->view_dispatcher, 0, view); view_dispatcher_switch_to_view(app->view_dispatcher, 0); diff --git a/applications/settings/power_settings_app/power_settings_app.c b/applications/settings/power_settings_app/power_settings_app.c index 1f87b4959..718fe0ef0 100644 --- a/applications/settings/power_settings_app/power_settings_app.c +++ b/applications/settings/power_settings_app/power_settings_app.c @@ -34,7 +34,6 @@ PowerSettingsApp* power_settings_app_alloc(uint32_t first_scene, ViewDispatcherT // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); app->scene_manager = scene_manager_alloc(&power_settings_scene_handlers, app); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( app->view_dispatcher, power_settings_custom_event_callback); diff --git a/applications/settings/power_settings_app/scenes/power_settings_scene_reboot_confirm.c b/applications/settings/power_settings_app/scenes/power_settings_scene_reboot_confirm.c index 62e06de92..25e7b2bc4 100644 --- a/applications/settings/power_settings_app/scenes/power_settings_scene_reboot_confirm.c +++ b/applications/settings/power_settings_app/scenes/power_settings_scene_reboot_confirm.c @@ -49,10 +49,12 @@ bool power_settings_scene_reboot_confirm_on_event(void* context, SceneManagerEve if(event.event == DialogExResultLeft) { scene_manager_previous_scene(app->scene_manager); } else if(event.event == DialogExResultRight) { + Power* power = furi_record_open(RECORD_POWER); + if(reboot_type == RebootTypeDFU) { - power_reboot(PowerBootModeDfu); + power_reboot(power, PowerBootModeDfu); } else { - power_reboot(PowerBootModeNormal); + power_reboot(power, PowerBootModeNormal); } } consumed = true; diff --git a/applications/settings/storage_settings/scenes/storage_settings_scene_factory_reset.c b/applications/settings/storage_settings/scenes/storage_settings_scene_factory_reset.c index 2d977176a..0f8e1aa96 100644 --- a/applications/settings/storage_settings/scenes/storage_settings_scene_factory_reset.c +++ b/applications/settings/storage_settings/scenes/storage_settings_scene_factory_reset.c @@ -65,7 +65,9 @@ bool storage_settings_scene_factory_reset_on_event(void* context, SceneManagerEv } else { furi_hal_rtc_reset_registers(); furi_hal_rtc_set_flag(FuriHalRtcFlagStorageFormatInternal); - power_reboot(PowerBootModeNormal); + + Power* power = furi_record_open(RECORD_POWER); + power_reboot(power, PowerBootModeNormal); } consumed = true; diff --git a/applications/settings/storage_settings/storage_settings.c b/applications/settings/storage_settings/storage_settings.c index 82759a288..a6a7b26aa 100644 --- a/applications/settings/storage_settings/storage_settings.c +++ b/applications/settings/storage_settings/storage_settings.c @@ -23,7 +23,6 @@ static StorageSettings* storage_settings_alloc(void) { app->scene_manager = scene_manager_alloc(&storage_settings_scene_handlers, app); app->text_string = furi_string_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback( diff --git a/applications/settings/system/system_settings.c b/applications/settings/system/system_settings.c index 55b428d96..b853abd33 100644 --- a/applications/settings/system/system_settings.c +++ b/applications/settings/system/system_settings.c @@ -220,7 +220,6 @@ SystemSettings* system_settings_alloc(void) { app->gui = furi_record_open(RECORD_GUI); app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); diff --git a/applications/system/hid_app/hid.c b/applications/system/hid_app/hid.c index 586d198a9..e297e0738 100644 --- a/applications/system/hid_app/hid.c +++ b/applications/system/hid_app/hid.c @@ -72,7 +72,6 @@ Hid* hid_alloc() { // View dispatcher app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_set_event_callback_context(app->view_dispatcher, app); view_dispatcher_set_custom_event_callback(app->view_dispatcher, hid_custom_event_callback); view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hid_back_event_callback); diff --git a/applications/system/js_app/js_app.c b/applications/system/js_app/js_app.c index 72ee4234a..f058d095b 100644 --- a/applications/system/js_app/js_app.c +++ b/applications/system/js_app/js_app.c @@ -69,7 +69,6 @@ static JsApp* js_app_alloc(void) { app->loading = loading_alloc(); app->gui = furi_record_open("gui"); - view_dispatcher_enable_queue(app->view_dispatcher); view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); view_dispatcher_add_view( app->view_dispatcher, JsAppViewLoading, loading_get_view(app->loading)); diff --git a/applications/system/js_app/modules/js_submenu.c b/applications/system/js_app/modules/js_submenu.c index 058b32fd0..5ab9bef77 100644 --- a/applications/system/js_app/modules/js_submenu.c +++ b/applications/system/js_app/modules/js_submenu.c @@ -97,10 +97,9 @@ static void js_submenu_show(struct mjs* mjs) { view_holder_set_back_callback(submenu->view_holder, submenu_exit, submenu); view_holder_set_view(submenu->view_holder, submenu_get_view(submenu->submenu)); - view_holder_start(submenu->view_holder); api_lock_wait_unlock(submenu->lock); - view_holder_stop(submenu->view_holder); + view_holder_set_view(submenu->view_holder, NULL); view_holder_free(submenu->view_holder); furi_record_close(RECORD_GUI); api_lock_free(submenu->lock); diff --git a/applications/system/js_app/modules/js_textbox.c b/applications/system/js_app/modules/js_textbox.c index 33798b296..b90dbc153 100644 --- a/applications/system/js_app/modules/js_textbox.c +++ b/applications/system/js_app/modules/js_textbox.c @@ -125,7 +125,7 @@ static void js_textbox_is_open(struct mjs* mjs) { static void textbox_callback(void* context, uint32_t arg) { UNUSED(arg); JsTextboxInst* textbox = context; - view_holder_stop(textbox->view_holder); + view_holder_set_view(textbox->view_holder, NULL); textbox->is_shown = false; } @@ -145,7 +145,7 @@ static void js_textbox_show(struct mjs* mjs) { return; } - view_holder_start(textbox->view_holder); + view_holder_set_view(textbox->view_holder, text_box_get_view(textbox->text_box)); textbox->is_shown = true; mjs_return(mjs, MJS_UNDEFINED); @@ -155,7 +155,7 @@ static void js_textbox_close(struct mjs* mjs) { JsTextboxInst* textbox = get_this_ctx(mjs); if(!check_arg_count(mjs, 0)) return; - view_holder_stop(textbox->view_holder); + view_holder_set_view(textbox->view_holder, NULL); textbox->is_shown = false; mjs_return(mjs, MJS_UNDEFINED); @@ -180,7 +180,6 @@ static void* js_textbox_create(struct mjs* mjs, mjs_val_t* object) { textbox->view_holder = view_holder_alloc(); view_holder_attach_to_gui(textbox->view_holder, gui); view_holder_set_back_callback(textbox->view_holder, textbox_exit, textbox); - view_holder_set_view(textbox->view_holder, text_box_get_view(textbox->text_box)); *object = textbox_obj; return textbox; @@ -189,7 +188,7 @@ static void* js_textbox_create(struct mjs* mjs, mjs_val_t* object) { static void js_textbox_destroy(void* inst) { JsTextboxInst* textbox = inst; - view_holder_stop(textbox->view_holder); + view_holder_set_view(textbox->view_holder, NULL); view_holder_free(textbox->view_holder); textbox->view_holder = NULL; diff --git a/applications/system/updater/updater.c b/applications/system/updater/updater.c index 4c7fd29e9..15d7dd3a9 100644 --- a/applications/system/updater/updater.c +++ b/applications/system/updater/updater.c @@ -47,8 +47,6 @@ Updater* updater_alloc(const char* arg) { updater->view_dispatcher = view_dispatcher_alloc(); updater->scene_manager = scene_manager_alloc(&updater_scene_handlers, updater); - view_dispatcher_enable_queue(updater->view_dispatcher); - view_dispatcher_set_event_callback_context(updater->view_dispatcher, updater); view_dispatcher_set_custom_event_callback( updater->view_dispatcher, updater_custom_event_callback); diff --git a/assets/icons/Keyboard/KeySignSelected_21x11.png b/assets/icons/Keyboard/KeySignSelected_21x11.png new file mode 100644 index 000000000..23ec2a9c4 Binary files /dev/null and b/assets/icons/Keyboard/KeySignSelected_21x11.png differ diff --git a/assets/icons/Keyboard/KeySign_21x11.png b/assets/icons/Keyboard/KeySign_21x11.png new file mode 100644 index 000000000..f31e9e0fa Binary files /dev/null and b/assets/icons/Keyboard/KeySign_21x11.png differ diff --git a/furi/core/event_loop.c b/furi/core/event_loop.c index feed8d6f4..2a6cd51d3 100644 --- a/furi/core/event_loop.c +++ b/furi/core/event_loop.c @@ -1,5 +1,4 @@ #include "event_loop_i.h" -#include "message_queue_i.h" #include "log.h" #include "check.h" @@ -22,13 +21,17 @@ static FuriEventLoopItem* furi_event_loop_item_alloc( static void furi_event_loop_item_free(FuriEventLoopItem* instance); +static void furi_event_loop_item_free_later(FuriEventLoopItem* instance); + static void furi_event_loop_item_set_callback( FuriEventLoopItem* instance, - FuriEventLoopMessageQueueCallback callback, + FuriEventLoopEventCallback callback, void* callback_context); static void furi_event_loop_item_notify(FuriEventLoopItem* instance); +static bool furi_event_loop_item_is_waiting(FuriEventLoopItem* instance); + static void furi_event_loop_process_pending_callbacks(FuriEventLoop* instance) { for(; !PendingQueue_empty_p(instance->pending_queue); PendingQueue_pop_back(NULL, instance->pending_queue)) { @@ -37,6 +40,21 @@ static void furi_event_loop_process_pending_callbacks(FuriEventLoop* instance) { } } +static bool furi_event_loop_signal_callback(uint32_t signal, void* arg, void* context) { + furi_assert(context); + FuriEventLoop* instance = context; + UNUSED(arg); + + switch(signal) { + case FuriSignalExit: + furi_event_loop_stop(instance); + return true; + // Room for possible other standard signal handlers + default: + return false; + } +} + /* * Main public API */ @@ -67,6 +85,7 @@ void furi_event_loop_free(FuriEventLoop* instance) { furi_event_loop_process_timer_queue(instance); furi_check(TimerList_empty_p(instance->timer_list)); + furi_check(WaitingList_empty_p(instance->waiting_list)); FuriEventLoopTree_clear(instance->tree); PendingQueue_clear(instance->pending_queue); @@ -81,21 +100,81 @@ void furi_event_loop_free(FuriEventLoop* instance) { free(instance); } -static FuriEventLoopProcessStatus - furi_event_loop_poll_process_event(FuriEventLoop* instance, FuriEventLoopItem* item) { - UNUSED(instance); - +static inline FuriEventLoopProcessStatus + furi_event_loop_poll_process_level_event(FuriEventLoopItem* item) { if(!item->contract->get_level(item->object, item->event)) { return FuriEventLoopProcessStatusComplete; - } - - if(item->callback(item->object, item->callback_context)) { + } else if(item->callback(item->object, item->callback_context)) { return FuriEventLoopProcessStatusIncomplete; } else { return FuriEventLoopProcessStatusAgain; } } +static inline FuriEventLoopProcessStatus + furi_event_loop_poll_process_edge_event(FuriEventLoopItem* item) { + if(item->callback(item->object, item->callback_context)) { + return FuriEventLoopProcessStatusComplete; + } else { + return FuriEventLoopProcessStatusAgain; + } +} + +static inline FuriEventLoopProcessStatus + furi_event_loop_poll_process_event(FuriEventLoop* instance, FuriEventLoopItem* item) { + FuriEventLoopProcessStatus status; + if(item->event & FuriEventLoopEventFlagOnce) { + furi_event_loop_unsubscribe(instance, item->object); + } + + if(item->event & FuriEventLoopEventFlagEdge) { + status = furi_event_loop_poll_process_edge_event(item); + } else { + status = furi_event_loop_poll_process_level_event(item); + } + + if(item->owner == NULL) { + status = FuriEventLoopProcessStatusFreeLater; + } + + return status; +} + +static void furi_event_loop_process_waiting_list(FuriEventLoop* instance) { + FuriEventLoopItem* item = NULL; + + FURI_CRITICAL_ENTER(); + + if(!WaitingList_empty_p(instance->waiting_list)) { + item = WaitingList_pop_front(instance->waiting_list); + WaitingList_init_field(item); + } + + FURI_CRITICAL_EXIT(); + + if(!item) return; + + while(true) { + FuriEventLoopProcessStatus ret = furi_event_loop_poll_process_event(instance, item); + + if(ret == FuriEventLoopProcessStatusComplete) { + // Event processing complete, break from loop + break; + } else if(ret == FuriEventLoopProcessStatusIncomplete) { + // Event processing incomplete more processing needed + } else if(ret == FuriEventLoopProcessStatusAgain) { //-V547 + furi_event_loop_item_notify(item); + break; + // Unsubscribed from inside the callback, delete item + } else if(ret == FuriEventLoopProcessStatusFreeLater) { //-V547 + furi_event_loop_item_free(item); + break; + } else { + furi_crash(); + } + } +} + static void furi_event_loop_restore_flags(FuriEventLoop* instance, uint32_t flags) { if(flags) { xTaskNotifyIndexed( @@ -134,34 +213,7 @@ void furi_event_loop_run(FuriEventLoop* instance) { break; } else if(flags & FuriEventLoopFlagEvent) { - FuriEventLoopItem* item = NULL; - FURI_CRITICAL_ENTER(); - - if(!WaitingList_empty_p(instance->waiting_list)) { - item = WaitingList_pop_front(instance->waiting_list); - WaitingList_init_field(item); - } - - FURI_CRITICAL_EXIT(); - - if(item) { - while(true) { - FuriEventLoopProcessStatus ret = - furi_event_loop_poll_process_event(instance, item); - if(ret == FuriEventLoopProcessStatusComplete) { - // Event processing complete, break from loop - break; - } else if(ret == FuriEventLoopProcessStatusIncomplete) { - // Event processing incomplete more processing needed - } else if(ret == FuriEventLoopProcessStatusAgain) { //-V547 - furi_event_loop_item_notify(item); - break; - } else { - furi_crash(); - } - } - } - + furi_event_loop_process_waiting_list(instance); furi_event_loop_restore_flags(instance, flags & ~FuriEventLoopFlagEvent); } else if(flags & FuriEventLoopFlagTimer) { @@ -217,87 +269,150 @@ void furi_event_loop_pend_callback( } /* - * Message queue API + * Private generic susbscription API */ -void furi_event_loop_message_queue_subscribe( +static void furi_event_loop_object_subscribe( FuriEventLoop* instance, - FuriMessageQueue* message_queue, + FuriEventLoopObject* object, + const FuriEventLoopContract* contract, FuriEventLoopEvent event, - FuriEventLoopMessageQueueCallback callback, + FuriEventLoopEventCallback callback, void* context) { furi_check(instance); furi_check(instance->thread_id == furi_thread_get_current_id()); - furi_check(instance->state == FuriEventLoopStateStopped); - furi_check(message_queue); + furi_check(object); + furi_assert(contract); + furi_check(callback); FURI_CRITICAL_ENTER(); - furi_check(FuriEventLoopTree_get(instance->tree, message_queue) == NULL); + furi_check(FuriEventLoopTree_get(instance->tree, object) == NULL); // Allocate and setup item - FuriEventLoopItem* item = furi_event_loop_item_alloc( - instance, &furi_message_queue_event_loop_contract, message_queue, event); + FuriEventLoopItem* item = furi_event_loop_item_alloc(instance, contract, object, event); furi_event_loop_item_set_callback(item, callback, context); - FuriEventLoopTree_set_at(instance->tree, message_queue, item); + FuriEventLoopTree_set_at(instance->tree, object, item); - FuriEventLoopLink* link = item->contract->get_link(message_queue); + FuriEventLoopLink* link = item->contract->get_link(object); + FuriEventLoopEvent event_noflags = item->event & FuriEventLoopEventMask; - if(item->event == FuriEventLoopEventIn) { + if(event_noflags == FuriEventLoopEventIn) { furi_check(link->item_in == NULL); link->item_in = item; - } else if(item->event == FuriEventLoopEventOut) { + } else if(event_noflags == FuriEventLoopEventOut) { furi_check(link->item_out == NULL); link->item_out = item; } else { furi_crash(); } - if(item->contract->get_level(item->object, item->event)) { - furi_event_loop_item_notify(item); + if(!(item->event & FuriEventLoopEventFlagEdge)) { + if(item->contract->get_level(item->object, event_noflags)) { + furi_event_loop_item_notify(item); + } } FURI_CRITICAL_EXIT(); } -void furi_event_loop_message_queue_unsubscribe( +/** + * Public specialized subscription API + */ + +void furi_event_loop_subscribe_message_queue( FuriEventLoop* instance, - FuriMessageQueue* message_queue) { + FuriMessageQueue* message_queue, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context) { + extern const FuriEventLoopContract furi_message_queue_event_loop_contract; + + furi_event_loop_object_subscribe( + instance, message_queue, &furi_message_queue_event_loop_contract, event, callback, context); +} + +void furi_event_loop_subscribe_stream_buffer( + FuriEventLoop* instance, + FuriStreamBuffer* stream_buffer, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context) { + extern const FuriEventLoopContract furi_stream_buffer_event_loop_contract; + + furi_event_loop_object_subscribe( + instance, stream_buffer, &furi_stream_buffer_event_loop_contract, event, callback, context); +} + +void furi_event_loop_subscribe_semaphore( + FuriEventLoop* instance, + FuriSemaphore* semaphore, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context) { + extern const FuriEventLoopContract furi_semaphore_event_loop_contract; + + furi_event_loop_object_subscribe( + instance, semaphore, &furi_semaphore_event_loop_contract, event, callback, context); +} + +void furi_event_loop_subscribe_mutex( + FuriEventLoop* instance, + FuriMutex* mutex, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context) { + extern const FuriEventLoopContract furi_mutex_event_loop_contract; + + furi_event_loop_object_subscribe( + instance, mutex, &furi_mutex_event_loop_contract, event, callback, context); +} + +/** + * Public generic unsubscription API + */ + +void furi_event_loop_unsubscribe(FuriEventLoop* instance, FuriEventLoopObject* object) { furi_check(instance); - furi_check(instance->state == FuriEventLoopStateStopped); furi_check(instance->thread_id == furi_thread_get_current_id()); FURI_CRITICAL_ENTER(); - FuriEventLoopItem** item_ptr = FuriEventLoopTree_get(instance->tree, message_queue); - furi_check(item_ptr); + FuriEventLoopItem* item = NULL; + furi_check(FuriEventLoopTree_pop_at(&item, instance->tree, object)); - FuriEventLoopItem* item = *item_ptr; furi_check(item); furi_check(item->owner == instance); - FuriEventLoopLink* link = item->contract->get_link(message_queue); + FuriEventLoopLink* link = item->contract->get_link(object); + FuriEventLoopEvent event_noflags = item->event & FuriEventLoopEventMask; - if(item->event == FuriEventLoopEventIn) { + if(event_noflags == FuriEventLoopEventIn) { furi_check(link->item_in == item); link->item_in = NULL; - } else if(item->event == FuriEventLoopEventOut) { + } else if(event_noflags == FuriEventLoopEventOut) { furi_check(link->item_out == item); link->item_out = NULL; } else { furi_crash(); } - furi_event_loop_item_free(item); + if(furi_event_loop_item_is_waiting(item)) { + WaitingList_unlink(item); + } - FuriEventLoopTree_erase(instance->tree, message_queue); + if(instance->state == FuriEventLoopStateProcessing) { + furi_event_loop_item_free_later(item); + } else { + furi_event_loop_item_free(item); + } FURI_CRITICAL_EXIT(); } /* - * Event Loop Item API, used internally + * Private Event Loop Item functions */ static FuriEventLoopItem* furi_event_loop_item_alloc( @@ -322,12 +437,19 @@ static FuriEventLoopItem* furi_event_loop_item_alloc( static void furi_event_loop_item_free(FuriEventLoopItem* instance) { furi_assert(instance); + furi_assert(!furi_event_loop_item_is_waiting(instance)); free(instance); } +static void furi_event_loop_item_free_later(FuriEventLoopItem* instance) { + furi_assert(instance); + furi_assert(!furi_event_loop_item_is_waiting(instance)); + instance->owner = NULL; +} + static void furi_event_loop_item_set_callback( FuriEventLoopItem* instance, - FuriEventLoopMessageQueueCallback callback, + FuriEventLoopEventCallback callback, void* callback_context) { furi_assert(instance); furi_assert(!instance->callback); @@ -341,27 +463,35 @@ static void furi_event_loop_item_notify(FuriEventLoopItem* instance) { FURI_CRITICAL_ENTER(); - if(!instance->WaitingList.prev && !instance->WaitingList.next) { - WaitingList_push_back(instance->owner->waiting_list, instance); + FuriEventLoop* owner = instance->owner; + furi_assert(owner); + + if(!furi_event_loop_item_is_waiting(instance)) { + WaitingList_push_back(owner->waiting_list, instance); } FURI_CRITICAL_EXIT(); xTaskNotifyIndexed( - instance->owner->thread_id, - FURI_EVENT_LOOP_FLAG_NOTIFY_INDEX, - FuriEventLoopFlagEvent, - eSetBits); + owner->thread_id, FURI_EVENT_LOOP_FLAG_NOTIFY_INDEX, FuriEventLoopFlagEvent, eSetBits); } +static bool furi_event_loop_item_is_waiting(FuriEventLoopItem* instance) { + return instance->WaitingList.prev || instance->WaitingList.next; +} + +/* + * Internal event loop link API, used by supported primitives + */ + void furi_event_loop_link_notify(FuriEventLoopLink* instance, FuriEventLoopEvent event) { furi_assert(instance); FURI_CRITICAL_ENTER(); - if(event == FuriEventLoopEventIn) { + if(event & FuriEventLoopEventIn) { if(instance->item_in) furi_event_loop_item_notify(instance->item_in); - } else if(event == FuriEventLoopEventOut) { + } else if(event & FuriEventLoopEventOut) { if(instance->item_out) furi_event_loop_item_notify(instance->item_out); } else { furi_crash(); @@ -369,18 +499,3 @@ void furi_event_loop_link_notify(FuriEventLoopLink* instance, FuriEventLoopEvent FURI_CRITICAL_EXIT(); } - -bool furi_event_loop_signal_callback(uint32_t signal, void* arg, void* context) { - furi_assert(context); - FuriEventLoop* instance = context; - UNUSED(arg); - - switch(signal) { - case FuriSignalExit: - furi_event_loop_stop(instance); - return true; - // Room for possible other standard signal handlers - default: - return false; - } -} diff --git a/furi/core/event_loop.h b/furi/core/event_loop.h index 9ae9f6c4d..af5987101 100644 --- a/furi/core/event_loop.h +++ b/furi/core/event_loop.h @@ -20,10 +20,83 @@ extern "C" { #endif -/** Event Loop events */ +/** + * @brief Enumeration of event types, flags and masks. + * + * Only one event direction (In or Out) can be used per subscription. + * An object can have no more than one subscription for each direction. + * + * Additional flags that modify the behaviour can be + * set using the bitwise OR operation (see flag description). + */ typedef enum { - FuriEventLoopEventOut, /**< On departure: item was retrieved from container, flag reset, etc... */ - FuriEventLoopEventIn, /**< On arrival: item was inserted into container, flag set, etc... */ + /** + * @brief Subscribe to In events. + * + * In events occur on the following conditions: + * - One or more items were inserted into a FuriMessageQueue, + * - Enough data has been written to a FuriStreamBuffer, + * - A FuriSemaphore has been released at least once, + * - A FuriMutex has been released. + */ + FuriEventLoopEventIn = 0x00000001U, + /** + * @brief Subscribe to Out events. + * + * Out events occur on the following conditions: + * - One or more items were removed from a FuriMessageQueue, + * - Any amount of data has been read out of a FuriStreamBuffer, + * - A FuriSemaphore has been acquired at least once, + * - A FuriMutex has been acquired. + */ + FuriEventLoopEventOut = 0x00000002U, + /** + * @brief Special value containing the event direction bits, used internally. + */ + FuriEventLoopEventMask = 0x00000003U, + /** + * @brief Use edge triggered events. + * + * By default, level triggered events are used. A level above zero + * is reported based on the following conditions: + * + * In events: + * - a FuriMessageQueue contains one or more items, + * - a FuriStreamBuffer contains one or more bytes, + * - a FuriSemaphore can be acquired at least once, + * - a FuriMutex can be acquired. + * + * Out events: + * - a FuriMessageQueue has at least one item of free space, + * - a FuriStreamBuffer has at least one byte of free space, + * - a FuriSemaphore has been acquired at least once, + * - a FuriMutex has been acquired. + * + * If this flag is NOT set, the event will be generated repeatedly until + * the level becomes zero (e.g. all items have been removed from + * a FuriMessageQueue in case of the "In" event, etc.) + * + * If this flag IS set, then the above check is skipped and the event + * is generated ONLY when a change occurs, with the event direction + * (In or Out) taken into account. + */ + FuriEventLoopEventFlagEdge = 0x00000004U, + /** + * @brief Automatically unsubscribe from events after one time. + * + * By default, events will be generated each time the specified conditions + * have been met. If this flag IS set, the event subscription will be cancelled + * upon the first occurred event and no further events will be generated. + */ + FuriEventLoopEventFlagOnce = 0x00000008U, + /** + * @brief Special value containing the event flag bits, used internally. + */ + FuriEventLoopEventFlagMask = 0xFFFFFFFCU, + /** + * @brief Special value to force the enum to 32-bit values. + */ + FuriEventLoopEventReserved = UINT32_MAX, } FuriEventLoopEvent; /** Anonymous message queue type */ @@ -115,21 +188,22 @@ void furi_event_loop_pend_callback( void* context); /* - * Message queue related APIs + * Event subscription/notification APIs */ -/** Anonymous message queue type */ -typedef struct FuriMessageQueue FuriMessageQueue; +typedef void FuriEventLoopObject; -/** Callback type for message queue +/** Callback type for event loop events * - * @param queue The queue that triggered event - * @param context The context that was provided on - * furi_event_loop_message_queue_subscribe call + * @param object The object that triggered the event + * @param context The context that was provided upon subscription * * @return true if event was processed, false if we need to delay processing */ -typedef bool (*FuriEventLoopMessageQueueCallback)(FuriMessageQueue* queue, void* context); +typedef bool (*FuriEventLoopEventCallback)(FuriEventLoopObject* object, void* context); + +/** Opaque message queue type */ +typedef struct FuriMessageQueue FuriMessageQueue; /** Subscribe to message queue events * @@ -141,21 +215,79 @@ typedef bool (*FuriEventLoopMessageQueueCallback)(FuriMessageQueue* queue, void* * @param[in] callback The callback to call on event * @param context The context for callback */ -void furi_event_loop_message_queue_subscribe( +void furi_event_loop_subscribe_message_queue( FuriEventLoop* instance, FuriMessageQueue* message_queue, FuriEventLoopEvent event, - FuriEventLoopMessageQueueCallback callback, + FuriEventLoopEventCallback callback, void* context); -/** Unsubscribe from message queue +/** Opaque stream buffer type */ +typedef struct FuriStreamBuffer FuriStreamBuffer; + +/** Subscribe to stream buffer events + * + * @warning you can only have one subscription for one event type. * * @param instance The Event Loop instance - * @param message_queue The message queue + * @param stream_buffer The stream buffer to add + * @param[in] event The Event Loop event to trigger on + * @param[in] callback The callback to call on event + * @param context The context for callback */ -void furi_event_loop_message_queue_unsubscribe( +void furi_event_loop_subscribe_stream_buffer( FuriEventLoop* instance, - FuriMessageQueue* message_queue); + FuriStreamBuffer* stream_buffer, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context); + +/** Opaque semaphore type */ +typedef struct FuriSemaphore FuriSemaphore; + +/** Subscribe to semaphore events + * + * @warning you can only have one subscription for one event type. + * + * @param instance The Event Loop instance + * @param semaphore The semaphore to add + * @param[in] event The Event Loop event to trigger on + * @param[in] callback The callback to call on event + * @param context The context for callback + */ +void furi_event_loop_subscribe_semaphore( + FuriEventLoop* instance, + FuriSemaphore* semaphore, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context); + +/** Opaque mutex type */ +typedef struct FuriMutex FuriMutex; + +/** Subscribe to mutex events + * + * @warning you can only have one subscription for one event type. + * + * @param instance The Event Loop instance + * @param mutex The mutex to add + * @param[in] event The Event Loop event to trigger on + * @param[in] callback The callback to call on event + * @param context The context for callback + */ +void furi_event_loop_subscribe_mutex( + FuriEventLoop* instance, + FuriMutex* mutex, + FuriEventLoopEvent event, + FuriEventLoopEventCallback callback, + void* context); + +/** Unsubscribe from events (common) + * + * @param instance The Event Loop instance + * @param object The object to unsubscribe from + */ +void furi_event_loop_unsubscribe(FuriEventLoop* instance, FuriEventLoopObject* object); #ifdef __cplusplus } diff --git a/furi/core/event_loop_i.h b/furi/core/event_loop_i.h index cd1014867..15efa8f86 100644 --- a/furi/core/event_loop_i.h +++ b/furi/core/event_loop_i.h @@ -16,16 +16,16 @@ struct FuriEventLoopItem { FuriEventLoop* owner; // Tracking item - const FuriEventLoopContract* contract; - void* object; FuriEventLoopEvent event; + FuriEventLoopObject* object; + const FuriEventLoopContract* contract; // Callback and context - FuriEventLoopMessageQueueCallback callback; + FuriEventLoopEventCallback callback; void* callback_context; // Waiting list - ILIST_INTERFACE(WaitingList, struct FuriEventLoopItem); + ILIST_INTERFACE(WaitingList, FuriEventLoopItem); }; ILIST_DEF(WaitingList, FuriEventLoopItem, M_POD_OPLIST) @@ -36,7 +36,7 @@ ILIST_DEF(WaitingList, FuriEventLoopItem, M_POD_OPLIST) BPTREE_DEF2( // NOLINT FuriEventLoopTree, FURI_EVENT_LOOP_TREE_RANK, - void*, /* pointer to object we track */ + FuriEventLoopObject*, /* pointer to object we track */ M_PTR_OPLIST, FuriEventLoopItem*, /* pointer to the FuriEventLoopItem */ M_PTR_OPLIST) @@ -60,6 +60,7 @@ typedef enum { FuriEventLoopProcessStatusComplete, FuriEventLoopProcessStatusIncomplete, FuriEventLoopProcessStatusAgain, + FuriEventLoopProcessStatusFreeLater, } FuriEventLoopProcessStatus; typedef enum { diff --git a/furi/core/event_loop_link_i.h b/furi/core/event_loop_link_i.h index 5c0b144a1..992ca6555 100644 --- a/furi/core/event_loop_link_i.h +++ b/furi/core/event_loop_link_i.h @@ -19,17 +19,16 @@ void furi_event_loop_link_notify(FuriEventLoopLink* instance, FuriEventLoopEvent /* Contract between event loop and an object */ -typedef FuriEventLoopLink* (*FuriEventLoopContractGetLink)(void* object); +typedef FuriEventLoopLink* (*FuriEventLoopContractGetLink)(FuriEventLoopObject* object); -typedef uint32_t (*FuriEventLoopContractGetLevel)(void* object, FuriEventLoopEvent event); +typedef uint32_t ( + *FuriEventLoopContractGetLevel)(FuriEventLoopObject* object, FuriEventLoopEvent event); typedef struct { const FuriEventLoopContractGetLink get_link; const FuriEventLoopContractGetLevel get_level; } FuriEventLoopContract; -bool furi_event_loop_signal_callback(uint32_t signal, void* arg, void* context); - #ifdef __cplusplus } #endif diff --git a/furi/core/message_queue.c b/furi/core/message_queue.c index 3521ceb30..bd0cec021 100644 --- a/furi/core/message_queue.c +++ b/furi/core/message_queue.c @@ -1,4 +1,4 @@ -#include "message_queue_i.h" +#include "message_queue.h" #include #include @@ -6,6 +6,8 @@ #include "kernel.h" #include "check.h" +#include "event_loop_link_i.h" + // Internal FreeRTOS member names #define uxMessagesWaiting uxDummy4[0] #define uxLength uxDummy4[1] @@ -13,10 +15,7 @@ struct FuriMessageQueue { StaticQueue_t container; - - // Event Loop Link FuriEventLoopLink event_loop_link; - uint8_t buffer[]; }; @@ -208,13 +207,14 @@ FuriStatus furi_message_queue_reset(FuriMessageQueue* instance) { return stat; } -static FuriEventLoopLink* furi_message_queue_event_loop_get_link(void* object) { +static FuriEventLoopLink* furi_message_queue_event_loop_get_link(FuriEventLoopObject* object) { FuriMessageQueue* instance = object; furi_assert(instance); return &instance->event_loop_link; } -static uint32_t furi_message_queue_event_loop_get_level(void* object, FuriEventLoopEvent event) { +static uint32_t + furi_message_queue_event_loop_get_level(FuriEventLoopObject* object, FuriEventLoopEvent event) { FuriMessageQueue* instance = object; furi_assert(instance); diff --git a/furi/core/message_queue_i.h b/furi/core/message_queue_i.h deleted file mode 100644 index a88d04131..000000000 --- a/furi/core/message_queue_i.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include "message_queue.h" -#include "event_loop_link_i.h" - -extern const FuriEventLoopContract furi_message_queue_event_loop_contract; diff --git a/furi/core/mutex.c b/furi/core/mutex.c index f59ae83ad..f9848e1ba 100644 --- a/furi/core/mutex.c +++ b/furi/core/mutex.c @@ -1,15 +1,18 @@ #include "mutex.h" -#include "check.h" -#include "common_defines.h" #include #include +#include "check.h" + +#include "event_loop_link_i.h" + // Internal FreeRTOS member names #define ucQueueType ucDummy9 struct FuriMutex { StaticSemaphore_t container; + FuriEventLoopLink event_loop_link; }; // IMPORTANT: container MUST be the FIRST struct member @@ -39,6 +42,10 @@ void furi_mutex_free(FuriMutex* instance) { furi_check(!FURI_IS_IRQ_MODE()); furi_check(instance); + // Event Loop must be disconnected + furi_check(!instance->event_loop_link.item_in); + furi_check(!instance->event_loop_link.item_out); + vSemaphoreDelete((SemaphoreHandle_t)instance); free(instance); } @@ -76,6 +83,10 @@ FuriStatus furi_mutex_acquire(FuriMutex* instance, uint32_t timeout) { furi_crash(); } + if(stat == FuriStatusOk) { + furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventOut); + } + return stat; } @@ -104,6 +115,10 @@ FuriStatus furi_mutex_release(FuriMutex* instance) { furi_crash(); } + if(stat == FuriStatusOk) { + furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventIn); + } + return stat; } @@ -122,3 +137,26 @@ FuriThreadId furi_mutex_get_owner(FuriMutex* instance) { return owner; } + +static FuriEventLoopLink* furi_mutex_event_loop_get_link(FuriEventLoopObject* object) { + FuriMutex* instance = object; + furi_assert(instance); + return &instance->event_loop_link; +} + +static uint32_t + furi_mutex_event_loop_get_level(FuriEventLoopObject* object, FuriEventLoopEvent event) { + FuriMutex* instance = object; + furi_assert(instance); + + if(event == FuriEventLoopEventIn || event == FuriEventLoopEventOut) { + return furi_mutex_get_owner(instance) ? 0 : 1; + } else { + furi_crash(); + } +} + +const FuriEventLoopContract furi_mutex_event_loop_contract = { + .get_link = furi_mutex_event_loop_get_link, + .get_level = furi_mutex_event_loop_get_level, +}; diff --git a/furi/core/semaphore.c b/furi/core/semaphore.c index 6413eb65f..850169ad6 100644 --- a/furi/core/semaphore.c +++ b/furi/core/semaphore.c @@ -1,12 +1,20 @@ #include "semaphore.h" -#include "check.h" -#include "common_defines.h" #include #include +#include "check.h" +#include "kernel.h" + +#include "event_loop_link_i.h" + +// Internal FreeRTOS member names +#define uxMessagesWaiting uxDummy4[0] +#define uxLength uxDummy4[1] + struct FuriSemaphore { StaticSemaphore_t container; + FuriEventLoopLink event_loop_link; }; // IMPORTANT: container MUST be the FIRST struct member @@ -40,6 +48,10 @@ void furi_semaphore_free(FuriSemaphore* instance) { furi_check(instance); furi_check(!FURI_IS_IRQ_MODE()); + // Event Loop must be disconnected + furi_check(!instance->event_loop_link.item_in); + furi_check(!instance->event_loop_link.item_out); + vSemaphoreDelete((SemaphoreHandle_t)instance); free(instance); } @@ -76,6 +88,10 @@ FuriStatus furi_semaphore_acquire(FuriSemaphore* instance, uint32_t timeout) { } } + if(stat == FuriStatusOk) { + furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventOut); + } + return stat; } @@ -103,6 +119,10 @@ FuriStatus furi_semaphore_release(FuriSemaphore* instance) { } } + if(stat == FuriStatusOk) { + furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventIn); + } + return stat; } @@ -120,3 +140,46 @@ uint32_t furi_semaphore_get_count(FuriSemaphore* instance) { return count; } + +uint32_t furi_semaphore_get_space(FuriSemaphore* instance) { + furi_assert(instance); + + uint32_t space; + + if(furi_kernel_is_irq_or_masked() != 0U) { + uint32_t isrm = taskENTER_CRITICAL_FROM_ISR(); + + space = instance->container.uxLength - instance->container.uxMessagesWaiting; + + taskEXIT_CRITICAL_FROM_ISR(isrm); + } else { + space = uxQueueSpacesAvailable((QueueHandle_t)instance); + } + + return space; +} + +static FuriEventLoopLink* furi_semaphore_event_loop_get_link(FuriEventLoopObject* object) { + FuriSemaphore* instance = object; + furi_assert(instance); + return &instance->event_loop_link; +} + +static uint32_t + furi_semaphore_event_loop_get_level(FuriEventLoopObject* object, FuriEventLoopEvent event) { + FuriSemaphore* instance = object; + furi_assert(instance); + + if(event == FuriEventLoopEventIn) { + return furi_semaphore_get_count(instance); + } else if(event == FuriEventLoopEventOut) { + return furi_semaphore_get_space(instance); + } else { + furi_crash(); + } +} + +const FuriEventLoopContract furi_semaphore_event_loop_contract = { + .get_link = furi_semaphore_event_loop_get_link, + .get_level = furi_semaphore_event_loop_get_level, +}; diff --git a/furi/core/semaphore.h b/furi/core/semaphore.h index c6b9a1176..47a77ed55 100644 --- a/furi/core/semaphore.h +++ b/furi/core/semaphore.h @@ -53,6 +53,14 @@ FuriStatus furi_semaphore_release(FuriSemaphore* instance); */ uint32_t furi_semaphore_get_count(FuriSemaphore* instance); +/** Get available space + * + * @param instance The pointer to FuriSemaphore instance + * + * @return Semaphore available space + */ +uint32_t furi_semaphore_get_space(FuriSemaphore* instance); + #ifdef __cplusplus } #endif diff --git a/furi/core/stream_buffer.c b/furi/core/stream_buffer.c index ef8869dea..f35abec64 100644 --- a/furi/core/stream_buffer.c +++ b/furi/core/stream_buffer.c @@ -1,13 +1,19 @@ #include "stream_buffer.h" -#include "check.h" -#include "common_defines.h" - #include #include +#include "check.h" +#include "common_defines.h" + +#include "event_loop_link_i.h" + +// Internal FreeRTOS member names +#define xTriggerLevelBytes uxDummy1[3] + struct FuriStreamBuffer { StaticStreamBuffer_t container; + FuriEventLoopLink event_loop_link; uint8_t buffer[]; }; @@ -34,6 +40,10 @@ FuriStreamBuffer* furi_stream_buffer_alloc(size_t size, size_t trigger_level) { void furi_stream_buffer_free(FuriStreamBuffer* stream_buffer) { furi_check(stream_buffer); + // Event Loop must be disconnected + furi_check(!stream_buffer->event_loop_link.item_in); + furi_check(!stream_buffer->event_loop_link.item_out); + vStreamBufferDelete((StreamBufferHandle_t)stream_buffer); free(stream_buffer); } @@ -61,6 +71,16 @@ size_t furi_stream_buffer_send( ret = xStreamBufferSend((StreamBufferHandle_t)stream_buffer, data, length, timeout); } + if(ret > 0) { + const size_t bytes_available = + xStreamBufferBytesAvailable((StreamBufferHandle_t)stream_buffer); + const size_t trigger_level = ((StaticStreamBuffer_t*)stream_buffer)->xTriggerLevelBytes; + + if(bytes_available >= trigger_level) { + furi_event_loop_link_notify(&stream_buffer->event_loop_link, FuriEventLoopEventIn); + } + } + return ret; } @@ -82,6 +102,10 @@ size_t furi_stream_buffer_receive( ret = xStreamBufferReceive((StreamBufferHandle_t)stream_buffer, data, length, timeout); } + if(ret > 0) { + furi_event_loop_link_notify(&stream_buffer->event_loop_link, FuriEventLoopEventOut); + } + return ret; } @@ -112,9 +136,42 @@ bool furi_stream_buffer_is_empty(FuriStreamBuffer* stream_buffer) { FuriStatus furi_stream_buffer_reset(FuriStreamBuffer* stream_buffer) { furi_check(stream_buffer); + FuriStatus status; + if(xStreamBufferReset((StreamBufferHandle_t)stream_buffer) == pdPASS) { - return FuriStatusOk; + status = FuriStatusOk; } else { - return FuriStatusError; + status = FuriStatusError; + } + + if(status == FuriStatusOk) { + furi_event_loop_link_notify(&stream_buffer->event_loop_link, FuriEventLoopEventOut); + } + + return status; +} + +static FuriEventLoopLink* furi_stream_buffer_event_loop_get_link(FuriEventLoopObject* object) { + FuriStreamBuffer* stream_buffer = object; + furi_assert(stream_buffer); + return &stream_buffer->event_loop_link; +} + +static uint32_t + furi_stream_buffer_event_loop_get_level(FuriEventLoopObject* object, FuriEventLoopEvent event) { + FuriStreamBuffer* stream_buffer = object; + furi_assert(stream_buffer); + + if(event == FuriEventLoopEventIn) { + return xStreamBufferBytesAvailable((StreamBufferHandle_t)stream_buffer); + } else if(event == FuriEventLoopEventOut) { + return xStreamBufferSpacesAvailable((StreamBufferHandle_t)stream_buffer); + } else { + furi_crash(); } } + +const FuriEventLoopContract furi_stream_buffer_event_loop_contract = { + .get_link = furi_stream_buffer_event_loop_get_link, + .get_level = furi_stream_buffer_event_loop_get_level, +}; diff --git a/furi/core/string.c b/furi/core/string.c index f3e40fe5e..804445e22 100644 --- a/furi/core/string.c +++ b/furi/core/string.c @@ -17,6 +17,7 @@ struct FuriString { #undef furi_string_replace_all #undef furi_string_start_with #undef furi_string_end_with +#undef furi_string_end_withi #undef furi_string_search_char #undef furi_string_search_rchar #undef furi_string_trim @@ -218,10 +219,28 @@ bool furi_string_end_with(const FuriString* v, const FuriString* v2) { return string_end_with_string_p(v->string, v2->string); } +bool furi_string_end_withi(const FuriString* v, const FuriString* v2) { + return furi_string_end_withi_str(v, string_get_cstr(v2->string)); +} + bool furi_string_end_with_str(const FuriString* v, const char str[]) { return string_end_with_str_p(v->string, str); } +bool furi_string_end_withi_str(const FuriString* v, const char str[]) { + M_STR1NG_CONTRACT(v); + M_ASSERT(str != NULL); + + const size_t str_len = strlen(str); + const size_t v_len = string_size(v->string); + + if(v_len < str_len) { + return false; + } + + return strcasecmp(&string_get_cstr(v->string)[v_len - str_len], str) == 0; +} + size_t furi_string_search_char(const FuriString* v, char c, size_t start) { return string_search_char(v->string, c, start); } diff --git a/furi/core/string.h b/furi/core/string.h index 2bfb60e49..84b8c6a24 100644 --- a/furi/core/string.h +++ b/furi/core/string.h @@ -510,6 +510,15 @@ bool furi_string_start_with_str(const FuriString* string, const char start[]); */ bool furi_string_end_with(const FuriString* string, const FuriString* end); +/** Test if the string ends with the given string (case insensitive according to the current locale). + * + * @param string The FuriString instance + * @param end The end + * + * @return true if string ends with + */ +bool furi_string_end_withi(const FuriString* string, const FuriString* end); + /** Test if the string ends with the given C string. * * @param string The FuriString instance @@ -519,6 +528,15 @@ bool furi_string_end_with(const FuriString* string, const FuriString* end); */ bool furi_string_end_with_str(const FuriString* string, const char end[]); +/** Test if the string ends with the given C string (case insensitive according to the current locale). + * + * @param string The FuriString instance + * @param end The end + * + * @return true if string ends with + */ +bool furi_string_end_withi_str(const FuriString* string, const char end[]); + //--------------------------------------------------------------------------- // Trim //--------------------------------------------------------------------------- @@ -699,6 +717,13 @@ void furi_string_utf8_decode(char c, FuriStringUTF8State* state, FuriStringUnico #define furi_string_end_with(a, b) \ FURI_STRING_SELECT2(furi_string_end_with, furi_string_end_with_str, a, b) +/** Test if the string ends with the given string (or C string) (case insensitive according to the current locale). + * + * (string, [c]string) + */ +#define furi_string_end_withi(a, b) \ + FURI_STRING_SELECT2(furi_string_end_withi, furi_string_end_withi_str, a, b) + /** Append a string (or C string) to the string. * * (string, [c]string) diff --git a/lib/SConscript b/lib/SConscript index 83eec71ff..27e155523 100644 --- a/lib/SConscript +++ b/lib/SConscript @@ -33,7 +33,6 @@ libs = env.BuildModules( "digital_signal", "pulse_reader", "signal_reader", - "appframe", "u8g2", "lfrfid", "flipper_application", diff --git a/lib/app-scened-template/generic_scene.hpp b/lib/app-scened-template/generic_scene.hpp deleted file mode 100644 index 580346c8c..000000000 --- a/lib/app-scened-template/generic_scene.hpp +++ /dev/null @@ -1,10 +0,0 @@ -template -class GenericScene { -public: - virtual void on_enter(TApp* app, bool need_restore) = 0; - virtual bool on_event(TApp* app, typename TApp::Event* event) = 0; - virtual void on_exit(TApp* app) = 0; - virtual ~GenericScene() {}; - -private: -}; diff --git a/lib/app-scened-template/record_controller.hpp b/lib/app-scened-template/record_controller.hpp deleted file mode 100644 index 3453c12f3..000000000 --- a/lib/app-scened-template/record_controller.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include - -/** - * @brief Class for opening, casting, holding and closing records - * - * @tparam TRecordClass record class - */ -template -class RecordController { -public: - /** - * @brief Construct a new Record Controller object for record with record name - * - * @param record_name record name - */ - RecordController(const char* record_name) { - name = record_name; - value = static_cast(furi_record_open(name)); - } - - ~RecordController() { - furi_record_close(name); - } - - /** - * @brief Record getter - * - * @return TRecordClass* record value - */ - TRecordClass* get() { - return value; - } - - /** - * @brief Record getter (by cast) - * - * @return TRecordClass* record value - */ - operator TRecordClass*() const { - return value; - } - -private: - const char* name; - TRecordClass* value; -}; diff --git a/lib/app-scened-template/scene_controller.hpp b/lib/app-scened-template/scene_controller.hpp deleted file mode 100644 index eb4310958..000000000 --- a/lib/app-scened-template/scene_controller.hpp +++ /dev/null @@ -1,246 +0,0 @@ -#include -#include -#include - -#define GENERIC_SCENE_ENUM_VALUES Exit, Start -#define GENERIC_EVENT_ENUM_VALUES Tick, Back - -/** - * @brief Controller for scene navigation in application - * - * @tparam TScene generic scene class - * @tparam TApp application class - */ -template -class SceneController { -public: - /** - * @brief Add scene to scene container - * - * @param scene_index scene index - * @param scene_pointer scene object pointer - */ - void add_scene(typename TApp::SceneType scene_index, TScene* scene_pointer) { - furi_check(scenes.count(scene_index) == 0); - scenes[scene_index] = scene_pointer; - } - - /** - * @brief Switch to next scene and store current scene in previous scenes list - * - * @param scene_index next scene index - * @param need_restore true, if we want the scene to restore its parameters - */ - void switch_to_next_scene(typename TApp::SceneType scene_index, bool need_restore = false) { - previous_scenes_list.push_front(current_scene_index); - switch_to_scene(scene_index, need_restore); - } - - /** - * @brief Switch to next scene without ability to return to current scene - * - * @param scene_index next scene index - * @param need_restore true, if we want the scene to restore its parameters - */ - void switch_to_scene(typename TApp::SceneType scene_index, bool need_restore = false) { - if(scene_index != TApp::SceneType::Exit) { - scenes[current_scene_index]->on_exit(app); - current_scene_index = scene_index; - scenes[current_scene_index]->on_enter(app, need_restore); - } - } - - /** - * @brief Search the scene in the list of previous scenes and switch to it - * - * @param scene_index_list list of scene indexes to which you want to switch - */ - bool search_and_switch_to_previous_scene( - const std::initializer_list& scene_index_list) { - auto previous_scene_index = TApp::SceneType::Exit; - bool scene_found = false; - bool result = false; - - while(!scene_found) { - previous_scene_index = get_previous_scene_index(); - for(const auto& element : scene_index_list) { - if(previous_scene_index == element) { - scene_found = true; - result = true; - break; - } - - if(previous_scene_index == TApp::SceneType::Exit) { - scene_found = true; - break; - } - } - } - - if(result) { - switch_to_scene(previous_scene_index, true); - } - - return result; - } - - bool search_and_switch_to_another_scene( - const std::initializer_list& scene_index_list, - typename TApp::SceneType scene_index) { - auto previous_scene_index = TApp::SceneType::Exit; - bool scene_found = false; - bool result = false; - - while(!scene_found) { - previous_scene_index = get_previous_scene_index(); - for(const auto& element : scene_index_list) { - if(previous_scene_index == element) { - scene_found = true; - result = true; - break; - } - - if(previous_scene_index == TApp::SceneType::Exit) { - scene_found = true; - break; - } - } - } - - if(result) { - switch_to_scene(scene_index, true); - } - - return result; - } - - bool has_previous_scene( - const std::initializer_list& scene_index_list) { - bool result = false; - - for(auto const& previous_element : previous_scenes_list) { - for(const auto& element : scene_index_list) { - if(previous_element == element) { - result = true; - break; - } - - if(previous_element == TApp::SceneType::Exit) { - break; - } - } - - if(result) break; - } - - return result; - } - - /** - * @brief Start application main cycle - * - * @param tick_length_ms tick event length in milliseconds - */ - void process( - uint32_t /* tick_length_ms */ = 100, - typename TApp::SceneType start_scene_index = TApp::SceneType::Start) { - typename TApp::Event event; - bool consumed; - bool exit = false; - - current_scene_index = start_scene_index; - scenes[current_scene_index]->on_enter(app, false); - - while(!exit) { - app->view_controller.receive_event(&event); - - consumed = scenes[current_scene_index]->on_event(app, &event); - - if(!consumed) { - if(event.type == TApp::EventType::Back) { - exit = switch_to_previous_scene(); - } - } - }; - - scenes[current_scene_index]->on_exit(app); - } - - /** - * @brief Switch to previous scene - * - * @param count how many steps back - * @return true if app need to exit - */ - bool switch_to_previous_scene(uint8_t count = 1) { - auto previous_scene_index = TApp::SceneType::Start; - - for(uint8_t i = 0; i < count; i++) - previous_scene_index = get_previous_scene_index(); - - if(previous_scene_index == TApp::SceneType::Exit) return true; - - switch_to_scene(previous_scene_index, true); - return false; - } - - /** - * @brief Construct a new Scene Controller object - * - * @param app_pointer pointer to application class - */ - SceneController(TApp* app_pointer) { - app = app_pointer; - current_scene_index = TApp::SceneType::Exit; - } - - /** - * @brief Destroy the Scene Controller object - * - */ - ~SceneController() { - for(auto& it : scenes) - delete it.second; - } - -private: - /** - * @brief Scenes pointers container - * - */ - std::map scenes; - - /** - * @brief List of indexes of previous scenes - * - */ - std::forward_list previous_scenes_list; - - /** - * @brief Current scene index holder - * - */ - typename TApp::SceneType current_scene_index; - - /** - * @brief Application pointer holder - * - */ - TApp* app; - - /** - * @brief Get the previous scene index - * - * @return previous scene index - */ - typename TApp::SceneType get_previous_scene_index() { - auto scene_index = TApp::SceneType::Exit; - - if(!previous_scenes_list.empty()) { - scene_index = previous_scenes_list.front(); - previous_scenes_list.pop_front(); - } - - return scene_index; - } -}; diff --git a/lib/app-scened-template/text_store.cpp b/lib/app-scened-template/text_store.cpp deleted file mode 100644 index c81a2c4e7..000000000 --- a/lib/app-scened-template/text_store.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "text_store.h" -#include - -TextStore::TextStore(uint8_t _text_size) - : text_size(_text_size) { - text = static_cast(malloc(text_size + 1)); -} - -TextStore::~TextStore() { - free(text); -} - -void TextStore::set(const char* _text...) { - va_list args; - va_start(args, _text); - vsnprintf(text, text_size, _text, args); - va_end(args); -} diff --git a/lib/app-scened-template/text_store.h b/lib/app-scened-template/text_store.h deleted file mode 100644 index 3fe58ed1d..000000000 --- a/lib/app-scened-template/text_store.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -class TextStore { -public: - TextStore(uint8_t text_size); - ~TextStore(void); - - void set(const char* text...); - const uint8_t text_size; - char* text; -}; diff --git a/lib/app-scened-template/typeindex_no_rtti.hpp b/lib/app-scened-template/typeindex_no_rtti.hpp deleted file mode 100644 index 579a0189d..000000000 --- a/lib/app-scened-template/typeindex_no_rtti.hpp +++ /dev/null @@ -1,129 +0,0 @@ -/* - * type_index without RTTI - * - * Copyright frickiericker 2016. - * Distributed under the Boost Software License, Version 1.0. - * - * Permission is hereby granted, free of charge, to any person or organization - * obtaining a copy of the software and accompanying documentation covered by - * this license (the "Software") to use, reproduce, display, distribute, - * execute, and transmit the Software, and to prepare derivative works of the - * Software, and to permit third-parties to whom the Software is furnished to - * do so, all subject to the following: - * - * The copyright notices in the Software and this entire statement, including - * the above license grant, this restriction and the following disclaimer, - * must be included in all copies of the Software, in whole or in part, and - * all derivative works of the Software, unless such copies or derivative - * works are solely in the form of machine-executable object code generated by - * a source language processor. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT - * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE - * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -#include - -namespace ext { -/** - * Dummy type for tag-dispatching. - */ -template -struct tag_type {}; - -/** - * A value of tag_type. - */ -template -constexpr tag_type tag{}; - -/** - * A type_index implementation without RTTI. - */ -struct type_index { - /** - * Creates a type_index object for the specified type. - */ - template - type_index(tag_type) noexcept - : hash_code_{index} { - } - - /** - * Returns the hash code. - */ - std::size_t hash_code() const noexcept { - return hash_code_; - } - -private: - /** - * Unique integral index associated to template type argument. - */ - template - static std::size_t const index; - - /** - * Global counter for generating index values. - */ - static std::size_t& counter() noexcept { - static std::size_t counter_; - return counter_; - } - -private: - std::size_t hash_code_; -}; - -template -std::size_t const type_index::index = type_index::counter()++; - -/** - * Creates a type_index object for the specified type. - * - * Equivalent to `ext::type_index{ext::tag}`. - */ -template -type_index make_type_index() noexcept { - return tag; -} - -inline bool operator==(type_index const& a, type_index const& b) noexcept { - return a.hash_code() == b.hash_code(); -} - -inline bool operator!=(type_index const& a, type_index const& b) noexcept { - return !(a == b); -} - -inline bool operator<(type_index const& a, type_index const& b) noexcept { - return a.hash_code() < b.hash_code(); -} - -inline bool operator<=(type_index const& a, type_index const& b) noexcept { - return a.hash_code() <= b.hash_code(); -} - -inline bool operator>(type_index const& a, type_index const& b) noexcept { - return !(a <= b); -} - -inline bool operator>=(type_index const& a, type_index const& b) noexcept { - return !(a < b); -} -} - -template <> -struct std::hash { - using argument_type = ext::type_index; - using result_type = std::size_t; - - result_type operator()(argument_type const& t) const noexcept { - return t.hash_code(); - } -}; diff --git a/lib/app-scened-template/view_controller.hpp b/lib/app-scened-template/view_controller.hpp deleted file mode 100644 index ccd3c0fd3..000000000 --- a/lib/app-scened-template/view_controller.hpp +++ /dev/null @@ -1,170 +0,0 @@ -#pragma once -#include "view_modules/generic_view_module.h" -#include -#include -#include -#include -#include "typeindex_no_rtti.hpp" - -/** - * @brief Controller for switching application views and handling inputs and events - * - * @tparam TApp application class - * @tparam TViewModules variadic list of ViewModules - */ -template -class ViewController { -public: - ViewController() { - event_queue = furi_message_queue_alloc(10, sizeof(typename TApp::Event)); - - view_dispatcher = view_dispatcher_alloc(); - previous_view_callback_pointer = cbc::obtain_connector( - this, &ViewController::previous_view_callback); - - [](...) { - }((this->add_view(ext::make_type_index().hash_code(), new TViewModules()), - 0)...); - - gui = static_cast(furi_record_open("gui")); - } - - ~ViewController() { - for(auto& it : holder) { - view_dispatcher_remove_view(view_dispatcher, static_cast(it.first)); - delete it.second; - } - - view_dispatcher_free(view_dispatcher); - furi_message_queue_free(event_queue); - } - - /** - * @brief Get ViewModule pointer - * - * @tparam T Concrete ViewModule class - * @return T* ViewModule pointer - */ - template - T* get() { - uint32_t view_index = ext::make_type_index().hash_code(); - furi_check(holder.count(view_index) != 0); - return static_cast(holder[view_index]); - } - - /** - * @brief Get ViewModule pointer by cast - * - * @tparam T Concrete ViewModule class - * @return T* ViewModule pointer - */ - template - operator T*() { - uint32_t view_index = ext::make_type_index().hash_code(); - furi_check(holder.count(view_index) != 0); - return static_cast(holder[view_index]); - } - - /** - * @brief Switch view to ViewModule - * - * @tparam T Concrete ViewModule class - * @return T* ViewModule pointer - */ - template - void switch_to() { - uint32_t view_index = ext::make_type_index().hash_code(); - furi_check(holder.count(view_index) != 0); - view_dispatcher_switch_to_view(view_dispatcher, view_index); - } - - /** - * @brief Receive event from app event queue - * - * @param event event pointer - */ - void receive_event(typename TApp::Event* event) { - if(furi_message_queue_get(event_queue, event, 100) != FuriStatusOk) { - event->type = TApp::EventType::Tick; - } - } - - /** - * @brief Send event to app event queue - * - * @param event event pointer - */ - void send_event(typename TApp::Event* event) { - FuriStatus result = furi_message_queue_put(event_queue, event, FuriWaitForever); - furi_check(result == FuriStatusOk); - } - - void attach_to_gui(ViewDispatcherType type) { - view_dispatcher_attach_to_gui(view_dispatcher, gui, type); - } - -private: - /** - * @brief ViewModulesHolder - * - */ - std::map holder; - - /** - * @brief App event queue - * - */ - FuriMessageQueue* event_queue; - - /** - * @brief Main ViewDispatcher pointer - * - */ - ViewDispatcher* view_dispatcher; - - /** - * @brief Gui record pointer - * - */ - Gui* gui; - - /** - * @brief Previous view callback fn pointer - * - */ - ViewNavigationCallback previous_view_callback_pointer; - - /** - * @brief Previous view callback fn - * - * @param context not used - * @return uint32_t VIEW_IGNORE - */ - uint32_t previous_view_callback(void* context) { - (void)context; - - typename TApp::Event event; - event.type = TApp::EventType::Back; - - if(event_queue != NULL) { - send_event(&event); - } - - return VIEW_IGNORE; - } - - /** - * @brief Add ViewModule to holder - * - * @param view_index view index in holder - * @param view_module view module pointer - */ - void add_view(size_t view_index, GenericViewModule* view_module) { - furi_check(holder.count(view_index) == 0); - holder[view_index] = view_module; - - View* view = view_module->get_view(); - view_dispatcher_add_view(view_dispatcher, static_cast(view_index), view); - view_set_previous_callback(view, previous_view_callback_pointer); - } -}; diff --git a/lib/app-scened-template/view_modules/byte_input_vm.cpp b/lib/app-scened-template/view_modules/byte_input_vm.cpp deleted file mode 100644 index 754de9111..000000000 --- a/lib/app-scened-template/view_modules/byte_input_vm.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "byte_input_vm.h" - -ByteInputVM::ByteInputVM() { - byte_input = byte_input_alloc(); -} - -ByteInputVM::~ByteInputVM() { - byte_input_free(byte_input); -} - -View* ByteInputVM::get_view() { - return byte_input_get_view(byte_input); -} - -void ByteInputVM::clean() { - byte_input_set_header_text(byte_input, ""); - byte_input_set_result_callback(byte_input, NULL, NULL, NULL, NULL, 0); -} - -void ByteInputVM::set_result_callback( - ByteInputCallback input_callback, - ByteChangedCallback changed_callback, - void* callback_context, - uint8_t* bytes, - uint8_t bytes_count) { - byte_input_set_result_callback( - byte_input, input_callback, changed_callback, callback_context, bytes, bytes_count); -} - -void ByteInputVM::set_header_text(const char* text) { - byte_input_set_header_text(byte_input, text); -} diff --git a/lib/app-scened-template/view_modules/byte_input_vm.h b/lib/app-scened-template/view_modules/byte_input_vm.h deleted file mode 100644 index 69031fbee..000000000 --- a/lib/app-scened-template/view_modules/byte_input_vm.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include "generic_view_module.h" -#include - -class ByteInputVM : public GenericViewModule { -public: - ByteInputVM(void); - ~ByteInputVM() final; - View* get_view() final; - void clean() final; - - /** - * @brief Set byte input result callback - * - * @param input_callback input callback fn - * @param changed_callback changed callback fn - * @param callback_context callback context - * @param bytes buffer to use - * @param bytes_count buffer length - */ - void set_result_callback( - ByteInputCallback input_callback, - ByteChangedCallback changed_callback, - void* callback_context, - uint8_t* bytes, - uint8_t bytes_count); - - /** - * @brief Set byte input header text - * - * @param text text to be shown - */ - void set_header_text(const char* text); - -private: - ByteInput* byte_input; -}; diff --git a/lib/app-scened-template/view_modules/dialog_ex_vm.cpp b/lib/app-scened-template/view_modules/dialog_ex_vm.cpp deleted file mode 100644 index 34f4d0336..000000000 --- a/lib/app-scened-template/view_modules/dialog_ex_vm.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "dialog_ex_vm.h" - -DialogExVM::DialogExVM() { - dialog_ex = dialog_ex_alloc(); -} - -DialogExVM::~DialogExVM() { - dialog_ex_free(dialog_ex); -} - -View* DialogExVM::get_view() { - return dialog_ex_get_view(dialog_ex); -} - -void DialogExVM::clean() { - set_result_callback(NULL); - set_context(NULL); - set_header(NULL, 0, 0, AlignLeft, AlignBottom); - set_text(NULL, 0, 0, AlignLeft, AlignBottom); - set_icon(0, 0, NULL); - set_left_button_text(NULL); - set_center_button_text(NULL); - set_right_button_text(NULL); -} - -void DialogExVM::set_result_callback(DialogExResultCallback callback) { - dialog_ex_set_result_callback(dialog_ex, callback); -} - -void DialogExVM::set_context(void* context) { - dialog_ex_set_context(dialog_ex, context); -} - -void DialogExVM::set_header( - const char* text, - uint8_t x, - uint8_t y, - Align horizontal, - Align vertical) { - dialog_ex_set_header(dialog_ex, text, x, y, horizontal, vertical); -} - -void DialogExVM::set_text(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical) { - dialog_ex_set_text(dialog_ex, text, x, y, horizontal, vertical); -} - -void DialogExVM::set_icon(uint8_t x, uint8_t y, const Icon* icon) { - dialog_ex_set_icon(dialog_ex, x, y, icon); -} - -void DialogExVM::set_left_button_text(const char* text) { - dialog_ex_set_left_button_text(dialog_ex, text); -} - -void DialogExVM::set_center_button_text(const char* text) { - dialog_ex_set_center_button_text(dialog_ex, text); -} - -void DialogExVM::set_right_button_text(const char* text) { - dialog_ex_set_right_button_text(dialog_ex, text); -} diff --git a/lib/app-scened-template/view_modules/dialog_ex_vm.h b/lib/app-scened-template/view_modules/dialog_ex_vm.h deleted file mode 100644 index cb63ccdbc..000000000 --- a/lib/app-scened-template/view_modules/dialog_ex_vm.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once -#include "generic_view_module.h" -#include - -class DialogExVM : public GenericViewModule { -public: - DialogExVM(void); - ~DialogExVM() final; - View* get_view() final; - void clean() final; - - /** - * Set dialog result callback - * @param callback - result callback function - */ - void set_result_callback(DialogExResultCallback callback); - - /** - * Set dialog context - * @param context - context pointer, will be passed to result callback - */ - void set_context(void* context); - - /** - * Set dialog header text - * If text is null, dialog header will not be rendered - * @param text - text to be shown, can be multiline - * @param x, y - text position - * @param horizontal, vertical - text aligment - */ - void set_header(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical); - - /** - * Set dialog text - * If text is null, dialog text will not be rendered - * @param text - text to be shown, can be multiline - * @param x, y - text position - * @param horizontal, vertical - text aligment - */ - void set_text(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical); - - /** - * Set dialog icon - * If x or y is negative, dialog icon will not be rendered - * @param x, y - icon position - * @param name - icon to be shown - */ - void set_icon(uint8_t x, uint8_t y, const Icon* icon); - - /** - * Set left button text - * If text is null, left button will not be rendered and processed - * @param text - text to be shown - */ - void set_left_button_text(const char* text); - - /** - * Set center button text - * If text is null, center button will not be rendered and processed - * @param text - text to be shown - */ - void set_center_button_text(const char* text); - - /** - * Set right button text - * If text is null, right button will not be rendered and processed - * @param text - text to be shown - */ - void set_right_button_text(const char* text); - -private: - DialogEx* dialog_ex; -}; diff --git a/lib/app-scened-template/view_modules/generic_view_module.h b/lib/app-scened-template/view_modules/generic_view_module.h deleted file mode 100644 index f6c56a911..000000000 --- a/lib/app-scened-template/view_modules/generic_view_module.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include - -class GenericViewModule { -public: - GenericViewModule() {}; - virtual ~GenericViewModule() {}; - virtual View* get_view() = 0; - virtual void clean() = 0; -}; diff --git a/lib/app-scened-template/view_modules/popup_vm.cpp b/lib/app-scened-template/view_modules/popup_vm.cpp deleted file mode 100644 index 330aa44ca..000000000 --- a/lib/app-scened-template/view_modules/popup_vm.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "popup_vm.h" -#include - -PopupVM::PopupVM() { - popup = popup_alloc(); -} - -PopupVM::~PopupVM() { - popup_free(popup); -} - -View* PopupVM::get_view() { - return popup_get_view(popup); -} - -void PopupVM::clean() { - set_callback(NULL); - set_context(NULL); - set_header(NULL, 0, 0, AlignLeft, AlignBottom); - set_text(NULL, 0, 0, AlignLeft, AlignBottom); - set_icon(0, 0, NULL); - disable_timeout(); - set_timeout(1000); -} - -void PopupVM::set_callback(PopupCallback callback) { - popup_set_callback(popup, callback); -} - -void PopupVM::set_context(void* context) { - popup_set_context(popup, context); -} - -void PopupVM::set_header(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical) { - popup_set_header(popup, text, x, y, horizontal, vertical); -} - -void PopupVM::set_text(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical) { - popup_set_text(popup, text, x, y, horizontal, vertical); -} - -void PopupVM::set_icon(int8_t x, int8_t y, const Icon* icon) { - popup_set_icon(popup, x, y, icon); -} - -void PopupVM::set_timeout(uint32_t timeout_in_ms) { - popup_set_timeout(popup, timeout_in_ms); -} - -void PopupVM::enable_timeout() { - popup_enable_timeout(popup); -} - -void PopupVM::disable_timeout() { - popup_disable_timeout(popup); -} diff --git a/lib/app-scened-template/view_modules/popup_vm.h b/lib/app-scened-template/view_modules/popup_vm.h deleted file mode 100644 index 234f33774..000000000 --- a/lib/app-scened-template/view_modules/popup_vm.h +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once -#include "generic_view_module.h" -#include - -class PopupVM : public GenericViewModule { -public: - PopupVM(void); - ~PopupVM() final; - View* get_view() final; - void clean() final; - - /** - * Set popup header text - * @param text - text to be shown - */ - void set_callback(PopupCallback callback); - - /** - * Set popup context - * @param context - context pointer, will be passed to result callback - */ - void set_context(void* context); - - /** - * Set popup header text - * If text is null, popup header will not be rendered - * @param text - text to be shown, can be multiline - * @param x, y - text position - * @param horizontal, vertical - text aligment - */ - void set_header(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical); - - /** - * Set popup text - * If text is null, popup text will not be rendered - * @param text - text to be shown, can be multiline - * @param x, y - text position - * @param horizontal, vertical - text aligment - */ - void set_text(const char* text, uint8_t x, uint8_t y, Align horizontal, Align vertical); - - /** - * Set popup icon - * If icon position is negative, popup icon will not be rendered - * @param x, y - icon position - * @param name - icon to be shown - */ - void set_icon(int8_t x, int8_t y, const Icon* icon); - - /** - * Set popup timeout - * @param timeout_in_ms - popup timeout value in milliseconds - */ - void set_timeout(uint32_t timeout_in_ms); - - /** - * Enable popup timeout - */ - void enable_timeout(void); - - /** - * Disable popup timeout - */ - void disable_timeout(void); - -private: - Popup* popup; -}; diff --git a/lib/app-scened-template/view_modules/submenu_vm.cpp b/lib/app-scened-template/view_modules/submenu_vm.cpp deleted file mode 100644 index 939bb6b1c..000000000 --- a/lib/app-scened-template/view_modules/submenu_vm.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "submenu_vm.h" - -SubmenuVM::SubmenuVM() { - submenu = submenu_alloc(); -} - -SubmenuVM::~SubmenuVM() { - submenu_free(submenu); -} - -View* SubmenuVM::get_view() { - return submenu_get_view(submenu); -} - -void SubmenuVM::clean() { - submenu_reset(submenu); -} - -void SubmenuVM::add_item( - const char* label, - uint32_t index, - SubmenuItemCallback callback, - void* callback_context) { - submenu_add_item(submenu, label, index, callback, callback_context); -} - -void SubmenuVM::set_selected_item(uint32_t index) { - submenu_set_selected_item(submenu, index); -} - -void SubmenuVM::set_header(const char* header) { - submenu_set_header(submenu, header); -} diff --git a/lib/app-scened-template/view_modules/submenu_vm.h b/lib/app-scened-template/view_modules/submenu_vm.h deleted file mode 100644 index 223fbd531..000000000 --- a/lib/app-scened-template/view_modules/submenu_vm.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "generic_view_module.h" -#include - -class SubmenuVM : public GenericViewModule { -public: - SubmenuVM(void); - ~SubmenuVM() final; - View* get_view() final; - void clean() final; - - /** - * @brief Add item to submenu - * - * @param label - menu item label - * @param index - menu item index, used for callback, may be the same with other items - * @param callback - menu item callback - * @param callback_context - menu item callback context - */ - void add_item( - const char* label, - uint32_t index, - SubmenuItemCallback callback, - void* callback_context); - - /** - * @brief Set submenu item selector - * - * @param index index of the item to be selected - */ - void set_selected_item(uint32_t index); - - /** - * @brief Set optional header for submenu - * - * @param header header to set - */ - void set_header(const char* header); - -private: - Submenu* submenu; -}; diff --git a/lib/app-scened-template/view_modules/text_input_vm.cpp b/lib/app-scened-template/view_modules/text_input_vm.cpp deleted file mode 100644 index 05e5ed1d6..000000000 --- a/lib/app-scened-template/view_modules/text_input_vm.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "text_input_vm.h" - -TextInputVM::TextInputVM() { - text_input = text_input_alloc(); -} - -TextInputVM::~TextInputVM() { - text_input_free(text_input); -} - -View* TextInputVM::get_view() { - return text_input_get_view(text_input); -} - -void TextInputVM::clean() { - text_input_reset(text_input); -} - -void TextInputVM::set_result_callback( - TextInputCallback callback, - void* callback_context, - char* text, - uint8_t max_text_length, - bool clear_default_text) { - text_input_set_result_callback( - text_input, callback, callback_context, text, max_text_length, clear_default_text); -} - -void TextInputVM::set_header_text(const char* text) { - text_input_set_header_text(text_input, text); -} - -void TextInputVM::set_validator(TextInputValidatorCallback callback, void* callback_context) { - text_input_set_validator(text_input, callback, callback_context); -} - -void* TextInputVM::get_validator_callback_context() { - return text_input_get_validator_callback_context(text_input); -} diff --git a/lib/app-scened-template/view_modules/text_input_vm.h b/lib/app-scened-template/view_modules/text_input_vm.h deleted file mode 100644 index 5c71c4318..000000000 --- a/lib/app-scened-template/view_modules/text_input_vm.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "generic_view_module.h" -#include - -class TextInputVM : public GenericViewModule { -public: - TextInputVM(void); - ~TextInputVM() final; - View* get_view() final; - void clean() final; - - /** - * @brief Set text input result callback - * - * @param callback - callback fn - * @param callback_context - callback context - * @param text - text buffer to use - * @param max_text_length - text buffer length - * @param clear_default_text - clears given buffer on OK event - */ - void set_result_callback( - TextInputCallback callback, - void* callback_context, - char* text, - uint8_t max_text_length, - bool clear_default_text); - - /** - * @brief Set text input header text - * - * @param text - text to be shown - */ - void set_header_text(const char* text); - - void set_validator(TextInputValidatorCallback callback, void* callback_context); - - void* get_validator_callback_context(void); - -private: - TextInput* text_input; -}; diff --git a/lib/appframe.scons b/lib/appframe.scons deleted file mode 100644 index fb268579d..000000000 --- a/lib/appframe.scons +++ /dev/null @@ -1,29 +0,0 @@ -Import("env") - -env.Append( - CPPPATH=[ - "#/lib/app-scened-template", - "#/lib/callback-connector", - ], - LINT_SOURCES=[ - Dir("app-scened-template"), - ], -) - - -libenv = env.Clone(FW_LIB_NAME="appframe") -libenv.ApplyLibFlags() - -sources = [] - -recurse_dirs = [ - "app-scened-template", - "callback-connector", -] - -for recurse_dir in recurse_dirs: - sources += libenv.GlobRecursive("*.c*", recurse_dir) - -lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources) -libenv.Install("${LIB_DIST_DIR}", lib) -Return("lib") diff --git a/lib/drivers/st25r3916.c b/lib/drivers/st25r3916.c index 477261213..f8dc9a5eb 100644 --- a/lib/drivers/st25r3916.c +++ b/lib/drivers/st25r3916.c @@ -57,9 +57,12 @@ bool st25r3916_read_fifo( do { uint8_t fifo_status[2] = {}; st25r3916_read_burst_regs(handle, ST25R3916_REG_FIFO_STATUS1, fifo_status, 2); - size_t bytes = ((fifo_status[1] & ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >> - ST25R3916_REG_FIFO_STATUS2_fifo_b_shift) | - fifo_status[0]; + + uint16_t fifo_status_b9_b8 = + ((fifo_status[1] & ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >> + ST25R3916_REG_FIFO_STATUS2_fifo_b_shift); + size_t bytes = (fifo_status_b9_b8 << 8) | fifo_status[0]; + uint8_t bits = ((fifo_status[1] & ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) >> ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift); diff --git a/lib/subghz/protocols/dickert_mahs.c b/lib/subghz/protocols/dickert_mahs.c new file mode 100644 index 000000000..4691e3423 --- /dev/null +++ b/lib/subghz/protocols/dickert_mahs.c @@ -0,0 +1,385 @@ +#include "dickert_mahs.h" + +#include "../blocks/const.h" +#include "../blocks/decoder.h" +#include "../blocks/encoder.h" +#include "../blocks/generic.h" +#include "../blocks/math.h" + +#include +#include +#include + +#define TAG "SubGhzProtocolDicketMAHS" + +static const SubGhzBlockConst subghz_protocol_dickert_mahs_const = { + .te_short = 400, + .te_long = 800, + .te_delta = 100, + .min_count_bit_for_found = 36, +}; + +struct SubGhzProtocolDecoderDickertMAHS { + SubGhzProtocolDecoderBase base; + + SubGhzBlockDecoder decoder; + SubGhzBlockGeneric generic; + + uint32_t tmp[2]; + uint8_t tmp_cnt; +}; + +struct SubGhzProtocolEncoderDickertMAHS { + SubGhzProtocolEncoderBase base; + + SubGhzProtocolBlockEncoder encoder; + SubGhzBlockGeneric generic; +}; + +typedef enum { + DickertMAHSDecoderStepReset = 0, + DickertMAHSDecoderStepInitial, + DickertMAHSDecoderStepRecording, +} DickertMAHSDecoderStep; + +const SubGhzProtocolDecoder subghz_protocol_dickert_mahs_decoder = { + .alloc = subghz_protocol_decoder_dickert_mahs_alloc, + .free = subghz_protocol_decoder_dickert_mahs_free, + + .feed = subghz_protocol_decoder_dickert_mahs_feed, + .reset = subghz_protocol_decoder_dickert_mahs_reset, + + .get_hash_data = subghz_protocol_decoder_dickert_mahs_get_hash_data, + .serialize = subghz_protocol_decoder_dickert_mahs_serialize, + .deserialize = subghz_protocol_decoder_dickert_mahs_deserialize, + .get_string = subghz_protocol_decoder_dickert_mahs_get_string, +}; + +const SubGhzProtocolEncoder subghz_protocol_dickert_mahs_encoder = { + .alloc = subghz_protocol_encoder_dickert_mahs_alloc, + .free = subghz_protocol_encoder_dickert_mahs_free, + + .deserialize = subghz_protocol_encoder_dickert_mahs_deserialize, + .stop = subghz_protocol_encoder_dickert_mahs_stop, + .yield = subghz_protocol_encoder_dickert_mahs_yield, +}; + +const SubGhzProtocol subghz_protocol_dickert_mahs = { + .name = SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME, + .type = SubGhzProtocolTypeStatic, + .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | + SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send, + + .decoder = &subghz_protocol_dickert_mahs_decoder, + .encoder = &subghz_protocol_dickert_mahs_encoder, +}; + +static void subghz_protocol_encoder_dickert_mahs_parse_buffer( + SubGhzProtocolDecoderDickertMAHS* instance, + FuriString* output) { + // We assume we have only decodes < 64 bit! + uint64_t data = instance->generic.data; + uint8_t bits[36] = {}; + + // Convert uint64_t into bit array + for(int i = 35; i >= 0; i--) { + if(data & 1) { + bits[i] = 1; + } + data >>= 1; + } + + // Decode symbols + FuriString* code = furi_string_alloc(); + for(size_t i = 0; i < 35; i += 2) { + uint8_t dip = (bits[i] << 1) + bits[i + 1]; + // PLUS = 3, // 0b11 + // ZERO = 1, // 0b01 + // MINUS = 0, // 0x00 + if(dip == 0x01) { + furi_string_cat(code, "0"); + } else if(dip == 0x00) { + furi_string_cat(code, "-"); + } else if(dip == 0x03) { + furi_string_cat(code, "+"); + } else { + furi_string_cat(code, "?"); + } + } + + FuriString* user_dips = furi_string_alloc(); + FuriString* fact_dips = furi_string_alloc(); + furi_string_set_n(user_dips, code, 0, 10); + furi_string_set_n(fact_dips, code, 10, 8); + + furi_string_cat_printf( + output, + "%s\r\n" + "User-Dips:\t%s\r\n" + "Fac-Code:\t%s\r\n", + instance->generic.protocol_name, + furi_string_get_cstr(user_dips), + furi_string_get_cstr(fact_dips)); + furi_string_free(user_dips); + furi_string_free(fact_dips); + furi_string_free(code); +} + +void* subghz_protocol_encoder_dickert_mahs_alloc(SubGhzEnvironment* environment) { + UNUSED(environment); + SubGhzProtocolEncoderDickertMAHS* instance = malloc(sizeof(SubGhzProtocolEncoderDickertMAHS)); + + instance->base.protocol = &subghz_protocol_dickert_mahs; + instance->generic.protocol_name = instance->base.protocol->name; + + instance->encoder.repeat = 10; + instance->encoder.size_upload = 128; + instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration)); + instance->encoder.is_running = false; + return instance; +} + +void subghz_protocol_encoder_dickert_mahs_free(void* context) { + furi_assert(context); + SubGhzProtocolEncoderDickertMAHS* instance = context; + free(instance->encoder.upload); + free(instance); +} + +/** + * Generating an upload from data. + * @param instance Pointer to a SubGhzProtocolEncoderDickertMAHS instance + * @return true On success + */ +static bool + subghz_protocol_encoder_dickert_mahs_get_upload(SubGhzProtocolEncoderDickertMAHS* instance) { + furi_assert(instance); + size_t index = 0; + size_t size_upload = (instance->generic.data_count_bit * 2) + 2; + if(size_upload > instance->encoder.size_upload) { + FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer."); + return false; + } else { + instance->encoder.size_upload = size_upload; + } + + instance->encoder.upload[index++] = + level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_short * 112); + // Send start bit + instance->encoder.upload[index++] = + level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_short); + + //Send key data + for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) { + if(bit_read(instance->generic.data, i - 1)) { + //send bit 1 + instance->encoder.upload[index++] = + level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_long); + instance->encoder.upload[index++] = + level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_short); + } else { + //send bit 0 + instance->encoder.upload[index++] = + level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_short); + instance->encoder.upload[index++] = + level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_long); + } + } + + return true; +} + +SubGhzProtocolStatus + subghz_protocol_encoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format) { + furi_assert(context); + SubGhzProtocolEncoderDickertMAHS* instance = context; + SubGhzProtocolStatus ret = SubGhzProtocolStatusError; + do { + ret = subghz_block_generic_deserialize(&instance->generic, flipper_format); + if(ret != SubGhzProtocolStatusOk) { + break; + } + + // Allow for longer keys (<) instead of != + if((instance->generic.data_count_bit < + subghz_protocol_dickert_mahs_const.min_count_bit_for_found)) { + FURI_LOG_E(TAG, "Wrong number of bits in key"); + ret = SubGhzProtocolStatusErrorValueBitCount; + break; + } + //optional parameter parameter + flipper_format_read_uint32( + flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1); + + if(!subghz_protocol_encoder_dickert_mahs_get_upload(instance)) { + ret = SubGhzProtocolStatusErrorEncoderGetUpload; + break; + } + instance->encoder.is_running = true; + } while(false); + + return ret; +} + +void subghz_protocol_encoder_dickert_mahs_stop(void* context) { + SubGhzProtocolEncoderDickertMAHS* instance = context; + instance->encoder.is_running = false; +} + +LevelDuration subghz_protocol_encoder_dickert_mahs_yield(void* context) { + SubGhzProtocolEncoderDickertMAHS* instance = context; + + if(instance->encoder.repeat == 0 || !instance->encoder.is_running) { + instance->encoder.is_running = false; + return level_duration_reset(); + } + + LevelDuration ret = instance->encoder.upload[instance->encoder.front]; + + if(++instance->encoder.front == instance->encoder.size_upload) { + instance->encoder.repeat--; + instance->encoder.front = 0; + } + + return ret; +} + +void* subghz_protocol_decoder_dickert_mahs_alloc(SubGhzEnvironment* environment) { + UNUSED(environment); + SubGhzProtocolDecoderDickertMAHS* instance = malloc(sizeof(SubGhzProtocolDecoderDickertMAHS)); + instance->base.protocol = &subghz_protocol_dickert_mahs; + instance->generic.protocol_name = instance->base.protocol->name; + instance->tmp_cnt = 0; + + return instance; +} + +void subghz_protocol_decoder_dickert_mahs_free(void* context) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + free(instance); +} + +void subghz_protocol_decoder_dickert_mahs_reset(void* context) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + instance->decoder.parser_step = DickertMAHSDecoderStepReset; +} + +void subghz_protocol_decoder_dickert_mahs_feed(void* context, bool level, uint32_t duration) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + + switch(instance->decoder.parser_step) { + case DickertMAHSDecoderStepReset: + // Check if done + if(instance->decoder.decode_count_bit >= + subghz_protocol_dickert_mahs_const.min_count_bit_for_found) { + instance->generic.serial = 0x0; + instance->generic.btn = 0x0; + + instance->generic.data = instance->decoder.decode_data; + instance->generic.data_count_bit = instance->decoder.decode_count_bit; + + if(instance->base.callback) + instance->base.callback(&instance->base, instance->base.context); + + instance->decoder.decode_data = 0; + instance->decoder.decode_count_bit = 0; + } + + if((!level) && (duration > 10 * subghz_protocol_dickert_mahs_const.te_short)) { + //Found header DICKERT_MAHS + instance->decoder.parser_step = DickertMAHSDecoderStepInitial; + } + break; + case DickertMAHSDecoderStepInitial: + if(!level) { + break; + } else if( + DURATION_DIFF(duration, subghz_protocol_dickert_mahs_const.te_short) < + subghz_protocol_dickert_mahs_const.te_delta) { + //Found start bit DICKERT_MAHS + instance->decoder.parser_step = DickertMAHSDecoderStepRecording; + instance->decoder.decode_data = 0; + instance->decoder.decode_count_bit = 0; + } else { + instance->decoder.parser_step = DickertMAHSDecoderStepReset; + } + break; + case DickertMAHSDecoderStepRecording: + if((!level && instance->tmp_cnt == 0) || (level && instance->tmp_cnt == 1)) { + instance->tmp[instance->tmp_cnt] = duration; + + instance->tmp_cnt++; + + if(instance->tmp_cnt == 2) { + if(DURATION_DIFF(instance->tmp[0] + instance->tmp[1], 1200) < + subghz_protocol_dickert_mahs_const.te_delta) { + if(DURATION_DIFF(instance->tmp[0], subghz_protocol_dickert_mahs_const.te_long) < + subghz_protocol_dickert_mahs_const.te_delta) { + subghz_protocol_blocks_add_bit(&instance->decoder, 1); + } else if( + DURATION_DIFF( + instance->tmp[0], subghz_protocol_dickert_mahs_const.te_short) < + subghz_protocol_dickert_mahs_const.te_delta) { + subghz_protocol_blocks_add_bit(&instance->decoder, 0); + } + + instance->tmp_cnt = 0; + } else { + instance->tmp_cnt = 0; + instance->decoder.parser_step = DickertMAHSDecoderStepReset; + } + } + } else { + instance->tmp_cnt = 0; + instance->decoder.parser_step = DickertMAHSDecoderStepReset; + } + + break; + } +} + +uint8_t subghz_protocol_decoder_dickert_mahs_get_hash_data(void* context) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + return subghz_protocol_blocks_get_hash_data( + &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1); +} + +SubGhzProtocolStatus subghz_protocol_decoder_dickert_mahs_serialize( + void* context, + FlipperFormat* flipper_format, + SubGhzRadioPreset* preset) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + return subghz_block_generic_serialize(&instance->generic, flipper_format, preset); +} + +SubGhzProtocolStatus + subghz_protocol_decoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format) { + furi_assert(context); + SubGhzProtocolDecoderDickertMAHS* instance = context; + SubGhzProtocolStatus ret = SubGhzProtocolStatusError; + do { + ret = subghz_block_generic_deserialize(&instance->generic, flipper_format); + if(ret != SubGhzProtocolStatusOk) { + break; + } + + // Allow for longer keys (<) instead of != + if((instance->generic.data_count_bit < + subghz_protocol_dickert_mahs_const.min_count_bit_for_found)) { + FURI_LOG_E(TAG, "Wrong number of bits in key"); + ret = SubGhzProtocolStatusErrorValueBitCount; + break; + } + } while(false); + return ret; +} + +void subghz_protocol_decoder_dickert_mahs_get_string(void* context, FuriString* output) { + furi_assert(context); + subghz_protocol_encoder_dickert_mahs_parse_buffer(context, output); +} diff --git a/lib/subghz/protocols/dickert_mahs.h b/lib/subghz/protocols/dickert_mahs.h new file mode 100644 index 000000000..3f682cee0 --- /dev/null +++ b/lib/subghz/protocols/dickert_mahs.h @@ -0,0 +1,120 @@ +#pragma once + +#include "base.h" + +#define SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME "Dickert_MAHS" + +typedef struct SubGhzProtocolDecoderDickertMAHS SubGhzProtocolDecoderDickertMAHS; +typedef struct SubGhzProtocolEncoderDickertMAHS SubGhzProtocolEncoderDickertMAHS; + +extern const SubGhzProtocolDecoder subghz_protocol_dickert_mahs_decoder; +extern const SubGhzProtocolEncoder subghz_protocol_dickert_mahs_encoder; +extern const SubGhzProtocol subghz_protocol_dickert_mahs; + +/** Allocate SubGhzProtocolEncoderDickertMAHS. + * + * @param environment Pointer to a SubGhzEnvironment instance + * + * @return pointer to a SubGhzProtocolEncoderDickertMAHS instance + */ +void* subghz_protocol_encoder_dickert_mahs_alloc(SubGhzEnvironment* environment); + +/** Free SubGhzProtocolEncoderDickertMAHS. + * + * @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance + */ +void subghz_protocol_encoder_dickert_mahs_free(void* context); + +/** Deserialize and generating an upload to send. + * + * @param context Pointer to a SubGhzProtocolEncoderDickertMAHS + * instance + * @param flipper_format Pointer to a FlipperFormat instance + * + * @return status + */ +SubGhzProtocolStatus + subghz_protocol_encoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format); + +/** Forced transmission stop. + * + * @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance + */ +void subghz_protocol_encoder_dickert_mahs_stop(void* context); + +/** Getting the level and duration of the upload to be loaded into DMA. + * + * @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance + * + * @return LevelDuration + */ +LevelDuration subghz_protocol_encoder_dickert_mahs_yield(void* context); + +/** Allocate SubGhzProtocolDecoderDickertMAHS. + * + * @param environment Pointer to a SubGhzEnvironment instance + * + * @return pointer to a SubGhzProtocolDecoderDickertMAHS instance + */ +void* subghz_protocol_decoder_dickert_mahs_alloc(SubGhzEnvironment* environment); + +/** Free SubGhzProtocolDecoderDickertMAHS. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance + */ +void subghz_protocol_decoder_dickert_mahs_free(void* context); + +/** Reset decoder SubGhzProtocolDecoderDickertMAHS. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance + */ +void subghz_protocol_decoder_dickert_mahs_reset(void* context); + +/** Parse a raw sequence of levels and durations received from the air. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance + * @param level Signal level true-high false-low + * @param duration Duration of this level in, us + */ +void subghz_protocol_decoder_dickert_mahs_feed(void* context, bool level, uint32_t duration); + +/** Getting the hash sum of the last randomly received parcel. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance + * + * @return hash Hash sum + */ +uint8_t subghz_protocol_decoder_dickert_mahs_get_hash_data(void* context); + +/** Serialize data SubGhzProtocolDecoderDickertMAHS. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS + * instance + * @param flipper_format Pointer to a FlipperFormat instance + * @param preset The modulation on which the signal was received, + * SubGhzRadioPreset + * + * @return status + */ +SubGhzProtocolStatus subghz_protocol_decoder_dickert_mahs_serialize( + void* context, + FlipperFormat* flipper_format, + SubGhzRadioPreset* preset); + +/** Deserialize data SubGhzProtocolDecoderDickertMAHS. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS + * instance + * @param flipper_format Pointer to a FlipperFormat instance + * + * @return status + */ +SubGhzProtocolStatus + subghz_protocol_decoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format); + +/** Getting a textual representation of the received data. + * + * @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance + * @param output Resulting text + */ +void subghz_protocol_decoder_dickert_mahs_get_string(void* context, FuriString* output); diff --git a/lib/subghz/protocols/protocol_items.c b/lib/subghz/protocols/protocol_items.c index 8275071eb..ca966390c 100644 --- a/lib/subghz/protocols/protocol_items.c +++ b/lib/subghz/protocols/protocol_items.c @@ -72,6 +72,7 @@ const SubGhzProtocol* subghz_protocol_registry_items[] = { &subghz_protocol_x10, &subghz_protocol_hormann_bisecur, &subghz_protocol_legrand, + &subghz_protocol_dickert_mahs, }; const SubGhzProtocolRegistry subghz_protocol_registry = { diff --git a/lib/subghz/protocols/protocol_items.h b/lib/subghz/protocols/protocol_items.h index 367025951..16dabf6b5 100644 --- a/lib/subghz/protocols/protocol_items.h +++ b/lib/subghz/protocols/protocol_items.h @@ -73,3 +73,4 @@ #include "x10.h" #include "hormann_bisecur.h" #include "legrand.h" +#include "dickert_mahs.h" diff --git a/lib/toolbox/api_lock.h b/lib/toolbox/api_lock.h index 5902a4922..a370514da 100644 --- a/lib/toolbox/api_lock.h +++ b/lib/toolbox/api_lock.h @@ -41,3 +41,7 @@ typedef FuriEventFlag* FuriApiLock; #define api_lock_wait_unlock_and_free(_lock) \ api_lock_wait_unlock(_lock); \ api_lock_free(_lock); + +#define api_lock_is_locked(_lock) (!(furi_event_flag_get(_lock) & API_LOCK_EVENT)) + +#define api_lock_relock(_lock) furi_event_flag_clear(_lock, API_LOCK_EVENT) diff --git a/scripts/imglint.py b/scripts/imglint.py index e28098653..7325343ac 100644 --- a/scripts/imglint.py +++ b/scripts/imglint.py @@ -4,7 +4,7 @@ import os from pathlib import Path from flipper.app import App -from PIL import Image, ImageOps +from PIL import Image _logger = logging.getLogger(__name__) diff --git a/scripts/update.py b/scripts/update.py index 2297cff99..641295371 100755 --- a/scripts/update.py +++ b/scripts/update.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import io import math import os import shutil @@ -9,7 +8,6 @@ import zlib from os.path import exists, join import pathlib -import heatshrink2 from flipper.app import App from flipper.assets.coprobin import CoproBinary, get_stack_type from flipper.assets.heatshrink_stream import HeatshrinkDataStreamHeader @@ -36,7 +34,12 @@ class Main(App): ) FLASH_BASE = 0x8000000 - MIN_LFS_PAGES = 6 + FLASH_PAGE_SIZE = 4 * 1024 + MIN_GAP_PAGES = 2 + + # Update stage file larger than that is not loadable without fix + # https://github.com/flipperdevices/flipperzero-firmware/pull/3676 + UPDATER_SIZE_THRESHOLD = 128 * 1024 HEATSHRINK_WINDOW_SIZE = 13 HEATSHRINK_LOOKAHEAD_SIZE = 6 @@ -118,7 +121,7 @@ class Main(App): self.logger.error( f"You are trying to bundle a non-standard stack type '{self.args.radiotype}'." ) - self.disclaimer() + self.show_disclaimer() return 1 if radio_addr == 0: @@ -131,14 +134,9 @@ class Main(App): if not exists(self.args.directory): os.makedirs(self.args.directory) - stage_size = os.stat(self.args.stage).st_size - max_stage_size = 131072 # 2 * MAX_READ in src/update.c - if stage_size > max_stage_size: - self.logger.warn( - f"RAM {stage_basename} size too big ({stage_size} > {max_stage_size} bytes)" - ) - return 2 + updater_stage_size = os.stat(self.args.stage).st_size shutil.copyfile(self.args.stage, join(self.args.directory, stage_basename)) + dfu_size = 0 if self.args.dfu: dfu_size = os.stat(self.args.dfu).st_size @@ -165,10 +163,10 @@ class Main(App): ): return 3 - if not self.layout_check(dfu_size, radio_addr): + if not self.layout_check(updater_stage_size, dfu_size, radio_addr): self.logger.warn("Memory layout looks suspicious") - if not self.args.disclaimer == "yes": - self.disclaimer() + if self.args.disclaimer != "yes": + self.show_disclaimer() return 2 if self.args.splash: @@ -225,22 +223,33 @@ class Main(App): return 0 - def layout_check(self, fw_size, radio_addr): + def layout_check(self, stage_size, fw_size, radio_addr): + if stage_size > self.UPDATER_SIZE_THRESHOLD: + self.logger.warn( + f"Updater size {stage_size}b > {self.UPDATER_SIZE_THRESHOLD}b and is not loadable on older firmwares!" + ) + if fw_size == 0 or radio_addr == 0: self.logger.info("Cannot validate layout for partial package") return True - lfs_span = radio_addr - self.FLASH_BASE - fw_size - self.logger.debug(f"Expected LFS size: {lfs_span}") - lfs_span_pages = lfs_span / (4 * 1024) - if lfs_span_pages < self.MIN_LFS_PAGES: + fw2stack_gap = radio_addr - self.FLASH_BASE - fw_size + self.logger.debug(f"Expected reserved space size: {fw2stack_gap}") + fw2stack_gap_pages = fw2stack_gap / self.FLASH_PAGE_SIZE + if fw2stack_gap_pages < 0: self.logger.warn( - f"Expected LFS size is too small (~{int(lfs_span_pages)} pages)" + f"Firmware image overlaps C2 region and is not programmable!" + ) + return False + + elif fw2stack_gap_pages < self.MIN_GAP_PAGES: + self.logger.warn( + f"Expected reserved flash size is too small (~{int(fw2stack_gap_pages)} page(s), need >={self.MIN_GAP_PAGES} page(s))" ) return False return True - def disclaimer(self): + def show_disclaimer(self): self.logger.error( "You might brick your device into a state in which you'd need an SWD programmer to fix it." ) diff --git a/targets/f18/api_symbols.csv b/targets/f18/api_symbols.csv index 66690007b..4745dc956 100644 --- a/targets/f18/api_symbols.csv +++ b/targets/f18/api_symbols.csv @@ -13,6 +13,7 @@ Header,+,applications/services/gui/icon_i.h,, Header,+,applications/services/gui/modules/button_menu.h,, Header,+,applications/services/gui/modules/button_panel.h,, Header,+,applications/services/gui/modules/byte_input.h,, +Header,+,applications/services/gui/modules/number_input.h,, Header,+,applications/services/gui/modules/dialog_ex.h,, Header,+,applications/services/gui/modules/empty_screen.h,, Header,+,applications/services/gui/modules/file_browser.h,, @@ -722,6 +723,11 @@ Function,+,byte_input_free,void,ByteInput* Function,+,byte_input_get_view,View*,ByteInput* Function,+,byte_input_set_header_text,void,"ByteInput*, const char*" Function,+,byte_input_set_result_callback,void,"ByteInput*, ByteInputCallback, ByteChangedCallback, void*, uint8_t*, uint8_t" +Function,+,number_input_alloc,NumberInput*, +Function,+,number_input_free,void,NumberInput* +Function,+,number_input_get_view,View*,NumberInput* +Function,+,number_input_set_header_text,void,"NumberInput*, const char*" +Function,+,number_input_set_result_callback,void,"NumberInput*, NumberInputCallback, void*, int32_t, int32_t, int32_t" Function,-,bzero,void,"void*, size_t" Function,+,calloc,void*,"size_t, size_t" Function,+,canvas_clear,void,Canvas* @@ -1108,11 +1114,13 @@ Function,+,furi_event_flag_set,uint32_t,"FuriEventFlag*, uint32_t" Function,+,furi_event_flag_wait,uint32_t,"FuriEventFlag*, uint32_t, uint32_t, uint32_t" Function,+,furi_event_loop_alloc,FuriEventLoop*, Function,+,furi_event_loop_free,void,FuriEventLoop* -Function,+,furi_event_loop_message_queue_subscribe,void,"FuriEventLoop*, FuriMessageQueue*, FuriEventLoopEvent, FuriEventLoopMessageQueueCallback, void*" -Function,+,furi_event_loop_message_queue_unsubscribe,void,"FuriEventLoop*, FuriMessageQueue*" Function,+,furi_event_loop_pend_callback,void,"FuriEventLoop*, FuriEventLoopPendingCallback, void*" Function,+,furi_event_loop_run,void,FuriEventLoop* Function,+,furi_event_loop_stop,void,FuriEventLoop* +Function,+,furi_event_loop_subscribe_message_queue,void,"FuriEventLoop*, FuriMessageQueue*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_mutex,void,"FuriEventLoop*, FuriMutex*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_semaphore,void,"FuriEventLoop*, FuriSemaphore*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_stream_buffer,void,"FuriEventLoop*, FuriStreamBuffer*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" Function,+,furi_event_loop_tick_set,void,"FuriEventLoop*, uint32_t, FuriEventLoopTickCallback, void*" Function,+,furi_event_loop_timer_alloc,FuriEventLoopTimer*,"FuriEventLoop*, FuriEventLoopTimerCallback, FuriEventLoopTimerType, void*" Function,+,furi_event_loop_timer_free,void,FuriEventLoopTimer* @@ -1122,6 +1130,7 @@ Function,+,furi_event_loop_timer_is_running,_Bool,const FuriEventLoopTimer* Function,+,furi_event_loop_timer_restart,void,FuriEventLoopTimer* Function,+,furi_event_loop_timer_start,void,"FuriEventLoopTimer*, uint32_t" Function,+,furi_event_loop_timer_stop,void,FuriEventLoopTimer* +Function,+,furi_event_loop_unsubscribe,void,"FuriEventLoop*, FuriEventLoopObject*" Function,+,furi_get_tick,uint32_t, Function,+,furi_hal_adc_acquire,FuriHalAdcHandle*, Function,+,furi_hal_adc_configure,void,FuriHalAdcHandle* @@ -1544,6 +1553,7 @@ Function,+,furi_semaphore_acquire,FuriStatus,"FuriSemaphore*, uint32_t" Function,+,furi_semaphore_alloc,FuriSemaphore*,"uint32_t, uint32_t" Function,+,furi_semaphore_free,void,FuriSemaphore* Function,+,furi_semaphore_get_count,uint32_t,FuriSemaphore* +Function,+,furi_semaphore_get_space,uint32_t,FuriSemaphore* Function,+,furi_semaphore_release,FuriStatus,FuriSemaphore* Function,+,furi_stream_buffer_alloc,FuriStreamBuffer*,"size_t, size_t" Function,+,furi_stream_buffer_bytes_available,size_t,FuriStreamBuffer* @@ -1572,6 +1582,8 @@ Function,+,furi_string_cmpi_str,int,"const FuriString*, const char[]" Function,+,furi_string_empty,_Bool,const FuriString* Function,+,furi_string_end_with,_Bool,"const FuriString*, const FuriString*" Function,+,furi_string_end_with_str,_Bool,"const FuriString*, const char[]" +Function,+,furi_string_end_withi,_Bool,"const FuriString*, const FuriString*" +Function,+,furi_string_end_withi_str,_Bool,"const FuriString*, const char[]" Function,+,furi_string_equal,_Bool,"const FuriString*, const FuriString*" Function,+,furi_string_equal_str,_Bool,"const FuriString*, const char[]" Function,+,furi_string_free,void,FuriString* @@ -2282,7 +2294,7 @@ Function,+,power_get_info,void,"Power*, PowerInfo*" Function,+,power_get_pubsub,FuriPubSub*,Power* Function,+,power_is_battery_healthy,_Bool,Power* Function,+,power_off,void,Power* -Function,+,power_reboot,void,PowerBootMode +Function,+,power_reboot,void,"Power*, PowerBootMode" Function,+,powf,float,"float, float" Function,-,powl,long double,"long double, long double" Function,+,pretty_format_bytes_hex_canonical,void,"FuriString*, size_t, const char*, const uint8_t*, size_t" @@ -2757,11 +2769,11 @@ Function,+,view_holder_alloc,ViewHolder*, Function,+,view_holder_attach_to_gui,void,"ViewHolder*, Gui*" Function,+,view_holder_free,void,ViewHolder* Function,+,view_holder_get_free_context,void*,ViewHolder* +Function,+,view_holder_send_to_back,void,ViewHolder* +Function,+,view_holder_send_to_front,void,ViewHolder* Function,+,view_holder_set_back_callback,void,"ViewHolder*, BackCallback, void*" Function,+,view_holder_set_free_callback,void,"ViewHolder*, FreeCallback, void*" Function,+,view_holder_set_view,void,"ViewHolder*, View*" -Function,+,view_holder_start,void,ViewHolder* -Function,+,view_holder_stop,void,ViewHolder* Function,+,view_holder_update,void,"View*, void*" Function,+,view_port_alloc,ViewPort*, Function,+,view_port_draw_callback_set,void,"ViewPort*, ViewPortDrawCallback, void*" diff --git a/targets/f18/target.json b/targets/f18/target.json index 9c450aa83..3452c6707 100644 --- a/targets/f18/target.json +++ b/targets/f18/target.json @@ -22,7 +22,6 @@ "signal_reader", "microtar", "usb_stm32", - "appframe", "assets", "one_wire", "music_worker", diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index 3a577ad72..980d5903d 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -23,6 +23,7 @@ Header,+,applications/services/gui/modules/file_browser.h,, Header,+,applications/services/gui/modules/file_browser_worker.h,, Header,+,applications/services/gui/modules/loading.h,, Header,+,applications/services/gui/modules/menu.h,, +Header,+,applications/services/gui/modules/number_input.h,, Header,+,applications/services/gui/modules/popup.h,, Header,+,applications/services/gui/modules/submenu.h,, Header,+,applications/services/gui/modules/text_box.h,, @@ -1263,11 +1264,13 @@ Function,+,furi_event_flag_set,uint32_t,"FuriEventFlag*, uint32_t" Function,+,furi_event_flag_wait,uint32_t,"FuriEventFlag*, uint32_t, uint32_t, uint32_t" Function,+,furi_event_loop_alloc,FuriEventLoop*, Function,+,furi_event_loop_free,void,FuriEventLoop* -Function,+,furi_event_loop_message_queue_subscribe,void,"FuriEventLoop*, FuriMessageQueue*, FuriEventLoopEvent, FuriEventLoopMessageQueueCallback, void*" -Function,+,furi_event_loop_message_queue_unsubscribe,void,"FuriEventLoop*, FuriMessageQueue*" Function,+,furi_event_loop_pend_callback,void,"FuriEventLoop*, FuriEventLoopPendingCallback, void*" Function,+,furi_event_loop_run,void,FuriEventLoop* Function,+,furi_event_loop_stop,void,FuriEventLoop* +Function,+,furi_event_loop_subscribe_message_queue,void,"FuriEventLoop*, FuriMessageQueue*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_mutex,void,"FuriEventLoop*, FuriMutex*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_semaphore,void,"FuriEventLoop*, FuriSemaphore*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" +Function,+,furi_event_loop_subscribe_stream_buffer,void,"FuriEventLoop*, FuriStreamBuffer*, FuriEventLoopEvent, FuriEventLoopEventCallback, void*" Function,+,furi_event_loop_tick_set,void,"FuriEventLoop*, uint32_t, FuriEventLoopTickCallback, void*" Function,+,furi_event_loop_timer_alloc,FuriEventLoopTimer*,"FuriEventLoop*, FuriEventLoopTimerCallback, FuriEventLoopTimerType, void*" Function,+,furi_event_loop_timer_free,void,FuriEventLoopTimer* @@ -1277,6 +1280,7 @@ Function,+,furi_event_loop_timer_is_running,_Bool,const FuriEventLoopTimer* Function,+,furi_event_loop_timer_restart,void,FuriEventLoopTimer* Function,+,furi_event_loop_timer_start,void,"FuriEventLoopTimer*, uint32_t" Function,+,furi_event_loop_timer_stop,void,FuriEventLoopTimer* +Function,+,furi_event_loop_unsubscribe,void,"FuriEventLoop*, FuriEventLoopObject*" Function,+,furi_get_tick,uint32_t, Function,+,furi_hal_adc_acquire,FuriHalAdcHandle*, Function,+,furi_hal_adc_configure,void,FuriHalAdcHandle* @@ -1821,6 +1825,7 @@ Function,+,furi_semaphore_acquire,FuriStatus,"FuriSemaphore*, uint32_t" Function,+,furi_semaphore_alloc,FuriSemaphore*,"uint32_t, uint32_t" Function,+,furi_semaphore_free,void,FuriSemaphore* Function,+,furi_semaphore_get_count,uint32_t,FuriSemaphore* +Function,+,furi_semaphore_get_space,uint32_t,FuriSemaphore* Function,+,furi_semaphore_release,FuriStatus,FuriSemaphore* Function,+,furi_stream_buffer_alloc,FuriStreamBuffer*,"size_t, size_t" Function,+,furi_stream_buffer_bytes_available,size_t,FuriStreamBuffer* @@ -1849,6 +1854,8 @@ Function,+,furi_string_cmpi_str,int,"const FuriString*, const char[]" Function,+,furi_string_empty,_Bool,const FuriString* Function,+,furi_string_end_with,_Bool,"const FuriString*, const FuriString*" Function,+,furi_string_end_with_str,_Bool,"const FuriString*, const char[]" +Function,+,furi_string_end_withi,_Bool,"const FuriString*, const FuriString*" +Function,+,furi_string_end_withi_str,_Bool,"const FuriString*, const char[]" Function,+,furi_string_equal,_Bool,"const FuriString*, const FuriString*" Function,+,furi_string_equal_str,_Bool,"const FuriString*, const char[]" Function,+,furi_string_free,void,FuriString* @@ -2888,6 +2895,11 @@ Function,+,notification_internal_message_block,void,"NotificationApp*, const Not Function,+,notification_message,void,"NotificationApp*, const NotificationSequence*" Function,+,notification_message_block,void,"NotificationApp*, const NotificationSequence*" Function,-,nrand48,long,unsigned short[3] +Function,+,number_input_alloc,NumberInput*, +Function,+,number_input_free,void,NumberInput* +Function,+,number_input_get_view,View*,NumberInput* +Function,+,number_input_set_header_text,void,"NumberInput*, const char*" +Function,+,number_input_set_result_callback,void,"NumberInput*, NumberInputCallback, void*, int32_t, int32_t, int32_t" Function,-,on_exit,int,"void (*)(int, void*), void*" Function,+,onewire_host_alloc,OneWireHost*,const GpioPin* Function,+,onewire_host_free,void,OneWireHost* @@ -2987,7 +2999,7 @@ Function,+,power_get_pubsub,FuriPubSub*,Power* Function,+,power_get_settings_events_pubsub,FuriPubSub*,Power* Function,+,power_is_battery_healthy,_Bool,Power* Function,+,power_off,void,Power* -Function,+,power_reboot,void,PowerBootMode +Function,+,power_reboot,void,"Power*, PowerBootMode" Function,+,power_set_battery_icon_enabled,void,"Power*, _Bool" Function,-,power_trigger_ui_update,void,Power* Function,+,powf,float,"float, float" @@ -3740,11 +3752,11 @@ Function,+,view_holder_alloc,ViewHolder*, Function,+,view_holder_attach_to_gui,void,"ViewHolder*, Gui*" Function,+,view_holder_free,void,ViewHolder* Function,+,view_holder_get_free_context,void*,ViewHolder* +Function,+,view_holder_send_to_back,void,ViewHolder* +Function,+,view_holder_send_to_front,void,ViewHolder* Function,+,view_holder_set_back_callback,void,"ViewHolder*, BackCallback, void*" Function,+,view_holder_set_free_callback,void,"ViewHolder*, FreeCallback, void*" Function,+,view_holder_set_view,void,"ViewHolder*, View*" -Function,+,view_holder_start,void,ViewHolder* -Function,+,view_holder_stop,void,ViewHolder* Function,+,view_holder_update,void,"View*, void*" Function,+,view_port_alloc,ViewPort*, Function,+,view_port_ascii_callback_set,void,"ViewPort*, ViewPortAsciiCallback, void*" diff --git a/targets/f7/target.json b/targets/f7/target.json index 73189f45b..c5a65704a 100644 --- a/targets/f7/target.json +++ b/targets/f7/target.json @@ -35,7 +35,6 @@ "microtar", "usb_stm32", "infrared", - "appframe", "assets", "one_wire", "ibutton",