tty.c 4.84 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
14
15
16
17
18
#ifdef DEBUG
#define debug(s) {puts(s); fflush(stdout);}
#else
#define debug(s) ;
#endif

Mart Lubbers's avatar
Mart Lubbers committed
19
20
21
22
#ifdef __APPLE__
int CMSPAR = 0;
#endif

23
#define INITIAL_BUFFERSIZE 2
24

Mart Lubbers's avatar
Mart Lubbers committed
25
26
27
28
29
30
31
32
#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
33
34
35
36
37
38
39
40
41
struct termioslist
{
	int fd;
	struct termios to;
	struct termioslist *next;
};

struct termioslist *head = NULL;

42
43
44
45
46
47
48
49
static void *my_malloc(size_t s)
{
	void *r = malloc(s);
	if(r == NULL)
		die("malloc");
	return r;
}

Mart Lubbers's avatar
Mart Lubbers committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
65
			if(beforeit == NULL)
Mart Lubbers's avatar
Mart Lubbers committed
66
				head = it->next;
Mart Lubbers's avatar
Mart Lubbers committed
67
			else
Mart Lubbers's avatar
Mart Lubbers committed
68
69
70
71
72
73
74
75
76
				beforeit->next = it->next;
			free(it);
			break;
		}
		beforeit = it;
		it = it->next;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
77
78
79
static char *cleanStringToCString(CleanString s)
{
	unsigned long len = CleanStringLength(s);
80
	char *cs = (char *)my_malloc(len+1);
Mart Lubbers's avatar
Mart Lubbers committed
81
82
83
	memcpy(cs, CleanStringCharacters(s), len);
	cs[len] = '\0';
	return cs;
Mart Lubbers's avatar
update    
Mart Lubbers committed
84
}
Mart Lubbers's avatar
Mart Lubbers committed
85

Mart Lubbers's avatar
Mart Lubbers committed
86
87
static void addTermios(int fd, struct termios *t)
{
88
	struct termioslist *new = my_malloc(sizeof(struct termioslist));
Mart Lubbers's avatar
Mart Lubbers committed
89
90
91
92
93
94
95
96
97
98
99
100
101
	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
102
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
103
	int stopbits, int xonoff, int *status, int *fd)
Mart Lubbers's avatar
Mart Lubbers committed
104
{
105
	debug("ttyopen");
Mart Lubbers's avatar
Mart Lubbers committed
106
107
	struct termios tio;
	char *cs_fn = cleanStringToCString(fn);
Mart Lubbers's avatar
Mart Lubbers committed
108
	*fd = open(cs_fn, O_RDWR | O_NOCTTY | O_NONBLOCK);
Mart Lubbers's avatar
Mart Lubbers committed
109
	*status = 0;
Mart Lubbers's avatar
test    
Mart Lubbers committed
110
	fcntl(*fd, F_SETFL, 0);
111
	if(*fd < 0){
Mart Lubbers's avatar
Mart Lubbers committed
112
113
114
		error = strerror(errno);
	} else {
		//Get
115
116
		tcgetattr(*fd, &tio);
		addTermios(*fd, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
117
118
119
		//Baudrate
		cfsetispeed(&tio, baudrates[baudrate]);
		//Bytesize
120
		tio.c_cflag &= ~CSIZE;
Mart Lubbers's avatar
Mart Lubbers committed
121
122
		tio.c_cflag |= bytesizes[bytesize];
		//Parity
123
		if(parity == 0) {
Mart Lubbers's avatar
Mart Lubbers committed
124
			tio.c_cflag &= ~PARENB | ~INPCK;
125
126
127
		} else if(parity == 1) {
			tio.c_cflag |= PARODD | PARENB;
		} else if(parity == 2) {
Mart Lubbers's avatar
Mart Lubbers committed
128
			tio.c_cflag |= PARENB;
129
130
			tio.c_cflag &= ~PARODD;
		} else if(parity == 3) {
Mart Lubbers's avatar
Mart Lubbers committed
131
			tio.c_cflag |= PARENB | CMSPAR;
132
133
134
135
			tio.c_cflag &= ~PARODD;
		} else if( parity == 4) {
			tio.c_cflag |= PARENB | CMSPAR | PARODD;
		}
Mart Lubbers's avatar
Mart Lubbers committed
136
		//Stopbits
Mart Lubbers's avatar
Mart Lubbers committed
137
138
139
140
		if(stopbits != 0)
			tio.c_cflag |= CSTOPB;
		else
			tio.c_cflag &= ~CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
141
		//Xonoff
Mart Lubbers's avatar
Mart Lubbers committed
142
143
144
145
		if(xonoff == 1)
			tio.c_cflag |= IXON;
		else
			tio.c_cflag &= ~IXON;
Mart Lubbers's avatar
Mart Lubbers committed
146
		//Set
147
148
		tio.c_oflag = 0;
		tio.c_lflag |= ICANON;
Mart Lubbers's avatar
test    
Mart Lubbers committed
149
150
151

		tio.c_cc[VMIN]=5;
		tio.c_cc[VTIME]=0;
152
		tcsetattr(*fd, TCSANOW, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
153

154
		*status = 1;
Mart Lubbers's avatar
Mart Lubbers committed
155
156
157
		error = strerror(errno);
	}
	free(cs_fn);
158
	debug("ttyopen-done");
Mart Lubbers's avatar
Mart Lubbers committed
159
160
}

161
unsigned long *errcl = NULL;
Mart Lubbers's avatar
Mart Lubbers committed
162
163
void ttyerror(CleanString *result)
{
164
	debug("ttyerror");
Mart Lubbers's avatar
Mart Lubbers committed
165
	if(errcl != NULL)
166
167
168
169
170
171
172
		free(errcl);
	errcl = my_malloc(
		sizeof(unsigned long)*CleanStringSizeInts(strlen(error)));
	*result = (CleanString) errcl;
	memcpy(CleanStringCharacters(errcl), error, strlen(error));
	CleanStringLength(errcl) = strlen(error);
	debug("ttyerror-done");
Mart Lubbers's avatar
Mart Lubbers committed
173
174
}

175
unsigned long *readlinecl = NULL;
176
void ttyreadline(int fd, CleanString *result, int *fdo)
Mart Lubbers's avatar
Mart Lubbers committed
177
{
178
	debug("ttyreadline");
Mart Lubbers's avatar
Mart Lubbers committed
179
	size_t bufsize = INITIAL_BUFFERSIZE;
180
	char *buf = NULL;
Mart Lubbers's avatar
Mart Lubbers committed
181
	ssize_t cr, charsread = 0; 
182
183
184
	while(buf == NULL || buf[charsread-1] != '\n'){
		if((buf = realloc(buf, (bufsize*=2)+1)) == NULL)
			die("realloc");
Mart Lubbers's avatar
Mart Lubbers committed
185
186
187
		if((cr = read(fd, buf+charsread, bufsize-charsread)) < 0)
			die("read");
		charsread += cr;
Mart Lubbers's avatar
Mart Lubbers committed
188
	}
189
	buf[charsread] = '\0';
Mart Lubbers's avatar
Mart Lubbers committed
190

Mart Lubbers's avatar
Mart Lubbers committed
191
	if(readlinecl != NULL)
192
193
194
195
196
197
		free(readlinecl);
	readlinecl = my_malloc(
		sizeof(unsigned long)*CleanStringSizeInts(charsread));
	*result = (CleanString) readlinecl;
	memcpy(CleanStringCharacters(readlinecl), buf, charsread);
	CleanStringLength(readlinecl) = charsread;
Mart Lubbers's avatar
Mart Lubbers committed
198
	*fdo = fd;
199

Mart Lubbers's avatar
Mart Lubbers committed
200
	free(buf);
201
	debug("ttyreadline-done");
Mart Lubbers's avatar
Mart Lubbers committed
202
203
}

Mart Lubbers's avatar
Mart Lubbers committed
204
205
void ttyavailable(int fd, int *r, int *fdo)
{
206
	debug("ttyavailable");
Mart Lubbers's avatar
Mart Lubbers committed
207
	fd_set fds;
Mart Lubbers's avatar
test    
Mart Lubbers committed
208
209
210
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 0;
Mart Lubbers's avatar
Mart Lubbers committed
211
212
213
214

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

Mart Lubbers's avatar
test    
Mart Lubbers committed
215
	*r = select(fd+1, &fds, NULL, NULL, &tv);
Mart Lubbers's avatar
Mart Lubbers committed
216
217
218
	if(*r == -1)
		die("select");
	*fdo = fd;
219
	debug("ttyavailable-done");
Mart Lubbers's avatar
Mart Lubbers committed
220
221
}

222
int ttywrite(int fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
223
{
224
	debug("ttywrite");
225
226
	write(fd, CleanStringCharacters(s), CleanStringLength(s));
	tcdrain(fd);
227
	debug("ttywrite-done");
Mart Lubbers's avatar
Mart Lubbers committed
228
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
229
230
}

231
int ttyclose(int fd)
Mart Lubbers's avatar
Mart Lubbers committed
232
{
233
	debug("ttyclose");
Mart Lubbers's avatar
Mart Lubbers committed
234
235
236
	struct termios *to = getTermios(fd);
	tcsetattr(fd, TCSANOW, to);
	remTermios(fd);
237
238
	int ret = close(fd);
	error = strerror(errno);
239
	debug("ttyclose-done");
Mart Lubbers's avatar
Mart Lubbers committed
240
	return ret + 1;
Mart Lubbers's avatar
Mart Lubbers committed
241
}