tty.c 4.81 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

19
#define INITIAL_BUFFERSIZE 2
20

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

struct termioslist *head = NULL;

38
39
40
41
42
43
44
45
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
61
			if(beforeit == NULL)
Mart Lubbers's avatar
Mart Lubbers committed
62
				head = it->next;
Mart Lubbers's avatar
Mart Lubbers committed
63
			else
Mart Lubbers's avatar
Mart Lubbers committed
64
65
66
67
68
69
70
71
72
				beforeit->next = it->next;
			free(it);
			break;
		}
		beforeit = it;
		it = it->next;
	}
}

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

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

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

150
		*status = 1;
Mart Lubbers's avatar
Mart Lubbers committed
151
152
153
		error = strerror(errno);
	}
	free(cs_fn);
154
	debug("ttyopen-done");
Mart Lubbers's avatar
Mart Lubbers committed
155
156
}

157
unsigned long *errcl = NULL;
Mart Lubbers's avatar
Mart Lubbers committed
158
159
void ttyerror(CleanString *result)
{
160
161
162
163
164
165
166
167
168
	debug("ttyerror");
	if(errcl == NULL)
		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
169
170
}

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

187
188
189
190
191
192
193
	if(readlinecl == NULL)
		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
194
	*fdo = fd;
195

Mart Lubbers's avatar
Mart Lubbers committed
196
	free(buf);
197
	debug("ttyreadline-done");
Mart Lubbers's avatar
Mart Lubbers committed
198
199
}

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

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

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

218
int ttywrite(int fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
219
{
220
	debug("ttywrite");
221
222
	write(fd, CleanStringCharacters(s), CleanStringLength(s));
	tcdrain(fd);
223
	debug("ttywrite-done");
Mart Lubbers's avatar
Mart Lubbers committed
224
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
225
226
}

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