1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 12:42:30 +04:00

Merge remote-tracking branch 'OFW/porta/cli-event-ordering' into dev

This commit is contained in:
MX
2025-04-10 19:56:39 +03:00

View File

@@ -30,20 +30,16 @@ typedef struct {
} CliVcpMessage; } CliVcpMessage;
typedef enum { typedef enum {
CliVcpInternalEventConnected = (1 << 0), CliVcpInternalEventConnected,
CliVcpInternalEventDisconnected = (1 << 1), CliVcpInternalEventDisconnected,
CliVcpInternalEventTxDone = (1 << 2), CliVcpInternalEventTxDone,
CliVcpInternalEventRx = (1 << 3), CliVcpInternalEventRx,
} CliVcpInternalEvent; } CliVcpInternalEvent;
#define CliVcpInternalEventAll \
(CliVcpInternalEventConnected | CliVcpInternalEventDisconnected | CliVcpInternalEventTxDone | \
CliVcpInternalEventRx)
struct CliVcp { struct CliVcp {
FuriEventLoop* event_loop; FuriEventLoop* event_loop;
FuriMessageQueue* message_queue; // <! external messages FuriMessageQueue* message_queue; // <! external messages
FuriThreadId thread_id; FuriMessageQueue* internal_evt_queue;
bool is_enabled, is_connected; bool is_enabled, is_connected;
FuriHalUsbInterface* previous_interface; FuriHalUsbInterface* previous_interface;
@@ -101,7 +97,7 @@ static void cli_vcp_maybe_receive_data(CliVcp* cli_vcp) {
// ============= // =============
static void cli_vcp_signal_internal_event(CliVcp* cli_vcp, CliVcpInternalEvent event) { static void cli_vcp_signal_internal_event(CliVcp* cli_vcp, CliVcpInternalEvent event) {
furi_thread_flags_set(cli_vcp->thread_id, event); furi_check(furi_message_queue_put(cli_vcp->internal_evt_queue, &event, 0) == FuriStatusOk);
} }
static void cli_vcp_cdc_tx_done(void* context) { static void cli_vcp_cdc_tx_done(void* context) {
@@ -191,22 +187,25 @@ static void cli_vcp_message_received(FuriEventLoopObject* object, void* context)
/** /**
* Processes messages arriving from CDC event callbacks * Processes messages arriving from CDC event callbacks
*/ */
static void cli_vcp_internal_event_happened(void* context) { static void cli_vcp_internal_event_happened(FuriEventLoopObject* object, void* context) {
CliVcp* cli_vcp = context; CliVcp* cli_vcp = context;
CliVcpInternalEvent event = furi_thread_flags_wait(CliVcpInternalEventAll, FuriFlagWaitAny, 0); CliVcpInternalEvent event;
furi_check(!(event & FuriFlagError)); furi_check(furi_message_queue_get(object, &event, 0) == FuriStatusOk);
if(event & CliVcpInternalEventRx) { switch(event) {
case CliVcpInternalEventRx: {
VCP_TRACE(TAG, "Rx"); VCP_TRACE(TAG, "Rx");
cli_vcp_maybe_receive_data(cli_vcp); cli_vcp_maybe_receive_data(cli_vcp);
break;
} }
if(event & CliVcpInternalEventTxDone) { case CliVcpInternalEventTxDone: {
VCP_TRACE(TAG, "TxDone"); VCP_TRACE(TAG, "TxDone");
cli_vcp_maybe_send_data(cli_vcp); cli_vcp_maybe_send_data(cli_vcp);
break;
} }
if(event & CliVcpInternalEventDisconnected) { case CliVcpInternalEventDisconnected: {
if(!cli_vcp->is_connected) return; if(!cli_vcp->is_connected) return;
FURI_LOG_D(TAG, "Disconnected"); FURI_LOG_D(TAG, "Disconnected");
cli_vcp->is_connected = false; cli_vcp->is_connected = false;
@@ -215,9 +214,10 @@ static void cli_vcp_internal_event_happened(void* context) {
pipe_detach_from_event_loop(cli_vcp->own_pipe); pipe_detach_from_event_loop(cli_vcp->own_pipe);
pipe_free(cli_vcp->own_pipe); pipe_free(cli_vcp->own_pipe);
cli_vcp->own_pipe = NULL; cli_vcp->own_pipe = NULL;
break;
} }
if(event & CliVcpInternalEventConnected) { case CliVcpInternalEventConnected: {
if(cli_vcp->is_connected) return; if(cli_vcp->is_connected) return;
FURI_LOG_D(TAG, "Connected"); FURI_LOG_D(TAG, "Connected");
cli_vcp->is_connected = true; cli_vcp->is_connected = true;
@@ -244,6 +244,8 @@ static void cli_vcp_internal_event_happened(void* context) {
cli_vcp->shell = cli_shell_alloc( cli_vcp->shell = cli_shell_alloc(
cli_main_motd, NULL, cli_vcp->shell_pipe, cli_vcp->main_registry, &cli_main_ext_config); cli_main_motd, NULL, cli_vcp->shell_pipe, cli_vcp->main_registry, &cli_main_ext_config);
cli_shell_start(cli_vcp->shell); cli_shell_start(cli_vcp->shell);
break;
}
} }
} }
@@ -253,7 +255,6 @@ static void cli_vcp_internal_event_happened(void* context) {
static CliVcp* cli_vcp_alloc(void) { static CliVcp* cli_vcp_alloc(void) {
CliVcp* cli_vcp = malloc(sizeof(CliVcp)); CliVcp* cli_vcp = malloc(sizeof(CliVcp));
cli_vcp->thread_id = furi_thread_get_current_id();
cli_vcp->event_loop = furi_event_loop_alloc(); cli_vcp->event_loop = furi_event_loop_alloc();
@@ -265,8 +266,14 @@ static CliVcp* cli_vcp_alloc(void) {
cli_vcp_message_received, cli_vcp_message_received,
cli_vcp); cli_vcp);
furi_event_loop_subscribe_thread_flags( cli_vcp->internal_evt_queue =
cli_vcp->event_loop, cli_vcp_internal_event_happened, cli_vcp); furi_message_queue_alloc(VCP_MESSAGE_Q_LEN, sizeof(CliVcpInternalEvent));
furi_event_loop_subscribe_message_queue(
cli_vcp->event_loop,
cli_vcp->internal_evt_queue,
FuriEventLoopEventIn,
cli_vcp_internal_event_happened,
cli_vcp);
cli_vcp->main_registry = furi_record_open(RECORD_CLI); cli_vcp->main_registry = furi_record_open(RECORD_CLI);