--- /dev/null
+/*
+ * the routines used by ser_get and ser_serv
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+
+#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)&&(count<BUFSIZE+1)&&(retval>0)) {
+ 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)&&(count<BUFSIZE+1)&&(retval>0)) {
+ 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;
+}
+