summaryrefslogtreecommitdiffabout
path: root/gammu/emb/common/device/serial/ser_unx.c
blob: 18b5f6f4fa60b662e8730553adc4234dcb7537ef (plain)
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
/* (c) 2002-2004 by Marcin Wiacek */
/* locking device and settings all speeds by Michal Cihar */
/* based on some work from Gnokii (www.gnokii.org)
 * (C) 1999-2000 Hugh Blemings & Pavel Janik ml. (C) 2001-2004 Pawel Kot 
 * GNU GPL version 2 or later
 */
/* Due to a problem in the source code management, the names of some of
 * the authors have unfortunately been lost. We do not mean to belittle
 * their efforts and hope they will contact us to see their names
 * properly added to the Copyright notice above.
 * Having published their contributions under the terms of the GNU
 * General Public License (GPL) [version 2], the Copyright of these
 * authors will remain respected by adhering to the license they chose
 * to publish their code under.
 */

#include "../../gsmstate.h"

#ifdef GSM_ENABLE_SERIALDEVICE
#ifndef WIN32
#ifndef DJGPP

#include <sys/file.h>
#include <sys/time.h>
#include <string.h>
#include <termios.h>
#include <errno.h>

#include "../../gsmcomon.h"
#include "ser_unx.h"

#ifndef O_NONBLOCK
#  define O_NONBLOCK  0
#endif

#ifdef __NetBSD__
#  define FNONBLOCK O_NONBLOCK

#  define  B57600   0010001
#  define  B115200  0010002
#  define  B230400  0010003
#  define  B460800  0010004
#  define  B500000  0010005
#  define  B576000  0010006
#  define  B921600  0010007
#  define  B1000000 0010010
#  define  B1152000 0010011
#  define  B1500000 0010012
#  define  B2000000 0010013
#  define  B2500000 0010014
#  define  B3000000 0010015
#  define  B3500000 0010016
#  define  B4000000 0010017
#endif

static GSM_Error serial_close(GSM_StateMachine *s)
{
	GSM_Device_SerialData *d = &s->Device.Data.Serial;

	/* Restores old settings */
	tcsetattr(d->hPhone, TCSANOW, &d->old_settings);

	/* Closes device */
	close(d->hPhone);

	return ERR_NONE;
}

static GSM_Error serial_open (GSM_StateMachine *s)
{
	GSM_Device_SerialData   *d = &s->Device.Data.Serial;
	struct termios	  	t;
	int			i;

	/* O_NONBLOCK MUST is required to avoid waiting for DCD */
	d->hPhone = open(s->CurrentConfig->Device, O_RDWR | O_NOCTTY | O_NONBLOCK);
	if (d->hPhone < 0) {
		i = errno;
		GSM_OSErrorInfo(s,"open in serial_open");
		if (i ==  2) return ERR_DEVICENOTEXIST;		//no such file or directory
		if (i == 13) return ERR_DEVICENOPERMISSION;	//permission denied
		return ERR_DEVICEOPENERROR;
	}

#ifdef TIOCEXCL
	/* open() calls from other applications shall fail now */
	ioctl(d->hPhone, TIOCEXCL, (char *) 0);
#endif

	if (tcgetattr(d->hPhone, &d->old_settings) == -1) {
		close(d->hPhone);
		GSM_OSErrorInfo(s,"tcgetattr in serial_open");
		return ERR_DEVICEREADERROR;
    	}

    	if (tcflush(d->hPhone, TCIOFLUSH) == -1) {
		serial_close(s);
		GSM_OSErrorInfo(s,"tcflush in serial_open");
		return ERR_DEVICEOPENERROR;
    	}

    	memcpy(&t, &d->old_settings, sizeof(struct termios));

	/* Opening without parity */
    	t.c_iflag       = IGNPAR;
    	t.c_oflag       = 0;
    	/* disconnect line, 8 bits, enable receiver,
     	 * ignore modem lines,lower modem line after disconnect
     	 */
    	t.c_cflag       = B0 | CS8 | CREAD | CLOCAL | HUPCL;
    	/* enable hardware (RTS/CTS) flow control (NON POSIX) 	*/
    	/* t.c_cflag 	|= CRTSCTS;			  	*/
    	t.c_lflag       = 0;
    	t.c_cc[VMIN]    = 1;
    	t.c_cc[VTIME]   = 0;

    	if (tcsetattr(d->hPhone, TCSANOW, &t) == -1) {
		serial_close(s);
		GSM_OSErrorInfo(s,"tcsetattr in serial_open");
		return ERR_DEVICEOPENERROR;
    	}

    	/* Making file descriptor asynchronous. */
    	if (fcntl(d->hPhone, F_SETFL, FASYNC | FNONBLOCK) == -1) {
		serial_close(s);
		GSM_OSErrorInfo(s,"fcntl in serial_open");
		return ERR_DEVICEOPENERROR;
    	}

    	return ERR_NONE;
}

static GSM_Error serial_setparity(GSM_StateMachine *s, bool parity)
{
    	GSM_Device_SerialData   *d = &s->Device.Data.Serial;
    	struct termios	  	t;

    	if (tcgetattr(d->hPhone, &t)) {
		GSM_OSErrorInfo(s,"tcgetattr in serial_setparity");
		return ERR_DEVICEREADERROR;
    	}

    	if (parity) {
		t.c_cflag |= (PARENB | PARODD);
		t.c_iflag = 0;
    	} else {
		t.c_iflag = IGNPAR;
    	}

    	if (tcsetattr(d->hPhone, TCSANOW, &t) == -1){
		serial_close(s);
		GSM_OSErrorInfo(s,"tcsetattr in serial_setparity");
		return ERR_DEVICEPARITYERROR;
    	}

    	return ERR_NONE;
}

static GSM_Error serial_setdtrrts(GSM_StateMachine *s, bool dtr, bool rts)
{
    	GSM_Device_SerialData   *d = &s->Device.Data.Serial;
    	struct termios	  	t;
    	unsigned int	    	flags;

    	if (tcgetattr(d->hPhone, &t)) {
		GSM_OSErrorInfo(s,"tcgetattr in serial_setdtrrts");
		return ERR_DEVICEREADERROR;
    	}

#ifdef CRTSCTS
    	/* Disabling hardware flow control */
    	t.c_cflag &= ~CRTSCTS;
#endif

    	if (tcsetattr(d->hPhone, TCSANOW, &t) == -1) {
		serial_close(s);
		GSM_OSErrorInfo(s,"tcsetattr in serial_setdtrrts");
		return ERR_DEVICEDTRRTSERROR;
    	}

    	flags = TIOCM_DTR;
    	if (dtr) {
		ioctl(d->hPhone, TIOCMBIS, &flags);
	} else {
		ioctl(d->hPhone, TIOCMBIC, &flags);
	}

    	flags = TIOCM_RTS;
    	if (rts) {
		ioctl(d->hPhone, TIOCMBIS, &flags);
	} else {
		ioctl(d->hPhone, TIOCMBIC, &flags);
	}

    	flags = 0;
    	ioctl(d->hPhone, TIOCMGET, &flags);

    	dbgprintf("Serial device:");
    	dbgprintf(" DTR is %s",       flags&TIOCM_DTR?"up":"down");
    	dbgprintf(", RTS is %s",      flags&TIOCM_RTS?"up":"down");
    	dbgprintf(", CAR is %s",      flags&TIOCM_CAR?"up":"down");
    	dbgprintf(", CTS is %s\n",    flags&TIOCM_CTS?"up":"down");
    	if (((flags&TIOCM_DTR)==TIOCM_DTR) != dtr) return ERR_DEVICEDTRRTSERROR;
    	if (((flags&TIOCM_RTS)==TIOCM_RTS) != rts) return ERR_DEVICEDTRRTSERROR;

    	return ERR_NONE;
}

static GSM_Error serial_setspeed(GSM_StateMachine *s, int speed)
{
    	GSM_Device_SerialData 	*d = &s->Device.Data.Serial;
    	struct termios  	t;
    	int	     		speed2 = B19200;

    	if (tcgetattr(d->hPhone, &t)) {
		GSM_OSErrorInfo(s,"tcgetattr in serial_setspeed");
		return ERR_DEVICEREADERROR;
    	}

    	smprintf(s, "Setting speed to %d\n", speed);

    	switch (speed) {
		case 50:	speed2 = B50;		break;
		case 75:	speed2 = B75;		break;
		case 110:	speed2 = B110;		break;
		case 134:	speed2 = B134;		break;
		case 150:	speed2 = B150;		break;
		case 200:	speed2 = B200;		break;
		case 300:	speed2 = B300;		break;
		case 600:	speed2 = B600;		break;
		case 1200:	speed2 = B1200;		break;
		case 1800:	speed2 = B1800;		break;
		case 2400:	speed2 = B2400;		break;
		case 4800:	speed2 = B4800;		break;
		case 9600:	speed2 = B9600;		break;
		case 19200:	speed2 = B19200;	break;
		case 38400:	speed2 = B38400;	break;
#ifdef B57600
		case 57600:	speed2 = B57600;	break;
		case 115200:	speed2 = B115200;	break;
		case 230400:	speed2 = B230400;	break;
		case 460800:	speed2 = B460800;	break;
#ifdef B500000
		case 500000:	speed2 = B500000;	break;
		case 576000:	speed2 = B576000;	break;
		case 921600:	speed2 = B921600;	break;
		case 1000000:	speed2 = B1000000;	break;
		case 1152000:	speed2 = B1152000;	break;
		case 1500000:	speed2 = B1500000;	break;
		case 2000000:	speed2 = B2000000;	break;
		case 2500000:	speed2 = B2500000;	break;
		case 3000000:	speed2 = B3000000;	break;
		case 3500000:	speed2 = B3500000;	break;
		case 4000000:	speed2 = B4000000;	break;	
#endif
#endif
	}

    	/* This should work on all systems because it is done according to POSIX */
    	cfsetispeed(&t, speed2);
    	cfsetospeed(&t, speed2);

    	if (tcsetattr(d->hPhone, TCSADRAIN, &t) == -1) {
		serial_close(s);
		GSM_OSErrorInfo(s,"tcsetattr in serial_setspeed");
		return ERR_DEVICECHANGESPEEDERROR;
    	}

    	return ERR_NONE;
}

static int serial_read(GSM_StateMachine *s, void *buf, size_t nbytes)
{
    	GSM_Device_SerialData 		*d = &s->Device.Data.Serial;
    	struct timeval  		timeout2;
    	fd_set	  			readfds;
    	int	     			actual = 0;

    	FD_ZERO(&readfds);
    	FD_SET(d->hPhone, &readfds);

    	timeout2.tv_sec     = 0;
    	timeout2.tv_usec    = 1;

    	if (select(d->hPhone+1, &readfds, NULL, NULL, &timeout2)) {
		actual = read(d->hPhone, buf, nbytes);
		if (actual == -1) GSM_OSErrorInfo(s,"serial_read");
    	}
    	return actual;
}

static int serial_write(GSM_StateMachine *s, void *buf, size_t nbytes)
{
    	GSM_Device_SerialData   *d = &s->Device.Data.Serial;
    	int		     	ret;
    	size_t		  	actual = 0;

    	do {
		ret = write(d->hPhone, (unsigned char *)buf, nbytes - actual);
		if (ret < 0 && errno == EAGAIN) continue;
		if (ret < 0) {
	    		if (actual != nbytes) GSM_OSErrorInfo(s,"serial_write");
	    		return actual;
		}
		actual  += ret;
		buf     += ret;
    		if (s->ConnectionType == GCT_FBUS2PL2303) my_sleep(1);
    	} while (actual < nbytes);
    	return actual;
}

GSM_Device_Functions SerialDevice = {
    	serial_open,
    	serial_close,
    	serial_setparity,
    	serial_setdtrrts,
    	serial_setspeed,
    	serial_read,
    	serial_write
};

#endif
#endif
#endif

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */