tty.c 4.53 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
		tio.c_oflag = 0;
Mart Lubbers's avatar
Mart Lubbers committed
149
150
		tio.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
//		tio.c_lflag |= ICANON;
Mart Lubbers's avatar
test    
Mart Lubbers committed
151
152
153

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

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

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

Mart Lubbers's avatar
Mart Lubbers committed
177
void ttyread(int fd, int *ch, int *fdo)
Mart Lubbers's avatar
Mart Lubbers committed
178
{
Mart Lubbers's avatar
Mart Lubbers committed
179
180
181
182
	debug("ttyread");
	unsigned int c;
	if(read(fd, &c, 1) == -1){
		die("read");
Mart Lubbers's avatar
Mart Lubbers committed
183
	}
Mart Lubbers's avatar
Mart Lubbers committed
184
185
	printf("read: %o\n", c);
	*ch = (int)c;
Mart Lubbers's avatar
Mart Lubbers committed
186
	*fdo = fd;
Mart Lubbers's avatar
Mart Lubbers committed
187
	debug("ttyread done");
Mart Lubbers's avatar
Mart Lubbers committed
188
189
}

Mart Lubbers's avatar
Mart Lubbers committed
190
191
void ttyavailable(int fd, int *r, int *fdo)
{
Mart Lubbers's avatar
Mart Lubbers committed
192
//	debug("ttyavailable");
Mart Lubbers's avatar
Mart Lubbers committed
193
	fd_set fds;
Mart Lubbers's avatar
test    
Mart Lubbers committed
194
195
196
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 0;
Mart Lubbers's avatar
Mart Lubbers committed
197
198
199
200

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

Mart Lubbers's avatar
test    
Mart Lubbers committed
201
	*r = select(fd+1, &fds, NULL, NULL, &tv);
Mart Lubbers's avatar
Mart Lubbers committed
202
203
204
	if(*r == -1)
		die("select");
	*fdo = fd;
Mart Lubbers's avatar
Mart Lubbers committed
205
//	debug("ttyavailable-done");
Mart Lubbers's avatar
Mart Lubbers committed
206
207
}

Mart Lubbers's avatar
Mart Lubbers committed
208
int ttywrite(CleanString s, int fd)
Mart Lubbers's avatar
Mart Lubbers committed
209
{
210
	debug("ttywrite");
Mart Lubbers's avatar
Mart Lubbers committed
211
212
	int i;
	for(i = 0; i< CleanStringLength(s); i++){
Mart Lubbers's avatar
Mart Lubbers committed
213
214
215
216
		unsigned char c = ((unsigned char*)CleanStringCharacters(s))[i];
		printf("%02x(%u) ", c, c);
	}
	write(fd, (void *)CleanStringCharacters(s), CleanStringLength(s));
217
	tcdrain(fd);
218
	debug("ttywrite-done");
Mart Lubbers's avatar
Mart Lubbers committed
219
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
220
221
}

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