Commit 2db8582b authored by Matheus Andrade's avatar Matheus Andrade Committed by Mart Lubbers

Checking for device errors on TTYAvailable

parent ce8c8653
......@@ -190,18 +190,29 @@ void ttyread(int fd, int *ch, int *fdo)
debug("ttyread done");
}
void ttyavailable(int fd, int *r, int *fdo)
void ttyavailable(int fd, int *r, int *e, int *fdo)
{
// debug("ttyavailable");
fd_set fds;
fd_set rfds, efds;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
FD_ZERO(&efds);
FD_SET(fd, &efds);
*e = 0;
*r = select(fd+1, &rfds, NULL, &efds, &tv);
if (FD_ISSET(fd, &efds)){
*e = 1;
*fdo = fd;
return;
}
*r = select(fd+1, &fds, NULL, NULL, &tv);
if(*r == -1)
die("select");
*fdo = fd;
......
......@@ -29,5 +29,12 @@ TTYerror :: !*env -> (!String, !*env)
TTYopen :: !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYread :: !*TTY -> (!Int, !*TTY)
TTYreadline :: !*TTY -> (!String, !*TTY)
TTYavailable :: !*TTY -> (!Bool, !*TTY)
/**
* Checks if the TTY device is available for reading
*
* @param The TTY Device
* @return A 3-tuple containing whether there is data available,
* whether there was an error and the TTY device
*/
TTYavailable :: !*TTY -> (!Bool, !Bool, !*TTY)
TTYwrite :: !String !*TTY -> *TTY
......@@ -90,9 +90,9 @@ TTYwrite _ _ = code {
ccall ttywrite "SI:I"
}
TTYavailable :: !*TTY -> (!Bool, !*TTY)
TTYavailable :: !*TTY -> (!Bool, !Bool, !*TTY)
TTYavailable _ = code {
ccall ttyavailable "I:VII"
ccall ttyavailable "I:VIII"
}
TTYerror :: !*env -> (!String, !*env)
......
......@@ -72,9 +72,10 @@ where
, {iworld & resources=[TTYd dp tty:resources]})
(r,s,ss)
# tty = foldr TTYwrite tty $ reverse $ map enc s
# (newdata, tty) = readWhileAvailable tty
# (merr, tty) = readWhileAvailable tty
| isError merr = (exc (fromError merr), iworld)
# iworld = {iworld & resources=[TTYd dp tty:iworld.resources]}
= case dec (acc +++ toString newdata) of
= case dec (acc +++ toString (fromOk merr)) of
(Left err, newacc) = (exc "Error while parsing", iworld)
(Right msgs, newacc)
# (merr, iworld) = if (msgs =: [] && s =: [])
......@@ -104,10 +105,12 @@ where
getResource = iworldResource (\t=:(TTYd p _)->(p == opts.devicePath, t))
exc = ExceptionResult o exception
readWhileAvailable :: !*TTY -> ([Char], !*TTY)
readWhileAvailable :: !*TTY -> (MaybeError String [Char], !*TTY)
readWhileAvailable tty
# (available, tty) = TTYavailable tty
| not available = ([], tty)
# (available, error, tty) = TTYavailable tty
| error = (Error "TTY device disconnected", tty)
| not available = (Ok [], tty)
# (c, tty) = TTYread tty
# (cs, tty) = readWhileAvailable tty
= ([toChar c:cs], tty)
# (merr, tty) = readWhileAvailable tty
| isError merr = (merr, tty)
= (Ok [toChar c:fromOk merr], tty)
......@@ -17,7 +17,7 @@ Start w
# (ok, tty, w) = TTYopen {zero & devicePath="/dev/ttyUSB0"} w
| not ok = TTYerrorclose io w
#! tty = TTYwrite "echo123\n" tty
#! (av, tty) = TTYavailable tty
#! (av, e, tty) = TTYavailable tty
# io = io <<< ("Bytes available: " +++ toString av +++ "\n")
#! (l, tty) = TTYreadline tty
# io = io <<< ("Line read: " +++ l)
......
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