Skip to content

Commit

Permalink
testing subghz dynamic limit based on freeheap + RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Dec 21, 2023
1 parent 8fa21c4 commit 77f458f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
15 changes: 9 additions & 6 deletions applications/main/subghz/subghz_history.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "subghz_history.h"
#include <lib/subghz/receiver.h>
#include <rpc/rpc.h>

#include <furi.h>

#define SUBGHZ_HISTORY_MAX 55
#define SUBGHZ_HISTORY_FREE_HEAP 20480
#define SUBGHZ_HISTORY_MAX 65530 // uint16_t index max, ram limit below
#define SUBGHZ_HISTORY_FREE_HEAP (10240 * (3 - MIN(rpc_get_sessions_count(instance->rpc), 2U)))
#define TAG "SubGhzHistory"

typedef struct {
Expand All @@ -29,13 +30,15 @@ struct SubGhzHistory {
uint8_t code_last_hash_data;
FuriString* tmp_string;
SubGhzHistoryStruct* history;
Rpc* rpc;
};

SubGhzHistory* subghz_history_alloc(void) {
SubGhzHistory* instance = malloc(sizeof(SubGhzHistory));
instance->tmp_string = furi_string_alloc();
instance->history = malloc(sizeof(SubGhzHistoryStruct));
SubGhzHistoryItemArray_init(instance->history->data);
instance->rpc = furi_record_open(RECORD_RPC);
return instance;
}

Expand All @@ -52,6 +55,7 @@ void subghz_history_free(SubGhzHistory* instance) {
}
SubGhzHistoryItemArray_clear(instance->history->data);
free(instance->history);
furi_record_close(RECORD_RPC);
free(instance);
}

Expand Down Expand Up @@ -143,15 +147,14 @@ FlipperFormat* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx
bool subghz_history_get_text_space_left(SubGhzHistory* instance, FuriString* output) {
furi_assert(instance);
if(memmgr_get_free_heap() < SUBGHZ_HISTORY_FREE_HEAP) {
if(output != NULL) furi_string_printf(output, " Free heap LOW");
if(output != NULL) furi_string_printf(output, " Memory is FULL");
return true;
}
if(instance->last_index_write == SUBGHZ_HISTORY_MAX) {
if(output != NULL) furi_string_printf(output, " Memory is FULL");
if(output != NULL) furi_string_printf(output, " History is FULL");
return true;
}
if(output != NULL)
furi_string_printf(output, "%02u/%02u", instance->last_index_write, SUBGHZ_HISTORY_MAX);
if(output != NULL) furi_string_printf(output, "%02u", instance->last_index_write);
return false;
}

Expand Down
22 changes: 20 additions & 2 deletions applications/main/subghz/views/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,16 @@ void subghz_view_receiver_draw(Canvas* canvas, SubGhzViewReceiverModel* model) {
#else
canvas_draw_str(canvas, 79, 62, furi_string_get_cstr(model->preset_str));
#endif
canvas_draw_str(canvas, 96, 62, furi_string_get_cstr(model->history_stat_str));
if(!furi_string_empty(model->history_stat_str)) {
canvas_draw_str_aligned(
canvas,
114,
62,
AlignRight,
AlignBottom,
furi_string_get_cstr(model->history_stat_str));
canvas_draw_icon(canvas, 116, 53, &I_sub1_10px);
}
canvas_set_font(canvas, FontSecondary);
elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
Expand Down Expand Up @@ -419,7 +428,16 @@ void subghz_view_receiver_draw(Canvas* canvas, SubGhzViewReceiverModel* model) {
#else
canvas_draw_str(canvas, 79, 62, furi_string_get_cstr(model->preset_str));
#endif
canvas_draw_str(canvas, 96, 62, furi_string_get_cstr(model->history_stat_str));
if(!furi_string_empty(model->history_stat_str)) {
canvas_draw_str_aligned(
canvas,
114,
62,
AlignRight,
AlignBottom,
furi_string_get_cstr(model->history_stat_str));
canvas_draw_icon(canvas, 116, 53, &I_sub1_10px);
}
} break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions applications/services/rpc/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct RpcSession {

struct Rpc {
FuriMutex* busy_mutex;
size_t sessions_count;
};

RpcOwner rpc_session_get_owner(RpcSession* session) {
Expand Down Expand Up @@ -407,13 +408,17 @@ RpcSession* rpc_session_open(Rpc* rpc, RpcOwner owner) {

furi_thread_start(session->thread);

rpc->sessions_count++;

return session;
}

void rpc_session_close(RpcSession* session) {
furi_assert(session);
furi_assert(session->rpc);

session->rpc->sessions_count--;

rpc_session_set_send_bytes_callback(session, NULL);
rpc_session_set_close_callback(session, NULL);
rpc_session_set_buffer_is_empty_callback(session, NULL);
Expand Down Expand Up @@ -489,3 +494,7 @@ void rpc_send_and_release_empty(RpcSession* session, uint32_t command_id, PB_Com
rpc_send_and_release(session, &message);
pb_release(&PB_Main_msg, &message);
}

size_t rpc_get_sessions_count(Rpc* rpc) {
return rpc->sessions_count;
}
7 changes: 7 additions & 0 deletions applications/services/rpc/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, uint3
*/
size_t rpc_session_get_available_size(RpcSession* session);

/** Get number of open RPC sessions
*
* @param rpc instance
* @return sessions count
*/
size_t rpc_get_sessions_count(Rpc* rpc);

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,7 @@ Function,-,rintl,long double,long double
Function,-,round,double,double
Function,+,roundf,float,float
Function,-,roundl,long double,long double
Function,+,rpc_get_sessions_count,size_t,Rpc*
Function,+,rpc_session_close,void,RpcSession*
Function,+,rpc_session_feed,size_t,"RpcSession*, uint8_t*, size_t, uint32_t"
Function,+,rpc_session_get_available_size,size_t,RpcSession*
Expand Down

0 comments on commit 77f458f

Please sign in to comment.