tty.c 2.96 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
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
59
60
61
62
#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;
}	

void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, int stopbits, int xonoff, int *status, FILE **f)
{
	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;
		if(parity == 1){
			tio.c_cflag |= PARENB | PARODD;
		} else if(parity == 2){
			tio.c_cflag |= PARENB;
		} else if(parity == 3){
			tio.c_cflag |= PARODD | PARENB | CMSPAR;
		} else if(parity == 4){
			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
71
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
119
120
121
122
123
124
125
126
127
		//Xonoff
		tio.c_cflag |= IXON;
		tio.c_cflag -= xonoff == 1 ? 0 : IXON;
		//Set
		tcsetattr(fd, TCSANOW, &tio);

		*f = fdopen(fd, "r+");
		if(*f == NULL){
			printf("Couldn't open\n");
			fflush(stdout);
		} else {
			printf("Succesfully opened\n");
			fflush(stdout);
			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);
}

int ttyclose(FILE *fd)
{
	return fclose(fd) == 0 ? 1 : 0;
}