tty.c 2.95 KB
Newer Older
Mart Lubbers's avatar
Mart Lubbers committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>

#include "Clean.h"

#define INITIAL_BUFFERSIZE 1024
#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 int stopbits[3] = {StopbitsOne, StopbitsOnePointFive, StopbitsTwo};

static char *error = "";

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
Mart Lubbers committed
32
33
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
	int stopbits, int xonoff, int *status, FILE **f)
Mart Lubbers's avatar
Mart Lubbers committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
	struct termios tio;
	char *cs_fn = cleanStringToCString(fn);
	int fd = open(cs_fn, O_RDWR | O_NOCTTY);
	*status = 0;
	if(fd < 0){
		error = strerror(errno);
	} else {
		//Get
		tcgetattr(fd, &tio);
		//Baudrate
		cfsetispeed(&tio, baudrates[baudrate]);
		//Bytesize
		tio.c_cflag |= CS5 | CS6 | CS7 | CS8;
		tio.c_cflag -= CS5 | CS6 | CS7 | CS8;
		tio.c_cflag |= bytesizes[bytesize];
		//Parity
		tio.c_cflag |= PARENB | PARODD | CMSPAR;
		tio.c_cflag -= PARENB | PARODD | CMSPAR;
Mart Lubbers's avatar
Mart Lubbers committed
53
		if(parity == 1)
Mart Lubbers's avatar
Mart Lubbers committed
54
			tio.c_cflag |= PARENB | PARODD;
Mart Lubbers's avatar
Mart Lubbers committed
55
		else if(parity == 2)
Mart Lubbers's avatar
Mart Lubbers committed
56
			tio.c_cflag |= PARENB;
Mart Lubbers's avatar
Mart Lubbers committed
57
		else if(parity == 3)
Mart Lubbers's avatar
Mart Lubbers committed
58
			tio.c_cflag |= PARODD | PARENB | CMSPAR;
Mart Lubbers's avatar
Mart Lubbers committed
59
		else if(parity == 4)
Mart Lubbers's avatar
Mart Lubbers committed
60
61
62
			tio.c_cflag |= PARENB | CMSPAR;
		//Stopbits
		tio.c_cflag |= CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
63
		tio.c_cflag -= stopbits == 0 ? 0 : CSTOPB;
Mart Lubbers's avatar
Mart Lubbers committed
64
65
66
67
68
69
70
		//Xonoff
		tio.c_cflag |= IXON;
		tio.c_cflag -= xonoff == 1 ? 0 : IXON;
		//Set
		tcsetattr(fd, TCSANOW, &tio);

		*f = fdopen(fd, "r+");
Mart Lubbers's avatar
Mart Lubbers committed
71
		if(*f != NULL){
Mart Lubbers's avatar
Mart Lubbers committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
			setbuf(*f, NULL);
			*status = 1;
		}
		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);
}

void ttyreadc(FILE *fd, int *c, FILE **fdo)
{
	*c = fgetc(fd);
	*fdo = fd;
}

void ttyreadline(FILE *fd, CleanString *result, FILE **fdo)
{
	size_t bufsize = INITIAL_BUFFERSIZE;
	char *buf = (char *)malloc(bufsize+1);
	int c, i = 0;

	if(buf == NULL)
		die("malloc");

	while((c = fgetc(fd)) != EOF && c != '\n'){
		if(i >= bufsize)
			if((buf = realloc(buf, bufsize *= 2)) == NULL)
				die("realloc");
		buf[i++] = c;
	}
	buf[i] = '\0';

	CleanStringVariable(cleanOutput, strlen(buf));
	*result = (CleanString) cleanOutput;
	memcpy(CleanStringCharacters(cleanOutput), buf, strlen(buf));
	CleanStringLength(cleanOutput) = strlen(buf);
	*fdo = fd;
	free(buf);
}

Mart Lubbers's avatar
Mart Lubbers committed
119
FILE *ttywrite(FILE *fd, CleanString s)
Mart Lubbers's avatar
Mart Lubbers committed
120
{
Mart Lubbers's avatar
Mart Lubbers committed
121
122
	fwrite(CleanStringCharacters(s), 1, CleanStringLength(s), fd);
	return fd;
Mart Lubbers's avatar
Mart Lubbers committed
123
124
}

Mart Lubbers's avatar
Mart Lubbers committed
125
126
127
128
int ttyclose(FILE *fd)
{
	return fclose(fd) == 0 ? 1 : 0;
}