tty.c 4.57 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 <time.h>
Mart Lubbers's avatar
Mart Lubbers committed
9

Mart Lubbers's avatar
Mart Lubbers committed
10
11
12
#include <termios.h>
#include <sys/ioctl.h>

Mart Lubbers's avatar
Mart Lubbers committed
13
#include "../Clean.h"
Mart Lubbers's avatar
Mart Lubbers committed
14

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

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

25
#define INITIAL_BUFFERSIZE 2
26

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

struct termioslist *head = NULL;

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

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

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

154
155
156
		#ifdef __APPLE__
		tio.c_cflag |= CLOCAL;
		#endif
Mart Lubbers's avatar
test    
Mart Lubbers committed
157
158
		tio.c_cc[VMIN]=5;
		tio.c_cc[VTIME]=0;
159
		tcsetattr(*fd, TCSANOW, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
160

161
		*status = 1;
Mart Lubbers's avatar
Mart Lubbers committed
162
163
164
		error = strerror(errno);
	}
	free(cs_fn);
165
	debug("ttyopen-done");
Mart Lubbers's avatar
Mart Lubbers committed
166
167
}

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

Mart Lubbers's avatar
Mart Lubbers committed
182
void ttyread(int fd, int *ch, int *fdo)
Mart Lubbers's avatar
Mart Lubbers committed
183
{
Mart Lubbers's avatar
Mart Lubbers committed
184
185
186
187
	debug("ttyread");
	unsigned int c;
	if(read(fd, &c, 1) == -1){
		die("read");
Mart Lubbers's avatar
Mart Lubbers committed
188
	}
Mart Lubbers's avatar
Mart Lubbers committed
189
	*ch = (int)c;
Mart Lubbers's avatar
Mart Lubbers committed
190
	*fdo = fd;
Mart Lubbers's avatar
Mart Lubbers committed
191
	debug("ttyread done");
Mart Lubbers's avatar
Mart Lubbers committed
192
193
}

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

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

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

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

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