comio_nix.c 9.22 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
deuce committed
198
char* 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
deuce committed
208
COM_HANDLE 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
deuce committed
259
BOOL comClose(COM_HANDLE handle)
deuce's avatar
deuce committed
260
261
262
263
{
	return (!close(handle));
}

deuce's avatar
deuce committed
264
long 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
deuce committed
282
BOOL 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;
}

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
BOOL comGetTxFlowControl(COM_HANDLE handle)
{
	BOOL ret = FALSE;
	struct termios t;

	if(tcgetattr(handle, &t)==-1)
		return FALSE;

	ret = (t.c_cflag & (0
/* 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
				)) ? TRUE : FALSE;

	return ret;
}

BOOL comSetTxFlowControl(COM_HANDLE handle, BOOL enable)
{
	struct termios t;

	if(tcgetattr(handle, &t)==-1)
		return FALSE;

	if (enable) {
		t.c_cflag |= (0
/* 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
				);
	}
	else {
		t.c_cflag &= ~(0
/* 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
				);
	}
	if(tcsetattr(handle, TCSANOW, &t)==-1)
		return FALSE;
	return TRUE;
}

deuce's avatar
deuce committed
361
int comGetModemStatus(COM_HANDLE handle)
deuce's avatar
deuce committed
362
363
364
365
366
367
368
369
370
{
	int status;

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

	return status;
}

deuce's avatar
deuce committed
371
372
373
374
375
376
377
static BOOL comSetFlags(COM_HANDLE handle, int flags, BOOL set)
{
	int cmd = set ? TIOCMBIS : TIOCMBIC;

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

deuce's avatar
deuce committed
378
BOOL comRaiseDTR(COM_HANDLE handle)
deuce's avatar
deuce committed
379
{
deuce's avatar
deuce committed
380
	return comSetFlags(handle, TIOCM_DTR, TRUE);
deuce's avatar
deuce committed
381
382
}

deuce's avatar
deuce committed
383
BOOL comLowerDTR(COM_HANDLE handle)
deuce's avatar
deuce committed
384
{
deuce's avatar
deuce committed
385
	return comSetFlags(handle, TIOCM_DTR, FALSE);
deuce's avatar
deuce committed
386
387
}

deuce's avatar
deuce committed
388
BOOL comRaiseRTS(COM_HANDLE handle)
deuce's avatar
deuce committed
389
390
391
392
{
	return comSetFlags(handle, TIOCM_RTS, TRUE);
}

deuce's avatar
deuce committed
393
BOOL comLowerRTS(COM_HANDLE handle)
deuce's avatar
deuce committed
394
395
396
{
	return comSetFlags(handle, TIOCM_RTS, FALSE);
}
deuce's avatar
deuce committed
397
BOOL comWriteByte(COM_HANDLE handle, BYTE ch)
deuce's avatar
deuce committed
398
399
400
401
{
	return(write(handle, &ch, 1)==1);
}

deuce's avatar
deuce committed
402
int comWriteBuf(COM_HANDLE handle, const BYTE* buf, size_t buflen)
deuce's avatar
deuce committed
403
{
deuce's avatar
deuce committed
404
	return write(handle, buf, buflen);
deuce's avatar
deuce committed
405
406
407
408
409
}

/*
 * TODO: This seem kinda dangerous for short writes...
 */
deuce's avatar
deuce committed
410
int comWriteString(COM_HANDLE handle, const char* str)
deuce's avatar
deuce committed
411
{
deuce's avatar
deuce committed
412
	return comWriteBuf(handle, (BYTE*)str, strlen(str));
deuce's avatar
deuce committed
413
414
}

deuce's avatar
deuce committed
415
BOOL comReadByte(COM_HANDLE handle, BYTE* ch)
deuce's avatar
deuce committed
416
417
418
419
{
	return(read(handle, ch, 1)==1);
}

deuce's avatar
deuce committed
420
BOOL comPurgeInput(COM_HANDLE handle)
deuce's avatar
deuce committed
421
{
deuce's avatar
deuce committed
422
	return(tcflush(handle, TCIFLUSH)==0);
deuce's avatar
deuce committed
423
424
}

deuce's avatar
deuce committed
425
BOOL comPurgeOutput(COM_HANDLE handle)
deuce's avatar
deuce committed
426
{
deuce's avatar
deuce committed
427
	return(tcflush(handle, TCOFLUSH)==0);
deuce's avatar
deuce committed
428
429
}

deuce's avatar
deuce committed
430
BOOL comDrainOutput(COM_HANDLE handle)
431
432
433
{
	return(tcdrain(handle)==0);
}