Commit d6152aae authored by Mart Lubbers's avatar Mart Lubbers

use unistd instead of stdio, create test script

parent 53e4d72a
TTY: TTY.prj TTY.icl TTY.dcl Clean\ System\ Files/tty.o
clm -desc -exl -tst -ns -nt TTY -o TTY
test: test.icl TTY.icl TTY.dcl Clean\ System\ Files/tty.o
clm -desc -exl -tst -ns -nt $(basename $<) -o $@
Clean\ System\ Files/tty.o: tty.c
gcc -g -c $< -o "$@"
clean:
$(RM) -r Clean\ System\ Files/* TTY sapl
$(RM) -r Clean\ System\ Files/* test sapl
definition module TTY
from StdFile import class FileSystem
from StdClass import class zero
:: *TTY
:: ByteSize = BytesizeFive | BytesizeSix | BytesizeSeven | BytesizeEight
:: Parity = ParityNone | ParityEven | ParityOdd | ParityMark | ParitySpace
:: Parity = ParityNone | ParityOdd | ParityEven | ParitySpace | ParityMark
:: BaudRate = B0 | B50 | B75 | B110 | B134 | B150 | B200 | B300 | B600 |
B1200 | B1800 | B2400 | B4800 | B9600 | B19200 | B38400 | B57600 |
B115200 | B230400
......@@ -25,8 +24,6 @@ TTYopen :: !String !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadline :: !*TTY -> (!String, !*TTY)
TTYwrite :: !*TTY !String -> *TTY
......
implementation module TTY
import StdFunc
import StdFile
import StdMisc
import StdBool
import StdString
import StdClass
import code from "tty."
......@@ -31,8 +27,8 @@ instance toInt ByteSize where
instance toInt Parity where
toInt p = case p of
ParityNone = 0; ParityEven = 1; ParityOdd = 2; ParityMark = 3;
ParitySpace = 4
ParityNone = 0; ParityOdd = 1; ParityEven = 2; ParitySpace = 3;
ParityMark = 4
TTYopen :: !String !TTYSettings !*env -> (!Bool, !*TTY, !*env)
TTYopen fn ts w = TTYopen2
......@@ -54,15 +50,10 @@ TTYclose f w = code {
ccall ttyclose "I:I:A"
}
TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadc w = code {
ccall ttyreadc "I:VII"
}
TTYreadline :: !*TTY -> (!String, !*TTY)
TTYreadline t = code {
ccall ttyreadline "I:VSI"
}
}
TTYwrite :: !*TTY !String -> *TTY
TTYwrite s e = code {
......@@ -74,13 +65,4 @@ TTYerror w = code {
ccall ttyerror ":S:A"
}
Start :: *World -> (!String, *World)
Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerror w
#! tty = TTYwrite tty "echo123\n"
#! (c, tty) = TTYreadline tty
#! (ok, w) = TTYclose tty w
| not ok = TTYerror w
#! (s, w) = TTYerror w
= (c, w)
Start =0
module test
import StdEnv
import TTY
Start :: *World -> (!String, *World)
Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerror w
#! tty = TTYwrite tty "echo123\n"
#! (c, tty) = TTYreadline tty
#! (ok, w) = TTYclose tty w
| not ok = TTYerror w
#! (s, w) = TTYerror w
= ("Read: " +++ c, w)
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
#include "Clean.h"
#define INITIAL_BUFFERSIZE 1024
#define INITIAL_BUFFERSIZE 2
#define die(s) {perror(s);exit(EXIT_FAILURE);}
static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
......@@ -83,33 +86,37 @@ static void addTermios(int fd, struct termios *t)
}
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
int stopbits, int xonoff, int *status, FILE **f)
int stopbits, int xonoff, int *status, int *fd)
{
struct termios tio;
char *cs_fn = cleanStringToCString(fn);
int fd = open(cs_fn, O_RDWR | O_NOCTTY);
*fd = open(cs_fn, O_RDWR | O_NOCTTY);
*status = 0;
if(fd < 0){
if(*fd < 0){
error = strerror(errno);
} else {
//Get
tcgetattr(fd, &tio);
addTermios(fd, &tio);
tcgetattr(*fd, &tio);
addTermios(*fd, &tio);
//Baudrate
cfsetispeed(&tio, baudrates[baudrate]);
//Bytesize
tio.c_cflag &= ~CS5 | ~CS6 | ~CS7 | ~CS8;
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= bytesizes[bytesize];
//Parity
tio.c_cflag &= ~PARENB | ~PARODD | ~CMSPAR;
if(parity == 1)
tio.c_cflag |= PARENB | PARODD;
else if(parity == 2)
if(parity == 0) {
tio.c_cflag &= ~PARENB | ~INPCK;
} else if(parity == 1) {
tio.c_cflag |= PARODD | PARENB;
} 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 &= ~PARODD;
} else if(parity == 3) {
tio.c_cflag |= PARENB | CMSPAR;
tio.c_cflag &= ~PARODD;
} else if( parity == 4) {
tio.c_cflag |= PARENB | CMSPAR | PARODD;
}
//Stopbits
if(stopbits != 0)
tio.c_cflag |= CSTOPB;
......@@ -121,13 +128,11 @@ void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
else
tio.c_cflag &= ~IXON;
//Set
tcsetattr(fd, TCSANOW, &tio);
tio.c_oflag = 0;
tio.c_lflag |= ICANON;
tcsetattr(*fd, TCSANOW, &tio);
*f = fdopen(fd, "r+");
if(*f != NULL){
setbuf(*f, NULL);
*status = 1;
}
*status = 1;
error = strerror(errno);
}
free(cs_fn);
......@@ -141,49 +146,40 @@ void ttyerror(CleanString *result)
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)
void ttyreadline(int fd, CleanString *result, int *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;
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);
}
buf[i] = '\0';
buf[charsread] = '\0';
CleanStringVariable(cleanOutput, strlen(buf));
CleanStringVariable(cleanOutput, charsread+1);
*result = (CleanString) cleanOutput;
memcpy(CleanStringCharacters(cleanOutput), buf, strlen(buf));
CleanStringLength(cleanOutput) = strlen(buf);
memcpy(CleanStringCharacters(cleanOutput), buf, charsread+1);
CleanStringLength(cleanOutput) = charsread+1;
*fdo = fd;
free(buf);
}
FILE *ttywrite(FILE *fd, CleanString s)
int ttywrite(int fd, CleanString s)
{
fwrite(CleanStringCharacters(s), 1, CleanStringLength(s), fd);
write(fd, CleanStringCharacters(s), CleanStringLength(s));
tcdrain(fd);
return fd;
}
int ttyclose(FILE *fdes)
int ttyclose(int fd)
{
int fd = fileno(fdes);
struct termios *to = getTermios(fd);
fflush(fdes);
tcsetattr(fd, TCSANOW, to);
remTermios(fd);
return fclose(fdes) == 0 ? 1 : 0;
int ret = close(fd);
error = strerror(errno);
return ret+1;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment