tty.c 3.88 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
#define INITIAL_BUFFERSIZE 2
Mart Lubbers's avatar
Mart Lubbers committed
14
15
16
17
18
19
20
21
#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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
struct termioslist
{
	int fd;
	struct termios to;
	struct termioslist *next;
};

struct termioslist *head = NULL;

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){
			if(beforeit == NULL){
				head = it->next;
			} else {
				beforeit->next = it->next;
			}
			free(it);
			break;
		}
		beforeit = it;
		it = it->next;
	}
}

Mart Lubbers's avatar
Mart Lubbers committed
59
60
61
62
63
64
65
66
67
static char *cleanStringToCString(CleanString s)
{
	unsigned long len = CleanStringLength(s);
	char *cs = (char *)malloc(len+1);
	if(cs == NULL)
		die("malloc");
	memcpy(cs, CleanStringCharacters(s), len);
	cs[len] = '\0';
	return cs;
Mart Lubbers's avatar
update    
Mart Lubbers committed
68
}
Mart Lubbers's avatar
Mart Lubbers committed
69

Mart Lubbers's avatar
Mart Lubbers committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static void addTermios(int fd, struct termios *t)
{
	struct termioslist *new = malloc(sizeof(struct termioslist));
	if(new == NULL)
		die("malloc");
	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
88
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
89
	int stopbits, int xonoff, int *status, int *fd)
Mart Lubbers's avatar
Mart Lubbers committed
90
91
92
{
	struct termios tio;
	char *cs_fn = cleanStringToCString(fn);
93
	*fd = open(cs_fn, O_RDWR | O_NOCTTY);
Mart Lubbers's avatar
Mart Lubbers committed
94
	*status = 0;
95
	if(*fd < 0){
Mart Lubbers's avatar
Mart Lubbers committed
96
97
98
		error = strerror(errno);
	} else {
		//Get
99
100
		tcgetattr(*fd, &tio);
		addTermios(*fd, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
101
102
103
		//Baudrate
		cfsetispeed(&tio, baudrates[baudrate]);
		//Bytesize
104
		tio.c_cflag &= ~CSIZE;
Mart Lubbers's avatar
Mart Lubbers committed
105
106
		tio.c_cflag |= bytesizes[bytesize];
		//Parity
107
108
109
110
111
		if(parity == 0) {
			tio.c_cflag &= ~PARENB |  ~INPCK;
		} else if(parity == 1) {
			tio.c_cflag |= PARODD | PARENB;
		} else if(parity == 2) {
Mart Lubbers's avatar
Mart Lubbers committed
112
			tio.c_cflag |= PARENB;
113
114
			tio.c_cflag &= ~PARODD;
		} else if(parity == 3) {
Mart Lubbers's avatar
Mart Lubbers committed
115
			tio.c_cflag |= PARENB | CMSPAR;
116
117
118
119
			tio.c_cflag &= ~PARODD;
		} else if( parity == 4) {
			tio.c_cflag |= PARENB | CMSPAR | PARODD;
		}
Mart Lubbers's avatar
Mart Lubbers committed
120
		//Stopbits
Mart Lubbers's avatar
Mart Lubbers committed
121
122
123
124
		if(stopbits != 0)
			tio.c_cflag |= CSTOPB;
		else
			tio.c_cflag &= ~CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
125
		//Xonoff
Mart Lubbers's avatar
Mart Lubbers committed
126
127
128
129
		if(xonoff == 1)
			tio.c_cflag |= IXON;
		else
			tio.c_cflag &= ~IXON;
Mart Lubbers's avatar
Mart Lubbers committed
130
		//Set
131
132
133
		tio.c_oflag = 0;
		tio.c_lflag |= ICANON;
		tcsetattr(*fd, TCSANOW, &tio);
Mart Lubbers's avatar
Mart Lubbers committed
134

135
		*status = 1;
Mart Lubbers's avatar
Mart Lubbers committed
136
137
138
139
140
141
142
143
144
145
146
147
148
		error = strerror(errno);
	}
	free(cs_fn);
}

void ttyerror(CleanString *result)
{
	CleanStringVariable(clean_string, strlen(error));
	*result = (CleanString) clean_string;
	memcpy(CleanStringCharacters(clean_string), error, strlen(error));
	CleanStringLength(clean_string) = strlen(error);
}

149
void ttyreadline(int fd, CleanString *result, int *fdo)
Mart Lubbers's avatar
Mart Lubbers committed
150
151
{
	size_t bufsize = INITIAL_BUFFERSIZE;
152
153
154
155
156
157
	char *buf = NULL;
	ssize_t charsread = 0;
	while(buf == NULL || buf[charsread-1] != '\n'){
		if((buf = realloc(buf, (bufsize*=2)+1)) == NULL)
			die("realloc");
		charsread += read(fd, buf+charsread, bufsize-charsread);
Mart Lubbers's avatar
Mart Lubbers committed
158
	}
159
	buf[charsread] = '\0';
Mart Lubbers's avatar
Mart Lubbers committed
160

161
	CleanStringVariable(cleanOutput, charsread+1);
Mart Lubbers's avatar
Mart Lubbers committed
162
	*result = (CleanString) cleanOutput;
163
164
	memcpy(CleanStringCharacters(cleanOutput), buf, charsread+1);
	CleanStringLength(cleanOutput) = charsread+1;
Mart Lubbers's avatar
Mart Lubbers committed
165
	*fdo = fd;
166

Mart Lubbers's avatar
Mart Lubbers committed
167
168
169
	free(buf);
}

170
int ttywrite(int fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
171
{
172
173
	write(fd, CleanStringCharacters(s), CleanStringLength(s));
	tcdrain(fd);
Mart Lubbers's avatar
Mart Lubbers committed
174
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
175
176
}

177
int ttyclose(int fd)
Mart Lubbers's avatar
Mart Lubbers committed
178
{
Mart Lubbers's avatar
Mart Lubbers committed
179
180
181
	struct termios *to = getTermios(fd);
	tcsetattr(fd, TCSANOW, to);
	remTermios(fd);
182
183
184
	int ret = close(fd);
	error = strerror(errno);
	return ret+1;
Mart Lubbers's avatar
Mart Lubbers committed
185
}