@@ -30,27 +30,23 @@ typedef struct {
3030} CliVcpMessage ;
3131
3232typedef 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-
4339struct 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
10399static 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
107103static 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
254256static 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
0 commit comments