File: tty.c

package info (click to toggle)
jpnevulator 2.1.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 256 kB
  • ctags: 188
  • sloc: ansic: 1,411; makefile: 99
file content (77 lines) | stat: -rw-r--r-- 2,086 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
/* jpnevulator - serial reader/writer
 * Copyright (C) 2006-2014 Freddy Spierenburg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>

#include "jpnevulator.h"
#include "interface.h"
#include "tty.h"

static int ttyOpen(char *name,int length) {
	int fd;
	struct termios options;
	fd=open(name,O_RDWR);
	if(fd!=-1) {
		/* Make sure we send to a raw serial interface! */
		fcntl(fd,F_SETFL,0);
		tcgetattr(fd,&options);
		cfmakeraw(&options);
		tcsetattr(fd,TCSANOW,&options);
	}
	return(fd);
}

static int ttyControlGet(int fd,char *name) {
	int control;
	control=0;
	if(ioctl(fd,TIOCMGET,&control)) {
		char error[256];
		sprintf(error,"%s: Can't get the modem control bits of interface %s",PROGRAM_NAME,name);
		perror(error);
	}
	return control;
}

static void ttyControlWrite(FILE *output,int control) {
	fprintf(
		output,
		"le=%d, dtr=%d, rts=%d, st=%d, sr=%d, cts=%d, cd=%d, ri=%d, dsr=%d\n",
		control&TIOCM_LE?1:0,
		control&TIOCM_DTR?1:0,
		control&TIOCM_RTS?1:0,
		control&TIOCM_ST?1:0,
		control&TIOCM_SR?1:0,
		control&TIOCM_CTS?1:0,
		control&TIOCM_CD?1:0,
		control&TIOCM_RI?1:0,
		control&TIOCM_DSR?1:0
	);
}

static void ttyClose(int fd) {
	close(fd);
}

enum interfaceRtrn ttyAdd(char *name) {
	return(interfaceAdd(name,ttyOpen,ttyControlGet,ttyControlWrite,ttyClose));
}