Test for Error Recovery added to BSCP
authorPhilipp Dunkel <Philipp Dunkel@d4452222-2f33-11de-9270-010000000000>
Mon, 27 Apr 2009 21:16:12 +0000 (21:16 +0000)
committerPhilipp Dunkel <Philipp Dunkel@d4452222-2f33-11de-9270-010000000000>
Mon, 27 Apr 2009 21:16:12 +0000 (21:16 +0000)
git-svn-id: http://svn.cacert.cl/Software/BirdShack@23 d4452222-2f33-11de-9270-010000000000

TailFeather/trunk/src/bscp/Makefile
TailFeather/trunk/src/bscp/comm.c
TailFeather/trunk/src/bscp/packet.c
TailFeather/trunk/src/bscp/recovertest.c [new file with mode: 0644]
TailFeather/trunk/src/bscp/stdpipe.c

index 8f874d3..629417e 100644 (file)
@@ -7,11 +7,15 @@ stdpipe : stdpipe.o bscp.a
 bscp.a : packet.o comm.o crc.o
        $(AR) r bscp.a packet.o comm.o crc.o
 
-simpletest : simpletest.o
+simpletest : simpletest.o stdpipe
        $(CC) -o simpletest simpletest.o
 
+recovertest : recovertest.o bscp.a
+       $(CC) -o recovertest recovertest.o bscp.a
+
 clean :
        rm -f stdpipe
        rm -f simpletest
+       rm -f recovertest
        rm -f *.a
        rm -f *.o
index 7bf06eb..cf886b9 100644 (file)
@@ -90,7 +90,8 @@ void comm_fault(comm *chnl) {
        chnl->faults++;
        pthread_mutex_unlock(&(chnl->readlock));
        if (chnl->faults > COMM_FAULTMAX) {
-               comm_sync(chnl);
+               fprintf(stderr,"?");
+               //comm_sync(chnl);
        }
 }
 void comm_cleanup(comm *chnl) {
index 2b09eb6..510b749 100644 (file)
@@ -65,9 +65,14 @@ int packet_fetch(comm *channel, packet *pkt) {
        if (comm_get(channel, (void*)&len, PACKET_HEADER) != PACKET_HEADER) return 1;
        memcpy(pkt->data, &len, PACKET_HEADER);
        pkt->length = ntohl(len);
-       comm_get(channel,pkt->data + PACKET_HEADER,pkt->length + PACKET_FOOTER);
-       memcpy(&(pkt->checksum),pkt->data+ PACKET_HEADER + pkt->length, PACKET_FOOTER);
-       if (pkt->checksum != calculate_crc(pkt->data,PACKET_HEADER + pkt->length)) {
+       if ((pkt->length > 0) && (pkt->length <= PACKET_MAXLEN)) {
+               comm_get(channel,pkt->data + PACKET_HEADER,pkt->length + PACKET_FOOTER);
+               memcpy(&(pkt->checksum),pkt->data+ PACKET_HEADER + pkt->length, PACKET_FOOTER);
+               if (pkt->checksum != calculate_crc(pkt->data,PACKET_HEADER + pkt->length)) {
+                       comm_fault(channel);
+                       return 1;
+               }
+       } else {
                comm_fault(channel);
                return 1;
        }
diff --git a/TailFeather/trunk/src/bscp/recovertest.c b/TailFeather/trunk/src/bscp/recovertest.c
new file mode 100644 (file)
index 0000000..ccf4be4
--- /dev/null
@@ -0,0 +1,123 @@
+/* 
+ For copyright information see copyright.txt of the project 
+ Author: Philipp Dunkel
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <signal.h>
+#include "packet.h"
+
+int running;
+comm one,two;
+
+void bugout(int sig) {
+       if (!running) {
+               return;
+       }
+       running = 0;
+       comm_cleanup(&one);
+       comm_cleanup(&two);
+       signal(SIGHUP,NULL);
+       signal(SIGINT,NULL);
+       signal(SIGTERM,NULL);
+}
+
+void *bscp2bscp(void *args) {
+       packet pkt;
+       while (running) {
+               if ((packet_fetch(&one, &pkt) == 0)&&running) {
+                       packet_send(&two, &pkt);
+               }
+       }
+       fprintf(stderr,"bscp2bscp complete\n");
+       pthread_exit(NULL);
+       return NULL;
+}
+void *stdin2bscp(void *args) {
+       packet pkt;
+       char *line;
+       int len;
+       comm *port = (comm*)args;
+       while (running) {
+               line = fgetln(stdin,(size_t *)&len);
+               if ((len > 0) && running) {
+                       if ((len > 7)&&(memcmp("PDUEOF",line,6) != 0)) {
+                               packet_setdata(&pkt, line, len);
+                               packet_send(port,&pkt);
+                       }
+               }
+               if ((len==-1)||((len>7)&&(memcmp("EOF",line,6) == 0))) {
+                       bugout(0);
+                       close(one.write);
+                       close(two.write);
+               }
+       }
+       fprintf(stderr,"stdin2bscp complete\n");
+       pthread_exit(NULL);
+       return NULL;
+}
+void *bscp2stdout(void *args) {
+       packet pkt;
+       comm *port = (comm*)args;
+       char *buffer;
+       int len;
+       while (running) {
+               if ((packet_fetch(port, &pkt) == 0)&&running) {
+                       buffer = (char*)packet_data(&pkt,&len);
+                       if (len > -1) {
+                               buffer[len-1] = 0;
+                               fprintf(stderr,"*");
+                       }
+               }
+       }
+       fprintf(stderr,"bscp2stdout complete\n");
+       pthread_exit(NULL);
+       return NULL;
+}
+void *doerror(void* na) {
+       while (running) {
+               pthread_mutex_lock(&one.writelock);
+               //write(one.write,"\2",1);
+               pthread_mutex_unlock(&one.writelock);
+               sleep(10);
+       }
+}
+int main(int argc, char *argv[]) {
+       pthread_t th_bscp2bscp;
+       pthread_t th_stdin2bscp;
+       pthread_t th_bscp2stdout;
+       pthread_t th_error;
+       
+       int pone[2];
+       int ptwo[2];
+       
+       if (pipe(pone) || pipe(ptwo)) {
+               fprintf(stderr,"Could not create redirect pipe!\n");
+               return -1;
+       }
+       running = 0;
+       signal(SIGHUP,bugout);
+       siginterrupt(SIGHUP, 1);
+       signal(SIGINT,bugout);
+       siginterrupt(SIGINT, 1);
+       signal(SIGTERM,bugout);
+       siginterrupt(SIGTERM, 1);
+
+       comm_init(&one,pone[0],pone[1]);
+       comm_init(&two,ptwo[0],ptwo[1]);
+       running = 1;
+       
+       
+       pthread_create(&th_stdin2bscp,NULL,stdin2bscp,(void*)&one);
+       pthread_create(&th_bscp2bscp,NULL,bscp2bscp,NULL);
+       pthread_create(&th_bscp2stdout,NULL,bscp2stdout,(void*)&two);
+       pthread_create(&th_bscp2stdout,NULL,doerror,NULL);
+       
+       pthread_join(th_bscp2bscp, NULL);
+       pthread_join(th_stdin2bscp, NULL);
+       pthread_join(th_bscp2stdout, NULL);
+       fprintf(stderr,"main complete\n");
+       return 0;
+}
index 1355791..29a539f 100644 (file)
@@ -44,6 +44,7 @@ void *bscp2pipe(void *argdata) {
                        }
                }
        }
+       return NULL;
 }
 void *pipe2bscp(void *argdata) {
        char **argv = (char**)argdata;
@@ -62,6 +63,7 @@ void *pipe2bscp(void *argdata) {
                        }
                }
        }
+       return NULL;
 }
 int main(int argc, char *argv[]) {
        pthread_t th_bscp2pipe;