From 11f22bf7028fe80ddd4d6bfc36f524a80edf81d9 Mon Sep 17 00:00:00 2001 From: Tomi Fontanilles Date: Wed, 14 Feb 2024 05:42:22 +0200 Subject: [PATCH] modem: ppp: fix receiving of successive frames when one 0x7E is omitted The SOF delimiter byte may be omitted when a frame follows another that just ended with that byte. The parsing used to expect that second delimiter anyway, which resulted in PPP frames going missing. As an additional improvement, dropped bytes as well as the length of received frames are now (debug) logged. Signed-off-by: Tomi Fontanilles --- subsys/modem/modem_ppp.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/subsys/modem/modem_ppp.c b/subsys/modem/modem_ppp.c index dffa4f68dbf..91085317d99 100644 --- a/subsys/modem/modem_ppp.c +++ b/subsys/modem/modem_ppp.c @@ -192,40 +192,45 @@ static uint8_t modem_ppp_wrap_net_pkt_byte(struct modem_ppp *ppp) return 0; } +static bool modem_ppp_is_byte_expected(uint8_t byte, uint8_t expected_byte) +{ + if (byte == expected_byte) { + return true; + } + LOG_DBG("Dropping byte 0x%02hhx because 0x%02hhx was expected.", byte, expected_byte); + return false; +} + static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) { switch (ppp->receive_state) { case MODEM_PPP_RECEIVE_STATE_HDR_SOF: - if (byte == MODEM_PPP_CODE_DELIMITER) { + if (modem_ppp_is_byte_expected(byte, MODEM_PPP_CODE_DELIMITER)) { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_FF; } - break; case MODEM_PPP_RECEIVE_STATE_HDR_FF: if (byte == MODEM_PPP_CODE_DELIMITER) { break; } - - if (byte == 0xFF) { + if (modem_ppp_is_byte_expected(byte, 0xFF)) { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_7D; } else { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; } - break; case MODEM_PPP_RECEIVE_STATE_HDR_7D: - if (byte == MODEM_PPP_CODE_ESCAPE) { + if (modem_ppp_is_byte_expected(byte, MODEM_PPP_CODE_ESCAPE)) { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_23; } else { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; } - break; case MODEM_PPP_RECEIVE_STATE_HDR_23: - if (byte == 0x23) { + if (modem_ppp_is_byte_expected(byte, 0x23)) { ppp->rx_pkt = net_pkt_rx_alloc_with_buffer(ppp->iface, CONFIG_MODEM_PPP_NET_BUF_FRAG_SIZE, AF_UNSPEC, 0, K_NO_WAIT); @@ -238,7 +243,6 @@ static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) LOG_DBG("Receiving PPP frame"); ppp->receive_state = MODEM_PPP_RECEIVE_STATE_WRITING; net_pkt_cursor_init(ppp->rx_pkt); - } else { ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; } @@ -247,11 +251,10 @@ static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) case MODEM_PPP_RECEIVE_STATE_WRITING: if (byte == MODEM_PPP_CODE_DELIMITER) { - LOG_DBG("Received PPP frame"); + LOG_DBG("Received PPP frame (len %zu)", net_pkt_get_len(ppp->rx_pkt)); /* Remove FCS */ net_pkt_remove_tail(ppp->rx_pkt, MODEM_PPP_FRAME_TAIL_SIZE); - net_pkt_cursor_init(ppp->rx_pkt); net_pkt_set_ppp(ppp->rx_pkt, true); if (net_recv_data(ppp->iface, ppp->rx_pkt) < 0) { @@ -260,7 +263,8 @@ static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) } ppp->rx_pkt = NULL; - ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_SOF; + /* Skip SOF because the delimiter may be omitted for successive frames. */ + ppp->receive_state = MODEM_PPP_RECEIVE_STATE_HDR_FF; break; }