[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
10
applications/services/dolphin/application.fam
Normal file
10
applications/services/dolphin/application.fam
Normal file
@@ -0,0 +1,10 @@
|
||||
App(
|
||||
appid="dolphin",
|
||||
name="DolphinSrv",
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
entry_point="dolphin_srv",
|
||||
cdefines=["SRV_DOLPHIN"],
|
||||
stack_size=1 * 1024,
|
||||
order=50,
|
||||
sdk_headers=["dolphin.h"],
|
||||
)
|
209
applications/services/dolphin/dolphin.c
Normal file
209
applications/services/dolphin/dolphin.c
Normal file
@@ -0,0 +1,209 @@
|
||||
#include "dolphin/dolphin.h"
|
||||
#include "dolphin/helpers/dolphin_state.h"
|
||||
#include "dolphin_i.h"
|
||||
#include "portmacro.h"
|
||||
#include "projdefs.h"
|
||||
#include <furi_hal.h>
|
||||
#include <stdint.h>
|
||||
#include <furi.h>
|
||||
#define DOLPHIN_LOCK_EVENT_FLAG (0x1)
|
||||
|
||||
#define TAG "Dolphin"
|
||||
#define HOURS_IN_TICKS(x) ((x)*60 * 60 * 1000)
|
||||
|
||||
static void dolphin_update_clear_limits_timer_period(Dolphin* dolphin);
|
||||
|
||||
void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
|
||||
furi_assert(dolphin);
|
||||
DolphinEvent event;
|
||||
event.type = DolphinEventTypeDeed;
|
||||
event.deed = deed;
|
||||
dolphin_event_send_async(dolphin, &event);
|
||||
}
|
||||
|
||||
DolphinStats dolphin_stats(Dolphin* dolphin) {
|
||||
furi_assert(dolphin);
|
||||
|
||||
DolphinStats stats;
|
||||
DolphinEvent event;
|
||||
|
||||
event.type = DolphinEventTypeStats;
|
||||
event.stats = &stats;
|
||||
|
||||
dolphin_event_send_wait(dolphin, &event);
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
void dolphin_flush(Dolphin* dolphin) {
|
||||
furi_assert(dolphin);
|
||||
|
||||
DolphinEvent event;
|
||||
event.type = DolphinEventTypeFlush;
|
||||
|
||||
dolphin_event_send_wait(dolphin, &event);
|
||||
}
|
||||
|
||||
void dolphin_butthurt_timer_callback(TimerHandle_t xTimer) {
|
||||
Dolphin* dolphin = pvTimerGetTimerID(xTimer);
|
||||
furi_assert(dolphin);
|
||||
|
||||
DolphinEvent event;
|
||||
event.type = DolphinEventTypeIncreaseButthurt;
|
||||
dolphin_event_send_async(dolphin, &event);
|
||||
}
|
||||
|
||||
void dolphin_flush_timer_callback(TimerHandle_t xTimer) {
|
||||
Dolphin* dolphin = pvTimerGetTimerID(xTimer);
|
||||
furi_assert(dolphin);
|
||||
|
||||
DolphinEvent event;
|
||||
event.type = DolphinEventTypeFlush;
|
||||
dolphin_event_send_async(dolphin, &event);
|
||||
}
|
||||
|
||||
void dolphin_clear_limits_timer_callback(TimerHandle_t xTimer) {
|
||||
Dolphin* dolphin = pvTimerGetTimerID(xTimer);
|
||||
furi_assert(dolphin);
|
||||
|
||||
xTimerChangePeriod(dolphin->clear_limits_timer, HOURS_IN_TICKS(24), portMAX_DELAY);
|
||||
|
||||
DolphinEvent event;
|
||||
event.type = DolphinEventTypeClearLimits;
|
||||
dolphin_event_send_async(dolphin, &event);
|
||||
}
|
||||
|
||||
Dolphin* dolphin_alloc() {
|
||||
Dolphin* dolphin = malloc(sizeof(Dolphin));
|
||||
|
||||
dolphin->state = dolphin_state_alloc();
|
||||
dolphin->event_queue = furi_message_queue_alloc(8, sizeof(DolphinEvent));
|
||||
dolphin->pubsub = furi_pubsub_alloc();
|
||||
dolphin->butthurt_timer = xTimerCreate(
|
||||
NULL, HOURS_IN_TICKS(2 * 24), pdTRUE, dolphin, dolphin_butthurt_timer_callback);
|
||||
dolphin->flush_timer =
|
||||
xTimerCreate(NULL, 30 * 1000, pdFALSE, dolphin, dolphin_flush_timer_callback);
|
||||
dolphin->clear_limits_timer = xTimerCreate(
|
||||
NULL, HOURS_IN_TICKS(24), pdTRUE, dolphin, dolphin_clear_limits_timer_callback);
|
||||
|
||||
return dolphin;
|
||||
}
|
||||
|
||||
void dolphin_free(Dolphin* dolphin) {
|
||||
furi_assert(dolphin);
|
||||
|
||||
dolphin_state_free(dolphin->state);
|
||||
furi_message_queue_free(dolphin->event_queue);
|
||||
|
||||
free(dolphin);
|
||||
}
|
||||
|
||||
void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
|
||||
furi_assert(dolphin);
|
||||
furi_assert(event);
|
||||
event->flag = NULL;
|
||||
furi_check(
|
||||
furi_message_queue_put(dolphin->event_queue, event, FuriWaitForever) == FuriStatusOk);
|
||||
}
|
||||
|
||||
void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
|
||||
furi_assert(dolphin);
|
||||
furi_assert(event);
|
||||
event->flag = furi_event_flag_alloc();
|
||||
furi_check(event->flag);
|
||||
furi_check(
|
||||
furi_message_queue_put(dolphin->event_queue, event, FuriWaitForever) == FuriStatusOk);
|
||||
furi_check(
|
||||
furi_event_flag_wait(
|
||||
event->flag, DOLPHIN_LOCK_EVENT_FLAG, FuriFlagWaitAny, FuriWaitForever) ==
|
||||
DOLPHIN_LOCK_EVENT_FLAG);
|
||||
furi_event_flag_free(event->flag);
|
||||
}
|
||||
|
||||
void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event) {
|
||||
UNUSED(dolphin);
|
||||
if(event->flag) {
|
||||
furi_event_flag_set(event->flag, DOLPHIN_LOCK_EVENT_FLAG);
|
||||
}
|
||||
}
|
||||
|
||||
FuriPubSub* dolphin_get_pubsub(Dolphin* dolphin) {
|
||||
return dolphin->pubsub;
|
||||
}
|
||||
|
||||
static void dolphin_update_clear_limits_timer_period(Dolphin* dolphin) {
|
||||
furi_assert(dolphin);
|
||||
TickType_t now_ticks = xTaskGetTickCount();
|
||||
TickType_t timer_expires_at = xTimerGetExpiryTime(dolphin->clear_limits_timer);
|
||||
|
||||
if((timer_expires_at - now_ticks) > HOURS_IN_TICKS(0.1)) {
|
||||
FuriHalRtcDateTime date;
|
||||
furi_hal_rtc_get_datetime(&date);
|
||||
TickType_t now_time_in_ms = ((date.hour * 60 + date.minute) * 60 + date.second) * 1000;
|
||||
TickType_t time_to_clear_limits = 0;
|
||||
|
||||
if(date.hour < 5) {
|
||||
time_to_clear_limits = HOURS_IN_TICKS(5) - now_time_in_ms;
|
||||
} else {
|
||||
time_to_clear_limits = HOURS_IN_TICKS(24 + 5) - now_time_in_ms;
|
||||
}
|
||||
|
||||
xTimerChangePeriod(dolphin->clear_limits_timer, time_to_clear_limits, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t dolphin_srv(void* p) {
|
||||
UNUSED(p);
|
||||
Dolphin* dolphin = dolphin_alloc();
|
||||
furi_record_create(RECORD_DOLPHIN, dolphin);
|
||||
|
||||
dolphin_state_load(dolphin->state);
|
||||
xTimerReset(dolphin->butthurt_timer, portMAX_DELAY);
|
||||
dolphin_update_clear_limits_timer_period(dolphin);
|
||||
xTimerReset(dolphin->clear_limits_timer, portMAX_DELAY);
|
||||
|
||||
DolphinEvent event;
|
||||
while(1) {
|
||||
if(furi_message_queue_get(dolphin->event_queue, &event, HOURS_IN_TICKS(1)) ==
|
||||
FuriStatusOk) {
|
||||
if(event.type == DolphinEventTypeDeed) {
|
||||
dolphin_state_on_deed(dolphin->state, event.deed);
|
||||
DolphinPubsubEvent event = DolphinPubsubEventUpdate;
|
||||
furi_pubsub_publish(dolphin->pubsub, &event);
|
||||
xTimerReset(dolphin->butthurt_timer, portMAX_DELAY);
|
||||
xTimerReset(dolphin->flush_timer, portMAX_DELAY);
|
||||
} else if(event.type == DolphinEventTypeStats) {
|
||||
event.stats->icounter = dolphin->state->data.icounter;
|
||||
event.stats->butthurt = dolphin->state->data.butthurt;
|
||||
event.stats->timestamp = dolphin->state->data.timestamp;
|
||||
event.stats->level = dolphin_get_level(dolphin->state->data.icounter);
|
||||
event.stats->level_up_is_pending =
|
||||
!dolphin_state_xp_to_levelup(dolphin->state->data.icounter);
|
||||
} else if(event.type == DolphinEventTypeFlush) {
|
||||
FURI_LOG_I(TAG, "Flush stats");
|
||||
dolphin_state_save(dolphin->state);
|
||||
} else if(event.type == DolphinEventTypeClearLimits) {
|
||||
FURI_LOG_I(TAG, "Clear limits");
|
||||
dolphin_state_clear_limits(dolphin->state);
|
||||
dolphin_state_save(dolphin->state);
|
||||
} else if(event.type == DolphinEventTypeIncreaseButthurt) {
|
||||
FURI_LOG_I(TAG, "Increase butthurt");
|
||||
dolphin_state_butthurted(dolphin->state);
|
||||
dolphin_state_save(dolphin->state);
|
||||
}
|
||||
dolphin_event_release(dolphin, &event);
|
||||
} else {
|
||||
/* once per hour check rtc time is not changed */
|
||||
dolphin_update_clear_limits_timer_period(dolphin);
|
||||
}
|
||||
}
|
||||
|
||||
dolphin_free(dolphin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dolphin_upgrade_level(Dolphin* dolphin) {
|
||||
dolphin_state_increase_level(dolphin->state);
|
||||
dolphin_flush(dolphin);
|
||||
}
|
57
applications/services/dolphin/dolphin.h
Normal file
57
applications/services/dolphin/dolphin.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include <core/pubsub.h>
|
||||
#include "gui/view.h"
|
||||
#include "helpers/dolphin_deed.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RECORD_DOLPHIN "dolphin"
|
||||
|
||||
typedef struct Dolphin Dolphin;
|
||||
|
||||
typedef struct {
|
||||
uint32_t icounter;
|
||||
uint32_t butthurt;
|
||||
uint64_t timestamp;
|
||||
uint8_t level;
|
||||
bool level_up_is_pending;
|
||||
} DolphinStats;
|
||||
|
||||
typedef enum {
|
||||
DolphinPubsubEventUpdate,
|
||||
} DolphinPubsubEvent;
|
||||
|
||||
#define DOLPHIN_DEED(deed) \
|
||||
do { \
|
||||
Dolphin* dolphin = (Dolphin*)furi_record_open("dolphin"); \
|
||||
dolphin_deed(dolphin, deed); \
|
||||
furi_record_close("dolphin"); \
|
||||
} while(0)
|
||||
|
||||
/** Deed complete notification. Call it on deed completion.
|
||||
* See dolphin_deed.h for available deeds. In futures it will become part of assets.
|
||||
* Thread safe, async
|
||||
*/
|
||||
void dolphin_deed(Dolphin* dolphin, DolphinDeed deed);
|
||||
|
||||
/** Retrieve dolphin stats
|
||||
* Thread safe, blocking
|
||||
*/
|
||||
DolphinStats dolphin_stats(Dolphin* dolphin);
|
||||
|
||||
/** Flush dolphin queue and save state
|
||||
* Thread safe, blocking
|
||||
*/
|
||||
void dolphin_flush(Dolphin* dolphin);
|
||||
|
||||
void dolphin_upgrade_level(Dolphin* dolphin);
|
||||
|
||||
FuriPubSub* dolphin_get_pubsub(Dolphin* dolphin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
46
applications/services/dolphin/dolphin_i.h
Normal file
46
applications/services/dolphin/dolphin_i.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <core/pubsub.h>
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include "dolphin.h"
|
||||
#include "helpers/dolphin_state.h"
|
||||
|
||||
typedef enum {
|
||||
DolphinEventTypeDeed,
|
||||
DolphinEventTypeStats,
|
||||
DolphinEventTypeFlush,
|
||||
DolphinEventTypeIncreaseButthurt,
|
||||
DolphinEventTypeClearLimits,
|
||||
} DolphinEventType;
|
||||
|
||||
typedef struct {
|
||||
DolphinEventType type;
|
||||
FuriEventFlag* flag;
|
||||
union {
|
||||
DolphinDeed deed;
|
||||
DolphinStats* stats;
|
||||
};
|
||||
} DolphinEvent;
|
||||
|
||||
struct Dolphin {
|
||||
// State
|
||||
DolphinState* state;
|
||||
// Queue
|
||||
FuriMessageQueue* event_queue;
|
||||
FuriPubSub* pubsub;
|
||||
TimerHandle_t butthurt_timer;
|
||||
TimerHandle_t flush_timer;
|
||||
TimerHandle_t clear_limits_timer;
|
||||
};
|
||||
|
||||
Dolphin* dolphin_alloc();
|
||||
|
||||
void dolphin_free(Dolphin* dolphin);
|
||||
|
||||
void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event);
|
||||
|
||||
void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event);
|
||||
|
||||
void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event);
|
65
applications/services/dolphin/helpers/dolphin_deed.c
Normal file
65
applications/services/dolphin/helpers/dolphin_deed.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "dolphin_deed.h"
|
||||
#include <furi.h>
|
||||
|
||||
static const DolphinDeedWeight dolphin_deed_weights[] = {
|
||||
{1, DolphinAppSubGhz}, // DolphinDeedSubGhzReceiverInfo
|
||||
{3, DolphinAppSubGhz}, // DolphinDeedSubGhzSave
|
||||
{1, DolphinAppSubGhz}, // DolphinDeedSubGhzRawRec
|
||||
{2, DolphinAppSubGhz}, // DolphinDeedSubGhzAddManually
|
||||
{2, DolphinAppSubGhz}, // DolphinDeedSubGhzSend
|
||||
{1, DolphinAppSubGhz}, // DolphinDeedSubGhzFrequencyAnalyzer
|
||||
|
||||
{1, DolphinAppRfid}, // DolphinDeedRfidRead
|
||||
{3, DolphinAppRfid}, // DolphinDeedRfidReadSuccess
|
||||
{3, DolphinAppRfid}, // DolphinDeedRfidSave
|
||||
{2, DolphinAppRfid}, // DolphinDeedRfidEmulate
|
||||
{2, DolphinAppRfid}, // DolphinDeedRfidAdd
|
||||
|
||||
{1, DolphinAppNfc}, // DolphinDeedNfcRead
|
||||
{3, DolphinAppNfc}, // DolphinDeedNfcReadSuccess
|
||||
{3, DolphinAppNfc}, // DolphinDeedNfcSave
|
||||
{2, DolphinAppNfc}, // DolphinDeedNfcEmulate
|
||||
{2, DolphinAppNfc}, // DolphinDeedNfcAdd
|
||||
|
||||
{1, DolphinAppIr}, // DolphinDeedIrSend
|
||||
{3, DolphinAppIr}, // DolphinDeedIrLearnSuccess
|
||||
{3, DolphinAppIr}, // DolphinDeedIrSave
|
||||
{2, DolphinAppIr}, // DolphinDeedIrBruteForce
|
||||
|
||||
{1, DolphinAppIbutton}, // DolphinDeedIbuttonRead
|
||||
{3, DolphinAppIbutton}, // DolphinDeedIbuttonReadSuccess
|
||||
{3, DolphinAppIbutton}, // DolphinDeedIbuttonSave
|
||||
{2, DolphinAppIbutton}, // DolphinDeedIbuttonEmulate
|
||||
{2, DolphinAppIbutton}, // DolphinDeedIbuttonAdd
|
||||
|
||||
{3, DolphinAppBadusb}, // DolphinDeedBadUsbPlayScript
|
||||
{3, DolphinAppU2f}, // DolphinDeedU2fAuthorized
|
||||
};
|
||||
|
||||
static uint8_t dolphin_deed_limits[] = {
|
||||
15, // DolphinAppSubGhz
|
||||
15, // DolphinAppRfid
|
||||
15, // DolphinAppNfc
|
||||
15, // DolphinAppIr
|
||||
15, // DolphinAppIbutton
|
||||
15, // DolphinAppBadusb
|
||||
15, // DolphinAppU2f
|
||||
};
|
||||
|
||||
_Static_assert(COUNT_OF(dolphin_deed_weights) == DolphinDeedMAX, "dolphin_deed_weights size error");
|
||||
_Static_assert(COUNT_OF(dolphin_deed_limits) == DolphinAppMAX, "dolphin_deed_limits size error");
|
||||
|
||||
uint8_t dolphin_deed_get_weight(DolphinDeed deed) {
|
||||
furi_check(deed < DolphinDeedMAX);
|
||||
return dolphin_deed_weights[deed].icounter;
|
||||
}
|
||||
|
||||
DolphinApp dolphin_deed_get_app(DolphinDeed deed) {
|
||||
furi_check(deed < DolphinDeedMAX);
|
||||
return dolphin_deed_weights[deed].app;
|
||||
}
|
||||
|
||||
uint8_t dolphin_deed_get_app_limit(DolphinApp app) {
|
||||
furi_check(app < DolphinAppMAX);
|
||||
return dolphin_deed_limits[app];
|
||||
}
|
77
applications/services/dolphin/helpers/dolphin_deed.h
Normal file
77
applications/services/dolphin/helpers/dolphin_deed.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
DolphinAppSubGhz,
|
||||
DolphinAppRfid,
|
||||
DolphinAppNfc,
|
||||
DolphinAppIr,
|
||||
DolphinAppIbutton,
|
||||
DolphinAppBadusb,
|
||||
DolphinAppU2f,
|
||||
DolphinAppMAX,
|
||||
} DolphinApp;
|
||||
|
||||
typedef enum {
|
||||
DolphinDeedSubGhzReceiverInfo,
|
||||
DolphinDeedSubGhzSave,
|
||||
DolphinDeedSubGhzRawRec,
|
||||
DolphinDeedSubGhzAddManually,
|
||||
DolphinDeedSubGhzSend,
|
||||
DolphinDeedSubGhzFrequencyAnalyzer,
|
||||
|
||||
DolphinDeedRfidRead,
|
||||
DolphinDeedRfidReadSuccess,
|
||||
DolphinDeedRfidSave,
|
||||
DolphinDeedRfidEmulate,
|
||||
DolphinDeedRfidAdd,
|
||||
|
||||
DolphinDeedNfcRead,
|
||||
DolphinDeedNfcReadSuccess,
|
||||
DolphinDeedNfcSave,
|
||||
DolphinDeedNfcEmulate,
|
||||
DolphinDeedNfcAdd,
|
||||
|
||||
DolphinDeedIrSend,
|
||||
DolphinDeedIrLearnSuccess,
|
||||
DolphinDeedIrSave,
|
||||
DolphinDeedIrBruteForce,
|
||||
|
||||
DolphinDeedIbuttonRead,
|
||||
DolphinDeedIbuttonReadSuccess,
|
||||
DolphinDeedIbuttonSave,
|
||||
DolphinDeedIbuttonEmulate,
|
||||
DolphinDeedIbuttonAdd,
|
||||
|
||||
DolphinDeedBadUsbPlayScript,
|
||||
|
||||
DolphinDeedU2fAuthorized,
|
||||
|
||||
DolphinDeedMAX,
|
||||
|
||||
DolphinDeedTestLeft,
|
||||
DolphinDeedTestRight,
|
||||
} DolphinDeed;
|
||||
|
||||
typedef struct {
|
||||
uint8_t icounter;
|
||||
DolphinApp app;
|
||||
} DolphinDeedWeight;
|
||||
|
||||
typedef struct {
|
||||
DolphinApp app;
|
||||
uint8_t icounter_limit;
|
||||
} DolphinDeedLimits;
|
||||
|
||||
DolphinApp dolphin_deed_get_app(DolphinDeed deed);
|
||||
uint8_t dolphin_deed_get_app_limit(DolphinApp app);
|
||||
uint8_t dolphin_deed_get_weight(DolphinDeed deed);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
201
applications/services/dolphin/helpers/dolphin_state.c
Normal file
201
applications/services/dolphin/helpers/dolphin_state.c
Normal file
@@ -0,0 +1,201 @@
|
||||
#include "dolphin_state.h"
|
||||
#include "dolphin/helpers/dolphin_deed.h"
|
||||
#include "dolphin_state_filename.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <storage/storage.h>
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <math.h>
|
||||
#include <toolbox/saved_struct.h>
|
||||
|
||||
#define TAG "DolphinState"
|
||||
|
||||
#define DOLPHIN_STATE_PATH INT_PATH(DOLPHIN_STATE_FILE_NAME)
|
||||
#define DOLPHIN_STATE_HEADER_MAGIC 0xD0
|
||||
#define DOLPHIN_STATE_HEADER_VERSION 0x01
|
||||
#define LEVEL2_THRESHOLD 300
|
||||
#define LEVEL3_THRESHOLD 1800
|
||||
#define BUTTHURT_MAX 14
|
||||
#define BUTTHURT_MIN 0
|
||||
|
||||
DolphinState* dolphin_state_alloc() {
|
||||
return malloc(sizeof(DolphinState));
|
||||
}
|
||||
|
||||
void dolphin_state_free(DolphinState* dolphin_state) {
|
||||
free(dolphin_state);
|
||||
}
|
||||
|
||||
bool dolphin_state_save(DolphinState* dolphin_state) {
|
||||
if(!dolphin_state->dirty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool result = saved_struct_save(
|
||||
DOLPHIN_STATE_PATH,
|
||||
&dolphin_state->data,
|
||||
sizeof(DolphinStoreData),
|
||||
DOLPHIN_STATE_HEADER_MAGIC,
|
||||
DOLPHIN_STATE_HEADER_VERSION);
|
||||
|
||||
if(result) {
|
||||
FURI_LOG_I(TAG, "State saved");
|
||||
dolphin_state->dirty = false;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to save state");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool dolphin_state_load(DolphinState* dolphin_state) {
|
||||
bool success = saved_struct_load(
|
||||
DOLPHIN_STATE_PATH,
|
||||
&dolphin_state->data,
|
||||
sizeof(DolphinStoreData),
|
||||
DOLPHIN_STATE_HEADER_MAGIC,
|
||||
DOLPHIN_STATE_HEADER_VERSION);
|
||||
|
||||
if(success) {
|
||||
if((dolphin_state->data.butthurt > BUTTHURT_MAX) ||
|
||||
(dolphin_state->data.butthurt < BUTTHURT_MIN)) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!success) {
|
||||
FURI_LOG_W(TAG, "Reset dolphin-state");
|
||||
memset(dolphin_state, 0, sizeof(*dolphin_state));
|
||||
dolphin_state->dirty = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
uint64_t dolphin_state_timestamp() {
|
||||
FuriHalRtcDateTime datetime;
|
||||
furi_hal_rtc_get_datetime(&datetime);
|
||||
return furi_hal_rtc_datetime_to_timestamp(&datetime);
|
||||
}
|
||||
|
||||
bool dolphin_state_is_levelup(uint32_t icounter) {
|
||||
return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD);
|
||||
}
|
||||
|
||||
uint8_t dolphin_get_level(uint32_t icounter) {
|
||||
if(icounter <= LEVEL2_THRESHOLD) {
|
||||
return 1;
|
||||
} else if(icounter <= LEVEL3_THRESHOLD) {
|
||||
return 2;
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter) {
|
||||
uint32_t threshold = 0;
|
||||
if(icounter <= LEVEL2_THRESHOLD) {
|
||||
threshold = 0;
|
||||
} else if(icounter <= LEVEL3_THRESHOLD) {
|
||||
threshold = LEVEL2_THRESHOLD + 1;
|
||||
} else {
|
||||
threshold = LEVEL3_THRESHOLD + 1;
|
||||
}
|
||||
return icounter - threshold;
|
||||
}
|
||||
|
||||
uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
|
||||
uint32_t threshold = 0;
|
||||
if(icounter <= LEVEL2_THRESHOLD) {
|
||||
threshold = LEVEL2_THRESHOLD;
|
||||
} else if(icounter <= LEVEL3_THRESHOLD) {
|
||||
threshold = LEVEL3_THRESHOLD;
|
||||
} else {
|
||||
threshold = (uint32_t)-1;
|
||||
}
|
||||
return threshold - icounter;
|
||||
}
|
||||
|
||||
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
|
||||
// Special case for testing
|
||||
if(deed > DolphinDeedMAX) {
|
||||
if(deed == DolphinDeedTestLeft) {
|
||||
dolphin_state->data.butthurt =
|
||||
CLAMP(dolphin_state->data.butthurt + 1, BUTTHURT_MAX, BUTTHURT_MIN);
|
||||
if(dolphin_state->data.icounter > 0) dolphin_state->data.icounter--;
|
||||
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
||||
dolphin_state->dirty = true;
|
||||
} else if(deed == DolphinDeedTestRight) {
|
||||
dolphin_state->data.butthurt = BUTTHURT_MIN;
|
||||
if(dolphin_state->data.icounter < UINT32_MAX) dolphin_state->data.icounter++;
|
||||
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
||||
dolphin_state->dirty = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
DolphinApp app = dolphin_deed_get_app(deed);
|
||||
int8_t weight_limit =
|
||||
dolphin_deed_get_app_limit(app) - dolphin_state->data.icounter_daily_limit[app];
|
||||
uint8_t deed_weight = CLAMP(dolphin_deed_get_weight(deed), weight_limit, 0);
|
||||
|
||||
uint32_t xp_to_levelup = dolphin_state_xp_to_levelup(dolphin_state->data.icounter);
|
||||
if(xp_to_levelup) {
|
||||
deed_weight = MIN(xp_to_levelup, deed_weight);
|
||||
dolphin_state->data.icounter += deed_weight;
|
||||
dolphin_state->data.icounter_daily_limit[app] += deed_weight;
|
||||
}
|
||||
|
||||
/* decrease butthurt:
|
||||
* 0 deeds accumulating --> 0 butthurt
|
||||
* +1....+15 deeds accumulating --> -1 butthurt
|
||||
* +16...+30 deeds accumulating --> -1 butthurt
|
||||
* +31...+45 deeds accumulating --> -1 butthurt
|
||||
* +46...... deeds accumulating --> -1 butthurt
|
||||
* -4 butthurt per day is maximum
|
||||
* */
|
||||
uint8_t butthurt_icounter_level_old = dolphin_state->data.butthurt_daily_limit / 15 +
|
||||
!!(dolphin_state->data.butthurt_daily_limit % 15);
|
||||
dolphin_state->data.butthurt_daily_limit =
|
||||
CLAMP(dolphin_state->data.butthurt_daily_limit + deed_weight, 46, 0);
|
||||
uint8_t butthurt_icounter_level_new = dolphin_state->data.butthurt_daily_limit / 15 +
|
||||
!!(dolphin_state->data.butthurt_daily_limit % 15);
|
||||
int32_t new_butthurt = ((int32_t)dolphin_state->data.butthurt) -
|
||||
(butthurt_icounter_level_old != butthurt_icounter_level_new);
|
||||
new_butthurt = CLAMP(new_butthurt, BUTTHURT_MAX, BUTTHURT_MIN);
|
||||
|
||||
dolphin_state->data.butthurt = new_butthurt;
|
||||
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
||||
dolphin_state->dirty = true;
|
||||
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"icounter %d, butthurt %d",
|
||||
dolphin_state->data.icounter,
|
||||
dolphin_state->data.butthurt);
|
||||
}
|
||||
|
||||
void dolphin_state_butthurted(DolphinState* dolphin_state) {
|
||||
if(dolphin_state->data.butthurt < BUTTHURT_MAX) {
|
||||
dolphin_state->data.butthurt++;
|
||||
dolphin_state->data.timestamp = dolphin_state_timestamp();
|
||||
dolphin_state->dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void dolphin_state_increase_level(DolphinState* dolphin_state) {
|
||||
furi_assert(dolphin_state_is_levelup(dolphin_state->data.icounter));
|
||||
++dolphin_state->data.icounter;
|
||||
dolphin_state->dirty = true;
|
||||
}
|
||||
|
||||
void dolphin_state_clear_limits(DolphinState* dolphin_state) {
|
||||
furi_assert(dolphin_state);
|
||||
|
||||
for(int i = 0; i < DolphinAppMAX; ++i) {
|
||||
dolphin_state->data.icounter_daily_limit[i] = 0;
|
||||
}
|
||||
dolphin_state->data.butthurt_daily_limit = 0;
|
||||
dolphin_state->dirty = true;
|
||||
}
|
48
applications/services/dolphin/helpers/dolphin_state.h
Normal file
48
applications/services/dolphin/helpers/dolphin_state.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "dolphin_deed.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct DolphinState DolphinState;
|
||||
typedef struct {
|
||||
uint8_t icounter_daily_limit[DolphinAppMAX];
|
||||
uint8_t butthurt_daily_limit;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t icounter;
|
||||
int32_t butthurt;
|
||||
uint64_t timestamp;
|
||||
} DolphinStoreData;
|
||||
|
||||
struct DolphinState {
|
||||
DolphinStoreData data;
|
||||
bool dirty;
|
||||
};
|
||||
|
||||
DolphinState* dolphin_state_alloc();
|
||||
|
||||
void dolphin_state_free(DolphinState* dolphin_state);
|
||||
|
||||
bool dolphin_state_save(DolphinState* dolphin_state);
|
||||
|
||||
bool dolphin_state_load(DolphinState* dolphin_state);
|
||||
|
||||
void dolphin_state_clear_limits(DolphinState* dolphin_state);
|
||||
|
||||
uint64_t dolphin_state_timestamp();
|
||||
|
||||
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed);
|
||||
|
||||
void dolphin_state_butthurted(DolphinState* dolphin_state);
|
||||
|
||||
uint32_t dolphin_state_xp_to_levelup(uint32_t icounter);
|
||||
|
||||
uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter);
|
||||
|
||||
bool dolphin_state_is_levelup(uint32_t icounter);
|
||||
|
||||
void dolphin_state_increase_level(DolphinState* dolphin_state);
|
||||
|
||||
uint8_t dolphin_get_level(uint32_t icounter);
|
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define DOLPHIN_STATE_FILE_NAME ".dolphin.state"
|
Reference in New Issue
Block a user