]> icculus.org git repositories - btb/d2x.git/blob - arch/linux/serial.c
cruft removal
[btb/d2x.git] / arch / linux / serial.c
1 //added 06/09/99 Matt Mueller - fix nonetwork compile
2 #ifdef NETWORK
3 //end addition -MM
4 #include <sys/types.h>
5 #include <sys/time.h>
6 #include <sys/stat.h>
7 #include <sys/ioctl.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <termios.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include "serial.h"
16 //added 02/06/99 Matt Mueller - allow selection of different devices
17 #include "args.h"
18 //end addition
19 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
20 //#include "checker.h"
21 //end addition
22
23 #define BAUDRATE B38400
24 #define MODEMDEVICE "/dev/modem"
25 #define FALSE 0
26 #define TRUE 1
27
28 //edited 03/05/99 Matt Mueller - allow reading from different device
29 static int fd,rfd;
30 //end edit -MM
31 static struct termios oldtio, newtio;
32
33 void com_done(void)
34 {
35  if (!commlib_initialised) return;
36  commlib_initialised=0;
37  tcsetattr(fd, TCSANOW, &oldtio);
38  close(fd);
39 }
40
41 int com_init(void)
42 {
43 //edited 02/06/99 Matt Mueller - allow selection of different devices
44  char *modem=MODEMDEVICE;
45  int t;
46  if ((t = FindArg( "-serialdevice" ))) {
47          modem=Args[t+1];
48  }
49  //edited 03/05/99 Matt Mueller - allow reading from different device
50  if ((t = FindArg( "-serialread" ))) {
51      char *readpipe=NULL;
52      readpipe=Args[t+1];
53      rfd=open(readpipe, O_RDONLY | O_NOCTTY | O_NONBLOCK);
54      if (rfd < 0) { perror(readpipe); return -1; }
55  }else
56      rfd=-1;
57              
58  if (commlib_initialised) return -1;
59
60  fd=open(modem, (rfd<0?O_RDWR:O_WRONLY) | O_NOCTTY);
61  if (fd < 0) { perror(modem); return -1; }
62  if (rfd<0)
63     rfd=fd;
64  //end edit -MM
65 //end edit -MM
66             
67  tcgetattr(fd, &oldtio);
68  bzero(&newtio, sizeof(newtio));
69  
70  newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
71  newtio.c_iflag = IGNPAR;
72  newtio.c_oflag = 0;
73
74  /* set input mode (non-canonical, no echo,...) */
75  newtio.c_lflag = 0;
76  
77  newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
78  newtio.c_cc[VMIN]     = 0;   /* don't block on reads */
79
80  tcflush(fd, TCIFLUSH);
81  tcsetattr(fd, TCSANOW, &newtio);
82     
83  atexit(com_done);
84  commlib_initialised=1;
85  return 0;
86 }
87
88 //edited 03/05/99 Matt Mueller - allow reading from different device
89 int com_read(char *buffer, int len, int timeout_value)
90 {
91  struct timeval timeout;
92  fd_set set;
93  int i;
94
95  if (!commlib_initialised) return -1;
96  if (timeout_value==0) return read(rfd,buffer,len);
97
98  /* Initialise the file descriptor set */
99  FD_ZERO(&set);
100  FD_SET (rfd, &set);
101
102  /* Initialise the timeout timer value */
103  timeout.tv_sec=timeout_value / 1000;
104 //edited 02/06/99 Matt Mueller - microseconds, not milliseconds
105  timeout.tv_usec=(timeout_value % 1000) * 1000;
106 //end edit -MM
107
108  i=select(FD_SETSIZE, &set, NULL, NULL, &timeout);
109  if (i>0) {
110   i=read(rfd,buffer,len);
111   return i;
112  }
113  return i;
114 }
115
116 int com_write(char *buffer, int len)
117 {
118  if (!commlib_initialised) return -1;
119  return write(fd,buffer,len);
120 }
121 //end edit -MM
122
123 /* Hangup by dropping speed down to 0 and raising it again */
124 //edited 02/06/99 Matt Mueller - added the standard ath stuff, since the baud=0 didn't work for my modem
125 void com_port_hangup()
126 {
127  struct termios save, temp;
128
129  if (!commlib_initialised) return;
130
131  com_write("+++",3);
132  tcgetattr(fd, &save);
133  tcgetattr(fd, &temp);
134  cfsetispeed(&temp, B0);
135  cfsetospeed(&temp, B0);
136  tcsetattr(fd, TCSANOW, &temp);
137  sleep(1);
138  tcsetattr(fd, TCSANOW, &save);
139  com_write("ath0\r\n",5);
140  com_flushbuffers();//this should (hopefully?) clear out the OK and such.
141 //end edit -MM
142 }
143
144 /* Get the status of the DCD (Data Carrier Detect) line */
145 int com_getdcd()
146 {
147  int msr;
148  if (!commlib_initialised) return -1;
149  ioctl(fd, TIOCMGET, &msr);
150  return ((msr&TIOCM_CAR)?1:0);
151 }
152
153 void com_flushbuffers()
154 {
155  if (!commlib_initialised) return;
156  ioctl(fd, TCFLSH, 2);
157 }
158
159 void com_setbaudrate(int rate)
160 {
161  int speed;
162  struct termios temp;
163
164  if (!commlib_initialised) return;
165  switch(rate) {
166    case 9600: speed=B9600; break;
167    case 19200: speed=B19200; break;
168    case 38400: speed=B38400; break;
169    default: speed=B19200; break;
170  }
171  tcgetattr(fd, &temp);
172  cfsetispeed(&temp, speed);
173  cfsetospeed(&temp, speed);
174  tcsetattr(fd, TCSANOW, &temp);
175 }
176
177 int com_readline(int timeout_value, char *input_buffer,int len)
178 {
179  char c;
180  int j;
181  int i=0;
182  struct timeval timeout;
183  fd_set set;
184
185  if (timeout_value>0) {
186  /* Initialise the file descriptor set */
187  FD_ZERO(&set);
188 //edited 03/05/99 Matt Mueller - allow reading from different device
189  FD_SET (rfd, &set);
190 //end edit -MM
191
192  /* Initialise the timeout timer value */
193  timeout.tv_sec=timeout_value / 1000;
194 //edited 02/06/99 Matt Mueller - microseconds, not milliseconds
195  timeout.tv_usec=(timeout_value % 1000) * 1000;
196 //end edit -MM
197          
198  j=select(FD_SETSIZE, &set, NULL, NULL, &timeout);
199  if (j==0) return 0;
200  }
201  
202  do {
203 //  j=com_read(&c,1,0);
204   j=com_read(&c,1,timeout_value);
205   if (isprint(c) && (j>0)) {
206    input_buffer[i++]=c;
207    if (i>=len) return i;
208   }
209  } while (c!='\n');
210  input_buffer[i]=0;
211  return i;
212 }
213
214 //added 06/09/99 Matt Mueller - fix nonetwork compile
215 #endif
216 //end addition -MM