You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/10/15 11:47:54 UTC

(nuttx-apps) 03/06: logging/nxscope: keep a stream frame intact until it is fully sent

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit d8f1282431f63a13aac88a0f7f658fd85f4280ed
Author: raiden00pl <ra...@railab.me>
AuthorDate: Fri Oct 2 10:05:16 2026 +0200

    logging/nxscope: keep a stream frame intact until it is fully sent
    
    A failed stream send kept the finalized frame for retry, but later puts
    were appended after its footer and the retry sent them without a frame
    header, so the client dropped them silently. A partial send was taken as
    success and the rest of the frame was lost. A failed critical channel
    send also marked the stream buffer for retry, so the next stream frame
    went out unfinalized.
    
    A retry now continues where the previous send stopped, puts while a
    frame waits are dropped and flagged in the next frame, and only the
    stream buffer keeps a frame for retry.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: raiden00pl <ra...@railab.me>
---
 include/logging/nxscope/nxscope.h   |  2 ++
 logging/nxscope/nxscope.c           |  6 ++++--
 logging/nxscope/nxscope_chan.c      | 19 ++++++++++++++++++-
 logging/nxscope/nxscope_internals.c | 38 ++++++++++++++++++++++++++++---------
 4 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/include/logging/nxscope/nxscope.h b/include/logging/nxscope/nxscope.h
index 79d8c59fc..4f6f902ca 100644
--- a/include/logging/nxscope/nxscope.h
+++ b/include/logging/nxscope/nxscope.h
@@ -371,6 +371,8 @@ struct nxscope_s
   size_t                       streambuf_len;
   size_t                       stream_i;
   bool                         stream_retry;
+  bool                         stream_ovf;
+  size_t                       stream_sent;
 
 #ifdef CONFIG_LOGGING_NXSCOPE_CRICHANNELS
   /* Critical buffer data */
diff --git a/logging/nxscope/nxscope.c b/logging/nxscope/nxscope.c
index 094395dd8..fa487d864 100644
--- a/logging/nxscope/nxscope.c
+++ b/logging/nxscope/nxscope.c
@@ -472,9 +472,11 @@ static void nxscope_stream_reset(FAR struct nxscope_s *s)
 
   s->stream_i = s->proto_stream->hdrlen + 1;
 
-  /* Reset flags */
+  /* Reset flags, keep an overflow from a frame that waited for retry */
 
-  s->streambuf[s->proto_stream->hdrlen] = 0;
+  s->streambuf[s->proto_stream->hdrlen] =
+    s->stream_ovf ? NXSCOPE_STREAM_FLAGS_OVERFLOW : 0;
+  s->stream_ovf = false;
 }
 
 /****************************************************************************
diff --git a/logging/nxscope/nxscope_chan.c b/logging/nxscope/nxscope_chan.c
index 472864c3a..f8013726c 100644
--- a/logging/nxscope/nxscope_chan.c
+++ b/logging/nxscope/nxscope_chan.c
@@ -99,7 +99,16 @@ static void nxscope_stream_overflow(FAR struct nxscope_s *s)
 {
   DEBUGASSERT(s);
 
-  s->streambuf[s->proto_stream->hdrlen] |= NXSCOPE_STREAM_FLAGS_OVERFLOW;
+  /* A frame waiting for retry is final: flag the next one */
+
+  if (s->stream_retry)
+    {
+      s->stream_ovf = true;
+    }
+  else
+    {
+      s->streambuf[s->proto_stream->hdrlen] |= NXSCOPE_STREAM_FLAGS_OVERFLOW;
+    }
 }
 
 /****************************************************************************
@@ -250,6 +259,14 @@ static int nxscope_ch_space(FAR struct nxscope_s *s, uint8_t type,
     }
 #endif
 
+  /* The frame in the buffer waits for its retry */
+
+  if (s->stream_retry)
+    {
+      nxscope_stream_overflow(s);
+      return -ENOBUFS;
+    }
+
   next_i = (s->stream_i + 1 + type_size * d + mlen +
             s->proto_stream->footlen);
 
diff --git a/logging/nxscope/nxscope_internals.c b/logging/nxscope/nxscope_internals.c
index b001b4187..7995c1965 100644
--- a/logging/nxscope/nxscope_internals.c
+++ b/logging/nxscope/nxscope_internals.c
@@ -55,15 +55,17 @@
 int nxscope_stream_send(FAR struct nxscope_s *s, FAR uint8_t *buff,
                         FAR size_t *buff_i)
 {
-  int ret = OK;
+  bool   stream = (buff == s->streambuf);
+  size_t sent   = 0;
+  int    ret    = OK;
 
   DEBUGASSERT(s);
   DEBUGASSERT(buff);
   DEBUGASSERT(buff_i);
 
-  /* Finalize stream frame */
+  /* Finalize stream frame, unless it is a stream frame waiting for retry */
 
-  if (!s->stream_retry)
+  if (!stream || !s->stream_retry)
     {
       ret = PROTO_FRAME_FINAL(s, s->proto_stream,
                               NXSCOPE_HDRID_STREAM, buff, buff_i);
@@ -74,17 +76,35 @@ int nxscope_stream_send(FAR struct nxscope_s *s, FAR uint8_t *buff,
         }
     }
 
-  /* Send stream data */
+  /* Send stream data, a retry continues where the previous send stopped */
 
-  ret = INTF_SEND(s, s->intf_stream, buff, *buff_i);
-  if (ret < 0)
+  if (stream && s->stream_retry)
+    {
+      sent = s->stream_sent;
+    }
+
+  ret = INTF_SEND(s, s->intf_stream, &buff[sent], *buff_i - sent);
+  if (ret >= 0 && (size_t)ret < *buff_i - sent)
+    {
+      /* Partially sent */
+
+      sent += ret;
+      ret   = -EAGAIN;
+    }
+
+  /* A partial send or a full port is backpressure, not an error */
+
+  if (ret < 0 && ret != -EAGAIN)
     {
       _err("ERROR: INTF_SEND failed %d\n", ret);
-      s->stream_retry = true;
     }
-  else
+
+  /* Only the stream buffer keeps its frame for a retry */
+
+  if (stream)
     {
-      s->stream_retry = false;
+      s->stream_retry = (ret < 0);
+      s->stream_sent  = (ret < 0) ? sent : 0;
     }
 
 errout: