braindead repos smp-waschbecken opened. initial checkin of ser_cp.
authorhackbard <hackbard>
Sat, 31 Jan 2004 06:17:53 +0000 (06:17 +0000)
committerhackbard <hackbard>
Sat, 31 Jan 2004 06:17:53 +0000 (06:17 +0000)
Makefile [new file with mode: 0644]
ser.c [new file with mode: 0644]
ser.h [new file with mode: 0644]
ser_get.c [new file with mode: 0644]
ser_serv.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
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 (file)
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 <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;
+}
+
diff --git a/ser.h b/ser.h
new file mode 100644 (file)
index 0000000..587830a
--- /dev/null
+++ b/ser.h
@@ -0,0 +1,28 @@
+/*
+ * ser.h -- ser_serv and serv_get
+ *
+ */
+
+#include <termios.h>
+
+#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 (file)
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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ser.h"
+
+/* code */
+int main(int argc, char **argv) {
+  info_t info;
+
+  if(argc!=2) {
+    puts("usage:\n");
+    puts("ser_get <tts device>");
+    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 (file)
index 0000000..5d21032
--- /dev/null
@@ -0,0 +1,36 @@
+/* ser_serv -- hackbard@hackdaworld.dyndns.org
+ *
+ * no ethernet, no irda, no wlan ... use uart
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#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 <tts device>");
+    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;
+}
+