Skip to contentSkip to conversation

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

PATCH In review uescher 2 participants 3 messages Jun 1, 2013 – Jun 15, 2013 CAN network drivers

lore
Share & tools
lore

uescher

author
#0 lore raw

PATCH v2 1/1 slcanpty: Improve the data handling from pty2can() to avoid, incomplete messages in further processing

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

From 1ce314cb99592d4e6021717fffbe8130001f3838 Mon Sep 17 00:00:00 2001
From: ulrich <[email protected]>
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[].

---
 slcanpty.c |   39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)
slcanpty.c +31 -8
index f3628b9..8d1a6b3 100644
@@ -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;
+			}
 		}
 	}
 
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 13 lines · uescher

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

Hi Ulrich,

thanks for the fix!

Indeed I needed some time to get the point %-)

I removed all the debug stuff and added some comments to reduce the time to get the point the next time i look into that code.

I changed the for() statement with the break to a simpler implementation.

Please check out the updated version which should do the same as your patch.

Best regards,
Oliver

author
#2 lore raw

Am 08.06.2013 13:18, schrieb Oliver Hartkopp:

Quoted 4 lines

Hi Ulrich,

thanks for the fix!

Quoted 8 lines

Indeed I needed some time to get the point %-)

I removed all the debug stuff and added some comments to reduce the time to get the point the next time i look into that code.

I changed the for() statement with the break to a simpler implementation.

Please check out the updated version which should do the same as your patch.

It works fine. Thanks you.