Skip to content

Commit 4a77d64

Browse files
authored
Refactoring 6 6 (#157)
1 parent 74ba300 commit 4a77d64

File tree

8 files changed

+148
-16
lines changed

8 files changed

+148
-16
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![issues - flipper-zero_authenticator](https://img.shields.io/github/issues/akopachov/flipper-zero_authenticator)](https://github.com/akopachov/flipper-zero_authenticator/issues)
66
![maintained - yes](https://img.shields.io/badge/maintained-yes-blue)
77
![contributions - welcome](https://img.shields.io/badge/contributions-welcome-blue)
8-
[![Discord server](https://img.shields.io/discord/937479784148115456)](https://discord.gg/flipperzero-unofficial)
8+
[![Discord server](https://img.shields.io/discord/937479784148115456)](https://discord.gg/flipper-xtreme)
99
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=akopachov_flipper-zero_authenticator&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=akopachov_flipper-zero_authenticator)
1010
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=akopachov_flipper-zero_authenticator&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=akopachov_flipper-zero_authenticator)
1111
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=akopachov_flipper-zero_authenticator&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=akopachov_flipper-zero_authenticator)
@@ -37,5 +37,5 @@ It is like [Google Authenticator](https://play.google.com/store/apps/details?id=
3737

3838
## Have questions?
3939

40-
Checkout [FAQ](https://github.com/akopachov/flipper-zero_authenticator/wiki/FAQ) or ask in [Discord channel](https://discord.gg/flipperzero-unofficial)
40+
Checkout [FAQ](https://github.com/akopachov/flipper-zero_authenticator/wiki/FAQ) or ask in [Discord channel](https://discord.gg/flipper-xtreme)
4141

totp/features_config.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Application automatic lock timeout if user IDLE. (ticks)
2+
#ifndef TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC
3+
#define TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC (60)
4+
#endif
5+
16
// Include Bluetooth token input automation
27
#ifndef TOTP_NO_BADBT_TYPE
38
#define TOTP_BADBT_TYPE_ENABLED
@@ -46,4 +51,4 @@
4651
// Active font for TOTP codes
4752
#ifndef TOTP_FONT
4853
#define TOTP_FONT TOTP_FONT_MODENINE
49-
#endif
54+
#endif

totp/services/crypto/crypto.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ CryptoSeedIVResult
9191
max_i = uid_size;
9292
}
9393

94-
const uint8_t* uid = (const uint8_t*)UID64_BASE;
94+
const uint8_t* uid = (const uint8_t*)UID64_BASE; //-V566
9595
for(uint8_t i = 0; i < max_i; i++) {
9696
plugin_state->iv[i] = plugin_state->iv[i] ^ uid[i];
9797
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "idle_timeout.h"
2+
#include <stdlib.h>
3+
#include <furi/core/timer.h>
4+
5+
#define IDLE_TIMER_CHECK_PERIODICITY_SEC (1)
6+
#define SEC_TO_TICKS(sec) ((sec) * 1000)
7+
8+
struct IdleTimeoutContext {
9+
FuriTimer* timer;
10+
bool activity_reported;
11+
void* on_idle_callback_context;
12+
IDLE_TIMEOUT_CALLBACK on_idle_callback;
13+
uint16_t timeout_sec;
14+
uint16_t idle_period_sec;
15+
bool idle_handled;
16+
};
17+
18+
static void idle_timer_callback(void* context) {
19+
IdleTimeoutContext* instance = context;
20+
if (instance->activity_reported) {
21+
instance->idle_period_sec = 0;
22+
instance->idle_handled = false;
23+
instance->activity_reported = false;
24+
} else if (!instance->idle_handled) {
25+
if (instance->idle_period_sec >= instance->timeout_sec) {
26+
instance->idle_handled = instance->on_idle_callback(instance->on_idle_callback_context);
27+
} else {
28+
instance->idle_period_sec += IDLE_TIMER_CHECK_PERIODICITY_SEC;
29+
}
30+
}
31+
}
32+
33+
IdleTimeoutContext* idle_timeout_alloc(uint16_t timeout_sec, IDLE_TIMEOUT_CALLBACK on_idle_callback, void* on_idle_callback_context) {
34+
IdleTimeoutContext* instance = malloc(sizeof(IdleTimeoutContext));
35+
if (instance == NULL) return NULL;
36+
37+
instance->timer = furi_timer_alloc(&idle_timer_callback, FuriTimerTypePeriodic, instance);
38+
if (instance->timer == NULL) return NULL;
39+
40+
instance->timeout_sec = timeout_sec;
41+
instance->on_idle_callback = on_idle_callback;
42+
instance->on_idle_callback_context = on_idle_callback_context;
43+
return instance;
44+
}
45+
46+
void idle_timeout_start(IdleTimeoutContext* context) {
47+
furi_timer_start(context->timer, SEC_TO_TICKS(IDLE_TIMER_CHECK_PERIODICITY_SEC));
48+
}
49+
50+
void idle_timeout_stop(IdleTimeoutContext* context) {
51+
furi_timer_stop(context->timer);
52+
}
53+
54+
void idle_timeout_report_activity(IdleTimeoutContext* context) {
55+
context->activity_reported = true;
56+
}
57+
58+
void idle_timeout_free(IdleTimeoutContext* context) {
59+
furi_timer_stop(context->timer);
60+
furi_timer_free(context->timer);
61+
free(context);
62+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <inttypes.h>
4+
#include <stdbool.h>
5+
6+
typedef struct IdleTimeoutContext IdleTimeoutContext;
7+
8+
typedef bool (*IDLE_TIMEOUT_CALLBACK)(void* context);
9+
10+
/**
11+
* @brief Initializes a new instance of IDLE timeout
12+
* @param timeout_sec IDLE timeout in seconds
13+
* @param on_idle_callback callback function to trigger when IDLE timeout happened
14+
* @param on_idle_callback_context callback function context
15+
* @return IDLE timeout context
16+
*/
17+
IdleTimeoutContext* idle_timeout_alloc(uint16_t timeout_sec, IDLE_TIMEOUT_CALLBACK on_idle_callback, void* on_idle_callback_context);
18+
19+
/**
20+
* @brief Starts IDLE timeout
21+
* @param context IDLE timeout context
22+
*/
23+
void idle_timeout_start(IdleTimeoutContext* context);
24+
25+
/**
26+
* @brief Stops IDLE timeout
27+
* @param context IDLE timeout context
28+
*/
29+
void idle_timeout_stop(IdleTimeoutContext* context);
30+
31+
/**
32+
* @brief Reports activity to IDLE timeout
33+
* @param context IDLE timeout context
34+
*/
35+
void idle_timeout_report_activity(IdleTimeoutContext* context);
36+
37+
/**
38+
* @brief Disposes IDLE timeout and releases all the resources
39+
* @param context IDLE timeout context
40+
*/
41+
void idle_timeout_free(IdleTimeoutContext* context);

totp/totp_app.c

+26-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include "services/crypto/crypto.h"
1919
#include "cli/cli.h"
2020

21-
#define IDLE_TIMEOUT (60000)
22-
2321
static void render_callback(Canvas* const canvas, void* ctx) {
2422
furi_assert(ctx);
2523
PluginState* plugin_state = ctx;
@@ -106,6 +104,17 @@ static bool totp_activate_initial_scene(PluginState* const plugin_state) {
106104
return true;
107105
}
108106

107+
static bool on_user_idle(void* context) {
108+
PluginState* plugin_state = context;
109+
if(plugin_state->current_scene != TotpSceneAuthentication &&
110+
plugin_state->current_scene != TotpSceneStandby) {
111+
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
112+
return true;
113+
}
114+
115+
return false;
116+
}
117+
109118
static bool totp_plugin_state_init(PluginState* const plugin_state) {
110119
plugin_state->gui = furi_record_open(RECORD_GUI);
111120
plugin_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
@@ -127,10 +136,22 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
127136
}
128137
#endif
129138

139+
if (plugin_state->pin_set) {
140+
plugin_state->idle_timeout_context = idle_timeout_alloc(TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC, &on_user_idle, plugin_state);
141+
idle_timeout_start(plugin_state->idle_timeout_context);
142+
} else {
143+
plugin_state->idle_timeout_context = NULL;
144+
}
145+
130146
return true;
131147
}
132148

133149
static void totp_plugin_state_free(PluginState* plugin_state) {
150+
if (plugin_state->idle_timeout_context != NULL) {
151+
idle_timeout_stop(plugin_state->idle_timeout_context);
152+
idle_timeout_free(plugin_state->idle_timeout_context);
153+
}
154+
134155
furi_record_close(RECORD_GUI);
135156
furi_record_close(RECORD_NOTIFICATION);
136157
furi_record_close(RECORD_DIALOGS);
@@ -184,26 +205,20 @@ int32_t totp_app() {
184205

185206
PluginEvent event;
186207
bool processing = true;
187-
uint32_t last_user_interaction_time = furi_get_tick();
188208
while(processing) {
189-
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
209+
FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
190210

191211
if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
192212
if(event_status == FuriStatusOk) {
193-
if(event.type == EventTypeKey) {
194-
last_user_interaction_time = furi_get_tick();
213+
if(event.type == EventTypeKey && plugin_state->idle_timeout_context != NULL) {
214+
idle_timeout_report_activity(plugin_state->idle_timeout_context);
195215
}
196216

197217
if(event.type == EventForceCloseApp) {
198218
processing = false;
199219
} else {
200220
processing = totp_scene_director_handle_event(&event, plugin_state);
201221
}
202-
} else if(
203-
plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
204-
plugin_state->current_scene != TotpSceneStandby &&
205-
furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
206-
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
207222
}
208223

209224
view_port_update(view_port);

totp/types/plugin_state.h

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../features_config.h"
77
#include "../ui/totp_scenes_enum.h"
88
#include "../services/config/config_file_context.h"
9+
#include "../services/idle_timeout/idle_timeout.h"
910
#include "notification_method.h"
1011
#include "automation_method.h"
1112
#ifdef TOTP_BADBT_TYPE_ENABLED
@@ -48,6 +49,9 @@ typedef struct {
4849
*/
4950
float timezone_offset;
5051

52+
/**
53+
* @brief Config file context
54+
*/
5155
ConfigFileContext* config_file_context;
5256

5357
/**
@@ -96,4 +100,9 @@ typedef struct {
96100
*/
97101
TotpBtTypeCodeWorkerContext* bt_type_code_worker_context;
98102
#endif
103+
104+
/**
105+
* @brief IDLE timeout context
106+
*/
107+
IdleTimeoutContext* idle_timeout_context;
99108
} PluginState;

totp/workers/bt_type_code/bt_type_code.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void totp_type_code_worker_bt_set_app_mac(uint8_t* mac) {
4949
max_i = TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN;
5050
}
5151

52-
const uint8_t* uid = (const uint8_t*)UID64_BASE;
52+
const uint8_t* uid = (const uint8_t*)UID64_BASE; //-V566
5353
memcpy(mac, uid, max_i);
5454
for(uint8_t i = max_i; i < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN; i++) {
5555
mac[i] = 0;

0 commit comments

Comments
 (0)