comio_nix.c 8.16 KB
Newer Older
deuce's avatar
deuce committed
1
2
3
4
5
6
7
8
9
10
/* comio_nix.c */

/* Synchronet Serial Communications I/O Library Functions for *nix */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
deuce's avatar
deuce committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

deuce's avatar
deuce committed
38
#include <sys/ioctl.h>
deuce's avatar
deuce committed
39
#include <sys/file.h>
rswindell's avatar
rswindell committed
40
#include <fcntl.h>		// O_NONBLOCK
deuce's avatar
deuce committed
41
42
43
#include "comio.h"
#include "genwrap.h"

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifdef SPEED_MACROS_ONLY

#define SUPPORTED_SPEED(x) \
	if (speed <= (x)) \
		return B##x

speed_t rate_to_macro(unsigned long speed)
{
	// Standard values
	SUPPORTED_SPEED(0);
	SUPPORTED_SPEED(50);
	SUPPORTED_SPEED(75);
	SUPPORTED_SPEED(110);
	SUPPORTED_SPEED(134);
	SUPPORTED_SPEED(150);
	SUPPORTED_SPEED(200);
	SUPPORTED_SPEED(300);
	SUPPORTED_SPEED(600);
	SUPPORTED_SPEED(1200);
	SUPPORTED_SPEED(1800);
	SUPPORTED_SPEED(2400);
	SUPPORTED_SPEED(4800);
	SUPPORTED_SPEED(9600);
	SUPPORTED_SPEED(19200);
	SUPPORTED_SPEED(38400);

	// Non-POSIX
#ifdef B57600
	SUPPORTED_SPEED(57600);
#endif
#ifdef B115200
	SUPPORTED_SPEED(115200);
#endif
#ifdef B230400
	SUPPORTED_SPEED(230400);
#endif
#ifdef B460800
	SUPPORTED_SPEED(460800);
#endif
#ifdef B500000
	SUPPORTED_SPEED(500000);
#endif
#ifdef B576000
	SUPPORTED_SPEED(576000);
#endif
#ifdef B921600
	SUPPORTED_SPEED(921600);
#endif
#ifdef B1000000
	SUPPORTED_SPEED(1000000);
#endif
#ifdef B1152000
	SUPPORTED_SPEED(1152000);
#endif
#ifdef B1500000
	SUPPORTED_SPEED(1500000);
#endif
#ifdef B2000000
	SUPPORTED_SPEED(2000000);
#endif
#ifdef B2500000
	SUPPORTED_SPEED(2500000);
#endif
#ifdef B3000000
	SUPPORTED_SPEED(3000000);
#endif
#ifdef B3500000
	SUPPORTED_SPEED(3500000);
#endif
#ifdef B4000000
	SUPPORTED_SPEED(4000000);
#endif
	return B0;
}
#undef SUPPORTED_SPEED
#define SUPPORTED_SPEED(x) \
	if (speed == B##x) \
		return x;

unsigned long macro_to_rate(speed_t speed)
{
	// Standard values
	SUPPORTED_SPEED(0);
	SUPPORTED_SPEED(50);
	SUPPORTED_SPEED(75);
	SUPPORTED_SPEED(110);
	SUPPORTED_SPEED(134);
	SUPPORTED_SPEED(150);
	SUPPORTED_SPEED(200);
	SUPPORTED_SPEED(300);
	SUPPORTED_SPEED(600);
	SUPPORTED_SPEED(1200);
	SUPPORTED_SPEED(1800);
	SUPPORTED_SPEED(2400);
	SUPPORTED_SPEED(4800);
	SUPPORTED_SPEED(9600);
	SUPPORTED_SPEED(19200);
	SUPPORTED_SPEED(38400);

	// Non-POSIX
#ifdef B57600
	SUPPORTED_SPEED(57600);
#endif
#ifdef B115200
	SUPPORTED_SPEED(115200);
#endif
#ifdef B230400
	SUPPORTED_SPEED(230400);
#endif
#ifdef B460800
	SUPPORTED_SPEED(460800);
#endif
#ifdef B500000
	SUPPORTED_SPEED(500000);
#endif
#ifdef B576000
	SUPPORTED_SPEED(576000);
#endif
#ifdef B921600
	SUPPORTED_SPEED(921600);
#endif
#ifdef B1000000
	SUPPORTED_SPEED(1000000);
#endif
#ifdef B1152000
	SUPPORTED_SPEED(1152000);
#endif
#ifdef B1500000
	SUPPORTED_SPEED(1500000);
#endif
#ifdef B2000000
	SUPPORTED_SPEED(2000000);
#endif
#ifdef B2500000
	SUPPORTED_SPEED(2500000);
#endif
#ifdef B3000000
	SUPPORTED_SPEED(3000000);
#endif
#ifdef B3500000
	SUPPORTED_SPEED(3500000);
#endif
#ifdef B4000000
	SUPPORTED_SPEED(4000000);
#endif
	return 0;
}
#undef SUPPORTED_SPEED

#else
#define rate_to_macro(x)	(x)
#define macro_to_rate(x)	(x)
#endif

deuce's avatar
DLL-ify    
deuce committed
198
char* COMIOCALL comVersion(char* str, size_t len)
deuce's avatar
deuce committed
199
200
201
202
203
204
205
206
207
{
	char revision[16];

	sscanf("$Revision$", "%*s %s", revision);

	safe_snprintf(str,len,"Synchronet Communications I/O Library for "PLATFORM_DESC" v%s", revision);
	return str;
}

deuce's avatar
DLL-ify    
deuce committed
208
COM_HANDLE COMIOCALL comOpen(const char* device)
deuce's avatar
deuce committed
209
210
211
212
{
	COM_HANDLE handle;
	struct termios t;

deuce's avatar
deuce committed
213
	if((handle=open(device, O_NONBLOCK|O_RDWR))==COM_HANDLE_INVALID)
deuce's avatar
deuce committed
214
215
216
		return COM_HANDLE_INVALID;

	if(tcgetattr(handle, &t)==-1) {
deuce's avatar
deuce committed
217
		close(handle);
deuce's avatar
deuce committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		return COM_HANDLE_INVALID;
	}

	t.c_iflag = (
				  IGNBRK   /* ignore BREAK condition */
				| IGNPAR   /* ignore (discard) parity errors */
				);
	t.c_oflag = 0;	/* No output processing */
	t.c_cflag = (
				  CS8         /* 8 bits */
				| CREAD       /* enable receiver */
/*
Fun snippet from the FreeBSD manpage:

     If CREAD is set, the receiver is enabled.  Otherwise, no character is
     received.  Not all hardware supports this bit.  In fact, this flag is
     pretty silly and if it were not part of the termios specification it
     would be omitted.
*/
				| HUPCL       /* hang up on last close */
				| CLOCAL      /* ignore modem status lines */
/* The next two are pretty much never used */
#ifdef CCTX_OFLOW
				| CCTS_OFLOW  /* CTS flow control of output */
#endif
#ifdef CRTSCTS
				| CRTSCTS     /* same as CCTS_OFLOW */
#endif
#ifdef CRTS_IFLOW
				| CRTS_IFLOW  /* RTS flow control of input */
#endif
				);
	t.c_lflag = 0;	/* No local modes */
	if(tcsetattr(handle, TCSANOW, &t)==-1) {
deuce's avatar
deuce committed
252
		close(handle);
deuce's avatar
deuce committed
253
254
255
256
257
258
		return COM_HANDLE_INVALID;
	}
	
	return handle;
}

deuce's avatar
DLL-ify    
deuce committed
259
BOOL COMIOCALL comClose(COM_HANDLE handle)
deuce's avatar
deuce committed
260
261
262
263
{
	return (!close(handle));
}

deuce's avatar
DLL-ify    
deuce committed
264
long COMIOCALL comGetBaudRate(COM_HANDLE handle)
deuce's avatar
deuce committed
265
266
267
268
269
270
271
272
273
274
275
276
{
	struct termios t;
	speed_t	in;
	speed_t	out;

	if(tcgetattr(handle, &t))
		return COM_ERROR;

	/* 
	 * We actually have TWO speeds available...
	 * return the biggest one
	 */
277
278
	in = macro_to_rate(cfgetispeed(&t));
	out = macro_to_rate(cfgetospeed(&t));
deuce's avatar
deuce committed
279
280
281
	return ((long)(in>out?in:out));
}

deuce's avatar
DLL-ify    
deuce committed
282
BOOL COMIOCALL comSetBaudRate(COM_HANDLE handle, unsigned long rate)
deuce's avatar
deuce committed
283
284
285
286
287
288
{
	struct termios t;

	if(tcgetattr(handle, &t))
		return FALSE;

289
290
	cfsetispeed(&t, rate_to_macro(rate));
	cfsetospeed(&t, rate_to_macro(rate));
deuce's avatar
deuce committed
291
292
293
294
295
296
	if(tcsetattr(handle, TCSANOW, &t)==-1)
		return FALSE;

	return TRUE;
}

deuce's avatar
DLL-ify    
deuce committed
297
int COMIOCALL comGetModemStatus(COM_HANDLE handle)
deuce's avatar
deuce committed
298
299
300
301
302
303
304
305
306
{
	int status;

	if(ioctl(handle, TIOCMGET, &status)==-1)
		return COM_ERROR;

	return status;
}

deuce's avatar
deuce committed
307
308
309
310
311
312
313
static BOOL comSetFlags(COM_HANDLE handle, int flags, BOOL set)
{
	int cmd = set ? TIOCMBIS : TIOCMBIC;

	return ioctl(handle, cmd, &flags);
}

deuce's avatar
DLL-ify    
deuce committed
314
BOOL COMIOCALL comRaiseDTR(COM_HANDLE handle)
deuce's avatar
deuce committed
315
{
deuce's avatar
deuce committed
316
	return comSetFlags(handle, TIOCM_DTR, TRUE);
deuce's avatar
deuce committed
317
318
}

deuce's avatar
DLL-ify    
deuce committed
319
BOOL COMIOCALL comLowerDTR(COM_HANDLE handle)
deuce's avatar
deuce committed
320
{
deuce's avatar
deuce committed
321
	return comSetFlags(handle, TIOCM_DTR, FALSE);
deuce's avatar
deuce committed
322
323
}

deuce's avatar
deuce committed
324
325
326
327
328
329
330
331
332
BOOL COMIOCALL comRaiseRTS(COM_HANDLE handle)
{
	return comSetFlags(handle, TIOCM_RTS, TRUE);
}

BOOL COMIOCALL comLowerRTS(COM_HANDLE handle)
{
	return comSetFlags(handle, TIOCM_RTS, FALSE);
}
deuce's avatar
DLL-ify    
deuce committed
333
BOOL COMIOCALL comWriteByte(COM_HANDLE handle, BYTE ch)
deuce's avatar
deuce committed
334
335
336
337
{
	return(write(handle, &ch, 1)==1);
}

deuce's avatar
DLL-ify    
deuce committed
338
int COMIOCALL comWriteBuf(COM_HANDLE handle, const BYTE* buf, size_t buflen)
deuce's avatar
deuce committed
339
{
deuce's avatar
deuce committed
340
	return write(handle, buf, buflen);
deuce's avatar
deuce committed
341
342
343
344
345
}

/*
 * TODO: This seem kinda dangerous for short writes...
 */
deuce's avatar
DLL-ify    
deuce committed
346
int COMIOCALL comWriteString(COM_HANDLE handle, const char* str)
deuce's avatar
deuce committed
347
{
deuce's avatar
deuce committed
348
	return comWriteBuf(handle, (BYTE*)str, strlen(str));
deuce's avatar
deuce committed
349
350
}

deuce's avatar
DLL-ify    
deuce committed
351
BOOL COMIOCALL comReadByte(COM_HANDLE handle, BYTE* ch)
deuce's avatar
deuce committed
352
353
354
355
{
	return(read(handle, ch, 1)==1);
}

deuce's avatar
DLL-ify    
deuce committed
356
BOOL COMIOCALL comPurgeInput(COM_HANDLE handle)
deuce's avatar
deuce committed
357
{
deuce's avatar
deuce committed
358
	return(tcflush(handle, TCIFLUSH)==0);
deuce's avatar
deuce committed
359
360
}

deuce's avatar
DLL-ify    
deuce committed
361
BOOL COMIOCALL comPurgeOutput(COM_HANDLE handle)
deuce's avatar
deuce committed
362
{
deuce's avatar
deuce committed
363
	return(tcflush(handle, TCOFLUSH)==0);
deuce's avatar
deuce committed
364
365
}

deuce's avatar
DLL-ify    
deuce committed
366
BOOL COMIOCALL comDrainOutput(COM_HANDLE handle)
367
368
369
{
	return(tcdrain(handle)==0);
}