2 * the routines used by ser_get and ser_serv
4 * author: hackbard@hackdaworld.dyndns.org
12 #include <sys/types.h>
16 #include <sys/ioctl.h>
20 int ser_send_hello(info_t *info) {
25 while((count>=0)&&(count<5)) {
26 retval=write(info->ttsfd,msg+count,5-count);
27 if(retval>=0) count+=retval;
33 int ser_connect(info_t *info) {
34 unsigned char msg,stat=0;
36 while(!(stat&CONN_EST)) {
38 if(!(stat&CONN_ACK)) {
40 if(write(info->ttsfd,&msg,1)<0) {
41 perror("failed writing CONN_REQ");
47 if(read(info->ttsfd,&msg,1)<0) {
48 perror("reading CONN_ACK failed");
52 dprintf(2,"[ser_get] got ack\n");
55 if(write(info->ttsfd,&msg,1)<0) {
56 perror("writing CONN_EST failed");
67 int ser_listen_and_accept(info_t *info) {
68 unsigned char msg,stat=0;
70 while(!(stat&CONN_EST)) {
72 if(!(stat&CONN_REQ)) {
73 if(read(info->ttsfd,&msg,1)<0) {
74 perror("reading CONN_REQ failed");
78 dprintf(2,"[ser_serv] got req\n");
81 if(write(info->ttsfd,&msg,1)<0) {
82 perror("writing CANN_ACK failed");
89 if(read(info->ttsfd,&msg,1)<0) {
90 perror("reading CONN_EST failed");
94 dprintf(2,"[ser_serv] got est\n");
103 int ser_read_and_out(info_t *info) {
104 unsigned char buf[BUFSIZE];
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;
118 retval=write(1,buf+BUFSIZE-count+1,count-1);
119 if(retval>0) count-=retval;
126 int ser_init(info_t *info) {
127 if((info->ttsfd=open(info->ttsdev,O_RDWR))<0) {
128 perror("unable to open ttsdev");
131 if(tcsetattr(info->ttsfd,TCSANOW,&(info->ttsconf))<0) {
132 perror("unable to set tts attributes");
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);
152 int ser_read_and_write(info_t *info) {
153 unsigned char buf[BUFSIZE];
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;
167 retval=write(info->ttsfd,buf+BUFSIZE-count+1,count-1);
168 if(retval>0) count-=retval;