tty.c 4.15 KB
Newer Older
1
2
3
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
Mart Lubbers's avatar
Mart Lubbers committed
4
5
6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
7
#include <sys/ioctl.h>
Mart Lubbers's avatar
Mart Lubbers committed
8
#include <termios.h>
9
#include <time.h>
Mart Lubbers's avatar
Mart Lubbers committed
10
11
12

#include "Clean.h"

13
#define INITIAL_BUFFERSIZE 2
Mart Lubbers's avatar
Mart Lubbers committed
14
#define DEVICE_TIMEOUT {5, 0}
Mart Lubbers's avatar
Mart Lubbers committed
15
16
17
18
19
20
21
22
#define die(s) {perror(s);exit(EXIT_FAILURE);}

static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
	B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200,
	B230400};
static int bytesizes[4] = {CS5, CS6, CS7, CS8};
static char *error = "";

Mart Lubbers's avatar
Mart Lubbers committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct termioslist
{
	int fd;
	struct termios to;
	struct termioslist *next;
};

struct termioslist *head = NULL;

static struct termios *getTermios(int fd)
{
	struct termioslist *h = head;
	while(h != NULL)
		if(h->fd == fd)
			return &h->to;
	return NULL;
}

static void remTermios(int fd)
{
	struct termioslist *beforeit = NULL;
	struct termioslist *it = head;
	while(it != NULL){
		if(it->fd == fd){
Mart Lubbers's avatar
Mart Lubbers committed
47
			if(beforeit == NULL)
Mart Lubbers's avatar
Mart Lubbers committed
48
				head = it->next;
Mart Lubbers's avatar
Mart Lubbers committed
49
			else
Mart Lubbers's avatar
Mart Lubbers committed
50
51
52
53
54
55
56
57
58
				beforeit->next = it->next;
			free(it);
			break;
		}
		beforeit = it;
		it = it->next;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
59
60
61
62
63
64
65
66
67
static char *cleanStringToCString(CleanString s)
{
	unsigned long len = CleanStringLength(s);
	char *cs = (char *)malloc(len+1);
	if(cs == NULL)
		die("malloc");
	memcpy(cs, CleanStringCharacters(s), len);
	cs[len] = '\0';
	return cs;
Mart Lubbers's avatar
update    
Mart Lubbers committed
68
}
Mart Lubbers's avatar
Mart Lubbers committed
69

Mart Lubbers's avatar
Mart Lubbers committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static void addTermios(int fd, struct termios *t)
{
	struct termioslist *new = malloc(sizeof(struct termioslist));
	if(new == NULL)
		die("malloc");
	new->fd = fd;
	memcpy(&new->to, t, sizeof(struct termios));
	new->next = NULL;
	if(head == NULL){
		head = new;
	} else {
		struct termioslist *h = head;
		while(h->next != NULL)
			h = h->next;
		h->next = new;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
88
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
89
	int stopbits, int xonoff, int *status, int *fd)
Mart Lubbers's avatar
Mart Lubbers committed
90
91
92
{
	struct termios tio;
	char *cs_fn = cleanStringToCString(fn);
93
	*fd = open(cs_fn, O_RDWR | O_NOCTTY);
Mart Lubbers's avatar
Mart Lubbers committed
94
	*status = 0;
95
	if(*fd < 0){
Mart Lubbers's avatar
Mart Lubbers committed
96
97
98
		error = strerror(errno);
	} else {
		//Get
99
100
		tcgetattr(*fd, &tio);
		addTermios(*fd, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
101
102
103
		//Baudrate
		cfsetispeed(&tio, baudrates[baudrate]);
		//Bytesize
104
		tio.c_cflag &= ~CSIZE;
Mart Lubbers's avatar
Mart Lubbers committed
105
106
		tio.c_cflag |= bytesizes[bytesize];
		//Parity
107
		if(parity == 0) {
Mart Lubbers's avatar
Mart Lubbers committed
108
			tio.c_cflag &= ~PARENB | ~INPCK;
109
110
111
		} else if(parity == 1) {
			tio.c_cflag |= PARODD | PARENB;
		} else if(parity == 2) {
Mart Lubbers's avatar
Mart Lubbers committed
112
			tio.c_cflag |= PARENB;
113
114
			tio.c_cflag &= ~PARODD;
		} else if(parity == 3) {
Mart Lubbers's avatar
Mart Lubbers committed
115
			tio.c_cflag |= PARENB | CMSPAR;
116
117
118
119
			tio.c_cflag &= ~PARODD;
		} else if( parity == 4) {
			tio.c_cflag |= PARENB | CMSPAR | PARODD;
		}
Mart Lubbers's avatar
Mart Lubbers committed
120
		//Stopbits
Mart Lubbers's avatar
Mart Lubbers committed
121
122
123
124
		if(stopbits != 0)
			tio.c_cflag |= CSTOPB;
		else
			tio.c_cflag &= ~CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
125
		//Xonoff
Mart Lubbers's avatar
Mart Lubbers committed
126
127
128
129
		if(xonoff == 1)
			tio.c_cflag |= IXON;
		else
			tio.c_cflag &= ~IXON;
Mart Lubbers's avatar
Mart Lubbers committed
130
		//Set
131
132
133
		tio.c_oflag = 0;
		tio.c_lflag |= ICANON;
		tcsetattr(*fd, TCSANOW, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
134

135
		*status = 1;
Mart Lubbers's avatar
Mart Lubbers committed
136
137
138
139
140
141
142
143
144
145
146
147
148
		error = strerror(errno);
	}
	free(cs_fn);
}

void ttyerror(CleanString *result)
{
	CleanStringVariable(clean_string, strlen(error));
	*result = (CleanString) clean_string;
	memcpy(CleanStringCharacters(clean_string), error, strlen(error));
	CleanStringLength(clean_string) = strlen(error);
}

149
void ttyreadline(int fd, CleanString *result, int *fdo)
Mart Lubbers's avatar
Mart Lubbers committed
150
151
{
	size_t bufsize = INITIAL_BUFFERSIZE;
152
	char *buf = NULL;
Mart Lubbers's avatar
Mart Lubbers committed
153
	ssize_t cr, charsread = 0; 
154
155
156
	while(buf == NULL || buf[charsread-1] != '\n'){
		if((buf = realloc(buf, (bufsize*=2)+1)) == NULL)
			die("realloc");
Mart Lubbers's avatar
Mart Lubbers committed
157
158
159
		if((cr = read(fd, buf+charsread, bufsize-charsread)) < 0)
			die("read");
		charsread += cr;
Mart Lubbers's avatar
Mart Lubbers committed
160
	}
161
	buf[charsread] = '\0';
Mart Lubbers's avatar
Mart Lubbers committed
162

Mart Lubbers's avatar
Mart Lubbers committed
163
	CleanStringVariable(cleanOutput, charsread);
Mart Lubbers's avatar
Mart Lubbers committed
164
	*result = (CleanString) cleanOutput;
Mart Lubbers's avatar
Mart Lubbers committed
165
166
	memcpy(CleanStringCharacters(cleanOutput), buf, charsread);
	CleanStringLength(cleanOutput) = charsread;
Mart Lubbers's avatar
Mart Lubbers committed
167
	*fdo = fd;
168

Mart Lubbers's avatar
Mart Lubbers committed
169
170
171
	free(buf);
}

Mart Lubbers's avatar
Mart Lubbers committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
void ttyavailable(int fd, int *r, int *fdo)
{
	fd_set fds;
	struct timeval tv = DEVICE_TIMEOUT;

	FD_ZERO(&fds);
	FD_SET(fd, &fds);

	*r = select(fd+1, &fds, NULL, NULL, NULL);
	if(*r == -1)
		die("select");
	*fdo = fd;
}

186
int ttywrite(int fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
187
{
188
189
	write(fd, CleanStringCharacters(s), CleanStringLength(s));
	tcdrain(fd);
Mart Lubbers's avatar
Mart Lubbers committed
190
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
191
192
}

193
int ttyclose(int fd)
Mart Lubbers's avatar
Mart Lubbers committed
194
{
Mart Lubbers's avatar
Mart Lubbers committed
195
196
197
	struct termios *to = getTermios(fd);
	tcsetattr(fd, TCSANOW, to);
	remTermios(fd);
198
199
	int ret = close(fd);
	error = strerror(errno);
Mart Lubbers's avatar
Mart Lubbers committed
200
	return ret + 1;
Mart Lubbers's avatar
Mart Lubbers committed
201
}