linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: uescher <linux-can_ger.kernel.org@myvdr.de>
To: linux-can@vger.kernel.org
Subject: [PATCH 1/1 V2] slcanpty: Improve the data handling from pty2can() to avoid, incomplete messages in further processing
Date: Sat, 01 Jun 2013 21:18:50 +0200	[thread overview]
Message-ID: <51AA491A.7060307@myvdr.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

slcanpty: Improve the data handling from pty2can() to avoid
  incomplete messages in further processing

This Patch ensure that we have minimum one complete SLCAN messages from 
pty in our buf[]
before we start the processing.
Leave framgents of an messages in the buf[] and wait with processing 
until we have a '\r' in the buf[].

Signed-off-by: Ulrich Escher <git@myvdr.de>




[-- Attachment #2: 0001-slcanpty-Improve-the-data-handling-from-pty2can-to-a.patch --]
[-- Type: text/x-patch, Size: 2222 bytes --]

From 1ce314cb99592d4e6021717fffbe8130001f3838 Mon Sep 17 00:00:00 2001
From: ulrich <git@myvdr.de>
Date: Sat, 1 Jun 2013 21:06:49 +0200
Subject: [PATCH] slcanpty: Improve the data handling from pty2can() to avoid
 incomplete messages in further processing

This Patch ensure that we have minimum one complete SLCAN messages from pty in our buf[]
before we start the processing.
Leave framgents of an messages in the buf[] and wait with processing until we have a '\r' in the buf[].

Signed-off-by: Ulrich Escher <git@myvdr.de>
---
 slcanpty.c |   39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/slcanpty.c b/slcanpty.c
index f3628b9..8d1a6b3 100644
--- a/slcanpty.c
+++ b/slcanpty.c
@@ -73,12 +73,18 @@ int pty2can(int pty, int socket, struct can_filter *fi,
 	int ptr;
 	struct can_frame frame;
 	int tmp, i;
+	static int nbytes_saved = 0;
 
-	nbytes = read(pty, &buf, sizeof(buf)-1);
+	nbytes = read(pty, &buf[nbytes_saved], sizeof(buf)-nbytes_saved-1);
 	if (nbytes < 0) {
 		perror("read pty");
 		return 1;
 	}
+#ifdef DEBUG
+	printf("read %d bytes from pty\n",nbytes);
+#endif
+	nbytes = nbytes_saved + nbytes;
+	nbytes_saved = 0;
 
 rx_restart:
 	/* remove trailing '\r' characters to be robust against some apps */
@@ -91,6 +97,19 @@ rx_restart:
 	if (!nbytes)
 		return 0;
 
+	for (tmp = 0; tmp <= nbytes; tmp++) {
+		if (tmp == nbytes) {
+			nbytes_saved = nbytes;
+#ifdef DEBUG
+			printf("nbytes_saveds ist nun: %d return\n", nbytes);
+#endif
+			return 0;
+		}
+		if (buf[tmp] == '\r') {
+			break;
+		}
+	}
+
 	cmd = buf[0];
 	buf[nbytes] = 0;
 
@@ -359,7 +378,9 @@ int can2pty(int pty, int socket, int *tstamp)
 		return 1;
 	}
 	fflush(NULL);
-
+#ifdef DEBUG
+	printf("%s\n",buf);
+#endif
 	return 0;
 }
 
@@ -478,16 +499,18 @@ int main(int argc, char **argv)
 			continue;
 		}
 
-		if (FD_ISSET(p, &rdfs))
+		if (FD_ISSET(p, &rdfs)) {
 			if (pty2can(p, s, &fi, &is_open, &tstamp)) {
-			running = 0;
-			continue;
+				running = 0;
+				continue;
+			}
 		}
 
-		if (FD_ISSET(s, &rdfs))
+		if (FD_ISSET(s, &rdfs)) {
 			if (can2pty(p, s, &tstamp)) {
-			running = 0;
-			continue;
+				running = 0;
+				continue;
+			}
 		}
 	}
 
-- 
1.7.9.5


             reply	other threads:[~2013-06-01 19:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-01 19:18 uescher [this message]
2013-06-08 11:18 ` [PATCH 1/1 V2] slcanpty: Improve the data handling from pty2can() to avoid, incomplete messages in further processing Oliver Hartkopp
2013-06-15 20:39   ` uescher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=51AA491A.7060307@myvdr.de \
    --to=linux-can_ger.kernel.org@myvdr.de \
    --cc=linux-can@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).