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

#include "Clean.h"

14
#ifdef DEBUG
Mart Lubbers's avatar
Mart Lubbers committed
15
#define debug(s) {puts(s); fflush(stdout);}
16
#else
Mart Lubbers's avatar
Mart Lubbers committed
17
#define debug(s) ;
18
19
#endif

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

24
#define INITIAL_BUFFERSIZE 2
25

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

struct termioslist *head = NULL;

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

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

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

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

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

162
unsigned long *errcl = NULL;
Mart Lubbers's avatar
Mart Lubbers committed
163
164
void ttyerror(CleanString *result)
{
165
	debug("ttyerror");
Mart Lubbers's avatar
Mart Lubbers committed
166
	if(errcl != NULL)
167
168
169
170
171
172
173
		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
174
175
}

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

Mart Lubbers's avatar
Mart Lubbers committed
192
	if(readlinecl != NULL)
193
194
195
196
197
198
		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
199
	*fdo = fd;
200

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

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

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

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

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

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