tty.c 4.55 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
		tio.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
Mart Lubbers's avatar
test  
Mart Lubbers committed
152

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

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

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

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

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

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

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

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

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