Skip to content

Commit a9bc72a

Browse files
SeppoTakalokartben
authored andcommitted
modem: cmux: Send disconnect event only after responding to CLD
If we immediately send disconnected event when CLD is received, we might close the UART pipe before the response is actually send. Also, shutdown_handler should not retry indefinitely. Signed-off-by: Seppo Takalo <[email protected]>
1 parent 822a501 commit a9bc72a

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

subsys/modem/modem_cmux.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,17 @@ static void modem_cmux_on_fcoff_command(struct modem_cmux *cmux)
451451
modem_cmux_acknowledge_received_frame(cmux);
452452
}
453453

454+
static void disconnect(struct modem_cmux *cmux)
455+
{
456+
LOG_DBG("CMUX disconnected");
457+
k_work_cancel_delayable(&cmux->disconnect_work);
458+
set_state(cmux, MODEM_CMUX_STATE_DISCONNECTED);
459+
k_mutex_lock(&cmux->transmit_rb_lock, K_FOREVER);
460+
cmux->flow_control_on = false;
461+
k_mutex_unlock(&cmux->transmit_rb_lock);
462+
modem_cmux_raise_event(cmux, MODEM_CMUX_EVENT_DISCONNECTED);
463+
}
464+
454465
static void modem_cmux_on_cld_command(struct modem_cmux *cmux, struct modem_cmux_command *command)
455466
{
456467
if (command->type.cr) {
@@ -464,16 +475,11 @@ static void modem_cmux_on_cld_command(struct modem_cmux *cmux, struct modem_cmux
464475
}
465476

466477
if (cmux->state == MODEM_CMUX_STATE_DISCONNECTING) {
467-
k_work_cancel_delayable(&cmux->disconnect_work);
478+
disconnect(cmux);
479+
} else {
480+
set_state(cmux, MODEM_CMUX_STATE_DISCONNECTING);
481+
k_work_schedule(&cmux->disconnect_work, MODEM_CMUX_T1_TIMEOUT);
468482
}
469-
470-
LOG_DBG("CMUX disconnected");
471-
set_state(cmux, MODEM_CMUX_STATE_DISCONNECTED);
472-
k_mutex_lock(&cmux->transmit_rb_lock, K_FOREVER);
473-
cmux->flow_control_on = false;
474-
k_mutex_unlock(&cmux->transmit_rb_lock);
475-
476-
modem_cmux_raise_event(cmux, MODEM_CMUX_EVENT_DISCONNECTED);
477483
}
478484

479485
static void modem_cmux_on_control_frame_ua(struct modem_cmux *cmux)
@@ -1150,7 +1156,12 @@ static void modem_cmux_disconnect_handler(struct k_work *item)
11501156
struct modem_cmux_command *command;
11511157
uint8_t data[2];
11521158

1153-
set_state(cmux, MODEM_CMUX_STATE_DISCONNECTING);
1159+
if (cmux->state == MODEM_CMUX_STATE_DISCONNECTING) {
1160+
disconnect(cmux);
1161+
} else {
1162+
set_state(cmux, MODEM_CMUX_STATE_DISCONNECTING);
1163+
k_work_schedule(&cmux->disconnect_work, MODEM_CMUX_T1_TIMEOUT);
1164+
}
11541165

11551166
command = modem_cmux_command_wrap(data);
11561167
command->type.ea = 1;

tests/subsys/modem/modem_cmux_pair/src/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,10 @@ ZTEST(modem_cmux_pair, test_modem_cmux_disconnect_connect)
507507
modem_backend_mock_reset(&bus_mock_dte);
508508
zassert_true(modem_cmux_disconnect_async(&cmux_dte) == 0, "Failed to disconnect CMUX");
509509

510-
k_msleep(100);
510+
events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DISCONNECTED), false, K_MSEC(660));
511+
zassert_true((events & EVENT_CMUX_DISCONNECTED), "Failed to disconnect CMUX");
511512

512-
events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DISCONNECTED), false, K_MSEC(100));
513+
events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DISCONNECTED), false, K_MSEC(660));
513514
zassert_true((events & EVENT_CMUX_DISCONNECTED), "Failed to disconnect CMUX");
514515

515516
/* Reconnect CMUX */
@@ -554,6 +555,9 @@ ZTEST(modem_cmux_pair, test_modem_cmux_disconnect_connect_sync)
554555
zassert_true(modem_cmux_disconnect(&cmux_dte) == 0, "Failed to disconnect CMUX");
555556
zassert_true(modem_cmux_disconnect(&cmux_dte) == -EALREADY,
556557
"Should already be disconnected");
558+
559+
events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DISCONNECTED), false, K_MSEC(660));
560+
zassert_true((events & EVENT_CMUX_DISCONNECTED), "Failed to disconnect CMUX");
557561
zassert_true(modem_cmux_disconnect(&cmux_dce) == -EALREADY,
558562
"Should already be disconnected");
559563

0 commit comments

Comments
 (0)