added ignore file (cvs)
[smp-waschbecken/ser_cp.git] / ser.c
1 /*
2  * the routines used by ser_get and ser_serv
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17
18 #include "ser.h"
19
20 int ser_send_hello(info_t *info) {
21   char msg[8]="hello";
22   int count=0;
23   int retval;
24
25   while((count>=0)&&(count<5)) {
26     retval=write(info->ttsfd,msg+count,5-count);
27     if(retval>=0) count+=retval;
28   }
29
30   return 1;
31 }
32
33 int ser_connect(info_t *info) {
34   unsigned char msg,stat=0;
35
36   while(!(stat&CONN_EST)) {
37
38     if(!(stat&CONN_ACK)) {
39       msg=CONN_REQ;
40       if(write(info->ttsfd,&msg,1)<0) {
41         perror("failed writing CONN_REQ");
42         return -1;
43       }
44       stat|=CONN_REQ;
45     }
46
47     if(read(info->ttsfd,&msg,1)<0) {
48       perror("reading CONN_ACK failed");
49       return -1;
50     }
51     if(msg==CONN_ACK) {
52       dprintf(2,"[ser_get] got ack\n");
53       stat|=CONN_ACK;
54       msg=CONN_EST;
55       if(write(info->ttsfd,&msg,1)<0) {
56         perror("writing CONN_EST failed");
57         return -1;
58       }
59       stat|=CONN_EST;
60     }
61
62   }
63
64   return 1;
65 }
66
67 int ser_listen_and_accept(info_t *info) {
68   unsigned char msg,stat=0;
69
70   while(!(stat&CONN_EST)) {
71
72     if(!(stat&CONN_REQ)) {
73       if(read(info->ttsfd,&msg,1)<0) {
74         perror("reading CONN_REQ failed");
75         return -1;
76       }
77       if(msg==CONN_REQ) {
78         dprintf(2,"[ser_serv] got req\n");
79         stat|=CONN_REQ;
80         msg=CONN_ACK;
81         if(write(info->ttsfd,&msg,1)<0) {
82           perror("writing CANN_ACK failed");
83           return -1;
84         }
85         stat|=CONN_ACK;
86       }
87     }
88     else {
89       if(read(info->ttsfd,&msg,1)<0) {
90         perror("reading CONN_EST failed");
91         return -1;
92       }
93       if(msg==CONN_EST) {
94         dprintf(2,"[ser_serv] got est\n");
95         stat|=CONN_EST;
96       }
97     }
98   }
99
100   return 1;
101 }      
102
103 int ser_read_and_out(info_t *info) {
104   unsigned char buf[BUFSIZE];
105   int count=1;
106   int retval=1;
107   int i=1;
108
109   while(i) {
110
111     while((count>0)&&(count<BUFSIZE+1)&&(retval>0)) {
112       retval=read(info->ttsfd,buf+count-1,BUFSIZE-count+1);
113       if(retval>0) count+=retval;
114       else i=0;
115     }
116
117     while(count-1>0) {
118       retval=write(1,buf+BUFSIZE-count+1,count-1);
119       if(retval>0) count-=retval;
120     }
121   }
122
123   return 1;
124 }
125
126 int ser_init(info_t *info) {
127  if((info->ttsfd=open(info->ttsdev,O_RDWR))<0) {
128    perror("unable to open ttsdev");
129    return -1;
130  }
131  if(tcsetattr(info->ttsfd,TCSANOW,&(info->ttsconf))<0) {
132    perror("unable to set tts attributes");
133    return -1;
134  }
135
136  return info->ttsfd;
137 }
138
139 int ser_config(info_t *info) {
140   info->ttsconf.c_iflag=IXOFF|IGNBRK|BRKINT|IGNPAR;
141   info->ttsconf.c_oflag=0;
142   info->ttsconf.c_cflag=BAUDRATE|CS8|CREAD|CLOCAL|CSTOPB|PARENB;
143   info->ttsconf.c_lflag=0;
144   info->ttsconf.c_cc[VMIN]=0;
145   info->ttsconf.c_cc[VTIME]=2; /* .2 sec */
146   cfsetospeed(&(info->ttsconf),BAUDRATE);
147   cfsetispeed(&(info->ttsconf),BAUDRATE);
148
149   return 1;
150 }
151
152 int ser_read_and_write(info_t *info) {
153   unsigned char buf[BUFSIZE];
154   int count=1;
155   int retval=1;
156   int i=1; 
157     
158   while(i) {
159    
160     while((count>0)&&(count<BUFSIZE+1)&&(retval>0)) {
161       retval=read(0,buf+count-1,BUFSIZE-count+1);
162       if(retval>0) count+=retval;
163       else i=0;
164     }
165  
166     while(count-1>0) {
167       retval=write(info->ttsfd,buf+BUFSIZE-count+1,count-1);
168       if(retval>0) count-=retval;
169     }
170   }
171  
172   return 1;
173 }
174