1 //added 06/09/99 Matt Mueller - fix nonetwork compile
16 //added 02/06/99 Matt Mueller - allow selection of different devices
19 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
20 //#include "checker.h"
23 #define BAUDRATE B38400
24 #define MODEMDEVICE "/dev/modem"
28 //edited 03/05/99 Matt Mueller - allow reading from different device
31 static struct termios oldtio, newtio;
35 if (!commlib_initialised) return;
36 commlib_initialised=0;
37 tcsetattr(fd, TCSANOW, &oldtio);
43 //edited 02/06/99 Matt Mueller - allow selection of different devices
44 char *modem=MODEMDEVICE;
46 if ((t = FindArg( "-serialdevice" ))) {
49 //edited 03/05/99 Matt Mueller - allow reading from different device
50 if ((t = FindArg( "-serialread" ))) {
53 rfd=open(readpipe, O_RDONLY | O_NOCTTY | O_NONBLOCK);
54 if (rfd < 0) { perror(readpipe); return -1; }
58 if (commlib_initialised) return -1;
60 fd=open(modem, (rfd<0?O_RDWR:O_WRONLY) | O_NOCTTY);
61 if (fd < 0) { perror(modem); return -1; }
67 tcgetattr(fd, &oldtio);
68 bzero(&newtio, sizeof(newtio));
70 newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
71 newtio.c_iflag = IGNPAR;
74 /* set input mode (non-canonical, no echo,...) */
77 newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
78 newtio.c_cc[VMIN] = 0; /* don't block on reads */
80 tcflush(fd, TCIFLUSH);
81 tcsetattr(fd, TCSANOW, &newtio);
84 commlib_initialised=1;
88 //edited 03/05/99 Matt Mueller - allow reading from different device
89 int com_read(char *buffer, int len, int timeout_value)
91 struct timeval timeout;
95 if (!commlib_initialised) return -1;
96 if (timeout_value==0) return read(rfd,buffer,len);
98 /* Initialise the file descriptor set */
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;
108 i=select(FD_SETSIZE, &set, NULL, NULL, &timeout);
110 i=read(rfd,buffer,len);
116 int com_write(char *buffer, int len)
118 if (!commlib_initialised) return -1;
119 return write(fd,buffer,len);
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()
127 struct termios save, temp;
129 if (!commlib_initialised) return;
132 tcgetattr(fd, &save);
133 tcgetattr(fd, &temp);
134 cfsetispeed(&temp, B0);
135 cfsetospeed(&temp, B0);
136 tcsetattr(fd, TCSANOW, &temp);
138 tcsetattr(fd, TCSANOW, &save);
139 com_write("ath0\r\n",5);
140 com_flushbuffers();//this should (hopefully?) clear out the OK and such.
144 /* Get the status of the DCD (Data Carrier Detect) line */
148 if (!commlib_initialised) return -1;
149 ioctl(fd, TIOCMGET, &msr);
150 return ((msr&TIOCM_CAR)?1:0);
153 void com_flushbuffers()
155 if (!commlib_initialised) return;
156 ioctl(fd, TCFLSH, 2);
159 void com_setbaudrate(int rate)
164 if (!commlib_initialised) return;
166 case 9600: speed=B9600; break;
167 case 19200: speed=B19200; break;
168 case 38400: speed=B38400; break;
169 default: speed=B19200; break;
171 tcgetattr(fd, &temp);
172 cfsetispeed(&temp, speed);
173 cfsetospeed(&temp, speed);
174 tcsetattr(fd, TCSANOW, &temp);
177 int com_readline(int timeout_value, char *input_buffer,int len)
182 struct timeval timeout;
185 if (timeout_value>0) {
186 /* Initialise the file descriptor set */
188 //edited 03/05/99 Matt Mueller - allow reading from different device
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;
198 j=select(FD_SETSIZE, &set, NULL, NULL, &timeout);
203 // j=com_read(&c,1,0);
204 j=com_read(&c,1,timeout_value);
205 if (isprint(c) && (j>0)) {
207 if (i>=len) return i;
214 //added 06/09/99 Matt Mueller - fix nonetwork compile