Commit d6152aae authored by Mart Lubbers's avatar Mart Lubbers
Browse files

use unistd instead of stdio, create test script

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