-
Notifications
You must be signed in to change notification settings - Fork 207
/
tty.c
518 lines (496 loc) · 10.1 KB
/
tty.c
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway
*
* tty.c - terminal I/O related procedures
*
* Copyright (c) 2002-2003, 2013, Victor Antonovich (v.antonovich@gmail.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: tty.c,v 1.4 2015/02/25 10:33:58 kapyar Exp $
*/
#include "tty.h"
extern cfg_t cfg;
static int tty_break;
/*
* Flag signal SIG
*/
void
tty_sighup(void)
{
tty_break = TRUE;
return;
}
/*
* Init serial link parameters
*/
void
tty_init(ttydata_t *mod)
{
mod->fd = -1;
mod->port = cfg.ttyport;
mod->speed = cfg.ttyspeed;
mod->bpc = DEFAULT_BITS_PER_CHAR;
if (toupper(cfg.ttymode[1]) != 'N')
{
mod->bpc++;
}
if (cfg.ttymode[2] == '2')
{
mod->bpc++;
}
#ifdef HAVE_TIOCRS485
mod->rs485 = cfg.rs485;
#endif
#ifdef TRXCTL
mod->trxcntl = cfg.trxcntl;
#endif
}
#ifdef HAVE_LIBUTIL
char *tty_get_name(char *ttyfullname);
char *
tty_get_name(char *ttyfullname)
{
static char ttynamebuf[INTBUFSIZE + 1];
char *ttyname = ttynamebuf, *ttynameptr = ttyname;
strncpy(ttynamebuf, ttyfullname, INTBUFSIZE);
for (ttynameptr = strtok(ttynamebuf, "/");
ttynameptr;
ttynameptr = strtok(NULL, "/"))
{
ttyname = ttynameptr;
}
return ttyname;
}
#endif
/*
* Opening serial link whith parameters in MOD
*/
int
tty_open(ttydata_t *mod)
{
#ifdef HAVE_LIBUTIL
int buferr, uuerr;
char *ttyname = tty_get_name(mod->port);
#endif
if (mod->fd > 0)
return RC_AOPEN; /* if already open... */
tty_break = FALSE;
#ifdef HAVE_LIBUTIL
if ((uuerr = uu_lock(ttyname)) != UU_LOCK_OK)
{
buferr = errno;
#ifdef LOG
logw(0, "uu_lock(): can't lock tty device %s (%s)",
ttyname, uu_lockerr(uuerr));
#endif
errno = buferr;
return RC_ERR;
}
#endif
#ifdef LOG
logw(2, "tty: trying to open %s (speed %d mode %s)", mod->port, mod->speed, cfg.ttymode);
#endif
mod->fd = open(mod->port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (mod->fd < 0)
return RC_ERR; /* attempt failed */
return tty_set_attr(mod);
}
/*
* Setting up tty device MOD attributes
*/
int
tty_set_attr(ttydata_t *mod)
{
int flag;
speed_t tspeed = tty_transpeed(mod->speed);
memset(&mod->savedtios, 0, sizeof(struct termios));
if (tcgetattr(mod->fd, &mod->savedtios))
return RC_ERR;
memcpy(&mod->tios, &mod->savedtios, sizeof(mod->tios));
#ifdef HAVE_CFMAKERAW
cfmakeraw(&mod->tios);
#else
mod->tios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
mod->tios.c_oflag &= ~OPOST;
mod->tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
#endif
mod->tios.c_cflag |= (CREAD | CLOCAL);
mod->tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CRTSCTS);
switch (cfg.ttymode[0])
{
case '5':
mod->tios.c_cflag |= CS5;
break;
case '6':
mod->tios.c_cflag |= CS6;
break;
case '7':
mod->tios.c_cflag |= CS7;
break;
case '8':
mod->tios.c_cflag |= CS8;
break;
}
switch (toupper(cfg.ttymode[1]))
{
case 'E':
mod->tios.c_cflag |= PARENB;
break;
case 'O':
mod->tios.c_cflag |= (PARENB | PARODD);
break;
}
if (cfg.ttymode[2] == '2')
{
mod->tios.c_cflag |= CSTOPB;
}
mod->tios.c_cc[VTIME] = 0;
mod->tios.c_cc[VMIN] = 1;
#ifdef HAVE_CFSETSPEED
cfsetspeed(&mod->tios, tspeed);
#else
cfsetispeed(&mod->tios, tspeed);
cfsetospeed(&mod->tios, tspeed);
#endif
if (tcsetattr(mod->fd, TCSANOW, &mod->tios))
return RC_ERR;
tcflush(mod->fd, TCIOFLUSH);
#ifdef TRXCTL
tty_clr_rts(mod->fd);
#endif
#ifdef HAVE_TIOCRS485
if (mod->rs485)
{
struct serial_rs485 rs485conf;
#ifdef LOG
logw(2, "tty: trying to enable RS-485 support for %s", mod->port);
#endif
if (ioctl(mod->fd, TIOCGRS485, &rs485conf) < 0) {
return RC_ERR;
}
rs485conf.flags |= SER_RS485_ENABLED;
if (ioctl(mod->fd, TIOCSRS485, &rs485conf) < 0) {
return RC_ERR;
}
#ifdef LOG
logw(2, "tty: enabled RS-485 support for %s", mod->port);
#endif
}
#endif
flag = fcntl(mod->fd, F_GETFL, 0);
if (flag < 0)
return RC_ERR;
return fcntl(mod->fd, F_SETFL, flag | O_NONBLOCK);
}
/*
* Translate integer SPEED value to speed_t constant
*/
speed_t
tty_transpeed(int speed)
{
speed_t tspeed;
switch (speed)
{
#if defined(B50)
case 50:
tspeed = B50;
break;
#endif
#if defined(B75)
case 75:
tspeed = B75;
break;
#endif
#if defined(B110)
case 110:
tspeed = B110;
break;
#endif
#if defined(B134)
case 134:
tspeed = B134;
break;
#endif
#if defined(B150)
case 150:
tspeed = B150;
break;
#endif
#if defined(B200)
case 200:
tspeed = B200;
break;
#endif
#if defined(B300)
case 300:
tspeed = B300;
break;
#endif
#if defined(B600)
case 600:
tspeed = B600;
break;
#endif
#if defined(B1200)
case 1200:
tspeed = B1200;
break;
#endif
#if defined(B1800)
case 1800:
tspeed = B1800;
break;
#endif
#if defined(B2400)
case 2400:
tspeed = B2400;
break;
#endif
#if defined(B4800)
case 4800:
tspeed = B4800;
break;
#endif
#if defined(B7200)
case 7200:
tspeed = B7200;
break;
#endif
#if defined(B9600)
case 9600:
tspeed = B9600;
break;
#endif
#if defined(B12000)
case 12000:
tspeed = B12000;
break;
#endif
#if defined(B14400)
case 14400:
tspeed = B14400;
break;
#endif
#if defined(B19200)
case 19200:
tspeed = B19200;
break;
#elif defined(EXTA)
case 19200:
tspeed = EXTA;
break;
#endif
#if defined(B38400)
case 38400:
tspeed = B38400;
break;
#elif defined(EXTB)
case 38400:
tspeed = EXTB;
break;
#endif
#if defined(B57600)
case 57600:
tspeed = B57600;
break;
#endif
#if defined(B115200)
case 115200:
tspeed = B115200;
break;
#endif
#if defined(B230400)
case 230400:
tspeed = B230400;
break;
#endif
#if defined(B460800)
case 460800:
tspeed = B460800;
break;
#endif
#if defined(B500000)
case 500000:
tspeed = B500000;
break;
#endif
#if defined(B576000)
case 576000:
tspeed = B576000;
break;
#endif
#if defined(B921600)
case 921600:
tspeed = B921600;
break;
#endif
#if defined(B1000000)
case 1000000:
tspeed = B1000000;
break;
#endif
#if defined(B1152000)
case 1152000:
tspeed = B1152000;
break;
#endif
#if defined(B1500000)
case 1500000:
tspeed = B1500000;
break;
#endif
#if defined(B2000000)
case 2000000:
tspeed = B2000000;
break;
#endif
#if defined(B2500000)
case 2500000:
tspeed = B2500000;
break;
#endif
#if defined(B3000000)
case 3000000:
tspeed = B3000000;
break;
#endif
#if defined(B3500000)
case 3500000:
tspeed = B3500000;
break;
#endif
#if defined(B4000000)
case 4000000:
tspeed = B4000000;
break;
#endif
default:
#ifdef LOG
logw(0, "tty: unsupported speed: %d", speed);
#endif
exit (-1);
}
return tspeed;
}
/*
* Prepare tty device MOD to closing
*/
int
tty_cooked(ttydata_t *mod)
{
if (!isatty(mod->fd))
return RC_ERR;
if (tcsetattr(mod->fd, TCSAFLUSH, &mod->savedtios))
return RC_ERR;
return RC_OK;
}
/*
* Closing tty device MOD
*/
int
tty_close(ttydata_t *mod)
{
#ifdef HAVE_LIBUTIL
int buferr;
char *ttyname = tty_get_name(mod->port);
#endif
if (mod->fd < 0)
return RC_ACLOSE; /* already closed */
tty_cooked(mod);
#ifdef HAVE_LIBUTIL
if (close(mod->fd))
return RC_ERR;
if (uu_unlock(ttyname))
{
buferr = errno;
#ifdef LOG
logw(0, "uu_lock(): can't unlock tty device %s",
ttyname);
#endif
errno = buferr;
return RC_ERR;
}
return RC_OK;
#else
return close(mod->fd);
#endif
}
#ifdef TRXCTL
void sysfs_gpio_set(char *filename, char *value) {
int fd;
fd = open(filename, O_WRONLY);
/* write first byte. Should only be 0 or 1 */
write(fd, value, 1);
close(fd);
logw(9, "tty: sysfs_gpio_set(%s,%s)\n",filename,value);
}
/* Set RTS line to active state */
void
tty_set_rts(int fd)
{
if ( TRX_RTS_1 == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIS, &mstat);
} else if ( TRX_RTS_0 == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
}
}
/* Set RTS line to passive state */
void
tty_clr_rts(int fd)
{
if ( TRX_RTS_1 == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &mstat);
} else if ( TRX_RTS_0 == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIS, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
}
}
#endif
/*
* Delay for USEC microsecs
*/
void
tty_delay(int usec)
{
struct timeval tv, ttv;
long ts;
gettimeofday(&tv, NULL);
do
{
(void)gettimeofday(&ttv, NULL);
ts = 1000000l * (ttv.tv_sec - tv.tv_sec) + (ttv.tv_usec - tv.tv_usec);
} while (ts < usec && !tty_break);
}