linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1 V2] slcanpty: Improve the data handling from pty2can() to avoid, incomplete messages in further processing
@ 2013-06-01 19:18 uescher
  2013-06-08 11:18 ` Oliver Hartkopp
  0 siblings, 1 reply; 3+ messages in thread
From: uescher @ 2013-06-01 19:18 UTC (permalink / raw)
  To: linux-can

[-- 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-06-15 20:39 UTC | newest]

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

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).