From: hackbard Date: Sat, 31 Jan 2004 06:17:53 +0000 (+0000) Subject: braindead repos smp-waschbecken opened. initial checkin of ser_cp. X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed56ec047adf2a7b47e23873bf60aca37f83e222;p=smp-waschbecken%2Fser_cp.git braindead repos smp-waschbecken opened. initial checkin of ser_cp. --- ed56ec047adf2a7b47e23873bf60aca37f83e222 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7839a57 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# Makefle of ser_{get,serv} + +CFLAGS = -O3 -Wall +LIBS = + +OBJS = ser_get ser_serv + +BASE = ser.o + +all: $(OBJS) + +ser_get: $(BASE) +ser_serv: $(BASE) + +clean: + rm $(OBJS) $(BASE) + +remake: clean all diff --git a/ser.c b/ser.c new file mode 100644 index 0000000..2050d82 --- /dev/null +++ b/ser.c @@ -0,0 +1,174 @@ +/* + * the routines used by ser_get and ser_serv + * + * author: hackbard@hackdaworld.dyndns.org + * + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ser.h" + +int ser_send_hello(info_t *info) { + char msg[8]="hello"; + int count=0; + int retval; + + while((count>=0)&&(count<5)) { + retval=write(info->ttsfd,msg+count,5-count); + if(retval>=0) count+=retval; + } + + return 1; +} + +int ser_connect(info_t *info) { + unsigned char msg,stat=0; + + while(!(stat&CONN_EST)) { + + if(!(stat&CONN_ACK)) { + msg=CONN_REQ; + if(write(info->ttsfd,&msg,1)<0) { + perror("failed writing CONN_REQ"); + return -1; + } + stat|=CONN_REQ; + } + + if(read(info->ttsfd,&msg,1)<0) { + perror("reading CONN_ACK failed"); + return -1; + } + if(msg==CONN_ACK) { + dprintf(2,"[ser_get] got ack\n"); + stat|=CONN_ACK; + msg=CONN_EST; + if(write(info->ttsfd,&msg,1)<0) { + perror("writing CONN_EST failed"); + return -1; + } + stat|=CONN_EST; + } + + } + + return 1; +} + +int ser_listen_and_accept(info_t *info) { + unsigned char msg,stat=0; + + while(!(stat&CONN_EST)) { + + if(!(stat&CONN_REQ)) { + if(read(info->ttsfd,&msg,1)<0) { + perror("reading CONN_REQ failed"); + return -1; + } + if(msg==CONN_REQ) { + dprintf(2,"[ser_serv] got req\n"); + stat|=CONN_REQ; + msg=CONN_ACK; + if(write(info->ttsfd,&msg,1)<0) { + perror("writing CANN_ACK failed"); + return -1; + } + stat|=CONN_ACK; + } + } + else { + if(read(info->ttsfd,&msg,1)<0) { + perror("reading CONN_EST failed"); + return -1; + } + if(msg==CONN_EST) { + dprintf(2,"[ser_serv] got est\n"); + stat|=CONN_EST; + } + } + } + + return 1; +} + +int ser_read_and_out(info_t *info) { + unsigned char buf[BUFSIZE]; + int count=1; + int retval=1; + int i=1; + + while(i) { + + while((count>0)&&(count0)) { + retval=read(info->ttsfd,buf+count-1,BUFSIZE-count+1); + if(retval>0) count+=retval; + else i=0; + } + + while(count-1>0) { + retval=write(1,buf+BUFSIZE-count+1,count-1); + if(retval>0) count-=retval; + } + } + + return 1; +} + +int ser_init(info_t *info) { + if((info->ttsfd=open(info->ttsdev,O_RDWR))<0) { + perror("unable to open ttsdev"); + return -1; + } + if(tcsetattr(info->ttsfd,TCSANOW,&(info->ttsconf))<0) { + perror("unable to set tts attributes"); + return -1; + } + + return info->ttsfd; +} + +int ser_config(info_t *info) { + info->ttsconf.c_iflag=IXOFF|IGNBRK|BRKINT|IGNPAR; + info->ttsconf.c_oflag=0; + info->ttsconf.c_cflag=BAUDRATE|CS8|CREAD|CLOCAL|CSTOPB|PARENB; + info->ttsconf.c_lflag=0; + info->ttsconf.c_cc[VMIN]=0; + info->ttsconf.c_cc[VTIME]=2; /* .2 sec */ + cfsetospeed(&(info->ttsconf),BAUDRATE); + cfsetispeed(&(info->ttsconf),BAUDRATE); + + return 1; +} + +int ser_read_and_write(info_t *info) { + unsigned char buf[BUFSIZE]; + int count=1; + int retval=1; + int i=1; + + while(i) { + + while((count>0)&&(count0)) { + retval=read(0,buf+count-1,BUFSIZE-count+1); + if(retval>0) count+=retval; + else i=0; + } + + while(count-1>0) { + retval=write(info->ttsfd,buf+BUFSIZE-count+1,count-1); + if(retval>0) count-=retval; + } + } + + return 1; +} + diff --git a/ser.h b/ser.h new file mode 100644 index 0000000..587830a --- /dev/null +++ b/ser.h @@ -0,0 +1,28 @@ +/* + * ser.h -- ser_serv and serv_get + * + */ + +#include + +#define BAUDRATE B38400 +#define BUFSIZE 1024 + +#define CONN_REQ 1 +#define CONN_ACK 2 +#define CONN_EST 4 + +typedef struct info_s { + char ttsdev[32]; + int ttsfd; + struct termios ttsconf; +} info_t; + +/* function prototypes */ +int ser_init(info_t *info); +int ser_config(info_t *info); +int ser_send_hello(info_t *info); +int ser_read_and_out(info_t *info); +int ser_read_and_write(info_t *info); +int ser_connect(info_t *info); +int ser_listen_and_accept(info_t *info); diff --git a/ser_get.c b/ser_get.c new file mode 100644 index 0000000..63a0459 --- /dev/null +++ b/ser_get.c @@ -0,0 +1,34 @@ +/* ser_get.c -- hackbard@hackdaworld.dyndns.org + * + * no ethernet, no irda, no wlan ... use uart + */ + +#include +#include +#include + +#include "ser.h" + +/* code */ +int main(int argc, char **argv) { + info_t info; + + if(argc!=2) { + puts("usage:\n"); + puts("ser_get "); + puts(""); + return -1; + } + + strcpy(info.ttsdev,argv[1]); + + ser_config(&info); + ser_init(&info); + ser_connect(&info); + ser_read_and_out(&info); + + close(info.ttsfd); + + return 1; +} + diff --git a/ser_serv.c b/ser_serv.c new file mode 100644 index 0000000..5d21032 --- /dev/null +++ b/ser_serv.c @@ -0,0 +1,36 @@ +/* ser_serv -- hackbard@hackdaworld.dyndns.org + * + * no ethernet, no irda, no wlan ... use uart + * + */ + +#include +#include +#include + +#include "ser.h" + +int ser_read_and_write(info_t *info); // from ser_get + +int main(int argc,char **argv) { + info_t info; + + if(argc!=2) { + puts("usage:\n"); + puts("ser_get "); + puts(""); + return -1; + } + + strcpy(info.ttsdev,argv[1]); + + ser_config(&info); + ser_init(&info); + ser_listen_and_accept(&info); + ser_read_and_write(&info); + + close(info.ttsfd); + + return 1; +} +