From 2034b6ac4a7dfc262ca0be959966dd6e84205bfe Mon Sep 17 00:00:00 2001 From: Nikolay Minaylov Date: Wed, 27 Apr 2022 19:30:37 +0300 Subject: [PATCH] [FL-2494, FL-2497] PIN Lock refactoring, IR library fix (#1167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * PIN Lock refactoring * Change tv.ir file type to prevent opening from archive * Manifest update * Assets: enforce sorting in fs traversal Co-authored-by: あく --- applications/desktop/desktop.c | 16 ++- applications/desktop/desktop_helpers.c | 82 ------------- applications/desktop/desktop_helpers.h | 9 -- .../desktop_settings/desktop_settings.h | 12 -- .../scenes/desktop_settings_scene_pin_auth.c | 4 +- .../scenes/desktop_settings_scene_pin_error.c | 4 +- .../scenes/desktop_settings_scene_pin_setup.c | 3 +- applications/desktop/helpers/pin_lock.c | 112 ++++++++++++++++++ applications/desktop/helpers/pin_lock.h | 21 ++++ .../desktop/scenes/desktop_scene_lock_menu.c | 4 +- .../desktop/scenes/desktop_scene_locked.c | 15 ++- .../desktop/scenes/desktop_scene_pin_input.c | 14 +-- assets/resources/Manifest | 4 +- assets/resources/infrared/assets/tv.ir | 2 +- scripts/flipper/assets/manifest.py | 2 + 15 files changed, 168 insertions(+), 136 deletions(-) delete mode 100644 applications/desktop/desktop_helpers.c delete mode 100644 applications/desktop/desktop_helpers.h create mode 100644 applications/desktop/helpers/pin_lock.c create mode 100644 applications/desktop/helpers/pin_lock.h diff --git a/applications/desktop/desktop.c b/applications/desktop/desktop.c index 1c1ce04f..daaffc2d 100644 --- a/applications/desktop/desktop.c +++ b/applications/desktop/desktop.c @@ -14,7 +14,7 @@ #include "desktop/views/desktop_view_pin_input.h" #include "desktop/views/desktop_view_pin_timeout.h" #include "desktop_i.h" -#include "desktop_helpers.h" +#include "helpers/pin_lock.h" static void desktop_auto_lock_arm(Desktop*); static void desktop_auto_lock_inhibit(Desktop*); @@ -117,7 +117,6 @@ static void desktop_auto_lock_inhibit(Desktop* desktop) { } void desktop_lock(Desktop* desktop) { - furi_hal_rtc_set_pin_fails(0); desktop_auto_lock_inhibit(desktop); scene_manager_set_scene_state( desktop->scene_manager, DesktopSceneLocked, SCENE_LOCKED_FIRST_ENTER); @@ -126,8 +125,10 @@ void desktop_lock(Desktop* desktop) { } void desktop_unlock(Desktop* desktop) { - furi_hal_rtc_set_pin_fails(0); - desktop_helpers_unlock_system(desktop); + view_port_enabled_set(desktop->lock_viewport, false); + Gui* gui = furi_record_open("gui"); + gui_set_lockdown(gui, false); + furi_record_close("gui"); desktop_view_locked_unlock(desktop->locked_view); scene_manager_search_and_switch_to_previous_scene(desktop->scene_manager, DesktopSceneMain); desktop_auto_lock_arm(desktop); @@ -301,18 +302,15 @@ int32_t desktop_srv(void* p) { bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings); if(!loaded) { - furi_hal_rtc_reset_flag(FuriHalRtcFlagLock); memset(&desktop->settings, 0, sizeof(desktop->settings)); SAVE_DESKTOP_SETTINGS(&desktop->settings); } scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain); - if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock) && !desktop->settings.pin_code.length) { - furi_hal_rtc_reset_flag(FuriHalRtcFlagLock); - } + desktop_pin_lock_init(&desktop->settings); - if(!furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) { + if(!desktop_pin_lock_is_locked()) { if(!loader_is_locked(desktop->loader)) { desktop_auto_lock_arm(desktop); } diff --git a/applications/desktop/desktop_helpers.c b/applications/desktop/desktop_helpers.c deleted file mode 100644 index c3025ae1..00000000 --- a/applications/desktop/desktop_helpers.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "desktop_helpers.h" -#include "desktop_i.h" - -static const NotificationSequence sequence_pin_fail = { - &message_display_on, - - &message_red_255, - &message_vibro_on, - &message_delay_100, - &message_vibro_off, - &message_red_0, - - &message_delay_250, - - &message_red_255, - &message_vibro_on, - &message_delay_100, - &message_vibro_off, - &message_red_0, - NULL, -}; - -static const uint8_t desktop_helpers_fails_timeout[] = { - 0, - 0, - 0, - 0, - 30, - 60, - 90, - 120, - 150, - 180, - /* +60 for every next fail */ -}; - -void desktop_helpers_emit_error_notification() { - NotificationApp* notification = furi_record_open("notification"); - notification_message(notification, &sequence_pin_fail); - furi_record_close("notification"); -} - -void desktop_helpers_lock_system(Desktop* desktop, bool hard_lock) { - view_port_enabled_set(desktop->lock_viewport, true); - if(hard_lock) { - furi_hal_rtc_set_flag(FuriHalRtcFlagLock); - furi_hal_usb_disable(); - } - - Gui* gui = furi_record_open("gui"); - gui_set_lockdown(gui, true); - furi_record_close("gui"); -} - -void desktop_helpers_unlock_system(Desktop* desktop) { - furi_hal_rtc_reset_flag(FuriHalRtcFlagLock); - furi_hal_usb_enable(); - view_port_enabled_set(desktop->lock_viewport, false); - - Gui* gui = furi_record_open("gui"); - gui_set_lockdown(gui, false); - furi_record_close("gui"); -} - -uint32_t desktop_helpers_get_pin_fail_timeout(uint32_t pin_fails) { - uint32_t pin_timeout = 0; - uint32_t max_index = COUNT_OF(desktop_helpers_fails_timeout) - 1; - if(pin_fails <= max_index) { - pin_timeout = desktop_helpers_fails_timeout[pin_fails]; - } else { - pin_timeout = desktop_helpers_fails_timeout[max_index] + (pin_fails - max_index) * 60; - } - - return pin_timeout; -} diff --git a/applications/desktop/desktop_helpers.h b/applications/desktop/desktop_helpers.h deleted file mode 100644 index f8393df8..00000000 --- a/applications/desktop/desktop_helpers.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include -#include "desktop.h" - -void desktop_helpers_emit_error_notification(); -void desktop_helpers_lock_system(Desktop* desktop, bool hard_lock); -void desktop_helpers_unlock_system(Desktop* desktop); -uint32_t desktop_helpers_get_pin_fail_timeout(uint32_t pin_fails); diff --git a/applications/desktop/desktop_settings/desktop_settings.h b/applications/desktop/desktop_settings/desktop_settings.h index b56d63a7..e096288d 100644 --- a/applications/desktop/desktop_settings/desktop_settings.h +++ b/applications/desktop/desktop_settings/desktop_settings.h @@ -41,15 +41,3 @@ typedef struct { PinCode pin_code; uint32_t auto_lock_delay_ms; } DesktopSettings; - -static inline bool pins_are_equal(const PinCode* pin_code1, const PinCode* pin_code2) { - furi_assert(pin_code1); - furi_assert(pin_code2); - bool result = false; - - if(pin_code1->length == pin_code2->length) { - result = !memcmp(pin_code1->data, pin_code2->data, pin_code1->length); - } - - return result; -} diff --git a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_auth.c b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_auth.c index c57506f9..54330c74 100644 --- a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_auth.c +++ b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_auth.c @@ -1,7 +1,7 @@ #include #include #include - +#include "../../helpers/pin_lock.h" #include "../desktop_settings_app.h" #include "desktop/desktop_settings/desktop_settings.h" #include "desktop/views/desktop_view_pin_input.h" @@ -18,7 +18,7 @@ static void pin_auth_done_callback(const PinCode* pin_code, void* context) { DesktopSettingsApp* app = context; app->pincode_buffer = *pin_code; - if(pins_are_equal(&app->settings.pin_code, pin_code)) { + if(desktop_pins_are_equal(&app->settings.pin_code, pin_code)) { view_dispatcher_send_custom_event(app->view_dispatcher, SCENE_EVENT_PINS_EQUAL); } else { view_dispatcher_send_custom_event(app->view_dispatcher, SCENE_EVENT_PINS_DIFFERENT); diff --git a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_error.c b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_error.c index 07bba4a8..882b8f0a 100644 --- a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_error.c +++ b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_error.c @@ -6,7 +6,7 @@ #include "desktop/views/desktop_view_pin_input.h" #include "desktop_settings_scene.h" #include "desktop_settings_scene_i.h" -#include "../../desktop_helpers.h" +#include "../../helpers/pin_lock.h" #include "../desktop_settings_app.h" #define SCENE_EVENT_EXIT (0U) @@ -25,7 +25,7 @@ static void pin_error_done_callback(const PinCode* pin_code, void* context) { void desktop_settings_scene_pin_error_on_enter(void* context) { DesktopSettingsApp* app = context; - desktop_helpers_emit_error_notification(); + desktop_pin_lock_error_notify(); desktop_view_pin_input_set_context(app->pin_input_view, app); desktop_view_pin_input_set_back_callback(app->pin_input_view, pin_error_back_callback); diff --git a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_setup.c b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_setup.c index 5659684f..282bf853 100644 --- a/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_setup.c +++ b/applications/desktop/desktop_settings/scenes/desktop_settings_scene_pin_setup.c @@ -7,6 +7,7 @@ #include "desktop/views/desktop_view_pin_input.h" #include "desktop_settings_scene.h" #include "desktop_settings_scene_i.h" +#include "../../helpers/pin_lock.h" #define SCENE_EVENT_EXIT (0U) #define SCENE_EVENT_1ST_PIN_ENTERED (1U) @@ -24,7 +25,7 @@ static void pin_setup_done_callback(const PinCode* pin_code, void* context) { view_dispatcher_send_custom_event(app->view_dispatcher, SCENE_EVENT_1ST_PIN_ENTERED); } else { app->pincode_buffer_filled = false; - if(pins_are_equal(&app->pincode_buffer, pin_code)) { + if(desktop_pins_are_equal(&app->pincode_buffer, pin_code)) { view_dispatcher_send_custom_event(app->view_dispatcher, SCENE_EVENT_PINS_EQUAL); } else { view_dispatcher_send_custom_event(app->view_dispatcher, SCENE_EVENT_PINS_DIFFERENT); diff --git a/applications/desktop/helpers/pin_lock.c b/applications/desktop/helpers/pin_lock.c new file mode 100644 index 00000000..6bde782c --- /dev/null +++ b/applications/desktop/helpers/pin_lock.c @@ -0,0 +1,112 @@ + +#include +#include +#include +#include +#include +#include + +#include "../helpers/pin_lock.h" +#include "../desktop_i.h" + +static const NotificationSequence sequence_pin_fail = { + &message_display_on, + + &message_red_255, + &message_vibro_on, + &message_delay_100, + &message_vibro_off, + &message_red_0, + + &message_delay_250, + + &message_red_255, + &message_vibro_on, + &message_delay_100, + &message_vibro_off, + &message_red_0, + NULL, +}; + +static const uint8_t desktop_helpers_fails_timeout[] = { + 0, + 0, + 0, + 0, + 30, + 60, + 90, + 120, + 150, + 180, + /* +60 for every next fail */ +}; + +void desktop_pin_lock_error_notify() { + NotificationApp* notification = furi_record_open("notification"); + notification_message(notification, &sequence_pin_fail); + furi_record_close("notification"); +} + +uint32_t desktop_pin_lock_get_fail_timeout() { + uint32_t pin_fails = furi_hal_rtc_get_pin_fails(); + uint32_t pin_timeout = 0; + uint32_t max_index = COUNT_OF(desktop_helpers_fails_timeout) - 1; + if(pin_fails <= max_index) { + pin_timeout = desktop_helpers_fails_timeout[pin_fails]; + } else { + pin_timeout = desktop_helpers_fails_timeout[max_index] + (pin_fails - max_index) * 60; + } + + return pin_timeout; +} + +void desktop_pin_lock() { + furi_hal_rtc_set_pin_fails(0); + furi_hal_rtc_set_flag(FuriHalRtcFlagLock); + furi_hal_usb_disable(); +} + +void desktop_pin_unlock() { + furi_hal_rtc_reset_flag(FuriHalRtcFlagLock); + furi_hal_usb_enable(); +} + +void desktop_pin_lock_init(DesktopSettings* settings) { + if(settings->pin_code.length > 0) { + furi_hal_rtc_set_flag(FuriHalRtcFlagLock); + furi_hal_usb_disable(); + } else { + furi_hal_rtc_reset_flag(FuriHalRtcFlagLock); + furi_hal_usb_enable(); + } +} + +bool desktop_pin_lock_verify(const PinCode* pin_set, const PinCode* pin_entered) { + bool result = false; + if(desktop_pins_are_equal(pin_set, pin_entered)) { + furi_hal_rtc_set_pin_fails(0); + result = true; + } else { + uint32_t pin_fails = furi_hal_rtc_get_pin_fails(); + furi_hal_rtc_set_pin_fails(pin_fails + 1); + result = false; + } + return result; +} + +bool desktop_pin_lock_is_locked() { + return furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock); +} + +bool desktop_pins_are_equal(const PinCode* pin_code1, const PinCode* pin_code2) { + furi_assert(pin_code1); + furi_assert(pin_code2); + bool result = false; + + if(pin_code1->length == pin_code2->length) { + result = !memcmp(pin_code1->data, pin_code2->data, pin_code1->length); + } + + return result; +} diff --git a/applications/desktop/helpers/pin_lock.h b/applications/desktop/helpers/pin_lock.h new file mode 100644 index 00000000..e99ef9f3 --- /dev/null +++ b/applications/desktop/helpers/pin_lock.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include "../desktop.h" +#include "../desktop_settings/desktop_settings.h" + +void desktop_pin_lock_error_notify(); + +uint32_t desktop_pin_lock_get_fail_timeout(); + +void desktop_pin_lock(); + +void desktop_pin_unlock(); + +bool desktop_pin_lock_is_locked(); + +void desktop_pin_lock_init(DesktopSettings* settings); + +bool desktop_pin_lock_verify(const PinCode* pin_set, const PinCode* pin_entered); + +bool desktop_pins_are_equal(const PinCode* pin_code1, const PinCode* pin_code2); diff --git a/applications/desktop/scenes/desktop_scene_lock_menu.c b/applications/desktop/scenes/desktop_scene_lock_menu.c index 7b7606b1..b11b90a3 100644 --- a/applications/desktop/scenes/desktop_scene_lock_menu.c +++ b/applications/desktop/scenes/desktop_scene_lock_menu.c @@ -10,6 +10,7 @@ #include "../views/desktop_view_lock_menu.h" #include "desktop_scene_i.h" #include "desktop_scene.h" +#include "../helpers/pin_lock.h" #define TAG "DesktopSceneLock" @@ -53,7 +54,7 @@ bool desktop_scene_lock_menu_on_event(void* context, SceneManagerEvent event) { break; case DesktopLockMenuEventPinLock: if(desktop->settings.pin_code.length > 0) { - furi_hal_rtc_set_flag(FuriHalRtcFlagLock); + desktop_pin_lock(); desktop_lock(desktop); } else { LoaderStatus status = @@ -64,7 +65,6 @@ bool desktop_scene_lock_menu_on_event(void* context, SceneManagerEvent event) { FURI_LOG_E(TAG, "Unable to start desktop settings"); } } - consumed = true; break; case DesktopLockMenuEventExit: diff --git a/applications/desktop/scenes/desktop_scene_locked.c b/applications/desktop/scenes/desktop_scene_locked.c index 65d064d8..118bbce4 100644 --- a/applications/desktop/scenes/desktop_scene_locked.c +++ b/applications/desktop/scenes/desktop_scene_locked.c @@ -7,7 +7,7 @@ #include "../desktop.h" #include "../desktop_i.h" -#include "../desktop_helpers.h" +#include "../helpers/pin_lock.h" #include "../animations/animation_manager.h" #include "../views/desktop_events.h" #include "../views/desktop_view_pin_input.h" @@ -45,14 +45,17 @@ void desktop_scene_locked_on_enter(void* context) { bool switch_to_timeout_scene = false; uint32_t state = scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneLocked); if(state == SCENE_LOCKED_FIRST_ENTER) { - bool pin_locked = furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock); - desktop_helpers_lock_system(desktop, pin_locked); + bool pin_locked = desktop_pin_lock_is_locked(); + view_port_enabled_set(desktop->lock_viewport, true); + Gui* gui = furi_record_open("gui"); + gui_set_lockdown(gui, true); + furi_record_close("gui"); + if(pin_locked) { LOAD_DESKTOP_SETTINGS(&desktop->settings); desktop_view_locked_lock(desktop->locked_view, true); - uint32_t pin_fails = furi_hal_rtc_get_pin_fails(); - uint32_t pin_timeout = desktop_helpers_get_pin_fail_timeout(pin_fails); - if(pin_timeout) { + uint32_t pin_timeout = desktop_pin_lock_get_fail_timeout(); + if(pin_timeout > 0) { scene_manager_set_scene_state( desktop->scene_manager, DesktopScenePinTimeout, pin_timeout); switch_to_timeout_scene = true; diff --git a/applications/desktop/scenes/desktop_scene_pin_input.c b/applications/desktop/scenes/desktop_scene_pin_input.c index 827b74a6..156a5eb2 100644 --- a/applications/desktop/scenes/desktop_scene_pin_input.c +++ b/applications/desktop/scenes/desktop_scene_pin_input.c @@ -12,7 +12,7 @@ #include "../animations/animation_manager.h" #include "../views/desktop_events.h" #include "../views/desktop_view_pin_input.h" -#include "../desktop_helpers.h" +#include "../helpers/pin_lock.h" #include "desktop_scene.h" #include "desktop_scene_i.h" @@ -54,7 +54,7 @@ static void desktop_scene_pin_input_back_callback(void* context) { static void desktop_scene_pin_input_done_callback(const PinCode* pin_code, void* context) { Desktop* desktop = (Desktop*)context; - if(pins_are_equal(&desktop->settings.pin_code, pin_code)) { + if(desktop_pin_lock_verify(&desktop->settings.pin_code, pin_code)) { view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventUnlocked); } else { view_dispatcher_send_custom_event( @@ -97,17 +97,14 @@ void desktop_scene_pin_input_on_enter(void* context) { bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) { Desktop* desktop = (Desktop*)context; bool consumed = false; - uint32_t pin_fails = 0; + uint32_t pin_timeout = 0; if(event.type == SceneManagerEventTypeCustom) { switch(event.event) { case DesktopPinInputEventUnlockFailed: - pin_fails = furi_hal_rtc_get_pin_fails(); - pin_fails++; - furi_hal_rtc_set_pin_fails(pin_fails); - uint32_t pin_timeout = desktop_helpers_get_pin_fail_timeout(pin_fails); + pin_timeout = desktop_pin_lock_get_fail_timeout(); if(pin_timeout > 0) { - desktop_helpers_emit_error_notification(); + desktop_pin_lock_error_notify(); scene_manager_set_scene_state( desktop->scene_manager, DesktopScenePinTimeout, pin_timeout); scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout); @@ -129,6 +126,7 @@ bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) { consumed = true; break; case DesktopPinInputEventUnlocked: + desktop_pin_unlock(); desktop_unlock(desktop); consumed = true; break; diff --git a/assets/resources/Manifest b/assets/resources/Manifest index 12773c00..f19edd7d 100644 --- a/assets/resources/Manifest +++ b/assets/resources/Manifest @@ -1,5 +1,5 @@ V:0 -T:1650389893 +T:1651076680 D:badusb D:dolphin D:infrared @@ -222,7 +222,7 @@ F:33b8fde22f34ef556b64b77164bc19b0:578:dolphin/L3_Lab_research_128x54/frame_8.bm F:f267f0654781049ca323b11bb4375519:581:dolphin/L3_Lab_research_128x54/frame_9.bm F:41106c0cbc5144f151b2b2d3daaa0527:727:dolphin/L3_Lab_research_128x54/meta.txt D:infrared/assets -F:5b16e1a59daf3ef1d0fc95b3b5596d67:74300:infrared/assets/tv.ir +F:d895fda2f48c6cc4c55e8a398ff52e43:74300:infrared/assets/tv.ir D:nfc/assets F:c6826a621d081d68309e4be424d3d974:4715:nfc/assets/aid.nfc F:86efbebdf41bb6bf15cc51ef88f069d5:2565:nfc/assets/country_code.nfc diff --git a/assets/resources/infrared/assets/tv.ir b/assets/resources/infrared/assets/tv.ir index 1031db63..98292a8e 100755 --- a/assets/resources/infrared/assets/tv.ir +++ b/assets/resources/infrared/assets/tv.ir @@ -1,4 +1,4 @@ -Filetype: IR signals file +Filetype: IR library file Version: 1 # name: POWER diff --git a/scripts/flipper/assets/manifest.py b/scripts/flipper/assets/manifest.py index 103f07b6..f7526ac7 100644 --- a/scripts/flipper/assets/manifest.py +++ b/scripts/flipper/assets/manifest.py @@ -135,6 +135,8 @@ class Manifest: def create(self, directory_path, ignore_files=["Manifest"]): for root, dirs, files in os.walk(directory_path): + dirs.sort() + files.sort() relative_root = root.replace(directory_path, "", 1) if relative_root.startswith("/"): relative_root = relative_root[1:]