File: PSerial_unix.cpp

package info (click to toggle)
nqc 2.3.r1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,452 kB
  • ctags: 2,325
  • sloc: cpp: 14,379; lex: 304; yacc: 259; makefile: 126; ansic: 25
file content (381 lines) | stat: -rw-r--r-- 6,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
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
38
39
40
41
42
43
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Initial Developer of this code is Rodd Zurcher.
 * Portions created by Rodd Zurcher are Copyright (C) 1998 Rodd Zurcher.
 * All Rights Reserved.
 */

/*
 * NOTE - this is only a partial implementation of the PSerial abstract
 * class.  It defaults to 2400 bps, 8 data bits, 1 stop, odd parity.  It
 * does not support changing the baud rate or other attributes.  It does
 * now support manual control of RTS and DTR lines.
 */
 
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>

// need another #include for Solaris - Richard Mitchell <gpsphoto@designprofessionals.com>
#ifdef SOLARIS
#include <sys/filio.h>
#endif


#include "PSerial.h"

#ifndef DEFAULT_SERIAL_NAME
#define DEFAULT_SERIAL_NAME "/dev/ttyS0"
#endif


class PSerial_unix : public PSerial
{
public:
					PSerial_unix(void);
	virtual	bool	Open(const char *name);
	virtual void	Close();
	virtual long	Write(const void *ptr, long count);
	virtual void	FlushWrite();

	virtual long	Read(void *ptr, long count);
	virtual bool	SetTimeout(long timeout_ms);
	virtual bool	SetSpeed(int speed, int opts = 0);
	virtual bool	SetDTR(bool state);
	virtual bool	SetRTS(bool state);


private:
	bool	SetRaw(void);
	bool	SetLineData(int bits, bool state);
	  
	int		fTerm;
	long	fTimeout;
};


static int tv_ge(const struct timeval *a, const struct timeval *b);
static void tv_sub(const struct timeval *a, struct timeval *b);
static bool SetTermiosSpeed(termios &tios, int speed);


PSerial* PSerial::NewSerial()
{
	return new PSerial_unix();
}


const char *PSerial::GetDefaultName()
{
	return DEFAULT_SERIAL_NAME;
}


PSerial_unix::PSerial_unix(void) :
	fTerm(-1),
	fTimeout(kPStream_NeverTimeout)
{
}


bool PSerial_unix::Open(const char *name)
{
	if ((fTerm = open(name, O_RDWR)) < 0)
	{
		return false;
	}

	if (!SetRaw())
	{
		return false;
	}

	return true;
}


// Put the tty into raw mode
// Adapted from tty_raw, "Advanced Programing in the UNIX
// Environment", W. Richard Stevens, pp354-355
bool PSerial_unix::SetRaw(void)
{
	termios tios;

	if (tcgetattr(fTerm, &tios) < 0)
		return false;

	// echo off, canonical mode off, extended input processing off
	// signal chars off
	tios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);

	// no SIGINT on BREAK, CR-to-NL off, input parity check off,
	// don't strip 8th bit on input, output flow control off
	tios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);

	// clear size bits, 1 stop bit, disable parity
	tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);

	// set 8 bits/char
	tios.c_cflag |= (CS8);

	// output processing off
	tios.c_oflag &= ~(OPOST);

	// 1 byte at a time, no timer
	tios.c_cc[VMIN] = 1;
	tios.c_cc[VTIME] = 0;


	if (tcsetattr(fTerm, TCSAFLUSH, &tios) < 0)
		return false;


	SetSpeed(9600, 0);
	
	return true;
}


void PSerial_unix::Close()
{
	if (fTerm != -1)
	{
		close(fTerm);
	}
	fTerm = -1;
}


bool PSerial_unix::SetSpeed(int speed, int opts)
{
	termios tios;
	
	if (tcgetattr(fTerm, &tios) < 0)
		return false;

	// set the speed
	if (!SetTermiosSpeed(tios, speed))
		return false;
	
	// set the parity
	switch(opts & kPSerial_ParityMask)
	{
		case kPSerial_ParityNone:
			tios.c_cflag &= ~(PARENB);
			break;
		case kPSerial_ParityOdd:
			tios.c_cflag |= (PARENB | PARODD);
			break;
		case kPSerial_ParityEven:
			tios.c_cflag |= (PARENB);
			tios.c_cflag &= ~(PARODD);
			break;
	};
	
	if (tcsetattr(fTerm, TCSAFLUSH, &tios) < 0)
		return false;

	return true;
}


long PSerial_unix::Write(const void *ptr, long count)
{
	return write(fTerm, ptr, count);
}


void PSerial_unix::FlushWrite()
{
	//tcdrain(fTerm);
}


bool PSerial_unix::SetTimeout(long timeout_ms)
{
	fTimeout = timeout_ms;
	return true;
}


long PSerial_unix::Read(void *ptr, long count)
{
	// Blocking read
	if (fTimeout == kPStream_NeverTimeout)
	{
		long rval = read(fTerm, ptr, count);
		return rval;
	}

	// time limited read
	char *cur = (char *) ptr;
	struct timeval expire;
	struct timezone tz;

	if (gettimeofday(&expire, &tz) < 0)
	{
		return -1;
	}

	expire.tv_sec += fTimeout / 1000;
	expire.tv_usec += (fTimeout % 1000) * 1000;

	while (count)
	{
		fd_set rfds;
		FD_ZERO(&rfds);
		FD_SET(fTerm, &rfds);
		struct timeval delay;
		long nread;

		if (gettimeofday(&delay, &tz) < 0)
		{
			return -1;
		}
		
		if (tv_ge(&delay, &expire))
		{
			// delay (current time) >= expire
		  	break;
		}
		
		tv_sub(&expire, &delay);	// delay = expire - delay

		if (select(fTerm + 1, &rfds, NULL, NULL, &delay))
		{
			if (ioctl(fTerm, FIONREAD, &nread) < 0)
			{
				return -1;
		  	}
		  	
		  	nread = (count < nread) ? count : nread;
		  	if ((nread = read(fTerm, cur, nread)) < 0)
		  	{
				return -1;
		  	}
			count -= nread;
			cur += nread;
		}
		else
		{
		  // timeout
		  break;
		}
	} // while

	return (cur - (char *)ptr);
}

//
// This part implemented by Peter Ljungstrand (SetDTR and SetRTS)
// modifided by Dave Baum
//

bool PSerial_unix::SetDTR(bool state)
{
	return SetLineData(TIOCM_DTR, state);
}


bool PSerial_unix::SetRTS(bool state)
{
	return SetLineData(TIOCM_RTS, state);
}


bool PSerial_unix::SetLineData(int bits, bool state)
{
	int lineData;

	if (ioctl(fTerm, TIOCMGET, &lineData))
	{
		return false;
	} 
	else
	{
		if (state)
		{
			lineData |= bits;
		}
		else
		{
			lineData &= ~bits;
		}
		return !ioctl(fTerm, TIOCMSET, &lineData);
	}
}



bool SetTermiosSpeed(termios &tios, int speed)
{
	speed_t s;
	
	switch(speed)
	{
		case 2400:
			s = B2400;
			break;
		case 4800:
			s = B4800;
			break;
		case 9600:
			s = B9600;
			break;
		default:
			return false;
	}
	
	if (cfsetispeed(&tios, s) < 0)
		return false;
	
	if (cfsetospeed(&tios, s) < 0)
		return false;

	return true;
}


int tv_ge(const struct timeval *a, const struct timeval *b)
{
	if (a->tv_sec < b->tv_sec)
	return 0;			// asec < bsec

	if (a->tv_sec == b->tv_sec)
	{
  		if (a->tv_usec < b->tv_usec)
  		{
			return 0;		// ausec < busec
  		}
		return 1;			// ausec >= busec
	}
	return 1;			// asec > bsec
}


void tv_sub(const struct timeval *a, struct timeval *b)
{
	b->tv_sec = a->tv_sec - b->tv_sec;
	b->tv_usec = a->tv_usec - b->tv_usec;
	if (b->tv_usec < 0)
	{
		b->tv_usec += 1000000;
		b->tv_sec--;
	}
}