diff --git a/applications/services/cli/cli_vcp.c b/applications/services/cli/cli_vcp.c index 99f2d7880..def1949e2 100644 --- a/applications/services/cli/cli_vcp.c +++ b/applications/services/cli/cli_vcp.c @@ -30,20 +30,16 @@ typedef struct { } CliVcpMessage; typedef enum { - CliVcpInternalEventConnected = (1 << 0), - CliVcpInternalEventDisconnected = (1 << 1), - CliVcpInternalEventTxDone = (1 << 2), - CliVcpInternalEventRx = (1 << 3), + CliVcpInternalEventConnected, + CliVcpInternalEventDisconnected, + CliVcpInternalEventTxDone, + CliVcpInternalEventRx, } CliVcpInternalEvent; -#define CliVcpInternalEventAll \ - (CliVcpInternalEventConnected | CliVcpInternalEventDisconnected | CliVcpInternalEventTxDone | \ - CliVcpInternalEventRx) - struct CliVcp { FuriEventLoop* event_loop; FuriMessageQueue* message_queue; // 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) { @@ -191,22 +187,25 @@ static void cli_vcp_message_received(FuriEventLoopObject* object, void* context) /** * 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; - CliVcpInternalEvent event = furi_thread_flags_wait(CliVcpInternalEventAll, FuriFlagWaitAny, 0); - furi_check(!(event & FuriFlagError)); + CliVcpInternalEvent event; + furi_check(furi_message_queue_get(object, &event, 0) == FuriStatusOk); - if(event & CliVcpInternalEventRx) { + switch(event) { + case CliVcpInternalEventRx: { VCP_TRACE(TAG, "Rx"); cli_vcp_maybe_receive_data(cli_vcp); + break; } - if(event & CliVcpInternalEventTxDone) { + case CliVcpInternalEventTxDone: { VCP_TRACE(TAG, "TxDone"); cli_vcp_maybe_send_data(cli_vcp); + break; } - if(event & CliVcpInternalEventDisconnected) { + case CliVcpInternalEventDisconnected: { if(!cli_vcp->is_connected) return; FURI_LOG_D(TAG, "Disconnected"); 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_free(cli_vcp->own_pipe); cli_vcp->own_pipe = NULL; + break; } - if(event & CliVcpInternalEventConnected) { + case CliVcpInternalEventConnected: { if(cli_vcp->is_connected) return; FURI_LOG_D(TAG, "Connected"); 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_main_motd, NULL, cli_vcp->shell_pipe, cli_vcp->main_registry, &cli_main_ext_config); 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) { CliVcp* cli_vcp = malloc(sizeof(CliVcp)); - cli_vcp->thread_id = furi_thread_get_current_id(); cli_vcp->event_loop = furi_event_loop_alloc(); @@ -265,8 +266,14 @@ static CliVcp* cli_vcp_alloc(void) { cli_vcp_message_received, cli_vcp); - furi_event_loop_subscribe_thread_flags( - cli_vcp->event_loop, cli_vcp_internal_event_happened, cli_vcp); + cli_vcp->internal_evt_queue = + 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);