Skip to content

Commit 096c088

Browse files
vcp, cli: Handle Tx/Rx events before Connect/Disconnect + extra fixes (#4181)
* cli_vcp: handle tx/rx before connext/disconnect * cli_vcp: disable trace * cli_perf: advanced error reporting * cli_vcp: reset tx flag directly in event handler * fix formatting * cli_vcp: make tx flag volatile * storage_settings: fix scene ids * cli_shell: add safety check to set_prompt * cli_registry: move from bptree to dict, fix memory leak * cli_vcp: go back to message queue for event signaling * loader: move BeforeLoad event even earlier * fix formatting
1 parent 5f66425 commit 096c088

9 files changed

Lines changed: 92 additions & 82 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ PVS-Studio.log
6767

6868
# JS packages
6969
node_modules/
70+
71+
# cli_perf script output in case of errors
72+
/block.bin
73+
/return_block.bin

applications/services/cli/cli_vcp.c

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,23 @@ typedef struct {
3030
} CliVcpMessage;
3131

3232
typedef enum {
33-
CliVcpInternalEventConnected = (1 << 0),
34-
CliVcpInternalEventDisconnected = (1 << 1),
35-
CliVcpInternalEventTxDone = (1 << 2),
36-
CliVcpInternalEventRx = (1 << 3),
33+
CliVcpInternalEventConnected,
34+
CliVcpInternalEventDisconnected,
35+
CliVcpInternalEventTxDone,
36+
CliVcpInternalEventRx,
3737
} CliVcpInternalEvent;
3838

39-
#define CliVcpInternalEventAll \
40-
(CliVcpInternalEventConnected | CliVcpInternalEventDisconnected | CliVcpInternalEventTxDone | \
41-
CliVcpInternalEventRx)
42-
4339
struct CliVcp {
4440
FuriEventLoop* event_loop;
4541
FuriMessageQueue* message_queue; // <! external messages
46-
FuriThreadId thread_id;
42+
FuriMessageQueue* internal_evt_queue;
4743

4844
bool is_enabled, is_connected;
4945
FuriHalUsbInterface* previous_interface;
5046

5147
PipeSide* own_pipe;
5248
PipeSide* shell_pipe;
53-
bool is_currently_transmitting;
49+
volatile bool is_currently_transmitting;
5450
size_t previous_tx_length;
5551

5652
CliRegistry* main_registry;
@@ -101,11 +97,12 @@ static void cli_vcp_maybe_receive_data(CliVcp* cli_vcp) {
10197
// =============
10298

10399
static void cli_vcp_signal_internal_event(CliVcp* cli_vcp, CliVcpInternalEvent event) {
104-
furi_thread_flags_set(cli_vcp->thread_id, event);
100+
furi_check(furi_message_queue_put(cli_vcp->internal_evt_queue, &event, 0) == FuriStatusOk);
105101
}
106102

107103
static void cli_vcp_cdc_tx_done(void* context) {
108104
CliVcp* cli_vcp = context;
105+
cli_vcp->is_currently_transmitting = false;
109106
cli_vcp_signal_internal_event(cli_vcp, CliVcpInternalEventTxDone);
110107
}
111108

@@ -190,12 +187,25 @@ static void cli_vcp_message_received(FuriEventLoopObject* object, void* context)
190187
/**
191188
* Processes messages arriving from CDC event callbacks
192189
*/
193-
static void cli_vcp_internal_event_happened(void* context) {
190+
static void cli_vcp_internal_event_happened(FuriEventLoopObject* object, void* context) {
194191
CliVcp* cli_vcp = context;
195-
CliVcpInternalEvent event = furi_thread_flags_wait(CliVcpInternalEventAll, FuriFlagWaitAny, 0);
196-
furi_check(!(event & FuriFlagError));
192+
CliVcpInternalEvent event;
193+
furi_check(furi_message_queue_get(object, &event, 0) == FuriStatusOk);
194+
195+
switch(event) {
196+
case CliVcpInternalEventRx: {
197+
VCP_TRACE(TAG, "Rx");
198+
cli_vcp_maybe_receive_data(cli_vcp);
199+
break;
200+
}
197201

198-
if(event & CliVcpInternalEventDisconnected) {
202+
case CliVcpInternalEventTxDone: {
203+
VCP_TRACE(TAG, "TxDone");
204+
cli_vcp_maybe_send_data(cli_vcp);
205+
break;
206+
}
207+
208+
case CliVcpInternalEventDisconnected: {
199209
if(!cli_vcp->is_connected) return;
200210
FURI_LOG_D(TAG, "Disconnected");
201211
cli_vcp->is_connected = false;
@@ -204,9 +214,10 @@ static void cli_vcp_internal_event_happened(void* context) {
204214
pipe_detach_from_event_loop(cli_vcp->own_pipe);
205215
pipe_free(cli_vcp->own_pipe);
206216
cli_vcp->own_pipe = NULL;
217+
break;
207218
}
208219

209-
if(event & CliVcpInternalEventConnected) {
220+
case CliVcpInternalEventConnected: {
210221
if(cli_vcp->is_connected) return;
211222
FURI_LOG_D(TAG, "Connected");
212223
cli_vcp->is_connected = true;
@@ -233,17 +244,8 @@ static void cli_vcp_internal_event_happened(void* context) {
233244
cli_vcp->shell = cli_shell_alloc(
234245
cli_main_motd, NULL, cli_vcp->shell_pipe, cli_vcp->main_registry, &cli_main_ext_config);
235246
cli_shell_start(cli_vcp->shell);
247+
break;
236248
}
237-
238-
if(event & CliVcpInternalEventRx) {
239-
VCP_TRACE(TAG, "Rx");
240-
cli_vcp_maybe_receive_data(cli_vcp);
241-
}
242-
243-
if(event & CliVcpInternalEventTxDone) {
244-
VCP_TRACE(TAG, "TxDone");
245-
cli_vcp->is_currently_transmitting = false;
246-
cli_vcp_maybe_send_data(cli_vcp);
247249
}
248250
}
249251

@@ -253,7 +255,6 @@ static void cli_vcp_internal_event_happened(void* context) {
253255

254256
static CliVcp* cli_vcp_alloc(void) {
255257
CliVcp* cli_vcp = malloc(sizeof(CliVcp));
256-
cli_vcp->thread_id = furi_thread_get_current_id();
257258

258259
cli_vcp->event_loop = furi_event_loop_alloc();
259260

@@ -265,8 +266,14 @@ static CliVcp* cli_vcp_alloc(void) {
265266
cli_vcp_message_received,
266267
cli_vcp);
267268

268-
furi_event_loop_subscribe_thread_flags(
269-
cli_vcp->event_loop, cli_vcp_internal_event_happened, cli_vcp);
269+
cli_vcp->internal_evt_queue =
270+
furi_message_queue_alloc(VCP_MESSAGE_Q_LEN, sizeof(CliVcpInternalEvent));
271+
furi_event_loop_subscribe_message_queue(
272+
cli_vcp->event_loop,
273+
cli_vcp->internal_evt_queue,
274+
FuriEventLoopEventIn,
275+
cli_vcp_internal_event_happened,
276+
cli_vcp);
270277

271278
cli_vcp->main_registry = furi_record_open(RECORD_CLI);
272279

applications/services/loader/loader.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,6 @@ static void loader_start_internal_app(
418418
const FlipperInternalApplication* app,
419419
const char* args) {
420420
FURI_LOG_I(TAG, "Starting %s", app->name);
421-
LoaderEvent event;
422-
event.type = LoaderEventTypeApplicationBeforeLoad;
423-
furi_pubsub_publish(loader->pubsub, &event);
424421

425422
// store args
426423
furi_assert(loader->app.args == NULL);
@@ -508,10 +505,6 @@ static LoaderMessageLoaderStatusResult loader_start_external_app(
508505
result.value = loader_make_success_status(error_message);
509506
result.error = LoaderStatusErrorUnknown;
510507

511-
LoaderEvent event;
512-
event.type = LoaderEventTypeApplicationBeforeLoad;
513-
furi_pubsub_publish(loader->pubsub, &event);
514-
515508
do {
516509
loader->app.fap = flipper_application_alloc(storage, firmware_api_interface);
517510
size_t start = furi_get_tick();
@@ -566,6 +559,7 @@ static LoaderMessageLoaderStatusResult loader_start_external_app(
566559
if(result.value != LoaderStatusOk) {
567560
flipper_application_free(loader->app.fap);
568561
loader->app.fap = NULL;
562+
LoaderEvent event;
569563
event.type = LoaderEventTypeApplicationLoadFailed;
570564
furi_pubsub_publish(loader->pubsub, &event);
571565
}
@@ -615,6 +609,10 @@ static LoaderMessageLoaderStatusResult loader_do_start_by_name(
615609
status.value = loader_make_success_status(error_message);
616610
status.error = LoaderStatusErrorUnknown;
617611

612+
LoaderEvent event;
613+
event.type = LoaderEventTypeApplicationBeforeLoad;
614+
furi_pubsub_publish(loader->pubsub, &event);
615+
618616
do {
619617
// check lock
620618
if(loader_do_is_locked(loader)) {

applications/settings/storage_settings/storage_settings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const SubmenuSettingsHelperDescriptor descriptor_template = {
55
.options_cnt = 6,
66
.options =
77
{
8-
{.name = "About Internal Storage", .scene_id = StorageSettingsSDInfo},
9-
{.name = "About SD Card", .scene_id = StorageSettingsInternalInfo},
8+
{.name = "About Internal Storage", .scene_id = StorageSettingsInternalInfo},
9+
{.name = "About SD Card", .scene_id = StorageSettingsSDInfo},
1010
{.name = "Unmount SD Card", .scene_id = StorageSettingsUnmountConfirm},
1111
{.name = "Format SD Card", .scene_id = StorageSettingsFormatConfirm},
1212
{.name = "Benchmark SD Card", .scene_id = StorageSettingsBenchmarkConfirm},

lib/toolbox/cli/cli_registry.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
#include <toolbox/pipe.h>
44
#include <storage/storage.h>
55

6-
#define TAG "cli"
6+
#define TAG "CliRegistry"
77

88
struct CliRegistry {
9-
CliCommandTree_t commands;
9+
CliCommandDict_t commands;
1010
FuriMutex* mutex;
1111
};
1212

1313
CliRegistry* cli_registry_alloc(void) {
1414
CliRegistry* registry = malloc(sizeof(CliRegistry));
15-
CliCommandTree_init(registry->commands);
15+
CliCommandDict_init(registry->commands);
1616
registry->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
1717
return registry;
1818
}
1919

2020
void cli_registry_free(CliRegistry* registry) {
2121
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
2222
furi_mutex_free(registry->mutex);
23-
CliCommandTree_clear(registry->commands);
23+
CliCommandDict_clear(registry->commands);
2424
free(registry);
2525
}
2626

@@ -61,7 +61,7 @@ void cli_registry_add_command_ex(
6161
};
6262

6363
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
64-
CliCommandTree_set_at(registry->commands, name_str, command);
64+
CliCommandDict_set_at(registry->commands, name_str, command);
6565
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
6666

6767
furi_string_free(name_str);
@@ -79,7 +79,7 @@ void cli_registry_delete_command(CliRegistry* registry, const char* name) {
7979
} while(name_replace != FURI_STRING_FAILURE);
8080

8181
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
82-
CliCommandTree_erase(registry->commands, name_str);
82+
CliCommandDict_erase(registry->commands, name_str);
8383
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
8484

8585
furi_string_free(name_str);
@@ -91,7 +91,7 @@ bool cli_registry_get_command(
9191
CliRegistryCommand* result) {
9292
furi_assert(registry);
9393
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
94-
CliRegistryCommand* data = CliCommandTree_get(registry->commands, command);
94+
CliRegistryCommand* data = CliCommandDict_get(registry->commands, command);
9595
if(data) *result = *data;
9696

9797
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
@@ -103,16 +103,14 @@ void cli_registry_remove_external_commands(CliRegistry* registry) {
103103
furi_check(registry);
104104
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
105105

106-
// FIXME FL-3977: memory leak somewhere within this function
107-
108-
CliCommandTree_t internal_cmds;
109-
CliCommandTree_init(internal_cmds);
106+
CliCommandDict_t internal_cmds;
107+
CliCommandDict_init(internal_cmds);
110108
for
111-
M_EACH(item, registry->commands, CliCommandTree_t) {
112-
if(!(item->value_ptr->flags & CliCommandFlagExternal))
113-
CliCommandTree_set_at(internal_cmds, *item->key_ptr, *item->value_ptr);
109+
M_EACH(item, registry->commands, CliCommandDict_t) {
110+
if(!(item->value.flags & CliCommandFlagExternal))
111+
CliCommandDict_set_at(internal_cmds, item->key, item->value);
114112
}
115-
CliCommandTree_move(registry->commands, internal_cmds);
113+
CliCommandDict_move(registry->commands, internal_cmds);
116114

117115
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
118116
}
@@ -148,7 +146,7 @@ void cli_registry_reload_external_commands(
148146
.execute_callback = NULL,
149147
.flags = CliCommandFlagExternal,
150148
};
151-
CliCommandTree_set_at(registry->commands, plugin_name, command);
149+
CliCommandDict_set_at(registry->commands, plugin_name, command);
152150
}
153151

154152
furi_string_free(plugin_name);
@@ -172,7 +170,7 @@ void cli_registry_unlock(CliRegistry* registry) {
172170
furi_mutex_release(registry->mutex);
173171
}
174172

175-
CliCommandTree_t* cli_registry_get_commands(CliRegistry* registry) {
173+
CliCommandDict_t* cli_registry_get_commands(CliRegistry* registry) {
176174
furi_assert(registry);
177175
return &registry->commands;
178176
}

lib/toolbox/cli/cli_registry_i.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#pragma once
77

88
#include <furi.h>
9-
#include <m-bptree.h>
9+
#include <m-dict.h>
1010
#include "cli_registry.h"
1111

1212
#ifdef __cplusplus
@@ -22,19 +22,9 @@ typedef struct {
2222
size_t stack_depth;
2323
} CliRegistryCommand;
2424

25-
#define CLI_COMMANDS_TREE_RANK 4
25+
DICT_DEF2(CliCommandDict, FuriString*, FURI_STRING_OPLIST, CliRegistryCommand, M_POD_OPLIST);
2626

27-
// -V:BPTREE_DEF2:1103
28-
// -V:BPTREE_DEF2:524
29-
BPTREE_DEF2(
30-
CliCommandTree,
31-
CLI_COMMANDS_TREE_RANK,
32-
FuriString*,
33-
FURI_STRING_OPLIST,
34-
CliRegistryCommand,
35-
M_POD_OPLIST);
36-
37-
#define M_OPL_CliCommandTree_t() BPTREE_OPLIST2(CliCommandTree, FURI_STRING_OPLIST, M_POD_OPLIST)
27+
#define M_OPL_CliCommandDict_t() DICT_OPLIST(CliCommandDict, FURI_STRING_OPLIST, M_POD_OPLIST)
3828

3929
bool cli_registry_get_command(
4030
CliRegistry* registry,
@@ -48,7 +38,7 @@ void cli_registry_unlock(CliRegistry* registry);
4838
/**
4939
* @warning Surround calls to this function with `cli_registry_[un]lock`
5040
*/
51-
CliCommandTree_t* cli_registry_get_commands(CliRegistry* registry);
41+
CliCommandDict_t* cli_registry_get_commands(CliRegistry* registry);
5242

5343
#ifdef __cplusplus
5444
}

lib/toolbox/cli/shell/cli_shell.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ void cli_command_help(PipeSide* pipe, FuriString* args, void* context) {
103103

104104
printf("Available commands:\r\n" ANSI_FG_GREEN);
105105
cli_registry_lock(registry);
106-
CliCommandTree_t* commands = cli_registry_get_commands(registry);
107-
size_t commands_count = CliCommandTree_size(*commands);
106+
CliCommandDict_t* commands = cli_registry_get_commands(registry);
107+
size_t commands_count = CliCommandDict_size(*commands);
108108

109-
CliCommandTree_it_t iterator;
110-
CliCommandTree_it(iterator, *commands);
109+
CliCommandDict_it_t iterator;
110+
CliCommandDict_it(iterator, *commands);
111111
for(size_t i = 0; i < commands_count; i++) {
112-
const CliCommandTree_itref_t* item = CliCommandTree_cref(iterator);
113-
printf("%-30s", furi_string_get_cstr(*item->key_ptr));
114-
CliCommandTree_next(iterator);
112+
const CliCommandDict_itref_t* item = CliCommandDict_cref(iterator);
113+
printf("%-30s", furi_string_get_cstr(item->key));
114+
CliCommandDict_next(iterator);
115115

116116
if(i % columns == columns - 1) printf("\r\n");
117117
}
@@ -474,5 +474,6 @@ void cli_shell_join(CliShell* shell) {
474474

475475
void cli_shell_set_prompt(CliShell* shell, const char* prompt) {
476476
furi_check(shell);
477+
furi_check(furi_thread_get_state(shell->thread) == FuriThreadStateStopped);
477478
shell->prompt = prompt;
478479
}

lib/toolbox/cli/shell/cli_shell_completions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ void cli_shell_completions_fill_variants(CliShellCompletions* completions) {
111111
if(segment.type == CliShellCompletionSegmentTypeCommand) {
112112
CliRegistry* registry = completions->registry;
113113
cli_registry_lock(registry);
114-
CliCommandTree_t* commands = cli_registry_get_commands(registry);
114+
CliCommandDict_t* commands = cli_registry_get_commands(registry);
115115
for
116-
M_EACH(registered_command, *commands, CliCommandTree_t) {
117-
FuriString* command_name = *registered_command->key_ptr;
116+
M_EACH(registered_command, *commands, CliCommandDict_t) {
117+
FuriString* command_name = registered_command->key;
118118
if(furi_string_start_with(command_name, input)) {
119119
CommandCompletions_push_back(completions->variants, command_name);
120120
}

0 commit comments

Comments
 (0)